examples/ip_pipeline: rework passthrough pipeline
[dpdk.git] / examples / ip_pipeline / init.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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
38 #include <rte_cycles.h>
39 #include <rte_ethdev.h>
40 #include <rte_ether.h>
41 #include <rte_ip.h>
42 #include <rte_eal.h>
43 #include <rte_malloc.h>
44
45 #include "app.h"
46 #include "pipeline.h"
47 #include "pipeline_common_fe.h"
48 #include "pipeline_master.h"
49 #include "pipeline_passthrough.h"
50
51 #define APP_NAME_SIZE   32
52
53 static void
54 app_init_core_map(struct app_params *app)
55 {
56         APP_LOG(app, HIGH, "Initializing CPU core map ...");
57         app->core_map = cpu_core_map_init(4, 32, 4, 0);
58
59         if (app->core_map == NULL)
60                 rte_panic("Cannot create CPU core map\n");
61
62         if (app->log_level >= APP_LOG_LEVEL_LOW)
63                 cpu_core_map_print(app->core_map);
64 }
65
66 static void
67 app_init_core_mask(struct app_params *app)
68 {
69         uint64_t mask = 0;
70         uint32_t i;
71
72         for (i = 0; i < app->n_pipelines; i++) {
73                 struct app_pipeline_params *p = &app->pipeline_params[i];
74                 int lcore_id;
75
76                 lcore_id = cpu_core_map_get_lcore_id(app->core_map,
77                         p->socket_id,
78                         p->core_id,
79                         p->hyper_th_id);
80
81                 if (lcore_id < 0)
82                         rte_panic("Cannot create CPU core mask\n");
83
84                 mask |= 1LLU << lcore_id;
85         }
86
87         app->core_mask = mask;
88         APP_LOG(app, HIGH, "CPU core mask = 0x%016" PRIx64, app->core_mask);
89 }
90
91 static void
92 app_init_eal(struct app_params *app)
93 {
94         char buffer[32];
95         struct app_eal_params *p = &app->eal_params;
96         uint32_t n_args = 0;
97         int status;
98
99         app->eal_argv[n_args++] = strdup(app->app_name);
100
101         snprintf(buffer, sizeof(buffer), "-c%" PRIx64, app->core_mask);
102         app->eal_argv[n_args++] = strdup(buffer);
103
104         if (p->coremap) {
105                 snprintf(buffer, sizeof(buffer), "--lcores=%s", p->coremap);
106                 app->eal_argv[n_args++] = strdup(buffer);
107         }
108
109         if (p->master_lcore_present) {
110                 snprintf(buffer,
111                         sizeof(buffer),
112                         "--master-lcore=%" PRIu32,
113                         p->master_lcore);
114                 app->eal_argv[n_args++] = strdup(buffer);
115         }
116
117         snprintf(buffer, sizeof(buffer), "-n%" PRIu32, p->channels);
118         app->eal_argv[n_args++] = strdup(buffer);
119
120         if (p->memory_present) {
121                 snprintf(buffer, sizeof(buffer), "-m%" PRIu32, p->memory);
122                 app->eal_argv[n_args++] = strdup(buffer);
123         }
124
125         if (p->ranks_present) {
126                 snprintf(buffer, sizeof(buffer), "-r%" PRIu32, p->ranks);
127                 app->eal_argv[n_args++] = strdup(buffer);
128         }
129
130         if (p->pci_blacklist) {
131                 snprintf(buffer,
132                         sizeof(buffer),
133                         "--pci-blacklist=%s",
134                         p->pci_blacklist);
135                 app->eal_argv[n_args++] = strdup(buffer);
136         }
137
138         if (p->pci_whitelist) {
139                 snprintf(buffer,
140                         sizeof(buffer),
141                         "--pci-whitelist=%s",
142                         p->pci_whitelist);
143                 app->eal_argv[n_args++] = strdup(buffer);
144         }
145
146         if (p->vdev) {
147                 snprintf(buffer, sizeof(buffer), "--vdev=%s", p->vdev);
148                 app->eal_argv[n_args++] = strdup(buffer);
149         }
150
151         if ((p->vmware_tsc_map_present) && p->vmware_tsc_map) {
152                 snprintf(buffer, sizeof(buffer), "--vmware-tsc-map");
153                 app->eal_argv[n_args++] = strdup(buffer);
154         }
155
156         if (p->proc_type) {
157                 snprintf(buffer,
158                         sizeof(buffer),
159                         "--proc-type=%s",
160                         p->proc_type);
161                 app->eal_argv[n_args++] = strdup(buffer);
162         }
163
164         if (p->syslog) {
165                 snprintf(buffer, sizeof(buffer), "--syslog=%s", p->syslog);
166                 app->eal_argv[n_args++] = strdup(buffer);
167         }
168
169         if (p->log_level_present) {
170                 snprintf(buffer,
171                         sizeof(buffer),
172                         "--log-level=%" PRIu32,
173                         p->log_level);
174                 app->eal_argv[n_args++] = strdup(buffer);
175         }
176
177         if ((p->version_present) && p->version) {
178                 snprintf(buffer, sizeof(buffer), "-v");
179                 app->eal_argv[n_args++] = strdup(buffer);
180         }
181
182         if ((p->help_present) && p->help) {
183                 snprintf(buffer, sizeof(buffer), "--help");
184                 app->eal_argv[n_args++] = strdup(buffer);
185         }
186
187         if ((p->no_huge_present) && p->no_huge) {
188                 snprintf(buffer, sizeof(buffer), "--no-huge");
189                 app->eal_argv[n_args++] = strdup(buffer);
190         }
191
192         if ((p->no_pci_present) && p->no_pci) {
193                 snprintf(buffer, sizeof(buffer), "--no-pci");
194                 app->eal_argv[n_args++] = strdup(buffer);
195         }
196
197         if ((p->no_hpet_present) && p->no_hpet) {
198                 snprintf(buffer, sizeof(buffer), "--no-hpet");
199                 app->eal_argv[n_args++] = strdup(buffer);
200         }
201
202         if ((p->no_shconf_present) && p->no_shconf) {
203                 snprintf(buffer, sizeof(buffer), "--no-shconf");
204                 app->eal_argv[n_args++] = strdup(buffer);
205         }
206
207         if (p->add_driver) {
208                 snprintf(buffer, sizeof(buffer), "-d=%s", p->add_driver);
209                 app->eal_argv[n_args++] = strdup(buffer);
210         }
211
212         if (p->socket_mem) {
213                 snprintf(buffer,
214                         sizeof(buffer),
215                         "--socket-mem=%s",
216                         p->socket_mem);
217                 app->eal_argv[n_args++] = strdup(buffer);
218         }
219
220         if (p->huge_dir) {
221                 snprintf(buffer, sizeof(buffer), "--huge-dir=%s", p->huge_dir);
222                 app->eal_argv[n_args++] = strdup(buffer);
223         }
224
225         if (p->file_prefix) {
226                 snprintf(buffer,
227                         sizeof(buffer),
228                         "--file-prefix=%s",
229                         p->file_prefix);
230                 app->eal_argv[n_args++] = strdup(buffer);
231         }
232
233         if (p->base_virtaddr) {
234                 snprintf(buffer,
235                         sizeof(buffer),
236                         "--base-virtaddr=%s",
237                         p->base_virtaddr);
238                 app->eal_argv[n_args++] = strdup(buffer);
239         }
240
241         if ((p->create_uio_dev_present) && p->create_uio_dev) {
242                 snprintf(buffer, sizeof(buffer), "--create-uio-dev");
243                 app->eal_argv[n_args++] = strdup(buffer);
244         }
245
246         if (p->vfio_intr) {
247                 snprintf(buffer,
248                         sizeof(buffer),
249                         "--vfio-intr=%s",
250                         p->vfio_intr);
251                 app->eal_argv[n_args++] = strdup(buffer);
252         }
253
254         if ((p->xen_dom0_present) && (p->xen_dom0)) {
255                 snprintf(buffer, sizeof(buffer), "--xen-dom0");
256                 app->eal_argv[n_args++] = strdup(buffer);
257         }
258
259         snprintf(buffer, sizeof(buffer), "--");
260         app->eal_argv[n_args++] = strdup(buffer);
261
262         app->eal_argc = n_args;
263
264         APP_LOG(app, HIGH, "Initializing EAL ...");
265         status = rte_eal_init(app->eal_argc, app->eal_argv);
266         if (status < 0)
267                 rte_panic("EAL init error\n");
268 }
269
270 static void
271 app_init_mempool(struct app_params *app)
272 {
273         uint32_t i;
274
275         for (i = 0; i < app->n_mempools; i++) {
276                 struct app_mempool_params *p = &app->mempool_params[i];
277
278                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
279                 app->mempool[i] = rte_mempool_create(
280                                 p->name,
281                                 p->pool_size,
282                                 p->buffer_size,
283                                 p->cache_size,
284                                 sizeof(struct rte_pktmbuf_pool_private),
285                                 rte_pktmbuf_pool_init, NULL,
286                                 rte_pktmbuf_init, NULL,
287                                 p->cpu_socket_id,
288                                 0);
289
290                 if (app->mempool[i] == NULL)
291                         rte_panic("%s init error\n", p->name);
292         }
293 }
294
295 static inline int
296 app_link_filter_arp_add(struct app_link_params *link)
297 {
298         struct rte_eth_ethertype_filter filter = {
299                 .ether_type = ETHER_TYPE_ARP,
300                 .flags = 0,
301                 .queue = link->arp_q,
302         };
303
304         return rte_eth_dev_filter_ctrl(link->pmd_id,
305                 RTE_ETH_FILTER_ETHERTYPE,
306                 RTE_ETH_FILTER_ADD,
307                 &filter);
308 }
309
310 static inline int
311 app_link_filter_tcp_syn_add(struct app_link_params *link)
312 {
313         struct rte_eth_syn_filter filter = {
314                 .hig_pri = 1,
315                 .queue = link->tcp_syn_local_q,
316         };
317
318         return rte_eth_dev_filter_ctrl(link->pmd_id,
319                 RTE_ETH_FILTER_SYN,
320                 RTE_ETH_FILTER_ADD,
321                 &filter);
322 }
323
324 static inline int
325 app_link_filter_ip_add(struct app_link_params *l1, struct app_link_params *l2)
326 {
327         struct rte_eth_ntuple_filter filter = {
328                 .flags = RTE_5TUPLE_FLAGS,
329                 .dst_ip = rte_bswap32(l2->ip),
330                 .dst_ip_mask = UINT32_MAX, /* Enable */
331                 .src_ip = 0,
332                 .src_ip_mask = 0, /* Disable */
333                 .dst_port = 0,
334                 .dst_port_mask = 0, /* Disable */
335                 .src_port = 0,
336                 .src_port_mask = 0, /* Disable */
337                 .proto = 0,
338                 .proto_mask = 0, /* Disable */
339                 .tcp_flags = 0,
340                 .priority = 1, /* Lowest */
341                 .queue = l1->ip_local_q,
342         };
343
344         return rte_eth_dev_filter_ctrl(l1->pmd_id,
345                 RTE_ETH_FILTER_NTUPLE,
346                 RTE_ETH_FILTER_ADD,
347                 &filter);
348 }
349
350 static inline int
351 app_link_filter_ip_del(struct app_link_params *l1, struct app_link_params *l2)
352 {
353         struct rte_eth_ntuple_filter filter = {
354                 .flags = RTE_5TUPLE_FLAGS,
355                 .dst_ip = rte_bswap32(l2->ip),
356                 .dst_ip_mask = UINT32_MAX, /* Enable */
357                 .src_ip = 0,
358                 .src_ip_mask = 0, /* Disable */
359                 .dst_port = 0,
360                 .dst_port_mask = 0, /* Disable */
361                 .src_port = 0,
362                 .src_port_mask = 0, /* Disable */
363                 .proto = 0,
364                 .proto_mask = 0, /* Disable */
365                 .tcp_flags = 0,
366                 .priority = 1, /* Lowest */
367                 .queue = l1->ip_local_q,
368         };
369
370         return rte_eth_dev_filter_ctrl(l1->pmd_id,
371                 RTE_ETH_FILTER_NTUPLE,
372                 RTE_ETH_FILTER_DELETE,
373                 &filter);
374 }
375
376 static inline int
377 app_link_filter_tcp_add(struct app_link_params *l1, struct app_link_params *l2)
378 {
379         struct rte_eth_ntuple_filter filter = {
380                 .flags = RTE_5TUPLE_FLAGS,
381                 .dst_ip = rte_bswap32(l2->ip),
382                 .dst_ip_mask = UINT32_MAX, /* Enable */
383                 .src_ip = 0,
384                 .src_ip_mask = 0, /* Disable */
385                 .dst_port = 0,
386                 .dst_port_mask = 0, /* Disable */
387                 .src_port = 0,
388                 .src_port_mask = 0, /* Disable */
389                 .proto = IPPROTO_TCP,
390                 .proto_mask = UINT8_MAX, /* Enable */
391                 .tcp_flags = 0,
392                 .priority = 2, /* Higher priority than IP */
393                 .queue = l1->tcp_local_q,
394         };
395
396         return rte_eth_dev_filter_ctrl(l1->pmd_id,
397                 RTE_ETH_FILTER_NTUPLE,
398                 RTE_ETH_FILTER_ADD,
399                 &filter);
400 }
401
402 static inline int
403 app_link_filter_tcp_del(struct app_link_params *l1, struct app_link_params *l2)
404 {
405         struct rte_eth_ntuple_filter filter = {
406                 .flags = RTE_5TUPLE_FLAGS,
407                 .dst_ip = rte_bswap32(l2->ip),
408                 .dst_ip_mask = UINT32_MAX, /* Enable */
409                 .src_ip = 0,
410                 .src_ip_mask = 0, /* Disable */
411                 .dst_port = 0,
412                 .dst_port_mask = 0, /* Disable */
413                 .src_port = 0,
414                 .src_port_mask = 0, /* Disable */
415                 .proto = IPPROTO_TCP,
416                 .proto_mask = UINT8_MAX, /* Enable */
417                 .tcp_flags = 0,
418                 .priority = 2, /* Higher priority than IP */
419                 .queue = l1->tcp_local_q,
420         };
421
422         return rte_eth_dev_filter_ctrl(l1->pmd_id,
423                 RTE_ETH_FILTER_NTUPLE,
424                 RTE_ETH_FILTER_DELETE,
425                 &filter);
426 }
427
428 static inline int
429 app_link_filter_udp_add(struct app_link_params *l1, struct app_link_params *l2)
430 {
431         struct rte_eth_ntuple_filter filter = {
432                 .flags = RTE_5TUPLE_FLAGS,
433                 .dst_ip = rte_bswap32(l2->ip),
434                 .dst_ip_mask = UINT32_MAX, /* Enable */
435                 .src_ip = 0,
436                 .src_ip_mask = 0, /* Disable */
437                 .dst_port = 0,
438                 .dst_port_mask = 0, /* Disable */
439                 .src_port = 0,
440                 .src_port_mask = 0, /* Disable */
441                 .proto = IPPROTO_UDP,
442                 .proto_mask = UINT8_MAX, /* Enable */
443                 .tcp_flags = 0,
444                 .priority = 2, /* Higher priority than IP */
445                 .queue = l1->udp_local_q,
446         };
447
448         return rte_eth_dev_filter_ctrl(l1->pmd_id,
449                 RTE_ETH_FILTER_NTUPLE,
450                 RTE_ETH_FILTER_ADD,
451                 &filter);
452 }
453
454 static inline int
455 app_link_filter_udp_del(struct app_link_params *l1, struct app_link_params *l2)
456 {
457         struct rte_eth_ntuple_filter filter = {
458                 .flags = RTE_5TUPLE_FLAGS,
459                 .dst_ip = rte_bswap32(l2->ip),
460                 .dst_ip_mask = UINT32_MAX, /* Enable */
461                 .src_ip = 0,
462                 .src_ip_mask = 0, /* Disable */
463                 .dst_port = 0,
464                 .dst_port_mask = 0, /* Disable */
465                 .src_port = 0,
466                 .src_port_mask = 0, /* Disable */
467                 .proto = IPPROTO_UDP,
468                 .proto_mask = UINT8_MAX, /* Enable */
469                 .tcp_flags = 0,
470                 .priority = 2, /* Higher priority than IP */
471                 .queue = l1->udp_local_q,
472         };
473
474         return rte_eth_dev_filter_ctrl(l1->pmd_id,
475                 RTE_ETH_FILTER_NTUPLE,
476                 RTE_ETH_FILTER_DELETE,
477                 &filter);
478 }
479
480 static inline int
481 app_link_filter_sctp_add(struct app_link_params *l1, struct app_link_params *l2)
482 {
483         struct rte_eth_ntuple_filter filter = {
484                 .flags = RTE_5TUPLE_FLAGS,
485                 .dst_ip = rte_bswap32(l2->ip),
486                 .dst_ip_mask = UINT32_MAX, /* Enable */
487                 .src_ip = 0,
488                 .src_ip_mask = 0, /* Disable */
489                 .dst_port = 0,
490                 .dst_port_mask = 0, /* Disable */
491                 .src_port = 0,
492                 .src_port_mask = 0, /* Disable */
493                 .proto = IPPROTO_SCTP,
494                 .proto_mask = UINT8_MAX, /* Enable */
495                 .tcp_flags = 0,
496                 .priority = 2, /* Higher priority than IP */
497                 .queue = l1->sctp_local_q,
498         };
499
500         return rte_eth_dev_filter_ctrl(l1->pmd_id,
501                 RTE_ETH_FILTER_NTUPLE,
502                 RTE_ETH_FILTER_ADD,
503                 &filter);
504 }
505
506 static inline int
507 app_link_filter_sctp_del(struct app_link_params *l1, struct app_link_params *l2)
508 {
509         struct rte_eth_ntuple_filter filter = {
510                 .flags = RTE_5TUPLE_FLAGS,
511                 .dst_ip = rte_bswap32(l2->ip),
512                 .dst_ip_mask = UINT32_MAX, /* Enable */
513                 .src_ip = 0,
514                 .src_ip_mask = 0, /* Disable */
515                 .dst_port = 0,
516                 .dst_port_mask = 0, /* Disable */
517                 .src_port = 0,
518                 .src_port_mask = 0, /* Disable */
519                 .proto = IPPROTO_SCTP,
520                 .proto_mask = UINT8_MAX, /* Enable */
521                 .tcp_flags = 0,
522                 .priority = 2, /* Higher priority than IP */
523                 .queue = l1->sctp_local_q,
524         };
525
526         return rte_eth_dev_filter_ctrl(l1->pmd_id,
527                 RTE_ETH_FILTER_NTUPLE,
528                 RTE_ETH_FILTER_DELETE,
529                 &filter);
530 }
531
532 static void
533 app_link_set_arp_filter(struct app_params *app, struct app_link_params *cp)
534 {
535         if (cp->arp_q != 0) {
536                 int status = app_link_filter_arp_add(cp);
537
538                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
539                         "Adding ARP filter (queue = %" PRIu32 ")",
540                         cp->name, cp->pmd_id, cp->arp_q);
541
542                 if (status)
543                         rte_panic("%s (%" PRIu32 "): "
544                                 "Error adding ARP filter "
545                                 "(queue = %" PRIu32 ") (%" PRId32 ")\n",
546                                 cp->name, cp->pmd_id, cp->arp_q, status);
547         }
548 }
549
550 static void
551 app_link_set_tcp_syn_filter(struct app_params *app, struct app_link_params *cp)
552 {
553         if (cp->tcp_syn_local_q != 0) {
554                 int status = app_link_filter_tcp_syn_add(cp);
555
556                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
557                         "Adding TCP SYN filter (queue = %" PRIu32 ")",
558                         cp->name, cp->pmd_id, cp->tcp_syn_local_q);
559
560                 if (status)
561                         rte_panic("%s (%" PRIu32 "): "
562                                 "Error adding TCP SYN filter "
563                                 "(queue = %" PRIu32 ") (%" PRId32 ")\n",
564                                 cp->name, cp->pmd_id, cp->tcp_syn_local_q,
565                                 status);
566         }
567 }
568
569 void
570 app_link_up_internal(struct app_params *app, struct app_link_params *cp)
571 {
572         uint32_t i;
573         int status;
574
575         /* For each link, add filters for IP of current link */
576         if (cp->ip != 0) {
577                 for (i = 0; i < app->n_links; i++) {
578                         struct app_link_params *p = &app->link_params[i];
579
580                         /* IP */
581                         if (p->ip_local_q != 0) {
582                                 int status = app_link_filter_ip_add(p, cp);
583
584                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
585                                         "Adding IP filter (queue= %" PRIu32
586                                         ", IP = 0x%08" PRIx32 ")",
587                                         p->name, p->pmd_id, p->ip_local_q,
588                                         cp->ip);
589
590                                 if (status)
591                                         rte_panic("%s (%" PRIu32 "): "
592                                                 "Error adding IP "
593                                                 "filter (queue= %" PRIu32 ", "
594                                                 "IP = 0x%08" PRIx32
595                                                 ") (%" PRId32 ")\n",
596                                                 p->name, p->pmd_id,
597                                                 p->ip_local_q, cp->ip, status);
598                         }
599
600                         /* TCP */
601                         if (p->tcp_local_q != 0) {
602                                 int status = app_link_filter_tcp_add(p, cp);
603
604                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
605                                         "Adding TCP filter "
606                                         "(queue = %" PRIu32
607                                         ", IP = 0x%08" PRIx32 ")",
608                                         p->name, p->pmd_id, p->tcp_local_q,
609                                         cp->ip);
610
611                                 if (status)
612                                         rte_panic("%s (%" PRIu32 "): "
613                                                 "Error adding TCP "
614                                                 "filter (queue = %" PRIu32 ", "
615                                                 "IP = 0x%08" PRIx32
616                                                 ") (%" PRId32 ")\n",
617                                                 p->name, p->pmd_id,
618                                                 p->tcp_local_q, cp->ip, status);
619                         }
620
621                         /* UDP */
622                         if (p->udp_local_q != 0) {
623                                 int status = app_link_filter_udp_add(p, cp);
624
625                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
626                                         "Adding UDP filter "
627                                         "(queue = %" PRIu32
628                                         ", IP = 0x%08" PRIx32 ")",
629                                         p->name, p->pmd_id, p->udp_local_q,
630                                         cp->ip);
631
632                                 if (status)
633                                         rte_panic("%s (%" PRIu32 "): "
634                                                 "Error adding UDP "
635                                                 "filter (queue = %" PRIu32 ", "
636                                                 "IP = 0x%08" PRIx32
637                                                 ") (%" PRId32 ")\n",
638                                                 p->name, p->pmd_id,
639                                                 p->udp_local_q, cp->ip, status);
640                         }
641
642                         /* SCTP */
643                         if (p->sctp_local_q != 0) {
644                                 int status = app_link_filter_sctp_add(p, cp);
645
646                                 APP_LOG(app, LOW, "%s (%" PRIu32
647                                         "): Adding SCTP filter "
648                                         "(queue = %" PRIu32
649                                         ", IP = 0x%08" PRIx32 ")",
650                                         p->name, p->pmd_id, p->sctp_local_q,
651                                         cp->ip);
652
653                                 if (status)
654                                         rte_panic("%s (%" PRIu32 "): "
655                                                 "Error adding SCTP "
656                                                 "filter (queue = %" PRIu32 ", "
657                                                 "IP = 0x%08" PRIx32
658                                                 ") (%" PRId32 ")\n",
659                                                 p->name, p->pmd_id,
660                                                 p->sctp_local_q, cp->ip,
661                                                 status);
662                         }
663                 }
664         }
665
666         /* PMD link up */
667         status = rte_eth_dev_set_link_up(cp->pmd_id);
668         if (status < 0)
669                 rte_panic("%s (%" PRIu32 "): PMD set up error %" PRId32 "\n",
670                         cp->name, cp->pmd_id, status);
671
672         /* Mark link as UP */
673         cp->state = 1;
674 }
675
676 void
677 app_link_down_internal(struct app_params *app, struct app_link_params *cp)
678 {
679         uint32_t i;
680
681         /* PMD link down */
682         rte_eth_dev_set_link_down(cp->pmd_id);
683
684         /* Mark link as DOWN */
685         cp->state = 0;
686
687         /* Return if current link IP is not valid */
688         if (cp->ip == 0)
689                 return;
690
691         /* For each link, remove filters for IP of current link */
692         for (i = 0; i < app->n_links; i++) {
693                 struct app_link_params *p = &app->link_params[i];
694
695                 /* IP */
696                 if (p->ip_local_q != 0) {
697                         int status = app_link_filter_ip_del(p, cp);
698
699                         APP_LOG(app, LOW, "%s (%" PRIu32
700                                 "): Deleting IP filter "
701                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
702                                 p->name, p->pmd_id, p->ip_local_q, cp->ip);
703
704                         if (status)
705                                 rte_panic("%s (%" PRIu32
706                                         "): Error deleting IP filter "
707                                         "(queue = %" PRIu32
708                                         ", IP = 0x%" PRIx32
709                                         ") (%" PRId32 ")\n",
710                                         p->name, p->pmd_id, p->ip_local_q,
711                                         cp->ip, status);
712                 }
713
714                 /* TCP */
715                 if (p->tcp_local_q != 0) {
716                         int status = app_link_filter_tcp_del(p, cp);
717
718                         APP_LOG(app, LOW, "%s (%" PRIu32
719                                 "): Deleting TCP filter "
720                                 "(queue = %" PRIu32
721                                 ", IP = 0x%" PRIx32 ")",
722                                 p->name, p->pmd_id, p->tcp_local_q, cp->ip);
723
724                         if (status)
725                                 rte_panic("%s (%" PRIu32
726                                         "): Error deleting TCP filter "
727                                         "(queue = %" PRIu32
728                                         ", IP = 0x%" PRIx32
729                                         ") (%" PRId32 ")\n",
730                                         p->name, p->pmd_id, p->tcp_local_q,
731                                         cp->ip, status);
732                 }
733
734                 /* UDP */
735                 if (p->udp_local_q != 0) {
736                         int status = app_link_filter_udp_del(p, cp);
737
738                         APP_LOG(app, LOW, "%s (%" PRIu32
739                                 "): Deleting UDP filter "
740                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
741                                 p->name, p->pmd_id, p->udp_local_q, cp->ip);
742
743                         if (status)
744                                 rte_panic("%s (%" PRIu32
745                                         "): Error deleting UDP filter "
746                                         "(queue = %" PRIu32
747                                         ", IP = 0x%" PRIx32
748                                         ") (%" PRId32 ")\n",
749                                         p->name, p->pmd_id, p->udp_local_q,
750                                         cp->ip, status);
751                 }
752
753                 /* SCTP */
754                 if (p->sctp_local_q != 0) {
755                         int status = app_link_filter_sctp_del(p, cp);
756
757                         APP_LOG(app, LOW, "%s (%" PRIu32
758                                 "): Deleting SCTP filter "
759                                 "(queue = %" PRIu32
760                                 ", IP = 0x%" PRIx32 ")",
761                                 p->name, p->pmd_id, p->sctp_local_q, cp->ip);
762
763                         if (status)
764                                 rte_panic("%s (%" PRIu32
765                                         "): Error deleting SCTP filter "
766                                         "(queue = %" PRIu32
767                                         ", IP = 0x%" PRIx32
768                                         ") (%" PRId32 ")\n",
769                                         p->name, p->pmd_id, p->sctp_local_q,
770                                         cp->ip, status);
771                 }
772         }
773 }
774
775 static void
776 app_check_link(struct app_params *app)
777 {
778         uint32_t all_links_up, i;
779
780         all_links_up = 1;
781
782         for (i = 0; i < app->n_links; i++) {
783                 struct app_link_params *p = &app->link_params[i];
784                 struct rte_eth_link link_params;
785
786                 memset(&link_params, 0, sizeof(link_params));
787                 rte_eth_link_get(p->pmd_id, &link_params);
788
789                 APP_LOG(app, HIGH, "%s (%" PRIu32 ") (%" PRIu32 " Gbps) %s",
790                         p->name,
791                         p->pmd_id,
792                         link_params.link_speed / 1000,
793                         link_params.link_status ? "UP" : "DOWN");
794
795                 if (link_params.link_status == 0)
796                         all_links_up = 0;
797         }
798
799         if (all_links_up == 0)
800                 rte_panic("Some links are DOWN\n");
801 }
802
803 static void
804 app_init_link(struct app_params *app)
805 {
806         uint32_t i;
807
808         for (i = 0; i < app->n_links; i++) {
809                 struct app_link_params *p_link = &app->link_params[i];
810                 uint32_t link_id, n_hwq_in, n_hwq_out, j;
811                 int status;
812
813                 sscanf(p_link->name, "LINK%" PRIu32, &link_id);
814                 n_hwq_in = app_link_get_n_rxq(app, p_link);
815                 n_hwq_out = app_link_get_n_txq(app, p_link);
816
817                 APP_LOG(app, HIGH, "Initializing %s (%" PRIu32") "
818                         "(%" PRIu32 " RXQ, %" PRIu32 " TXQ) ...",
819                         p_link->name,
820                         p_link->pmd_id,
821                         n_hwq_in,
822                         n_hwq_out);
823
824                 /* LINK */
825                 status = rte_eth_dev_configure(
826                         p_link->pmd_id,
827                         n_hwq_in,
828                         n_hwq_out,
829                         &p_link->conf);
830                 if (status < 0)
831                         rte_panic("%s (%" PRId32 "): "
832                                 "init error (%" PRId32 ")\n",
833                                 p_link->name, p_link->pmd_id, status);
834
835                 rte_eth_macaddr_get(p_link->pmd_id,
836                         (struct ether_addr *) &p_link->mac_addr);
837
838                 if (p_link->promisc)
839                         rte_eth_promiscuous_enable(p_link->pmd_id);
840
841                 /* RXQ */
842                 for (j = 0; j < app->n_pktq_hwq_in; j++) {
843                         struct app_pktq_hwq_in_params *p_rxq =
844                                 &app->hwq_in_params[j];
845                         uint32_t rxq_link_id, rxq_queue_id;
846
847                         sscanf(p_rxq->name, "RXQ%" PRIu32 ".%" PRIu32,
848                                 &rxq_link_id, &rxq_queue_id);
849                         if (rxq_link_id != link_id)
850                                 continue;
851
852                         status = rte_eth_rx_queue_setup(
853                                 p_link->pmd_id,
854                                 rxq_queue_id,
855                                 p_rxq->size,
856                                 rte_eth_dev_socket_id(p_link->pmd_id),
857                                 &p_rxq->conf,
858                                 app->mempool[p_rxq->mempool_id]);
859                         if (status < 0)
860                                 rte_panic("%s (%" PRIu32 "): "
861                                         "%s init error (%" PRId32 ")\n",
862                                         p_link->name,
863                                         p_link->pmd_id,
864                                         p_rxq->name,
865                                         status);
866                 }
867
868                 /* TXQ */
869                 for (j = 0; j < app->n_pktq_hwq_out; j++) {
870                         struct app_pktq_hwq_out_params *p_txq =
871                                 &app->hwq_out_params[j];
872                         uint32_t txq_link_id, txq_queue_id;
873
874                         sscanf(p_txq->name, "TXQ%" PRIu32 ".%" PRIu32,
875                                 &txq_link_id, &txq_queue_id);
876                         if (txq_link_id != link_id)
877                                 continue;
878
879                         status = rte_eth_tx_queue_setup(
880                                 p_link->pmd_id,
881                                 txq_queue_id,
882                                 p_txq->size,
883                                 rte_eth_dev_socket_id(p_link->pmd_id),
884                                 &p_txq->conf);
885                         if (status < 0)
886                                 rte_panic("%s (%" PRIu32 "): "
887                                         "%s init error (%" PRId32 ")\n",
888                                         p_link->name,
889                                         p_link->pmd_id,
890                                         p_txq->name,
891                                         status);
892                 }
893
894                 /* LINK START */
895                 status = rte_eth_dev_start(p_link->pmd_id);
896                 if (status < 0)
897                         rte_panic("Cannot start %s (error %" PRId32 ")\n",
898                                 p_link->name, status);
899
900                 /* LINK UP */
901                 app_link_set_arp_filter(app, p_link);
902                 app_link_set_tcp_syn_filter(app, p_link);
903                 app_link_up_internal(app, p_link);
904         }
905
906         app_check_link(app);
907 }
908
909 static void
910 app_init_swq(struct app_params *app)
911 {
912         uint32_t i;
913
914         for (i = 0; i < app->n_pktq_swq; i++) {
915                 struct app_pktq_swq_params *p = &app->swq_params[i];
916
917                 APP_LOG(app, HIGH, "Initializing %s...", p->name);
918                 app->swq[i] = rte_ring_create(
919                                 p->name,
920                                 p->size,
921                                 p->cpu_socket_id,
922                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
923
924                 if (app->swq[i] == NULL)
925                         rte_panic("%s init error\n", p->name);
926         }
927 }
928
929 static void
930 app_init_tm(struct app_params *app)
931 {
932         uint32_t i;
933
934         for (i = 0; i < app->n_pktq_tm; i++) {
935                 struct app_pktq_tm_params *p_tm = &app->tm_params[i];
936                 struct app_link_params *p_link;
937                 struct rte_eth_link link_eth_params;
938                 struct rte_sched_port *sched;
939                 uint32_t n_subports, subport_id;
940                 int status;
941
942                 p_link = app_get_link_for_tm(app, p_tm);
943                 /* LINK */
944                 rte_eth_link_get(p_link->pmd_id, &link_eth_params);
945
946                 /* TM */
947                 p_tm->sched_port_params.name = p_tm->name;
948                 p_tm->sched_port_params.socket =
949                         rte_eth_dev_socket_id(p_link->pmd_id);
950                 p_tm->sched_port_params.rate =
951                         (uint64_t) link_eth_params.link_speed * 1000 * 1000 / 8;
952
953                 APP_LOG(app, HIGH, "Initializing %s ...", p_tm->name);
954                 sched = rte_sched_port_config(&p_tm->sched_port_params);
955                 if (sched == NULL)
956                         rte_panic("%s init error\n", p_tm->name);
957                 app->tm[i] = sched;
958
959                 /* Subport */
960                 n_subports = p_tm->sched_port_params.n_subports_per_port;
961                 for (subport_id = 0; subport_id < n_subports; subport_id++) {
962                         uint32_t n_pipes_per_subport, pipe_id;
963
964                         status = rte_sched_subport_config(sched,
965                                 subport_id,
966                                 &p_tm->sched_subport_params[subport_id]);
967                         if (status)
968                                 rte_panic("%s subport %" PRIu32
969                                         " init error (%" PRId32 ")\n",
970                                         p_tm->name, subport_id, status);
971
972                         /* Pipe */
973                         n_pipes_per_subport =
974                                 p_tm->sched_port_params.n_pipes_per_subport;
975                         for (pipe_id = 0;
976                                 pipe_id < n_pipes_per_subport;
977                                 pipe_id++) {
978                                 int profile_id = p_tm->sched_pipe_to_profile[
979                                         subport_id * APP_MAX_SCHED_PIPES +
980                                         pipe_id];
981
982                                 if (profile_id == -1)
983                                         continue;
984
985                                 status = rte_sched_pipe_config(sched,
986                                         subport_id,
987                                         pipe_id,
988                                         profile_id);
989                                 if (status)
990                                         rte_panic("%s subport %" PRIu32
991                                                 " pipe %" PRIu32
992                                                 " (profile %" PRId32 ") "
993                                                 "init error (% " PRId32 ")\n",
994                                                 p_tm->name, subport_id, pipe_id,
995                                                 profile_id, status);
996                         }
997                 }
998         }
999 }
1000
1001 static void
1002 app_init_msgq(struct app_params *app)
1003 {
1004         uint32_t i;
1005
1006         for (i = 0; i < app->n_msgq; i++) {
1007                 struct app_msgq_params *p = &app->msgq_params[i];
1008
1009                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
1010                 app->msgq[i] = rte_ring_create(
1011                                 p->name,
1012                                 p->size,
1013                                 p->cpu_socket_id,
1014                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
1015
1016                 if (app->msgq[i] == NULL)
1017                         rte_panic("%s init error\n", p->name);
1018         }
1019 }
1020
1021 static void app_pipeline_params_get(struct app_params *app,
1022         struct app_pipeline_params *p_in,
1023         struct pipeline_params *p_out)
1024 {
1025         uint32_t i;
1026
1027         strcpy(p_out->name, p_in->name);
1028
1029         p_out->socket_id = (int) p_in->socket_id;
1030
1031         p_out->log_level = app->log_level;
1032
1033         /* pktq_in */
1034         p_out->n_ports_in = p_in->n_pktq_in;
1035         for (i = 0; i < p_in->n_pktq_in; i++) {
1036                 struct app_pktq_in_params *in = &p_in->pktq_in[i];
1037                 struct pipeline_port_in_params *out = &p_out->port_in[i];
1038
1039                 switch (in->type) {
1040                 case APP_PKTQ_IN_HWQ:
1041                 {
1042                         struct app_pktq_hwq_in_params *p_hwq_in =
1043                                 &app->hwq_in_params[in->id];
1044                         struct app_link_params *p_link =
1045                                 app_get_link_for_rxq(app, p_hwq_in);
1046                         uint32_t rxq_link_id, rxq_queue_id;
1047
1048                         sscanf(p_hwq_in->name, "RXQ%" SCNu32 ".%" SCNu32,
1049                                 &rxq_link_id,
1050                                 &rxq_queue_id);
1051
1052                         out->type = PIPELINE_PORT_IN_ETHDEV_READER;
1053                         out->params.ethdev.port_id = p_link->pmd_id;
1054                         out->params.ethdev.queue_id = rxq_queue_id;
1055                         out->burst_size = p_hwq_in->burst;
1056                         break;
1057                 }
1058                 case APP_PKTQ_IN_SWQ:
1059                         out->type = PIPELINE_PORT_IN_RING_READER;
1060                         out->params.ring.ring = app->swq[in->id];
1061                         out->burst_size = app->swq_params[in->id].burst_read;
1062                         /* What about frag and ras ports? */
1063                         break;
1064                 case APP_PKTQ_IN_TM:
1065                         out->type = PIPELINE_PORT_IN_SCHED_READER;
1066                         out->params.sched.sched = app->tm[in->id];
1067                         out->burst_size = app->tm_params[in->id].burst_read;
1068                         break;
1069                 case APP_PKTQ_IN_SOURCE:
1070                         out->type = PIPELINE_PORT_IN_SOURCE;
1071                         out->params.source.mempool = app->mempool[in->id];
1072                         out->burst_size = app->source_params[in->id].burst;
1073                         break;
1074                 default:
1075                         break;
1076                 }
1077         }
1078
1079         /* pktq_out */
1080         p_out->n_ports_out = p_in->n_pktq_out;
1081         for (i = 0; i < p_in->n_pktq_out; i++) {
1082                 struct app_pktq_out_params *in = &p_in->pktq_out[i];
1083                 struct pipeline_port_out_params *out = &p_out->port_out[i];
1084
1085                 switch (in->type) {
1086                 case APP_PKTQ_OUT_HWQ:
1087                 {
1088                         struct app_pktq_hwq_out_params *p_hwq_out =
1089                                 &app->hwq_out_params[in->id];
1090                         struct app_link_params *p_link =
1091                                 app_get_link_for_txq(app, p_hwq_out);
1092                         uint32_t txq_link_id, txq_queue_id;
1093
1094                         sscanf(p_hwq_out->name,
1095                                 "TXQ%" SCNu32 ".%" SCNu32,
1096                                 &txq_link_id,
1097                                 &txq_queue_id);
1098
1099                         if (p_hwq_out->dropless == 0) {
1100                                 struct rte_port_ethdev_writer_params *params =
1101                                         &out->params.ethdev;
1102
1103                                 out->type = PIPELINE_PORT_OUT_ETHDEV_WRITER;
1104                                 params->port_id = p_link->pmd_id;
1105                                 params->queue_id = txq_queue_id;
1106                                 params->tx_burst_sz =
1107                                         app->hwq_out_params[in->id].burst;
1108                         } else {
1109                                 struct rte_port_ethdev_writer_nodrop_params
1110                                         *params = &out->params.ethdev_nodrop;
1111
1112                                 out->type =
1113                                         PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP;
1114                                 params->port_id = p_link->pmd_id;
1115                                 params->queue_id = txq_queue_id;
1116                                 params->tx_burst_sz = p_hwq_out->burst;
1117                                 params->n_retries = p_hwq_out->n_retries;
1118                         }
1119                         break;
1120                 }
1121                 case APP_PKTQ_OUT_SWQ:
1122                         if (app->swq_params[in->id].dropless == 0) {
1123                                 struct rte_port_ring_writer_params *params =
1124                                         &out->params.ring;
1125
1126                                 out->type = PIPELINE_PORT_OUT_RING_WRITER;
1127                                 params->ring = app->swq[in->id];
1128                                 params->tx_burst_sz =
1129                                         app->swq_params[in->id].burst_write;
1130                         } else {
1131                                 struct rte_port_ring_writer_nodrop_params
1132                                         *params = &out->params.ring_nodrop;
1133
1134                                 out->type =
1135                                         PIPELINE_PORT_OUT_RING_WRITER_NODROP;
1136                                 params->ring = app->swq[in->id];
1137                                 params->tx_burst_sz =
1138                                         app->swq_params[in->id].burst_write;
1139                                 params->n_retries =
1140                                         app->swq_params[in->id].n_retries;
1141                         }
1142                         /* What about frag and ras ports? */
1143                         break;
1144                 case APP_PKTQ_OUT_TM: {
1145                         struct rte_port_sched_writer_params *params =
1146                                 &out->params.sched;
1147
1148                         out->type = PIPELINE_PORT_OUT_SCHED_WRITER;
1149                         params->sched = app->tm[in->id];
1150                         params->tx_burst_sz =
1151                                 app->tm_params[in->id].burst_write;
1152                         break;
1153                 }
1154                 case APP_PKTQ_OUT_SINK:
1155                         out->type = PIPELINE_PORT_OUT_SINK;
1156                         break;
1157                 default:
1158                         break;
1159                 }
1160         }
1161
1162         /* msgq */
1163         p_out->n_msgq = p_in->n_msgq_in;
1164
1165         for (i = 0; i < p_in->n_msgq_in; i++)
1166                 p_out->msgq_in[i] = app->msgq[p_in->msgq_in[i]];
1167
1168         for (i = 0; i < p_in->n_msgq_out; i++)
1169                 p_out->msgq_out[i] = app->msgq[p_in->msgq_out[i]];
1170
1171         /* args */
1172         p_out->n_args = p_in->n_args;
1173         for (i = 0; i < p_in->n_args; i++) {
1174                 p_out->args_name[i] = p_in->args_name[i];
1175                 p_out->args_value[i] = p_in->args_value[i];
1176         }
1177 }
1178
1179 static void
1180 app_init_pipelines(struct app_params *app)
1181 {
1182         uint32_t p_id;
1183
1184         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1185                 struct app_pipeline_params *params =
1186                         &app->pipeline_params[p_id];
1187                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1188                 struct pipeline_type *ptype;
1189                 struct pipeline_params pp;
1190
1191                 APP_LOG(app, HIGH, "Initializing %s ...", params->name);
1192
1193                 ptype = app_pipeline_type_find(app, params->type);
1194                 if (ptype == NULL)
1195                         rte_panic("Init error: Unknown pipeline type \"%s\"\n",
1196                                 params->type);
1197
1198                 app_pipeline_params_get(app, params, &pp);
1199
1200                 /* Back-end */
1201                 data->be = NULL;
1202                 if (ptype->be_ops->f_init) {
1203                         data->be = ptype->be_ops->f_init(&pp, (void *) app);
1204
1205                         if (data->be == NULL)
1206                                 rte_panic("Pipeline instance \"%s\" back-end "
1207                                         "init error\n", params->name);
1208                 }
1209
1210                 /* Front-end */
1211                 data->fe = NULL;
1212                 if (ptype->fe_ops->f_init) {
1213                         data->fe = ptype->fe_ops->f_init(&pp, (void *) app);
1214
1215                         if (data->fe == NULL)
1216                                 rte_panic("Pipeline instance \"%s\" front-end "
1217                                 "init error\n", params->name);
1218                 }
1219
1220                 data->timer_period = (rte_get_tsc_hz() * params->timer_period)
1221                         / 1000;
1222         }
1223 }
1224
1225 static void
1226 app_init_threads(struct app_params *app)
1227 {
1228         uint64_t time = rte_get_tsc_cycles();
1229         uint32_t p_id;
1230
1231         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1232                 struct app_pipeline_params *params =
1233                         &app->pipeline_params[p_id];
1234                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1235                 struct pipeline_type *ptype;
1236                 struct app_thread_data *t;
1237                 struct app_thread_pipeline_data *p;
1238                 int lcore_id;
1239
1240                 lcore_id = cpu_core_map_get_lcore_id(app->core_map,
1241                         params->socket_id,
1242                         params->core_id,
1243                         params->hyper_th_id);
1244
1245                 if (lcore_id < 0)
1246                         rte_panic("Invalid core s%" PRIu32 "c%" PRIu32 "%s\n",
1247                                 params->socket_id,
1248                                 params->core_id,
1249                                 (params->hyper_th_id) ? "h" : "");
1250
1251                 t = &app->thread_data[lcore_id];
1252
1253                 ptype = app_pipeline_type_find(app, params->type);
1254                 if (ptype == NULL)
1255                         rte_panic("Init error: Unknown pipeline "
1256                                 "type \"%s\"\n", params->type);
1257
1258                 p = (ptype->be_ops->f_run == NULL) ?
1259                         &t->regular[t->n_regular] :
1260                         &t->custom[t->n_custom];
1261
1262                 p->be = data->be;
1263                 p->f_run = ptype->be_ops->f_run;
1264                 p->f_timer = ptype->be_ops->f_timer;
1265                 p->timer_period = data->timer_period;
1266                 p->deadline = time + data->timer_period;
1267
1268                 if (ptype->be_ops->f_run == NULL)
1269                         t->n_regular++;
1270                 else
1271                         t->n_custom++;
1272         }
1273 }
1274
1275 int app_init(struct app_params *app)
1276 {
1277         app_init_core_map(app);
1278         app_init_core_mask(app);
1279
1280         app_init_eal(app);
1281         app_init_mempool(app);
1282         app_init_link(app);
1283         app_init_swq(app);
1284         app_init_tm(app);
1285         app_init_msgq(app);
1286
1287         app_pipeline_common_cmd_push(app);
1288         app_pipeline_type_register(app, &pipeline_master);
1289         app_pipeline_type_register(app, &pipeline_passthrough);
1290
1291         app_init_pipelines(app);
1292         app_init_threads(app);
1293
1294         return 0;
1295 }
1296
1297 static int
1298 app_pipeline_type_cmd_push(struct app_params *app,
1299         struct pipeline_type *ptype)
1300 {
1301         cmdline_parse_ctx_t *cmds;
1302         uint32_t n_cmds, i;
1303
1304         /* Check input arguments */
1305         if ((app == NULL) ||
1306                 (ptype == NULL))
1307                 return -EINVAL;
1308
1309         n_cmds = pipeline_type_cmds_count(ptype);
1310         if (n_cmds == 0)
1311                 return 0;
1312
1313         cmds = ptype->fe_ops->cmds;
1314
1315         /* Check for available slots in the application commands array */
1316         if (n_cmds > APP_MAX_CMDS - app->n_cmds)
1317                 return -ENOMEM;
1318
1319         /* Push pipeline commands into the application */
1320         memcpy(&app->cmds[app->n_cmds],
1321                 cmds,
1322                 n_cmds * sizeof(cmdline_parse_ctx_t *));
1323
1324         for (i = 0; i < n_cmds; i++)
1325                 app->cmds[app->n_cmds + i]->data = app;
1326
1327         app->n_cmds += n_cmds;
1328         app->cmds[app->n_cmds] = NULL;
1329
1330         return 0;
1331 }
1332
1333 int
1334 app_pipeline_type_register(struct app_params *app, struct pipeline_type *ptype)
1335 {
1336         uint32_t n_cmds, i;
1337
1338         /* Check input arguments */
1339         if ((app == NULL) ||
1340                 (ptype == NULL) ||
1341                 (ptype->name == NULL) ||
1342                 (strlen(ptype->name) == 0) ||
1343                 (ptype->be_ops->f_init == NULL) ||
1344                 (ptype->be_ops->f_timer == NULL))
1345                 return -EINVAL;
1346
1347         /* Check for duplicate entry */
1348         for (i = 0; i < app->n_pipeline_types; i++)
1349                 if (strcmp(app->pipeline_type[i].name, ptype->name) == 0)
1350                         return -EEXIST;
1351
1352         /* Check for resource availability */
1353         n_cmds = pipeline_type_cmds_count(ptype);
1354         if ((app->n_pipeline_types == APP_MAX_PIPELINE_TYPES) ||
1355                 (n_cmds > APP_MAX_CMDS - app->n_cmds))
1356                 return -ENOMEM;
1357
1358         /* Copy pipeline type */
1359         memcpy(&app->pipeline_type[app->n_pipeline_types++],
1360                 ptype,
1361                 sizeof(struct pipeline_type));
1362
1363         /* Copy CLI commands */
1364         if (n_cmds)
1365                 app_pipeline_type_cmd_push(app, ptype);
1366
1367         return 0;
1368 }
1369
1370 struct
1371 pipeline_type *app_pipeline_type_find(struct app_params *app, char *name)
1372 {
1373         uint32_t i;
1374
1375         for (i = 0; i < app->n_pipeline_types; i++)
1376                 if (strcmp(app->pipeline_type[i].name, name) == 0)
1377                         return &app->pipeline_type[i];
1378
1379         return NULL;
1380 }