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