New upstream version 18.02
[deb_dpdk.git] / examples / ip_pipeline / thread_fe.c
1 #include <rte_common.h>
2 #include <rte_ring.h>
3 #include <rte_malloc.h>
4 #include <cmdline_rdline.h>
5 #include <cmdline_parse.h>
6 #include <cmdline_parse_num.h>
7 #include <cmdline_parse_string.h>
8
9 #include "thread.h"
10 #include "thread_fe.h"
11 #include "pipeline.h"
12 #include "pipeline_common_fe.h"
13 #include "app.h"
14
15 static inline void *
16 thread_msg_send_recv(struct app_params *app,
17         uint32_t socket_id, uint32_t core_id, uint32_t ht_id,
18         void *msg,
19         uint32_t timeout_ms)
20 {
21         struct rte_ring *r_req = app_thread_msgq_in_get(app,
22                 socket_id, core_id, ht_id);
23         struct rte_ring *r_rsp = app_thread_msgq_out_get(app,
24                 socket_id, core_id, ht_id);
25         uint64_t hz = rte_get_tsc_hz();
26         void *msg_recv;
27         uint64_t deadline;
28         int status;
29
30         /* send */
31         do {
32                 status = rte_ring_sp_enqueue(r_req, (void *) msg);
33         } while (status == -ENOBUFS);
34
35         /* recv */
36         deadline = (timeout_ms) ?
37                 (rte_rdtsc() + ((hz * timeout_ms) / 1000)) :
38                 UINT64_MAX;
39
40         do {
41                 if (rte_rdtsc() > deadline)
42                         return NULL;
43
44                 status = rte_ring_sc_dequeue(r_rsp, &msg_recv);
45         } while (status != 0);
46
47         return msg_recv;
48 }
49
50 int
51 app_pipeline_enable(struct app_params *app,
52                 uint32_t socket_id,
53                 uint32_t core_id,
54                 uint32_t hyper_th_id,
55                 uint32_t pipeline_id)
56 {
57         struct thread_pipeline_enable_msg_req *req;
58         struct thread_pipeline_enable_msg_rsp *rsp;
59         int thread_id;
60         struct app_pipeline_data *p;
61         struct app_pipeline_params *p_params;
62         struct pipeline_type *p_type;
63         int status;
64
65         if (app == NULL)
66                 return -1;
67
68         thread_id = cpu_core_map_get_lcore_id(app->core_map,
69                         socket_id,
70                         core_id,
71                         hyper_th_id);
72
73         if ((thread_id < 0) || !app_core_is_enabled(app, thread_id))
74                 return -1;
75
76         if (app_pipeline_data(app, pipeline_id) == NULL)
77                 return -1;
78
79         p = &app->pipeline_data[pipeline_id];
80         p_params = &app->pipeline_params[pipeline_id];
81         p_type = app_pipeline_type_find(app, p_params->type);
82
83         if (p_type == NULL)
84                 return -1;
85
86         if (p->enabled == 1)
87                 return -1;
88
89         req = app_msg_alloc(app);
90         if (req == NULL)
91                 return -1;
92
93         req->type = THREAD_MSG_REQ_PIPELINE_ENABLE;
94         req->pipeline_id = pipeline_id;
95         req->be = p->be;
96         req->f_run = p_type->be_ops->f_run;
97         req->f_timer = p_type->be_ops->f_timer;
98         req->timer_period = p->timer_period;
99
100         rsp = thread_msg_send_recv(app,
101                 socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);
102         if (rsp == NULL)
103                 return -1;
104
105         status = rsp->status;
106         app_msg_free(app, rsp);
107
108         if (status != 0)
109                 return -1;
110
111         p->enabled = 1;
112         return 0;
113 }
114
115 int
116 app_pipeline_disable(struct app_params *app,
117                 uint32_t socket_id,
118                 uint32_t core_id,
119                 uint32_t hyper_th_id,
120                 uint32_t pipeline_id)
121 {
122         struct thread_pipeline_disable_msg_req *req;
123         struct thread_pipeline_disable_msg_rsp *rsp;
124         int thread_id;
125         struct app_pipeline_data *p;
126         int status;
127
128         if (app == NULL)
129                 return -1;
130
131         thread_id = cpu_core_map_get_lcore_id(app->core_map,
132                         socket_id,
133                         core_id,
134                         hyper_th_id);
135
136         if ((thread_id < 0) || !app_core_is_enabled(app, thread_id))
137                 return -1;
138
139         if (app_pipeline_data(app, pipeline_id) == NULL)
140                 return -1;
141
142         p = &app->pipeline_data[pipeline_id];
143
144         if (p->enabled == 0)
145                 return -1;
146
147         req = app_msg_alloc(app);
148         if (req == NULL)
149                 return -1;
150
151         req->type = THREAD_MSG_REQ_PIPELINE_DISABLE;
152         req->pipeline_id = pipeline_id;
153
154         rsp = thread_msg_send_recv(app,
155                 socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);
156
157         if (rsp == NULL)
158                 return -1;
159
160         status = rsp->status;
161         app_msg_free(app, rsp);
162
163         if (status != 0)
164                 return -1;
165
166         p->enabled = 0;
167         return 0;
168 }
169
170 int
171 app_thread_headroom(struct app_params *app,
172                 uint32_t socket_id,
173                 uint32_t core_id,
174                 uint32_t hyper_th_id)
175 {
176         struct thread_headroom_read_msg_req *req;
177         struct thread_headroom_read_msg_rsp *rsp;
178         int thread_id;
179         int status;
180
181         if (app == NULL)
182                 return -1;
183
184         thread_id = cpu_core_map_get_lcore_id(app->core_map,
185                         socket_id,
186                         core_id,
187                         hyper_th_id);
188
189         if ((thread_id < 0) || !app_core_is_enabled(app, thread_id))
190                 return -1;
191
192         req = app_msg_alloc(app);
193         if (req == NULL)
194                 return -1;
195
196         req->type = THREAD_MSG_REQ_HEADROOM_READ;
197
198         rsp = thread_msg_send_recv(app,
199                 socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);
200
201         if (rsp == NULL)
202                 return -1;
203
204         status = rsp->status;
205
206         if (status != 0)
207                 return -1;
208
209         printf("%.3f%%\n", rsp->headroom_ratio * 100);
210
211
212         app_msg_free(app, rsp);
213
214         return 0;
215 }
216
217 /*
218  * pipeline enable
219  */
220
221 struct cmd_pipeline_enable_result {
222         cmdline_fixed_string_t t_string;
223         cmdline_fixed_string_t t_id_string;
224         cmdline_fixed_string_t pipeline_string;
225         uint32_t pipeline_id;
226         cmdline_fixed_string_t enable_string;
227 };
228
229 static void
230 cmd_pipeline_enable_parsed(
231         void *parsed_result,
232         __rte_unused struct cmdline *cl,
233          void *data)
234 {
235         struct cmd_pipeline_enable_result *params = parsed_result;
236         struct app_params *app = data;
237         int status;
238         uint32_t core_id, socket_id, hyper_th_id;
239
240         if (parse_pipeline_core(&socket_id,
241                         &core_id,
242                         &hyper_th_id,
243                         params->t_id_string) != 0) {
244                 printf("Command failed\n");
245                 return;
246         }
247
248         status = app_pipeline_enable(app,
249                         socket_id,
250                         core_id,
251                         hyper_th_id,
252                         params->pipeline_id);
253
254         if (status != 0)
255                 printf("Command failed\n");
256 }
257
258 static cmdline_parse_token_string_t cmd_pipeline_enable_t_string =
259         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, t_string, "t");
260
261 static cmdline_parse_token_string_t cmd_pipeline_enable_t_id_string =
262         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, t_id_string,
263                 NULL);
264
265 static cmdline_parse_token_string_t cmd_pipeline_enable_pipeline_string =
266         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, pipeline_string,
267                 "pipeline");
268
269 static cmdline_parse_token_num_t cmd_pipeline_enable_pipeline_id =
270         TOKEN_NUM_INITIALIZER(struct cmd_pipeline_enable_result, pipeline_id,
271                 UINT32);
272
273 static cmdline_parse_token_string_t cmd_pipeline_enable_enable_string =
274         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_enable_result, enable_string,
275                 "enable");
276
277 static cmdline_parse_inst_t cmd_pipeline_enable = {
278         .f = cmd_pipeline_enable_parsed,
279         .data = NULL,
280         .help_str = "Enable pipeline on specified core",
281         .tokens = {
282                 (void *)&cmd_pipeline_enable_t_string,
283                 (void *)&cmd_pipeline_enable_t_id_string,
284                 (void *)&cmd_pipeline_enable_pipeline_string,
285                 (void *)&cmd_pipeline_enable_pipeline_id,
286                 (void *)&cmd_pipeline_enable_enable_string,
287                 NULL,
288         },
289 };
290
291 /*
292  * pipeline disable
293  */
294
295 struct cmd_pipeline_disable_result {
296         cmdline_fixed_string_t t_string;
297         cmdline_fixed_string_t t_id_string;
298         cmdline_fixed_string_t pipeline_string;
299         uint32_t pipeline_id;
300         cmdline_fixed_string_t disable_string;
301 };
302
303 static void
304 cmd_pipeline_disable_parsed(
305         void *parsed_result,
306         __rte_unused struct cmdline *cl,
307          void *data)
308 {
309         struct cmd_pipeline_disable_result *params = parsed_result;
310         struct app_params *app = data;
311         int status;
312         uint32_t core_id, socket_id, hyper_th_id;
313
314         if (parse_pipeline_core(&socket_id,
315                         &core_id,
316                         &hyper_th_id,
317                         params->t_id_string) != 0) {
318                 printf("Command failed\n");
319                 return;
320         }
321
322         status = app_pipeline_disable(app,
323                         socket_id,
324                         core_id,
325                         hyper_th_id,
326                         params->pipeline_id);
327
328         if (status != 0)
329                 printf("Command failed\n");
330 }
331
332 static cmdline_parse_token_string_t cmd_pipeline_disable_t_string =
333         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result, t_string, "t");
334
335 static cmdline_parse_token_string_t cmd_pipeline_disable_t_id_string =
336         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result, t_id_string,
337                 NULL);
338
339 static cmdline_parse_token_string_t cmd_pipeline_disable_pipeline_string =
340         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result,
341                 pipeline_string, "pipeline");
342
343 static cmdline_parse_token_num_t cmd_pipeline_disable_pipeline_id =
344         TOKEN_NUM_INITIALIZER(struct cmd_pipeline_disable_result, pipeline_id,
345                 UINT32);
346
347 static cmdline_parse_token_string_t cmd_pipeline_disable_disable_string =
348         TOKEN_STRING_INITIALIZER(struct cmd_pipeline_disable_result, disable_string,
349                 "disable");
350
351 static cmdline_parse_inst_t cmd_pipeline_disable = {
352         .f = cmd_pipeline_disable_parsed,
353         .data = NULL,
354         .help_str = "Disable pipeline on specified core",
355         .tokens = {
356                 (void *)&cmd_pipeline_disable_t_string,
357                 (void *)&cmd_pipeline_disable_t_id_string,
358                 (void *)&cmd_pipeline_disable_pipeline_string,
359                 (void *)&cmd_pipeline_disable_pipeline_id,
360                 (void *)&cmd_pipeline_disable_disable_string,
361                 NULL,
362         },
363 };
364
365
366 /*
367  * thread headroom
368  */
369
370 struct cmd_thread_headroom_result {
371         cmdline_fixed_string_t t_string;
372         cmdline_fixed_string_t t_id_string;
373         cmdline_fixed_string_t headroom_string;
374 };
375
376 static void
377 cmd_thread_headroom_parsed(
378         void *parsed_result,
379         __rte_unused struct cmdline *cl,
380          void *data)
381 {
382         struct cmd_thread_headroom_result *params = parsed_result;
383         struct app_params *app = data;
384         int status;
385         uint32_t core_id, socket_id, hyper_th_id;
386
387         if (parse_pipeline_core(&socket_id,
388                         &core_id,
389                         &hyper_th_id,
390                         params->t_id_string) != 0) {
391                 printf("Command failed\n");
392                 return;
393         }
394
395         status = app_thread_headroom(app,
396                         socket_id,
397                         core_id,
398                         hyper_th_id);
399
400         if (status != 0)
401                 printf("Command failed\n");
402 }
403
404 static cmdline_parse_token_string_t cmd_thread_headroom_t_string =
405         TOKEN_STRING_INITIALIZER(struct cmd_thread_headroom_result,
406         t_string, "t");
407
408 static cmdline_parse_token_string_t cmd_thread_headroom_t_id_string =
409         TOKEN_STRING_INITIALIZER(struct cmd_thread_headroom_result,
410         t_id_string, NULL);
411
412 static cmdline_parse_token_string_t cmd_thread_headroom_headroom_string =
413         TOKEN_STRING_INITIALIZER(struct cmd_thread_headroom_result,
414                 headroom_string, "headroom");
415
416 static cmdline_parse_inst_t cmd_thread_headroom = {
417         .f = cmd_thread_headroom_parsed,
418         .data = NULL,
419         .help_str = "Display thread headroom",
420         .tokens = {
421                 (void *)&cmd_thread_headroom_t_string,
422                 (void *)&cmd_thread_headroom_t_id_string,
423                 (void *)&cmd_thread_headroom_headroom_string,
424                 NULL,
425         },
426 };
427
428
429 static cmdline_parse_ctx_t thread_cmds[] = {
430         (cmdline_parse_inst_t *) &cmd_pipeline_enable,
431         (cmdline_parse_inst_t *) &cmd_pipeline_disable,
432         (cmdline_parse_inst_t *) &cmd_thread_headroom,
433         NULL,
434 };
435
436 int
437 app_pipeline_thread_cmd_push(struct app_params *app)
438 {
439         uint32_t n_cmds, i;
440
441         /* Check for available slots in the application commands array */
442         n_cmds = RTE_DIM(thread_cmds) - 1;
443         if (n_cmds > APP_MAX_CMDS - app->n_cmds)
444                 return -ENOMEM;
445
446         /* Push thread commands into the application */
447         memcpy(&app->cmds[app->n_cmds], thread_cmds,
448                 n_cmds * sizeof(cmdline_parse_ctx_t));
449
450         for (i = 0; i < n_cmds; i++)
451                 app->cmds[app->n_cmds + i]->data = app;
452
453         app->n_cmds += n_cmds;
454         app->cmds[app->n_cmds] = NULL;
455
456         return 0;
457 }