vCGNAPT VNF initial check-in
[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                         rte_panic ("Port Setup Failed: %s - %"PRIu32"\n", p_link->name, p_link->pmd_id);
1030
1031 #if 0
1032                 /* LINK */
1033                 status = rte_eth_dev_configure(
1034                         p_link->pmd_id,
1035                         n_hwq_in,
1036                         n_hwq_out,
1037                         &p_link->conf);
1038                 if (status < 0)
1039                         rte_panic("%s (%" PRId32 "): "
1040                                 "init error (%" PRId32 ")\n",
1041                                 p_link->name, p_link->pmd_id, status);
1042
1043                 rte_eth_macaddr_get(p_link->pmd_id,
1044                         (struct ether_addr *) &p_link->mac_addr);
1045
1046                 if (p_link->promisc)
1047                         rte_eth_promiscuous_enable(p_link->pmd_id);
1048
1049                 /* RXQ */
1050                 for (j = 0; j < app->n_pktq_hwq_in; j++) {
1051                         struct app_pktq_hwq_in_params *p_rxq =
1052                                 &app->hwq_in_params[j];
1053                         uint32_t rxq_link_id, rxq_queue_id;
1054
1055                         status =
1056                         sscanf(p_rxq->name, "RXQ%" PRIu32 ".%" PRIu32,
1057                                 &rxq_link_id, &rxq_queue_id);
1058                         if (status < 0)
1059                                 rte_panic("%s (%" PRId32 "): "
1060                                 "init error (%" PRId32 ")\n",
1061                                 p_rxq->name, rxq_queue_id, status);
1062
1063                         if (rxq_link_id != link_id)
1064                                 continue;
1065
1066                         status = rte_eth_rx_queue_setup(
1067                                 p_link->pmd_id,
1068                                 rxq_queue_id,
1069                                 p_rxq->size,
1070                                 app_get_cpu_socket_id(p_link->pmd_id),
1071                                 &p_rxq->conf,
1072                                 app->mempool[p_rxq->mempool_id]);
1073                         if (status < 0)
1074                                 rte_panic("%s (%" PRIu32 "): "
1075                                         "%s init error (%" PRId32 ")\n",
1076                                         p_link->name,
1077                                         p_link->pmd_id,
1078                                         p_rxq->name,
1079                                         status);
1080                 }
1081
1082                 /* TXQ */
1083                 for (j = 0; j < app->n_pktq_hwq_out; j++) {
1084                         struct app_pktq_hwq_out_params *p_txq =
1085                                 &app->hwq_out_params[j];
1086                         uint32_t txq_link_id, txq_queue_id;
1087
1088                         status =
1089                         sscanf(p_txq->name, "TXQ%" PRIu32 ".%" PRIu32,
1090                                 &txq_link_id, &txq_queue_id);
1091
1092                         if (status < 0)
1093                                 rte_panic("%s (%" PRId32 "): "
1094                                 "init error (%" PRId32 ")\n",
1095                                 p_txq->name, txq_link_id, status);
1096
1097                         if (txq_link_id != link_id)
1098                                 continue;
1099
1100                         if (app->header_csum_req) {
1101                                 /* Enable TCP and UDP HW Checksum */
1102                                 p_txq->conf.txq_flags &=
1103                                         ~(ETH_TXQ_FLAGS_NOXSUMTCP|
1104                                         ETH_TXQ_FLAGS_NOXSUMUDP);
1105                         }
1106
1107                         status = rte_eth_tx_queue_setup(
1108                                 p_link->pmd_id,
1109                                 txq_queue_id,
1110                                 p_txq->size,
1111                                 app_get_cpu_socket_id(p_link->pmd_id),
1112                                 &p_txq->conf);
1113
1114                         if (status < 0)
1115                                 rte_panic("%s (%" PRIu32 "): "
1116                                         "%s init error (%" PRId32 ")\n",
1117                                         p_link->name,
1118                                         p_link->pmd_id,
1119                                         p_txq->name,
1120                                         status);
1121                 }
1122
1123                 /* LINK START */
1124                 status = rte_eth_dev_start(p_link->pmd_id);
1125                 if (status < 0)
1126                         rte_panic("Cannot start %s (error %" PRId32 ")\n",
1127                                 p_link->name, status);
1128
1129                 /* LINK UP */
1130                 app_link_set_arp_filter(app, p_link);
1131                 app_link_set_tcp_syn_filter(app, p_link);
1132 #endif
1133                 app_link_up_internal(app, p_link);
1134         }
1135
1136         app_check_link(app);
1137 }
1138
1139 static void
1140 app_init_swq(struct app_params *app)
1141 {
1142         uint32_t i;
1143
1144         for (i = 0; i < app->n_pktq_swq; i++) {
1145                 struct app_pktq_swq_params *p = &app->swq_params[i];
1146                 unsigned int flags = 0;
1147
1148                 if (app_swq_get_readers(app, p) == 1)
1149                         flags |= RING_F_SC_DEQ;
1150                 if (app_swq_get_writers(app, p) == 1)
1151                         flags |= RING_F_SP_ENQ;
1152
1153                 APP_LOG(app, HIGH, "Initializing %s...", p->name);
1154                 app->swq[i] = rte_ring_create(
1155                                 p->name,
1156                                 p->size,
1157                                 p->cpu_socket_id,
1158                                 flags);
1159
1160                 if (app->swq[i] == NULL)
1161                         rte_panic("%s init error\n", p->name);
1162         }
1163 }
1164
1165 static void
1166 app_init_tm(struct app_params *app)
1167 {
1168         uint32_t i;
1169
1170         for (i = 0; i < app->n_pktq_tm; i++) {
1171                 struct app_pktq_tm_params *p_tm = &app->tm_params[i];
1172                 struct app_link_params *p_link;
1173                 struct rte_eth_link link_eth_params;
1174                 struct rte_sched_port *sched;
1175                 uint32_t n_subports, subport_id;
1176                 int status;
1177
1178                 p_link = app_get_link_for_tm(app, p_tm);
1179                 /* LINK */
1180                 rte_eth_link_get(p_link->pmd_id, &link_eth_params);
1181
1182                 /* TM */
1183                 p_tm->sched_port_params.name = p_tm->name;
1184                 p_tm->sched_port_params.socket =
1185                         app_get_cpu_socket_id(p_link->pmd_id);
1186                 p_tm->sched_port_params.rate =
1187                         (uint64_t) link_eth_params.link_speed * 1000 * 1000 / 8;
1188
1189                 APP_LOG(app, HIGH, "Initializing %s ...", p_tm->name);
1190                 sched = rte_sched_port_config(&p_tm->sched_port_params);
1191                 if (sched == NULL)
1192                         rte_panic("%s init error\n", p_tm->name);
1193                 app->tm[i] = sched;
1194
1195                 /* Subport */
1196                 n_subports = p_tm->sched_port_params.n_subports_per_port;
1197                 for (subport_id = 0; subport_id < n_subports; subport_id++) {
1198                         uint32_t n_pipes_per_subport, pipe_id;
1199
1200                         status = rte_sched_subport_config(sched,
1201                                 subport_id,
1202                                 &p_tm->sched_subport_params[subport_id]);
1203                         if (status)
1204                                 rte_panic("%s subport %" PRIu32
1205                                         " init error (%" PRId32 ")\n",
1206                                         p_tm->name, subport_id, status);
1207
1208                         /* Pipe */
1209                         n_pipes_per_subport =
1210                                 p_tm->sched_port_params.n_pipes_per_subport;
1211                         for (pipe_id = 0;
1212                                 pipe_id < n_pipes_per_subport;
1213                                 pipe_id++) {
1214                                 int profile_id = p_tm->sched_pipe_to_profile[
1215                                         subport_id * APP_MAX_SCHED_PIPES +
1216                                         pipe_id];
1217
1218                                 if (profile_id == -1)
1219                                         continue;
1220
1221                                 status = rte_sched_pipe_config(sched,
1222                                         subport_id,
1223                                         pipe_id,
1224                                         profile_id);
1225                                 if (status)
1226                                         rte_panic("%s subport %" PRIu32
1227                                                 " pipe %" PRIu32
1228                                                 " (profile %" PRId32 ") "
1229                                                 "init error (% " PRId32 ")\n",
1230                                                 p_tm->name, subport_id, pipe_id,
1231                                                 profile_id, status);
1232                         }
1233                 }
1234         }
1235 }
1236
1237 static void
1238 app_init_msgq(struct app_params *app)
1239 {
1240         uint32_t i;
1241
1242         for (i = 0; i < app->n_msgq; i++) {
1243                 struct app_msgq_params *p = &app->msgq_params[i];
1244
1245                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
1246                 app->msgq[i] = rte_ring_create(
1247                                 p->name,
1248                                 p->size,
1249                                 p->cpu_socket_id,
1250                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
1251
1252                 if (app->msgq[i] == NULL)
1253                         rte_panic("%s init error\n", p->name);
1254         }
1255 }
1256
1257 static void app_pipeline_params_get(struct app_params *app,
1258         struct app_pipeline_params *p_in,
1259         struct pipeline_params *p_out)
1260 {
1261         uint32_t i;
1262         uint32_t mempool_id;
1263
1264         snprintf(p_out->name, PIPELINE_NAME_SIZE, "%s", p_in->name);
1265
1266         p_out->socket_id = (int) p_in->socket_id;
1267
1268         p_out->log_level = app->log_level;
1269
1270         /* pktq_in */
1271         p_out->n_ports_in = p_in->n_pktq_in;
1272         for (i = 0; i < p_in->n_pktq_in; i++) {
1273                 struct app_pktq_in_params *in = &p_in->pktq_in[i];
1274                 struct pipeline_port_in_params *out = &p_out->port_in[i];
1275
1276                 switch (in->type) {
1277                 case APP_PKTQ_IN_HWQ:
1278                 {
1279                         struct app_pktq_hwq_in_params *p_hwq_in =
1280                                 &app->hwq_in_params[in->id];
1281                         struct app_link_params *p_link =
1282                                 app_get_link_for_rxq(app, p_hwq_in);
1283                         uint32_t rxq_link_id, rxq_queue_id;
1284
1285                         int status =
1286                         sscanf(p_hwq_in->name, "RXQ%" SCNu32 ".%" SCNu32,
1287                                 &rxq_link_id,
1288                                 &rxq_queue_id);
1289                         if(status < 0)
1290                                 rte_panic("%s (%" PRId32 "): "
1291                                 "init error (%" PRId32 ")\n",
1292                                 p_hwq_in->name, rxq_link_id, status);
1293
1294                         out->type = PIPELINE_PORT_IN_ETHDEV_READER;
1295                         out->params.ethdev.port_id = p_link->pmd_id;
1296                         out->params.ethdev.queue_id = rxq_queue_id;
1297                         out->burst_size = p_hwq_in->burst;
1298                         break;
1299                 }
1300                 case APP_PKTQ_IN_SWQ:
1301                 {
1302                         struct app_pktq_swq_params *swq_params =
1303                                 &app->swq_params[in->id];
1304
1305                         if ((swq_params->ipv4_frag == 0) &&
1306                                 (swq_params->ipv6_frag == 0)) {
1307                                 if (app_swq_get_readers(app,
1308                                         swq_params) == 1) {
1309                                         out->type =
1310                                                 PIPELINE_PORT_IN_RING_READER;
1311                                         out->params.ring.ring =
1312                                                 app->swq[in->id];
1313                                         out->burst_size =
1314                                                 app->swq_params[in->id].
1315                                                         burst_read;
1316                                 } else {
1317                                 out->type = PIPELINE_PORT_IN_RING_MULTI_READER;
1318                                 out->params.ring_multi.ring = app->swq[in->id];
1319                                 out->burst_size = swq_params->burst_read;
1320                                 }
1321                         } else {
1322                                 if (swq_params->ipv4_frag == 1) {
1323                                 struct rte_port_ring_reader_ipv4_frag_params
1324                                         *params =
1325                                                 &out->params.ring_ipv4_frag;
1326
1327                                 out->type =
1328                                         PIPELINE_PORT_IN_RING_READER_IPV4_FRAG;
1329                                 params->ring = app->swq[in->id];
1330                                 params->mtu = swq_params->mtu;
1331                                 params->metadata_size =
1332                                         swq_params->metadata_size;
1333                                 params->pool_direct =
1334                                         app->mempool
1335                                         [swq_params->mempool_direct_id];
1336                                 params->pool_indirect =
1337                                         app->mempool
1338                                         [swq_params->mempool_indirect_id];
1339                                 out->burst_size = swq_params->burst_read;
1340                                 } else {
1341                                 struct rte_port_ring_reader_ipv6_frag_params
1342                                         *params =
1343                                                 &out->params.ring_ipv6_frag;
1344
1345                                 out->type =
1346                                         PIPELINE_PORT_IN_RING_READER_IPV6_FRAG;
1347                                 params->ring = app->swq[in->id];
1348                                 params->mtu = swq_params->mtu;
1349                                 params->metadata_size =
1350                                         swq_params->metadata_size;
1351                                 params->pool_direct =
1352                                         app->mempool
1353                                         [swq_params->mempool_direct_id];
1354                                 params->pool_indirect =
1355                                         app->mempool
1356                                         [swq_params->mempool_indirect_id];
1357                                 out->burst_size = swq_params->burst_read;
1358                                 }
1359                         }
1360                         break;
1361                 }
1362                 case APP_PKTQ_IN_TM:
1363                         out->type = PIPELINE_PORT_IN_SCHED_READER;
1364                         out->params.sched.sched = app->tm[in->id];
1365                         out->burst_size = app->tm_params[in->id].burst_read;
1366                         break;
1367                 case APP_PKTQ_IN_SOURCE:
1368                         mempool_id = app->source_params[in->id].mempool_id;
1369                         out->type = PIPELINE_PORT_IN_SOURCE;
1370                         out->params.source.mempool = app->mempool[mempool_id];
1371                         out->burst_size = app->source_params[in->id].burst;
1372
1373 #ifdef RTE_NEXT_ABI
1374                         if (app->source_params[in->id].file_name
1375                                 != NULL) {
1376                                 out->params.source.file_name = strdup(
1377                                         app->source_params[in->id].
1378                                         file_name);
1379                                 if (out->params.source.file_name == NULL) {
1380                                         out->params.source.
1381                                                 n_bytes_per_pkt = 0;
1382                                         break;
1383                                 }
1384                                 out->params.source.n_bytes_per_pkt =
1385                                         app->source_params[in->id].
1386                                         n_bytes_per_pkt;
1387                         }
1388 #endif
1389
1390                         break;
1391                 default:
1392                         break;
1393                 }
1394         }
1395
1396         /* pktq_out */
1397         p_out->n_ports_out = p_in->n_pktq_out;
1398         for (i = 0; i < p_in->n_pktq_out; i++) {
1399                 struct app_pktq_out_params *in = &p_in->pktq_out[i];
1400                 struct pipeline_port_out_params *out = &p_out->port_out[i];
1401
1402                 switch (in->type) {
1403                 case APP_PKTQ_OUT_HWQ:
1404                 {
1405                         struct app_pktq_hwq_out_params *p_hwq_out =
1406                                 &app->hwq_out_params[in->id];
1407                         struct app_link_params *p_link =
1408                                 app_get_link_for_txq(app, p_hwq_out);
1409                         uint32_t txq_link_id, txq_queue_id;
1410
1411                         int status =
1412                         sscanf(p_hwq_out->name,
1413                                 "TXQ%" SCNu32 ".%" SCNu32,
1414                                 &txq_link_id,
1415                                 &txq_queue_id);
1416                         if(status < 0)
1417                                 rte_panic("%s (%" PRId32 "): "
1418                                 "init error (%" PRId32 ")\n",
1419                                 p_hwq_out->name, txq_link_id, status);
1420
1421                         if (p_hwq_out->dropless == 0) {
1422                                 struct rte_port_ethdev_writer_params *params =
1423                                         &out->params.ethdev;
1424
1425                                 out->type = PIPELINE_PORT_OUT_ETHDEV_WRITER;
1426                                 params->port_id = p_link->pmd_id;
1427                                 params->queue_id = txq_queue_id;
1428                                 params->tx_burst_sz =
1429                                         app->hwq_out_params[in->id].burst;
1430                         } else {
1431                                 struct rte_port_ethdev_writer_nodrop_params
1432                                         *params = &out->params.ethdev_nodrop;
1433
1434                                 out->type =
1435                                         PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP;
1436                                 params->port_id = p_link->pmd_id;
1437                                 params->queue_id = txq_queue_id;
1438                                 params->tx_burst_sz = p_hwq_out->burst;
1439                                 params->n_retries = p_hwq_out->n_retries;
1440                         }
1441                         break;
1442                 }
1443                 case APP_PKTQ_OUT_SWQ:
1444                 {
1445                 struct app_pktq_swq_params *swq_params =
1446                         &app->swq_params[in->id];
1447
1448                 if ((swq_params->ipv4_ras == 0) &&
1449                         (swq_params->ipv6_ras == 0)) {
1450                         if (app_swq_get_writers(app, swq_params) == 1) {
1451                                 if (app->swq_params[in->id].dropless == 0) {
1452                                 struct rte_port_ring_writer_params *params =
1453                                         &out->params.ring;
1454
1455                                 out->type = PIPELINE_PORT_OUT_RING_WRITER;
1456                                 params->ring = app->swq[in->id];
1457                                 params->tx_burst_sz =
1458                                         app->swq_params[in->id].burst_write;
1459                                 } else {
1460                                 struct rte_port_ring_writer_nodrop_params
1461                                         *params = &out->params.ring_nodrop;
1462
1463                                 out->type =
1464                                         PIPELINE_PORT_OUT_RING_WRITER_NODROP;
1465                                 params->ring = app->swq[in->id];
1466                                 params->tx_burst_sz =
1467                                         app->swq_params[in->id].burst_write;
1468                                 params->n_retries =
1469                                 app->swq_params[in->id].n_retries;
1470                                 }
1471                         } else {
1472                                 if (swq_params->dropless == 0) {
1473                                 struct rte_port_ring_multi_writer_params
1474                                         *params =
1475                                                 &out->params.ring_multi;
1476
1477                                 out->type =
1478                                         PIPELINE_PORT_OUT_RING_MULTI_WRITER;
1479                                 params->ring = app->swq[in->id];
1480                                 params->tx_burst_sz = swq_params->burst_write;
1481                                 } else {
1482                                 struct rte_port_ring_multi_writer_nodrop_params
1483                                         *params =
1484                                                 &out->params.ring_multi_nodrop;
1485
1486                                 out->type =
1487                                 PIPELINE_PORT_OUT_RING_MULTI_WRITER_NODROP;
1488
1489                                 params->ring = app->swq[in->id];
1490                                 params->tx_burst_sz = swq_params->burst_write;
1491                                 params->n_retries = swq_params->n_retries;
1492                                 }
1493                                 }
1494                         } else {
1495                         if (swq_params->ipv4_ras == 1) {
1496                                 struct rte_port_ring_writer_ipv4_ras_params
1497                                         *params =
1498                                                 &out->params.ring_ipv4_ras;
1499
1500                                 out->type =
1501                                         PIPELINE_PORT_OUT_RING_WRITER_IPV4_RAS;
1502                                 params->ring = app->swq[in->id];
1503                                 params->tx_burst_sz = swq_params->burst_write;
1504                         } else {
1505                                 struct rte_port_ring_writer_ipv6_ras_params
1506                                         *params =
1507                                                 &out->params.ring_ipv6_ras;
1508
1509                                 out->type =
1510                                         PIPELINE_PORT_OUT_RING_WRITER_IPV6_RAS;
1511                                 params->ring = app->swq[in->id];
1512                                 params->tx_burst_sz = swq_params->burst_write;
1513                         }
1514                         }
1515                         break;
1516                 }
1517                 case APP_PKTQ_OUT_TM: {
1518                         struct rte_port_sched_writer_params *params =
1519                                 &out->params.sched;
1520
1521                         out->type = PIPELINE_PORT_OUT_SCHED_WRITER;
1522                         params->sched = app->tm[in->id];
1523                         params->tx_burst_sz =
1524                                 app->tm_params[in->id].burst_write;
1525                         break;
1526                 }
1527                 case APP_PKTQ_OUT_SINK:
1528                         out->type = PIPELINE_PORT_OUT_SINK;
1529                         if (app->sink_params[in->id].file_name != NULL) {
1530                                 out->params.sink.file_name = strdup(
1531                                         app->sink_params[in->id].
1532                                         file_name);
1533                                 if (out->params.sink.file_name == NULL) {
1534                                         out->params.sink.max_n_pkts = 0;
1535                                         break;
1536                                 }
1537                                 out->params.sink.max_n_pkts =
1538                                         app->sink_params[in->id].
1539                                         n_pkts_to_dump;
1540                         } else {
1541                                 out->params.sink.file_name = NULL;
1542                                 out->params.sink.max_n_pkts = 0;
1543                         }
1544                         break;
1545                 default:
1546                         break;
1547                 }
1548         }
1549
1550         /* msgq */
1551         p_out->n_msgq = p_in->n_msgq_in;
1552
1553         for (i = 0; i < p_in->n_msgq_in; i++)
1554                 p_out->msgq_in[i] = app->msgq[p_in->msgq_in[i]];
1555
1556         for (i = 0; i < p_in->n_msgq_out; i++)
1557                 p_out->msgq_out[i] = app->msgq[p_in->msgq_out[i]];
1558
1559         /* args */
1560         p_out->n_args = p_in->n_args;
1561         for (i = 0; i < p_in->n_args; i++) {
1562                 p_out->args_name[i] = p_in->args_name[i];
1563                 p_out->args_value[i] = p_in->args_value[i];
1564         }
1565 }
1566
1567 static void
1568 app_init_pipelines(struct app_params *app)
1569 {
1570         uint32_t p_id;
1571
1572         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1573                 struct app_pipeline_params *params =
1574                         &app->pipeline_params[p_id];
1575                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1576                 struct pipeline_type *ptype;
1577                 struct pipeline_params pp;
1578
1579                 APP_LOG(app, HIGH, "Initializing %s ...", params->name);
1580
1581                 ptype = app_pipeline_type_find(app, params->type);
1582                 if (ptype == NULL)
1583                         rte_panic("Init error: Unknown pipeline type \"%s\"\n",
1584                                 params->type);
1585
1586                 app_pipeline_params_get(app, params, &pp);
1587
1588                 /* Back-end */
1589                 data->be = NULL;
1590                 if (ptype->be_ops->f_init) {
1591                         data->be = ptype->be_ops->f_init(&pp, (void *) app);
1592
1593                         if (data->be == NULL)
1594                                 rte_panic("Pipeline instance \"%s\" back-end "
1595                                         "init error\n", params->name);
1596                 }
1597
1598                 /* Front-end */
1599                 data->fe = NULL;
1600                 if (ptype->fe_ops->f_init) {
1601                         data->fe = ptype->fe_ops->f_init(&pp, (void *) app);
1602
1603                         if (data->fe == NULL)
1604                                 rte_panic("Pipeline instance \"%s\" front-end "
1605                                 "init error\n", params->name);
1606                 }
1607
1608                 data->ptype = ptype;
1609
1610                 data->timer_period = (rte_get_tsc_hz() *
1611                         params->timer_period) / 100;
1612         }
1613 }
1614
1615 static void
1616 app_init_threads(struct app_params *app)
1617 {
1618         uint64_t time = rte_get_tsc_cycles();
1619         uint32_t p_id;
1620
1621         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1622                 struct app_pipeline_params *params =
1623                         &app->pipeline_params[p_id];
1624                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1625                 struct pipeline_type *ptype;
1626                 struct app_thread_data *t;
1627                 struct app_thread_pipeline_data *p;
1628                 int lcore_id;
1629
1630                 lcore_id = cpu_core_map_get_lcore_id(app->core_map,
1631                         params->socket_id,
1632                         params->core_id,
1633                         params->hyper_th_id);
1634
1635                 if (lcore_id < 0)
1636                         rte_panic("Invalid core s%" PRIu32 "c%" PRIu32 "%s\n",
1637                                 params->socket_id,
1638                                 params->core_id,
1639                                 (params->hyper_th_id) ? "h" : "");
1640
1641                 t = &app->thread_data[lcore_id];
1642
1643                 t->timer_period = (rte_get_tsc_hz() *
1644                         APP_THREAD_TIMER_PERIOD) / 1000;
1645                 t->thread_req_deadline = time + t->timer_period;
1646
1647                 t->headroom_cycles = 0;
1648                 t->headroom_time = rte_get_tsc_cycles();
1649                 t->headroom_ratio = 0.0;
1650
1651                 t->msgq_in = app_thread_msgq_in_get(app,
1652                                 params->socket_id,
1653                                 params->core_id,
1654                                 params->hyper_th_id);
1655                 if (t->msgq_in == NULL)
1656                         rte_panic("Init error: Cannot find MSGQ_IN "
1657                                 "for thread %" PRId32, lcore_id);
1658
1659                 t->msgq_out = app_thread_msgq_out_get(app,
1660                                 params->socket_id,
1661                                 params->core_id,
1662                                 params->hyper_th_id);
1663                 if (t->msgq_out == NULL)
1664                         rte_panic("Init error: Cannot find MSGQ_OUT "
1665                                 "for thread %" PRId32, lcore_id);
1666
1667                 ptype = app_pipeline_type_find(app, params->type);
1668                 if (ptype == NULL)
1669                         rte_panic("Init error: Unknown pipeline "
1670                                 "type \"%s\"\n", params->type);
1671
1672                 p = (ptype->be_ops->f_run == NULL) ?
1673                         &t->regular[t->n_regular] :
1674                         &t->custom[t->n_custom];
1675
1676                 p->pipeline_id = p_id;
1677                 p->be = data->be;
1678                 p->f_run = ptype->be_ops->f_run;
1679                 p->f_timer = ptype->be_ops->f_timer;
1680                 p->timer_period = data->timer_period;
1681                 p->deadline = time + data->timer_period;
1682
1683                 data->enabled = 1;
1684
1685                 if (ptype->be_ops->f_run == NULL)
1686                         t->n_regular++;
1687                 else
1688                         t->n_custom++;
1689         }
1690 }
1691
1692 int app_init(struct app_params *app)
1693 {
1694         app_init_core_map(app);
1695         app_init_core_mask(app);
1696
1697         app_init_eal(app);
1698         ifm_init();
1699         //app_init_mempool(app);
1700         app_init_link(app);
1701         app_init_swq(app);
1702         app_init_tm(app);
1703         app_init_msgq(app);
1704
1705         app_pipeline_common_cmd_push(app);
1706         app_pipeline_thread_cmd_push(app);
1707         app_pipeline_type_register(app, &pipeline_master);
1708         app_pipeline_type_register(app, &pipeline_cgnapt);
1709         app_pipeline_type_register(app, &pipeline_loadb);
1710         app_pipeline_type_register(app, &pipeline_timer);
1711         app_pipeline_type_register(app, &pipeline_txrx);
1712         app_pipeline_type_register(app, &pipeline_arpicmp);
1713
1714         app_init_pipelines(app);
1715         app_init_threads(app);
1716
1717         l3fwd_init();
1718         create_arp_table();
1719         create_nd_table();
1720         populate_lpm_routes();
1721         print_interface_details();
1722
1723         return 0;
1724 }
1725
1726 static int
1727 app_pipeline_type_cmd_push(struct app_params *app,
1728         struct pipeline_type *ptype)
1729 {
1730         cmdline_parse_ctx_t *cmds;
1731         uint32_t n_cmds, i;
1732
1733         /* Check input arguments */
1734         if ((app == NULL) ||
1735                 (ptype == NULL))
1736                 return -EINVAL;
1737
1738         n_cmds = pipeline_type_cmds_count(ptype);
1739         if (n_cmds == 0)
1740                 return 0;
1741
1742         cmds = ptype->fe_ops->cmds;
1743
1744         /* Check for available slots in the application commands array */
1745         if (n_cmds > APP_MAX_CMDS - app->n_cmds)
1746                 return -ENOMEM;
1747
1748         /* Push pipeline commands into the application */
1749         memcpy(&app->cmds[app->n_cmds],
1750                 cmds,
1751                 n_cmds * sizeof(cmdline_parse_ctx_t));
1752
1753         for (i = 0; i < n_cmds; i++)
1754                 app->cmds[app->n_cmds + i]->data = app;
1755
1756         app->n_cmds += n_cmds;
1757         app->cmds[app->n_cmds] = NULL;
1758
1759         return 0;
1760 }
1761
1762 int
1763 app_pipeline_type_register(struct app_params *app, struct pipeline_type *ptype)
1764 {
1765         uint32_t n_cmds, i;
1766
1767         /* Check input arguments */
1768         if ((app == NULL) ||
1769                 (ptype == NULL) ||
1770                 (ptype->name == NULL) ||
1771                 (strlen(ptype->name) == 0) ||
1772                 (ptype->be_ops->f_init == NULL) ||
1773                 (ptype->be_ops->f_timer == NULL))
1774                 return -EINVAL;
1775
1776         /* Check for duplicate entry */
1777         for (i = 0; i < app->n_pipeline_types; i++)
1778                 if (strcmp(app->pipeline_type[i].name, ptype->name) == 0)
1779                         return -EEXIST;
1780
1781         /* Check for resource availability */
1782         n_cmds = pipeline_type_cmds_count(ptype);
1783         if ((app->n_pipeline_types == APP_MAX_PIPELINE_TYPES) ||
1784                 (n_cmds > APP_MAX_CMDS - app->n_cmds))
1785                 return -ENOMEM;
1786
1787         /* Copy pipeline type */
1788         memcpy(&app->pipeline_type[app->n_pipeline_types++],
1789                 ptype,
1790                 sizeof(struct pipeline_type));
1791
1792         /* Copy CLI commands */
1793         if (n_cmds)
1794                 app_pipeline_type_cmd_push(app, ptype);
1795
1796         return 0;
1797 }
1798
1799 struct
1800 pipeline_type *app_pipeline_type_find(struct app_params *app, char *name)
1801 {
1802         uint32_t i;
1803
1804         for (i = 0; i < app->n_pipeline_types; i++)
1805                 if (strcmp(app->pipeline_type[i].name, name) == 0)
1806                         return &app->pipeline_type[i];
1807
1808         return NULL;
1809 }