Support packets in flight
[samplevnf.git] / VNFs / DPPD-PROX / cmd_parser.c
1 /*
2 // Copyright (c) 2010-2020 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 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <rte_cycles.h>
21 #include <rte_version.h>
22
23 #include "input.h"
24 #include "cmd_parser.h"
25 #include "commands.h"
26 #include "run.h"
27 #include "display.h"
28 #include "log.h"
29 #include "prox_cfg.h"
30 #include "prox_port_cfg.h"
31 #include "task_base.h"
32 #include "lconf.h"
33 #include "main.h"
34 #include "parse_utils.h"
35 #include "stats_parser.h"
36 #include "stats_port.h"
37 #include "stats_latency.h"
38 #include "stats_global.h"
39 #include "stats_prio_task.h"
40
41 #include "handle_routing.h"
42 #include "handle_qinq_decap4.h"
43 #include "handle_lat.h"
44 #include "handle_arp.h"
45 #include "handle_gen.h"
46 #include "handle_acl.h"
47 #include "handle_irq.h"
48 #include "defines.h"
49 #include "prox_cfg.h"
50 #include "version.h"
51 #include "stats_latency.h"
52 #include "handle_cgnat.h"
53 #include "handle_impair.h"
54 #include "rx_pkt.h"
55 #include "prox_compat.h"
56 #include "igmp.h"
57
58 static int core_task_is_valid(int lcore_id, int task_id)
59 {
60         if (lcore_id >= RTE_MAX_LCORE) {
61                 plog_err("Invalid core id %u (lcore ID above %d)\n", lcore_id, RTE_MAX_LCORE);
62                 return 0;
63         }
64         else if (!prox_core_active(lcore_id, 0)) {
65                 plog_err("Invalid core id %u (lcore is not active)\n", lcore_id);
66                 return 0;
67         }
68         else if (task_id >= lcore_cfg[lcore_id].n_tasks_all) {
69                 plog_err("Invalid task id (valid task IDs for core %u are below %u)\n",
70                          lcore_id, lcore_cfg[lcore_id].n_tasks_all);
71                 return 0;
72         }
73         return 1;
74 }
75
76 static int cores_task_are_valid(unsigned int *lcores, int task_id, unsigned int nb_cores)
77 {
78         unsigned int lcore_id;
79         for (unsigned int i = 0; i < nb_cores; i++) {
80                 lcore_id = lcores[i];
81                 if (core_task_is_valid(lcore_id, task_id) == 0)
82                         return 0;
83         }
84         return 1;
85 }
86
87 static int parse_cores_task(const char *str, uint32_t *lcore_id, uint32_t *task_id, unsigned *nb_cores)
88 {
89         char str_lcore_id[128];
90         int ret;
91
92         if (2 != sscanf(str, "%s %u", str_lcore_id, task_id))
93                 return -1;
94
95         if ((ret = parse_list_set(lcore_id, str_lcore_id, RTE_MAX_LCORE)) <= 0) {
96                 plog_err("Invalid core while parsing command (%s)\n", get_parse_err());
97                 return -1;
98         }
99         *nb_cores = ret;
100
101         return 0;
102 }
103
104 static int parse_cores_tasks(const char *str, uint32_t *lcore_id, uint32_t *task_id, unsigned *nb_cores, unsigned *nb_tasks)
105 {
106         char str_lcore_id[128], str_task_id[128];
107         int ret;
108
109         if (2 != sscanf(str, "%s %s", str_lcore_id, str_task_id))
110                 return -1;
111
112         if ((ret = parse_list_set(lcore_id, str_lcore_id, RTE_MAX_LCORE)) <= 0) {
113                 plog_err("Invalid core while parsing command (%s)\n", get_parse_err());
114                 return -1;
115         }
116         *nb_cores = ret;
117
118         if ((ret = parse_list_set(task_id, str_task_id, MAX_TASKS_PER_CORE)) <= 0) {
119                 plog_err("Invalid task while parsing command (%s)\n", get_parse_err());
120                 return -1;
121         }
122         *nb_tasks = ret;
123
124         return 0;
125 }
126
127 static const char *strchr_skip_twice(const char *str, int chr)
128 {
129         str = strchr(str, chr);
130         if (!str)
131                 return NULL;
132         str = str + 1;
133
134         str = strchr(str, chr);
135         if (!str)
136                 return NULL;
137         return str + 1;
138 }
139
140 static int parse_cmd_quit(const char *str, struct input *input)
141 {
142         if (strcmp(str, "") != 0) {
143                 return -1;
144         }
145
146         quit();
147         return 0;
148 }
149
150 static int parse_cmd_quit_force(const char *str, struct input *input)
151 {
152         if (strcmp(str, "") != 0) {
153                 return -1;
154         }
155
156         abort();
157 }
158
159 static int parse_cmd_history(const char *str, struct input *input)
160 {
161         if (strcmp(str, "") != 0) {
162                 return -1;
163         }
164
165         if (input->history) {
166                 input->history(input);
167                 return 0;
168         }
169         plog_err("Invalid history comand ");
170         return -1;
171 }
172
173 static int parse_cmd_echo(const char *str, struct input *input)
174 {
175         if (strcmp(str, "") == 0) {
176                 return -1;
177         }
178
179         char resolved[2048];
180
181         if (parse_vars(resolved, sizeof(resolved), str)) {
182                 return 0;
183         }
184
185         if (input->reply) {
186                 if (strlen(resolved) + 2 < sizeof(resolved)) {
187                         resolved[strlen(resolved) + 1] = 0;
188                         resolved[strlen(resolved)] = '\n';
189                 }
190                 else
191                         return 0;
192
193                 input->reply(input, resolved, strlen(resolved));
194         } else
195                 plog_info("%s\n", resolved);
196
197         return 0;
198 }
199
200 static int parse_cmd_reset_stats(const char *str, struct input *input)
201 {
202         if (strcmp(str, "") != 0) {
203                 return -1;
204         }
205
206         stats_reset();
207         return 0;
208 }
209
210 static int parse_cmd_reset_lat_stats(const char *str, struct input *input)
211 {
212         if (strcmp(str, "") != 0) {
213                 return -1;
214         }
215
216         stats_latency_reset();
217         return 0;
218 }
219
220 static int parse_cmd_trace(const char *str, struct input *input)
221 {
222         unsigned lcores[RTE_MAX_LCORE], task_id, nb_packets, nb_cores;
223
224         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
225                 return -1;
226         if (!(str = strchr_skip_twice(str, ' ')))
227                 return -1;
228         if (sscanf(str, "%u", &nb_packets) != 1)
229                 return -1;
230
231         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
232                 for (unsigned int i = 0; i < nb_cores; i++) {
233                         cmd_trace(lcores[i], task_id, nb_packets);
234                 }
235         }
236         return 0;
237 }
238
239 static int parse_cmd_dump_rx(const char *str, struct input *input)
240 {
241         unsigned lcores[RTE_MAX_LCORE], task_id, nb_packets, nb_cores;
242
243         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
244                 return -1;
245         if (!(str = strchr_skip_twice(str, ' ')))
246                 return -1;
247         if (sscanf(str, "%u", &nb_packets) != 1) {
248                 return -1;
249         }
250
251         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
252                 for (unsigned int i = 0; i < nb_cores; i++) {
253                         if (lcores[i] > RTE_MAX_LCORE) {
254                                 plog_warn("core_id too high, maximum allowed is: %u\n", RTE_MAX_LCORE);
255                                 return -1;
256                         } else if (task_id >= lcore_cfg[lcores[i]].n_tasks_all) {
257                                 plog_warn("task_id too high, should be in [0, %u]\n", lcore_cfg[lcores[i]].n_tasks_all - 1);
258                                 return -1;
259                         } else {
260                                 struct lcore_cfg *lconf = &lcore_cfg[lcores[i]];
261                                 struct task_base *tbase = lconf->tasks_all[task_id];
262                                 int prev_count = tbase->aux->rx_prev_count;
263                                 if (((prev_count) && (tbase->aux->rx_pkt_prev[prev_count - 1] == rx_pkt_dummy))
264                                         || (tbase->rx_pkt == rx_pkt_dummy)) {
265                                         plog_warn("Unable to dump_rx as rx_pkt_dummy\n");
266                                         return -1;
267                                 }
268                         }
269                         cmd_dump(lcores[i], task_id, nb_packets, input, 1, 0);
270                 }
271         }
272         return 0;
273 }
274
275 static int parse_cmd_pps_unit(const char *str, struct input *input)
276 {
277         uint32_t val;
278
279         if (sscanf(str, "%u", &val) != 1) {
280                 return -1;
281         }
282         display_set_pps_unit(val);
283         return 0;
284 }
285
286 static int parse_cmd_dump_tx(const char *str, struct input *input)
287 {
288         unsigned lcores[RTE_MAX_LCORE], task_id, nb_packets, nb_cores;
289
290         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
291                 return -1;
292         if (!(str = strchr_skip_twice(str, ' ')))
293                 return -1;
294         if (sscanf(str, "%u", &nb_packets) != 1) {
295                 return -1;
296         }
297
298         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
299                 for (unsigned int i = 0; i < nb_cores; i++) {
300                         cmd_dump(lcores[i], task_id, nb_packets, input, 0, 1);
301                 }
302         }
303         return 0;
304 }
305
306 static int parse_cmd_rate(const char *str, struct input *input)
307 {
308         unsigned queue, port, rate;
309
310         if (sscanf(str, "%u %u %u", &queue, &port, &rate) != 3) {
311                 return -1;
312         }
313
314         if (port > PROX_MAX_PORTS) {
315                 plog_err("Max port id allowed is %u (specified %u)\n", PROX_MAX_PORTS, port);
316         }
317         else if (!prox_port_cfg[port].active) {
318                 plog_err("Port %u not active\n", port);
319         }
320         else if (queue >= prox_port_cfg[port].n_txq) {
321                 plog_err("Number of active queues is %u\n",
322                          prox_port_cfg[port].n_txq);
323         }
324         else if (rate > prox_port_cfg[port].link_speed) {
325                 plog_err("Max rate allowed on port %u queue %u is %u Mbps\n",
326                          port, queue, prox_port_cfg[port].link_speed);
327         }
328         else {
329                 if (rate == 0) {
330                         plog_info("Disabling rate limiting on port %u queue %u\n",
331                                   port, queue);
332                 }
333                 else {
334                         plog_info("Setting rate limiting to %u Mbps on port %u queue %u\n",
335                                   rate, port, queue);
336                 }
337                 rte_eth_set_queue_rate_limit(port, queue, rate);
338         }
339         return 0;
340 }
341
342 int task_is_mode_and_submode(uint32_t lcore_id, uint32_t task_id, const char *mode, const char *sub_mode)
343 {
344         struct task_args *targs = &lcore_cfg[lcore_id].targs[task_id];
345
346         return !strcmp(targs->task_init->mode_str, mode) && !strcmp(targs->sub_mode_str, sub_mode);
347 }
348
349 int task_is_mode(uint32_t lcore_id, uint32_t task_id, const char *mode)
350 {
351         struct task_init *t = lcore_cfg[lcore_id].targs[task_id].task_init;
352
353         return !strcmp(t->mode_str, mode);
354 }
355
356 int task_is_sub_mode(uint32_t lcore_id, uint32_t task_id, const char *sub_mode)
357 {
358         struct task_args *targs = &lcore_cfg[lcore_id].targs[task_id];
359
360         return !strcmp(targs->sub_mode_str, sub_mode);
361 }
362
363 static void log_pkt_count(uint32_t count, uint32_t lcore_id, uint32_t task_id)
364 {
365         if (count == UINT32_MAX)
366                 plog_info("Core %u task %u will keep sending packets\n", lcore_id, task_id);
367         else if (count == 0)
368                 plog_info("Core %u task %u waits for next count command\n", lcore_id, task_id);
369         else
370                 plog_info("Core %u task %u stopping after %u packets\n", lcore_id, task_id, count);
371 }
372
373 static int parse_cmd_count(const char *str, struct input *input)
374 {
375         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, count, nb_cores;
376
377         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
378                 return -1;
379         if (!(str = strchr_skip_twice(str, ' ')))
380                 return -1;
381         if (sscanf(str, "%u", &count) != 1)
382                 return -1;
383
384         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
385                 for (unsigned int i = 0; i < nb_cores; i++) {
386                         lcore_id = lcores[i];
387                         if (!task_is_mode(lcore_id, task_id, "gen")) {
388                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
389                         }
390                         else {
391                                 struct task_base *task = lcore_cfg[lcore_id].tasks_all[task_id];
392
393                                 log_pkt_count(count, lcore_id, task_id);
394                                 task_gen_set_pkt_count(task, count);
395                         }
396                 }
397         }
398         return 0;
399 }
400
401 static int parse_cmd_set_proba_no_drop(const char *str, struct input *input)
402 {
403         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
404         float proba_no_drop;
405
406         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
407                 return -1;
408         if (!(str = strchr_skip_twice(str, ' ')))
409                 return -1;
410         if (sscanf(str, "%f", &proba_no_drop) != 1)
411                 return -1;
412
413         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
414                 for (unsigned int i = 0; i < nb_cores; i++) {
415                         lcore_id = lcores[i];
416                         if (!task_is_mode(lcore_id, task_id, "impair")) {
417                                 plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
418                         } else {
419                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
420                                 task_impair_set_proba_no_drop(tbase, proba_no_drop);
421                         }
422                 }
423         }
424         return 0;
425 }
426
427 static int parse_cmd_set_proba_delay(const char *str, struct input *input)
428 {
429         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
430         float proba_delay;
431
432         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
433                 return -1;
434         if (!(str = strchr_skip_twice(str, ' ')))
435                 return -1;
436         if (sscanf(str, "%f", &proba_delay) != 1)
437                 return -1;
438
439         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
440                 for (unsigned int i = 0; i < nb_cores; i++) {
441                         lcore_id = lcores[i];
442                         if (!task_is_mode(lcore_id, task_id, "impair")) {
443                                 plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
444                         } else {
445                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
446                                 task_impair_set_proba_delay(tbase, proba_delay);
447                         }
448                 }
449         }
450         return 0;
451 }
452
453 static int parse_cmd_set_proba_duplicate(const char *str, struct input *input)
454 {
455         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
456         float proba_duplicate;
457
458         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
459                 return -1;
460         if (!(str = strchr_skip_twice(str, ' ')))
461                 return -1;
462         if (sscanf(str, "%f", &proba_duplicate) != 1)
463                 return -1;
464
465         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
466                 for (unsigned int i = 0; i < nb_cores; i++) {
467                         lcore_id = lcores[i];
468                         if (!task_is_mode(lcore_id, task_id, "impair")) {
469                                 plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
470                         } else {
471                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
472                                 task_impair_set_proba_duplicate(tbase, proba_duplicate);
473                         }
474                 }
475         }
476         return 0;
477 }
478
479 static int parse_cmd_delay_us(const char *str, struct input *input)
480 {
481         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, delay_us, nb_cores;
482
483         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
484                 return -1;
485         if (!(str = strchr_skip_twice(str, ' ')))
486                 return -1;
487         if (sscanf(str, "%d", &delay_us) != 1)
488                 return -1;
489
490         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
491                 for (unsigned int i = 0; i < nb_cores; i++) {
492                         lcore_id = lcores[i];
493                         if (!task_is_mode(lcore_id, task_id, "impair")) {
494                                 plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
495                         } else {
496                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
497                                 task_impair_set_delay_us(tbase, delay_us, 0);
498                         }
499                 }
500         }
501         return 0;
502 }
503
504 static int parse_cmd_random_delay_us(const char *str, struct input *input)
505 {
506         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, delay_us, nb_cores;
507
508         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
509                 return -1;
510         if (!(str = strchr_skip_twice(str, ' ')))
511                 return -1;
512         if (sscanf(str, "%d", &delay_us) != 1)
513                 return -1;
514
515         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
516                 for (unsigned int i = 0; i < nb_cores; i++) {
517                         lcore_id = lcores[i];
518                         if (!task_is_mode(lcore_id, task_id, "impair")) {
519                                 plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
520                         } else {
521                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
522                                 task_impair_set_delay_us(tbase, 0, delay_us);
523                         }
524                 }
525         }
526         return 0;
527 }
528
529 static int parse_cmd_bypass(const char *str, struct input *input)
530 {
531         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;
532
533         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
534                 return -1;
535         if ((prox_cfg.flags & DSF_ENABLE_BYPASS) == 0) {
536                 plog_err("enable bypass not set => command not supported\n");
537                 return -1;
538         }
539
540         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
541                 for (unsigned int i = 0; i < nb_cores; i++) {
542                         lcore_id = lcores[i];
543                         if (bypass_task(lcore_id, task_id) != 0)
544                                 return -1;
545                 }
546         }
547         return 0;
548 }
549
550 static int parse_cmd_reconnect(const char *str, struct input *input)
551 {
552         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;
553
554         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
555                 return -1;
556         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
557                 for (unsigned int i = 0; i < nb_cores; i++) {
558                         lcore_id = lcores[i];
559                         if (reconnect_task(lcore_id, task_id) != 0)
560                                 return -1;
561                 }
562         }
563         return 0;
564 }
565
566 static int parse_cmd_pkt_size(const char *str, struct input *input)
567 {
568         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;
569
570         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
571                 return -1;
572         if (!(str = strchr_skip_twice(str, ' ')))
573                 return -1;
574         if (sscanf(str, "%d", &pkt_size) != 1)
575                 return -1;
576
577         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
578                 for (unsigned int i = 0; i < nb_cores; i++) {
579                         lcore_id = lcores[i];
580                         if (!task_is_mode(lcore_id, task_id, "gen")) {
581                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
582                         } else {
583                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
584                                 task_gen_set_pkt_size(tbase, pkt_size); /* error printed within function */
585                         }
586                 }
587         }
588         return 0;
589 }
590
591 static int parse_cmd_imix(const char *str, struct input *input)
592 {
593         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
594         uint32_t pkt_sizes[MAX_IMIX_PKTS], tmp;
595         uint32_t pkt_index = 0;
596
597         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
598                 return -1;
599         if (!(str = strchr_skip_twice(str, ' ')))
600                 return -1;
601         while (pkt_index < MAX_IMIX_PKTS) {
602                 if (sscanf(str, "%d", &pkt_sizes[pkt_index]) != 1)
603                         break;
604                 pkt_index++;
605                 if ((str = strchr(str, ',')) == NULL)
606                         break;
607                 str = str + 1;
608         }
609         if (pkt_index == 0) {
610                 plog_err("No pkt size found\n");
611                 return -1;
612         }
613         if ((pkt_index == MAX_IMIX_PKTS) && (str) && (sscanf(str, "%d", &tmp) == 1)) {
614                 plog_err("Too many inputs - unexpected inputs starting at %s\n", str);
615                 return -1;
616         }
617
618         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
619                 for (unsigned int i = 0; i < nb_cores; i++) {
620                         lcore_id = lcores[i];
621                         if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
622                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
623                         } else {
624                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
625                                 task_gen_set_imix(tbase, pkt_index, pkt_sizes); /* error printed within function */
626                         }
627                 }
628         }
629         return 0;
630 }
631
632 static int parse_cmd_speed(const char *str, struct input *input)
633 {
634         unsigned lcores[RTE_MAX_LCORE], task_id, lcore_id, nb_cores;
635         float speed;
636         unsigned i;
637
638         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
639                 return -1;
640         if (!(str = strchr_skip_twice(str, ' ')))
641                 return -1;
642         if (sscanf(str, "%f", &speed) != 1) {
643                 return -1;
644         }
645
646         if (!cores_task_are_valid(lcores, task_id, nb_cores)) {
647                 return 0;
648         }
649
650         for (i = 0; i < nb_cores; i++) {
651                 lcore_id = lcores[i];
652                         if (!task_is_mode(lcore_id, task_id, "gen")) {
653                         plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
654                 }
655                 else if (speed > 1000.0f || speed < 0.0f) {     // Up to 100 Gbps
656                         plog_err("Speed out of range (must be betweeen 0%% and 1000%%)\n");
657                 }
658                 else {
659                         struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
660                         uint64_t bps = speed * 12500000;
661
662                         plog_info("Setting rate to %"PRIu64" Bps\n", bps);
663
664                         task_gen_set_rate(tbase, bps);
665                 }
666         }
667         return 0;
668 }
669
670 static int parse_cmd_speed_byte(const char *str, struct input *input)
671 {
672         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
673         uint64_t bps;
674
675         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
676                 return -1;
677         if (!(str = strchr_skip_twice(str, ' ')))
678                 return -1;
679         if (sscanf(str, "%"PRIu64"", &bps) != 1)
680                 return -1;
681
682         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
683                 for (unsigned int i = 0; i < nb_cores; i++) {
684                         lcore_id = lcores[i];
685
686                         if (!task_is_mode(lcore_id, task_id, "gen")) {
687                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
688                         }
689                         else if (bps > 12500000000) {   // Up to 100Gbps
690                                 plog_err("Speed out of range (must be <= 12500000000)\n");
691                         }
692                         else {
693                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
694
695                                 plog_info("Setting rate to %"PRIu64" Bps\n", bps);
696                                 task_gen_set_rate(tbase, bps);
697                         }
698                 }
699         }
700         return 0;
701 }
702
703 static int parse_cmd_reset_randoms_all(const char *str, struct input *input)
704 {
705         if (strcmp(str, "") != 0) {
706                 return -1;
707         }
708
709         unsigned task_id, lcore_id = -1;
710         while (prox_core_next(&lcore_id, 0) == 0) {
711                 for (task_id = 0; task_id < lcore_cfg[lcore_id].n_tasks_all; task_id++) {
712                         if (task_is_mode(lcore_id, task_id, "gen")) {
713                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
714                                 uint32_t n_rands = task_gen_get_n_randoms(tbase);
715
716                                 plog_info("Resetting randoms on core %d task %d from %d randoms\n", lcore_id, task_id, n_rands);
717                                 task_gen_reset_randoms(tbase);
718                         }
719                 }
720         }
721         return 0;
722 }
723
724 static int parse_cmd_reset_ranges_all(const char *str, struct input *input)
725 {
726         if (strcmp(str, "") != 0) {
727                 return -1;
728         }
729
730         unsigned task_id, lcore_id = -1;
731         while (prox_core_next(&lcore_id, 0) == 0) {
732                 for (task_id = 0; task_id < lcore_cfg[lcore_id].n_tasks_all; task_id++) {
733                         if (task_is_mode(lcore_id, task_id, "gen")) {
734                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
735                                 uint32_t n_ranges = task_gen_get_n_ranges(tbase);
736
737                                 plog_info("Resetting ranges on core %d task %d from %d ranges\n", lcore_id, task_id, n_ranges);
738                                 task_gen_reset_ranges(tbase);
739                         }
740                 }
741         }
742         return 0;
743 }
744
745 static int parse_cmd_reset_values_all(const char *str, struct input *input)
746 {
747         if (strcmp(str, "") != 0) {
748                 return -1;
749         }
750
751         unsigned task_id, lcore_id = -1;
752         while (prox_core_next(&lcore_id, 0) == 0) {
753                 for (task_id = 0; task_id < lcore_cfg[lcore_id].n_tasks_all; task_id++) {
754                         if (task_is_mode(lcore_id, task_id, "gen")) {
755                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
756
757                                 plog_info("Resetting values on core %d task %d\n", lcore_id, task_id);
758                                 task_gen_reset_values(tbase);
759                         }
760                 }
761         }
762         return 0;
763 }
764
765 static int parse_cmd_reset_values(const char *str, struct input *input)
766 {
767         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
768
769         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
770                 return -1;
771
772         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
773                 for (unsigned int i = 0; i < nb_cores; i++) {
774                         lcore_id = lcores[i];
775                         if (!task_is_mode(lcore_id, task_id, "gen")) {
776                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
777                         }
778                         else {
779                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
780
781                                 plog_info("Resetting values on core %d task %d\n", lcore_id, task_id);
782                                 task_gen_reset_values(tbase);
783                         }
784                 }
785         }
786         return 0;
787 }
788
789 static int parse_cmd_set_value(const char *str, struct input *input)
790 {
791         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, value, nb_cores;
792         unsigned short offset;
793         uint8_t value_len;
794
795         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
796                 return -1;
797         if (!(str = strchr_skip_twice(str, ' ')))
798                 return -1;
799         if (sscanf(str, "%hu %u %hhu", &offset, &value, &value_len) != 3) {
800                 return -1;
801         }
802
803         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
804                 for (unsigned int i = 0; i < nb_cores; i++) {
805                         lcore_id = lcores[i];
806                         if (!task_is_mode(lcore_id, task_id, "gen")) {
807                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
808                         }
809                         // do not check offset here - gen knows better than us the maximum frame size
810                         else if (value_len > 4) {
811                                 plog_err("Length out of range (must be less then 4)\n");
812                         }
813                         else {
814                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
815
816                                 if (task_gen_set_value(tbase, value, offset, value_len))
817                                         plog_info("Unable to set Byte %"PRIu16" to %"PRIu8" - invalid offset/len\n", offset, value);
818                                 else
819                                         plog_info("Setting Byte %"PRIu16" to %"PRIu32"\n", offset, value);
820                         }
821                 }
822         }
823         return 0;
824 }
825
826 static int parse_cmd_set_random(const char *str, struct input *input)
827 {
828         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
829         unsigned short offset;
830         uint8_t value_len;
831         char rand_str[64];
832         int16_t rand_id = -1;
833
834         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
835                 return -1;
836         if (!(str = strchr_skip_twice(str, ' ')))
837                 return -1;
838         if (sscanf(str, "%hu %32s %hhu", &offset, rand_str, &value_len) != 3) {
839                 return -1;
840         }
841
842         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
843                 for (unsigned int i = 0; i < nb_cores; i++) {
844                         lcore_id = lcores[i];
845                         if (!task_is_mode(lcore_id, task_id, "gen")) {
846                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
847                         }
848                         else if (offset > PROX_RTE_ETHER_MAX_LEN) {
849                                 plog_err("Offset out of range (must be less then %u)\n", PROX_RTE_ETHER_MAX_LEN);
850                         }
851                         else if (value_len > 4) {
852                                 plog_err("Length out of range (must be less then 4)\n");
853                         } else {
854                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
855
856                                 if (task_gen_add_rand(tbase, rand_str, offset, rand_id)) {
857                                         plog_warn("Random not added on core %u task %u\n", lcore_id, task_id);
858                                 }
859                         }
860                 }
861         }
862         return 0;
863 }
864
865 static int parse_cmd_set_range(const char *str, struct input *input)
866 {
867         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
868         struct range range;
869
870         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
871                 return -1;
872         if (!(str = strchr_skip_twice(str, ' ')))
873                 return -1;
874         if (sscanf(str, "%u %u %u", &range.offset, &range.min, &range.max) != 3) {
875                 return -1;
876         }
877
878         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
879                 for (unsigned int i = 0; i < nb_cores; i++) {
880                         lcore_id = lcores[i];
881                         if (!task_is_mode(lcore_id, task_id, "gen")) {
882                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
883                         } else if (range.offset > PROX_RTE_ETHER_MAX_LEN) {
884                                 plog_err("Offset out of range (must be less then %u)\n", PROX_RTE_ETHER_MAX_LEN);
885                         } else if (range.min > range.max) {
886                                 plog_err("Wrong range: end (%d) must be >= start (%d)\n", range.max, range.min);
887                         } else {
888                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
889                                 if (task_gen_add_range(tbase, &range)) {
890                                         plog_warn("Range not added on core %u task %u\n", lcore_id, task_id);
891                                 }
892                         }
893                 }
894         }
895         return 0;
896 }
897
898 static int parse_cmd_thread_info(const char *str, struct input *input)
899 {
900         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
901
902         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
903                 return -1;
904         for (unsigned int i = 0; i < nb_cores; i++) {
905                 cmd_thread_info(lcores[i], task_id);
906         }
907         return 0;
908 }
909
910 static int parse_cmd_verbose(const char *str, struct input *input)
911 {
912         unsigned id;
913
914         if (sscanf(str, "%u", &id) != 1) {
915                 return -1;
916         }
917
918         if (plog_set_lvl(id) != 0) {
919                 plog_err("Cannot set log level to %u\n", id);
920         }
921         return 0;
922 }
923
924 static int parse_cmd_arp_add(const char *str, struct input *input)
925 {
926         struct arp_msg amsg;
927         struct arp_msg *pmsg = &amsg;
928         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
929         struct rte_ring *ring;
930
931         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
932                 return -1;
933         if (!(str = strchr_skip_twice(str, ' ')))
934                 return -1;
935         if (strcmp(str, ""))
936                 return -1;
937         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
938                 if (str_to_arp_msg(&amsg, str) == 0) {
939                         for (unsigned int i = 0; i < nb_cores; i++) {
940                                 lcore_id = lcores[i];
941                                 ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
942                                 if (!ring) {
943                                         plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
944                                 }
945                                 else {
946 #if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
947                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1));
948 #else
949                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1, NULL) == 0);
950 #endif
951                                         while (!rte_ring_empty(ring));
952                                 }
953                         }
954                         return 0;
955                 }
956         }
957         return -1;
958 }
959
960 static int parse_cmd_rule_add(const char *str, struct input *input)
961 {
962         struct rte_ring *ring;
963         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
964
965         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
966                 return -1;
967         if (!(str = strchr_skip_twice(str, ' ')))
968                 return -1;
969         if (strcmp(str, ""))
970                 return -1;
971         char *fields[9];
972         char str_cpy[255];
973         prox_strncpy(str_cpy, str, 255);
974         // example add rule command: rule add 15 0 1&0x0fff 1&0x0fff 0&0 128.0.0.0/1 128.0.0.0/1 5000-5000 5000-5000 allow
975         int ret = rte_strsplit(str_cpy, 255, fields, 9, ' ');
976         if (ret != 8) {
977                 return -1;
978         }
979
980         struct acl4_rule rule;
981         struct acl4_rule *prule = &rule;
982         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
983                 if (str_to_rule(&rule, fields, -1, 1) == 0) {
984                         for (unsigned int i = 0; i < nb_cores; i++) {
985                                 lcore_id = lcores[i];
986                                 ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
987                                 if (!ring) {
988                                         plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
989                                 }
990                                 else {
991 #if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
992                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&prule, 1));
993 #else
994                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&prule, 1, NULL) == 0);
995 #endif
996                                         while (!rte_ring_empty(ring));
997                                 }
998                         }
999                         return 0;
1000                 }
1001         }
1002         return -1;
1003 }
1004
1005 static int parse_cmd_gateway_ip(const char *str, struct input *input)
1006 {
1007         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, ip[4], nb_cores, i;
1008
1009         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1010                 return -1;
1011         if (!(str = strchr_skip_twice(str, ' ')))
1012                 return -1;
1013         if (!strcmp(str, ""))
1014                 return -1;
1015         if (sscanf(str, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) {
1016                 return -1;
1017         }
1018         for (i = 0; i < nb_cores; i++) {
1019                 lcore_id = lcores[i];
1020
1021                 if (!task_is_sub_mode(lcore_id, task_id, "l3")) {
1022                         plog_err("Core %u task %u is not in l3 mode\n", lcore_id, task_id);
1023                 }
1024                 else {
1025                         uint32_t gateway_ip = ((ip[3] & 0xFF) << 24) | ((ip[2] & 0xFF) << 16) | ((ip[1] & 0xFF) << 8) | ((ip[0] & 0xFF) << 0);
1026                         struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
1027                         plog_info("Setting gateway ip to %s\n", str);
1028                         task_set_gateway_ip(tbase, gateway_ip);
1029                 }
1030         }
1031         return 0;
1032 }
1033
1034 static int parse_cmd_local_ip(const char *str, struct input *input)
1035 {
1036         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, ip[4], nb_cores, i;
1037
1038         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1039                 return -1;
1040         if (!(str = strchr_skip_twice(str, ' ')))
1041                 return -1;
1042         if (!strcmp(str, ""))
1043                 return -1;
1044         if (sscanf(str, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) {
1045                 return -1;
1046         }
1047         for (i = 0; i < nb_cores; i++) {
1048                 lcore_id = lcores[i];
1049                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
1050                 uint32_t local_ip = ((ip[3] & 0xFF) << 24) | ((ip[2] & 0xFF) << 16) | ((ip[1] & 0xFF) << 8) | ((ip[0] & 0xFF) << 0);
1051                 if (!task_is_mode_and_submode(lcore_id, task_id, "arp", "local")) {
1052                         plog_err("Core %u task %u is not in arp mode\n", lcore_id, task_id);
1053                 } else {
1054                         plog_info("Setting local ip to %s\n", str);
1055                         task_arp_set_local_ip(tbase, local_ip);
1056                 }
1057         }
1058         return 0;
1059 }
1060
1061 static int parse_cmd_route_add(const char *str, struct input *input)
1062 {
1063         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, prefix, next_hop_idx, ip[4], nb_cores;
1064
1065         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1066                 return -1;
1067         if (!(str = strchr_skip_twice(str, ' ')))
1068                 return -1;
1069         if (strcmp(str, ""))
1070                 return -1;
1071         if (sscanf(str, "%u.%u.%u.%u/%u %u", ip, ip + 1, ip + 2, ip + 3,
1072                    &prefix, &next_hop_idx) != 8) {
1073                 return -1;
1074         }
1075         struct rte_ring *ring;
1076
1077         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1078                 for (unsigned int i = 0; i < nb_cores; i++) {
1079                         lcore_id = lcores[i];
1080                         ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
1081                         if (!ring) {
1082                                 plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
1083                         }
1084                         else {
1085                                 struct route_msg rmsg;
1086                                 struct route_msg *pmsg = &rmsg;
1087
1088                                 rmsg.ip_bytes[0] = ip[0];
1089                                 rmsg.ip_bytes[1] = ip[1];
1090                                 rmsg.ip_bytes[2] = ip[2];
1091                                 rmsg.ip_bytes[3] = ip[3];
1092                                 rmsg.prefix = prefix;
1093                                 rmsg.nh = next_hop_idx;
1094 #if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
1095                                 while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1));
1096 #else
1097                                 while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1, NULL) == 0);
1098 #endif
1099                                 while (!rte_ring_empty(ring));
1100                         }
1101                 }
1102         }
1103         return 0;
1104 }
1105
1106 static int parse_cmd_start(const char *str, struct input *input)
1107 {
1108         int task_id = -1;
1109
1110         if (strncmp(str, "all", 3) == 0) {
1111                 str += 3;
1112                 sscanf(str, "%d", &task_id);
1113
1114                 start_core_all(task_id);
1115                 req_refresh();
1116                 return 0;
1117         }
1118
1119         uint32_t cores[64] = {0};
1120         int ret;
1121         ret = parse_list_set(cores, str, 64);
1122         if (ret < 0) {
1123                 return -1;
1124         }
1125         str = strchr(str, ' ');
1126
1127         if (str) {
1128                 sscanf(str, "%d", &task_id);
1129         }
1130         start_cores(cores, ret, task_id);
1131         req_refresh();
1132         return 0;
1133 }
1134
1135 static int parse_cmd_stop(const char *str, struct input *input)
1136 {
1137         int task_id = -1;
1138
1139         if (strncmp(str, "all", 3) == 0) {
1140                 str += 3;
1141                 sscanf(str, "%d", &task_id);
1142                 stop_core_all(task_id);
1143                 req_refresh();
1144                 return 0;
1145         }
1146
1147         uint32_t cores[64] = {0};
1148         int ret;
1149         ret = parse_list_set(cores, str, 64);
1150         if (ret < 0) {
1151                 return -1;
1152         }
1153         str = strchr(str, ' ');
1154
1155         if (str) {
1156                 sscanf(str, "%d", &task_id);
1157         }
1158         stop_cores(cores, ret, task_id);
1159         req_refresh();
1160
1161         return 0;
1162 }
1163
1164 static int parse_cmd_rx_distr_start(const char *str, struct input *input)
1165 {
1166         unsigned lcore_id[RTE_MAX_LCORE];
1167
1168         int nb_cores;
1169
1170         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1171
1172         if (nb_cores <= 0) {
1173                 return -1;
1174         }
1175
1176         for (int i = 0; i < nb_cores; ++i)
1177                 cmd_rx_distr_start(lcore_id[i]);
1178         return 0;
1179 }
1180
1181 static int parse_cmd_tx_distr_start(const char *str, struct input *input)
1182 {
1183         unsigned lcore_id[RTE_MAX_LCORE];
1184
1185         int nb_cores;
1186
1187         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1188
1189         if (nb_cores <= 0) {
1190                 return -1;
1191         }
1192
1193         for (int i = 0; i < nb_cores; ++i)
1194                 cmd_tx_distr_start(lcore_id[i]);
1195         return 0;
1196 }
1197
1198 static int parse_cmd_rx_distr_stop(const char *str, struct input *input)
1199 {
1200         unsigned lcore_id[RTE_MAX_LCORE];
1201
1202         int nb_cores;
1203
1204         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1205
1206         if (nb_cores <= 0) {
1207                 return -1;
1208         }
1209
1210         for (int i = 0; i < nb_cores; ++i)
1211                 cmd_rx_distr_stop(lcore_id[i]);
1212         return 0;
1213 }
1214
1215 static int parse_cmd_tx_distr_stop(const char *str, struct input *input)
1216 {
1217         unsigned lcore_id[RTE_MAX_LCORE];
1218
1219         int nb_cores;
1220
1221         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1222
1223         if (nb_cores <= 0) {
1224                 return -1;
1225         }
1226
1227         for (int i = 0; i < nb_cores; ++i)
1228                 cmd_tx_distr_stop(lcore_id[i]);
1229         return 0;
1230 }
1231
1232 static int parse_cmd_rx_distr_reset(const char *str, struct input *input)
1233 {
1234         unsigned lcore_id[RTE_MAX_LCORE];
1235
1236         int nb_cores;
1237
1238         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1239
1240         if (nb_cores <= 0) {
1241                 return -1;
1242         }
1243
1244         for (int i = 0; i < nb_cores; ++i)
1245                 cmd_rx_distr_rst(lcore_id[i]);
1246         return 0;
1247 }
1248
1249 static int parse_cmd_tx_distr_reset(const char *str, struct input *input)
1250 {
1251         unsigned lcore_id[RTE_MAX_LCORE];
1252
1253         int nb_cores;
1254
1255         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1256
1257         if (nb_cores <= 0) {
1258                 return -1;
1259         }
1260
1261         for (int i = 0; i < nb_cores; ++i)
1262                 cmd_tx_distr_rst(lcore_id[i]);
1263         return 0;
1264 }
1265
1266 static int parse_cmd_rx_distr_show(const char *str, struct input *input)
1267 {
1268         unsigned lcore_id[RTE_MAX_LCORE];
1269
1270         int nb_cores;
1271
1272         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1273
1274         if (nb_cores <= 0) {
1275                 return -1;
1276         }
1277
1278         for (int i = 0; i < nb_cores; ++i)
1279                 cmd_rx_distr_show(lcore_id[i]);
1280         return 0;
1281 }
1282
1283 static int parse_cmd_tx_distr_show(const char *str, struct input *input)
1284 {
1285         unsigned lcore_id[RTE_MAX_LCORE];
1286
1287         int nb_cores;
1288
1289         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1290
1291         if (nb_cores <= 0) {
1292                 return -1;
1293         }
1294
1295         for (int i = 0; i < nb_cores; ++i)
1296                 cmd_tx_distr_show(lcore_id[i]);
1297         return 0;
1298 }
1299
1300 static int parse_cmd_tot_stats(const char *str, struct input *input)
1301 {
1302         if (strcmp("", str) != 0) {
1303                 return -1;
1304         }
1305
1306         struct global_stats_sample *gsl = stats_get_global_stats(1);
1307         uint64_t tot_rx = gsl->host_rx_packets;
1308         uint64_t tot_tx = gsl->host_tx_packets;
1309         uint64_t last_tsc = gsl->tsc;
1310
1311         if (input->reply) {
1312                 char buf[128];
1313                 snprintf(buf, sizeof(buf), "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
1314                          tot_rx, tot_tx, last_tsc, rte_get_tsc_hz());
1315                 input->reply(input, buf, strlen(buf));
1316         }
1317         else {
1318                 plog_info("RX: %"PRIu64", TX: %"PRIu64"\n", tot_rx, tot_tx);
1319         }
1320         return 0;
1321 }
1322
1323 static int parse_cmd_update_interval(const char *str, struct input *input)
1324 {
1325         unsigned val;
1326
1327         if (sscanf(str, "%u", &val) != 1) {
1328                 return -1;
1329         }
1330
1331         if (val == 0) {
1332                 plog_err("Minimum update interval is 1 ms\n");
1333         }
1334         else {
1335                 plog_info("Setting update interval to %d ms\n", val);
1336                 set_update_interval(val);
1337         }
1338         return 0;
1339 }
1340
1341 static int parse_cmd_mem_info(const char *str, struct input *input)
1342 {
1343         if (strcmp("", str) != 0) {
1344                 return -1;
1345         }
1346
1347         cmd_mem_stats();
1348         cmd_mem_layout();
1349         return 0;
1350 }
1351
1352 static int parse_cmd_tot_ierrors_tot(const char *str, struct input *input)
1353 {
1354         if (strcmp(str, "") != 0) {
1355                 return -1;
1356         }
1357
1358         struct global_stats_sample *gsl = stats_get_global_stats(1);
1359         uint64_t tot = gsl->nics_ierrors;
1360         uint64_t last_tsc = gsl->tsc;
1361
1362         if (input->reply) {
1363                 char buf[128];
1364                 snprintf(buf, sizeof(buf),
1365                          "%"PRIu64",%"PRIu64",%"PRIu64"\n",
1366                          tot, last_tsc, rte_get_tsc_hz());
1367                 input->reply(input, buf, strlen(buf));
1368         }
1369         else {
1370                 plog_info("ierrors: %"PRIu64"\n", tot);
1371         }
1372         return 0;
1373 }
1374
1375 static int parse_cmd_tot_imissed_tot(const char *str, struct input *input)
1376 {
1377         if (strcmp(str, "") != 0) {
1378                 return -1;
1379         }
1380
1381         struct global_stats_sample *gsl = stats_get_global_stats(1);
1382         uint64_t tot = gsl->nics_imissed;
1383         uint64_t last_tsc = gsl->tsc;
1384
1385         if (input->reply) {
1386                 char buf[128];
1387                 snprintf(buf, sizeof(buf),
1388                          "%"PRIu64",%"PRIu64",%"PRIu64"\n",
1389                          tot, last_tsc, rte_get_tsc_hz());
1390                 input->reply(input, buf, strlen(buf));
1391         }
1392         else {
1393                 plog_info("imissed: %"PRIu64"\n", tot);
1394         }
1395         return 0;
1396 }
1397
1398 static int parse_cmd_enable_multicast(const char *str, struct input *input)
1399 {
1400         uint8_t port_id;
1401         prox_rte_ether_addr mac;
1402
1403         if (sscanf(str, "%hhu %hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &port_id, mac.addr_bytes, mac.addr_bytes + 1, mac.addr_bytes + 2, mac.addr_bytes + 3, mac.addr_bytes + 4, mac.addr_bytes + 5 ) != 7) {
1404                 return -1;
1405         }
1406         cmd_multicast(port_id, 1, &mac);
1407         return 0;
1408 }
1409
1410 static int parse_cmd_disable_multicast(const char *str, struct input *input)
1411 {
1412         uint8_t port_id;
1413         prox_rte_ether_addr mac;
1414
1415         if (sscanf(str, "%hhu %hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &port_id, mac.addr_bytes, mac.addr_bytes + 1, mac.addr_bytes + 2, mac.addr_bytes + 3, mac.addr_bytes + 4, mac.addr_bytes + 5 ) != 7) {
1416                 return -1;
1417         }
1418
1419         cmd_multicast(port_id, 0, &mac);
1420         return 0;
1421 }
1422
1423 static int parse_cmd_reset_port(const char *str, struct input *input)
1424 {
1425         uint32_t port_id;
1426
1427         if (sscanf(str, "%u", &port_id ) != 1) {
1428                 return -1;
1429         }
1430
1431         cmd_reset_port(port_id);
1432         return 0;
1433 }
1434
1435 static int parse_cmd_write_reg(const char *str, struct input *input)
1436 {
1437         uint32_t port_id;
1438         uint32_t id, val;
1439
1440         if (sscanf(str, "%u %x %u", &port_id, &id, &val) != 3) {
1441                 return -1;
1442         }
1443
1444         cmd_write_reg(port_id, id, val);
1445         return 0;
1446 }
1447
1448 static int parse_cmd_read_reg(const char *str, struct input *input)
1449 {
1450         uint32_t port_id;
1451         uint32_t id;
1452
1453         if (sscanf(str, "%u %x", &port_id, &id) != 2) {
1454                 return -1;
1455         }
1456
1457         cmd_read_reg(port_id, id);
1458         return 0;
1459 }
1460
1461 static int parse_cmd_cache_reset(const char *str, struct input *input)
1462 {
1463         cmd_cache_reset();
1464         return 0;
1465 }
1466
1467 static int parse_cmd_set_cache_class_mask(const char *str, struct input *input)
1468 {
1469         uint32_t lcore_id;
1470         uint32_t set;
1471         uint32_t val;
1472
1473         if (sscanf(str, "%u %u %u", &lcore_id, &set, &val) != 3) {
1474                 return -1;
1475         }
1476
1477         cmd_set_cache_class_mask(lcore_id, set, val);
1478         return 0;
1479 }
1480
1481 static int parse_cmd_set_cache_class(const char *str, struct input *input)
1482 {
1483         uint32_t lcore_id;
1484         uint32_t set;
1485
1486         if (sscanf(str, "%u %u", &lcore_id, &set) != 2) {
1487                 return -1;
1488         }
1489
1490         cmd_set_cache_class(lcore_id, set);
1491         return 0;
1492 }
1493
1494 static int parse_cmd_get_cache_class_mask(const char *str, struct input *input)
1495 {
1496         uint32_t lcore_id;
1497         uint32_t set;
1498         uint32_t val = 0;
1499
1500         if (sscanf(str, "%u %u", &lcore_id, &set) != 2) {
1501                 return -1;
1502         }
1503
1504         cmd_get_cache_class_mask(lcore_id, set, &val);
1505         if (input->reply) {
1506                 char buf[128];
1507                 snprintf(buf, sizeof(buf), "%d, %d, %x\n", lcore_id, set, val);
1508                 input->reply(input, buf, strlen(buf));
1509         } else {
1510                 plog_info("core=%d, set=%d, mask=%x\n", lcore_id, set, val);
1511         }
1512         return 0;
1513 }
1514
1515 static int parse_cmd_get_cache_class(const char *str, struct input *input)
1516 {
1517         uint32_t lcore_id;
1518         uint32_t set;
1519         uint32_t val;
1520
1521         if (sscanf(str, "%u", &lcore_id) != 1) {
1522                 return -1;
1523         }
1524
1525         cmd_get_cache_class(lcore_id, &set);
1526         if (input->reply) {
1527                 char buf[128];
1528                 snprintf(buf, sizeof(buf), "%d, %d\n", lcore_id, set);
1529                 input->reply(input, buf, strlen(buf));
1530         } else {
1531                 plog_info("core=%d, cos=%d\n", lcore_id, set);
1532         }
1533         return 0;
1534 }
1535
1536 static int parse_cmd_get_cache_mask(const char *str, struct input *input)
1537 {
1538         uint32_t lcore_id;
1539         uint32_t set;
1540         uint32_t mask;
1541
1542         if (sscanf(str, "%u", &lcore_id) != 1) {
1543                 return -1;
1544         }
1545
1546         cmd_get_cache_class(lcore_id, &set);
1547         cmd_get_cache_class_mask(lcore_id, set, &mask);
1548         if (input->reply) {
1549                 char buf[128];
1550                 snprintf(buf, sizeof(buf), "%d, %x\n", lcore_id, mask);
1551                 input->reply(input, buf, strlen(buf));
1552         } else {
1553                 plog_info("core=%d, mask=%x\n", lcore_id, mask);
1554         }
1555         return 0;
1556 }
1557
1558 static int parse_cmd_set_vlan_offload(const char *str, struct input *input)
1559 {
1560         uint32_t port_id;
1561         uint32_t val;
1562
1563         if (sscanf(str, "%u %u", &port_id, &val) != 2) {
1564                 return -1;
1565         }
1566
1567         cmd_set_vlan_offload(port_id, val);
1568         return 0;
1569 }
1570
1571 static int parse_cmd_set_vlan_filter(const char *str, struct input *input)
1572 {
1573         uint32_t port_id;
1574         uint32_t id, val;
1575
1576         if (sscanf(str, "%u %d %u", &port_id, &id, &val) != 3) {
1577                 return -1;
1578         }
1579
1580         cmd_set_vlan_filter(port_id, id, val);
1581         return 0;
1582 }
1583
1584 static int parse_cmd_ring_info_all(const char *str, struct input *input)
1585 {
1586         if (strcmp(str, "") != 0) {
1587                 return -1;
1588         }
1589         cmd_ringinfo_all();
1590         return 0;
1591 }
1592
1593 static int parse_cmd_port_up(const char *str, struct input *input)
1594 {
1595         unsigned val;
1596
1597         if (sscanf(str, "%u", &val) != 1) {
1598                 return -1;
1599         }
1600
1601         cmd_port_up(val);
1602         return 0;
1603 }
1604
1605 static int parse_cmd_port_down(const char *str, struct input *input)
1606 {
1607         unsigned val;
1608
1609         if (sscanf(str, "%u", &val) != 1) {
1610                 return -1;
1611         }
1612
1613         cmd_port_down(val);
1614         return 0;
1615 }
1616
1617 static int parse_cmd_port_link_state(const char *str, struct input *input)
1618 {
1619         unsigned val;
1620
1621         if (sscanf(str, "%u", &val) != 1) {
1622                 return -1;
1623         }
1624
1625         if (!port_is_active(val))
1626                 return -1;
1627
1628         int active = prox_port_cfg[val].link_up;
1629         const char *state = active? "up\n" : "down\n";
1630
1631         if (input->reply)
1632                 input->reply(input, state, strlen(state));
1633         else
1634                 plog_info("%s", state);
1635
1636         return 0;
1637 }
1638
1639 static int parse_cmd_xstats(const char *str, struct input *input)
1640 {
1641         unsigned val;
1642
1643         if (sscanf(str, "%u", &val) != 1) {
1644                 return -1;
1645         }
1646
1647         cmd_xstats(val);
1648         return 0;
1649 }
1650
1651 static int parse_cmd_stats(const char *str, struct input *input)
1652 {
1653         if (strcmp(str, "") == 0)
1654                 return -1;
1655
1656         char buf[32768];
1657         char ret2[32768];
1658         char *ret = ret2;
1659         int list = 0;
1660
1661         prox_strncpy(buf, str, sizeof(buf) - 1);
1662         char *tok;
1663         uint64_t stat_val;
1664
1665         while ((tok = strchr(str, ','))) {
1666                 *tok = 0;
1667                 stat_val = stats_parser_get(str);
1668
1669                 ret += sprintf(ret, "%s%"PRIu64"", list? "," :"", stat_val);
1670                 list = 1;
1671                 str = tok + 1;
1672         }
1673
1674         stat_val = stats_parser_get(str);
1675         ret += sprintf(ret, "%s%"PRIu64"", list? "," :"", stat_val);
1676
1677         sprintf(ret, "\n");
1678
1679         if (input->reply)
1680                 input->reply(input, ret2, strlen(ret2));
1681         else
1682                 plog_info("%s", ret2);
1683         return 0;
1684 }
1685
1686 static void replace_char(char *str, char to_replace, char by)
1687 {
1688         for (size_t i = 0; str[i] != '\0'; ++i) {
1689                 if (str[i] == to_replace)
1690                         str[i] = by;
1691         }
1692 }
1693
1694 static int parse_cmd_port_info(const char *str, struct input *input)
1695 {
1696         int val;
1697
1698         if (strcmp(str, "all") == 0) {
1699                 val = -1;
1700         }
1701         else if (sscanf(str, "%d", &val) != 1) {
1702                 return -1;
1703         }
1704
1705         char port_info[2048];
1706
1707         cmd_portinfo(val, port_info, sizeof(port_info));
1708
1709         if (input->reply) {
1710                 replace_char(port_info, '\n', ',');
1711                 port_info[strlen(port_info) - 1] = '\n';
1712                 input->reply(input, port_info, strlen(port_info));
1713         } else
1714                 plog_info("%s", port_info);
1715
1716         return 0;
1717 }
1718
1719 static int parse_cmd_ring_info(const char *str, struct input *input)
1720 {
1721         unsigned lcores[RTE_MAX_LCORE], task_id, nb_cores;
1722
1723         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1724                 return -1;
1725
1726         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1727                 for (unsigned int i = 0; i < nb_cores; i++) {
1728                         cmd_ringinfo(lcores[i], task_id);
1729                 }
1730         }
1731         return 0;
1732 }
1733
1734 static int parse_cmd_port_stats(const char *str, struct input *input)
1735 {
1736         unsigned val;
1737
1738         if (sscanf(str, "%u", &val) != 1) {
1739                 return -1;
1740         }
1741
1742         struct get_port_stats s;
1743         if (stats_port(val, &s)) {
1744                 plog_err("Invalid port %u\n", val);
1745                 return 0;
1746         }
1747         char buf[256];
1748         snprintf(buf, sizeof(buf),
1749                  "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
1750                  "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
1751                  "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
1752                  s.no_mbufs_diff, s.ierrors_diff + s.imissed_diff,
1753                  s.rx_bytes_diff, s.tx_bytes_diff,
1754                  s.rx_pkts_diff, s.tx_pkts_diff,
1755                  s.rx_tot, s.tx_tot,
1756                  s.no_mbufs_tot, s.ierrors_tot + s.imissed_tot,
1757                  s.last_tsc, s.prev_tsc);
1758         plog_info("%s", buf);
1759         if (input->reply)
1760                 input->reply(input, buf, strlen(buf));
1761         return 0;
1762 }
1763
1764 static int parse_cmd_multi_port_stats(const char *str, struct input *input)
1765 {
1766         uint32_t ports[PROX_MAX_PORTS];
1767         int nb_ports = parse_list_set(ports, str, PROX_MAX_PORTS);
1768         if (nb_ports <= 0) {
1769                 return -1;
1770         }
1771
1772         char buf[PROX_MAX_PORTS * (11+5*21) + 1], *pbuf = buf;
1773         int left = sizeof(buf);
1774         for (int i = 0; i < nb_ports; ++i) {
1775                 struct get_port_stats s;
1776                 if (stats_port(ports[i], &s)) {
1777                         plog_err("Invalid port %u\n", ports[i]);
1778                         return 0;
1779                 }
1780
1781                 int len = snprintf(pbuf, left,
1782                                 "%u,"
1783                                 "%"PRIu64",%"PRIu64","
1784                                 "%"PRIu64",%"PRIu64","
1785                                 "%"PRIu64";",
1786                                 //TODO: adjust buf size above when adding fields
1787                                 ports[i],
1788                                 s.rx_tot, s.tx_tot,
1789                                 s.no_mbufs_tot, s.ierrors_tot + s.imissed_tot,
1790                                 s.last_tsc);
1791                 if ((len < 0) || (len >= left)) {
1792                         plog_err("Cannot print stats for port %u\n", ports[i]);
1793                         return 0;
1794                 }
1795                 pbuf += len;
1796                 left -= len;
1797         }
1798         pbuf--;
1799         *pbuf = '\n';
1800
1801         plog_info("%s", buf);
1802         if (input->reply)
1803                 input->reply(input, buf, sizeof(buf) - left);
1804         return 0;
1805 }
1806
1807 static int parse_cmd_core_stats(const char *str, struct input *input)
1808 {
1809         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
1810
1811         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1812                 return -1;
1813
1814         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1815                 for (unsigned int i = 0; i < nb_cores; i++) {
1816                         lcore_id = lcores[i];
1817                         uint64_t tot_rx = stats_core_task_tot_rx(lcore_id, task_id);
1818                         uint64_t tot_tx = stats_core_task_tot_tx(lcore_id, task_id);
1819                         uint64_t tot_drop = stats_core_task_tot_drop(lcore_id, task_id);
1820                         uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);
1821
1822                         if (input->reply) {
1823                                 char buf[128];
1824                                 snprintf(buf, sizeof(buf),
1825                                         "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
1826                                         tot_rx, tot_tx, tot_drop, last_tsc, rte_get_tsc_hz());
1827                                 input->reply(input, buf, strlen(buf));
1828                         }
1829                         else {
1830                                 plog_info("RX: %"PRIu64", TX: %"PRIu64", DROP: %"PRIu64"\n",
1831                                         tot_rx, tot_tx, tot_drop);
1832                         }
1833                 }
1834         }
1835         return 0;
1836 }
1837
1838 typedef void (*parser_handler)(unsigned, unsigned, struct input *);
1839 static int handle_cores_tasks(const char *str, struct input *input, const char *mode_str, const char *mode_name, parser_handler f)
1840 {
1841         // This function either outputs a single line, in case of syntax error on the lists of cores and/or tasks
1842         // or outputs (nb_cores * nb_tasks) lines, one line for each core/task pair:
1843         // - if the core/task pair is invalid, the output line reports an error
1844         // - otherwise, the output line provides the latency statistics for the core/task pair
1845
1846         unsigned lcores[RTE_MAX_LCORE], tasks[MAX_TASKS_PER_CORE], lcore_id, task_id, nb_cores, nb_tasks;
1847         if (parse_cores_tasks(str, lcores, tasks, &nb_cores, &nb_tasks)) {
1848                 if (input->reply) {
1849                         char buf[128];
1850                         snprintf(buf, sizeof(buf), "error: invalid syntax\n");
1851                         input->reply(input, buf, strlen(buf));
1852                 }
1853                 return -1;
1854         }
1855
1856         for (unsigned int i = 0; i < nb_cores; i++) {
1857                 for (unsigned int j = 0; j < nb_tasks; j++) {
1858                         lcore_id = lcores[i];
1859                         task_id = tasks[j];
1860                         if (core_task_is_valid(lcore_id, task_id) == 0) {
1861                                 if (input->reply) {
1862                                         char buf[128];
1863                                         snprintf(buf, sizeof(buf), "error: invalid core %u, task %u\n", lcore_id, task_id);
1864                                         input->reply(input, buf, strlen(buf));
1865                                 } else {
1866                                         plog_info("error: invalid core %u, task %u\n", lcore_id, task_id);
1867                                 }
1868                                 continue;
1869                         }
1870                         if ((mode_str) && (!task_is_mode(lcore_id, task_id, mode_str))) {
1871                                 if (input->reply) {
1872                                         char buf[128];
1873                                         snprintf(buf, sizeof(buf), "error: core %u task %u is not measuring %s\n", lcore_id, task_id, mode_name);
1874                                         input->reply(input, buf, strlen(buf));
1875                                 } else {
1876                                         plog_info("error: core %u task %u is not measuring %s\n", lcore_id, task_id, mode_name);
1877                                 }
1878                                 continue;
1879                         }
1880                         f(lcore_id, task_id, input);
1881                 }
1882         }
1883         return 0;
1884 }
1885
1886 static void handle_dp_core_stats(unsigned lcore_id, unsigned task_id, struct input *input)
1887 {
1888         uint64_t tot_rx = stats_core_task_tot_rx(lcore_id, task_id);
1889         uint64_t tot_tx = stats_core_task_tot_tx(lcore_id, task_id);
1890         uint64_t tot_tx_fail = stats_core_task_tot_tx_fail(lcore_id, task_id);
1891         uint64_t tot_rx_non_dp = stats_core_task_tot_rx_non_dp(lcore_id, task_id);
1892         uint64_t tot_tx_non_dp = stats_core_task_tot_tx_non_dp(lcore_id, task_id);
1893         uint64_t tot_drop = stats_core_task_tot_drop(lcore_id, task_id);
1894         uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);
1895
1896         if (input->reply) {
1897                 char buf[128];
1898                 snprintf(buf, sizeof(buf),
1899                         "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%u,%u\n",
1900                         tot_rx, tot_tx, tot_rx_non_dp, tot_tx_non_dp, tot_drop, tot_tx_fail, last_tsc, rte_get_tsc_hz(), lcore_id, task_id);
1901                 input->reply(input, buf, strlen(buf));
1902         }
1903         else {
1904                 plog_info("core: %u, task: %u, RX: %"PRIu64", TX: %"PRIu64", RX_NON_DP: %"PRIu64", TX_NON_DP: %"PRIu64", DROP: %"PRIu64", TX_FAIL: %"PRIu64"\n",
1905                         lcore_id, task_id, tot_rx, tot_tx, tot_rx_non_dp, tot_tx_non_dp, tot_drop, tot_tx_fail);
1906         }
1907 }
1908
1909 static void handle_lat_stats(unsigned lcore_id, unsigned task_id, struct input *input)
1910 {
1911         struct stats_latency *stats = stats_latency_find(lcore_id, task_id);
1912         struct stats_latency *tot = stats_latency_tot_find(lcore_id, task_id);
1913         if (!stats || !tot) {
1914                 if (input->reply) {
1915                         char buf[128];
1916                         snprintf(buf, sizeof(buf),
1917                                  "error: core %u task %u stats = %p tot = %p\n",
1918                                  lcore_id, task_id, stats, tot);
1919                         input->reply(input, buf, strlen(buf));
1920                 } else {
1921                         plog_info("error: core %u task %u stats = %p tot = %p\n",
1922                                   lcore_id, task_id, stats, tot);
1923                 }
1924                 return;
1925         }
1926
1927         uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);
1928         uint64_t lat_min_usec = time_unit_to_usec(&stats->min.time);
1929         uint64_t lat_max_usec = time_unit_to_usec(&stats->max.time);
1930         uint64_t tot_lat_min_usec = time_unit_to_usec(&tot->min.time);
1931         uint64_t tot_lat_max_usec = time_unit_to_usec(&tot->max.time);
1932         uint64_t lat_avg_usec = time_unit_to_usec(&stats->avg.time);
1933
1934         if (input->reply) {
1935                 char buf[128];
1936                 snprintf(buf, sizeof(buf),
1937                          "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%u,%u,%"PRIu64",%"PRIu64",%"PRIu64"\n",
1938                          lat_min_usec,
1939                          lat_max_usec,
1940                          lat_avg_usec,
1941                          tot_lat_min_usec,
1942                          tot_lat_max_usec,
1943                          last_tsc,
1944                          rte_get_tsc_hz(),
1945                          lcore_id,
1946                          task_id,
1947                          stats->mis_ordered,
1948                          stats->extent,
1949                          stats->duplicate);
1950                 input->reply(input, buf, strlen(buf));
1951         }
1952         else {
1953                 plog_info("core: %u, task: %u, min: %"PRIu64", max: %"PRIu64", avg: %"PRIu64", min since reset: %"PRIu64", max since reset: %"PRIu64", mis_ordered: %"PRIu64", extent: %"PRIu64", duplicates: %"PRIu64"\n",
1954                          lcore_id,
1955                          task_id,
1956                          lat_min_usec,
1957                          lat_max_usec,
1958                          lat_avg_usec,
1959                          tot_lat_min_usec,
1960                          tot_lat_max_usec,
1961                          stats->mis_ordered,
1962                          stats->extent,
1963                          stats->duplicate);
1964         }
1965 }
1966
1967 #ifdef LATENCY_HISTOGRAM
1968 static void handle_latency_histogram(unsigned lcore_id, unsigned task_id, struct input *input)
1969 {
1970         uint64_t *buckets;
1971
1972         stats_core_lat_histogram(lcore_id, task_id, &buckets);
1973
1974         if (buckets == NULL) {
1975                 if (input->reply) {
1976                         char buf[128];
1977                         snprintf(buf, sizeof(buf), "error: unexpected NULL bucket\n");
1978                         input->reply(input, buf, strlen(buf));
1979                 }
1980                 return;
1981         }
1982
1983         if (input->reply) {
1984                 char buf[4096] = {0};
1985                 for (size_t i = 0; i < LAT_BUCKET_COUNT; i++)
1986                         sprintf(buf+strlen(buf), "Bucket [%zu]: %"PRIu64"\n", i, buckets[i]);
1987                 input->reply(input, buf, strlen(buf));
1988         }
1989         else {
1990                 for (size_t i = 0; i < LAT_BUCKET_COUNT; i++)
1991                         if (buckets[i])
1992                                 plog_info("Bucket [%zu]: %"PRIu64"\n", i, buckets[i]);
1993         }
1994 }
1995
1996 static void handle_stats_and_packets(unsigned lcore_id, unsigned task_id, struct input *input)
1997 {
1998         handle_lat_stats(lcore_id, task_id, input);
1999         handle_latency_histogram(lcore_id, task_id, input);
2000 }
2001 #endif
2002
2003 static int parse_cmd_dp_core_stats(const char *str, struct input *input)
2004 {
2005         handle_cores_tasks(str, input, NULL, NULL, handle_dp_core_stats);
2006         return 0;
2007 }
2008
2009 static int parse_cmd_lat_stats(const char *str, struct input *input)
2010 {
2011         handle_cores_tasks(str, input, "lat", "latency", handle_lat_stats);
2012         return 0;
2013 }
2014
2015 static int parse_cmd_lat_packets(const char *str, struct input *input)
2016 {
2017 #ifdef LATENCY_HISTOGRAM
2018         handle_cores_tasks(str, input, "lat", "latency", handle_latency_histogram);
2019 #else
2020         if (input->reply) {
2021                 char buf[128];
2022                 snprintf(buf, sizeof(buf), "error: invalid syntax (LATENCY_HISTOGRAM disabled)\n");
2023                 input->reply(input, buf, strlen(buf));
2024         } else {
2025                 plog_info("LATENCY_HISTOGRAMS disabled\n");
2026         }
2027 #endif
2028         return 0;
2029 }
2030
2031 static int parse_cmd_lat_stats_and_packets(const char *str, struct input *input)
2032 {
2033 #ifdef LATENCY_HISTOGRAM
2034         handle_cores_tasks(str, input, "lat", "latency", handle_stats_and_packets);
2035 #else
2036         if (input->reply) {
2037                 char buf[128];
2038                 snprintf(buf, sizeof(buf), "error: invalid syntax (LATENCY_HISTOGRAMS disabled)\n");
2039                 input->reply(input, buf, strlen(buf));
2040         } else {
2041                 plog_info("LATENCY_HISTOGRAMS disabled\n");
2042         }
2043 #endif
2044         return 0;
2045 }
2046
2047 static int parse_cmd_show_irq_buckets(const char *str, struct input *input)
2048 {
2049         char buf[4096] = {0};
2050         unsigned int i, c;
2051         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2052
2053         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2054                 return -1;
2055
2056         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2057                 for (c = 0; c < nb_cores; c++) {
2058                         lcore_id = lcores[c];
2059                         get_irq_buckets_by_core_task(buf, lcore_id, task_id);
2060                         plog_info("%s", buf);
2061                         if (input->reply)
2062                                 input->reply(input, buf, strlen(buf));
2063                         buf[0] = 0;
2064                 }
2065         }
2066         return 0;
2067 }
2068
2069 static int parse_cmd_irq(const char *str, struct input *input)
2070 {
2071         unsigned int i, c;
2072         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2073
2074         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2075                 return -1;
2076
2077         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2078                 for (c = 0; c < nb_cores; c++) {
2079                         lcore_id = lcores[c];
2080                         if (!task_is_mode(lcore_id, task_id, "irq")) {
2081                                 plog_err("Core %u task %u is not in irq mode\n", lcore_id, task_id);
2082                         } else {
2083                                 struct task_irq *task_irq = (struct task_irq *)(lcore_cfg[lcore_id].tasks_all[task_id]);
2084
2085                                 task_irq_show_stats(task_irq, input);
2086                         }
2087                 }
2088         }
2089         return 0;
2090 }
2091
2092 static int parse_cmd_cgnat_public_hash(const char *str, struct input *input)
2093 {
2094         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2095
2096         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2097                 return -1;
2098
2099         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2100                 for (unsigned int i = 0; i < nb_cores; i++) {
2101                         lcore_id = lcores[i];
2102
2103                         if (!task_is_mode(lcore_id, task_id, "cgnat")) {
2104                                 plog_err("Core %u task %u is not cgnat\n", lcore_id, task_id);
2105                         }
2106                         else {
2107                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2108                                 task_cgnat_dump_public_hash((struct task_nat *)tbase);
2109                         }
2110                 }
2111         }
2112         return 0;
2113 }
2114
2115 static int parse_cmd_cgnat_private_hash(const char *str, struct input *input)
2116 {
2117         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2118         uint32_t val;
2119
2120         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2121                 return -1;
2122
2123         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2124                 for (unsigned int i = 0; i < nb_cores; i++) {
2125                         lcore_id = lcores[i];
2126
2127                         if (!task_is_mode(lcore_id, task_id, "cgnat")) {
2128                                 plog_err("Core %u task %u is not cgnat\n", lcore_id, task_id);
2129                         }
2130                         else {
2131                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2132                                 task_cgnat_dump_private_hash((struct task_nat *)tbase);
2133                         }
2134                 }
2135         }
2136         return 0;
2137 }
2138
2139 static int parse_cmd_accuracy(const char *str, struct input *input)
2140 {
2141         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2142         uint32_t val;
2143
2144         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2145                 return -1;
2146         if (!(str = strchr_skip_twice(str, ' ')))
2147                 return -1;
2148         if (sscanf(str, "%"PRIu32"", &val) != 1)
2149                 return -1;
2150
2151         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2152                 for (unsigned int i = 0; i < nb_cores; i++) {
2153                         lcore_id = lcores[i];
2154
2155                         if (!task_is_mode(lcore_id, task_id, "lat")) {
2156                                 plog_err("Core %u task %u is not measuring latency\n", lcore_id, task_id);
2157                         }
2158                         else {
2159                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2160
2161                                 task_lat_set_accuracy_limit((struct task_lat *)tbase, val);
2162                         }
2163                 }
2164         }
2165         return 0;
2166 }
2167
2168 static int parse_cmd_leave_igmp(const char *str, struct input *input)
2169 {
2170         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2171
2172         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2173                 return -1;
2174
2175         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2176                 for (unsigned int i = 0; i < nb_cores; i++) {
2177                         lcore_id = lcores[i];
2178
2179                         if (!task_is_mode(lcore_id, task_id, "swap")) {
2180                                 plog_err("Core %u task %u is not running swap\n", lcore_id, task_id);
2181                         }
2182                         else {
2183                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2184                                 igmp_leave_group(tbase);
2185                         }
2186                 }
2187         }
2188         return 0;
2189 }
2190
2191 static int parse_cmd_join_igmp(const char *str, struct input *input)
2192 {
2193         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2194         uint32_t igmp_ip;
2195         uint8_t *igmp_bytes = (uint8_t *)&igmp_ip;
2196
2197         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2198                 return -1;
2199         if (!(str = strchr_skip_twice(str, ' ')))
2200                 return -1;
2201         if (sscanf(str, "%hhu.%hhu.%hhu.%hhu", igmp_bytes, igmp_bytes + 1, igmp_bytes + 2, igmp_bytes + 3) != 4) {
2202                 return -1;
2203         }
2204         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2205                 for (unsigned int i = 0; i < nb_cores; i++) {
2206                         lcore_id = lcores[i];
2207
2208                         if (!task_is_mode(lcore_id, task_id, "swap")) {
2209                                 plog_err("Core %u task %u is not running swap\n", lcore_id, task_id);
2210                         }
2211                         else {
2212                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2213                                 igmp_join_group(tbase, igmp_ip);
2214                         }
2215                 }
2216         }
2217         return 0;
2218 }
2219
2220 static int parse_cmd_send_unsollicited_na(const char *str, struct input *input)
2221 {
2222         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2223
2224         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2225                 return -1;
2226
2227         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2228                 for (unsigned int i = 0; i < nb_cores; i++) {
2229                         lcore_id = lcores[i];
2230
2231                         if (!task_is_sub_mode(lcore_id, task_id, "ndp")) {
2232                                 plog_err("Core %u task %u is not running ndp\n", lcore_id, task_id);
2233                         }
2234                         else {
2235                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2236                                 send_unsollicited_neighbour_advertisement(tbase);
2237                         }
2238                 }
2239         }
2240         return 0;
2241 }
2242
2243 static int parse_cmd_rx_tx_info(const char *str, struct input *input)
2244 {
2245         if (strcmp(str, "") != 0) {
2246                 return -1;
2247         }
2248
2249         cmd_rx_tx_info();
2250         return 0;
2251 }
2252
2253 static int parse_cmd_version(const char *str, struct input *input)
2254 {
2255         if (strcmp(str, "") != 0) {
2256                 return -1;
2257         }
2258
2259         if (input->reply) {
2260                 uint64_t version =
2261                         ((uint64_t)VERSION_MAJOR) << 24 |
2262                         ((uint64_t)VERSION_MINOR) << 16 |
2263                         ((uint64_t)VERSION_REV) << 8;
2264
2265                 char buf[128];
2266                 snprintf(buf, sizeof(buf), "%"PRIu64",%"PRIu64"\n", version, (uint64_t)RTE_VERSION);
2267                 input->reply(input, buf, strlen(buf));
2268         }
2269         else {
2270                 plog_info("prox version: %d.%d, DPDK version: %s\n",
2271                           VERSION_MAJOR, VERSION_MINOR,
2272                           rte_version() + sizeof(RTE_VER_PREFIX));
2273         }
2274         return 0;
2275 }
2276
2277 struct cmd_str {
2278         const char *cmd;
2279         const char *args;
2280         const char *help;
2281         int (*parse)(const char *args, struct input *input);
2282 };
2283
2284 static int parse_cmd_help(const char *str, struct input *input);
2285
2286 static struct cmd_str cmd_strings[] = {
2287         {"history", "", "Print command history", parse_cmd_history},
2288         {"echo", "", "echo parameter, useful to resolving variables", parse_cmd_echo},
2289         {"quit", "", "Stop all cores and quit", parse_cmd_quit},
2290         {"quit_force", "", "Quit without waiting on cores to stop", parse_cmd_quit_force},
2291         {"help", "<substr>", "Show list of commands that have <substr> as a substring. If no substring is provided, all commands are shown.", parse_cmd_help},
2292         {"verbose", "<level>", "Set verbosity level", parse_cmd_verbose},
2293         {"thread info", "<core_id> <task_id>", "", parse_cmd_thread_info},
2294         {"mem info", "", "Show information about system memory (number of huge pages and addresses of these huge pages)", parse_cmd_mem_info},
2295         {"update interval", "<value>", "Update statistics refresh rate, in msec (must be >=10). Default is 1 second", parse_cmd_update_interval},
2296         {"rx tx info", "", "Print connections between tasks on all cores", parse_cmd_rx_tx_info},
2297         {"start", "<core list>|all <task_id>", "Start core <core_id> or all cores", parse_cmd_start},
2298         {"stop", "<core list>|all <task_id>", "Stop core <core id> or all cores", parse_cmd_stop},
2299
2300         {"dump", "<core id> <task id> <nb packets>", "Create a hex dump of <nb_packets> from <task_id> on <core_id> showing how packets have changed between RX and TX.", parse_cmd_trace},
2301         {"dump_rx", "<core id> <task id> <nb packets>", "Create a hex dump of <nb_packets> from <task_id> on <core_id> at RX", parse_cmd_dump_rx},
2302         {"dump_tx", "<core id> <task id> <nb packets>", "Create a hex dump of <nb_packets> from <task_id> on <core_id> at TX", parse_cmd_dump_tx},
2303         {"rx distr start", "", "Start gathering statistical distribution of received packets", parse_cmd_rx_distr_start},
2304         {"rx distr stop", "", "Stop gathering statistical distribution of received packets", parse_cmd_rx_distr_stop},
2305         {"rx distr reset", "", "Reset gathered statistical distribution of received packets", parse_cmd_rx_distr_reset},
2306         {"rx distr show", "", "Display gathered statistical distribution of received packets", parse_cmd_rx_distr_show},
2307         {"tx distr start", "", "Start gathering statistical distribution of xmitted packets", parse_cmd_tx_distr_start},
2308         {"tx distr stop", "", "Stop gathering statistical distribution of xmitted packets", parse_cmd_tx_distr_stop},
2309         {"tx distr reset", "", "Reset gathered statistical distribution of xmitted packets", parse_cmd_tx_distr_reset},
2310         {"tx distr show", "", "Display gathered statistical distribution of xmitted packets", parse_cmd_tx_distr_show},
2311
2312         {"rate", "<port id> <queue id> <rate>", "rate does not include preamble, SFD and IFG", parse_cmd_rate},
2313         {"count","<core id> <task id> <count>", "Generate <count> packets", parse_cmd_count},
2314         {"bypass", "<core_id> <task_id>", "Bypass task", parse_cmd_bypass},
2315         {"reconnect", "<core_id> <task_id>", "Reconnect task", parse_cmd_reconnect},
2316         {"pkt_size", "<core_id> <task_id> <pkt_size>", "Set the packet size to <pkt_size>", parse_cmd_pkt_size},
2317         {"imix", "<core_id> <task_id> <pkt_size,pkt_size ... >", "Set the packet sizes to <pkt_size>", parse_cmd_imix},
2318         {"speed", "<core_id> <task_id> <speed percentage>", "Change the speed to <speed percentage> at which packets are being generated on core <core_id> in task <task_id>.", parse_cmd_speed},
2319         {"speed_byte", "<core_id> <task_id> <speed>", "Change speed to <speed>. The speed is specified in units of bytes per second.", parse_cmd_speed_byte},
2320         {"set value", "<core_id> <task_id> <offset> <value> <value_len>", "Set <value_len> bytes to <value> at offset <offset> in packets generated on <core_id> <task_id>", parse_cmd_set_value},
2321         {"set random", "<core_id> <task_id> <offset> <random_str> <value_len>", "Set <value_len> bytes to <rand_str> at offset <offset> in packets generated on <core_id> <task_id>", parse_cmd_set_random},
2322         {"set range", "<core_id> <task_id> <offset> <range_start> <range_end>", "Set bytes from <range_start>  to <range_end> at offset <offset> in packets generated on <core_id> <task_id>", parse_cmd_set_range},
2323         {"reset values all", "", "Undo all \"set value\" commands on all cores/tasks", parse_cmd_reset_values_all},
2324         {"reset randoms all", "", "Undo all \"set random\" commands on all cores/tasks", parse_cmd_reset_randoms_all},
2325         {"reset ranges all", "", "Undo all \"set range\" commands on all cores/tasks", parse_cmd_reset_ranges_all},
2326         {"reset values", "<core id> <task id>", "Undo all \"set value\" commands on specified core/task", parse_cmd_reset_values},
2327
2328         {"arp add", "<core id> <task id> <port id> <gre id> <svlan> <cvlan> <ip addr> <mac addr> <user>", "Add a single ARP entry into a CPE table on <core id>/<task id>.", parse_cmd_arp_add},
2329         {"rule add", "<core id> <task id> svlan_id&mask cvlan_id&mask ip_proto&mask source_ip/prefix destination_ip/prefix range dport_range action", "Add a rule to the ACL table on <core id>/<task id>", parse_cmd_rule_add},
2330         {"route add", "<core id> <task id> <ip/prefix> <next hop id>", "Add a route to the routing table on core <core id> <task id>. Example: route add 10.0.16.0/24 9", parse_cmd_route_add},
2331         {"gateway ip", "<core id> <task id> <ip>", "Define/Change IP address of destination gateway on core <core id> <task id>.", parse_cmd_gateway_ip},
2332         {"local ip", "<core id> <task id> <ip>", "Define/Change IP address of destination gateway on core <core id> <task id>.", parse_cmd_local_ip},
2333
2334         {"pps unit", "", "Change core stats pps unit", parse_cmd_pps_unit},
2335         {"reset stats", "", "Reset all statistics", parse_cmd_reset_stats},
2336         {"reset lat stats", "", "Reset all latency statistics", parse_cmd_reset_lat_stats},
2337         {"tot stats", "", "Print total RX and TX packets", parse_cmd_tot_stats},
2338         {"tot ierrors tot", "", "Print total number of ierrors since reset", parse_cmd_tot_ierrors_tot},
2339         {"tot imissed tot", "", "Print total number of imissed since reset", parse_cmd_tot_imissed_tot},
2340         {"lat stats", "<core id> <task id>", "Print min,max,avg latency as measured during last sampling interval", parse_cmd_lat_stats},
2341         {"irq stats", "<core id> <task id>", "Print irq related infos", parse_cmd_irq},
2342         {"show irq buckets", "<core id> <task id>", "Print irq buckets", parse_cmd_show_irq_buckets},
2343         {"lat packets", "<core id> <task id>", "Print the latency for each of the last set of packets", parse_cmd_lat_packets},
2344         {"lat all stats", "<core id> <task id>", "Print the latency for each of the last set of packets as well as latency distribution", parse_cmd_lat_stats_and_packets},
2345         {"accuracy limit", "<core id> <task id> <nsec>", "Only consider latency of packets that were measured with an error no more than <nsec>", parse_cmd_accuracy},
2346         {"core stats", "<core id> <task id>", "Print rx/tx/drop for task <task id> running on core <core id>", parse_cmd_core_stats},
2347         {"dp core stats", "<core id> <task id>", "Print rx/tx/non_dp_rx/non_dp_tx/drop for task <task id> running on core <core id>", parse_cmd_dp_core_stats},
2348         {"port_stats", "<port id>", "Print rate for no_mbufs, ierrors + imissed, rx_bytes, tx_bytes, rx_pkts, tx_pkts; totals for RX, TX, no_mbufs, ierrors + imissed for port <port id>", parse_cmd_port_stats},
2349         {"multi port stats", "<port list>", "Get stats for multiple ports, semi-colon separated: port id, total for rx_pkts, tx_pkts, no_mbufs, ierrors + imissed, last_tsc", parse_cmd_multi_port_stats},
2350         {"read reg", "", "Read register", parse_cmd_read_reg},
2351         {"write reg", "", "Read register", parse_cmd_write_reg},
2352         {"set vlan offload", "", "Set Vlan offload", parse_cmd_set_vlan_offload},
2353         {"set vlan filter", "", "Set Vlan filter", parse_cmd_set_vlan_filter},
2354         {"reset cache", "", "Reset cache", parse_cmd_cache_reset},
2355         {"set cache class mask", "<core id> <class> <mask>", "Set cache class mask for <core id>", parse_cmd_set_cache_class_mask},
2356         {"get cache class mask", "<core id> <class>", "Get cache class mask", parse_cmd_get_cache_class_mask},
2357         {"set cache class", "<core id> <class>", "Set cache class", parse_cmd_set_cache_class},
2358         {"get cache class", "<core id>", "Get cache class", parse_cmd_get_cache_class},
2359         {"get cache mask", "<core id>", "Get cache mask", parse_cmd_get_cache_mask},
2360         {"reset port", "<port id>", "Reset port", parse_cmd_reset_port},
2361         {"enable multicast", "<port id> <MAC>", "Enable multicast", parse_cmd_enable_multicast},
2362         {"disable multicast", "<port id> <MAC>", "Disable multicast", parse_cmd_disable_multicast},
2363         {"ring info all", "", "Get information about ring, such as ring size and number of elements in the ring", parse_cmd_ring_info_all},
2364         {"ring info", "<core id> <task id>", "Get information about ring on core <core id> in task <task id>, such as ring size and number of elements in the ring", parse_cmd_ring_info},
2365         {"port info", "<port id> [brief?]", "Get port related information, such as MAC address, socket, number of descriptors..., . Adding \"brief\" after command prints short version of output.", parse_cmd_port_info},
2366         {"port up", "<port id>", "Set the port up", parse_cmd_port_up},
2367         {"port down", "<port id>", "Set the port down", parse_cmd_port_down},
2368         {"port link state", "<port id>", "Get link state (up or down) for port", parse_cmd_port_link_state},
2369         {"port xstats", "<port id>", "Get extra statistics for the port", parse_cmd_xstats},
2370         {"stats", "<stats_path>", "Get stats as specified by <stats_path>. A comma-separated list of <stats_path> can be supplied", parse_cmd_stats},
2371         {"cgnat dump public hash", "<core id> <task id>", "Dump cgnat public hash table", parse_cmd_cgnat_public_hash},
2372         {"cgnat dump private hash", "<core id> <task id>", "Dump cgnat private hash table", parse_cmd_cgnat_private_hash},
2373         {"delay_us", "<core_id> <task_id> <delay_us>", "Set the delay in usec for the impair mode to <delay_us>", parse_cmd_delay_us},
2374         {"random delay_us", "<core_id> <task_id> <random delay_us>", "Set the delay in usec for the impair mode to <random delay_us>", parse_cmd_random_delay_us},
2375         {"probability", "<core_id> <task_id> <probability>", "Old - Use <proba no drop> instead. Set the percent of forwarded packets for the impair mode", parse_cmd_set_proba_no_drop}, // old - backward compatibility
2376         {"proba no drop", "<core_id> <task_id> <probability>", "Set the percent of forwarded packets for the impair mode", parse_cmd_set_proba_no_drop},
2377         {"proba delay", "<core_id> <task_id> <probability>", "Set the percent of delayed packets for the impair mode", parse_cmd_set_proba_delay},
2378 #if RTE_VERSION >= RTE_VERSION_NUM(19,11,0,0)
2379         {"proba duplicate", "<core_id> <task_id> <probability>", "Set the percent of duplicate packets for the impair mode", parse_cmd_set_proba_duplicate},
2380 #endif
2381         {"version", "", "Show version", parse_cmd_version},
2382         {"join igmp", "<core_id> <task_id> <ip>", "Send igmp membership report for group <ip>", parse_cmd_join_igmp},
2383         {"leave igmp", "<core_id> <task_id>", "Send igmp leave group", parse_cmd_leave_igmp},
2384         {"send unsollicited na", "<core_id> <task_id>", "Send Unsollicited Neighbor Advertisement", parse_cmd_send_unsollicited_na},
2385         {0,0,0,0},
2386 };
2387
2388 static int parse_cmd_help(const char *str, struct input *input)
2389 {
2390         /* str contains the arguments, all commands that have str as a
2391            substring will be shown. */
2392         size_t len, len2, longest_cmd = 0;
2393         for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
2394                 if (longest_cmd <strlen(cmd_strings[i].cmd))
2395                         longest_cmd = strlen(cmd_strings[i].cmd);
2396         }
2397         /* A single call to log will be executed after the help string
2398            has been built. The reason for this is to make use of the
2399            built-in pager. */
2400         char buf[32768] = {0};
2401
2402         for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
2403                 int is_substr = 0;
2404                 const size_t cmd_len = strlen(cmd_strings[i].cmd);
2405                 for (size_t j = 0; j < cmd_len; ++j) {
2406                         is_substr = 1;
2407                         for (size_t k = 0; k < strlen(str); ++k) {
2408                                 if (str[k] != (cmd_strings[i].cmd + j)[k]) {
2409                                         is_substr = 0;
2410                                         break;
2411                                 }
2412                         }
2413                         if (is_substr)
2414                                 break;
2415                 }
2416                 if (!is_substr)
2417                         continue;
2418
2419                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s", cmd_strings[i].cmd);
2420                 len = strlen(cmd_strings[i].cmd);
2421                 while (len < longest_cmd) {
2422                         len++;
2423                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
2424                 }
2425
2426                 if (strlen(cmd_strings[i].args)) {
2427                         char tmp[256] = {0};
2428                         prox_strncpy(tmp, cmd_strings[i].args, 128);
2429                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "Arguments: %s\n", tmp);
2430                         len2 = len;
2431                         if (strlen(cmd_strings[i].help)) {
2432                                 while (len2) {
2433                                         len2--;
2434                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
2435                                 }
2436                         }
2437                 }
2438
2439                 if (strlen(cmd_strings[i].help)) {
2440                         int add = 0;
2441                         const char *h = cmd_strings[i].help;
2442                         do {
2443                                 if (add) {
2444                                         len2 = len;
2445                                         while (len2) {
2446                                                 len2--;
2447                                                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
2448                                         }
2449                                 }
2450                                 char tmp[128] = {0};
2451                                 const size_t max_len = strlen(h) > 80? 80 : strlen(h);
2452                                 size_t len3 = max_len;
2453                                 if (len3 == 80) {
2454                                         while (len3 && h[len3] != ' ')
2455                                                 len3--;
2456                                         if (len3 == 0)
2457                                                 len3 = max_len;
2458                                 }
2459
2460                                 // Use strncpy here and not prox_strncpy. The dest (tmp) has been initialized with 0.
2461                                 // The fact that we are copying 80 characters potentially not null terminated is hence not an issue.
2462                                 // Using prox_strncpy here might cause a PROX_PANIC
2463                                 strncpy(tmp, h, len3);
2464                                 h += len3;
2465                                 while (h[0] == ' ' && strlen(h))
2466                                         h++;
2467
2468                                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s\n", tmp);
2469                                 add = 1;
2470                         } while(strlen(h));
2471                 }
2472                 if (strlen(cmd_strings[i].help) == 0&& strlen(cmd_strings[i].args) == 0) {
2473                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "\n");
2474                 }
2475         }
2476         plog_info("%s", buf);
2477
2478         return 0;
2479 }
2480
2481 const char *cmd_parser_cmd(size_t i)
2482 {
2483         i = i < cmd_parser_n_cmd()? i: cmd_parser_n_cmd();
2484         return cmd_strings[i].cmd;
2485 }
2486
2487 size_t cmd_parser_n_cmd(void)
2488 {
2489         return sizeof(cmd_strings)/sizeof(cmd_strings[0]) - 1;
2490 }
2491
2492 void cmd_parser_parse(const char *str, struct input *input)
2493 {
2494         size_t skip;
2495
2496         for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
2497                 skip = strlen(cmd_strings[i].cmd);
2498                 if (strncmp(cmd_strings[i].cmd, str, skip) == 0 &&
2499                     (str[skip] == ' ' || str[skip] == 0)) {
2500                         while (str[skip] == ' ')
2501                                 skip++;
2502
2503                         if (cmd_strings[i].parse(str + skip, input) != 0) {
2504                                 plog_warn("Invalid syntax for command '%s': %s %s\n",
2505                                           cmd_strings[i].cmd, cmd_strings[i].args, cmd_strings[i].help);
2506                         }
2507                         return ;
2508                 }
2509         }
2510
2511         plog_err("Unknown command: '%s'\n", str);
2512 }