examples/ip_pipeline: support RSS
[dpdk.git] / examples / ip_pipeline / init.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <inttypes.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include <rte_cycles.h>
39 #include <rte_ethdev.h>
40 #include <rte_ether.h>
41 #include <rte_ip.h>
42 #include <rte_eal.h>
43 #include <rte_malloc.h>
44
45 #include "app.h"
46 #include "pipeline.h"
47 #include "pipeline_common_fe.h"
48 #include "pipeline_master.h"
49 #include "pipeline_passthrough.h"
50 #include "pipeline_firewall.h"
51 #include "pipeline_flow_classification.h"
52 #include "pipeline_flow_actions.h"
53 #include "pipeline_routing.h"
54 #include "thread_fe.h"
55
56 #define APP_NAME_SIZE   32
57
58 #define APP_RETA_SIZE_MAX     (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE)
59
60 static void
61 app_init_core_map(struct app_params *app)
62 {
63         APP_LOG(app, HIGH, "Initializing CPU core map ...");
64         app->core_map = cpu_core_map_init(4, 32, 4, 0);
65
66         if (app->core_map == NULL)
67                 rte_panic("Cannot create CPU core map\n");
68
69         if (app->log_level >= APP_LOG_LEVEL_LOW)
70                 cpu_core_map_print(app->core_map);
71 }
72
73 static void
74 app_init_core_mask(struct app_params *app)
75 {
76         uint64_t mask = 0;
77         uint32_t i;
78
79         for (i = 0; i < app->n_pipelines; i++) {
80                 struct app_pipeline_params *p = &app->pipeline_params[i];
81                 int lcore_id;
82
83                 lcore_id = cpu_core_map_get_lcore_id(app->core_map,
84                         p->socket_id,
85                         p->core_id,
86                         p->hyper_th_id);
87
88                 if (lcore_id < 0)
89                         rte_panic("Cannot create CPU core mask\n");
90
91                 mask |= 1LLU << lcore_id;
92         }
93
94         app->core_mask = mask;
95         APP_LOG(app, HIGH, "CPU core mask = 0x%016" PRIx64, app->core_mask);
96 }
97
98 static void
99 app_init_eal(struct app_params *app)
100 {
101         char buffer[256];
102         struct app_eal_params *p = &app->eal_params;
103         uint32_t n_args = 0;
104         uint32_t i;
105         int status;
106
107         app->eal_argv[n_args++] = strdup(app->app_name);
108
109         snprintf(buffer, sizeof(buffer), "-c%" PRIx64, app->core_mask);
110         app->eal_argv[n_args++] = strdup(buffer);
111
112         if (p->coremap) {
113                 snprintf(buffer, sizeof(buffer), "--lcores=%s", p->coremap);
114                 app->eal_argv[n_args++] = strdup(buffer);
115         }
116
117         if (p->master_lcore_present) {
118                 snprintf(buffer,
119                         sizeof(buffer),
120                         "--master-lcore=%" PRIu32,
121                         p->master_lcore);
122                 app->eal_argv[n_args++] = strdup(buffer);
123         }
124
125         snprintf(buffer, sizeof(buffer), "-n%" PRIu32, p->channels);
126         app->eal_argv[n_args++] = strdup(buffer);
127
128         if (p->memory_present) {
129                 snprintf(buffer, sizeof(buffer), "-m%" PRIu32, p->memory);
130                 app->eal_argv[n_args++] = strdup(buffer);
131         }
132
133         if (p->ranks_present) {
134                 snprintf(buffer, sizeof(buffer), "-r%" PRIu32, p->ranks);
135                 app->eal_argv[n_args++] = strdup(buffer);
136         }
137
138         for (i = 0; i < APP_MAX_LINKS; i++) {
139                 if (p->pci_blacklist[i] == NULL)
140                         break;
141
142                 snprintf(buffer,
143                         sizeof(buffer),
144                         "--pci-blacklist=%s",
145                         p->pci_blacklist[i]);
146                 app->eal_argv[n_args++] = strdup(buffer);
147         }
148
149         if (app->port_mask != 0)
150                 for (i = 0; i < APP_MAX_LINKS; i++) {
151                         if (p->pci_whitelist[i] == NULL)
152                                 break;
153
154                         snprintf(buffer,
155                                 sizeof(buffer),
156                                 "--pci-whitelist=%s",
157                                 p->pci_whitelist[i]);
158                         app->eal_argv[n_args++] = strdup(buffer);
159                 }
160         else
161                 for (i = 0; i < app->n_links; i++) {
162                         char *pci_bdf = app->link_params[i].pci_bdf;
163
164                         snprintf(buffer,
165                                 sizeof(buffer),
166                                 "--pci-whitelist=%s",
167                                 pci_bdf);
168                         app->eal_argv[n_args++] = strdup(buffer);
169                 }
170
171         for (i = 0; i < APP_MAX_LINKS; i++) {
172                 if (p->vdev[i] == NULL)
173                         break;
174
175                 snprintf(buffer,
176                         sizeof(buffer),
177                         "--vdev=%s",
178                         p->vdev[i]);
179                 app->eal_argv[n_args++] = strdup(buffer);
180         }
181
182         if ((p->vmware_tsc_map_present) && p->vmware_tsc_map) {
183                 snprintf(buffer, sizeof(buffer), "--vmware-tsc-map");
184                 app->eal_argv[n_args++] = strdup(buffer);
185         }
186
187         if (p->proc_type) {
188                 snprintf(buffer,
189                         sizeof(buffer),
190                         "--proc-type=%s",
191                         p->proc_type);
192                 app->eal_argv[n_args++] = strdup(buffer);
193         }
194
195         if (p->syslog) {
196                 snprintf(buffer, sizeof(buffer), "--syslog=%s", p->syslog);
197                 app->eal_argv[n_args++] = strdup(buffer);
198         }
199
200         if (p->log_level_present) {
201                 snprintf(buffer,
202                         sizeof(buffer),
203                         "--log-level=%" PRIu32,
204                         p->log_level);
205                 app->eal_argv[n_args++] = strdup(buffer);
206         }
207
208         if ((p->version_present) && p->version) {
209                 snprintf(buffer, sizeof(buffer), "-v");
210                 app->eal_argv[n_args++] = strdup(buffer);
211         }
212
213         if ((p->help_present) && p->help) {
214                 snprintf(buffer, sizeof(buffer), "--help");
215                 app->eal_argv[n_args++] = strdup(buffer);
216         }
217
218         if ((p->no_huge_present) && p->no_huge) {
219                 snprintf(buffer, sizeof(buffer), "--no-huge");
220                 app->eal_argv[n_args++] = strdup(buffer);
221         }
222
223         if ((p->no_pci_present) && p->no_pci) {
224                 snprintf(buffer, sizeof(buffer), "--no-pci");
225                 app->eal_argv[n_args++] = strdup(buffer);
226         }
227
228         if ((p->no_hpet_present) && p->no_hpet) {
229                 snprintf(buffer, sizeof(buffer), "--no-hpet");
230                 app->eal_argv[n_args++] = strdup(buffer);
231         }
232
233         if ((p->no_shconf_present) && p->no_shconf) {
234                 snprintf(buffer, sizeof(buffer), "--no-shconf");
235                 app->eal_argv[n_args++] = strdup(buffer);
236         }
237
238         if (p->add_driver) {
239                 snprintf(buffer, sizeof(buffer), "-d=%s", p->add_driver);
240                 app->eal_argv[n_args++] = strdup(buffer);
241         }
242
243         if (p->socket_mem) {
244                 snprintf(buffer,
245                         sizeof(buffer),
246                         "--socket-mem=%s",
247                         p->socket_mem);
248                 app->eal_argv[n_args++] = strdup(buffer);
249         }
250
251         if (p->huge_dir) {
252                 snprintf(buffer, sizeof(buffer), "--huge-dir=%s", p->huge_dir);
253                 app->eal_argv[n_args++] = strdup(buffer);
254         }
255
256         if (p->file_prefix) {
257                 snprintf(buffer,
258                         sizeof(buffer),
259                         "--file-prefix=%s",
260                         p->file_prefix);
261                 app->eal_argv[n_args++] = strdup(buffer);
262         }
263
264         if (p->base_virtaddr) {
265                 snprintf(buffer,
266                         sizeof(buffer),
267                         "--base-virtaddr=%s",
268                         p->base_virtaddr);
269                 app->eal_argv[n_args++] = strdup(buffer);
270         }
271
272         if ((p->create_uio_dev_present) && p->create_uio_dev) {
273                 snprintf(buffer, sizeof(buffer), "--create-uio-dev");
274                 app->eal_argv[n_args++] = strdup(buffer);
275         }
276
277         if (p->vfio_intr) {
278                 snprintf(buffer,
279                         sizeof(buffer),
280                         "--vfio-intr=%s",
281                         p->vfio_intr);
282                 app->eal_argv[n_args++] = strdup(buffer);
283         }
284
285         if ((p->xen_dom0_present) && (p->xen_dom0)) {
286                 snprintf(buffer, sizeof(buffer), "--xen-dom0");
287                 app->eal_argv[n_args++] = strdup(buffer);
288         }
289
290         snprintf(buffer, sizeof(buffer), "--");
291         app->eal_argv[n_args++] = strdup(buffer);
292
293         app->eal_argc = n_args;
294
295         APP_LOG(app, HIGH, "Initializing EAL ...");
296         if (app->log_level >= APP_LOG_LEVEL_LOW) {
297                 int i;
298
299                 fprintf(stdout, "[APP] EAL arguments: \"");
300                 for (i = 1; i < app->eal_argc; i++)
301                         fprintf(stdout, "%s ", app->eal_argv[i]);
302                 fprintf(stdout, "\"\n");
303         }
304
305         status = rte_eal_init(app->eal_argc, app->eal_argv);
306         if (status < 0)
307                 rte_panic("EAL init error\n");
308 }
309
310 static void
311 app_init_mempool(struct app_params *app)
312 {
313         uint32_t i;
314
315         for (i = 0; i < app->n_mempools; i++) {
316                 struct app_mempool_params *p = &app->mempool_params[i];
317
318                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
319                 app->mempool[i] = rte_mempool_create(
320                                 p->name,
321                                 p->pool_size,
322                                 p->buffer_size,
323                                 p->cache_size,
324                                 sizeof(struct rte_pktmbuf_pool_private),
325                                 rte_pktmbuf_pool_init, NULL,
326                                 rte_pktmbuf_init, NULL,
327                                 p->cpu_socket_id,
328                                 0);
329
330                 if (app->mempool[i] == NULL)
331                         rte_panic("%s init error\n", p->name);
332         }
333 }
334
335 static inline int
336 app_link_filter_arp_add(struct app_link_params *link)
337 {
338         struct rte_eth_ethertype_filter filter = {
339                 .ether_type = ETHER_TYPE_ARP,
340                 .flags = 0,
341                 .queue = link->arp_q,
342         };
343
344         return rte_eth_dev_filter_ctrl(link->pmd_id,
345                 RTE_ETH_FILTER_ETHERTYPE,
346                 RTE_ETH_FILTER_ADD,
347                 &filter);
348 }
349
350 static inline int
351 app_link_filter_tcp_syn_add(struct app_link_params *link)
352 {
353         struct rte_eth_syn_filter filter = {
354                 .hig_pri = 1,
355                 .queue = link->tcp_syn_q,
356         };
357
358         return rte_eth_dev_filter_ctrl(link->pmd_id,
359                 RTE_ETH_FILTER_SYN,
360                 RTE_ETH_FILTER_ADD,
361                 &filter);
362 }
363
364 static inline int
365 app_link_filter_ip_add(struct app_link_params *l1, struct app_link_params *l2)
366 {
367         struct rte_eth_ntuple_filter filter = {
368                 .flags = RTE_5TUPLE_FLAGS,
369                 .dst_ip = rte_bswap32(l2->ip),
370                 .dst_ip_mask = UINT32_MAX, /* Enable */
371                 .src_ip = 0,
372                 .src_ip_mask = 0, /* Disable */
373                 .dst_port = 0,
374                 .dst_port_mask = 0, /* Disable */
375                 .src_port = 0,
376                 .src_port_mask = 0, /* Disable */
377                 .proto = 0,
378                 .proto_mask = 0, /* Disable */
379                 .tcp_flags = 0,
380                 .priority = 1, /* Lowest */
381                 .queue = l1->ip_local_q,
382         };
383
384         return rte_eth_dev_filter_ctrl(l1->pmd_id,
385                 RTE_ETH_FILTER_NTUPLE,
386                 RTE_ETH_FILTER_ADD,
387                 &filter);
388 }
389
390 static inline int
391 app_link_filter_ip_del(struct app_link_params *l1, struct app_link_params *l2)
392 {
393         struct rte_eth_ntuple_filter filter = {
394                 .flags = RTE_5TUPLE_FLAGS,
395                 .dst_ip = rte_bswap32(l2->ip),
396                 .dst_ip_mask = UINT32_MAX, /* Enable */
397                 .src_ip = 0,
398                 .src_ip_mask = 0, /* Disable */
399                 .dst_port = 0,
400                 .dst_port_mask = 0, /* Disable */
401                 .src_port = 0,
402                 .src_port_mask = 0, /* Disable */
403                 .proto = 0,
404                 .proto_mask = 0, /* Disable */
405                 .tcp_flags = 0,
406                 .priority = 1, /* Lowest */
407                 .queue = l1->ip_local_q,
408         };
409
410         return rte_eth_dev_filter_ctrl(l1->pmd_id,
411                 RTE_ETH_FILTER_NTUPLE,
412                 RTE_ETH_FILTER_DELETE,
413                 &filter);
414 }
415
416 static inline int
417 app_link_filter_tcp_add(struct app_link_params *l1, struct app_link_params *l2)
418 {
419         struct rte_eth_ntuple_filter filter = {
420                 .flags = RTE_5TUPLE_FLAGS,
421                 .dst_ip = rte_bswap32(l2->ip),
422                 .dst_ip_mask = UINT32_MAX, /* Enable */
423                 .src_ip = 0,
424                 .src_ip_mask = 0, /* Disable */
425                 .dst_port = 0,
426                 .dst_port_mask = 0, /* Disable */
427                 .src_port = 0,
428                 .src_port_mask = 0, /* Disable */
429                 .proto = IPPROTO_TCP,
430                 .proto_mask = UINT8_MAX, /* Enable */
431                 .tcp_flags = 0,
432                 .priority = 2, /* Higher priority than IP */
433                 .queue = l1->tcp_local_q,
434         };
435
436         return rte_eth_dev_filter_ctrl(l1->pmd_id,
437                 RTE_ETH_FILTER_NTUPLE,
438                 RTE_ETH_FILTER_ADD,
439                 &filter);
440 }
441
442 static inline int
443 app_link_filter_tcp_del(struct app_link_params *l1, struct app_link_params *l2)
444 {
445         struct rte_eth_ntuple_filter filter = {
446                 .flags = RTE_5TUPLE_FLAGS,
447                 .dst_ip = rte_bswap32(l2->ip),
448                 .dst_ip_mask = UINT32_MAX, /* Enable */
449                 .src_ip = 0,
450                 .src_ip_mask = 0, /* Disable */
451                 .dst_port = 0,
452                 .dst_port_mask = 0, /* Disable */
453                 .src_port = 0,
454                 .src_port_mask = 0, /* Disable */
455                 .proto = IPPROTO_TCP,
456                 .proto_mask = UINT8_MAX, /* Enable */
457                 .tcp_flags = 0,
458                 .priority = 2, /* Higher priority than IP */
459                 .queue = l1->tcp_local_q,
460         };
461
462         return rte_eth_dev_filter_ctrl(l1->pmd_id,
463                 RTE_ETH_FILTER_NTUPLE,
464                 RTE_ETH_FILTER_DELETE,
465                 &filter);
466 }
467
468 static inline int
469 app_link_filter_udp_add(struct app_link_params *l1, struct app_link_params *l2)
470 {
471         struct rte_eth_ntuple_filter filter = {
472                 .flags = RTE_5TUPLE_FLAGS,
473                 .dst_ip = rte_bswap32(l2->ip),
474                 .dst_ip_mask = UINT32_MAX, /* Enable */
475                 .src_ip = 0,
476                 .src_ip_mask = 0, /* Disable */
477                 .dst_port = 0,
478                 .dst_port_mask = 0, /* Disable */
479                 .src_port = 0,
480                 .src_port_mask = 0, /* Disable */
481                 .proto = IPPROTO_UDP,
482                 .proto_mask = UINT8_MAX, /* Enable */
483                 .tcp_flags = 0,
484                 .priority = 2, /* Higher priority than IP */
485                 .queue = l1->udp_local_q,
486         };
487
488         return rte_eth_dev_filter_ctrl(l1->pmd_id,
489                 RTE_ETH_FILTER_NTUPLE,
490                 RTE_ETH_FILTER_ADD,
491                 &filter);
492 }
493
494 static inline int
495 app_link_filter_udp_del(struct app_link_params *l1, struct app_link_params *l2)
496 {
497         struct rte_eth_ntuple_filter filter = {
498                 .flags = RTE_5TUPLE_FLAGS,
499                 .dst_ip = rte_bswap32(l2->ip),
500                 .dst_ip_mask = UINT32_MAX, /* Enable */
501                 .src_ip = 0,
502                 .src_ip_mask = 0, /* Disable */
503                 .dst_port = 0,
504                 .dst_port_mask = 0, /* Disable */
505                 .src_port = 0,
506                 .src_port_mask = 0, /* Disable */
507                 .proto = IPPROTO_UDP,
508                 .proto_mask = UINT8_MAX, /* Enable */
509                 .tcp_flags = 0,
510                 .priority = 2, /* Higher priority than IP */
511                 .queue = l1->udp_local_q,
512         };
513
514         return rte_eth_dev_filter_ctrl(l1->pmd_id,
515                 RTE_ETH_FILTER_NTUPLE,
516                 RTE_ETH_FILTER_DELETE,
517                 &filter);
518 }
519
520 static inline int
521 app_link_filter_sctp_add(struct app_link_params *l1, struct app_link_params *l2)
522 {
523         struct rte_eth_ntuple_filter filter = {
524                 .flags = RTE_5TUPLE_FLAGS,
525                 .dst_ip = rte_bswap32(l2->ip),
526                 .dst_ip_mask = UINT32_MAX, /* Enable */
527                 .src_ip = 0,
528                 .src_ip_mask = 0, /* Disable */
529                 .dst_port = 0,
530                 .dst_port_mask = 0, /* Disable */
531                 .src_port = 0,
532                 .src_port_mask = 0, /* Disable */
533                 .proto = IPPROTO_SCTP,
534                 .proto_mask = UINT8_MAX, /* Enable */
535                 .tcp_flags = 0,
536                 .priority = 2, /* Higher priority than IP */
537                 .queue = l1->sctp_local_q,
538         };
539
540         return rte_eth_dev_filter_ctrl(l1->pmd_id,
541                 RTE_ETH_FILTER_NTUPLE,
542                 RTE_ETH_FILTER_ADD,
543                 &filter);
544 }
545
546 static inline int
547 app_link_filter_sctp_del(struct app_link_params *l1, struct app_link_params *l2)
548 {
549         struct rte_eth_ntuple_filter filter = {
550                 .flags = RTE_5TUPLE_FLAGS,
551                 .dst_ip = rte_bswap32(l2->ip),
552                 .dst_ip_mask = UINT32_MAX, /* Enable */
553                 .src_ip = 0,
554                 .src_ip_mask = 0, /* Disable */
555                 .dst_port = 0,
556                 .dst_port_mask = 0, /* Disable */
557                 .src_port = 0,
558                 .src_port_mask = 0, /* Disable */
559                 .proto = IPPROTO_SCTP,
560                 .proto_mask = UINT8_MAX, /* Enable */
561                 .tcp_flags = 0,
562                 .priority = 2, /* Higher priority than IP */
563                 .queue = l1->sctp_local_q,
564         };
565
566         return rte_eth_dev_filter_ctrl(l1->pmd_id,
567                 RTE_ETH_FILTER_NTUPLE,
568                 RTE_ETH_FILTER_DELETE,
569                 &filter);
570 }
571
572 static void
573 app_link_set_arp_filter(struct app_params *app, struct app_link_params *cp)
574 {
575         if (cp->arp_q != 0) {
576                 int status = app_link_filter_arp_add(cp);
577
578                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
579                         "Adding ARP filter (queue = %" PRIu32 ")",
580                         cp->name, cp->pmd_id, cp->arp_q);
581
582                 if (status)
583                         rte_panic("%s (%" PRIu32 "): "
584                                 "Error adding ARP filter "
585                                 "(queue = %" PRIu32 ") (%" PRId32 ")\n",
586                                 cp->name, cp->pmd_id, cp->arp_q, status);
587         }
588 }
589
590 static void
591 app_link_set_tcp_syn_filter(struct app_params *app, struct app_link_params *cp)
592 {
593         if (cp->tcp_syn_q != 0) {
594                 int status = app_link_filter_tcp_syn_add(cp);
595
596                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
597                         "Adding TCP SYN filter (queue = %" PRIu32 ")",
598                         cp->name, cp->pmd_id, cp->tcp_syn_q);
599
600                 if (status)
601                         rte_panic("%s (%" PRIu32 "): "
602                                 "Error adding TCP SYN filter "
603                                 "(queue = %" PRIu32 ") (%" PRId32 ")\n",
604                                 cp->name, cp->pmd_id, cp->tcp_syn_q,
605                                 status);
606         }
607 }
608
609 static int
610 app_link_is_virtual(struct app_link_params *p)
611 {
612         uint32_t pmd_id = p->pmd_id;
613         struct rte_eth_dev *dev = &rte_eth_devices[pmd_id];
614
615         if (dev->dev_type == RTE_ETH_DEV_VIRTUAL)
616                 return 1;
617
618         return 0;
619 }
620
621 void
622 app_link_up_internal(struct app_params *app, struct app_link_params *cp)
623 {
624         uint32_t i;
625         int status;
626
627         if (app_link_is_virtual(cp)) {
628                 cp->state = 1;
629                 return;
630         }
631
632         /* For each link, add filters for IP of current link */
633         if (cp->ip != 0) {
634                 for (i = 0; i < app->n_links; i++) {
635                         struct app_link_params *p = &app->link_params[i];
636
637                         /* IP */
638                         if (p->ip_local_q != 0) {
639                                 int status = app_link_filter_ip_add(p, cp);
640
641                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
642                                         "Adding IP filter (queue= %" PRIu32
643                                         ", IP = 0x%08" PRIx32 ")",
644                                         p->name, p->pmd_id, p->ip_local_q,
645                                         cp->ip);
646
647                                 if (status)
648                                         rte_panic("%s (%" PRIu32 "): "
649                                                 "Error adding IP "
650                                                 "filter (queue= %" PRIu32 ", "
651                                                 "IP = 0x%08" PRIx32
652                                                 ") (%" PRId32 ")\n",
653                                                 p->name, p->pmd_id,
654                                                 p->ip_local_q, cp->ip, status);
655                         }
656
657                         /* TCP */
658                         if (p->tcp_local_q != 0) {
659                                 int status = app_link_filter_tcp_add(p, cp);
660
661                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
662                                         "Adding TCP filter "
663                                         "(queue = %" PRIu32
664                                         ", IP = 0x%08" PRIx32 ")",
665                                         p->name, p->pmd_id, p->tcp_local_q,
666                                         cp->ip);
667
668                                 if (status)
669                                         rte_panic("%s (%" PRIu32 "): "
670                                                 "Error adding TCP "
671                                                 "filter (queue = %" PRIu32 ", "
672                                                 "IP = 0x%08" PRIx32
673                                                 ") (%" PRId32 ")\n",
674                                                 p->name, p->pmd_id,
675                                                 p->tcp_local_q, cp->ip, status);
676                         }
677
678                         /* UDP */
679                         if (p->udp_local_q != 0) {
680                                 int status = app_link_filter_udp_add(p, cp);
681
682                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
683                                         "Adding UDP filter "
684                                         "(queue = %" PRIu32
685                                         ", IP = 0x%08" PRIx32 ")",
686                                         p->name, p->pmd_id, p->udp_local_q,
687                                         cp->ip);
688
689                                 if (status)
690                                         rte_panic("%s (%" PRIu32 "): "
691                                                 "Error adding UDP "
692                                                 "filter (queue = %" PRIu32 ", "
693                                                 "IP = 0x%08" PRIx32
694                                                 ") (%" PRId32 ")\n",
695                                                 p->name, p->pmd_id,
696                                                 p->udp_local_q, cp->ip, status);
697                         }
698
699                         /* SCTP */
700                         if (p->sctp_local_q != 0) {
701                                 int status = app_link_filter_sctp_add(p, cp);
702
703                                 APP_LOG(app, LOW, "%s (%" PRIu32
704                                         "): Adding SCTP filter "
705                                         "(queue = %" PRIu32
706                                         ", IP = 0x%08" PRIx32 ")",
707                                         p->name, p->pmd_id, p->sctp_local_q,
708                                         cp->ip);
709
710                                 if (status)
711                                         rte_panic("%s (%" PRIu32 "): "
712                                                 "Error adding SCTP "
713                                                 "filter (queue = %" PRIu32 ", "
714                                                 "IP = 0x%08" PRIx32
715                                                 ") (%" PRId32 ")\n",
716                                                 p->name, p->pmd_id,
717                                                 p->sctp_local_q, cp->ip,
718                                                 status);
719                         }
720                 }
721         }
722
723         /* PMD link up */
724         status = rte_eth_dev_set_link_up(cp->pmd_id);
725         if (status < 0)
726                 rte_panic("%s (%" PRIu32 "): PMD set link up error %"
727                         PRId32 "\n", cp->name, cp->pmd_id, status);
728
729         /* Mark link as UP */
730         cp->state = 1;
731 }
732
733 void
734 app_link_down_internal(struct app_params *app, struct app_link_params *cp)
735 {
736         uint32_t i;
737         int status;
738
739         if (app_link_is_virtual(cp)) {
740                 cp->state = 0;
741                 return;
742         }
743
744         /* PMD link down */
745         status = rte_eth_dev_set_link_down(cp->pmd_id);
746         if (status < 0)
747                 rte_panic("%s (%" PRIu32 "): PMD set link down error %"
748                         PRId32 "\n", cp->name, cp->pmd_id, status);
749
750         /* Mark link as DOWN */
751         cp->state = 0;
752
753         /* Return if current link IP is not valid */
754         if (cp->ip == 0)
755                 return;
756
757         /* For each link, remove filters for IP of current link */
758         for (i = 0; i < app->n_links; i++) {
759                 struct app_link_params *p = &app->link_params[i];
760
761                 /* IP */
762                 if (p->ip_local_q != 0) {
763                         int status = app_link_filter_ip_del(p, cp);
764
765                         APP_LOG(app, LOW, "%s (%" PRIu32
766                                 "): Deleting IP filter "
767                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
768                                 p->name, p->pmd_id, p->ip_local_q, cp->ip);
769
770                         if (status)
771                                 rte_panic("%s (%" PRIu32
772                                         "): Error deleting IP filter "
773                                         "(queue = %" PRIu32
774                                         ", IP = 0x%" PRIx32
775                                         ") (%" PRId32 ")\n",
776                                         p->name, p->pmd_id, p->ip_local_q,
777                                         cp->ip, status);
778                 }
779
780                 /* TCP */
781                 if (p->tcp_local_q != 0) {
782                         int status = app_link_filter_tcp_del(p, cp);
783
784                         APP_LOG(app, LOW, "%s (%" PRIu32
785                                 "): Deleting TCP filter "
786                                 "(queue = %" PRIu32
787                                 ", IP = 0x%" PRIx32 ")",
788                                 p->name, p->pmd_id, p->tcp_local_q, cp->ip);
789
790                         if (status)
791                                 rte_panic("%s (%" PRIu32
792                                         "): Error deleting TCP filter "
793                                         "(queue = %" PRIu32
794                                         ", IP = 0x%" PRIx32
795                                         ") (%" PRId32 ")\n",
796                                         p->name, p->pmd_id, p->tcp_local_q,
797                                         cp->ip, status);
798                 }
799
800                 /* UDP */
801                 if (p->udp_local_q != 0) {
802                         int status = app_link_filter_udp_del(p, cp);
803
804                         APP_LOG(app, LOW, "%s (%" PRIu32
805                                 "): Deleting UDP filter "
806                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
807                                 p->name, p->pmd_id, p->udp_local_q, cp->ip);
808
809                         if (status)
810                                 rte_panic("%s (%" PRIu32
811                                         "): Error deleting UDP filter "
812                                         "(queue = %" PRIu32
813                                         ", IP = 0x%" PRIx32
814                                         ") (%" PRId32 ")\n",
815                                         p->name, p->pmd_id, p->udp_local_q,
816                                         cp->ip, status);
817                 }
818
819                 /* SCTP */
820                 if (p->sctp_local_q != 0) {
821                         int status = app_link_filter_sctp_del(p, cp);
822
823                         APP_LOG(app, LOW, "%s (%" PRIu32
824                                 "): Deleting SCTP filter "
825                                 "(queue = %" PRIu32
826                                 ", IP = 0x%" PRIx32 ")",
827                                 p->name, p->pmd_id, p->sctp_local_q, cp->ip);
828
829                         if (status)
830                                 rte_panic("%s (%" PRIu32
831                                         "): Error deleting SCTP filter "
832                                         "(queue = %" PRIu32
833                                         ", IP = 0x%" PRIx32
834                                         ") (%" PRId32 ")\n",
835                                         p->name, p->pmd_id, p->sctp_local_q,
836                                         cp->ip, status);
837                 }
838         }
839 }
840
841 static void
842 app_check_link(struct app_params *app)
843 {
844         uint32_t all_links_up, i;
845
846         all_links_up = 1;
847
848         for (i = 0; i < app->n_links; i++) {
849                 struct app_link_params *p = &app->link_params[i];
850                 struct rte_eth_link link_params;
851
852                 memset(&link_params, 0, sizeof(link_params));
853                 rte_eth_link_get(p->pmd_id, &link_params);
854
855                 APP_LOG(app, HIGH, "%s (%" PRIu32 ") (%" PRIu32 " Gbps) %s",
856                         p->name,
857                         p->pmd_id,
858                         link_params.link_speed / 1000,
859                         link_params.link_status ? "UP" : "DOWN");
860
861                 if (link_params.link_status == ETH_LINK_DOWN)
862                         all_links_up = 0;
863         }
864
865         if (all_links_up == 0)
866                 rte_panic("Some links are DOWN\n");
867 }
868
869 static uint32_t
870 is_any_swq_frag_or_ras(struct app_params *app)
871 {
872         uint32_t i;
873
874         for (i = 0; i < app->n_pktq_swq; i++) {
875                 struct app_pktq_swq_params *p = &app->swq_params[i];
876
877                 if ((p->ipv4_frag == 1) || (p->ipv6_frag == 1) ||
878                         (p->ipv4_ras == 1) || (p->ipv6_ras == 1))
879                         return 1;
880         }
881
882         return 0;
883 }
884
885 static void
886 app_init_link_frag_ras(struct app_params *app)
887 {
888         uint32_t i;
889
890         if (is_any_swq_frag_or_ras(app)) {
891                 for (i = 0; i < app->n_pktq_hwq_out; i++) {
892                         struct app_pktq_hwq_out_params *p_txq = &app->hwq_out_params[i];
893
894                         p_txq->conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
895                 }
896         }
897 }
898
899 static inline int
900 app_get_cpu_socket_id(uint32_t pmd_id)
901 {
902         int status = rte_eth_dev_socket_id(pmd_id);
903
904         return (status != SOCKET_ID_ANY) ? status : 0;
905 }
906
907 static inline int
908 app_link_rss_enabled(struct app_link_params *cp)
909 {
910         return (cp->n_rss_qs) ? 1 : 0;
911 }
912
913 static void
914 app_link_rss_setup(struct app_link_params *cp)
915 {
916         struct rte_eth_dev_info dev_info;
917         struct rte_eth_rss_reta_entry64 reta_conf[APP_RETA_SIZE_MAX];
918         uint32_t i;
919         int status;
920
921     /* Get RETA size */
922         memset(&dev_info, 0, sizeof(dev_info));
923         rte_eth_dev_info_get(cp->pmd_id, &dev_info);
924
925         if (dev_info.reta_size == 0)
926                 rte_panic("%s (%u): RSS setup error (null RETA size)\n",
927                         cp->name, cp->pmd_id);
928
929         if (dev_info.reta_size > ETH_RSS_RETA_SIZE_512)
930                 rte_panic("%s (%u): RSS setup error (RETA size too big)\n",
931                         cp->name, cp->pmd_id);
932
933         /* Setup RETA contents */
934         memset(reta_conf, 0, sizeof(reta_conf));
935
936         for (i = 0; i < dev_info.reta_size; i++)
937                 reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
938
939         for (i = 0; i < dev_info.reta_size; i++) {
940                 uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
941                 uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
942                 uint32_t rss_qs_pos = i % cp->n_rss_qs;
943
944                 reta_conf[reta_id].reta[reta_pos] =
945                         (uint16_t) cp->rss_qs[rss_qs_pos];
946         }
947
948         /* RETA update */
949         status = rte_eth_dev_rss_reta_update(cp->pmd_id,
950                 reta_conf,
951                 dev_info.reta_size);
952         if (status != 0)
953                 rte_panic("%s (%u): RSS setup error (RETA update failed)\n",
954                         cp->name, cp->pmd_id);
955 }
956
957 static void
958 app_init_link_set_config(struct app_link_params *p)
959 {
960         if (p->n_rss_qs) {
961                 p->conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
962                 p->conf.rx_adv_conf.rss_conf.rss_hf = p->rss_proto_ipv4 |
963                         p->rss_proto_ipv6 |
964                         p->rss_proto_l2;
965         }
966 }
967
968 static void
969 app_init_link(struct app_params *app)
970 {
971         uint32_t i;
972
973         app_init_link_frag_ras(app);
974
975         for (i = 0; i < app->n_links; i++) {
976                 struct app_link_params *p_link = &app->link_params[i];
977                 uint32_t link_id, n_hwq_in, n_hwq_out, j;
978                 int status;
979
980                 sscanf(p_link->name, "LINK%" PRIu32, &link_id);
981                 n_hwq_in = app_link_get_n_rxq(app, p_link);
982                 n_hwq_out = app_link_get_n_txq(app, p_link);
983                 app_init_link_set_config(p_link);
984
985                 APP_LOG(app, HIGH, "Initializing %s (%" PRIu32") "
986                         "(%" PRIu32 " RXQ, %" PRIu32 " TXQ) ...",
987                         p_link->name,
988                         p_link->pmd_id,
989                         n_hwq_in,
990                         n_hwq_out);
991
992                 /* LINK */
993                 status = rte_eth_dev_configure(
994                         p_link->pmd_id,
995                         n_hwq_in,
996                         n_hwq_out,
997                         &p_link->conf);
998                 if (status < 0)
999                         rte_panic("%s (%" PRId32 "): "
1000                                 "init error (%" PRId32 ")\n",
1001                                 p_link->name, p_link->pmd_id, status);
1002
1003                 rte_eth_macaddr_get(p_link->pmd_id,
1004                         (struct ether_addr *) &p_link->mac_addr);
1005
1006                 if (p_link->promisc)
1007                         rte_eth_promiscuous_enable(p_link->pmd_id);
1008
1009                 /* RXQ */
1010                 for (j = 0; j < app->n_pktq_hwq_in; j++) {
1011                         struct app_pktq_hwq_in_params *p_rxq =
1012                                 &app->hwq_in_params[j];
1013                         uint32_t rxq_link_id, rxq_queue_id;
1014
1015                         sscanf(p_rxq->name, "RXQ%" PRIu32 ".%" PRIu32,
1016                                 &rxq_link_id, &rxq_queue_id);
1017                         if (rxq_link_id != link_id)
1018                                 continue;
1019
1020                         status = rte_eth_rx_queue_setup(
1021                                 p_link->pmd_id,
1022                                 rxq_queue_id,
1023                                 p_rxq->size,
1024                                 app_get_cpu_socket_id(p_link->pmd_id),
1025                                 &p_rxq->conf,
1026                                 app->mempool[p_rxq->mempool_id]);
1027                         if (status < 0)
1028                                 rte_panic("%s (%" PRIu32 "): "
1029                                         "%s init error (%" PRId32 ")\n",
1030                                         p_link->name,
1031                                         p_link->pmd_id,
1032                                         p_rxq->name,
1033                                         status);
1034                 }
1035
1036                 /* TXQ */
1037                 for (j = 0; j < app->n_pktq_hwq_out; j++) {
1038                         struct app_pktq_hwq_out_params *p_txq =
1039                                 &app->hwq_out_params[j];
1040                         uint32_t txq_link_id, txq_queue_id;
1041
1042                         sscanf(p_txq->name, "TXQ%" PRIu32 ".%" PRIu32,
1043                                 &txq_link_id, &txq_queue_id);
1044                         if (txq_link_id != link_id)
1045                                 continue;
1046
1047                         status = rte_eth_tx_queue_setup(
1048                                 p_link->pmd_id,
1049                                 txq_queue_id,
1050                                 p_txq->size,
1051                                 app_get_cpu_socket_id(p_link->pmd_id),
1052                                 &p_txq->conf);
1053                         if (status < 0)
1054                                 rte_panic("%s (%" PRIu32 "): "
1055                                         "%s init error (%" PRId32 ")\n",
1056                                         p_link->name,
1057                                         p_link->pmd_id,
1058                                         p_txq->name,
1059                                         status);
1060                 }
1061
1062                 /* LINK START */
1063                 status = rte_eth_dev_start(p_link->pmd_id);
1064                 if (status < 0)
1065                         rte_panic("Cannot start %s (error %" PRId32 ")\n",
1066                                 p_link->name, status);
1067
1068                 /* LINK FILTERS */
1069                 app_link_set_arp_filter(app, p_link);
1070                 app_link_set_tcp_syn_filter(app, p_link);
1071                 if (app_link_rss_enabled(p_link))
1072                         app_link_rss_setup(p_link);
1073
1074                 /* LINK UP */
1075                 app_link_up_internal(app, p_link);
1076         }
1077
1078         app_check_link(app);
1079 }
1080
1081 static void
1082 app_init_swq(struct app_params *app)
1083 {
1084         uint32_t i;
1085
1086         for (i = 0; i < app->n_pktq_swq; i++) {
1087                 struct app_pktq_swq_params *p = &app->swq_params[i];
1088                 unsigned flags = 0;
1089
1090                 if (app_swq_get_readers(app, p) == 1)
1091                         flags |= RING_F_SC_DEQ;
1092                 if (app_swq_get_writers(app, p) == 1)
1093                         flags |= RING_F_SP_ENQ;
1094
1095                 APP_LOG(app, HIGH, "Initializing %s...", p->name);
1096                 app->swq[i] = rte_ring_create(
1097                                 p->name,
1098                                 p->size,
1099                                 p->cpu_socket_id,
1100                                 flags);
1101
1102                 if (app->swq[i] == NULL)
1103                         rte_panic("%s init error\n", p->name);
1104         }
1105 }
1106
1107 static void
1108 app_init_tm(struct app_params *app)
1109 {
1110         uint32_t i;
1111
1112         for (i = 0; i < app->n_pktq_tm; i++) {
1113                 struct app_pktq_tm_params *p_tm = &app->tm_params[i];
1114                 struct app_link_params *p_link;
1115                 struct rte_eth_link link_eth_params;
1116                 struct rte_sched_port *sched;
1117                 uint32_t n_subports, subport_id;
1118                 int status;
1119
1120                 p_link = app_get_link_for_tm(app, p_tm);
1121                 /* LINK */
1122                 rte_eth_link_get(p_link->pmd_id, &link_eth_params);
1123
1124                 /* TM */
1125                 p_tm->sched_port_params.name = p_tm->name;
1126                 p_tm->sched_port_params.socket =
1127                         app_get_cpu_socket_id(p_link->pmd_id);
1128                 p_tm->sched_port_params.rate =
1129                         (uint64_t) link_eth_params.link_speed * 1000 * 1000 / 8;
1130
1131                 APP_LOG(app, HIGH, "Initializing %s ...", p_tm->name);
1132                 sched = rte_sched_port_config(&p_tm->sched_port_params);
1133                 if (sched == NULL)
1134                         rte_panic("%s init error\n", p_tm->name);
1135                 app->tm[i] = sched;
1136
1137                 /* Subport */
1138                 n_subports = p_tm->sched_port_params.n_subports_per_port;
1139                 for (subport_id = 0; subport_id < n_subports; subport_id++) {
1140                         uint32_t n_pipes_per_subport, pipe_id;
1141
1142                         status = rte_sched_subport_config(sched,
1143                                 subport_id,
1144                                 &p_tm->sched_subport_params[subport_id]);
1145                         if (status)
1146                                 rte_panic("%s subport %" PRIu32
1147                                         " init error (%" PRId32 ")\n",
1148                                         p_tm->name, subport_id, status);
1149
1150                         /* Pipe */
1151                         n_pipes_per_subport =
1152                                 p_tm->sched_port_params.n_pipes_per_subport;
1153                         for (pipe_id = 0;
1154                                 pipe_id < n_pipes_per_subport;
1155                                 pipe_id++) {
1156                                 int profile_id = p_tm->sched_pipe_to_profile[
1157                                         subport_id * APP_MAX_SCHED_PIPES +
1158                                         pipe_id];
1159
1160                                 if (profile_id == -1)
1161                                         continue;
1162
1163                                 status = rte_sched_pipe_config(sched,
1164                                         subport_id,
1165                                         pipe_id,
1166                                         profile_id);
1167                                 if (status)
1168                                         rte_panic("%s subport %" PRIu32
1169                                                 " pipe %" PRIu32
1170                                                 " (profile %" PRId32 ") "
1171                                                 "init error (% " PRId32 ")\n",
1172                                                 p_tm->name, subport_id, pipe_id,
1173                                                 profile_id, status);
1174                         }
1175                 }
1176         }
1177 }
1178
1179 static void
1180 app_init_msgq(struct app_params *app)
1181 {
1182         uint32_t i;
1183
1184         for (i = 0; i < app->n_msgq; i++) {
1185                 struct app_msgq_params *p = &app->msgq_params[i];
1186
1187                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
1188                 app->msgq[i] = rte_ring_create(
1189                                 p->name,
1190                                 p->size,
1191                                 p->cpu_socket_id,
1192                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
1193
1194                 if (app->msgq[i] == NULL)
1195                         rte_panic("%s init error\n", p->name);
1196         }
1197 }
1198
1199 static void app_pipeline_params_get(struct app_params *app,
1200         struct app_pipeline_params *p_in,
1201         struct pipeline_params *p_out)
1202 {
1203         uint32_t i;
1204
1205         snprintf(p_out->name, PIPELINE_NAME_SIZE, "%s", p_in->name);
1206
1207         p_out->socket_id = (int) p_in->socket_id;
1208
1209         p_out->log_level = app->log_level;
1210
1211         /* pktq_in */
1212         p_out->n_ports_in = p_in->n_pktq_in;
1213         for (i = 0; i < p_in->n_pktq_in; i++) {
1214                 struct app_pktq_in_params *in = &p_in->pktq_in[i];
1215                 struct pipeline_port_in_params *out = &p_out->port_in[i];
1216
1217                 switch (in->type) {
1218                 case APP_PKTQ_IN_HWQ:
1219                 {
1220                         struct app_pktq_hwq_in_params *p_hwq_in =
1221                                 &app->hwq_in_params[in->id];
1222                         struct app_link_params *p_link =
1223                                 app_get_link_for_rxq(app, p_hwq_in);
1224                         uint32_t rxq_link_id, rxq_queue_id;
1225
1226                         sscanf(p_hwq_in->name, "RXQ%" SCNu32 ".%" SCNu32,
1227                                 &rxq_link_id,
1228                                 &rxq_queue_id);
1229
1230                         out->type = PIPELINE_PORT_IN_ETHDEV_READER;
1231                         out->params.ethdev.port_id = p_link->pmd_id;
1232                         out->params.ethdev.queue_id = rxq_queue_id;
1233                         out->burst_size = p_hwq_in->burst;
1234                         break;
1235                 }
1236                 case APP_PKTQ_IN_SWQ:
1237                 {
1238                         struct app_pktq_swq_params *swq_params = &app->swq_params[in->id];
1239
1240                         if ((swq_params->ipv4_frag == 0) && (swq_params->ipv6_frag == 0)) {
1241                                 if (app_swq_get_readers(app, swq_params) == 1) {
1242                                         out->type = PIPELINE_PORT_IN_RING_READER;
1243                                         out->params.ring.ring = app->swq[in->id];
1244                                         out->burst_size = app->swq_params[in->id].burst_read;
1245                                 } else {
1246                                         out->type = PIPELINE_PORT_IN_RING_MULTI_READER;
1247                                         out->params.ring_multi.ring = app->swq[in->id];
1248                                         out->burst_size = swq_params->burst_read;
1249                                 }
1250                         } else {
1251                                 if (swq_params->ipv4_frag == 1) {
1252                                         struct rte_port_ring_reader_ipv4_frag_params *params =
1253                                                 &out->params.ring_ipv4_frag;
1254
1255                                         out->type = PIPELINE_PORT_IN_RING_READER_IPV4_FRAG;
1256                                         params->ring = app->swq[in->id];
1257                                         params->mtu = swq_params->mtu;
1258                                         params->metadata_size = swq_params->metadata_size;
1259                                         params->pool_direct =
1260                                                 app->mempool[swq_params->mempool_direct_id];
1261                                         params->pool_indirect =
1262                                                 app->mempool[swq_params->mempool_indirect_id];
1263                                         out->burst_size = swq_params->burst_read;
1264                                 } else {
1265                                         struct rte_port_ring_reader_ipv6_frag_params *params =
1266                                                 &out->params.ring_ipv6_frag;
1267
1268                                         out->type = PIPELINE_PORT_IN_RING_READER_IPV6_FRAG;
1269                                         params->ring = app->swq[in->id];
1270                                         params->mtu = swq_params->mtu;
1271                                         params->metadata_size = swq_params->metadata_size;
1272                                         params->pool_direct =
1273                                                 app->mempool[swq_params->mempool_direct_id];
1274                                         params->pool_indirect =
1275                                                 app->mempool[swq_params->mempool_indirect_id];
1276                                         out->burst_size = swq_params->burst_read;
1277                                 }
1278                         }
1279                         break;
1280                 }
1281                 case APP_PKTQ_IN_TM:
1282                         out->type = PIPELINE_PORT_IN_SCHED_READER;
1283                         out->params.sched.sched = app->tm[in->id];
1284                         out->burst_size = app->tm_params[in->id].burst_read;
1285                         break;
1286                 case APP_PKTQ_IN_SOURCE:
1287                 {
1288                         uint32_t mempool_id =
1289                                 app->source_params[in->id].mempool_id;
1290
1291                         out->type = PIPELINE_PORT_IN_SOURCE;
1292                         out->params.source.mempool = app->mempool[mempool_id];
1293                         out->burst_size = app->source_params[in->id].burst;
1294                         out->params.source.file_name =
1295                                 app->source_params[in->id].file_name;
1296                         out->params.source.n_bytes_per_pkt =
1297                                 app->source_params[in->id].n_bytes_per_pkt;
1298                         break;
1299                 }
1300                 default:
1301                         break;
1302                 }
1303         }
1304
1305         /* pktq_out */
1306         p_out->n_ports_out = p_in->n_pktq_out;
1307         for (i = 0; i < p_in->n_pktq_out; i++) {
1308                 struct app_pktq_out_params *in = &p_in->pktq_out[i];
1309                 struct pipeline_port_out_params *out = &p_out->port_out[i];
1310
1311                 switch (in->type) {
1312                 case APP_PKTQ_OUT_HWQ:
1313                 {
1314                         struct app_pktq_hwq_out_params *p_hwq_out =
1315                                 &app->hwq_out_params[in->id];
1316                         struct app_link_params *p_link =
1317                                 app_get_link_for_txq(app, p_hwq_out);
1318                         uint32_t txq_link_id, txq_queue_id;
1319
1320                         sscanf(p_hwq_out->name,
1321                                 "TXQ%" SCNu32 ".%" SCNu32,
1322                                 &txq_link_id,
1323                                 &txq_queue_id);
1324
1325                         if (p_hwq_out->dropless == 0) {
1326                                 struct rte_port_ethdev_writer_params *params =
1327                                         &out->params.ethdev;
1328
1329                                 out->type = PIPELINE_PORT_OUT_ETHDEV_WRITER;
1330                                 params->port_id = p_link->pmd_id;
1331                                 params->queue_id = txq_queue_id;
1332                                 params->tx_burst_sz =
1333                                         app->hwq_out_params[in->id].burst;
1334                         } else {
1335                                 struct rte_port_ethdev_writer_nodrop_params
1336                                         *params = &out->params.ethdev_nodrop;
1337
1338                                 out->type =
1339                                         PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP;
1340                                 params->port_id = p_link->pmd_id;
1341                                 params->queue_id = txq_queue_id;
1342                                 params->tx_burst_sz = p_hwq_out->burst;
1343                                 params->n_retries = p_hwq_out->n_retries;
1344                         }
1345                         break;
1346                 }
1347                 case APP_PKTQ_OUT_SWQ:
1348                 {
1349                         struct app_pktq_swq_params *swq_params = &app->swq_params[in->id];
1350
1351                         if ((swq_params->ipv4_ras == 0) && (swq_params->ipv6_ras == 0)) {
1352                                 if (app_swq_get_writers(app, swq_params) == 1) {
1353                                         if (app->swq_params[in->id].dropless == 0) {
1354                                                 struct rte_port_ring_writer_params *params =
1355                                                         &out->params.ring;
1356
1357                                                 out->type = PIPELINE_PORT_OUT_RING_WRITER;
1358                                                 params->ring = app->swq[in->id];
1359                                                 params->tx_burst_sz =
1360                                                         app->swq_params[in->id].burst_write;
1361                                         } else {
1362                                                 struct rte_port_ring_writer_nodrop_params
1363                                                         *params = &out->params.ring_nodrop;
1364
1365                                                 out->type =
1366                                                         PIPELINE_PORT_OUT_RING_WRITER_NODROP;
1367                                                 params->ring = app->swq[in->id];
1368                                                 params->tx_burst_sz =
1369                                                         app->swq_params[in->id].burst_write;
1370                                                 params->n_retries =
1371                                                         app->swq_params[in->id].n_retries;
1372                                         }
1373                                 } else {
1374                                         if (swq_params->dropless == 0) {
1375                                                 struct rte_port_ring_multi_writer_params *params =
1376                                                         &out->params.ring_multi;
1377
1378                                                 out->type = PIPELINE_PORT_OUT_RING_MULTI_WRITER;
1379                                                 params->ring = app->swq[in->id];
1380                                                 params->tx_burst_sz = swq_params->burst_write;
1381                                         } else {
1382                                                 struct rte_port_ring_multi_writer_nodrop_params
1383                                                         *params = &out->params.ring_multi_nodrop;
1384
1385                                                 out->type = PIPELINE_PORT_OUT_RING_MULTI_WRITER_NODROP;
1386                                                 params->ring = app->swq[in->id];
1387                                                 params->tx_burst_sz = swq_params->burst_write;
1388                                                 params->n_retries = swq_params->n_retries;
1389                                         }
1390                                 }
1391                         } else {
1392                                 if (swq_params->ipv4_ras == 1) {
1393                                         struct rte_port_ring_writer_ipv4_ras_params *params =
1394                                                 &out->params.ring_ipv4_ras;
1395
1396                                         out->type = PIPELINE_PORT_OUT_RING_WRITER_IPV4_RAS;
1397                                         params->ring = app->swq[in->id];
1398                                         params->tx_burst_sz = swq_params->burst_write;
1399                                 } else {
1400                                         struct rte_port_ring_writer_ipv6_ras_params *params =
1401                                                 &out->params.ring_ipv6_ras;
1402
1403                                         out->type = PIPELINE_PORT_OUT_RING_WRITER_IPV6_RAS;
1404                                         params->ring = app->swq[in->id];
1405                                         params->tx_burst_sz = swq_params->burst_write;
1406                                 }
1407                         }
1408                         break;
1409                 }
1410                 case APP_PKTQ_OUT_TM: {
1411                         struct rte_port_sched_writer_params *params =
1412                                 &out->params.sched;
1413
1414                         out->type = PIPELINE_PORT_OUT_SCHED_WRITER;
1415                         params->sched = app->tm[in->id];
1416                         params->tx_burst_sz =
1417                                 app->tm_params[in->id].burst_write;
1418                         break;
1419                 }
1420                 case APP_PKTQ_OUT_SINK:
1421                 {
1422                         out->type = PIPELINE_PORT_OUT_SINK;
1423                         out->params.sink.file_name =
1424                                 app->sink_params[in->id].file_name;
1425                         out->params.sink.max_n_pkts =
1426                                 app->sink_params[in->id].
1427                                 n_pkts_to_dump;
1428
1429                         break;
1430                 }
1431                 default:
1432                         break;
1433                 }
1434         }
1435
1436         /* msgq */
1437         p_out->n_msgq = p_in->n_msgq_in;
1438
1439         for (i = 0; i < p_in->n_msgq_in; i++)
1440                 p_out->msgq_in[i] = app->msgq[p_in->msgq_in[i]];
1441
1442         for (i = 0; i < p_in->n_msgq_out; i++)
1443                 p_out->msgq_out[i] = app->msgq[p_in->msgq_out[i]];
1444
1445         /* args */
1446         p_out->n_args = p_in->n_args;
1447         for (i = 0; i < p_in->n_args; i++) {
1448                 p_out->args_name[i] = p_in->args_name[i];
1449                 p_out->args_value[i] = p_in->args_value[i];
1450         }
1451 }
1452
1453 static void
1454 app_init_pipelines(struct app_params *app)
1455 {
1456         uint32_t p_id;
1457
1458         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1459                 struct app_pipeline_params *params =
1460                         &app->pipeline_params[p_id];
1461                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1462                 struct pipeline_type *ptype;
1463                 struct pipeline_params pp;
1464
1465                 APP_LOG(app, HIGH, "Initializing %s ...", params->name);
1466
1467                 ptype = app_pipeline_type_find(app, params->type);
1468                 if (ptype == NULL)
1469                         rte_panic("Init error: Unknown pipeline type \"%s\"\n",
1470                                 params->type);
1471
1472                 app_pipeline_params_get(app, params, &pp);
1473
1474                 /* Back-end */
1475                 data->be = NULL;
1476                 if (ptype->be_ops->f_init) {
1477                         data->be = ptype->be_ops->f_init(&pp, (void *) app);
1478
1479                         if (data->be == NULL)
1480                                 rte_panic("Pipeline instance \"%s\" back-end "
1481                                         "init error\n", params->name);
1482                 }
1483
1484                 /* Front-end */
1485                 data->fe = NULL;
1486                 if (ptype->fe_ops->f_init) {
1487                         data->fe = ptype->fe_ops->f_init(&pp, (void *) app);
1488
1489                         if (data->fe == NULL)
1490                                 rte_panic("Pipeline instance \"%s\" front-end "
1491                                 "init error\n", params->name);
1492                 }
1493
1494                 data->ptype = ptype;
1495
1496                 data->timer_period = (rte_get_tsc_hz() *
1497                         params->timer_period) / 100;
1498         }
1499 }
1500
1501 static void
1502 app_init_threads(struct app_params *app)
1503 {
1504         uint64_t time = rte_get_tsc_cycles();
1505         uint32_t p_id;
1506
1507         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1508                 struct app_pipeline_params *params =
1509                         &app->pipeline_params[p_id];
1510                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1511                 struct pipeline_type *ptype;
1512                 struct app_thread_data *t;
1513                 struct app_thread_pipeline_data *p;
1514                 int lcore_id;
1515
1516                 lcore_id = cpu_core_map_get_lcore_id(app->core_map,
1517                         params->socket_id,
1518                         params->core_id,
1519                         params->hyper_th_id);
1520
1521                 if (lcore_id < 0)
1522                         rte_panic("Invalid core s%" PRIu32 "c%" PRIu32 "%s\n",
1523                                 params->socket_id,
1524                                 params->core_id,
1525                                 (params->hyper_th_id) ? "h" : "");
1526
1527                 t = &app->thread_data[lcore_id];
1528
1529                 t->timer_period = (rte_get_tsc_hz() * APP_THREAD_TIMER_PERIOD) / 1000;
1530                 t->thread_req_deadline = time + t->timer_period;
1531
1532                 t->headroom_cycles = 0;
1533                 t->headroom_time = rte_get_tsc_cycles();
1534                 t->headroom_ratio = 0.0;
1535
1536                 t->msgq_in = app_thread_msgq_in_get(app,
1537                                 params->socket_id,
1538                                 params->core_id,
1539                                 params->hyper_th_id);
1540                 if (t->msgq_in == NULL)
1541                         rte_panic("Init error: Cannot find MSGQ_IN for thread %" PRId32,
1542                                 lcore_id);
1543
1544                 t->msgq_out = app_thread_msgq_out_get(app,
1545                                 params->socket_id,
1546                                 params->core_id,
1547                                 params->hyper_th_id);
1548                 if (t->msgq_out == NULL)
1549                         rte_panic("Init error: Cannot find MSGQ_OUT for thread %" PRId32,
1550                                 lcore_id);
1551
1552                 ptype = app_pipeline_type_find(app, params->type);
1553                 if (ptype == NULL)
1554                         rte_panic("Init error: Unknown pipeline "
1555                                 "type \"%s\"\n", params->type);
1556
1557                 p = (ptype->be_ops->f_run == NULL) ?
1558                         &t->regular[t->n_regular] :
1559                         &t->custom[t->n_custom];
1560
1561                 p->pipeline_id = p_id;
1562                 p->be = data->be;
1563                 p->f_run = ptype->be_ops->f_run;
1564                 p->f_timer = ptype->be_ops->f_timer;
1565                 p->timer_period = data->timer_period;
1566                 p->deadline = time + data->timer_period;
1567
1568                 data->enabled = 1;
1569
1570                 if (ptype->be_ops->f_run == NULL)
1571                         t->n_regular++;
1572                 else
1573                         t->n_custom++;
1574         }
1575 }
1576
1577 int app_init(struct app_params *app)
1578 {
1579         app_init_core_map(app);
1580         app_init_core_mask(app);
1581
1582         app_init_eal(app);
1583         app_init_mempool(app);
1584         app_init_link(app);
1585         app_init_swq(app);
1586         app_init_tm(app);
1587         app_init_msgq(app);
1588
1589         app_pipeline_common_cmd_push(app);
1590         app_pipeline_thread_cmd_push(app);
1591         app_pipeline_type_register(app, &pipeline_master);
1592         app_pipeline_type_register(app, &pipeline_passthrough);
1593         app_pipeline_type_register(app, &pipeline_flow_classification);
1594         app_pipeline_type_register(app, &pipeline_flow_actions);
1595         app_pipeline_type_register(app, &pipeline_firewall);
1596         app_pipeline_type_register(app, &pipeline_routing);
1597
1598         app_init_pipelines(app);
1599         app_init_threads(app);
1600
1601         return 0;
1602 }
1603
1604 static int
1605 app_pipeline_type_cmd_push(struct app_params *app,
1606         struct pipeline_type *ptype)
1607 {
1608         cmdline_parse_ctx_t *cmds;
1609         uint32_t n_cmds, i;
1610
1611         /* Check input arguments */
1612         if ((app == NULL) ||
1613                 (ptype == NULL))
1614                 return -EINVAL;
1615
1616         n_cmds = pipeline_type_cmds_count(ptype);
1617         if (n_cmds == 0)
1618                 return 0;
1619
1620         cmds = ptype->fe_ops->cmds;
1621
1622         /* Check for available slots in the application commands array */
1623         if (n_cmds > APP_MAX_CMDS - app->n_cmds)
1624                 return -ENOMEM;
1625
1626         /* Push pipeline commands into the application */
1627         memcpy(&app->cmds[app->n_cmds],
1628                 cmds,
1629                 n_cmds * sizeof(cmdline_parse_ctx_t));
1630
1631         for (i = 0; i < n_cmds; i++)
1632                 app->cmds[app->n_cmds + i]->data = app;
1633
1634         app->n_cmds += n_cmds;
1635         app->cmds[app->n_cmds] = NULL;
1636
1637         return 0;
1638 }
1639
1640 int
1641 app_pipeline_type_register(struct app_params *app, struct pipeline_type *ptype)
1642 {
1643         uint32_t n_cmds, i;
1644
1645         /* Check input arguments */
1646         if ((app == NULL) ||
1647                 (ptype == NULL) ||
1648                 (ptype->name == NULL) ||
1649                 (strlen(ptype->name) == 0) ||
1650                 (ptype->be_ops->f_init == NULL) ||
1651                 (ptype->be_ops->f_timer == NULL))
1652                 return -EINVAL;
1653
1654         /* Check for duplicate entry */
1655         for (i = 0; i < app->n_pipeline_types; i++)
1656                 if (strcmp(app->pipeline_type[i].name, ptype->name) == 0)
1657                         return -EEXIST;
1658
1659         /* Check for resource availability */
1660         n_cmds = pipeline_type_cmds_count(ptype);
1661         if ((app->n_pipeline_types == APP_MAX_PIPELINE_TYPES) ||
1662                 (n_cmds > APP_MAX_CMDS - app->n_cmds))
1663                 return -ENOMEM;
1664
1665         /* Copy pipeline type */
1666         memcpy(&app->pipeline_type[app->n_pipeline_types++],
1667                 ptype,
1668                 sizeof(struct pipeline_type));
1669
1670         /* Copy CLI commands */
1671         if (n_cmds)
1672                 app_pipeline_type_cmd_push(app, ptype);
1673
1674         return 0;
1675 }
1676
1677 struct
1678 pipeline_type *app_pipeline_type_find(struct app_params *app, char *name)
1679 {
1680         uint32_t i;
1681
1682         for (i = 0; i < app->n_pipeline_types; i++)
1683                 if (strcmp(app->pipeline_type[i].name, name) == 0)
1684                         return &app->pipeline_type[i];
1685
1686         return NULL;
1687 }