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