examples/ip_pipeline: fix false cacheline sharing among threads
[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_params;
245
246 typedef void (*app_link_op)(struct app_params *app,
247         uint32_t link_id,
248         uint32_t up,
249         void *arg);
250
251 #ifndef APP_MAX_PIPELINES
252 #define APP_MAX_PIPELINES                        64
253 #endif
254
255 struct app_link_data {
256         app_link_op f_link[APP_MAX_PIPELINES];
257         void *arg[APP_MAX_PIPELINES];
258 };
259
260 struct app_pipeline_data {
261         void *be;
262         void *fe;
263         struct pipeline_type *ptype;
264         uint64_t timer_period;
265         uint32_t enabled;
266 };
267
268 struct app_thread_pipeline_data {
269         uint32_t pipeline_id;
270         void *be;
271         pipeline_be_op_run f_run;
272         pipeline_be_op_timer f_timer;
273         uint64_t timer_period;
274         uint64_t deadline;
275 };
276
277 #ifndef APP_MAX_THREAD_PIPELINES
278 #define APP_MAX_THREAD_PIPELINES                 64
279 #endif
280
281 #ifndef APP_THREAD_TIMER_PERIOD
282 #define APP_THREAD_TIMER_PERIOD                  1
283 #endif
284
285 struct app_thread_data {
286         struct app_thread_pipeline_data regular[APP_MAX_THREAD_PIPELINES];
287         struct app_thread_pipeline_data custom[APP_MAX_THREAD_PIPELINES];
288
289         uint32_t n_regular;
290         uint32_t n_custom;
291
292         uint64_t timer_period;
293         uint64_t thread_req_deadline;
294
295         uint64_t deadline;
296
297         struct rte_ring *msgq_in;
298         struct rte_ring *msgq_out;
299
300         uint64_t headroom_time;
301         uint64_t headroom_cycles;
302         double headroom_ratio;
303 } __rte_cache_aligned;
304
305 #ifndef APP_MAX_LINKS
306 #define APP_MAX_LINKS                            16
307 #endif
308
309 struct app_eal_params {
310         /* Map lcore set to physical cpu set */
311         char *coremap;
312
313         /* Core ID that is used as master */
314         uint32_t master_lcore_present;
315         uint32_t master_lcore;
316
317         /* Number of memory channels */
318         uint32_t channels_present;
319         uint32_t channels;
320
321         /* Memory to allocate (see also --socket-mem) */
322         uint32_t memory_present;
323         uint32_t memory;
324
325         /* Force number of memory ranks (don't detect) */
326         uint32_t ranks_present;
327         uint32_t ranks;
328
329         /* Add a PCI device in black list. */
330         char *pci_blacklist[APP_MAX_LINKS];
331
332         /* Add a PCI device in white list. */
333         char *pci_whitelist[APP_MAX_LINKS];
334
335         /* Add a virtual device. */
336         char *vdev[APP_MAX_LINKS];
337
338          /* Use VMware TSC map instead of native RDTSC */
339         uint32_t vmware_tsc_map_present;
340         int vmware_tsc_map;
341
342          /* Type of this process (primary|secondary|auto) */
343         char *proc_type;
344
345          /* Set syslog facility */
346         char *syslog;
347
348         /* Set default log level */
349         uint32_t log_level_present;
350         uint32_t log_level;
351
352         /* Display version information on startup */
353         uint32_t version_present;
354         int version;
355
356         /* This help */
357         uint32_t help_present;
358         int help;
359
360          /* Use malloc instead of hugetlbfs */
361         uint32_t no_huge_present;
362         int no_huge;
363
364         /* Disable PCI */
365         uint32_t no_pci_present;
366         int no_pci;
367
368         /* Disable HPET */
369         uint32_t no_hpet_present;
370         int no_hpet;
371
372         /* No shared config (mmap'd files) */
373         uint32_t no_shconf_present;
374         int no_shconf;
375
376         /* Add driver */
377         char *add_driver;
378
379         /*  Memory to allocate on sockets (comma separated values)*/
380         char *socket_mem;
381
382         /* Directory where hugetlbfs is mounted */
383         char *huge_dir;
384
385         /* Prefix for hugepage filenames */
386         char *file_prefix;
387
388         /* Base virtual address */
389         char *base_virtaddr;
390
391         /* Create /dev/uioX (usually done by hotplug) */
392         uint32_t create_uio_dev_present;
393         int create_uio_dev;
394
395         /* Interrupt mode for VFIO (legacy|msi|msix) */
396         char *vfio_intr;
397
398         /* Support running on Xen dom0 without hugetlbfs */
399         uint32_t xen_dom0_present;
400         int xen_dom0;
401
402         uint32_t parsed;
403 };
404
405 #ifndef APP_APPNAME_SIZE
406 #define APP_APPNAME_SIZE                         256
407 #endif
408
409 #ifndef APP_MAX_MEMPOOLS
410 #define APP_MAX_MEMPOOLS                         8
411 #endif
412
413 #define APP_MAX_HWQ_IN                  (APP_MAX_LINKS * APP_LINK_MAX_HWQ_IN)
414
415 #define APP_MAX_HWQ_OUT                 (APP_MAX_LINKS * APP_LINK_MAX_HWQ_OUT)
416
417 #ifndef APP_MAX_PKTQ_SWQ
418 #define APP_MAX_PKTQ_SWQ                         256
419 #endif
420
421 #define APP_MAX_PKTQ_TM                          APP_MAX_LINKS
422
423 #ifndef APP_MAX_PKTQ_SOURCE
424 #define APP_MAX_PKTQ_SOURCE                      64
425 #endif
426
427 #ifndef APP_MAX_PKTQ_SINK
428 #define APP_MAX_PKTQ_SINK                        64
429 #endif
430
431 #ifndef APP_MAX_MSGQ
432 #define APP_MAX_MSGQ                             256
433 #endif
434
435 #ifndef APP_EAL_ARGC
436 #define APP_EAL_ARGC                             64
437 #endif
438
439 #ifndef APP_MAX_PIPELINE_TYPES
440 #define APP_MAX_PIPELINE_TYPES                   64
441 #endif
442
443 #ifndef APP_MAX_THREADS
444 #define APP_MAX_THREADS                          RTE_MAX_LCORE
445 #endif
446
447 #ifndef APP_MAX_CMDS
448 #define APP_MAX_CMDS                             64
449 #endif
450
451 #ifndef APP_THREAD_HEADROOM_STATS_COLLECT
452 #define APP_THREAD_HEADROOM_STATS_COLLECT        1
453 #endif
454
455 struct app_params {
456         /* Config */
457         char app_name[APP_APPNAME_SIZE];
458         const char *config_file;
459         const char *script_file;
460         const char *parser_file;
461         const char *output_file;
462         const char *preproc;
463         const char *preproc_args;
464         uint64_t port_mask;
465         uint32_t log_level;
466
467         struct app_eal_params eal_params;
468         struct app_mempool_params mempool_params[APP_MAX_MEMPOOLS];
469         struct app_link_params link_params[APP_MAX_LINKS];
470         struct app_pktq_hwq_in_params hwq_in_params[APP_MAX_HWQ_IN];
471         struct app_pktq_hwq_out_params hwq_out_params[APP_MAX_HWQ_OUT];
472         struct app_pktq_swq_params swq_params[APP_MAX_PKTQ_SWQ];
473         struct app_pktq_tm_params tm_params[APP_MAX_PKTQ_TM];
474         struct app_pktq_source_params source_params[APP_MAX_PKTQ_SOURCE];
475         struct app_pktq_sink_params sink_params[APP_MAX_PKTQ_SINK];
476         struct app_msgq_params msgq_params[APP_MAX_MSGQ];
477         struct app_pipeline_params pipeline_params[APP_MAX_PIPELINES];
478
479         uint32_t n_mempools;
480         uint32_t n_links;
481         uint32_t n_pktq_hwq_in;
482         uint32_t n_pktq_hwq_out;
483         uint32_t n_pktq_swq;
484         uint32_t n_pktq_tm;
485         uint32_t n_pktq_source;
486         uint32_t n_pktq_sink;
487         uint32_t n_msgq;
488         uint32_t n_pipelines;
489
490         /* Init */
491         char *eal_argv[1 + APP_EAL_ARGC];
492         struct cpu_core_map *core_map;
493         uint64_t core_mask;
494         struct rte_mempool *mempool[APP_MAX_MEMPOOLS];
495         struct app_link_data link_data[APP_MAX_LINKS];
496         struct rte_ring *swq[APP_MAX_PKTQ_SWQ];
497         struct rte_sched_port *tm[APP_MAX_PKTQ_TM];
498         struct rte_ring *msgq[APP_MAX_MSGQ];
499         struct pipeline_type pipeline_type[APP_MAX_PIPELINE_TYPES];
500         struct app_pipeline_data pipeline_data[APP_MAX_PIPELINES];
501         struct app_thread_data thread_data[APP_MAX_THREADS];
502         cmdline_parse_ctx_t cmds[APP_MAX_CMDS + 1];
503
504         int eal_argc;
505         uint32_t n_pipeline_types;
506         uint32_t n_cmds;
507 };
508
509 #define APP_PARAM_VALID(obj) ((obj)->name != NULL)
510
511 #define APP_PARAM_COUNT(obj_array, n_objs)                              \
512 {                                                                       \
513         size_t i;                                                       \
514                                                                         \
515         n_objs = 0;                                                     \
516         for (i = 0; i < RTE_DIM(obj_array); i++)                        \
517                 if (APP_PARAM_VALID(&((obj_array)[i])))                 \
518                         n_objs++;                                       \
519 }
520
521 #define APP_PARAM_FIND(obj_array, key)                                  \
522 ({                                                                      \
523         ssize_t obj_idx;                                                \
524         const ssize_t obj_count = RTE_DIM(obj_array);                   \
525                                                                         \
526         for (obj_idx = 0; obj_idx < obj_count; obj_idx++) {             \
527                 if (!APP_PARAM_VALID(&((obj_array)[obj_idx])))          \
528                         continue;                                       \
529                                                                         \
530                 if (strcmp(key, (obj_array)[obj_idx].name) == 0)        \
531                         break;                                          \
532         }                                                               \
533         obj_idx < obj_count ? obj_idx : -ENOENT;                        \
534 })
535
536 #define APP_PARAM_FIND_BY_ID(obj_array, prefix, id, obj)                \
537 do {                                                                    \
538         char name[APP_PARAM_NAME_SIZE];                                 \
539         ssize_t pos;                                                    \
540                                                                         \
541         sprintf(name, prefix "%" PRIu32, id);                           \
542         pos = APP_PARAM_FIND(obj_array, name);                          \
543         obj = (pos < 0) ? NULL : &((obj_array)[pos]);                   \
544 } while (0)
545
546 #define APP_PARAM_GET_ID(obj, prefix, id)                               \
547 do                                                                      \
548         sscanf(obj->name, prefix "%" SCNu32, &id);                              \
549 while (0)                                                               \
550
551 #define APP_CHECK(exp, fmt, ...)                                        \
552 do {                                                                    \
553         if (!(exp)) {                                                   \
554                 fprintf(stderr, fmt "\n", ## __VA_ARGS__);              \
555                 abort();                                                \
556         }                                                               \
557 } while (0)
558
559 enum app_log_level {
560         APP_LOG_LEVEL_HIGH = 1,
561         APP_LOG_LEVEL_LOW,
562         APP_LOG_LEVELS
563 };
564
565 #define APP_LOG(app, level, fmt, ...)                                   \
566 do {                                                                    \
567         if (app->log_level >= APP_LOG_LEVEL_ ## level)                  \
568                 fprintf(stdout, "[APP] " fmt "\n", ## __VA_ARGS__);     \
569 } while (0)
570
571 static inline uint32_t
572 app_link_get_n_rxq(struct app_params *app, struct app_link_params *link)
573 {
574         uint32_t n_rxq = 0, link_id, i;
575         uint32_t n_pktq_hwq_in = RTE_MIN(app->n_pktq_hwq_in,
576                 RTE_DIM(app->hwq_in_params));
577
578         APP_PARAM_GET_ID(link, "LINK", link_id);
579
580         for (i = 0; i < n_pktq_hwq_in; i++) {
581                 struct app_pktq_hwq_in_params *p = &app->hwq_in_params[i];
582                 uint32_t rxq_link_id, rxq_queue_id;
583
584                 sscanf(p->name, "RXQ%" SCNu32 ".%" SCNu32,
585                         &rxq_link_id, &rxq_queue_id);
586                 if (rxq_link_id == link_id)
587                         n_rxq++;
588         }
589
590         return n_rxq;
591 }
592
593 static inline uint32_t
594 app_link_get_n_txq(struct app_params *app, struct app_link_params *link)
595 {
596         uint32_t n_txq = 0, link_id, i;
597         uint32_t n_pktq_hwq_out = RTE_MIN(app->n_pktq_hwq_out,
598                 RTE_DIM(app->hwq_out_params));
599
600         APP_PARAM_GET_ID(link, "LINK", link_id);
601
602         for (i = 0; i < n_pktq_hwq_out; i++) {
603                 struct app_pktq_hwq_out_params *p = &app->hwq_out_params[i];
604                 uint32_t txq_link_id, txq_queue_id;
605
606                 sscanf(p->name, "TXQ%" SCNu32 ".%" SCNu32,
607                         &txq_link_id, &txq_queue_id);
608                 if (txq_link_id == link_id)
609                         n_txq++;
610         }
611
612         return n_txq;
613 }
614
615 static inline uint32_t
616 app_rxq_get_readers(struct app_params *app, struct app_pktq_hwq_in_params *rxq)
617 {
618         uint32_t pos = rxq - app->hwq_in_params;
619         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
620                 RTE_DIM(app->pipeline_params));
621         uint32_t n_readers = 0, i;
622
623         for (i = 0; i < n_pipelines; i++) {
624                 struct app_pipeline_params *p = &app->pipeline_params[i];
625                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
626                 uint32_t j;
627
628                 for (j = 0; j < n_pktq_in; j++) {
629                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
630
631                         if ((pktq->type == APP_PKTQ_IN_HWQ) &&
632                                 (pktq->id == pos))
633                                 n_readers++;
634                 }
635         }
636
637         return n_readers;
638 }
639
640 static inline uint32_t
641 app_swq_get_readers(struct app_params *app, struct app_pktq_swq_params *swq)
642 {
643         uint32_t pos = swq - app->swq_params;
644         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
645                 RTE_DIM(app->pipeline_params));
646         uint32_t n_readers = 0, i;
647
648         for (i = 0; i < n_pipelines; i++) {
649                 struct app_pipeline_params *p = &app->pipeline_params[i];
650                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
651                 uint32_t j;
652
653                 for (j = 0; j < n_pktq_in; j++) {
654                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
655
656                         if ((pktq->type == APP_PKTQ_IN_SWQ) &&
657                                 (pktq->id == pos))
658                                 n_readers++;
659                 }
660         }
661
662         return n_readers;
663 }
664
665 static inline struct app_pipeline_params *
666 app_swq_get_reader(struct app_params *app,
667         struct app_pktq_swq_params *swq,
668         uint32_t *pktq_in_id)
669 {
670         struct app_pipeline_params *reader;
671         uint32_t pos = swq - app->swq_params;
672         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
673                 RTE_DIM(app->pipeline_params));
674         uint32_t n_readers = 0, id, i;
675
676         for (i = 0; i < n_pipelines; i++) {
677                 struct app_pipeline_params *p = &app->pipeline_params[i];
678                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
679                 uint32_t j;
680
681                 for (j = 0; j < n_pktq_in; j++) {
682                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
683
684                         if ((pktq->type == APP_PKTQ_IN_SWQ) &&
685                                 (pktq->id == pos)) {
686                                 n_readers++;
687                                 reader = p;
688                                 id = j;
689                         }
690                 }
691         }
692
693         if (n_readers != 1)
694                 return NULL;
695
696         *pktq_in_id = id;
697         return reader;
698 }
699
700 static inline uint32_t
701 app_tm_get_readers(struct app_params *app, struct app_pktq_tm_params *tm)
702 {
703         uint32_t pos = tm - app->tm_params;
704         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
705                 RTE_DIM(app->pipeline_params));
706         uint32_t n_readers = 0, i;
707
708         for (i = 0; i < n_pipelines; i++) {
709                 struct app_pipeline_params *p = &app->pipeline_params[i];
710                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
711                 uint32_t j;
712
713                 for (j = 0; j < n_pktq_in; j++) {
714                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
715
716                         if ((pktq->type == APP_PKTQ_IN_TM) &&
717                                 (pktq->id == pos))
718                                 n_readers++;
719                 }
720         }
721
722         return n_readers;
723 }
724
725 static inline struct app_pipeline_params *
726 app_tm_get_reader(struct app_params *app,
727         struct app_pktq_tm_params *tm,
728         uint32_t *pktq_in_id)
729 {
730         struct app_pipeline_params *reader;
731         uint32_t pos = tm - app->tm_params;
732         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
733                 RTE_DIM(app->pipeline_params));
734         uint32_t n_readers = 0, id, i;
735
736         for (i = 0; i < n_pipelines; i++) {
737                 struct app_pipeline_params *p = &app->pipeline_params[i];
738                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
739                 uint32_t j;
740
741                 for (j = 0; j < n_pktq_in; j++) {
742                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
743
744                         if ((pktq->type == APP_PKTQ_IN_TM) &&
745                                 (pktq->id == pos)) {
746                                 n_readers++;
747                                 reader = p;
748                                 id = j;
749                         }
750                 }
751         }
752
753         if (n_readers != 1)
754                 return NULL;
755
756         *pktq_in_id = id;
757         return reader;
758 }
759
760 static inline uint32_t
761 app_source_get_readers(struct app_params *app,
762 struct app_pktq_source_params *source)
763 {
764         uint32_t pos = source - app->source_params;
765         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
766                 RTE_DIM(app->pipeline_params));
767         uint32_t n_readers = 0, i;
768
769         for (i = 0; i < n_pipelines; i++) {
770                 struct app_pipeline_params *p = &app->pipeline_params[i];
771                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
772                 uint32_t j;
773
774                 for (j = 0; j < n_pktq_in; j++) {
775                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
776
777                         if ((pktq->type == APP_PKTQ_IN_SOURCE) &&
778                                 (pktq->id == pos))
779                                 n_readers++;
780                 }
781         }
782
783         return n_readers;
784 }
785
786 static inline uint32_t
787 app_msgq_get_readers(struct app_params *app, struct app_msgq_params *msgq)
788 {
789         uint32_t pos = msgq - app->msgq_params;
790         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
791                 RTE_DIM(app->pipeline_params));
792         uint32_t n_readers = 0, i;
793
794         for (i = 0; i < n_pipelines; i++) {
795                 struct app_pipeline_params *p = &app->pipeline_params[i];
796                 uint32_t n_msgq_in = RTE_MIN(p->n_msgq_in, RTE_DIM(p->msgq_in));
797                 uint32_t j;
798
799                 for (j = 0; j < n_msgq_in; j++)
800                         if (p->msgq_in[j] == pos)
801                                 n_readers++;
802         }
803
804         return n_readers;
805 }
806
807 static inline uint32_t
808 app_txq_get_writers(struct app_params *app, struct app_pktq_hwq_out_params *txq)
809 {
810         uint32_t pos = txq - app->hwq_out_params;
811         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
812                 RTE_DIM(app->pipeline_params));
813         uint32_t n_writers = 0, i;
814
815         for (i = 0; i < n_pipelines; i++) {
816                 struct app_pipeline_params *p = &app->pipeline_params[i];
817                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
818                         RTE_DIM(p->pktq_out));
819                 uint32_t j;
820
821                 for (j = 0; j < n_pktq_out; j++) {
822                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
823
824                         if ((pktq->type == APP_PKTQ_OUT_HWQ) &&
825                                 (pktq->id == pos))
826                                 n_writers++;
827                 }
828         }
829
830         return n_writers;
831 }
832
833 static inline uint32_t
834 app_swq_get_writers(struct app_params *app, struct app_pktq_swq_params *swq)
835 {
836         uint32_t pos = swq - app->swq_params;
837         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
838                 RTE_DIM(app->pipeline_params));
839         uint32_t n_writers = 0, i;
840
841         for (i = 0; i < n_pipelines; i++) {
842                 struct app_pipeline_params *p = &app->pipeline_params[i];
843                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
844                         RTE_DIM(p->pktq_out));
845                 uint32_t j;
846
847                 for (j = 0; j < n_pktq_out; j++) {
848                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
849
850                         if ((pktq->type == APP_PKTQ_OUT_SWQ) &&
851                                 (pktq->id == pos))
852                                 n_writers++;
853                 }
854         }
855
856         return n_writers;
857 }
858
859 static inline struct app_pipeline_params *
860 app_swq_get_writer(struct app_params *app,
861         struct app_pktq_swq_params *swq,
862         uint32_t *pktq_out_id)
863 {
864         struct app_pipeline_params *writer;
865         uint32_t pos = swq - app->swq_params;
866         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
867                 RTE_DIM(app->pipeline_params));
868         uint32_t n_writers = 0, id, i;
869
870         for (i = 0; i < n_pipelines; i++) {
871                 struct app_pipeline_params *p = &app->pipeline_params[i];
872                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
873                         RTE_DIM(p->pktq_out));
874                 uint32_t j;
875
876                 for (j = 0; j < n_pktq_out; j++) {
877                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
878
879                         if ((pktq->type == APP_PKTQ_OUT_SWQ) &&
880                                 (pktq->id == pos)) {
881                                 n_writers++;
882                                 writer = p;
883                                 id = j;
884                         }
885                 }
886         }
887
888         if (n_writers != 1)
889                 return NULL;
890
891         *pktq_out_id = id;
892         return writer;
893 }
894
895 static inline uint32_t
896 app_tm_get_writers(struct app_params *app, struct app_pktq_tm_params *tm)
897 {
898         uint32_t pos = tm - app->tm_params;
899         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
900                 RTE_DIM(app->pipeline_params));
901         uint32_t n_writers = 0, i;
902
903         for (i = 0; i < n_pipelines; i++) {
904                 struct app_pipeline_params *p = &app->pipeline_params[i];
905                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
906                         RTE_DIM(p->pktq_out));
907                 uint32_t j;
908
909                 for (j = 0; j < n_pktq_out; j++) {
910                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
911
912                         if ((pktq->type == APP_PKTQ_OUT_TM) &&
913                                 (pktq->id == pos))
914                                 n_writers++;
915                 }
916         }
917
918         return n_writers;
919 }
920
921 static inline struct app_pipeline_params *
922 app_tm_get_writer(struct app_params *app,
923         struct app_pktq_tm_params *tm,
924         uint32_t *pktq_out_id)
925 {
926         struct app_pipeline_params *writer;
927         uint32_t pos = tm - app->tm_params;
928         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
929                 RTE_DIM(app->pipeline_params));
930         uint32_t n_writers = 0, id, i;
931
932         for (i = 0; i < n_pipelines; i++) {
933                 struct app_pipeline_params *p = &app->pipeline_params[i];
934                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
935                         RTE_DIM(p->pktq_out));
936                 uint32_t j;
937
938                 for (j = 0; j < n_pktq_out; j++) {
939                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
940
941                         if ((pktq->type == APP_PKTQ_OUT_TM) &&
942                                 (pktq->id == pos))
943                                 n_writers++;
944                                 writer = p;
945                                 id = j;
946                 }
947         }
948
949         if (n_writers != 1)
950                 return NULL;
951
952         *pktq_out_id = id;
953         return writer;
954 }
955
956 static inline uint32_t
957 app_sink_get_writers(struct app_params *app, struct app_pktq_sink_params *sink)
958 {
959         uint32_t pos = sink - app->sink_params;
960         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
961                 RTE_DIM(app->pipeline_params));
962         uint32_t n_writers = 0, i;
963
964         for (i = 0; i < n_pipelines; i++) {
965                 struct app_pipeline_params *p = &app->pipeline_params[i];
966                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
967                         RTE_DIM(p->pktq_out));
968                 uint32_t j;
969
970                 for (j = 0; j < n_pktq_out; j++) {
971                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
972
973                         if ((pktq->type == APP_PKTQ_OUT_SINK) &&
974                                 (pktq->id == pos))
975                                 n_writers++;
976                 }
977         }
978
979         return n_writers;
980 }
981
982 static inline uint32_t
983 app_msgq_get_writers(struct app_params *app, struct app_msgq_params *msgq)
984 {
985         uint32_t pos = msgq - app->msgq_params;
986         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
987                 RTE_DIM(app->pipeline_params));
988         uint32_t n_writers = 0, i;
989
990         for (i = 0; i < n_pipelines; i++) {
991                 struct app_pipeline_params *p = &app->pipeline_params[i];
992                 uint32_t n_msgq_out = RTE_MIN(p->n_msgq_out,
993                         RTE_DIM(p->msgq_out));
994                 uint32_t j;
995
996                 for (j = 0; j < n_msgq_out; j++)
997                         if (p->msgq_out[j] == pos)
998                                 n_writers++;
999         }
1000
1001         return n_writers;
1002 }
1003
1004 static inline struct app_link_params *
1005 app_get_link_for_rxq(struct app_params *app, struct app_pktq_hwq_in_params *p)
1006 {
1007         char link_name[APP_PARAM_NAME_SIZE];
1008         ssize_t link_param_idx;
1009         uint32_t rxq_link_id, rxq_queue_id;
1010
1011         sscanf(p->name, "RXQ%" SCNu32 ".%" SCNu32,
1012                 &rxq_link_id, &rxq_queue_id);
1013         sprintf(link_name, "LINK%" PRIu32, rxq_link_id);
1014         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
1015         APP_CHECK((link_param_idx >= 0),
1016                 "Cannot find %s for %s", link_name, p->name);
1017
1018         return &app->link_params[link_param_idx];
1019 }
1020
1021 static inline struct app_link_params *
1022 app_get_link_for_txq(struct app_params *app, struct app_pktq_hwq_out_params *p)
1023 {
1024         char link_name[APP_PARAM_NAME_SIZE];
1025         ssize_t link_param_idx;
1026         uint32_t txq_link_id, txq_queue_id;
1027
1028         sscanf(p->name, "TXQ%" SCNu32 ".%" SCNu32,
1029                 &txq_link_id, &txq_queue_id);
1030         sprintf(link_name, "LINK%" PRIu32, txq_link_id);
1031         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
1032         APP_CHECK((link_param_idx >= 0),
1033                 "Cannot find %s for %s", link_name, p->name);
1034
1035         return &app->link_params[link_param_idx];
1036 }
1037
1038 static inline struct app_link_params *
1039 app_get_link_for_tm(struct app_params *app, struct app_pktq_tm_params *p_tm)
1040 {
1041         char link_name[APP_PARAM_NAME_SIZE];
1042         uint32_t link_id;
1043         ssize_t link_param_idx;
1044
1045         sscanf(p_tm->name, "TM%" PRIu32, &link_id);
1046         sprintf(link_name, "LINK%" PRIu32, link_id);
1047         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
1048         APP_CHECK((link_param_idx >= 0),
1049                 "Cannot find %s for %s", link_name, p_tm->name);
1050
1051         return &app->link_params[link_param_idx];
1052 }
1053
1054 void app_pipeline_params_get(struct app_params *app,
1055         struct app_pipeline_params *p_in,
1056         struct pipeline_params *p_out);
1057
1058 int app_config_init(struct app_params *app);
1059
1060 int app_config_args(struct app_params *app,
1061         int argc, char **argv);
1062
1063 int app_config_preproc(struct app_params *app);
1064
1065 int app_config_parse(struct app_params *app,
1066         const char *file_name);
1067
1068 int app_config_parse_tm(struct app_params *app);
1069
1070 void app_config_save(struct app_params *app,
1071         const char *file_name);
1072
1073 int app_config_check(struct app_params *app);
1074
1075 int app_init(struct app_params *app);
1076
1077 int app_post_init(struct app_params *app);
1078
1079 int app_thread(void *arg);
1080
1081 int app_pipeline_type_register(struct app_params *app,
1082         struct pipeline_type *ptype);
1083
1084 struct pipeline_type *app_pipeline_type_find(struct app_params *app,
1085         char *name);
1086
1087 void app_link_up_internal(struct app_params *app,
1088         struct app_link_params *cp);
1089
1090 void app_link_down_internal(struct app_params *app,
1091         struct app_link_params *cp);
1092
1093 #endif