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