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