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