examples/ip_pipeline: link routing output ports to devices
[dpdk.git] / examples / ip_pipeline / app.h
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 #ifndef __INCLUDE_APP_H__
35 #define __INCLUDE_APP_H__
36
37 #include <stdint.h>
38 #include <string.h>
39
40 #include <rte_common.h>
41 #include <rte_mempool.h>
42 #include <rte_ring.h>
43 #include <rte_sched.h>
44 #include <cmdline_parse.h>
45
46 #include <rte_ethdev.h>
47
48 #include "cpu_core_map.h"
49 #include "pipeline.h"
50
51 #define APP_PARAM_NAME_SIZE                      PIPELINE_NAME_SIZE
52 #define APP_LINK_PCI_BDF_SIZE                    16
53
54 #ifndef APP_LINK_MAX_HWQ_IN
55 #define APP_LINK_MAX_HWQ_IN                      128
56 #endif
57
58 #ifndef APP_LINK_MAX_HWQ_OUT
59 #define APP_LINK_MAX_HWQ_OUT                     128
60 #endif
61
62 struct app_mempool_params {
63         char *name;
64         uint32_t parsed;
65         uint32_t buffer_size;
66         uint32_t pool_size;
67         uint32_t cache_size;
68         uint32_t cpu_socket_id;
69 };
70
71 struct app_link_params {
72         char *name;
73         uint32_t parsed;
74         uint32_t pmd_id; /* Generated based on port mask */
75         uint32_t arp_q; /* 0 = Disabled (packets go to default queue 0) */
76         uint32_t tcp_syn_q; /* 0 = Disabled (pkts go to default queue) */
77         uint32_t ip_local_q; /* 0 = Disabled (pkts go to default queue 0) */
78         uint32_t tcp_local_q; /* 0 = Disabled (pkts go to default queue 0) */
79         uint32_t udp_local_q; /* 0 = Disabled (pkts go to default queue 0) */
80         uint32_t sctp_local_q; /* 0 = Disabled (pkts go to default queue 0) */
81         uint32_t rss_qs[APP_LINK_MAX_HWQ_IN];
82         uint32_t n_rss_qs;
83         uint64_t rss_proto_ipv4;
84         uint64_t rss_proto_ipv6;
85         uint64_t rss_proto_l2;
86         uint32_t promisc;
87         uint32_t state; /* DOWN = 0, UP = 1 */
88         uint32_t ip; /* 0 = Invalid */
89         uint32_t depth; /* Valid only when IP is valid */
90         uint64_t mac_addr; /* Read from HW */
91         char pci_bdf[APP_LINK_PCI_BDF_SIZE];
92
93         struct rte_eth_conf conf;
94 };
95
96 struct app_pktq_hwq_in_params {
97         char *name;
98         uint32_t parsed;
99         uint32_t mempool_id; /* Position in the app->mempool_params */
100         uint32_t size;
101         uint32_t burst;
102
103         struct rte_eth_rxconf conf;
104 };
105
106 struct app_pktq_hwq_out_params {
107         char *name;
108         uint32_t parsed;
109         uint32_t size;
110         uint32_t burst;
111         uint32_t dropless;
112         uint64_t n_retries;
113         struct rte_eth_txconf conf;
114 };
115
116 struct app_pktq_swq_params {
117         char *name;
118         uint32_t parsed;
119         uint32_t size;
120         uint32_t burst_read;
121         uint32_t burst_write;
122         uint32_t dropless;
123         uint64_t n_retries;
124         uint32_t cpu_socket_id;
125         uint32_t ipv4_frag;
126         uint32_t ipv6_frag;
127         uint32_t ipv4_ras;
128         uint32_t ipv6_ras;
129         uint32_t mtu;
130         uint32_t metadata_size;
131         uint32_t mempool_direct_id;
132         uint32_t mempool_indirect_id;
133 };
134
135 #ifndef APP_FILE_NAME_SIZE
136 #define APP_FILE_NAME_SIZE                       256
137 #endif
138
139 #ifndef APP_MAX_SCHED_SUBPORTS
140 #define APP_MAX_SCHED_SUBPORTS                   8
141 #endif
142
143 #ifndef APP_MAX_SCHED_PIPES
144 #define APP_MAX_SCHED_PIPES                      4096
145 #endif
146
147 struct app_pktq_tm_params {
148         char *name;
149         uint32_t parsed;
150         const char *file_name;
151         struct rte_sched_port_params sched_port_params;
152         struct rte_sched_subport_params
153                 sched_subport_params[APP_MAX_SCHED_SUBPORTS];
154         struct rte_sched_pipe_params
155                 sched_pipe_profiles[RTE_SCHED_PIPE_PROFILES_PER_PORT];
156         int sched_pipe_to_profile[APP_MAX_SCHED_SUBPORTS * APP_MAX_SCHED_PIPES];
157         uint32_t burst_read;
158         uint32_t burst_write;
159 };
160
161 struct app_pktq_source_params {
162         char *name;
163         uint32_t parsed;
164         uint32_t mempool_id; /* Position in the app->mempool_params array */
165         uint32_t burst;
166         char *file_name; /* Full path of PCAP file to be copied to mbufs */
167         uint32_t n_bytes_per_pkt;
168 };
169
170 struct app_pktq_sink_params {
171         char *name;
172         uint8_t parsed;
173         char *file_name; /* Full path of PCAP file to be copied to mbufs */
174         uint32_t n_pkts_to_dump;
175 };
176
177 struct app_msgq_params {
178         char *name;
179         uint32_t parsed;
180         uint32_t size;
181         uint32_t cpu_socket_id;
182 };
183
184 enum app_pktq_in_type {
185         APP_PKTQ_IN_HWQ,
186         APP_PKTQ_IN_SWQ,
187         APP_PKTQ_IN_TM,
188         APP_PKTQ_IN_SOURCE,
189 };
190
191 struct app_pktq_in_params {
192         enum app_pktq_in_type type;
193         uint32_t id; /* Position in the appropriate app array */
194 };
195
196 enum app_pktq_out_type {
197         APP_PKTQ_OUT_HWQ,
198         APP_PKTQ_OUT_SWQ,
199         APP_PKTQ_OUT_TM,
200         APP_PKTQ_OUT_SINK,
201 };
202
203 struct app_pktq_out_params {
204         enum app_pktq_out_type type;
205         uint32_t id; /* Position in the appropriate app array */
206 };
207
208 #define APP_PIPELINE_TYPE_SIZE                   PIPELINE_TYPE_SIZE
209
210 #define APP_MAX_PIPELINE_PKTQ_IN                 PIPELINE_MAX_PORT_IN
211 #define APP_MAX_PIPELINE_PKTQ_OUT                PIPELINE_MAX_PORT_OUT
212 #define APP_MAX_PIPELINE_MSGQ_IN                 PIPELINE_MAX_MSGQ_IN
213 #define APP_MAX_PIPELINE_MSGQ_OUT                PIPELINE_MAX_MSGQ_OUT
214
215 #define APP_MAX_PIPELINE_ARGS                    PIPELINE_MAX_ARGS
216
217 struct app_pipeline_params {
218         char *name;
219         uint8_t parsed;
220
221         char type[APP_PIPELINE_TYPE_SIZE];
222
223         uint32_t socket_id;
224         uint32_t core_id;
225         uint32_t hyper_th_id;
226
227         struct app_pktq_in_params pktq_in[APP_MAX_PIPELINE_PKTQ_IN];
228         struct app_pktq_out_params pktq_out[APP_MAX_PIPELINE_PKTQ_OUT];
229         uint32_t msgq_in[APP_MAX_PIPELINE_MSGQ_IN];
230         uint32_t msgq_out[APP_MAX_PIPELINE_MSGQ_OUT];
231
232         uint32_t n_pktq_in;
233         uint32_t n_pktq_out;
234         uint32_t n_msgq_in;
235         uint32_t n_msgq_out;
236
237         uint32_t timer_period;
238
239         char *args_name[APP_MAX_PIPELINE_ARGS];
240         char *args_value[APP_MAX_PIPELINE_ARGS];
241         uint32_t n_args;
242 };
243
244 struct app_pipeline_data {
245         void *be;
246         void *fe;
247         struct pipeline_type *ptype;
248         uint64_t timer_period;
249         uint32_t enabled;
250 };
251
252 struct app_thread_pipeline_data {
253         uint32_t pipeline_id;
254         void *be;
255         pipeline_be_op_run f_run;
256         pipeline_be_op_timer f_timer;
257         uint64_t timer_period;
258         uint64_t deadline;
259 };
260
261 #ifndef APP_MAX_THREAD_PIPELINES
262 #define APP_MAX_THREAD_PIPELINES                 64
263 #endif
264
265 #ifndef APP_THREAD_TIMER_PERIOD
266 #define APP_THREAD_TIMER_PERIOD                  1
267 #endif
268
269 struct app_thread_data {
270         struct app_thread_pipeline_data regular[APP_MAX_THREAD_PIPELINES];
271         struct app_thread_pipeline_data custom[APP_MAX_THREAD_PIPELINES];
272
273         uint32_t n_regular;
274         uint32_t n_custom;
275
276         uint64_t timer_period;
277         uint64_t thread_req_deadline;
278
279         uint64_t deadline;
280
281         struct rte_ring *msgq_in;
282         struct rte_ring *msgq_out;
283
284         uint64_t headroom_time;
285         uint64_t headroom_cycles;
286         double headroom_ratio;
287 };
288
289 #ifndef APP_MAX_LINKS
290 #define APP_MAX_LINKS                            16
291 #endif
292
293 struct app_eal_params {
294         /* Map lcore set to physical cpu set */
295         char *coremap;
296
297         /* Core ID that is used as master */
298         uint32_t master_lcore_present;
299         uint32_t master_lcore;
300
301         /* Number of memory channels */
302         uint32_t channels_present;
303         uint32_t channels;
304
305         /* Memory to allocate (see also --socket-mem) */
306         uint32_t memory_present;
307         uint32_t memory;
308
309         /* Force number of memory ranks (don't detect) */
310         uint32_t ranks_present;
311         uint32_t ranks;
312
313         /* Add a PCI device in black list. */
314         char *pci_blacklist[APP_MAX_LINKS];
315
316         /* Add a PCI device in white list. */
317         char *pci_whitelist[APP_MAX_LINKS];
318
319         /* Add a virtual device. */
320         char *vdev[APP_MAX_LINKS];
321
322          /* Use VMware TSC map instead of native RDTSC */
323         uint32_t vmware_tsc_map_present;
324         int vmware_tsc_map;
325
326          /* Type of this process (primary|secondary|auto) */
327         char *proc_type;
328
329          /* Set syslog facility */
330         char *syslog;
331
332         /* Set default log level */
333         uint32_t log_level_present;
334         uint32_t log_level;
335
336         /* Display version information on startup */
337         uint32_t version_present;
338         int version;
339
340         /* This help */
341         uint32_t help_present;
342         int help;
343
344          /* Use malloc instead of hugetlbfs */
345         uint32_t no_huge_present;
346         int no_huge;
347
348         /* Disable PCI */
349         uint32_t no_pci_present;
350         int no_pci;
351
352         /* Disable HPET */
353         uint32_t no_hpet_present;
354         int no_hpet;
355
356         /* No shared config (mmap'd files) */
357         uint32_t no_shconf_present;
358         int no_shconf;
359
360         /* Add driver */
361         char *add_driver;
362
363         /*  Memory to allocate on sockets (comma separated values)*/
364         char *socket_mem;
365
366         /* Directory where hugetlbfs is mounted */
367         char *huge_dir;
368
369         /* Prefix for hugepage filenames */
370         char *file_prefix;
371
372         /* Base virtual address */
373         char *base_virtaddr;
374
375         /* Create /dev/uioX (usually done by hotplug) */
376         uint32_t create_uio_dev_present;
377         int create_uio_dev;
378
379         /* Interrupt mode for VFIO (legacy|msi|msix) */
380         char *vfio_intr;
381
382         /* Support running on Xen dom0 without hugetlbfs */
383         uint32_t xen_dom0_present;
384         int xen_dom0;
385
386         uint32_t parsed;
387 };
388
389 #ifndef APP_APPNAME_SIZE
390 #define APP_APPNAME_SIZE                         256
391 #endif
392
393 #ifndef APP_MAX_MEMPOOLS
394 #define APP_MAX_MEMPOOLS                         8
395 #endif
396
397 #define APP_MAX_HWQ_IN                  (APP_MAX_LINKS * APP_LINK_MAX_HWQ_IN)
398
399 #define APP_MAX_HWQ_OUT                 (APP_MAX_LINKS * APP_LINK_MAX_HWQ_OUT)
400
401 #ifndef APP_MAX_PKTQ_SWQ
402 #define APP_MAX_PKTQ_SWQ                         256
403 #endif
404
405 #define APP_MAX_PKTQ_TM                          APP_MAX_LINKS
406
407 #ifndef APP_MAX_PKTQ_SOURCE
408 #define APP_MAX_PKTQ_SOURCE                      64
409 #endif
410
411 #ifndef APP_MAX_PKTQ_SINK
412 #define APP_MAX_PKTQ_SINK                        64
413 #endif
414
415 #ifndef APP_MAX_MSGQ
416 #define APP_MAX_MSGQ                             256
417 #endif
418
419 #ifndef APP_MAX_PIPELINES
420 #define APP_MAX_PIPELINES                        64
421 #endif
422
423 #ifndef APP_EAL_ARGC
424 #define APP_EAL_ARGC                             64
425 #endif
426
427 #ifndef APP_MAX_PIPELINE_TYPES
428 #define APP_MAX_PIPELINE_TYPES                   64
429 #endif
430
431 #ifndef APP_MAX_THREADS
432 #define APP_MAX_THREADS                          RTE_MAX_LCORE
433 #endif
434
435 #ifndef APP_MAX_CMDS
436 #define APP_MAX_CMDS                             64
437 #endif
438
439 #ifndef APP_THREAD_HEADROOM_STATS_COLLECT
440 #define APP_THREAD_HEADROOM_STATS_COLLECT        1
441 #endif
442
443 struct app_params {
444         /* Config */
445         char app_name[APP_APPNAME_SIZE];
446         const char *config_file;
447         const char *script_file;
448         const char *parser_file;
449         const char *output_file;
450         const char *preproc;
451         const char *preproc_args;
452         uint64_t port_mask;
453         uint32_t log_level;
454
455         struct app_eal_params eal_params;
456         struct app_mempool_params mempool_params[APP_MAX_MEMPOOLS];
457         struct app_link_params link_params[APP_MAX_LINKS];
458         struct app_pktq_hwq_in_params hwq_in_params[APP_MAX_HWQ_IN];
459         struct app_pktq_hwq_out_params hwq_out_params[APP_MAX_HWQ_OUT];
460         struct app_pktq_swq_params swq_params[APP_MAX_PKTQ_SWQ];
461         struct app_pktq_tm_params tm_params[APP_MAX_PKTQ_TM];
462         struct app_pktq_source_params source_params[APP_MAX_PKTQ_SOURCE];
463         struct app_pktq_sink_params sink_params[APP_MAX_PKTQ_SINK];
464         struct app_msgq_params msgq_params[APP_MAX_MSGQ];
465         struct app_pipeline_params pipeline_params[APP_MAX_PIPELINES];
466
467         uint32_t n_mempools;
468         uint32_t n_links;
469         uint32_t n_pktq_hwq_in;
470         uint32_t n_pktq_hwq_out;
471         uint32_t n_pktq_swq;
472         uint32_t n_pktq_tm;
473         uint32_t n_pktq_source;
474         uint32_t n_pktq_sink;
475         uint32_t n_msgq;
476         uint32_t n_pipelines;
477
478         /* Init */
479         char *eal_argv[1 + APP_EAL_ARGC];
480         struct cpu_core_map *core_map;
481         uint64_t core_mask;
482         struct rte_mempool *mempool[APP_MAX_MEMPOOLS];
483         struct rte_ring *swq[APP_MAX_PKTQ_SWQ];
484         struct rte_sched_port *tm[APP_MAX_PKTQ_TM];
485         struct rte_ring *msgq[APP_MAX_MSGQ];
486         struct pipeline_type pipeline_type[APP_MAX_PIPELINE_TYPES];
487         struct app_pipeline_data pipeline_data[APP_MAX_PIPELINES];
488         struct app_thread_data thread_data[APP_MAX_THREADS];
489         cmdline_parse_ctx_t cmds[APP_MAX_CMDS + 1];
490
491         int eal_argc;
492         uint32_t n_pipeline_types;
493         uint32_t n_cmds;
494 };
495
496 #define APP_PARAM_VALID(obj) ((obj)->name != NULL)
497
498 #define APP_PARAM_COUNT(obj_array, n_objs)                              \
499 {                                                                       \
500         size_t i;                                                       \
501                                                                         \
502         n_objs = 0;                                                     \
503         for (i = 0; i < RTE_DIM(obj_array); i++)                        \
504                 if (APP_PARAM_VALID(&((obj_array)[i])))                 \
505                         n_objs++;                                       \
506 }
507
508 #define APP_PARAM_FIND(obj_array, key)                                  \
509 ({                                                                      \
510         ssize_t obj_idx;                                                \
511         const ssize_t obj_count = RTE_DIM(obj_array);                   \
512                                                                         \
513         for (obj_idx = 0; obj_idx < obj_count; obj_idx++) {             \
514                 if (!APP_PARAM_VALID(&((obj_array)[obj_idx])))          \
515                         continue;                                       \
516                                                                         \
517                 if (strcmp(key, (obj_array)[obj_idx].name) == 0)        \
518                         break;                                          \
519         }                                                               \
520         obj_idx < obj_count ? obj_idx : -ENOENT;                        \
521 })
522
523 #define APP_PARAM_FIND_BY_ID(obj_array, prefix, id, obj)                \
524 do {                                                                    \
525         char name[APP_PARAM_NAME_SIZE];                                 \
526         ssize_t pos;                                                    \
527                                                                         \
528         sprintf(name, prefix "%" PRIu32, id);                           \
529         pos = APP_PARAM_FIND(obj_array, name);                          \
530         obj = (pos < 0) ? NULL : &((obj_array)[pos]);                   \
531 } while (0)
532
533 #define APP_PARAM_GET_ID(obj, prefix, id)                               \
534 do                                                                      \
535         sscanf(obj->name, prefix "%" SCNu32, &id);                              \
536 while (0)                                                               \
537
538 #define APP_CHECK(exp, fmt, ...)                                        \
539 do {                                                                    \
540         if (!(exp)) {                                                   \
541                 fprintf(stderr, fmt "\n", ## __VA_ARGS__);              \
542                 abort();                                                \
543         }                                                               \
544 } while (0)
545
546 enum app_log_level {
547         APP_LOG_LEVEL_HIGH = 1,
548         APP_LOG_LEVEL_LOW,
549         APP_LOG_LEVELS
550 };
551
552 #define APP_LOG(app, level, fmt, ...)                                   \
553 do {                                                                    \
554         if (app->log_level >= APP_LOG_LEVEL_ ## level)                  \
555                 fprintf(stdout, "[APP] " fmt "\n", ## __VA_ARGS__);     \
556 } while (0)
557
558 static inline uint32_t
559 app_link_get_n_rxq(struct app_params *app, struct app_link_params *link)
560 {
561         uint32_t n_rxq = 0, link_id, i;
562         uint32_t n_pktq_hwq_in = RTE_MIN(app->n_pktq_hwq_in,
563                 RTE_DIM(app->hwq_in_params));
564
565         APP_PARAM_GET_ID(link, "LINK", link_id);
566
567         for (i = 0; i < n_pktq_hwq_in; i++) {
568                 struct app_pktq_hwq_in_params *p = &app->hwq_in_params[i];
569                 uint32_t rxq_link_id, rxq_queue_id;
570
571                 sscanf(p->name, "RXQ%" SCNu32 ".%" SCNu32,
572                         &rxq_link_id, &rxq_queue_id);
573                 if (rxq_link_id == link_id)
574                         n_rxq++;
575         }
576
577         return n_rxq;
578 }
579
580 static inline uint32_t
581 app_link_get_n_txq(struct app_params *app, struct app_link_params *link)
582 {
583         uint32_t n_txq = 0, link_id, i;
584         uint32_t n_pktq_hwq_out = RTE_MIN(app->n_pktq_hwq_out,
585                 RTE_DIM(app->hwq_out_params));
586
587         APP_PARAM_GET_ID(link, "LINK", link_id);
588
589         for (i = 0; i < n_pktq_hwq_out; i++) {
590                 struct app_pktq_hwq_out_params *p = &app->hwq_out_params[i];
591                 uint32_t txq_link_id, txq_queue_id;
592
593                 sscanf(p->name, "TXQ%" SCNu32 ".%" SCNu32,
594                         &txq_link_id, &txq_queue_id);
595                 if (txq_link_id == link_id)
596                         n_txq++;
597         }
598
599         return n_txq;
600 }
601
602 static inline uint32_t
603 app_rxq_get_readers(struct app_params *app, struct app_pktq_hwq_in_params *rxq)
604 {
605         uint32_t pos = rxq - app->hwq_in_params;
606         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
607                 RTE_DIM(app->pipeline_params));
608         uint32_t n_readers = 0, i;
609
610         for (i = 0; i < n_pipelines; i++) {
611                 struct app_pipeline_params *p = &app->pipeline_params[i];
612                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
613                 uint32_t j;
614
615                 for (j = 0; j < n_pktq_in; j++) {
616                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
617
618                         if ((pktq->type == APP_PKTQ_IN_HWQ) &&
619                                 (pktq->id == pos))
620                                 n_readers++;
621                 }
622         }
623
624         return n_readers;
625 }
626
627 static inline uint32_t
628 app_swq_get_readers(struct app_params *app, struct app_pktq_swq_params *swq)
629 {
630         uint32_t pos = swq - app->swq_params;
631         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
632                 RTE_DIM(app->pipeline_params));
633         uint32_t n_readers = 0, i;
634
635         for (i = 0; i < n_pipelines; i++) {
636                 struct app_pipeline_params *p = &app->pipeline_params[i];
637                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
638                 uint32_t j;
639
640                 for (j = 0; j < n_pktq_in; j++) {
641                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
642
643                         if ((pktq->type == APP_PKTQ_IN_SWQ) &&
644                                 (pktq->id == pos))
645                                 n_readers++;
646                 }
647         }
648
649         return n_readers;
650 }
651
652 static inline struct app_pipeline_params *
653 app_swq_get_reader(struct app_params *app,
654         struct app_pktq_swq_params *swq,
655         uint32_t *pktq_in_id)
656 {
657         struct app_pipeline_params *reader;
658         uint32_t pos = swq - app->swq_params;
659         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
660                 RTE_DIM(app->pipeline_params));
661         uint32_t n_readers = 0, id, i;
662
663         for (i = 0; i < n_pipelines; i++) {
664                 struct app_pipeline_params *p = &app->pipeline_params[i];
665                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
666                 uint32_t j;
667
668                 for (j = 0; j < n_pktq_in; j++) {
669                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
670
671                         if ((pktq->type == APP_PKTQ_IN_SWQ) &&
672                                 (pktq->id == pos)) {
673                                 n_readers++;
674                                 reader = p;
675                                 id = j;
676                         }
677                 }
678         }
679
680         if (n_readers != 1)
681                 return NULL;
682
683         *pktq_in_id = id;
684         return reader;
685 }
686
687 static inline uint32_t
688 app_tm_get_readers(struct app_params *app, struct app_pktq_tm_params *tm)
689 {
690         uint32_t pos = tm - app->tm_params;
691         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
692                 RTE_DIM(app->pipeline_params));
693         uint32_t n_readers = 0, i;
694
695         for (i = 0; i < n_pipelines; i++) {
696                 struct app_pipeline_params *p = &app->pipeline_params[i];
697                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
698                 uint32_t j;
699
700                 for (j = 0; j < n_pktq_in; j++) {
701                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
702
703                         if ((pktq->type == APP_PKTQ_IN_TM) &&
704                                 (pktq->id == pos))
705                                 n_readers++;
706                 }
707         }
708
709         return n_readers;
710 }
711
712 static inline struct app_pipeline_params *
713 app_tm_get_reader(struct app_params *app,
714         struct app_pktq_tm_params *tm,
715         uint32_t *pktq_in_id)
716 {
717         struct app_pipeline_params *reader;
718         uint32_t pos = tm - app->tm_params;
719         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
720                 RTE_DIM(app->pipeline_params));
721         uint32_t n_readers = 0, id, i;
722
723         for (i = 0; i < n_pipelines; i++) {
724                 struct app_pipeline_params *p = &app->pipeline_params[i];
725                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
726                 uint32_t j;
727
728                 for (j = 0; j < n_pktq_in; j++) {
729                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
730
731                         if ((pktq->type == APP_PKTQ_IN_TM) &&
732                                 (pktq->id == pos)) {
733                                 n_readers++;
734                                 reader = p;
735                                 id = j;
736                         }
737                 }
738         }
739
740         if (n_readers != 1)
741                 return NULL;
742
743         *pktq_in_id = id;
744         return reader;
745 }
746
747 static inline uint32_t
748 app_source_get_readers(struct app_params *app,
749 struct app_pktq_source_params *source)
750 {
751         uint32_t pos = source - app->source_params;
752         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
753                 RTE_DIM(app->pipeline_params));
754         uint32_t n_readers = 0, i;
755
756         for (i = 0; i < n_pipelines; i++) {
757                 struct app_pipeline_params *p = &app->pipeline_params[i];
758                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
759                 uint32_t j;
760
761                 for (j = 0; j < n_pktq_in; j++) {
762                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
763
764                         if ((pktq->type == APP_PKTQ_IN_SOURCE) &&
765                                 (pktq->id == pos))
766                                 n_readers++;
767                 }
768         }
769
770         return n_readers;
771 }
772
773 static inline uint32_t
774 app_msgq_get_readers(struct app_params *app, struct app_msgq_params *msgq)
775 {
776         uint32_t pos = msgq - app->msgq_params;
777         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
778                 RTE_DIM(app->pipeline_params));
779         uint32_t n_readers = 0, i;
780
781         for (i = 0; i < n_pipelines; i++) {
782                 struct app_pipeline_params *p = &app->pipeline_params[i];
783                 uint32_t n_msgq_in = RTE_MIN(p->n_msgq_in, RTE_DIM(p->msgq_in));
784                 uint32_t j;
785
786                 for (j = 0; j < n_msgq_in; j++)
787                         if (p->msgq_in[j] == pos)
788                                 n_readers++;
789         }
790
791         return n_readers;
792 }
793
794 static inline uint32_t
795 app_txq_get_writers(struct app_params *app, struct app_pktq_hwq_out_params *txq)
796 {
797         uint32_t pos = txq - app->hwq_out_params;
798         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
799                 RTE_DIM(app->pipeline_params));
800         uint32_t n_writers = 0, i;
801
802         for (i = 0; i < n_pipelines; i++) {
803                 struct app_pipeline_params *p = &app->pipeline_params[i];
804                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
805                         RTE_DIM(p->pktq_out));
806                 uint32_t j;
807
808                 for (j = 0; j < n_pktq_out; j++) {
809                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
810
811                         if ((pktq->type == APP_PKTQ_OUT_HWQ) &&
812                                 (pktq->id == pos))
813                                 n_writers++;
814                 }
815         }
816
817         return n_writers;
818 }
819
820 static inline uint32_t
821 app_swq_get_writers(struct app_params *app, struct app_pktq_swq_params *swq)
822 {
823         uint32_t pos = swq - app->swq_params;
824         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
825                 RTE_DIM(app->pipeline_params));
826         uint32_t n_writers = 0, i;
827
828         for (i = 0; i < n_pipelines; i++) {
829                 struct app_pipeline_params *p = &app->pipeline_params[i];
830                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
831                         RTE_DIM(p->pktq_out));
832                 uint32_t j;
833
834                 for (j = 0; j < n_pktq_out; j++) {
835                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
836
837                         if ((pktq->type == APP_PKTQ_OUT_SWQ) &&
838                                 (pktq->id == pos))
839                                 n_writers++;
840                 }
841         }
842
843         return n_writers;
844 }
845
846 static inline struct app_pipeline_params *
847 app_swq_get_writer(struct app_params *app,
848         struct app_pktq_swq_params *swq,
849         uint32_t *pktq_out_id)
850 {
851         struct app_pipeline_params *writer;
852         uint32_t pos = swq - app->swq_params;
853         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
854                 RTE_DIM(app->pipeline_params));
855         uint32_t n_writers = 0, id, i;
856
857         for (i = 0; i < n_pipelines; i++) {
858                 struct app_pipeline_params *p = &app->pipeline_params[i];
859                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
860                         RTE_DIM(p->pktq_out));
861                 uint32_t j;
862
863                 for (j = 0; j < n_pktq_out; j++) {
864                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
865
866                         if ((pktq->type == APP_PKTQ_OUT_SWQ) &&
867                                 (pktq->id == pos)) {
868                                 n_writers++;
869                                 writer = p;
870                                 id = j;
871                         }
872                 }
873         }
874
875         if (n_writers != 1)
876                 return NULL;
877
878         *pktq_out_id = id;
879         return writer;
880 }
881
882 static inline uint32_t
883 app_tm_get_writers(struct app_params *app, struct app_pktq_tm_params *tm)
884 {
885         uint32_t pos = tm - app->tm_params;
886         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
887                 RTE_DIM(app->pipeline_params));
888         uint32_t n_writers = 0, i;
889
890         for (i = 0; i < n_pipelines; i++) {
891                 struct app_pipeline_params *p = &app->pipeline_params[i];
892                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
893                         RTE_DIM(p->pktq_out));
894                 uint32_t j;
895
896                 for (j = 0; j < n_pktq_out; j++) {
897                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
898
899                         if ((pktq->type == APP_PKTQ_OUT_TM) &&
900                                 (pktq->id == pos))
901                                 n_writers++;
902                 }
903         }
904
905         return n_writers;
906 }
907
908 static inline struct app_pipeline_params *
909 app_tm_get_writer(struct app_params *app,
910         struct app_pktq_tm_params *tm,
911         uint32_t *pktq_out_id)
912 {
913         struct app_pipeline_params *writer;
914         uint32_t pos = tm - app->tm_params;
915         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
916                 RTE_DIM(app->pipeline_params));
917         uint32_t n_writers = 0, id, i;
918
919         for (i = 0; i < n_pipelines; i++) {
920                 struct app_pipeline_params *p = &app->pipeline_params[i];
921                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
922                         RTE_DIM(p->pktq_out));
923                 uint32_t j;
924
925                 for (j = 0; j < n_pktq_out; j++) {
926                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
927
928                         if ((pktq->type == APP_PKTQ_OUT_TM) &&
929                                 (pktq->id == pos))
930                                 n_writers++;
931                                 writer = p;
932                                 id = j;
933                 }
934         }
935
936         if (n_writers != 1)
937                 return NULL;
938
939         *pktq_out_id = id;
940         return writer;
941 }
942
943 static inline uint32_t
944 app_sink_get_writers(struct app_params *app, struct app_pktq_sink_params *sink)
945 {
946         uint32_t pos = sink - app->sink_params;
947         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
948                 RTE_DIM(app->pipeline_params));
949         uint32_t n_writers = 0, i;
950
951         for (i = 0; i < n_pipelines; i++) {
952                 struct app_pipeline_params *p = &app->pipeline_params[i];
953                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
954                         RTE_DIM(p->pktq_out));
955                 uint32_t j;
956
957                 for (j = 0; j < n_pktq_out; j++) {
958                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
959
960                         if ((pktq->type == APP_PKTQ_OUT_SINK) &&
961                                 (pktq->id == pos))
962                                 n_writers++;
963                 }
964         }
965
966         return n_writers;
967 }
968
969 static inline uint32_t
970 app_msgq_get_writers(struct app_params *app, struct app_msgq_params *msgq)
971 {
972         uint32_t pos = msgq - app->msgq_params;
973         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
974                 RTE_DIM(app->pipeline_params));
975         uint32_t n_writers = 0, i;
976
977         for (i = 0; i < n_pipelines; i++) {
978                 struct app_pipeline_params *p = &app->pipeline_params[i];
979                 uint32_t n_msgq_out = RTE_MIN(p->n_msgq_out,
980                         RTE_DIM(p->msgq_out));
981                 uint32_t j;
982
983                 for (j = 0; j < n_msgq_out; j++)
984                         if (p->msgq_out[j] == pos)
985                                 n_writers++;
986         }
987
988         return n_writers;
989 }
990
991 static inline struct app_link_params *
992 app_get_link_for_rxq(struct app_params *app, struct app_pktq_hwq_in_params *p)
993 {
994         char link_name[APP_PARAM_NAME_SIZE];
995         ssize_t link_param_idx;
996         uint32_t rxq_link_id, rxq_queue_id;
997
998         sscanf(p->name, "RXQ%" SCNu32 ".%" SCNu32,
999                 &rxq_link_id, &rxq_queue_id);
1000         sprintf(link_name, "LINK%" PRIu32, rxq_link_id);
1001         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
1002         APP_CHECK((link_param_idx >= 0),
1003                 "Cannot find %s for %s", link_name, p->name);
1004
1005         return &app->link_params[link_param_idx];
1006 }
1007
1008 static inline struct app_link_params *
1009 app_get_link_for_txq(struct app_params *app, struct app_pktq_hwq_out_params *p)
1010 {
1011         char link_name[APP_PARAM_NAME_SIZE];
1012         ssize_t link_param_idx;
1013         uint32_t txq_link_id, txq_queue_id;
1014
1015         sscanf(p->name, "TXQ%" SCNu32 ".%" SCNu32,
1016                 &txq_link_id, &txq_queue_id);
1017         sprintf(link_name, "LINK%" PRIu32, txq_link_id);
1018         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
1019         APP_CHECK((link_param_idx >= 0),
1020                 "Cannot find %s for %s", link_name, p->name);
1021
1022         return &app->link_params[link_param_idx];
1023 }
1024
1025 static inline struct app_link_params *
1026 app_get_link_for_tm(struct app_params *app, struct app_pktq_tm_params *p_tm)
1027 {
1028         char link_name[APP_PARAM_NAME_SIZE];
1029         uint32_t link_id;
1030         ssize_t link_param_idx;
1031
1032         sscanf(p_tm->name, "TM%" PRIu32, &link_id);
1033         sprintf(link_name, "LINK%" PRIu32, link_id);
1034         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
1035         APP_CHECK((link_param_idx >= 0),
1036                 "Cannot find %s for %s", link_name, p_tm->name);
1037
1038         return &app->link_params[link_param_idx];
1039 }
1040
1041 void app_pipeline_params_get(struct app_params *app,
1042         struct app_pipeline_params *p_in,
1043         struct pipeline_params *p_out);
1044
1045 int app_config_init(struct app_params *app);
1046
1047 int app_config_args(struct app_params *app,
1048         int argc, char **argv);
1049
1050 int app_config_preproc(struct app_params *app);
1051
1052 int app_config_parse(struct app_params *app,
1053         const char *file_name);
1054
1055 int app_config_parse_tm(struct app_params *app);
1056
1057 void app_config_save(struct app_params *app,
1058         const char *file_name);
1059
1060 int app_config_check(struct app_params *app);
1061
1062 int app_init(struct app_params *app);
1063
1064 int app_post_init(struct app_params *app);
1065
1066 int app_thread(void *arg);
1067
1068 int app_pipeline_type_register(struct app_params *app,
1069         struct pipeline_type *ptype);
1070
1071 struct pipeline_type *app_pipeline_type_find(struct app_params *app,
1072         char *name);
1073
1074 void app_link_up_internal(struct app_params *app,
1075         struct app_link_params *cp);
1076
1077 void app_link_down_internal(struct app_params *app,
1078         struct app_link_params *cp);
1079
1080 #endif