1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Intel Corporation
8 #include <sys/socket.h>
10 #include <rte_cycles.h>
11 #include <rte_errno.h>
13 #include <rte_random.h>
14 #include <rte_malloc.h>
20 #define PRINT_USAGE_START "%s [EAL options] --\n"
22 #define GET_CB_FIELD(in, fd, base, lim, dlm) do { \
26 val = strtoul((in), &end_fld, (base)); \
27 if (errno != 0 || end_fld[0] != (dlm) || val > (lim)) \
29 (fd) = (typeof(fd))val; \
33 #define DEF_ROUTES_NUM 0x10000
34 #define DEF_LOOKUP_IPS_NUM 0x100000
36 #define DEFAULT_LPM_TBL8 100000U
38 #define CMP_FLAG (1 << 0)
39 #define CMP_ALL_FLAG (1 << 1)
40 #define IPV6_FLAG (1 << 2)
41 #define FIB_RIB_TYPE (1 << 3)
42 #define FIB_V4_DIR_TYPE (1 << 4)
43 #define FIB_V6_TRIE_TYPE (1 << 4)
44 #define FIB_TYPE_MASK (FIB_RIB_TYPE|FIB_V4_DIR_TYPE|FIB_V6_TRIE_TYPE)
45 #define SHUFFLE_FLAG (1 << 7)
46 #define DRY_RUN_FLAG (1 << 8)
48 static char *distrib_string;
49 static char line[LINE_MAX];
58 #define NIPQUAD_FMT "%u.%u.%u.%u"
59 #define NIPQUAD(addr) \
60 (unsigned)((unsigned char *)&addr)[3], \
61 (unsigned)((unsigned char *)&addr)[2], \
62 (unsigned)((unsigned char *)&addr)[1], \
63 (unsigned)((unsigned char *)&addr)[0]
65 #define NIPQUAD6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
66 #define NIPQUAD6(addr) \
67 ((uint8_t *)addr)[0] << 8 | \
68 ((uint8_t *)addr)[1], \
69 ((uint8_t *)addr)[2] << 8 | \
70 ((uint8_t *)addr)[3], \
71 ((uint8_t *)addr)[4] << 8 | \
72 ((uint8_t *)addr)[5], \
73 ((uint8_t *)addr)[6] << 8 | \
74 ((uint8_t *)addr)[7], \
75 ((uint8_t *)addr)[8] << 8 | \
76 ((uint8_t *)addr)[9], \
77 ((uint8_t *)addr)[10] << 8 | \
78 ((uint8_t *)addr)[11], \
79 ((uint8_t *)addr)[12] << 8 | \
80 ((uint8_t *)addr)[13], \
81 ((uint8_t *)addr)[14] << 8 | \
87 const char *routes_file;
88 const char *lookup_ips_file;
89 const char *routes_file_s;
90 const char *lookup_ips_file_s;
94 uint32_t nb_lookup_ips;
95 uint32_t nb_lookup_ips_rnd;
96 uint32_t nb_routes_per_depth[128 + 1];
100 uint8_t rnd_lookup_ips_ratio;
104 .lookup_ips_file = NULL,
105 .nb_routes = DEF_ROUTES_NUM,
106 .nb_lookup_ips = DEF_LOOKUP_IPS_NUM,
107 .nb_lookup_ips_rnd = 0,
108 .nb_routes_per_depth = {0},
109 .flags = FIB_V4_DIR_TYPE,
110 .tbl8 = DEFAULT_LPM_TBL8,
112 .rnd_lookup_ips_ratio = 0,
129 get_rnd_rng(uint64_t l, uint64_t u)
134 return (rte_rand() % (u - l) + l);
137 static __rte_always_inline __attribute__((pure)) uint8_t
138 bits_in_nh(uint8_t nh_sz)
140 return 8 * (1 << nh_sz);
143 static __rte_always_inline __attribute__((pure)) uint64_t
144 get_max_nh(uint8_t nh_sz)
146 /* min between fib and lpm6 which is 21 bits */
147 return RTE_MIN(((1ULL << (bits_in_nh(nh_sz) - 1)) - 1),
154 if (config.flags & IPV6_FLAG) {
155 if ((config.flags & FIB_TYPE_MASK) == FIB_V6_TRIE_TYPE)
156 return RTE_FIB6_TRIE;
158 return RTE_FIB6_DUMMY;
160 if ((config.flags & FIB_TYPE_MASK) == FIB_V4_DIR_TYPE)
161 return RTE_FIB_DIR24_8;
162 if ((config.flags & FIB_TYPE_MASK) == FIB_RIB_TYPE)
163 return RTE_FIB_DUMMY;
169 complete_distrib(uint8_t depth_lim, const uint32_t n, uint8_t rpd[],
177 * complete number of routes for every depth
178 * that was configured with ratio
180 for (depth = 0; depth <= depth_lim; depth++) {
181 if (rpd[depth] != 0) {
182 if (rpd[depth] == UINT8_MAX)
183 config.nb_routes_per_depth[depth] =
186 config.nb_routes_per_depth[depth] =
187 (n * rpd[depth]) / 100;
189 nr += config.nb_routes_per_depth[depth];
195 printf("Too much configured routes\n");
199 /*complete number of routes for every unspecified depths*/
200 for (depth = 0; depth <= depth_lim; depth++) {
201 if (rpd[depth] == 0) {
202 /*we don't need more than two /1 routes*/
203 uint64_t max_routes_per_depth =
204 1ULL << RTE_MIN(depth, 63);
205 uint32_t avg_routes_left = (n - nr) /
206 (depth_lim + 1 - m++);
207 config.nb_routes_per_depth[depth] =
208 RTE_MIN(max_routes_per_depth, avg_routes_left);
209 nr += config.nb_routes_per_depth[depth];
217 parse_distrib(uint8_t depth_lim, const uint32_t n)
219 uint8_t rpd[128 + 1] = {0}; /*routes ratios per depth including /0 */
220 uint32_t nrpd[128 + 1] = {0}; /* number of routes per depth */
222 uint8_t depth, ratio, ratio_acc = 0;
225 in = strtok(distrib_string, ",");
227 /*parse configures routes percentage ratios*/
229 GET_CB_FIELD(in, depth, 0, UINT8_MAX, ':');
230 if (in[strlen(in) - 1] == '%') {
231 in[strlen(in) - 1] = 0;
232 GET_CB_FIELD(in, ratio, 0, UINT8_MAX, '\0');
233 if (depth > depth_lim) {
234 printf("Depth /%d is bigger than maximum "
235 "allowed depth /%d for this AF\n",
240 printf("Ratio for depth /%d is bigger "
241 "than 100%%\n", depth);
244 if ((depth < 64) && ((n * ratio) / 100) >
246 printf("Configured ratio %d%% for depth /%d "
247 "has %d different routes, but maximum "
248 "is %lu\n", ratio, depth,
249 ((n * ratio) / 100), (1UL << depth));
253 /*configured zero routes for a given depth*/
255 rpd[depth] = UINT8_MAX;
256 /*sum of all percentage ratios*/
259 GET_CB_FIELD(in, n_routes, 0, UINT32_MAX, '\0');
260 rpd[depth] = UINT8_MAX;
261 nrpd[depth] = n_routes;
264 /*number of configured depths in*/
265 in = strtok(NULL, ",");
268 if (ratio_acc > 100) {
269 printf("Total ratio's sum is bigger than 100%%\n");
273 return complete_distrib(depth_lim, n, rpd, nrpd);
277 shuffle_rt_4(struct rt_rule_4 *rt, int n)
279 struct rt_rule_4 tmp;
282 for (i = 0; i < n; i++) {
284 tmp.addr = rt[i].addr;
285 tmp.depth = rt[i].depth;
288 rt[i].addr = rt[j].addr;
289 rt[i].depth = rt[j].depth;
292 rt[j].addr = tmp.addr;
293 rt[j].depth = tmp.depth;
299 shuffle_rt_6(struct rt_rule_6 *rt, int n)
301 struct rt_rule_6 tmp;
304 for (i = 0; i < n; i++) {
306 memcpy(tmp.addr, rt[i].addr, 16);
307 tmp.depth = rt[i].depth;
310 memcpy(rt[i].addr, rt[j].addr, 16);
311 rt[i].depth = rt[j].depth;
314 memcpy(rt[j].addr, tmp.addr, 16);
315 rt[j].depth = tmp.depth;
321 gen_random_rt_4(struct rt_rule_4 *rt, int nh_sz)
323 uint32_t i, j, k = 0;
325 if (config.nb_routes_per_depth[0] != 0) {
328 rt[k++].nh = rte_rand() & get_max_nh(nh_sz);
331 for (i = 1; i <= 32; i++) {
334 step = (double)(1ULL << i) / config.nb_routes_per_depth[i];
335 for (j = 0; j < config.nb_routes_per_depth[i];
336 j++, k++, edge += step) {
337 uint64_t rnd_val = get_rnd_rng((uint64_t)edge,
338 (uint64_t)(edge + step));
339 rt[k].addr = rnd_val << (32 - i);
341 rt[k].nh = rte_rand() & get_max_nh(nh_sz);
347 complete_v6_addr(uint32_t *addr, uint32_t rnd, int n)
351 for (i = 0; i < n; i++)
352 addr[i] = rte_rand();
359 gen_random_rt_6(struct rt_rule_6 *rt, int nh_sz)
361 uint32_t a, i, j, k = 0;
363 if (config.nb_routes_per_depth[0] != 0) {
364 memset(rt[k].addr, 0, 16);
366 rt[k++].nh = rte_rand() & get_max_nh(nh_sz);
369 for (a = 0; a < 4; a++) {
370 for (i = 1; i <= 32; i++) {
373 double step = (double)(1ULL << i) /
374 config.nb_routes_per_depth[(a * 32) + i];
375 for (j = 0; j < config.nb_routes_per_depth[a * 32 + i];
376 j++, k++, edge += step) {
377 uint64_t rnd_val = get_rnd_rng((uint64_t)edge,
378 (uint64_t)(edge + step));
379 rnd = rte_cpu_to_be_32(rnd_val << (32 - i));
380 complete_v6_addr((uint32_t *)rt[k].addr,
382 rt[k].depth = (a * 32) + i;
383 rt[k].nh = rte_rand() & get_max_nh(nh_sz);
390 set_rnd_ipv6(uint8_t *addr, uint8_t *route, int depth)
394 for (i = 0; i < 16; i++)
395 addr[i] = rte_rand();
397 for (i = 0; i < 16; i++) {
400 else if (depth > 0) {
401 addr[i] &= (uint16_t)UINT8_MAX >> depth;
402 addr[i] |= route[i] & UINT8_MAX << (8 - depth);
410 gen_rnd_lookup_tbl(int af)
412 uint32_t *tbl4 = config.lookup_tbl;
413 uint8_t *tbl6 = config.lookup_tbl;
414 struct rt_rule_4 *rt4 = (struct rt_rule_4 *)config.rt;
415 struct rt_rule_6 *rt6 = (struct rt_rule_6 *)config.rt;
419 for (i = 0, j = 0; i < config.nb_lookup_ips;
420 i++, j = (j + 1) % config.nb_routes) {
421 if ((rte_rand() % 100) < config.rnd_lookup_ips_ratio) {
422 tbl4[i] = rte_rand();
423 config.nb_lookup_ips_rnd++;
425 tbl4[i] = rt4[j].addr | (rte_rand() &
426 ((1ULL << (32 - rt4[j].depth)) - 1));
429 for (i = 0, j = 0; i < config.nb_lookup_ips;
430 i++, j = (j + 1) % config.nb_routes) {
431 if ((rte_rand() % 100) < config.rnd_lookup_ips_ratio) {
432 set_rnd_ipv6(&tbl6[i * 16], rt6[j].addr, 0);
433 config.nb_lookup_ips_rnd++;
435 set_rnd_ipv6(&tbl6[i * 16], rt6[j].addr,
443 _inet_net_pton(int af, char *prefix, void *addr)
445 const char *dlm = "/";
448 unsigned int max_depth;
450 if ((prefix == NULL) || (addr == NULL))
453 s = strtok_r(prefix, dlm, &sp);
457 ret = inet_pton(af, s, addr);
461 s = strtok_r(NULL, dlm, &sp);
462 max_depth = (af == AF_INET) ? 32 : 128;
463 GET_CB_FIELD(s, depth, 0, max_depth, 0);
472 char *s, *sp, *in[RT_NUM];
473 static const char *dlm = " \t\n";
474 int string_tok_nb = RTE_DIM(in);
475 struct rt_rule_4 *rt;
477 rt = (struct rt_rule_4 *)config.rt;
479 while (fgets(line, sizeof(line), f) != NULL) {
481 for (i = 0; i != string_tok_nb; i++) {
482 in[i] = strtok_r(s, dlm, &sp);
488 ret = _inet_net_pton(AF_INET, in[RT_PREFIX], &rt[j].addr);
492 rt[j].addr = rte_be_to_cpu_32(rt[j].addr);
494 config.nb_routes_per_depth[ret]++;
495 GET_CB_FIELD(in[RT_NEXTHOP], rt[j].nh, 0,
506 char *s, *sp, *in[RT_NUM];
507 static const char *dlm = " \t\n";
508 int string_tok_nb = RTE_DIM(in);
509 struct rt_rule_6 *rt;
511 rt = (struct rt_rule_6 *)config.rt;
513 while (fgets(line, sizeof(line), f) != NULL) {
515 for (i = 0; i != string_tok_nb; i++) {
516 in[i] = strtok_r(s, dlm, &sp);
522 ret = _inet_net_pton(AF_INET6, in[RT_PREFIX], rt[j].addr);
527 config.nb_routes_per_depth[ret]++;
528 GET_CB_FIELD(in[RT_NEXTHOP], rt[j].nh, 0,
537 parse_lookup(FILE *f, int af)
540 uint8_t *tbl = (uint8_t *)config.lookup_tbl;
541 int step = (af == AF_INET) ? 4 : 16;
544 while (fgets(line, sizeof(line), f) != NULL) {
545 s = strtok(line, " \t\n");
546 ret = inet_pton(af, s, &tbl[i]);
558 uint32_t *tbl4 = config.lookup_tbl;
559 uint8_t *tbl6 = config.lookup_tbl;
562 f = fopen(config.lookup_ips_file_s, "w");
564 printf("Can not open file %s\n", config.lookup_ips_file_s);
569 for (i = 0; i < config.nb_lookup_ips; i++)
570 fprintf(f, NIPQUAD_FMT"\n", NIPQUAD(tbl4[i]));
572 for (i = 0; i < config.nb_lookup_ips; i++)
573 fprintf(f, NIPQUAD6_FMT"\n", NIPQUAD6(&tbl6[i * 16]));
586 depth_lim = ((config.flags & IPV6_FLAG) == IPV6_FLAG) ? 128 : 32;
590 "Routes distribution:\n", config.nb_routes);
592 for (i = 1; i <= depth_lim; i++) {
594 "depth /%d:%u", i, config.nb_routes_per_depth[i]);
599 fprintf(stdout, "%c", dlm);
603 "Lookup tuples: %u\n"
604 "Configured ratios of random ips for lookup: %u\n"
605 "Random lookup ips: %u\n",
606 config.nb_lookup_ips, config.rnd_lookup_ips_ratio,
607 config.nb_lookup_ips_rnd);
615 "[-f <routes file>]\n"
616 "[-t <ip's file for lookup>]\n"
617 "[-n <number of routes (if -f is not specified)>]\n"
618 "[-l <number of ip's for lookup (if -t is not specified)>]\n"
619 "[-d <\",\" separated \"depth:n%%\"routes depth distribution"
620 "(if -f is not specified)>]\n"
621 "[-r <percentage ratio of random ip's to lookup"
622 "(if -t is not specified)>]\n"
623 "[-c <do comarison with LPM library>]\n"
624 "[-6 <do tests with ipv6 (default ipv4)>]\n"
625 "[-s <shuffle randomly generated routes>]\n"
626 "[-a <check nexthops for all ipv4 address space"
627 "(only valid with -c)>]\n"
628 "[-b <fib algorithm>]\n\tavailible options for ipv4\n"
629 "\t\trib - RIB based FIB\n"
630 "\t\tdir - DIR24_8 based FIB\n"
631 "\tavailible options for ipv6:\n"
632 "\t\trib - RIB based FIB\n"
633 "\t\ttrie - TRIE based FIB\n"
634 "defaults are: dir for ipv4 and trie for ipv6\n"
635 "[-e <entry size (valid only for dir and trie fib types): "
636 "1/2/4/8 (default 4)>]\n"
637 "[-g <number of tbl8's for dir24_8 or trie FIBs>]\n"
638 "[-w <path to the file to dump routing table>]\n"
639 "[-u <path to the file to dump ip's for lookup>]\n",
646 if ((config.routes_file == NULL) && (config.lookup_ips_file != NULL)) {
647 printf("-t option only valid with -f option\n");
651 if ((config.flags & CMP_ALL_FLAG) && (config.flags & IPV6_FLAG)) {
652 printf("-a flag is only valid for ipv4\n");
656 if ((config.flags & CMP_ALL_FLAG) &&
657 ((config.flags & CMP_FLAG) != CMP_FLAG)) {
658 printf("-a flag is valid only with -c flag\n");
662 if (!((config.ent_sz == 1) || (config.ent_sz == 2) ||
663 (config.ent_sz == 4) || (config.ent_sz == 8))) {
664 printf("wrong -e option %d, can be 1 or 2 or 4 or 8\n",
669 if ((config.ent_sz == 1) && (config.flags & IPV6_FLAG)) {
670 printf("-e 1 is valid only for ipv4\n");
677 parse_opts(int argc, char **argv)
682 while ((opt = getopt(argc, argv, "f:t:n:d:l:r:c6ab:e:g:w:u:s")) !=
686 config.routes_file = optarg;
689 config.lookup_ips_file = optarg;
692 config.routes_file_s = optarg;
693 config.flags |= DRY_RUN_FLAG;
696 config.lookup_ips_file_s = optarg;
697 config.flags |= DRY_RUN_FLAG;
701 config.nb_routes = strtoul(optarg, &endptr, 10);
702 if ((errno != 0) || (config.nb_routes == 0)) {
704 rte_exit(-EINVAL, "Invalid option -n\n");
708 distrib_string = optarg;
712 config.nb_lookup_ips = strtoul(optarg, &endptr, 10);
713 if ((errno != 0) || (config.nb_lookup_ips == 0)) {
715 rte_exit(-EINVAL, "Invalid option -l\n");
720 config.rnd_lookup_ips_ratio =
721 strtoul(optarg, &endptr, 10);
723 (config.rnd_lookup_ips_ratio == 0) ||
724 (config.rnd_lookup_ips_ratio >= 100)) {
726 rte_exit(-EINVAL, "Invalid option -r\n");
730 config.flags |= SHUFFLE_FLAG;
733 config.flags |= CMP_FLAG;
736 config.flags |= IPV6_FLAG;
739 config.flags |= CMP_ALL_FLAG;
742 if (strcmp(optarg, "rib") == 0) {
743 config.flags &= ~FIB_TYPE_MASK;
744 config.flags |= FIB_RIB_TYPE;
745 } else if (strcmp(optarg, "dir") == 0) {
746 config.flags &= ~FIB_TYPE_MASK;
747 config.flags |= FIB_V4_DIR_TYPE;
748 } else if (strcmp(optarg, "trie") == 0) {
749 config.flags &= ~FIB_TYPE_MASK;
750 config.flags |= FIB_V6_TRIE_TYPE;
752 rte_exit(-EINVAL, "Invalid option -b\n");
756 config.ent_sz = strtoul(optarg, &endptr, 10);
759 rte_exit(-EINVAL, "Invalid option -e\n");
764 config.tbl8 = strtoul(optarg, &endptr, 10);
765 if ((errno != 0) || (config.tbl8 == 0)) {
767 rte_exit(-EINVAL, "Invalid option -g\n");
772 rte_exit(-EINVAL, "Invalid options\n");
778 dump_rt_4(struct rt_rule_4 *rt)
783 f = fopen(config.routes_file_s, "w");
785 printf("Can not open file %s\n", config.routes_file_s);
789 for (i = 0; i < config.nb_routes; i++)
790 fprintf(f, NIPQUAD_FMT"/%d %"PRIu64"\n", NIPQUAD(rt[i].addr),
791 rt[i].depth, rt[i].nh);
798 print_depth_err(void)
800 printf("LPM does not support /0 prefix length (default route), use "
801 "-d 0:0 option or remove /0 prefix from routes file\n");
810 struct rte_fib_conf conf = {0};
811 struct rt_rule_4 *rt;
814 struct rte_lpm *lpm = NULL;
815 struct rte_lpm_config lpm_conf;
816 uint32_t *tbl4 = config.lookup_tbl;
817 uint64_t fib_nh[BURST_SZ];
818 uint32_t lpm_nh[BURST_SZ];
820 rt = (struct rt_rule_4 *)config.rt;
822 if (config.flags & DRY_RUN_FLAG) {
823 if (config.routes_file_s != NULL)
827 if (config.lookup_ips_file_s != NULL)
828 ret = dump_lookup(AF_INET);
832 conf.type = get_fib_type();
833 conf.default_nh = def_nh;
834 conf.max_routes = config.nb_routes * 2;
835 if (conf.type == RTE_FIB_DIR24_8) {
836 conf.dir24_8.nh_sz = __builtin_ctz(config.ent_sz);
837 conf.dir24_8.num_tbl8 = RTE_MIN(config.tbl8,
838 get_max_nh(conf.dir24_8.nh_sz));
841 fib = rte_fib_create("test", -1, &conf);
843 printf("Can not alloc FIB, err %d\n", rte_errno);
847 for (k = config.print_fract, i = 0; k > 0; k--) {
848 start = rte_rdtsc_precise();
849 for (j = 0; j < (config.nb_routes - i) / k; j++) {
850 ret = rte_fib_add(fib, rt[i + j].addr, rt[i + j].depth,
852 if (unlikely(ret != 0)) {
853 printf("Can not add a route to FIB, err %d\n",
858 printf("AVG FIB add %"PRIu64"\n",
859 (rte_rdtsc_precise() - start) / j);
863 if (config.flags & CMP_FLAG) {
864 lpm_conf.max_rules = config.nb_routes * 2;
865 lpm_conf.number_tbl8s = RTE_MAX(conf.dir24_8.num_tbl8,
868 lpm = rte_lpm_create("test_lpm", -1, &lpm_conf);
870 printf("Can not alloc LPM, err %d\n", rte_errno);
873 for (k = config.print_fract, i = 0; k > 0; k--) {
874 start = rte_rdtsc_precise();
875 for (j = 0; j < (config.nb_routes - i) / k; j++) {
876 ret = rte_lpm_add(lpm, rt[i + j].addr,
877 rt[i + j].depth, rt[i + j].nh);
879 if (rt[i + j].depth == 0)
881 printf("Can not add a route to LPM, "
886 printf("AVG LPM add %"PRIu64"\n",
887 (rte_rdtsc_precise() - start) / j);
893 for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
894 start = rte_rdtsc_precise();
895 ret = rte_fib_lookup_bulk(fib, tbl4 + i, fib_nh, BURST_SZ);
896 acc += rte_rdtsc_precise() - start;
898 printf("FIB lookup fails, err %d\n", ret);
902 printf("AVG FIB lookup %.1f\n", (double)acc / (double)i);
904 if (config.flags & CMP_FLAG) {
906 for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
907 start = rte_rdtsc_precise();
908 ret = rte_lpm_lookup_bulk(lpm, tbl4 + i, lpm_nh,
910 acc += rte_rdtsc_precise() - start;
912 printf("LPM lookup fails, err %d\n", ret);
916 printf("AVG LPM lookup %.1f\n", (double)acc / (double)i);
918 for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
919 rte_fib_lookup_bulk(fib, tbl4 + i, fib_nh, BURST_SZ);
920 rte_lpm_lookup_bulk(lpm, tbl4 + i, lpm_nh, BURST_SZ);
921 for (j = 0; j < BURST_SZ; j++) {
922 struct rte_lpm_tbl_entry *tbl;
923 tbl = (struct rte_lpm_tbl_entry *)&lpm_nh[j];
924 if ((fib_nh[j] != tbl->next_hop) &&
925 !((tbl->valid == 0) &&
926 (fib_nh[j] == def_nh))) {
932 printf("FIB and LPM lookup returns same values\n");
935 for (k = config.print_fract, i = 0; k > 0; k--) {
936 start = rte_rdtsc_precise();
937 for (j = 0; j < (config.nb_routes - i) / k; j++)
938 rte_fib_delete(fib, rt[i + j].addr, rt[i + j].depth);
940 printf("AVG FIB delete %"PRIu64"\n",
941 (rte_rdtsc_precise() - start) / j);
945 if (config.flags & CMP_FLAG) {
946 for (k = config.print_fract, i = 0; k > 0; k--) {
947 start = rte_rdtsc_precise();
948 for (j = 0; j < (config.nb_routes - i) / k; j++)
949 rte_lpm_delete(lpm, rt[i + j].addr,
952 printf("AVG LPM delete %"PRIu64"\n",
953 (rte_rdtsc_precise() - start) / j);
962 dump_rt_6(struct rt_rule_6 *rt)
967 f = fopen(config.routes_file_s, "w");
969 printf("Can not open file %s\n", config.routes_file_s);
973 for (i = 0; i < config.nb_routes; i++) {
974 fprintf(f, NIPQUAD6_FMT"/%d %"PRIu64"\n", NIPQUAD6(rt[i].addr),
975 rt[i].depth, rt[i].nh);
987 struct rte_fib6 *fib;
988 struct rte_fib6_conf conf = {0};
989 struct rt_rule_6 *rt;
992 struct rte_lpm6 *lpm = NULL;
993 struct rte_lpm6_config lpm_conf;
995 uint64_t fib_nh[BURST_SZ];
996 int32_t lpm_nh[BURST_SZ];
998 rt = (struct rt_rule_6 *)config.rt;
999 tbl6 = config.lookup_tbl;
1001 if (config.flags & DRY_RUN_FLAG) {
1002 if (config.routes_file_s != NULL)
1003 ret = dump_rt_6(rt);
1006 if (config.lookup_ips_file_s != NULL)
1007 ret = dump_lookup(AF_INET6);
1011 conf.type = get_fib_type();
1012 conf.default_nh = def_nh;
1013 conf.max_routes = config.nb_routes * 2;
1014 if (conf.type == RTE_FIB6_TRIE) {
1015 conf.trie.nh_sz = __builtin_ctz(config.ent_sz);
1016 conf.trie.num_tbl8 = RTE_MIN(config.tbl8,
1017 get_max_nh(conf.trie.nh_sz));
1020 fib = rte_fib6_create("test", -1, &conf);
1022 printf("Can not alloc FIB, err %d\n", rte_errno);
1026 for (k = config.print_fract, i = 0; k > 0; k--) {
1027 start = rte_rdtsc_precise();
1028 for (j = 0; j < (config.nb_routes - i) / k; j++) {
1029 ret = rte_fib6_add(fib, rt[i + j].addr,
1030 rt[i + j].depth, rt[i + j].nh);
1031 if (unlikely(ret != 0)) {
1032 printf("Can not add a route to FIB, err %d\n",
1037 printf("AVG FIB add %"PRIu64"\n",
1038 (rte_rdtsc_precise() - start) / j);
1042 if (config.flags & CMP_FLAG) {
1043 lpm_conf.max_rules = config.nb_routes * 2;
1044 lpm_conf.number_tbl8s = RTE_MAX(conf.trie.num_tbl8,
1047 lpm = rte_lpm6_create("test_lpm", -1, &lpm_conf);
1049 printf("Can not alloc LPM, err %d\n", rte_errno);
1052 for (k = config.print_fract, i = 0; k > 0; k--) {
1053 start = rte_rdtsc_precise();
1054 for (j = 0; j < (config.nb_routes - i) / k; j++) {
1055 ret = rte_lpm6_add(lpm, rt[i + j].addr,
1056 rt[i + j].depth, rt[i + j].nh);
1058 if (rt[i + j].depth == 0)
1060 printf("Can not add a route to LPM, "
1065 printf("AVG LPM add %"PRIu64"\n",
1066 (rte_rdtsc_precise() - start) / j);
1072 for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
1073 start = rte_rdtsc_precise();
1074 ret = rte_fib6_lookup_bulk(fib, (uint8_t (*)[16])(tbl6 + i*16),
1076 acc += rte_rdtsc_precise() - start;
1078 printf("FIB lookup fails, err %d\n", ret);
1082 printf("AVG FIB lookup %.1f\n", (double)acc / (double)i);
1084 if (config.flags & CMP_FLAG) {
1086 for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
1087 start = rte_rdtsc_precise();
1088 ret = rte_lpm6_lookup_bulk_func(lpm,
1089 (uint8_t (*)[16])(tbl6 + i*16),
1091 acc += rte_rdtsc_precise() - start;
1093 printf("LPM lookup fails, err %d\n", ret);
1097 printf("AVG LPM lookup %.1f\n", (double)acc / (double)i);
1099 for (i = 0; i < config.nb_lookup_ips; i += BURST_SZ) {
1100 rte_fib6_lookup_bulk(fib,
1101 (uint8_t (*)[16])(tbl6 + i*16),
1103 rte_lpm6_lookup_bulk_func(lpm,
1104 (uint8_t (*)[16])(tbl6 + i*16),
1106 for (j = 0; j < BURST_SZ; j++) {
1107 if ((fib_nh[j] != (uint32_t)lpm_nh[j]) &&
1108 !((lpm_nh[j] == -1) &&
1109 (fib_nh[j] == def_nh))) {
1115 printf("FIB and LPM lookup returns same values\n");
1118 for (k = config.print_fract, i = 0; k > 0; k--) {
1119 start = rte_rdtsc_precise();
1120 for (j = 0; j < (config.nb_routes - i) / k; j++)
1121 rte_fib6_delete(fib, rt[i + j].addr, rt[i + j].depth);
1123 printf("AVG FIB delete %"PRIu64"\n",
1124 (rte_rdtsc_precise() - start) / j);
1128 if (config.flags & CMP_FLAG) {
1129 for (k = config.print_fract, i = 0; k > 0; k--) {
1130 start = rte_rdtsc_precise();
1131 for (j = 0; j < (config.nb_routes - i) / k; j++)
1132 rte_lpm6_delete(lpm, rt[i + j].addr,
1135 printf("AVG LPM delete %"PRIu64"\n",
1136 (rte_rdtsc_precise() - start) / j);
1144 main(int argc, char **argv)
1146 int ret, af, rt_ent_sz, lookup_ent_sz;
1151 ret = rte_eal_init(argc, argv);
1153 rte_panic("Cannot init EAL\n");
1158 config.prgname = argv[0];
1160 parse_opts(argc, argv);
1162 ret = check_config();
1164 rte_exit(-ret, "Bad configuration\n");
1166 af = ((config.flags & IPV6_FLAG) == 0) ? AF_INET : AF_INET6;
1167 depth_lim = (af == AF_INET) ? 32 : 128;
1168 rt_ent_sz = (af == AF_INET) ? sizeof(struct rt_rule_4) :
1169 sizeof(struct rt_rule_6);
1170 lookup_ent_sz = (af == AF_INET) ? 4 : 16;
1172 /* Count number of rules in file*/
1173 if (config.routes_file != NULL) {
1174 fr = fopen(config.routes_file, "r");
1176 rte_exit(-errno, "Can not open file with routes %s\n",
1177 config.routes_file);
1179 config.nb_routes = 0;
1180 while (fgets(line, sizeof(line), fr) != NULL)
1185 /* Count number of ip's in file*/
1186 if (config.lookup_ips_file != NULL) {
1187 fl = fopen(config.lookup_ips_file, "r");
1189 rte_exit(-errno, "Can not open file with ip's %s\n",
1190 config.lookup_ips_file);
1192 config.nb_lookup_ips = 0;
1193 while (fgets(line, sizeof(line), fl) != NULL)
1194 config.nb_lookup_ips++;
1198 /* Alloc routes table*/
1199 config.rt = rte_malloc(NULL, rt_ent_sz * config.nb_routes, 0);
1200 if (config.rt == NULL)
1201 rte_exit(-ENOMEM, "Can not alloc rt\n");
1203 /* Alloc table with ip's for lookup*/
1204 config.lookup_tbl = rte_malloc(NULL, lookup_ent_sz *
1205 config.nb_lookup_ips, 0);
1206 if (config.lookup_tbl == NULL)
1207 rte_exit(-ENOMEM, "Can not alloc lookup table\n");
1209 /* Fill routes table */
1211 if (distrib_string != NULL)
1212 ret = parse_distrib(depth_lim, config.nb_routes);
1214 uint8_t rpd[129] = {0};
1215 uint32_t nrpd[129] = {0};
1216 ret = complete_distrib(depth_lim, config.nb_routes,
1221 "Bad routes distribution configuration\n");
1222 if (af == AF_INET) {
1223 gen_random_rt_4(config.rt,
1224 __builtin_ctz(config.ent_sz));
1225 if (config.flags & SHUFFLE_FLAG)
1226 shuffle_rt_4(config.rt, config.nb_routes);
1228 gen_random_rt_6(config.rt,
1229 __builtin_ctz(config.ent_sz));
1230 if (config.flags & SHUFFLE_FLAG)
1231 shuffle_rt_6(config.rt, config.nb_routes);
1235 ret = parse_rt_4(fr);
1237 ret = parse_rt_6(fr);
1240 rte_exit(-ret, "failed to parse routes file %s\n",
1241 config.routes_file);
1245 /* Fill lookup table with ip's*/
1247 gen_rnd_lookup_tbl(af);
1249 ret = parse_lookup(fl, af);
1251 rte_exit(-ret, "failed to parse lookup file\n");