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_ITERATION 100000
45 struct rte_flow *flow;
46 static uint8_t flow_group;
48 static uint64_t encap_data;
49 static uint64_t decap_data;
51 static uint64_t flow_items[MAX_ITEMS_NUM];
52 static uint64_t flow_actions[MAX_ACTIONS_NUM];
53 static uint64_t flow_attrs[MAX_ATTRS_NUM];
54 static uint8_t items_idx, actions_idx, attrs_idx;
56 static uint64_t ports_mask;
57 static volatile bool force_quit;
58 static bool dump_iterations;
59 static bool delete_flag;
60 static bool dump_socket_mem_flag;
61 static bool enable_fwd;
63 static struct rte_mempool *mbuf_mp;
64 static uint32_t nb_lcores;
65 static uint32_t flows_count;
66 static uint32_t iterations_number;
67 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
68 static uint32_t nb_lcores;
70 #define MAX_PKT_BURST 32
71 #define LCORE_MODE_PKT 1
72 #define LCORE_MODE_STATS 2
73 #define MAX_STREAMS 64
86 struct stream streams[MAX_STREAMS];
91 struct rte_mbuf *pkts[MAX_PKT_BURST];
92 } __rte_cache_aligned;
94 static struct lcore_info lcore_infos[MAX_LCORES];
99 printf("\nusage: %s\n", progname);
100 printf("\nControl configurations:\n");
101 printf(" --flows-count=N: to set the number of needed"
102 " flows to insert, default is 4,000,000\n");
103 printf(" --dump-iterations: To print rates for each"
105 printf(" --deletion-rate: Enable deletion rate"
107 printf(" --dump-socket-mem: To dump all socket memory\n");
108 printf(" --enable-fwd: To enable packets forwarding"
109 " after insertion\n");
110 printf(" --portmask=N: hexadecimal bitmask of ports used\n");
112 printf("To set flow attributes:\n");
113 printf(" --ingress: set ingress attribute in flows\n");
114 printf(" --egress: set egress attribute in flows\n");
115 printf(" --transfer: set transfer attribute in flows\n");
116 printf(" --group=N: set group for all flows,"
119 printf("To set flow items:\n");
120 printf(" --ether: add ether layer in flow items\n");
121 printf(" --vlan: add vlan layer in flow items\n");
122 printf(" --ipv4: add ipv4 layer in flow items\n");
123 printf(" --ipv6: add ipv6 layer in flow items\n");
124 printf(" --tcp: add tcp layer in flow items\n");
125 printf(" --udp: add udp layer in flow items\n");
126 printf(" --vxlan: add vxlan layer in flow items\n");
127 printf(" --vxlan-gpe: add vxlan-gpe layer in flow items\n");
128 printf(" --gre: add gre layer in flow items\n");
129 printf(" --geneve: add geneve layer in flow items\n");
130 printf(" --gtp: add gtp layer in flow items\n");
131 printf(" --meta: add meta layer in flow items\n");
132 printf(" --tag: add tag layer in flow items\n");
133 printf(" --icmpv4: add icmpv4 layer in flow items\n");
134 printf(" --icmpv6: add icmpv6 layer in flow items\n");
136 printf("To set flow actions:\n");
137 printf(" --port-id: add port-id action in flow actions\n");
138 printf(" --rss: add rss action in flow actions\n");
139 printf(" --queue: add queue action in flow actions\n");
140 printf(" --jump: add jump action in flow actions\n");
141 printf(" --mark: add mark action in flow actions\n");
142 printf(" --count: add count action in flow actions\n");
143 printf(" --set-meta: add set meta action in flow actions\n");
144 printf(" --set-tag: add set tag action in flow actions\n");
145 printf(" --drop: add drop action in flow actions\n");
146 printf(" --hairpin-queue=N: add hairpin-queue action in flow actions\n");
147 printf(" --hairpin-rss=N: add hairpin-rss action in flow actions\n");
148 printf(" --set-src-mac: add set src mac action to flow actions\n"
149 "Src mac to be set is random each flow\n");
150 printf(" --set-dst-mac: add set dst mac action to flow actions\n"
151 "Dst mac to be set is random each flow\n");
152 printf(" --set-src-ipv4: add set src ipv4 action to flow actions\n"
153 "Src ipv4 to be set is random each flow\n");
154 printf(" --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
155 "Dst ipv4 to be set is random each flow\n");
156 printf(" --set-src-ipv6: add set src ipv6 action to flow actions\n"
157 "Src ipv6 to be set is random each flow\n");
158 printf(" --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
159 "Dst ipv6 to be set is random each flow\n");
160 printf(" --set-src-tp: add set src tp action to flow actions\n"
161 "Src tp to be set is random each flow\n");
162 printf(" --set-dst-tp: add set dst tp action to flow actions\n"
163 "Dst tp to be set is random each flow\n");
164 printf(" --inc-tcp-ack: add inc tcp ack action to flow actions\n"
165 "tcp ack will be increments by 1\n");
166 printf(" --dec-tcp-ack: add dec tcp ack action to flow actions\n"
167 "tcp ack will be decrements by 1\n");
168 printf(" --inc-tcp-seq: add inc tcp seq action to flow actions\n"
169 "tcp seq will be increments by 1\n");
170 printf(" --dec-tcp-seq: add dec tcp seq action to flow actions\n"
171 "tcp seq will be decrements by 1\n");
172 printf(" --set-ttl: add set ttl action to flow actions\n"
173 "L3 ttl to be set is random each flow\n");
174 printf(" --dec-ttl: add dec ttl action to flow actions\n"
175 "L3 ttl will be decrements by 1\n");
176 printf(" --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
177 "ipv4 dscp value to be set is random each flow\n");
178 printf(" --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
179 "ipv6 dscp value to be set is random each flow\n");
180 printf(" --flag: add flag action to flow actions\n");
181 printf(" --raw-encap=<data>: add raw encap action to flow actions\n"
182 "Data is the data needed to be encaped\n"
183 "Example: raw-encap=ether,ipv4,udp,vxlan\n");
184 printf(" --raw-decap=<data>: add raw decap action to flow actions\n"
185 "Data is the data needed to be decaped\n"
186 "Example: raw-decap=ether,ipv4,udp,vxlan\n");
187 printf(" --vxlan-encap: add vxlan-encap action to flow actions\n"
188 "Encapped data is fixed with pattern: ether,ipv4,udp,vxlan\n"
189 "With fixed values\n");
190 printf(" --vxlan-decap: add vxlan_decap action to flow actions\n");
194 args_parse(int argc, char **argv)
204 static const struct option_dict {
213 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
214 .map = &flow_items[0],
215 .map_idx = &items_idx
219 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
220 .map = &flow_items[0],
221 .map_idx = &items_idx
225 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
226 .map = &flow_items[0],
227 .map_idx = &items_idx
231 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
232 .map = &flow_items[0],
233 .map_idx = &items_idx
237 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
238 .map = &flow_items[0],
239 .map_idx = &items_idx
243 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
244 .map = &flow_items[0],
245 .map_idx = &items_idx
249 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
250 .map = &flow_items[0],
251 .map_idx = &items_idx
255 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
256 .map = &flow_items[0],
257 .map_idx = &items_idx
261 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
262 .map = &flow_items[0],
263 .map_idx = &items_idx
267 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
268 .map = &flow_items[0],
269 .map_idx = &items_idx
273 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
274 .map = &flow_items[0],
275 .map_idx = &items_idx
279 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
280 .map = &flow_items[0],
281 .map_idx = &items_idx
285 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
286 .map = &flow_items[0],
287 .map_idx = &items_idx
291 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP),
292 .map = &flow_items[0],
293 .map_idx = &items_idx
297 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP6),
298 .map = &flow_items[0],
299 .map_idx = &items_idx
304 .map = &flow_attrs[0],
305 .map_idx = &attrs_idx
310 .map = &flow_attrs[0],
311 .map_idx = &attrs_idx
316 .map = &flow_attrs[0],
317 .map_idx = &attrs_idx
321 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
322 .map = &flow_actions[0],
323 .map_idx = &actions_idx
327 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
328 .map = &flow_actions[0],
329 .map_idx = &actions_idx
333 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
334 .map = &flow_actions[0],
335 .map_idx = &actions_idx
339 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
340 .map = &flow_actions[0],
341 .map_idx = &actions_idx
345 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
346 .map = &flow_actions[0],
347 .map_idx = &actions_idx
351 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
352 .map = &flow_actions[0],
353 .map_idx = &actions_idx
357 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
358 .map = &flow_actions[0],
359 .map_idx = &actions_idx
363 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
364 .map = &flow_actions[0],
365 .map_idx = &actions_idx
369 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
370 .map = &flow_actions[0],
371 .map_idx = &actions_idx
374 .str = "set-src-mac",
375 .mask = FLOW_ACTION_MASK(
376 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
378 .map = &flow_actions[0],
379 .map_idx = &actions_idx
382 .str = "set-dst-mac",
383 .mask = FLOW_ACTION_MASK(
384 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
386 .map = &flow_actions[0],
387 .map_idx = &actions_idx
390 .str = "set-src-ipv4",
391 .mask = FLOW_ACTION_MASK(
392 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
394 .map = &flow_actions[0],
395 .map_idx = &actions_idx
398 .str = "set-dst-ipv4",
399 .mask = FLOW_ACTION_MASK(
400 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
402 .map = &flow_actions[0],
403 .map_idx = &actions_idx
406 .str = "set-src-ipv6",
407 .mask = FLOW_ACTION_MASK(
408 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
410 .map = &flow_actions[0],
411 .map_idx = &actions_idx
414 .str = "set-dst-ipv6",
415 .mask = FLOW_ACTION_MASK(
416 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
418 .map = &flow_actions[0],
419 .map_idx = &actions_idx
423 .mask = FLOW_ACTION_MASK(
424 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
426 .map = &flow_actions[0],
427 .map_idx = &actions_idx
431 .mask = FLOW_ACTION_MASK(
432 RTE_FLOW_ACTION_TYPE_SET_TP_DST
434 .map = &flow_actions[0],
435 .map_idx = &actions_idx
438 .str = "inc-tcp-ack",
439 .mask = FLOW_ACTION_MASK(
440 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
442 .map = &flow_actions[0],
443 .map_idx = &actions_idx
446 .str = "dec-tcp-ack",
447 .mask = FLOW_ACTION_MASK(
448 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
450 .map = &flow_actions[0],
451 .map_idx = &actions_idx
454 .str = "inc-tcp-seq",
455 .mask = FLOW_ACTION_MASK(
456 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
458 .map = &flow_actions[0],
459 .map_idx = &actions_idx
462 .str = "dec-tcp-seq",
463 .mask = FLOW_ACTION_MASK(
464 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
466 .map = &flow_actions[0],
467 .map_idx = &actions_idx
471 .mask = FLOW_ACTION_MASK(
472 RTE_FLOW_ACTION_TYPE_SET_TTL
474 .map = &flow_actions[0],
475 .map_idx = &actions_idx
479 .mask = FLOW_ACTION_MASK(
480 RTE_FLOW_ACTION_TYPE_DEC_TTL
482 .map = &flow_actions[0],
483 .map_idx = &actions_idx
486 .str = "set-ipv4-dscp",
487 .mask = FLOW_ACTION_MASK(
488 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
490 .map = &flow_actions[0],
491 .map_idx = &actions_idx
494 .str = "set-ipv6-dscp",
495 .mask = FLOW_ACTION_MASK(
496 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
498 .map = &flow_actions[0],
499 .map_idx = &actions_idx
503 .mask = FLOW_ACTION_MASK(
504 RTE_FLOW_ACTION_TYPE_FLAG
506 .map = &flow_actions[0],
507 .map_idx = &actions_idx
510 .str = "vxlan-encap",
511 .mask = FLOW_ACTION_MASK(
512 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP
514 .map = &flow_actions[0],
515 .map_idx = &actions_idx
518 .str = "vxlan-decap",
519 .mask = FLOW_ACTION_MASK(
520 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP
522 .map = &flow_actions[0],
523 .map_idx = &actions_idx
527 static const struct option lgopts[] = {
530 { "flows-count", 1, 0, 0 },
531 { "dump-iterations", 0, 0, 0 },
532 { "deletion-rate", 0, 0, 0 },
533 { "dump-socket-mem", 0, 0, 0 },
534 { "enable-fwd", 0, 0, 0 },
535 { "portmask", 1, 0, 0 },
537 { "ingress", 0, 0, 0 },
538 { "egress", 0, 0, 0 },
539 { "transfer", 0, 0, 0 },
540 { "group", 1, 0, 0 },
542 { "ether", 0, 0, 0 },
548 { "vxlan", 0, 0, 0 },
549 { "vxlan-gpe", 0, 0, 0 },
551 { "geneve", 0, 0, 0 },
555 { "icmpv4", 0, 0, 0 },
556 { "icmpv6", 0, 0, 0 },
558 { "port-id", 0, 0, 0 },
560 { "queue", 0, 0, 0 },
563 { "count", 0, 0, 0 },
564 { "set-meta", 0, 0, 0 },
565 { "set-tag", 0, 0, 0 },
567 { "hairpin-queue", 1, 0, 0 },
568 { "hairpin-rss", 1, 0, 0 },
569 { "set-src-mac", 0, 0, 0 },
570 { "set-dst-mac", 0, 0, 0 },
571 { "set-src-ipv4", 0, 0, 0 },
572 { "set-dst-ipv4", 0, 0, 0 },
573 { "set-src-ipv6", 0, 0, 0 },
574 { "set-dst-ipv6", 0, 0, 0 },
575 { "set-src-tp", 0, 0, 0 },
576 { "set-dst-tp", 0, 0, 0 },
577 { "inc-tcp-ack", 0, 0, 0 },
578 { "dec-tcp-ack", 0, 0, 0 },
579 { "inc-tcp-seq", 0, 0, 0 },
580 { "dec-tcp-seq", 0, 0, 0 },
581 { "set-ttl", 0, 0, 0 },
582 { "dec-ttl", 0, 0, 0 },
583 { "set-ipv4-dscp", 0, 0, 0 },
584 { "set-ipv6-dscp", 0, 0, 0 },
586 { "raw-encap", 1, 0, 0 },
587 { "raw-decap", 1, 0, 0 },
588 { "vxlan-encap", 0, 0, 0 },
589 { "vxlan-decap", 0, 0, 0 },
592 RTE_ETH_FOREACH_DEV(i)
593 ports_mask |= 1 << i;
595 hairpin_queues_num = 0;
598 printf(":: Flow -> ");
599 while ((opt = getopt_long(argc, argvopt, "",
600 lgopts, &opt_idx)) != EOF) {
603 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
605 rte_exit(EXIT_SUCCESS, "Displayed help\n");
608 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
613 rte_exit(EXIT_SUCCESS,
614 "flow group should be >= 0\n");
615 printf("group %d / ", flow_group);
618 for (i = 0; i < RTE_DIM(flow_options); i++)
619 if (strcmp(lgopts[opt_idx].name,
620 flow_options[i].str) == 0) {
622 (*flow_options[i].map_idx)++] =
623 flow_options[i].mask;
624 printf("%s / ", flow_options[i].str);
627 if (strcmp(lgopts[opt_idx].name,
628 "hairpin-rss") == 0) {
631 hairpin_queues_num = n;
633 rte_exit(EXIT_SUCCESS,
634 "Hairpin queues should be > 0\n");
636 flow_actions[actions_idx++] =
638 printf("hairpin-rss / ");
640 if (strcmp(lgopts[opt_idx].name,
641 "hairpin-queue") == 0) {
644 hairpin_queues_num = n;
646 rte_exit(EXIT_SUCCESS,
647 "Hairpin queues should be > 0\n");
649 flow_actions[actions_idx++] =
650 HAIRPIN_QUEUE_ACTION;
651 printf("hairpin-queue / ");
654 if (strcmp(lgopts[opt_idx].name, "raw-encap") == 0) {
655 printf("raw-encap ");
656 flow_actions[actions_idx++] =
658 RTE_FLOW_ACTION_TYPE_RAW_ENCAP
661 token = strtok(optarg, ",");
662 while (token != NULL) {
663 for (i = 0; i < RTE_DIM(flow_options); i++) {
664 if (strcmp(flow_options[i].str, token) == 0) {
665 printf("%s,", token);
666 encap_data |= flow_options[i].mask;
669 /* Reached last item with no match */
670 if (i == (RTE_DIM(flow_options) - 1)) {
671 fprintf(stderr, "Invalid encap item: %s\n", token);
673 rte_exit(EXIT_SUCCESS, "Invalid encap item\n");
676 token = strtok(NULL, ",");
680 if (strcmp(lgopts[opt_idx].name, "raw-decap") == 0) {
681 printf("raw-decap ");
682 flow_actions[actions_idx++] =
684 RTE_FLOW_ACTION_TYPE_RAW_DECAP
687 token = strtok(optarg, ",");
688 while (token != NULL) {
689 for (i = 0; i < RTE_DIM(flow_options); i++) {
690 if (strcmp(flow_options[i].str, token) == 0) {
691 printf("%s,", token);
692 encap_data |= flow_options[i].mask;
695 /* Reached last item with no match */
696 if (i == (RTE_DIM(flow_options) - 1)) {
697 fprintf(stderr, "Invalid decap item: %s\n", token);
699 rte_exit(EXIT_SUCCESS, "Invalid decap item\n");
702 token = strtok(NULL, ",");
707 if (strcmp(lgopts[opt_idx].name,
708 "flows-count") == 0) {
710 if (n > (int) iterations_number)
713 printf("\n\nflows_count should be > %d\n",
715 rte_exit(EXIT_SUCCESS, " ");
718 if (strcmp(lgopts[opt_idx].name,
719 "dump-iterations") == 0)
720 dump_iterations = true;
721 if (strcmp(lgopts[opt_idx].name,
722 "deletion-rate") == 0)
724 if (strcmp(lgopts[opt_idx].name,
725 "dump-socket-mem") == 0)
726 dump_socket_mem_flag = true;
727 if (strcmp(lgopts[opt_idx].name,
730 if (strcmp(lgopts[opt_idx].name,
732 /* parse hexadecimal string */
734 pm = strtoull(optarg, &end, 16);
735 if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
736 rte_exit(EXIT_FAILURE, "Invalid fwd port mask\n");
741 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
743 rte_exit(EXIT_SUCCESS, "Invalid option\n");
747 printf("end_flow\n");
750 /* Dump the socket memory statistics on console */
752 dump_socket_mem(FILE *f)
754 struct rte_malloc_socket_stats socket_stats;
759 unsigned int n_alloc = 0;
760 unsigned int n_free = 0;
761 bool active_nodes = false;
764 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
765 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
766 !socket_stats.heap_totalsz_bytes)
769 total += socket_stats.heap_totalsz_bytes;
770 alloc += socket_stats.heap_allocsz_bytes;
771 free += socket_stats.heap_freesz_bytes;
772 n_alloc += socket_stats.alloc_count;
773 n_free += socket_stats.free_count;
774 if (dump_socket_mem_flag) {
775 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
777 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
778 " %.6lf(%.3lf%%)\nfree: %.6lf"
780 "\ncount alloc: %u\nfree: %u\n",
782 socket_stats.heap_totalsz_bytes / 1.0e6,
783 socket_stats.heap_allocsz_bytes / 1.0e6,
784 (double)socket_stats.heap_allocsz_bytes * 100 /
785 (double)socket_stats.heap_totalsz_bytes,
786 socket_stats.heap_freesz_bytes / 1.0e6,
787 socket_stats.greatest_free_size / 1.0e6,
788 socket_stats.alloc_count,
789 socket_stats.free_count);
790 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
793 if (dump_socket_mem_flag && active_nodes) {
795 "\nTotal: size(M)\ntotal: %.6lf"
796 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
797 "\ncount alloc: %u\nfree: %u\n",
798 total / 1.0e6, alloc / 1.0e6,
799 (double)alloc * 100 / (double)total, free / 1.0e6,
801 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
807 print_flow_error(struct rte_flow_error error)
809 printf("Flow can't be created %d message: %s\n",
811 error.message ? error.message : "(no stated reason)");
815 destroy_flows(int port_id, struct rte_flow **flow_list)
817 struct rte_flow_error error;
818 clock_t start_iter, end_iter;
819 double cpu_time_used = 0;
821 double cpu_time_per_iter[MAX_ITERATIONS];
826 for (i = 0; i < MAX_ITERATIONS; i++)
827 cpu_time_per_iter[i] = -1;
829 if (iterations_number > flows_count)
830 iterations_number = flows_count;
833 printf("Flows Deletion on port = %d\n", port_id);
834 start_iter = clock();
835 for (i = 0; i < flows_count; i++) {
836 if (flow_list[i] == 0)
839 memset(&error, 0x33, sizeof(error));
840 if (rte_flow_destroy(port_id, flow_list[i], &error)) {
841 print_flow_error(error);
842 rte_exit(EXIT_FAILURE, "Error in deleting flow");
845 if (i && !((i + 1) % iterations_number)) {
846 /* Save the deletion rate of each iter */
848 delta = (double) (end_iter - start_iter);
849 iter_id = ((i + 1) / iterations_number) - 1;
850 cpu_time_per_iter[iter_id] =
851 delta / CLOCKS_PER_SEC;
852 cpu_time_used += cpu_time_per_iter[iter_id];
853 start_iter = clock();
857 /* Deletion rate per iteration */
859 for (i = 0; i < MAX_ITERATIONS; i++) {
860 if (cpu_time_per_iter[i] == -1)
862 delta = (double)(iterations_number /
863 cpu_time_per_iter[i]);
864 flows_rate = delta / 1000;
865 printf(":: Iteration #%d: %d flows "
866 "in %f sec[ Rate = %f K/Sec ]\n",
867 i, iterations_number,
868 cpu_time_per_iter[i], flows_rate);
871 /* Deletion rate for all flows */
872 flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
873 printf("\n:: Total flow deletion rate -> %f K/Sec\n",
875 printf(":: The time for deleting %d in flows %f seconds\n",
876 flows_count, cpu_time_used);
882 struct rte_flow **flow_list;
883 struct rte_flow_error error;
884 clock_t start_iter, end_iter;
885 double cpu_time_used;
887 double cpu_time_per_iter[MAX_ITERATIONS];
894 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
895 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
897 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
898 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
900 nr_ports = rte_eth_dev_count_avail();
902 for (i = 0; i < MAX_ITERATIONS; i++)
903 cpu_time_per_iter[i] = -1;
905 if (iterations_number > flows_count)
906 iterations_number = flows_count;
908 printf(":: Flows Count per port: %d\n", flows_count);
910 flow_list = rte_zmalloc("flow_list",
911 (sizeof(struct rte_flow *) * flows_count) + 1, 0);
912 if (flow_list == NULL)
913 rte_exit(EXIT_FAILURE, "No Memory available!");
915 for (port_id = 0; port_id < nr_ports; port_id++) {
916 /* If port outside portmask */
917 if (!((ports_mask >> port_id) & 0x1))
921 if (flow_group > 0) {
923 * Create global rule to jump into flow_group,
924 * this way the app will avoid the default rules.
927 * group 0 eth / end actions jump group <flow_group>
930 flow = generate_flow(port_id, 0, flow_attrs,
931 global_items, global_actions,
932 flow_group, 0, 0, 0, 0, &error);
935 print_flow_error(error);
936 rte_exit(EXIT_FAILURE, "error in creating flow");
938 flow_list[flow_index++] = flow;
942 printf("Flows insertion on port = %d\n", port_id);
943 start_iter = clock();
944 for (i = 0; i < flows_count; i++) {
945 flow = generate_flow(port_id, flow_group,
946 flow_attrs, flow_items, flow_actions,
947 JUMP_ACTION_TABLE, i,
949 encap_data, decap_data,
956 print_flow_error(error);
957 rte_exit(EXIT_FAILURE, "error in creating flow");
960 flow_list[flow_index++] = flow;
962 if (i && !((i + 1) % iterations_number)) {
963 /* Save the insertion rate of each iter */
965 delta = (double) (end_iter - start_iter);
966 iter_id = ((i + 1) / iterations_number) - 1;
967 cpu_time_per_iter[iter_id] =
968 delta / CLOCKS_PER_SEC;
969 cpu_time_used += cpu_time_per_iter[iter_id];
970 start_iter = clock();
974 /* Iteration rate per iteration */
976 for (i = 0; i < MAX_ITERATIONS; i++) {
977 if (cpu_time_per_iter[i] == -1)
979 delta = (double)(iterations_number /
980 cpu_time_per_iter[i]);
981 flows_rate = delta / 1000;
982 printf(":: Iteration #%d: %d flows "
983 "in %f sec[ Rate = %f K/Sec ]\n",
984 i, iterations_number,
985 cpu_time_per_iter[i], flows_rate);
988 /* Insertion rate for all flows */
989 flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
990 printf("\n:: Total flow insertion rate -> %f K/Sec\n",
992 printf(":: The time for creating %d in flows %f seconds\n",
993 flows_count, cpu_time_used);
996 destroy_flows(port_id, flow_list);
1001 signal_handler(int signum)
1003 if (signum == SIGINT || signum == SIGTERM) {
1004 printf("\n\nSignal %d received, preparing to exit...\n",
1006 printf("Error: Stats are wrong due to sudden signal!\n\n");
1011 static inline uint16_t
1012 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
1015 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
1021 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
1027 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
1028 li->tx_pkts += nr_tx;
1029 li->tx_drops += cnt - nr_tx;
1031 for (i = nr_tx; i < cnt; i++)
1032 rte_pktmbuf_free(li->pkts[i]);
1036 * Method to convert numbers into pretty numbers that easy
1037 * to read. The design here is to add comma after each three
1038 * digits and set all of this inside buffer.
1040 * For example if n = 1799321, the output will be
1041 * 1,799,321 after this method which is easier to read.
1044 pretty_number(uint64_t n, char *buf)
1051 sprintf(p[i], "%03d", (int)(n % 1000));
1056 sprintf(p[i++], "%d", (int)n);
1059 off += sprintf(buf + off, "%s,", p[i]);
1060 buf[strlen(buf) - 1] = '\0';
1066 packet_per_second_stats(void)
1068 struct lcore_info *old;
1069 struct lcore_info *li, *oli;
1073 old = rte_zmalloc("old",
1074 sizeof(struct lcore_info) * MAX_LCORES, 0);
1076 rte_exit(EXIT_FAILURE, "No Memory available!");
1078 memcpy(old, lcore_infos,
1079 sizeof(struct lcore_info) * MAX_LCORES);
1081 while (!force_quit) {
1082 uint64_t total_tx_pkts = 0;
1083 uint64_t total_rx_pkts = 0;
1084 uint64_t total_tx_drops = 0;
1085 uint64_t tx_delta, rx_delta, drops_delta;
1087 int nr_valid_core = 0;
1092 char go_up_nr_lines[16];
1094 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
1095 printf("%s\r", go_up_nr_lines);
1098 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
1099 printf("%6s %16s %16s %16s\n", "------", "----------------",
1100 "----------------", "----------------");
1102 for (i = 0; i < MAX_LCORES; i++) {
1103 li = &lcore_infos[i];
1105 if (li->mode != LCORE_MODE_PKT)
1108 tx_delta = li->tx_pkts - oli->tx_pkts;
1109 rx_delta = li->rx_pkts - oli->rx_pkts;
1110 drops_delta = li->tx_drops - oli->tx_drops;
1111 printf("%6d %16s %16s %16s\n", i,
1112 pretty_number(tx_delta, buf[0]),
1113 pretty_number(drops_delta, buf[1]),
1114 pretty_number(rx_delta, buf[2]));
1116 total_tx_pkts += tx_delta;
1117 total_rx_pkts += rx_delta;
1118 total_tx_drops += drops_delta;
1124 if (nr_valid_core > 1) {
1125 printf("%6s %16s %16s %16s\n", "total",
1126 pretty_number(total_tx_pkts, buf[0]),
1127 pretty_number(total_tx_drops, buf[1]),
1128 pretty_number(total_rx_pkts, buf[2]));
1132 memcpy(old, lcore_infos,
1133 sizeof(struct lcore_info) * MAX_LCORES);
1138 start_forwarding(void *data __rte_unused)
1140 int lcore = rte_lcore_id();
1143 struct lcore_info *li = &lcore_infos[lcore];
1148 if (li->mode == LCORE_MODE_STATS) {
1149 printf(":: started stats on lcore %u\n", lcore);
1150 packet_per_second_stats();
1155 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1156 if (li->streams[stream_id].rx_port == -1)
1160 li->streams[stream_id].rx_port,
1161 li->streams[stream_id].rx_queue);
1164 li->streams[stream_id].tx_port,
1165 li->streams[stream_id].tx_queue);
1171 init_lcore_info(void)
1179 int streams_per_core;
1180 int unassigned_streams;
1182 nr_port = rte_eth_dev_count_avail();
1184 /* First logical core is reserved for stats printing */
1185 lcore = rte_get_next_lcore(-1, 0, 0);
1186 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1189 * Initialize all cores
1190 * All cores at first must have -1 value in all streams
1191 * This means that this stream is not used, or not set
1194 for (i = 0; i < MAX_LCORES; i++)
1195 for (j = 0; j < MAX_STREAMS; j++) {
1196 lcore_infos[i].streams[j].tx_port = -1;
1197 lcore_infos[i].streams[j].rx_port = -1;
1198 lcore_infos[i].streams[j].tx_queue = -1;
1199 lcore_infos[i].streams[j].rx_queue = -1;
1200 lcore_infos[i].streams_nb = 0;
1204 * Calculate the total streams count.
1205 * Also distribute those streams count between the available
1206 * logical cores except first core, since it's reserved for
1209 nb_fwd_streams = nr_port * RXQ_NUM;
1210 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1211 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1212 lcore = rte_get_next_lcore(lcore, 0, 0);
1213 lcore_infos[lcore].streams_nb = 1;
1216 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1217 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1218 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1219 lcore = rte_get_next_lcore(lcore, 0, 0);
1220 lcore_infos[lcore].streams_nb = streams_per_core;
1221 if (unassigned_streams) {
1222 lcore_infos[lcore].streams_nb++;
1223 unassigned_streams--;
1229 * Set the streams for the cores according to each logical
1230 * core stream count.
1231 * The streams is built on the design of what received should
1232 * forward as well, this means that if you received packets on
1233 * port 0 queue 0 then the same queue should forward the
1234 * packets, using the same logical core.
1236 lcore = rte_get_next_lcore(-1, 0, 0);
1237 for (port = 0; port < nr_port; port++) {
1238 /* Create FWD stream */
1239 for (queue = 0; queue < RXQ_NUM; queue++) {
1240 if (!lcore_infos[lcore].streams_nb ||
1241 !(stream_id % lcore_infos[lcore].streams_nb)) {
1242 lcore = rte_get_next_lcore(lcore, 0, 0);
1243 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1246 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1247 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1248 lcore_infos[lcore].streams[stream_id].rx_port = port;
1249 lcore_infos[lcore].streams[stream_id].tx_port = port;
1254 /* Print all streams */
1255 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1256 for (i = 0; i < MAX_LCORES; i++)
1257 for (j = 0; j < MAX_STREAMS; j++) {
1258 /* No streams for this core */
1259 if (lcore_infos[i].streams[j].tx_port == -1)
1261 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1263 lcore_infos[i].streams[j].rx_port,
1264 lcore_infos[i].streams[j].rx_queue,
1265 lcore_infos[i].streams[j].tx_port,
1266 lcore_infos[i].streams[j].tx_queue);
1275 uint16_t hairpin_queue;
1279 struct rte_eth_hairpin_conf hairpin_conf = {
1282 struct rte_eth_conf port_conf = {
1288 struct rte_eth_txconf txq_conf;
1289 struct rte_eth_rxconf rxq_conf;
1290 struct rte_eth_dev_info dev_info;
1292 nr_queues = RXQ_NUM;
1293 if (hairpin_queues_num != 0)
1294 nr_queues = RXQ_NUM + hairpin_queues_num;
1296 nr_ports = rte_eth_dev_count_avail();
1298 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1300 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1301 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1304 if (mbuf_mp == NULL)
1305 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1307 for (port_id = 0; port_id < nr_ports; port_id++) {
1308 ret = rte_eth_dev_info_get(port_id, &dev_info);
1310 rte_exit(EXIT_FAILURE,
1311 "Error during getting device"
1312 " (port %u) info: %s\n",
1313 port_id, strerror(-ret));
1315 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1316 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1318 printf(":: initializing port: %d\n", port_id);
1320 ret = rte_eth_dev_configure(port_id, nr_queues,
1321 nr_queues, &port_conf);
1323 rte_exit(EXIT_FAILURE,
1324 ":: cannot configure device: err=%d, port=%u\n",
1327 rxq_conf = dev_info.default_rxconf;
1328 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1329 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1330 rte_eth_dev_socket_id(port_id),
1334 rte_exit(EXIT_FAILURE,
1335 ":: Rx queue setup failed: err=%d, port=%u\n",
1339 txq_conf = dev_info.default_txconf;
1340 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1341 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1342 rte_eth_dev_socket_id(port_id),
1345 rte_exit(EXIT_FAILURE,
1346 ":: Tx queue setup failed: err=%d, port=%u\n",
1350 /* Catch all packets from traffic generator. */
1351 ret = rte_eth_promiscuous_enable(port_id);
1353 rte_exit(EXIT_FAILURE,
1354 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1355 rte_strerror(-ret), port_id);
1357 if (hairpin_queues_num != 0) {
1359 * Configure peer which represents hairpin Tx.
1360 * Hairpin queue numbers start after standard queues
1361 * (RXQ_NUM and TXQ_NUM).
1363 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1364 hairpin_queue < nr_queues;
1365 hairpin_queue++, std_queue++) {
1366 hairpin_conf.peers[0].port = port_id;
1367 hairpin_conf.peers[0].queue =
1368 std_queue + TXQ_NUM;
1369 ret = rte_eth_rx_hairpin_queue_setup(
1370 port_id, hairpin_queue,
1371 NR_RXD, &hairpin_conf);
1373 rte_exit(EXIT_FAILURE,
1374 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1378 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1379 hairpin_queue < nr_queues;
1380 hairpin_queue++, std_queue++) {
1381 hairpin_conf.peers[0].port = port_id;
1382 hairpin_conf.peers[0].queue =
1383 std_queue + RXQ_NUM;
1384 ret = rte_eth_tx_hairpin_queue_setup(
1385 port_id, hairpin_queue,
1386 NR_TXD, &hairpin_conf);
1388 rte_exit(EXIT_FAILURE,
1389 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1394 ret = rte_eth_dev_start(port_id);
1396 rte_exit(EXIT_FAILURE,
1397 "rte_eth_dev_start:err=%d, port=%u\n",
1400 printf(":: initializing port: %d done\n", port_id);
1405 main(int argc, char **argv)
1409 struct rte_flow_error error;
1410 int64_t alloc, last_alloc;
1412 ret = rte_eal_init(argc, argv);
1414 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1417 dump_iterations = false;
1418 flows_count = DEFAULT_RULES_COUNT;
1419 iterations_number = DEFAULT_ITERATION;
1420 delete_flag = false;
1421 dump_socket_mem_flag = false;
1424 signal(SIGINT, signal_handler);
1425 signal(SIGTERM, signal_handler);
1430 args_parse(argc, argv);
1434 nb_lcores = rte_lcore_count();
1436 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1438 last_alloc = (int64_t)dump_socket_mem(stdout);
1440 alloc = (int64_t)dump_socket_mem(stdout);
1443 fprintf(stdout, ":: Memory allocation change(M): %.6lf\n",
1444 (alloc - last_alloc) / 1.0e6);
1448 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MASTER);
1451 RTE_ETH_FOREACH_DEV(port) {
1452 rte_flow_flush(port, &error);
1453 if (rte_eth_dev_stop(port) != 0)
1454 printf("Failed to stop device on port %u\n", port);
1455 rte_eth_dev_close(port);