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