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>
42 #define MAX_BATCHES_COUNT 100
43 #define DEFAULT_RULES_COUNT 4000000
44 #define DEFAULT_RULES_BATCH 100000
45 #define DEFAULT_GROUP 0
47 struct rte_flow *flow;
48 static uint8_t flow_group;
50 static uint64_t encap_data;
51 static uint64_t decap_data;
53 static uint64_t flow_items[MAX_ITEMS_NUM];
54 static uint64_t flow_actions[MAX_ACTIONS_NUM];
55 static uint64_t flow_attrs[MAX_ATTRS_NUM];
56 static uint8_t items_idx, actions_idx, attrs_idx;
58 static uint64_t ports_mask;
59 static volatile bool force_quit;
60 static bool dump_iterations;
61 static bool delete_flag;
62 static bool dump_socket_mem_flag;
63 static bool enable_fwd;
64 static bool unique_data;
66 static struct rte_mempool *mbuf_mp;
67 static uint32_t nb_lcores;
68 static uint32_t rules_count;
69 static uint32_t rules_batch;
70 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
71 static uint32_t nb_lcores;
73 #define MAX_PKT_BURST 32
74 #define LCORE_MODE_PKT 1
75 #define LCORE_MODE_STATS 2
76 #define MAX_STREAMS 64
77 #define METER_CREATE 1
78 #define METER_DELETE 2
90 struct stream streams[MAX_STREAMS];
95 struct rte_mbuf *pkts[MAX_PKT_BURST];
96 } __rte_cache_aligned;
98 static struct lcore_info lcore_infos[RTE_MAX_LCORE];
100 struct used_cpu_time {
101 double insertion[MAX_PORTS][RTE_MAX_LCORE];
102 double deletion[MAX_PORTS][RTE_MAX_LCORE];
105 struct multi_cores_pool {
106 uint32_t cores_count;
107 uint32_t rules_count;
108 struct used_cpu_time meters_record;
109 struct used_cpu_time flows_record;
110 int64_t last_alloc[RTE_MAX_LCORE];
111 int64_t current_alloc[RTE_MAX_LCORE];
112 } __rte_cache_aligned;
114 static struct multi_cores_pool mc_pool = {
119 usage(char *progname)
121 printf("\nusage: %s\n", progname);
122 printf("\nControl configurations:\n");
123 printf(" --rules-count=N: to set the number of needed"
124 " rules to insert, default is %d\n", DEFAULT_RULES_COUNT);
125 printf(" --rules-batch=N: set number of batched rules,"
126 " default is %d\n", DEFAULT_RULES_BATCH);
127 printf(" --dump-iterations: To print rates for each"
129 printf(" --deletion-rate: Enable deletion rate"
131 printf(" --dump-socket-mem: To dump all socket memory\n");
132 printf(" --enable-fwd: To enable packets forwarding"
133 " after insertion\n");
134 printf(" --portmask=N: hexadecimal bitmask of ports used\n");
135 printf(" --unique-data: flag to set using unique data for all"
136 " actions that support data, such as header modify and encap actions\n");
138 printf("To set flow attributes:\n");
139 printf(" --ingress: set ingress attribute in flows\n");
140 printf(" --egress: set egress attribute in flows\n");
141 printf(" --transfer: set transfer attribute in flows\n");
142 printf(" --group=N: set group for all flows,"
143 " default is %d\n", DEFAULT_GROUP);
144 printf(" --cores=N: to set the number of needed "
145 "cores to insert rte_flow rules, default is 1\n");
147 printf("To set flow items:\n");
148 printf(" --ether: add ether layer in flow items\n");
149 printf(" --vlan: add vlan layer in flow items\n");
150 printf(" --ipv4: add ipv4 layer in flow items\n");
151 printf(" --ipv6: add ipv6 layer in flow items\n");
152 printf(" --tcp: add tcp layer in flow items\n");
153 printf(" --udp: add udp layer in flow items\n");
154 printf(" --vxlan: add vxlan layer in flow items\n");
155 printf(" --vxlan-gpe: add vxlan-gpe layer in flow items\n");
156 printf(" --gre: add gre layer in flow items\n");
157 printf(" --geneve: add geneve layer in flow items\n");
158 printf(" --gtp: add gtp layer in flow items\n");
159 printf(" --meta: add meta layer in flow items\n");
160 printf(" --tag: add tag layer in flow items\n");
161 printf(" --icmpv4: add icmpv4 layer in flow items\n");
162 printf(" --icmpv6: add icmpv6 layer in flow items\n");
164 printf("To set flow actions:\n");
165 printf(" --port-id: add port-id action in flow actions\n");
166 printf(" --rss: add rss action in flow actions\n");
167 printf(" --queue: add queue action in flow actions\n");
168 printf(" --jump: add jump action in flow actions\n");
169 printf(" --mark: add mark action in flow actions\n");
170 printf(" --count: add count action in flow actions\n");
171 printf(" --set-meta: add set meta action in flow actions\n");
172 printf(" --set-tag: add set tag action in flow actions\n");
173 printf(" --drop: add drop action in flow actions\n");
174 printf(" --hairpin-queue=N: add hairpin-queue action in flow actions\n");
175 printf(" --hairpin-rss=N: add hairpin-rss action in flow actions\n");
176 printf(" --set-src-mac: add set src mac action to flow actions\n"
177 "Src mac to be set is random each flow\n");
178 printf(" --set-dst-mac: add set dst mac action to flow actions\n"
179 "Dst mac to be set is random each flow\n");
180 printf(" --set-src-ipv4: add set src ipv4 action to flow actions\n"
181 "Src ipv4 to be set is random each flow\n");
182 printf(" --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
183 "Dst ipv4 to be set is random each flow\n");
184 printf(" --set-src-ipv6: add set src ipv6 action to flow actions\n"
185 "Src ipv6 to be set is random each flow\n");
186 printf(" --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
187 "Dst ipv6 to be set is random each flow\n");
188 printf(" --set-src-tp: add set src tp action to flow actions\n"
189 "Src tp to be set is random each flow\n");
190 printf(" --set-dst-tp: add set dst tp action to flow actions\n"
191 "Dst tp to be set is random each flow\n");
192 printf(" --inc-tcp-ack: add inc tcp ack action to flow actions\n"
193 "tcp ack will be increments by 1\n");
194 printf(" --dec-tcp-ack: add dec tcp ack action to flow actions\n"
195 "tcp ack will be decrements by 1\n");
196 printf(" --inc-tcp-seq: add inc tcp seq action to flow actions\n"
197 "tcp seq will be increments by 1\n");
198 printf(" --dec-tcp-seq: add dec tcp seq action to flow actions\n"
199 "tcp seq will be decrements by 1\n");
200 printf(" --set-ttl: add set ttl action to flow actions\n"
201 "L3 ttl to be set is random each flow\n");
202 printf(" --dec-ttl: add dec ttl action to flow actions\n"
203 "L3 ttl will be decrements by 1\n");
204 printf(" --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
205 "ipv4 dscp value to be set is random each flow\n");
206 printf(" --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
207 "ipv6 dscp value to be set is random each flow\n");
208 printf(" --flag: add flag action to flow actions\n");
209 printf(" --meter: add meter action to flow actions\n");
210 printf(" --raw-encap=<data>: add raw encap action to flow actions\n"
211 "Data is the data needed to be encaped\n"
212 "Example: raw-encap=ether,ipv4,udp,vxlan\n");
213 printf(" --raw-decap=<data>: add raw decap action to flow actions\n"
214 "Data is the data needed to be decaped\n"
215 "Example: raw-decap=ether,ipv4,udp,vxlan\n");
216 printf(" --vxlan-encap: add vxlan-encap action to flow actions\n"
217 "Encapped data is fixed with pattern: ether,ipv4,udp,vxlan\n"
218 "With fixed values\n");
219 printf(" --vxlan-decap: add vxlan_decap action to flow actions\n");
223 args_parse(int argc, char **argv)
233 static const struct option_dict {
242 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
243 .map = &flow_items[0],
244 .map_idx = &items_idx
248 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
249 .map = &flow_items[0],
250 .map_idx = &items_idx
254 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
255 .map = &flow_items[0],
256 .map_idx = &items_idx
260 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
261 .map = &flow_items[0],
262 .map_idx = &items_idx
266 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
267 .map = &flow_items[0],
268 .map_idx = &items_idx
272 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
273 .map = &flow_items[0],
274 .map_idx = &items_idx
278 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
279 .map = &flow_items[0],
280 .map_idx = &items_idx
284 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
285 .map = &flow_items[0],
286 .map_idx = &items_idx
290 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
291 .map = &flow_items[0],
292 .map_idx = &items_idx
296 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
297 .map = &flow_items[0],
298 .map_idx = &items_idx
302 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
303 .map = &flow_items[0],
304 .map_idx = &items_idx
308 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
309 .map = &flow_items[0],
310 .map_idx = &items_idx
314 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
315 .map = &flow_items[0],
316 .map_idx = &items_idx
320 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP),
321 .map = &flow_items[0],
322 .map_idx = &items_idx
326 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP6),
327 .map = &flow_items[0],
328 .map_idx = &items_idx
333 .map = &flow_attrs[0],
334 .map_idx = &attrs_idx
339 .map = &flow_attrs[0],
340 .map_idx = &attrs_idx
345 .map = &flow_attrs[0],
346 .map_idx = &attrs_idx
350 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
351 .map = &flow_actions[0],
352 .map_idx = &actions_idx
356 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
357 .map = &flow_actions[0],
358 .map_idx = &actions_idx
362 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
363 .map = &flow_actions[0],
364 .map_idx = &actions_idx
368 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
369 .map = &flow_actions[0],
370 .map_idx = &actions_idx
374 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
375 .map = &flow_actions[0],
376 .map_idx = &actions_idx
380 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
381 .map = &flow_actions[0],
382 .map_idx = &actions_idx
386 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
387 .map = &flow_actions[0],
388 .map_idx = &actions_idx
392 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
393 .map = &flow_actions[0],
394 .map_idx = &actions_idx
398 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
399 .map = &flow_actions[0],
400 .map_idx = &actions_idx
403 .str = "set-src-mac",
404 .mask = FLOW_ACTION_MASK(
405 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
407 .map = &flow_actions[0],
408 .map_idx = &actions_idx
411 .str = "set-dst-mac",
412 .mask = FLOW_ACTION_MASK(
413 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
415 .map = &flow_actions[0],
416 .map_idx = &actions_idx
419 .str = "set-src-ipv4",
420 .mask = FLOW_ACTION_MASK(
421 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
423 .map = &flow_actions[0],
424 .map_idx = &actions_idx
427 .str = "set-dst-ipv4",
428 .mask = FLOW_ACTION_MASK(
429 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
431 .map = &flow_actions[0],
432 .map_idx = &actions_idx
435 .str = "set-src-ipv6",
436 .mask = FLOW_ACTION_MASK(
437 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
439 .map = &flow_actions[0],
440 .map_idx = &actions_idx
443 .str = "set-dst-ipv6",
444 .mask = FLOW_ACTION_MASK(
445 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
447 .map = &flow_actions[0],
448 .map_idx = &actions_idx
452 .mask = FLOW_ACTION_MASK(
453 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
455 .map = &flow_actions[0],
456 .map_idx = &actions_idx
460 .mask = FLOW_ACTION_MASK(
461 RTE_FLOW_ACTION_TYPE_SET_TP_DST
463 .map = &flow_actions[0],
464 .map_idx = &actions_idx
467 .str = "inc-tcp-ack",
468 .mask = FLOW_ACTION_MASK(
469 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
471 .map = &flow_actions[0],
472 .map_idx = &actions_idx
475 .str = "dec-tcp-ack",
476 .mask = FLOW_ACTION_MASK(
477 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
479 .map = &flow_actions[0],
480 .map_idx = &actions_idx
483 .str = "inc-tcp-seq",
484 .mask = FLOW_ACTION_MASK(
485 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
487 .map = &flow_actions[0],
488 .map_idx = &actions_idx
491 .str = "dec-tcp-seq",
492 .mask = FLOW_ACTION_MASK(
493 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
495 .map = &flow_actions[0],
496 .map_idx = &actions_idx
500 .mask = FLOW_ACTION_MASK(
501 RTE_FLOW_ACTION_TYPE_SET_TTL
503 .map = &flow_actions[0],
504 .map_idx = &actions_idx
508 .mask = FLOW_ACTION_MASK(
509 RTE_FLOW_ACTION_TYPE_DEC_TTL
511 .map = &flow_actions[0],
512 .map_idx = &actions_idx
515 .str = "set-ipv4-dscp",
516 .mask = FLOW_ACTION_MASK(
517 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
519 .map = &flow_actions[0],
520 .map_idx = &actions_idx
523 .str = "set-ipv6-dscp",
524 .mask = FLOW_ACTION_MASK(
525 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
527 .map = &flow_actions[0],
528 .map_idx = &actions_idx
532 .mask = FLOW_ACTION_MASK(
533 RTE_FLOW_ACTION_TYPE_FLAG
535 .map = &flow_actions[0],
536 .map_idx = &actions_idx
540 .mask = FLOW_ACTION_MASK(
541 RTE_FLOW_ACTION_TYPE_METER
543 .map = &flow_actions[0],
544 .map_idx = &actions_idx
547 .str = "vxlan-encap",
548 .mask = FLOW_ACTION_MASK(
549 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP
551 .map = &flow_actions[0],
552 .map_idx = &actions_idx
555 .str = "vxlan-decap",
556 .mask = FLOW_ACTION_MASK(
557 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP
559 .map = &flow_actions[0],
560 .map_idx = &actions_idx
564 static const struct option lgopts[] = {
567 { "rules-count", 1, 0, 0 },
568 { "rules-batch", 1, 0, 0 },
569 { "dump-iterations", 0, 0, 0 },
570 { "deletion-rate", 0, 0, 0 },
571 { "dump-socket-mem", 0, 0, 0 },
572 { "enable-fwd", 0, 0, 0 },
573 { "unique-data", 0, 0, 0 },
574 { "portmask", 1, 0, 0 },
575 { "cores", 1, 0, 0 },
577 { "ingress", 0, 0, 0 },
578 { "egress", 0, 0, 0 },
579 { "transfer", 0, 0, 0 },
580 { "group", 1, 0, 0 },
582 { "ether", 0, 0, 0 },
588 { "vxlan", 0, 0, 0 },
589 { "vxlan-gpe", 0, 0, 0 },
591 { "geneve", 0, 0, 0 },
595 { "icmpv4", 0, 0, 0 },
596 { "icmpv6", 0, 0, 0 },
598 { "port-id", 0, 0, 0 },
600 { "queue", 0, 0, 0 },
603 { "count", 0, 0, 0 },
604 { "set-meta", 0, 0, 0 },
605 { "set-tag", 0, 0, 0 },
607 { "hairpin-queue", 1, 0, 0 },
608 { "hairpin-rss", 1, 0, 0 },
609 { "set-src-mac", 0, 0, 0 },
610 { "set-dst-mac", 0, 0, 0 },
611 { "set-src-ipv4", 0, 0, 0 },
612 { "set-dst-ipv4", 0, 0, 0 },
613 { "set-src-ipv6", 0, 0, 0 },
614 { "set-dst-ipv6", 0, 0, 0 },
615 { "set-src-tp", 0, 0, 0 },
616 { "set-dst-tp", 0, 0, 0 },
617 { "inc-tcp-ack", 0, 0, 0 },
618 { "dec-tcp-ack", 0, 0, 0 },
619 { "inc-tcp-seq", 0, 0, 0 },
620 { "dec-tcp-seq", 0, 0, 0 },
621 { "set-ttl", 0, 0, 0 },
622 { "dec-ttl", 0, 0, 0 },
623 { "set-ipv4-dscp", 0, 0, 0 },
624 { "set-ipv6-dscp", 0, 0, 0 },
626 { "meter", 0, 0, 0 },
627 { "raw-encap", 1, 0, 0 },
628 { "raw-decap", 1, 0, 0 },
629 { "vxlan-encap", 0, 0, 0 },
630 { "vxlan-decap", 0, 0, 0 },
633 RTE_ETH_FOREACH_DEV(i)
634 ports_mask |= 1 << i;
636 hairpin_queues_num = 0;
639 printf(":: Flow -> ");
640 while ((opt = getopt_long(argc, argvopt, "",
641 lgopts, &opt_idx)) != EOF) {
644 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
649 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
654 rte_exit(EXIT_FAILURE,
655 "flow group should be >= 0\n");
656 printf("group %d / ", flow_group);
659 for (i = 0; i < RTE_DIM(flow_options); i++)
660 if (strcmp(lgopts[opt_idx].name,
661 flow_options[i].str) == 0) {
663 (*flow_options[i].map_idx)++] =
664 flow_options[i].mask;
665 printf("%s / ", flow_options[i].str);
668 if (strcmp(lgopts[opt_idx].name,
669 "hairpin-rss") == 0) {
672 hairpin_queues_num = n;
674 rte_exit(EXIT_FAILURE,
675 "Hairpin queues should be > 0\n");
677 flow_actions[actions_idx++] =
679 printf("hairpin-rss / ");
681 if (strcmp(lgopts[opt_idx].name,
682 "hairpin-queue") == 0) {
685 hairpin_queues_num = n;
687 rte_exit(EXIT_FAILURE,
688 "Hairpin queues should be > 0\n");
690 flow_actions[actions_idx++] =
691 HAIRPIN_QUEUE_ACTION;
692 printf("hairpin-queue / ");
695 if (strcmp(lgopts[opt_idx].name, "raw-encap") == 0) {
696 printf("raw-encap ");
697 flow_actions[actions_idx++] =
699 RTE_FLOW_ACTION_TYPE_RAW_ENCAP
702 token = strtok(optarg, ",");
703 while (token != NULL) {
704 for (i = 0; i < RTE_DIM(flow_options); i++) {
705 if (strcmp(flow_options[i].str, token) == 0) {
706 printf("%s,", token);
707 encap_data |= flow_options[i].mask;
710 /* Reached last item with no match */
711 if (i == (RTE_DIM(flow_options) - 1))
712 rte_exit(EXIT_FAILURE,
713 "Invalid encap item: %s\n", token);
715 token = strtok(NULL, ",");
719 if (strcmp(lgopts[opt_idx].name, "raw-decap") == 0) {
720 printf("raw-decap ");
721 flow_actions[actions_idx++] =
723 RTE_FLOW_ACTION_TYPE_RAW_DECAP
726 token = strtok(optarg, ",");
727 while (token != NULL) {
728 for (i = 0; i < RTE_DIM(flow_options); i++) {
729 if (strcmp(flow_options[i].str, token) == 0) {
730 printf("%s,", token);
731 decap_data |= flow_options[i].mask;
734 /* Reached last item with no match */
735 if (i == (RTE_DIM(flow_options) - 1))
736 rte_exit(EXIT_FAILURE,
737 "Invalid decap item %s\n", token);
739 token = strtok(NULL, ",");
744 if (strcmp(lgopts[opt_idx].name,
745 "rules-batch") == 0) {
747 if (n >= DEFAULT_RULES_BATCH)
750 rte_exit(EXIT_FAILURE,
751 "rules_batch should be >= %d\n",
752 DEFAULT_RULES_BATCH);
755 if (strcmp(lgopts[opt_idx].name,
756 "rules-count") == 0) {
758 if (n >= (int) rules_batch)
761 rte_exit(EXIT_FAILURE,
762 "rules_count should be >= %d\n",
766 if (strcmp(lgopts[opt_idx].name,
767 "dump-iterations") == 0)
768 dump_iterations = true;
769 if (strcmp(lgopts[opt_idx].name,
772 if (strcmp(lgopts[opt_idx].name,
773 "deletion-rate") == 0)
775 if (strcmp(lgopts[opt_idx].name,
776 "dump-socket-mem") == 0)
777 dump_socket_mem_flag = true;
778 if (strcmp(lgopts[opt_idx].name,
781 if (strcmp(lgopts[opt_idx].name,
783 /* parse hexadecimal string */
785 pm = strtoull(optarg, &end, 16);
786 if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
787 rte_exit(EXIT_FAILURE, "Invalid fwd port mask\n");
790 if (strcmp(lgopts[opt_idx].name, "cores") == 0) {
792 if ((int) rte_lcore_count() <= n) {
793 rte_exit(EXIT_FAILURE,
794 "Error: you need %d cores to run on multi-cores\n"
795 "Existing cores are: %d\n", n, rte_lcore_count());
797 if (n <= RTE_MAX_LCORE && n > 0)
798 mc_pool.cores_count = n;
800 rte_exit(EXIT_FAILURE,
801 "Error: cores count must be > 0 and < %d\n",
808 rte_exit(EXIT_FAILURE, "Invalid option: %s\n",
813 printf("end_flow\n");
816 /* Dump the socket memory statistics on console */
818 dump_socket_mem(FILE *f)
820 struct rte_malloc_socket_stats socket_stats;
825 unsigned int n_alloc = 0;
826 unsigned int n_free = 0;
827 bool active_nodes = false;
830 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
831 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
832 !socket_stats.heap_totalsz_bytes)
835 total += socket_stats.heap_totalsz_bytes;
836 alloc += socket_stats.heap_allocsz_bytes;
837 free += socket_stats.heap_freesz_bytes;
838 n_alloc += socket_stats.alloc_count;
839 n_free += socket_stats.free_count;
840 if (dump_socket_mem_flag) {
841 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
843 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
844 " %.6lf(%.3lf%%)\nfree: %.6lf"
846 "\ncount alloc: %u\nfree: %u\n",
848 socket_stats.heap_totalsz_bytes / 1.0e6,
849 socket_stats.heap_allocsz_bytes / 1.0e6,
850 (double)socket_stats.heap_allocsz_bytes * 100 /
851 (double)socket_stats.heap_totalsz_bytes,
852 socket_stats.heap_freesz_bytes / 1.0e6,
853 socket_stats.greatest_free_size / 1.0e6,
854 socket_stats.alloc_count,
855 socket_stats.free_count);
856 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
859 if (dump_socket_mem_flag && active_nodes) {
861 "\nTotal: size(M)\ntotal: %.6lf"
862 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
863 "\ncount alloc: %u\nfree: %u\n",
864 total / 1.0e6, alloc / 1.0e6,
865 (double)alloc * 100 / (double)total, free / 1.0e6,
867 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
873 print_flow_error(struct rte_flow_error error)
875 printf("Flow can't be created %d message: %s\n",
877 error.message ? error.message : "(no stated reason)");
881 print_rules_batches(double *cpu_time_per_batch)
887 for (idx = 0; idx < MAX_BATCHES_COUNT; idx++) {
888 if (!cpu_time_per_batch[idx])
890 delta = (double)(rules_batch / cpu_time_per_batch[idx]);
891 rate = delta / 1000; /* Save rate in K unit. */
892 printf(":: Rules batch #%d: %d rules "
893 "in %f sec[ Rate = %f K Rule/Sec ]\n",
895 cpu_time_per_batch[idx], rate);
905 for (i = 0; i < MAX_ACTIONS_NUM; i++) {
906 if (flow_actions[i] == 0)
909 & FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_METER))
916 create_meter_rule(int port_id, uint32_t counter)
919 struct rte_mtr_params params;
920 uint32_t default_prof_id = 100;
921 struct rte_mtr_error error;
923 memset(¶ms, 0, sizeof(struct rte_mtr_params));
924 params.meter_enable = 1;
925 params.stats_mask = 0xffff;
926 params.use_prev_mtr_color = 0;
927 params.dscp_table = NULL;
930 params.meter_profile_id = default_prof_id;
931 ret = rte_mtr_create(port_id, counter, ¶ms, 1, &error);
933 printf("Port %u create meter idx(%d) error(%d) message: %s\n",
934 port_id, counter, error.type,
935 error.message ? error.message : "(no stated reason)");
936 rte_exit(EXIT_FAILURE, "Error in creating meter\n");
941 destroy_meter_rule(int port_id, uint32_t counter)
943 struct rte_mtr_error error;
945 if (rte_mtr_destroy(port_id, counter, &error)) {
946 printf("Port %u destroy meter(%d) error(%d) message: %s\n",
947 port_id, counter, error.type,
948 error.message ? error.message : "(no stated reason)");
949 rte_exit(EXIT_FAILURE, "Error in deleting meter rule\n");
954 meters_handler(int port_id, uint8_t core_id, uint8_t ops)
956 uint64_t start_batch;
957 double cpu_time_used, insertion_rate;
958 int rules_count_per_core, rules_batch_idx;
959 uint32_t counter, start_counter = 0, end_counter;
960 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
962 rules_count_per_core = rules_count / mc_pool.cores_count;
965 start_counter = core_id * rules_count_per_core;
966 end_counter = (core_id + 1) * rules_count_per_core;
969 start_batch = rte_get_timer_cycles();
970 for (counter = start_counter; counter < end_counter; counter++) {
971 if (ops == METER_CREATE)
972 create_meter_rule(port_id, counter);
974 destroy_meter_rule(port_id, counter);
976 * Save the insertion rate for rules batch.
977 * Check if the insertion reached the rules
978 * patch counter, then save the insertion rate
981 if (!((counter + 1) % rules_batch)) {
982 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
983 cpu_time_per_batch[rules_batch_idx] =
984 ((double)(rte_get_timer_cycles() - start_batch))
985 / rte_get_timer_hz();
986 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
987 start_batch = rte_get_timer_cycles();
991 /* Print insertion rates for all batches */
993 print_rules_batches(cpu_time_per_batch);
996 ((double) (rules_count_per_core / cpu_time_used) / 1000);
998 /* Insertion rate for all rules in one core */
999 printf(":: Port %d :: Core %d Meter %s :: start @[%d] - end @[%d],"
1000 " use:%.02fs, rate:%.02fk Rule/Sec\n",
1001 port_id, core_id, ops == METER_CREATE ? "create" : "delete",
1002 start_counter, end_counter - 1,
1003 cpu_time_used, insertion_rate);
1005 if (ops == METER_CREATE)
1006 mc_pool.meters_record.insertion[port_id][core_id]
1009 mc_pool.meters_record.deletion[port_id][core_id]
1014 destroy_meter_profile(void)
1016 struct rte_mtr_error error;
1020 nr_ports = rte_eth_dev_count_avail();
1021 for (port_id = 0; port_id < nr_ports; port_id++) {
1022 /* If port outside portmask */
1023 if (!((ports_mask >> port_id) & 0x1))
1026 if (rte_mtr_meter_profile_delete
1027 (port_id, DEFAULT_METER_PROF_ID, &error)) {
1028 printf("Port %u del profile error(%d) message: %s\n",
1029 port_id, error.type,
1030 error.message ? error.message : "(no stated reason)");
1031 rte_exit(EXIT_FAILURE, "Error: Destroy meter profile Failed!\n");
1037 create_meter_profile(void)
1041 struct rte_mtr_meter_profile mp;
1042 struct rte_mtr_error error;
1045 *currently , only create one meter file for one port
1046 *1 meter profile -> N meter rules -> N rte flows
1048 memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
1049 nr_ports = rte_eth_dev_count_avail();
1050 for (port_id = 0; port_id < nr_ports; port_id++) {
1051 /* If port outside portmask */
1052 if (!((ports_mask >> port_id) & 0x1))
1055 mp.alg = RTE_MTR_SRTCM_RFC2697;
1056 mp.srtcm_rfc2697.cir = METER_CIR;
1057 mp.srtcm_rfc2697.cbs = METER_CIR / 8;
1058 mp.srtcm_rfc2697.ebs = 0;
1060 ret = rte_mtr_meter_profile_add
1061 (port_id, DEFAULT_METER_PROF_ID, &mp, &error);
1063 printf("Port %u create Profile error(%d) message: %s\n",
1064 port_id, error.type,
1065 error.message ? error.message : "(no stated reason)");
1066 rte_exit(EXIT_FAILURE, "Error: Creation meter profile Failed!\n");
1072 destroy_flows(int port_id, uint8_t core_id, struct rte_flow **flows_list)
1074 struct rte_flow_error error;
1075 clock_t start_batch, end_batch;
1076 double cpu_time_used = 0;
1077 double deletion_rate;
1078 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1081 int rules_batch_idx;
1082 int rules_count_per_core;
1084 rules_count_per_core = rules_count / mc_pool.cores_count;
1085 /* If group > 0 , should add 1 flow which created in group 0 */
1086 if (flow_group > 0 && core_id == 0)
1087 rules_count_per_core++;
1089 start_batch = rte_get_timer_cycles();
1090 for (i = 0; i < (uint32_t) rules_count_per_core; i++) {
1091 if (flows_list[i] == 0)
1094 memset(&error, 0x33, sizeof(error));
1095 if (rte_flow_destroy(port_id, flows_list[i], &error)) {
1096 print_flow_error(error);
1097 rte_exit(EXIT_FAILURE, "Error in deleting flow\n");
1101 * Save the deletion rate for rules batch.
1102 * Check if the deletion reached the rules
1103 * patch counter, then save the deletion rate
1106 if (!((i + 1) % rules_batch)) {
1107 end_batch = rte_get_timer_cycles();
1108 delta = (double) (end_batch - start_batch);
1109 rules_batch_idx = ((i + 1) / rules_batch) - 1;
1110 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_timer_hz();
1111 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1112 start_batch = rte_get_timer_cycles();
1116 /* Print deletion rates for all batches */
1117 if (dump_iterations)
1118 print_rules_batches(cpu_time_per_batch);
1120 /* Deletion rate for all rules */
1121 deletion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1122 printf(":: Port %d :: Core %d :: Rules deletion rate -> %f K Rule/Sec\n",
1123 port_id, core_id, deletion_rate);
1124 printf(":: Port %d :: Core %d :: The time for deleting %d rules is %f seconds\n",
1125 port_id, core_id, rules_count_per_core, cpu_time_used);
1127 mc_pool.flows_record.deletion[port_id][core_id] = cpu_time_used;
1130 static struct rte_flow **
1131 insert_flows(int port_id, uint8_t core_id)
1133 struct rte_flow **flows_list;
1134 struct rte_flow_error error;
1135 clock_t start_batch, end_batch;
1136 double first_flow_latency;
1137 double cpu_time_used;
1138 double insertion_rate;
1139 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1141 uint32_t flow_index;
1142 uint32_t counter, start_counter = 0, end_counter;
1143 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
1144 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
1145 int rules_batch_idx;
1146 int rules_count_per_core;
1148 rules_count_per_core = rules_count / mc_pool.cores_count;
1150 /* Set boundaries of rules for each core. */
1152 start_counter = core_id * rules_count_per_core;
1153 end_counter = (core_id + 1) * rules_count_per_core;
1155 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
1156 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
1158 flows_list = rte_zmalloc("flows_list",
1159 (sizeof(struct rte_flow *) * rules_count_per_core) + 1, 0);
1160 if (flows_list == NULL)
1161 rte_exit(EXIT_FAILURE, "No Memory available!\n");
1165 if (flow_group > 0 && core_id == 0) {
1167 * Create global rule to jump into flow_group,
1168 * this way the app will avoid the default rules.
1170 * This rule will be created only once.
1173 * group 0 eth / end actions jump group <flow_group>
1175 flow = generate_flow(port_id, 0, flow_attrs,
1176 global_items, global_actions,
1177 flow_group, 0, 0, 0, 0, core_id, unique_data, &error);
1180 print_flow_error(error);
1181 rte_exit(EXIT_FAILURE, "Error in creating flow\n");
1183 flows_list[flow_index++] = flow;
1186 start_batch = rte_get_timer_cycles();
1187 for (counter = start_counter; counter < end_counter; counter++) {
1188 flow = generate_flow(port_id, flow_group,
1189 flow_attrs, flow_items, flow_actions,
1190 JUMP_ACTION_TABLE, counter,
1192 encap_data, decap_data,
1193 core_id, unique_data, &error);
1196 first_flow_latency = (double) (rte_get_timer_cycles() - start_batch);
1197 first_flow_latency /= rte_get_timer_hz();
1198 /* In millisecond */
1199 first_flow_latency *= 1000;
1200 printf(":: First Flow Latency :: Port %d :: First flow "
1201 "installed in %f milliseconds\n",
1202 port_id, first_flow_latency);
1206 counter = end_counter;
1209 print_flow_error(error);
1210 rte_exit(EXIT_FAILURE, "Error in creating flow\n");
1213 flows_list[flow_index++] = flow;
1216 * Save the insertion rate for rules batch.
1217 * Check if the insertion reached the rules
1218 * patch counter, then save the insertion rate
1221 if (!((counter + 1) % rules_batch)) {
1222 end_batch = rte_get_timer_cycles();
1223 delta = (double) (end_batch - start_batch);
1224 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
1225 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_timer_hz();
1226 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1227 start_batch = rte_get_timer_cycles();
1231 /* Print insertion rates for all batches */
1232 if (dump_iterations)
1233 print_rules_batches(cpu_time_per_batch);
1235 printf(":: Port %d :: Core %d boundaries :: start @[%d] - end @[%d]\n",
1236 port_id, core_id, start_counter, end_counter - 1);
1238 /* Insertion rate for all rules in one core */
1239 insertion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1240 printf(":: Port %d :: Core %d :: Rules insertion rate -> %f K Rule/Sec\n",
1241 port_id, core_id, insertion_rate);
1242 printf(":: Port %d :: Core %d :: The time for creating %d in rules %f seconds\n",
1243 port_id, core_id, rules_count_per_core, cpu_time_used);
1245 mc_pool.flows_record.insertion[port_id][core_id] = cpu_time_used;
1250 flows_handler(uint8_t core_id)
1252 struct rte_flow **flows_list;
1256 nr_ports = rte_eth_dev_count_avail();
1258 if (rules_batch > rules_count)
1259 rules_batch = rules_count;
1261 printf(":: Rules Count per port: %d\n\n", rules_count);
1263 for (port_id = 0; port_id < nr_ports; port_id++) {
1264 /* If port outside portmask */
1265 if (!((ports_mask >> port_id) & 0x1))
1268 /* Insertion part. */
1269 mc_pool.last_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1271 meters_handler(port_id, core_id, METER_CREATE);
1272 flows_list = insert_flows(port_id, core_id);
1273 if (flows_list == NULL)
1274 rte_exit(EXIT_FAILURE, "Error: Insertion Failed!\n");
1275 mc_pool.current_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1277 /* Deletion part. */
1279 destroy_flows(port_id, core_id, flows_list);
1281 meters_handler(port_id, core_id, METER_DELETE);
1287 dump_used_cpu_time(const char *item,
1288 uint16_t port, struct used_cpu_time *used_time)
1291 /* Latency: total count of rte rules divided
1292 * over max time used by thread between all
1295 * Throughput: total count of rte rules divided
1296 * over the average of the time cosumed by all
1299 double insertion_latency_time;
1300 double insertion_throughput_time;
1301 double deletion_latency_time;
1302 double deletion_throughput_time;
1303 double insertion_latency, insertion_throughput;
1304 double deletion_latency, deletion_throughput;
1306 /* Save first insertion/deletion rates from first thread.
1307 * Start comparing with all threads, if any thread used
1308 * time more than current saved, replace it.
1310 * Thus in the end we will have the max time used for
1311 * insertion/deletion by one thread.
1313 * As for memory consumption, save the min of all threads
1314 * of last alloc, and save the max for all threads for
1318 insertion_latency_time = used_time->insertion[port][0];
1319 deletion_latency_time = used_time->deletion[port][0];
1320 insertion_throughput_time = used_time->insertion[port][0];
1321 deletion_throughput_time = used_time->deletion[port][0];
1323 i = mc_pool.cores_count;
1325 insertion_throughput_time += used_time->insertion[port][i];
1326 deletion_throughput_time += used_time->deletion[port][i];
1327 if (insertion_latency_time < used_time->insertion[port][i])
1328 insertion_latency_time = used_time->insertion[port][i];
1329 if (deletion_latency_time < used_time->deletion[port][i])
1330 deletion_latency_time = used_time->deletion[port][i];
1333 insertion_latency = ((double) (mc_pool.rules_count
1334 / insertion_latency_time) / 1000);
1335 deletion_latency = ((double) (mc_pool.rules_count
1336 / deletion_latency_time) / 1000);
1338 insertion_throughput_time /= mc_pool.cores_count;
1339 deletion_throughput_time /= mc_pool.cores_count;
1340 insertion_throughput = ((double) (mc_pool.rules_count
1341 / insertion_throughput_time) / 1000);
1342 deletion_throughput = ((double) (mc_pool.rules_count
1343 / deletion_throughput_time) / 1000);
1346 printf("\n%s\n:: [Latency | Insertion] All Cores :: Port %d :: ",
1348 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1350 printf(":: [Latency | Insertion] All Cores :: Port %d :: ", port);
1351 printf("The time for creating %d rules is %f seconds\n",
1352 mc_pool.rules_count, insertion_latency_time);
1354 /* Throughput stats */
1355 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1356 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1357 insertion_throughput);
1358 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1359 printf("The average time for creating %d rules is %f seconds\n",
1360 mc_pool.rules_count, insertion_throughput_time);
1364 printf(":: [Latency | Deletion] All Cores :: Port %d :: Total "
1365 "deletion rate -> %f K Rules/Sec\n",
1366 port, deletion_latency);
1367 printf(":: [Latency | Deletion] All Cores :: Port %d :: ",
1369 printf("The time for deleting %d rules is %f seconds\n",
1370 mc_pool.rules_count, deletion_latency_time);
1372 /* Throughput stats */
1373 printf(":: [Throughput | Deletion] All Cores :: Port %d :: Total "
1374 "deletion rate -> %f K Rules/Sec\n",
1375 port, deletion_throughput);
1376 printf(":: [Throughput | Deletion] All Cores :: Port %d :: ",
1378 printf("The average time for deleting %d rules is %f seconds\n",
1379 mc_pool.rules_count, deletion_throughput_time);
1384 dump_used_mem(uint16_t port)
1387 int64_t last_alloc, current_alloc;
1388 int flow_size_in_bytes;
1390 last_alloc = mc_pool.last_alloc[0];
1391 current_alloc = mc_pool.current_alloc[0];
1393 i = mc_pool.cores_count;
1395 if (last_alloc > mc_pool.last_alloc[i])
1396 last_alloc = mc_pool.last_alloc[i];
1397 if (current_alloc < mc_pool.current_alloc[i])
1398 current_alloc = mc_pool.current_alloc[i];
1401 flow_size_in_bytes = (current_alloc - last_alloc) / mc_pool.rules_count;
1402 printf("\n:: Port %d :: rte_flow size in DPDK layer: %d Bytes\n",
1403 port, flow_size_in_bytes);
1407 run_rte_flow_handler_cores(void *data __rte_unused)
1410 int lcore_counter = 0;
1411 int lcore_id = rte_lcore_id();
1414 RTE_LCORE_FOREACH(i) {
1415 /* If core not needed return. */
1416 if (lcore_id == i) {
1417 printf(":: lcore %d mapped with index %d\n", lcore_id, lcore_counter);
1418 if (lcore_counter >= (int) mc_pool.cores_count)
1424 lcore_id = lcore_counter;
1426 if (lcore_id >= (int) mc_pool.cores_count)
1429 mc_pool.rules_count = rules_count;
1431 flows_handler(lcore_id);
1433 /* Only main core to print total results. */
1437 /* Make sure all cores finished insertion/deletion process. */
1438 rte_eal_mp_wait_lcore();
1440 RTE_ETH_FOREACH_DEV(port) {
1441 /* If port outside portmask */
1442 if (!((ports_mask >> port) & 0x1))
1445 dump_used_cpu_time("Meters:",
1446 port, &mc_pool.meters_record);
1447 dump_used_cpu_time("Flows:",
1448 port, &mc_pool.flows_record);
1449 dump_used_mem(port);
1456 signal_handler(int signum)
1458 if (signum == SIGINT || signum == SIGTERM) {
1459 printf("\n\nSignal %d received, preparing to exit...\n",
1461 printf("Error: Stats are wrong due to sudden signal!\n\n");
1466 static inline uint16_t
1467 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
1470 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
1476 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
1482 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
1483 li->tx_pkts += nr_tx;
1484 li->tx_drops += cnt - nr_tx;
1486 for (i = nr_tx; i < cnt; i++)
1487 rte_pktmbuf_free(li->pkts[i]);
1491 * Method to convert numbers into pretty numbers that easy
1492 * to read. The design here is to add comma after each three
1493 * digits and set all of this inside buffer.
1495 * For example if n = 1799321, the output will be
1496 * 1,799,321 after this method which is easier to read.
1499 pretty_number(uint64_t n, char *buf)
1506 sprintf(p[i], "%03d", (int)(n % 1000));
1511 sprintf(p[i++], "%d", (int)n);
1514 off += sprintf(buf + off, "%s,", p[i]);
1515 buf[strlen(buf) - 1] = '\0';
1521 packet_per_second_stats(void)
1523 struct lcore_info *old;
1524 struct lcore_info *li, *oli;
1528 old = rte_zmalloc("old",
1529 sizeof(struct lcore_info) * RTE_MAX_LCORE, 0);
1531 rte_exit(EXIT_FAILURE, "No Memory available!\n");
1533 memcpy(old, lcore_infos,
1534 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1536 while (!force_quit) {
1537 uint64_t total_tx_pkts = 0;
1538 uint64_t total_rx_pkts = 0;
1539 uint64_t total_tx_drops = 0;
1540 uint64_t tx_delta, rx_delta, drops_delta;
1542 int nr_valid_core = 0;
1547 char go_up_nr_lines[16];
1549 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
1550 printf("%s\r", go_up_nr_lines);
1553 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
1554 printf("%6s %16s %16s %16s\n", "------", "----------------",
1555 "----------------", "----------------");
1557 for (i = 0; i < RTE_MAX_LCORE; i++) {
1558 li = &lcore_infos[i];
1560 if (li->mode != LCORE_MODE_PKT)
1563 tx_delta = li->tx_pkts - oli->tx_pkts;
1564 rx_delta = li->rx_pkts - oli->rx_pkts;
1565 drops_delta = li->tx_drops - oli->tx_drops;
1566 printf("%6d %16s %16s %16s\n", i,
1567 pretty_number(tx_delta, buf[0]),
1568 pretty_number(drops_delta, buf[1]),
1569 pretty_number(rx_delta, buf[2]));
1571 total_tx_pkts += tx_delta;
1572 total_rx_pkts += rx_delta;
1573 total_tx_drops += drops_delta;
1579 if (nr_valid_core > 1) {
1580 printf("%6s %16s %16s %16s\n", "total",
1581 pretty_number(total_tx_pkts, buf[0]),
1582 pretty_number(total_tx_drops, buf[1]),
1583 pretty_number(total_rx_pkts, buf[2]));
1587 memcpy(old, lcore_infos,
1588 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1593 start_forwarding(void *data __rte_unused)
1595 int lcore = rte_lcore_id();
1598 struct lcore_info *li = &lcore_infos[lcore];
1603 if (li->mode == LCORE_MODE_STATS) {
1604 printf(":: started stats on lcore %u\n", lcore);
1605 packet_per_second_stats();
1610 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1611 if (li->streams[stream_id].rx_port == -1)
1615 li->streams[stream_id].rx_port,
1616 li->streams[stream_id].rx_queue);
1619 li->streams[stream_id].tx_port,
1620 li->streams[stream_id].tx_queue);
1626 init_lcore_info(void)
1634 int streams_per_core;
1635 int unassigned_streams;
1637 nr_port = rte_eth_dev_count_avail();
1639 /* First logical core is reserved for stats printing */
1640 lcore = rte_get_next_lcore(-1, 0, 0);
1641 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1644 * Initialize all cores
1645 * All cores at first must have -1 value in all streams
1646 * This means that this stream is not used, or not set
1649 for (i = 0; i < RTE_MAX_LCORE; i++)
1650 for (j = 0; j < MAX_STREAMS; j++) {
1651 lcore_infos[i].streams[j].tx_port = -1;
1652 lcore_infos[i].streams[j].rx_port = -1;
1653 lcore_infos[i].streams[j].tx_queue = -1;
1654 lcore_infos[i].streams[j].rx_queue = -1;
1655 lcore_infos[i].streams_nb = 0;
1659 * Calculate the total streams count.
1660 * Also distribute those streams count between the available
1661 * logical cores except first core, since it's reserved for
1664 nb_fwd_streams = nr_port * RXQ_NUM;
1665 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1666 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1667 lcore = rte_get_next_lcore(lcore, 0, 0);
1668 lcore_infos[lcore].streams_nb = 1;
1671 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1672 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1673 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1674 lcore = rte_get_next_lcore(lcore, 0, 0);
1675 lcore_infos[lcore].streams_nb = streams_per_core;
1676 if (unassigned_streams) {
1677 lcore_infos[lcore].streams_nb++;
1678 unassigned_streams--;
1684 * Set the streams for the cores according to each logical
1685 * core stream count.
1686 * The streams is built on the design of what received should
1687 * forward as well, this means that if you received packets on
1688 * port 0 queue 0 then the same queue should forward the
1689 * packets, using the same logical core.
1691 lcore = rte_get_next_lcore(-1, 0, 0);
1692 for (port = 0; port < nr_port; port++) {
1693 /* Create FWD stream */
1694 for (queue = 0; queue < RXQ_NUM; queue++) {
1695 if (!lcore_infos[lcore].streams_nb ||
1696 !(stream_id % lcore_infos[lcore].streams_nb)) {
1697 lcore = rte_get_next_lcore(lcore, 0, 0);
1698 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1701 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1702 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1703 lcore_infos[lcore].streams[stream_id].rx_port = port;
1704 lcore_infos[lcore].streams[stream_id].tx_port = port;
1709 /* Print all streams */
1710 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1711 for (i = 0; i < RTE_MAX_LCORE; i++)
1712 for (j = 0; j < MAX_STREAMS; j++) {
1713 /* No streams for this core */
1714 if (lcore_infos[i].streams[j].tx_port == -1)
1716 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1718 lcore_infos[i].streams[j].rx_port,
1719 lcore_infos[i].streams[j].rx_queue,
1720 lcore_infos[i].streams[j].tx_port,
1721 lcore_infos[i].streams[j].tx_queue);
1730 uint16_t hairpin_queue;
1734 struct rte_eth_hairpin_conf hairpin_conf = {
1737 struct rte_eth_conf port_conf = {
1743 struct rte_eth_txconf txq_conf;
1744 struct rte_eth_rxconf rxq_conf;
1745 struct rte_eth_dev_info dev_info;
1747 nr_queues = RXQ_NUM;
1748 if (hairpin_queues_num != 0)
1749 nr_queues = RXQ_NUM + hairpin_queues_num;
1751 nr_ports = rte_eth_dev_count_avail();
1753 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1755 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1756 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1759 if (mbuf_mp == NULL)
1760 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1762 for (port_id = 0; port_id < nr_ports; port_id++) {
1763 uint64_t rx_metadata = 0;
1765 rx_metadata |= RTE_ETH_RX_METADATA_USER_FLAG;
1766 rx_metadata |= RTE_ETH_RX_METADATA_USER_MARK;
1768 ret = rte_eth_rx_metadata_negotiate(port_id, &rx_metadata);
1770 if (!(rx_metadata & RTE_ETH_RX_METADATA_USER_FLAG)) {
1771 printf(":: flow action FLAG will not affect Rx mbufs on port=%u\n",
1775 if (!(rx_metadata & RTE_ETH_RX_METADATA_USER_MARK)) {
1776 printf(":: flow action MARK will not affect Rx mbufs on port=%u\n",
1779 } else if (ret != -ENOTSUP) {
1780 rte_exit(EXIT_FAILURE, "Error when negotiating Rx meta features on port=%u: %s\n",
1781 port_id, rte_strerror(-ret));
1784 ret = rte_eth_dev_info_get(port_id, &dev_info);
1786 rte_exit(EXIT_FAILURE,
1787 "Error during getting device"
1788 " (port %u) info: %s\n",
1789 port_id, strerror(-ret));
1791 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1792 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1794 printf(":: initializing port: %d\n", port_id);
1796 ret = rte_eth_dev_configure(port_id, nr_queues,
1797 nr_queues, &port_conf);
1799 rte_exit(EXIT_FAILURE,
1800 ":: cannot configure device: err=%d, port=%u\n",
1803 rxq_conf = dev_info.default_rxconf;
1804 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1805 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1806 rte_eth_dev_socket_id(port_id),
1810 rte_exit(EXIT_FAILURE,
1811 ":: Rx queue setup failed: err=%d, port=%u\n",
1815 txq_conf = dev_info.default_txconf;
1816 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1817 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1818 rte_eth_dev_socket_id(port_id),
1821 rte_exit(EXIT_FAILURE,
1822 ":: Tx queue setup failed: err=%d, port=%u\n",
1826 /* Catch all packets from traffic generator. */
1827 ret = rte_eth_promiscuous_enable(port_id);
1829 rte_exit(EXIT_FAILURE,
1830 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1831 rte_strerror(-ret), port_id);
1833 if (hairpin_queues_num != 0) {
1835 * Configure peer which represents hairpin Tx.
1836 * Hairpin queue numbers start after standard queues
1837 * (RXQ_NUM and TXQ_NUM).
1839 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1840 hairpin_queue < nr_queues;
1841 hairpin_queue++, std_queue++) {
1842 hairpin_conf.peers[0].port = port_id;
1843 hairpin_conf.peers[0].queue =
1844 std_queue + TXQ_NUM;
1845 ret = rte_eth_rx_hairpin_queue_setup(
1846 port_id, hairpin_queue,
1847 NR_RXD, &hairpin_conf);
1849 rte_exit(EXIT_FAILURE,
1850 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1854 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1855 hairpin_queue < nr_queues;
1856 hairpin_queue++, std_queue++) {
1857 hairpin_conf.peers[0].port = port_id;
1858 hairpin_conf.peers[0].queue =
1859 std_queue + RXQ_NUM;
1860 ret = rte_eth_tx_hairpin_queue_setup(
1861 port_id, hairpin_queue,
1862 NR_TXD, &hairpin_conf);
1864 rte_exit(EXIT_FAILURE,
1865 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1870 ret = rte_eth_dev_start(port_id);
1872 rte_exit(EXIT_FAILURE,
1873 "rte_eth_dev_start:err=%d, port=%u\n",
1876 printf(":: initializing port: %d done\n", port_id);
1881 main(int argc, char **argv)
1885 struct rte_flow_error error;
1887 ret = rte_eal_init(argc, argv);
1889 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1892 dump_iterations = false;
1893 rules_count = DEFAULT_RULES_COUNT;
1894 rules_batch = DEFAULT_RULES_BATCH;
1895 delete_flag = false;
1896 dump_socket_mem_flag = false;
1897 flow_group = DEFAULT_GROUP;
1898 unique_data = false;
1900 signal(SIGINT, signal_handler);
1901 signal(SIGTERM, signal_handler);
1906 args_parse(argc, argv);
1910 nb_lcores = rte_lcore_count();
1912 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1914 printf(":: Flows Count per port: %d\n\n", rules_count);
1917 create_meter_profile();
1918 rte_eal_mp_remote_launch(run_rte_flow_handler_cores, NULL, CALL_MAIN);
1922 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MAIN);
1924 if (has_meter() && delete_flag)
1925 destroy_meter_profile();
1927 RTE_ETH_FOREACH_DEV(port) {
1928 rte_flow_flush(port, &error);
1929 if (rte_eth_dev_stop(port) != 0)
1930 printf("Failed to stop device on port %u\n", port);
1931 rte_eth_dev_close(port);
1933 printf("\nBye ...\n");