1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
4 * This file contain the application main file
5 * This application provides the user the ability to test the
6 * insertion rate for specific rte_flow rule under stress state ~4M rule/
8 * Then it will also provide packet per second measurement after installing
9 * all rules, the user may send traffic to test the PPS that match the rules
10 * after all rules are installed, to check performance or functionality after
13 * The flows insertion will go for all ports first, then it will print the
14 * results, after that the application will go into forwarding packets mode
15 * it will start receiving traffic if any and then forwarding it back and
16 * gives packet per second measurement.
32 #include <rte_malloc.h>
33 #include <rte_mempool.h>
35 #include <rte_ethdev.h>
41 #define MAX_ITERATIONS 100
42 #define DEFAULT_RULES_COUNT 4000000
43 #define DEFAULT_RULES_BATCH 100000
44 #define DEFAULT_GROUP 0
46 struct rte_flow *flow;
47 static uint8_t flow_group;
49 static uint64_t encap_data;
50 static uint64_t decap_data;
52 static uint64_t flow_items[MAX_ITEMS_NUM];
53 static uint64_t flow_actions[MAX_ACTIONS_NUM];
54 static uint64_t flow_attrs[MAX_ATTRS_NUM];
55 static uint8_t items_idx, actions_idx, attrs_idx;
57 static uint64_t ports_mask;
58 static volatile bool force_quit;
59 static bool dump_iterations;
60 static bool delete_flag;
61 static bool dump_socket_mem_flag;
62 static bool enable_fwd;
64 static struct rte_mempool *mbuf_mp;
65 static uint32_t nb_lcores;
66 static uint32_t rules_count;
67 static uint32_t rules_batch;
68 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
69 static uint32_t nb_lcores;
71 #define MAX_PKT_BURST 32
72 #define LCORE_MODE_PKT 1
73 #define LCORE_MODE_STATS 2
74 #define MAX_STREAMS 64
87 struct stream streams[MAX_STREAMS];
92 struct rte_mbuf *pkts[MAX_PKT_BURST];
93 } __rte_cache_aligned;
95 static struct lcore_info lcore_infos[MAX_LCORES];
100 printf("\nusage: %s\n", progname);
101 printf("\nControl configurations:\n");
102 printf(" --rules-count=N: to set the number of needed"
103 " rules to insert, default is %d\n", DEFAULT_RULES_COUNT);
104 printf(" --rules-batch=N: set number of batched rules,"
105 " default is %d\n", DEFAULT_RULES_BATCH);
106 printf(" --dump-iterations: To print rates for each"
108 printf(" --deletion-rate: Enable deletion rate"
110 printf(" --dump-socket-mem: To dump all socket memory\n");
111 printf(" --enable-fwd: To enable packets forwarding"
112 " after insertion\n");
113 printf(" --portmask=N: hexadecimal bitmask of ports used\n");
115 printf("To set flow attributes:\n");
116 printf(" --ingress: set ingress attribute in flows\n");
117 printf(" --egress: set egress attribute in flows\n");
118 printf(" --transfer: set transfer attribute in flows\n");
119 printf(" --group=N: set group for all flows,"
120 " default is %d\n", DEFAULT_GROUP);
122 printf("To set flow items:\n");
123 printf(" --ether: add ether layer in flow items\n");
124 printf(" --vlan: add vlan layer in flow items\n");
125 printf(" --ipv4: add ipv4 layer in flow items\n");
126 printf(" --ipv6: add ipv6 layer in flow items\n");
127 printf(" --tcp: add tcp layer in flow items\n");
128 printf(" --udp: add udp layer in flow items\n");
129 printf(" --vxlan: add vxlan layer in flow items\n");
130 printf(" --vxlan-gpe: add vxlan-gpe layer in flow items\n");
131 printf(" --gre: add gre layer in flow items\n");
132 printf(" --geneve: add geneve layer in flow items\n");
133 printf(" --gtp: add gtp layer in flow items\n");
134 printf(" --meta: add meta layer in flow items\n");
135 printf(" --tag: add tag layer in flow items\n");
136 printf(" --icmpv4: add icmpv4 layer in flow items\n");
137 printf(" --icmpv6: add icmpv6 layer in flow items\n");
139 printf("To set flow actions:\n");
140 printf(" --port-id: add port-id action in flow actions\n");
141 printf(" --rss: add rss action in flow actions\n");
142 printf(" --queue: add queue action in flow actions\n");
143 printf(" --jump: add jump action in flow actions\n");
144 printf(" --mark: add mark action in flow actions\n");
145 printf(" --count: add count action in flow actions\n");
146 printf(" --set-meta: add set meta action in flow actions\n");
147 printf(" --set-tag: add set tag action in flow actions\n");
148 printf(" --drop: add drop action in flow actions\n");
149 printf(" --hairpin-queue=N: add hairpin-queue action in flow actions\n");
150 printf(" --hairpin-rss=N: add hairpin-rss action in flow actions\n");
151 printf(" --set-src-mac: add set src mac action to flow actions\n"
152 "Src mac to be set is random each flow\n");
153 printf(" --set-dst-mac: add set dst mac action to flow actions\n"
154 "Dst mac to be set is random each flow\n");
155 printf(" --set-src-ipv4: add set src ipv4 action to flow actions\n"
156 "Src ipv4 to be set is random each flow\n");
157 printf(" --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
158 "Dst ipv4 to be set is random each flow\n");
159 printf(" --set-src-ipv6: add set src ipv6 action to flow actions\n"
160 "Src ipv6 to be set is random each flow\n");
161 printf(" --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
162 "Dst ipv6 to be set is random each flow\n");
163 printf(" --set-src-tp: add set src tp action to flow actions\n"
164 "Src tp to be set is random each flow\n");
165 printf(" --set-dst-tp: add set dst tp action to flow actions\n"
166 "Dst tp to be set is random each flow\n");
167 printf(" --inc-tcp-ack: add inc tcp ack action to flow actions\n"
168 "tcp ack will be increments by 1\n");
169 printf(" --dec-tcp-ack: add dec tcp ack action to flow actions\n"
170 "tcp ack will be decrements by 1\n");
171 printf(" --inc-tcp-seq: add inc tcp seq action to flow actions\n"
172 "tcp seq will be increments by 1\n");
173 printf(" --dec-tcp-seq: add dec tcp seq action to flow actions\n"
174 "tcp seq will be decrements by 1\n");
175 printf(" --set-ttl: add set ttl action to flow actions\n"
176 "L3 ttl to be set is random each flow\n");
177 printf(" --dec-ttl: add dec ttl action to flow actions\n"
178 "L3 ttl will be decrements by 1\n");
179 printf(" --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
180 "ipv4 dscp value to be set is random each flow\n");
181 printf(" --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
182 "ipv6 dscp value to be set is random each flow\n");
183 printf(" --flag: add flag action to flow actions\n");
184 printf(" --raw-encap=<data>: add raw encap action to flow actions\n"
185 "Data is the data needed to be encaped\n"
186 "Example: raw-encap=ether,ipv4,udp,vxlan\n");
187 printf(" --raw-decap=<data>: add raw decap action to flow actions\n"
188 "Data is the data needed to be decaped\n"
189 "Example: raw-decap=ether,ipv4,udp,vxlan\n");
190 printf(" --vxlan-encap: add vxlan-encap action to flow actions\n"
191 "Encapped data is fixed with pattern: ether,ipv4,udp,vxlan\n"
192 "With fixed values\n");
193 printf(" --vxlan-decap: add vxlan_decap action to flow actions\n");
197 args_parse(int argc, char **argv)
207 static const struct option_dict {
216 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
217 .map = &flow_items[0],
218 .map_idx = &items_idx
222 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
223 .map = &flow_items[0],
224 .map_idx = &items_idx
228 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
229 .map = &flow_items[0],
230 .map_idx = &items_idx
234 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
235 .map = &flow_items[0],
236 .map_idx = &items_idx
240 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
241 .map = &flow_items[0],
242 .map_idx = &items_idx
246 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
247 .map = &flow_items[0],
248 .map_idx = &items_idx
252 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
253 .map = &flow_items[0],
254 .map_idx = &items_idx
258 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
259 .map = &flow_items[0],
260 .map_idx = &items_idx
264 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
265 .map = &flow_items[0],
266 .map_idx = &items_idx
270 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
271 .map = &flow_items[0],
272 .map_idx = &items_idx
276 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
277 .map = &flow_items[0],
278 .map_idx = &items_idx
282 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
283 .map = &flow_items[0],
284 .map_idx = &items_idx
288 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
289 .map = &flow_items[0],
290 .map_idx = &items_idx
294 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP),
295 .map = &flow_items[0],
296 .map_idx = &items_idx
300 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP6),
301 .map = &flow_items[0],
302 .map_idx = &items_idx
307 .map = &flow_attrs[0],
308 .map_idx = &attrs_idx
313 .map = &flow_attrs[0],
314 .map_idx = &attrs_idx
319 .map = &flow_attrs[0],
320 .map_idx = &attrs_idx
324 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
325 .map = &flow_actions[0],
326 .map_idx = &actions_idx
330 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
331 .map = &flow_actions[0],
332 .map_idx = &actions_idx
336 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
337 .map = &flow_actions[0],
338 .map_idx = &actions_idx
342 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
343 .map = &flow_actions[0],
344 .map_idx = &actions_idx
348 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
349 .map = &flow_actions[0],
350 .map_idx = &actions_idx
354 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
355 .map = &flow_actions[0],
356 .map_idx = &actions_idx
360 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
361 .map = &flow_actions[0],
362 .map_idx = &actions_idx
366 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
367 .map = &flow_actions[0],
368 .map_idx = &actions_idx
372 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
373 .map = &flow_actions[0],
374 .map_idx = &actions_idx
377 .str = "set-src-mac",
378 .mask = FLOW_ACTION_MASK(
379 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
381 .map = &flow_actions[0],
382 .map_idx = &actions_idx
385 .str = "set-dst-mac",
386 .mask = FLOW_ACTION_MASK(
387 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
389 .map = &flow_actions[0],
390 .map_idx = &actions_idx
393 .str = "set-src-ipv4",
394 .mask = FLOW_ACTION_MASK(
395 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
397 .map = &flow_actions[0],
398 .map_idx = &actions_idx
401 .str = "set-dst-ipv4",
402 .mask = FLOW_ACTION_MASK(
403 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
405 .map = &flow_actions[0],
406 .map_idx = &actions_idx
409 .str = "set-src-ipv6",
410 .mask = FLOW_ACTION_MASK(
411 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
413 .map = &flow_actions[0],
414 .map_idx = &actions_idx
417 .str = "set-dst-ipv6",
418 .mask = FLOW_ACTION_MASK(
419 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
421 .map = &flow_actions[0],
422 .map_idx = &actions_idx
426 .mask = FLOW_ACTION_MASK(
427 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
429 .map = &flow_actions[0],
430 .map_idx = &actions_idx
434 .mask = FLOW_ACTION_MASK(
435 RTE_FLOW_ACTION_TYPE_SET_TP_DST
437 .map = &flow_actions[0],
438 .map_idx = &actions_idx
441 .str = "inc-tcp-ack",
442 .mask = FLOW_ACTION_MASK(
443 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
445 .map = &flow_actions[0],
446 .map_idx = &actions_idx
449 .str = "dec-tcp-ack",
450 .mask = FLOW_ACTION_MASK(
451 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
453 .map = &flow_actions[0],
454 .map_idx = &actions_idx
457 .str = "inc-tcp-seq",
458 .mask = FLOW_ACTION_MASK(
459 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
461 .map = &flow_actions[0],
462 .map_idx = &actions_idx
465 .str = "dec-tcp-seq",
466 .mask = FLOW_ACTION_MASK(
467 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
469 .map = &flow_actions[0],
470 .map_idx = &actions_idx
474 .mask = FLOW_ACTION_MASK(
475 RTE_FLOW_ACTION_TYPE_SET_TTL
477 .map = &flow_actions[0],
478 .map_idx = &actions_idx
482 .mask = FLOW_ACTION_MASK(
483 RTE_FLOW_ACTION_TYPE_DEC_TTL
485 .map = &flow_actions[0],
486 .map_idx = &actions_idx
489 .str = "set-ipv4-dscp",
490 .mask = FLOW_ACTION_MASK(
491 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
493 .map = &flow_actions[0],
494 .map_idx = &actions_idx
497 .str = "set-ipv6-dscp",
498 .mask = FLOW_ACTION_MASK(
499 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
501 .map = &flow_actions[0],
502 .map_idx = &actions_idx
506 .mask = FLOW_ACTION_MASK(
507 RTE_FLOW_ACTION_TYPE_FLAG
509 .map = &flow_actions[0],
510 .map_idx = &actions_idx
513 .str = "vxlan-encap",
514 .mask = FLOW_ACTION_MASK(
515 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP
517 .map = &flow_actions[0],
518 .map_idx = &actions_idx
521 .str = "vxlan-decap",
522 .mask = FLOW_ACTION_MASK(
523 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP
525 .map = &flow_actions[0],
526 .map_idx = &actions_idx
530 static const struct option lgopts[] = {
533 { "rules-count", 1, 0, 0 },
534 { "rules-batch", 1, 0, 0 },
535 { "dump-iterations", 0, 0, 0 },
536 { "deletion-rate", 0, 0, 0 },
537 { "dump-socket-mem", 0, 0, 0 },
538 { "enable-fwd", 0, 0, 0 },
539 { "portmask", 1, 0, 0 },
541 { "ingress", 0, 0, 0 },
542 { "egress", 0, 0, 0 },
543 { "transfer", 0, 0, 0 },
544 { "group", 1, 0, 0 },
546 { "ether", 0, 0, 0 },
552 { "vxlan", 0, 0, 0 },
553 { "vxlan-gpe", 0, 0, 0 },
555 { "geneve", 0, 0, 0 },
559 { "icmpv4", 0, 0, 0 },
560 { "icmpv6", 0, 0, 0 },
562 { "port-id", 0, 0, 0 },
564 { "queue", 0, 0, 0 },
567 { "count", 0, 0, 0 },
568 { "set-meta", 0, 0, 0 },
569 { "set-tag", 0, 0, 0 },
571 { "hairpin-queue", 1, 0, 0 },
572 { "hairpin-rss", 1, 0, 0 },
573 { "set-src-mac", 0, 0, 0 },
574 { "set-dst-mac", 0, 0, 0 },
575 { "set-src-ipv4", 0, 0, 0 },
576 { "set-dst-ipv4", 0, 0, 0 },
577 { "set-src-ipv6", 0, 0, 0 },
578 { "set-dst-ipv6", 0, 0, 0 },
579 { "set-src-tp", 0, 0, 0 },
580 { "set-dst-tp", 0, 0, 0 },
581 { "inc-tcp-ack", 0, 0, 0 },
582 { "dec-tcp-ack", 0, 0, 0 },
583 { "inc-tcp-seq", 0, 0, 0 },
584 { "dec-tcp-seq", 0, 0, 0 },
585 { "set-ttl", 0, 0, 0 },
586 { "dec-ttl", 0, 0, 0 },
587 { "set-ipv4-dscp", 0, 0, 0 },
588 { "set-ipv6-dscp", 0, 0, 0 },
590 { "raw-encap", 1, 0, 0 },
591 { "raw-decap", 1, 0, 0 },
592 { "vxlan-encap", 0, 0, 0 },
593 { "vxlan-decap", 0, 0, 0 },
596 RTE_ETH_FOREACH_DEV(i)
597 ports_mask |= 1 << i;
599 hairpin_queues_num = 0;
602 printf(":: Flow -> ");
603 while ((opt = getopt_long(argc, argvopt, "",
604 lgopts, &opt_idx)) != EOF) {
607 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
609 rte_exit(EXIT_SUCCESS, "Displayed help\n");
612 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
617 rte_exit(EXIT_SUCCESS,
618 "flow group should be >= 0\n");
619 printf("group %d / ", flow_group);
622 for (i = 0; i < RTE_DIM(flow_options); i++)
623 if (strcmp(lgopts[opt_idx].name,
624 flow_options[i].str) == 0) {
626 (*flow_options[i].map_idx)++] =
627 flow_options[i].mask;
628 printf("%s / ", flow_options[i].str);
631 if (strcmp(lgopts[opt_idx].name,
632 "hairpin-rss") == 0) {
635 hairpin_queues_num = n;
637 rte_exit(EXIT_SUCCESS,
638 "Hairpin queues should be > 0\n");
640 flow_actions[actions_idx++] =
642 printf("hairpin-rss / ");
644 if (strcmp(lgopts[opt_idx].name,
645 "hairpin-queue") == 0) {
648 hairpin_queues_num = n;
650 rte_exit(EXIT_SUCCESS,
651 "Hairpin queues should be > 0\n");
653 flow_actions[actions_idx++] =
654 HAIRPIN_QUEUE_ACTION;
655 printf("hairpin-queue / ");
658 if (strcmp(lgopts[opt_idx].name, "raw-encap") == 0) {
659 printf("raw-encap ");
660 flow_actions[actions_idx++] =
662 RTE_FLOW_ACTION_TYPE_RAW_ENCAP
665 token = strtok(optarg, ",");
666 while (token != NULL) {
667 for (i = 0; i < RTE_DIM(flow_options); i++) {
668 if (strcmp(flow_options[i].str, token) == 0) {
669 printf("%s,", token);
670 encap_data |= flow_options[i].mask;
673 /* Reached last item with no match */
674 if (i == (RTE_DIM(flow_options) - 1)) {
675 fprintf(stderr, "Invalid encap item: %s\n", token);
677 rte_exit(EXIT_SUCCESS, "Invalid encap item\n");
680 token = strtok(NULL, ",");
684 if (strcmp(lgopts[opt_idx].name, "raw-decap") == 0) {
685 printf("raw-decap ");
686 flow_actions[actions_idx++] =
688 RTE_FLOW_ACTION_TYPE_RAW_DECAP
691 token = strtok(optarg, ",");
692 while (token != NULL) {
693 for (i = 0; i < RTE_DIM(flow_options); i++) {
694 if (strcmp(flow_options[i].str, token) == 0) {
695 printf("%s,", token);
696 encap_data |= flow_options[i].mask;
699 /* Reached last item with no match */
700 if (i == (RTE_DIM(flow_options) - 1)) {
701 fprintf(stderr, "Invalid decap item: %s\n", token);
703 rte_exit(EXIT_SUCCESS, "Invalid decap item\n");
706 token = strtok(NULL, ",");
711 if (strcmp(lgopts[opt_idx].name,
712 "rules-batch") == 0) {
714 if (n >= DEFAULT_RULES_BATCH)
717 printf("\n\nrules_batch should be >= %d\n",
718 DEFAULT_RULES_BATCH);
719 rte_exit(EXIT_SUCCESS, " ");
722 if (strcmp(lgopts[opt_idx].name,
723 "rules-count") == 0) {
725 if (n >= (int) rules_batch)
728 printf("\n\nrules_count should be >= %d\n",
732 if (strcmp(lgopts[opt_idx].name,
733 "dump-iterations") == 0)
734 dump_iterations = true;
735 if (strcmp(lgopts[opt_idx].name,
736 "deletion-rate") == 0)
738 if (strcmp(lgopts[opt_idx].name,
739 "dump-socket-mem") == 0)
740 dump_socket_mem_flag = true;
741 if (strcmp(lgopts[opt_idx].name,
744 if (strcmp(lgopts[opt_idx].name,
746 /* parse hexadecimal string */
748 pm = strtoull(optarg, &end, 16);
749 if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
750 rte_exit(EXIT_FAILURE, "Invalid fwd port mask\n");
755 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
757 rte_exit(EXIT_SUCCESS, "Invalid option\n");
761 printf("end_flow\n");
764 /* Dump the socket memory statistics on console */
766 dump_socket_mem(FILE *f)
768 struct rte_malloc_socket_stats socket_stats;
773 unsigned int n_alloc = 0;
774 unsigned int n_free = 0;
775 bool active_nodes = false;
778 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
779 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
780 !socket_stats.heap_totalsz_bytes)
783 total += socket_stats.heap_totalsz_bytes;
784 alloc += socket_stats.heap_allocsz_bytes;
785 free += socket_stats.heap_freesz_bytes;
786 n_alloc += socket_stats.alloc_count;
787 n_free += socket_stats.free_count;
788 if (dump_socket_mem_flag) {
789 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
791 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
792 " %.6lf(%.3lf%%)\nfree: %.6lf"
794 "\ncount alloc: %u\nfree: %u\n",
796 socket_stats.heap_totalsz_bytes / 1.0e6,
797 socket_stats.heap_allocsz_bytes / 1.0e6,
798 (double)socket_stats.heap_allocsz_bytes * 100 /
799 (double)socket_stats.heap_totalsz_bytes,
800 socket_stats.heap_freesz_bytes / 1.0e6,
801 socket_stats.greatest_free_size / 1.0e6,
802 socket_stats.alloc_count,
803 socket_stats.free_count);
804 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
807 if (dump_socket_mem_flag && active_nodes) {
809 "\nTotal: size(M)\ntotal: %.6lf"
810 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
811 "\ncount alloc: %u\nfree: %u\n",
812 total / 1.0e6, alloc / 1.0e6,
813 (double)alloc * 100 / (double)total, free / 1.0e6,
815 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
821 print_flow_error(struct rte_flow_error error)
823 printf("Flow can't be created %d message: %s\n",
825 error.message ? error.message : "(no stated reason)");
829 destroy_flows(int port_id, struct rte_flow **flow_list)
831 struct rte_flow_error error;
832 clock_t start_iter, end_iter;
833 double cpu_time_used = 0;
835 double cpu_time_per_iter[MAX_ITERATIONS];
840 for (i = 0; i < MAX_ITERATIONS; i++)
841 cpu_time_per_iter[i] = -1;
843 if (rules_batch > rules_count)
844 rules_batch = rules_count;
847 printf("Flows Deletion on port = %d\n", port_id);
848 start_iter = clock();
849 for (i = 0; i < rules_count; i++) {
850 if (flow_list[i] == 0)
853 memset(&error, 0x33, sizeof(error));
854 if (rte_flow_destroy(port_id, flow_list[i], &error)) {
855 print_flow_error(error);
856 rte_exit(EXIT_FAILURE, "Error in deleting flow");
859 if (i && !((i + 1) % rules_batch)) {
860 /* Save the deletion rate of each iter */
862 delta = (double) (end_iter - start_iter);
863 iter_id = ((i + 1) / rules_batch) - 1;
864 cpu_time_per_iter[iter_id] =
865 delta / CLOCKS_PER_SEC;
866 cpu_time_used += cpu_time_per_iter[iter_id];
867 start_iter = clock();
871 /* Deletion rate per iteration */
873 for (i = 0; i < MAX_ITERATIONS; i++) {
874 if (cpu_time_per_iter[i] == -1)
876 delta = (double)(rules_batch /
877 cpu_time_per_iter[i]);
878 flows_rate = delta / 1000;
879 printf(":: Iteration #%d: %d flows "
880 "in %f sec[ Rate = %f K/Sec ]\n",
882 cpu_time_per_iter[i], flows_rate);
885 /* Deletion rate for all flows */
886 flows_rate = ((double) (rules_count / cpu_time_used) / 1000);
887 printf("\n:: Total flow deletion rate -> %f K/Sec\n",
889 printf(":: The time for deleting %d in flows %f seconds\n",
890 rules_count, cpu_time_used);
896 struct rte_flow **flow_list;
897 struct rte_flow_error error;
898 clock_t start_iter, end_iter;
899 double cpu_time_used;
901 double cpu_time_per_iter[MAX_ITERATIONS];
908 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
909 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
911 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
912 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
914 nr_ports = rte_eth_dev_count_avail();
916 for (i = 0; i < MAX_ITERATIONS; i++)
917 cpu_time_per_iter[i] = -1;
919 if (rules_batch > rules_count)
920 rules_batch = rules_count;
922 printf(":: Flows Count per port: %d\n", rules_count);
924 flow_list = rte_zmalloc("flow_list",
925 (sizeof(struct rte_flow *) * rules_count) + 1, 0);
926 if (flow_list == NULL)
927 rte_exit(EXIT_FAILURE, "No Memory available!");
929 for (port_id = 0; port_id < nr_ports; port_id++) {
930 /* If port outside portmask */
931 if (!((ports_mask >> port_id) & 0x1))
935 if (flow_group > 0) {
937 * Create global rule to jump into flow_group,
938 * this way the app will avoid the default rules.
941 * group 0 eth / end actions jump group <flow_group>
944 flow = generate_flow(port_id, 0, flow_attrs,
945 global_items, global_actions,
946 flow_group, 0, 0, 0, 0, &error);
949 print_flow_error(error);
950 rte_exit(EXIT_FAILURE, "error in creating flow");
952 flow_list[flow_index++] = flow;
956 printf("Flows insertion on port = %d\n", port_id);
957 start_iter = clock();
958 for (i = 0; i < rules_count; i++) {
959 flow = generate_flow(port_id, flow_group,
960 flow_attrs, flow_items, flow_actions,
961 JUMP_ACTION_TABLE, i,
963 encap_data, decap_data,
970 print_flow_error(error);
971 rte_exit(EXIT_FAILURE, "error in creating flow");
974 flow_list[flow_index++] = flow;
976 if (i && !((i + 1) % rules_batch)) {
977 /* Save the insertion rate of each iter */
979 delta = (double) (end_iter - start_iter);
980 iter_id = ((i + 1) / rules_batch) - 1;
981 cpu_time_per_iter[iter_id] =
982 delta / CLOCKS_PER_SEC;
983 cpu_time_used += cpu_time_per_iter[iter_id];
984 start_iter = clock();
988 /* Iteration rate per iteration */
990 for (i = 0; i < MAX_ITERATIONS; i++) {
991 if (cpu_time_per_iter[i] == -1)
993 delta = (double)(rules_batch /
994 cpu_time_per_iter[i]);
995 flows_rate = delta / 1000;
996 printf(":: Iteration #%d: %d flows "
997 "in %f sec[ Rate = %f K/Sec ]\n",
999 cpu_time_per_iter[i], flows_rate);
1002 /* Insertion rate for all flows */
1003 flows_rate = ((double) (rules_count / cpu_time_used) / 1000);
1004 printf("\n:: Total flow insertion rate -> %f K/Sec\n",
1006 printf(":: The time for creating %d in flows %f seconds\n",
1007 rules_count, cpu_time_used);
1010 destroy_flows(port_id, flow_list);
1015 signal_handler(int signum)
1017 if (signum == SIGINT || signum == SIGTERM) {
1018 printf("\n\nSignal %d received, preparing to exit...\n",
1020 printf("Error: Stats are wrong due to sudden signal!\n\n");
1025 static inline uint16_t
1026 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
1029 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
1035 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
1041 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
1042 li->tx_pkts += nr_tx;
1043 li->tx_drops += cnt - nr_tx;
1045 for (i = nr_tx; i < cnt; i++)
1046 rte_pktmbuf_free(li->pkts[i]);
1050 * Method to convert numbers into pretty numbers that easy
1051 * to read. The design here is to add comma after each three
1052 * digits and set all of this inside buffer.
1054 * For example if n = 1799321, the output will be
1055 * 1,799,321 after this method which is easier to read.
1058 pretty_number(uint64_t n, char *buf)
1065 sprintf(p[i], "%03d", (int)(n % 1000));
1070 sprintf(p[i++], "%d", (int)n);
1073 off += sprintf(buf + off, "%s,", p[i]);
1074 buf[strlen(buf) - 1] = '\0';
1080 packet_per_second_stats(void)
1082 struct lcore_info *old;
1083 struct lcore_info *li, *oli;
1087 old = rte_zmalloc("old",
1088 sizeof(struct lcore_info) * MAX_LCORES, 0);
1090 rte_exit(EXIT_FAILURE, "No Memory available!");
1092 memcpy(old, lcore_infos,
1093 sizeof(struct lcore_info) * MAX_LCORES);
1095 while (!force_quit) {
1096 uint64_t total_tx_pkts = 0;
1097 uint64_t total_rx_pkts = 0;
1098 uint64_t total_tx_drops = 0;
1099 uint64_t tx_delta, rx_delta, drops_delta;
1101 int nr_valid_core = 0;
1106 char go_up_nr_lines[16];
1108 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
1109 printf("%s\r", go_up_nr_lines);
1112 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
1113 printf("%6s %16s %16s %16s\n", "------", "----------------",
1114 "----------------", "----------------");
1116 for (i = 0; i < MAX_LCORES; i++) {
1117 li = &lcore_infos[i];
1119 if (li->mode != LCORE_MODE_PKT)
1122 tx_delta = li->tx_pkts - oli->tx_pkts;
1123 rx_delta = li->rx_pkts - oli->rx_pkts;
1124 drops_delta = li->tx_drops - oli->tx_drops;
1125 printf("%6d %16s %16s %16s\n", i,
1126 pretty_number(tx_delta, buf[0]),
1127 pretty_number(drops_delta, buf[1]),
1128 pretty_number(rx_delta, buf[2]));
1130 total_tx_pkts += tx_delta;
1131 total_rx_pkts += rx_delta;
1132 total_tx_drops += drops_delta;
1138 if (nr_valid_core > 1) {
1139 printf("%6s %16s %16s %16s\n", "total",
1140 pretty_number(total_tx_pkts, buf[0]),
1141 pretty_number(total_tx_drops, buf[1]),
1142 pretty_number(total_rx_pkts, buf[2]));
1146 memcpy(old, lcore_infos,
1147 sizeof(struct lcore_info) * MAX_LCORES);
1152 start_forwarding(void *data __rte_unused)
1154 int lcore = rte_lcore_id();
1157 struct lcore_info *li = &lcore_infos[lcore];
1162 if (li->mode == LCORE_MODE_STATS) {
1163 printf(":: started stats on lcore %u\n", lcore);
1164 packet_per_second_stats();
1169 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1170 if (li->streams[stream_id].rx_port == -1)
1174 li->streams[stream_id].rx_port,
1175 li->streams[stream_id].rx_queue);
1178 li->streams[stream_id].tx_port,
1179 li->streams[stream_id].tx_queue);
1185 init_lcore_info(void)
1193 int streams_per_core;
1194 int unassigned_streams;
1196 nr_port = rte_eth_dev_count_avail();
1198 /* First logical core is reserved for stats printing */
1199 lcore = rte_get_next_lcore(-1, 0, 0);
1200 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1203 * Initialize all cores
1204 * All cores at first must have -1 value in all streams
1205 * This means that this stream is not used, or not set
1208 for (i = 0; i < MAX_LCORES; i++)
1209 for (j = 0; j < MAX_STREAMS; j++) {
1210 lcore_infos[i].streams[j].tx_port = -1;
1211 lcore_infos[i].streams[j].rx_port = -1;
1212 lcore_infos[i].streams[j].tx_queue = -1;
1213 lcore_infos[i].streams[j].rx_queue = -1;
1214 lcore_infos[i].streams_nb = 0;
1218 * Calculate the total streams count.
1219 * Also distribute those streams count between the available
1220 * logical cores except first core, since it's reserved for
1223 nb_fwd_streams = nr_port * RXQ_NUM;
1224 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1225 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1226 lcore = rte_get_next_lcore(lcore, 0, 0);
1227 lcore_infos[lcore].streams_nb = 1;
1230 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1231 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1232 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1233 lcore = rte_get_next_lcore(lcore, 0, 0);
1234 lcore_infos[lcore].streams_nb = streams_per_core;
1235 if (unassigned_streams) {
1236 lcore_infos[lcore].streams_nb++;
1237 unassigned_streams--;
1243 * Set the streams for the cores according to each logical
1244 * core stream count.
1245 * The streams is built on the design of what received should
1246 * forward as well, this means that if you received packets on
1247 * port 0 queue 0 then the same queue should forward the
1248 * packets, using the same logical core.
1250 lcore = rte_get_next_lcore(-1, 0, 0);
1251 for (port = 0; port < nr_port; port++) {
1252 /* Create FWD stream */
1253 for (queue = 0; queue < RXQ_NUM; queue++) {
1254 if (!lcore_infos[lcore].streams_nb ||
1255 !(stream_id % lcore_infos[lcore].streams_nb)) {
1256 lcore = rte_get_next_lcore(lcore, 0, 0);
1257 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1260 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1261 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1262 lcore_infos[lcore].streams[stream_id].rx_port = port;
1263 lcore_infos[lcore].streams[stream_id].tx_port = port;
1268 /* Print all streams */
1269 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1270 for (i = 0; i < MAX_LCORES; i++)
1271 for (j = 0; j < MAX_STREAMS; j++) {
1272 /* No streams for this core */
1273 if (lcore_infos[i].streams[j].tx_port == -1)
1275 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1277 lcore_infos[i].streams[j].rx_port,
1278 lcore_infos[i].streams[j].rx_queue,
1279 lcore_infos[i].streams[j].tx_port,
1280 lcore_infos[i].streams[j].tx_queue);
1289 uint16_t hairpin_queue;
1293 struct rte_eth_hairpin_conf hairpin_conf = {
1296 struct rte_eth_conf port_conf = {
1302 struct rte_eth_txconf txq_conf;
1303 struct rte_eth_rxconf rxq_conf;
1304 struct rte_eth_dev_info dev_info;
1306 nr_queues = RXQ_NUM;
1307 if (hairpin_queues_num != 0)
1308 nr_queues = RXQ_NUM + hairpin_queues_num;
1310 nr_ports = rte_eth_dev_count_avail();
1312 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1314 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1315 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1318 if (mbuf_mp == NULL)
1319 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1321 for (port_id = 0; port_id < nr_ports; port_id++) {
1322 ret = rte_eth_dev_info_get(port_id, &dev_info);
1324 rte_exit(EXIT_FAILURE,
1325 "Error during getting device"
1326 " (port %u) info: %s\n",
1327 port_id, strerror(-ret));
1329 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1330 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1332 printf(":: initializing port: %d\n", port_id);
1334 ret = rte_eth_dev_configure(port_id, nr_queues,
1335 nr_queues, &port_conf);
1337 rte_exit(EXIT_FAILURE,
1338 ":: cannot configure device: err=%d, port=%u\n",
1341 rxq_conf = dev_info.default_rxconf;
1342 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1343 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1344 rte_eth_dev_socket_id(port_id),
1348 rte_exit(EXIT_FAILURE,
1349 ":: Rx queue setup failed: err=%d, port=%u\n",
1353 txq_conf = dev_info.default_txconf;
1354 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1355 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1356 rte_eth_dev_socket_id(port_id),
1359 rte_exit(EXIT_FAILURE,
1360 ":: Tx queue setup failed: err=%d, port=%u\n",
1364 /* Catch all packets from traffic generator. */
1365 ret = rte_eth_promiscuous_enable(port_id);
1367 rte_exit(EXIT_FAILURE,
1368 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1369 rte_strerror(-ret), port_id);
1371 if (hairpin_queues_num != 0) {
1373 * Configure peer which represents hairpin Tx.
1374 * Hairpin queue numbers start after standard queues
1375 * (RXQ_NUM and TXQ_NUM).
1377 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1378 hairpin_queue < nr_queues;
1379 hairpin_queue++, std_queue++) {
1380 hairpin_conf.peers[0].port = port_id;
1381 hairpin_conf.peers[0].queue =
1382 std_queue + TXQ_NUM;
1383 ret = rte_eth_rx_hairpin_queue_setup(
1384 port_id, hairpin_queue,
1385 NR_RXD, &hairpin_conf);
1387 rte_exit(EXIT_FAILURE,
1388 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1392 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1393 hairpin_queue < nr_queues;
1394 hairpin_queue++, std_queue++) {
1395 hairpin_conf.peers[0].port = port_id;
1396 hairpin_conf.peers[0].queue =
1397 std_queue + RXQ_NUM;
1398 ret = rte_eth_tx_hairpin_queue_setup(
1399 port_id, hairpin_queue,
1400 NR_TXD, &hairpin_conf);
1402 rte_exit(EXIT_FAILURE,
1403 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1408 ret = rte_eth_dev_start(port_id);
1410 rte_exit(EXIT_FAILURE,
1411 "rte_eth_dev_start:err=%d, port=%u\n",
1414 printf(":: initializing port: %d done\n", port_id);
1419 main(int argc, char **argv)
1423 struct rte_flow_error error;
1424 int64_t alloc, last_alloc;
1426 ret = rte_eal_init(argc, argv);
1428 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1431 dump_iterations = false;
1432 rules_count = DEFAULT_RULES_COUNT;
1433 rules_batch = DEFAULT_RULES_BATCH;
1434 delete_flag = false;
1435 dump_socket_mem_flag = false;
1436 flow_group = DEFAULT_GROUP;
1438 signal(SIGINT, signal_handler);
1439 signal(SIGTERM, signal_handler);
1444 args_parse(argc, argv);
1448 nb_lcores = rte_lcore_count();
1450 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1452 last_alloc = (int64_t)dump_socket_mem(stdout);
1454 alloc = (int64_t)dump_socket_mem(stdout);
1457 fprintf(stdout, ":: Memory allocation change(M): %.6lf\n",
1458 (alloc - last_alloc) / 1.0e6);
1462 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MAIN);
1465 RTE_ETH_FOREACH_DEV(port) {
1466 rte_flow_flush(port, &error);
1467 if (rte_eth_dev_stop(port) != 0)
1468 printf("Failed to stop device on port %u\n", port);
1469 rte_eth_dev_close(port);