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