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