bpf: allow self-xor operation
[dpdk.git] / app / test / test_event_crypto_adapter.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation.
3  * All rights reserved.
4  */
5
6 #include <string.h>
7 #include <rte_common.h>
8 #include <rte_mempool.h>
9 #include <rte_mbuf.h>
10 #include <rte_cryptodev.h>
11 #include <rte_eventdev.h>
12 #include <rte_bus_vdev.h>
13 #include <rte_service.h>
14 #include <rte_event_crypto_adapter.h>
15 #include "test.h"
16
17 #define PKT_TRACE                  0
18 #define NUM                        1
19 #define DEFAULT_NUM_XFORMS        (2)
20 #define NUM_MBUFS                 (8191)
21 #define MBUF_CACHE_SIZE           (256)
22 #define MAXIMUM_IV_LENGTH         (16)
23 #define DEFAULT_NUM_OPS_INFLIGHT  (128)
24 #define MAX_NB_SESSIONS            4
25 #define TEST_APP_PORT_ID           0
26 #define TEST_APP_EV_QUEUE_ID       0
27 #define TEST_APP_EV_PRIORITY       0
28 #define TEST_APP_EV_FLOWID         0xAABB
29 #define TEST_CRYPTO_EV_QUEUE_ID    1
30 #define TEST_ADAPTER_ID            0
31 #define TEST_CDEV_ID               0
32 #define TEST_CDEV_QP_ID            0
33 #define PACKET_LENGTH              64
34 #define NB_TEST_PORTS              1
35 #define NB_TEST_QUEUES             2
36 #define NUM_CORES                  1
37 #define CRYPTODEV_NAME_NULL_PMD    crypto_null
38
39 #define MBUF_SIZE              (sizeof(struct rte_mbuf) + \
40                                 RTE_PKTMBUF_HEADROOM + PACKET_LENGTH)
41 #define IV_OFFSET              (sizeof(struct rte_crypto_op) + \
42                                 sizeof(struct rte_crypto_sym_op) + \
43                                 DEFAULT_NUM_XFORMS * \
44                                 sizeof(struct rte_crypto_sym_xform))
45
46 /* Handle log statements in same manner as test macros */
47 #define LOG_DBG(...)    RTE_LOG(DEBUG, EAL, __VA_ARGS__)
48
49 static const uint8_t text_64B[] = {
50         0x05, 0x15, 0x77, 0x32, 0xc9, 0x66, 0x91, 0x50,
51         0x93, 0x9f, 0xbb, 0x4e, 0x2e, 0x5a, 0x02, 0xd0,
52         0x2d, 0x9d, 0x31, 0x5d, 0xc8, 0x9e, 0x86, 0x36,
53         0x54, 0x5c, 0x50, 0xe8, 0x75, 0x54, 0x74, 0x5e,
54         0xd5, 0xa2, 0x84, 0x21, 0x2d, 0xc5, 0xf8, 0x1c,
55         0x55, 0x1a, 0xba, 0x91, 0xce, 0xb5, 0xa3, 0x1e,
56         0x31, 0xbf, 0xe9, 0xa1, 0x97, 0x5c, 0x2b, 0xd6,
57         0x57, 0xa5, 0x9f, 0xab, 0xbd, 0xb0, 0x9b, 0x9c
58 };
59
60 struct event_crypto_adapter_test_params {
61         struct rte_mempool *mbuf_pool;
62         struct rte_mempool *op_mpool;
63         struct rte_mempool *session_mpool;
64         struct rte_mempool *session_priv_mpool;
65         struct rte_cryptodev_config *config;
66         uint8_t crypto_event_port_id;
67         uint8_t internal_port_op_fwd;
68 };
69
70 struct rte_event response_info = {
71         .queue_id = TEST_APP_EV_QUEUE_ID,
72         .sched_type = RTE_SCHED_TYPE_ATOMIC,
73         .flow_id = TEST_APP_EV_FLOWID,
74         .priority = TEST_APP_EV_PRIORITY
75 };
76
77 struct rte_event_crypto_request request_info = {
78         .cdev_id = TEST_CDEV_ID,
79         .queue_pair_id = TEST_CDEV_QP_ID
80 };
81
82 static struct event_crypto_adapter_test_params params;
83 static uint8_t crypto_adapter_setup_done;
84 static uint32_t slcore_id;
85 static int evdev;
86
87 static struct rte_mbuf *
88 alloc_fill_mbuf(struct rte_mempool *mpool, const uint8_t *data,
89                 size_t len, uint8_t blocksize)
90 {
91         struct rte_mbuf *m = rte_pktmbuf_alloc(mpool);
92         size_t t_len = len - (blocksize ? (len % blocksize) : 0);
93
94         if (m) {
95                 char *dst = rte_pktmbuf_append(m, t_len);
96
97                 if (!dst) {
98                         rte_pktmbuf_free(m);
99                         return NULL;
100                 }
101
102                 rte_memcpy(dst, (const void *)data, t_len);
103         }
104         return m;
105 }
106
107 static int
108 send_recv_ev(struct rte_event *ev)
109 {
110         struct rte_crypto_op *op;
111         struct rte_event recv_ev;
112         int ret;
113
114         if (params.internal_port_op_fwd)
115                 ret = rte_event_crypto_adapter_enqueue(evdev, TEST_APP_PORT_ID,
116                                                        ev, NUM);
117         else
118                 ret = rte_event_enqueue_burst(evdev, TEST_APP_PORT_ID, ev, NUM);
119         TEST_ASSERT_EQUAL(ret, NUM, "Failed to send event to crypto adapter\n");
120
121         while (rte_event_dequeue_burst(evdev,
122                         TEST_APP_PORT_ID, &recv_ev, NUM, 0) == 0)
123                 rte_pause();
124
125         op = recv_ev.event_ptr;
126 #if PKT_TRACE
127         struct rte_mbuf *m = op->sym->m_src;
128         rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
129 #endif
130         rte_pktmbuf_free(op->sym->m_src);
131         rte_crypto_op_free(op);
132
133         return TEST_SUCCESS;
134 }
135
136 static int
137 test_crypto_adapter_stats(void)
138 {
139         struct rte_event_crypto_adapter_stats stats;
140
141         rte_event_crypto_adapter_stats_get(TEST_ADAPTER_ID, &stats);
142         printf(" +------------------------------------------------------+\n");
143         printf(" + Crypto adapter stats for instance %u:\n", TEST_ADAPTER_ID);
144         printf(" + Event port poll count          %" PRIx64 "\n",
145                 stats.event_poll_count);
146         printf(" + Event dequeue count            %" PRIx64 "\n",
147                 stats.event_deq_count);
148         printf(" + Cryptodev enqueue count        %" PRIx64 "\n",
149                 stats.crypto_enq_count);
150         printf(" + Cryptodev enqueue failed count %" PRIx64 "\n",
151                 stats.crypto_enq_fail);
152         printf(" + Cryptodev dequeue count        %" PRIx64 "\n",
153                 stats.crypto_deq_count);
154         printf(" + Event enqueue count            %" PRIx64 "\n",
155                 stats.event_enq_count);
156         printf(" + Event enqueue retry count      %" PRIx64 "\n",
157                 stats.event_enq_retry_count);
158         printf(" + Event enqueue fail count       %" PRIx64 "\n",
159                 stats.event_enq_fail_count);
160         printf(" +------------------------------------------------------+\n");
161
162         rte_event_crypto_adapter_stats_reset(TEST_ADAPTER_ID);
163         return TEST_SUCCESS;
164 }
165
166 static int
167 test_op_forward_mode(uint8_t session_less)
168 {
169         struct rte_crypto_sym_xform cipher_xform;
170         struct rte_cryptodev_sym_session *sess;
171         union rte_event_crypto_metadata m_data;
172         struct rte_crypto_sym_op *sym_op;
173         struct rte_crypto_op *op;
174         struct rte_mbuf *m;
175         struct rte_event ev;
176         uint32_t cap;
177         int ret;
178
179         memset(&m_data, 0, sizeof(m_data));
180
181         m = alloc_fill_mbuf(params.mbuf_pool, text_64B, PACKET_LENGTH, 0);
182         TEST_ASSERT_NOT_NULL(m, "Failed to allocate mbuf!\n");
183 #if PKT_TRACE
184         rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
185 #endif
186         /* Setup Cipher Parameters */
187         cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
188         cipher_xform.next = NULL;
189         cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_NULL;
190         cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
191
192         op = rte_crypto_op_alloc(params.op_mpool,
193                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
194         TEST_ASSERT_NOT_NULL(op,
195                 "Failed to allocate symmetric crypto operation struct\n");
196
197         sym_op = op->sym;
198
199         if (!session_less) {
200                 sess = rte_cryptodev_sym_session_create(
201                                 params.session_mpool);
202                 TEST_ASSERT_NOT_NULL(sess, "Session creation failed\n");
203
204                 /* Create Crypto session*/
205                 ret = rte_cryptodev_sym_session_init(TEST_CDEV_ID, sess,
206                                 &cipher_xform, params.session_priv_mpool);
207                 TEST_ASSERT_SUCCESS(ret, "Failed to init session\n");
208
209                 ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID,
210                                                         &cap);
211                 TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
212
213                 if (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_SESSION_PRIVATE_DATA) {
214                         /* Fill in private user data information */
215                         m_data.request_info.cdev_id = request_info.cdev_id;
216                         m_data.request_info.queue_pair_id =
217                                 request_info.queue_pair_id;
218                         m_data.response_info.event = response_info.event;
219                         rte_cryptodev_sym_session_set_user_data(sess,
220                                                 &m_data, sizeof(m_data));
221                 }
222
223                 rte_crypto_op_attach_sym_session(op, sess);
224         } else {
225                 struct rte_crypto_sym_xform *first_xform;
226
227                 rte_crypto_op_sym_xforms_alloc(op, NUM);
228                 op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
229                 first_xform = &cipher_xform;
230                 sym_op->xform = first_xform;
231                 uint32_t len = IV_OFFSET + MAXIMUM_IV_LENGTH;
232                 op->private_data_offset = len;
233                 /* Fill in private data information */
234                 m_data.request_info.cdev_id = request_info.cdev_id;
235                 m_data.request_info.queue_pair_id = request_info.queue_pair_id;
236                 m_data.response_info.event = response_info.event;
237                 rte_memcpy((uint8_t *)op + len, &m_data, sizeof(m_data));
238         }
239
240         sym_op->m_src = m;
241         sym_op->cipher.data.offset = 0;
242         sym_op->cipher.data.length = PACKET_LENGTH;
243
244         /* Fill in event info and update event_ptr with rte_crypto_op */
245         memset(&ev, 0, sizeof(ev));
246         ev.queue_id = TEST_CRYPTO_EV_QUEUE_ID;
247         ev.sched_type = RTE_SCHED_TYPE_ATOMIC;
248         ev.flow_id = 0xAABB;
249         ev.event_ptr = op;
250
251         ret = send_recv_ev(&ev);
252         TEST_ASSERT_SUCCESS(ret, "Failed to send/receive event to "
253                                 "crypto adapter\n");
254
255         test_crypto_adapter_stats();
256
257         return TEST_SUCCESS;
258 }
259
260 static int
261 map_adapter_service_core(void)
262 {
263         uint32_t adapter_service_id;
264         int ret;
265
266         if (rte_event_crypto_adapter_service_id_get(TEST_ADAPTER_ID,
267                                                 &adapter_service_id) == 0) {
268                 uint32_t core_list[NUM_CORES];
269
270                 ret = rte_service_lcore_list(core_list, NUM_CORES);
271                 TEST_ASSERT(ret >= 0, "Failed to get service core list!");
272
273                 if (core_list[0] != slcore_id) {
274                         TEST_ASSERT_SUCCESS(rte_service_lcore_add(slcore_id),
275                                                 "Failed to add service core");
276                         TEST_ASSERT_SUCCESS(rte_service_lcore_start(slcore_id),
277                                                 "Failed to start service core");
278                 }
279
280                 TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(
281                                         adapter_service_id, slcore_id, 1),
282                                         "Failed to map adapter service");
283         }
284
285         return TEST_SUCCESS;
286 }
287
288 static int
289 test_sessionless_with_op_forward_mode(void)
290 {
291         uint32_t cap;
292         int ret;
293
294         ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID, &cap);
295         TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
296
297         if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) &&
298             !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW))
299                 map_adapter_service_core();
300         else {
301                 if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD))
302                         return TEST_SKIPPED;
303         }
304
305         TEST_ASSERT_SUCCESS(rte_event_crypto_adapter_start(TEST_ADAPTER_ID),
306                                 "Failed to start event crypto adapter");
307
308         ret = test_op_forward_mode(1);
309         TEST_ASSERT_SUCCESS(ret, "Sessionless - FORWARD mode test failed\n");
310         return TEST_SUCCESS;
311 }
312
313 static int
314 test_session_with_op_forward_mode(void)
315 {
316         uint32_t cap;
317         int ret;
318
319         ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID, &cap);
320         TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
321
322         if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) &&
323             !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW))
324                 map_adapter_service_core();
325         else {
326                 if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD))
327                         return TEST_SKIPPED;
328         }
329
330         TEST_ASSERT_SUCCESS(rte_event_crypto_adapter_start(TEST_ADAPTER_ID
331                                 ), "Failed to start event crypto adapter");
332
333         ret = test_op_forward_mode(0);
334         TEST_ASSERT_SUCCESS(ret, "Session based - FORWARD mode test failed\n");
335         return TEST_SUCCESS;
336 }
337
338 static int
339 send_op_recv_ev(struct rte_crypto_op *op)
340 {
341         struct rte_crypto_op *recv_op;
342         struct rte_event ev;
343         int ret;
344
345         ret = rte_cryptodev_enqueue_burst(TEST_CDEV_ID, TEST_CDEV_QP_ID,
346                                           &op, NUM);
347         TEST_ASSERT_EQUAL(ret, NUM, "Failed to enqueue to cryptodev\n");
348         memset(&ev, 0, sizeof(ev));
349
350         while (rte_event_dequeue_burst(evdev,
351                 TEST_APP_PORT_ID, &ev, NUM, 0) == 0)
352                 rte_pause();
353
354         recv_op = ev.event_ptr;
355 #if PKT_TRACE
356         struct rte_mbuf *m = recv_op->sym->m_src;
357         rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
358 #endif
359         rte_pktmbuf_free(recv_op->sym->m_src);
360         rte_crypto_op_free(recv_op);
361
362         return TEST_SUCCESS;
363 }
364
365 static int
366 test_op_new_mode(uint8_t session_less)
367 {
368         struct rte_crypto_sym_xform cipher_xform;
369         struct rte_cryptodev_sym_session *sess;
370         union rte_event_crypto_metadata m_data;
371         struct rte_crypto_sym_op *sym_op;
372         struct rte_crypto_op *op;
373         struct rte_mbuf *m;
374         uint32_t cap;
375         int ret;
376
377         memset(&m_data, 0, sizeof(m_data));
378
379         m = alloc_fill_mbuf(params.mbuf_pool, text_64B, PACKET_LENGTH, 0);
380         TEST_ASSERT_NOT_NULL(m, "Failed to allocate mbuf!\n");
381 #if PKT_TRACE
382         rte_pktmbuf_dump(stdout, m, rte_pktmbuf_pkt_len(m));
383 #endif
384         /* Setup Cipher Parameters */
385         cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
386         cipher_xform.next = NULL;
387         cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_NULL;
388         cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
389
390         op = rte_crypto_op_alloc(params.op_mpool,
391                         RTE_CRYPTO_OP_TYPE_SYMMETRIC);
392         TEST_ASSERT_NOT_NULL(op, "Failed to allocate crypto_op!\n");
393
394         sym_op = op->sym;
395
396         if (!session_less) {
397                 sess = rte_cryptodev_sym_session_create(
398                                 params.session_mpool);
399                 TEST_ASSERT_NOT_NULL(sess, "Session creation failed\n");
400
401                 ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID,
402                                                         &cap);
403                 TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
404
405                 if (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_SESSION_PRIVATE_DATA) {
406                         /* Fill in private user data information */
407                         m_data.response_info.event = response_info.event;
408                         rte_cryptodev_sym_session_set_user_data(sess,
409                                                 &m_data, sizeof(m_data));
410                 }
411                 ret = rte_cryptodev_sym_session_init(TEST_CDEV_ID, sess,
412                                 &cipher_xform, params.session_priv_mpool);
413                 TEST_ASSERT_SUCCESS(ret, "Failed to init session\n");
414
415                 rte_crypto_op_attach_sym_session(op, sess);
416         } else {
417                 struct rte_crypto_sym_xform *first_xform;
418
419                 rte_crypto_op_sym_xforms_alloc(op, NUM);
420                 op->sess_type = RTE_CRYPTO_OP_SESSIONLESS;
421                 first_xform = &cipher_xform;
422                 sym_op->xform = first_xform;
423                 uint32_t len = IV_OFFSET + MAXIMUM_IV_LENGTH;
424                 op->private_data_offset = len;
425                 /* Fill in private data information */
426                 m_data.response_info.event = response_info.event;
427                 rte_memcpy((uint8_t *)op + len, &m_data, sizeof(m_data));
428         }
429
430         sym_op->m_src = m;
431         sym_op->cipher.data.offset = 0;
432         sym_op->cipher.data.length = PACKET_LENGTH;
433
434         ret = send_op_recv_ev(op);
435         TEST_ASSERT_SUCCESS(ret, "Failed to enqueue op to cryptodev\n");
436
437         test_crypto_adapter_stats();
438
439         return TEST_SUCCESS;
440 }
441
442 static int
443 test_sessionless_with_op_new_mode(void)
444 {
445         uint32_t cap;
446         int ret;
447
448         ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID, &cap);
449         TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
450
451         if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) &&
452             !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW))
453                 map_adapter_service_core();
454         else {
455                 if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW))
456                         return TEST_SKIPPED;
457         }
458
459         /* start the event crypto adapter */
460         TEST_ASSERT_SUCCESS(rte_event_crypto_adapter_start(TEST_ADAPTER_ID),
461                                 "Failed to start event crypto adapter");
462
463         ret = test_op_new_mode(1);
464         TEST_ASSERT_SUCCESS(ret, "Sessionless - NEW mode test failed\n");
465         return TEST_SUCCESS;
466 }
467
468 static int
469 test_session_with_op_new_mode(void)
470 {
471         uint32_t cap;
472         int ret;
473
474         ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID, &cap);
475         TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
476
477         if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) &&
478             !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW))
479                 map_adapter_service_core();
480         else {
481                 if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW))
482                         return TEST_SKIPPED;
483         }
484
485         TEST_ASSERT_SUCCESS(rte_event_crypto_adapter_start(TEST_ADAPTER_ID),
486                                 "Failed to start event crypto adapter");
487
488         ret = test_op_new_mode(0);
489         TEST_ASSERT_SUCCESS(ret, "Session based - NEW mode test failed\n");
490         return TEST_SUCCESS;
491 }
492
493 static int
494 configure_cryptodev(void)
495 {
496         struct rte_cryptodev_qp_conf qp_conf;
497         struct rte_cryptodev_config conf;
498         struct rte_cryptodev_info info;
499         unsigned int session_size;
500         uint8_t nb_devs;
501         int ret;
502
503         params.mbuf_pool = rte_pktmbuf_pool_create(
504                         "CRYPTO_ADAPTER_MBUFPOOL",
505                         NUM_MBUFS, MBUF_CACHE_SIZE, 0, MBUF_SIZE,
506                         rte_socket_id());
507         if (params.mbuf_pool == NULL) {
508                 RTE_LOG(ERR, USER1, "Can't create CRYPTO_MBUFPOOL\n");
509                 return TEST_FAILED;
510         }
511
512         params.op_mpool = rte_crypto_op_pool_create(
513                         "EVENT_CRYPTO_SYM_OP_POOL",
514                         RTE_CRYPTO_OP_TYPE_SYMMETRIC,
515                         NUM_MBUFS, MBUF_CACHE_SIZE,
516                         DEFAULT_NUM_XFORMS *
517                         sizeof(struct rte_crypto_sym_xform) +
518                         MAXIMUM_IV_LENGTH +
519                         sizeof(union rte_event_crypto_metadata),
520                         rte_socket_id());
521         if (params.op_mpool == NULL) {
522                 RTE_LOG(ERR, USER1, "Can't create CRYPTO_OP_POOL\n");
523                 return TEST_FAILED;
524         }
525
526         /* Create a NULL crypto device */
527         nb_devs = rte_cryptodev_device_count_by_driver(
528                         rte_cryptodev_driver_id_get(
529                         RTE_STR(CRYPTODEV_NAME_NULL_PMD)));
530         if (!nb_devs) {
531                 ret = rte_vdev_init(
532                         RTE_STR(CRYPTODEV_NAME_NULL_PMD), NULL);
533
534                 TEST_ASSERT(ret == 0, "Failed to create pmd:%s instance\n",
535                             RTE_STR(CRYPTODEV_NAME_NULL_PMD));
536         }
537
538         nb_devs = rte_cryptodev_count();
539         if (!nb_devs) {
540                 RTE_LOG(ERR, USER1, "No crypto devices found!\n");
541                 return TEST_FAILED;
542         }
543
544         /*
545          * Create mempool with maximum number of sessions * 2,
546          * to include the session headers & private data
547          */
548         session_size = rte_cryptodev_sym_get_private_session_size(TEST_CDEV_ID);
549         session_size += sizeof(union rte_event_crypto_metadata);
550
551         params.session_mpool = rte_cryptodev_sym_session_pool_create(
552                         "CRYPTO_ADAPTER_SESSION_MP",
553                         MAX_NB_SESSIONS, 0, 0,
554                         sizeof(union rte_event_crypto_metadata),
555                         SOCKET_ID_ANY);
556         TEST_ASSERT_NOT_NULL(params.session_mpool,
557                         "session mempool allocation failed\n");
558
559         params.session_priv_mpool = rte_mempool_create(
560                                 "CRYPTO_AD_SESS_MP_PRIV",
561                                 MAX_NB_SESSIONS,
562                                 session_size,
563                                 0, 0, NULL, NULL, NULL,
564                                 NULL, SOCKET_ID_ANY,
565                                 0);
566         TEST_ASSERT_NOT_NULL(params.session_priv_mpool,
567                         "session mempool allocation failed\n");
568
569         rte_cryptodev_info_get(TEST_CDEV_ID, &info);
570         conf.nb_queue_pairs = info.max_nb_queue_pairs;
571         conf.socket_id = SOCKET_ID_ANY;
572         conf.ff_disable = RTE_CRYPTODEV_FF_SECURITY;
573
574         TEST_ASSERT_SUCCESS(rte_cryptodev_configure(TEST_CDEV_ID, &conf),
575                         "Failed to configure cryptodev %u with %u qps\n",
576                         TEST_CDEV_ID, conf.nb_queue_pairs);
577
578         qp_conf.nb_descriptors = DEFAULT_NUM_OPS_INFLIGHT;
579         qp_conf.mp_session = params.session_mpool;
580         qp_conf.mp_session_private = params.session_priv_mpool;
581
582         TEST_ASSERT_SUCCESS(rte_cryptodev_queue_pair_setup(
583                         TEST_CDEV_ID, TEST_CDEV_QP_ID, &qp_conf,
584                         rte_cryptodev_socket_id(TEST_CDEV_ID)),
585                         "Failed to setup queue pair %u on cryptodev %u\n",
586                         TEST_CDEV_QP_ID, TEST_CDEV_ID);
587
588         return TEST_SUCCESS;
589 }
590
591 static inline void
592 evdev_set_conf_values(struct rte_event_dev_config *dev_conf,
593                         struct rte_event_dev_info *info)
594 {
595         memset(dev_conf, 0, sizeof(struct rte_event_dev_config));
596         dev_conf->dequeue_timeout_ns = info->min_dequeue_timeout_ns;
597         dev_conf->nb_event_ports = NB_TEST_PORTS;
598         dev_conf->nb_event_queues = NB_TEST_QUEUES;
599         dev_conf->nb_event_queue_flows = info->max_event_queue_flows;
600         dev_conf->nb_event_port_dequeue_depth =
601                         info->max_event_port_dequeue_depth;
602         dev_conf->nb_event_port_enqueue_depth =
603                         info->max_event_port_enqueue_depth;
604         dev_conf->nb_event_port_enqueue_depth =
605                         info->max_event_port_enqueue_depth;
606         dev_conf->nb_events_limit =
607                         info->max_num_events;
608 }
609
610 static int
611 configure_eventdev(void)
612 {
613         struct rte_event_queue_conf queue_conf;
614         struct rte_event_dev_config devconf;
615         struct rte_event_dev_info info;
616         uint32_t queue_count;
617         uint32_t port_count;
618         int ret;
619         uint8_t qid;
620
621         if (!rte_event_dev_count()) {
622                 /* If there is no hardware eventdev, or no software vdev was
623                  * specified on the command line, create an instance of
624                  * event_sw.
625                  */
626                 LOG_DBG("Failed to find a valid event device... "
627                         "testing with event_sw device\n");
628                 TEST_ASSERT_SUCCESS(rte_vdev_init("event_sw0", NULL),
629                                         "Error creating eventdev");
630                 evdev = rte_event_dev_get_dev_id("event_sw0");
631         }
632
633         ret = rte_event_dev_info_get(evdev, &info);
634         TEST_ASSERT_SUCCESS(ret, "Failed to get event dev info\n");
635
636         evdev_set_conf_values(&devconf, &info);
637
638         ret = rte_event_dev_configure(evdev, &devconf);
639         TEST_ASSERT_SUCCESS(ret, "Failed to configure eventdev\n");
640
641         /* Set up event queue */
642         ret = rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_QUEUE_COUNT,
643                                         &queue_count);
644         TEST_ASSERT_SUCCESS(ret, "Queue count get failed\n");
645         TEST_ASSERT_EQUAL(queue_count, 2, "Unexpected queue count\n");
646
647         qid = TEST_APP_EV_QUEUE_ID;
648         ret = rte_event_queue_setup(evdev, qid, NULL);
649         TEST_ASSERT_SUCCESS(ret, "Failed to setup queue=%d\n", qid);
650
651         queue_conf.nb_atomic_flows = info.max_event_queue_flows;
652         queue_conf.nb_atomic_order_sequences = 32;
653         queue_conf.schedule_type = RTE_SCHED_TYPE_ATOMIC;
654         queue_conf.priority = RTE_EVENT_DEV_PRIORITY_HIGHEST;
655         queue_conf.event_queue_cfg = RTE_EVENT_QUEUE_CFG_SINGLE_LINK;
656
657         qid = TEST_CRYPTO_EV_QUEUE_ID;
658         ret = rte_event_queue_setup(evdev, qid, &queue_conf);
659         TEST_ASSERT_SUCCESS(ret, "Failed to setup queue=%u\n", qid);
660
661         /* Set up event port */
662         ret = rte_event_dev_attr_get(evdev, RTE_EVENT_DEV_ATTR_PORT_COUNT,
663                                         &port_count);
664         TEST_ASSERT_SUCCESS(ret, "Port count get failed\n");
665         TEST_ASSERT_EQUAL(port_count, 1, "Unexpected port count\n");
666
667         ret = rte_event_port_setup(evdev, TEST_APP_PORT_ID, NULL);
668         TEST_ASSERT_SUCCESS(ret, "Failed to setup port=%d\n",
669                             TEST_APP_PORT_ID);
670
671         qid = TEST_APP_EV_QUEUE_ID;
672         ret = rte_event_port_link(evdev, TEST_APP_PORT_ID, &qid, NULL, 1);
673         TEST_ASSERT(ret >= 0, "Failed to link queue port=%d\n",
674                     TEST_APP_PORT_ID);
675
676         return TEST_SUCCESS;
677 }
678
679 static void
680 test_crypto_adapter_free(void)
681 {
682         rte_event_crypto_adapter_free(TEST_ADAPTER_ID);
683 }
684
685 static int
686 test_crypto_adapter_create(void)
687 {
688         struct rte_event_port_conf conf = {
689                 .dequeue_depth = 8,
690                 .enqueue_depth = 8,
691                 .new_event_threshold = 1200,
692         };
693         int ret;
694
695         /* Create adapter with default port creation callback */
696         ret = rte_event_crypto_adapter_create(TEST_ADAPTER_ID,
697                                               evdev,
698                                               &conf, 0);
699         TEST_ASSERT_SUCCESS(ret, "Failed to create event crypto adapter\n");
700
701         return TEST_SUCCESS;
702 }
703
704 static int
705 test_crypto_adapter_qp_add_del(void)
706 {
707         uint32_t cap;
708         int ret;
709
710         ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID, &cap);
711         TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
712
713         if (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_QP_EV_BIND) {
714                 ret = rte_event_crypto_adapter_queue_pair_add(TEST_ADAPTER_ID,
715                                 TEST_CDEV_ID, TEST_CDEV_QP_ID, &response_info);
716         } else
717                 ret = rte_event_crypto_adapter_queue_pair_add(TEST_ADAPTER_ID,
718                                         TEST_CDEV_ID, TEST_CDEV_QP_ID, NULL);
719
720         TEST_ASSERT_SUCCESS(ret, "Failed to create add queue pair\n");
721
722         ret = rte_event_crypto_adapter_queue_pair_del(TEST_ADAPTER_ID,
723                                         TEST_CDEV_ID, TEST_CDEV_QP_ID);
724         TEST_ASSERT_SUCCESS(ret, "Failed to delete add queue pair\n");
725
726         return TEST_SUCCESS;
727 }
728
729 static int
730 configure_event_crypto_adapter(enum rte_event_crypto_adapter_mode mode)
731 {
732         struct rte_event_port_conf conf = {
733                 .dequeue_depth = 8,
734                 .enqueue_depth = 8,
735                 .new_event_threshold = 1200,
736         };
737
738         uint32_t cap;
739         int ret;
740
741         ret = rte_event_crypto_adapter_caps_get(evdev, TEST_CDEV_ID, &cap);
742         TEST_ASSERT_SUCCESS(ret, "Failed to get adapter capabilities\n");
743
744         /* Skip mode and capability mismatch check for SW eventdev */
745         if (!(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW) &&
746             !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD) &&
747             !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_QP_EV_BIND))
748                 goto adapter_create;
749
750         if (mode == RTE_EVENT_CRYPTO_ADAPTER_OP_FORWARD) {
751                 if (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_FWD)
752                         params.internal_port_op_fwd = 1;
753                 else
754                         return -ENOTSUP;
755         }
756
757         if ((mode == RTE_EVENT_CRYPTO_ADAPTER_OP_NEW) &&
758             !(cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_OP_NEW))
759                 return -ENOTSUP;
760
761 adapter_create:
762         /* Create adapter with default port creation callback */
763         ret = rte_event_crypto_adapter_create(TEST_ADAPTER_ID,
764                                               evdev,
765                                               &conf, mode);
766         TEST_ASSERT_SUCCESS(ret, "Failed to create event crypto adapter\n");
767
768         if (cap & RTE_EVENT_CRYPTO_ADAPTER_CAP_INTERNAL_PORT_QP_EV_BIND) {
769                 ret = rte_event_crypto_adapter_queue_pair_add(TEST_ADAPTER_ID,
770                                 TEST_CDEV_ID, TEST_CDEV_QP_ID, &response_info);
771         } else
772                 ret = rte_event_crypto_adapter_queue_pair_add(TEST_ADAPTER_ID,
773                                 TEST_CDEV_ID, TEST_CDEV_QP_ID, NULL);
774
775         TEST_ASSERT_SUCCESS(ret, "Failed to add queue pair\n");
776
777         if (!params.internal_port_op_fwd) {
778                 ret = rte_event_crypto_adapter_event_port_get(TEST_ADAPTER_ID,
779                                                 &params.crypto_event_port_id);
780                 TEST_ASSERT_SUCCESS(ret, "Failed to get event port\n");
781         }
782
783         return TEST_SUCCESS;
784 }
785
786 static void
787 test_crypto_adapter_stop(void)
788 {
789         uint32_t evdev_service_id, adapter_service_id;
790
791         /* retrieve service ids & stop services */
792         if (rte_event_crypto_adapter_service_id_get(TEST_ADAPTER_ID,
793                                                 &adapter_service_id) == 0) {
794                 rte_service_runstate_set(adapter_service_id, 0);
795                 rte_service_lcore_stop(slcore_id);
796                 rte_service_lcore_del(slcore_id);
797                 rte_event_crypto_adapter_stop(TEST_ADAPTER_ID);
798         }
799
800         if (rte_event_dev_service_id_get(evdev, &evdev_service_id) == 0) {
801                 rte_service_runstate_set(evdev_service_id, 0);
802                 rte_service_lcore_stop(slcore_id);
803                 rte_service_lcore_del(slcore_id);
804                 rte_cryptodev_stop(TEST_CDEV_ID);
805                 rte_event_dev_stop(evdev);
806         } else {
807                 rte_cryptodev_stop(TEST_CDEV_ID);
808                 rte_event_dev_stop(evdev);
809         }
810 }
811
812 static int
813 test_crypto_adapter_conf(enum rte_event_crypto_adapter_mode mode)
814 {
815         uint32_t evdev_service_id;
816         uint8_t qid;
817         int ret;
818
819         if (!crypto_adapter_setup_done) {
820                 ret = configure_event_crypto_adapter(mode);
821                 if (ret)
822                         return ret;
823                 if (!params.internal_port_op_fwd) {
824                         qid = TEST_CRYPTO_EV_QUEUE_ID;
825                         ret = rte_event_port_link(evdev,
826                                 params.crypto_event_port_id, &qid, NULL, 1);
827                         TEST_ASSERT(ret >= 0, "Failed to link queue %d "
828                                         "port=%u\n", qid,
829                                         params.crypto_event_port_id);
830                 }
831                 crypto_adapter_setup_done = 1;
832         }
833
834         /* retrieve service ids */
835         if (rte_event_dev_service_id_get(evdev, &evdev_service_id) == 0) {
836                 /* add a service core and start it */
837                 TEST_ASSERT_SUCCESS(rte_service_lcore_add(slcore_id),
838                                         "Failed to add service core");
839                 TEST_ASSERT_SUCCESS(rte_service_lcore_start(slcore_id),
840                                         "Failed to start service core");
841
842                 /* map services to it */
843                 TEST_ASSERT_SUCCESS(rte_service_map_lcore_set(evdev_service_id,
844                                 slcore_id, 1), "Failed to map evdev service");
845
846                 /* set services to running */
847                 TEST_ASSERT_SUCCESS(rte_service_runstate_set(evdev_service_id,
848                                         1), "Failed to start evdev service");
849         }
850
851         /* start the eventdev */
852         TEST_ASSERT_SUCCESS(rte_event_dev_start(evdev),
853                                 "Failed to start event device");
854
855         /* start the cryptodev */
856         TEST_ASSERT_SUCCESS(rte_cryptodev_start(TEST_CDEV_ID),
857                                 "Failed to start crypto device");
858
859         return TEST_SUCCESS;
860 }
861
862 static int
863 test_crypto_adapter_conf_op_forward_mode(void)
864 {
865         enum rte_event_crypto_adapter_mode mode;
866
867         mode = RTE_EVENT_CRYPTO_ADAPTER_OP_FORWARD;
868
869         return test_crypto_adapter_conf(mode);
870 }
871
872 static int
873 test_crypto_adapter_conf_op_new_mode(void)
874 {
875         enum rte_event_crypto_adapter_mode mode;
876
877         mode = RTE_EVENT_CRYPTO_ADAPTER_OP_NEW;
878
879         return test_crypto_adapter_conf(mode);
880 }
881
882
883 static int
884 testsuite_setup(void)
885 {
886         int ret;
887
888         slcore_id = rte_get_next_lcore(-1, 1, 0);
889         TEST_ASSERT_NOT_EQUAL(slcore_id, RTE_MAX_LCORE, "At least 2 lcores "
890                         "are required to run this autotest\n");
891
892         /* Setup and start event device. */
893         ret = configure_eventdev();
894         TEST_ASSERT_SUCCESS(ret, "Failed to setup eventdev\n");
895
896         /* Setup and start crypto device. */
897         ret = configure_cryptodev();
898         TEST_ASSERT_SUCCESS(ret, "cryptodev initialization failed\n");
899
900         return TEST_SUCCESS;
901 }
902
903 static void
904 crypto_adapter_teardown(void)
905 {
906         int ret;
907
908         ret = rte_event_crypto_adapter_stop(TEST_ADAPTER_ID);
909         if (ret < 0)
910                 RTE_LOG(ERR, USER1, "Failed to stop adapter!");
911
912         ret = rte_event_crypto_adapter_queue_pair_del(TEST_ADAPTER_ID,
913                                         TEST_CDEV_ID, TEST_CDEV_QP_ID);
914         if (ret < 0)
915                 RTE_LOG(ERR, USER1, "Failed to delete queue pair!");
916
917         ret = rte_event_crypto_adapter_free(TEST_ADAPTER_ID);
918         if (ret < 0)
919                 RTE_LOG(ERR, USER1, "Failed to free adapter!");
920
921         crypto_adapter_setup_done = 0;
922 }
923
924 static void
925 crypto_teardown(void)
926 {
927         /* Free mbuf mempool */
928         if (params.mbuf_pool != NULL) {
929                 RTE_LOG(DEBUG, USER1, "CRYPTO_ADAPTER_MBUFPOOL count %u\n",
930                 rte_mempool_avail_count(params.mbuf_pool));
931                 rte_mempool_free(params.mbuf_pool);
932                 params.mbuf_pool = NULL;
933         }
934
935         /* Free session mempool */
936         if (params.session_mpool != NULL) {
937                 RTE_LOG(DEBUG, USER1, "CRYPTO_ADAPTER_SESSION_MP count %u\n",
938                 rte_mempool_avail_count(params.session_mpool));
939                 rte_mempool_free(params.session_mpool);
940                 params.session_mpool = NULL;
941         }
942         if (params.session_priv_mpool != NULL) {
943                 rte_mempool_avail_count(params.session_priv_mpool);
944                 rte_mempool_free(params.session_priv_mpool);
945                 params.session_priv_mpool = NULL;
946         }
947
948         /* Free ops mempool */
949         if (params.op_mpool != NULL) {
950                 RTE_LOG(DEBUG, USER1, "EVENT_CRYPTO_SYM_OP_POOL count %u\n",
951                 rte_mempool_avail_count(params.op_mpool));
952                 rte_mempool_free(params.op_mpool);
953                 params.op_mpool = NULL;
954         }
955 }
956
957 static void
958 eventdev_teardown(void)
959 {
960         rte_event_dev_stop(evdev);
961 }
962
963 static void
964 testsuite_teardown(void)
965 {
966         crypto_adapter_teardown();
967         crypto_teardown();
968         eventdev_teardown();
969 }
970
971 static struct unit_test_suite functional_testsuite = {
972         .suite_name = "Event crypto adapter test suite",
973         .setup = testsuite_setup,
974         .teardown = testsuite_teardown,
975         .unit_test_cases = {
976
977                 TEST_CASE_ST(NULL, test_crypto_adapter_free,
978                                 test_crypto_adapter_create),
979
980                 TEST_CASE_ST(test_crypto_adapter_create,
981                                 test_crypto_adapter_free,
982                                 test_crypto_adapter_qp_add_del),
983
984                 TEST_CASE_ST(test_crypto_adapter_create,
985                                 test_crypto_adapter_free,
986                                 test_crypto_adapter_stats),
987
988                 TEST_CASE_ST(test_crypto_adapter_conf_op_forward_mode,
989                                 test_crypto_adapter_stop,
990                                 test_session_with_op_forward_mode),
991
992                 TEST_CASE_ST(test_crypto_adapter_conf_op_forward_mode,
993                                 test_crypto_adapter_stop,
994                                 test_sessionless_with_op_forward_mode),
995
996                 TEST_CASE_ST(test_crypto_adapter_conf_op_new_mode,
997                                 test_crypto_adapter_stop,
998                                 test_session_with_op_new_mode),
999
1000                 TEST_CASE_ST(test_crypto_adapter_conf_op_new_mode,
1001                                 test_crypto_adapter_stop,
1002                                 test_sessionless_with_op_new_mode),
1003
1004                 TEST_CASES_END() /**< NULL terminate unit test array */
1005         }
1006 };
1007
1008 static int
1009 test_event_crypto_adapter(void)
1010 {
1011         return unit_test_suite_runner(&functional_testsuite);
1012 }
1013
1014 REGISTER_TEST_COMMAND(event_crypto_adapter_autotest,
1015                 test_event_crypto_adapter);