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