app/crypto-perf: add options parsing check
[dpdk.git] / app / test-crypto-perf / cperf_options_parsing.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <getopt.h>
34 #include <unistd.h>
35
36 #include <rte_malloc.h>
37
38 #include "cperf_options.h"
39
40 struct name_id_map {
41         const char *name;
42         uint32_t id;
43 };
44
45 static int
46 get_str_key_id_mapping(struct name_id_map *map, unsigned int map_len,
47                 const char *str_key)
48 {
49         unsigned int i;
50
51         for (i = 0; i < map_len; i++) {
52
53                 if (strcmp(str_key, map[i].name) == 0)
54                         return map[i].id;
55         }
56
57         return -1;
58 }
59
60 static int
61 parse_cperf_test_type(struct cperf_options *opts, const char *arg)
62 {
63         struct name_id_map cperftest_namemap[] = {
64                 {
65                         cperf_test_type_strs[CPERF_TEST_TYPE_THROUGHPUT],
66                         CPERF_TEST_TYPE_THROUGHPUT
67                 },
68                 {
69                         cperf_test_type_strs[CPERF_TEST_TYPE_CYCLECOUNT],
70                         CPERF_TEST_TYPE_CYCLECOUNT
71                 },
72                 {
73                         cperf_test_type_strs[CPERF_TEST_TYPE_LATENCY],
74                         CPERF_TEST_TYPE_LATENCY
75                 }
76         };
77
78         int id = get_str_key_id_mapping(
79                         (struct name_id_map *)cperftest_namemap,
80                         RTE_DIM(cperftest_namemap), arg);
81         if (id < 0) {
82                 RTE_LOG(ERR, USER1, "failed to parse test type");
83                 return -1;
84         }
85
86         opts->test = (enum cperf_perf_test_type)id;
87
88         return 0;
89 }
90
91 static int
92 parse_uint32_t(uint32_t *value, const char *arg)
93 {
94         char *end = NULL;
95         unsigned long n = strtoul(arg, &end, 10);
96
97         if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
98                 return -1;
99
100         if (n > UINT32_MAX)
101                 return -ERANGE;
102
103         *value = (uint32_t) n;
104
105         return 0;
106 }
107
108 static int
109 parse_uint16_t(uint16_t *value, const char *arg)
110 {
111         uint32_t val = 0;
112         int ret = parse_uint32_t(&val, arg);
113
114         if (ret < 0)
115                 return ret;
116
117         if (val > UINT16_MAX)
118                 return -ERANGE;
119
120         *value = (uint16_t) val;
121
122         return 0;
123 }
124
125 static int
126 parse_total_ops(struct cperf_options *opts, const char *arg)
127 {
128         int ret = parse_uint32_t(&opts->total_ops, arg);
129
130         if (ret)
131                 RTE_LOG(ERR, USER1, "failed to parse total operations count\n");
132
133         if (opts->total_ops == 0) {
134                 RTE_LOG(ERR, USER1,
135                                 "invalid total operations count number specified\n");
136                 return -1;
137         }
138
139         return ret;
140 }
141
142 static int
143 parse_pool_sz(struct cperf_options *opts, const char *arg)
144 {
145         int ret =  parse_uint32_t(&opts->pool_sz, arg);
146
147         if (ret)
148                 RTE_LOG(ERR, USER1, "failed to parse pool size");
149         return ret;
150 }
151
152 static int
153 parse_burst_sz(struct cperf_options *opts, const char *arg)
154 {
155         int ret = parse_uint32_t(&opts->burst_sz, arg);
156
157         if (ret)
158                 RTE_LOG(ERR, USER1, "failed to parse burst size");
159         return ret;
160 }
161
162 static int
163 parse_buffer_sz(struct cperf_options *opts, const char *arg)
164 {
165         uint32_t i, valid_buf_sz[] = {
166                         32, 64, 128, 256, 384, 512, 768, 1024, 1280, 1536, 1792,
167                         2048
168         };
169
170         if (parse_uint32_t(&opts->buffer_sz, arg)) {
171                 RTE_LOG(ERR, USER1, "failed to parse buffer size");
172                 return -1;
173         }
174
175         for (i = 0; i < RTE_DIM(valid_buf_sz); i++)
176                 if (valid_buf_sz[i] == opts->buffer_sz)
177                         return 0;
178
179         RTE_LOG(ERR, USER1, "invalid buffer size specified");
180         return -1;
181 }
182
183 static int
184 parse_segments_nb(struct cperf_options *opts, const char *arg)
185 {
186         int ret = parse_uint32_t(&opts->segments_nb, arg);
187
188         if (ret) {
189                 RTE_LOG(ERR, USER1, "failed to parse segments number\n");
190                 return -1;
191         }
192
193         if ((opts->segments_nb == 0) || (opts->segments_nb > 255)) {
194                 RTE_LOG(ERR, USER1, "invalid segments number specified\n");
195                 return -1;
196         }
197
198         return 0;
199 }
200
201 static int
202 parse_device_type(struct cperf_options *opts, const char *arg)
203 {
204         if (strlen(arg) > (sizeof(opts->device_type) - 1))
205                 return -1;
206
207         strncpy(opts->device_type, arg, sizeof(opts->device_type) - 1);
208         *(opts->device_type + sizeof(opts->device_type) - 1) = '\0';
209
210         return 0;
211 }
212
213 static int
214 parse_op_type(struct cperf_options *opts, const char *arg)
215 {
216         struct name_id_map optype_namemap[] = {
217                 {
218                         cperf_op_type_strs[CPERF_CIPHER_ONLY],
219                         CPERF_CIPHER_ONLY
220                 },
221                 {
222                         cperf_op_type_strs[CPERF_AUTH_ONLY],
223                         CPERF_AUTH_ONLY
224                 },
225                 {
226                         cperf_op_type_strs[CPERF_CIPHER_THEN_AUTH],
227                         CPERF_CIPHER_THEN_AUTH
228                 },
229                 {
230                         cperf_op_type_strs[CPERF_AUTH_THEN_CIPHER],
231                         CPERF_AUTH_THEN_CIPHER
232                 },
233                 {
234                         cperf_op_type_strs[CPERF_AEAD],
235                         CPERF_AEAD
236                 }
237         };
238
239         int id = get_str_key_id_mapping(optype_namemap,
240                         RTE_DIM(optype_namemap), arg);
241         if (id < 0) {
242                 RTE_LOG(ERR, USER1, "invalid opt type specified\n");
243                 return -1;
244         }
245
246         opts->op_type = (enum cperf_op_type)id;
247
248         return 0;
249 }
250
251 static int
252 parse_sessionless(struct cperf_options *opts,
253                 const char *arg __rte_unused)
254 {
255         opts->sessionless = 1;
256         return 0;
257 }
258
259 static int
260 parse_out_of_place(struct cperf_options *opts,
261                 const char *arg __rte_unused)
262 {
263         opts->out_of_place = 1;
264         return 0;
265 }
266
267 static int
268 parse_verify(struct cperf_options *opts,
269                 const char *arg __rte_unused)
270 {
271         opts->verify = 1;
272
273         return 0;
274 }
275
276 static int
277 parse_test_file(struct cperf_options *opts,
278                 const char *arg)
279 {
280         opts->test_file = strdup(arg);
281         if (access(opts->test_file, F_OK) != -1)
282                 return 0;
283         RTE_LOG(ERR, USER1, "Test vector file doesn't exist\n");
284
285         return -1;
286 }
287
288 static int
289 parse_test_name(struct cperf_options *opts,
290                 const char *arg)
291 {
292         char *test_name = (char *) rte_zmalloc(NULL,
293                 sizeof(char) * (strlen(arg) + 3), 0);
294         snprintf(test_name, strlen(arg) + 3, "[%s]", arg);
295         opts->test_name = test_name;
296
297         return 0;
298 }
299
300 static int
301 parse_silent(struct cperf_options *opts,
302                 const char *arg __rte_unused)
303 {
304         opts->silent = 1;
305
306         return 0;
307 }
308
309 static int
310 parse_cipher_algo(struct cperf_options *opts, const char *arg)
311 {
312         struct name_id_map cipher_algo_namemap[] = {
313                 {
314                         rte_crypto_cipher_algorithm_strings
315                         [RTE_CRYPTO_CIPHER_3DES_CBC],
316                         RTE_CRYPTO_CIPHER_3DES_CBC
317                 },
318                 {
319                         rte_crypto_cipher_algorithm_strings
320                         [RTE_CRYPTO_CIPHER_3DES_ECB],
321                         RTE_CRYPTO_CIPHER_3DES_ECB
322                 },
323                 {
324                         rte_crypto_cipher_algorithm_strings
325                         [RTE_CRYPTO_CIPHER_3DES_CTR],
326                         RTE_CRYPTO_CIPHER_3DES_CTR
327                 },
328                 {
329                         rte_crypto_cipher_algorithm_strings
330                         [RTE_CRYPTO_CIPHER_AES_CBC],
331                         RTE_CRYPTO_CIPHER_AES_CBC
332                 },
333                 {
334                         rte_crypto_cipher_algorithm_strings
335                         [RTE_CRYPTO_CIPHER_AES_CCM],
336                         RTE_CRYPTO_CIPHER_AES_CCM
337                 },
338                 {
339                         rte_crypto_cipher_algorithm_strings
340                         [RTE_CRYPTO_CIPHER_AES_CTR],
341                         RTE_CRYPTO_CIPHER_AES_CTR
342                 },
343                 {
344                         rte_crypto_cipher_algorithm_strings
345                         [RTE_CRYPTO_CIPHER_AES_ECB],
346                         RTE_CRYPTO_CIPHER_AES_ECB
347                 },
348                 {
349                         rte_crypto_cipher_algorithm_strings
350                         [RTE_CRYPTO_CIPHER_AES_GCM],
351                         RTE_CRYPTO_CIPHER_AES_GCM
352                 },
353                 {
354                         rte_crypto_cipher_algorithm_strings
355                         [RTE_CRYPTO_CIPHER_AES_F8],
356                         RTE_CRYPTO_CIPHER_AES_F8
357                 },
358                 {
359                         rte_crypto_cipher_algorithm_strings
360                         [RTE_CRYPTO_CIPHER_AES_XTS],
361                         RTE_CRYPTO_CIPHER_AES_XTS
362                 },
363                 {
364                         rte_crypto_cipher_algorithm_strings
365                         [RTE_CRYPTO_CIPHER_ARC4],
366                         RTE_CRYPTO_CIPHER_ARC4
367                 },
368                 {
369                         rte_crypto_cipher_algorithm_strings
370                         [RTE_CRYPTO_CIPHER_NULL],
371                         RTE_CRYPTO_CIPHER_NULL
372                 },
373                 {
374                         rte_crypto_cipher_algorithm_strings
375                         [RTE_CRYPTO_CIPHER_KASUMI_F8],
376                         RTE_CRYPTO_CIPHER_KASUMI_F8
377                 },
378                 {
379                         rte_crypto_cipher_algorithm_strings
380                         [RTE_CRYPTO_CIPHER_SNOW3G_UEA2],
381                         RTE_CRYPTO_CIPHER_SNOW3G_UEA2
382                 },
383                 {
384                         rte_crypto_cipher_algorithm_strings
385                         [RTE_CRYPTO_CIPHER_ZUC_EEA3],
386                         RTE_CRYPTO_CIPHER_ZUC_EEA3
387                 },
388         };
389
390
391         int id = get_str_key_id_mapping(cipher_algo_namemap,
392                         RTE_DIM(cipher_algo_namemap), arg);
393         if (id < 0) {
394                 RTE_LOG(ERR, USER1, "Invalid cipher algorithm specified\n");
395                 return -1;
396         }
397
398         opts->cipher_algo = (enum rte_crypto_cipher_algorithm)id;
399
400         return 0;
401 }
402
403 static int
404 parse_cipher_op(struct cperf_options *opts, const char *arg)
405 {
406         struct name_id_map cipher_op_namemap[] = {
407                 {
408                         rte_crypto_cipher_operation_strings
409                         [RTE_CRYPTO_CIPHER_OP_ENCRYPT],
410                         RTE_CRYPTO_CIPHER_OP_ENCRYPT },
411                 {
412                         rte_crypto_cipher_operation_strings
413                         [RTE_CRYPTO_CIPHER_OP_DECRYPT],
414                         RTE_CRYPTO_CIPHER_OP_DECRYPT
415                 }
416         };
417
418         int id = get_str_key_id_mapping(cipher_op_namemap,
419                         RTE_DIM(cipher_op_namemap), arg);
420         if (id < 0) {
421                 RTE_LOG(ERR, USER1, "Invalid cipher operation specified\n");
422                 return -1;
423         }
424
425         opts->cipher_op = (enum rte_crypto_cipher_operation)id;
426
427         return 0;
428 }
429
430 static int
431 parse_cipher_key_sz(struct cperf_options *opts, const char *arg)
432 {
433         return parse_uint16_t(&opts->cipher_key_sz, arg);
434 }
435
436 static int
437 parse_cipher_iv_sz(struct cperf_options *opts, const char *arg)
438 {
439         return parse_uint16_t(&opts->cipher_iv_sz, arg);
440 }
441
442 static int
443 parse_auth_algo(struct cperf_options *opts, const char *arg) {
444         struct name_id_map cipher_auth_namemap[] = {
445                 {
446                         rte_crypto_auth_algorithm_strings
447                         [RTE_CRYPTO_AUTH_AES_CBC_MAC],
448                         RTE_CRYPTO_AUTH_AES_CBC_MAC
449                 },
450                 {
451                         rte_crypto_auth_algorithm_strings
452                         [RTE_CRYPTO_AUTH_AES_CCM],
453                         RTE_CRYPTO_AUTH_AES_CCM
454                 },
455                 {
456                         rte_crypto_auth_algorithm_strings
457                         [RTE_CRYPTO_AUTH_AES_CMAC],
458                         RTE_CRYPTO_AUTH_AES_CMAC
459                 },
460                 {
461                         rte_crypto_auth_algorithm_strings
462                         [RTE_CRYPTO_AUTH_AES_GCM],
463                         RTE_CRYPTO_AUTH_AES_GCM
464                 },
465                 {
466                         rte_crypto_auth_algorithm_strings
467                         [RTE_CRYPTO_AUTH_AES_GMAC],
468                         RTE_CRYPTO_AUTH_AES_GMAC
469                 },
470                 {
471                         rte_crypto_auth_algorithm_strings
472                         [RTE_CRYPTO_AUTH_AES_XCBC_MAC],
473                         RTE_CRYPTO_AUTH_AES_XCBC_MAC
474                 },
475                 {
476                         rte_crypto_auth_algorithm_strings
477                         [RTE_CRYPTO_AUTH_MD5],
478                         RTE_CRYPTO_AUTH_MD5
479                 },
480                 {
481                         rte_crypto_auth_algorithm_strings
482                         [RTE_CRYPTO_AUTH_MD5_HMAC],
483                         RTE_CRYPTO_AUTH_MD5_HMAC
484                 },
485                 {
486                         rte_crypto_auth_algorithm_strings
487                         [RTE_CRYPTO_AUTH_SHA1],
488                         RTE_CRYPTO_AUTH_SHA1
489                 },
490                 {
491                         rte_crypto_auth_algorithm_strings
492                         [RTE_CRYPTO_AUTH_SHA1_HMAC],
493                         RTE_CRYPTO_AUTH_SHA1_HMAC
494                 },
495                 {
496                         rte_crypto_auth_algorithm_strings
497                         [RTE_CRYPTO_AUTH_SHA224],
498                         RTE_CRYPTO_AUTH_SHA224
499                 },
500                 {
501                         rte_crypto_auth_algorithm_strings
502                         [RTE_CRYPTO_AUTH_SHA224_HMAC],
503                         RTE_CRYPTO_AUTH_SHA224_HMAC
504                 },
505                 {
506                         rte_crypto_auth_algorithm_strings
507                         [RTE_CRYPTO_AUTH_SHA256],
508                         RTE_CRYPTO_AUTH_SHA256
509                 },
510                 {
511                         rte_crypto_auth_algorithm_strings
512                         [RTE_CRYPTO_AUTH_SHA256_HMAC],
513                         RTE_CRYPTO_AUTH_SHA256_HMAC
514                 },
515                 {
516                         rte_crypto_auth_algorithm_strings
517                         [RTE_CRYPTO_AUTH_SHA384],
518                         RTE_CRYPTO_AUTH_SHA384
519                 },
520                 {
521                         rte_crypto_auth_algorithm_strings
522                         [RTE_CRYPTO_AUTH_SHA384_HMAC],
523                         RTE_CRYPTO_AUTH_SHA384_HMAC
524                 },
525                 {
526                         rte_crypto_auth_algorithm_strings
527                         [RTE_CRYPTO_AUTH_SHA512],
528                         RTE_CRYPTO_AUTH_SHA512
529                 },
530                 {
531                         rte_crypto_auth_algorithm_strings
532                         [RTE_CRYPTO_AUTH_SHA512_HMAC],
533                         RTE_CRYPTO_AUTH_SHA512_HMAC
534                 },
535                 {
536                         rte_crypto_auth_algorithm_strings
537                         [RTE_CRYPTO_AUTH_KASUMI_F9],
538                         RTE_CRYPTO_AUTH_KASUMI_F9
539                 },
540                 {
541                         rte_crypto_auth_algorithm_strings
542                         [RTE_CRYPTO_AUTH_SNOW3G_UIA2],
543                         RTE_CRYPTO_AUTH_SNOW3G_UIA2
544                 },
545                 {
546                         rte_crypto_auth_algorithm_strings
547                         [RTE_CRYPTO_AUTH_ZUC_EIA3],
548                         RTE_CRYPTO_AUTH_ZUC_EIA3
549                 },
550         };
551
552
553         int id = get_str_key_id_mapping(cipher_auth_namemap,
554                         RTE_DIM(cipher_auth_namemap), arg);
555         if (id < 0) {
556                 RTE_LOG(ERR, USER1, "invalid authentication algorithm specified"
557                                 "\n");
558                 return -1;
559         }
560
561         opts->auth_algo = (enum rte_crypto_auth_algorithm)id;
562
563         return 0;
564 }
565
566 static int
567 parse_auth_op(struct cperf_options *opts, const char *arg)
568 {
569         struct name_id_map auth_op_namemap[] = {
570                 {
571                         rte_crypto_auth_operation_strings
572                         [RTE_CRYPTO_AUTH_OP_GENERATE],
573                         RTE_CRYPTO_AUTH_OP_GENERATE },
574                 {
575                         rte_crypto_auth_operation_strings
576                         [RTE_CRYPTO_AUTH_OP_VERIFY],
577                         RTE_CRYPTO_AUTH_OP_VERIFY
578                 }
579         };
580
581         int id = get_str_key_id_mapping(auth_op_namemap,
582                         RTE_DIM(auth_op_namemap), arg);
583         if (id < 0) {
584                 RTE_LOG(ERR, USER1, "invalid authentication operation specified"
585                                 "\n");
586                 return -1;
587         }
588
589         opts->auth_op = (enum rte_crypto_auth_operation)id;
590
591         return 0;
592 }
593
594 static int
595 parse_auth_key_sz(struct cperf_options *opts, const char *arg)
596 {
597         return parse_uint16_t(&opts->auth_key_sz, arg);
598 }
599
600 static int
601 parse_auth_digest_sz(struct cperf_options *opts, const char *arg)
602 {
603         return parse_uint16_t(&opts->auth_digest_sz, arg);
604 }
605
606 static int
607 parse_auth_aad_sz(struct cperf_options *opts, const char *arg)
608 {
609         return parse_uint16_t(&opts->auth_aad_sz, arg);
610 }
611
612 static int
613 parse_csv_friendly(struct cperf_options *opts, const char *arg __rte_unused)
614 {
615         opts->csv = 1;
616         opts->silent = 1;
617         return 0;
618 }
619
620 typedef int (*option_parser_t)(struct cperf_options *opts,
621                 const char *arg);
622
623 struct long_opt_parser {
624         const char *lgopt_name;
625         option_parser_t parser_fn;
626
627 };
628
629 static struct option lgopts[] = {
630
631         { CPERF_PTEST_TYPE, required_argument, 0, 0 },
632
633         { CPERF_POOL_SIZE, required_argument, 0, 0 },
634         { CPERF_TOTAL_OPS, required_argument, 0, 0 },
635         { CPERF_BURST_SIZE, required_argument, 0, 0 },
636         { CPERF_BUFFER_SIZE, required_argument, 0, 0 },
637         { CPERF_SEGMENTS_NB, required_argument, 0, 0 },
638
639         { CPERF_DEVTYPE, required_argument, 0, 0 },
640         { CPERF_OPTYPE, required_argument, 0, 0 },
641
642         { CPERF_SILENT, no_argument, 0, 0 },
643         { CPERF_SESSIONLESS, no_argument, 0, 0 },
644         { CPERF_OUT_OF_PLACE, no_argument, 0, 0 },
645         { CPERF_VERIFY, no_argument, 0, 0 },
646         { CPERF_TEST_FILE, required_argument, 0, 0 },
647         { CPERF_TEST_NAME, required_argument, 0, 0 },
648
649         { CPERF_CIPHER_ALGO, required_argument, 0, 0 },
650         { CPERF_CIPHER_OP, required_argument, 0, 0 },
651
652         { CPERF_CIPHER_KEY_SZ, required_argument, 0, 0 },
653         { CPERF_CIPHER_IV_SZ, required_argument, 0, 0 },
654
655         { CPERF_AUTH_ALGO, required_argument, 0, 0 },
656         { CPERF_AUTH_OP, required_argument, 0, 0 },
657
658         { CPERF_AUTH_KEY_SZ, required_argument, 0, 0 },
659         { CPERF_AUTH_DIGEST_SZ, required_argument, 0, 0 },
660         { CPERF_AUTH_AAD_SZ, required_argument, 0, 0 },
661         { CPERF_CSV, no_argument, 0, 0},
662
663         { NULL, 0, 0, 0 }
664 };
665
666 void
667 cperf_options_default(struct cperf_options *opts)
668 {
669         opts->test = CPERF_TEST_TYPE_THROUGHPUT;
670
671         opts->pool_sz = 8192;
672         opts->total_ops = 10000000;
673         opts->burst_sz = 32;
674         opts->buffer_sz = 64;
675         opts->segments_nb = 1;
676
677         strncpy(opts->device_type, "crypto_aesni_mb",
678                         sizeof(opts->device_type));
679
680         opts->op_type = CPERF_CIPHER_THEN_AUTH;
681
682         opts->silent = 0;
683         opts->verify = 0;
684         opts->test_file = NULL;
685         opts->test_name = NULL;
686         opts->sessionless = 0;
687         opts->out_of_place = 0;
688         opts->csv = 0;
689
690         opts->cipher_algo = RTE_CRYPTO_CIPHER_AES_CBC;
691         opts->cipher_op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
692         opts->cipher_key_sz = 16;
693         opts->cipher_iv_sz = 16;
694
695         opts->auth_algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
696         opts->auth_op = RTE_CRYPTO_AUTH_OP_GENERATE;
697
698         opts->auth_key_sz = 64;
699         opts->auth_digest_sz = 12;
700         opts->auth_aad_sz = 0;
701 }
702
703 static int
704 cperf_opts_parse_long(int opt_idx, struct cperf_options *opts)
705 {
706         struct long_opt_parser parsermap[] = {
707                 { CPERF_PTEST_TYPE,     parse_cperf_test_type },
708                 { CPERF_SILENT,         parse_silent },
709                 { CPERF_POOL_SIZE,      parse_pool_sz },
710                 { CPERF_TOTAL_OPS,      parse_total_ops },
711                 { CPERF_BURST_SIZE,     parse_burst_sz },
712                 { CPERF_BUFFER_SIZE,    parse_buffer_sz },
713                 { CPERF_SEGMENTS_NB,    parse_segments_nb },
714                 { CPERF_DEVTYPE,        parse_device_type },
715                 { CPERF_OPTYPE,         parse_op_type },
716                 { CPERF_SESSIONLESS,    parse_sessionless },
717                 { CPERF_OUT_OF_PLACE,   parse_out_of_place },
718                 { CPERF_VERIFY,         parse_verify },
719                 { CPERF_TEST_FILE,      parse_test_file },
720                 { CPERF_TEST_NAME,      parse_test_name },
721                 { CPERF_CIPHER_ALGO,    parse_cipher_algo },
722                 { CPERF_CIPHER_OP,      parse_cipher_op },
723                 { CPERF_CIPHER_KEY_SZ,  parse_cipher_key_sz },
724                 { CPERF_CIPHER_IV_SZ,   parse_cipher_iv_sz },
725                 { CPERF_AUTH_ALGO,      parse_auth_algo },
726                 { CPERF_AUTH_OP,        parse_auth_op },
727                 { CPERF_AUTH_KEY_SZ,    parse_auth_key_sz },
728                 { CPERF_AUTH_DIGEST_SZ, parse_auth_digest_sz },
729                 { CPERF_AUTH_AAD_SZ,    parse_auth_aad_sz },
730                 { CPERF_CSV,    parse_csv_friendly},
731         };
732         unsigned int i;
733
734         for (i = 0; i < RTE_DIM(parsermap); i++) {
735                 if (strncmp(lgopts[opt_idx].name, parsermap[i].lgopt_name,
736                                 strlen(lgopts[opt_idx].name)) == 0)
737                         return parsermap[i].parser_fn(opts, optarg);
738         }
739
740         return -EINVAL;
741 }
742
743 int
744 cperf_options_parse(struct cperf_options *options, int argc, char **argv)
745 {
746         int opt, retval, opt_idx;
747
748         while ((opt = getopt_long(argc, argv, "", lgopts, &opt_idx)) != EOF) {
749                 switch (opt) {
750                 /* long options */
751                 case 0:
752
753                         retval = cperf_opts_parse_long(opt_idx, options);
754                         if (retval != 0)
755                                 return retval;
756
757                         break;
758
759                 default:
760                         return -EINVAL;
761                 }
762         }
763
764         return 0;
765 }
766
767 int
768 cperf_options_check(struct cperf_options *options)
769 {
770         if (options->segments_nb > options->buffer_sz) {
771                 RTE_LOG(ERR, USER1,
772                                 "Segments number greater than buffer size.\n");
773                 return -EINVAL;
774         }
775
776         if (options->verify && options->test_file == NULL) {
777                 RTE_LOG(ERR, USER1, "Define path to the file with test"
778                                 " vectors.\n");
779                 return -EINVAL;
780         }
781
782         if (options->test_name != NULL && options->test_file == NULL) {
783                 RTE_LOG(ERR, USER1, "Define path to the file with test"
784                                 " vectors.\n");
785                 return -EINVAL;
786         }
787
788         if (options->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY &&
789                         options->test_file == NULL) {
790                 RTE_LOG(ERR, USER1, "Define path to the file with test"
791                                 " vectors.\n");
792                 return -EINVAL;
793         }
794
795         if (options->verify &&
796                         options->total_ops > options->pool_sz) {
797                 RTE_LOG(ERR, USER1, "Total number of ops must be less than or"
798                                 " equal to the pool size.\n");
799                 return -EINVAL;
800         }
801
802         if (options->op_type == CPERF_CIPHER_THEN_AUTH) {
803                 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
804                                 options->auth_op !=
805                                 RTE_CRYPTO_AUTH_OP_GENERATE) {
806                         RTE_LOG(ERR, USER1, "Option cipher then auth must use"
807                                         " options: encrypt and generate.\n");
808                         return -EINVAL;
809                 }
810         } else if (options->op_type == CPERF_AUTH_THEN_CIPHER) {
811                 if (options->cipher_op != RTE_CRYPTO_CIPHER_OP_DECRYPT &&
812                                 options->auth_op !=
813                                 RTE_CRYPTO_AUTH_OP_VERIFY) {
814                         RTE_LOG(ERR, USER1, "Option auth then cipher must use"
815                                         " options: decrypt and verify.\n");
816                         return -EINVAL;
817                 }
818         } else if (options->op_type == CPERF_AEAD) {
819                 if (!(options->cipher_op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
820                                 options->auth_op ==
821                                 RTE_CRYPTO_AUTH_OP_GENERATE) &&
822                                 !(options->cipher_op ==
823                                 RTE_CRYPTO_CIPHER_OP_DECRYPT &&
824                                 options->auth_op ==
825                                 RTE_CRYPTO_AUTH_OP_VERIFY)) {
826                         RTE_LOG(ERR, USER1, "Use together options: encrypt and"
827                                         " generate or decrypt and verify.\n");
828                         return -EINVAL;
829                 }
830         }
831
832         return 0;
833 }
834
835 void
836 cperf_options_dump(struct cperf_options *opts)
837 {
838         printf("# Crypto Performance Application Options:\n");
839         printf("#\n");
840         printf("# cperf test: %s\n", cperf_test_type_strs[opts->test]);
841         printf("#\n");
842         printf("# size of crypto op / mbuf pool: %u\n", opts->pool_sz);
843         printf("# total number of ops: %u\n", opts->total_ops);
844         printf("# burst size: %u\n", opts->burst_sz);
845         printf("# buffer size: %u\n", opts->buffer_sz);
846         printf("# segments per buffer: %u\n", opts->segments_nb);
847         printf("#\n");
848         printf("# cryptodev type: %s\n", opts->device_type);
849         printf("#\n");
850         printf("# crypto operation: %s\n", cperf_op_type_strs[opts->op_type]);
851         printf("# verify operation: %s\n", opts->verify ? "yes" : "no");
852         printf("# sessionless: %s\n", opts->sessionless ? "yes" : "no");
853         printf("# out of place: %s\n", opts->out_of_place ? "yes" : "no");
854
855         printf("#\n");
856
857         if (opts->op_type == CPERF_AUTH_ONLY ||
858                         opts->op_type == CPERF_CIPHER_THEN_AUTH ||
859                         opts->op_type == CPERF_AUTH_THEN_CIPHER ||
860                         opts->op_type == CPERF_AEAD) {
861                 printf("# auth algorithm: %s\n",
862                         rte_crypto_auth_algorithm_strings[opts->auth_algo]);
863                 printf("# auth operation: %s\n",
864                         rte_crypto_auth_operation_strings[opts->auth_op]);
865                 printf("# auth key size: %u\n", opts->auth_key_sz);
866                 printf("# auth digest size: %u\n", opts->auth_digest_sz);
867                 printf("# auth aad size: %u\n", opts->auth_aad_sz);
868                 printf("#\n");
869         }
870
871         if (opts->op_type == CPERF_CIPHER_ONLY ||
872                         opts->op_type == CPERF_CIPHER_THEN_AUTH ||
873                         opts->op_type == CPERF_AUTH_THEN_CIPHER ||
874                         opts->op_type == CPERF_AEAD) {
875                 printf("# cipher algorithm: %s\n",
876                         rte_crypto_cipher_algorithm_strings[opts->cipher_algo]);
877                 printf("# cipher operation: %s\n",
878                         rte_crypto_cipher_operation_strings[opts->cipher_op]);
879                 printf("# cipher key size: %u\n", opts->cipher_key_sz);
880                 printf("# cipher iv size: %u\n", opts->cipher_iv_sz);
881                 printf("#\n");
882         }
883 }