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;
65 static struct rte_mempool *mbuf_mp;
66 static uint32_t nb_lcores;
67 static uint32_t rules_count;
68 static uint32_t rules_batch;
69 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
70 static uint32_t nb_lcores;
72 #define MAX_PKT_BURST 32
73 #define LCORE_MODE_PKT 1
74 #define LCORE_MODE_STATS 2
75 #define MAX_STREAMS 64
76 #define METER_CREATE 1
77 #define METER_DELETE 2
89 struct stream streams[MAX_STREAMS];
94 struct rte_mbuf *pkts[MAX_PKT_BURST];
95 } __rte_cache_aligned;
97 static struct lcore_info lcore_infos[RTE_MAX_LCORE];
99 struct used_cpu_time {
100 double insertion[MAX_PORTS][RTE_MAX_LCORE];
101 double deletion[MAX_PORTS][RTE_MAX_LCORE];
104 struct multi_cores_pool {
105 uint32_t cores_count;
106 uint32_t rules_count;
107 struct used_cpu_time create_meter;
108 struct used_cpu_time create_flow;
109 int64_t last_alloc[RTE_MAX_LCORE];
110 int64_t current_alloc[RTE_MAX_LCORE];
111 } __rte_cache_aligned;
113 static struct multi_cores_pool mc_pool = {
118 usage(char *progname)
120 printf("\nusage: %s\n", progname);
121 printf("\nControl configurations:\n");
122 printf(" --rules-count=N: to set the number of needed"
123 " rules to insert, default is %d\n", DEFAULT_RULES_COUNT);
124 printf(" --rules-batch=N: set number of batched rules,"
125 " default is %d\n", DEFAULT_RULES_BATCH);
126 printf(" --dump-iterations: To print rates for each"
128 printf(" --deletion-rate: Enable deletion rate"
130 printf(" --dump-socket-mem: To dump all socket memory\n");
131 printf(" --enable-fwd: To enable packets forwarding"
132 " after insertion\n");
133 printf(" --portmask=N: hexadecimal bitmask of ports used\n");
135 printf("To set flow attributes:\n");
136 printf(" --ingress: set ingress attribute in flows\n");
137 printf(" --egress: set egress attribute in flows\n");
138 printf(" --transfer: set transfer attribute in flows\n");
139 printf(" --group=N: set group for all flows,"
140 " default is %d\n", DEFAULT_GROUP);
141 printf(" --cores=N: to set the number of needed "
142 "cores to insert rte_flow rules, default is 1\n");
144 printf("To set flow items:\n");
145 printf(" --ether: add ether layer in flow items\n");
146 printf(" --vlan: add vlan layer in flow items\n");
147 printf(" --ipv4: add ipv4 layer in flow items\n");
148 printf(" --ipv6: add ipv6 layer in flow items\n");
149 printf(" --tcp: add tcp layer in flow items\n");
150 printf(" --udp: add udp layer in flow items\n");
151 printf(" --vxlan: add vxlan layer in flow items\n");
152 printf(" --vxlan-gpe: add vxlan-gpe layer in flow items\n");
153 printf(" --gre: add gre layer in flow items\n");
154 printf(" --geneve: add geneve layer in flow items\n");
155 printf(" --gtp: add gtp layer in flow items\n");
156 printf(" --meta: add meta layer in flow items\n");
157 printf(" --tag: add tag layer in flow items\n");
158 printf(" --icmpv4: add icmpv4 layer in flow items\n");
159 printf(" --icmpv6: add icmpv6 layer in flow items\n");
161 printf("To set flow actions:\n");
162 printf(" --port-id: add port-id action in flow actions\n");
163 printf(" --rss: add rss action in flow actions\n");
164 printf(" --queue: add queue action in flow actions\n");
165 printf(" --jump: add jump action in flow actions\n");
166 printf(" --mark: add mark action in flow actions\n");
167 printf(" --count: add count action in flow actions\n");
168 printf(" --set-meta: add set meta action in flow actions\n");
169 printf(" --set-tag: add set tag action in flow actions\n");
170 printf(" --drop: add drop action in flow actions\n");
171 printf(" --hairpin-queue=N: add hairpin-queue action in flow actions\n");
172 printf(" --hairpin-rss=N: add hairpin-rss action in flow actions\n");
173 printf(" --set-src-mac: add set src mac action to flow actions\n"
174 "Src mac to be set is random each flow\n");
175 printf(" --set-dst-mac: add set dst mac action to flow actions\n"
176 "Dst mac to be set is random each flow\n");
177 printf(" --set-src-ipv4: add set src ipv4 action to flow actions\n"
178 "Src ipv4 to be set is random each flow\n");
179 printf(" --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
180 "Dst ipv4 to be set is random each flow\n");
181 printf(" --set-src-ipv6: add set src ipv6 action to flow actions\n"
182 "Src ipv6 to be set is random each flow\n");
183 printf(" --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
184 "Dst ipv6 to be set is random each flow\n");
185 printf(" --set-src-tp: add set src tp action to flow actions\n"
186 "Src tp to be set is random each flow\n");
187 printf(" --set-dst-tp: add set dst tp action to flow actions\n"
188 "Dst tp to be set is random each flow\n");
189 printf(" --inc-tcp-ack: add inc tcp ack action to flow actions\n"
190 "tcp ack will be increments by 1\n");
191 printf(" --dec-tcp-ack: add dec tcp ack action to flow actions\n"
192 "tcp ack will be decrements by 1\n");
193 printf(" --inc-tcp-seq: add inc tcp seq action to flow actions\n"
194 "tcp seq will be increments by 1\n");
195 printf(" --dec-tcp-seq: add dec tcp seq action to flow actions\n"
196 "tcp seq will be decrements by 1\n");
197 printf(" --set-ttl: add set ttl action to flow actions\n"
198 "L3 ttl to be set is random each flow\n");
199 printf(" --dec-ttl: add dec ttl action to flow actions\n"
200 "L3 ttl will be decrements by 1\n");
201 printf(" --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
202 "ipv4 dscp value to be set is random each flow\n");
203 printf(" --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
204 "ipv6 dscp value to be set is random each flow\n");
205 printf(" --flag: add flag action to flow actions\n");
206 printf(" --meter: add meter action to flow actions\n");
207 printf(" --raw-encap=<data>: add raw encap action to flow actions\n"
208 "Data is the data needed to be encaped\n"
209 "Example: raw-encap=ether,ipv4,udp,vxlan\n");
210 printf(" --raw-decap=<data>: add raw decap action to flow actions\n"
211 "Data is the data needed to be decaped\n"
212 "Example: raw-decap=ether,ipv4,udp,vxlan\n");
213 printf(" --vxlan-encap: add vxlan-encap action to flow actions\n"
214 "Encapped data is fixed with pattern: ether,ipv4,udp,vxlan\n"
215 "With fixed values\n");
216 printf(" --vxlan-decap: add vxlan_decap action to flow actions\n");
220 args_parse(int argc, char **argv)
230 static const struct option_dict {
239 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
240 .map = &flow_items[0],
241 .map_idx = &items_idx
245 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
246 .map = &flow_items[0],
247 .map_idx = &items_idx
251 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
252 .map = &flow_items[0],
253 .map_idx = &items_idx
257 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
258 .map = &flow_items[0],
259 .map_idx = &items_idx
263 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
264 .map = &flow_items[0],
265 .map_idx = &items_idx
269 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
270 .map = &flow_items[0],
271 .map_idx = &items_idx
275 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
276 .map = &flow_items[0],
277 .map_idx = &items_idx
281 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
282 .map = &flow_items[0],
283 .map_idx = &items_idx
287 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
288 .map = &flow_items[0],
289 .map_idx = &items_idx
293 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
294 .map = &flow_items[0],
295 .map_idx = &items_idx
299 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
300 .map = &flow_items[0],
301 .map_idx = &items_idx
305 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
306 .map = &flow_items[0],
307 .map_idx = &items_idx
311 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
312 .map = &flow_items[0],
313 .map_idx = &items_idx
317 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP),
318 .map = &flow_items[0],
319 .map_idx = &items_idx
323 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP6),
324 .map = &flow_items[0],
325 .map_idx = &items_idx
330 .map = &flow_attrs[0],
331 .map_idx = &attrs_idx
336 .map = &flow_attrs[0],
337 .map_idx = &attrs_idx
342 .map = &flow_attrs[0],
343 .map_idx = &attrs_idx
347 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
348 .map = &flow_actions[0],
349 .map_idx = &actions_idx
353 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
354 .map = &flow_actions[0],
355 .map_idx = &actions_idx
359 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
360 .map = &flow_actions[0],
361 .map_idx = &actions_idx
365 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
366 .map = &flow_actions[0],
367 .map_idx = &actions_idx
371 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
372 .map = &flow_actions[0],
373 .map_idx = &actions_idx
377 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
378 .map = &flow_actions[0],
379 .map_idx = &actions_idx
383 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
384 .map = &flow_actions[0],
385 .map_idx = &actions_idx
389 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
390 .map = &flow_actions[0],
391 .map_idx = &actions_idx
395 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
396 .map = &flow_actions[0],
397 .map_idx = &actions_idx
400 .str = "set-src-mac",
401 .mask = FLOW_ACTION_MASK(
402 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
404 .map = &flow_actions[0],
405 .map_idx = &actions_idx
408 .str = "set-dst-mac",
409 .mask = FLOW_ACTION_MASK(
410 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
412 .map = &flow_actions[0],
413 .map_idx = &actions_idx
416 .str = "set-src-ipv4",
417 .mask = FLOW_ACTION_MASK(
418 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
420 .map = &flow_actions[0],
421 .map_idx = &actions_idx
424 .str = "set-dst-ipv4",
425 .mask = FLOW_ACTION_MASK(
426 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
428 .map = &flow_actions[0],
429 .map_idx = &actions_idx
432 .str = "set-src-ipv6",
433 .mask = FLOW_ACTION_MASK(
434 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
436 .map = &flow_actions[0],
437 .map_idx = &actions_idx
440 .str = "set-dst-ipv6",
441 .mask = FLOW_ACTION_MASK(
442 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
444 .map = &flow_actions[0],
445 .map_idx = &actions_idx
449 .mask = FLOW_ACTION_MASK(
450 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
452 .map = &flow_actions[0],
453 .map_idx = &actions_idx
457 .mask = FLOW_ACTION_MASK(
458 RTE_FLOW_ACTION_TYPE_SET_TP_DST
460 .map = &flow_actions[0],
461 .map_idx = &actions_idx
464 .str = "inc-tcp-ack",
465 .mask = FLOW_ACTION_MASK(
466 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
468 .map = &flow_actions[0],
469 .map_idx = &actions_idx
472 .str = "dec-tcp-ack",
473 .mask = FLOW_ACTION_MASK(
474 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
476 .map = &flow_actions[0],
477 .map_idx = &actions_idx
480 .str = "inc-tcp-seq",
481 .mask = FLOW_ACTION_MASK(
482 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
484 .map = &flow_actions[0],
485 .map_idx = &actions_idx
488 .str = "dec-tcp-seq",
489 .mask = FLOW_ACTION_MASK(
490 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
492 .map = &flow_actions[0],
493 .map_idx = &actions_idx
497 .mask = FLOW_ACTION_MASK(
498 RTE_FLOW_ACTION_TYPE_SET_TTL
500 .map = &flow_actions[0],
501 .map_idx = &actions_idx
505 .mask = FLOW_ACTION_MASK(
506 RTE_FLOW_ACTION_TYPE_DEC_TTL
508 .map = &flow_actions[0],
509 .map_idx = &actions_idx
512 .str = "set-ipv4-dscp",
513 .mask = FLOW_ACTION_MASK(
514 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
516 .map = &flow_actions[0],
517 .map_idx = &actions_idx
520 .str = "set-ipv6-dscp",
521 .mask = FLOW_ACTION_MASK(
522 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
524 .map = &flow_actions[0],
525 .map_idx = &actions_idx
529 .mask = FLOW_ACTION_MASK(
530 RTE_FLOW_ACTION_TYPE_FLAG
532 .map = &flow_actions[0],
533 .map_idx = &actions_idx
537 .mask = FLOW_ACTION_MASK(
538 RTE_FLOW_ACTION_TYPE_METER
540 .map = &flow_actions[0],
541 .map_idx = &actions_idx
544 .str = "vxlan-encap",
545 .mask = FLOW_ACTION_MASK(
546 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP
548 .map = &flow_actions[0],
549 .map_idx = &actions_idx
552 .str = "vxlan-decap",
553 .mask = FLOW_ACTION_MASK(
554 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP
556 .map = &flow_actions[0],
557 .map_idx = &actions_idx
561 static const struct option lgopts[] = {
564 { "rules-count", 1, 0, 0 },
565 { "rules-batch", 1, 0, 0 },
566 { "dump-iterations", 0, 0, 0 },
567 { "deletion-rate", 0, 0, 0 },
568 { "dump-socket-mem", 0, 0, 0 },
569 { "enable-fwd", 0, 0, 0 },
570 { "portmask", 1, 0, 0 },
571 { "cores", 1, 0, 0 },
573 { "ingress", 0, 0, 0 },
574 { "egress", 0, 0, 0 },
575 { "transfer", 0, 0, 0 },
576 { "group", 1, 0, 0 },
578 { "ether", 0, 0, 0 },
584 { "vxlan", 0, 0, 0 },
585 { "vxlan-gpe", 0, 0, 0 },
587 { "geneve", 0, 0, 0 },
591 { "icmpv4", 0, 0, 0 },
592 { "icmpv6", 0, 0, 0 },
594 { "port-id", 0, 0, 0 },
596 { "queue", 0, 0, 0 },
599 { "count", 0, 0, 0 },
600 { "set-meta", 0, 0, 0 },
601 { "set-tag", 0, 0, 0 },
603 { "hairpin-queue", 1, 0, 0 },
604 { "hairpin-rss", 1, 0, 0 },
605 { "set-src-mac", 0, 0, 0 },
606 { "set-dst-mac", 0, 0, 0 },
607 { "set-src-ipv4", 0, 0, 0 },
608 { "set-dst-ipv4", 0, 0, 0 },
609 { "set-src-ipv6", 0, 0, 0 },
610 { "set-dst-ipv6", 0, 0, 0 },
611 { "set-src-tp", 0, 0, 0 },
612 { "set-dst-tp", 0, 0, 0 },
613 { "inc-tcp-ack", 0, 0, 0 },
614 { "dec-tcp-ack", 0, 0, 0 },
615 { "inc-tcp-seq", 0, 0, 0 },
616 { "dec-tcp-seq", 0, 0, 0 },
617 { "set-ttl", 0, 0, 0 },
618 { "dec-ttl", 0, 0, 0 },
619 { "set-ipv4-dscp", 0, 0, 0 },
620 { "set-ipv6-dscp", 0, 0, 0 },
622 { "meter", 0, 0, 0 },
623 { "raw-encap", 1, 0, 0 },
624 { "raw-decap", 1, 0, 0 },
625 { "vxlan-encap", 0, 0, 0 },
626 { "vxlan-decap", 0, 0, 0 },
629 RTE_ETH_FOREACH_DEV(i)
630 ports_mask |= 1 << i;
632 hairpin_queues_num = 0;
635 printf(":: Flow -> ");
636 while ((opt = getopt_long(argc, argvopt, "",
637 lgopts, &opt_idx)) != EOF) {
640 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
642 rte_exit(EXIT_SUCCESS, "Displayed help\n");
645 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
650 rte_exit(EXIT_SUCCESS,
651 "flow group should be >= 0\n");
652 printf("group %d / ", flow_group);
655 for (i = 0; i < RTE_DIM(flow_options); i++)
656 if (strcmp(lgopts[opt_idx].name,
657 flow_options[i].str) == 0) {
659 (*flow_options[i].map_idx)++] =
660 flow_options[i].mask;
661 printf("%s / ", flow_options[i].str);
664 if (strcmp(lgopts[opt_idx].name,
665 "hairpin-rss") == 0) {
668 hairpin_queues_num = n;
670 rte_exit(EXIT_SUCCESS,
671 "Hairpin queues should be > 0\n");
673 flow_actions[actions_idx++] =
675 printf("hairpin-rss / ");
677 if (strcmp(lgopts[opt_idx].name,
678 "hairpin-queue") == 0) {
681 hairpin_queues_num = n;
683 rte_exit(EXIT_SUCCESS,
684 "Hairpin queues should be > 0\n");
686 flow_actions[actions_idx++] =
687 HAIRPIN_QUEUE_ACTION;
688 printf("hairpin-queue / ");
691 if (strcmp(lgopts[opt_idx].name, "raw-encap") == 0) {
692 printf("raw-encap ");
693 flow_actions[actions_idx++] =
695 RTE_FLOW_ACTION_TYPE_RAW_ENCAP
698 token = strtok(optarg, ",");
699 while (token != NULL) {
700 for (i = 0; i < RTE_DIM(flow_options); i++) {
701 if (strcmp(flow_options[i].str, token) == 0) {
702 printf("%s,", token);
703 encap_data |= flow_options[i].mask;
706 /* Reached last item with no match */
707 if (i == (RTE_DIM(flow_options) - 1)) {
708 fprintf(stderr, "Invalid encap item: %s\n", token);
710 rte_exit(EXIT_SUCCESS, "Invalid encap item\n");
713 token = strtok(NULL, ",");
717 if (strcmp(lgopts[opt_idx].name, "raw-decap") == 0) {
718 printf("raw-decap ");
719 flow_actions[actions_idx++] =
721 RTE_FLOW_ACTION_TYPE_RAW_DECAP
724 token = strtok(optarg, ",");
725 while (token != NULL) {
726 for (i = 0; i < RTE_DIM(flow_options); i++) {
727 if (strcmp(flow_options[i].str, token) == 0) {
728 printf("%s,", token);
729 encap_data |= flow_options[i].mask;
732 /* Reached last item with no match */
733 if (i == (RTE_DIM(flow_options) - 1)) {
734 fprintf(stderr, "Invalid decap item: %s\n", token);
736 rte_exit(EXIT_SUCCESS, "Invalid decap item\n");
739 token = strtok(NULL, ",");
744 if (strcmp(lgopts[opt_idx].name,
745 "rules-batch") == 0) {
747 if (n >= DEFAULT_RULES_BATCH)
750 printf("\n\nrules_batch should be >= %d\n",
751 DEFAULT_RULES_BATCH);
752 rte_exit(EXIT_SUCCESS, " ");
755 if (strcmp(lgopts[opt_idx].name,
756 "rules-count") == 0) {
758 if (n >= (int) rules_batch)
761 printf("\n\nrules_count should be >= %d\n",
765 if (strcmp(lgopts[opt_idx].name,
766 "dump-iterations") == 0)
767 dump_iterations = true;
768 if (strcmp(lgopts[opt_idx].name,
769 "deletion-rate") == 0)
771 if (strcmp(lgopts[opt_idx].name,
772 "dump-socket-mem") == 0)
773 dump_socket_mem_flag = true;
774 if (strcmp(lgopts[opt_idx].name,
777 if (strcmp(lgopts[opt_idx].name,
779 /* parse hexadecimal string */
781 pm = strtoull(optarg, &end, 16);
782 if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
783 rte_exit(EXIT_FAILURE, "Invalid fwd port mask\n");
786 if (strcmp(lgopts[opt_idx].name, "cores") == 0) {
788 if ((int) rte_lcore_count() <= n) {
789 printf("\nError: you need %d cores to run on multi-cores\n"
790 "Existing cores are: %d\n", n, rte_lcore_count());
791 rte_exit(EXIT_FAILURE, " ");
793 if (n <= RTE_MAX_LCORE && n > 0)
794 mc_pool.cores_count = n;
796 printf("Error: cores count must be > 0 "
797 " and < %d\n", RTE_MAX_LCORE);
798 rte_exit(EXIT_FAILURE, " ");
803 fprintf(stderr, "Invalid option: %s\n", argv[optind]);
805 rte_exit(EXIT_SUCCESS, "Invalid option\n");
809 printf("end_flow\n");
812 /* Dump the socket memory statistics on console */
814 dump_socket_mem(FILE *f)
816 struct rte_malloc_socket_stats socket_stats;
821 unsigned int n_alloc = 0;
822 unsigned int n_free = 0;
823 bool active_nodes = false;
826 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
827 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
828 !socket_stats.heap_totalsz_bytes)
831 total += socket_stats.heap_totalsz_bytes;
832 alloc += socket_stats.heap_allocsz_bytes;
833 free += socket_stats.heap_freesz_bytes;
834 n_alloc += socket_stats.alloc_count;
835 n_free += socket_stats.free_count;
836 if (dump_socket_mem_flag) {
837 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
839 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
840 " %.6lf(%.3lf%%)\nfree: %.6lf"
842 "\ncount alloc: %u\nfree: %u\n",
844 socket_stats.heap_totalsz_bytes / 1.0e6,
845 socket_stats.heap_allocsz_bytes / 1.0e6,
846 (double)socket_stats.heap_allocsz_bytes * 100 /
847 (double)socket_stats.heap_totalsz_bytes,
848 socket_stats.heap_freesz_bytes / 1.0e6,
849 socket_stats.greatest_free_size / 1.0e6,
850 socket_stats.alloc_count,
851 socket_stats.free_count);
852 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
855 if (dump_socket_mem_flag && active_nodes) {
857 "\nTotal: size(M)\ntotal: %.6lf"
858 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
859 "\ncount alloc: %u\nfree: %u\n",
860 total / 1.0e6, alloc / 1.0e6,
861 (double)alloc * 100 / (double)total, free / 1.0e6,
863 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
869 print_flow_error(struct rte_flow_error error)
871 printf("Flow can't be created %d message: %s\n",
873 error.message ? error.message : "(no stated reason)");
877 print_rules_batches(double *cpu_time_per_batch)
883 for (idx = 0; idx < MAX_BATCHES_COUNT; idx++) {
884 if (!cpu_time_per_batch[idx])
886 delta = (double)(rules_batch / cpu_time_per_batch[idx]);
887 rate = delta / 1000; /* Save rate in K unit. */
888 printf(":: Rules batch #%d: %d rules "
889 "in %f sec[ Rate = %f K Rule/Sec ]\n",
891 cpu_time_per_batch[idx], rate);
901 for (i = 0; i < MAX_ACTIONS_NUM; i++) {
902 if (flow_actions[i] == 0)
905 & FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_METER))
912 create_meter_rule(int port_id, uint32_t counter)
915 struct rte_mtr_params params;
916 uint32_t default_prof_id = 100;
917 struct rte_mtr_error error;
919 memset(¶ms, 0, sizeof(struct rte_mtr_params));
920 params.meter_enable = 1;
921 params.stats_mask = 0xffff;
922 params.use_prev_mtr_color = 0;
923 params.dscp_table = NULL;
926 params.meter_profile_id = default_prof_id;
927 params.action[RTE_COLOR_GREEN] =
928 MTR_POLICER_ACTION_COLOR_GREEN;
929 params.action[RTE_COLOR_YELLOW] =
930 MTR_POLICER_ACTION_COLOR_YELLOW;
931 params.action[RTE_COLOR_RED] =
932 MTR_POLICER_ACTION_DROP;
934 ret = rte_mtr_create(port_id, counter, ¶ms, 1, &error);
936 printf("Port %u create meter idx(%d) error(%d) message: %s\n",
937 port_id, counter, error.type,
938 error.message ? error.message : "(no stated reason)");
939 rte_exit(EXIT_FAILURE, "error in creating meter");
944 destroy_meter_rule(int port_id, uint32_t counter)
946 struct rte_mtr_error error;
948 if (rte_mtr_destroy(port_id, counter, &error)) {
949 printf("Port %u destroy meter(%d) error(%d) message: %s\n",
950 port_id, counter, error.type,
951 error.message ? error.message : "(no stated reason)");
952 rte_exit(EXIT_FAILURE, "Error in deleting meter rule");
957 meters_handler(int port_id, uint8_t core_id, uint8_t ops)
959 uint64_t start_batch;
960 double cpu_time_used, insertion_rate;
961 int rules_count_per_core, rules_batch_idx;
962 uint32_t counter, start_counter = 0, end_counter;
963 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
965 rules_count_per_core = rules_count / mc_pool.cores_count;
968 start_counter = core_id * rules_count_per_core;
969 end_counter = (core_id + 1) * rules_count_per_core;
972 start_batch = rte_rdtsc();
973 for (counter = start_counter; counter < end_counter; counter++) {
974 if (ops == METER_CREATE)
975 create_meter_rule(port_id, counter);
977 destroy_meter_rule(port_id, counter);
979 * Save the insertion rate for rules batch.
980 * Check if the insertion reached the rules
981 * patch counter, then save the insertion rate
984 if (!((counter + 1) % rules_batch)) {
985 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
986 cpu_time_per_batch[rules_batch_idx] =
987 ((double)(rte_rdtsc() - start_batch))
989 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
990 start_batch = rte_rdtsc();
994 /* Print insertion rates for all batches */
996 print_rules_batches(cpu_time_per_batch);
999 ((double) (rules_count_per_core / cpu_time_used) / 1000);
1001 /* Insertion rate for all rules in one core */
1002 printf(":: Port %d :: Core %d Meter %s :: start @[%d] - end @[%d],"
1003 " use:%.02fs, rate:%.02fk Rule/Sec\n",
1004 port_id, core_id, ops == METER_CREATE ? "create" : "delete",
1005 start_counter, end_counter - 1,
1006 cpu_time_used, insertion_rate);
1008 if (ops == METER_CREATE)
1009 mc_pool.create_meter.insertion[port_id][core_id]
1012 mc_pool.create_meter.deletion[port_id][core_id]
1017 destroy_meter_profile(void)
1019 struct rte_mtr_error error;
1023 nr_ports = rte_eth_dev_count_avail();
1024 for (port_id = 0; port_id < nr_ports; port_id++) {
1025 /* If port outside portmask */
1026 if (!((ports_mask >> port_id) & 0x1))
1029 if (rte_mtr_meter_profile_delete
1030 (port_id, DEFAULT_METER_PROF_ID, &error)) {
1031 printf("Port %u del profile error(%d) message: %s\n",
1032 port_id, error.type,
1033 error.message ? error.message : "(no stated reason)");
1034 rte_exit(EXIT_FAILURE, "Error: Destroy meter profile Failed!\n");
1040 create_meter_profile(void)
1044 struct rte_mtr_meter_profile mp;
1045 struct rte_mtr_error error;
1048 *currently , only create one meter file for one port
1049 *1 meter profile -> N meter rules -> N rte flows
1051 memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
1052 nr_ports = rte_eth_dev_count_avail();
1053 for (port_id = 0; port_id < nr_ports; port_id++) {
1054 /* If port outside portmask */
1055 if (!((ports_mask >> port_id) & 0x1))
1058 mp.alg = RTE_MTR_SRTCM_RFC2697;
1059 mp.srtcm_rfc2697.cir = METER_CIR;
1060 mp.srtcm_rfc2697.cbs = METER_CIR / 8;
1061 mp.srtcm_rfc2697.ebs = 0;
1063 ret = rte_mtr_meter_profile_add
1064 (port_id, DEFAULT_METER_PROF_ID, &mp, &error);
1066 printf("Port %u create Profile error(%d) message: %s\n",
1067 port_id, error.type,
1068 error.message ? error.message : "(no stated reason)");
1069 rte_exit(EXIT_FAILURE, "Error: Creation meter profile Failed!\n");
1075 destroy_flows(int port_id, uint8_t core_id, struct rte_flow **flows_list)
1077 struct rte_flow_error error;
1078 clock_t start_batch, end_batch;
1079 double cpu_time_used = 0;
1080 double deletion_rate;
1081 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1084 int rules_batch_idx;
1085 int rules_count_per_core;
1087 rules_count_per_core = rules_count / mc_pool.cores_count;
1088 /* If group > 0 , should add 1 flow which created in group 0 */
1089 if (flow_group > 0 && core_id == 0)
1090 rules_count_per_core++;
1092 start_batch = rte_rdtsc();
1093 for (i = 0; i < (uint32_t) rules_count_per_core; i++) {
1094 if (flows_list[i] == 0)
1097 memset(&error, 0x33, sizeof(error));
1098 if (rte_flow_destroy(port_id, flows_list[i], &error)) {
1099 print_flow_error(error);
1100 rte_exit(EXIT_FAILURE, "Error in deleting flow");
1104 * Save the deletion rate for rules batch.
1105 * Check if the deletion reached the rules
1106 * patch counter, then save the deletion rate
1109 if (!((i + 1) % rules_batch)) {
1110 end_batch = rte_rdtsc();
1111 delta = (double) (end_batch - start_batch);
1112 rules_batch_idx = ((i + 1) / rules_batch) - 1;
1113 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_tsc_hz();
1114 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1115 start_batch = rte_rdtsc();
1119 /* Print deletion rates for all batches */
1120 if (dump_iterations)
1121 print_rules_batches(cpu_time_per_batch);
1123 /* Deletion rate for all rules */
1124 deletion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1125 printf(":: Port %d :: Core %d :: Rules deletion rate -> %f K Rule/Sec\n",
1126 port_id, core_id, deletion_rate);
1127 printf(":: Port %d :: Core %d :: The time for deleting %d rules is %f seconds\n",
1128 port_id, core_id, rules_count_per_core, cpu_time_used);
1130 mc_pool.create_flow.deletion[port_id][core_id] = cpu_time_used;
1133 static struct rte_flow **
1134 insert_flows(int port_id, uint8_t core_id)
1136 struct rte_flow **flows_list;
1137 struct rte_flow_error error;
1138 clock_t start_batch, end_batch;
1139 double cpu_time_used;
1140 double insertion_rate;
1141 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1143 uint32_t flow_index;
1144 uint32_t counter, start_counter = 0, end_counter;
1145 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
1146 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
1147 int rules_batch_idx;
1148 int rules_count_per_core;
1150 rules_count_per_core = rules_count / mc_pool.cores_count;
1152 /* Set boundaries of rules for each core. */
1154 start_counter = core_id * rules_count_per_core;
1155 end_counter = (core_id + 1) * rules_count_per_core;
1157 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
1158 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
1160 flows_list = rte_zmalloc("flows_list",
1161 (sizeof(struct rte_flow *) * rules_count_per_core) + 1, 0);
1162 if (flows_list == NULL)
1163 rte_exit(EXIT_FAILURE, "No Memory available!");
1167 if (flow_group > 0 && core_id == 0) {
1169 * Create global rule to jump into flow_group,
1170 * this way the app will avoid the default rules.
1172 * This rule will be created only once.
1175 * group 0 eth / end actions jump group <flow_group>
1177 flow = generate_flow(port_id, 0, flow_attrs,
1178 global_items, global_actions,
1179 flow_group, 0, 0, 0, 0, core_id, &error);
1182 print_flow_error(error);
1183 rte_exit(EXIT_FAILURE, "error in creating flow");
1185 flows_list[flow_index++] = flow;
1188 start_batch = rte_rdtsc();
1189 for (counter = start_counter; counter < end_counter; counter++) {
1190 flow = generate_flow(port_id, flow_group,
1191 flow_attrs, flow_items, flow_actions,
1192 JUMP_ACTION_TABLE, counter,
1194 encap_data, decap_data,
1198 counter = end_counter;
1201 print_flow_error(error);
1202 rte_exit(EXIT_FAILURE, "error in creating flow");
1205 flows_list[flow_index++] = flow;
1208 * Save the insertion rate for rules batch.
1209 * Check if the insertion reached the rules
1210 * patch counter, then save the insertion rate
1213 if (!((counter + 1) % rules_batch)) {
1214 end_batch = rte_rdtsc();
1215 delta = (double) (end_batch - start_batch);
1216 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
1217 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_tsc_hz();
1218 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1219 start_batch = rte_rdtsc();
1223 /* Print insertion rates for all batches */
1224 if (dump_iterations)
1225 print_rules_batches(cpu_time_per_batch);
1227 printf(":: Port %d :: Core %d boundaries :: start @[%d] - end @[%d]\n",
1228 port_id, core_id, start_counter, end_counter - 1);
1230 /* Insertion rate for all rules in one core */
1231 insertion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1232 printf(":: Port %d :: Core %d :: Rules insertion rate -> %f K Rule/Sec\n",
1233 port_id, core_id, insertion_rate);
1234 printf(":: Port %d :: Core %d :: The time for creating %d in rules %f seconds\n",
1235 port_id, core_id, rules_count_per_core, cpu_time_used);
1237 mc_pool.create_flow.insertion[port_id][core_id] = cpu_time_used;
1242 flows_handler(uint8_t core_id)
1244 struct rte_flow **flows_list;
1248 nr_ports = rte_eth_dev_count_avail();
1250 if (rules_batch > rules_count)
1251 rules_batch = rules_count;
1253 printf(":: Rules Count per port: %d\n\n", rules_count);
1255 for (port_id = 0; port_id < nr_ports; port_id++) {
1256 /* If port outside portmask */
1257 if (!((ports_mask >> port_id) & 0x1))
1260 /* Insertion part. */
1261 mc_pool.last_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1263 meters_handler(port_id, core_id, METER_CREATE);
1264 flows_list = insert_flows(port_id, core_id);
1265 if (flows_list == NULL)
1266 rte_exit(EXIT_FAILURE, "Error: Insertion Failed!\n");
1267 mc_pool.current_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1269 /* Deletion part. */
1271 destroy_flows(port_id, core_id, flows_list);
1273 meters_handler(port_id, core_id, METER_DELETE);
1279 dump_used_cpu_time(const char *item,
1280 uint16_t port, struct used_cpu_time *used_time)
1283 /* Latency: total count of rte rules divided
1284 * over max time used by thread between all
1287 * Throughput: total count of rte rules divided
1288 * over the average of the time cosumed by all
1291 double insertion_latency_time;
1292 double insertion_throughput_time;
1293 double deletion_latency_time;
1294 double deletion_throughput_time;
1295 double insertion_latency, insertion_throughput;
1296 double deletion_latency, deletion_throughput;
1298 /* Save first insertion/deletion rates from first thread.
1299 * Start comparing with all threads, if any thread used
1300 * time more than current saved, replace it.
1302 * Thus in the end we will have the max time used for
1303 * insertion/deletion by one thread.
1305 * As for memory consumption, save the min of all threads
1306 * of last alloc, and save the max for all threads for
1310 insertion_latency_time = used_time->insertion[port][0];
1311 deletion_latency_time = used_time->deletion[port][0];
1312 insertion_throughput_time = used_time->insertion[port][0];
1313 deletion_throughput_time = used_time->deletion[port][0];
1315 i = mc_pool.cores_count;
1317 insertion_throughput_time += used_time->insertion[port][i];
1318 deletion_throughput_time += used_time->deletion[port][i];
1319 if (insertion_latency_time < used_time->insertion[port][i])
1320 insertion_latency_time = used_time->insertion[port][i];
1321 if (deletion_latency_time < used_time->deletion[port][i])
1322 deletion_latency_time = used_time->deletion[port][i];
1325 insertion_latency = ((double) (mc_pool.rules_count
1326 / insertion_latency_time) / 1000);
1327 deletion_latency = ((double) (mc_pool.rules_count
1328 / deletion_latency_time) / 1000);
1330 insertion_throughput_time /= mc_pool.cores_count;
1331 deletion_throughput_time /= mc_pool.cores_count;
1332 insertion_throughput = ((double) (mc_pool.rules_count
1333 / insertion_throughput_time) / 1000);
1334 deletion_throughput = ((double) (mc_pool.rules_count
1335 / deletion_throughput_time) / 1000);
1338 printf("\n%s\n:: [Latency | Insertion] All Cores :: Port %d :: ",
1340 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1342 printf(":: [Latency | Insertion] All Cores :: Port %d :: ", port);
1343 printf("The time for creating %d rules is %f seconds\n",
1344 mc_pool.rules_count, insertion_latency_time);
1346 /* Throughput stats */
1347 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1348 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1349 insertion_throughput);
1350 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1351 printf("The average time for creating %d rules is %f seconds\n",
1352 mc_pool.rules_count, insertion_throughput_time);
1356 printf(":: [Latency | Deletion] All Cores :: Port %d :: Total "
1357 "deletion rate -> %f K Rules/Sec\n",
1358 port, deletion_latency);
1359 printf(":: [Latency | Deletion] All Cores :: Port %d :: ",
1361 printf("The time for deleting %d rules is %f seconds\n",
1362 mc_pool.rules_count, deletion_latency_time);
1364 /* Throughput stats */
1365 printf(":: [Throughput | Deletion] All Cores :: Port %d :: Total "
1366 "deletion rate -> %f K Rules/Sec\n",
1367 port, deletion_throughput);
1368 printf(":: [Throughput | Deletion] All Cores :: Port %d :: ",
1370 printf("The average time for deleting %d rules is %f seconds\n",
1371 mc_pool.rules_count, deletion_throughput_time);
1376 dump_used_mem(uint16_t port)
1379 int64_t last_alloc, current_alloc;
1380 int flow_size_in_bytes;
1382 last_alloc = mc_pool.last_alloc[0];
1383 current_alloc = mc_pool.current_alloc[0];
1385 i = mc_pool.cores_count;
1387 if (last_alloc > mc_pool.last_alloc[i])
1388 last_alloc = mc_pool.last_alloc[i];
1389 if (current_alloc < mc_pool.current_alloc[i])
1390 current_alloc = mc_pool.current_alloc[i];
1393 flow_size_in_bytes = (current_alloc - last_alloc) / mc_pool.rules_count;
1394 printf("\n:: Port %d :: rte_flow size in DPDK layer: %d Bytes\n",
1395 port, flow_size_in_bytes);
1399 run_rte_flow_handler_cores(void *data __rte_unused)
1402 int lcore_counter = 0;
1403 int lcore_id = rte_lcore_id();
1406 RTE_LCORE_FOREACH(i) {
1407 /* If core not needed return. */
1408 if (lcore_id == i) {
1409 printf(":: lcore %d mapped with index %d\n", lcore_id, lcore_counter);
1410 if (lcore_counter >= (int) mc_pool.cores_count)
1416 lcore_id = lcore_counter;
1418 if (lcore_id >= (int) mc_pool.cores_count)
1421 mc_pool.rules_count = rules_count;
1423 flows_handler(lcore_id);
1425 /* Only main core to print total results. */
1429 /* Make sure all cores finished insertion/deletion process. */
1430 rte_eal_mp_wait_lcore();
1432 RTE_ETH_FOREACH_DEV(port) {
1434 dump_used_cpu_time("Meters:",
1435 port, &mc_pool.create_meter);
1436 dump_used_cpu_time("Flows:",
1437 port, &mc_pool.create_flow);
1438 dump_used_mem(port);
1445 signal_handler(int signum)
1447 if (signum == SIGINT || signum == SIGTERM) {
1448 printf("\n\nSignal %d received, preparing to exit...\n",
1450 printf("Error: Stats are wrong due to sudden signal!\n\n");
1455 static inline uint16_t
1456 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
1459 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
1465 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
1471 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
1472 li->tx_pkts += nr_tx;
1473 li->tx_drops += cnt - nr_tx;
1475 for (i = nr_tx; i < cnt; i++)
1476 rte_pktmbuf_free(li->pkts[i]);
1480 * Method to convert numbers into pretty numbers that easy
1481 * to read. The design here is to add comma after each three
1482 * digits and set all of this inside buffer.
1484 * For example if n = 1799321, the output will be
1485 * 1,799,321 after this method which is easier to read.
1488 pretty_number(uint64_t n, char *buf)
1495 sprintf(p[i], "%03d", (int)(n % 1000));
1500 sprintf(p[i++], "%d", (int)n);
1503 off += sprintf(buf + off, "%s,", p[i]);
1504 buf[strlen(buf) - 1] = '\0';
1510 packet_per_second_stats(void)
1512 struct lcore_info *old;
1513 struct lcore_info *li, *oli;
1517 old = rte_zmalloc("old",
1518 sizeof(struct lcore_info) * RTE_MAX_LCORE, 0);
1520 rte_exit(EXIT_FAILURE, "No Memory available!");
1522 memcpy(old, lcore_infos,
1523 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1525 while (!force_quit) {
1526 uint64_t total_tx_pkts = 0;
1527 uint64_t total_rx_pkts = 0;
1528 uint64_t total_tx_drops = 0;
1529 uint64_t tx_delta, rx_delta, drops_delta;
1531 int nr_valid_core = 0;
1536 char go_up_nr_lines[16];
1538 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
1539 printf("%s\r", go_up_nr_lines);
1542 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
1543 printf("%6s %16s %16s %16s\n", "------", "----------------",
1544 "----------------", "----------------");
1546 for (i = 0; i < RTE_MAX_LCORE; i++) {
1547 li = &lcore_infos[i];
1549 if (li->mode != LCORE_MODE_PKT)
1552 tx_delta = li->tx_pkts - oli->tx_pkts;
1553 rx_delta = li->rx_pkts - oli->rx_pkts;
1554 drops_delta = li->tx_drops - oli->tx_drops;
1555 printf("%6d %16s %16s %16s\n", i,
1556 pretty_number(tx_delta, buf[0]),
1557 pretty_number(drops_delta, buf[1]),
1558 pretty_number(rx_delta, buf[2]));
1560 total_tx_pkts += tx_delta;
1561 total_rx_pkts += rx_delta;
1562 total_tx_drops += drops_delta;
1568 if (nr_valid_core > 1) {
1569 printf("%6s %16s %16s %16s\n", "total",
1570 pretty_number(total_tx_pkts, buf[0]),
1571 pretty_number(total_tx_drops, buf[1]),
1572 pretty_number(total_rx_pkts, buf[2]));
1576 memcpy(old, lcore_infos,
1577 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1582 start_forwarding(void *data __rte_unused)
1584 int lcore = rte_lcore_id();
1587 struct lcore_info *li = &lcore_infos[lcore];
1592 if (li->mode == LCORE_MODE_STATS) {
1593 printf(":: started stats on lcore %u\n", lcore);
1594 packet_per_second_stats();
1599 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1600 if (li->streams[stream_id].rx_port == -1)
1604 li->streams[stream_id].rx_port,
1605 li->streams[stream_id].rx_queue);
1608 li->streams[stream_id].tx_port,
1609 li->streams[stream_id].tx_queue);
1615 init_lcore_info(void)
1623 int streams_per_core;
1624 int unassigned_streams;
1626 nr_port = rte_eth_dev_count_avail();
1628 /* First logical core is reserved for stats printing */
1629 lcore = rte_get_next_lcore(-1, 0, 0);
1630 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1633 * Initialize all cores
1634 * All cores at first must have -1 value in all streams
1635 * This means that this stream is not used, or not set
1638 for (i = 0; i < RTE_MAX_LCORE; i++)
1639 for (j = 0; j < MAX_STREAMS; j++) {
1640 lcore_infos[i].streams[j].tx_port = -1;
1641 lcore_infos[i].streams[j].rx_port = -1;
1642 lcore_infos[i].streams[j].tx_queue = -1;
1643 lcore_infos[i].streams[j].rx_queue = -1;
1644 lcore_infos[i].streams_nb = 0;
1648 * Calculate the total streams count.
1649 * Also distribute those streams count between the available
1650 * logical cores except first core, since it's reserved for
1653 nb_fwd_streams = nr_port * RXQ_NUM;
1654 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1655 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1656 lcore = rte_get_next_lcore(lcore, 0, 0);
1657 lcore_infos[lcore].streams_nb = 1;
1660 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1661 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1662 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1663 lcore = rte_get_next_lcore(lcore, 0, 0);
1664 lcore_infos[lcore].streams_nb = streams_per_core;
1665 if (unassigned_streams) {
1666 lcore_infos[lcore].streams_nb++;
1667 unassigned_streams--;
1673 * Set the streams for the cores according to each logical
1674 * core stream count.
1675 * The streams is built on the design of what received should
1676 * forward as well, this means that if you received packets on
1677 * port 0 queue 0 then the same queue should forward the
1678 * packets, using the same logical core.
1680 lcore = rte_get_next_lcore(-1, 0, 0);
1681 for (port = 0; port < nr_port; port++) {
1682 /* Create FWD stream */
1683 for (queue = 0; queue < RXQ_NUM; queue++) {
1684 if (!lcore_infos[lcore].streams_nb ||
1685 !(stream_id % lcore_infos[lcore].streams_nb)) {
1686 lcore = rte_get_next_lcore(lcore, 0, 0);
1687 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1690 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1691 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1692 lcore_infos[lcore].streams[stream_id].rx_port = port;
1693 lcore_infos[lcore].streams[stream_id].tx_port = port;
1698 /* Print all streams */
1699 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1700 for (i = 0; i < RTE_MAX_LCORE; i++)
1701 for (j = 0; j < MAX_STREAMS; j++) {
1702 /* No streams for this core */
1703 if (lcore_infos[i].streams[j].tx_port == -1)
1705 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1707 lcore_infos[i].streams[j].rx_port,
1708 lcore_infos[i].streams[j].rx_queue,
1709 lcore_infos[i].streams[j].tx_port,
1710 lcore_infos[i].streams[j].tx_queue);
1719 uint16_t hairpin_queue;
1723 struct rte_eth_hairpin_conf hairpin_conf = {
1726 struct rte_eth_conf port_conf = {
1732 struct rte_eth_txconf txq_conf;
1733 struct rte_eth_rxconf rxq_conf;
1734 struct rte_eth_dev_info dev_info;
1736 nr_queues = RXQ_NUM;
1737 if (hairpin_queues_num != 0)
1738 nr_queues = RXQ_NUM + hairpin_queues_num;
1740 nr_ports = rte_eth_dev_count_avail();
1742 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1744 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1745 TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1748 if (mbuf_mp == NULL)
1749 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1751 for (port_id = 0; port_id < nr_ports; port_id++) {
1752 ret = rte_eth_dev_info_get(port_id, &dev_info);
1754 rte_exit(EXIT_FAILURE,
1755 "Error during getting device"
1756 " (port %u) info: %s\n",
1757 port_id, strerror(-ret));
1759 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1760 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1762 printf(":: initializing port: %d\n", port_id);
1764 ret = rte_eth_dev_configure(port_id, nr_queues,
1765 nr_queues, &port_conf);
1767 rte_exit(EXIT_FAILURE,
1768 ":: cannot configure device: err=%d, port=%u\n",
1771 rxq_conf = dev_info.default_rxconf;
1772 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1773 ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1774 rte_eth_dev_socket_id(port_id),
1778 rte_exit(EXIT_FAILURE,
1779 ":: Rx queue setup failed: err=%d, port=%u\n",
1783 txq_conf = dev_info.default_txconf;
1784 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1785 ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1786 rte_eth_dev_socket_id(port_id),
1789 rte_exit(EXIT_FAILURE,
1790 ":: Tx queue setup failed: err=%d, port=%u\n",
1794 /* Catch all packets from traffic generator. */
1795 ret = rte_eth_promiscuous_enable(port_id);
1797 rte_exit(EXIT_FAILURE,
1798 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1799 rte_strerror(-ret), port_id);
1801 if (hairpin_queues_num != 0) {
1803 * Configure peer which represents hairpin Tx.
1804 * Hairpin queue numbers start after standard queues
1805 * (RXQ_NUM and TXQ_NUM).
1807 for (hairpin_queue = RXQ_NUM, std_queue = 0;
1808 hairpin_queue < nr_queues;
1809 hairpin_queue++, std_queue++) {
1810 hairpin_conf.peers[0].port = port_id;
1811 hairpin_conf.peers[0].queue =
1812 std_queue + TXQ_NUM;
1813 ret = rte_eth_rx_hairpin_queue_setup(
1814 port_id, hairpin_queue,
1815 NR_RXD, &hairpin_conf);
1817 rte_exit(EXIT_FAILURE,
1818 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1822 for (hairpin_queue = TXQ_NUM, std_queue = 0;
1823 hairpin_queue < nr_queues;
1824 hairpin_queue++, std_queue++) {
1825 hairpin_conf.peers[0].port = port_id;
1826 hairpin_conf.peers[0].queue =
1827 std_queue + RXQ_NUM;
1828 ret = rte_eth_tx_hairpin_queue_setup(
1829 port_id, hairpin_queue,
1830 NR_TXD, &hairpin_conf);
1832 rte_exit(EXIT_FAILURE,
1833 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1838 ret = rte_eth_dev_start(port_id);
1840 rte_exit(EXIT_FAILURE,
1841 "rte_eth_dev_start:err=%d, port=%u\n",
1844 printf(":: initializing port: %d done\n", port_id);
1849 main(int argc, char **argv)
1853 struct rte_flow_error error;
1855 ret = rte_eal_init(argc, argv);
1857 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1860 dump_iterations = false;
1861 rules_count = DEFAULT_RULES_COUNT;
1862 rules_batch = DEFAULT_RULES_BATCH;
1863 delete_flag = false;
1864 dump_socket_mem_flag = false;
1865 flow_group = DEFAULT_GROUP;
1867 signal(SIGINT, signal_handler);
1868 signal(SIGTERM, signal_handler);
1873 args_parse(argc, argv);
1877 nb_lcores = rte_lcore_count();
1879 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1882 printf(":: Flows Count per port: %d\n\n", rules_count);
1885 create_meter_profile();
1886 rte_eal_mp_remote_launch(run_rte_flow_handler_cores, NULL, CALL_MAIN);
1890 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MAIN);
1892 if (has_meter() && delete_flag)
1893 destroy_meter_profile();
1895 RTE_ETH_FOREACH_DEV(port) {
1896 rte_flow_flush(port, &error);
1897 if (rte_eth_dev_stop(port) != 0)
1898 printf("Failed to stop device on port %u\n", port);
1899 rte_eth_dev_close(port);
1901 printf("\nBye ...\n");