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_BATCHES_COUNT 100
42 #define DEFAULT_RULES_COUNT 4000000
43 #define DEFAULT_RULES_BATCH 100000
44 #define DEFAULT_GROUP 0
46 struct rte_flow *flow;
47 static uint8_t flow_group;
49 static uint64_t encap_data;
50 static uint64_t decap_data;
52 static uint64_t flow_items[MAX_ITEMS_NUM];
53 static uint64_t flow_actions[MAX_ACTIONS_NUM];
54 static uint64_t flow_attrs[MAX_ATTRS_NUM];
55 static uint8_t items_idx, actions_idx, attrs_idx;
57 static uint64_t ports_mask;
58 static volatile bool force_quit;
59 static bool dump_iterations;
60 static bool delete_flag;
61 static bool dump_socket_mem_flag;
62 static bool enable_fwd;
64 static struct rte_mempool *mbuf_mp;
65 static uint32_t nb_lcores;
66 static uint32_t rules_count;
67 static uint32_t rules_batch;
68 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
69 static uint32_t nb_lcores;
71 #define MAX_PKT_BURST 32
72 #define LCORE_MODE_PKT 1
73 #define LCORE_MODE_STATS 2
74 #define MAX_STREAMS 64
87 struct stream streams[MAX_STREAMS];
92 struct rte_mbuf *pkts[MAX_PKT_BURST];
93 } __rte_cache_aligned;
95 static struct lcore_info lcore_infos[MAX_LCORES];
100 printf("\nusage: %s\n", progname);
101 printf("\nControl configurations:\n");
102 printf(" --rules-count=N: to set the number of needed"
103 " rules to insert, default is %d\n", DEFAULT_RULES_COUNT);
104 printf(" --rules-batch=N: set number of batched rules,"
105 " default is %d\n", DEFAULT_RULES_BATCH);
106 printf(" --dump-iterations: To print rates for each"
108 printf(" --deletion-rate: Enable deletion rate"
110 printf(" --dump-socket-mem: To dump all socket memory\n");
111 printf(" --enable-fwd: To enable packets forwarding"
112 " after insertion\n");
113 printf(" --portmask=N: hexadecimal bitmask of ports used\n");
115 printf("To set flow attributes:\n");
116 printf(" --ingress: set ingress attribute in flows\n");
117 printf(" --egress: set egress attribute in flows\n");
118 printf(" --transfer: set transfer attribute in flows\n");
119 printf(" --group=N: set group for all flows,"
120 " default is %d\n", DEFAULT_GROUP);
122 printf("To set flow items:\n");
123 printf(" --ether: add ether layer in flow items\n");
124 printf(" --vlan: add vlan layer in flow items\n");
125 printf(" --ipv4: add ipv4 layer in flow items\n");
126 printf(" --ipv6: add ipv6 layer in flow items\n");
127 printf(" --tcp: add tcp layer in flow items\n");
128 printf(" --udp: add udp layer in flow items\n");
129 printf(" --vxlan: add vxlan layer in flow items\n");
130 printf(" --vxlan-gpe: add vxlan-gpe layer in flow items\n");
131 printf(" --gre: add gre layer in flow items\n");
132 printf(" --geneve: add geneve layer in flow items\n");
133 printf(" --gtp: add gtp layer in flow items\n");
134 printf(" --meta: add meta layer in flow items\n");
135 printf(" --tag: add tag layer in flow items\n");
136 printf(" --icmpv4: add icmpv4 layer in flow items\n");
137 printf(" --icmpv6: add icmpv6 layer in flow items\n");
139 printf("To set flow actions:\n");
140 printf(" --port-id: add port-id action in flow actions\n");
141 printf(" --rss: add rss action in flow actions\n");
142 printf(" --queue: add queue action in flow actions\n");
143 printf(" --jump: add jump action in flow actions\n");
144 printf(" --mark: add mark action in flow actions\n");
145 printf(" --count: add count action in flow actions\n");
146 printf(" --set-meta: add set meta action in flow actions\n");
147 printf(" --set-tag: add set tag action in flow actions\n");
148 printf(" --drop: add drop action in flow actions\n");
149 printf(" --hairpin-queue=N: add hairpin-queue action in flow actions\n");
150 printf(" --hairpin-rss=N: add hairpin-rss action in flow actions\n");
151 printf(" --set-src-mac: add set src mac action to flow actions\n"
152 "Src mac to be set is random each flow\n");
153 printf(" --set-dst-mac: add set dst mac action to flow actions\n"
154 "Dst mac to be set is random each flow\n");
155 printf(" --set-src-ipv4: add set src ipv4 action to flow actions\n"
156 "Src ipv4 to be set is random each flow\n");
157 printf(" --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
158 "Dst ipv4 to be set is random each flow\n");
159 printf(" --set-src-ipv6: add set src ipv6 action to flow actions\n"
160 "Src ipv6 to be set is random each flow\n");
161 printf(" --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
162 "Dst ipv6 to be set is random each flow\n");
163 printf(" --set-src-tp: add set src tp action to flow actions\n"
164 "Src tp to be set is random each flow\n");
165 printf(" --set-dst-tp: add set dst tp action to flow actions\n"
166 "Dst tp to be set is random each flow\n");
167 printf(" --inc-tcp-ack: add inc tcp ack action to flow actions\n"
168 "tcp ack will be increments by 1\n");
169 printf(" --dec-tcp-ack: add dec tcp ack action to flow actions\n"
170 "tcp ack will be decrements by 1\n");
171 printf(" --inc-tcp-seq: add inc tcp seq action to flow actions\n"
172 "tcp seq will be increments by 1\n");
173 printf(" --dec-tcp-seq: add dec tcp seq action to flow actions\n"
174 "tcp seq will be decrements by 1\n");
175 printf(" --set-ttl: add set ttl action to flow actions\n"
176 "L3 ttl to be set is random each flow\n");
177 printf(" --dec-ttl: add dec ttl action to flow actions\n"
178 "L3 ttl will be decrements by 1\n");
179 printf(" --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
180 "ipv4 dscp value to be set is random each flow\n");
181 printf(" --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
182 "ipv6 dscp value to be set is random each flow\n");
183 printf(" --flag: add flag action to flow actions\n");
184 printf(" --raw-encap=<data>: add raw encap action to flow actions\n"
185 "Data is the data needed to be encaped\n"
186 "Example: raw-encap=ether,ipv4,udp,vxlan\n");
187 printf(" --raw-decap=<data>: add raw decap action to flow actions\n"
188 "Data is the data needed to be decaped\n"
189 "Example: raw-decap=ether,ipv4,udp,vxlan\n");
190 printf(" --vxlan-encap: add vxlan-encap action to flow actions\n"
191 "Encapped data is fixed with pattern: ether,ipv4,udp,vxlan\n"
192 "With fixed values\n");
193 printf(" --vxlan-decap: add vxlan_decap action to flow actions\n");
197 args_parse(int argc, char **argv)
207 static const struct option_dict {
216 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
217 .map = &flow_items[0],
218 .map_idx = &items_idx
222 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
223 .map = &flow_items[0],
224 .map_idx = &items_idx
228 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
229 .map = &flow_items[0],
230 .map_idx = &items_idx
234 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
235 .map = &flow_items[0],
236 .map_idx = &items_idx
240 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
241 .map = &flow_items[0],
242 .map_idx = &items_idx
246 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
247 .map = &flow_items[0],
248 .map_idx = &items_idx
252 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
253 .map = &flow_items[0],
254 .map_idx = &items_idx
258 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
259 .map = &flow_items[0],
260 .map_idx = &items_idx
264 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
265 .map = &flow_items[0],
266 .map_idx = &items_idx
270 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
271 .map = &flow_items[0],
272 .map_idx = &items_idx
276 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
277 .map = &flow_items[0],
278 .map_idx = &items_idx
282 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
283 .map = &flow_items[0],
284 .map_idx = &items_idx
288 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
289 .map = &flow_items[0],
290 .map_idx = &items_idx
294 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP),
295 .map = &flow_items[0],
296 .map_idx = &items_idx
300 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP6),
301 .map = &flow_items[0],
302 .map_idx = &items_idx
307 .map = &flow_attrs[0],
308 .map_idx = &attrs_idx
313 .map = &flow_attrs[0],
314 .map_idx = &attrs_idx
319 .map = &flow_attrs[0],
320 .map_idx = &attrs_idx
324 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
325 .map = &flow_actions[0],
326 .map_idx = &actions_idx
330 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
331 .map = &flow_actions[0],
332 .map_idx = &actions_idx
336 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
337 .map = &flow_actions[0],
338 .map_idx = &actions_idx
342 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
343 .map = &flow_actions[0],
344 .map_idx = &actions_idx
348 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
349 .map = &flow_actions[0],
350 .map_idx = &actions_idx
354 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
355 .map = &flow_actions[0],
356 .map_idx = &actions_idx
360 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
361 .map = &flow_actions[0],
362 .map_idx = &actions_idx
366 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
367 .map = &flow_actions[0],
368 .map_idx = &actions_idx
372 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
373 .map = &flow_actions[0],
374 .map_idx = &actions_idx
377 .str = "set-src-mac",
378 .mask = FLOW_ACTION_MASK(
379 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
381 .map = &flow_actions[0],
382 .map_idx = &actions_idx
385 .str = "set-dst-mac",
386 .mask = FLOW_ACTION_MASK(
387 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
389 .map = &flow_actions[0],
390 .map_idx = &actions_idx
393 .str = "set-src-ipv4",
394 .mask = FLOW_ACTION_MASK(
395 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
397 .map = &flow_actions[0],
398 .map_idx = &actions_idx
401 .str = "set-dst-ipv4",
402 .mask = FLOW_ACTION_MASK(
403 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
405 .map = &flow_actions[0],
406 .map_idx = &actions_idx
409 .str = "set-src-ipv6",
410 .mask = FLOW_ACTION_MASK(
411 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
413 .map = &flow_actions[0],
414 .map_idx = &actions_idx
417 .str = "set-dst-ipv6",
418 .mask = FLOW_ACTION_MASK(
419 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
421 .map = &flow_actions[0],
422 .map_idx = &actions_idx
426 .mask = FLOW_ACTION_MASK(
427 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
429 .map = &flow_actions[0],
430 .map_idx = &actions_idx
434 .mask = FLOW_ACTION_MASK(
435 RTE_FLOW_ACTION_TYPE_SET_TP_DST
437 .map = &flow_actions[0],
438 .map_idx = &actions_idx
441 .str = "inc-tcp-ack",
442 .mask = FLOW_ACTION_MASK(
443 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
445 .map = &flow_actions[0],
446 .map_idx = &actions_idx
449 .str = "dec-tcp-ack",
450 .mask = FLOW_ACTION_MASK(
451 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
453 .map = &flow_actions[0],
454 .map_idx = &actions_idx
457 .str = "inc-tcp-seq",
458 .mask = FLOW_ACTION_MASK(
459 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
461 .map = &flow_actions[0],
462 .map_idx = &actions_idx
465 .str = "dec-tcp-seq",
466 .mask = FLOW_ACTION_MASK(
467 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
469 .map = &flow_actions[0],
470 .map_idx = &actions_idx
474 .mask = FLOW_ACTION_MASK(
475 RTE_FLOW_ACTION_TYPE_SET_TTL
477 .map = &flow_actions[0],
478 .map_idx = &actions_idx
482 .mask = FLOW_ACTION_MASK(
483 RTE_FLOW_ACTION_TYPE_DEC_TTL
485 .map = &flow_actions[0],
486 .map_idx = &actions_idx
489 .str = "set-ipv4-dscp",
490 .mask = FLOW_ACTION_MASK(
491 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
493 .map = &flow_actions[0],
494 .map_idx = &actions_idx
497 .str = "set-ipv6-dscp",
498 .mask = FLOW_ACTION_MASK(
499 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
501 .map = &flow_actions[0],
502 .map_idx = &actions_idx
506 .mask = FLOW_ACTION_MASK(
507 RTE_FLOW_ACTION_TYPE_FLAG
509 .map = &flow_actions[0],
510 .map_idx = &actions_idx
513 .str = "vxlan-encap",
514 .mask = FLOW_ACTION_MASK(
515 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP
517 .map = &flow_actions[0],
518 .map_idx = &actions_idx
521 .str = "vxlan-decap",
522 .mask = FLOW_ACTION_MASK(
523 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP
525 .map = &flow_actions[0],
526 .map_idx = &actions_idx
530 static const struct option lgopts[] = {
533 { "rules-count", 1, 0, 0 },
534 { "rules-batch", 1, 0, 0 },
535 { "dump-iterations", 0, 0, 0 },
536 { "deletion-rate", 0, 0, 0 },
537 { "dump-socket-mem", 0, 0, 0 },
538 { "enable-fwd", 0, 0, 0 },
539 { "portmask", 1, 0, 0 },
541 { "ingress", 0, 0, 0 },
542 { "egress", 0, 0, 0 },
543 { "transfer", 0, 0, 0 },
544 { "group", 1, 0, 0 },
546 { "ether", 0, 0, 0 },
552 { "vxlan", 0, 0, 0 },
553 { "vxlan-gpe", 0, 0, 0 },
555 { "geneve", 0, 0, 0 },
559 { "icmpv4", 0, 0, 0 },
560 { "icmpv6", 0, 0, 0 },
562 { "port-id", 0, 0, 0 },
564 { "queue", 0, 0, 0 },
567 { "count", 0, 0, 0 },
568 { "set-meta", 0, 0, 0 },
569 { "set-tag", 0, 0, 0 },
571 { "hairpin-queue", 1, 0, 0 },
572 { "hairpin-rss", 1, 0, 0 },
573 { "set-src-mac", 0, 0, 0 },
574 { "set-dst-mac", 0, 0, 0 },
575 { "set-src-ipv4", 0, 0, 0 },
576 { "set-dst-ipv4", 0, 0, 0 },
577 { "set-src-ipv6", 0, 0, 0 },
578 { "set-dst-ipv6", 0, 0, 0 },
579 { "set-src-tp", 0, 0, 0 },
580 { "set-dst-tp", 0, 0, 0 },
581 { "inc-tcp-ack", 0, 0, 0 },
582 { "dec-tcp-ack", 0, 0, 0 },
583 { "inc-tcp-seq", 0, 0, 0 },
584 { "dec-tcp-seq", 0, 0, 0 },
585 { "set-ttl", 0, 0, 0 },
586 { "dec-ttl", 0, 0, 0 },
587 { "set-ipv4-dscp", 0, 0, 0 },
588 { "set-ipv6-dscp", 0, 0, 0 },
590 { "raw-encap", 1, 0, 0 },
591 { "raw-decap", 1, 0, 0 },
592 { "vxlan-encap", 0, 0, 0 },
593 { "vxlan-decap", 0, 0, 0 },
596 RTE_ETH_FOREACH_DEV(i)
597 ports_mask |= 1 << i;
599 hairpin_queues_num = 0;
602 printf(":: Flow -> ");
603 while ((opt = getopt_long(argc, argvopt, "",
604 lgopts, &opt_idx)) != EOF) {
607 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
609 rte_exit(EXIT_SUCCESS, "Displayed help\n");
612 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
617 rte_exit(EXIT_SUCCESS,
618 "flow group should be >= 0\n");
619 printf("group %d / ", flow_group);
622 for (i = 0; i < RTE_DIM(flow_options); i++)
623 if (strcmp(lgopts[opt_idx].name,
624 flow_options[i].str) == 0) {
626 (*flow_options[i].map_idx)++] =
627 flow_options[i].mask;
628 printf("%s / ", flow_options[i].str);
631 if (strcmp(lgopts[opt_idx].name,
632 "hairpin-rss") == 0) {
635 hairpin_queues_num = n;
637 rte_exit(EXIT_SUCCESS,
638 "Hairpin queues should be > 0\n");
640 flow_actions[actions_idx++] =
642 printf("hairpin-rss / ");
644 if (strcmp(lgopts[opt_idx].name,
645 "hairpin-queue") == 0) {
648 hairpin_queues_num = n;
650 rte_exit(EXIT_SUCCESS,
651 "Hairpin queues should be > 0\n");
653 flow_actions[actions_idx++] =
654 HAIRPIN_QUEUE_ACTION;
655 printf("hairpin-queue / ");
658 if (strcmp(lgopts[opt_idx].name, "raw-encap") == 0) {
659 printf("raw-encap ");
660 flow_actions[actions_idx++] =
662 RTE_FLOW_ACTION_TYPE_RAW_ENCAP
665 token = strtok(optarg, ",");
666 while (token != NULL) {
667 for (i = 0; i < RTE_DIM(flow_options); i++) {
668 if (strcmp(flow_options[i].str, token) == 0) {
669 printf("%s,", token);
670 encap_data |= flow_options[i].mask;
673 /* Reached last item with no match */
674 if (i == (RTE_DIM(flow_options) - 1)) {
675 fprintf(stderr, "Invalid encap item: %s\n", token);
677 rte_exit(EXIT_SUCCESS, "Invalid encap item\n");
680 token = strtok(NULL, ",");
684 if (strcmp(lgopts[opt_idx].name, "raw-decap") == 0) {
685 printf("raw-decap ");
686 flow_actions[actions_idx++] =
688 RTE_FLOW_ACTION_TYPE_RAW_DECAP
691 token = strtok(optarg, ",");
692 while (token != NULL) {
693 for (i = 0; i < RTE_DIM(flow_options); i++) {
694 if (strcmp(flow_options[i].str, token) == 0) {
695 printf("%s,", token);
696 encap_data |= flow_options[i].mask;
699 /* Reached last item with no match */
700 if (i == (RTE_DIM(flow_options) - 1)) {
701 fprintf(stderr, "Invalid decap item: %s\n", token);
703 rte_exit(EXIT_SUCCESS, "Invalid decap item\n");
706 token = strtok(NULL, ",");
711 if (strcmp(lgopts[opt_idx].name,
712 "rules-batch") == 0) {
714 if (n >= DEFAULT_RULES_BATCH)
717 printf("\n\nrules_batch should be >= %d\n",
718 DEFAULT_RULES_BATCH);
719 rte_exit(EXIT_SUCCESS, " ");
722 if (strcmp(lgopts[opt_idx].name,
723 "rules-count") == 0) {
725 if (n >= (int) rules_batch)
728 printf("\n\nrules_count should be >= %d\n",
732 if (strcmp(lgopts[opt_idx].name,
733 "dump-iterations") == 0)
734 dump_iterations = true;
735 if (strcmp(lgopts[opt_idx].name,
736 "deletion-rate") == 0)
738 if (strcmp(lgopts[opt_idx].name,
739 "dump-socket-mem") == 0)
740 dump_socket_mem_flag = true;
741 if (strcmp(lgopts[opt_idx].name,
744 if (strcmp(lgopts[opt_idx].name,
746 /* parse hexadecimal string */
748 pm = strtoull(optarg, &end, 16);
749 if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
750 rte_exit(EXIT_FAILURE, "Invalid fwd port mask\n");
755 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
757 rte_exit(EXIT_SUCCESS, "Invalid option\n");
761 printf("end_flow\n");
764 /* Dump the socket memory statistics on console */
766 dump_socket_mem(FILE *f)
768 struct rte_malloc_socket_stats socket_stats;
773 unsigned int n_alloc = 0;
774 unsigned int n_free = 0;
775 bool active_nodes = false;
778 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
779 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
780 !socket_stats.heap_totalsz_bytes)
783 total += socket_stats.heap_totalsz_bytes;
784 alloc += socket_stats.heap_allocsz_bytes;
785 free += socket_stats.heap_freesz_bytes;
786 n_alloc += socket_stats.alloc_count;
787 n_free += socket_stats.free_count;
788 if (dump_socket_mem_flag) {
789 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
791 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
792 " %.6lf(%.3lf%%)\nfree: %.6lf"
794 "\ncount alloc: %u\nfree: %u\n",
796 socket_stats.heap_totalsz_bytes / 1.0e6,
797 socket_stats.heap_allocsz_bytes / 1.0e6,
798 (double)socket_stats.heap_allocsz_bytes * 100 /
799 (double)socket_stats.heap_totalsz_bytes,
800 socket_stats.heap_freesz_bytes / 1.0e6,
801 socket_stats.greatest_free_size / 1.0e6,
802 socket_stats.alloc_count,
803 socket_stats.free_count);
804 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
807 if (dump_socket_mem_flag && active_nodes) {
809 "\nTotal: size(M)\ntotal: %.6lf"
810 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
811 "\ncount alloc: %u\nfree: %u\n",
812 total / 1.0e6, alloc / 1.0e6,
813 (double)alloc * 100 / (double)total, free / 1.0e6,
815 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
821 print_flow_error(struct rte_flow_error error)
823 printf("Flow can't be created %d message: %s\n",
825 error.message ? error.message : "(no stated reason)");
829 print_rules_batches(double *cpu_time_per_batch)
835 for (idx = 0; idx < MAX_BATCHES_COUNT; idx++) {
836 if (!cpu_time_per_batch[idx])
838 delta = (double)(rules_batch / cpu_time_per_batch[idx]);
839 rate = delta / 1000; /* Save rate in K unit. */
840 printf(":: Rules batch #%d: %d rules "
841 "in %f sec[ Rate = %f K Rule/Sec ]\n",
843 cpu_time_per_batch[idx], rate);
848 destroy_flows(int port_id, struct rte_flow **flows_list)
850 struct rte_flow_error error;
851 clock_t start_batch, end_batch;
852 double cpu_time_used = 0;
853 double deletion_rate;
854 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
860 printf("\nRules Deletion on port = %d\n", port_id);
862 start_batch = clock();
863 for (i = 0; i < rules_count; i++) {
864 if (flows_list[i] == 0)
867 memset(&error, 0x33, sizeof(error));
868 if (rte_flow_destroy(port_id, flows_list[i], &error)) {
869 print_flow_error(error);
870 rte_exit(EXIT_FAILURE, "Error in deleting flow");
874 * Save the deletion rate for rules batch.
875 * Check if the deletion reached the rules
876 * patch counter, then save the deletion rate
879 if (!((i + 1) % rules_batch)) {
881 delta = (double) (end_batch - start_batch);
882 rules_batch_idx = ((i + 1) / rules_batch) - 1;
883 cpu_time_per_batch[rules_batch_idx] = delta / CLOCKS_PER_SEC;
884 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
885 start_batch = clock();
889 /* Print deletion rates for all batches */
891 print_rules_batches(cpu_time_per_batch);
893 /* Deletion rate for all rules */
894 deletion_rate = ((double) (rules_count / cpu_time_used) / 1000);
895 printf(":: Total rules deletion rate -> %f K Rule/Sec\n",
897 printf(":: The time for deleting %d in rules %f seconds\n",
898 rules_count, cpu_time_used);
901 static struct rte_flow **
902 insert_flows(int port_id)
904 struct rte_flow **flows_list;
905 struct rte_flow_error error;
906 clock_t start_batch, end_batch;
907 double cpu_time_used;
908 double insertion_rate;
909 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
913 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
914 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
917 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
918 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
920 flows_list = rte_zmalloc("flows_list",
921 (sizeof(struct rte_flow *) * rules_count) + 1, 0);
922 if (flows_list == NULL)
923 rte_exit(EXIT_FAILURE, "No Memory available!");
927 if (flow_group > 0) {
929 * Create global rule to jump into flow_group,
930 * this way the app will avoid the default rules.
933 * group 0 eth / end actions jump group <flow_group>
935 flow = generate_flow(port_id, 0, flow_attrs,
936 global_items, global_actions,
937 flow_group, 0, 0, 0, 0, &error);
940 print_flow_error(error);
941 rte_exit(EXIT_FAILURE, "error in creating flow");
943 flows_list[flow_index++] = flow;
947 printf("Rules insertion on port = %d\n", port_id);
948 start_batch = clock();
949 for (counter = 0; counter < rules_count; counter++) {
950 flow = generate_flow(port_id, flow_group,
951 flow_attrs, flow_items, flow_actions,
952 JUMP_ACTION_TABLE, counter,
954 encap_data, decap_data,
958 counter = rules_count;
961 print_flow_error(error);
962 rte_exit(EXIT_FAILURE, "error in creating flow");
965 flows_list[flow_index++] = flow;
968 * Save the insertion rate for rules batch.
969 * Check if the insertion reached the rules
970 * patch counter, then save the insertion rate
973 if (!((counter + 1) % rules_batch)) {
975 delta = (double) (end_batch - start_batch);
976 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
977 cpu_time_per_batch[rules_batch_idx] = delta / CLOCKS_PER_SEC;
978 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
979 start_batch = clock();
983 /* Print insertion rates for all batches */
985 print_rules_batches(cpu_time_per_batch);
987 /* Insertion rate for all rules */
988 insertion_rate = ((double) (rules_count / cpu_time_used) / 1000);
989 printf(":: Total flow insertion rate -> %f K Rule/Sec\n",
991 printf(":: The time for creating %d in flows %f seconds\n",
992 rules_count, cpu_time_used);
1000 struct rte_flow **flows_list;
1002 int64_t alloc, last_alloc;
1003 int flow_size_in_bytes;
1006 nr_ports = rte_eth_dev_count_avail();
1008 if (rules_batch > rules_count)
1009 rules_batch = rules_count;
1011 printf(":: Rules Count per port: %d\n\n", rules_count);
1013 for (port_id = 0; port_id < nr_ports; port_id++) {
1014 /* If port outside portmask */
1015 if (!((ports_mask >> port_id) & 0x1))
1018 /* Insertion part. */
1019 last_alloc = (int64_t)dump_socket_mem(stdout);
1020 flows_list = insert_flows(port_id);
1021 alloc = (int64_t)dump_socket_mem(stdout);
1023 /* Deletion part. */
1025 destroy_flows(port_id, flows_list);
1027 /* Report rte_flow size in huge pages. */
1029 flow_size_in_bytes = (alloc - last_alloc) / rules_count;
1030 printf("\n:: rte_flow size in DPDK layer: %d Bytes",
1031 flow_size_in_bytes);
1037 signal_handler(int signum)
1039 if (signum == SIGINT || signum == SIGTERM) {
1040 printf("\n\nSignal %d received, preparing to exit...\n",
1042 printf("Error: Stats are wrong due to sudden signal!\n\n");
1047 static inline uint16_t
1048 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
1051 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
1057 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
1063 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
1064 li->tx_pkts += nr_tx;
1065 li->tx_drops += cnt - nr_tx;
1067 for (i = nr_tx; i < cnt; i++)
1068 rte_pktmbuf_free(li->pkts[i]);
1072 * Method to convert numbers into pretty numbers that easy
1073 * to read. The design here is to add comma after each three
1074 * digits and set all of this inside buffer.
1076 * For example if n = 1799321, the output will be
1077 * 1,799,321 after this method which is easier to read.
1080 pretty_number(uint64_t n, char *buf)
1087 sprintf(p[i], "%03d", (int)(n % 1000));
1092 sprintf(p[i++], "%d", (int)n);
1095 off += sprintf(buf + off, "%s,", p[i]);
1096 buf[strlen(buf) - 1] = '\0';
1102 packet_per_second_stats(void)
1104 struct lcore_info *old;
1105 struct lcore_info *li, *oli;
1109 old = rte_zmalloc("old",
1110 sizeof(struct lcore_info) * MAX_LCORES, 0);
1112 rte_exit(EXIT_FAILURE, "No Memory available!");
1114 memcpy(old, lcore_infos,
1115 sizeof(struct lcore_info) * MAX_LCORES);
1117 while (!force_quit) {
1118 uint64_t total_tx_pkts = 0;
1119 uint64_t total_rx_pkts = 0;
1120 uint64_t total_tx_drops = 0;
1121 uint64_t tx_delta, rx_delta, drops_delta;
1123 int nr_valid_core = 0;
1128 char go_up_nr_lines[16];
1130 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
1131 printf("%s\r", go_up_nr_lines);
1134 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
1135 printf("%6s %16s %16s %16s\n", "------", "----------------",
1136 "----------------", "----------------");
1138 for (i = 0; i < MAX_LCORES; i++) {
1139 li = &lcore_infos[i];
1141 if (li->mode != LCORE_MODE_PKT)
1144 tx_delta = li->tx_pkts - oli->tx_pkts;
1145 rx_delta = li->rx_pkts - oli->rx_pkts;
1146 drops_delta = li->tx_drops - oli->tx_drops;
1147 printf("%6d %16s %16s %16s\n", i,
1148 pretty_number(tx_delta, buf[0]),
1149 pretty_number(drops_delta, buf[1]),
1150 pretty_number(rx_delta, buf[2]));
1152 total_tx_pkts += tx_delta;
1153 total_rx_pkts += rx_delta;
1154 total_tx_drops += drops_delta;
1160 if (nr_valid_core > 1) {
1161 printf("%6s %16s %16s %16s\n", "total",
1162 pretty_number(total_tx_pkts, buf[0]),
1163 pretty_number(total_tx_drops, buf[1]),
1164 pretty_number(total_rx_pkts, buf[2]));
1168 memcpy(old, lcore_infos,
1169 sizeof(struct lcore_info) * MAX_LCORES);
1174 start_forwarding(void *data __rte_unused)
1176 int lcore = rte_lcore_id();
1179 struct lcore_info *li = &lcore_infos[lcore];
1184 if (li->mode == LCORE_MODE_STATS) {
1185 printf(":: started stats on lcore %u\n", lcore);
1186 packet_per_second_stats();
1191 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1192 if (li->streams[stream_id].rx_port == -1)
1196 li->streams[stream_id].rx_port,
1197 li->streams[stream_id].rx_queue);
1200 li->streams[stream_id].tx_port,
1201 li->streams[stream_id].tx_queue);
1207 init_lcore_info(void)
1215 int streams_per_core;
1216 int unassigned_streams;
1218 nr_port = rte_eth_dev_count_avail();
1220 /* First logical core is reserved for stats printing */
1221 lcore = rte_get_next_lcore(-1, 0, 0);
1222 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1225 * Initialize all cores
1226 * All cores at first must have -1 value in all streams
1227 * This means that this stream is not used, or not set
1230 for (i = 0; i < MAX_LCORES; i++)
1231 for (j = 0; j < MAX_STREAMS; j++) {
1232 lcore_infos[i].streams[j].tx_port = -1;
1233 lcore_infos[i].streams[j].rx_port = -1;
1234 lcore_infos[i].streams[j].tx_queue = -1;
1235 lcore_infos[i].streams[j].rx_queue = -1;
1236 lcore_infos[i].streams_nb = 0;
1240 * Calculate the total streams count.
1241 * Also distribute those streams count between the available
1242 * logical cores except first core, since it's reserved for
1245 nb_fwd_streams = nr_port * RXQ_NUM;
1246 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1247 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1248 lcore = rte_get_next_lcore(lcore, 0, 0);
1249 lcore_infos[lcore].streams_nb = 1;
1252 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1253 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1254 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1255 lcore = rte_get_next_lcore(lcore, 0, 0);
1256 lcore_infos[lcore].streams_nb = streams_per_core;
1257 if (unassigned_streams) {
1258 lcore_infos[lcore].streams_nb++;
1259 unassigned_streams--;
1265 * Set the streams for the cores according to each logical
1266 * core stream count.
1267 * The streams is built on the design of what received should
1268 * forward as well, this means that if you received packets on
1269 * port 0 queue 0 then the same queue should forward the
1270 * packets, using the same logical core.
1272 lcore = rte_get_next_lcore(-1, 0, 0);
1273 for (port = 0; port < nr_port; port++) {
1274 /* Create FWD stream */
1275 for (queue = 0; queue < RXQ_NUM; queue++) {
1276 if (!lcore_infos[lcore].streams_nb ||
1277 !(stream_id % lcore_infos[lcore].streams_nb)) {
1278 lcore = rte_get_next_lcore(lcore, 0, 0);
1279 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1282 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1283 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1284 lcore_infos[lcore].streams[stream_id].rx_port = port;
1285 lcore_infos[lcore].streams[stream_id].tx_port = port;
1290 /* Print all streams */
1291 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1292 for (i = 0; i < MAX_LCORES; i++)
1293 for (j = 0; j < MAX_STREAMS; j++) {
1294 /* No streams for this core */
1295 if (lcore_infos[i].streams[j].tx_port == -1)
1297 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1299 lcore_infos[i].streams[j].rx_port,
1300 lcore_infos[i].streams[j].rx_queue,
1301 lcore_infos[i].streams[j].tx_port,
1302 lcore_infos[i].streams[j].tx_queue);
1311 uint16_t hairpin_queue;
1315 struct rte_eth_hairpin_conf hairpin_conf = {
1318 struct rte_eth_conf port_conf = {
1324 struct rte_eth_txconf txq_conf;
1325 struct rte_eth_rxconf rxq_conf;
1326 struct rte_eth_dev_info dev_info;
1328 nr_queues = RXQ_NUM;
1329 if (hairpin_queues_num != 0)
1330 nr_queues = RXQ_NUM + hairpin_queues_num;
1332 nr_ports = rte_eth_dev_count_avail();
1334 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1336 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1337 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1340 if (mbuf_mp == NULL)
1341 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1343 for (port_id = 0; port_id < nr_ports; port_id++) {
1344 ret = rte_eth_dev_info_get(port_id, &dev_info);
1346 rte_exit(EXIT_FAILURE,
1347 "Error during getting device"
1348 " (port %u) info: %s\n",
1349 port_id, strerror(-ret));
1351 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1352 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1354 printf(":: initializing port: %d\n", port_id);
1356 ret = rte_eth_dev_configure(port_id, nr_queues,
1357 nr_queues, &port_conf);
1359 rte_exit(EXIT_FAILURE,
1360 ":: cannot configure device: err=%d, port=%u\n",
1363 rxq_conf = dev_info.default_rxconf;
1364 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1365 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1366 rte_eth_dev_socket_id(port_id),
1370 rte_exit(EXIT_FAILURE,
1371 ":: Rx queue setup failed: err=%d, port=%u\n",
1375 txq_conf = dev_info.default_txconf;
1376 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1377 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1378 rte_eth_dev_socket_id(port_id),
1381 rte_exit(EXIT_FAILURE,
1382 ":: Tx queue setup failed: err=%d, port=%u\n",
1386 /* Catch all packets from traffic generator. */
1387 ret = rte_eth_promiscuous_enable(port_id);
1389 rte_exit(EXIT_FAILURE,
1390 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1391 rte_strerror(-ret), port_id);
1393 if (hairpin_queues_num != 0) {
1395 * Configure peer which represents hairpin Tx.
1396 * Hairpin queue numbers start after standard queues
1397 * (RXQ_NUM and TXQ_NUM).
1399 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1400 hairpin_queue < nr_queues;
1401 hairpin_queue++, std_queue++) {
1402 hairpin_conf.peers[0].port = port_id;
1403 hairpin_conf.peers[0].queue =
1404 std_queue + TXQ_NUM;
1405 ret = rte_eth_rx_hairpin_queue_setup(
1406 port_id, hairpin_queue,
1407 NR_RXD, &hairpin_conf);
1409 rte_exit(EXIT_FAILURE,
1410 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1414 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1415 hairpin_queue < nr_queues;
1416 hairpin_queue++, std_queue++) {
1417 hairpin_conf.peers[0].port = port_id;
1418 hairpin_conf.peers[0].queue =
1419 std_queue + RXQ_NUM;
1420 ret = rte_eth_tx_hairpin_queue_setup(
1421 port_id, hairpin_queue,
1422 NR_TXD, &hairpin_conf);
1424 rte_exit(EXIT_FAILURE,
1425 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1430 ret = rte_eth_dev_start(port_id);
1432 rte_exit(EXIT_FAILURE,
1433 "rte_eth_dev_start:err=%d, port=%u\n",
1436 printf(":: initializing port: %d done\n", port_id);
1441 main(int argc, char **argv)
1445 struct rte_flow_error error;
1447 ret = rte_eal_init(argc, argv);
1449 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1452 dump_iterations = false;
1453 rules_count = DEFAULT_RULES_COUNT;
1454 rules_batch = DEFAULT_RULES_BATCH;
1455 delete_flag = false;
1456 dump_socket_mem_flag = false;
1457 flow_group = DEFAULT_GROUP;
1459 signal(SIGINT, signal_handler);
1460 signal(SIGTERM, signal_handler);
1465 args_parse(argc, argv);
1469 nb_lcores = rte_lcore_count();
1471 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1477 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MAIN);
1480 RTE_ETH_FOREACH_DEV(port) {
1481 rte_flow_flush(port, &error);
1482 if (rte_eth_dev_stop(port) != 0)
1483 printf("Failed to stop device on port %u\n", port);
1484 rte_eth_dev_close(port);
1486 printf("\nBye ...\n");