test/crypto-perf: support DOCSIS protocol
[dpdk.git] / app / test-crypto-perf / cperf_options_parsing.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4
5 #include <getopt.h>
6 #include <unistd.h>
7
8 #include <rte_cryptodev.h>
9 #include <rte_malloc.h>
10 #include <rte_ether.h>
11
12 #include "cperf_options.h"
13
14 #define AES_BLOCK_SIZE 16
15 #define DES_BLOCK_SIZE 8
16
17 struct name_id_map {
18         const char *name;
19         uint32_t id;
20 };
21
22 static void
23 usage(char *progname)
24 {
25         printf("%s [EAL options] --\n"
26                 " --silent: disable options dump\n"
27                 " --ptest throughput / latency / verify / pmd-cycleount :"
28                 " set test type\n"
29                 " --pool_sz N: set the number of crypto ops/mbufs allocated\n"
30                 " --total-ops N: set the number of total operations performed\n"
31                 " --burst-sz N: set the number of packets per burst\n"
32                 " --buffer-sz N: set the size of a single packet\n"
33                 " --imix N: set the distribution of packet sizes\n"
34                 " --segment-sz N: set the size of the segment to use\n"
35                 " --desc-nb N: set number of descriptors for each crypto device\n"
36                 " --devtype TYPE: set crypto device type to use\n"
37                 " --optype cipher-only / auth-only / cipher-then-auth /\n"
38                 "           auth-then-cipher / aead : set operation type\n"
39                 " --sessionless: enable session-less crypto operations\n"
40                 " --out-of-place: enable out-of-place crypto operations\n"
41                 " --test-file NAME: set the test vector file path\n"
42                 " --test-name NAME: set specific test name section in test file\n"
43                 " --cipher-algo ALGO: set cipher algorithm\n"
44                 " --cipher-op encrypt / decrypt: set the cipher operation\n"
45                 " --cipher-key-sz N: set the cipher key size\n"
46                 " --cipher-iv-sz N: set the cipher IV size\n"
47                 " --auth-algo ALGO: set auth algorithm\n"
48                 " --auth-op generate / verify: set the auth operation\n"
49                 " --auth-key-sz N: set the auth key size\n"
50                 " --auth-iv-sz N: set the auth IV size\n"
51                 " --aead-algo ALGO: set AEAD algorithm\n"
52                 " --aead-op encrypt / decrypt: set the AEAD operation\n"
53                 " --aead-key-sz N: set the AEAD key size\n"
54                 " --aead-iv-sz N: set the AEAD IV size\n"
55                 " --aead-aad-sz N: set the AEAD AAD size\n"
56                 " --digest-sz N: set the digest size\n"
57                 " --pmd-cyclecount-delay-ms N: set delay between enqueue\n"
58                 "           and dequeue in pmd-cyclecount benchmarking mode\n"
59                 " --csv-friendly: enable test result output CSV friendly\n"
60 #ifdef RTE_LIBRTE_SECURITY
61                 " --docsis-hdr-sz: set DOCSIS header size\n"
62 #endif
63                 " -h: prints this help\n",
64                 progname);
65 }
66
67 static int
68 get_str_key_id_mapping(struct name_id_map *map, unsigned int map_len,
69                 const char *str_key)
70 {
71         unsigned int i;
72
73         for (i = 0; i < map_len; i++) {
74
75                 if (strcmp(str_key, map[i].name) == 0)
76                         return map[i].id;
77         }
78
79         return -1;
80 }
81
82 static int
83 parse_cperf_test_type(struct cperf_options *opts, const char *arg)
84 {
85         struct name_id_map cperftest_namemap[] = {
86                 {
87                         cperf_test_type_strs[CPERF_TEST_TYPE_THROUGHPUT],
88                         CPERF_TEST_TYPE_THROUGHPUT
89                 },
90                 {
91                         cperf_test_type_strs[CPERF_TEST_TYPE_VERIFY],
92                         CPERF_TEST_TYPE_VERIFY
93                 },
94                 {
95                         cperf_test_type_strs[CPERF_TEST_TYPE_LATENCY],
96                         CPERF_TEST_TYPE_LATENCY
97                 },
98                 {
99                         cperf_test_type_strs[CPERF_TEST_TYPE_PMDCC],
100                         CPERF_TEST_TYPE_PMDCC
101                 }
102         };
103
104         int id = get_str_key_id_mapping(
105                         (struct name_id_map *)cperftest_namemap,
106                         RTE_DIM(cperftest_namemap), arg);
107         if (id < 0) {
108                 RTE_LOG(ERR, USER1, "failed to parse test type");
109                 return -1;
110         }
111
112         opts->test = (enum cperf_perf_test_type)id;
113
114         return 0;
115 }
116
117 static int
118 parse_uint32_t(uint32_t *value, const char *arg)
119 {
120         char *end = NULL;
121         unsigned long n = strtoul(arg, &end, 10);
122
123         if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
124                 return -1;
125
126         if (n > UINT32_MAX)
127                 return -ERANGE;
128
129         *value = (uint32_t) n;
130
131         return 0;
132 }
133
134 static int
135 parse_uint16_t(uint16_t *value, const char *arg)
136 {
137         uint32_t val = 0;
138         int ret = parse_uint32_t(&val, arg);
139
140         if (ret < 0)
141                 return ret;
142
143         if (val > UINT16_MAX)
144                 return -ERANGE;
145
146         *value = (uint16_t) val;
147
148         return 0;
149 }
150
151 static int
152 parse_range(const char *arg, uint32_t *min, uint32_t *max, uint32_t *inc)
153 {
154         char *token;
155         uint32_t number;
156
157         char *copy_arg = strdup(arg);
158
159         if (copy_arg == NULL)
160                 return -1;
161
162         errno = 0;
163         token = strtok(copy_arg, ":");
164
165         /* Parse minimum value */
166         if (token != NULL) {
167                 number = strtoul(token, NULL, 10);
168
169                 if (errno == EINVAL || errno == ERANGE ||
170                                 number == 0)
171                         goto err_range;
172
173                 *min = number;
174         } else
175                 goto err_range;
176
177         token = strtok(NULL, ":");
178
179         /* Parse increment value */
180         if (token != NULL) {
181                 number = strtoul(token, NULL, 10);
182
183                 if (errno == EINVAL || errno == ERANGE ||
184                                 number == 0)
185                         goto err_range;
186
187                 *inc = number;
188         } else
189                 goto err_range;
190
191         token = strtok(NULL, ":");
192
193         /* Parse maximum value */
194         if (token != NULL) {
195                 number = strtoul(token, NULL, 10);
196
197                 if (errno == EINVAL || errno == ERANGE ||
198                                 number == 0 ||
199                                 number < *min)
200                         goto err_range;
201
202                 *max = number;
203         } else
204                 goto err_range;
205
206         if (strtok(NULL, ":") != NULL)
207                 goto err_range;
208
209         free(copy_arg);
210         return 0;
211
212 err_range:
213         free(copy_arg);
214         return -1;
215 }
216
217 static int
218 parse_list(const char *arg, uint32_t *list, uint32_t *min, uint32_t *max)
219 {
220         char *token;
221         uint32_t number;
222         uint8_t count = 0;
223         uint32_t temp_min;
224         uint32_t temp_max;
225
226         char *copy_arg = strdup(arg);
227
228         if (copy_arg == NULL)
229                 return -1;
230
231         errno = 0;
232         token = strtok(copy_arg, ",");
233
234         /* Parse first value */
235         if (token != NULL) {
236                 number = strtoul(token, NULL, 10);
237
238                 if (errno == EINVAL || errno == ERANGE ||
239                                 number == 0)
240                         goto err_list;
241
242                 list[count++] = number;
243                 temp_min = number;
244                 temp_max = number;
245         } else
246                 goto err_list;
247
248         token = strtok(NULL, ",");
249
250         while (token != NULL) {
251                 if (count == MAX_LIST) {
252                         RTE_LOG(WARNING, USER1, "Using only the first %u sizes\n",
253                                         MAX_LIST);
254                         break;
255                 }
256
257                 number = strtoul(token, NULL, 10);
258
259                 if (errno == EINVAL || errno == ERANGE ||
260                                 number == 0)
261                         goto err_list;
262
263                 list[count++] = number;
264
265                 if (number < temp_min)
266                         temp_min = number;
267                 if (number > temp_max)
268                         temp_max = number;
269
270                 token = strtok(NULL, ",");
271         }
272
273         if (min)
274                 *min = temp_min;
275         if (max)
276                 *max = temp_max;
277
278         free(copy_arg);
279         return count;
280
281 err_list:
282         free(copy_arg);
283         return -1;
284 }
285
286 static int
287 parse_total_ops(struct cperf_options *opts, const char *arg)
288 {
289         int ret = parse_uint32_t(&opts->total_ops, arg);
290
291         if (ret)
292                 RTE_LOG(ERR, USER1, "failed to parse total operations count\n");
293
294         if (opts->total_ops == 0) {
295                 RTE_LOG(ERR, USER1,
296                                 "invalid total operations count number specified\n");
297                 return -1;
298         }
299
300         return ret;
301 }
302
303 static int
304 parse_pool_sz(struct cperf_options *opts, const char *arg)
305 {
306         int ret =  parse_uint32_t(&opts->pool_sz, arg);
307
308         if (ret)
309                 RTE_LOG(ERR, USER1, "failed to parse pool size");
310         return ret;
311 }
312
313 static int
314 parse_burst_sz(struct cperf_options *opts, const char *arg)
315 {
316         int ret;
317
318         /* Try parsing the argument as a range, if it fails, parse it as a list */
319         if (parse_range(arg, &opts->min_burst_size, &opts->max_burst_size,
320                         &opts->inc_burst_size) < 0) {
321                 ret = parse_list(arg, opts->burst_size_list,
322                                         &opts->min_burst_size,
323                                         &opts->max_burst_size);
324                 if (ret < 0) {
325                         RTE_LOG(ERR, USER1, "failed to parse burst size/s\n");
326                         return -1;
327                 }
328                 opts->burst_size_count = ret;
329         }
330
331         return 0;
332 }
333
334 static int
335 parse_buffer_sz(struct cperf_options *opts, const char *arg)
336 {
337         int ret;
338
339         /* Try parsing the argument as a range, if it fails, parse it as a list */
340         if (parse_range(arg, &opts->min_buffer_size, &opts->max_buffer_size,
341                         &opts->inc_buffer_size) < 0) {
342                 ret = parse_list(arg, opts->buffer_size_list,
343                                         &opts->min_buffer_size,
344                                         &opts->max_buffer_size);
345                 if (ret < 0) {
346                         RTE_LOG(ERR, USER1, "failed to parse buffer size/s\n");
347                         return -1;
348                 }
349                 opts->buffer_size_count = ret;
350         }
351
352         return 0;
353 }
354
355 static int
356 parse_segment_sz(struct cperf_options *opts, const char *arg)
357 {
358         int ret = parse_uint32_t(&opts->segment_sz, arg);
359
360         if (ret) {
361                 RTE_LOG(ERR, USER1, "failed to parse segment size\n");
362                 return -1;
363         }
364
365         if (opts->segment_sz == 0) {
366                 RTE_LOG(ERR, USER1, "Segment size has to be bigger than 0\n");
367                 return -1;
368         }
369
370         return 0;
371 }
372
373 static int
374 parse_imix(struct cperf_options *opts, const char *arg)
375 {
376         int ret;
377
378         ret = parse_list(arg, opts->imix_distribution_list,
379                                 NULL, NULL);
380         if (ret < 0) {
381                 RTE_LOG(ERR, USER1, "failed to parse imix distribution\n");
382                 return -1;
383         }
384
385         opts->imix_distribution_count = ret;
386
387         if (opts->imix_distribution_count <= 1) {
388                 RTE_LOG(ERR, USER1, "imix distribution should have "
389                                 "at least two entries\n");
390                 return -1;
391         }
392
393         return 0;
394 }
395
396 static int
397 parse_desc_nb(struct cperf_options *opts, const char *arg)
398 {
399         int ret = parse_uint32_t(&opts->nb_descriptors, arg);
400
401         if (ret) {
402                 RTE_LOG(ERR, USER1, "failed to parse descriptors number\n");
403                 return -1;
404         }
405
406         if (opts->nb_descriptors == 0) {
407                 RTE_LOG(ERR, USER1, "invalid descriptors number specified\n");
408                 return -1;
409         }
410
411         return 0;
412 }
413
414 static int
415 parse_device_type(struct cperf_options *opts, const char *arg)
416 {
417         if (strlen(arg) > (sizeof(opts->device_type) - 1))
418                 return -1;
419
420         strncpy(opts->device_type, arg, sizeof(opts->device_type) - 1);
421         *(opts->device_type + sizeof(opts->device_type) - 1) = '\0';
422
423         return 0;
424 }
425
426 static int
427 parse_op_type(struct cperf_options *opts, const char *arg)
428 {
429         struct name_id_map optype_namemap[] = {
430                 {
431                         cperf_op_type_strs[CPERF_CIPHER_ONLY],
432                         CPERF_CIPHER_ONLY
433                 },
434                 {
435                         cperf_op_type_strs[CPERF_AUTH_ONLY],
436                         CPERF_AUTH_ONLY
437                 },
438                 {
439                         cperf_op_type_strs[CPERF_CIPHER_THEN_AUTH],
440                         CPERF_CIPHER_THEN_AUTH
441                 },
442                 {
443                         cperf_op_type_strs[CPERF_AUTH_THEN_CIPHER],
444                         CPERF_AUTH_THEN_CIPHER
445                 },
446                 {
447                         cperf_op_type_strs[CPERF_AEAD],
448                         CPERF_AEAD
449                 },
450                 {
451                         cperf_op_type_strs[CPERF_PDCP],
452                         CPERF_PDCP
453                 },
454                 {
455                         cperf_op_type_strs[CPERF_DOCSIS],
456                         CPERF_DOCSIS
457                 }
458         };
459
460         int id = get_str_key_id_mapping(optype_namemap,
461                         RTE_DIM(optype_namemap), arg);
462         if (id < 0) {
463                 RTE_LOG(ERR, USER1, "invalid opt type specified\n");
464                 return -1;
465         }
466
467         opts->op_type = (enum cperf_op_type)id;
468
469         return 0;
470 }
471
472 static int
473 parse_sessionless(struct cperf_options *opts,
474                 const char *arg __rte_unused)
475 {
476         opts->sessionless = 1;
477         return 0;
478 }
479
480 static int
481 parse_out_of_place(struct cperf_options *opts,
482                 const char *arg __rte_unused)
483 {
484         opts->out_of_place = 1;
485         return 0;
486 }
487
488 static int
489 parse_test_file(struct cperf_options *opts,
490                 const char *arg)
491 {
492         opts->test_file = strdup(arg);
493         if (access(opts->test_file, F_OK) != -1)
494                 return 0;
495         RTE_LOG(ERR, USER1, "Test vector file doesn't exist\n");
496
497         return -1;
498 }
499
500 static int
501 parse_test_name(struct cperf_options *opts,
502                 const char *arg)
503 {
504         char *test_name = (char *) rte_zmalloc(NULL,
505                 sizeof(char) * (strlen(arg) + 3), 0);
506         snprintf(test_name, strlen(arg) + 3, "[%s]", arg);
507         opts->test_name = test_name;
508
509         return 0;
510 }
511
512 static int
513 parse_silent(struct cperf_options *opts,
514                 const char *arg __rte_unused)
515 {
516         opts->silent = 1;
517
518         return 0;
519 }
520
521 static int
522 parse_cipher_algo(struct cperf_options *opts, const char *arg)
523 {
524
525         enum rte_crypto_cipher_algorithm cipher_algo;
526
527         if (rte_cryptodev_get_cipher_algo_enum(&cipher_algo, arg) < 0) {
528                 RTE_LOG(ERR, USER1, "Invalid cipher algorithm specified\n");
529                 return -1;
530         }
531
532         opts->cipher_algo = cipher_algo;
533
534         return 0;
535 }
536
537 static int
538 parse_cipher_op(struct cperf_options *opts, const char *arg)
539 {
540         struct name_id_map cipher_op_namemap[] = {
541                 {
542                         rte_crypto_cipher_operation_strings
543                         [RTE_CRYPTO_CIPHER_OP_ENCRYPT],
544                         RTE_CRYPTO_CIPHER_OP_ENCRYPT },
545                 {
546                         rte_crypto_cipher_operation_strings
547                         [RTE_CRYPTO_CIPHER_OP_DECRYPT],
548                         RTE_CRYPTO_CIPHER_OP_DECRYPT
549                 }
550         };
551
552         int id = get_str_key_id_mapping(cipher_op_namemap,
553                         RTE_DIM(cipher_op_namemap), arg);
554         if (id < 0) {
555                 RTE_LOG(ERR, USER1, "Invalid cipher operation specified\n");
556                 return -1;
557         }
558
559         opts->cipher_op = (enum rte_crypto_cipher_operation)id;
560
561         return 0;
562 }
563
564 static int
565 parse_cipher_key_sz(struct cperf_options *opts, const char *arg)
566 {
567         return parse_uint16_t(&opts->cipher_key_sz, arg);
568 }
569
570 static int
571 parse_cipher_iv_sz(struct cperf_options *opts, const char *arg)
572 {
573         return parse_uint16_t(&opts->cipher_iv_sz, arg);
574 }
575
576 static int
577 parse_auth_algo(struct cperf_options *opts, const char *arg)
578 {
579         enum rte_crypto_auth_algorithm auth_algo;
580
581         if (rte_cryptodev_get_auth_algo_enum(&auth_algo, arg) < 0) {
582                 RTE_LOG(ERR, USER1, "Invalid authentication algorithm specified\n");
583                 return -1;
584         }
585
586         opts->auth_algo = auth_algo;
587
588         return 0;
589 }
590
591 static int
592 parse_auth_op(struct cperf_options *opts, const char *arg)
593 {
594         struct name_id_map auth_op_namemap[] = {
595                 {
596                         rte_crypto_auth_operation_strings
597                         [RTE_CRYPTO_AUTH_OP_GENERATE],
598                         RTE_CRYPTO_AUTH_OP_GENERATE },
599                 {
600                         rte_crypto_auth_operation_strings
601                         [RTE_CRYPTO_AUTH_OP_VERIFY],
602                         RTE_CRYPTO_AUTH_OP_VERIFY
603                 }
604         };
605
606         int id = get_str_key_id_mapping(auth_op_namemap,
607                         RTE_DIM(auth_op_namemap), arg);
608         if (id < 0) {
609                 RTE_LOG(ERR, USER1, "invalid authentication operation specified"
610                                 "\n");
611                 return -1;
612         }
613
614         opts->auth_op = (enum rte_crypto_auth_operation)id;
615
616         return 0;
617 }
618
619 static int
620 parse_auth_key_sz(struct cperf_options *opts, const char *arg)
621 {
622         return parse_uint16_t(&opts->auth_key_sz, arg);
623 }
624
625 static int
626 parse_digest_sz(struct cperf_options *opts, const char *arg)
627 {
628         return parse_uint16_t(&opts->digest_sz, arg);
629 }
630
631 #ifdef RTE_LIBRTE_SECURITY
632 static int
633 parse_pdcp_sn_sz(struct cperf_options *opts, const char *arg)
634 {
635         uint32_t val = 0;
636         int ret = parse_uint32_t(&val, arg);
637
638         if (ret < 0)
639                 return ret;
640
641         if (val != RTE_SECURITY_PDCP_SN_SIZE_5 &&
642                         val != RTE_SECURITY_PDCP_SN_SIZE_7 &&
643                         val != RTE_SECURITY_PDCP_SN_SIZE_12 &&
644                         val != RTE_SECURITY_PDCP_SN_SIZE_15 &&
645                         val != RTE_SECURITY_PDCP_SN_SIZE_18) {
646                 printf("\nInvalid pdcp SN size: %u\n", val);
647                 return -ERANGE;
648         }
649         opts->pdcp_sn_sz = val;
650
651         return 0;
652 }
653
654 const char *cperf_pdcp_domain_strs[] = {
655         [RTE_SECURITY_PDCP_MODE_CONTROL] = "control",
656         [RTE_SECURITY_PDCP_MODE_DATA] = "data"
657 };
658
659 static int
660 parse_pdcp_domain(struct cperf_options *opts, const char *arg)
661 {
662         struct name_id_map pdcp_domain_namemap[] = {
663                 {
664                         cperf_pdcp_domain_strs
665                         [RTE_SECURITY_PDCP_MODE_CONTROL],
666                         RTE_SECURITY_PDCP_MODE_CONTROL },
667                 {
668                         cperf_pdcp_domain_strs
669                         [RTE_SECURITY_PDCP_MODE_DATA],
670                         RTE_SECURITY_PDCP_MODE_DATA
671                 }
672         };
673
674         int id = get_str_key_id_mapping(pdcp_domain_namemap,
675                         RTE_DIM(pdcp_domain_namemap), arg);
676         if (id < 0) {
677                 RTE_LOG(ERR, USER1, "invalid pdcp domain specified"
678                                 "\n");
679                 return -1;
680         }
681
682         opts->pdcp_domain = (enum rte_security_pdcp_domain)id;
683
684         return 0;
685 }
686
687 static int
688 parse_docsis_hdr_sz(struct cperf_options *opts, const char *arg)
689 {
690         return parse_uint16_t(&opts->docsis_hdr_sz, arg);
691 }
692 #endif
693
694 static int
695 parse_auth_iv_sz(struct cperf_options *opts, const char *arg)
696 {
697         return parse_uint16_t(&opts->auth_iv_sz, arg);
698 }
699
700 static int
701 parse_aead_algo(struct cperf_options *opts, const char *arg)
702 {
703         enum rte_crypto_aead_algorithm aead_algo;
704
705         if (rte_cryptodev_get_aead_algo_enum(&aead_algo, arg) < 0) {
706                 RTE_LOG(ERR, USER1, "Invalid AEAD algorithm specified\n");
707                 return -1;
708         }
709
710         opts->aead_algo = aead_algo;
711
712         return 0;
713 }
714
715 static int
716 parse_aead_op(struct cperf_options *opts, const char *arg)
717 {
718         struct name_id_map aead_op_namemap[] = {
719                 {
720                         rte_crypto_aead_operation_strings
721                         [RTE_CRYPTO_AEAD_OP_ENCRYPT],
722                         RTE_CRYPTO_AEAD_OP_ENCRYPT },
723                 {
724                         rte_crypto_aead_operation_strings
725                         [RTE_CRYPTO_AEAD_OP_DECRYPT],
726                         RTE_CRYPTO_AEAD_OP_DECRYPT
727                 }
728         };
729
730         int id = get_str_key_id_mapping(aead_op_namemap,
731                         RTE_DIM(aead_op_namemap), arg);
732         if (id < 0) {
733                 RTE_LOG(ERR, USER1, "invalid AEAD operation specified"
734                                 "\n");
735                 return -1;
736         }
737
738         opts->aead_op = (enum rte_crypto_aead_operation)id;
739
740         return 0;
741 }
742
743 static int
744 parse_aead_key_sz(struct cperf_options *opts, const char *arg)
745 {
746         return parse_uint16_t(&opts->aead_key_sz, arg);
747 }
748
749 static int
750 parse_aead_iv_sz(struct cperf_options *opts, const char *arg)
751 {
752         return parse_uint16_t(&opts->aead_iv_sz, arg);
753 }
754
755 static int
756 parse_aead_aad_sz(struct cperf_options *opts, const char *arg)
757 {
758         return parse_uint16_t(&opts->aead_aad_sz, arg);
759 }
760
761 static int
762 parse_csv_friendly(struct cperf_options *opts, const char *arg __rte_unused)
763 {
764         opts->csv = 1;
765         opts->silent = 1;
766         return 0;
767 }
768
769 static int
770 parse_pmd_cyclecount_delay_ms(struct cperf_options *opts,
771                         const char *arg)
772 {
773         int ret = parse_uint32_t(&opts->pmdcc_delay, arg);
774
775         if (ret) {
776                 RTE_LOG(ERR, USER1, "failed to parse pmd-cyclecount delay\n");
777                 return -1;
778         }
779
780         return 0;
781 }
782
783 typedef int (*option_parser_t)(struct cperf_options *opts,
784                 const char *arg);
785
786 struct long_opt_parser {
787         const char *lgopt_name;
788         option_parser_t parser_fn;
789
790 };
791
792 static struct option lgopts[] = {
793
794         { CPERF_PTEST_TYPE, required_argument, 0, 0 },
795
796         { CPERF_POOL_SIZE, required_argument, 0, 0 },
797         { CPERF_TOTAL_OPS, required_argument, 0, 0 },
798         { CPERF_BURST_SIZE, required_argument, 0, 0 },
799         { CPERF_BUFFER_SIZE, required_argument, 0, 0 },
800         { CPERF_SEGMENT_SIZE, required_argument, 0, 0 },
801         { CPERF_DESC_NB, required_argument, 0, 0 },
802
803         { CPERF_IMIX, required_argument, 0, 0 },
804         { CPERF_DEVTYPE, required_argument, 0, 0 },
805         { CPERF_OPTYPE, required_argument, 0, 0 },
806
807         { CPERF_SILENT, no_argument, 0, 0 },
808         { CPERF_SESSIONLESS, no_argument, 0, 0 },
809         { CPERF_OUT_OF_PLACE, no_argument, 0, 0 },
810         { CPERF_TEST_FILE, required_argument, 0, 0 },
811         { CPERF_TEST_NAME, required_argument, 0, 0 },
812
813         { CPERF_CIPHER_ALGO, required_argument, 0, 0 },
814         { CPERF_CIPHER_OP, required_argument, 0, 0 },
815
816         { CPERF_CIPHER_KEY_SZ, required_argument, 0, 0 },
817         { CPERF_CIPHER_IV_SZ, required_argument, 0, 0 },
818
819         { CPERF_AUTH_ALGO, required_argument, 0, 0 },
820         { CPERF_AUTH_OP, required_argument, 0, 0 },
821
822         { CPERF_AUTH_KEY_SZ, required_argument, 0, 0 },
823         { CPERF_AUTH_IV_SZ, required_argument, 0, 0 },
824
825         { CPERF_AEAD_ALGO, required_argument, 0, 0 },
826         { CPERF_AEAD_OP, required_argument, 0, 0 },
827
828         { CPERF_AEAD_KEY_SZ, required_argument, 0, 0 },
829         { CPERF_AEAD_AAD_SZ, required_argument, 0, 0 },
830         { CPERF_AEAD_IV_SZ, required_argument, 0, 0 },
831
832         { CPERF_DIGEST_SZ, required_argument, 0, 0 },
833
834 #ifdef RTE_LIBRTE_SECURITY
835         { CPERF_PDCP_SN_SZ, required_argument, 0, 0 },
836         { CPERF_PDCP_DOMAIN, required_argument, 0, 0 },
837         { CPERF_DOCSIS_HDR_SZ, required_argument, 0, 0 },
838 #endif
839         { CPERF_CSV, no_argument, 0, 0},
840
841         { CPERF_PMDCC_DELAY_MS, required_argument, 0, 0 },
842
843         { NULL, 0, 0, 0 }
844 };
845
846 void
847 cperf_options_default(struct cperf_options *opts)
848 {
849         opts->test = CPERF_TEST_TYPE_THROUGHPUT;
850
851         opts->pool_sz = 8192;
852         opts->total_ops = 10000000;
853         opts->nb_descriptors = 2048;
854
855         opts->buffer_size_list[0] = 64;
856         opts->buffer_size_count = 1;
857         opts->max_buffer_size = 64;
858         opts->min_buffer_size = 64;
859         opts->inc_buffer_size = 0;
860
861         opts->burst_size_list[0] = 32;
862         opts->burst_size_count = 1;
863         opts->max_burst_size = 32;
864         opts->min_burst_size = 32;
865         opts->inc_burst_size = 0;
866
867         /*
868          * Will be parsed from command line or set to
869          * maximum buffer size + digest, later
870          */
871         opts->segment_sz = 0;
872
873         opts->imix_distribution_count = 0;
874         strncpy(opts->device_type, "crypto_aesni_mb",
875                         sizeof(opts->device_type));
876         opts->nb_qps = 1;
877
878         opts->op_type = CPERF_CIPHER_THEN_AUTH;
879
880         opts->silent = 0;
881         opts->test_file = NULL;
882         opts->test_name = NULL;
883         opts->sessionless = 0;
884         opts->out_of_place = 0;
885         opts->csv = 0;
886
887         opts->cipher_algo = RTE_CRYPTO_CIPHER_AES_CBC;
888         opts->cipher_op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
889         opts->cipher_key_sz = 16;
890         opts->cipher_iv_sz = 16;
891
892         opts->auth_algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
893         opts->auth_op = RTE_CRYPTO_AUTH_OP_GENERATE;
894
895         opts->auth_key_sz = 64;
896         opts->auth_iv_sz = 0;
897
898         opts->aead_key_sz = 0;
899         opts->aead_iv_sz = 0;
900         opts->aead_aad_sz = 0;
901
902         opts->digest_sz = 12;
903
904         opts->pmdcc_delay = 0;
905 #ifdef RTE_LIBRTE_SECURITY
906         opts->pdcp_sn_sz = 12;
907         opts->pdcp_domain = RTE_SECURITY_PDCP_MODE_CONTROL;
908         opts->docsis_hdr_sz = 17;
909 #endif
910 }
911
912 static int
913 cperf_opts_parse_long(int opt_idx, struct cperf_options *opts)
914 {
915         struct long_opt_parser parsermap[] = {
916                 { CPERF_PTEST_TYPE,     parse_cperf_test_type },
917                 { CPERF_SILENT,         parse_silent },
918                 { CPERF_POOL_SIZE,      parse_pool_sz },
919                 { CPERF_TOTAL_OPS,      parse_total_ops },
920                 { CPERF_BURST_SIZE,     parse_burst_sz },
921                 { CPERF_BUFFER_SIZE,    parse_buffer_sz },
922                 { CPERF_SEGMENT_SIZE,   parse_segment_sz },
923                 { CPERF_DESC_NB,        parse_desc_nb },
924                 { CPERF_DEVTYPE,        parse_device_type },
925                 { CPERF_OPTYPE,         parse_op_type },
926                 { CPERF_SESSIONLESS,    parse_sessionless },
927                 { CPERF_OUT_OF_PLACE,   parse_out_of_place },
928                 { CPERF_IMIX,           parse_imix },
929                 { CPERF_TEST_FILE,      parse_test_file },
930                 { CPERF_TEST_NAME,      parse_test_name },
931                 { CPERF_CIPHER_ALGO,    parse_cipher_algo },
932                 { CPERF_CIPHER_OP,      parse_cipher_op },
933                 { CPERF_CIPHER_KEY_SZ,  parse_cipher_key_sz },
934                 { CPERF_CIPHER_IV_SZ,   parse_cipher_iv_sz },
935                 { CPERF_AUTH_ALGO,      parse_auth_algo },
936                 { CPERF_AUTH_OP,        parse_auth_op },
937                 { CPERF_AUTH_KEY_SZ,    parse_auth_key_sz },
938                 { CPERF_AUTH_IV_SZ,     parse_auth_iv_sz },
939                 { CPERF_AEAD_ALGO,      parse_aead_algo },
940                 { CPERF_AEAD_OP,        parse_aead_op },
941                 { CPERF_AEAD_KEY_SZ,    parse_aead_key_sz },
942                 { CPERF_AEAD_IV_SZ,     parse_aead_iv_sz },
943                 { CPERF_AEAD_AAD_SZ,    parse_aead_aad_sz },
944                 { CPERF_DIGEST_SZ,      parse_digest_sz },
945 #ifdef RTE_LIBRTE_SECURITY
946                 { CPERF_PDCP_SN_SZ,     parse_pdcp_sn_sz },
947                 { CPERF_PDCP_DOMAIN,    parse_pdcp_domain },
948                 { CPERF_DOCSIS_HDR_SZ,  parse_docsis_hdr_sz },
949 #endif
950                 { CPERF_CSV,            parse_csv_friendly},
951                 { CPERF_PMDCC_DELAY_MS, parse_pmd_cyclecount_delay_ms},
952         };
953         unsigned int i;
954
955         for (i = 0; i < RTE_DIM(parsermap); i++) {
956                 if (strncmp(lgopts[opt_idx].name, parsermap[i].lgopt_name,
957                                 strlen(lgopts[opt_idx].name)) == 0)
958                         return parsermap[i].parser_fn(opts, optarg);
959         }
960
961         return -EINVAL;
962 }
963
964 int
965 cperf_options_parse(struct cperf_options *options, int argc, char **argv)
966 {
967         int opt, retval, opt_idx;
968
969         while ((opt = getopt_long(argc, argv, "h", lgopts, &opt_idx)) != EOF) {
970                 switch (opt) {
971                 case 'h':
972                         usage(argv[0]);
973                         rte_exit(EXIT_SUCCESS, "Displayed help\n");
974                         break;
975                 /* long options */
976                 case 0:
977                         retval = cperf_opts_parse_long(opt_idx, options);
978                         if (retval != 0)
979                                 return retval;
980
981                         break;
982
983                 default:
984                         usage(argv[0]);
985                         return -EINVAL;
986                 }
987         }
988
989         return 0;
990 }
991
992 static int
993 check_cipher_buffer_length(struct cperf_options *options)
994 {
995         uint32_t buffer_size, buffer_size_idx = 0;
996
997         if (options->cipher_algo == RTE_CRYPTO_CIPHER_AES_CBC ||
998                         options->cipher_algo == RTE_CRYPTO_CIPHER_AES_ECB) {
999                 if (options->inc_buffer_size != 0)
1000                         buffer_size = options->min_buffer_size;
1001                 else
1002                         buffer_size = options->buffer_size_list[0];
1003
1004                 while (buffer_size <= options->max_buffer_size) {
1005                         if ((buffer_size % AES_BLOCK_SIZE) != 0) {
1006                                 RTE_LOG(ERR, USER1, "Some of the buffer sizes are "
1007                                         "not suitable for the algorithm selected\n");
1008                                 return -EINVAL;
1009                         }
1010
1011                         if (options->inc_buffer_size != 0)
1012                                 buffer_size += options->inc_buffer_size;
1013                         else {
1014                                 if (++buffer_size_idx == options->buffer_size_count)
1015                                         break;
1016                                 buffer_size = options->buffer_size_list[buffer_size_idx];
1017                         }
1018
1019                 }
1020         }
1021
1022         if (options->cipher_algo == RTE_CRYPTO_CIPHER_DES_CBC ||
1023                         options->cipher_algo == RTE_CRYPTO_CIPHER_3DES_CBC ||
1024                         options->cipher_algo == RTE_CRYPTO_CIPHER_3DES_ECB) {
1025                 if (options->inc_buffer_size != 0)
1026                         buffer_size = options->min_buffer_size;
1027                 else
1028                         buffer_size = options->buffer_size_list[0];
1029
1030                 while (buffer_size <= options->max_buffer_size) {
1031                         if ((buffer_size % DES_BLOCK_SIZE) != 0) {
1032                                 RTE_LOG(ERR, USER1, "Some of the buffer sizes are "
1033                                         "not suitable for the algorithm selected\n");
1034                                 return -EINVAL;
1035                         }
1036
1037                         if (options->inc_buffer_size != 0)
1038                                 buffer_size += options->inc_buffer_size;
1039                         else {
1040                                 if (++buffer_size_idx == options->buffer_size_count)
1041                                         break;
1042                                 buffer_size = options->buffer_size_list[buffer_size_idx];
1043                         }
1044
1045                 }
1046         }
1047
1048         return 0;
1049 }
1050
1051 #ifdef RTE_LIBRTE_SECURITY
1052 static int
1053 check_docsis_buffer_length(struct cperf_options *options)
1054 {
1055         uint32_t buffer_size, buffer_size_idx = 0;
1056
1057         if (options->inc_buffer_size != 0)
1058                 buffer_size = options->min_buffer_size;
1059         else
1060                 buffer_size = options->buffer_size_list[0];
1061
1062         while (buffer_size <= options->max_buffer_size) {
1063                 if (buffer_size < (uint32_t)(options->docsis_hdr_sz +
1064                                 RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN)) {
1065                         RTE_LOG(ERR, USER1, "Some of the buffer sizes are not "
1066                                 "valid for DOCSIS\n");
1067                         return -EINVAL;
1068                 }
1069
1070                 if (options->inc_buffer_size != 0)
1071                         buffer_size += options->inc_buffer_size;
1072                 else {
1073                         if (++buffer_size_idx == options->buffer_size_count)
1074                                 break;
1075                         buffer_size =
1076                                 options->buffer_size_list[buffer_size_idx];
1077                 }
1078         }
1079
1080         return 0;
1081 }
1082 #endif
1083
1084 int
1085 cperf_options_check(struct cperf_options *options)
1086 {
1087         if (options->op_type == CPERF_CIPHER_ONLY ||
1088                         options->op_type == CPERF_DOCSIS)
1089                 options->digest_sz = 0;
1090
1091         if (options->out_of_place &&
1092                         options->segment_sz <= options->max_buffer_size) {
1093                 RTE_LOG(ERR, USER1, "Out of place mode can only work "
1094                                         "with non segmented buffers\n");
1095                 return -EINVAL;
1096         }
1097
1098         /*
1099          * If segment size is not set, assume only one segment,
1100          * big enough to contain the largest buffer and the digest
1101          */
1102         if (options->segment_sz == 0)
1103                 options->segment_sz = options->max_buffer_size +
1104                                 options->digest_sz;
1105
1106         if (options->segment_sz < options->digest_sz) {
1107                 RTE_LOG(ERR, USER1,
1108                                 "Segment size should be at least "
1109                                 "the size of the digest\n");
1110                 return -EINVAL;
1111         }
1112
1113         if ((options->imix_distribution_count != 0) &&
1114                         (options->imix_distribution_count !=
1115                                 options->buffer_size_count)) {
1116                 RTE_LOG(ERR, USER1, "IMIX distribution must have the same "
1117                                 "number of buffer sizes\n");
1118                 return -EINVAL;
1119         }
1120
1121         if (options->test == CPERF_TEST_TYPE_VERIFY &&
1122                         options->test_file == NULL) {
1123                 RTE_LOG(ERR, USER1, "Define path to the file with test"
1124                                 " vectors.\n");
1125                 return -EINVAL;
1126         }
1127
1128         if (options->test == CPERF_TEST_TYPE_VERIFY &&
1129                         options->op_type != CPERF_CIPHER_ONLY &&
1130                         options->test_name == NULL) {
1131                 RTE_LOG(ERR, USER1, "Define test name to get the correct digest"
1132                                 " from the test vectors.\n");
1133                 return -EINVAL;
1134         }
1135
1136         if (options->test_name != NULL && options->test_file == NULL) {
1137                 RTE_LOG(ERR, USER1, "Define path to the file with test"
1138                                 " vectors.\n");
1139                 return -EINVAL;
1140         }
1141
1142         if (options->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY &&
1143                         options->test_file == NULL) {
1144                 RTE_LOG(ERR, USER1, "Define path to the file with test"
1145                                 " vectors.\n");
1146                 return -EINVAL;
1147         }
1148
1149         if (options->test == CPERF_TEST_TYPE_VERIFY &&
1150                         (options->inc_buffer_size != 0 ||
1151                         options->buffer_size_count > 1)) {
1152                 RTE_LOG(ERR, USER1, "Only one buffer size is allowed when "
1153                                 "using the verify test.\n");
1154                 return -EINVAL;
1155         }
1156
1157         if (options->test == CPERF_TEST_TYPE_VERIFY &&
1158                         (options->inc_burst_size != 0 ||
1159                         options->burst_size_count > 1)) {
1160                 RTE_LOG(ERR, USER1, "Only one burst size is allowed when "
1161                                 "using the verify test.\n");
1162                 return -EINVAL;
1163         }
1164
1165         if (options->test == CPERF_TEST_TYPE_PMDCC &&
1166                         options->pool_sz < options->nb_descriptors) {
1167                 RTE_LOG(ERR, USER1, "For pmd cyclecount benchmarks, pool size "
1168                                 "must be equal or greater than the number of "
1169                                 "cryptodev descriptors.\n");
1170                 return -EINVAL;
1171         }
1172
1173         if (options->test == CPERF_TEST_TYPE_VERIFY &&
1174                         options->imix_distribution_count > 0) {
1175                 RTE_LOG(ERR, USER1, "IMIX is not allowed when "
1176                                 "using the verify test.\n");
1177                 return -EINVAL;
1178         }
1179
1180         if (options->op_type == CPERF_CIPHER_THEN_AUTH) {
1181                 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
1182                                 options->auth_op !=
1183                                 RTE_CRYPTO_AUTH_OP_GENERATE) {
1184                         RTE_LOG(ERR, USER1, "Option cipher then auth must use"
1185                                         " options: encrypt and generate.\n");
1186                         return -EINVAL;
1187                 }
1188         } else if (options->op_type == CPERF_AUTH_THEN_CIPHER) {
1189                 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_DECRYPT &&
1190                                 options->auth_op !=
1191                                 RTE_CRYPTO_AUTH_OP_VERIFY) {
1192                         RTE_LOG(ERR, USER1, "Option auth then cipher must use"
1193                                         " options: decrypt and verify.\n");
1194                         return -EINVAL;
1195                 }
1196         }
1197
1198         if (options->op_type == CPERF_CIPHER_ONLY ||
1199                         options->op_type == CPERF_CIPHER_THEN_AUTH ||
1200                         options->op_type == CPERF_AUTH_THEN_CIPHER) {
1201                 if (check_cipher_buffer_length(options) < 0)
1202                         return -EINVAL;
1203         }
1204
1205 #ifdef RTE_LIBRTE_SECURITY
1206         if (options->op_type == CPERF_DOCSIS) {
1207                 if (check_docsis_buffer_length(options) < 0)
1208                         return -EINVAL;
1209         }
1210 #endif
1211
1212         return 0;
1213 }
1214
1215 void
1216 cperf_options_dump(struct cperf_options *opts)
1217 {
1218         uint8_t size_idx;
1219
1220         printf("# Crypto Performance Application Options:\n");
1221         printf("#\n");
1222         printf("# cperf test: %s\n", cperf_test_type_strs[opts->test]);
1223         printf("#\n");
1224         printf("# size of crypto op / mbuf pool: %u\n", opts->pool_sz);
1225         printf("# total number of ops: %u\n", opts->total_ops);
1226         if (opts->inc_buffer_size != 0) {
1227                 printf("# buffer size:\n");
1228                 printf("#\t min: %u\n", opts->min_buffer_size);
1229                 printf("#\t max: %u\n", opts->max_buffer_size);
1230                 printf("#\t inc: %u\n", opts->inc_buffer_size);
1231         } else {
1232                 printf("# buffer sizes: ");
1233                 for (size_idx = 0; size_idx < opts->buffer_size_count; size_idx++)
1234                         printf("%u ", opts->buffer_size_list[size_idx]);
1235                 printf("\n");
1236         }
1237         if (opts->inc_burst_size != 0) {
1238                 printf("# burst size:\n");
1239                 printf("#\t min: %u\n", opts->min_burst_size);
1240                 printf("#\t max: %u\n", opts->max_burst_size);
1241                 printf("#\t inc: %u\n", opts->inc_burst_size);
1242         } else {
1243                 printf("# burst sizes: ");
1244                 for (size_idx = 0; size_idx < opts->burst_size_count; size_idx++)
1245                         printf("%u ", opts->burst_size_list[size_idx]);
1246                 printf("\n");
1247         }
1248         printf("\n# segment size: %u\n", opts->segment_sz);
1249         printf("#\n");
1250         printf("# cryptodev type: %s\n", opts->device_type);
1251         printf("#\n");
1252         printf("# number of queue pairs per device: %u\n", opts->nb_qps);
1253         printf("# crypto operation: %s\n", cperf_op_type_strs[opts->op_type]);
1254         printf("# sessionless: %s\n", opts->sessionless ? "yes" : "no");
1255         printf("# out of place: %s\n", opts->out_of_place ? "yes" : "no");
1256         if (opts->test == CPERF_TEST_TYPE_PMDCC)
1257                 printf("# inter-burst delay: %u ms\n", opts->pmdcc_delay);
1258
1259         printf("#\n");
1260
1261         if (opts->op_type == CPERF_AUTH_ONLY ||
1262                         opts->op_type == CPERF_CIPHER_THEN_AUTH ||
1263                         opts->op_type == CPERF_AUTH_THEN_CIPHER) {
1264                 printf("# auth algorithm: %s\n",
1265                         rte_crypto_auth_algorithm_strings[opts->auth_algo]);
1266                 printf("# auth operation: %s\n",
1267                         rte_crypto_auth_operation_strings[opts->auth_op]);
1268                 printf("# auth key size: %u\n", opts->auth_key_sz);
1269                 printf("# auth iv size: %u\n", opts->auth_iv_sz);
1270                 printf("# auth digest size: %u\n", opts->digest_sz);
1271                 printf("#\n");
1272         }
1273
1274         if (opts->op_type == CPERF_CIPHER_ONLY ||
1275                         opts->op_type == CPERF_CIPHER_THEN_AUTH ||
1276                         opts->op_type == CPERF_AUTH_THEN_CIPHER) {
1277                 printf("# cipher algorithm: %s\n",
1278                         rte_crypto_cipher_algorithm_strings[opts->cipher_algo]);
1279                 printf("# cipher operation: %s\n",
1280                         rte_crypto_cipher_operation_strings[opts->cipher_op]);
1281                 printf("# cipher key size: %u\n", opts->cipher_key_sz);
1282                 printf("# cipher iv size: %u\n", opts->cipher_iv_sz);
1283                 printf("#\n");
1284         }
1285
1286         if (opts->op_type == CPERF_AEAD) {
1287                 printf("# aead algorithm: %s\n",
1288                         rte_crypto_aead_algorithm_strings[opts->aead_algo]);
1289                 printf("# aead operation: %s\n",
1290                         rte_crypto_aead_operation_strings[opts->aead_op]);
1291                 printf("# aead key size: %u\n", opts->aead_key_sz);
1292                 printf("# aead iv size: %u\n", opts->aead_iv_sz);
1293                 printf("# aead digest size: %u\n", opts->digest_sz);
1294                 printf("# aead aad size: %u\n", opts->aead_aad_sz);
1295                 printf("#\n");
1296         }
1297
1298 #ifdef RTE_LIBRTE_SECURITY
1299         if (opts->op_type == CPERF_DOCSIS) {
1300                 printf("# docsis header size: %u\n", opts->docsis_hdr_sz);
1301                 printf("#\n");
1302         }
1303 #endif
1304 }