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