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