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>
40 #include "actions_gen.h"
43 #define MAX_BATCHES_COUNT 100
44 #define DEFAULT_RULES_COUNT 4000000
45 #define DEFAULT_RULES_BATCH 100000
46 #define DEFAULT_GROUP 0
48 struct rte_flow *flow;
49 static uint8_t flow_group;
51 static uint64_t encap_data;
52 static uint64_t decap_data;
53 static uint64_t all_actions[RTE_COLORS][MAX_ACTIONS_NUM];
54 static char *actions_str[RTE_COLORS];
56 static uint64_t flow_items[MAX_ITEMS_NUM];
57 static uint64_t flow_actions[MAX_ACTIONS_NUM];
58 static uint64_t flow_attrs[MAX_ATTRS_NUM];
59 static uint32_t policy_id[MAX_PORTS];
60 static uint8_t items_idx, actions_idx, attrs_idx;
62 static uint64_t ports_mask;
63 static uint16_t dst_ports[RTE_MAX_ETHPORTS];
64 static volatile bool force_quit;
65 static bool dump_iterations;
66 static bool delete_flag;
67 static bool dump_socket_mem_flag;
68 static bool enable_fwd;
69 static bool unique_data;
70 static bool policy_mtr;
71 static bool packet_mode;
73 static uint8_t rx_queues_count;
74 static uint8_t tx_queues_count;
75 static uint8_t rxd_count;
76 static uint8_t txd_count;
77 static uint32_t mbuf_size;
78 static uint32_t mbuf_cache_size;
79 static uint32_t total_mbuf_num;
81 static struct rte_mempool *mbuf_mp;
82 static uint32_t nb_lcores;
83 static uint32_t rules_count;
84 static uint32_t rules_batch;
85 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
86 static uint32_t nb_lcores;
87 static uint8_t max_priority;
88 static uint32_t rand_seed;
89 static uint64_t meter_profile_values[3]; /* CIR CBS EBS values. */
91 #define MAX_PKT_BURST 32
92 #define LCORE_MODE_PKT 1
93 #define LCORE_MODE_STATS 2
94 #define MAX_STREAMS 64
95 #define METER_CREATE 1
96 #define METER_DELETE 2
108 struct stream streams[MAX_STREAMS];
113 struct rte_mbuf *pkts[MAX_PKT_BURST];
114 } __rte_cache_aligned;
116 static struct lcore_info lcore_infos[RTE_MAX_LCORE];
118 struct used_cpu_time {
119 double insertion[MAX_PORTS][RTE_MAX_LCORE];
120 double deletion[MAX_PORTS][RTE_MAX_LCORE];
123 struct multi_cores_pool {
124 uint32_t cores_count;
125 uint32_t rules_count;
126 struct used_cpu_time meters_record;
127 struct used_cpu_time flows_record;
128 int64_t last_alloc[RTE_MAX_LCORE];
129 int64_t current_alloc[RTE_MAX_LCORE];
130 } __rte_cache_aligned;
132 static struct multi_cores_pool mc_pool = {
136 static const struct option_dict {
145 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
146 .map = &flow_items[0],
147 .map_idx = &items_idx
151 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
152 .map = &flow_items[0],
153 .map_idx = &items_idx
157 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
158 .map = &flow_items[0],
159 .map_idx = &items_idx
163 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
164 .map = &flow_items[0],
165 .map_idx = &items_idx
169 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
170 .map = &flow_items[0],
171 .map_idx = &items_idx
175 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
176 .map = &flow_items[0],
177 .map_idx = &items_idx
181 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
182 .map = &flow_items[0],
183 .map_idx = &items_idx
187 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
188 .map = &flow_items[0],
189 .map_idx = &items_idx
193 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
194 .map = &flow_items[0],
195 .map_idx = &items_idx
199 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
200 .map = &flow_items[0],
201 .map_idx = &items_idx
205 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
206 .map = &flow_items[0],
207 .map_idx = &items_idx
211 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
212 .map = &flow_items[0],
213 .map_idx = &items_idx
217 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
218 .map = &flow_items[0],
219 .map_idx = &items_idx
223 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP),
224 .map = &flow_items[0],
225 .map_idx = &items_idx
229 .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ICMP6),
230 .map = &flow_items[0],
231 .map_idx = &items_idx
236 .map = &flow_attrs[0],
237 .map_idx = &attrs_idx
242 .map = &flow_attrs[0],
243 .map_idx = &attrs_idx
248 .map = &flow_attrs[0],
249 .map_idx = &attrs_idx
253 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
254 .map = &flow_actions[0],
255 .map_idx = &actions_idx
259 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
260 .map = &flow_actions[0],
261 .map_idx = &actions_idx
265 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
266 .map = &flow_actions[0],
267 .map_idx = &actions_idx
271 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
272 .map = &flow_actions[0],
273 .map_idx = &actions_idx
277 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
278 .map = &flow_actions[0],
279 .map_idx = &actions_idx
283 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
284 .map = &flow_actions[0],
285 .map_idx = &actions_idx
289 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
290 .map = &flow_actions[0],
291 .map_idx = &actions_idx
295 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
296 .map = &flow_actions[0],
297 .map_idx = &actions_idx
301 .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
302 .map = &flow_actions[0],
303 .map_idx = &actions_idx
306 .str = "set-src-mac",
307 .mask = FLOW_ACTION_MASK(
308 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
310 .map = &flow_actions[0],
311 .map_idx = &actions_idx
314 .str = "set-dst-mac",
315 .mask = FLOW_ACTION_MASK(
316 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
318 .map = &flow_actions[0],
319 .map_idx = &actions_idx
322 .str = "set-src-ipv4",
323 .mask = FLOW_ACTION_MASK(
324 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
326 .map = &flow_actions[0],
327 .map_idx = &actions_idx
330 .str = "set-dst-ipv4",
331 .mask = FLOW_ACTION_MASK(
332 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
334 .map = &flow_actions[0],
335 .map_idx = &actions_idx
338 .str = "set-src-ipv6",
339 .mask = FLOW_ACTION_MASK(
340 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
342 .map = &flow_actions[0],
343 .map_idx = &actions_idx
346 .str = "set-dst-ipv6",
347 .mask = FLOW_ACTION_MASK(
348 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
350 .map = &flow_actions[0],
351 .map_idx = &actions_idx
355 .mask = FLOW_ACTION_MASK(
356 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
358 .map = &flow_actions[0],
359 .map_idx = &actions_idx
363 .mask = FLOW_ACTION_MASK(
364 RTE_FLOW_ACTION_TYPE_SET_TP_DST
366 .map = &flow_actions[0],
367 .map_idx = &actions_idx
370 .str = "inc-tcp-ack",
371 .mask = FLOW_ACTION_MASK(
372 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
374 .map = &flow_actions[0],
375 .map_idx = &actions_idx
378 .str = "dec-tcp-ack",
379 .mask = FLOW_ACTION_MASK(
380 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
382 .map = &flow_actions[0],
383 .map_idx = &actions_idx
386 .str = "inc-tcp-seq",
387 .mask = FLOW_ACTION_MASK(
388 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
390 .map = &flow_actions[0],
391 .map_idx = &actions_idx
394 .str = "dec-tcp-seq",
395 .mask = FLOW_ACTION_MASK(
396 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
398 .map = &flow_actions[0],
399 .map_idx = &actions_idx
403 .mask = FLOW_ACTION_MASK(
404 RTE_FLOW_ACTION_TYPE_SET_TTL
406 .map = &flow_actions[0],
407 .map_idx = &actions_idx
411 .mask = FLOW_ACTION_MASK(
412 RTE_FLOW_ACTION_TYPE_DEC_TTL
414 .map = &flow_actions[0],
415 .map_idx = &actions_idx
418 .str = "set-ipv4-dscp",
419 .mask = FLOW_ACTION_MASK(
420 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
422 .map = &flow_actions[0],
423 .map_idx = &actions_idx
426 .str = "set-ipv6-dscp",
427 .mask = FLOW_ACTION_MASK(
428 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
430 .map = &flow_actions[0],
431 .map_idx = &actions_idx
435 .mask = FLOW_ACTION_MASK(
436 RTE_FLOW_ACTION_TYPE_FLAG
438 .map = &flow_actions[0],
439 .map_idx = &actions_idx
443 .mask = FLOW_ACTION_MASK(
444 RTE_FLOW_ACTION_TYPE_METER
446 .map = &flow_actions[0],
447 .map_idx = &actions_idx
450 .str = "vxlan-encap",
451 .mask = FLOW_ACTION_MASK(
452 RTE_FLOW_ACTION_TYPE_VXLAN_ENCAP
454 .map = &flow_actions[0],
455 .map_idx = &actions_idx
458 .str = "vxlan-decap",
459 .mask = FLOW_ACTION_MASK(
460 RTE_FLOW_ACTION_TYPE_VXLAN_DECAP
462 .map = &flow_actions[0],
463 .map_idx = &actions_idx
468 usage(char *progname)
470 printf("\nusage: %s\n", progname);
471 printf("\nControl configurations:\n");
472 printf(" --rules-count=N: to set the number of needed"
473 " rules to insert, default is %d\n", DEFAULT_RULES_COUNT);
474 printf(" --rules-batch=N: set number of batched rules,"
475 " default is %d\n", DEFAULT_RULES_BATCH);
476 printf(" --dump-iterations: To print rates for each"
478 printf(" --deletion-rate: Enable deletion rate"
480 printf(" --dump-socket-mem: To dump all socket memory\n");
481 printf(" --enable-fwd: To enable packets forwarding"
482 " after insertion\n");
483 printf(" --portmask=N: hexadecimal bitmask of ports used\n");
484 printf(" --random-priority=N,S: use random priority levels "
485 "from 0 to (N - 1) for flows "
486 "and S as seed for pseudo-random number generator\n");
487 printf(" --unique-data: flag to set using unique data for all"
488 " actions that support data, such as header modify and encap actions\n");
489 printf(" --meter-profile=cir,cbs,ebs: set CIR CBS EBS parameters in meter"
490 " profile, default values are %d,%d,%d\n", METER_CIR,
492 printf(" --packet-mode: to enable packet mode for meter profile\n");
494 printf("To set flow attributes:\n");
495 printf(" --ingress: set ingress attribute in flows\n");
496 printf(" --egress: set egress attribute in flows\n");
497 printf(" --transfer: set transfer attribute in flows\n");
498 printf(" --group=N: set group for all flows,"
499 " default is %d\n", DEFAULT_GROUP);
500 printf(" --cores=N: to set the number of needed "
501 "cores to insert rte_flow rules, default is 1\n");
502 printf(" --rxq=N: to set the count of receive queues\n");
503 printf(" --txq=N: to set the count of send queues\n");
504 printf(" --rxd=N: to set the count of rxd\n");
505 printf(" --txd=N: to set the count of txd\n");
506 printf(" --mbuf-size=N: to set the size of mbuf\n");
507 printf(" --mbuf-cache-size=N: to set the size of mbuf cache\n");
508 printf(" --total-mbuf-count=N: to set the count of total mbuf count\n");
511 printf("To set flow items:\n");
512 printf(" --ether: add ether layer in flow items\n");
513 printf(" --vlan: add vlan layer in flow items\n");
514 printf(" --ipv4: add ipv4 layer in flow items\n");
515 printf(" --ipv6: add ipv6 layer in flow items\n");
516 printf(" --tcp: add tcp layer in flow items\n");
517 printf(" --udp: add udp layer in flow items\n");
518 printf(" --vxlan: add vxlan layer in flow items\n");
519 printf(" --vxlan-gpe: add vxlan-gpe layer in flow items\n");
520 printf(" --gre: add gre layer in flow items\n");
521 printf(" --geneve: add geneve layer in flow items\n");
522 printf(" --gtp: add gtp layer in flow items\n");
523 printf(" --meta: add meta layer in flow items\n");
524 printf(" --tag: add tag layer in flow items\n");
525 printf(" --icmpv4: add icmpv4 layer in flow items\n");
526 printf(" --icmpv6: add icmpv6 layer in flow items\n");
528 printf("To set flow actions:\n");
529 printf(" --port-id: add port-id action in flow actions\n");
530 printf(" --rss: add rss action in flow actions\n");
531 printf(" --queue: add queue action in flow actions\n");
532 printf(" --jump: add jump action in flow actions\n");
533 printf(" --mark: add mark action in flow actions\n");
534 printf(" --count: add count action in flow actions\n");
535 printf(" --set-meta: add set meta action in flow actions\n");
536 printf(" --set-tag: add set tag action in flow actions\n");
537 printf(" --drop: add drop action in flow actions\n");
538 printf(" --hairpin-queue=N: add hairpin-queue action in flow actions\n");
539 printf(" --hairpin-rss=N: add hairpin-rss action in flow actions\n");
540 printf(" --set-src-mac: add set src mac action to flow actions\n"
541 "Src mac to be set is random each flow\n");
542 printf(" --set-dst-mac: add set dst mac action to flow actions\n"
543 "Dst mac to be set is random each flow\n");
544 printf(" --set-src-ipv4: add set src ipv4 action to flow actions\n"
545 "Src ipv4 to be set is random each flow\n");
546 printf(" --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
547 "Dst ipv4 to be set is random each flow\n");
548 printf(" --set-src-ipv6: add set src ipv6 action to flow actions\n"
549 "Src ipv6 to be set is random each flow\n");
550 printf(" --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
551 "Dst ipv6 to be set is random each flow\n");
552 printf(" --set-src-tp: add set src tp action to flow actions\n"
553 "Src tp to be set is random each flow\n");
554 printf(" --set-dst-tp: add set dst tp action to flow actions\n"
555 "Dst tp to be set is random each flow\n");
556 printf(" --inc-tcp-ack: add inc tcp ack action to flow actions\n"
557 "tcp ack will be increments by 1\n");
558 printf(" --dec-tcp-ack: add dec tcp ack action to flow actions\n"
559 "tcp ack will be decrements by 1\n");
560 printf(" --inc-tcp-seq: add inc tcp seq action to flow actions\n"
561 "tcp seq will be increments by 1\n");
562 printf(" --dec-tcp-seq: add dec tcp seq action to flow actions\n"
563 "tcp seq will be decrements by 1\n");
564 printf(" --set-ttl: add set ttl action to flow actions\n"
565 "L3 ttl to be set is random each flow\n");
566 printf(" --dec-ttl: add dec ttl action to flow actions\n"
567 "L3 ttl will be decrements by 1\n");
568 printf(" --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
569 "ipv4 dscp value to be set is random each flow\n");
570 printf(" --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
571 "ipv6 dscp value to be set is random each flow\n");
572 printf(" --flag: add flag action to flow actions\n");
573 printf(" --meter: add meter action to flow actions\n");
574 printf(" --policy-mtr=\"g1,g2:y1:r1\": to create meter with specified "
575 "colored actions\n");
576 printf(" --raw-encap=<data>: add raw encap action to flow actions\n"
577 "Data is the data needed to be encaped\n"
578 "Example: raw-encap=ether,ipv4,udp,vxlan\n");
579 printf(" --raw-decap=<data>: add raw decap action to flow actions\n"
580 "Data is the data needed to be decaped\n"
581 "Example: raw-decap=ether,ipv4,udp,vxlan\n");
582 printf(" --vxlan-encap: add vxlan-encap action to flow actions\n"
583 "Encapped data is fixed with pattern: ether,ipv4,udp,vxlan\n"
584 "With fixed values\n");
585 printf(" --vxlan-decap: add vxlan_decap action to flow actions\n");
589 read_meter_policy(char *prog, char *arg)
597 token = strsep(&arg, ":\0");
598 while (token != NULL && j < RTE_COLORS) {
599 actions_str[j++] = token;
600 token = strsep(&arg, ":\0");
603 token = strtok(actions_str[0], ",\0");
604 while (token == NULL && j < RTE_COLORS - 1)
605 token = strtok(actions_str[++j], ",\0");
606 while (j < RTE_COLORS && token != NULL) {
607 for (i = 0; i < RTE_DIM(flow_options); i++) {
608 if (!strcmp(token, flow_options[i].str)) {
609 all_actions[j][k++] = flow_options[i].mask;
613 /* Reached last action with no match */
614 if (i >= RTE_DIM(flow_options)) {
615 fprintf(stderr, "Invalid colored actions: %s\n", token);
617 rte_exit(EXIT_SUCCESS, "Invalid colored actions\n");
619 token = strtok(NULL, ",\0");
620 while (!token && j < RTE_COLORS - 1) {
621 token = strtok(actions_str[++j], ",\0");
628 args_parse(int argc, char **argv)
639 static const struct option lgopts[] = {
642 { "rules-count", 1, 0, 0 },
643 { "rules-batch", 1, 0, 0 },
644 { "dump-iterations", 0, 0, 0 },
645 { "deletion-rate", 0, 0, 0 },
646 { "dump-socket-mem", 0, 0, 0 },
647 { "enable-fwd", 0, 0, 0 },
648 { "unique-data", 0, 0, 0 },
649 { "portmask", 1, 0, 0 },
650 { "cores", 1, 0, 0 },
651 { "random-priority", 1, 0, 0 },
652 { "meter-profile-alg", 1, 0, 0 },
657 { "mbuf-size", 1, 0, 0 },
658 { "mbuf-cache-size", 1, 0, 0 },
659 { "total-mbuf-count", 1, 0, 0 },
661 { "ingress", 0, 0, 0 },
662 { "egress", 0, 0, 0 },
663 { "transfer", 0, 0, 0 },
664 { "group", 1, 0, 0 },
666 { "ether", 0, 0, 0 },
672 { "vxlan", 0, 0, 0 },
673 { "vxlan-gpe", 0, 0, 0 },
675 { "geneve", 0, 0, 0 },
679 { "icmpv4", 0, 0, 0 },
680 { "icmpv6", 0, 0, 0 },
682 { "port-id", 2, 0, 0 },
684 { "queue", 0, 0, 0 },
687 { "count", 0, 0, 0 },
688 { "set-meta", 0, 0, 0 },
689 { "set-tag", 0, 0, 0 },
691 { "hairpin-queue", 1, 0, 0 },
692 { "hairpin-rss", 1, 0, 0 },
693 { "set-src-mac", 0, 0, 0 },
694 { "set-dst-mac", 0, 0, 0 },
695 { "set-src-ipv4", 0, 0, 0 },
696 { "set-dst-ipv4", 0, 0, 0 },
697 { "set-src-ipv6", 0, 0, 0 },
698 { "set-dst-ipv6", 0, 0, 0 },
699 { "set-src-tp", 0, 0, 0 },
700 { "set-dst-tp", 0, 0, 0 },
701 { "inc-tcp-ack", 0, 0, 0 },
702 { "dec-tcp-ack", 0, 0, 0 },
703 { "inc-tcp-seq", 0, 0, 0 },
704 { "dec-tcp-seq", 0, 0, 0 },
705 { "set-ttl", 0, 0, 0 },
706 { "dec-ttl", 0, 0, 0 },
707 { "set-ipv4-dscp", 0, 0, 0 },
708 { "set-ipv6-dscp", 0, 0, 0 },
710 { "meter", 0, 0, 0 },
711 { "raw-encap", 1, 0, 0 },
712 { "raw-decap", 1, 0, 0 },
713 { "vxlan-encap", 0, 0, 0 },
714 { "vxlan-decap", 0, 0, 0 },
715 { "policy-mtr", 1, 0, 0 },
716 { "meter-profile", 1, 0, 0 },
717 { "packet-mode", 0, 0, 0 },
720 RTE_ETH_FOREACH_DEV(i)
721 ports_mask |= 1 << i;
723 for (i = 0; i < RTE_MAX_ETHPORTS; i++)
724 dst_ports[i] = PORT_ID_DST;
726 hairpin_queues_num = 0;
729 printf(":: Flow -> ");
730 while ((opt = getopt_long(argc, argvopt, "",
731 lgopts, &opt_idx)) != EOF) {
734 if (strcmp(lgopts[opt_idx].name, "help") == 0) {
739 if (strcmp(lgopts[opt_idx].name, "group") == 0) {
744 rte_exit(EXIT_FAILURE,
745 "flow group should be >= 0\n");
746 printf("group %d / ", flow_group);
749 for (i = 0; i < RTE_DIM(flow_options); i++)
750 if (strcmp(lgopts[opt_idx].name,
751 flow_options[i].str) == 0) {
753 (*flow_options[i].map_idx)++] =
754 flow_options[i].mask;
755 printf("%s / ", flow_options[i].str);
758 if (strcmp(lgopts[opt_idx].name,
759 "hairpin-rss") == 0) {
762 hairpin_queues_num = n;
764 rte_exit(EXIT_FAILURE,
765 "Hairpin queues should be > 0\n");
767 flow_actions[actions_idx++] =
769 printf("hairpin-rss / ");
771 if (strcmp(lgopts[opt_idx].name,
772 "hairpin-queue") == 0) {
775 hairpin_queues_num = n;
777 rte_exit(EXIT_FAILURE,
778 "Hairpin queues should be > 0\n");
780 flow_actions[actions_idx++] =
781 HAIRPIN_QUEUE_ACTION;
782 printf("hairpin-queue / ");
785 if (strcmp(lgopts[opt_idx].name, "raw-encap") == 0) {
786 printf("raw-encap ");
787 flow_actions[actions_idx++] =
789 RTE_FLOW_ACTION_TYPE_RAW_ENCAP
792 token = strtok(optarg, ",");
793 while (token != NULL) {
794 for (i = 0; i < RTE_DIM(flow_options); i++) {
795 if (strcmp(flow_options[i].str, token) == 0) {
796 printf("%s,", token);
797 encap_data |= flow_options[i].mask;
800 /* Reached last item with no match */
801 if (i == (RTE_DIM(flow_options) - 1))
802 rte_exit(EXIT_FAILURE,
803 "Invalid encap item: %s\n", token);
805 token = strtok(NULL, ",");
809 if (strcmp(lgopts[opt_idx].name, "raw-decap") == 0) {
810 printf("raw-decap ");
811 flow_actions[actions_idx++] =
813 RTE_FLOW_ACTION_TYPE_RAW_DECAP
816 token = strtok(optarg, ",");
817 while (token != NULL) {
818 for (i = 0; i < RTE_DIM(flow_options); i++) {
819 if (strcmp(flow_options[i].str, token) == 0) {
820 printf("%s,", token);
821 decap_data |= flow_options[i].mask;
824 /* Reached last item with no match */
825 if (i == (RTE_DIM(flow_options) - 1))
826 rte_exit(EXIT_FAILURE,
827 "Invalid decap item %s\n", token);
829 token = strtok(NULL, ",");
834 if (strcmp(lgopts[opt_idx].name,
835 "rules-batch") == 0) {
836 rules_batch = atoi(optarg);
838 if (strcmp(lgopts[opt_idx].name,
839 "rules-count") == 0) {
840 rules_count = atoi(optarg);
842 if (strcmp(lgopts[opt_idx].name, "random-priority") ==
845 prio = strtol(optarg, &end, 10);
846 if ((optarg[0] == '\0') || (end == NULL))
847 rte_exit(EXIT_FAILURE,
848 "Invalid value for random-priority\n");
851 seed = strtoll(token, &end, 10);
852 if ((token[0] == '\0') || (*end != '\0'))
853 rte_exit(EXIT_FAILURE,
854 "Invalid value for random-priority\n");
857 if (strcmp(lgopts[opt_idx].name,
858 "dump-iterations") == 0)
859 dump_iterations = true;
860 if (strcmp(lgopts[opt_idx].name,
863 if (strcmp(lgopts[opt_idx].name,
864 "deletion-rate") == 0)
866 if (strcmp(lgopts[opt_idx].name,
867 "dump-socket-mem") == 0)
868 dump_socket_mem_flag = true;
869 if (strcmp(lgopts[opt_idx].name,
872 if (strcmp(lgopts[opt_idx].name,
874 /* parse hexadecimal string */
876 pm = strtoull(optarg, &end, 16);
877 if ((optarg[0] == '\0') || (end == NULL) || (*end != '\0'))
878 rte_exit(EXIT_FAILURE, "Invalid fwd port mask\n");
881 if (strcmp(lgopts[opt_idx].name,
883 uint16_t port_idx = 0;
886 token = strtok(optarg, ",");
887 while (token != NULL) {
888 dst_ports[port_idx++] = atoi(token);
889 token = strtok(NULL, ",");
892 if (strcmp(lgopts[opt_idx].name, "rxq") == 0) {
894 rx_queues_count = (uint8_t) n;
896 if (strcmp(lgopts[opt_idx].name, "txq") == 0) {
898 tx_queues_count = (uint8_t) n;
900 if (strcmp(lgopts[opt_idx].name, "rxd") == 0) {
902 rxd_count = (uint8_t) n;
904 if (strcmp(lgopts[opt_idx].name, "txd") == 0) {
906 txd_count = (uint8_t) n;
908 if (strcmp(lgopts[opt_idx].name, "mbuf-size") == 0) {
910 mbuf_size = (uint32_t) n;
912 if (strcmp(lgopts[opt_idx].name, "mbuf-cache-size") == 0) {
914 mbuf_cache_size = (uint32_t) n;
916 if (strcmp(lgopts[opt_idx].name, "total-mbuf-count") == 0) {
918 total_mbuf_num = (uint32_t) n;
920 if (strcmp(lgopts[opt_idx].name, "cores") == 0) {
922 if ((int) rte_lcore_count() <= n) {
923 rte_exit(EXIT_FAILURE,
924 "Error: you need %d cores to run on multi-cores\n"
925 "Existing cores are: %d\n", n, rte_lcore_count());
927 if (n <= RTE_MAX_LCORE && n > 0)
928 mc_pool.cores_count = n;
930 rte_exit(EXIT_FAILURE,
931 "Error: cores count must be > 0 and < %d\n",
935 if (strcmp(lgopts[opt_idx].name, "policy-mtr") == 0)
936 read_meter_policy(argv[0], optarg);
937 if (strcmp(lgopts[opt_idx].name,
938 "meter-profile") == 0) {
940 token = strsep(&optarg, ",\0");
941 while (token != NULL && i < sizeof(
942 meter_profile_values) /
944 meter_profile_values[i++] = atol(token);
945 token = strsep(&optarg, ",\0");
948 if (strcmp(lgopts[opt_idx].name, "packet-mode") == 0)
953 rte_exit(EXIT_FAILURE, "Invalid option: %s\n",
958 if (rules_count % rules_batch != 0) {
959 rte_exit(EXIT_FAILURE,
960 "rules_count %% rules_batch should be 0\n");
962 if (rules_count / rules_batch > MAX_BATCHES_COUNT) {
963 rte_exit(EXIT_FAILURE,
964 "rules_count / rules_batch should be <= %d\n",
968 printf("end_flow\n");
971 /* Dump the socket memory statistics on console */
973 dump_socket_mem(FILE *f)
975 struct rte_malloc_socket_stats socket_stats;
980 unsigned int n_alloc = 0;
981 unsigned int n_free = 0;
982 bool active_nodes = false;
985 for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
986 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
987 !socket_stats.heap_totalsz_bytes)
990 total += socket_stats.heap_totalsz_bytes;
991 alloc += socket_stats.heap_allocsz_bytes;
992 free += socket_stats.heap_freesz_bytes;
993 n_alloc += socket_stats.alloc_count;
994 n_free += socket_stats.free_count;
995 if (dump_socket_mem_flag) {
996 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
998 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
999 " %.6lf(%.3lf%%)\nfree: %.6lf"
1001 "\ncount alloc: %u\nfree: %u\n",
1003 socket_stats.heap_totalsz_bytes / 1.0e6,
1004 socket_stats.heap_allocsz_bytes / 1.0e6,
1005 (double)socket_stats.heap_allocsz_bytes * 100 /
1006 (double)socket_stats.heap_totalsz_bytes,
1007 socket_stats.heap_freesz_bytes / 1.0e6,
1008 socket_stats.greatest_free_size / 1.0e6,
1009 socket_stats.alloc_count,
1010 socket_stats.free_count);
1011 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
1014 if (dump_socket_mem_flag && active_nodes) {
1016 "\nTotal: size(M)\ntotal: %.6lf"
1017 "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
1018 "\ncount alloc: %u\nfree: %u\n",
1019 total / 1.0e6, alloc / 1.0e6,
1020 (double)alloc * 100 / (double)total, free / 1.0e6,
1022 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
1028 print_flow_error(struct rte_flow_error error)
1030 printf("Flow can't be created %d message: %s\n",
1032 error.message ? error.message : "(no stated reason)");
1036 print_rules_batches(double *cpu_time_per_batch)
1042 for (idx = 0; idx < MAX_BATCHES_COUNT; idx++) {
1043 if (!cpu_time_per_batch[idx])
1045 delta = (double)(rules_batch / cpu_time_per_batch[idx]);
1046 rate = delta / 1000; /* Save rate in K unit. */
1047 printf(":: Rules batch #%d: %d rules "
1048 "in %f sec[ Rate = %f K Rule/Sec ]\n",
1050 cpu_time_per_batch[idx], rate);
1059 for (i = 0; i < MAX_ACTIONS_NUM; i++) {
1060 if (flow_actions[i] == 0)
1063 & FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_METER))
1070 create_meter_policy(void)
1072 struct rte_mtr_error error;
1074 struct rte_mtr_meter_policy_params policy;
1076 struct rte_flow_action actions[RTE_COLORS][MAX_ACTIONS_NUM];
1079 memset(actions, 0, sizeof(actions));
1080 memset(&policy, 0, sizeof(policy));
1081 nr_ports = rte_eth_dev_count_avail();
1082 for (port_id = 0; port_id < nr_ports; port_id++) {
1083 for (i = 0; i < RTE_COLORS; i++)
1084 fill_actions(actions[i], all_actions[i], 0, 0, 0,
1085 0, 0, 0, unique_data, rx_queues_count,
1086 dst_ports[port_id]);
1087 policy.actions[RTE_COLOR_GREEN] = actions[RTE_COLOR_GREEN];
1088 policy.actions[RTE_COLOR_YELLOW] = actions[RTE_COLOR_YELLOW];
1089 policy.actions[RTE_COLOR_RED] = actions[RTE_COLOR_RED];
1090 policy_id[port_id] = port_id + 10;
1091 ret = rte_mtr_meter_policy_add(port_id, policy_id[port_id],
1094 fprintf(stderr, "port %d: failed to create meter policy\n",
1096 policy_id[port_id] = UINT32_MAX;
1098 memset(actions, 0, sizeof(actions));
1103 destroy_meter_policy(void)
1105 struct rte_mtr_error error;
1109 nr_ports = rte_eth_dev_count_avail();
1110 for (port_id = 0; port_id < nr_ports; port_id++) {
1111 /* If port outside portmask */
1112 if (!((ports_mask >> port_id) & 0x1))
1115 if (rte_mtr_meter_policy_delete
1116 (port_id, policy_id[port_id], &error)) {
1117 fprintf(stderr, "port %u: failed to delete meter policy\n",
1119 rte_exit(EXIT_FAILURE, "Error: Failed to delete meter policy.\n");
1125 create_meter_rule(int port_id, uint32_t counter)
1128 struct rte_mtr_params params;
1129 struct rte_mtr_error error;
1131 memset(¶ms, 0, sizeof(struct rte_mtr_params));
1132 params.meter_enable = 1;
1133 params.stats_mask = 0xffff;
1134 params.use_prev_mtr_color = 0;
1135 params.dscp_table = NULL;
1138 params.meter_profile_id = DEFAULT_METER_PROF_ID;
1141 ret = rte_mtr_create(port_id, counter, ¶ms, 1, &error);
1143 params.meter_policy_id = policy_id[port_id];
1144 ret = rte_mtr_create(port_id, counter, ¶ms, 0, &error);
1148 printf("Port %u create meter idx(%d) error(%d) message: %s\n",
1149 port_id, counter, error.type,
1150 error.message ? error.message : "(no stated reason)");
1151 rte_exit(EXIT_FAILURE, "Error in creating meter\n");
1156 destroy_meter_rule(int port_id, uint32_t counter)
1158 struct rte_mtr_error error;
1160 if (policy_mtr && policy_id[port_id] != UINT32_MAX) {
1161 if (rte_mtr_meter_policy_delete(port_id, policy_id[port_id],
1163 fprintf(stderr, "Error: Failed to delete meter policy\n");
1164 policy_id[port_id] = UINT32_MAX;
1166 if (rte_mtr_destroy(port_id, counter, &error)) {
1167 fprintf(stderr, "Port %d: Failed to delete meter.\n",
1169 rte_exit(EXIT_FAILURE, "Error in deleting meter rule");
1174 meters_handler(int port_id, uint8_t core_id, uint8_t ops)
1176 uint64_t start_batch;
1177 double cpu_time_used, insertion_rate;
1178 int rules_count_per_core, rules_batch_idx;
1179 uint32_t counter, start_counter = 0, end_counter;
1180 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1182 rules_count_per_core = rules_count / mc_pool.cores_count;
1185 start_counter = core_id * rules_count_per_core;
1186 end_counter = (core_id + 1) * rules_count_per_core;
1189 start_batch = rte_get_timer_cycles();
1190 for (counter = start_counter; counter < end_counter; counter++) {
1191 if (ops == METER_CREATE)
1192 create_meter_rule(port_id, counter);
1194 destroy_meter_rule(port_id, counter);
1196 * Save the insertion rate for rules batch.
1197 * Check if the insertion reached the rules
1198 * patch counter, then save the insertion rate
1201 if (!((counter + 1) % rules_batch)) {
1202 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
1203 cpu_time_per_batch[rules_batch_idx] =
1204 ((double)(rte_get_timer_cycles() - start_batch))
1205 / rte_get_timer_hz();
1206 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1207 start_batch = rte_get_timer_cycles();
1211 /* Print insertion rates for all batches */
1212 if (dump_iterations)
1213 print_rules_batches(cpu_time_per_batch);
1216 ((double) (rules_count_per_core / cpu_time_used) / 1000);
1218 /* Insertion rate for all rules in one core */
1219 printf(":: Port %d :: Core %d Meter %s :: start @[%d] - end @[%d],"
1220 " use:%.02fs, rate:%.02fk Rule/Sec\n",
1221 port_id, core_id, ops == METER_CREATE ? "create" : "delete",
1222 start_counter, end_counter - 1,
1223 cpu_time_used, insertion_rate);
1225 if (ops == METER_CREATE)
1226 mc_pool.meters_record.insertion[port_id][core_id]
1229 mc_pool.meters_record.deletion[port_id][core_id]
1234 destroy_meter_profile(void)
1236 struct rte_mtr_error error;
1240 nr_ports = rte_eth_dev_count_avail();
1241 for (port_id = 0; port_id < nr_ports; port_id++) {
1242 /* If port outside portmask */
1243 if (!((ports_mask >> port_id) & 0x1))
1246 if (rte_mtr_meter_profile_delete
1247 (port_id, DEFAULT_METER_PROF_ID, &error)) {
1248 printf("Port %u del profile error(%d) message: %s\n",
1249 port_id, error.type,
1250 error.message ? error.message : "(no stated reason)");
1251 rte_exit(EXIT_FAILURE, "Error: Destroy meter profile Failed!\n");
1257 create_meter_profile(void)
1261 struct rte_mtr_meter_profile mp;
1262 struct rte_mtr_error error;
1265 *currently , only create one meter file for one port
1266 *1 meter profile -> N meter rules -> N rte flows
1268 memset(&mp, 0, sizeof(struct rte_mtr_meter_profile));
1269 nr_ports = rte_eth_dev_count_avail();
1270 for (port_id = 0; port_id < nr_ports; port_id++) {
1271 /* If port outside portmask */
1272 if (!((ports_mask >> port_id) & 0x1))
1274 mp.alg = RTE_MTR_SRTCM_RFC2697;
1275 mp.srtcm_rfc2697.cir = meter_profile_values[0] ?
1276 meter_profile_values[0] : METER_CIR;
1277 mp.srtcm_rfc2697.cbs = meter_profile_values[1] ?
1278 meter_profile_values[1] : METER_CIR / 8;
1279 mp.srtcm_rfc2697.ebs = meter_profile_values[2];
1280 mp.packet_mode = packet_mode;
1281 ret = rte_mtr_meter_profile_add
1282 (port_id, DEFAULT_METER_PROF_ID, &mp, &error);
1284 printf("Port %u create Profile error(%d) message: %s\n",
1285 port_id, error.type,
1286 error.message ? error.message : "(no stated reason)");
1287 rte_exit(EXIT_FAILURE, "Error: Creation meter profile Failed!\n");
1293 destroy_flows(int port_id, uint8_t core_id, struct rte_flow **flows_list)
1295 struct rte_flow_error error;
1296 clock_t start_batch, end_batch;
1297 double cpu_time_used = 0;
1298 double deletion_rate;
1299 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1302 int rules_batch_idx;
1303 int rules_count_per_core;
1305 rules_count_per_core = rules_count / mc_pool.cores_count;
1306 /* If group > 0 , should add 1 flow which created in group 0 */
1307 if (flow_group > 0 && core_id == 0)
1308 rules_count_per_core++;
1310 start_batch = rte_get_timer_cycles();
1311 for (i = 0; i < (uint32_t) rules_count_per_core; i++) {
1312 if (flows_list[i] == 0)
1315 memset(&error, 0x33, sizeof(error));
1316 if (rte_flow_destroy(port_id, flows_list[i], &error)) {
1317 print_flow_error(error);
1318 rte_exit(EXIT_FAILURE, "Error in deleting flow\n");
1322 * Save the deletion rate for rules batch.
1323 * Check if the deletion reached the rules
1324 * patch counter, then save the deletion rate
1327 if (!((i + 1) % rules_batch)) {
1328 end_batch = rte_get_timer_cycles();
1329 delta = (double) (end_batch - start_batch);
1330 rules_batch_idx = ((i + 1) / rules_batch) - 1;
1331 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_timer_hz();
1332 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1333 start_batch = rte_get_timer_cycles();
1337 /* Print deletion rates for all batches */
1338 if (dump_iterations)
1339 print_rules_batches(cpu_time_per_batch);
1341 /* Deletion rate for all rules */
1342 deletion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1343 printf(":: Port %d :: Core %d :: Rules deletion rate -> %f K Rule/Sec\n",
1344 port_id, core_id, deletion_rate);
1345 printf(":: Port %d :: Core %d :: The time for deleting %d rules is %f seconds\n",
1346 port_id, core_id, rules_count_per_core, cpu_time_used);
1348 mc_pool.flows_record.deletion[port_id][core_id] = cpu_time_used;
1351 static struct rte_flow **
1352 insert_flows(int port_id, uint8_t core_id, uint16_t dst_port_id)
1354 struct rte_flow **flows_list;
1355 struct rte_flow_error error;
1356 clock_t start_batch, end_batch;
1357 double first_flow_latency;
1358 double cpu_time_used;
1359 double insertion_rate;
1360 double cpu_time_per_batch[MAX_BATCHES_COUNT] = { 0 };
1362 uint32_t flow_index;
1363 uint32_t counter, start_counter = 0, end_counter;
1364 uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
1365 uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
1366 int rules_batch_idx;
1367 int rules_count_per_core;
1369 rules_count_per_core = rules_count / mc_pool.cores_count;
1371 /* Set boundaries of rules for each core. */
1373 start_counter = core_id * rules_count_per_core;
1374 end_counter = (core_id + 1) * rules_count_per_core;
1376 global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
1377 global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
1379 flows_list = rte_zmalloc("flows_list",
1380 (sizeof(struct rte_flow *) * rules_count_per_core) + 1, 0);
1381 if (flows_list == NULL)
1382 rte_exit(EXIT_FAILURE, "No Memory available!\n");
1386 if (flow_group > 0 && core_id == 0) {
1388 * Create global rule to jump into flow_group,
1389 * this way the app will avoid the default rules.
1391 * This rule will be created only once.
1394 * group 0 eth / end actions jump group <flow_group>
1396 flow = generate_flow(port_id, 0, flow_attrs,
1397 global_items, global_actions,
1398 flow_group, 0, 0, 0, 0, dst_port_id, core_id,
1399 rx_queues_count, unique_data, max_priority, &error);
1402 print_flow_error(error);
1403 rte_exit(EXIT_FAILURE, "Error in creating flow\n");
1405 flows_list[flow_index++] = flow;
1408 start_batch = rte_get_timer_cycles();
1409 for (counter = start_counter; counter < end_counter; counter++) {
1410 flow = generate_flow(port_id, flow_group,
1411 flow_attrs, flow_items, flow_actions,
1412 JUMP_ACTION_TABLE, counter,
1413 hairpin_queues_num, encap_data,
1414 decap_data, dst_port_id,
1415 core_id, rx_queues_count,
1416 unique_data, max_priority, &error);
1419 first_flow_latency = (double) (rte_get_timer_cycles() - start_batch);
1420 first_flow_latency /= rte_get_timer_hz();
1421 /* In millisecond */
1422 first_flow_latency *= 1000;
1423 printf(":: First Flow Latency :: Port %d :: First flow "
1424 "installed in %f milliseconds\n",
1425 port_id, first_flow_latency);
1429 counter = end_counter;
1432 print_flow_error(error);
1433 rte_exit(EXIT_FAILURE, "Error in creating flow\n");
1436 flows_list[flow_index++] = flow;
1439 * Save the insertion rate for rules batch.
1440 * Check if the insertion reached the rules
1441 * patch counter, then save the insertion rate
1444 if (!((counter + 1) % rules_batch)) {
1445 end_batch = rte_get_timer_cycles();
1446 delta = (double) (end_batch - start_batch);
1447 rules_batch_idx = ((counter + 1) / rules_batch) - 1;
1448 cpu_time_per_batch[rules_batch_idx] = delta / rte_get_timer_hz();
1449 cpu_time_used += cpu_time_per_batch[rules_batch_idx];
1450 start_batch = rte_get_timer_cycles();
1454 /* Print insertion rates for all batches */
1455 if (dump_iterations)
1456 print_rules_batches(cpu_time_per_batch);
1458 printf(":: Port %d :: Core %d boundaries :: start @[%d] - end @[%d]\n",
1459 port_id, core_id, start_counter, end_counter - 1);
1461 /* Insertion rate for all rules in one core */
1462 insertion_rate = ((double) (rules_count_per_core / cpu_time_used) / 1000);
1463 printf(":: Port %d :: Core %d :: Rules insertion rate -> %f K Rule/Sec\n",
1464 port_id, core_id, insertion_rate);
1465 printf(":: Port %d :: Core %d :: The time for creating %d in rules %f seconds\n",
1466 port_id, core_id, rules_count_per_core, cpu_time_used);
1468 mc_pool.flows_record.insertion[port_id][core_id] = cpu_time_used;
1473 flows_handler(uint8_t core_id)
1475 struct rte_flow **flows_list;
1476 uint16_t port_idx = 0;
1480 nr_ports = rte_eth_dev_count_avail();
1482 if (rules_batch > rules_count)
1483 rules_batch = rules_count;
1485 printf(":: Rules Count per port: %d\n\n", rules_count);
1487 for (port_id = 0; port_id < nr_ports; port_id++) {
1488 /* If port outside portmask */
1489 if (!((ports_mask >> port_id) & 0x1))
1492 /* Insertion part. */
1493 mc_pool.last_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1495 meters_handler(port_id, core_id, METER_CREATE);
1496 flows_list = insert_flows(port_id, core_id,
1497 dst_ports[port_idx++]);
1498 if (flows_list == NULL)
1499 rte_exit(EXIT_FAILURE, "Error: Insertion Failed!\n");
1500 mc_pool.current_alloc[core_id] = (int64_t)dump_socket_mem(stdout);
1502 /* Deletion part. */
1504 destroy_flows(port_id, core_id, flows_list);
1506 meters_handler(port_id, core_id, METER_DELETE);
1512 dump_used_cpu_time(const char *item,
1513 uint16_t port, struct used_cpu_time *used_time)
1516 /* Latency: total count of rte rules divided
1517 * over max time used by thread between all
1520 * Throughput: total count of rte rules divided
1521 * over the average of the time cosumed by all
1524 double insertion_latency_time;
1525 double insertion_throughput_time;
1526 double deletion_latency_time;
1527 double deletion_throughput_time;
1528 double insertion_latency, insertion_throughput;
1529 double deletion_latency, deletion_throughput;
1531 /* Save first insertion/deletion rates from first thread.
1532 * Start comparing with all threads, if any thread used
1533 * time more than current saved, replace it.
1535 * Thus in the end we will have the max time used for
1536 * insertion/deletion by one thread.
1538 * As for memory consumption, save the min of all threads
1539 * of last alloc, and save the max for all threads for
1543 insertion_latency_time = used_time->insertion[port][0];
1544 deletion_latency_time = used_time->deletion[port][0];
1545 insertion_throughput_time = used_time->insertion[port][0];
1546 deletion_throughput_time = used_time->deletion[port][0];
1548 i = mc_pool.cores_count;
1550 insertion_throughput_time += used_time->insertion[port][i];
1551 deletion_throughput_time += used_time->deletion[port][i];
1552 if (insertion_latency_time < used_time->insertion[port][i])
1553 insertion_latency_time = used_time->insertion[port][i];
1554 if (deletion_latency_time < used_time->deletion[port][i])
1555 deletion_latency_time = used_time->deletion[port][i];
1558 insertion_latency = ((double) (mc_pool.rules_count
1559 / insertion_latency_time) / 1000);
1560 deletion_latency = ((double) (mc_pool.rules_count
1561 / deletion_latency_time) / 1000);
1563 insertion_throughput_time /= mc_pool.cores_count;
1564 deletion_throughput_time /= mc_pool.cores_count;
1565 insertion_throughput = ((double) (mc_pool.rules_count
1566 / insertion_throughput_time) / 1000);
1567 deletion_throughput = ((double) (mc_pool.rules_count
1568 / deletion_throughput_time) / 1000);
1571 printf("\n%s\n:: [Latency | Insertion] All Cores :: Port %d :: ",
1573 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1575 printf(":: [Latency | Insertion] All Cores :: Port %d :: ", port);
1576 printf("The time for creating %d rules is %f seconds\n",
1577 mc_pool.rules_count, insertion_latency_time);
1579 /* Throughput stats */
1580 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1581 printf("Total flows insertion rate -> %f K Rules/Sec\n",
1582 insertion_throughput);
1583 printf(":: [Throughput | Insertion] All Cores :: Port %d :: ", port);
1584 printf("The average time for creating %d rules is %f seconds\n",
1585 mc_pool.rules_count, insertion_throughput_time);
1589 printf(":: [Latency | Deletion] All Cores :: Port %d :: Total "
1590 "deletion rate -> %f K Rules/Sec\n",
1591 port, deletion_latency);
1592 printf(":: [Latency | Deletion] All Cores :: Port %d :: ",
1594 printf("The time for deleting %d rules is %f seconds\n",
1595 mc_pool.rules_count, deletion_latency_time);
1597 /* Throughput stats */
1598 printf(":: [Throughput | Deletion] All Cores :: Port %d :: Total "
1599 "deletion rate -> %f K Rules/Sec\n",
1600 port, deletion_throughput);
1601 printf(":: [Throughput | Deletion] All Cores :: Port %d :: ",
1603 printf("The average time for deleting %d rules is %f seconds\n",
1604 mc_pool.rules_count, deletion_throughput_time);
1609 dump_used_mem(uint16_t port)
1612 int64_t last_alloc, current_alloc;
1613 int flow_size_in_bytes;
1615 last_alloc = mc_pool.last_alloc[0];
1616 current_alloc = mc_pool.current_alloc[0];
1618 i = mc_pool.cores_count;
1620 if (last_alloc > mc_pool.last_alloc[i])
1621 last_alloc = mc_pool.last_alloc[i];
1622 if (current_alloc < mc_pool.current_alloc[i])
1623 current_alloc = mc_pool.current_alloc[i];
1626 flow_size_in_bytes = (current_alloc - last_alloc) / mc_pool.rules_count;
1627 printf("\n:: Port %d :: rte_flow size in DPDK layer: %d Bytes\n",
1628 port, flow_size_in_bytes);
1632 run_rte_flow_handler_cores(void *data __rte_unused)
1635 int lcore_counter = 0;
1636 int lcore_id = rte_lcore_id();
1639 RTE_LCORE_FOREACH(i) {
1640 /* If core not needed return. */
1641 if (lcore_id == i) {
1642 printf(":: lcore %d mapped with index %d\n", lcore_id, lcore_counter);
1643 if (lcore_counter >= (int) mc_pool.cores_count)
1649 lcore_id = lcore_counter;
1651 if (lcore_id >= (int) mc_pool.cores_count)
1654 mc_pool.rules_count = rules_count;
1656 flows_handler(lcore_id);
1658 /* Only main core to print total results. */
1662 /* Make sure all cores finished insertion/deletion process. */
1663 rte_eal_mp_wait_lcore();
1665 RTE_ETH_FOREACH_DEV(port) {
1666 /* If port outside portmask */
1667 if (!((ports_mask >> port) & 0x1))
1670 dump_used_cpu_time("Meters:",
1671 port, &mc_pool.meters_record);
1672 dump_used_cpu_time("Flows:",
1673 port, &mc_pool.flows_record);
1674 dump_used_mem(port);
1681 signal_handler(int signum)
1683 if (signum == SIGINT || signum == SIGTERM) {
1684 printf("\n\nSignal %d received, preparing to exit...\n",
1686 printf("Error: Stats are wrong due to sudden signal!\n\n");
1691 static inline uint16_t
1692 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
1695 cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
1701 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
1707 nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
1708 li->tx_pkts += nr_tx;
1709 li->tx_drops += cnt - nr_tx;
1711 for (i = nr_tx; i < cnt; i++)
1712 rte_pktmbuf_free(li->pkts[i]);
1716 * Method to convert numbers into pretty numbers that easy
1717 * to read. The design here is to add comma after each three
1718 * digits and set all of this inside buffer.
1720 * For example if n = 1799321, the output will be
1721 * 1,799,321 after this method which is easier to read.
1724 pretty_number(uint64_t n, char *buf)
1731 sprintf(p[i], "%03d", (int)(n % 1000));
1736 sprintf(p[i++], "%d", (int)n);
1739 off += sprintf(buf + off, "%s,", p[i]);
1740 buf[strlen(buf) - 1] = '\0';
1746 packet_per_second_stats(void)
1748 struct lcore_info *old;
1749 struct lcore_info *li, *oli;
1753 old = rte_zmalloc("old",
1754 sizeof(struct lcore_info) * RTE_MAX_LCORE, 0);
1756 rte_exit(EXIT_FAILURE, "No Memory available!\n");
1758 memcpy(old, lcore_infos,
1759 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1761 while (!force_quit) {
1762 uint64_t total_tx_pkts = 0;
1763 uint64_t total_rx_pkts = 0;
1764 uint64_t total_tx_drops = 0;
1765 uint64_t tx_delta, rx_delta, drops_delta;
1767 int nr_valid_core = 0;
1772 char go_up_nr_lines[16];
1774 sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
1775 printf("%s\r", go_up_nr_lines);
1778 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
1779 printf("%6s %16s %16s %16s\n", "------", "----------------",
1780 "----------------", "----------------");
1782 for (i = 0; i < RTE_MAX_LCORE; i++) {
1783 li = &lcore_infos[i];
1785 if (li->mode != LCORE_MODE_PKT)
1788 tx_delta = li->tx_pkts - oli->tx_pkts;
1789 rx_delta = li->rx_pkts - oli->rx_pkts;
1790 drops_delta = li->tx_drops - oli->tx_drops;
1791 printf("%6d %16s %16s %16s\n", i,
1792 pretty_number(tx_delta, buf[0]),
1793 pretty_number(drops_delta, buf[1]),
1794 pretty_number(rx_delta, buf[2]));
1796 total_tx_pkts += tx_delta;
1797 total_rx_pkts += rx_delta;
1798 total_tx_drops += drops_delta;
1804 if (nr_valid_core > 1) {
1805 printf("%6s %16s %16s %16s\n", "total",
1806 pretty_number(total_tx_pkts, buf[0]),
1807 pretty_number(total_tx_drops, buf[1]),
1808 pretty_number(total_rx_pkts, buf[2]));
1812 memcpy(old, lcore_infos,
1813 sizeof(struct lcore_info) * RTE_MAX_LCORE);
1818 start_forwarding(void *data __rte_unused)
1820 int lcore = rte_lcore_id();
1823 struct lcore_info *li = &lcore_infos[lcore];
1828 if (li->mode == LCORE_MODE_STATS) {
1829 printf(":: started stats on lcore %u\n", lcore);
1830 packet_per_second_stats();
1835 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1836 if (li->streams[stream_id].rx_port == -1)
1840 li->streams[stream_id].rx_port,
1841 li->streams[stream_id].rx_queue);
1844 li->streams[stream_id].tx_port,
1845 li->streams[stream_id].tx_queue);
1851 init_lcore_info(void)
1859 int streams_per_core;
1860 int unassigned_streams;
1862 nr_port = rte_eth_dev_count_avail();
1864 /* First logical core is reserved for stats printing */
1865 lcore = rte_get_next_lcore(-1, 0, 0);
1866 lcore_infos[lcore].mode = LCORE_MODE_STATS;
1869 * Initialize all cores
1870 * All cores at first must have -1 value in all streams
1871 * This means that this stream is not used, or not set
1874 for (i = 0; i < RTE_MAX_LCORE; i++)
1875 for (j = 0; j < MAX_STREAMS; j++) {
1876 lcore_infos[i].streams[j].tx_port = -1;
1877 lcore_infos[i].streams[j].rx_port = -1;
1878 lcore_infos[i].streams[j].tx_queue = -1;
1879 lcore_infos[i].streams[j].rx_queue = -1;
1880 lcore_infos[i].streams_nb = 0;
1884 * Calculate the total streams count.
1885 * Also distribute those streams count between the available
1886 * logical cores except first core, since it's reserved for
1889 nb_fwd_streams = nr_port * rx_queues_count;
1890 if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1891 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1892 lcore = rte_get_next_lcore(lcore, 0, 0);
1893 lcore_infos[lcore].streams_nb = 1;
1896 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1897 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1898 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1899 lcore = rte_get_next_lcore(lcore, 0, 0);
1900 lcore_infos[lcore].streams_nb = streams_per_core;
1901 if (unassigned_streams) {
1902 lcore_infos[lcore].streams_nb++;
1903 unassigned_streams--;
1909 * Set the streams for the cores according to each logical
1910 * core stream count.
1911 * The streams is built on the design of what received should
1912 * forward as well, this means that if you received packets on
1913 * port 0 queue 0 then the same queue should forward the
1914 * packets, using the same logical core.
1916 lcore = rte_get_next_lcore(-1, 0, 0);
1917 for (port = 0; port < nr_port; port++) {
1918 /* Create FWD stream */
1919 for (queue = 0; queue < rx_queues_count; queue++) {
1920 if (!lcore_infos[lcore].streams_nb ||
1921 !(stream_id % lcore_infos[lcore].streams_nb)) {
1922 lcore = rte_get_next_lcore(lcore, 0, 0);
1923 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1926 lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1927 lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1928 lcore_infos[lcore].streams[stream_id].rx_port = port;
1929 lcore_infos[lcore].streams[stream_id].tx_port = port;
1934 /* Print all streams */
1935 printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1936 for (i = 0; i < RTE_MAX_LCORE; i++)
1937 for (j = 0; j < MAX_STREAMS; j++) {
1938 /* No streams for this core */
1939 if (lcore_infos[i].streams[j].tx_port == -1)
1941 printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1943 lcore_infos[i].streams[j].rx_port,
1944 lcore_infos[i].streams[j].rx_queue,
1945 lcore_infos[i].streams[j].tx_port,
1946 lcore_infos[i].streams[j].tx_queue);
1955 uint16_t hairpin_queue;
1959 struct rte_eth_hairpin_conf hairpin_conf = {
1962 struct rte_eth_conf port_conf = {
1968 struct rte_eth_txconf txq_conf;
1969 struct rte_eth_rxconf rxq_conf;
1970 struct rte_eth_dev_info dev_info;
1972 nr_queues = rx_queues_count;
1973 if (hairpin_queues_num != 0)
1974 nr_queues = rx_queues_count + hairpin_queues_num;
1976 nr_ports = rte_eth_dev_count_avail();
1978 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1980 mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1981 total_mbuf_num, mbuf_cache_size,
1984 if (mbuf_mp == NULL)
1985 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1987 for (port_id = 0; port_id < nr_ports; port_id++) {
1988 uint64_t rx_metadata = 0;
1990 rx_metadata |= RTE_ETH_RX_METADATA_USER_FLAG;
1991 rx_metadata |= RTE_ETH_RX_METADATA_USER_MARK;
1993 ret = rte_eth_rx_metadata_negotiate(port_id, &rx_metadata);
1995 if (!(rx_metadata & RTE_ETH_RX_METADATA_USER_FLAG)) {
1996 printf(":: flow action FLAG will not affect Rx mbufs on port=%u\n",
2000 if (!(rx_metadata & RTE_ETH_RX_METADATA_USER_MARK)) {
2001 printf(":: flow action MARK will not affect Rx mbufs on port=%u\n",
2004 } else if (ret != -ENOTSUP) {
2005 rte_exit(EXIT_FAILURE, "Error when negotiating Rx meta features on port=%u: %s\n",
2006 port_id, rte_strerror(-ret));
2009 ret = rte_eth_dev_info_get(port_id, &dev_info);
2011 rte_exit(EXIT_FAILURE,
2012 "Error during getting device"
2013 " (port %u) info: %s\n",
2014 port_id, strerror(-ret));
2016 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
2017 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
2019 printf(":: initializing port: %d\n", port_id);
2021 ret = rte_eth_dev_configure(port_id, nr_queues,
2022 nr_queues, &port_conf);
2024 rte_exit(EXIT_FAILURE,
2025 ":: cannot configure device: err=%d, port=%u\n",
2028 rxq_conf = dev_info.default_rxconf;
2029 for (std_queue = 0; std_queue < rx_queues_count; std_queue++) {
2030 ret = rte_eth_rx_queue_setup(port_id, std_queue, rxd_count,
2031 rte_eth_dev_socket_id(port_id),
2035 rte_exit(EXIT_FAILURE,
2036 ":: Rx queue setup failed: err=%d, port=%u\n",
2040 txq_conf = dev_info.default_txconf;
2041 for (std_queue = 0; std_queue < tx_queues_count; std_queue++) {
2042 ret = rte_eth_tx_queue_setup(port_id, std_queue, txd_count,
2043 rte_eth_dev_socket_id(port_id),
2046 rte_exit(EXIT_FAILURE,
2047 ":: Tx queue setup failed: err=%d, port=%u\n",
2051 /* Catch all packets from traffic generator. */
2052 ret = rte_eth_promiscuous_enable(port_id);
2054 rte_exit(EXIT_FAILURE,
2055 ":: promiscuous mode enable failed: err=%s, port=%u\n",
2056 rte_strerror(-ret), port_id);
2058 if (hairpin_queues_num != 0) {
2060 * Configure peer which represents hairpin Tx.
2061 * Hairpin queue numbers start after standard queues
2062 * (rx_queues_count and tx_queues_count).
2064 for (hairpin_queue = rx_queues_count, std_queue = 0;
2065 hairpin_queue < nr_queues;
2066 hairpin_queue++, std_queue++) {
2067 hairpin_conf.peers[0].port = port_id;
2068 hairpin_conf.peers[0].queue =
2069 std_queue + tx_queues_count;
2070 ret = rte_eth_rx_hairpin_queue_setup(
2071 port_id, hairpin_queue,
2072 rxd_count, &hairpin_conf);
2074 rte_exit(EXIT_FAILURE,
2075 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
2079 for (hairpin_queue = tx_queues_count, std_queue = 0;
2080 hairpin_queue < nr_queues;
2081 hairpin_queue++, std_queue++) {
2082 hairpin_conf.peers[0].port = port_id;
2083 hairpin_conf.peers[0].queue =
2084 std_queue + rx_queues_count;
2085 ret = rte_eth_tx_hairpin_queue_setup(
2086 port_id, hairpin_queue,
2087 txd_count, &hairpin_conf);
2089 rte_exit(EXIT_FAILURE,
2090 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
2095 ret = rte_eth_dev_start(port_id);
2097 rte_exit(EXIT_FAILURE,
2098 "rte_eth_dev_start:err=%d, port=%u\n",
2101 printf(":: initializing port: %d done\n", port_id);
2106 main(int argc, char **argv)
2110 struct rte_flow_error error;
2112 ret = rte_eal_init(argc, argv);
2114 rte_exit(EXIT_FAILURE, "EAL init failed\n");
2117 dump_iterations = false;
2118 rules_count = DEFAULT_RULES_COUNT;
2119 rules_batch = DEFAULT_RULES_BATCH;
2120 delete_flag = false;
2121 dump_socket_mem_flag = false;
2122 flow_group = DEFAULT_GROUP;
2123 unique_data = false;
2125 rx_queues_count = (uint8_t) RXQ_NUM;
2126 tx_queues_count = (uint8_t) TXQ_NUM;
2127 rxd_count = (uint8_t) NR_RXD;
2128 txd_count = (uint8_t) NR_TXD;
2129 mbuf_size = (uint32_t) MBUF_SIZE;
2130 mbuf_cache_size = (uint32_t) MBUF_CACHE_SIZE;
2131 total_mbuf_num = (uint32_t) TOTAL_MBUF_NUM;
2133 signal(SIGINT, signal_handler);
2134 signal(SIGTERM, signal_handler);
2139 args_parse(argc, argv);
2143 nb_lcores = rte_lcore_count();
2145 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
2147 printf(":: Flows Count per port: %d\n\n", rules_count);
2149 rte_srand(rand_seed);
2152 create_meter_profile();
2154 create_meter_policy();
2156 rte_eal_mp_remote_launch(run_rte_flow_handler_cores, NULL, CALL_MAIN);
2160 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MAIN);
2162 if (has_meter() && delete_flag) {
2163 destroy_meter_profile();
2165 destroy_meter_policy();
2168 RTE_ETH_FOREACH_DEV(port) {
2169 rte_flow_flush(port, &error);
2170 if (rte_eth_dev_stop(port) != 0)
2171 printf("Failed to stop device on port %u\n", port);
2172 rte_eth_dev_close(port);
2174 printf("\nBye ...\n");