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 encap_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 params.action[RTE_COLOR_GREEN] =
932 MTR_POLICER_ACTION_COLOR_GREEN;
933 params.action[RTE_COLOR_YELLOW] =
934 MTR_POLICER_ACTION_COLOR_YELLOW;
935 params.action[RTE_COLOR_RED] =
936 MTR_POLICER_ACTION_DROP;
938 ret = rte_mtr_create(port_id, counter, ¶ms, 1, &error);
940 printf("Port %u create meter idx(%d) error(%d) message: %s\n",
941 port_id, counter, error.type,
942 error.message ? error.message : "(no stated reason)");
943 rte_exit(EXIT_FAILURE, "Error in creating meter\n");
948 destroy_meter_rule(int port_id, uint32_t counter)
950 struct rte_mtr_error error;
952 if (rte_mtr_destroy(port_id, counter, &error)) {
953 printf("Port %u destroy meter(%d) error(%d) message: %s\n",
954 port_id, counter, error.type,
955 error.message ? error.message : "(no stated reason)");
956 rte_exit(EXIT_FAILURE, "Error in deleting meter rule\n");
961 meters_handler(int port_id, uint8_t core_id, uint8_t ops)
963 uint64_t start_batch;
964 double cpu_time_used, insertion_rate;
965 int rules_count_per_core, rules_batch_idx;
966 uint32_t counter, start_counter = 0, end_counter;
967 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
969 rules_count_per_core = rules_count / mc_pool.cores_count;
972 start_counter = core_id * rules_count_per_core;
973 end_counter = (core_id + 1) * rules_count_per_core;
976 start_batch = rte_get_timer_cycles();
977 for (counter = start_counter; counter < end_counter; counter++) {
978 if (ops == METER_CREATE)
979 create_meter_rule(port_id, counter);
981 destroy_meter_rule(port_id, counter);
983 * Save the insertion rate for rules batch.
984 * Check if the insertion reached the rules
985 * patch counter, then save the insertion rate
988 if (!((counter + 1) % rules_batch)) {
989 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
990 cpu_time_per_batch[rules_batch_idx] =
991 ((double)(rte_get_timer_cycles() - start_batch))
992 / rte_get_timer_hz();
993 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
994 start_batch = rte_get_timer_cycles();
998 /* Print insertion rates for all batches */
1000 print_rules_batches(cpu_time_per_batch);
1003 ((double) (rules_count_per_core / cpu_time_used) / 1000);
1005 /* Insertion rate for all rules in one core */
1006 printf(":: Port %d :: Core %d Meter %s :: start @[%d] - end @[%d],"
1007 " use:%.02fs, rate:%.02fk Rule/Sec\n",
1008 port_id, core_id, ops == METER_CREATE ? "create" : "delete",
1009 start_counter, end_counter - 1,
1010 cpu_time_used, insertion_rate);
1012 if (ops == METER_CREATE)
1013 mc_pool.meters_record.insertion[port_id][core_id]
1016 mc_pool.meters_record.deletion[port_id][core_id]
1021 destroy_meter_profile(void)
1023 struct rte_mtr_error error;
1027 nr_ports = rte_eth_dev_count_avail();
1028 for (port_id = 0; port_id < nr_ports; port_id++) {
1029 /* If port outside portmask */
1030 if (!((ports_mask >> port_id) & 0x1))
1033 if (rte_mtr_meter_profile_delete
1034 (port_id, DEFAULT_METER_PROF_ID, &error)) {
1035 printf("Port %u del profile error(%d) message: %s\n",
1036 port_id, error.type,
1037 error.message ? error.message : "(no stated reason)");
1038 rte_exit(EXIT_FAILURE, "Error: Destroy meter profile Failed!\n");
1044 create_meter_profile(void)
1048 struct rte_mtr_meter_profile mp;
1049 struct rte_mtr_error error;
1052 *currently , only create one meter file for one port
1053 *1 meter profile -> N meter rules -> N rte flows
1055 memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
1056 nr_ports = rte_eth_dev_count_avail();
1057 for (port_id = 0; port_id < nr_ports; port_id++) {
1058 /* If port outside portmask */
1059 if (!((ports_mask >> port_id) & 0x1))
1062 mp.alg = RTE_MTR_SRTCM_RFC2697;
1063 mp.srtcm_rfc2697.cir = METER_CIR;
1064 mp.srtcm_rfc2697.cbs = METER_CIR / 8;
1065 mp.srtcm_rfc2697.ebs = 0;
1067 ret = rte_mtr_meter_profile_add
1068 (port_id, DEFAULT_METER_PROF_ID, &mp, &error);
1070 printf("Port %u create Profile error(%d) message: %s\n",
1071 port_id, error.type,
1072 error.message ? error.message : "(no stated reason)");
1073 rte_exit(EXIT_FAILURE, "Error: Creation meter profile Failed!\n");
1079 destroy_flows(int port_id, uint8_t core_id, struct rte_flow **flows_list)
1081 struct rte_flow_error error;
1082 clock_t start_batch, end_batch;
1083 double cpu_time_used = 0;
1084 double deletion_rate;
1085 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1088 int rules_batch_idx;
1089 int rules_count_per_core;
1091 rules_count_per_core = rules_count / mc_pool.cores_count;
1092 /* If group > 0 , should add 1 flow which created in group 0 */
1093 if (flow_group > 0 && core_id == 0)
1094 rules_count_per_core++;
1096 start_batch = rte_get_timer_cycles();
1097 for (i = 0; i < (uint32_t) rules_count_per_core; i++) {
1098 if (flows_list[i] == 0)
1101 memset(&error, 0x33, sizeof(error));
1102 if (rte_flow_destroy(port_id, flows_list[i], &error)) {
1103 print_flow_error(error);
1104 rte_exit(EXIT_FAILURE, "Error in deleting flow\n");
1108 * Save the deletion rate for rules batch.
1109 * Check if the deletion reached the rules
1110 * patch counter, then save the deletion rate
1113 if (!((i + 1) % rules_batch)) {
1114 end_batch = rte_get_timer_cycles();
1115 delta = (double) (end_batch - start_batch);
1116 rules_batch_idx = ((i + 1) / rules_batch) - 1;
1117 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_timer_hz();
1118 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1119 start_batch = rte_get_timer_cycles();
1123 /* Print deletion rates for all batches */
1124 if (dump_iterations)
1125 print_rules_batches(cpu_time_per_batch);
1127 /* Deletion rate for all rules */
1128 deletion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1129 printf(":: Port %d :: Core %d :: Rules deletion rate -> %f K Rule/Sec\n",
1130 port_id, core_id, deletion_rate);
1131 printf(":: Port %d :: Core %d :: The time for deleting %d rules is %f seconds\n",
1132 port_id, core_id, rules_count_per_core, cpu_time_used);
1134 mc_pool.flows_record.deletion[port_id][core_id] = cpu_time_used;
1137 static struct rte_flow **
1138 insert_flows(int port_id, uint8_t core_id)
1140 struct rte_flow **flows_list;
1141 struct rte_flow_error error;
1142 clock_t start_batch, end_batch;
1143 double first_flow_latency;
1144 double cpu_time_used;
1145 double insertion_rate;
1146 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1148 uint32_t flow_index;
1149 uint32_t counter, start_counter = 0, end_counter;
1150 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
1151 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
1152 int rules_batch_idx;
1153 int rules_count_per_core;
1155 rules_count_per_core = rules_count / mc_pool.cores_count;
1157 /* Set boundaries of rules for each core. */
1159 start_counter = core_id * rules_count_per_core;
1160 end_counter = (core_id + 1) * rules_count_per_core;
1162 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
1163 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
1165 flows_list = rte_zmalloc("flows_list",
1166 (sizeof(struct rte_flow *) * rules_count_per_core) + 1, 0);
1167 if (flows_list == NULL)
1168 rte_exit(EXIT_FAILURE, "No Memory available!\n");
1172 if (flow_group > 0 && core_id == 0) {
1174 * Create global rule to jump into flow_group,
1175 * this way the app will avoid the default rules.
1177 * This rule will be created only once.
1180 * group 0 eth / end actions jump group <flow_group>
1182 flow = generate_flow(port_id, 0, flow_attrs,
1183 global_items, global_actions,
1184 flow_group, 0, 0, 0, 0, core_id, unique_data, &error);
1187 print_flow_error(error);
1188 rte_exit(EXIT_FAILURE, "Error in creating flow\n");
1190 flows_list[flow_index++] = flow;
1193 start_batch = rte_get_timer_cycles();
1194 for (counter = start_counter; counter < end_counter; counter++) {
1195 flow = generate_flow(port_id, flow_group,
1196 flow_attrs, flow_items, flow_actions,
1197 JUMP_ACTION_TABLE, counter,
1199 encap_data, decap_data,
1200 core_id, unique_data, &error);
1203 first_flow_latency = (double) (rte_get_timer_cycles() - start_batch);
1204 first_flow_latency /= rte_get_timer_hz();
1205 /* In millisecond */
1206 first_flow_latency *= 1000;
1207 printf(":: First Flow Latency :: Port %d :: First flow "
1208 "installed in %f milliseconds\n",
1209 port_id, first_flow_latency);
1213 counter = end_counter;
1216 print_flow_error(error);
1217 rte_exit(EXIT_FAILURE, "Error in creating flow\n");
1220 flows_list[flow_index++] = flow;
1223 * Save the insertion rate for rules batch.
1224 * Check if the insertion reached the rules
1225 * patch counter, then save the insertion rate
1228 if (!((counter + 1) % rules_batch)) {
1229 end_batch = rte_get_timer_cycles();
1230 delta = (double) (end_batch - start_batch);
1231 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
1232 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_timer_hz();
1233 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1234 start_batch = rte_get_timer_cycles();
1238 /* Print insertion rates for all batches */
1239 if (dump_iterations)
1240 print_rules_batches(cpu_time_per_batch);
1242 printf(":: Port %d :: Core %d boundaries :: start @[%d] - end @[%d]\n",
1243 port_id, core_id, start_counter, end_counter - 1);
1245 /* Insertion rate for all rules in one core */
1246 insertion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1247 printf(":: Port %d :: Core %d :: Rules insertion rate -> %f K Rule/Sec\n",
1248 port_id, core_id, insertion_rate);
1249 printf(":: Port %d :: Core %d :: The time for creating %d in rules %f seconds\n",
1250 port_id, core_id, rules_count_per_core, cpu_time_used);
1252 mc_pool.flows_record.insertion[port_id][core_id] = cpu_time_used;
1257 flows_handler(uint8_t core_id)
1259 struct rte_flow **flows_list;
1263 nr_ports = rte_eth_dev_count_avail();
1265 if (rules_batch > rules_count)
1266 rules_batch = rules_count;
1268 printf(":: Rules Count per port: %d\n\n", rules_count);
1270 for (port_id = 0; port_id < nr_ports; port_id++) {
1271 /* If port outside portmask */
1272 if (!((ports_mask >> port_id) & 0x1))
1275 /* Insertion part. */
1276 mc_pool.last_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1278 meters_handler(port_id, core_id, METER_CREATE);
1279 flows_list = insert_flows(port_id, core_id);
1280 if (flows_list == NULL)
1281 rte_exit(EXIT_FAILURE, "Error: Insertion Failed!\n");
1282 mc_pool.current_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1284 /* Deletion part. */
1286 destroy_flows(port_id, core_id, flows_list);
1288 meters_handler(port_id, core_id, METER_DELETE);
1294 dump_used_cpu_time(const char *item,
1295 uint16_t port, struct used_cpu_time *used_time)
1298 /* Latency: total count of rte rules divided
1299 * over max time used by thread between all
1302 * Throughput: total count of rte rules divided
1303 * over the average of the time cosumed by all
1306 double insertion_latency_time;
1307 double insertion_throughput_time;
1308 double deletion_latency_time;
1309 double deletion_throughput_time;
1310 double insertion_latency, insertion_throughput;
1311 double deletion_latency, deletion_throughput;
1313 /* Save first insertion/deletion rates from first thread.
1314 * Start comparing with all threads, if any thread used
1315 * time more than current saved, replace it.
1317 * Thus in the end we will have the max time used for
1318 * insertion/deletion by one thread.
1320 * As for memory consumption, save the min of all threads
1321 * of last alloc, and save the max for all threads for
1325 insertion_latency_time = used_time->insertion[port][0];
1326 deletion_latency_time = used_time->deletion[port][0];
1327 insertion_throughput_time = used_time->insertion[port][0];
1328 deletion_throughput_time = used_time->deletion[port][0];
1330 i = mc_pool.cores_count;
1332 insertion_throughput_time += used_time->insertion[port][i];
1333 deletion_throughput_time += used_time->deletion[port][i];
1334 if (insertion_latency_time < used_time->insertion[port][i])
1335 insertion_latency_time = used_time->insertion[port][i];
1336 if (deletion_latency_time < used_time->deletion[port][i])
1337 deletion_latency_time = used_time->deletion[port][i];
1340 insertion_latency = ((double) (mc_pool.rules_count
1341 / insertion_latency_time) / 1000);
1342 deletion_latency = ((double) (mc_pool.rules_count
1343 / deletion_latency_time) / 1000);
1345 insertion_throughput_time /= mc_pool.cores_count;
1346 deletion_throughput_time /= mc_pool.cores_count;
1347 insertion_throughput = ((double) (mc_pool.rules_count
1348 / insertion_throughput_time) / 1000);
1349 deletion_throughput = ((double) (mc_pool.rules_count
1350 / deletion_throughput_time) / 1000);
1353 printf("\n%s\n:: [Latency | Insertion] All Cores :: Port %d :: ",
1355 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1357 printf(":: [Latency | Insertion] All Cores :: Port %d :: ", port);
1358 printf("The time for creating %d rules is %f seconds\n",
1359 mc_pool.rules_count, insertion_latency_time);
1361 /* Throughput stats */
1362 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1363 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1364 insertion_throughput);
1365 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1366 printf("The average time for creating %d rules is %f seconds\n",
1367 mc_pool.rules_count, insertion_throughput_time);
1371 printf(":: [Latency | Deletion] All Cores :: Port %d :: Total "
1372 "deletion rate -> %f K Rules/Sec\n",
1373 port, deletion_latency);
1374 printf(":: [Latency | Deletion] All Cores :: Port %d :: ",
1376 printf("The time for deleting %d rules is %f seconds\n",
1377 mc_pool.rules_count, deletion_latency_time);
1379 /* Throughput stats */
1380 printf(":: [Throughput | Deletion] All Cores :: Port %d :: Total "
1381 "deletion rate -> %f K Rules/Sec\n",
1382 port, deletion_throughput);
1383 printf(":: [Throughput | Deletion] All Cores :: Port %d :: ",
1385 printf("The average time for deleting %d rules is %f seconds\n",
1386 mc_pool.rules_count, deletion_throughput_time);
1391 dump_used_mem(uint16_t port)
1394 int64_t last_alloc, current_alloc;
1395 int flow_size_in_bytes;
1397 last_alloc = mc_pool.last_alloc[0];
1398 current_alloc = mc_pool.current_alloc[0];
1400 i = mc_pool.cores_count;
1402 if (last_alloc > mc_pool.last_alloc[i])
1403 last_alloc = mc_pool.last_alloc[i];
1404 if (current_alloc < mc_pool.current_alloc[i])
1405 current_alloc = mc_pool.current_alloc[i];
1408 flow_size_in_bytes = (current_alloc - last_alloc) / mc_pool.rules_count;
1409 printf("\n:: Port %d :: rte_flow size in DPDK layer: %d Bytes\n",
1410 port, flow_size_in_bytes);
1414 run_rte_flow_handler_cores(void *data __rte_unused)
1417 int lcore_counter = 0;
1418 int lcore_id = rte_lcore_id();
1421 RTE_LCORE_FOREACH(i) {
1422 /* If core not needed return. */
1423 if (lcore_id == i) {
1424 printf(":: lcore %d mapped with index %d\n", lcore_id, lcore_counter);
1425 if (lcore_counter >= (int) mc_pool.cores_count)
1431 lcore_id = lcore_counter;
1433 if (lcore_id >= (int) mc_pool.cores_count)
1436 mc_pool.rules_count = rules_count;
1438 flows_handler(lcore_id);
1440 /* Only main core to print total results. */
1444 /* Make sure all cores finished insertion/deletion process. */
1445 rte_eal_mp_wait_lcore();
1447 RTE_ETH_FOREACH_DEV(port) {
1448 /* If port outside portmask */
1449 if (!((ports_mask >> port) & 0x1))
1452 dump_used_cpu_time("Meters:",
1453 port, &mc_pool.meters_record);
1454 dump_used_cpu_time("Flows:",
1455 port, &mc_pool.flows_record);
1456 dump_used_mem(port);
1463 signal_handler(int signum)
1465 if (signum == SIGINT || signum == SIGTERM) {
1466 printf("\n\nSignal %d received, preparing to exit...\n",
1468 printf("Error: Stats are wrong due to sudden signal!\n\n");
1473 static inline uint16_t
1474 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
1477 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
1483 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
1489 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
1490 li->tx_pkts += nr_tx;
1491 li->tx_drops += cnt - nr_tx;
1493 for (i = nr_tx; i < cnt; i++)
1494 rte_pktmbuf_free(li->pkts[i]);
1498 * Method to convert numbers into pretty numbers that easy
1499 * to read. The design here is to add comma after each three
1500 * digits and set all of this inside buffer.
1502 * For example if n = 1799321, the output will be
1503 * 1,799,321 after this method which is easier to read.
1506 pretty_number(uint64_t n, char *buf)
1513 sprintf(p[i], "%03d", (int)(n % 1000));
1518 sprintf(p[i++], "%d", (int)n);
1521 off += sprintf(buf + off, "%s,", p[i]);
1522 buf[strlen(buf) - 1] = '\0';
1528 packet_per_second_stats(void)
1530 struct lcore_info *old;
1531 struct lcore_info *li, *oli;
1535 old = rte_zmalloc("old",
1536 sizeof(struct lcore_info) * RTE_MAX_LCORE, 0);
1538 rte_exit(EXIT_FAILURE, "No Memory available!\n");
1540 memcpy(old, lcore_infos,
1541 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1543 while (!force_quit) {
1544 uint64_t total_tx_pkts = 0;
1545 uint64_t total_rx_pkts = 0;
1546 uint64_t total_tx_drops = 0;
1547 uint64_t tx_delta, rx_delta, drops_delta;
1549 int nr_valid_core = 0;
1554 char go_up_nr_lines[16];
1556 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
1557 printf("%s\r", go_up_nr_lines);
1560 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
1561 printf("%6s %16s %16s %16s\n", "------", "----------------",
1562 "----------------", "----------------");
1564 for (i = 0; i < RTE_MAX_LCORE; i++) {
1565 li = &lcore_infos[i];
1567 if (li->mode != LCORE_MODE_PKT)
1570 tx_delta = li->tx_pkts - oli->tx_pkts;
1571 rx_delta = li->rx_pkts - oli->rx_pkts;
1572 drops_delta = li->tx_drops - oli->tx_drops;
1573 printf("%6d %16s %16s %16s\n", i,
1574 pretty_number(tx_delta, buf[0]),
1575 pretty_number(drops_delta, buf[1]),
1576 pretty_number(rx_delta, buf[2]));
1578 total_tx_pkts += tx_delta;
1579 total_rx_pkts += rx_delta;
1580 total_tx_drops += drops_delta;
1586 if (nr_valid_core > 1) {
1587 printf("%6s %16s %16s %16s\n", "total",
1588 pretty_number(total_tx_pkts, buf[0]),
1589 pretty_number(total_tx_drops, buf[1]),
1590 pretty_number(total_rx_pkts, buf[2]));
1594 memcpy(old, lcore_infos,
1595 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1600 start_forwarding(void *data __rte_unused)
1602 int lcore = rte_lcore_id();
1605 struct lcore_info *li = &lcore_infos[lcore];
1610 if (li->mode == LCORE_MODE_STATS) {
1611 printf(":: started stats on lcore %u\n", lcore);
1612 packet_per_second_stats();
1617 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1618 if (li->streams[stream_id].rx_port == -1)
1622 li->streams[stream_id].rx_port,
1623 li->streams[stream_id].rx_queue);
1626 li->streams[stream_id].tx_port,
1627 li->streams[stream_id].tx_queue);
1633 init_lcore_info(void)
1641 int streams_per_core;
1642 int unassigned_streams;
1644 nr_port = rte_eth_dev_count_avail();
1646 /* First logical core is reserved for stats printing */
1647 lcore = rte_get_next_lcore(-1, 0, 0);
1648 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1651 * Initialize all cores
1652 * All cores at first must have -1 value in all streams
1653 * This means that this stream is not used, or not set
1656 for (i = 0; i < RTE_MAX_LCORE; i++)
1657 for (j = 0; j < MAX_STREAMS; j++) {
1658 lcore_infos[i].streams[j].tx_port = -1;
1659 lcore_infos[i].streams[j].rx_port = -1;
1660 lcore_infos[i].streams[j].tx_queue = -1;
1661 lcore_infos[i].streams[j].rx_queue = -1;
1662 lcore_infos[i].streams_nb = 0;
1666 * Calculate the total streams count.
1667 * Also distribute those streams count between the available
1668 * logical cores except first core, since it's reserved for
1671 nb_fwd_streams = nr_port * RXQ_NUM;
1672 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
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 = 1;
1678 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1679 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1680 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1681 lcore = rte_get_next_lcore(lcore, 0, 0);
1682 lcore_infos[lcore].streams_nb = streams_per_core;
1683 if (unassigned_streams) {
1684 lcore_infos[lcore].streams_nb++;
1685 unassigned_streams--;
1691 * Set the streams for the cores according to each logical
1692 * core stream count.
1693 * The streams is built on the design of what received should
1694 * forward as well, this means that if you received packets on
1695 * port 0 queue 0 then the same queue should forward the
1696 * packets, using the same logical core.
1698 lcore = rte_get_next_lcore(-1, 0, 0);
1699 for (port = 0; port < nr_port; port++) {
1700 /* Create FWD stream */
1701 for (queue = 0; queue < RXQ_NUM; queue++) {
1702 if (!lcore_infos[lcore].streams_nb ||
1703 !(stream_id % lcore_infos[lcore].streams_nb)) {
1704 lcore = rte_get_next_lcore(lcore, 0, 0);
1705 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1708 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1709 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1710 lcore_infos[lcore].streams[stream_id].rx_port = port;
1711 lcore_infos[lcore].streams[stream_id].tx_port = port;
1716 /* Print all streams */
1717 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1718 for (i = 0; i < RTE_MAX_LCORE; i++)
1719 for (j = 0; j < MAX_STREAMS; j++) {
1720 /* No streams for this core */
1721 if (lcore_infos[i].streams[j].tx_port == -1)
1723 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1725 lcore_infos[i].streams[j].rx_port,
1726 lcore_infos[i].streams[j].rx_queue,
1727 lcore_infos[i].streams[j].tx_port,
1728 lcore_infos[i].streams[j].tx_queue);
1737 uint16_t hairpin_queue;
1741 struct rte_eth_hairpin_conf hairpin_conf = {
1744 struct rte_eth_conf port_conf = {
1750 struct rte_eth_txconf txq_conf;
1751 struct rte_eth_rxconf rxq_conf;
1752 struct rte_eth_dev_info dev_info;
1754 nr_queues = RXQ_NUM;
1755 if (hairpin_queues_num != 0)
1756 nr_queues = RXQ_NUM + hairpin_queues_num;
1758 nr_ports = rte_eth_dev_count_avail();
1760 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1762 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1763 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1766 if (mbuf_mp == NULL)
1767 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1769 for (port_id = 0; port_id < nr_ports; port_id++) {
1770 ret = rte_eth_dev_info_get(port_id, &dev_info);
1772 rte_exit(EXIT_FAILURE,
1773 "Error during getting device"
1774 " (port %u) info: %s\n",
1775 port_id, strerror(-ret));
1777 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1778 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1780 printf(":: initializing port: %d\n", port_id);
1782 ret = rte_eth_dev_configure(port_id, nr_queues,
1783 nr_queues, &port_conf);
1785 rte_exit(EXIT_FAILURE,
1786 ":: cannot configure device: err=%d, port=%u\n",
1789 rxq_conf = dev_info.default_rxconf;
1790 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1791 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1792 rte_eth_dev_socket_id(port_id),
1796 rte_exit(EXIT_FAILURE,
1797 ":: Rx queue setup failed: err=%d, port=%u\n",
1801 txq_conf = dev_info.default_txconf;
1802 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1803 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1804 rte_eth_dev_socket_id(port_id),
1807 rte_exit(EXIT_FAILURE,
1808 ":: Tx queue setup failed: err=%d, port=%u\n",
1812 /* Catch all packets from traffic generator. */
1813 ret = rte_eth_promiscuous_enable(port_id);
1815 rte_exit(EXIT_FAILURE,
1816 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1817 rte_strerror(-ret), port_id);
1819 if (hairpin_queues_num != 0) {
1821 * Configure peer which represents hairpin Tx.
1822 * Hairpin queue numbers start after standard queues
1823 * (RXQ_NUM and TXQ_NUM).
1825 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1826 hairpin_queue < nr_queues;
1827 hairpin_queue++, std_queue++) {
1828 hairpin_conf.peers[0].port = port_id;
1829 hairpin_conf.peers[0].queue =
1830 std_queue + TXQ_NUM;
1831 ret = rte_eth_rx_hairpin_queue_setup(
1832 port_id, hairpin_queue,
1833 NR_RXD, &hairpin_conf);
1835 rte_exit(EXIT_FAILURE,
1836 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1840 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1841 hairpin_queue < nr_queues;
1842 hairpin_queue++, std_queue++) {
1843 hairpin_conf.peers[0].port = port_id;
1844 hairpin_conf.peers[0].queue =
1845 std_queue + RXQ_NUM;
1846 ret = rte_eth_tx_hairpin_queue_setup(
1847 port_id, hairpin_queue,
1848 NR_TXD, &hairpin_conf);
1850 rte_exit(EXIT_FAILURE,
1851 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1856 ret = rte_eth_dev_start(port_id);
1858 rte_exit(EXIT_FAILURE,
1859 "rte_eth_dev_start:err=%d, port=%u\n",
1862 printf(":: initializing port: %d done\n", port_id);
1867 main(int argc, char **argv)
1871 struct rte_flow_error error;
1873 ret = rte_eal_init(argc, argv);
1875 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1878 dump_iterations = false;
1879 rules_count = DEFAULT_RULES_COUNT;
1880 rules_batch = DEFAULT_RULES_BATCH;
1881 delete_flag = false;
1882 dump_socket_mem_flag = false;
1883 flow_group = DEFAULT_GROUP;
1884 unique_data = false;
1886 signal(SIGINT, signal_handler);
1887 signal(SIGTERM, signal_handler);
1892 args_parse(argc, argv);
1896 nb_lcores = rte_lcore_count();
1898 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1900 printf(":: Flows Count per port: %d\n\n", rules_count);
1903 create_meter_profile();
1904 rte_eal_mp_remote_launch(run_rte_flow_handler_cores, NULL, CALL_MAIN);
1908 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MAIN);
1910 if (has_meter() && delete_flag)
1911 destroy_meter_profile();
1913 RTE_ETH_FOREACH_DEV(port) {
1914 rte_flow_flush(port, &error);
1915 if (rte_eth_dev_stop(port) != 0)
1916 printf("Failed to stop device on port %u\n", port);
1917 rte_eth_dev_close(port);
1919 printf("\nBye ...\n");