d61e419cf1c49bc2a78ca7ad105420351d1743fd
[samplevnf.git] / VNFs / vCGNAPT / 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 "thread_fe.h"
34 #include "pipeline_cgnapt.h"
35 #include "pipeline_loadb.h"
36 #include "pipeline_timer.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
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                 printf("lcore_id:%d\n", lcore_id);
79
80                 if (lcore_id < 0)
81                         rte_panic("Cannot create CPU core mask\n");
82
83                 app_core_enable_in_core_mask(app, lcore_id);
84         }
85
86         app_core_build_core_mask_string(app, core_mask_str);
87         APP_LOG(app, HIGH, "CPU core mask = 0x%s", core_mask_str);
88
89 }
90
91 static void
92 app_init_eal(struct app_params *app)
93 {
94         char buffer[256];
95         char core_mask_str[APP_CORE_MASK_STRING_SIZE];
96         struct app_eal_params *p = &app->eal_params;
97         uint8_t n_args = 0;
98         uint32_t i;
99         int status;
100
101         app->eal_argv[n_args++] = strdup(app->app_name);
102
103         app_core_build_core_mask_string(app, core_mask_str);
104         snprintf(buffer, sizeof(buffer), "-c%s", core_mask_str);
105         app->eal_argv[n_args++] = strdup(buffer);
106
107         if (p->coremap) {
108                 snprintf(buffer, sizeof(buffer), "--lcores=%s", p->coremap);
109                 app->eal_argv[n_args++] = strdup(buffer);
110         }
111
112         if (p->master_lcore_present) {
113                 snprintf(buffer,
114                         sizeof(buffer),
115                         "--master-lcore=%" PRIu32,
116                         p->master_lcore);
117                 app->eal_argv[n_args++] = strdup(buffer);
118         }
119
120         snprintf(buffer, sizeof(buffer), "-n%" PRIu32, p->channels);
121         app->eal_argv[n_args++] = strdup(buffer);
122
123         if (p->memory_present) {
124                 snprintf(buffer, sizeof(buffer), "-m%" PRIu32, p->memory);
125                 app->eal_argv[n_args++] = strdup(buffer);
126         }
127
128         if (p->ranks_present) {
129                 snprintf(buffer, sizeof(buffer), "-r%" PRIu32, p->ranks);
130                 app->eal_argv[n_args++] = strdup(buffer);
131         }
132
133         for (i = 0; i < APP_MAX_LINKS; i++) {
134                 if (p->pci_blacklist[i] == NULL)
135                         break;
136
137                 snprintf(buffer,
138                         sizeof(buffer),
139                         "--pci-blacklist=%s",
140                         p->pci_blacklist[i]);
141                 app->eal_argv[n_args++] = strdup(buffer);
142         }
143
144         if (app->port_mask != 0)
145                 for (i = 0; i < APP_MAX_LINKS; i++) {
146                         if (p->pci_whitelist[i] == NULL)
147                                 break;
148
149                         snprintf(buffer,
150                                 sizeof(buffer),
151                                 "--pci-whitelist=%s",
152                                 p->pci_whitelist[i]);
153                         if (n_args < 255)
154                         app->eal_argv[n_args++] = strdup(buffer);
155                 }
156         else
157                 for (i = 0; i < app->n_links; i++) {
158                         char *pci_bdf = app->link_params[i].pci_bdf;
159
160                         snprintf(buffer,
161                                 sizeof(buffer),
162                                 "--pci-whitelist=%s",
163                                 pci_bdf);
164                         app->eal_argv[n_args++] = strdup(buffer);
165                 }
166
167         for (i = 0; i < APP_MAX_LINKS; i++) {
168                 if (p->vdev[i] == NULL)
169                         break;
170
171                 snprintf(buffer,
172                         sizeof(buffer),
173                         "--vdev=%s",
174                         p->vdev[i]);
175                 app->eal_argv[n_args++] = strdup(buffer);
176         }
177
178         if ((p->vmware_tsc_map_present) && p->vmware_tsc_map) {
179                 snprintf(buffer, sizeof(buffer), "--vmware-tsc-map");
180                 app->eal_argv[n_args++] = strdup(buffer);
181         }
182
183         if (p->proc_type) {
184                 snprintf(buffer,
185                         sizeof(buffer),
186                         "--proc-type=%s",
187                         p->proc_type);
188                 app->eal_argv[n_args++] = strdup(buffer);
189         }
190
191         if (p->syslog) {
192                 snprintf(buffer, sizeof(buffer), "--syslog=%s", p->syslog);
193                 app->eal_argv[n_args++] = strdup(buffer);
194         }
195
196         if (p->log_level_present) {
197                 snprintf(buffer,
198                         sizeof(buffer),
199                         "--log-level=%" PRIu32,
200                         p->log_level);
201                 app->eal_argv[n_args++] = strdup(buffer);
202         }
203
204         if ((p->version_present) && p->version) {
205                 snprintf(buffer, sizeof(buffer), "-v");
206                 app->eal_argv[n_args++] = strdup(buffer);
207         }
208
209         if ((p->help_present) && p->help) {
210                 snprintf(buffer, sizeof(buffer), "--help");
211                 app->eal_argv[n_args++] = strdup(buffer);
212         }
213
214         if ((p->no_huge_present) && p->no_huge) {
215                 snprintf(buffer, sizeof(buffer), "--no-huge");
216                 app->eal_argv[n_args++] = strdup(buffer);
217         }
218
219         if ((p->no_pci_present) && p->no_pci) {
220                 snprintf(buffer, sizeof(buffer), "--no-pci");
221                 app->eal_argv[n_args++] = strdup(buffer);
222         }
223
224         if ((p->no_hpet_present) && p->no_hpet) {
225                 snprintf(buffer, sizeof(buffer), "--no-hpet");
226                 app->eal_argv[n_args++] = strdup(buffer);
227         }
228
229         if ((p->no_shconf_present) && p->no_shconf) {
230                 snprintf(buffer, sizeof(buffer), "--no-shconf");
231                 app->eal_argv[n_args++] = strdup(buffer);
232         }
233
234         if (p->add_driver) {
235                 snprintf(buffer, sizeof(buffer), "-d=%s", p->add_driver);
236                 app->eal_argv[n_args++] = strdup(buffer);
237         }
238
239         if (p->socket_mem) {
240                 snprintf(buffer,
241                         sizeof(buffer),
242                         "--socket-mem=%s",
243                         p->socket_mem);
244                 app->eal_argv[n_args++] = strdup(buffer);
245         }
246
247         if (p->huge_dir) {
248                 snprintf(buffer, sizeof(buffer), "--huge-dir=%s", p->huge_dir);
249                 app->eal_argv[n_args++] = strdup(buffer);
250         }
251
252         if (p->file_prefix) {
253                 snprintf(buffer,
254                         sizeof(buffer),
255                         "--file-prefix=%s",
256                         p->file_prefix);
257                 app->eal_argv[n_args++] = strdup(buffer);
258         }
259
260         if (p->base_virtaddr) {
261                 snprintf(buffer,
262                         sizeof(buffer),
263                         "--base-virtaddr=%s",
264                         p->base_virtaddr);
265                 app->eal_argv[n_args++] = strdup(buffer);
266         }
267
268         if ((p->create_uio_dev_present) && p->create_uio_dev) {
269                 snprintf(buffer, sizeof(buffer), "--create-uio-dev");
270                 app->eal_argv[n_args++] = strdup(buffer);
271         }
272
273         if (p->vfio_intr) {
274                 snprintf(buffer,
275                         sizeof(buffer),
276                         "--vfio-intr=%s",
277                         p->vfio_intr);
278                 app->eal_argv[n_args++] = strdup(buffer);
279         }
280
281         if ((p->xen_dom0_present) && (p->xen_dom0)) {
282                 snprintf(buffer, sizeof(buffer), "--xen-dom0");
283                 app->eal_argv[n_args++] = strdup(buffer);
284         }
285
286         snprintf(buffer, sizeof(buffer), "--");
287         app->eal_argv[n_args++] = strdup(buffer);
288
289         app->eal_argc = n_args;
290
291         APP_LOG(app, HIGH, "Initializing EAL ...");
292         if (app->log_level >= APP_LOG_LEVEL_LOW) {
293                 int i;
294
295                 fprintf(stdout, "[APP] EAL arguments: \"");
296                 for (i = 1; i < app->eal_argc; i++)
297                         fprintf(stdout, "%s ", app->eal_argv[i]);
298                 fprintf(stdout, "\"\n");
299         }
300
301         status = rte_eal_init(app->eal_argc, app->eal_argv);
302         if (status < 0)
303                 rte_panic("EAL init error\n");
304 }
305 #if 0
306 static void
307 app_init_mempool(struct app_params *app)
308 {
309         uint32_t i;
310
311         for (i = 0; i < app->n_mempools; i++) {
312                 struct app_mempool_params *p = &app->mempool_params[i];
313
314                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
315                 app->mempool[i] = rte_mempool_create(
316                                 p->name,
317                                 p->pool_size,
318                                 p->buffer_size,
319                                 p->cache_size,
320                                 sizeof(struct rte_pktmbuf_pool_private),
321                                 rte_pktmbuf_pool_init, NULL,
322                                 rte_pktmbuf_init, NULL,
323                                 p->cpu_socket_id,
324                                 0);
325
326                 if (app->mempool[i] == NULL)
327                         rte_panic("%s init error\n", p->name);
328         }
329 }
330 #endif
331 static inline int
332 app_link_filter_arp_add(struct app_link_params *link)
333 {
334         struct rte_eth_ethertype_filter filter = {
335                 .ether_type = ETHER_TYPE_ARP,
336                 .flags = 0,
337                 .queue = link->arp_q,
338         };
339
340         return rte_eth_dev_filter_ctrl(link->pmd_id,
341                 RTE_ETH_FILTER_ETHERTYPE,
342                 RTE_ETH_FILTER_ADD,
343                 &filter);
344 }
345
346 static inline int
347 app_link_filter_tcp_syn_add(struct app_link_params *link)
348 {
349         struct rte_eth_syn_filter filter = {
350                 .hig_pri = 1,
351                 .queue = link->tcp_syn_q,
352         };
353
354         return rte_eth_dev_filter_ctrl(link->pmd_id,
355                 RTE_ETH_FILTER_SYN,
356                 RTE_ETH_FILTER_ADD,
357                 &filter);
358 }
359
360 static inline int
361 app_link_filter_ip_add(struct app_link_params *l1, struct app_link_params *l2)
362 {
363         struct rte_eth_ntuple_filter filter = {
364                 .flags = RTE_5TUPLE_FLAGS,
365                 .dst_ip = rte_bswap32(l2->ip),
366                 .dst_ip_mask = UINT32_MAX, /* Enable */
367                 .src_ip = 0,
368                 .src_ip_mask = 0, /* Disable */
369                 .dst_port = 0,
370                 .dst_port_mask = 0, /* Disable */
371                 .src_port = 0,
372                 .src_port_mask = 0, /* Disable */
373                 .proto = 0,
374                 .proto_mask = 0, /* Disable */
375                 .tcp_flags = 0,
376                 .priority = 1, /* Lowest */
377                 .queue = l1->ip_local_q,
378         };
379
380         return rte_eth_dev_filter_ctrl(l1->pmd_id,
381                 RTE_ETH_FILTER_NTUPLE,
382                 RTE_ETH_FILTER_ADD,
383                 &filter);
384 }
385
386 static inline int
387 app_link_filter_ip_del(struct app_link_params *l1, struct app_link_params *l2)
388 {
389         struct rte_eth_ntuple_filter filter = {
390                 .flags = RTE_5TUPLE_FLAGS,
391                 .dst_ip = rte_bswap32(l2->ip),
392                 .dst_ip_mask = UINT32_MAX, /* Enable */
393                 .src_ip = 0,
394                 .src_ip_mask = 0, /* Disable */
395                 .dst_port = 0,
396                 .dst_port_mask = 0, /* Disable */
397                 .src_port = 0,
398                 .src_port_mask = 0, /* Disable */
399                 .proto = 0,
400                 .proto_mask = 0, /* Disable */
401                 .tcp_flags = 0,
402                 .priority = 1, /* Lowest */
403                 .queue = l1->ip_local_q,
404         };
405
406         return rte_eth_dev_filter_ctrl(l1->pmd_id,
407                 RTE_ETH_FILTER_NTUPLE,
408                 RTE_ETH_FILTER_DELETE,
409                 &filter);
410 }
411
412 static inline int
413 app_link_filter_tcp_add(struct app_link_params *l1, struct app_link_params *l2)
414 {
415         struct rte_eth_ntuple_filter filter = {
416                 .flags = RTE_5TUPLE_FLAGS,
417                 .dst_ip = rte_bswap32(l2->ip),
418                 .dst_ip_mask = UINT32_MAX, /* Enable */
419                 .src_ip = 0,
420                 .src_ip_mask = 0, /* Disable */
421                 .dst_port = 0,
422                 .dst_port_mask = 0, /* Disable */
423                 .src_port = 0,
424                 .src_port_mask = 0, /* Disable */
425                 .proto = IPPROTO_TCP,
426                 .proto_mask = UINT8_MAX, /* Enable */
427                 .tcp_flags = 0,
428                 .priority = 2, /* Higher priority than IP */
429                 .queue = l1->tcp_local_q,
430         };
431
432         return rte_eth_dev_filter_ctrl(l1->pmd_id,
433                 RTE_ETH_FILTER_NTUPLE,
434                 RTE_ETH_FILTER_ADD,
435                 &filter);
436 }
437
438 static inline int
439 app_link_filter_tcp_del(struct app_link_params *l1, struct app_link_params *l2)
440 {
441         struct rte_eth_ntuple_filter filter = {
442                 .flags = RTE_5TUPLE_FLAGS,
443                 .dst_ip = rte_bswap32(l2->ip),
444                 .dst_ip_mask = UINT32_MAX, /* Enable */
445                 .src_ip = 0,
446                 .src_ip_mask = 0, /* Disable */
447                 .dst_port = 0,
448                 .dst_port_mask = 0, /* Disable */
449                 .src_port = 0,
450                 .src_port_mask = 0, /* Disable */
451                 .proto = IPPROTO_TCP,
452                 .proto_mask = UINT8_MAX, /* Enable */
453                 .tcp_flags = 0,
454                 .priority = 2, /* Higher priority than IP */
455                 .queue = l1->tcp_local_q,
456         };
457
458         return rte_eth_dev_filter_ctrl(l1->pmd_id,
459                 RTE_ETH_FILTER_NTUPLE,
460                 RTE_ETH_FILTER_DELETE,
461                 &filter);
462 }
463
464 static inline int
465 app_link_filter_udp_add(struct app_link_params *l1, struct app_link_params *l2)
466 {
467         struct rte_eth_ntuple_filter filter = {
468                 .flags = RTE_5TUPLE_FLAGS,
469                 .dst_ip = rte_bswap32(l2->ip),
470                 .dst_ip_mask = UINT32_MAX, /* Enable */
471                 .src_ip = 0,
472                 .src_ip_mask = 0, /* Disable */
473                 .dst_port = 0,
474                 .dst_port_mask = 0, /* Disable */
475                 .src_port = 0,
476                 .src_port_mask = 0, /* Disable */
477                 .proto = IPPROTO_UDP,
478                 .proto_mask = UINT8_MAX, /* Enable */
479                 .tcp_flags = 0,
480                 .priority = 2, /* Higher priority than IP */
481                 .queue = l1->udp_local_q,
482         };
483
484         return rte_eth_dev_filter_ctrl(l1->pmd_id,
485                 RTE_ETH_FILTER_NTUPLE,
486                 RTE_ETH_FILTER_ADD,
487                 &filter);
488 }
489
490 static inline int
491 app_link_filter_udp_del(struct app_link_params *l1, struct app_link_params *l2)
492 {
493         struct rte_eth_ntuple_filter filter = {
494                 .flags = RTE_5TUPLE_FLAGS,
495                 .dst_ip = rte_bswap32(l2->ip),
496                 .dst_ip_mask = UINT32_MAX, /* Enable */
497                 .src_ip = 0,
498                 .src_ip_mask = 0, /* Disable */
499                 .dst_port = 0,
500                 .dst_port_mask = 0, /* Disable */
501                 .src_port = 0,
502                 .src_port_mask = 0, /* Disable */
503                 .proto = IPPROTO_UDP,
504                 .proto_mask = UINT8_MAX, /* Enable */
505                 .tcp_flags = 0,
506                 .priority = 2, /* Higher priority than IP */
507                 .queue = l1->udp_local_q,
508         };
509
510         return rte_eth_dev_filter_ctrl(l1->pmd_id,
511                 RTE_ETH_FILTER_NTUPLE,
512                 RTE_ETH_FILTER_DELETE,
513                 &filter);
514 }
515
516 static inline int
517 app_link_filter_sctp_add(struct app_link_params *l1, struct app_link_params *l2)
518 {
519         struct rte_eth_ntuple_filter filter = {
520                 .flags = RTE_5TUPLE_FLAGS,
521                 .dst_ip = rte_bswap32(l2->ip),
522                 .dst_ip_mask = UINT32_MAX, /* Enable */
523                 .src_ip = 0,
524                 .src_ip_mask = 0, /* Disable */
525                 .dst_port = 0,
526                 .dst_port_mask = 0, /* Disable */
527                 .src_port = 0,
528                 .src_port_mask = 0, /* Disable */
529                 .proto = IPPROTO_SCTP,
530                 .proto_mask = UINT8_MAX, /* Enable */
531                 .tcp_flags = 0,
532                 .priority = 2, /* Higher priority than IP */
533                 .queue = l1->sctp_local_q,
534         };
535
536         return rte_eth_dev_filter_ctrl(l1->pmd_id,
537                 RTE_ETH_FILTER_NTUPLE,
538                 RTE_ETH_FILTER_ADD,
539                 &filter);
540 }
541
542 static inline int
543 app_link_filter_sctp_del(struct app_link_params *l1, struct app_link_params *l2)
544 {
545         struct rte_eth_ntuple_filter filter = {
546                 .flags = RTE_5TUPLE_FLAGS,
547                 .dst_ip = rte_bswap32(l2->ip),
548                 .dst_ip_mask = UINT32_MAX, /* Enable */
549                 .src_ip = 0,
550                 .src_ip_mask = 0, /* Disable */
551                 .dst_port = 0,
552                 .dst_port_mask = 0, /* Disable */
553                 .src_port = 0,
554                 .src_port_mask = 0, /* Disable */
555                 .proto = IPPROTO_SCTP,
556                 .proto_mask = UINT8_MAX, /* Enable */
557                 .tcp_flags = 0,
558                 .priority = 2, /* Higher priority than IP */
559                 .queue = l1->sctp_local_q,
560         };
561
562         return rte_eth_dev_filter_ctrl(l1->pmd_id,
563                 RTE_ETH_FILTER_NTUPLE,
564                 RTE_ETH_FILTER_DELETE,
565                 &filter);
566 }
567 #if 0
568 static void
569 app_link_set_arp_filter(struct app_params *app, struct app_link_params *cp)
570 {
571         if (cp->arp_q != 0) {
572                 int status = app_link_filter_arp_add(cp);
573
574                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
575                         "Adding ARP filter (queue = %" PRIu32 ")",
576                         cp->name, cp->pmd_id, cp->arp_q);
577
578                 if (status)
579                         rte_panic("%s (%" PRIu32 "): "
580                                 "Error adding ARP filter "
581                                 "(queue = %" PRIu32 ") (%" PRId32 ")\n",
582                                 cp->name, cp->pmd_id, cp->arp_q, status);
583         }
584 }
585
586 static void
587 app_link_set_tcp_syn_filter(struct app_params *app, struct app_link_params *cp)
588 {
589         if (cp->tcp_syn_q != 0) {
590                 int status = app_link_filter_tcp_syn_add(cp);
591
592                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
593                         "Adding TCP SYN filter (queue = %" PRIu32 ")",
594                         cp->name, cp->pmd_id, cp->tcp_syn_q);
595
596                 if (status)
597                         rte_panic("%s (%" PRIu32 "): "
598                                 "Error adding TCP SYN filter "
599                                 "(queue = %" PRIu32 ") (%" PRId32 ")\n",
600                                 cp->name, cp->pmd_id, cp->tcp_syn_q,
601                                 status);
602         }
603 }
604
605 /* rte_eth_dev is removed in DPDK version 16.11 and onwards */
606 #if RTE_VERSION < 0x100b0000
607 static int
608 app_link_is_virtual(__rte_unused struct app_link_params *p)
609 {
610         uint32_t pmd_id = p->pmd_id;
611         struct rte_eth_dev *dev = &rte_eth_devices[pmd_id];
612         if (dev->dev_type == RTE_ETH_DEV_VIRTUAL)
613                 return 1;
614         return 0;
615 }
616 #endif
617
618 #endif
619
620 void
621 app_link_up_internal(__rte_unused struct app_params *app, struct app_link_params *cp)
622 {
623 #if 0
624         uint32_t i;
625         int status;
626         struct rte_eth_link link;
627
628 #if RTE_VERSION < 0x100b0000
629         if (app_link_is_virtual(cp)) {
630                 cp->state = 1;
631                 return;
632         }
633 #endif
634
635         /* For each link, add filters for IP of current link */
636         if (cp->ip != 0) {
637                 for (i = 0; i < app->n_links; i++) {
638                         struct app_link_params *p = &app->link_params[i];
639
640                         /* IP */
641                         if (p->ip_local_q != 0) {
642                                 int status = app_link_filter_ip_add(p, cp);
643
644                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
645                                         "Adding IP filter (queue= %" PRIu32
646                                         ", IP = 0x%08" PRIx32 ")",
647                                         p->name, p->pmd_id, p->ip_local_q,
648                                         cp->ip);
649
650                                 if (status)
651                                         rte_panic("%s (%" PRIu32 "): "
652                                                 "Error adding IP "
653                                                 "filter (queue= %" PRIu32 ", "
654                                                 "IP = 0x%08" PRIx32
655                                                 ") (%" PRId32 ")\n",
656                                                 p->name, p->pmd_id,
657                                                 p->ip_local_q, cp->ip, status);
658                         }
659
660                         /* TCP */
661                         if (p->tcp_local_q != 0) {
662                                 int status = app_link_filter_tcp_add(p, cp);
663
664                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
665                                         "Adding TCP filter "
666                                         "(queue = %" PRIu32
667                                         ", IP = 0x%08" PRIx32 ")",
668                                         p->name, p->pmd_id, p->tcp_local_q,
669                                         cp->ip);
670
671                                 if (status)
672                                         rte_panic("%s (%" PRIu32 "): "
673                                                 "Error adding TCP "
674                                                 "filter (queue = %" PRIu32 ", "
675                                                 "IP = 0x%08" PRIx32
676                                                 ") (%" PRId32 ")\n",
677                                                 p->name, p->pmd_id,
678                                                 p->tcp_local_q, cp->ip, status);
679                         }
680
681                         /* UDP */
682                         if (p->udp_local_q != 0) {
683                                 int status = app_link_filter_udp_add(p, cp);
684
685                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
686                                         "Adding UDP filter "
687                                         "(queue = %" PRIu32
688                                         ", IP = 0x%08" PRIx32 ")",
689                                         p->name, p->pmd_id, p->udp_local_q,
690                                         cp->ip);
691
692                                 if (status)
693                                         rte_panic("%s (%" PRIu32 "): "
694                                                 "Error adding UDP "
695                                                 "filter (queue = %" PRIu32 ", "
696                                                 "IP = 0x%08" PRIx32
697                                                 ") (%" PRId32 ")\n",
698                                                 p->name, p->pmd_id,
699                                                 p->udp_local_q, cp->ip, status);
700                         }
701
702                         /* SCTP */
703                         if (p->sctp_local_q != 0) {
704                                 int status = app_link_filter_sctp_add(p, cp);
705
706                                 APP_LOG(app, LOW, "%s (%" PRIu32
707                                         "): Adding SCTP filter "
708                                         "(queue = %" PRIu32
709                                         ", IP = 0x%08" PRIx32 ")",
710                                         p->name, p->pmd_id, p->sctp_local_q,
711                                         cp->ip);
712
713                                 if (status)
714                                         rte_panic("%s (%" PRIu32 "): "
715                                                 "Error adding SCTP "
716                                                 "filter (queue = %" PRIu32 ", "
717                                                 "IP = 0x%08" PRIx32
718                                                 ") (%" PRId32 ")\n",
719                                                 p->name, p->pmd_id,
720                                                 p->sctp_local_q, cp->ip,
721                                                 status);
722                         }
723                 }
724         }
725
726         rte_eth_link_get(cp->pmd_id, &link);
727         if (!link.link_status) {
728                 /* PMD link up */
729                 status = rte_eth_dev_set_link_up(cp->pmd_id);
730                 if (status < 0)
731                         rte_panic("%s (%" PRIu32 "): PMD set link up error %"
732                                 PRId32 "\n", cp->name, cp->pmd_id, status);
733         }
734 #endif
735         ifm_update_linkstatus(cp->pmd_id, IFM_ETH_LINK_UP);
736
737         /* Mark link as UP */
738         cp->state = 1;
739 }
740
741 void
742 app_link_down_internal(__rte_unused struct app_params *app, struct app_link_params *cp)
743 {
744 #if 0
745         uint32_t i;
746         int status;
747         struct rte_eth_link link;
748
749 #if RTE_VERSION < 0x100b0000
750         if (app_link_is_virtual(cp)) {
751                 cp->state = 0;
752                 return;
753         }
754 #endif
755
756         rte_eth_link_get(cp->pmd_id, &link);
757         if (link.link_status) {
758                 /* PMD link down */
759                 status = rte_eth_dev_set_link_down(cp->pmd_id);
760                 if (status < 0)
761                         rte_panic("%s (%" PRIu32 "): PMD set link down error %"
762                                 PRId32 "\n", cp->name, cp->pmd_id, status);
763         }
764 #endif
765         ifm_update_linkstatus(cp->pmd_id, IFM_ETH_LINK_DOWN);
766         /* Mark link as DOWN */
767         cp->state = 0;
768
769         /* Return if current link IP is not valid */
770         if (cp->ip == 0)
771                 return;
772 #if 0
773         /* For each link, remove filters for IP of current link */
774         for (i = 0; i < app->n_links; i++) {
775                 struct app_link_params *p = &app->link_params[i];
776
777                 /* IP */
778                 if (p->ip_local_q != 0) {
779                         int status = app_link_filter_ip_del(p, cp);
780
781                         APP_LOG(app, LOW, "%s (%" PRIu32
782                                 "): Deleting IP filter "
783                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
784                                 p->name, p->pmd_id, p->ip_local_q, cp->ip);
785
786                         if (status)
787                                 rte_panic("%s (%" PRIu32
788                                         "): Error deleting IP filter "
789                                         "(queue = %" PRIu32
790                                         ", IP = 0x%" PRIx32
791                                         ") (%" PRId32 ")\n",
792                                         p->name, p->pmd_id, p->ip_local_q,
793                                         cp->ip, status);
794                 }
795
796                 /* TCP */
797                 if (p->tcp_local_q != 0) {
798                         int status = app_link_filter_tcp_del(p, cp);
799
800                         APP_LOG(app, LOW, "%s (%" PRIu32
801                                 "): Deleting TCP filter "
802                                 "(queue = %" PRIu32
803                                 ", IP = 0x%" PRIx32 ")",
804                                 p->name, p->pmd_id, p->tcp_local_q, cp->ip);
805
806                         if (status)
807                                 rte_panic("%s (%" PRIu32
808                                         "): Error deleting TCP filter "
809                                         "(queue = %" PRIu32
810                                         ", IP = 0x%" PRIx32
811                                         ") (%" PRId32 ")\n",
812                                         p->name, p->pmd_id, p->tcp_local_q,
813                                         cp->ip, status);
814                 }
815
816                 /* UDP */
817                 if (p->udp_local_q != 0) {
818                         int status = app_link_filter_udp_del(p, cp);
819
820                         APP_LOG(app, LOW, "%s (%" PRIu32
821                                 "): Deleting UDP filter "
822                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
823                                 p->name, p->pmd_id, p->udp_local_q, cp->ip);
824
825                         if (status)
826                                 rte_panic("%s (%" PRIu32
827                                         "): Error deleting UDP filter "
828                                         "(queue = %" PRIu32
829                                         ", IP = 0x%" PRIx32
830                                         ") (%" PRId32 ")\n",
831                                         p->name, p->pmd_id, p->udp_local_q,
832                                         cp->ip, status);
833                 }
834
835                 /* SCTP */
836                 if (p->sctp_local_q != 0) {
837                         int status = app_link_filter_sctp_del(p, cp);
838
839                         APP_LOG(app, LOW, "%s (%" PRIu32
840                                 "): Deleting SCTP filter "
841                                 "(queue = %" PRIu32
842                                 ", IP = 0x%" PRIx32 ")",
843                                 p->name, p->pmd_id, p->sctp_local_q, cp->ip);
844
845                         if (status)
846                                 rte_panic("%s (%" PRIu32
847                                         "): Error deleting SCTP filter "
848                                         "(queue = %" PRIu32
849                                         ", IP = 0x%" PRIx32
850                                         ") (%" PRId32 ")\n",
851                                         p->name, p->pmd_id, p->sctp_local_q,
852                                         cp->ip, status);
853                 }
854         }
855 #endif
856 }
857
858 static void
859 app_check_link(struct app_params *app)
860 {
861         uint32_t all_links_up, i;
862
863         all_links_up = 1;
864
865         for (i = 0; i < app->n_links; i++) {
866                 struct app_link_params *p = &app->link_params[i];
867                 struct rte_eth_link link_params;
868
869                 memset(&link_params, 0, sizeof(link_params));
870                 rte_eth_link_get(p->pmd_id, &link_params);
871
872                 APP_LOG(app, HIGH, "%s (%" PRIu32 ") (%" PRIu32 " Gbps) %s",
873                         p->name,
874                         p->pmd_id,
875                         link_params.link_speed / 1000,
876                         link_params.link_status ? "UP" : "DOWN");
877
878                 if (link_params.link_status == ETH_LINK_DOWN)
879                         all_links_up = 0;
880         }
881
882         if (all_links_up == 0)
883                 rte_panic("Some links are DOWN\n");
884 }
885
886 static uint32_t
887 is_any_swq_frag_or_ras(struct app_params *app)
888 {
889         uint32_t i;
890
891         for (i = 0; i < app->n_pktq_swq; i++) {
892                 struct app_pktq_swq_params *p = &app->swq_params[i];
893
894                 if ((p->ipv4_frag == 1) || (p->ipv6_frag == 1) ||
895                         (p->ipv4_ras == 1) || (p->ipv6_ras == 1))
896                         return 1;
897         }
898
899         return 0;
900 }
901
902 static void
903 app_init_link_frag_ras(struct app_params *app)
904 {
905         uint32_t i;
906
907         if (is_any_swq_frag_or_ras(app)) {
908                 for (i = 0; i < app->n_pktq_hwq_out; i++) {
909                         struct app_pktq_hwq_out_params *p_txq =
910                                 &app->hwq_out_params[i];
911
912                         p_txq->conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
913                 }
914         }
915 }
916
917 static inline int
918 app_get_cpu_socket_id(uint32_t pmd_id)
919 {
920         int status = rte_eth_dev_socket_id(pmd_id);
921
922         return (status != SOCKET_ID_ANY) ? status : 0;
923 }
924
925 struct rte_eth_rxmode rx_mode = {
926         .max_rx_pkt_len = ETHER_MAX_LEN, /**< Default maximum frame length. */
927         .split_hdr_size = 0,
928         .header_split   = 0, /**< Header Split disabled. */
929         .hw_ip_checksum = 0, /**< IP checksum offload disabled. */
930         .hw_vlan_filter = 1, /**< VLAN filtering enabled. */
931         .hw_vlan_strip  = 1, /**< VLAN strip enabled. */
932         .hw_vlan_extend = 0, /**< Extended VLAN disabled. */
933         .jumbo_frame    = 0, /**< Jumbo Frame Support disabled. */
934         .hw_strip_crc   = 0, /**< CRC stripping by hardware disabled. */
935 };
936 struct rte_fdir_conf fdir_conf = {
937         .mode = RTE_FDIR_MODE_NONE,
938         .pballoc = RTE_FDIR_PBALLOC_64K,
939         .status = RTE_FDIR_REPORT_STATUS,
940         .mask = {
941                 .vlan_tci_mask = 0x0,
942                 .ipv4_mask     = {
943                 .src_ip = 0xFFFFFFFF,
944                 .dst_ip = 0xFFFFFFFF,
945         },
946         .ipv6_mask     = {
947                 .src_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
948                 .dst_ip = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF},
949         },
950         .src_port_mask = 0xFFFF,
951         .dst_port_mask = 0xFFFF,
952         .mac_addr_byte_mask = 0xFF,
953         .tunnel_type_mask = 1,
954         .tunnel_id_mask = 0xFFFFFFFF,
955         },
956         .drop_queue = 127,
957 };
958
959 static void
960 app_init_link(struct app_params *app)
961 {
962         uint32_t i, size;
963
964         app_init_link_frag_ras(app);
965
966         /*
967          *Configuring port_config_t structure for interface manager initialization
968          */
969         size = RTE_CACHE_LINE_ROUNDUP(sizeof(port_config_t));
970         port_config = rte_zmalloc(NULL, (app->n_links * size), RTE_CACHE_LINE_SIZE);
971         if (port_config == NULL)
972                 rte_panic("port_config is NULL: Memory Allocation failure\n");
973
974         for (i = 0; i < app->n_links; i++) {
975                 struct app_link_params *p_link = &app->link_params[i];
976                 uint32_t link_id, n_hwq_in, n_hwq_out;
977                 int status;
978
979                 status = sscanf(p_link->name, "LINK%" PRIu32, &link_id);
980                 if (status < 0)
981                         rte_panic("%s (%" PRId32 "): "
982                                 "init error (%" PRId32 ")\n",
983                                 p_link->name, link_id, status);
984
985                 n_hwq_in = app_link_get_n_rxq(app, p_link);
986                 n_hwq_out = app_link_get_n_txq(app, p_link);
987
988                 printf("\n\nn_hwq_in %d\n", n_hwq_in);
989                 struct rte_eth_conf *My_local_conf = &p_link->conf;
990                 if(enable_hwlb)
991                 {
992                         My_local_conf->rxmode = rx_mode;
993                         My_local_conf->fdir_conf = fdir_conf;
994                         My_local_conf->rxmode.mq_mode = ETH_MQ_RX_RSS;
995                         My_local_conf->rx_adv_conf.rss_conf.rss_key = NULL;
996                         My_local_conf->rx_adv_conf.rss_conf.rss_hf = ETH_RSS_IP | ETH_RSS_UDP | ETH_RSS_TCP;
997                         /* pkt-filter-mode is perfect */
998                         My_local_conf->fdir_conf.mode = RTE_FDIR_MODE_PERFECT;
999                 } else {
1000                         /* disable-rss */
1001                         My_local_conf->rx_adv_conf.rss_conf.rss_hf = 0;
1002                 }
1003
1004                 /* Set the hardware CRC stripping to avoid double stripping of FCS in VM */
1005                 p_link->conf.rxmode.hw_strip_crc=1;
1006
1007                 APP_LOG(app, HIGH, "Initializing %s (%" PRIu32") "
1008                         "(%" PRIu32 " RXQ, %" PRIu32 " TXQ) ...",
1009                         p_link->name,
1010                         p_link->pmd_id,
1011                         n_hwq_in,
1012                         n_hwq_out);
1013
1014                 port_config[i].port_id = p_link->pmd_id;
1015                 port_config[i].nrx_queue = n_hwq_in;
1016                 port_config[i].ntx_queue = n_hwq_out;
1017                 port_config[i].state = 1;
1018                 port_config[i].promisc = p_link->promisc;
1019                 port_config[i].mempool.pool_size = app->mempool_params[0].pool_size;
1020                 port_config[i].mempool.buffer_size = app->mempool_params[0].buffer_size;
1021                 port_config[i].mempool.cache_size = app->mempool_params[0].cache_size;
1022                 port_config[i].mempool.cpu_socket_id = app->mempool_params[0].cpu_socket_id;
1023                 memcpy (&port_config[i].port_conf, &p_link->conf, sizeof(struct rte_eth_conf));
1024                 memcpy (&port_config[i].rx_conf, &app->hwq_in_params[0].conf, sizeof(struct rte_eth_rxconf));
1025                 memcpy (&port_config[i].tx_conf, &app->hwq_out_params[0].conf, sizeof(struct rte_eth_txconf));
1026
1027                 if(app->header_csum_req) {
1028                         /* Enable TCP and UDP HW Checksum */
1029                         port_config[i].tx_conf.txq_flags &=
1030                                 ~(ETH_TXQ_FLAGS_NOXSUMTCP|ETH_TXQ_FLAGS_NOXSUMUDP);
1031                 }
1032
1033                 if (ifm_port_setup (p_link->pmd_id, &port_config[i])) {
1034                         printf("Failed to configure port %s - %"PRIu32
1035                                ".\n", p_link->name, p_link->pmd_id);
1036                         printf("Try again with offload disabled....\n");
1037                         port_config[i].tx_conf.txq_flags |= ETH_TXQ_FLAGS_NOOFFLOADS;
1038                         if (ifm_port_setup (p_link->pmd_id, &port_config[i]))
1039                              rte_panic ("Port Setup Failed: %s - %"PRIu32"\n", p_link->name, p_link->pmd_id);
1040                 }
1041
1042 #if 0
1043                 /* LINK */
1044                 status = rte_eth_dev_configure(
1045                         p_link->pmd_id,
1046                         n_hwq_in,
1047                         n_hwq_out,
1048                         &p_link->conf);
1049                 if (status < 0)
1050                         rte_panic("%s (%" PRId32 "): "
1051                                 "init error (%" PRId32 ")\n",
1052                                 p_link->name, p_link->pmd_id, status);
1053
1054                 rte_eth_macaddr_get(p_link->pmd_id,
1055                         (struct ether_addr *) &p_link->mac_addr);
1056
1057                 if (p_link->promisc)
1058                         rte_eth_promiscuous_enable(p_link->pmd_id);
1059
1060                 /* RXQ */
1061                 for (j = 0; j < app->n_pktq_hwq_in; j++) {
1062                         struct app_pktq_hwq_in_params *p_rxq =
1063                                 &app->hwq_in_params[j];
1064                         uint32_t rxq_link_id, rxq_queue_id;
1065
1066                         status =
1067                         sscanf(p_rxq->name, "RXQ%" PRIu32 ".%" PRIu32,
1068                                 &rxq_link_id, &rxq_queue_id);
1069                         if (status < 0)
1070                                 rte_panic("%s (%" PRId32 "): "
1071                                 "init error (%" PRId32 ")\n",
1072                                 p_rxq->name, rxq_queue_id, status);
1073
1074                         if (rxq_link_id != link_id)
1075                                 continue;
1076
1077                         status = rte_eth_rx_queue_setup(
1078                                 p_link->pmd_id,
1079                                 rxq_queue_id,
1080                                 p_rxq->size,
1081                                 app_get_cpu_socket_id(p_link->pmd_id),
1082                                 &p_rxq->conf,
1083                                 app->mempool[p_rxq->mempool_id]);
1084                         if (status < 0)
1085                                 rte_panic("%s (%" PRIu32 "): "
1086                                         "%s init error (%" PRId32 ")\n",
1087                                         p_link->name,
1088                                         p_link->pmd_id,
1089                                         p_rxq->name,
1090                                         status);
1091                 }
1092
1093                 /* TXQ */
1094                 for (j = 0; j < app->n_pktq_hwq_out; j++) {
1095                         struct app_pktq_hwq_out_params *p_txq =
1096                                 &app->hwq_out_params[j];
1097                         uint32_t txq_link_id, txq_queue_id;
1098
1099                         status =
1100                         sscanf(p_txq->name, "TXQ%" PRIu32 ".%" PRIu32,
1101                                 &txq_link_id, &txq_queue_id);
1102
1103                         if (status < 0)
1104                                 rte_panic("%s (%" PRId32 "): "
1105                                 "init error (%" PRId32 ")\n",
1106                                 p_txq->name, txq_link_id, status);
1107
1108                         if (txq_link_id != link_id)
1109                                 continue;
1110
1111                         if (app->header_csum_req) {
1112                                 /* Enable TCP and UDP HW Checksum */
1113                                 p_txq->conf.txq_flags &=
1114                                         ~(ETH_TXQ_FLAGS_NOXSUMTCP|
1115                                         ETH_TXQ_FLAGS_NOXSUMUDP);
1116                         }
1117
1118                         status = rte_eth_tx_queue_setup(
1119                                 p_link->pmd_id,
1120                                 txq_queue_id,
1121                                 p_txq->size,
1122                                 app_get_cpu_socket_id(p_link->pmd_id),
1123                                 &p_txq->conf);
1124
1125                         if (status < 0)
1126                                 rte_panic("%s (%" PRIu32 "): "
1127                                         "%s init error (%" PRId32 ")\n",
1128                                         p_link->name,
1129                                         p_link->pmd_id,
1130                                         p_txq->name,
1131                                         status);
1132                 }
1133
1134                 /* LINK START */
1135                 status = rte_eth_dev_start(p_link->pmd_id);
1136                 if (status < 0)
1137                         rte_panic("Cannot start %s (error %" PRId32 ")\n",
1138                                 p_link->name, status);
1139
1140                 /* LINK UP */
1141                 app_link_set_arp_filter(app, p_link);
1142                 app_link_set_tcp_syn_filter(app, p_link);
1143 #endif
1144                 app_link_up_internal(app, p_link);
1145         }
1146
1147         app_check_link(app);
1148 }
1149
1150 static void
1151 app_init_swq(struct app_params *app)
1152 {
1153         uint32_t i;
1154
1155         for (i = 0; i < app->n_pktq_swq; i++) {
1156                 struct app_pktq_swq_params *p = &app->swq_params[i];
1157                 unsigned int flags = 0;
1158
1159                 if (app_swq_get_readers(app, p) == 1)
1160                         flags |= RING_F_SC_DEQ;
1161                 if (app_swq_get_writers(app, p) == 1)
1162                         flags |= RING_F_SP_ENQ;
1163
1164                 APP_LOG(app, HIGH, "Initializing %s...", p->name);
1165                 app->swq[i] = rte_ring_create(
1166                                 p->name,
1167                                 p->size,
1168                                 p->cpu_socket_id,
1169                                 flags);
1170
1171                 if (app->swq[i] == NULL)
1172                         rte_panic("%s init error\n", p->name);
1173         }
1174 }
1175
1176 static void
1177 app_init_tm(struct app_params *app)
1178 {
1179         uint32_t i;
1180
1181         for (i = 0; i < app->n_pktq_tm; i++) {
1182                 struct app_pktq_tm_params *p_tm = &app->tm_params[i];
1183                 struct app_link_params *p_link;
1184                 struct rte_eth_link link_eth_params;
1185                 struct rte_sched_port *sched;
1186                 uint32_t n_subports, subport_id;
1187                 int status;
1188
1189                 p_link = app_get_link_for_tm(app, p_tm);
1190                 /* LINK */
1191                 rte_eth_link_get(p_link->pmd_id, &link_eth_params);
1192
1193                 /* TM */
1194                 p_tm->sched_port_params.name = p_tm->name;
1195                 p_tm->sched_port_params.socket =
1196                         app_get_cpu_socket_id(p_link->pmd_id);
1197                 p_tm->sched_port_params.rate =
1198                         (uint64_t) link_eth_params.link_speed * 1000 * 1000 / 8;
1199
1200                 APP_LOG(app, HIGH, "Initializing %s ...", p_tm->name);
1201                 sched = rte_sched_port_config(&p_tm->sched_port_params);
1202                 if (sched == NULL)
1203                         rte_panic("%s init error\n", p_tm->name);
1204                 app->tm[i] = sched;
1205
1206                 /* Subport */
1207                 n_subports = p_tm->sched_port_params.n_subports_per_port;
1208                 for (subport_id = 0; subport_id < n_subports; subport_id++) {
1209                         uint32_t n_pipes_per_subport, pipe_id;
1210
1211                         status = rte_sched_subport_config(sched,
1212                                 subport_id,
1213                                 &p_tm->sched_subport_params[subport_id]);
1214                         if (status)
1215                                 rte_panic("%s subport %" PRIu32
1216                                         " init error (%" PRId32 ")\n",
1217                                         p_tm->name, subport_id, status);
1218
1219                         /* Pipe */
1220                         n_pipes_per_subport =
1221                                 p_tm->sched_port_params.n_pipes_per_subport;
1222                         for (pipe_id = 0;
1223                                 pipe_id < n_pipes_per_subport;
1224                                 pipe_id++) {
1225                                 int profile_id = p_tm->sched_pipe_to_profile[
1226                                         subport_id * APP_MAX_SCHED_PIPES +
1227                                         pipe_id];
1228
1229                                 if (profile_id == -1)
1230                                         continue;
1231
1232                                 status = rte_sched_pipe_config(sched,
1233                                         subport_id,
1234                                         pipe_id,
1235                                         profile_id);
1236                                 if (status)
1237                                         rte_panic("%s subport %" PRIu32
1238                                                 " pipe %" PRIu32
1239                                                 " (profile %" PRId32 ") "
1240                                                 "init error (% " PRId32 ")\n",
1241                                                 p_tm->name, subport_id, pipe_id,
1242                                                 profile_id, status);
1243                         }
1244                 }
1245         }
1246 }
1247
1248 static void
1249 app_init_msgq(struct app_params *app)
1250 {
1251         uint32_t i;
1252
1253         for (i = 0; i < app->n_msgq; i++) {
1254                 struct app_msgq_params *p = &app->msgq_params[i];
1255
1256                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
1257                 app->msgq[i] = rte_ring_create(
1258                                 p->name,
1259                                 p->size,
1260                                 p->cpu_socket_id,
1261                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
1262
1263                 if (app->msgq[i] == NULL)
1264                         rte_panic("%s init error\n", p->name);
1265         }
1266 }
1267
1268 static void app_pipeline_params_get(struct app_params *app,
1269         struct app_pipeline_params *p_in,
1270         struct pipeline_params *p_out)
1271 {
1272         uint32_t i;
1273         uint32_t mempool_id;
1274
1275         snprintf(p_out->name, PIPELINE_NAME_SIZE, "%s", p_in->name);
1276
1277         p_out->socket_id = (int) p_in->socket_id;
1278
1279         p_out->log_level = app->log_level;
1280
1281         /* pktq_in */
1282         p_out->n_ports_in = p_in->n_pktq_in;
1283         for (i = 0; i < p_in->n_pktq_in; i++) {
1284                 struct app_pktq_in_params *in = &p_in->pktq_in[i];
1285                 struct pipeline_port_in_params *out = &p_out->port_in[i];
1286
1287                 switch (in->type) {
1288                 case APP_PKTQ_IN_HWQ:
1289                 {
1290                         struct app_pktq_hwq_in_params *p_hwq_in =
1291                                 &app->hwq_in_params[in->id];
1292                         struct app_link_params *p_link =
1293                                 app_get_link_for_rxq(app, p_hwq_in);
1294                         uint32_t rxq_link_id, rxq_queue_id;
1295
1296                         int status =
1297                         sscanf(p_hwq_in->name, "RXQ%" SCNu32 ".%" SCNu32,
1298                                 &rxq_link_id,
1299                                 &rxq_queue_id);
1300                         if(status < 0)
1301                                 rte_panic("%s (%" PRId32 "): "
1302                                 "init error (%" PRId32 ")\n",
1303                                 p_hwq_in->name, rxq_link_id, status);
1304
1305                         out->type = PIPELINE_PORT_IN_ETHDEV_READER;
1306                         out->params.ethdev.port_id = p_link->pmd_id;
1307                         out->params.ethdev.queue_id = rxq_queue_id;
1308                         out->burst_size = p_hwq_in->burst;
1309                         break;
1310                 }
1311                 case APP_PKTQ_IN_SWQ:
1312                 {
1313                         struct app_pktq_swq_params *swq_params =
1314                                 &app->swq_params[in->id];
1315
1316                         if ((swq_params->ipv4_frag == 0) &&
1317                                 (swq_params->ipv6_frag == 0)) {
1318                                 if (app_swq_get_readers(app,
1319                                         swq_params) == 1) {
1320                                         out->type =
1321                                                 PIPELINE_PORT_IN_RING_READER;
1322                                         out->params.ring.ring =
1323                                                 app->swq[in->id];
1324                                         out->burst_size =
1325                                                 app->swq_params[in->id].
1326                                                         burst_read;
1327                                 } else {
1328                                 out->type = PIPELINE_PORT_IN_RING_MULTI_READER;
1329                                 out->params.ring_multi.ring = app->swq[in->id];
1330                                 out->burst_size = swq_params->burst_read;
1331                                 }
1332                         } else {
1333                                 if (swq_params->ipv4_frag == 1) {
1334                                 struct rte_port_ring_reader_ipv4_frag_params
1335                                         *params =
1336                                                 &out->params.ring_ipv4_frag;
1337
1338                                 out->type =
1339                                         PIPELINE_PORT_IN_RING_READER_IPV4_FRAG;
1340                                 params->ring = app->swq[in->id];
1341                                 params->mtu = swq_params->mtu;
1342                                 params->metadata_size =
1343                                         swq_params->metadata_size;
1344                                 params->pool_direct =
1345                                         app->mempool
1346                                         [swq_params->mempool_direct_id];
1347                                 params->pool_indirect =
1348                                         app->mempool
1349                                         [swq_params->mempool_indirect_id];
1350                                 out->burst_size = swq_params->burst_read;
1351                                 } else {
1352                                 struct rte_port_ring_reader_ipv6_frag_params
1353                                         *params =
1354                                                 &out->params.ring_ipv6_frag;
1355
1356                                 out->type =
1357                                         PIPELINE_PORT_IN_RING_READER_IPV6_FRAG;
1358                                 params->ring = app->swq[in->id];
1359                                 params->mtu = swq_params->mtu;
1360                                 params->metadata_size =
1361                                         swq_params->metadata_size;
1362                                 params->pool_direct =
1363                                         app->mempool
1364                                         [swq_params->mempool_direct_id];
1365                                 params->pool_indirect =
1366                                         app->mempool
1367                                         [swq_params->mempool_indirect_id];
1368                                 out->burst_size = swq_params->burst_read;
1369                                 }
1370                         }
1371                         break;
1372                 }
1373                 case APP_PKTQ_IN_TM:
1374                         out->type = PIPELINE_PORT_IN_SCHED_READER;
1375                         out->params.sched.sched = app->tm[in->id];
1376                         out->burst_size = app->tm_params[in->id].burst_read;
1377                         break;
1378                 case APP_PKTQ_IN_SOURCE:
1379                         mempool_id = app->source_params[in->id].mempool_id;
1380                         out->type = PIPELINE_PORT_IN_SOURCE;
1381                         out->params.source.mempool = app->mempool[mempool_id];
1382                         out->burst_size = app->source_params[in->id].burst;
1383
1384 #ifdef RTE_NEXT_ABI
1385                         if (app->source_params[in->id].file_name
1386                                 != NULL) {
1387                                 out->params.source.file_name = strdup(
1388                                         app->source_params[in->id].
1389                                         file_name);
1390                                 if (out->params.source.file_name == NULL) {
1391                                         out->params.source.
1392                                                 n_bytes_per_pkt = 0;
1393                                         break;
1394                                 }
1395                                 out->params.source.n_bytes_per_pkt =
1396                                         app->source_params[in->id].
1397                                         n_bytes_per_pkt;
1398                         }
1399 #endif
1400
1401                         break;
1402                 default:
1403                         break;
1404                 }
1405         }
1406
1407         /* pktq_out */
1408         p_out->n_ports_out = p_in->n_pktq_out;
1409         for (i = 0; i < p_in->n_pktq_out; i++) {
1410                 struct app_pktq_out_params *in = &p_in->pktq_out[i];
1411                 struct pipeline_port_out_params *out = &p_out->port_out[i];
1412
1413                 switch (in->type) {
1414                 case APP_PKTQ_OUT_HWQ:
1415                 {
1416                         struct app_pktq_hwq_out_params *p_hwq_out =
1417                                 &app->hwq_out_params[in->id];
1418                         struct app_link_params *p_link =
1419                                 app_get_link_for_txq(app, p_hwq_out);
1420                         uint32_t txq_link_id, txq_queue_id;
1421
1422                         int status =
1423                         sscanf(p_hwq_out->name,
1424                                 "TXQ%" SCNu32 ".%" SCNu32,
1425                                 &txq_link_id,
1426                                 &txq_queue_id);
1427                         if(status < 0)
1428                                 rte_panic("%s (%" PRId32 "): "
1429                                 "init error (%" PRId32 ")\n",
1430                                 p_hwq_out->name, txq_link_id, status);
1431
1432                         if (p_hwq_out->dropless == 0) {
1433                                 struct rte_port_ethdev_writer_params *params =
1434                                         &out->params.ethdev;
1435
1436                                 out->type = PIPELINE_PORT_OUT_ETHDEV_WRITER;
1437                                 params->port_id = p_link->pmd_id;
1438                                 params->queue_id = txq_queue_id;
1439                                 params->tx_burst_sz =
1440                                         app->hwq_out_params[in->id].burst;
1441                         } else {
1442                                 struct rte_port_ethdev_writer_nodrop_params
1443                                         *params = &out->params.ethdev_nodrop;
1444
1445                                 out->type =
1446                                         PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP;
1447                                 params->port_id = p_link->pmd_id;
1448                                 params->queue_id = txq_queue_id;
1449                                 params->tx_burst_sz = p_hwq_out->burst;
1450                                 params->n_retries = p_hwq_out->n_retries;
1451                         }
1452                         break;
1453                 }
1454                 case APP_PKTQ_OUT_SWQ:
1455                 {
1456                 struct app_pktq_swq_params *swq_params =
1457                         &app->swq_params[in->id];
1458
1459                 if ((swq_params->ipv4_ras == 0) &&
1460                         (swq_params->ipv6_ras == 0)) {
1461                         if (app_swq_get_writers(app, swq_params) == 1) {
1462                                 if (app->swq_params[in->id].dropless == 0) {
1463                                 struct rte_port_ring_writer_params *params =
1464                                         &out->params.ring;
1465
1466                                 out->type = PIPELINE_PORT_OUT_RING_WRITER;
1467                                 params->ring = app->swq[in->id];
1468                                 params->tx_burst_sz =
1469                                         app->swq_params[in->id].burst_write;
1470                                 } else {
1471                                 struct rte_port_ring_writer_nodrop_params
1472                                         *params = &out->params.ring_nodrop;
1473
1474                                 out->type =
1475                                         PIPELINE_PORT_OUT_RING_WRITER_NODROP;
1476                                 params->ring = app->swq[in->id];
1477                                 params->tx_burst_sz =
1478                                         app->swq_params[in->id].burst_write;
1479                                 params->n_retries =
1480                                 app->swq_params[in->id].n_retries;
1481                                 }
1482                         } else {
1483                                 if (swq_params->dropless == 0) {
1484                                 struct rte_port_ring_multi_writer_params
1485                                         *params =
1486                                                 &out->params.ring_multi;
1487
1488                                 out->type =
1489                                         PIPELINE_PORT_OUT_RING_MULTI_WRITER;
1490                                 params->ring = app->swq[in->id];
1491                                 params->tx_burst_sz = swq_params->burst_write;
1492                                 } else {
1493                                 struct rte_port_ring_multi_writer_nodrop_params
1494                                         *params =
1495                                                 &out->params.ring_multi_nodrop;
1496
1497                                 out->type =
1498                                 PIPELINE_PORT_OUT_RING_MULTI_WRITER_NODROP;
1499
1500                                 params->ring = app->swq[in->id];
1501                                 params->tx_burst_sz = swq_params->burst_write;
1502                                 params->n_retries = swq_params->n_retries;
1503                                 }
1504                                 }
1505                         } else {
1506                         if (swq_params->ipv4_ras == 1) {
1507                                 struct rte_port_ring_writer_ipv4_ras_params
1508                                         *params =
1509                                                 &out->params.ring_ipv4_ras;
1510
1511                                 out->type =
1512                                         PIPELINE_PORT_OUT_RING_WRITER_IPV4_RAS;
1513                                 params->ring = app->swq[in->id];
1514                                 params->tx_burst_sz = swq_params->burst_write;
1515                         } else {
1516                                 struct rte_port_ring_writer_ipv6_ras_params
1517                                         *params =
1518                                                 &out->params.ring_ipv6_ras;
1519
1520                                 out->type =
1521                                         PIPELINE_PORT_OUT_RING_WRITER_IPV6_RAS;
1522                                 params->ring = app->swq[in->id];
1523                                 params->tx_burst_sz = swq_params->burst_write;
1524                         }
1525                         }
1526                         break;
1527                 }
1528                 case APP_PKTQ_OUT_TM: {
1529                         struct rte_port_sched_writer_params *params =
1530                                 &out->params.sched;
1531
1532                         out->type = PIPELINE_PORT_OUT_SCHED_WRITER;
1533                         params->sched = app->tm[in->id];
1534                         params->tx_burst_sz =
1535                                 app->tm_params[in->id].burst_write;
1536                         break;
1537                 }
1538                 case APP_PKTQ_OUT_SINK:
1539                         out->type = PIPELINE_PORT_OUT_SINK;
1540                         if (app->sink_params[in->id].file_name != NULL) {
1541                                 out->params.sink.file_name = strdup(
1542                                         app->sink_params[in->id].
1543                                         file_name);
1544                                 if (out->params.sink.file_name == NULL) {
1545                                         out->params.sink.max_n_pkts = 0;
1546                                         break;
1547                                 }
1548                                 out->params.sink.max_n_pkts =
1549                                         app->sink_params[in->id].
1550                                         n_pkts_to_dump;
1551                         } else {
1552                                 out->params.sink.file_name = NULL;
1553                                 out->params.sink.max_n_pkts = 0;
1554                         }
1555                         break;
1556                 default:
1557                         break;
1558                 }
1559         }
1560
1561         /* msgq */
1562         p_out->n_msgq = p_in->n_msgq_in;
1563
1564         for (i = 0; i < p_in->n_msgq_in; i++)
1565                 p_out->msgq_in[i] = app->msgq[p_in->msgq_in[i]];
1566
1567         for (i = 0; i < p_in->n_msgq_out; i++)
1568                 p_out->msgq_out[i] = app->msgq[p_in->msgq_out[i]];
1569
1570         /* args */
1571         p_out->n_args = p_in->n_args;
1572         for (i = 0; i < p_in->n_args; i++) {
1573                 p_out->args_name[i] = p_in->args_name[i];
1574                 p_out->args_value[i] = p_in->args_value[i];
1575         }
1576 }
1577
1578 static void
1579 app_init_pipelines(struct app_params *app)
1580 {
1581         uint32_t p_id;
1582
1583         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1584                 struct app_pipeline_params *params =
1585                         &app->pipeline_params[p_id];
1586                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1587                 struct pipeline_type *ptype;
1588                 struct pipeline_params pp;
1589
1590                 APP_LOG(app, HIGH, "Initializing %s ...", params->name);
1591
1592                 ptype = app_pipeline_type_find(app, params->type);
1593                 if (ptype == NULL)
1594                         rte_panic("Init error: Unknown pipeline type \"%s\"\n",
1595                                 params->type);
1596
1597                 app_pipeline_params_get(app, params, &pp);
1598
1599                 /* Back-end */
1600                 data->be = NULL;
1601                 if (ptype->be_ops->f_init) {
1602                         data->be = ptype->be_ops->f_init(&pp, (void *) app);
1603
1604                         if (data->be == NULL)
1605                                 rte_panic("Pipeline instance \"%s\" back-end "
1606                                         "init error\n", params->name);
1607                 }
1608
1609                 /* Front-end */
1610                 data->fe = NULL;
1611                 if (ptype->fe_ops->f_init) {
1612                         data->fe = ptype->fe_ops->f_init(&pp, (void *) app);
1613
1614                         if (data->fe == NULL)
1615                                 rte_panic("Pipeline instance \"%s\" front-end "
1616                                 "init error\n", params->name);
1617                 }
1618
1619                 data->ptype = ptype;
1620
1621                 data->timer_period = (rte_get_tsc_hz() *
1622                         params->timer_period) / 100;
1623         }
1624 }
1625
1626 static void
1627 app_init_threads(struct app_params *app)
1628 {
1629         uint64_t time = rte_get_tsc_cycles();
1630         uint32_t p_id;
1631
1632         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1633                 struct app_pipeline_params *params =
1634                         &app->pipeline_params[p_id];
1635                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1636                 struct pipeline_type *ptype;
1637                 struct app_thread_data *t;
1638                 struct app_thread_pipeline_data *p;
1639                 int lcore_id;
1640
1641                 lcore_id = cpu_core_map_get_lcore_id(app->core_map,
1642                         params->socket_id,
1643                         params->core_id,
1644                         params->hyper_th_id);
1645
1646                 if (lcore_id < 0)
1647                         rte_panic("Invalid core s%" PRIu32 "c%" PRIu32 "%s\n",
1648                                 params->socket_id,
1649                                 params->core_id,
1650                                 (params->hyper_th_id) ? "h" : "");
1651
1652                 t = &app->thread_data[lcore_id];
1653
1654                 t->timer_period = (rte_get_tsc_hz() *
1655                         APP_THREAD_TIMER_PERIOD) / 1000;
1656                 t->thread_req_deadline = time + t->timer_period;
1657
1658                 t->headroom_cycles = 0;
1659                 t->headroom_time = rte_get_tsc_cycles();
1660                 t->headroom_ratio = 0.0;
1661
1662                 t->msgq_in = app_thread_msgq_in_get(app,
1663                                 params->socket_id,
1664                                 params->core_id,
1665                                 params->hyper_th_id);
1666                 if (t->msgq_in == NULL)
1667                         rte_panic("Init error: Cannot find MSGQ_IN "
1668                                 "for thread %" PRId32, lcore_id);
1669
1670                 t->msgq_out = app_thread_msgq_out_get(app,
1671                                 params->socket_id,
1672                                 params->core_id,
1673                                 params->hyper_th_id);
1674                 if (t->msgq_out == NULL)
1675                         rte_panic("Init error: Cannot find MSGQ_OUT "
1676                                 "for thread %" PRId32, lcore_id);
1677
1678                 ptype = app_pipeline_type_find(app, params->type);
1679                 if (ptype == NULL)
1680                         rte_panic("Init error: Unknown pipeline "
1681                                 "type \"%s\"\n", params->type);
1682
1683                 p = (ptype->be_ops->f_run == NULL) ?
1684                         &t->regular[t->n_regular] :
1685                         &t->custom[t->n_custom];
1686
1687                 p->pipeline_id = p_id;
1688                 p->be = data->be;
1689                 p->f_run = ptype->be_ops->f_run;
1690                 p->f_timer = ptype->be_ops->f_timer;
1691                 p->timer_period = data->timer_period;
1692                 p->deadline = time + data->timer_period;
1693
1694                 data->enabled = 1;
1695
1696                 if (ptype->be_ops->f_run == NULL)
1697                         t->n_regular++;
1698                 else
1699                         t->n_custom++;
1700         }
1701 }
1702
1703 int app_init(struct app_params *app)
1704 {
1705         app_init_core_map(app);
1706         app_init_core_mask(app);
1707
1708         app_init_eal(app);
1709         ifm_init();
1710         //app_init_mempool(app);
1711         app_init_link(app);
1712         app_init_swq(app);
1713         app_init_tm(app);
1714         app_init_msgq(app);
1715
1716         app_pipeline_common_cmd_push(app);
1717         app_pipeline_thread_cmd_push(app);
1718         app_pipeline_type_register(app, &pipeline_master);
1719         app_pipeline_type_register(app, &pipeline_cgnapt);
1720         app_pipeline_type_register(app, &pipeline_loadb);
1721         app_pipeline_type_register(app, &pipeline_timer);
1722         app_pipeline_type_register(app, &pipeline_txrx);
1723         app_pipeline_type_register(app, &pipeline_arpicmp);
1724
1725         app_init_pipelines(app);
1726         app_init_threads(app);
1727
1728         #ifdef L3_STACK_SUPPORT
1729         l3fwd_init();
1730         create_arp_table();
1731         create_nd_table();
1732         populate_lpm_routes();
1733         print_interface_details();
1734         #endif
1735
1736         return 0;
1737 }
1738
1739 static int
1740 app_pipeline_type_cmd_push(struct app_params *app,
1741         struct pipeline_type *ptype)
1742 {
1743         cmdline_parse_ctx_t *cmds;
1744         uint32_t n_cmds, i;
1745
1746         /* Check input arguments */
1747         if ((app == NULL) ||
1748                 (ptype == NULL))
1749                 return -EINVAL;
1750
1751         n_cmds = pipeline_type_cmds_count(ptype);
1752         if (n_cmds == 0)
1753                 return 0;
1754
1755         cmds = ptype->fe_ops->cmds;
1756
1757         /* Check for available slots in the application commands array */
1758         if (n_cmds > APP_MAX_CMDS - app->n_cmds)
1759                 return -ENOMEM;
1760
1761         /* Push pipeline commands into the application */
1762         memcpy(&app->cmds[app->n_cmds],
1763                 cmds,
1764                 n_cmds * sizeof(cmdline_parse_ctx_t));
1765
1766         for (i = 0; i < n_cmds; i++)
1767                 app->cmds[app->n_cmds + i]->data = app;
1768
1769         app->n_cmds += n_cmds;
1770         app->cmds[app->n_cmds] = NULL;
1771
1772         return 0;
1773 }
1774
1775 int
1776 app_pipeline_type_register(struct app_params *app, struct pipeline_type *ptype)
1777 {
1778         uint32_t n_cmds, i;
1779
1780         /* Check input arguments */
1781         if ((app == NULL) ||
1782                 (ptype == NULL) ||
1783                 (ptype->name == NULL) ||
1784                 (strlen(ptype->name) == 0) ||
1785                 (ptype->be_ops->f_init == NULL) ||
1786                 (ptype->be_ops->f_timer == NULL))
1787                 return -EINVAL;
1788
1789         /* Check for duplicate entry */
1790         for (i = 0; i < app->n_pipeline_types; i++)
1791                 if (strcmp(app->pipeline_type[i].name, ptype->name) == 0)
1792                         return -EEXIST;
1793
1794         /* Check for resource availability */
1795         n_cmds = pipeline_type_cmds_count(ptype);
1796         if ((app->n_pipeline_types == APP_MAX_PIPELINE_TYPES) ||
1797                 (n_cmds > APP_MAX_CMDS - app->n_cmds))
1798                 return -ENOMEM;
1799
1800         /* Copy pipeline type */
1801         memcpy(&app->pipeline_type[app->n_pipeline_types++],
1802                 ptype,
1803                 sizeof(struct pipeline_type));
1804
1805         /* Copy CLI commands */
1806         if (n_cmds)
1807                 app_pipeline_type_cmd_push(app, ptype);
1808
1809         return 0;
1810 }
1811
1812 struct
1813 pipeline_type *app_pipeline_type_find(struct app_params *app, char *name)
1814 {
1815         uint32_t i;
1816
1817         for (i = 0; i < app->n_pipeline_types; i++)
1818                 if (strcmp(app->pipeline_type[i].name, name) == 0)
1819                         return &app->pipeline_type[i];
1820
1821         return NULL;
1822 }