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