ethdev: get rid of device type
[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 void
610 app_link_up_internal(struct app_params *app, struct app_link_params *cp)
611 {
612         uint32_t i;
613         int status;
614
615         /* For each link, add filters for IP of current link */
616         if (cp->ip != 0) {
617                 for (i = 0; i < app->n_links; i++) {
618                         struct app_link_params *p = &app->link_params[i];
619
620                         /* IP */
621                         if (p->ip_local_q != 0) {
622                                 int status = app_link_filter_ip_add(p, cp);
623
624                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
625                                         "Adding IP filter (queue= %" PRIu32
626                                         ", IP = 0x%08" PRIx32 ")",
627                                         p->name, p->pmd_id, p->ip_local_q,
628                                         cp->ip);
629
630                                 if (status)
631                                         rte_panic("%s (%" PRIu32 "): "
632                                                 "Error adding IP "
633                                                 "filter (queue= %" PRIu32 ", "
634                                                 "IP = 0x%08" PRIx32
635                                                 ") (%" PRId32 ")\n",
636                                                 p->name, p->pmd_id,
637                                                 p->ip_local_q, cp->ip, status);
638                         }
639
640                         /* TCP */
641                         if (p->tcp_local_q != 0) {
642                                 int status = app_link_filter_tcp_add(p, cp);
643
644                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
645                                         "Adding TCP filter "
646                                         "(queue = %" PRIu32
647                                         ", IP = 0x%08" PRIx32 ")",
648                                         p->name, p->pmd_id, p->tcp_local_q,
649                                         cp->ip);
650
651                                 if (status)
652                                         rte_panic("%s (%" PRIu32 "): "
653                                                 "Error adding TCP "
654                                                 "filter (queue = %" PRIu32 ", "
655                                                 "IP = 0x%08" PRIx32
656                                                 ") (%" PRId32 ")\n",
657                                                 p->name, p->pmd_id,
658                                                 p->tcp_local_q, cp->ip, status);
659                         }
660
661                         /* UDP */
662                         if (p->udp_local_q != 0) {
663                                 int status = app_link_filter_udp_add(p, cp);
664
665                                 APP_LOG(app, LOW, "%s (%" PRIu32 "): "
666                                         "Adding UDP filter "
667                                         "(queue = %" PRIu32
668                                         ", IP = 0x%08" PRIx32 ")",
669                                         p->name, p->pmd_id, p->udp_local_q,
670                                         cp->ip);
671
672                                 if (status)
673                                         rte_panic("%s (%" PRIu32 "): "
674                                                 "Error adding UDP "
675                                                 "filter (queue = %" PRIu32 ", "
676                                                 "IP = 0x%08" PRIx32
677                                                 ") (%" PRId32 ")\n",
678                                                 p->name, p->pmd_id,
679                                                 p->udp_local_q, cp->ip, status);
680                         }
681
682                         /* SCTP */
683                         if (p->sctp_local_q != 0) {
684                                 int status = app_link_filter_sctp_add(p, cp);
685
686                                 APP_LOG(app, LOW, "%s (%" PRIu32
687                                         "): Adding SCTP filter "
688                                         "(queue = %" PRIu32
689                                         ", IP = 0x%08" PRIx32 ")",
690                                         p->name, p->pmd_id, p->sctp_local_q,
691                                         cp->ip);
692
693                                 if (status)
694                                         rte_panic("%s (%" PRIu32 "): "
695                                                 "Error adding SCTP "
696                                                 "filter (queue = %" PRIu32 ", "
697                                                 "IP = 0x%08" PRIx32
698                                                 ") (%" PRId32 ")\n",
699                                                 p->name, p->pmd_id,
700                                                 p->sctp_local_q, cp->ip,
701                                                 status);
702                         }
703                 }
704         }
705
706         /* PMD link up */
707         status = rte_eth_dev_set_link_up(cp->pmd_id);
708         if (status < 0)
709                 rte_panic("%s (%" PRIu32 "): PMD set link up error %"
710                         PRId32 "\n", cp->name, cp->pmd_id, status);
711
712         /* Mark link as UP */
713         cp->state = 1;
714 }
715
716 void
717 app_link_down_internal(struct app_params *app, struct app_link_params *cp)
718 {
719         uint32_t i;
720         int status;
721
722         /* PMD link down */
723         status = rte_eth_dev_set_link_down(cp->pmd_id);
724         if (status < 0)
725                 rte_panic("%s (%" PRIu32 "): PMD set link down error %"
726                         PRId32 "\n", cp->name, cp->pmd_id, status);
727
728         /* Mark link as DOWN */
729         cp->state = 0;
730
731         /* Return if current link IP is not valid */
732         if (cp->ip == 0)
733                 return;
734
735         /* For each link, remove filters for IP of current link */
736         for (i = 0; i < app->n_links; i++) {
737                 struct app_link_params *p = &app->link_params[i];
738
739                 /* IP */
740                 if (p->ip_local_q != 0) {
741                         int status = app_link_filter_ip_del(p, cp);
742
743                         APP_LOG(app, LOW, "%s (%" PRIu32
744                                 "): Deleting IP filter "
745                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
746                                 p->name, p->pmd_id, p->ip_local_q, cp->ip);
747
748                         if (status)
749                                 rte_panic("%s (%" PRIu32
750                                         "): Error deleting IP filter "
751                                         "(queue = %" PRIu32
752                                         ", IP = 0x%" PRIx32
753                                         ") (%" PRId32 ")\n",
754                                         p->name, p->pmd_id, p->ip_local_q,
755                                         cp->ip, status);
756                 }
757
758                 /* TCP */
759                 if (p->tcp_local_q != 0) {
760                         int status = app_link_filter_tcp_del(p, cp);
761
762                         APP_LOG(app, LOW, "%s (%" PRIu32
763                                 "): Deleting TCP filter "
764                                 "(queue = %" PRIu32
765                                 ", IP = 0x%" PRIx32 ")",
766                                 p->name, p->pmd_id, p->tcp_local_q, cp->ip);
767
768                         if (status)
769                                 rte_panic("%s (%" PRIu32
770                                         "): Error deleting TCP filter "
771                                         "(queue = %" PRIu32
772                                         ", IP = 0x%" PRIx32
773                                         ") (%" PRId32 ")\n",
774                                         p->name, p->pmd_id, p->tcp_local_q,
775                                         cp->ip, status);
776                 }
777
778                 /* UDP */
779                 if (p->udp_local_q != 0) {
780                         int status = app_link_filter_udp_del(p, cp);
781
782                         APP_LOG(app, LOW, "%s (%" PRIu32
783                                 "): Deleting UDP filter "
784                                 "(queue = %" PRIu32 ", IP = 0x%" PRIx32 ")",
785                                 p->name, p->pmd_id, p->udp_local_q, cp->ip);
786
787                         if (status)
788                                 rte_panic("%s (%" PRIu32
789                                         "): Error deleting UDP filter "
790                                         "(queue = %" PRIu32
791                                         ", IP = 0x%" PRIx32
792                                         ") (%" PRId32 ")\n",
793                                         p->name, p->pmd_id, p->udp_local_q,
794                                         cp->ip, status);
795                 }
796
797                 /* SCTP */
798                 if (p->sctp_local_q != 0) {
799                         int status = app_link_filter_sctp_del(p, cp);
800
801                         APP_LOG(app, LOW, "%s (%" PRIu32
802                                 "): Deleting SCTP filter "
803                                 "(queue = %" PRIu32
804                                 ", IP = 0x%" PRIx32 ")",
805                                 p->name, p->pmd_id, p->sctp_local_q, cp->ip);
806
807                         if (status)
808                                 rte_panic("%s (%" PRIu32
809                                         "): Error deleting SCTP filter "
810                                         "(queue = %" PRIu32
811                                         ", IP = 0x%" PRIx32
812                                         ") (%" PRId32 ")\n",
813                                         p->name, p->pmd_id, p->sctp_local_q,
814                                         cp->ip, status);
815                 }
816         }
817 }
818
819 static void
820 app_check_link(struct app_params *app)
821 {
822         uint32_t all_links_up, i;
823
824         all_links_up = 1;
825
826         for (i = 0; i < app->n_links; i++) {
827                 struct app_link_params *p = &app->link_params[i];
828                 struct rte_eth_link link_params;
829
830                 memset(&link_params, 0, sizeof(link_params));
831                 rte_eth_link_get(p->pmd_id, &link_params);
832
833                 APP_LOG(app, HIGH, "%s (%" PRIu32 ") (%" PRIu32 " Gbps) %s",
834                         p->name,
835                         p->pmd_id,
836                         link_params.link_speed / 1000,
837                         link_params.link_status ? "UP" : "DOWN");
838
839                 if (link_params.link_status == ETH_LINK_DOWN)
840                         all_links_up = 0;
841         }
842
843         if (all_links_up == 0)
844                 rte_panic("Some links are DOWN\n");
845 }
846
847 static uint32_t
848 is_any_swq_frag_or_ras(struct app_params *app)
849 {
850         uint32_t i;
851
852         for (i = 0; i < app->n_pktq_swq; i++) {
853                 struct app_pktq_swq_params *p = &app->swq_params[i];
854
855                 if ((p->ipv4_frag == 1) || (p->ipv6_frag == 1) ||
856                         (p->ipv4_ras == 1) || (p->ipv6_ras == 1))
857                         return 1;
858         }
859
860         return 0;
861 }
862
863 static void
864 app_init_link_frag_ras(struct app_params *app)
865 {
866         uint32_t i;
867
868         if (is_any_swq_frag_or_ras(app)) {
869                 for (i = 0; i < app->n_pktq_hwq_out; i++) {
870                         struct app_pktq_hwq_out_params *p_txq = &app->hwq_out_params[i];
871
872                         p_txq->conf.txq_flags &= ~ETH_TXQ_FLAGS_NOMULTSEGS;
873                 }
874         }
875 }
876
877 static inline int
878 app_get_cpu_socket_id(uint32_t pmd_id)
879 {
880         int status = rte_eth_dev_socket_id(pmd_id);
881
882         return (status != SOCKET_ID_ANY) ? status : 0;
883 }
884
885 static inline int
886 app_link_rss_enabled(struct app_link_params *cp)
887 {
888         return (cp->n_rss_qs) ? 1 : 0;
889 }
890
891 static void
892 app_link_rss_setup(struct app_link_params *cp)
893 {
894         struct rte_eth_dev_info dev_info;
895         struct rte_eth_rss_reta_entry64 reta_conf[APP_RETA_SIZE_MAX];
896         uint32_t i;
897         int status;
898
899     /* Get RETA size */
900         memset(&dev_info, 0, sizeof(dev_info));
901         rte_eth_dev_info_get(cp->pmd_id, &dev_info);
902
903         if (dev_info.reta_size == 0)
904                 rte_panic("%s (%u): RSS setup error (null RETA size)\n",
905                         cp->name, cp->pmd_id);
906
907         if (dev_info.reta_size > ETH_RSS_RETA_SIZE_512)
908                 rte_panic("%s (%u): RSS setup error (RETA size too big)\n",
909                         cp->name, cp->pmd_id);
910
911         /* Setup RETA contents */
912         memset(reta_conf, 0, sizeof(reta_conf));
913
914         for (i = 0; i < dev_info.reta_size; i++)
915                 reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
916
917         for (i = 0; i < dev_info.reta_size; i++) {
918                 uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
919                 uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
920                 uint32_t rss_qs_pos = i % cp->n_rss_qs;
921
922                 reta_conf[reta_id].reta[reta_pos] =
923                         (uint16_t) cp->rss_qs[rss_qs_pos];
924         }
925
926         /* RETA update */
927         status = rte_eth_dev_rss_reta_update(cp->pmd_id,
928                 reta_conf,
929                 dev_info.reta_size);
930         if (status != 0)
931                 rte_panic("%s (%u): RSS setup error (RETA update failed)\n",
932                         cp->name, cp->pmd_id);
933 }
934
935 static void
936 app_init_link_set_config(struct app_link_params *p)
937 {
938         if (p->n_rss_qs) {
939                 p->conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
940                 p->conf.rx_adv_conf.rss_conf.rss_hf = p->rss_proto_ipv4 |
941                         p->rss_proto_ipv6 |
942                         p->rss_proto_l2;
943         }
944 }
945
946 static void
947 app_init_link(struct app_params *app)
948 {
949         uint32_t i;
950
951         app_init_link_frag_ras(app);
952
953         for (i = 0; i < app->n_links; i++) {
954                 struct app_link_params *p_link = &app->link_params[i];
955                 uint32_t link_id, n_hwq_in, n_hwq_out, j;
956                 int status;
957
958                 sscanf(p_link->name, "LINK%" PRIu32, &link_id);
959                 n_hwq_in = app_link_get_n_rxq(app, p_link);
960                 n_hwq_out = app_link_get_n_txq(app, p_link);
961                 app_init_link_set_config(p_link);
962
963                 APP_LOG(app, HIGH, "Initializing %s (%" PRIu32") "
964                         "(%" PRIu32 " RXQ, %" PRIu32 " TXQ) ...",
965                         p_link->name,
966                         p_link->pmd_id,
967                         n_hwq_in,
968                         n_hwq_out);
969
970                 /* LINK */
971                 status = rte_eth_dev_configure(
972                         p_link->pmd_id,
973                         n_hwq_in,
974                         n_hwq_out,
975                         &p_link->conf);
976                 if (status < 0)
977                         rte_panic("%s (%" PRId32 "): "
978                                 "init error (%" PRId32 ")\n",
979                                 p_link->name, p_link->pmd_id, status);
980
981                 rte_eth_macaddr_get(p_link->pmd_id,
982                         (struct ether_addr *) &p_link->mac_addr);
983
984                 if (p_link->promisc)
985                         rte_eth_promiscuous_enable(p_link->pmd_id);
986
987                 /* RXQ */
988                 for (j = 0; j < app->n_pktq_hwq_in; j++) {
989                         struct app_pktq_hwq_in_params *p_rxq =
990                                 &app->hwq_in_params[j];
991                         uint32_t rxq_link_id, rxq_queue_id;
992
993                         sscanf(p_rxq->name, "RXQ%" PRIu32 ".%" PRIu32,
994                                 &rxq_link_id, &rxq_queue_id);
995                         if (rxq_link_id != link_id)
996                                 continue;
997
998                         status = rte_eth_rx_queue_setup(
999                                 p_link->pmd_id,
1000                                 rxq_queue_id,
1001                                 p_rxq->size,
1002                                 app_get_cpu_socket_id(p_link->pmd_id),
1003                                 &p_rxq->conf,
1004                                 app->mempool[p_rxq->mempool_id]);
1005                         if (status < 0)
1006                                 rte_panic("%s (%" PRIu32 "): "
1007                                         "%s init error (%" PRId32 ")\n",
1008                                         p_link->name,
1009                                         p_link->pmd_id,
1010                                         p_rxq->name,
1011                                         status);
1012                 }
1013
1014                 /* TXQ */
1015                 for (j = 0; j < app->n_pktq_hwq_out; j++) {
1016                         struct app_pktq_hwq_out_params *p_txq =
1017                                 &app->hwq_out_params[j];
1018                         uint32_t txq_link_id, txq_queue_id;
1019
1020                         sscanf(p_txq->name, "TXQ%" PRIu32 ".%" PRIu32,
1021                                 &txq_link_id, &txq_queue_id);
1022                         if (txq_link_id != link_id)
1023                                 continue;
1024
1025                         status = rte_eth_tx_queue_setup(
1026                                 p_link->pmd_id,
1027                                 txq_queue_id,
1028                                 p_txq->size,
1029                                 app_get_cpu_socket_id(p_link->pmd_id),
1030                                 &p_txq->conf);
1031                         if (status < 0)
1032                                 rte_panic("%s (%" PRIu32 "): "
1033                                         "%s init error (%" PRId32 ")\n",
1034                                         p_link->name,
1035                                         p_link->pmd_id,
1036                                         p_txq->name,
1037                                         status);
1038                 }
1039
1040                 /* LINK START */
1041                 status = rte_eth_dev_start(p_link->pmd_id);
1042                 if (status < 0)
1043                         rte_panic("Cannot start %s (error %" PRId32 ")\n",
1044                                 p_link->name, status);
1045
1046                 /* LINK FILTERS */
1047                 app_link_set_arp_filter(app, p_link);
1048                 app_link_set_tcp_syn_filter(app, p_link);
1049                 if (app_link_rss_enabled(p_link))
1050                         app_link_rss_setup(p_link);
1051
1052                 /* LINK UP */
1053                 app_link_up_internal(app, p_link);
1054         }
1055
1056         app_check_link(app);
1057 }
1058
1059 static void
1060 app_init_swq(struct app_params *app)
1061 {
1062         uint32_t i;
1063
1064         for (i = 0; i < app->n_pktq_swq; i++) {
1065                 struct app_pktq_swq_params *p = &app->swq_params[i];
1066                 unsigned flags = 0;
1067
1068                 if (app_swq_get_readers(app, p) == 1)
1069                         flags |= RING_F_SC_DEQ;
1070                 if (app_swq_get_writers(app, p) == 1)
1071                         flags |= RING_F_SP_ENQ;
1072
1073                 APP_LOG(app, HIGH, "Initializing %s...", p->name);
1074                 app->swq[i] = rte_ring_create(
1075                                 p->name,
1076                                 p->size,
1077                                 p->cpu_socket_id,
1078                                 flags);
1079
1080                 if (app->swq[i] == NULL)
1081                         rte_panic("%s init error\n", p->name);
1082         }
1083 }
1084
1085 static void
1086 app_init_tm(struct app_params *app)
1087 {
1088         uint32_t i;
1089
1090         for (i = 0; i < app->n_pktq_tm; i++) {
1091                 struct app_pktq_tm_params *p_tm = &app->tm_params[i];
1092                 struct app_link_params *p_link;
1093                 struct rte_eth_link link_eth_params;
1094                 struct rte_sched_port *sched;
1095                 uint32_t n_subports, subport_id;
1096                 int status;
1097
1098                 p_link = app_get_link_for_tm(app, p_tm);
1099                 /* LINK */
1100                 rte_eth_link_get(p_link->pmd_id, &link_eth_params);
1101
1102                 /* TM */
1103                 p_tm->sched_port_params.name = p_tm->name;
1104                 p_tm->sched_port_params.socket =
1105                         app_get_cpu_socket_id(p_link->pmd_id);
1106                 p_tm->sched_port_params.rate =
1107                         (uint64_t) link_eth_params.link_speed * 1000 * 1000 / 8;
1108
1109                 APP_LOG(app, HIGH, "Initializing %s ...", p_tm->name);
1110                 sched = rte_sched_port_config(&p_tm->sched_port_params);
1111                 if (sched == NULL)
1112                         rte_panic("%s init error\n", p_tm->name);
1113                 app->tm[i] = sched;
1114
1115                 /* Subport */
1116                 n_subports = p_tm->sched_port_params.n_subports_per_port;
1117                 for (subport_id = 0; subport_id < n_subports; subport_id++) {
1118                         uint32_t n_pipes_per_subport, pipe_id;
1119
1120                         status = rte_sched_subport_config(sched,
1121                                 subport_id,
1122                                 &p_tm->sched_subport_params[subport_id]);
1123                         if (status)
1124                                 rte_panic("%s subport %" PRIu32
1125                                         " init error (%" PRId32 ")\n",
1126                                         p_tm->name, subport_id, status);
1127
1128                         /* Pipe */
1129                         n_pipes_per_subport =
1130                                 p_tm->sched_port_params.n_pipes_per_subport;
1131                         for (pipe_id = 0;
1132                                 pipe_id < n_pipes_per_subport;
1133                                 pipe_id++) {
1134                                 int profile_id = p_tm->sched_pipe_to_profile[
1135                                         subport_id * APP_MAX_SCHED_PIPES +
1136                                         pipe_id];
1137
1138                                 if (profile_id == -1)
1139                                         continue;
1140
1141                                 status = rte_sched_pipe_config(sched,
1142                                         subport_id,
1143                                         pipe_id,
1144                                         profile_id);
1145                                 if (status)
1146                                         rte_panic("%s subport %" PRIu32
1147                                                 " pipe %" PRIu32
1148                                                 " (profile %" PRId32 ") "
1149                                                 "init error (% " PRId32 ")\n",
1150                                                 p_tm->name, subport_id, pipe_id,
1151                                                 profile_id, status);
1152                         }
1153                 }
1154         }
1155 }
1156
1157 #ifdef RTE_LIBRTE_KNI
1158 static int
1159 kni_config_network_interface(uint8_t port_id, uint8_t if_up) {
1160         int ret = 0;
1161
1162         if (port_id >= rte_eth_dev_count())
1163                 return -EINVAL;
1164
1165         ret = (if_up) ?
1166                 rte_eth_dev_set_link_up(port_id) :
1167                 rte_eth_dev_set_link_down(port_id);
1168
1169         return ret;
1170 }
1171
1172 static int
1173 kni_change_mtu(uint8_t port_id, unsigned new_mtu) {
1174         int ret;
1175
1176         if (port_id >= rte_eth_dev_count())
1177                 return -EINVAL;
1178
1179         if (new_mtu > ETHER_MAX_LEN)
1180                 return -EINVAL;
1181
1182         /* Set new MTU */
1183         ret = rte_eth_dev_set_mtu(port_id, new_mtu);
1184         if (ret < 0)
1185                 return ret;
1186
1187         return 0;
1188 }
1189 #endif /* RTE_LIBRTE_KNI */
1190
1191 #ifndef RTE_LIBRTE_KNI
1192 static void
1193 app_init_kni(struct app_params *app) {
1194         if (app->n_pktq_kni == 0)
1195                 return;
1196
1197         rte_panic("Can not init KNI without librte_kni support.\n");
1198 }
1199 #else
1200 static void
1201 app_init_kni(struct app_params *app) {
1202         uint32_t i;
1203
1204         if (app->n_pktq_kni == 0)
1205                 return;
1206
1207         rte_kni_init(app->n_pktq_kni);
1208
1209         for (i = 0; i < app->n_pktq_kni; i++) {
1210                 struct app_pktq_kni_params *p_kni = &app->kni_params[i];
1211                 struct app_link_params *p_link;
1212                 struct rte_eth_dev_info dev_info;
1213                 struct app_mempool_params *mempool_params;
1214                 struct rte_mempool *mempool;
1215                 struct rte_kni_conf conf;
1216                 struct rte_kni_ops ops;
1217
1218                 /* LINK */
1219                 p_link = app_get_link_for_kni(app, p_kni);
1220                 memset(&dev_info, 0, sizeof(dev_info));
1221                 rte_eth_dev_info_get(p_link->pmd_id, &dev_info);
1222
1223                 /* MEMPOOL */
1224                 mempool_params = &app->mempool_params[p_kni->mempool_id];
1225                 mempool = app->mempool[p_kni->mempool_id];
1226
1227                 /* KNI */
1228                 memset(&conf, 0, sizeof(conf));
1229                 snprintf(conf.name, RTE_KNI_NAMESIZE, "%s", p_kni->name);
1230                 conf.force_bind = p_kni->force_bind;
1231                 if (conf.force_bind) {
1232                         int lcore_id;
1233
1234                         lcore_id = cpu_core_map_get_lcore_id(app->core_map,
1235                                 p_kni->socket_id,
1236                                 p_kni->core_id,
1237                                 p_kni->hyper_th_id);
1238
1239                         if (lcore_id < 0)
1240                                 rte_panic("%s invalid CPU core\n", p_kni->name);
1241
1242                         conf.core_id = (uint32_t) lcore_id;
1243                 }
1244                 conf.group_id = p_link->pmd_id;
1245                 conf.mbuf_size = mempool_params->buffer_size;
1246                 conf.addr = dev_info.pci_dev->addr;
1247                 conf.id = dev_info.pci_dev->id;
1248
1249                 memset(&ops, 0, sizeof(ops));
1250                 ops.port_id = (uint8_t) p_link->pmd_id;
1251                 ops.change_mtu = kni_change_mtu;
1252                 ops.config_network_if = kni_config_network_interface;
1253
1254                 APP_LOG(app, HIGH, "Initializing %s ...", p_kni->name);
1255                 app->kni[i] = rte_kni_alloc(mempool, &conf, &ops);
1256                 if (!app->kni[i])
1257                         rte_panic("%s init error\n", p_kni->name);
1258         }
1259 }
1260 #endif /* RTE_LIBRTE_KNI */
1261
1262 static void
1263 app_init_msgq(struct app_params *app)
1264 {
1265         uint32_t i;
1266
1267         for (i = 0; i < app->n_msgq; i++) {
1268                 struct app_msgq_params *p = &app->msgq_params[i];
1269
1270                 APP_LOG(app, HIGH, "Initializing %s ...", p->name);
1271                 app->msgq[i] = rte_ring_create(
1272                                 p->name,
1273                                 p->size,
1274                                 p->cpu_socket_id,
1275                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
1276
1277                 if (app->msgq[i] == NULL)
1278                         rte_panic("%s init error\n", p->name);
1279         }
1280 }
1281
1282 void app_pipeline_params_get(struct app_params *app,
1283         struct app_pipeline_params *p_in,
1284         struct pipeline_params *p_out)
1285 {
1286         uint32_t i;
1287
1288         snprintf(p_out->name, PIPELINE_NAME_SIZE, "%s", p_in->name);
1289
1290         snprintf(p_out->type, PIPELINE_TYPE_SIZE, "%s", p_in->type);
1291
1292         p_out->socket_id = (int) p_in->socket_id;
1293
1294         p_out->log_level = app->log_level;
1295
1296         /* pktq_in */
1297         p_out->n_ports_in = p_in->n_pktq_in;
1298         for (i = 0; i < p_in->n_pktq_in; i++) {
1299                 struct app_pktq_in_params *in = &p_in->pktq_in[i];
1300                 struct pipeline_port_in_params *out = &p_out->port_in[i];
1301
1302                 switch (in->type) {
1303                 case APP_PKTQ_IN_HWQ:
1304                 {
1305                         struct app_pktq_hwq_in_params *p_hwq_in =
1306                                 &app->hwq_in_params[in->id];
1307                         struct app_link_params *p_link =
1308                                 app_get_link_for_rxq(app, p_hwq_in);
1309                         uint32_t rxq_link_id, rxq_queue_id;
1310
1311                         sscanf(p_hwq_in->name, "RXQ%" SCNu32 ".%" SCNu32,
1312                                 &rxq_link_id,
1313                                 &rxq_queue_id);
1314
1315                         out->type = PIPELINE_PORT_IN_ETHDEV_READER;
1316                         out->params.ethdev.port_id = p_link->pmd_id;
1317                         out->params.ethdev.queue_id = rxq_queue_id;
1318                         out->burst_size = p_hwq_in->burst;
1319                         break;
1320                 }
1321                 case APP_PKTQ_IN_SWQ:
1322                 {
1323                         struct app_pktq_swq_params *swq_params = &app->swq_params[in->id];
1324
1325                         if ((swq_params->ipv4_frag == 0) && (swq_params->ipv6_frag == 0)) {
1326                                 if (app_swq_get_readers(app, swq_params) == 1) {
1327                                         out->type = PIPELINE_PORT_IN_RING_READER;
1328                                         out->params.ring.ring = app->swq[in->id];
1329                                         out->burst_size = app->swq_params[in->id].burst_read;
1330                                 } else {
1331                                         out->type = PIPELINE_PORT_IN_RING_MULTI_READER;
1332                                         out->params.ring_multi.ring = app->swq[in->id];
1333                                         out->burst_size = swq_params->burst_read;
1334                                 }
1335                         } else {
1336                                 if (swq_params->ipv4_frag == 1) {
1337                                         struct rte_port_ring_reader_ipv4_frag_params *params =
1338                                                 &out->params.ring_ipv4_frag;
1339
1340                                         out->type = PIPELINE_PORT_IN_RING_READER_IPV4_FRAG;
1341                                         params->ring = app->swq[in->id];
1342                                         params->mtu = swq_params->mtu;
1343                                         params->metadata_size = swq_params->metadata_size;
1344                                         params->pool_direct =
1345                                                 app->mempool[swq_params->mempool_direct_id];
1346                                         params->pool_indirect =
1347                                                 app->mempool[swq_params->mempool_indirect_id];
1348                                         out->burst_size = swq_params->burst_read;
1349                                 } else {
1350                                         struct rte_port_ring_reader_ipv6_frag_params *params =
1351                                                 &out->params.ring_ipv6_frag;
1352
1353                                         out->type = PIPELINE_PORT_IN_RING_READER_IPV6_FRAG;
1354                                         params->ring = app->swq[in->id];
1355                                         params->mtu = swq_params->mtu;
1356                                         params->metadata_size = swq_params->metadata_size;
1357                                         params->pool_direct =
1358                                                 app->mempool[swq_params->mempool_direct_id];
1359                                         params->pool_indirect =
1360                                                 app->mempool[swq_params->mempool_indirect_id];
1361                                         out->burst_size = swq_params->burst_read;
1362                                 }
1363                         }
1364                         break;
1365                 }
1366                 case APP_PKTQ_IN_TM:
1367                 {
1368                         out->type = PIPELINE_PORT_IN_SCHED_READER;
1369                         out->params.sched.sched = app->tm[in->id];
1370                         out->burst_size = app->tm_params[in->id].burst_read;
1371                         break;
1372                 }
1373 #ifdef RTE_LIBRTE_KNI
1374                 case APP_PKTQ_IN_KNI:
1375                 {
1376                         out->type = PIPELINE_PORT_IN_KNI_READER;
1377                         out->params.kni.kni = app->kni[in->id];
1378                         out->burst_size = app->kni_params[in->id].burst_read;
1379                         break;
1380                 }
1381 #endif /* RTE_LIBRTE_KNI */
1382                 case APP_PKTQ_IN_SOURCE:
1383                 {
1384                         uint32_t mempool_id =
1385                                 app->source_params[in->id].mempool_id;
1386
1387                         out->type = PIPELINE_PORT_IN_SOURCE;
1388                         out->params.source.mempool = app->mempool[mempool_id];
1389                         out->burst_size = app->source_params[in->id].burst;
1390                         out->params.source.file_name =
1391                                 app->source_params[in->id].file_name;
1392                         out->params.source.n_bytes_per_pkt =
1393                                 app->source_params[in->id].n_bytes_per_pkt;
1394                         break;
1395                 }
1396                 default:
1397                         break;
1398                 }
1399         }
1400
1401         /* pktq_out */
1402         p_out->n_ports_out = p_in->n_pktq_out;
1403         for (i = 0; i < p_in->n_pktq_out; i++) {
1404                 struct app_pktq_out_params *in = &p_in->pktq_out[i];
1405                 struct pipeline_port_out_params *out = &p_out->port_out[i];
1406
1407                 switch (in->type) {
1408                 case APP_PKTQ_OUT_HWQ:
1409                 {
1410                         struct app_pktq_hwq_out_params *p_hwq_out =
1411                                 &app->hwq_out_params[in->id];
1412                         struct app_link_params *p_link =
1413                                 app_get_link_for_txq(app, p_hwq_out);
1414                         uint32_t txq_link_id, txq_queue_id;
1415
1416                         sscanf(p_hwq_out->name,
1417                                 "TXQ%" SCNu32 ".%" SCNu32,
1418                                 &txq_link_id,
1419                                 &txq_queue_id);
1420
1421                         if (p_hwq_out->dropless == 0) {
1422                                 struct rte_port_ethdev_writer_params *params =
1423                                         &out->params.ethdev;
1424
1425                                 out->type = PIPELINE_PORT_OUT_ETHDEV_WRITER;
1426                                 params->port_id = p_link->pmd_id;
1427                                 params->queue_id = txq_queue_id;
1428                                 params->tx_burst_sz =
1429                                         app->hwq_out_params[in->id].burst;
1430                         } else {
1431                                 struct rte_port_ethdev_writer_nodrop_params
1432                                         *params = &out->params.ethdev_nodrop;
1433
1434                                 out->type =
1435                                         PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP;
1436                                 params->port_id = p_link->pmd_id;
1437                                 params->queue_id = txq_queue_id;
1438                                 params->tx_burst_sz = p_hwq_out->burst;
1439                                 params->n_retries = p_hwq_out->n_retries;
1440                         }
1441                         break;
1442                 }
1443                 case APP_PKTQ_OUT_SWQ:
1444                 {
1445                         struct app_pktq_swq_params *swq_params = &app->swq_params[in->id];
1446
1447                         if ((swq_params->ipv4_ras == 0) && (swq_params->ipv6_ras == 0)) {
1448                                 if (app_swq_get_writers(app, swq_params) == 1) {
1449                                         if (app->swq_params[in->id].dropless == 0) {
1450                                                 struct rte_port_ring_writer_params *params =
1451                                                         &out->params.ring;
1452
1453                                                 out->type = PIPELINE_PORT_OUT_RING_WRITER;
1454                                                 params->ring = app->swq[in->id];
1455                                                 params->tx_burst_sz =
1456                                                         app->swq_params[in->id].burst_write;
1457                                         } else {
1458                                                 struct rte_port_ring_writer_nodrop_params
1459                                                         *params = &out->params.ring_nodrop;
1460
1461                                                 out->type =
1462                                                         PIPELINE_PORT_OUT_RING_WRITER_NODROP;
1463                                                 params->ring = app->swq[in->id];
1464                                                 params->tx_burst_sz =
1465                                                         app->swq_params[in->id].burst_write;
1466                                                 params->n_retries =
1467                                                         app->swq_params[in->id].n_retries;
1468                                         }
1469                                 } else {
1470                                         if (swq_params->dropless == 0) {
1471                                                 struct rte_port_ring_multi_writer_params *params =
1472                                                         &out->params.ring_multi;
1473
1474                                                 out->type = PIPELINE_PORT_OUT_RING_MULTI_WRITER;
1475                                                 params->ring = app->swq[in->id];
1476                                                 params->tx_burst_sz = swq_params->burst_write;
1477                                         } else {
1478                                                 struct rte_port_ring_multi_writer_nodrop_params
1479                                                         *params = &out->params.ring_multi_nodrop;
1480
1481                                                 out->type = PIPELINE_PORT_OUT_RING_MULTI_WRITER_NODROP;
1482                                                 params->ring = app->swq[in->id];
1483                                                 params->tx_burst_sz = swq_params->burst_write;
1484                                                 params->n_retries = swq_params->n_retries;
1485                                         }
1486                                 }
1487                         } else {
1488                                 if (swq_params->ipv4_ras == 1) {
1489                                         struct rte_port_ring_writer_ipv4_ras_params *params =
1490                                                 &out->params.ring_ipv4_ras;
1491
1492                                         out->type = PIPELINE_PORT_OUT_RING_WRITER_IPV4_RAS;
1493                                         params->ring = app->swq[in->id];
1494                                         params->tx_burst_sz = swq_params->burst_write;
1495                                 } else {
1496                                         struct rte_port_ring_writer_ipv6_ras_params *params =
1497                                                 &out->params.ring_ipv6_ras;
1498
1499                                         out->type = PIPELINE_PORT_OUT_RING_WRITER_IPV6_RAS;
1500                                         params->ring = app->swq[in->id];
1501                                         params->tx_burst_sz = swq_params->burst_write;
1502                                 }
1503                         }
1504                         break;
1505                 }
1506                 case APP_PKTQ_OUT_TM:
1507                 {
1508                         struct rte_port_sched_writer_params *params =
1509                                 &out->params.sched;
1510
1511                         out->type = PIPELINE_PORT_OUT_SCHED_WRITER;
1512                         params->sched = app->tm[in->id];
1513                         params->tx_burst_sz =
1514                                 app->tm_params[in->id].burst_write;
1515                         break;
1516                 }
1517 #ifdef RTE_LIBRTE_KNI
1518                 case APP_PKTQ_OUT_KNI:
1519                 {
1520                         struct app_pktq_kni_params *p_kni =
1521                                 &app->kni_params[in->id];
1522
1523                         if (p_kni->dropless == 0) {
1524                                 struct rte_port_kni_writer_params *params =
1525                                         &out->params.kni;
1526
1527                                 out->type = PIPELINE_PORT_OUT_KNI_WRITER;
1528                                 params->kni = app->kni[in->id];
1529                                 params->tx_burst_sz =
1530                                         app->kni_params[in->id].burst_write;
1531                         } else {
1532                                 struct rte_port_kni_writer_nodrop_params
1533                                         *params = &out->params.kni_nodrop;
1534
1535                                 out->type = PIPELINE_PORT_OUT_KNI_WRITER_NODROP;
1536                                 params->kni = app->kni[in->id];
1537                                 params->tx_burst_sz =
1538                                         app->kni_params[in->id].burst_write;
1539                                 params->n_retries =
1540                                         app->kni_params[in->id].n_retries;
1541                         }
1542                         break;
1543                 }
1544 #endif /* RTE_LIBRTE_KNI */
1545                 case APP_PKTQ_OUT_SINK:
1546                 {
1547                         out->type = PIPELINE_PORT_OUT_SINK;
1548                         out->params.sink.file_name =
1549                                 app->sink_params[in->id].file_name;
1550                         out->params.sink.max_n_pkts =
1551                                 app->sink_params[in->id].
1552                                 n_pkts_to_dump;
1553
1554                         break;
1555                 }
1556                 default:
1557                         break;
1558                 }
1559         }
1560
1561         /* msgq */
1562         p_out->n_msgq = p_in->n_msgq_in;
1563
1564         for (i = 0; i < p_in->n_msgq_in; i++)
1565                 p_out->msgq_in[i] = app->msgq[p_in->msgq_in[i]];
1566
1567         for (i = 0; i < p_in->n_msgq_out; i++)
1568                 p_out->msgq_out[i] = app->msgq[p_in->msgq_out[i]];
1569
1570         /* args */
1571         p_out->n_args = p_in->n_args;
1572         for (i = 0; i < p_in->n_args; i++) {
1573                 p_out->args_name[i] = p_in->args_name[i];
1574                 p_out->args_value[i] = p_in->args_value[i];
1575         }
1576 }
1577
1578 static void
1579 app_init_pipelines(struct app_params *app)
1580 {
1581         uint32_t p_id;
1582
1583         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1584                 struct app_pipeline_params *params =
1585                         &app->pipeline_params[p_id];
1586                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1587                 struct pipeline_type *ptype;
1588                 struct pipeline_params pp;
1589
1590                 APP_LOG(app, HIGH, "Initializing %s ...", params->name);
1591
1592                 ptype = app_pipeline_type_find(app, params->type);
1593                 if (ptype == NULL)
1594                         rte_panic("Init error: Unknown pipeline type \"%s\"\n",
1595                                 params->type);
1596
1597                 app_pipeline_params_get(app, params, &pp);
1598
1599                 /* Back-end */
1600                 data->be = NULL;
1601                 if (ptype->be_ops->f_init) {
1602                         data->be = ptype->be_ops->f_init(&pp, (void *) app);
1603
1604                         if (data->be == NULL)
1605                                 rte_panic("Pipeline instance \"%s\" back-end "
1606                                         "init error\n", params->name);
1607                 }
1608
1609                 /* Front-end */
1610                 data->fe = NULL;
1611                 if (ptype->fe_ops->f_init) {
1612                         data->fe = ptype->fe_ops->f_init(&pp, (void *) app);
1613
1614                         if (data->fe == NULL)
1615                                 rte_panic("Pipeline instance \"%s\" front-end "
1616                                 "init error\n", params->name);
1617                 }
1618
1619                 data->ptype = ptype;
1620
1621                 data->timer_period = (rte_get_tsc_hz() *
1622                         params->timer_period) / 100;
1623         }
1624 }
1625
1626 static void
1627 app_post_init_pipelines(struct app_params *app)
1628 {
1629         uint32_t p_id;
1630
1631         for (p_id = 0; p_id < app->n_pipelines; p_id++) {
1632                 struct app_pipeline_params *params =
1633                         &app->pipeline_params[p_id];
1634                 struct app_pipeline_data *data = &app->pipeline_data[p_id];
1635                 int status;
1636
1637                 if (data->ptype->fe_ops->f_post_init == NULL)
1638                         continue;
1639
1640                 status = data->ptype->fe_ops->f_post_init(data->fe);
1641                 if (status)
1642                         rte_panic("Pipeline instance \"%s\" front-end "
1643                                 "post-init error\n", params->name);
1644         }
1645 }
1646
1647 static void
1648 app_init_threads(struct app_params *app)
1649 {
1650         uint64_t time = rte_get_tsc_cycles();
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                 struct pipeline_type *ptype;
1658                 struct app_thread_data *t;
1659                 struct app_thread_pipeline_data *p;
1660                 int lcore_id;
1661
1662                 lcore_id = cpu_core_map_get_lcore_id(app->core_map,
1663                         params->socket_id,
1664                         params->core_id,
1665                         params->hyper_th_id);
1666
1667                 if (lcore_id < 0)
1668                         rte_panic("Invalid core s%" PRIu32 "c%" PRIu32 "%s\n",
1669                                 params->socket_id,
1670                                 params->core_id,
1671                                 (params->hyper_th_id) ? "h" : "");
1672
1673                 t = &app->thread_data[lcore_id];
1674
1675                 t->timer_period = (rte_get_tsc_hz() * APP_THREAD_TIMER_PERIOD) / 1000;
1676                 t->thread_req_deadline = time + t->timer_period;
1677
1678                 t->headroom_cycles = 0;
1679                 t->headroom_time = rte_get_tsc_cycles();
1680                 t->headroom_ratio = 0.0;
1681
1682                 t->msgq_in = app_thread_msgq_in_get(app,
1683                                 params->socket_id,
1684                                 params->core_id,
1685                                 params->hyper_th_id);
1686                 if (t->msgq_in == NULL)
1687                         rte_panic("Init error: Cannot find MSGQ_IN for thread %" PRId32,
1688                                 lcore_id);
1689
1690                 t->msgq_out = app_thread_msgq_out_get(app,
1691                                 params->socket_id,
1692                                 params->core_id,
1693                                 params->hyper_th_id);
1694                 if (t->msgq_out == NULL)
1695                         rte_panic("Init error: Cannot find MSGQ_OUT for thread %" PRId32,
1696                                 lcore_id);
1697
1698                 ptype = app_pipeline_type_find(app, params->type);
1699                 if (ptype == NULL)
1700                         rte_panic("Init error: Unknown pipeline "
1701                                 "type \"%s\"\n", params->type);
1702
1703                 p = (ptype->be_ops->f_run == NULL) ?
1704                         &t->regular[t->n_regular] :
1705                         &t->custom[t->n_custom];
1706
1707                 p->pipeline_id = p_id;
1708                 p->be = data->be;
1709                 p->f_run = ptype->be_ops->f_run;
1710                 p->f_timer = ptype->be_ops->f_timer;
1711                 p->timer_period = data->timer_period;
1712                 p->deadline = time + data->timer_period;
1713
1714                 data->enabled = 1;
1715
1716                 if (ptype->be_ops->f_run == NULL)
1717                         t->n_regular++;
1718                 else
1719                         t->n_custom++;
1720         }
1721 }
1722
1723 int app_init(struct app_params *app)
1724 {
1725         app_init_core_map(app);
1726         app_init_core_mask(app);
1727
1728         app_init_eal(app);
1729         app_init_mempool(app);
1730         app_init_link(app);
1731         app_init_swq(app);
1732         app_init_tm(app);
1733         app_init_kni(app);
1734         app_init_msgq(app);
1735
1736         app_pipeline_common_cmd_push(app);
1737         app_pipeline_thread_cmd_push(app);
1738         app_pipeline_type_register(app, &pipeline_master);
1739         app_pipeline_type_register(app, &pipeline_passthrough);
1740         app_pipeline_type_register(app, &pipeline_flow_classification);
1741         app_pipeline_type_register(app, &pipeline_flow_actions);
1742         app_pipeline_type_register(app, &pipeline_firewall);
1743         app_pipeline_type_register(app, &pipeline_routing);
1744
1745         app_init_pipelines(app);
1746         app_init_threads(app);
1747
1748         return 0;
1749 }
1750
1751 int app_post_init(struct app_params *app)
1752 {
1753         app_post_init_pipelines(app);
1754
1755         return 0;
1756 }
1757
1758 static int
1759 app_pipeline_type_cmd_push(struct app_params *app,
1760         struct pipeline_type *ptype)
1761 {
1762         cmdline_parse_ctx_t *cmds;
1763         uint32_t n_cmds, i;
1764
1765         /* Check input arguments */
1766         if ((app == NULL) ||
1767                 (ptype == NULL))
1768                 return -EINVAL;
1769
1770         n_cmds = pipeline_type_cmds_count(ptype);
1771         if (n_cmds == 0)
1772                 return 0;
1773
1774         cmds = ptype->fe_ops->cmds;
1775
1776         /* Check for available slots in the application commands array */
1777         if (n_cmds > APP_MAX_CMDS - app->n_cmds)
1778                 return -ENOMEM;
1779
1780         /* Push pipeline commands into the application */
1781         memcpy(&app->cmds[app->n_cmds],
1782                 cmds,
1783                 n_cmds * sizeof(cmdline_parse_ctx_t));
1784
1785         for (i = 0; i < n_cmds; i++)
1786                 app->cmds[app->n_cmds + i]->data = app;
1787
1788         app->n_cmds += n_cmds;
1789         app->cmds[app->n_cmds] = NULL;
1790
1791         return 0;
1792 }
1793
1794 int
1795 app_pipeline_type_register(struct app_params *app, struct pipeline_type *ptype)
1796 {
1797         uint32_t n_cmds, i;
1798
1799         /* Check input arguments */
1800         if ((app == NULL) ||
1801                 (ptype == NULL) ||
1802                 (ptype->name == NULL) ||
1803                 (strlen(ptype->name) == 0) ||
1804                 (ptype->be_ops->f_init == NULL) ||
1805                 (ptype->be_ops->f_timer == NULL))
1806                 return -EINVAL;
1807
1808         /* Check for duplicate entry */
1809         for (i = 0; i < app->n_pipeline_types; i++)
1810                 if (strcmp(app->pipeline_type[i].name, ptype->name) == 0)
1811                         return -EEXIST;
1812
1813         /* Check for resource availability */
1814         n_cmds = pipeline_type_cmds_count(ptype);
1815         if ((app->n_pipeline_types == APP_MAX_PIPELINE_TYPES) ||
1816                 (n_cmds > APP_MAX_CMDS - app->n_cmds))
1817                 return -ENOMEM;
1818
1819         /* Copy pipeline type */
1820         memcpy(&app->pipeline_type[app->n_pipeline_types++],
1821                 ptype,
1822                 sizeof(struct pipeline_type));
1823
1824         /* Copy CLI commands */
1825         if (n_cmds)
1826                 app_pipeline_type_cmd_push(app, ptype);
1827
1828         return 0;
1829 }
1830
1831 struct
1832 pipeline_type *app_pipeline_type_find(struct app_params *app, char *name)
1833 {
1834         uint32_t i;
1835
1836         for (i = 0; i < app->n_pipeline_types; i++)
1837                 if (strcmp(app->pipeline_type[i].name, name) == 0)
1838                         return &app->pipeline_type[i];
1839
1840         return NULL;
1841 }