Imported Upstream version 16.07-rc1
[deb_dpdk.git] / examples / ip_pipeline / pipeline / pipeline_common_fe.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_PIPELINE_COMMON_FE_H__
35 #define __INCLUDE_PIPELINE_COMMON_FE_H__
36
37 #include <rte_common.h>
38 #include <rte_cycles.h>
39 #include <rte_malloc.h>
40 #include <cmdline_parse.h>
41
42 #include "pipeline_common_be.h"
43 #include "pipeline.h"
44 #include "app.h"
45
46 #ifndef MSG_TIMEOUT_DEFAULT
47 #define MSG_TIMEOUT_DEFAULT                      1000
48 #endif
49
50 static inline struct app_pipeline_data *
51 app_pipeline_data(struct app_params *app, uint32_t id)
52 {
53         struct app_pipeline_params *params;
54
55         APP_PARAM_FIND_BY_ID(app->pipeline_params, "PIPELINE", id, params);
56         if (params == NULL)
57                 return NULL;
58
59         return &app->pipeline_data[params - app->pipeline_params];
60 }
61
62 static inline void *
63 app_pipeline_data_fe(struct app_params *app, uint32_t id, struct pipeline_type *ptype)
64 {
65         struct app_pipeline_data *pipeline_data;
66
67         pipeline_data = app_pipeline_data(app, id);
68         if (pipeline_data == NULL)
69                 return NULL;
70
71         if (strcmp(pipeline_data->ptype->name, ptype->name) != 0)
72                 return NULL;
73
74         if (pipeline_data->enabled == 0)
75                 return NULL;
76
77         return pipeline_data->fe;
78 }
79
80 static inline struct rte_ring *
81 app_pipeline_msgq_in_get(struct app_params *app,
82         uint32_t pipeline_id)
83 {
84         struct app_msgq_params *p;
85
86         APP_PARAM_FIND_BY_ID(app->msgq_params,
87                 "MSGQ-REQ-PIPELINE",
88                 pipeline_id,
89                 p);
90         if (p == NULL)
91                 return NULL;
92
93         return app->msgq[p - app->msgq_params];
94 }
95
96 static inline struct rte_ring *
97 app_pipeline_msgq_out_get(struct app_params *app,
98         uint32_t pipeline_id)
99 {
100         struct app_msgq_params *p;
101
102         APP_PARAM_FIND_BY_ID(app->msgq_params,
103                 "MSGQ-RSP-PIPELINE",
104                 pipeline_id,
105                 p);
106         if (p == NULL)
107                 return NULL;
108
109         return app->msgq[p - app->msgq_params];
110 }
111
112 static inline void *
113 app_msg_alloc(__rte_unused struct app_params *app)
114 {
115         return rte_malloc(NULL, 2048, RTE_CACHE_LINE_SIZE);
116 }
117
118 static inline void
119 app_msg_free(__rte_unused struct app_params *app,
120         void *msg)
121 {
122         rte_free(msg);
123 }
124
125 static inline void
126 app_msg_send(struct app_params *app,
127         uint32_t pipeline_id,
128         void *msg)
129 {
130         struct rte_ring *r = app_pipeline_msgq_in_get(app, pipeline_id);
131         int status;
132
133         do {
134                 status = rte_ring_sp_enqueue(r, msg);
135         } while (status == -ENOBUFS);
136 }
137
138 static inline void *
139 app_msg_recv(struct app_params *app,
140         uint32_t pipeline_id)
141 {
142         struct rte_ring *r = app_pipeline_msgq_out_get(app, pipeline_id);
143         void *msg;
144         int status = rte_ring_sc_dequeue(r, &msg);
145
146         if (status != 0)
147                 return NULL;
148
149         return msg;
150 }
151
152 static inline void *
153 app_msg_send_recv(struct app_params *app,
154         uint32_t pipeline_id,
155         void *msg,
156         uint32_t timeout_ms)
157 {
158         struct rte_ring *r_req = app_pipeline_msgq_in_get(app, pipeline_id);
159         struct rte_ring *r_rsp = app_pipeline_msgq_out_get(app, pipeline_id);
160         uint64_t hz = rte_get_tsc_hz();
161         void *msg_recv;
162         uint64_t deadline;
163         int status;
164
165         /* send */
166         do {
167                 status = rte_ring_sp_enqueue(r_req, (void *) msg);
168         } while (status == -ENOBUFS);
169
170         /* recv */
171         deadline = (timeout_ms) ?
172                 (rte_rdtsc() + ((hz * timeout_ms) / 1000)) :
173                 UINT64_MAX;
174
175         do {
176                 if (rte_rdtsc() > deadline)
177                         return NULL;
178
179                 status = rte_ring_sc_dequeue(r_rsp, &msg_recv);
180         } while (status != 0);
181
182         return msg_recv;
183 }
184
185 struct app_link_params *
186 app_pipeline_track_pktq_out_to_link(struct app_params *app,
187         uint32_t pipeline_id,
188         uint32_t pktq_out_id);
189
190 int
191 app_pipeline_track_default(struct pipeline_params *params,
192         uint32_t port_in,
193         uint32_t *port_out);
194
195 int
196 app_pipeline_ping(struct app_params *app,
197         uint32_t pipeline_id);
198
199 int
200 app_pipeline_stats_port_in(struct app_params *app,
201         uint32_t pipeline_id,
202         uint32_t port_id,
203         struct rte_pipeline_port_in_stats *stats);
204
205 int
206 app_pipeline_stats_port_out(struct app_params *app,
207         uint32_t pipeline_id,
208         uint32_t port_id,
209         struct rte_pipeline_port_out_stats *stats);
210
211 int
212 app_pipeline_stats_table(struct app_params *app,
213         uint32_t pipeline_id,
214         uint32_t table_id,
215         struct rte_pipeline_table_stats *stats);
216
217 int
218 app_pipeline_port_in_enable(struct app_params *app,
219         uint32_t pipeline_id,
220         uint32_t port_id);
221
222 int
223 app_pipeline_port_in_disable(struct app_params *app,
224         uint32_t pipeline_id,
225         uint32_t port_id);
226
227 int
228 app_link_set_op(struct app_params *app,
229         uint32_t link_id,
230         uint32_t pipeline_id,
231         app_link_op op,
232         void *arg);
233
234 int
235 app_link_config(struct app_params *app,
236         uint32_t link_id,
237         uint32_t ip,
238         uint32_t depth);
239
240 int
241 app_link_up(struct app_params *app,
242         uint32_t link_id);
243
244 int
245 app_link_down(struct app_params *app,
246         uint32_t link_id);
247
248 int
249 app_pipeline_common_cmd_push(struct app_params *app);
250
251 #define CMD_MSG_OUT_OF_MEMORY   "Not enough memory\n"
252 #define CMD_MSG_NOT_ENOUGH_ARGS "Not enough arguments for command \"%s\"\n"
253 #define CMD_MSG_TOO_MANY_ARGS   "Too many arguments for command \"%s\"\n"
254 #define CMD_MSG_MISMATCH_ARGS   "Incorrect set of arguments for command \"%s\"\n"
255 #define CMD_MSG_INVALID_ARG     "Invalid value for argument \"%s\"\n"
256 #define CMD_MSG_ARG_NOT_FOUND   "Syntax error: \"%s\" not found\n"
257 #define CMD_MSG_FILE_ERR        "Error in file \"%s\" at line %u\n"
258 #define CMD_MSG_FAIL            "Command \"%s\" failed\n"
259
260 #endif