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