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