1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
5 #include <rte_string_fns.h>
6 #include <rte_ipsec_sad.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
15 #include <rte_cycles.h>
16 #include <rte_errno.h>
18 #include <rte_random.h>
19 #include <rte_malloc.h>
21 #define PRINT_USAGE_START "%s [EAL options] --\n"
23 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
27 val = strtoul((in), &end_fld, (base)); \
28 if (errno != 0 || end_fld[0] != (dlm) || val > (lim)) \
30 (fd) = (typeof(fd))val; \
34 #define DEF_RULE_NUM 0x10000
35 #define DEF_TUPLES_NUM 0x100000
36 #define BURST_SZ_MAX 64
40 const char *rules_file;
41 const char *tuples_file;
47 uint32_t nb_tuples_rnd;
52 uint8_t fract_rnd_tuples;
60 .nb_rules = DEF_RULE_NUM,
61 .nb_tuples = DEF_TUPLES_NUM,
66 .burst_sz = BURST_SZ_MAX,
70 .fract_rnd_tuples = 0,
85 static char line[LINE_MAX];
87 union rte_ipsec_sad_key tuple;
91 static struct rule *rules_tbl;
92 static struct rule *tuples_tbl;
95 parse_distrib(const char *in)
99 GET_CB_FIELD(in, a, 0, UINT8_MAX, '/');
100 GET_CB_FIELD(in, b, 0, UINT8_MAX, '/');
101 GET_CB_FIELD(in, c, 0, UINT8_MAX, 0);
103 if ((a + b + c) != 100)
118 "Configured rules distribution SPI/SPI_DIP/SIP_DIP_SIP:"
120 "SPI only rules: %u\n"
121 "SPI_DIP rules: %u\n"
122 "SPI_DIP_SIP rules: %u\n"
123 "Lookup tuples: %u\n"
124 "Lookup burst size %u\n"
125 "Configured fraction of random tuples: %u\n"
126 "Random lookup tuples: %u\n",
127 config.nb_rules, config.fract_32, config.fract_64,
128 config.fract_96, config.nb_rules_32, config.nb_rules_64,
129 config.nb_rules_96, config.nb_tuples, config.burst_sz,
130 config.fract_rnd_tuples, config.nb_tuples_rnd);
138 "[-f <rules file>]\n"
139 "[-t <tuples file for lookup>]\n"
140 "[-n <rules number (if -f is not specified)>]\n"
141 "[-l <lookup tuples number (if -t is not specified)>]\n"
142 "[-6 <ipv6 tests>]\n"
143 "[-d <\"/\" separated rules length distribution"
144 "(if -f is not specified)>]\n"
145 "[-r <random tuples fraction to lookup"
146 "(if -t is not specified)>]\n"
147 "[-b <lookup burst size: 1-64 >]\n"
148 "[-v <verbose, print results on lookup>]\n"
149 "[-p <parallel lookup on all available cores>]\n"
150 "[-c <init sad supporting read/write concurrency>]\n",
156 get_str_num(FILE *f, int num)
161 while (fgets(line, sizeof(line), f) != NULL)
171 parse_file(FILE *f, struct rule *tbl, int rule_tbl)
174 char *s, *sp, *in[CB_RULE_NUM];
175 static const char *dlm = " \t\n";
176 int string_tok_nb = RTE_DIM(in);
178 string_tok_nb -= (rule_tbl == 0) ? 1 : 0;
179 while (fgets(line, sizeof(line), f) != NULL) {
181 for (i = 0; i != string_tok_nb; i++) {
182 in[i] = strtok_r(s, dlm, &sp);
187 GET_CB_FIELD(in[CB_RULE_SPI], tbl[j].tuple.v4.spi, 0,
191 ret = inet_pton(AF_INET6, in[CB_RULE_DIP],
192 &tbl[j].tuple.v6.dip);
194 ret = inet_pton(AF_INET, in[CB_RULE_DIP],
195 &tbl[j].tuple.v4.dip);
199 ret = inet_pton(AF_INET6, in[CB_RULE_SIP],
200 &tbl[j].tuple.v6.sip);
202 ret = inet_pton(AF_INET, in[CB_RULE_SIP],
203 &tbl[j].tuple.v4.sip);
206 if ((rule_tbl) && (in[CB_RULE_LEN] != NULL)) {
207 if (strcmp(in[CB_RULE_LEN], "SPI_DIP_SIP") == 0) {
208 tbl[j].rule_type = RTE_IPSEC_SAD_SPI_DIP_SIP;
209 config.nb_rules_96++;
210 } else if (strcmp(in[CB_RULE_LEN], "SPI_DIP") == 0) {
211 tbl[j].rule_type = RTE_IPSEC_SAD_SPI_DIP;
212 config.nb_rules_64++;
213 } else if (strcmp(in[CB_RULE_LEN], "SPI") == 0) {
214 tbl[j].rule_type = RTE_IPSEC_SAD_SPI_ONLY;
215 config.nb_rules_32++;
226 get_rnd_rng(uint64_t l, uint64_t u)
231 return (rte_rand() % (u - l) + l);
235 get_random_rules(struct rule *tbl, uint32_t nb_rules, int rule_tbl)
237 unsigned int i, j, rnd;
242 step = (double)UINT32_MAX / nb_rules;
243 for (i = 0; i < nb_rules; i++, edge += step) {
244 rnd = rte_rand() % 100;
246 tbl[i].tuple.v4.spi = get_rnd_rng((uint64_t)edge,
247 (uint64_t)(edge + step));
249 for (j = 0; j < 16; j++) {
250 tbl[i].tuple.v6.dip[j] = rte_rand();
251 tbl[i].tuple.v6.sip[j] = rte_rand();
254 tbl[i].tuple.v4.dip = rte_rand();
255 tbl[i].tuple.v4.sip = rte_rand();
257 if (rnd >= (100UL - config.fract_32)) {
258 rule_type = RTE_IPSEC_SAD_SPI_ONLY;
259 config.nb_rules_32++;
260 } else if (rnd >= (100UL - (config.fract_32 +
262 rule_type = RTE_IPSEC_SAD_SPI_DIP;
263 config.nb_rules_64++;
265 rule_type = RTE_IPSEC_SAD_SPI_DIP_SIP;
266 config.nb_rules_96++;
268 tbl[i].rule_type = rule_type;
270 if (rnd >= 100UL - config.fract_rnd_tuples) {
271 tbl[i].tuple.v4.spi =
272 get_rnd_rng((uint64_t)edge,
273 (uint64_t)(edge + step));
275 for (j = 0; j < 16; j++) {
276 tbl[i].tuple.v6.dip[j] =
278 tbl[i].tuple.v6.sip[j] =
282 tbl[i].tuple.v4.dip = rte_rand();
283 tbl[i].tuple.v4.sip = rte_rand();
285 config.nb_tuples_rnd++;
287 tbl[i].tuple.v4.spi = rules_tbl[i %
288 config.nb_rules].tuple.v4.spi;
290 int r_idx = i % config.nb_rules;
291 memcpy(tbl[i].tuple.v6.dip,
292 rules_tbl[r_idx].tuple.v6.dip,
293 sizeof(tbl[i].tuple.v6.dip));
294 memcpy(tbl[i].tuple.v6.sip,
295 rules_tbl[r_idx].tuple.v6.sip,
296 sizeof(tbl[i].tuple.v6.sip));
298 tbl[i].tuple.v4.dip = rules_tbl[i %
299 config.nb_rules].tuple.v4.dip;
300 tbl[i].tuple.v4.sip = rules_tbl[i %
301 config.nb_rules].tuple.v4.sip;
309 tbl_init(struct rule **tbl, uint32_t *n_entries,
310 const char *file_name, int rule_tbl)
314 const char *rules = "rules";
315 const char *tuples = "tuples";
317 if (file_name != NULL) {
318 f = fopen(file_name, "r");
320 rte_exit(-EINVAL, "failed to open file: %s\n",
324 printf("init %s table...", (rule_tbl) ? rules : tuples);
325 *n_entries = get_str_num(f, *n_entries);
326 printf("%d entries\n", *n_entries);
327 *tbl = rte_zmalloc(NULL, sizeof(struct rule) * *n_entries,
328 RTE_CACHE_LINE_SIZE);
330 rte_exit(-ENOMEM, "failed to allocate tbl\n");
333 printf("parse file %s\n", file_name);
334 ret = parse_file(f, *tbl, rule_tbl);
336 rte_exit(-EINVAL, "failed to parse file %s\n"
337 "rules file must be: "
338 "<uint32_t: spi> <space> "
339 "<ip_addr: dip> <space> "
340 "<ip_addr: sip> <space> "
341 "<string: SPI|SPI_DIP|SIP_DIP_SIP>\n"
342 "tuples file must be: "
343 "<uint32_t: spi> <space> "
344 "<ip_addr: dip> <space> "
348 printf("generate random values in %s table\n",
349 (rule_tbl) ? rules : tuples);
350 get_random_rules(*tbl, *n_entries, rule_tbl);
357 parse_opts(int argc, char **argv)
362 while ((opt = getopt(argc, argv, "f:t:n:d:l:r:6b:vpc")) != -1) {
365 config.rules_file = optarg;
368 config.tuples_file = optarg;
372 config.nb_rules = strtoul(optarg, &endptr, 10);
373 if ((errno != 0) || (config.nb_rules == 0) ||
376 rte_exit(-EINVAL, "Invalid option -n\n");
380 ret = parse_distrib(optarg);
383 rte_exit(-EINVAL, "Invalid option -d\n");
388 config.burst_sz = strtoul(optarg, &endptr, 10);
389 if ((errno != 0) || (config.burst_sz == 0) ||
390 (config.burst_sz > BURST_SZ_MAX) ||
393 rte_exit(-EINVAL, "Invalid option -b\n");
398 config.nb_tuples = strtoul(optarg, &endptr, 10);
399 if ((errno != 0) || (config.nb_tuples == 0) ||
402 rte_exit(-EINVAL, "Invalid option -l\n");
407 config.fract_rnd_tuples = strtoul(optarg, &endptr, 10);
408 if ((errno != 0) || (config.fract_rnd_tuples == 0) ||
409 (config.fract_rnd_tuples >= 100) ||
412 rte_exit(-EINVAL, "Invalid option -r\n");
422 config.parallel_lookup = 1;
425 config.concurrent_rw = 1;
429 rte_exit(-EINVAL, "Invalid options\n");
435 print_addr(int af, const void *addr)
437 char str[INET6_ADDRSTRLEN];
440 ret = inet_ntop(af, addr, str, sizeof(str));
446 print_tuple(int af, uint32_t spi, const void *dip, const void *sip)
449 printf("<SPI: %u DIP: ", spi);
457 print_result(const union rte_ipsec_sad_key *key, void *res)
459 struct rule *rule = res;
460 const struct rte_ipsec_sadv4_key *v4;
461 const struct rte_ipsec_sadv6_key *v6;
462 const char *spi_only = "SPI_ONLY";
463 const char *spi_dip = "SPI_DIP";
464 const char *spi_dip_sip = "SPI_DIP_SIP";
465 const char *rule_type;
466 const void *dip, *sip;
470 af = (config.ipv6) ? AF_INET6 : AF_INET;
473 spi = (config.ipv6 == 0) ? v4->spi : v6->spi;
474 dip = (config.ipv6 == 0) ? &v4->dip : (const void *)v6->dip;
475 sip = (config.ipv6 == 0) ? &v4->sip : (const void *)v6->sip;
479 print_tuple(af, spi, dip, sip);
480 printf(" not found\n");
484 switch (rule->rule_type) {
485 case RTE_IPSEC_SAD_SPI_ONLY:
486 rule_type = spi_only;
488 case RTE_IPSEC_SAD_SPI_DIP:
491 case RTE_IPSEC_SAD_SPI_DIP_SIP:
492 rule_type = spi_dip_sip;
498 print_tuple(af, spi, dip, sip);
499 v4 = &rule->tuple.v4;
500 v6 = &rule->tuple.v6;
501 spi = (config.ipv6 == 0) ? v4->spi : v6->spi;
502 dip = (config.ipv6 == 0) ? &v4->dip : (const void *)v6->dip;
503 sip = (config.ipv6 == 0) ? &v4->sip : (const void *)v6->sip;
504 printf("\n\tpoints to RULE ID %zu ",
505 RTE_PTR_DIFF(res, rules_tbl)/sizeof(struct rule));
506 print_tuple(af, spi, dip, sip);
507 printf(" %s\n", rule_type);
515 const union rte_ipsec_sad_key *keys[BURST_SZ_MAX];
516 void *vals[BURST_SZ_MAX];
517 uint64_t start, acc = 0;
519 struct rte_ipsec_sad *sad = arg;
521 if (config.nb_tuples == 0)
524 burst_sz = RTE_MIN(config.burst_sz, config.nb_tuples);
525 for (i = 0; i < config.nb_tuples; i += burst_sz) {
526 for (j = 0; j < burst_sz; j++)
527 keys[j] = (union rte_ipsec_sad_key *)
528 (&tuples_tbl[i + j].tuple);
529 start = rte_rdtsc_precise();
530 ret = rte_ipsec_sad_lookup(sad, keys, vals, burst_sz);
531 acc += rte_rdtsc_precise() - start;
533 rte_exit(-EINVAL, "Lookup failed\n");
534 if (config.verbose) {
535 for (j = 0; j < burst_sz; j++)
536 print_result(keys[j], vals[j]);
539 acc = (acc == 0) ? UINT64_MAX : acc;
540 printf("Average lookup cycles %.2Lf, lookups/sec: %.2Lf\n",
541 (long double)acc / config.nb_tuples,
542 (long double)config.nb_tuples * rte_get_tsc_hz() / acc);
548 add_rules(struct rte_ipsec_sad *sad, uint32_t fract)
551 uint32_t i, j, f, fn, n;
552 uint64_t start, tm[fract + 1];
553 uint32_t nm[fract + 1];
555 f = (config.nb_rules > fract) ? config.nb_rules / fract : 1;
557 for (n = 0, j = 0; n != config.nb_rules; n = fn, j++) {
560 fn = fn > config.nb_rules ? config.nb_rules : fn;
562 start = rte_rdtsc_precise();
563 for (i = n; i != fn; i++) {
564 ret = rte_ipsec_sad_add(sad,
566 rules_tbl[i].rule_type, &rules_tbl[i]);
568 rte_exit(ret, "%s failed @ %u-th rule\n",
571 tm[j] = rte_rdtsc_precise() - start;
575 for (i = 0; i != j; i++)
576 printf("ADD %u rules, %.2Lf cycles/rule, %.2Lf ADD/sec\n",
577 nm[i], (long double)tm[i] / nm[i],
578 (long double)nm[i] * rte_get_tsc_hz() / tm[i]);
582 del_rules(struct rte_ipsec_sad *sad, uint32_t fract)
585 uint32_t i, j, f, fn, n;
586 uint64_t start, tm[fract + 1];
587 uint32_t nm[fract + 1];
589 f = (config.nb_rules > fract) ? config.nb_rules / fract : 1;
591 for (n = 0, j = 0; n != config.nb_rules; n = fn, j++) {
594 fn = fn > config.nb_rules ? config.nb_rules : fn;
596 start = rte_rdtsc_precise();
597 for (i = n; i != fn; i++) {
598 ret = rte_ipsec_sad_del(sad,
600 rules_tbl[i].rule_type);
601 if (ret != 0 && ret != -ENOENT)
602 rte_exit(ret, "%s failed @ %u-th rule\n",
605 tm[j] = rte_rdtsc_precise() - start;
609 for (i = 0; i != j; i++)
610 printf("DEL %u rules, %.2Lf cycles/rule, %.2Lf DEL/sec\n",
611 nm[i], (long double)tm[i] / nm[i],
612 (long double)nm[i] * rte_get_tsc_hz() / tm[i]);
616 main(int argc, char **argv)
619 struct rte_ipsec_sad *sad;
620 struct rte_ipsec_sad_conf conf = {0};
621 unsigned int lcore_id;
623 ret = rte_eal_init(argc, argv);
625 rte_panic("Cannot init EAL\n");
630 config.prgname = argv[0];
632 parse_opts(argc, argv);
633 tbl_init(&rules_tbl, &config.nb_rules, config.rules_file, 1);
634 tbl_init(&tuples_tbl, &config.nb_tuples, config.tuples_file, 0);
635 if (config.rules_file != NULL) {
636 config.fract_32 = (100 * config.nb_rules_32) / config.nb_rules;
637 config.fract_64 = (100 * config.nb_rules_64) / config.nb_rules;
638 config.fract_96 = (100 * config.nb_rules_96) / config.nb_rules;
640 if (config.tuples_file != NULL) {
641 config.fract_rnd_tuples = 0;
642 config.nb_tuples_rnd = 0;
645 conf.max_sa[RTE_IPSEC_SAD_SPI_ONLY] = config.nb_rules_32 * 5 / 4;
646 conf.max_sa[RTE_IPSEC_SAD_SPI_DIP] = config.nb_rules_64 * 5 / 4;
647 conf.max_sa[RTE_IPSEC_SAD_SPI_DIP_SIP] = config.nb_rules_96 * 5 / 4;
649 conf.flags |= RTE_IPSEC_SAD_FLAG_IPV6;
650 if (config.concurrent_rw)
651 conf.flags |= RTE_IPSEC_SAD_FLAG_RW_CONCURRENCY;
652 sad = rte_ipsec_sad_create("test", &conf);
654 rte_exit(-rte_errno, "can not allocate SAD table\n");
659 if (config.parallel_lookup)
660 rte_eal_mp_remote_launch(lookup, sad, SKIP_MAIN);
663 if (config.parallel_lookup)
664 RTE_LCORE_FOREACH_WORKER(lcore_id)
665 if (rte_eal_wait_lcore(lcore_id) < 0)