Imported Upstream version 16.04
[deb_dpdk.git] / examples / ip_pipeline / pipeline_be.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_PIPELINE_BE_H__
35 #define __INCLUDE_PIPELINE_BE_H__
36
37 #include <rte_port_ethdev.h>
38 #include <rte_port_ring.h>
39 #include <rte_port_frag.h>
40 #include <rte_port_ras.h>
41 #include <rte_port_sched.h>
42 #include <rte_port_source_sink.h>
43 #include <rte_pipeline.h>
44
45 enum pipeline_port_in_type {
46         PIPELINE_PORT_IN_ETHDEV_READER,
47         PIPELINE_PORT_IN_RING_READER,
48         PIPELINE_PORT_IN_RING_MULTI_READER,
49         PIPELINE_PORT_IN_RING_READER_IPV4_FRAG,
50         PIPELINE_PORT_IN_RING_READER_IPV6_FRAG,
51         PIPELINE_PORT_IN_SCHED_READER,
52         PIPELINE_PORT_IN_SOURCE,
53 };
54
55 struct pipeline_port_in_params {
56         enum pipeline_port_in_type type;
57         union {
58                 struct rte_port_ethdev_reader_params ethdev;
59                 struct rte_port_ring_reader_params ring;
60                 struct rte_port_ring_multi_reader_params ring_multi;
61                 struct rte_port_ring_reader_ipv4_frag_params ring_ipv4_frag;
62                 struct rte_port_ring_reader_ipv6_frag_params ring_ipv6_frag;
63                 struct rte_port_sched_reader_params sched;
64                 struct rte_port_source_params source;
65         } params;
66         uint32_t burst_size;
67 };
68
69 static inline void *
70 pipeline_port_in_params_convert(struct pipeline_port_in_params  *p)
71 {
72         switch (p->type) {
73         case PIPELINE_PORT_IN_ETHDEV_READER:
74                 return (void *) &p->params.ethdev;
75         case PIPELINE_PORT_IN_RING_READER:
76                 return (void *) &p->params.ring;
77         case PIPELINE_PORT_IN_RING_MULTI_READER:
78                 return (void *) &p->params.ring_multi;
79         case PIPELINE_PORT_IN_RING_READER_IPV4_FRAG:
80                 return (void *) &p->params.ring_ipv4_frag;
81         case PIPELINE_PORT_IN_RING_READER_IPV6_FRAG:
82                 return (void *) &p->params.ring_ipv6_frag;
83         case PIPELINE_PORT_IN_SCHED_READER:
84                 return (void *) &p->params.sched;
85         case PIPELINE_PORT_IN_SOURCE:
86                 return (void *) &p->params.source;
87         default:
88                 return NULL;
89         }
90 }
91
92 static inline struct rte_port_in_ops *
93 pipeline_port_in_params_get_ops(struct pipeline_port_in_params  *p)
94 {
95         switch (p->type) {
96         case PIPELINE_PORT_IN_ETHDEV_READER:
97                 return &rte_port_ethdev_reader_ops;
98         case PIPELINE_PORT_IN_RING_READER:
99                 return &rte_port_ring_reader_ops;
100         case PIPELINE_PORT_IN_RING_MULTI_READER:
101                 return &rte_port_ring_multi_reader_ops;
102         case PIPELINE_PORT_IN_RING_READER_IPV4_FRAG:
103                 return &rte_port_ring_reader_ipv4_frag_ops;
104         case PIPELINE_PORT_IN_RING_READER_IPV6_FRAG:
105                 return &rte_port_ring_reader_ipv6_frag_ops;
106         case PIPELINE_PORT_IN_SCHED_READER:
107                 return &rte_port_sched_reader_ops;
108         case PIPELINE_PORT_IN_SOURCE:
109                 return &rte_port_source_ops;
110         default:
111                 return NULL;
112         }
113 }
114
115 enum pipeline_port_out_type {
116         PIPELINE_PORT_OUT_ETHDEV_WRITER,
117         PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP,
118         PIPELINE_PORT_OUT_RING_WRITER,
119         PIPELINE_PORT_OUT_RING_MULTI_WRITER,
120         PIPELINE_PORT_OUT_RING_WRITER_NODROP,
121         PIPELINE_PORT_OUT_RING_MULTI_WRITER_NODROP,
122         PIPELINE_PORT_OUT_RING_WRITER_IPV4_RAS,
123         PIPELINE_PORT_OUT_RING_WRITER_IPV6_RAS,
124         PIPELINE_PORT_OUT_SCHED_WRITER,
125         PIPELINE_PORT_OUT_SINK,
126 };
127
128 struct pipeline_port_out_params {
129         enum pipeline_port_out_type type;
130         union {
131                 struct rte_port_ethdev_writer_params ethdev;
132                 struct rte_port_ethdev_writer_nodrop_params ethdev_nodrop;
133                 struct rte_port_ring_writer_params ring;
134                 struct rte_port_ring_multi_writer_params ring_multi;
135                 struct rte_port_ring_writer_nodrop_params ring_nodrop;
136                 struct rte_port_ring_multi_writer_nodrop_params ring_multi_nodrop;
137                 struct rte_port_ring_writer_ipv4_ras_params ring_ipv4_ras;
138                 struct rte_port_ring_writer_ipv6_ras_params ring_ipv6_ras;
139                 struct rte_port_sched_writer_params sched;
140                 struct rte_port_sink_params sink;
141         } params;
142 };
143
144 static inline void *
145 pipeline_port_out_params_convert(struct pipeline_port_out_params  *p)
146 {
147         switch (p->type) {
148         case PIPELINE_PORT_OUT_ETHDEV_WRITER:
149                 return (void *) &p->params.ethdev;
150         case PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP:
151                 return (void *) &p->params.ethdev_nodrop;
152         case PIPELINE_PORT_OUT_RING_WRITER:
153                 return (void *) &p->params.ring;
154         case PIPELINE_PORT_OUT_RING_MULTI_WRITER:
155                 return (void *) &p->params.ring_multi;
156         case PIPELINE_PORT_OUT_RING_WRITER_NODROP:
157                 return (void *) &p->params.ring_nodrop;
158         case PIPELINE_PORT_OUT_RING_MULTI_WRITER_NODROP:
159                 return (void *) &p->params.ring_multi_nodrop;
160         case PIPELINE_PORT_OUT_RING_WRITER_IPV4_RAS:
161                 return (void *) &p->params.ring_ipv4_ras;
162         case PIPELINE_PORT_OUT_RING_WRITER_IPV6_RAS:
163                 return (void *) &p->params.ring_ipv6_ras;
164         case PIPELINE_PORT_OUT_SCHED_WRITER:
165                 return (void *) &p->params.sched;
166         case PIPELINE_PORT_OUT_SINK:
167                 return (void *) &p->params.sink;
168         default:
169                 return NULL;
170         }
171 }
172
173 static inline void *
174 pipeline_port_out_params_get_ops(struct pipeline_port_out_params  *p)
175 {
176         switch (p->type) {
177         case PIPELINE_PORT_OUT_ETHDEV_WRITER:
178                 return &rte_port_ethdev_writer_ops;
179         case PIPELINE_PORT_OUT_ETHDEV_WRITER_NODROP:
180                 return &rte_port_ethdev_writer_nodrop_ops;
181         case PIPELINE_PORT_OUT_RING_WRITER:
182                 return &rte_port_ring_writer_ops;
183         case PIPELINE_PORT_OUT_RING_MULTI_WRITER:
184                 return &rte_port_ring_multi_writer_ops;
185         case PIPELINE_PORT_OUT_RING_WRITER_NODROP:
186                 return &rte_port_ring_writer_nodrop_ops;
187         case PIPELINE_PORT_OUT_RING_MULTI_WRITER_NODROP:
188                 return &rte_port_ring_multi_writer_nodrop_ops;
189         case PIPELINE_PORT_OUT_RING_WRITER_IPV4_RAS:
190                 return &rte_port_ring_writer_ipv4_ras_ops;
191         case PIPELINE_PORT_OUT_RING_WRITER_IPV6_RAS:
192                 return &rte_port_ring_writer_ipv6_ras_ops;
193         case PIPELINE_PORT_OUT_SCHED_WRITER:
194                 return &rte_port_sched_writer_ops;
195         case PIPELINE_PORT_OUT_SINK:
196                 return &rte_port_sink_ops;
197         default:
198                 return NULL;
199         }
200 }
201
202 #ifndef PIPELINE_NAME_SIZE
203 #define PIPELINE_NAME_SIZE                       32
204 #endif
205
206 #ifndef PIPELINE_MAX_PORT_IN
207 #define PIPELINE_MAX_PORT_IN                     16
208 #endif
209
210 #ifndef PIPELINE_MAX_PORT_OUT
211 #define PIPELINE_MAX_PORT_OUT                    16
212 #endif
213
214 #ifndef PIPELINE_MAX_TABLES
215 #define PIPELINE_MAX_TABLES                      16
216 #endif
217
218 #ifndef PIPELINE_MAX_MSGQ_IN
219 #define PIPELINE_MAX_MSGQ_IN                     16
220 #endif
221
222 #ifndef PIPELINE_MAX_MSGQ_OUT
223 #define PIPELINE_MAX_MSGQ_OUT                    16
224 #endif
225
226 #ifndef PIPELINE_MAX_ARGS
227 #define PIPELINE_MAX_ARGS                        32
228 #endif
229
230 struct pipeline_params {
231         char name[PIPELINE_NAME_SIZE];
232
233         struct pipeline_port_in_params port_in[PIPELINE_MAX_PORT_IN];
234         struct pipeline_port_out_params port_out[PIPELINE_MAX_PORT_OUT];
235         struct rte_ring *msgq_in[PIPELINE_MAX_MSGQ_IN];
236         struct rte_ring *msgq_out[PIPELINE_MAX_MSGQ_OUT];
237
238         uint32_t n_ports_in;
239         uint32_t n_ports_out;
240         uint32_t n_msgq;
241
242         int socket_id;
243
244         char *args_name[PIPELINE_MAX_ARGS];
245         char *args_value[PIPELINE_MAX_ARGS];
246         uint32_t n_args;
247
248         uint32_t log_level;
249 };
250
251 /*
252  * Pipeline type back-end operations
253  */
254
255 typedef void* (*pipeline_be_op_init)(struct pipeline_params *params,
256         void *arg);
257
258 typedef int (*pipeline_be_op_free)(void *pipeline);
259
260 typedef int (*pipeline_be_op_run)(void *pipeline);
261
262 typedef int (*pipeline_be_op_timer)(void *pipeline);
263
264 typedef int (*pipeline_be_op_track)(void *pipeline,
265         uint32_t port_in,
266         uint32_t *port_out);
267
268 struct pipeline_be_ops {
269         pipeline_be_op_init f_init;
270         pipeline_be_op_free f_free;
271         pipeline_be_op_run f_run;
272         pipeline_be_op_timer f_timer;
273         pipeline_be_op_track f_track;
274 };
275
276 /* Pipeline specific config parse error messages */
277 #define PIPELINE_ARG_CHECK(exp, fmt, ...)                               \
278 do {                                                                    \
279         if (!(exp)) {                                                   \
280                 fprintf(stderr, fmt "\n", ## __VA_ARGS__);              \
281                 return -1;                                              \
282         }                                                               \
283 } while (0)
284
285 #define PIPELINE_PARSE_ERR_INV_VAL(exp, section, entry, val)            \
286 PIPELINE_ARG_CHECK(exp, "Parse error in section \"%s\": entry \"%s\" "  \
287         "has invalid value (\"%s\")", section, entry, val)
288
289 #define PIPELINE_PARSE_ERR_OUT_RNG(exp, section, entry, val)            \
290 PIPELINE_ARG_CHECK(exp, "Parse error in section \"%s\": entry \"%s\" "  \
291         "value is out of range (\"%s\")", section, entry, val)
292
293 #define PIPELINE_PARSE_ERR_DUPLICATE(exp, section, entry)               \
294 PIPELINE_ARG_CHECK(exp, "Parse error in section \"%s\": duplicated "    \
295         "entry \"%s\"", section, entry)
296
297 #define PIPELINE_PARSE_ERR_INV_ENT(exp, section, entry)                 \
298 PIPELINE_ARG_CHECK(exp, "Parse error in section \"%s\": invalid entry " \
299         "\"%s\"", section, entry)
300
301 #define PIPELINE_PARSE_ERR_MANDATORY(exp, section, entry)               \
302 PIPELINE_ARG_CHECK(exp, "Parse error in section \"%s\": mandatory "     \
303         "entry \"%s\" is missing", section, entry)
304
305 #endif