examples/ip_pipeline: clean up configuration parser
[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) ||
74                 ((app->core_mask & (1LLU << thread_id)) == 0))
75                 return -1;
76
77         if (app_pipeline_data(app, pipeline_id) == NULL)
78                 return -1;
79
80         p = &app->pipeline_data[pipeline_id];
81         p_params = &app->pipeline_params[pipeline_id];
82         p_type = app_pipeline_type_find(app, p_params->type);
83
84         if (p->enabled == 1)
85                 return -1;
86
87         req = app_msg_alloc(app);
88         if (req == NULL)
89                 return -1;
90
91         req->type = THREAD_MSG_REQ_PIPELINE_ENABLE;
92         req->pipeline_id = pipeline_id;
93         req->be = p->be;
94         req->f_run = p_type->be_ops->f_run;
95         req->f_timer = p_type->be_ops->f_timer;
96         req->timer_period = p->timer_period;
97
98         rsp = thread_msg_send_recv(app,
99                 socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);
100         if (rsp == NULL)
101                 return -1;
102
103         status = rsp->status;
104         app_msg_free(app, rsp);
105
106         if (status != 0)
107                 return -1;
108
109         p->enabled = 1;
110         return 0;
111 }
112
113 int
114 app_pipeline_disable(struct app_params *app,
115                 uint32_t socket_id,
116                 uint32_t core_id,
117                 uint32_t hyper_th_id,
118                 uint32_t pipeline_id)
119 {
120         struct thread_pipeline_disable_msg_req *req;
121         struct thread_pipeline_disable_msg_rsp *rsp;
122         int thread_id;
123         struct app_pipeline_data *p;
124         int status;
125
126         if (app == NULL)
127                 return -1;
128
129         thread_id = cpu_core_map_get_lcore_id(app->core_map,
130                         socket_id,
131                         core_id,
132                         hyper_th_id);
133
134         if ((thread_id < 0) ||
135                 ((app->core_mask & (1LLU << thread_id)) == 0))
136                 return -1;
137
138         if (app_pipeline_data(app, pipeline_id) == NULL)
139                 return -1;
140
141         p = &app->pipeline_data[pipeline_id];
142
143         if (p->enabled == 0)
144                 return -1;
145
146         req = app_msg_alloc(app);
147         if (req == NULL)
148                 return -1;
149
150         req->type = THREAD_MSG_REQ_PIPELINE_DISABLE;
151         req->pipeline_id = pipeline_id;
152
153         rsp = thread_msg_send_recv(app,
154                 socket_id, core_id, hyper_th_id, req, MSG_TIMEOUT_DEFAULT);
155
156         if (rsp == NULL)
157                 return -1;
158
159         status = rsp->status;
160         app_msg_free(app, rsp);
161
162         if (status != 0)
163                 return -1;
164
165         p->enabled = 0;
166         return 0;
167 }
168
169 int
170 app_thread_headroom(struct app_params *app,
171                 uint32_t socket_id,
172                 uint32_t core_id,
173                 uint32_t hyper_th_id)
174 {
175         struct thread_headroom_read_msg_req *req;
176         struct thread_headroom_read_msg_rsp *rsp;
177         int thread_id;
178         int status;
179
180         if (app == NULL)
181                 return -1;
182
183         thread_id = cpu_core_map_get_lcore_id(app->core_map,
184                         socket_id,
185                         core_id,
186                         hyper_th_id);
187
188         if ((thread_id < 0) ||
189                 ((app->core_mask & (1LLU << thread_id)) == 0))
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 }