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