Fix warning when compiling with ninja
[samplevnf.git] / common / vnf_common / app.h
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef __INCLUDE_APP_H__
18 #define __INCLUDE_APP_H__
19
20 #include <stdint.h>
21 #include <string.h>
22
23 #include <rte_common.h>
24 #include <rte_mempool.h>
25 #include <rte_ring.h>
26 #include <rte_sched.h>
27 #include <rte_timer.h>
28 #include <cmdline_parse.h>
29
30 #include <rte_ethdev.h>
31
32 #include "cpu_core_map.h"
33 #include "pipeline.h"
34
35 #define APP_PARAM_NAME_SIZE                      PIPELINE_NAME_SIZE
36 #define APP_LINK_PCI_BDF_SIZE                    32
37 struct app_link_params *fdir_p_link;
38 struct app_mempool_params {
39         char *name;
40         uint32_t parsed;
41         uint32_t buffer_size;
42         uint32_t pool_size;
43         uint32_t cache_size;
44         uint32_t cpu_socket_id;
45 };
46
47 struct app_link_params {
48         char *name;
49         uint32_t parsed;
50         uint32_t pmd_id; /* Generated based on port mask */
51         uint32_t arp_q; /* 0 = Disabled (packets go to default queue 0) */
52         uint32_t tcp_syn_q; /* 0 = Disabled (pkts go to default queue) */
53         uint32_t ip_local_q; /* 0 = Disabled (pkts go to default queue 0) */
54         uint32_t tcp_local_q; /* 0 = Disabled (pkts go to default queue 0) */
55         uint32_t udp_local_q; /* 0 = Disabled (pkts go to default queue 0) */
56         uint32_t sctp_local_q; /* 0 = Disabled (pkts go to default queue 0) */
57         uint32_t state; /* DOWN = 0, UP = 1 */
58         uint32_t ip; /* 0 = Invalid */
59         uint8_t ipv6[16];
60         uint32_t depth;         /* Valid only when IP is valid */
61         uint32_t depth_ipv6;
62         uint64_t mac_addr; /* Read from HW */
63         char pci_bdf[APP_LINK_PCI_BDF_SIZE];
64
65         struct rte_eth_conf conf;
66         uint8_t promisc;
67 };
68
69 struct app_pktq_hwq_in_params {
70         char *name;
71         uint32_t parsed;
72         uint32_t mempool_id; /* Position in the app->mempool_params */
73         uint32_t size;
74         uint32_t burst;
75
76         struct rte_eth_rxconf conf;
77 };
78
79 struct app_pktq_hwq_out_params {
80         char *name;
81         uint32_t parsed;
82         uint32_t size;
83         uint32_t burst;
84         uint32_t dropless;
85         uint64_t n_retries;
86         struct rte_eth_txconf conf;
87 };
88
89 struct app_pktq_swq_params {
90         char *name;
91         uint32_t parsed;
92         uint32_t size;
93         uint32_t burst_read;
94         uint32_t burst_write;
95         uint32_t dropless;
96         uint64_t n_retries;
97         uint32_t cpu_socket_id;
98         uint32_t ipv4_frag;
99         uint32_t ipv6_frag;
100         uint32_t ipv4_ras;
101         uint32_t ipv6_ras;
102         uint32_t mtu;
103         uint32_t metadata_size;
104         uint32_t mempool_direct_id;
105         uint32_t mempool_indirect_id;
106 };
107
108 #ifndef APP_FILE_NAME_SIZE
109 #define APP_FILE_NAME_SIZE                       256
110 #endif
111
112 #ifndef APP_MAX_SCHED_SUBPORTS
113 #define APP_MAX_SCHED_SUBPORTS                   8
114 #endif
115
116 #ifndef APP_MAX_SCHED_PIPES
117 #define APP_MAX_SCHED_PIPES                      4096
118 #endif
119
120 struct app_pktq_tm_params {
121         char *name;
122         uint32_t parsed;
123         const char *file_name;
124         struct rte_sched_port_params sched_port_params;
125         struct rte_sched_subport_params
126                 sched_subport_params[APP_MAX_SCHED_SUBPORTS];
127         struct rte_sched_pipe_params
128                 sched_pipe_profiles[RTE_SCHED_PIPE_PROFILES_PER_PORT];
129         int sched_pipe_to_profile[APP_MAX_SCHED_SUBPORTS * APP_MAX_SCHED_PIPES];
130         uint32_t burst_read;
131         uint32_t burst_write;
132 };
133
134 struct app_pktq_source_params {
135         char *name;
136         uint32_t parsed;
137         uint32_t mempool_id; /* Position in the app->mempool_params array */
138         uint32_t burst;
139         char *file_name; /* Full path of PCAP file to be copied to mbufs */
140         uint32_t n_bytes_per_pkt;
141 };
142
143 struct app_pktq_sink_params {
144         char *name;
145         uint8_t parsed;
146         char *file_name; /* Full path of PCAP file to be copied to mbufs */
147         uint32_t n_pkts_to_dump;
148 };
149
150 struct app_msgq_params {
151         char *name;
152         uint32_t parsed;
153         uint32_t size;
154         uint32_t cpu_socket_id;
155 };
156
157 enum app_pktq_in_type {
158         APP_PKTQ_IN_HWQ,
159         APP_PKTQ_IN_SWQ,
160         APP_PKTQ_IN_TM,
161         APP_PKTQ_IN_SOURCE,
162 };
163
164 struct app_pktq_in_params {
165         enum app_pktq_in_type type;
166         uint32_t id; /* Position in the appropriate app array */
167 };
168
169 enum app_pktq_out_type {
170         APP_PKTQ_OUT_HWQ,
171         APP_PKTQ_OUT_SWQ,
172         APP_PKTQ_OUT_TM,
173         APP_PKTQ_OUT_SINK,
174 };
175
176 struct app_pktq_out_params {
177         enum app_pktq_out_type type;
178         uint32_t id; /* Position in the appropriate app array */
179 };
180
181 #ifndef APP_PIPELINE_TYPE_SIZE
182 #define APP_PIPELINE_TYPE_SIZE                   64
183 #endif
184
185 #define APP_MAX_PIPELINE_PKTQ_IN                 PIPELINE_MAX_PORT_IN
186 #define APP_MAX_PIPELINE_PKTQ_OUT                PIPELINE_MAX_PORT_OUT
187 #define APP_MAX_PIPELINE_MSGQ_IN                 PIPELINE_MAX_MSGQ_IN
188 #define APP_MAX_PIPELINE_MSGQ_OUT                PIPELINE_MAX_MSGQ_OUT
189
190 #define APP_MAX_PIPELINE_ARGS                    PIPELINE_MAX_ARGS
191
192 struct app_pipeline_params {
193         char *name;
194         uint8_t parsed;
195
196         char type[APP_PIPELINE_TYPE_SIZE];
197
198         uint32_t socket_id;
199         uint32_t core_id;
200         uint32_t hyper_th_id;
201
202         struct app_pktq_in_params pktq_in[APP_MAX_PIPELINE_PKTQ_IN];
203         struct app_pktq_out_params pktq_out[APP_MAX_PIPELINE_PKTQ_OUT];
204         uint32_t msgq_in[APP_MAX_PIPELINE_MSGQ_IN];
205         uint32_t msgq_out[APP_MAX_PIPELINE_MSGQ_OUT];
206
207         uint32_t n_pktq_in;
208         uint32_t n_pktq_out;
209         uint32_t n_msgq_in;
210         uint32_t n_msgq_out;
211
212         uint32_t timer_period;
213
214         char *args_name[APP_MAX_PIPELINE_ARGS];
215         char *args_value[APP_MAX_PIPELINE_ARGS];
216         uint32_t n_args;
217 };
218
219 struct app_pipeline_data {
220         void *be;
221         void *fe;
222         struct pipeline_type *ptype;
223         uint64_t timer_period;
224         uint32_t enabled;
225 };
226
227 struct app_thread_pipeline_data {
228         uint32_t pipeline_id;
229         void *be;
230         pipeline_be_op_run f_run;
231         pipeline_be_op_timer f_timer;
232         uint64_t timer_period;
233         uint64_t deadline;
234 };
235
236 #ifndef APP_MAX_THREAD_PIPELINES
237 #define APP_MAX_THREAD_PIPELINES                 32
238 #endif
239
240 #ifndef APP_THREAD_TIMER_PERIOD
241 #define APP_THREAD_TIMER_PERIOD                  1
242 #endif
243
244 struct app_thread_data {
245         struct app_thread_pipeline_data regular[APP_MAX_THREAD_PIPELINES];
246         struct app_thread_pipeline_data custom[APP_MAX_THREAD_PIPELINES];
247
248         uint32_t n_regular;
249         uint32_t n_custom;
250
251         uint64_t timer_period;
252         uint64_t thread_req_deadline;
253
254         uint64_t deadline;
255
256         struct rte_ring *msgq_in;
257         struct rte_ring *msgq_out;
258
259         uint64_t headroom_time;
260         uint64_t headroom_cycles;
261         double headroom_ratio;
262 };
263
264 #ifndef APP_MAX_LINKS
265 #define APP_MAX_LINKS                            32
266 #endif
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[APP_MAX_LINKS];
290
291         /* Add a PCI device in white list. */
292         char *pci_whitelist[APP_MAX_LINKS];
293
294         /* Add a virtual device. */
295         char *vdev[APP_MAX_LINKS];
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_LINK_MAX_HWQ_IN
371 #define APP_LINK_MAX_HWQ_IN                      64
372 #endif
373
374 #ifndef APP_LINK_MAX_HWQ_OUT
375 #define APP_LINK_MAX_HWQ_OUT                     64
376 #endif
377
378 #define APP_MAX_HWQ_IN                     (APP_MAX_LINKS * APP_LINK_MAX_HWQ_IN)
379
380 #define APP_MAX_HWQ_OUT                   (APP_MAX_LINKS * APP_LINK_MAX_HWQ_OUT)
381
382 #ifndef APP_MAX_PKTQ_SWQ
383 #define APP_MAX_PKTQ_SWQ                         256
384 #endif
385
386 #define APP_MAX_PKTQ_TM                          APP_MAX_LINKS
387
388 #ifndef APP_MAX_PKTQ_SOURCE
389 #define APP_MAX_PKTQ_SOURCE                      32
390 #endif
391
392 #ifndef APP_MAX_PKTQ_SINK
393 #define APP_MAX_PKTQ_SINK                        32
394 #endif
395
396 #ifndef APP_MAX_MSGQ
397 #define APP_MAX_MSGQ                             128
398 #endif
399
400 #ifndef APP_MAX_PIPELINES
401 #define APP_MAX_PIPELINES                        64
402 #endif
403
404 #ifndef APP_EAL_ARGC
405 #define APP_EAL_ARGC                             32
406 #endif
407
408 #ifndef APP_MAX_PIPELINE_TYPES
409 #define APP_MAX_PIPELINE_TYPES                   64
410 #endif
411
412 #ifndef APP_MAX_THREADS
413 #define APP_MAX_THREADS                          RTE_MAX_LCORE
414 #endif
415
416 #ifndef APP_MAX_CMDS
417 #define APP_MAX_CMDS                             128
418 #endif
419
420 #ifndef APP_THREAD_HEADROOM_STATS_COLLECT
421 #define APP_THREAD_HEADROOM_STATS_COLLECT        1
422 #endif
423
424 uint8_t enable_hwlb;
425 uint8_t enable_flow_dir;
426
427 #define APP_CORE_MASK_SIZE                                      \
428         (RTE_MAX_LCORE / 64 + ((RTE_MAX_LCORE % 64) ? 1 : 0))
429
430 struct app_params {
431         /* Config */
432         char app_name[APP_APPNAME_SIZE];
433         const char *config_file;
434         const char *script_file;
435         const char *parser_file;
436         const char *output_file;
437         const char *preproc;
438         const char *preproc_args;
439         uint64_t port_mask;
440         uint32_t log_level;
441
442         struct app_eal_params eal_params;
443         struct app_mempool_params mempool_params[APP_MAX_MEMPOOLS];
444         struct app_link_params link_params[APP_MAX_LINKS];
445         struct app_pktq_hwq_in_params hwq_in_params[APP_MAX_HWQ_IN];
446         struct app_pktq_hwq_out_params hwq_out_params[APP_MAX_HWQ_OUT];
447         struct app_pktq_swq_params swq_params[APP_MAX_PKTQ_SWQ];
448         struct app_pktq_tm_params tm_params[APP_MAX_PKTQ_TM];
449         struct app_pktq_source_params source_params[APP_MAX_PKTQ_SOURCE];
450         struct app_pktq_sink_params sink_params[APP_MAX_PKTQ_SINK];
451         struct app_msgq_params msgq_params[APP_MAX_MSGQ];
452         struct app_pipeline_params pipeline_params[APP_MAX_PIPELINES];
453
454         uint32_t n_mempools;
455         uint32_t n_links;
456         uint32_t n_pktq_hwq_in;
457         uint32_t n_pktq_hwq_out;
458         uint32_t n_pktq_swq;
459         uint32_t n_pktq_tm;
460         uint32_t n_pktq_source;
461         uint32_t n_pktq_sink;
462         uint32_t n_msgq;
463         uint32_t n_pipelines;
464
465         uint32_t header_csum_req;
466         uint32_t n_hwlb_q;
467         /* Init */
468         char *eal_argv[1 + APP_EAL_ARGC];
469         struct cpu_core_map *core_map;
470         uint64_t core_mask[APP_CORE_MASK_SIZE];
471         struct rte_mempool *mempool[APP_MAX_MEMPOOLS];
472         struct rte_ring *swq[APP_MAX_PKTQ_SWQ];
473         struct rte_sched_port *tm[APP_MAX_PKTQ_TM];
474         struct rte_ring *msgq[APP_MAX_MSGQ];
475         struct pipeline_type pipeline_type[APP_MAX_PIPELINE_TYPES];
476         struct app_pipeline_data pipeline_data[APP_MAX_PIPELINES];
477         struct app_thread_data thread_data[APP_MAX_THREADS];
478         cmdline_parse_ctx_t cmds[APP_MAX_CMDS + 1];
479
480         int eal_argc;
481         uint32_t n_pipeline_types;
482         uint32_t n_cmds;
483 };
484
485 #define APP_PARAM_VALID(obj) ((obj)->name != NULL)
486
487 #define APP_PARAM_COUNT(obj_array, n_objs)                              \
488 {                                                                       \
489         size_t i;                                                       \
490                                                                         \
491         n_objs = 0;                                                     \
492         for (i = 0; i < RTE_DIM(obj_array); i++)                        \
493                 if (APP_PARAM_VALID(&((obj_array)[i])))                 \
494                         n_objs++;                                       \
495 }
496
497 #define APP_PARAM_FIND(obj_array, key)                                  \
498 ({                                                                      \
499         ssize_t obj_idx;                                                \
500         const ssize_t obj_count = RTE_DIM(obj_array);                   \
501                                                                         \
502         for (obj_idx = 0; obj_idx < obj_count; obj_idx++) {             \
503                 if (!APP_PARAM_VALID(&((obj_array)[obj_idx])))          \
504                         continue;                                       \
505                                                                         \
506                 if (strcmp(key, (obj_array)[obj_idx].name) == 0)        \
507                         break;                                          \
508         }                                                               \
509         obj_idx < obj_count ? obj_idx : -ENOENT;                        \
510 })
511
512 #define APP_PARAM_FIND_BY_ID(obj_array, prefix, id, obj)                \
513 do {                                                                    \
514         char name[APP_PARAM_NAME_SIZE];                                 \
515         ssize_t pos;                                                    \
516                                                                         \
517         sprintf(name, prefix "%" PRIu32, id);                           \
518         pos = APP_PARAM_FIND(obj_array, name);                          \
519         obj = (pos < 0) ? NULL : &((obj_array)[pos]);                   \
520 } while (0)
521
522 #define APP_PARAM_GET_ID(obj, prefix, id)                               \
523 do                                                                      \
524         sscanf(obj->name, prefix "%" SCNu32, &id);                              \
525 while (0)                                                               \
526
527 #define APP_PARAM_ADD(obj_array, obj_name)                              \
528 ({                                                                      \
529         ssize_t obj_idx;                                                \
530         const ssize_t obj_count = RTE_DIM(obj_array);                   \
531                                                                         \
532         obj_idx = APP_PARAM_FIND(obj_array, obj_name);                  \
533         if (obj_idx < 0) {                                              \
534                 for (obj_idx = 0; obj_idx < obj_count; obj_idx++) {     \
535                         if (!APP_PARAM_VALID(&((obj_array)[obj_idx])))  \
536                                 break;                                  \
537                 }                                                       \
538                                                                         \
539                 if (obj_idx < obj_count) {                              \
540                         (obj_array)[obj_idx].name = strdup(obj_name);   \
541                         if ((obj_array)[obj_idx].name == NULL)          \
542                                 obj_idx = -EINVAL;                      \
543                 } else                                                  \
544                         obj_idx = -ENOMEM;                              \
545         }                                                               \
546         obj_idx;                                                        \
547 })
548
549 #define APP_CHECK(exp, fmt, ...)                                        \
550 do {                                                                    \
551         if (!(exp)) {                                                   \
552                 fprintf(stderr, fmt "\n", ## __VA_ARGS__);              \
553                 abort();                                                \
554         }                                                               \
555 } while (0)
556
557 enum app_log_level {
558         APP_LOG_LEVEL_HIGH = 1,
559         APP_LOG_LEVEL_LOW,
560         APP_LOG_LEVELS
561 };
562
563 #define APP_LOG(app, level, fmt, ...)                                   \
564 do {                                                                    \
565         if (app->log_level >= APP_LOG_LEVEL_ ## level)                  \
566                 fprintf(stdout, "[APP] " fmt "\n", ## __VA_ARGS__);     \
567 } while (0)
568
569 #ifdef REST_API_SUPPORT
570 static inline int rest_api_supported(void)
571 {
572         return 1;
573 }
574 #else
575 static inline int rest_api_supported(void)
576 {
577         return 0;
578 }
579 #endif
580 static inline uint32_t
581 app_link_get_n_rxq(struct app_params *app, struct app_link_params *link)
582 {
583         uint32_t n_rxq = 0, link_id, i;
584         uint32_t n_pktq_hwq_in = RTE_MIN(app->n_pktq_hwq_in,
585                 RTE_DIM(app->hwq_in_params));
586
587         APP_PARAM_GET_ID(link, "LINK", link_id);
588
589         for (i = 0; i < n_pktq_hwq_in; i++) {
590                 struct app_pktq_hwq_in_params *p = &app->hwq_in_params[i];
591                 uint32_t rxq_link_id, rxq_queue_id;
592
593                 sscanf(p->name, "RXQ%" SCNu32 ".%" SCNu32,
594                         &rxq_link_id, &rxq_queue_id);
595                 if (rxq_link_id == link_id)
596                         n_rxq++;
597         }
598
599         return n_rxq;
600 }
601
602 static inline uint32_t
603 app_link_get_n_txq(struct app_params *app, struct app_link_params *link)
604 {
605         uint32_t n_txq = 0, link_id, i;
606         uint32_t n_pktq_hwq_out = RTE_MIN(app->n_pktq_hwq_out,
607                 RTE_DIM(app->hwq_out_params));
608
609         APP_PARAM_GET_ID(link, "LINK", link_id);
610
611         for (i = 0; i < n_pktq_hwq_out; i++) {
612                 struct app_pktq_hwq_out_params *p = &app->hwq_out_params[i];
613                 uint32_t txq_link_id, txq_queue_id;
614
615                 sscanf(p->name, "TXQ%" SCNu32 ".%" SCNu32,
616                         &txq_link_id, &txq_queue_id);
617                 if (txq_link_id == link_id)
618                         n_txq++;
619         }
620
621         return n_txq;
622 }
623
624 static inline uint32_t
625 app_rxq_get_readers(struct app_params *app, struct app_pktq_hwq_in_params *rxq)
626 {
627         uint32_t pos = rxq - app->hwq_in_params;
628         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
629                 RTE_DIM(app->pipeline_params));
630         uint32_t n_readers = 0, i;
631
632         for (i = 0; i < n_pipelines; i++) {
633                 struct app_pipeline_params *p = &app->pipeline_params[i];
634                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
635                 uint32_t j;
636
637                 for (j = 0; j < n_pktq_in; j++) {
638                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
639
640                         if ((pktq->type == APP_PKTQ_IN_HWQ) &&
641                                 (pktq->id == pos))
642                                 n_readers++;
643                 }
644         }
645
646         return n_readers;
647 }
648
649 static inline uint32_t
650 app_swq_get_readers(struct app_params *app, struct app_pktq_swq_params *swq)
651 {
652         uint32_t pos = swq - app->swq_params;
653         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
654                 RTE_DIM(app->pipeline_params));
655         uint32_t n_readers = 0, i;
656
657         for (i = 0; i < n_pipelines; i++) {
658                 struct app_pipeline_params *p = &app->pipeline_params[i];
659                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
660                 uint32_t j;
661
662                 for (j = 0; j < n_pktq_in; j++) {
663                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
664
665                         if ((pktq->type == APP_PKTQ_IN_SWQ) &&
666                                 (pktq->id == pos))
667                                 n_readers++;
668                 }
669         }
670
671         return n_readers;
672 }
673
674 static inline uint32_t
675 app_tm_get_readers(struct app_params *app, struct app_pktq_tm_params *tm)
676 {
677         uint32_t pos = tm - app->tm_params;
678         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
679                 RTE_DIM(app->pipeline_params));
680         uint32_t n_readers = 0, i;
681
682         for (i = 0; i < n_pipelines; i++) {
683                 struct app_pipeline_params *p = &app->pipeline_params[i];
684                 uint32_t n_pktq_in = RTE_MIN(p->n_pktq_in, RTE_DIM(p->pktq_in));
685                 uint32_t j;
686
687                 for (j = 0; j < n_pktq_in; j++) {
688                         struct app_pktq_in_params *pktq = &p->pktq_in[j];
689
690                         if ((pktq->type == APP_PKTQ_IN_TM) &&
691                                 (pktq->id == pos))
692                                 n_readers++;
693                 }
694         }
695
696         return n_readers;
697 }
698
699 static inline uint32_t
700 app_source_get_readers(struct app_params *app,
701 struct app_pktq_source_params *source)
702 {
703         uint32_t pos = source - app->source_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_SOURCE) &&
717                                 (pktq->id == pos))
718                                 n_readers++;
719                 }
720         }
721
722         return n_readers;
723 }
724
725 static inline uint32_t
726 app_msgq_get_readers(struct app_params *app, struct app_msgq_params *msgq)
727 {
728         uint32_t pos = msgq - app->msgq_params;
729         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
730                 RTE_DIM(app->pipeline_params));
731         uint32_t n_readers = 0, i;
732
733         for (i = 0; i < n_pipelines; i++) {
734                 struct app_pipeline_params *p = &app->pipeline_params[i];
735                 uint32_t n_msgq_in = RTE_MIN(p->n_msgq_in, RTE_DIM(p->msgq_in));
736                 uint32_t j;
737
738                 for (j = 0; j < n_msgq_in; j++)
739                         if (p->msgq_in[j] == pos)
740                                 n_readers++;
741         }
742
743         return n_readers;
744 }
745
746 static inline uint32_t
747 app_txq_get_writers(struct app_params *app, struct app_pktq_hwq_out_params *txq)
748 {
749         uint32_t pos = txq - app->hwq_out_params;
750         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
751                 RTE_DIM(app->pipeline_params));
752         uint32_t n_writers = 0, i;
753
754         for (i = 0; i < n_pipelines; i++) {
755                 struct app_pipeline_params *p = &app->pipeline_params[i];
756                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
757                         RTE_DIM(p->pktq_out));
758                 uint32_t j;
759
760                 for (j = 0; j < n_pktq_out; j++) {
761                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
762
763                         if ((pktq->type == APP_PKTQ_OUT_HWQ) &&
764                                 (pktq->id == pos))
765                                 n_writers++;
766                 }
767         }
768
769         return n_writers;
770 }
771
772 static inline uint32_t
773 app_swq_get_writers(struct app_params *app, struct app_pktq_swq_params *swq)
774 {
775         uint32_t pos = swq - app->swq_params;
776         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
777                 RTE_DIM(app->pipeline_params));
778         uint32_t n_writers = 0, i;
779
780         for (i = 0; i < n_pipelines; i++) {
781                 struct app_pipeline_params *p = &app->pipeline_params[i];
782                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
783                         RTE_DIM(p->pktq_out));
784                 uint32_t j;
785
786                 for (j = 0; j < n_pktq_out; j++) {
787                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
788
789                         if ((pktq->type == APP_PKTQ_OUT_SWQ) &&
790                                 (pktq->id == pos))
791                                 n_writers++;
792                 }
793         }
794
795         return n_writers;
796 }
797
798 static inline uint32_t
799 app_tm_get_writers(struct app_params *app, struct app_pktq_tm_params *tm)
800 {
801         uint32_t pos = tm - app->tm_params;
802         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
803                 RTE_DIM(app->pipeline_params));
804         uint32_t n_writers = 0, i;
805
806         for (i = 0; i < n_pipelines; i++) {
807                 struct app_pipeline_params *p = &app->pipeline_params[i];
808                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
809                         RTE_DIM(p->pktq_out));
810                 uint32_t j;
811
812                 for (j = 0; j < n_pktq_out; j++) {
813                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
814
815                         if ((pktq->type == APP_PKTQ_OUT_TM) &&
816                                 (pktq->id == pos))
817                                 n_writers++;
818                 }
819         }
820
821         return n_writers;
822 }
823
824 static inline uint32_t
825 app_sink_get_writers(struct app_params *app, struct app_pktq_sink_params *sink)
826 {
827         uint32_t pos = sink - app->sink_params;
828         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
829                 RTE_DIM(app->pipeline_params));
830         uint32_t n_writers = 0, i;
831
832         for (i = 0; i < n_pipelines; i++) {
833                 struct app_pipeline_params *p = &app->pipeline_params[i];
834                 uint32_t n_pktq_out = RTE_MIN(p->n_pktq_out,
835                         RTE_DIM(p->pktq_out));
836                 uint32_t j;
837
838                 for (j = 0; j < n_pktq_out; j++) {
839                         struct app_pktq_out_params *pktq = &p->pktq_out[j];
840
841                         if ((pktq->type == APP_PKTQ_OUT_SINK) &&
842                                 (pktq->id == pos))
843                                 n_writers++;
844                 }
845         }
846
847         return n_writers;
848 }
849
850 static inline uint32_t
851 app_core_is_enabled(struct app_params *app, uint32_t lcore_id) {
852         return(app->core_mask[lcore_id / 64] &
853                 (1LLU << (lcore_id % 64)));
854 }
855
856 static inline void
857 app_core_enable_in_core_mask(struct app_params *app, int lcore_id) {
858         app->core_mask[lcore_id / 64] |= 1LLU << (lcore_id % 64);
859
860 }
861
862 static inline void
863 app_core_build_core_mask_string(struct app_params *app, char
864 *mask_buffer) {
865         int i;
866
867         mask_buffer[0] = '\0';
868         for (i = (int)RTE_DIM(app->core_mask); i > 0; i--) {
869                 /* For Hex representation of bits in uint64_t */
870                 char buffer[(64 / 8) * 2 + 1];
871                 memset(buffer, 0, sizeof(buffer));
872                 snprintf(buffer, sizeof(buffer), "%016" PRIx64,
873                          app->core_mask[i-1]);
874                 strcat(mask_buffer, buffer);
875         }
876 }
877
878 static inline uint32_t
879 app_msgq_get_writers(struct app_params *app, struct app_msgq_params *msgq)
880 {
881         uint32_t pos = msgq - app->msgq_params;
882         uint32_t n_pipelines = RTE_MIN(app->n_pipelines,
883                 RTE_DIM(app->pipeline_params));
884         uint32_t n_writers = 0, i;
885
886         for (i = 0; i < n_pipelines; i++) {
887                 struct app_pipeline_params *p = &app->pipeline_params[i];
888                 uint32_t n_msgq_out = RTE_MIN(p->n_msgq_out,
889                         RTE_DIM(p->msgq_out));
890                 uint32_t j;
891
892                 for (j = 0; j < n_msgq_out; j++)
893                         if (p->msgq_out[j] == pos)
894                                 n_writers++;
895         }
896
897         return n_writers;
898 }
899
900 static inline struct app_link_params *
901 app_get_link_for_rxq(struct app_params *app, struct app_pktq_hwq_in_params *p)
902 {
903         char link_name[APP_PARAM_NAME_SIZE];
904         ssize_t link_param_idx;
905         uint32_t rxq_link_id, rxq_queue_id;
906
907         sscanf(p->name, "RXQ%" SCNu32 ".%" SCNu32,
908                 &rxq_link_id, &rxq_queue_id);
909         sprintf(link_name, "LINK%" PRIu32, rxq_link_id);
910         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
911         APP_CHECK((link_param_idx >= 0),
912                 "Cannot find %s for %s", link_name, p->name);
913
914         return &app->link_params[link_param_idx];
915 }
916
917 static inline struct app_link_params *
918 app_get_link_for_txq(struct app_params *app, struct app_pktq_hwq_out_params *p)
919 {
920         char link_name[APP_PARAM_NAME_SIZE];
921         ssize_t link_param_idx;
922         uint32_t txq_link_id, txq_queue_id;
923
924         sscanf(p->name, "TXQ%" SCNu32 ".%" SCNu32,
925                 &txq_link_id, &txq_queue_id);
926         sprintf(link_name, "LINK%" PRIu32, txq_link_id);
927         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
928         APP_CHECK((link_param_idx >= 0),
929                 "Cannot find %s for %s", link_name, p->name);
930
931         return &app->link_params[link_param_idx];
932 }
933
934 static inline struct app_link_params *
935 app_get_link_for_tm(struct app_params *app, struct app_pktq_tm_params *p_tm)
936 {
937         char link_name[APP_PARAM_NAME_SIZE];
938         uint32_t link_id;
939         ssize_t link_param_idx;
940
941         sscanf(p_tm->name, "TM%" PRIu32, &link_id);
942         sprintf(link_name, "LINK%" PRIu32, link_id);
943         link_param_idx = APP_PARAM_FIND(app->link_params, link_name);
944         APP_CHECK((link_param_idx >= 0),
945                 "Cannot find %s for %s", link_name, p_tm->name);
946
947         return &app->link_params[link_param_idx];
948 }
949
950 int app_config_init(struct app_params *app);
951
952 int app_config_args(struct app_params *app,
953         int argc, char **argv);
954
955 int app_config_preproc(struct app_params *app);
956
957 int app_config_parse(struct app_params *app,
958         const char *file_name);
959
960 int app_config_parse_tm(struct app_params *app);
961
962 void app_config_save(struct app_params *app,
963         const char *file_name);
964
965 int app_config_check(struct app_params *app);
966
967 int app_init(struct app_params *app);
968
969 struct mg_context *rest_api_init(struct app_params *app);
970 void set_vnf_type(const char *type);
971 uint32_t is_rest_support(void);
972 extern uint32_t rest_support;
973
974 int app_thread(void *arg);
975
976 int app_pipeline_type_register(struct app_params *app,
977         struct pipeline_type *ptype);
978
979 struct pipeline_type *app_pipeline_type_find(struct app_params *app,
980         char *name);
981
982 void app_link_up_internal(struct app_params *app,
983         struct app_link_params *cp);
984
985 void app_link_down_internal(struct app_params *app,
986         struct app_link_params *cp);
987
988 #endif