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