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 flow_items[MAX_ITEMS_NUM];
49 static uint64_t flow_actions[MAX_ACTIONS_NUM];
50 static uint64_t flow_attrs[MAX_ATTRS_NUM];
51 static uint8_t items_idx, actions_idx, attrs_idx;
53 static volatile bool force_quit;
54 static bool dump_iterations;
55 static bool delete_flag;
56 static bool dump_socket_mem_flag;
57 static bool enable_fwd;
59 static struct rte_mempool *mbuf_mp;
60 static uint32_t nb_lcores;
61 static uint32_t flows_count;
62 static uint32_t iterations_number;
63 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
64 static uint32_t nb_lcores;
66 #define MAX_PKT_BURST 32
67 #define LCORE_MODE_PKT 1
68 #define LCORE_MODE_STATS 2
69 #define MAX_STREAMS 64
82 struct stream streams[MAX_STREAMS];
87 struct rte_mbuf *pkts[MAX_PKT_BURST];
88 } __rte_cache_aligned;
90 static struct lcore_info lcore_infos[MAX_LCORES];
95 printf("\nusage: %s\n", progname);
96 printf("\nControl configurations:\n");
97 printf(" --flows-count=N: to set the number of needed"
98 " flows to insert, default is 4,000,000\n");
99 printf(" --dump-iterations: To print rates for each"
101 printf(" --deletion-rate: Enable deletion rate"
103 printf(" --dump-socket-mem: To dump all socket memory\n");
104 printf(" --enable-fwd: To enable packets forwarding"
105 " after insertion\n");
107 printf("To set flow attributes:\n");
108 printf(" --ingress: set ingress attribute in flows\n");
109 printf(" --egress: set egress attribute in flows\n");
110 printf(" --transfer: set transfer attribute in flows\n");
111 printf(" --group=N: set group for all flows,"
114 printf("To set flow items:\n");
115 printf(" --ether: add ether layer in flow items\n");
116 printf(" --vlan: add vlan layer in flow items\n");
117 printf(" --ipv4: add ipv4 layer in flow items\n");
118 printf(" --ipv6: add ipv6 layer in flow items\n");
119 printf(" --tcp: add tcp layer in flow items\n");
120 printf(" --udp: add udp layer in flow items\n");
121 printf(" --vxlan: add vxlan layer in flow items\n");
122 printf(" --vxlan-gpe: add vxlan-gpe layer in flow items\n");
123 printf(" --gre: add gre layer in flow items\n");
124 printf(" --geneve: add geneve layer in flow items\n");
125 printf(" --gtp: add gtp layer in flow items\n");
126 printf(" --meta: add meta layer in flow items\n");
127 printf(" --tag: add tag layer in flow items\n");
129 printf("To set flow actions:\n");
130 printf(" --port-id: add port-id action in flow actions\n");
131 printf(" --rss: add rss action in flow actions\n");
132 printf(" --queue: add queue action in flow actions\n");
133 printf(" --jump: add jump action in flow actions\n");
134 printf(" --mark: add mark action in flow actions\n");
135 printf(" --count: add count action in flow actions\n");
136 printf(" --set-meta: add set meta action in flow actions\n");
137 printf(" --set-tag: add set tag action in flow actions\n");
138 printf(" --drop: add drop action in flow actions\n");
139 printf(" --hairpin-queue=N: add hairpin-queue action in flow actions\n");
140 printf(" --hairpin-rss=N: add hairpin-rss action in flow actions\n");
141 printf(" --set-src-mac: add set src mac action to flow actions\n"
142 "Src mac to be set is random each flow\n");
143 printf(" --set-dst-mac: add set dst mac action to flow actions\n"
144 "Dst mac to be set is random each flow\n");
145 printf(" --set-src-ipv4: add set src ipv4 action to flow actions\n"
146 "Src ipv4 to be set is random each flow\n");
147 printf(" --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
148 "Dst ipv4 to be set is random each flow\n");
149 printf(" --set-src-ipv6: add set src ipv6 action to flow actions\n"
150 "Src ipv6 to be set is random each flow\n");
151 printf(" --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
152 "Dst ipv6 to be set is random each flow\n");
153 printf(" --set-src-tp: add set src tp action to flow actions\n"
154 "Src tp to be set is random each flow\n");
155 printf(" --set-dst-tp: add set dst tp action to flow actions\n"
156 "Dst tp to be set is random each flow\n");
157 printf(" --inc-tcp-ack: add inc tcp ack action to flow actions\n"
158 "tcp ack will be increments by 1\n");
159 printf(" --dec-tcp-ack: add dec tcp ack action to flow actions\n"
160 "tcp ack will be decrements by 1\n");
161 printf(" --inc-tcp-seq: add inc tcp seq action to flow actions\n"
162 "tcp seq will be increments by 1\n");
163 printf(" --dec-tcp-seq: add dec tcp seq action to flow actions\n"
164 "tcp seq will be decrements by 1\n");
165 printf(" --set-ttl: add set ttl action to flow actions\n"
166 "L3 ttl to be set is random each flow\n");
167 printf(" --dec-ttl: add dec ttl action to flow actions\n"
168 "L3 ttl will be decrements by 1\n");
169 printf(" --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
170 "ipv4 dscp value to be set is random each flow\n");
171 printf(" --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
172 "ipv6 dscp value to be set is random each flow\n");
173 printf(" --flag: add flag action to flow actions\n");
177 args_parse(int argc, char **argv)
184 static const struct option_dict {
193 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
194 .map = &flow_items[0],
195 .map_idx = &items_idx
199 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
200 .map = &flow_items[0],
201 .map_idx = &items_idx
205 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
206 .map = &flow_items[0],
207 .map_idx = &items_idx
211 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
212 .map = &flow_items[0],
213 .map_idx = &items_idx
217 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
218 .map = &flow_items[0],
219 .map_idx = &items_idx
223 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
224 .map = &flow_items[0],
225 .map_idx = &items_idx
229 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
230 .map = &flow_items[0],
231 .map_idx = &items_idx
235 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
236 .map = &flow_items[0],
237 .map_idx = &items_idx
241 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
242 .map = &flow_items[0],
243 .map_idx = &items_idx
247 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
248 .map = &flow_items[0],
249 .map_idx = &items_idx
253 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
254 .map = &flow_items[0],
255 .map_idx = &items_idx
259 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
260 .map = &flow_items[0],
261 .map_idx = &items_idx
265 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
266 .map = &flow_items[0],
267 .map_idx = &items_idx
272 .map = &flow_attrs[0],
273 .map_idx = &attrs_idx
278 .map = &flow_attrs[0],
279 .map_idx = &attrs_idx
284 .map = &flow_attrs[0],
285 .map_idx = &attrs_idx
289 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
290 .map = &flow_actions[0],
291 .map_idx = &actions_idx
295 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
296 .map = &flow_actions[0],
297 .map_idx = &actions_idx
301 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
302 .map = &flow_actions[0],
303 .map_idx = &actions_idx
307 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
308 .map = &flow_actions[0],
309 .map_idx = &actions_idx
313 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
314 .map = &flow_actions[0],
315 .map_idx = &actions_idx
319 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
320 .map = &flow_actions[0],
321 .map_idx = &actions_idx
325 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
326 .map = &flow_actions[0],
327 .map_idx = &actions_idx
331 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
332 .map = &flow_actions[0],
333 .map_idx = &actions_idx
337 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
338 .map = &flow_actions[0],
339 .map_idx = &actions_idx
342 .str = "set-src-mac",
343 .mask = FLOW_ACTION_MASK(
344 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
346 .map = &flow_actions[0],
347 .map_idx = &actions_idx
350 .str = "set-dst-mac",
351 .mask = FLOW_ACTION_MASK(
352 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
354 .map = &flow_actions[0],
355 .map_idx = &actions_idx
358 .str = "set-src-ipv4",
359 .mask = FLOW_ACTION_MASK(
360 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
362 .map = &flow_actions[0],
363 .map_idx = &actions_idx
366 .str = "set-dst-ipv4",
367 .mask = FLOW_ACTION_MASK(
368 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
370 .map = &flow_actions[0],
371 .map_idx = &actions_idx
374 .str = "set-src-ipv6",
375 .mask = FLOW_ACTION_MASK(
376 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
378 .map = &flow_actions[0],
379 .map_idx = &actions_idx
382 .str = "set-dst-ipv6",
383 .mask = FLOW_ACTION_MASK(
384 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
386 .map = &flow_actions[0],
387 .map_idx = &actions_idx
391 .mask = FLOW_ACTION_MASK(
392 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
394 .map = &flow_actions[0],
395 .map_idx = &actions_idx
399 .mask = FLOW_ACTION_MASK(
400 RTE_FLOW_ACTION_TYPE_SET_TP_DST
402 .map = &flow_actions[0],
403 .map_idx = &actions_idx
406 .str = "inc-tcp-ack",
407 .mask = FLOW_ACTION_MASK(
408 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
410 .map = &flow_actions[0],
411 .map_idx = &actions_idx
414 .str = "dec-tcp-ack",
415 .mask = FLOW_ACTION_MASK(
416 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
418 .map = &flow_actions[0],
419 .map_idx = &actions_idx
422 .str = "inc-tcp-seq",
423 .mask = FLOW_ACTION_MASK(
424 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
426 .map = &flow_actions[0],
427 .map_idx = &actions_idx
430 .str = "dec-tcp-seq",
431 .mask = FLOW_ACTION_MASK(
432 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
434 .map = &flow_actions[0],
435 .map_idx = &actions_idx
439 .mask = FLOW_ACTION_MASK(
440 RTE_FLOW_ACTION_TYPE_SET_TTL
442 .map = &flow_actions[0],
443 .map_idx = &actions_idx
447 .mask = FLOW_ACTION_MASK(
448 RTE_FLOW_ACTION_TYPE_DEC_TTL
450 .map = &flow_actions[0],
451 .map_idx = &actions_idx
454 .str = "set-ipv4-dscp",
455 .mask = FLOW_ACTION_MASK(
456 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
458 .map = &flow_actions[0],
459 .map_idx = &actions_idx
462 .str = "set-ipv6-dscp",
463 .mask = FLOW_ACTION_MASK(
464 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
466 .map = &flow_actions[0],
467 .map_idx = &actions_idx
471 .mask = FLOW_ACTION_MASK(
472 RTE_FLOW_ACTION_TYPE_FLAG
474 .map = &flow_actions[0],
475 .map_idx = &actions_idx
479 static const struct option lgopts[] = {
482 { "flows-count", 1, 0, 0 },
483 { "dump-iterations", 0, 0, 0 },
484 { "deletion-rate", 0, 0, 0 },
485 { "dump-socket-mem", 0, 0, 0 },
486 { "enable-fwd", 0, 0, 0 },
488 { "ingress", 0, 0, 0 },
489 { "egress", 0, 0, 0 },
490 { "transfer", 0, 0, 0 },
491 { "group", 1, 0, 0 },
493 { "ether", 0, 0, 0 },
499 { "vxlan", 0, 0, 0 },
500 { "vxlan-gpe", 0, 0, 0 },
502 { "geneve", 0, 0, 0 },
507 { "port-id", 0, 0, 0 },
509 { "queue", 0, 0, 0 },
512 { "count", 0, 0, 0 },
513 { "set-meta", 0, 0, 0 },
514 { "set-tag", 0, 0, 0 },
516 { "hairpin-queue", 1, 0, 0 },
517 { "hairpin-rss", 1, 0, 0 },
518 { "set-src-mac", 0, 0, 0 },
519 { "set-dst-mac", 0, 0, 0 },
520 { "set-src-ipv4", 0, 0, 0 },
521 { "set-dst-ipv4", 0, 0, 0 },
522 { "set-src-ipv6", 0, 0, 0 },
523 { "set-dst-ipv6", 0, 0, 0 },
524 { "set-src-tp", 0, 0, 0 },
525 { "set-dst-tp", 0, 0, 0 },
526 { "inc-tcp-ack", 0, 0, 0 },
527 { "dec-tcp-ack", 0, 0, 0 },
528 { "inc-tcp-seq", 0, 0, 0 },
529 { "dec-tcp-seq", 0, 0, 0 },
530 { "set-ttl", 0, 0, 0 },
531 { "dec-ttl", 0, 0, 0 },
532 { "set-ipv4-dscp", 0, 0, 0 },
533 { "set-ipv6-dscp", 0, 0, 0 },
537 hairpin_queues_num = 0;
540 printf(":: Flow -> ");
541 while ((opt = getopt_long(argc, argvopt, "",
542 lgopts, &opt_idx)) != EOF) {
545 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
547 rte_exit(EXIT_SUCCESS, "Displayed help\n");
550 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
555 rte_exit(EXIT_SUCCESS,
556 "flow group should be >= 0\n");
557 printf("group %d / ", flow_group);
560 for (i = 0; i < RTE_DIM(flow_options); i++)
561 if (strcmp(lgopts[opt_idx].name,
562 flow_options[i].str) == 0) {
564 (*flow_options[i].map_idx)++] =
565 flow_options[i].mask;
566 printf("%s / ", flow_options[i].str);
569 if (strcmp(lgopts[opt_idx].name,
570 "hairpin-rss") == 0) {
573 hairpin_queues_num = n;
575 rte_exit(EXIT_SUCCESS,
576 "Hairpin queues should be > 0\n");
578 flow_actions[actions_idx++] =
580 printf("hairpin-rss / ");
582 if (strcmp(lgopts[opt_idx].name,
583 "hairpin-queue") == 0) {
586 hairpin_queues_num = n;
588 rte_exit(EXIT_SUCCESS,
589 "Hairpin queues should be > 0\n");
591 flow_actions[actions_idx++] =
592 HAIRPIN_QUEUE_ACTION;
593 printf("hairpin-queue / ");
597 if (strcmp(lgopts[opt_idx].name,
598 "flows-count") == 0) {
600 if (n > (int) iterations_number)
603 printf("\n\nflows_count should be > %d\n",
605 rte_exit(EXIT_SUCCESS, " ");
608 if (strcmp(lgopts[opt_idx].name,
609 "dump-iterations") == 0)
610 dump_iterations = true;
611 if (strcmp(lgopts[opt_idx].name,
612 "deletion-rate") == 0)
614 if (strcmp(lgopts[opt_idx].name,
615 "dump-socket-mem") == 0)
616 dump_socket_mem_flag = true;
617 if (strcmp(lgopts[opt_idx].name,
622 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
624 rte_exit(EXIT_SUCCESS, "Invalid option\n");
628 printf("end_flow\n");
631 /* Dump the socket memory statistics on console */
633 dump_socket_mem(FILE *f)
635 struct rte_malloc_socket_stats socket_stats;
640 unsigned int n_alloc = 0;
641 unsigned int n_free = 0;
642 bool active_nodes = false;
645 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
646 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
647 !socket_stats.heap_totalsz_bytes)
650 total += socket_stats.heap_totalsz_bytes;
651 alloc += socket_stats.heap_allocsz_bytes;
652 free += socket_stats.heap_freesz_bytes;
653 n_alloc += socket_stats.alloc_count;
654 n_free += socket_stats.free_count;
655 if (dump_socket_mem_flag) {
656 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
658 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
659 " %.6lf(%.3lf%%)\nfree: %.6lf"
661 "\ncount alloc: %u\nfree: %u\n",
663 socket_stats.heap_totalsz_bytes / 1.0e6,
664 socket_stats.heap_allocsz_bytes / 1.0e6,
665 (double)socket_stats.heap_allocsz_bytes * 100 /
666 (double)socket_stats.heap_totalsz_bytes,
667 socket_stats.heap_freesz_bytes / 1.0e6,
668 socket_stats.greatest_free_size / 1.0e6,
669 socket_stats.alloc_count,
670 socket_stats.free_count);
671 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
674 if (dump_socket_mem_flag && active_nodes) {
676 "\nTotal: size(M)\ntotal: %.6lf"
677 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
678 "\ncount alloc: %u\nfree: %u\n",
679 total / 1.0e6, alloc / 1.0e6,
680 (double)alloc * 100 / (double)total, free / 1.0e6,
682 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
688 print_flow_error(struct rte_flow_error error)
690 printf("Flow can't be created %d message: %s\n",
692 error.message ? error.message : "(no stated reason)");
696 destroy_flows(int port_id, struct rte_flow **flow_list)
698 struct rte_flow_error error;
699 clock_t start_iter, end_iter;
700 double cpu_time_used = 0;
702 double cpu_time_per_iter[MAX_ITERATIONS];
707 for (i = 0; i < MAX_ITERATIONS; i++)
708 cpu_time_per_iter[i] = -1;
710 if (iterations_number > flows_count)
711 iterations_number = flows_count;
714 printf("Flows Deletion on port = %d\n", port_id);
715 start_iter = clock();
716 for (i = 0; i < flows_count; i++) {
717 if (flow_list[i] == 0)
720 memset(&error, 0x33, sizeof(error));
721 if (rte_flow_destroy(port_id, flow_list[i], &error)) {
722 print_flow_error(error);
723 rte_exit(EXIT_FAILURE, "Error in deleting flow");
726 if (i && !((i + 1) % iterations_number)) {
727 /* Save the deletion rate of each iter */
729 delta = (double) (end_iter - start_iter);
730 iter_id = ((i + 1) / iterations_number) - 1;
731 cpu_time_per_iter[iter_id] =
732 delta / CLOCKS_PER_SEC;
733 cpu_time_used += cpu_time_per_iter[iter_id];
734 start_iter = clock();
738 /* Deletion rate per iteration */
740 for (i = 0; i < MAX_ITERATIONS; i++) {
741 if (cpu_time_per_iter[i] == -1)
743 delta = (double)(iterations_number /
744 cpu_time_per_iter[i]);
745 flows_rate = delta / 1000;
746 printf(":: Iteration #%d: %d flows "
747 "in %f sec[ Rate = %f K/Sec ]\n",
748 i, iterations_number,
749 cpu_time_per_iter[i], flows_rate);
752 /* Deletion rate for all flows */
753 flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
754 printf("\n:: Total flow deletion rate -> %f K/Sec\n",
756 printf(":: The time for deleting %d in flows %f seconds\n",
757 flows_count, cpu_time_used);
763 struct rte_flow **flow_list;
764 struct rte_flow_error error;
765 clock_t start_iter, end_iter;
766 double cpu_time_used;
768 double cpu_time_per_iter[MAX_ITERATIONS];
775 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
776 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
778 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
779 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
781 nr_ports = rte_eth_dev_count_avail();
783 for (i = 0; i < MAX_ITERATIONS; i++)
784 cpu_time_per_iter[i] = -1;
786 if (iterations_number > flows_count)
787 iterations_number = flows_count;
789 printf(":: Flows Count per port: %d\n", flows_count);
791 flow_list = rte_zmalloc("flow_list",
792 (sizeof(struct rte_flow *) * flows_count) + 1, 0);
793 if (flow_list == NULL)
794 rte_exit(EXIT_FAILURE, "No Memory available!");
796 for (port_id = 0; port_id < nr_ports; port_id++) {
799 if (flow_group > 0) {
801 * Create global rule to jump into flow_group,
802 * this way the app will avoid the default rules.
805 * group 0 eth / end actions jump group <flow_group>
808 flow = generate_flow(port_id, 0, flow_attrs,
809 global_items, global_actions,
810 flow_group, 0, 0, &error);
813 print_flow_error(error);
814 rte_exit(EXIT_FAILURE, "error in creating flow");
816 flow_list[flow_index++] = flow;
820 printf("Flows insertion on port = %d\n", port_id);
821 start_iter = clock();
822 for (i = 0; i < flows_count; i++) {
823 flow = generate_flow(port_id, flow_group,
824 flow_attrs, flow_items, flow_actions,
825 JUMP_ACTION_TABLE, i,
826 hairpin_queues_num, &error);
832 print_flow_error(error);
833 rte_exit(EXIT_FAILURE, "error in creating flow");
836 flow_list[flow_index++] = flow;
838 if (i && !((i + 1) % iterations_number)) {
839 /* Save the insertion rate of each iter */
841 delta = (double) (end_iter - start_iter);
842 iter_id = ((i + 1) / iterations_number) - 1;
843 cpu_time_per_iter[iter_id] =
844 delta / CLOCKS_PER_SEC;
845 cpu_time_used += cpu_time_per_iter[iter_id];
846 start_iter = clock();
850 /* Iteration rate per iteration */
852 for (i = 0; i < MAX_ITERATIONS; i++) {
853 if (cpu_time_per_iter[i] == -1)
855 delta = (double)(iterations_number /
856 cpu_time_per_iter[i]);
857 flows_rate = delta / 1000;
858 printf(":: Iteration #%d: %d flows "
859 "in %f sec[ Rate = %f K/Sec ]\n",
860 i, iterations_number,
861 cpu_time_per_iter[i], flows_rate);
864 /* Insertion rate for all flows */
865 flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
866 printf("\n:: Total flow insertion rate -> %f K/Sec\n",
868 printf(":: The time for creating %d in flows %f seconds\n",
869 flows_count, cpu_time_used);
872 destroy_flows(port_id, flow_list);
877 signal_handler(int signum)
879 if (signum == SIGINT || signum == SIGTERM) {
880 printf("\n\nSignal %d received, preparing to exit...\n",
882 printf("Error: Stats are wrong due to sudden signal!\n\n");
887 static inline uint16_t
888 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
891 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
897 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
903 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
904 li->tx_pkts += nr_tx;
905 li->tx_drops += cnt - nr_tx;
907 for (i = nr_tx; i < cnt; i++)
908 rte_pktmbuf_free(li->pkts[i]);
912 * Method to convert numbers into pretty numbers that easy
913 * to read. The design here is to add comma after each three
914 * digits and set all of this inside buffer.
916 * For example if n = 1799321, the output will be
917 * 1,799,321 after this method which is easier to read.
920 pretty_number(uint64_t n, char *buf)
927 sprintf(p[i], "%03d", (int)(n % 1000));
932 sprintf(p[i++], "%d", (int)n);
935 off += sprintf(buf + off, "%s,", p[i]);
936 buf[strlen(buf) - 1] = '\0';
942 packet_per_second_stats(void)
944 struct lcore_info *old;
945 struct lcore_info *li, *oli;
949 old = rte_zmalloc("old",
950 sizeof(struct lcore_info) * MAX_LCORES, 0);
952 rte_exit(EXIT_FAILURE, "No Memory available!");
954 memcpy(old, lcore_infos,
955 sizeof(struct lcore_info) * MAX_LCORES);
957 while (!force_quit) {
958 uint64_t total_tx_pkts = 0;
959 uint64_t total_rx_pkts = 0;
960 uint64_t total_tx_drops = 0;
961 uint64_t tx_delta, rx_delta, drops_delta;
963 int nr_valid_core = 0;
968 char go_up_nr_lines[16];
970 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
971 printf("%s\r", go_up_nr_lines);
974 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
975 printf("%6s %16s %16s %16s\n", "------", "----------------",
976 "----------------", "----------------");
978 for (i = 0; i < MAX_LCORES; i++) {
979 li = &lcore_infos[i];
981 if (li->mode != LCORE_MODE_PKT)
984 tx_delta = li->tx_pkts - oli->tx_pkts;
985 rx_delta = li->rx_pkts - oli->rx_pkts;
986 drops_delta = li->tx_drops - oli->tx_drops;
987 printf("%6d %16s %16s %16s\n", i,
988 pretty_number(tx_delta, buf[0]),
989 pretty_number(drops_delta, buf[1]),
990 pretty_number(rx_delta, buf[2]));
992 total_tx_pkts += tx_delta;
993 total_rx_pkts += rx_delta;
994 total_tx_drops += drops_delta;
1000 if (nr_valid_core > 1) {
1001 printf("%6s %16s %16s %16s\n", "total",
1002 pretty_number(total_tx_pkts, buf[0]),
1003 pretty_number(total_tx_drops, buf[1]),
1004 pretty_number(total_rx_pkts, buf[2]));
1008 memcpy(old, lcore_infos,
1009 sizeof(struct lcore_info) * MAX_LCORES);
1014 start_forwarding(void *data __rte_unused)
1016 int lcore = rte_lcore_id();
1019 struct lcore_info *li = &lcore_infos[lcore];
1024 if (li->mode == LCORE_MODE_STATS) {
1025 printf(":: started stats on lcore %u\n", lcore);
1026 packet_per_second_stats();
1031 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1032 if (li->streams[stream_id].rx_port == -1)
1036 li->streams[stream_id].rx_port,
1037 li->streams[stream_id].rx_queue);
1040 li->streams[stream_id].tx_port,
1041 li->streams[stream_id].tx_queue);
1047 init_lcore_info(void)
1055 int streams_per_core;
1056 int unassigned_streams;
1058 nr_port = rte_eth_dev_count_avail();
1060 /* First logical core is reserved for stats printing */
1061 lcore = rte_get_next_lcore(-1, 0, 0);
1062 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1065 * Initialize all cores
1066 * All cores at first must have -1 value in all streams
1067 * This means that this stream is not used, or not set
1070 for (i = 0; i < MAX_LCORES; i++)
1071 for (j = 0; j < MAX_STREAMS; j++) {
1072 lcore_infos[i].streams[j].tx_port = -1;
1073 lcore_infos[i].streams[j].rx_port = -1;
1074 lcore_infos[i].streams[j].tx_queue = -1;
1075 lcore_infos[i].streams[j].rx_queue = -1;
1076 lcore_infos[i].streams_nb = 0;
1080 * Calculate the total streams count.
1081 * Also distribute those streams count between the available
1082 * logical cores except first core, since it's reserved for
1085 nb_fwd_streams = nr_port * RXQ_NUM;
1086 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1087 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1088 lcore = rte_get_next_lcore(lcore, 0, 0);
1089 lcore_infos[lcore].streams_nb = 1;
1092 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1093 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1094 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1095 lcore = rte_get_next_lcore(lcore, 0, 0);
1096 lcore_infos[lcore].streams_nb = streams_per_core;
1097 if (unassigned_streams) {
1098 lcore_infos[lcore].streams_nb++;
1099 unassigned_streams--;
1105 * Set the streams for the cores according to each logical
1106 * core stream count.
1107 * The streams is built on the design of what received should
1108 * forward as well, this means that if you received packets on
1109 * port 0 queue 0 then the same queue should forward the
1110 * packets, using the same logical core.
1112 lcore = rte_get_next_lcore(-1, 0, 0);
1113 for (port = 0; port < nr_port; port++) {
1114 /* Create FWD stream */
1115 for (queue = 0; queue < RXQ_NUM; queue++) {
1116 if (!lcore_infos[lcore].streams_nb ||
1117 !(stream_id % lcore_infos[lcore].streams_nb)) {
1118 lcore = rte_get_next_lcore(lcore, 0, 0);
1119 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1122 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1123 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1124 lcore_infos[lcore].streams[stream_id].rx_port = port;
1125 lcore_infos[lcore].streams[stream_id].tx_port = port;
1130 /* Print all streams */
1131 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1132 for (i = 0; i < MAX_LCORES; i++)
1133 for (j = 0; j < MAX_STREAMS; j++) {
1134 /* No streams for this core */
1135 if (lcore_infos[i].streams[j].tx_port == -1)
1137 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1139 lcore_infos[i].streams[j].rx_port,
1140 lcore_infos[i].streams[j].rx_queue,
1141 lcore_infos[i].streams[j].tx_port,
1142 lcore_infos[i].streams[j].tx_queue);
1151 uint16_t hairpin_queue;
1155 struct rte_eth_hairpin_conf hairpin_conf = {
1158 struct rte_eth_conf port_conf = {
1164 struct rte_eth_txconf txq_conf;
1165 struct rte_eth_rxconf rxq_conf;
1166 struct rte_eth_dev_info dev_info;
1168 nr_queues = RXQ_NUM;
1169 if (hairpin_queues_num != 0)
1170 nr_queues = RXQ_NUM + hairpin_queues_num;
1172 nr_ports = rte_eth_dev_count_avail();
1174 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1176 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1177 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1180 if (mbuf_mp == NULL)
1181 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1183 for (port_id = 0; port_id < nr_ports; port_id++) {
1184 ret = rte_eth_dev_info_get(port_id, &dev_info);
1186 rte_exit(EXIT_FAILURE,
1187 "Error during getting device"
1188 " (port %u) info: %s\n",
1189 port_id, strerror(-ret));
1191 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1192 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1194 printf(":: initializing port: %d\n", port_id);
1196 ret = rte_eth_dev_configure(port_id, nr_queues,
1197 nr_queues, &port_conf);
1199 rte_exit(EXIT_FAILURE,
1200 ":: cannot configure device: err=%d, port=%u\n",
1203 rxq_conf = dev_info.default_rxconf;
1204 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1205 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1206 rte_eth_dev_socket_id(port_id),
1210 rte_exit(EXIT_FAILURE,
1211 ":: Rx queue setup failed: err=%d, port=%u\n",
1215 txq_conf = dev_info.default_txconf;
1216 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1217 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1218 rte_eth_dev_socket_id(port_id),
1221 rte_exit(EXIT_FAILURE,
1222 ":: Tx queue setup failed: err=%d, port=%u\n",
1226 /* Catch all packets from traffic generator. */
1227 ret = rte_eth_promiscuous_enable(port_id);
1229 rte_exit(EXIT_FAILURE,
1230 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1231 rte_strerror(-ret), port_id);
1233 if (hairpin_queues_num != 0) {
1235 * Configure peer which represents hairpin Tx.
1236 * Hairpin queue numbers start after standard queues
1237 * (RXQ_NUM and TXQ_NUM).
1239 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1240 hairpin_queue < nr_queues;
1241 hairpin_queue++, std_queue++) {
1242 hairpin_conf.peers[0].port = port_id;
1243 hairpin_conf.peers[0].queue =
1244 std_queue + TXQ_NUM;
1245 ret = rte_eth_rx_hairpin_queue_setup(
1246 port_id, hairpin_queue,
1247 NR_RXD, &hairpin_conf);
1249 rte_exit(EXIT_FAILURE,
1250 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1254 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1255 hairpin_queue < nr_queues;
1256 hairpin_queue++, std_queue++) {
1257 hairpin_conf.peers[0].port = port_id;
1258 hairpin_conf.peers[0].queue =
1259 std_queue + RXQ_NUM;
1260 ret = rte_eth_tx_hairpin_queue_setup(
1261 port_id, hairpin_queue,
1262 NR_TXD, &hairpin_conf);
1264 rte_exit(EXIT_FAILURE,
1265 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1270 ret = rte_eth_dev_start(port_id);
1272 rte_exit(EXIT_FAILURE,
1273 "rte_eth_dev_start:err=%d, port=%u\n",
1276 printf(":: initializing port: %d done\n", port_id);
1281 main(int argc, char **argv)
1285 struct rte_flow_error error;
1286 int64_t alloc, last_alloc;
1288 ret = rte_eal_init(argc, argv);
1290 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1293 dump_iterations = false;
1294 flows_count = DEFAULT_RULES_COUNT;
1295 iterations_number = DEFAULT_ITERATION;
1296 delete_flag = false;
1297 dump_socket_mem_flag = false;
1300 signal(SIGINT, signal_handler);
1301 signal(SIGTERM, signal_handler);
1306 args_parse(argc, argv);
1310 nb_lcores = rte_lcore_count();
1312 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1314 last_alloc = (int64_t)dump_socket_mem(stdout);
1316 alloc = (int64_t)dump_socket_mem(stdout);
1319 fprintf(stdout, ":: Memory allocation change(M): %.6lf\n",
1320 (alloc - last_alloc) / 1.0e6);
1324 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MASTER);
1327 RTE_ETH_FOREACH_DEV(port) {
1328 rte_flow_flush(port, &error);
1329 rte_eth_dev_stop(port);
1330 rte_eth_dev_close(port);