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