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");
176 args_parse(int argc, char **argv)
183 static const struct option_dict {
192 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
193 .map = &flow_items[0],
194 .map_idx = &items_idx
198 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
199 .map = &flow_items[0],
200 .map_idx = &items_idx
204 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
205 .map = &flow_items[0],
206 .map_idx = &items_idx
210 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
211 .map = &flow_items[0],
212 .map_idx = &items_idx
216 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
217 .map = &flow_items[0],
218 .map_idx = &items_idx
222 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
223 .map = &flow_items[0],
224 .map_idx = &items_idx
228 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
229 .map = &flow_items[0],
230 .map_idx = &items_idx
234 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
235 .map = &flow_items[0],
236 .map_idx = &items_idx
240 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
241 .map = &flow_items[0],
242 .map_idx = &items_idx
246 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
247 .map = &flow_items[0],
248 .map_idx = &items_idx
252 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
253 .map = &flow_items[0],
254 .map_idx = &items_idx
258 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
259 .map = &flow_items[0],
260 .map_idx = &items_idx
264 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
265 .map = &flow_items[0],
266 .map_idx = &items_idx
271 .map = &flow_attrs[0],
272 .map_idx = &attrs_idx
277 .map = &flow_attrs[0],
278 .map_idx = &attrs_idx
283 .map = &flow_attrs[0],
284 .map_idx = &attrs_idx
288 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
289 .map = &flow_actions[0],
290 .map_idx = &actions_idx
294 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
295 .map = &flow_actions[0],
296 .map_idx = &actions_idx
300 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
301 .map = &flow_actions[0],
302 .map_idx = &actions_idx
306 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
307 .map = &flow_actions[0],
308 .map_idx = &actions_idx
312 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
313 .map = &flow_actions[0],
314 .map_idx = &actions_idx
318 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
319 .map = &flow_actions[0],
320 .map_idx = &actions_idx
324 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
325 .map = &flow_actions[0],
326 .map_idx = &actions_idx
330 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
331 .map = &flow_actions[0],
332 .map_idx = &actions_idx
336 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
337 .map = &flow_actions[0],
338 .map_idx = &actions_idx
341 .str = "set-src-mac",
342 .mask = FLOW_ACTION_MASK(
343 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
345 .map = &flow_actions[0],
346 .map_idx = &actions_idx
349 .str = "set-dst-mac",
350 .mask = FLOW_ACTION_MASK(
351 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
353 .map = &flow_actions[0],
354 .map_idx = &actions_idx
357 .str = "set-src-ipv4",
358 .mask = FLOW_ACTION_MASK(
359 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
361 .map = &flow_actions[0],
362 .map_idx = &actions_idx
365 .str = "set-dst-ipv4",
366 .mask = FLOW_ACTION_MASK(
367 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
369 .map = &flow_actions[0],
370 .map_idx = &actions_idx
373 .str = "set-src-ipv6",
374 .mask = FLOW_ACTION_MASK(
375 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
377 .map = &flow_actions[0],
378 .map_idx = &actions_idx
381 .str = "set-dst-ipv6",
382 .mask = FLOW_ACTION_MASK(
383 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
385 .map = &flow_actions[0],
386 .map_idx = &actions_idx
390 .mask = FLOW_ACTION_MASK(
391 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
393 .map = &flow_actions[0],
394 .map_idx = &actions_idx
398 .mask = FLOW_ACTION_MASK(
399 RTE_FLOW_ACTION_TYPE_SET_TP_DST
401 .map = &flow_actions[0],
402 .map_idx = &actions_idx
405 .str = "inc-tcp-ack",
406 .mask = FLOW_ACTION_MASK(
407 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
409 .map = &flow_actions[0],
410 .map_idx = &actions_idx
413 .str = "dec-tcp-ack",
414 .mask = FLOW_ACTION_MASK(
415 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
417 .map = &flow_actions[0],
418 .map_idx = &actions_idx
421 .str = "inc-tcp-seq",
422 .mask = FLOW_ACTION_MASK(
423 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
425 .map = &flow_actions[0],
426 .map_idx = &actions_idx
429 .str = "dec-tcp-seq",
430 .mask = FLOW_ACTION_MASK(
431 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
433 .map = &flow_actions[0],
434 .map_idx = &actions_idx
438 .mask = FLOW_ACTION_MASK(
439 RTE_FLOW_ACTION_TYPE_SET_TTL
441 .map = &flow_actions[0],
442 .map_idx = &actions_idx
446 .mask = FLOW_ACTION_MASK(
447 RTE_FLOW_ACTION_TYPE_DEC_TTL
449 .map = &flow_actions[0],
450 .map_idx = &actions_idx
453 .str = "set-ipv4-dscp",
454 .mask = FLOW_ACTION_MASK(
455 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
457 .map = &flow_actions[0],
458 .map_idx = &actions_idx
461 .str = "set-ipv6-dscp",
462 .mask = FLOW_ACTION_MASK(
463 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
465 .map = &flow_actions[0],
466 .map_idx = &actions_idx
470 static const struct option lgopts[] = {
473 { "flows-count", 1, 0, 0 },
474 { "dump-iterations", 0, 0, 0 },
475 { "deletion-rate", 0, 0, 0 },
476 { "dump-socket-mem", 0, 0, 0 },
477 { "enable-fwd", 0, 0, 0 },
479 { "ingress", 0, 0, 0 },
480 { "egress", 0, 0, 0 },
481 { "transfer", 0, 0, 0 },
482 { "group", 1, 0, 0 },
484 { "ether", 0, 0, 0 },
490 { "vxlan", 0, 0, 0 },
491 { "vxlan-gpe", 0, 0, 0 },
493 { "geneve", 0, 0, 0 },
498 { "port-id", 0, 0, 0 },
500 { "queue", 0, 0, 0 },
503 { "count", 0, 0, 0 },
504 { "set-meta", 0, 0, 0 },
505 { "set-tag", 0, 0, 0 },
507 { "hairpin-queue", 1, 0, 0 },
508 { "hairpin-rss", 1, 0, 0 },
509 { "set-src-mac", 0, 0, 0 },
510 { "set-dst-mac", 0, 0, 0 },
511 { "set-src-ipv4", 0, 0, 0 },
512 { "set-dst-ipv4", 0, 0, 0 },
513 { "set-src-ipv6", 0, 0, 0 },
514 { "set-dst-ipv6", 0, 0, 0 },
515 { "set-src-tp", 0, 0, 0 },
516 { "set-dst-tp", 0, 0, 0 },
517 { "inc-tcp-ack", 0, 0, 0 },
518 { "dec-tcp-ack", 0, 0, 0 },
519 { "inc-tcp-seq", 0, 0, 0 },
520 { "dec-tcp-seq", 0, 0, 0 },
521 { "set-ttl", 0, 0, 0 },
522 { "dec-ttl", 0, 0, 0 },
523 { "set-ipv4-dscp", 0, 0, 0 },
524 { "set-ipv6-dscp", 0, 0, 0 },
527 hairpin_queues_num = 0;
530 printf(":: Flow -> ");
531 while ((opt = getopt_long(argc, argvopt, "",
532 lgopts, &opt_idx)) != EOF) {
535 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
537 rte_exit(EXIT_SUCCESS, "Displayed help\n");
540 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
545 rte_exit(EXIT_SUCCESS,
546 "flow group should be >= 0\n");
547 printf("group %d / ", flow_group);
550 for (i = 0; i < RTE_DIM(flow_options); i++)
551 if (strcmp(lgopts[opt_idx].name,
552 flow_options[i].str) == 0) {
554 (*flow_options[i].map_idx)++] =
555 flow_options[i].mask;
556 printf("%s / ", flow_options[i].str);
559 if (strcmp(lgopts[opt_idx].name,
560 "hairpin-rss") == 0) {
563 hairpin_queues_num = n;
565 rte_exit(EXIT_SUCCESS,
566 "Hairpin queues should be > 0\n");
568 flow_actions[actions_idx++] =
570 printf("hairpin-rss / ");
572 if (strcmp(lgopts[opt_idx].name,
573 "hairpin-queue") == 0) {
576 hairpin_queues_num = n;
578 rte_exit(EXIT_SUCCESS,
579 "Hairpin queues should be > 0\n");
581 flow_actions[actions_idx++] =
582 HAIRPIN_QUEUE_ACTION;
583 printf("hairpin-queue / ");
587 if (strcmp(lgopts[opt_idx].name,
588 "flows-count") == 0) {
590 if (n > (int) iterations_number)
593 printf("\n\nflows_count should be > %d\n",
595 rte_exit(EXIT_SUCCESS, " ");
598 if (strcmp(lgopts[opt_idx].name,
599 "dump-iterations") == 0)
600 dump_iterations = true;
601 if (strcmp(lgopts[opt_idx].name,
602 "deletion-rate") == 0)
604 if (strcmp(lgopts[opt_idx].name,
605 "dump-socket-mem") == 0)
606 dump_socket_mem_flag = true;
607 if (strcmp(lgopts[opt_idx].name,
612 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
614 rte_exit(EXIT_SUCCESS, "Invalid option\n");
618 printf("end_flow\n");
621 /* Dump the socket memory statistics on console */
623 dump_socket_mem(FILE *f)
625 struct rte_malloc_socket_stats socket_stats;
630 unsigned int n_alloc = 0;
631 unsigned int n_free = 0;
632 bool active_nodes = false;
635 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
636 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
637 !socket_stats.heap_totalsz_bytes)
640 total += socket_stats.heap_totalsz_bytes;
641 alloc += socket_stats.heap_allocsz_bytes;
642 free += socket_stats.heap_freesz_bytes;
643 n_alloc += socket_stats.alloc_count;
644 n_free += socket_stats.free_count;
645 if (dump_socket_mem_flag) {
646 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
648 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
649 " %.6lf(%.3lf%%)\nfree: %.6lf"
651 "\ncount alloc: %u\nfree: %u\n",
653 socket_stats.heap_totalsz_bytes / 1.0e6,
654 socket_stats.heap_allocsz_bytes / 1.0e6,
655 (double)socket_stats.heap_allocsz_bytes * 100 /
656 (double)socket_stats.heap_totalsz_bytes,
657 socket_stats.heap_freesz_bytes / 1.0e6,
658 socket_stats.greatest_free_size / 1.0e6,
659 socket_stats.alloc_count,
660 socket_stats.free_count);
661 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
664 if (dump_socket_mem_flag && active_nodes) {
666 "\nTotal: size(M)\ntotal: %.6lf"
667 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
668 "\ncount alloc: %u\nfree: %u\n",
669 total / 1.0e6, alloc / 1.0e6,
670 (double)alloc * 100 / (double)total, free / 1.0e6,
672 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
678 print_flow_error(struct rte_flow_error error)
680 printf("Flow can't be created %d message: %s\n",
682 error.message ? error.message : "(no stated reason)");
686 destroy_flows(int port_id, struct rte_flow **flow_list)
688 struct rte_flow_error error;
689 clock_t start_iter, end_iter;
690 double cpu_time_used = 0;
692 double cpu_time_per_iter[MAX_ITERATIONS];
697 for (i = 0; i < MAX_ITERATIONS; i++)
698 cpu_time_per_iter[i] = -1;
700 if (iterations_number > flows_count)
701 iterations_number = flows_count;
704 printf("Flows Deletion on port = %d\n", port_id);
705 start_iter = clock();
706 for (i = 0; i < flows_count; i++) {
707 if (flow_list[i] == 0)
710 memset(&error, 0x33, sizeof(error));
711 if (rte_flow_destroy(port_id, flow_list[i], &error)) {
712 print_flow_error(error);
713 rte_exit(EXIT_FAILURE, "Error in deleting flow");
716 if (i && !((i + 1) % iterations_number)) {
717 /* Save the deletion rate of each iter */
719 delta = (double) (end_iter - start_iter);
720 iter_id = ((i + 1) / iterations_number) - 1;
721 cpu_time_per_iter[iter_id] =
722 delta / CLOCKS_PER_SEC;
723 cpu_time_used += cpu_time_per_iter[iter_id];
724 start_iter = clock();
728 /* Deletion rate per iteration */
730 for (i = 0; i < MAX_ITERATIONS; i++) {
731 if (cpu_time_per_iter[i] == -1)
733 delta = (double)(iterations_number /
734 cpu_time_per_iter[i]);
735 flows_rate = delta / 1000;
736 printf(":: Iteration #%d: %d flows "
737 "in %f sec[ Rate = %f K/Sec ]\n",
738 i, iterations_number,
739 cpu_time_per_iter[i], flows_rate);
742 /* Deletion rate for all flows */
743 flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
744 printf("\n:: Total flow deletion rate -> %f K/Sec\n",
746 printf(":: The time for deleting %d in flows %f seconds\n",
747 flows_count, cpu_time_used);
753 struct rte_flow **flow_list;
754 struct rte_flow_error error;
755 clock_t start_iter, end_iter;
756 double cpu_time_used;
758 double cpu_time_per_iter[MAX_ITERATIONS];
765 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
766 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
768 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
769 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
771 nr_ports = rte_eth_dev_count_avail();
773 for (i = 0; i < MAX_ITERATIONS; i++)
774 cpu_time_per_iter[i] = -1;
776 if (iterations_number > flows_count)
777 iterations_number = flows_count;
779 printf(":: Flows Count per port: %d\n", flows_count);
781 flow_list = rte_zmalloc("flow_list",
782 (sizeof(struct rte_flow *) * flows_count) + 1, 0);
783 if (flow_list == NULL)
784 rte_exit(EXIT_FAILURE, "No Memory available!");
786 for (port_id = 0; port_id < nr_ports; port_id++) {
789 if (flow_group > 0) {
791 * Create global rule to jump into flow_group,
792 * this way the app will avoid the default rules.
795 * group 0 eth / end actions jump group <flow_group>
798 flow = generate_flow(port_id, 0, flow_attrs,
799 global_items, global_actions,
800 flow_group, 0, 0, &error);
803 print_flow_error(error);
804 rte_exit(EXIT_FAILURE, "error in creating flow");
806 flow_list[flow_index++] = flow;
810 printf("Flows insertion on port = %d\n", port_id);
811 start_iter = clock();
812 for (i = 0; i < flows_count; i++) {
813 flow = generate_flow(port_id, flow_group,
814 flow_attrs, flow_items, flow_actions,
815 JUMP_ACTION_TABLE, i,
816 hairpin_queues_num, &error);
822 print_flow_error(error);
823 rte_exit(EXIT_FAILURE, "error in creating flow");
826 flow_list[flow_index++] = flow;
828 if (i && !((i + 1) % iterations_number)) {
829 /* Save the insertion rate of each iter */
831 delta = (double) (end_iter - start_iter);
832 iter_id = ((i + 1) / iterations_number) - 1;
833 cpu_time_per_iter[iter_id] =
834 delta / CLOCKS_PER_SEC;
835 cpu_time_used += cpu_time_per_iter[iter_id];
836 start_iter = clock();
840 /* Iteration rate per iteration */
842 for (i = 0; i < MAX_ITERATIONS; i++) {
843 if (cpu_time_per_iter[i] == -1)
845 delta = (double)(iterations_number /
846 cpu_time_per_iter[i]);
847 flows_rate = delta / 1000;
848 printf(":: Iteration #%d: %d flows "
849 "in %f sec[ Rate = %f K/Sec ]\n",
850 i, iterations_number,
851 cpu_time_per_iter[i], flows_rate);
854 /* Insertion rate for all flows */
855 flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
856 printf("\n:: Total flow insertion rate -> %f K/Sec\n",
858 printf(":: The time for creating %d in flows %f seconds\n",
859 flows_count, cpu_time_used);
862 destroy_flows(port_id, flow_list);
867 signal_handler(int signum)
869 if (signum == SIGINT || signum == SIGTERM) {
870 printf("\n\nSignal %d received, preparing to exit...\n",
872 printf("Error: Stats are wrong due to sudden signal!\n\n");
877 static inline uint16_t
878 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
881 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
887 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
893 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
894 li->tx_pkts += nr_tx;
895 li->tx_drops += cnt - nr_tx;
897 for (i = nr_tx; i < cnt; i++)
898 rte_pktmbuf_free(li->pkts[i]);
902 * Method to convert numbers into pretty numbers that easy
903 * to read. The design here is to add comma after each three
904 * digits and set all of this inside buffer.
906 * For example if n = 1799321, the output will be
907 * 1,799,321 after this method which is easier to read.
910 pretty_number(uint64_t n, char *buf)
917 sprintf(p[i], "%03d", (int)(n % 1000));
922 sprintf(p[i++], "%d", (int)n);
925 off += sprintf(buf + off, "%s,", p[i]);
926 buf[strlen(buf) - 1] = '\0';
932 packet_per_second_stats(void)
934 struct lcore_info *old;
935 struct lcore_info *li, *oli;
939 old = rte_zmalloc("old",
940 sizeof(struct lcore_info) * MAX_LCORES, 0);
942 rte_exit(EXIT_FAILURE, "No Memory available!");
944 memcpy(old, lcore_infos,
945 sizeof(struct lcore_info) * MAX_LCORES);
947 while (!force_quit) {
948 uint64_t total_tx_pkts = 0;
949 uint64_t total_rx_pkts = 0;
950 uint64_t total_tx_drops = 0;
951 uint64_t tx_delta, rx_delta, drops_delta;
953 int nr_valid_core = 0;
958 char go_up_nr_lines[16];
960 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
961 printf("%s\r", go_up_nr_lines);
964 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
965 printf("%6s %16s %16s %16s\n", "------", "----------------",
966 "----------------", "----------------");
968 for (i = 0; i < MAX_LCORES; i++) {
969 li = &lcore_infos[i];
971 if (li->mode != LCORE_MODE_PKT)
974 tx_delta = li->tx_pkts - oli->tx_pkts;
975 rx_delta = li->rx_pkts - oli->rx_pkts;
976 drops_delta = li->tx_drops - oli->tx_drops;
977 printf("%6d %16s %16s %16s\n", i,
978 pretty_number(tx_delta, buf[0]),
979 pretty_number(drops_delta, buf[1]),
980 pretty_number(rx_delta, buf[2]));
982 total_tx_pkts += tx_delta;
983 total_rx_pkts += rx_delta;
984 total_tx_drops += drops_delta;
990 if (nr_valid_core > 1) {
991 printf("%6s %16s %16s %16s\n", "total",
992 pretty_number(total_tx_pkts, buf[0]),
993 pretty_number(total_tx_drops, buf[1]),
994 pretty_number(total_rx_pkts, buf[2]));
998 memcpy(old, lcore_infos,
999 sizeof(struct lcore_info) * MAX_LCORES);
1004 start_forwarding(void *data __rte_unused)
1006 int lcore = rte_lcore_id();
1009 struct lcore_info *li = &lcore_infos[lcore];
1014 if (li->mode == LCORE_MODE_STATS) {
1015 printf(":: started stats on lcore %u\n", lcore);
1016 packet_per_second_stats();
1021 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1022 if (li->streams[stream_id].rx_port == -1)
1026 li->streams[stream_id].rx_port,
1027 li->streams[stream_id].rx_queue);
1030 li->streams[stream_id].tx_port,
1031 li->streams[stream_id].tx_queue);
1037 init_lcore_info(void)
1045 int streams_per_core;
1046 int unassigned_streams;
1048 nr_port = rte_eth_dev_count_avail();
1050 /* First logical core is reserved for stats printing */
1051 lcore = rte_get_next_lcore(-1, 0, 0);
1052 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1055 * Initialize all cores
1056 * All cores at first must have -1 value in all streams
1057 * This means that this stream is not used, or not set
1060 for (i = 0; i < MAX_LCORES; i++)
1061 for (j = 0; j < MAX_STREAMS; j++) {
1062 lcore_infos[i].streams[j].tx_port = -1;
1063 lcore_infos[i].streams[j].rx_port = -1;
1064 lcore_infos[i].streams[j].tx_queue = -1;
1065 lcore_infos[i].streams[j].rx_queue = -1;
1066 lcore_infos[i].streams_nb = 0;
1070 * Calculate the total streams count.
1071 * Also distribute those streams count between the available
1072 * logical cores except first core, since it's reserved for
1075 nb_fwd_streams = nr_port * RXQ_NUM;
1076 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1077 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1078 lcore = rte_get_next_lcore(lcore, 0, 0);
1079 lcore_infos[lcore].streams_nb = 1;
1082 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1083 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1084 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1085 lcore = rte_get_next_lcore(lcore, 0, 0);
1086 lcore_infos[lcore].streams_nb = streams_per_core;
1087 if (unassigned_streams) {
1088 lcore_infos[lcore].streams_nb++;
1089 unassigned_streams--;
1095 * Set the streams for the cores according to each logical
1096 * core stream count.
1097 * The streams is built on the design of what received should
1098 * forward as well, this means that if you received packets on
1099 * port 0 queue 0 then the same queue should forward the
1100 * packets, using the same logical core.
1102 lcore = rte_get_next_lcore(-1, 0, 0);
1103 for (port = 0; port < nr_port; port++) {
1104 /* Create FWD stream */
1105 for (queue = 0; queue < RXQ_NUM; queue++) {
1106 if (!lcore_infos[lcore].streams_nb ||
1107 !(stream_id % lcore_infos[lcore].streams_nb)) {
1108 lcore = rte_get_next_lcore(lcore, 0, 0);
1109 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1112 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1113 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1114 lcore_infos[lcore].streams[stream_id].rx_port = port;
1115 lcore_infos[lcore].streams[stream_id].tx_port = port;
1120 /* Print all streams */
1121 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1122 for (i = 0; i < MAX_LCORES; i++)
1123 for (j = 0; j < MAX_STREAMS; j++) {
1124 /* No streams for this core */
1125 if (lcore_infos[i].streams[j].tx_port == -1)
1127 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1129 lcore_infos[i].streams[j].rx_port,
1130 lcore_infos[i].streams[j].rx_queue,
1131 lcore_infos[i].streams[j].tx_port,
1132 lcore_infos[i].streams[j].tx_queue);
1141 uint16_t hairpin_queue;
1145 struct rte_eth_hairpin_conf hairpin_conf = {
1148 struct rte_eth_conf port_conf = {
1154 struct rte_eth_txconf txq_conf;
1155 struct rte_eth_rxconf rxq_conf;
1156 struct rte_eth_dev_info dev_info;
1158 nr_queues = RXQ_NUM;
1159 if (hairpin_queues_num != 0)
1160 nr_queues = RXQ_NUM + hairpin_queues_num;
1162 nr_ports = rte_eth_dev_count_avail();
1164 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1166 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1167 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1170 if (mbuf_mp == NULL)
1171 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1173 for (port_id = 0; port_id < nr_ports; port_id++) {
1174 ret = rte_eth_dev_info_get(port_id, &dev_info);
1176 rte_exit(EXIT_FAILURE,
1177 "Error during getting device"
1178 " (port %u) info: %s\n",
1179 port_id, strerror(-ret));
1181 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1182 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1184 printf(":: initializing port: %d\n", port_id);
1186 ret = rte_eth_dev_configure(port_id, nr_queues,
1187 nr_queues, &port_conf);
1189 rte_exit(EXIT_FAILURE,
1190 ":: cannot configure device: err=%d, port=%u\n",
1193 rxq_conf = dev_info.default_rxconf;
1194 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1195 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1196 rte_eth_dev_socket_id(port_id),
1200 rte_exit(EXIT_FAILURE,
1201 ":: Rx queue setup failed: err=%d, port=%u\n",
1205 txq_conf = dev_info.default_txconf;
1206 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1207 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1208 rte_eth_dev_socket_id(port_id),
1211 rte_exit(EXIT_FAILURE,
1212 ":: Tx queue setup failed: err=%d, port=%u\n",
1216 /* Catch all packets from traffic generator. */
1217 ret = rte_eth_promiscuous_enable(port_id);
1219 rte_exit(EXIT_FAILURE,
1220 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1221 rte_strerror(-ret), port_id);
1223 if (hairpin_queues_num != 0) {
1225 * Configure peer which represents hairpin Tx.
1226 * Hairpin queue numbers start after standard queues
1227 * (RXQ_NUM and TXQ_NUM).
1229 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1230 hairpin_queue < nr_queues;
1231 hairpin_queue++, std_queue++) {
1232 hairpin_conf.peers[0].port = port_id;
1233 hairpin_conf.peers[0].queue =
1234 std_queue + TXQ_NUM;
1235 ret = rte_eth_rx_hairpin_queue_setup(
1236 port_id, hairpin_queue,
1237 NR_RXD, &hairpin_conf);
1239 rte_exit(EXIT_FAILURE,
1240 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1244 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1245 hairpin_queue < nr_queues;
1246 hairpin_queue++, std_queue++) {
1247 hairpin_conf.peers[0].port = port_id;
1248 hairpin_conf.peers[0].queue =
1249 std_queue + RXQ_NUM;
1250 ret = rte_eth_tx_hairpin_queue_setup(
1251 port_id, hairpin_queue,
1252 NR_TXD, &hairpin_conf);
1254 rte_exit(EXIT_FAILURE,
1255 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1260 ret = rte_eth_dev_start(port_id);
1262 rte_exit(EXIT_FAILURE,
1263 "rte_eth_dev_start:err=%d, port=%u\n",
1266 printf(":: initializing port: %d done\n", port_id);
1271 main(int argc, char **argv)
1275 struct rte_flow_error error;
1276 int64_t alloc, last_alloc;
1278 ret = rte_eal_init(argc, argv);
1280 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1283 dump_iterations = false;
1284 flows_count = DEFAULT_RULES_COUNT;
1285 iterations_number = DEFAULT_ITERATION;
1286 delete_flag = false;
1287 dump_socket_mem_flag = false;
1290 signal(SIGINT, signal_handler);
1291 signal(SIGTERM, signal_handler);
1296 args_parse(argc, argv);
1300 nb_lcores = rte_lcore_count();
1302 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1304 last_alloc = (int64_t)dump_socket_mem(stdout);
1306 alloc = (int64_t)dump_socket_mem(stdout);
1309 fprintf(stdout, ":: Memory allocation change(M): %.6lf\n",
1310 (alloc - last_alloc) / 1.0e6);
1314 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MASTER);
1317 RTE_ETH_FOREACH_DEV(port) {
1318 rte_flow_flush(port, &error);
1319 rte_eth_dev_stop(port);
1320 rte_eth_dev_close(port);