app/flow-perf: support flag action
[dpdk.git] / app / test-flow-perf / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  *
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/
7  *
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
11  * the stress.
12  *
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.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <inttypes.h>
24 #include <stdarg.h>
25 #include <errno.h>
26 #include <getopt.h>
27 #include <stdbool.h>
28 #include <sys/time.h>
29 #include <signal.h>
30 #include <unistd.h>
31
32 #include <rte_malloc.h>
33 #include <rte_mempool.h>
34 #include <rte_mbuf.h>
35 #include <rte_ethdev.h>
36 #include <rte_flow.h>
37
38 #include "config.h"
39 #include "flow_gen.h"
40
41 #define MAX_ITERATIONS             100
42 #define DEFAULT_RULES_COUNT    4000000
43 #define DEFAULT_ITERATION       100000
44
45 struct rte_flow *flow;
46 static uint8_t flow_group;
47
48 static uint64_t flow_items[MAX_ITEMS_NUM];
49 static uint64_t flow_actions[MAX_ACTIONS_NUM];
50 static uint64_t flow_attrs[MAX_ATTRS_NUM];
51 static uint8_t items_idx, actions_idx, attrs_idx;
52
53 static volatile bool force_quit;
54 static bool dump_iterations;
55 static bool delete_flag;
56 static bool dump_socket_mem_flag;
57 static bool enable_fwd;
58
59 static struct rte_mempool *mbuf_mp;
60 static uint32_t nb_lcores;
61 static uint32_t flows_count;
62 static uint32_t iterations_number;
63 static uint32_t hairpin_queues_num; /* total hairpin q number - default: 0 */
64 static uint32_t nb_lcores;
65
66 #define MAX_PKT_BURST    32
67 #define LCORE_MODE_PKT    1
68 #define LCORE_MODE_STATS  2
69 #define MAX_STREAMS      64
70 #define MAX_LCORES       64
71
72 struct stream {
73         int tx_port;
74         int tx_queue;
75         int rx_port;
76         int rx_queue;
77 };
78
79 struct lcore_info {
80         int mode;
81         int streams_nb;
82         struct stream streams[MAX_STREAMS];
83         /* stats */
84         uint64_t tx_pkts;
85         uint64_t tx_drops;
86         uint64_t rx_pkts;
87         struct rte_mbuf *pkts[MAX_PKT_BURST];
88 } __rte_cache_aligned;
89
90 static struct lcore_info lcore_infos[MAX_LCORES];
91
92 static void
93 usage(char *progname)
94 {
95         printf("\nusage: %s\n", progname);
96         printf("\nControl configurations:\n");
97         printf("  --flows-count=N: to set the number of needed"
98                 " flows to insert, default is 4,000,000\n");
99         printf("  --dump-iterations: To print rates for each"
100                 " iteration\n");
101         printf("  --deletion-rate: Enable deletion rate"
102                 " calculations\n");
103         printf("  --dump-socket-mem: To dump all socket memory\n");
104         printf("  --enable-fwd: To enable packets forwarding"
105                 " after insertion\n");
106
107         printf("To set flow attributes:\n");
108         printf("  --ingress: set ingress attribute in flows\n");
109         printf("  --egress: set egress attribute in flows\n");
110         printf("  --transfer: set transfer attribute in flows\n");
111         printf("  --group=N: set group for all flows,"
112                 " default is 0\n");
113
114         printf("To set flow items:\n");
115         printf("  --ether: add ether layer in flow items\n");
116         printf("  --vlan: add vlan layer in flow items\n");
117         printf("  --ipv4: add ipv4 layer in flow items\n");
118         printf("  --ipv6: add ipv6 layer in flow items\n");
119         printf("  --tcp: add tcp layer in flow items\n");
120         printf("  --udp: add udp layer in flow items\n");
121         printf("  --vxlan: add vxlan layer in flow items\n");
122         printf("  --vxlan-gpe: add vxlan-gpe layer in flow items\n");
123         printf("  --gre: add gre layer in flow items\n");
124         printf("  --geneve: add geneve layer in flow items\n");
125         printf("  --gtp: add gtp layer in flow items\n");
126         printf("  --meta: add meta layer in flow items\n");
127         printf("  --tag: add tag layer in flow items\n");
128
129         printf("To set flow actions:\n");
130         printf("  --port-id: add port-id action in flow actions\n");
131         printf("  --rss: add rss action in flow actions\n");
132         printf("  --queue: add queue action in flow actions\n");
133         printf("  --jump: add jump action in flow actions\n");
134         printf("  --mark: add mark action in flow actions\n");
135         printf("  --count: add count action in flow actions\n");
136         printf("  --set-meta: add set meta action in flow actions\n");
137         printf("  --set-tag: add set tag action in flow actions\n");
138         printf("  --drop: add drop action in flow actions\n");
139         printf("  --hairpin-queue=N: add hairpin-queue action in flow actions\n");
140         printf("  --hairpin-rss=N: add hairpin-rss action in flow actions\n");
141         printf("  --set-src-mac: add set src mac action to flow actions\n"
142                 "Src mac to be set is random each flow\n");
143         printf("  --set-dst-mac: add set dst mac action to flow actions\n"
144                  "Dst mac to be set is random each flow\n");
145         printf("  --set-src-ipv4: add set src ipv4 action to flow actions\n"
146                 "Src ipv4 to be set is random each flow\n");
147         printf("  --set-dst-ipv4 add set dst ipv4 action to flow actions\n"
148                 "Dst ipv4 to be set is random each flow\n");
149         printf("  --set-src-ipv6: add set src ipv6 action to flow actions\n"
150                 "Src ipv6 to be set is random each flow\n");
151         printf("  --set-dst-ipv6: add set dst ipv6 action to flow actions\n"
152                 "Dst ipv6 to be set is random each flow\n");
153         printf("  --set-src-tp: add set src tp action to flow actions\n"
154                 "Src tp to be set is random each flow\n");
155         printf("  --set-dst-tp: add set dst tp action to flow actions\n"
156                 "Dst tp to be set is random each flow\n");
157         printf("  --inc-tcp-ack: add inc tcp ack action to flow actions\n"
158                 "tcp ack will be increments by 1\n");
159         printf("  --dec-tcp-ack: add dec tcp ack action to flow actions\n"
160                 "tcp ack will be decrements by 1\n");
161         printf("  --inc-tcp-seq: add inc tcp seq action to flow actions\n"
162                 "tcp seq will be increments by 1\n");
163         printf("  --dec-tcp-seq: add dec tcp seq action to flow actions\n"
164                 "tcp seq will be decrements by 1\n");
165         printf("  --set-ttl: add set ttl action to flow actions\n"
166                 "L3 ttl to be set is random each flow\n");
167         printf("  --dec-ttl: add dec ttl action to flow actions\n"
168                 "L3 ttl will be decrements by 1\n");
169         printf("  --set-ipv4-dscp: add set ipv4 dscp action to flow actions\n"
170                 "ipv4 dscp value to be set is random each flow\n");
171         printf("  --set-ipv6-dscp: add set ipv6 dscp action to flow actions\n"
172                 "ipv6 dscp value to be set is random each flow\n");
173         printf("  --flag: add flag action to flow actions\n");
174 }
175
176 static void
177 args_parse(int argc, char **argv)
178 {
179         char **argvopt;
180         int n, opt;
181         int opt_idx;
182         size_t i;
183
184         static const struct option_dict {
185                 const char *str;
186                 const uint64_t mask;
187                 uint64_t *map;
188                 uint8_t *map_idx;
189
190         } flow_options[] = {
191                 {
192                         .str = "ether",
193                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH),
194                         .map = &flow_items[0],
195                         .map_idx = &items_idx
196                 },
197                 {
198                         .str = "ipv4",
199                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV4),
200                         .map = &flow_items[0],
201                         .map_idx = &items_idx
202                 },
203                 {
204                         .str = "ipv6",
205                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_IPV6),
206                         .map = &flow_items[0],
207                         .map_idx = &items_idx
208                 },
209                 {
210                         .str = "vlan",
211                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VLAN),
212                         .map = &flow_items[0],
213                         .map_idx = &items_idx
214                 },
215                 {
216                         .str = "tcp",
217                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TCP),
218                         .map = &flow_items[0],
219                         .map_idx = &items_idx
220                 },
221                 {
222                         .str = "udp",
223                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_UDP),
224                         .map = &flow_items[0],
225                         .map_idx = &items_idx
226                 },
227                 {
228                         .str = "vxlan",
229                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN),
230                         .map = &flow_items[0],
231                         .map_idx = &items_idx
232                 },
233                 {
234                         .str = "vxlan-gpe",
235                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_VXLAN_GPE),
236                         .map = &flow_items[0],
237                         .map_idx = &items_idx
238                 },
239                 {
240                         .str = "gre",
241                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GRE),
242                         .map = &flow_items[0],
243                         .map_idx = &items_idx
244                 },
245                 {
246                         .str = "geneve",
247                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GENEVE),
248                         .map = &flow_items[0],
249                         .map_idx = &items_idx
250                 },
251                 {
252                         .str = "gtp",
253                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_GTP),
254                         .map = &flow_items[0],
255                         .map_idx = &items_idx
256                 },
257                 {
258                         .str = "meta",
259                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_META),
260                         .map = &flow_items[0],
261                         .map_idx = &items_idx
262                 },
263                 {
264                         .str = "tag",
265                         .mask = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_TAG),
266                         .map = &flow_items[0],
267                         .map_idx = &items_idx
268                 },
269                 {
270                         .str = "ingress",
271                         .mask = INGRESS,
272                         .map = &flow_attrs[0],
273                         .map_idx = &attrs_idx
274                 },
275                 {
276                         .str = "egress",
277                         .mask = EGRESS,
278                         .map = &flow_attrs[0],
279                         .map_idx = &attrs_idx
280                 },
281                 {
282                         .str = "transfer",
283                         .mask = TRANSFER,
284                         .map = &flow_attrs[0],
285                         .map_idx = &attrs_idx
286                 },
287                 {
288                         .str = "port-id",
289                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_PORT_ID),
290                         .map = &flow_actions[0],
291                         .map_idx = &actions_idx
292                 },
293                 {
294                         .str = "rss",
295                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_RSS),
296                         .map = &flow_actions[0],
297                         .map_idx = &actions_idx
298                 },
299                 {
300                         .str = "queue",
301                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_QUEUE),
302                         .map = &flow_actions[0],
303                         .map_idx = &actions_idx
304                 },
305                 {
306                         .str = "jump",
307                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_JUMP),
308                         .map = &flow_actions[0],
309                         .map_idx = &actions_idx
310                 },
311                 {
312                         .str = "mark",
313                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_MARK),
314                         .map = &flow_actions[0],
315                         .map_idx = &actions_idx
316                 },
317                 {
318                         .str = "count",
319                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_COUNT),
320                         .map = &flow_actions[0],
321                         .map_idx = &actions_idx
322                 },
323                 {
324                         .str = "set-meta",
325                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_META),
326                         .map = &flow_actions[0],
327                         .map_idx = &actions_idx
328                 },
329                 {
330                         .str = "set-tag",
331                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_SET_TAG),
332                         .map = &flow_actions[0],
333                         .map_idx = &actions_idx
334                 },
335                 {
336                         .str = "drop",
337                         .mask = FLOW_ACTION_MASK(RTE_FLOW_ACTION_TYPE_DROP),
338                         .map = &flow_actions[0],
339                         .map_idx = &actions_idx
340                 },
341                 {
342                         .str = "set-src-mac",
343                         .mask = FLOW_ACTION_MASK(
344                                 RTE_FLOW_ACTION_TYPE_SET_MAC_SRC
345                         ),
346                         .map = &flow_actions[0],
347                         .map_idx = &actions_idx
348                 },
349                 {
350                         .str = "set-dst-mac",
351                         .mask = FLOW_ACTION_MASK(
352                                 RTE_FLOW_ACTION_TYPE_SET_MAC_DST
353                         ),
354                         .map = &flow_actions[0],
355                         .map_idx = &actions_idx
356                 },
357                 {
358                         .str = "set-src-ipv4",
359                         .mask = FLOW_ACTION_MASK(
360                                 RTE_FLOW_ACTION_TYPE_SET_IPV4_SRC
361                         ),
362                         .map = &flow_actions[0],
363                         .map_idx = &actions_idx
364                 },
365                 {
366                         .str = "set-dst-ipv4",
367                         .mask = FLOW_ACTION_MASK(
368                                 RTE_FLOW_ACTION_TYPE_SET_IPV4_DST
369                         ),
370                         .map = &flow_actions[0],
371                         .map_idx = &actions_idx
372                 },
373                 {
374                         .str = "set-src-ipv6",
375                         .mask = FLOW_ACTION_MASK(
376                                 RTE_FLOW_ACTION_TYPE_SET_IPV6_SRC
377                         ),
378                         .map = &flow_actions[0],
379                         .map_idx = &actions_idx
380                 },
381                 {
382                         .str = "set-dst-ipv6",
383                         .mask = FLOW_ACTION_MASK(
384                                 RTE_FLOW_ACTION_TYPE_SET_IPV6_DST
385                         ),
386                         .map = &flow_actions[0],
387                         .map_idx = &actions_idx
388                 },
389                 {
390                         .str = "set-src-tp",
391                         .mask = FLOW_ACTION_MASK(
392                                 RTE_FLOW_ACTION_TYPE_SET_TP_SRC
393                         ),
394                         .map = &flow_actions[0],
395                         .map_idx = &actions_idx
396                 },
397                 {
398                         .str = "set-dst-tp",
399                         .mask = FLOW_ACTION_MASK(
400                                 RTE_FLOW_ACTION_TYPE_SET_TP_DST
401                         ),
402                         .map = &flow_actions[0],
403                         .map_idx = &actions_idx
404                 },
405                 {
406                         .str = "inc-tcp-ack",
407                         .mask = FLOW_ACTION_MASK(
408                                 RTE_FLOW_ACTION_TYPE_INC_TCP_ACK
409                         ),
410                         .map = &flow_actions[0],
411                         .map_idx = &actions_idx
412                 },
413                 {
414                         .str = "dec-tcp-ack",
415                         .mask = FLOW_ACTION_MASK(
416                                 RTE_FLOW_ACTION_TYPE_DEC_TCP_ACK
417                         ),
418                         .map = &flow_actions[0],
419                         .map_idx = &actions_idx
420                 },
421                 {
422                         .str = "inc-tcp-seq",
423                         .mask = FLOW_ACTION_MASK(
424                                 RTE_FLOW_ACTION_TYPE_INC_TCP_SEQ
425                         ),
426                         .map = &flow_actions[0],
427                         .map_idx = &actions_idx
428                 },
429                 {
430                         .str = "dec-tcp-seq",
431                         .mask = FLOW_ACTION_MASK(
432                                 RTE_FLOW_ACTION_TYPE_DEC_TCP_SEQ
433                         ),
434                         .map = &flow_actions[0],
435                         .map_idx = &actions_idx
436                 },
437                 {
438                         .str = "set-ttl",
439                         .mask = FLOW_ACTION_MASK(
440                                 RTE_FLOW_ACTION_TYPE_SET_TTL
441                         ),
442                         .map = &flow_actions[0],
443                         .map_idx = &actions_idx
444                 },
445                 {
446                         .str = "dec-ttl",
447                         .mask = FLOW_ACTION_MASK(
448                                 RTE_FLOW_ACTION_TYPE_DEC_TTL
449                         ),
450                         .map = &flow_actions[0],
451                         .map_idx = &actions_idx
452                 },
453                 {
454                         .str = "set-ipv4-dscp",
455                         .mask = FLOW_ACTION_MASK(
456                                 RTE_FLOW_ACTION_TYPE_SET_IPV4_DSCP
457                         ),
458                         .map = &flow_actions[0],
459                         .map_idx = &actions_idx
460                 },
461                 {
462                         .str = "set-ipv6-dscp",
463                         .mask = FLOW_ACTION_MASK(
464                                 RTE_FLOW_ACTION_TYPE_SET_IPV6_DSCP
465                         ),
466                         .map = &flow_actions[0],
467                         .map_idx = &actions_idx
468                 },
469                 {
470                         .str = "flag",
471                         .mask = FLOW_ACTION_MASK(
472                                 RTE_FLOW_ACTION_TYPE_FLAG
473                         ),
474                         .map = &flow_actions[0],
475                         .map_idx = &actions_idx
476                 },
477         };
478
479         static const struct option lgopts[] = {
480                 /* Control */
481                 { "help",                       0, 0, 0 },
482                 { "flows-count",                1, 0, 0 },
483                 { "dump-iterations",            0, 0, 0 },
484                 { "deletion-rate",              0, 0, 0 },
485                 { "dump-socket-mem",            0, 0, 0 },
486                 { "enable-fwd",                 0, 0, 0 },
487                 /* Attributes */
488                 { "ingress",                    0, 0, 0 },
489                 { "egress",                     0, 0, 0 },
490                 { "transfer",                   0, 0, 0 },
491                 { "group",                      1, 0, 0 },
492                 /* Items */
493                 { "ether",                      0, 0, 0 },
494                 { "vlan",                       0, 0, 0 },
495                 { "ipv4",                       0, 0, 0 },
496                 { "ipv6",                       0, 0, 0 },
497                 { "tcp",                        0, 0, 0 },
498                 { "udp",                        0, 0, 0 },
499                 { "vxlan",                      0, 0, 0 },
500                 { "vxlan-gpe",                  0, 0, 0 },
501                 { "gre",                        0, 0, 0 },
502                 { "geneve",                     0, 0, 0 },
503                 { "gtp",                        0, 0, 0 },
504                 { "meta",                       0, 0, 0 },
505                 { "tag",                        0, 0, 0 },
506                 /* Actions */
507                 { "port-id",                    0, 0, 0 },
508                 { "rss",                        0, 0, 0 },
509                 { "queue",                      0, 0, 0 },
510                 { "jump",                       0, 0, 0 },
511                 { "mark",                       0, 0, 0 },
512                 { "count",                      0, 0, 0 },
513                 { "set-meta",                   0, 0, 0 },
514                 { "set-tag",                    0, 0, 0 },
515                 { "drop",                       0, 0, 0 },
516                 { "hairpin-queue",              1, 0, 0 },
517                 { "hairpin-rss",                1, 0, 0 },
518                 { "set-src-mac",                0, 0, 0 },
519                 { "set-dst-mac",                0, 0, 0 },
520                 { "set-src-ipv4",               0, 0, 0 },
521                 { "set-dst-ipv4",               0, 0, 0 },
522                 { "set-src-ipv6",               0, 0, 0 },
523                 { "set-dst-ipv6",               0, 0, 0 },
524                 { "set-src-tp",                 0, 0, 0 },
525                 { "set-dst-tp",                 0, 0, 0 },
526                 { "inc-tcp-ack",                0, 0, 0 },
527                 { "dec-tcp-ack",                0, 0, 0 },
528                 { "inc-tcp-seq",                0, 0, 0 },
529                 { "dec-tcp-seq",                0, 0, 0 },
530                 { "set-ttl",                    0, 0, 0 },
531                 { "dec-ttl",                    0, 0, 0 },
532                 { "set-ipv4-dscp",              0, 0, 0 },
533                 { "set-ipv6-dscp",              0, 0, 0 },
534                 { "flag",                       0, 0, 0 },
535         };
536
537         hairpin_queues_num = 0;
538         argvopt = argv;
539
540         printf(":: Flow -> ");
541         while ((opt = getopt_long(argc, argvopt, "",
542                                 lgopts, &opt_idx)) != EOF) {
543                 switch (opt) {
544                 case 0:
545                         if (strcmp(lgopts[opt_idx].name, "help") == 0) {
546                                 usage(argv[0]);
547                                 rte_exit(EXIT_SUCCESS, "Displayed help\n");
548                         }
549
550                         if (strcmp(lgopts[opt_idx].name, "group") == 0) {
551                                 n = atoi(optarg);
552                                 if (n >= 0)
553                                         flow_group = n;
554                                 else
555                                         rte_exit(EXIT_SUCCESS,
556                                                 "flow group should be >= 0\n");
557                                 printf("group %d / ", flow_group);
558                         }
559
560                         for (i = 0; i < RTE_DIM(flow_options); i++)
561                                 if (strcmp(lgopts[opt_idx].name,
562                                                 flow_options[i].str) == 0) {
563                                         flow_options[i].map[
564                                         (*flow_options[i].map_idx)++] =
565                                                 flow_options[i].mask;
566                                         printf("%s / ", flow_options[i].str);
567                                 }
568
569                         if (strcmp(lgopts[opt_idx].name,
570                                         "hairpin-rss") == 0) {
571                                 n = atoi(optarg);
572                                 if (n > 0)
573                                         hairpin_queues_num = n;
574                                 else
575                                         rte_exit(EXIT_SUCCESS,
576                                                 "Hairpin queues should be > 0\n");
577
578                                 flow_actions[actions_idx++] =
579                                         HAIRPIN_RSS_ACTION;
580                                 printf("hairpin-rss / ");
581                         }
582                         if (strcmp(lgopts[opt_idx].name,
583                                         "hairpin-queue") == 0) {
584                                 n = atoi(optarg);
585                                 if (n > 0)
586                                         hairpin_queues_num = n;
587                                 else
588                                         rte_exit(EXIT_SUCCESS,
589                                                 "Hairpin queues should be > 0\n");
590
591                                 flow_actions[actions_idx++] =
592                                         HAIRPIN_QUEUE_ACTION;
593                                 printf("hairpin-queue / ");
594                         }
595
596                         /* Control */
597                         if (strcmp(lgopts[opt_idx].name,
598                                         "flows-count") == 0) {
599                                 n = atoi(optarg);
600                                 if (n > (int) iterations_number)
601                                         flows_count = n;
602                                 else {
603                                         printf("\n\nflows_count should be > %d\n",
604                                                 iterations_number);
605                                         rte_exit(EXIT_SUCCESS, " ");
606                                 }
607                         }
608                         if (strcmp(lgopts[opt_idx].name,
609                                         "dump-iterations") == 0)
610                                 dump_iterations = true;
611                         if (strcmp(lgopts[opt_idx].name,
612                                         "deletion-rate") == 0)
613                                 delete_flag = true;
614                         if (strcmp(lgopts[opt_idx].name,
615                                         "dump-socket-mem") == 0)
616                                 dump_socket_mem_flag = true;
617                         if (strcmp(lgopts[opt_idx].name,
618                                         "enable-fwd") == 0)
619                                 enable_fwd = true;
620                         break;
621                 default:
622                         fprintf(stderr, "Invalid option: %s\n", argv[optind]);
623                         usage(argv[0]);
624                         rte_exit(EXIT_SUCCESS, "Invalid option\n");
625                         break;
626                 }
627         }
628         printf("end_flow\n");
629 }
630
631 /* Dump the socket memory statistics on console */
632 static size_t
633 dump_socket_mem(FILE *f)
634 {
635         struct rte_malloc_socket_stats socket_stats;
636         unsigned int i = 0;
637         size_t total = 0;
638         size_t alloc = 0;
639         size_t free = 0;
640         unsigned int n_alloc = 0;
641         unsigned int n_free = 0;
642         bool active_nodes = false;
643
644
645         for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
646                 if (rte_malloc_get_socket_stats(i, &socket_stats) ||
647                     !socket_stats.heap_totalsz_bytes)
648                         continue;
649                 active_nodes = true;
650                 total += socket_stats.heap_totalsz_bytes;
651                 alloc += socket_stats.heap_allocsz_bytes;
652                 free += socket_stats.heap_freesz_bytes;
653                 n_alloc += socket_stats.alloc_count;
654                 n_free += socket_stats.free_count;
655                 if (dump_socket_mem_flag) {
656                         fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
657                         fprintf(f,
658                                 "\nSocket %u:\nsize(M) total: %.6lf\nalloc:"
659                                 " %.6lf(%.3lf%%)\nfree: %.6lf"
660                                 "\nmax: %.6lf"
661                                 "\ncount alloc: %u\nfree: %u\n",
662                                 i,
663                                 socket_stats.heap_totalsz_bytes / 1.0e6,
664                                 socket_stats.heap_allocsz_bytes / 1.0e6,
665                                 (double)socket_stats.heap_allocsz_bytes * 100 /
666                                 (double)socket_stats.heap_totalsz_bytes,
667                                 socket_stats.heap_freesz_bytes / 1.0e6,
668                                 socket_stats.greatest_free_size / 1.0e6,
669                                 socket_stats.alloc_count,
670                                 socket_stats.free_count);
671                                 fprintf(f, "::::::::::::::::::::::::::::::::::::::::");
672                 }
673         }
674         if (dump_socket_mem_flag && active_nodes) {
675                 fprintf(f,
676                         "\nTotal: size(M)\ntotal: %.6lf"
677                         "\nalloc: %.6lf(%.3lf%%)\nfree: %.6lf"
678                         "\ncount alloc: %u\nfree: %u\n",
679                         total / 1.0e6, alloc / 1.0e6,
680                         (double)alloc * 100 / (double)total, free / 1.0e6,
681                         n_alloc, n_free);
682                 fprintf(f, "::::::::::::::::::::::::::::::::::::::::\n");
683         }
684         return alloc;
685 }
686
687 static void
688 print_flow_error(struct rte_flow_error error)
689 {
690         printf("Flow can't be created %d message: %s\n",
691                 error.type,
692                 error.message ? error.message : "(no stated reason)");
693 }
694
695 static inline void
696 destroy_flows(int port_id, struct rte_flow **flow_list)
697 {
698         struct rte_flow_error error;
699         clock_t start_iter, end_iter;
700         double cpu_time_used = 0;
701         double flows_rate;
702         double cpu_time_per_iter[MAX_ITERATIONS];
703         double delta;
704         uint32_t i;
705         int iter_id;
706
707         for (i = 0; i < MAX_ITERATIONS; i++)
708                 cpu_time_per_iter[i] = -1;
709
710         if (iterations_number > flows_count)
711                 iterations_number = flows_count;
712
713         /* Deletion Rate */
714         printf("Flows Deletion on port = %d\n", port_id);
715         start_iter = clock();
716         for (i = 0; i < flows_count; i++) {
717                 if (flow_list[i] == 0)
718                         break;
719
720                 memset(&error, 0x33, sizeof(error));
721                 if (rte_flow_destroy(port_id, flow_list[i], &error)) {
722                         print_flow_error(error);
723                         rte_exit(EXIT_FAILURE, "Error in deleting flow");
724                 }
725
726                 if (i && !((i + 1) % iterations_number)) {
727                         /* Save the deletion rate of each iter */
728                         end_iter = clock();
729                         delta = (double) (end_iter - start_iter);
730                         iter_id = ((i + 1) / iterations_number) - 1;
731                         cpu_time_per_iter[iter_id] =
732                                 delta / CLOCKS_PER_SEC;
733                         cpu_time_used += cpu_time_per_iter[iter_id];
734                         start_iter = clock();
735                 }
736         }
737
738         /* Deletion rate per iteration */
739         if (dump_iterations)
740                 for (i = 0; i < MAX_ITERATIONS; i++) {
741                         if (cpu_time_per_iter[i] == -1)
742                                 continue;
743                         delta = (double)(iterations_number /
744                                 cpu_time_per_iter[i]);
745                         flows_rate = delta / 1000;
746                         printf(":: Iteration #%d: %d flows "
747                                 "in %f sec[ Rate = %f K/Sec ]\n",
748                                 i, iterations_number,
749                                 cpu_time_per_iter[i], flows_rate);
750                 }
751
752         /* Deletion rate for all flows */
753         flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
754         printf("\n:: Total flow deletion rate -> %f K/Sec\n",
755                 flows_rate);
756         printf(":: The time for deleting %d in flows %f seconds\n",
757                 flows_count, cpu_time_used);
758 }
759
760 static inline void
761 flows_handler(void)
762 {
763         struct rte_flow **flow_list;
764         struct rte_flow_error error;
765         clock_t start_iter, end_iter;
766         double cpu_time_used;
767         double flows_rate;
768         double cpu_time_per_iter[MAX_ITERATIONS];
769         double delta;
770         uint16_t nr_ports;
771         uint32_t i;
772         int port_id;
773         int iter_id;
774         uint32_t flow_index;
775         uint64_t global_items[MAX_ITEMS_NUM] = { 0 };
776         uint64_t global_actions[MAX_ACTIONS_NUM] = { 0 };
777
778         global_items[0] = FLOW_ITEM_MASK(RTE_FLOW_ITEM_TYPE_ETH);
779         global_actions[0] = FLOW_ITEM_MASK(RTE_FLOW_ACTION_TYPE_JUMP);
780
781         nr_ports = rte_eth_dev_count_avail();
782
783         for (i = 0; i < MAX_ITERATIONS; i++)
784                 cpu_time_per_iter[i] = -1;
785
786         if (iterations_number > flows_count)
787                 iterations_number = flows_count;
788
789         printf(":: Flows Count per port: %d\n", flows_count);
790
791         flow_list = rte_zmalloc("flow_list",
792                 (sizeof(struct rte_flow *) * flows_count) + 1, 0);
793         if (flow_list == NULL)
794                 rte_exit(EXIT_FAILURE, "No Memory available!");
795
796         for (port_id = 0; port_id < nr_ports; port_id++) {
797                 cpu_time_used = 0;
798                 flow_index = 0;
799                 if (flow_group > 0) {
800                         /*
801                          * Create global rule to jump into flow_group,
802                          * this way the app will avoid the default rules.
803                          *
804                          * Global rule:
805                          * group 0 eth / end actions jump group <flow_group>
806                          *
807                          */
808                         flow = generate_flow(port_id, 0, flow_attrs,
809                                 global_items, global_actions,
810                                 flow_group, 0, 0, &error);
811
812                         if (flow == NULL) {
813                                 print_flow_error(error);
814                                 rte_exit(EXIT_FAILURE, "error in creating flow");
815                         }
816                         flow_list[flow_index++] = flow;
817                 }
818
819                 /* Insertion Rate */
820                 printf("Flows insertion on port = %d\n", port_id);
821                 start_iter = clock();
822                 for (i = 0; i < flows_count; i++) {
823                         flow = generate_flow(port_id, flow_group,
824                                 flow_attrs, flow_items, flow_actions,
825                                 JUMP_ACTION_TABLE, i,
826                                 hairpin_queues_num, &error);
827
828                         if (force_quit)
829                                 i = flows_count;
830
831                         if (!flow) {
832                                 print_flow_error(error);
833                                 rte_exit(EXIT_FAILURE, "error in creating flow");
834                         }
835
836                         flow_list[flow_index++] = flow;
837
838                         if (i && !((i + 1) % iterations_number)) {
839                                 /* Save the insertion rate of each iter */
840                                 end_iter = clock();
841                                 delta = (double) (end_iter - start_iter);
842                                 iter_id = ((i + 1) / iterations_number) - 1;
843                                 cpu_time_per_iter[iter_id] =
844                                         delta / CLOCKS_PER_SEC;
845                                 cpu_time_used += cpu_time_per_iter[iter_id];
846                                 start_iter = clock();
847                         }
848                 }
849
850                 /* Iteration rate per iteration */
851                 if (dump_iterations)
852                         for (i = 0; i < MAX_ITERATIONS; i++) {
853                                 if (cpu_time_per_iter[i] == -1)
854                                         continue;
855                                 delta = (double)(iterations_number /
856                                         cpu_time_per_iter[i]);
857                                 flows_rate = delta / 1000;
858                                 printf(":: Iteration #%d: %d flows "
859                                         "in %f sec[ Rate = %f K/Sec ]\n",
860                                         i, iterations_number,
861                                         cpu_time_per_iter[i], flows_rate);
862                         }
863
864                 /* Insertion rate for all flows */
865                 flows_rate = ((double) (flows_count / cpu_time_used) / 1000);
866                 printf("\n:: Total flow insertion rate -> %f K/Sec\n",
867                                                 flows_rate);
868                 printf(":: The time for creating %d in flows %f seconds\n",
869                                                 flows_count, cpu_time_used);
870
871                 if (delete_flag)
872                         destroy_flows(port_id, flow_list);
873         }
874 }
875
876 static void
877 signal_handler(int signum)
878 {
879         if (signum == SIGINT || signum == SIGTERM) {
880                 printf("\n\nSignal %d received, preparing to exit...\n",
881                                         signum);
882                 printf("Error: Stats are wrong due to sudden signal!\n\n");
883                 force_quit = true;
884         }
885 }
886
887 static inline uint16_t
888 do_rx(struct lcore_info *li, uint16_t rx_port, uint16_t rx_queue)
889 {
890         uint16_t cnt = 0;
891         cnt = rte_eth_rx_burst(rx_port, rx_queue, li->pkts, MAX_PKT_BURST);
892         li->rx_pkts += cnt;
893         return cnt;
894 }
895
896 static inline void
897 do_tx(struct lcore_info *li, uint16_t cnt, uint16_t tx_port,
898                         uint16_t tx_queue)
899 {
900         uint16_t nr_tx = 0;
901         uint16_t i;
902
903         nr_tx = rte_eth_tx_burst(tx_port, tx_queue, li->pkts, cnt);
904         li->tx_pkts  += nr_tx;
905         li->tx_drops += cnt - nr_tx;
906
907         for (i = nr_tx; i < cnt; i++)
908                 rte_pktmbuf_free(li->pkts[i]);
909 }
910
911 /*
912  * Method to convert numbers into pretty numbers that easy
913  * to read. The design here is to add comma after each three
914  * digits and set all of this inside buffer.
915  *
916  * For example if n = 1799321, the output will be
917  * 1,799,321 after this method which is easier to read.
918  */
919 static char *
920 pretty_number(uint64_t n, char *buf)
921 {
922         char p[6][4];
923         int i = 0;
924         int off = 0;
925
926         while (n > 1000) {
927                 sprintf(p[i], "%03d", (int)(n % 1000));
928                 n /= 1000;
929                 i += 1;
930         }
931
932         sprintf(p[i++], "%d", (int)n);
933
934         while (i--)
935                 off += sprintf(buf + off, "%s,", p[i]);
936         buf[strlen(buf) - 1] = '\0';
937
938         return buf;
939 }
940
941 static void
942 packet_per_second_stats(void)
943 {
944         struct lcore_info *old;
945         struct lcore_info *li, *oli;
946         int nr_lines = 0;
947         int i;
948
949         old = rte_zmalloc("old",
950                 sizeof(struct lcore_info) * MAX_LCORES, 0);
951         if (old == NULL)
952                 rte_exit(EXIT_FAILURE, "No Memory available!");
953
954         memcpy(old, lcore_infos,
955                 sizeof(struct lcore_info) * MAX_LCORES);
956
957         while (!force_quit) {
958                 uint64_t total_tx_pkts = 0;
959                 uint64_t total_rx_pkts = 0;
960                 uint64_t total_tx_drops = 0;
961                 uint64_t tx_delta, rx_delta, drops_delta;
962                 char buf[3][32];
963                 int nr_valid_core = 0;
964
965                 sleep(1);
966
967                 if (nr_lines) {
968                         char go_up_nr_lines[16];
969
970                         sprintf(go_up_nr_lines, "%c[%dA\r", 27, nr_lines);
971                         printf("%s\r", go_up_nr_lines);
972                 }
973
974                 printf("\n%6s %16s %16s %16s\n", "core", "tx", "tx drops", "rx");
975                 printf("%6s %16s %16s %16s\n", "------", "----------------",
976                         "----------------", "----------------");
977                 nr_lines = 3;
978                 for (i = 0; i < MAX_LCORES; i++) {
979                         li  = &lcore_infos[i];
980                         oli = &old[i];
981                         if (li->mode != LCORE_MODE_PKT)
982                                 continue;
983
984                         tx_delta    = li->tx_pkts  - oli->tx_pkts;
985                         rx_delta    = li->rx_pkts  - oli->rx_pkts;
986                         drops_delta = li->tx_drops - oli->tx_drops;
987                         printf("%6d %16s %16s %16s\n", i,
988                                 pretty_number(tx_delta,    buf[0]),
989                                 pretty_number(drops_delta, buf[1]),
990                                 pretty_number(rx_delta,    buf[2]));
991
992                         total_tx_pkts  += tx_delta;
993                         total_rx_pkts  += rx_delta;
994                         total_tx_drops += drops_delta;
995
996                         nr_valid_core++;
997                         nr_lines += 1;
998                 }
999
1000                 if (nr_valid_core > 1) {
1001                         printf("%6s %16s %16s %16s\n", "total",
1002                                 pretty_number(total_tx_pkts,  buf[0]),
1003                                 pretty_number(total_tx_drops, buf[1]),
1004                                 pretty_number(total_rx_pkts,  buf[2]));
1005                         nr_lines += 1;
1006                 }
1007
1008                 memcpy(old, lcore_infos,
1009                         sizeof(struct lcore_info) * MAX_LCORES);
1010         }
1011 }
1012
1013 static int
1014 start_forwarding(void *data __rte_unused)
1015 {
1016         int lcore = rte_lcore_id();
1017         int stream_id;
1018         uint16_t cnt;
1019         struct lcore_info *li = &lcore_infos[lcore];
1020
1021         if (!li->mode)
1022                 return 0;
1023
1024         if (li->mode == LCORE_MODE_STATS) {
1025                 printf(":: started stats on lcore %u\n", lcore);
1026                 packet_per_second_stats();
1027                 return 0;
1028         }
1029
1030         while (!force_quit)
1031                 for (stream_id = 0; stream_id < MAX_STREAMS; stream_id++) {
1032                         if (li->streams[stream_id].rx_port == -1)
1033                                 continue;
1034
1035                         cnt = do_rx(li,
1036                                         li->streams[stream_id].rx_port,
1037                                         li->streams[stream_id].rx_queue);
1038                         if (cnt)
1039                                 do_tx(li, cnt,
1040                                         li->streams[stream_id].tx_port,
1041                                         li->streams[stream_id].tx_queue);
1042                 }
1043         return 0;
1044 }
1045
1046 static void
1047 init_lcore_info(void)
1048 {
1049         int i, j;
1050         unsigned int lcore;
1051         uint16_t nr_port;
1052         uint16_t queue;
1053         int port;
1054         int stream_id = 0;
1055         int streams_per_core;
1056         int unassigned_streams;
1057         int nb_fwd_streams;
1058         nr_port = rte_eth_dev_count_avail();
1059
1060         /* First logical core is reserved for stats printing */
1061         lcore = rte_get_next_lcore(-1, 0, 0);
1062         lcore_infos[lcore].mode = LCORE_MODE_STATS;
1063
1064         /*
1065          * Initialize all cores
1066          * All cores at first must have -1 value in all streams
1067          * This means that this stream is not used, or not set
1068          * yet.
1069          */
1070         for (i = 0; i < MAX_LCORES; i++)
1071                 for (j = 0; j < MAX_STREAMS; j++) {
1072                         lcore_infos[i].streams[j].tx_port = -1;
1073                         lcore_infos[i].streams[j].rx_port = -1;
1074                         lcore_infos[i].streams[j].tx_queue = -1;
1075                         lcore_infos[i].streams[j].rx_queue = -1;
1076                         lcore_infos[i].streams_nb = 0;
1077                 }
1078
1079         /*
1080          * Calculate the total streams count.
1081          * Also distribute those streams count between the available
1082          * logical cores except first core, since it's reserved for
1083          * stats prints.
1084          */
1085         nb_fwd_streams = nr_port * RXQ_NUM;
1086         if ((int)(nb_lcores - 1) >= nb_fwd_streams)
1087                 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1088                         lcore = rte_get_next_lcore(lcore, 0, 0);
1089                         lcore_infos[lcore].streams_nb = 1;
1090                 }
1091         else {
1092                 streams_per_core = nb_fwd_streams / (nb_lcores - 1);
1093                 unassigned_streams = nb_fwd_streams % (nb_lcores - 1);
1094                 for (i = 0; i < (int)(nb_lcores - 1); i++) {
1095                         lcore = rte_get_next_lcore(lcore, 0, 0);
1096                         lcore_infos[lcore].streams_nb = streams_per_core;
1097                         if (unassigned_streams) {
1098                                 lcore_infos[lcore].streams_nb++;
1099                                 unassigned_streams--;
1100                         }
1101                 }
1102         }
1103
1104         /*
1105          * Set the streams for the cores according to each logical
1106          * core stream count.
1107          * The streams is built on the design of what received should
1108          * forward as well, this means that if you received packets on
1109          * port 0 queue 0 then the same queue should forward the
1110          * packets, using the same logical core.
1111          */
1112         lcore = rte_get_next_lcore(-1, 0, 0);
1113         for (port = 0; port < nr_port; port++) {
1114                 /* Create FWD stream */
1115                 for (queue = 0; queue < RXQ_NUM; queue++) {
1116                         if (!lcore_infos[lcore].streams_nb ||
1117                                 !(stream_id % lcore_infos[lcore].streams_nb)) {
1118                                 lcore = rte_get_next_lcore(lcore, 0, 0);
1119                                 lcore_infos[lcore].mode = LCORE_MODE_PKT;
1120                                 stream_id = 0;
1121                         }
1122                         lcore_infos[lcore].streams[stream_id].rx_queue = queue;
1123                         lcore_infos[lcore].streams[stream_id].tx_queue = queue;
1124                         lcore_infos[lcore].streams[stream_id].rx_port = port;
1125                         lcore_infos[lcore].streams[stream_id].tx_port = port;
1126                         stream_id++;
1127                 }
1128         }
1129
1130         /* Print all streams */
1131         printf(":: Stream -> core id[N]: (rx_port, rx_queue)->(tx_port, tx_queue)\n");
1132         for (i = 0; i < MAX_LCORES; i++)
1133                 for (j = 0; j < MAX_STREAMS; j++) {
1134                         /* No streams for this core */
1135                         if (lcore_infos[i].streams[j].tx_port == -1)
1136                                 break;
1137                         printf("Stream -> core id[%d]: (%d,%d)->(%d,%d)\n",
1138                                 i,
1139                                 lcore_infos[i].streams[j].rx_port,
1140                                 lcore_infos[i].streams[j].rx_queue,
1141                                 lcore_infos[i].streams[j].tx_port,
1142                                 lcore_infos[i].streams[j].tx_queue);
1143                 }
1144 }
1145
1146 static void
1147 init_port(void)
1148 {
1149         int ret;
1150         uint16_t std_queue;
1151         uint16_t hairpin_queue;
1152         uint16_t port_id;
1153         uint16_t nr_ports;
1154         uint16_t nr_queues;
1155         struct rte_eth_hairpin_conf hairpin_conf = {
1156                 .peer_count = 1,
1157         };
1158         struct rte_eth_conf port_conf = {
1159                 .rx_adv_conf = {
1160                         .rss_conf.rss_hf =
1161                                 GET_RSS_HF(),
1162                 }
1163         };
1164         struct rte_eth_txconf txq_conf;
1165         struct rte_eth_rxconf rxq_conf;
1166         struct rte_eth_dev_info dev_info;
1167
1168         nr_queues = RXQ_NUM;
1169         if (hairpin_queues_num != 0)
1170                 nr_queues = RXQ_NUM + hairpin_queues_num;
1171
1172         nr_ports = rte_eth_dev_count_avail();
1173         if (nr_ports == 0)
1174                 rte_exit(EXIT_FAILURE, "Error: no port detected\n");
1175
1176         mbuf_mp = rte_pktmbuf_pool_create("mbuf_pool",
1177                                         TOTAL_MBUF_NUM, MBUF_CACHE_SIZE,
1178                                         0, MBUF_SIZE,
1179                                         rte_socket_id());
1180         if (mbuf_mp == NULL)
1181                 rte_exit(EXIT_FAILURE, "Error: can't init mbuf pool\n");
1182
1183         for (port_id = 0; port_id < nr_ports; port_id++) {
1184                 ret = rte_eth_dev_info_get(port_id, &dev_info);
1185                 if (ret != 0)
1186                         rte_exit(EXIT_FAILURE,
1187                                 "Error during getting device"
1188                                 " (port %u) info: %s\n",
1189                                 port_id, strerror(-ret));
1190
1191                 port_conf.txmode.offloads &= dev_info.tx_offload_capa;
1192                 port_conf.rxmode.offloads &= dev_info.rx_offload_capa;
1193
1194                 printf(":: initializing port: %d\n", port_id);
1195
1196                 ret = rte_eth_dev_configure(port_id, nr_queues,
1197                                 nr_queues, &port_conf);
1198                 if (ret < 0)
1199                         rte_exit(EXIT_FAILURE,
1200                                 ":: cannot configure device: err=%d, port=%u\n",
1201                                 ret, port_id);
1202
1203                 rxq_conf = dev_info.default_rxconf;
1204                 for (std_queue = 0; std_queue < RXQ_NUM; std_queue++) {
1205                         ret = rte_eth_rx_queue_setup(port_id, std_queue, NR_RXD,
1206                                         rte_eth_dev_socket_id(port_id),
1207                                         &rxq_conf,
1208                                         mbuf_mp);
1209                         if (ret < 0)
1210                                 rte_exit(EXIT_FAILURE,
1211                                         ":: Rx queue setup failed: err=%d, port=%u\n",
1212                                         ret, port_id);
1213                 }
1214
1215                 txq_conf = dev_info.default_txconf;
1216                 for (std_queue = 0; std_queue < TXQ_NUM; std_queue++) {
1217                         ret = rte_eth_tx_queue_setup(port_id, std_queue, NR_TXD,
1218                                         rte_eth_dev_socket_id(port_id),
1219                                         &txq_conf);
1220                         if (ret < 0)
1221                                 rte_exit(EXIT_FAILURE,
1222                                         ":: Tx queue setup failed: err=%d, port=%u\n",
1223                                         ret, port_id);
1224                 }
1225
1226                 /* Catch all packets from traffic generator. */
1227                 ret = rte_eth_promiscuous_enable(port_id);
1228                 if (ret != 0)
1229                         rte_exit(EXIT_FAILURE,
1230                                 ":: promiscuous mode enable failed: err=%s, port=%u\n",
1231                                 rte_strerror(-ret), port_id);
1232
1233                 if (hairpin_queues_num != 0) {
1234                         /*
1235                          * Configure peer which represents hairpin Tx.
1236                          * Hairpin queue numbers start after standard queues
1237                          * (RXQ_NUM and TXQ_NUM).
1238                          */
1239                         for (hairpin_queue = RXQ_NUM, std_queue = 0;
1240                                         hairpin_queue < nr_queues;
1241                                         hairpin_queue++, std_queue++) {
1242                                 hairpin_conf.peers[0].port = port_id;
1243                                 hairpin_conf.peers[0].queue =
1244                                         std_queue + TXQ_NUM;
1245                                 ret = rte_eth_rx_hairpin_queue_setup(
1246                                                 port_id, hairpin_queue,
1247                                                 NR_RXD, &hairpin_conf);
1248                                 if (ret != 0)
1249                                         rte_exit(EXIT_FAILURE,
1250                                                 ":: Hairpin rx queue setup failed: err=%d, port=%u\n",
1251                                                 ret, port_id);
1252                         }
1253
1254                         for (hairpin_queue = TXQ_NUM, std_queue = 0;
1255                                         hairpin_queue < nr_queues;
1256                                         hairpin_queue++, std_queue++) {
1257                                 hairpin_conf.peers[0].port = port_id;
1258                                 hairpin_conf.peers[0].queue =
1259                                         std_queue + RXQ_NUM;
1260                                 ret = rte_eth_tx_hairpin_queue_setup(
1261                                                 port_id, hairpin_queue,
1262                                                 NR_TXD, &hairpin_conf);
1263                                 if (ret != 0)
1264                                         rte_exit(EXIT_FAILURE,
1265                                                 ":: Hairpin tx queue setup failed: err=%d, port=%u\n",
1266                                                 ret, port_id);
1267                         }
1268                 }
1269
1270                 ret = rte_eth_dev_start(port_id);
1271                 if (ret < 0)
1272                         rte_exit(EXIT_FAILURE,
1273                                 "rte_eth_dev_start:err=%d, port=%u\n",
1274                                 ret, port_id);
1275
1276                 printf(":: initializing port: %d done\n", port_id);
1277         }
1278 }
1279
1280 int
1281 main(int argc, char **argv)
1282 {
1283         int ret;
1284         uint16_t port;
1285         struct rte_flow_error error;
1286         int64_t alloc, last_alloc;
1287
1288         ret = rte_eal_init(argc, argv);
1289         if (ret < 0)
1290                 rte_exit(EXIT_FAILURE, "EAL init failed\n");
1291
1292         force_quit = false;
1293         dump_iterations = false;
1294         flows_count = DEFAULT_RULES_COUNT;
1295         iterations_number = DEFAULT_ITERATION;
1296         delete_flag = false;
1297         dump_socket_mem_flag = false;
1298         flow_group = 0;
1299
1300         signal(SIGINT, signal_handler);
1301         signal(SIGTERM, signal_handler);
1302
1303         argc -= ret;
1304         argv += ret;
1305         if (argc > 1)
1306                 args_parse(argc, argv);
1307
1308         init_port();
1309
1310         nb_lcores = rte_lcore_count();
1311         if (nb_lcores <= 1)
1312                 rte_exit(EXIT_FAILURE, "This app needs at least two cores\n");
1313
1314         last_alloc = (int64_t)dump_socket_mem(stdout);
1315         flows_handler();
1316         alloc = (int64_t)dump_socket_mem(stdout);
1317
1318         if (last_alloc)
1319                 fprintf(stdout, ":: Memory allocation change(M): %.6lf\n",
1320                 (alloc - last_alloc) / 1.0e6);
1321
1322         if (enable_fwd) {
1323                 init_lcore_info();
1324                 rte_eal_mp_remote_launch(start_forwarding, NULL, CALL_MASTER);
1325         }
1326
1327         RTE_ETH_FOREACH_DEV(port) {
1328                 rte_flow_flush(port, &error);
1329                 rte_eth_dev_stop(port);
1330                 rte_eth_dev_close(port);
1331         }
1332         return 0;
1333 }