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