Added support for IMIX through config and command line
[samplevnf.git] / VNFs / DPPD-PROX / cmd_parser.c
1 /*
2 // Copyright (c) 2010-2019 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_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
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_probability(const char *str, struct input *input)
402 {
403         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
404         float probability;
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", &probability) != 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_and_submode(lcore_id, task_id, "impair", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "impair", "l3"))){
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(tbase, probability);
421                         }
422                 }
423         }
424         return 0;
425 }
426
427 static int parse_cmd_delay_us(const char *str, struct input *input)
428 {
429         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, delay_us, nb_cores;
430
431         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
432                 return -1;
433         if (!(str = strchr_skip_twice(str, ' ')))
434                 return -1;
435         if (sscanf(str, "%d", &delay_us) != 1)
436                 return -1;
437
438         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
439                 for (unsigned int i = 0; i < nb_cores; i++) {
440                         lcore_id = lcores[i];
441                         if ((!task_is_mode_and_submode(lcore_id, task_id, "impair", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "impair", "l3"))){
442                                 plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
443                         } else {
444                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
445                                 task_impair_set_delay_us(tbase, delay_us, 0);
446                         }
447                 }
448         }
449         return 0;
450 }
451
452 static int parse_cmd_random_delay_us(const char *str, struct input *input)
453 {
454         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, delay_us, nb_cores;
455
456         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
457                 return -1;
458         if (!(str = strchr_skip_twice(str, ' ')))
459                 return -1;
460         if (sscanf(str, "%d", &delay_us) != 1)
461                 return -1;
462
463         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
464                 for (unsigned int i = 0; i < nb_cores; i++) {
465                         lcore_id = lcores[i];
466                         if ((!task_is_mode_and_submode(lcore_id, task_id, "impair", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "impair", "l3"))){
467                                 plog_err("Core %u task %u is not impairing packets\n", lcore_id, task_id);
468                         } else {
469                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
470                                 task_impair_set_delay_us(tbase, 0, delay_us);
471                         }
472                 }
473         }
474         return 0;
475 }
476
477 static int parse_cmd_bypass(const char *str, struct input *input)
478 {
479         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;
480
481         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
482                 return -1;
483         if ((prox_cfg.flags & DSF_ENABLE_BYPASS) == 0) {
484                 plog_err("enable bypass not set => command not supported\n");
485                 return -1;
486         }
487
488         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
489                 for (unsigned int i = 0; i < nb_cores; i++) {
490                         lcore_id = lcores[i];
491                         if (bypass_task(lcore_id, task_id) != 0)
492                                 return -1;
493                 }
494         }
495         return 0;
496 }
497
498 static int parse_cmd_reconnect(const char *str, struct input *input)
499 {
500         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;
501
502         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
503                 return -1;
504         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
505                 for (unsigned int i = 0; i < nb_cores; i++) {
506                         lcore_id = lcores[i];
507                         if (reconnect_task(lcore_id, task_id) != 0)
508                                 return -1;
509                 }
510         }
511         return 0;
512 }
513
514 static int parse_cmd_pkt_size(const char *str, struct input *input)
515 {
516         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, pkt_size, nb_cores;
517
518         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
519                 return -1;
520         if (!(str = strchr_skip_twice(str, ' ')))
521                 return -1;
522         if (sscanf(str, "%d", &pkt_size) != 1)
523                 return -1;
524
525         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
526                 for (unsigned int i = 0; i < nb_cores; i++) {
527                         lcore_id = lcores[i];
528                         if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
529                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
530                         } else {
531                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
532                                 task_gen_set_pkt_size(tbase, pkt_size); /* error printed within function */
533                         }
534                 }
535         }
536         return 0;
537 }
538
539 static int parse_cmd_imix(const char *str, struct input *input)
540 {
541         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
542         uint32_t pkt_sizes[MAX_IMIX_PKTS], tmp;
543         uint32_t pkt_index = 0;
544
545         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
546                 return -1;
547         if (!(str = strchr_skip_twice(str, ' ')))
548                 return -1;
549         while (pkt_index < MAX_IMIX_PKTS) {
550                 if (sscanf(str, "%d", &pkt_sizes[pkt_index]) != 1)
551                         break;
552                 pkt_index++;
553                 if ((str = strchr(str, ',')) == NULL)
554                         break;
555                 str = str + 1;
556         }
557         if (pkt_index == 0) {
558                 plog_err("No pkt size found\n");
559                 return -1;
560         }
561         if ((pkt_index == MAX_IMIX_PKTS) && (str) && (sscanf(str, "%d", &tmp) == 1)) {
562                 plog_err("Too many inputs - unexpected inputs starting at %s\n", str);
563                 return -1;
564         }
565
566         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
567                 for (unsigned int i = 0; i < nb_cores; i++) {
568                         lcore_id = lcores[i];
569                         if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
570                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
571                         } else {
572                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
573                                 task_gen_set_imix(tbase, pkt_index, pkt_sizes); /* error printed within function */
574                         }
575                 }
576         }
577         return 0;
578 }
579
580 static int parse_cmd_speed(const char *str, struct input *input)
581 {
582         unsigned lcores[RTE_MAX_LCORE], task_id, lcore_id, nb_cores;
583         float speed;
584         unsigned i;
585
586         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
587                 return -1;
588         if (!(str = strchr_skip_twice(str, ' ')))
589                 return -1;
590         if (sscanf(str, "%f", &speed) != 1) {
591                 return -1;
592         }
593
594         if (!cores_task_are_valid(lcores, task_id, nb_cores)) {
595                 return 0;
596         }
597
598         for (i = 0; i < nb_cores; i++) {
599                 lcore_id = lcores[i];
600                 if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
601                         plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
602                 }
603                 else if (speed > 1000.0f || speed < 0.0f) {     // Up to 100 Gbps
604                         plog_err("Speed out of range (must be betweeen 0%% and 1000%%)\n");
605                 }
606                 else {
607                         struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
608                         uint64_t bps = speed * 12500000;
609
610                         plog_info("Setting rate to %"PRIu64" Bps\n", bps);
611
612                         task_gen_set_rate(tbase, bps);
613                 }
614         }
615         return 0;
616 }
617
618 static int parse_cmd_speed_byte(const char *str, struct input *input)
619 {
620         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
621         uint64_t bps;
622
623         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
624                 return -1;
625         if (!(str = strchr_skip_twice(str, ' ')))
626                 return -1;
627         if (sscanf(str, "%"PRIu64"", &bps) != 1)
628                 return -1;
629
630         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
631                 for (unsigned int i = 0; i < nb_cores; i++) {
632                         lcore_id = lcores[i];
633
634                         if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
635                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
636                         }
637                         else if (bps > 12500000000) {   // Up to 100Gbps
638                                 plog_err("Speed out of range (must be <= 12500000000)\n");
639                         }
640                         else {
641                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
642
643                                 plog_info("Setting rate to %"PRIu64" Bps\n", bps);
644                                 task_gen_set_rate(tbase, bps);
645                         }
646                 }
647         }
648         return 0;
649 }
650
651 static int parse_cmd_reset_randoms_all(const char *str, struct input *input)
652 {
653         if (strcmp(str, "") != 0) {
654                 return -1;
655         }
656
657         unsigned task_id, lcore_id = -1;
658         while (prox_core_next(&lcore_id, 0) == 0) {
659                 for (task_id = 0; task_id < lcore_cfg[lcore_id].n_tasks_all; task_id++) {
660                         if ((task_is_mode_and_submode(lcore_id, task_id, "gen", "")) || (task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
661                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
662                                 uint32_t n_rands = task_gen_get_n_randoms(tbase);
663
664                                 plog_info("Resetting randoms on core %d task %d from %d randoms\n", lcore_id, task_id, n_rands);
665                                 task_gen_reset_randoms(tbase);
666                         }
667                 }
668         }
669         return 0;
670 }
671
672 static int parse_cmd_reset_values_all(const char *str, struct input *input)
673 {
674         if (strcmp(str, "") != 0) {
675                 return -1;
676         }
677
678         unsigned task_id, lcore_id = -1;
679         while (prox_core_next(&lcore_id, 0) == 0) {
680                 for (task_id = 0; task_id < lcore_cfg[lcore_id].n_tasks_all; task_id++) {
681                         if ((task_is_mode_and_submode(lcore_id, task_id, "gen", "")) || (task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
682                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
683
684                                 plog_info("Resetting values on core %d task %d\n", lcore_id, task_id);
685                                 task_gen_reset_values(tbase);
686                         }
687                 }
688         }
689         return 0;
690 }
691
692 static int parse_cmd_reset_values(const char *str, struct input *input)
693 {
694         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
695
696         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
697                 return -1;
698
699         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
700                 for (unsigned int i = 0; i < nb_cores; i++) {
701                         lcore_id = lcores[i];
702                         if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
703                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
704                         }
705                         else {
706                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
707
708                                 plog_info("Resetting values on core %d task %d\n", lcore_id, task_id);
709                                 task_gen_reset_values(tbase);
710                         }
711                 }
712         }
713         return 0;
714 }
715
716 static int parse_cmd_set_value(const char *str, struct input *input)
717 {
718         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, value, nb_cores;
719         unsigned short offset;
720         uint8_t value_len;
721
722         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
723                 return -1;
724         if (!(str = strchr_skip_twice(str, ' ')))
725                 return -1;
726         if (sscanf(str, "%hu %u %hhu", &offset, &value, &value_len) != 3) {
727                 return -1;
728         }
729
730         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
731                 for (unsigned int i = 0; i < nb_cores; i++) {
732                         lcore_id = lcores[i];
733                         if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
734                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
735                         }
736                         // do not check offset here - gen knows better than us the maximum frame size
737                         else if (value_len > 4) {
738                                 plog_err("Length out of range (must be less then 4)\n");
739                         }
740                         else {
741                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
742
743                                 if (task_gen_set_value(tbase, value, offset, value_len))
744                                         plog_info("Unable to set Byte %"PRIu16" to %"PRIu8" - invalid offset/len\n", offset, value);
745                                 else
746                                         plog_info("Setting Byte %"PRIu16" to %"PRIu32"\n", offset, value);
747                         }
748                 }
749         }
750         return 0;
751 }
752
753 static int parse_cmd_set_random(const char *str, struct input *input)
754 {
755         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
756         unsigned short offset;
757         uint8_t value_len;
758         char rand_str[64];
759         int16_t rand_id = -1;
760
761         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
762                 return -1;
763         if (!(str = strchr_skip_twice(str, ' ')))
764                 return -1;
765         if (sscanf(str, "%hu %32s %hhu", &offset, rand_str, &value_len) != 3) {
766                 return -1;
767         }
768
769         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
770                 for (unsigned int i = 0; i < nb_cores; i++) {
771                         lcore_id = lcores[i];
772                         if ((!task_is_mode_and_submode(lcore_id, task_id, "gen", "")) && (!task_is_mode_and_submode(lcore_id, task_id, "gen", "l3"))) {
773                                 plog_err("Core %u task %u is not generating packets\n", lcore_id, task_id);
774                         }
775                         else if (offset > PROX_RTE_ETHER_MAX_LEN) {
776                                 plog_err("Offset out of range (must be less then %u)\n", PROX_RTE_ETHER_MAX_LEN);
777                         }
778                         else if (value_len > 4) {
779                                 plog_err("Length out of range (must be less then 4)\n");
780                         } else {
781                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
782
783                                 if (task_gen_add_rand(tbase, rand_str, offset, rand_id)) {
784                                         plog_warn("Random not added on core %u task %u\n", lcore_id, task_id);
785                                 }
786                         }
787                 }
788         }
789         return 0;
790 }
791
792 static int parse_cmd_thread_info(const char *str, struct input *input)
793 {
794         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
795
796         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
797                 return -1;
798         for (unsigned int i = 0; i < nb_cores; i++) {
799                 cmd_thread_info(lcores[i], task_id);
800         }
801         return 0;
802 }
803
804 static int parse_cmd_verbose(const char *str, struct input *input)
805 {
806         unsigned id;
807
808         if (sscanf(str, "%u", &id) != 1) {
809                 return -1;
810         }
811
812         if (plog_set_lvl(id) != 0) {
813                 plog_err("Cannot set log level to %u\n", id);
814         }
815         return 0;
816 }
817
818 static int parse_cmd_arp_add(const char *str, struct input *input)
819 {
820         struct arp_msg amsg;
821         struct arp_msg *pmsg = &amsg;
822         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
823         struct rte_ring *ring;
824
825         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
826                 return -1;
827         if (!(str = strchr_skip_twice(str, ' ')))
828                 return -1;
829         if (strcmp(str, ""))
830                 return -1;
831         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
832                 if (str_to_arp_msg(&amsg, str) == 0) {
833                         for (unsigned int i = 0; i < nb_cores; i++) {
834                                 lcore_id = lcores[i];
835                                 ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
836                                 if (!ring) {
837                                         plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
838                                 }
839                                 else {
840 #if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
841                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1));
842 #else
843                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1, NULL) == 0);
844 #endif
845                                         while (!rte_ring_empty(ring));
846                                 }
847                         }
848                         return 0;
849                 }
850         }
851         return -1;
852 }
853
854 static int parse_cmd_rule_add(const char *str, struct input *input)
855 {
856         struct rte_ring *ring;
857         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
858
859         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
860                 return -1;
861         if (!(str = strchr_skip_twice(str, ' ')))
862                 return -1;
863         if (strcmp(str, ""))
864                 return -1;
865         char *fields[9];
866         char str_cpy[255];
867         prox_strncpy(str_cpy, str, 255);
868         // 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
869         int ret = rte_strsplit(str_cpy, 255, fields, 9, ' ');
870         if (ret != 8) {
871                 return -1;
872         }
873
874         struct acl4_rule rule;
875         struct acl4_rule *prule = &rule;
876         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
877                 if (str_to_rule(&rule, fields, -1, 1) == 0) {
878                         for (unsigned int i = 0; i < nb_cores; i++) {
879                                 lcore_id = lcores[i];
880                                 ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
881                                 if (!ring) {
882                                         plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
883                                 }
884                                 else {
885 #if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
886                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&prule, 1));
887 #else
888                                         while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&prule, 1, NULL) == 0);
889 #endif
890                                         while (!rte_ring_empty(ring));
891                                 }
892                         }
893                         return 0;
894                 }
895         }
896         return -1;
897 }
898
899 static int parse_cmd_gateway_ip(const char *str, struct input *input)
900 {
901         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, ip[4], nb_cores, i;
902
903         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
904                 return -1;
905         if (!(str = strchr_skip_twice(str, ' ')))
906                 return -1;
907         if (!strcmp(str, ""))
908                 return -1;
909         if (sscanf(str, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) {
910                 return -1;
911         }
912         for (i = 0; i < nb_cores; i++) {
913                 lcore_id = lcores[i];
914
915                 if (!task_is_sub_mode(lcore_id, task_id, "l3")) {
916                         plog_err("Core %u task %u is not in l3 mode\n", lcore_id, task_id);
917                 }
918                 else {
919                         uint32_t gateway_ip = ((ip[3] & 0xFF) << 24) | ((ip[2] & 0xFF) << 16) | ((ip[1] & 0xFF) << 8) | ((ip[0] & 0xFF) << 0);
920                         struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
921                         plog_info("Setting gateway ip to %s\n", str);
922                         task_set_gateway_ip(tbase, gateway_ip);
923                 }
924         }
925         return 0;
926 }
927
928 static int parse_cmd_local_ip(const char *str, struct input *input)
929 {
930         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, ip[4], nb_cores, i;
931
932         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
933                 return -1;
934         if (!(str = strchr_skip_twice(str, ' ')))
935                 return -1;
936         if (!strcmp(str, ""))
937                 return -1;
938         if (sscanf(str, "%u.%u.%u.%u", ip, ip + 1, ip + 2, ip + 3) != 4) {
939                 return -1;
940         }
941         for (i = 0; i < nb_cores; i++) {
942                 lcore_id = lcores[i];
943                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
944                 uint32_t local_ip = ((ip[3] & 0xFF) << 24) | ((ip[2] & 0xFF) << 16) | ((ip[1] & 0xFF) << 8) | ((ip[0] & 0xFF) << 0);
945                 if (!task_is_mode_and_submode(lcore_id, task_id, "arp", "local")) {
946                         if (!task_is_sub_mode(lcore_id, task_id, "l3")) {
947                                 plog_err("Core %u task %u is not in l3 mode\n", lcore_id, task_id);
948                         } else {
949                                 plog_info("Setting local ip to %s\n", str);
950                                 task_set_local_ip(tbase, local_ip);
951                         }
952                 } else {
953                         plog_info("Setting local ip to %s\n", str);
954                         task_arp_set_local_ip(tbase, local_ip);
955                 }
956         }
957         return 0;
958 }
959
960 static int parse_cmd_route_add(const char *str, struct input *input)
961 {
962         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, prefix, next_hop_idx, ip[4], nb_cores;
963
964         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
965                 return -1;
966         if (!(str = strchr_skip_twice(str, ' ')))
967                 return -1;
968         if (strcmp(str, ""))
969                 return -1;
970         if (sscanf(str, "%u.%u.%u.%u/%u %u", ip, ip + 1, ip + 2, ip + 3,
971                    &prefix, &next_hop_idx) != 8) {
972                 return -1;
973         }
974         struct rte_ring *ring;
975
976         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
977                 for (unsigned int i = 0; i < nb_cores; i++) {
978                         lcore_id = lcores[i];
979                         ring = ctrl_rings[lcore_id*MAX_TASKS_PER_CORE + task_id];
980                         if (!ring) {
981                                 plog_err("No ring for control messages to core %u task %u\n", lcore_id, task_id);
982                         }
983                         else {
984                                 struct route_msg rmsg;
985                                 struct route_msg *pmsg = &rmsg;
986
987                                 rmsg.ip_bytes[0] = ip[0];
988                                 rmsg.ip_bytes[1] = ip[1];
989                                 rmsg.ip_bytes[2] = ip[2];
990                                 rmsg.ip_bytes[3] = ip[3];
991                                 rmsg.prefix = prefix;
992                                 rmsg.nh = next_hop_idx;
993 #if RTE_VERSION < RTE_VERSION_NUM(17,5,0,1)
994                                 while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1));
995 #else
996                                 while (rte_ring_sp_enqueue_bulk(ring, (void *const *)&pmsg, 1, NULL) == 0);
997 #endif
998                                 while (!rte_ring_empty(ring));
999                         }
1000                 }
1001         }
1002         return 0;
1003 }
1004
1005 static int parse_cmd_start(const char *str, struct input *input)
1006 {
1007         int task_id = -1;
1008
1009         if (strncmp(str, "all", 3) == 0) {
1010                 str += 3;
1011                 sscanf(str, "%d", &task_id);
1012
1013                 start_core_all(task_id);
1014                 req_refresh();
1015                 return 0;
1016         }
1017
1018         uint32_t cores[64] = {0};
1019         int ret;
1020         ret = parse_list_set(cores, str, 64);
1021         if (ret < 0) {
1022                 return -1;
1023         }
1024         str = strchr(str, ' ');
1025
1026         if (str) {
1027                 sscanf(str, "%d", &task_id);
1028         }
1029         start_cores(cores, ret, task_id);
1030         req_refresh();
1031         return 0;
1032 }
1033
1034 static int parse_cmd_stop(const char *str, struct input *input)
1035 {
1036         int task_id = -1;
1037
1038         if (strncmp(str, "all", 3) == 0) {
1039                 str += 3;
1040                 sscanf(str, "%d", &task_id);
1041                 stop_core_all(task_id);
1042                 req_refresh();
1043                 return 0;
1044         }
1045
1046         uint32_t cores[64] = {0};
1047         int ret;
1048         ret = parse_list_set(cores, str, 64);
1049         if (ret < 0) {
1050                 return -1;
1051         }
1052         str = strchr(str, ' ');
1053
1054         if (str) {
1055                 sscanf(str, "%d", &task_id);
1056         }
1057         stop_cores(cores, ret, task_id);
1058         req_refresh();
1059
1060         return 0;
1061 }
1062
1063 static int parse_cmd_rx_distr_start(const char *str, struct input *input)
1064 {
1065         unsigned lcore_id[RTE_MAX_LCORE];
1066
1067         int nb_cores;
1068
1069         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1070
1071         if (nb_cores <= 0) {
1072                 return -1;
1073         }
1074
1075         for (int i = 0; i < nb_cores; ++i)
1076                 cmd_rx_distr_start(lcore_id[i]);
1077         return 0;
1078 }
1079
1080 static int parse_cmd_tx_distr_start(const char *str, struct input *input)
1081 {
1082         unsigned lcore_id[RTE_MAX_LCORE];
1083
1084         int nb_cores;
1085
1086         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1087
1088         if (nb_cores <= 0) {
1089                 return -1;
1090         }
1091
1092         for (int i = 0; i < nb_cores; ++i)
1093                 cmd_tx_distr_start(lcore_id[i]);
1094         return 0;
1095 }
1096
1097 static int parse_cmd_rx_distr_stop(const char *str, struct input *input)
1098 {
1099         unsigned lcore_id[RTE_MAX_LCORE];
1100
1101         int nb_cores;
1102
1103         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1104
1105         if (nb_cores <= 0) {
1106                 return -1;
1107         }
1108
1109         for (int i = 0; i < nb_cores; ++i)
1110                 cmd_rx_distr_stop(lcore_id[i]);
1111         return 0;
1112 }
1113
1114 static int parse_cmd_tx_distr_stop(const char *str, struct input *input)
1115 {
1116         unsigned lcore_id[RTE_MAX_LCORE];
1117
1118         int nb_cores;
1119
1120         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1121
1122         if (nb_cores <= 0) {
1123                 return -1;
1124         }
1125
1126         for (int i = 0; i < nb_cores; ++i)
1127                 cmd_tx_distr_stop(lcore_id[i]);
1128         return 0;
1129 }
1130
1131 static int parse_cmd_rx_distr_reset(const char *str, struct input *input)
1132 {
1133         unsigned lcore_id[RTE_MAX_LCORE];
1134
1135         int nb_cores;
1136
1137         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1138
1139         if (nb_cores <= 0) {
1140                 return -1;
1141         }
1142
1143         for (int i = 0; i < nb_cores; ++i)
1144                 cmd_rx_distr_rst(lcore_id[i]);
1145         return 0;
1146 }
1147
1148 static int parse_cmd_tx_distr_reset(const char *str, struct input *input)
1149 {
1150         unsigned lcore_id[RTE_MAX_LCORE];
1151
1152         int nb_cores;
1153
1154         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1155
1156         if (nb_cores <= 0) {
1157                 return -1;
1158         }
1159
1160         for (int i = 0; i < nb_cores; ++i)
1161                 cmd_tx_distr_rst(lcore_id[i]);
1162         return 0;
1163 }
1164
1165 static int parse_cmd_rx_distr_show(const char *str, struct input *input)
1166 {
1167         unsigned lcore_id[RTE_MAX_LCORE];
1168
1169         int nb_cores;
1170
1171         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1172
1173         if (nb_cores <= 0) {
1174                 return -1;
1175         }
1176
1177         for (int i = 0; i < nb_cores; ++i)
1178                 cmd_rx_distr_show(lcore_id[i]);
1179         return 0;
1180 }
1181
1182 static int parse_cmd_tx_distr_show(const char *str, struct input *input)
1183 {
1184         unsigned lcore_id[RTE_MAX_LCORE];
1185
1186         int nb_cores;
1187
1188         nb_cores = parse_list_set(lcore_id, str, sizeof(lcore_id)/sizeof(lcore_id[0]));
1189
1190         if (nb_cores <= 0) {
1191                 return -1;
1192         }
1193
1194         for (int i = 0; i < nb_cores; ++i)
1195                 cmd_tx_distr_show(lcore_id[i]);
1196         return 0;
1197 }
1198
1199 static int parse_cmd_tot_stats(const char *str, struct input *input)
1200 {
1201         if (strcmp("", str) != 0) {
1202                 return -1;
1203         }
1204
1205         struct global_stats_sample *gsl = stats_get_global_stats(1);
1206         uint64_t tot_rx = gsl->host_rx_packets;
1207         uint64_t tot_tx = gsl->host_tx_packets;
1208         uint64_t last_tsc = gsl->tsc;
1209
1210         if (input->reply) {
1211                 char buf[128];
1212                 snprintf(buf, sizeof(buf), "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
1213                          tot_rx, tot_tx, last_tsc, rte_get_tsc_hz());
1214                 input->reply(input, buf, strlen(buf));
1215         }
1216         else {
1217                 plog_info("RX: %"PRIu64", TX: %"PRIu64"\n", tot_rx, tot_tx);
1218         }
1219         return 0;
1220 }
1221
1222 static int parse_cmd_update_interval(const char *str, struct input *input)
1223 {
1224         unsigned val;
1225
1226         if (sscanf(str, "%u", &val) != 1) {
1227                 return -1;
1228         }
1229
1230         if (val == 0) {
1231                 plog_err("Minimum update interval is 1 ms\n");
1232         }
1233         else {
1234                 plog_info("Setting update interval to %d ms\n", val);
1235                 set_update_interval(val);
1236         }
1237         return 0;
1238 }
1239
1240 static int parse_cmd_mem_info(const char *str, struct input *input)
1241 {
1242         if (strcmp("", str) != 0) {
1243                 return -1;
1244         }
1245
1246         cmd_mem_stats();
1247         cmd_mem_layout();
1248         return 0;
1249 }
1250
1251 static int parse_cmd_tot_ierrors_tot(const char *str, struct input *input)
1252 {
1253         if (strcmp(str, "") != 0) {
1254                 return -1;
1255         }
1256
1257         struct global_stats_sample *gsl = stats_get_global_stats(1);
1258         uint64_t tot = gsl->nics_ierrors;
1259         uint64_t last_tsc = gsl->tsc;
1260
1261         if (input->reply) {
1262                 char buf[128];
1263                 snprintf(buf, sizeof(buf),
1264                          "%"PRIu64",%"PRIu64",%"PRIu64"\n",
1265                          tot, last_tsc, rte_get_tsc_hz());
1266                 input->reply(input, buf, strlen(buf));
1267         }
1268         else {
1269                 plog_info("ierrors: %"PRIu64"\n", tot);
1270         }
1271         return 0;
1272 }
1273
1274 static int parse_cmd_tot_imissed_tot(const char *str, struct input *input)
1275 {
1276         if (strcmp(str, "") != 0) {
1277                 return -1;
1278         }
1279
1280         struct global_stats_sample *gsl = stats_get_global_stats(1);
1281         uint64_t tot = gsl->nics_imissed;
1282         uint64_t last_tsc = gsl->tsc;
1283
1284         if (input->reply) {
1285                 char buf[128];
1286                 snprintf(buf, sizeof(buf),
1287                          "%"PRIu64",%"PRIu64",%"PRIu64"\n",
1288                          tot, last_tsc, rte_get_tsc_hz());
1289                 input->reply(input, buf, strlen(buf));
1290         }
1291         else {
1292                 plog_info("imissed: %"PRIu64"\n", tot);
1293         }
1294         return 0;
1295 }
1296
1297 static int parse_cmd_enable_multicast(const char *str, struct input *input)
1298 {
1299         uint8_t port_id;
1300         prox_rte_ether_addr mac;
1301
1302         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) {
1303                 return -1;
1304         }
1305         cmd_multicast(port_id, 1, &mac);
1306         return 0;
1307 }
1308
1309 static int parse_cmd_disable_multicast(const char *str, struct input *input)
1310 {
1311         uint8_t port_id;
1312         prox_rte_ether_addr mac;
1313
1314         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) {
1315                 return -1;
1316         }
1317
1318         cmd_multicast(port_id, 0, &mac);
1319         return 0;
1320 }
1321
1322 static int parse_cmd_reset_port(const char *str, struct input *input)
1323 {
1324         uint32_t port_id;
1325
1326         if (sscanf(str, "%u", &port_id ) != 1) {
1327                 return -1;
1328         }
1329
1330         cmd_reset_port(port_id);
1331         return 0;
1332 }
1333
1334 static int parse_cmd_write_reg(const char *str, struct input *input)
1335 {
1336         uint32_t port_id;
1337         uint32_t id, val;
1338
1339         if (sscanf(str, "%u %x %u", &port_id, &id, &val) != 3) {
1340                 return -1;
1341         }
1342
1343         cmd_write_reg(port_id, id, val);
1344         return 0;
1345 }
1346
1347 static int parse_cmd_read_reg(const char *str, struct input *input)
1348 {
1349         uint32_t port_id;
1350         uint32_t id;
1351
1352         if (sscanf(str, "%u %x", &port_id, &id) != 2) {
1353                 return -1;
1354         }
1355
1356         cmd_read_reg(port_id, id);
1357         return 0;
1358 }
1359
1360 static int parse_cmd_cache_reset(const char *str, struct input *input)
1361 {
1362         cmd_cache_reset();
1363         return 0;
1364 }
1365
1366 static int parse_cmd_set_cache_class_mask(const char *str, struct input *input)
1367 {
1368         uint32_t lcore_id;
1369         uint32_t set;
1370         uint32_t val;
1371
1372         if (sscanf(str, "%u %u %u", &lcore_id, &set, &val) != 3) {
1373                 return -1;
1374         }
1375
1376         cmd_set_cache_class_mask(lcore_id, set, val);
1377         return 0;
1378 }
1379
1380 static int parse_cmd_set_cache_class(const char *str, struct input *input)
1381 {
1382         uint32_t lcore_id;
1383         uint32_t set;
1384
1385         if (sscanf(str, "%u %u", &lcore_id, &set) != 2) {
1386                 return -1;
1387         }
1388
1389         cmd_set_cache_class(lcore_id, set);
1390         return 0;
1391 }
1392
1393 static int parse_cmd_get_cache_class_mask(const char *str, struct input *input)
1394 {
1395         uint32_t lcore_id;
1396         uint32_t set;
1397         uint32_t val = 0;
1398
1399         if (sscanf(str, "%u %u", &lcore_id, &set) != 2) {
1400                 return -1;
1401         }
1402
1403         cmd_get_cache_class_mask(lcore_id, set, &val);
1404         if (input->reply) {
1405                 char buf[128];
1406                 snprintf(buf, sizeof(buf), "%d, %d, %x\n", lcore_id, set, val);
1407                 input->reply(input, buf, strlen(buf));
1408         } else {
1409                 plog_info("core=%d, set=%d, mask=%x\n", lcore_id, set, val);
1410         }
1411         return 0;
1412 }
1413
1414 static int parse_cmd_get_cache_class(const char *str, struct input *input)
1415 {
1416         uint32_t lcore_id;
1417         uint32_t set;
1418         uint32_t val;
1419
1420         if (sscanf(str, "%u", &lcore_id) != 1) {
1421                 return -1;
1422         }
1423
1424         cmd_get_cache_class(lcore_id, &set);
1425         if (input->reply) {
1426                 char buf[128];
1427                 snprintf(buf, sizeof(buf), "%d, %d\n", lcore_id, set);
1428                 input->reply(input, buf, strlen(buf));
1429         } else {
1430                 plog_info("core=%d, cos=%d\n", lcore_id, set);
1431         }
1432         return 0;
1433 }
1434
1435 static int parse_cmd_get_cache_mask(const char *str, struct input *input)
1436 {
1437         uint32_t lcore_id;
1438         uint32_t set;
1439         uint32_t mask;
1440
1441         if (sscanf(str, "%u", &lcore_id) != 1) {
1442                 return -1;
1443         }
1444
1445         cmd_get_cache_class(lcore_id, &set);
1446         cmd_get_cache_class_mask(lcore_id, set, &mask);
1447         if (input->reply) {
1448                 char buf[128];
1449                 snprintf(buf, sizeof(buf), "%d, %x\n", lcore_id, mask);
1450                 input->reply(input, buf, strlen(buf));
1451         } else {
1452                 plog_info("core=%d, mask=%x\n", lcore_id, mask);
1453         }
1454         return 0;
1455 }
1456
1457 static int parse_cmd_set_vlan_offload(const char *str, struct input *input)
1458 {
1459         uint32_t port_id;
1460         uint32_t val;
1461
1462         if (sscanf(str, "%u %u", &port_id, &val) != 2) {
1463                 return -1;
1464         }
1465
1466         cmd_set_vlan_offload(port_id, val);
1467         return 0;
1468 }
1469
1470 static int parse_cmd_set_vlan_filter(const char *str, struct input *input)
1471 {
1472         uint32_t port_id;
1473         uint32_t id, val;
1474
1475         if (sscanf(str, "%u %d %u", &port_id, &id, &val) != 3) {
1476                 return -1;
1477         }
1478
1479         cmd_set_vlan_filter(port_id, id, val);
1480         return 0;
1481 }
1482
1483 static int parse_cmd_ring_info_all(const char *str, struct input *input)
1484 {
1485         if (strcmp(str, "") != 0) {
1486                 return -1;
1487         }
1488         cmd_ringinfo_all();
1489         return 0;
1490 }
1491
1492 static int parse_cmd_port_up(const char *str, struct input *input)
1493 {
1494         unsigned val;
1495
1496         if (sscanf(str, "%u", &val) != 1) {
1497                 return -1;
1498         }
1499
1500         cmd_port_up(val);
1501         return 0;
1502 }
1503
1504 static int parse_cmd_port_down(const char *str, struct input *input)
1505 {
1506         unsigned val;
1507
1508         if (sscanf(str, "%u", &val) != 1) {
1509                 return -1;
1510         }
1511
1512         cmd_port_down(val);
1513         return 0;
1514 }
1515
1516 static int parse_cmd_port_link_state(const char *str, struct input *input)
1517 {
1518         unsigned val;
1519
1520         if (sscanf(str, "%u", &val) != 1) {
1521                 return -1;
1522         }
1523
1524         if (!port_is_active(val))
1525                 return -1;
1526
1527         int active = prox_port_cfg[val].link_up;
1528         const char *state = active? "up\n" : "down\n";
1529
1530         if (input->reply)
1531                 input->reply(input, state, strlen(state));
1532         else
1533                 plog_info("%s", state);
1534
1535         return 0;
1536 }
1537
1538 static int parse_cmd_xstats(const char *str, struct input *input)
1539 {
1540         unsigned val;
1541
1542         if (sscanf(str, "%u", &val) != 1) {
1543                 return -1;
1544         }
1545
1546         cmd_xstats(val);
1547         return 0;
1548 }
1549
1550 static int parse_cmd_stats(const char *str, struct input *input)
1551 {
1552         if (strcmp(str, "") == 0)
1553                 return -1;
1554
1555         char buf[32768];
1556         char ret2[32768];
1557         char *ret = ret2;
1558         int list = 0;
1559
1560         prox_strncpy(buf, str, sizeof(buf) - 1);
1561         char *tok;
1562         uint64_t stat_val;
1563
1564         while ((tok = strchr(str, ','))) {
1565                 *tok = 0;
1566                 stat_val = stats_parser_get(str);
1567
1568                 ret += sprintf(ret, "%s%"PRIu64"", list? "," :"", stat_val);
1569                 list = 1;
1570                 str = tok + 1;
1571         }
1572
1573         stat_val = stats_parser_get(str);
1574         ret += sprintf(ret, "%s%"PRIu64"", list? "," :"", stat_val);
1575
1576         sprintf(ret, "\n");
1577
1578         if (input->reply)
1579                 input->reply(input, ret2, strlen(ret2));
1580         else
1581                 plog_info("%s", ret2);
1582         return 0;
1583 }
1584
1585 static void replace_char(char *str, char to_replace, char by)
1586 {
1587         for (size_t i = 0; str[i] != '\0'; ++i) {
1588                 if (str[i] == to_replace)
1589                         str[i] = by;
1590         }
1591 }
1592
1593 static int parse_cmd_port_info(const char *str, struct input *input)
1594 {
1595         int val;
1596
1597         if (strcmp(str, "all") == 0) {
1598                 val = -1;
1599         }
1600         else if (sscanf(str, "%d", &val) != 1) {
1601                 return -1;
1602         }
1603
1604         char port_info[2048];
1605
1606         cmd_portinfo(val, port_info, sizeof(port_info));
1607
1608         if (input->reply) {
1609                 replace_char(port_info, '\n', ',');
1610                 port_info[strlen(port_info) - 1] = '\n';
1611                 input->reply(input, port_info, strlen(port_info));
1612         } else
1613                 plog_info("%s", port_info);
1614
1615         return 0;
1616 }
1617
1618 static int parse_cmd_ring_info(const char *str, struct input *input)
1619 {
1620         unsigned lcores[RTE_MAX_LCORE], task_id, nb_cores;
1621
1622         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1623                 return -1;
1624
1625         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1626                 for (unsigned int i = 0; i < nb_cores; i++) {
1627                         cmd_ringinfo(lcores[i], task_id);
1628                 }
1629         }
1630         return 0;
1631 }
1632
1633 static int parse_cmd_port_stats(const char *str, struct input *input)
1634 {
1635         unsigned val;
1636
1637         if (sscanf(str, "%u", &val) != 1) {
1638                 return -1;
1639         }
1640
1641         struct get_port_stats s;
1642         if (stats_port(val, &s)) {
1643                 plog_err("Invalid port %u\n", val);
1644                 return 0;
1645         }
1646         char buf[256];
1647         snprintf(buf, sizeof(buf),
1648                  "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
1649                  "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64","
1650                  "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
1651                  s.no_mbufs_diff, s.ierrors_diff + s.imissed_diff,
1652                  s.rx_bytes_diff, s.tx_bytes_diff,
1653                  s.rx_pkts_diff, s.tx_pkts_diff,
1654                  s.rx_tot, s.tx_tot,
1655                  s.no_mbufs_tot, s.ierrors_tot + s.imissed_tot,
1656                  s.last_tsc, s.prev_tsc);
1657         plog_info("%s", buf);
1658         if (input->reply)
1659                 input->reply(input, buf, strlen(buf));
1660         return 0;
1661 }
1662
1663 static int parse_cmd_multi_port_stats(const char *str, struct input *input)
1664 {
1665         uint32_t ports[PROX_MAX_PORTS];
1666         int nb_ports = parse_list_set(ports, str, PROX_MAX_PORTS);
1667         if (nb_ports <= 0) {
1668                 return -1;
1669         }
1670
1671         char buf[PROX_MAX_PORTS * (11+5*21) + 1], *pbuf = buf;
1672         int left = sizeof(buf);
1673         for (int i = 0; i < nb_ports; ++i) {
1674                 struct get_port_stats s;
1675                 if (stats_port(ports[i], &s)) {
1676                         plog_err("Invalid port %u\n", ports[i]);
1677                         return 0;
1678                 }
1679
1680                 int len = snprintf(pbuf, left,
1681                                 "%u,"
1682                                 "%"PRIu64",%"PRIu64","
1683                                 "%"PRIu64",%"PRIu64","
1684                                 "%"PRIu64";",
1685                                 //TODO: adjust buf size above when adding fields
1686                                 ports[i],
1687                                 s.rx_tot, s.tx_tot,
1688                                 s.no_mbufs_tot, s.ierrors_tot + s.imissed_tot,
1689                                 s.last_tsc);
1690                 if ((len < 0) || (len >= left)) {
1691                         plog_err("Cannot print stats for port %u\n", ports[i]);
1692                         return 0;
1693                 }
1694                 pbuf += len;
1695                 left -= len;
1696         }
1697         pbuf--;
1698         *pbuf = '\n';
1699
1700         plog_info("%s", buf);
1701         if (input->reply)
1702                 input->reply(input, buf, sizeof(buf) - left);
1703         return 0;
1704 }
1705
1706 static int parse_cmd_core_stats(const char *str, struct input *input)
1707 {
1708         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
1709
1710         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1711                 return -1;
1712
1713         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1714                 for (unsigned int i = 0; i < nb_cores; i++) {
1715                         lcore_id = lcores[i];
1716                         uint64_t tot_rx = stats_core_task_tot_rx(lcore_id, task_id);
1717                         uint64_t tot_tx = stats_core_task_tot_tx(lcore_id, task_id);
1718                         uint64_t tot_drop = stats_core_task_tot_drop(lcore_id, task_id);
1719                         uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);
1720
1721                         if (input->reply) {
1722                                 char buf[128];
1723                                 snprintf(buf, sizeof(buf),
1724                                         "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64"\n",
1725                                         tot_rx, tot_tx, tot_drop, last_tsc, rte_get_tsc_hz());
1726                                 input->reply(input, buf, strlen(buf));
1727                         }
1728                         else {
1729                                 plog_info("RX: %"PRIu64", TX: %"PRIu64", DROP: %"PRIu64"\n",
1730                                         tot_rx, tot_tx, tot_drop);
1731                         }
1732                 }
1733         }
1734         return 0;
1735 }
1736
1737 typedef void (*parser_handler)(unsigned, unsigned, struct input *);
1738 static int handle_cores_tasks(const char *str, struct input *input, const char *mode_str, const char *mode_name, parser_handler f)
1739 {
1740         // This function either outputs a single line, in case of syntax error on the lists of cores and/or tasks
1741         // or outputs (nb_cores * nb_tasks) lines, one line for each core/task pair:
1742         // - if the core/task pair is invalid, the output line reports an error
1743         // - otherwise, the output line provides the latency statistics for the core/task pair
1744
1745         unsigned lcores[RTE_MAX_LCORE], tasks[MAX_TASKS_PER_CORE], lcore_id, task_id, nb_cores, nb_tasks;
1746         if (parse_cores_tasks(str, lcores, tasks, &nb_cores, &nb_tasks)) {
1747                 if (input->reply) {
1748                         char buf[128];
1749                         snprintf(buf, sizeof(buf), "error: invalid syntax\n");
1750                         input->reply(input, buf, strlen(buf));
1751                 }
1752                 return -1;
1753         }
1754
1755         for (unsigned int i = 0; i < nb_cores; i++) {
1756                 for (unsigned int j = 0; j < nb_tasks; j++) {
1757                         lcore_id = lcores[i];
1758                         task_id = tasks[j];
1759                         if (core_task_is_valid(lcore_id, task_id) == 0) {
1760                                 if (input->reply) {
1761                                         char buf[128];
1762                                         snprintf(buf, sizeof(buf), "error: invalid core %u, task %u\n", lcore_id, task_id);
1763                                         input->reply(input, buf, strlen(buf));
1764                                 } else {
1765                                         plog_info("error: invalid core %u, task %u\n", lcore_id, task_id);
1766                                 }
1767                                 continue;
1768                         }
1769                         if ((mode_str) && (!task_is_mode(lcore_id, task_id, mode_str))) {
1770                                 if (input->reply) {
1771                                         char buf[128];
1772                                         snprintf(buf, sizeof(buf), "error: core %u task %u is not measuring %s\n", lcore_id, task_id, mode_name);
1773                                         input->reply(input, buf, strlen(buf));
1774                                 } else {
1775                                         plog_info("error: core %u task %u is not measuring %s\n", lcore_id, task_id, mode_name);
1776                                 }
1777                                 continue;
1778                         }
1779                         f(lcore_id, task_id, input);
1780                 }
1781         }
1782         return 0;
1783 }
1784
1785 static void handle_dp_core_stats(unsigned lcore_id, unsigned task_id, struct input *input)
1786 {
1787         uint64_t tot_rx = stats_core_task_tot_rx(lcore_id, task_id);
1788         uint64_t tot_tx = stats_core_task_tot_tx(lcore_id, task_id);
1789         uint64_t tot_tx_fail = stats_core_task_tot_tx_fail(lcore_id, task_id);
1790         uint64_t tot_rx_non_dp = stats_core_task_tot_rx_non_dp(lcore_id, task_id);
1791         uint64_t tot_tx_non_dp = stats_core_task_tot_tx_non_dp(lcore_id, task_id);
1792         uint64_t tot_drop = stats_core_task_tot_drop(lcore_id, task_id);
1793         uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);
1794
1795         if (input->reply) {
1796                 char buf[128];
1797                 snprintf(buf, sizeof(buf),
1798                         "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%u,%u\n",
1799                         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);
1800                 input->reply(input, buf, strlen(buf));
1801         }
1802         else {
1803                 plog_info("core: %u, task: %u, RX: %"PRIu64", TX: %"PRIu64", RX_NON_DP: %"PRIu64", TX_NON_DP: %"PRIu64", DROP: %"PRIu64", TX_FAIL: %"PRIu64"\n",
1804                         lcore_id, task_id, tot_rx, tot_tx, tot_rx_non_dp, tot_tx_non_dp, tot_drop, tot_tx_fail);
1805         }
1806 }
1807
1808 static void handle_lat_stats(unsigned lcore_id, unsigned task_id, struct input *input)
1809 {
1810         struct stats_latency *stats = stats_latency_find(lcore_id, task_id);
1811         struct stats_latency *tot = stats_latency_tot_find(lcore_id, task_id);
1812         if (!stats || !tot) {
1813                 if (input->reply) {
1814                         char buf[128];
1815                         snprintf(buf, sizeof(buf),
1816                                  "error: core %u task %u stats = %p tot = %p\n",
1817                                  lcore_id, task_id, stats, tot);
1818                         input->reply(input, buf, strlen(buf));
1819                 } else {
1820                         plog_info("error: core %u task %u stats = %p tot = %p\n",
1821                                   lcore_id, task_id, stats, tot);
1822                 }
1823                 return;
1824         }
1825
1826         uint64_t last_tsc = stats_core_task_last_tsc(lcore_id, task_id);
1827         uint64_t lat_min_usec = time_unit_to_usec(&stats->min.time);
1828         uint64_t lat_max_usec = time_unit_to_usec(&stats->max.time);
1829         uint64_t tot_lat_min_usec = time_unit_to_usec(&tot->min.time);
1830         uint64_t tot_lat_max_usec = time_unit_to_usec(&tot->max.time);
1831         uint64_t lat_avg_usec = time_unit_to_usec(&stats->avg.time);
1832
1833         if (input->reply) {
1834                 char buf[128];
1835                 snprintf(buf, sizeof(buf),
1836                          "%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%"PRIu64",%u,%u\n",
1837                          lat_min_usec,
1838                          lat_max_usec,
1839                          lat_avg_usec,
1840                          tot_lat_min_usec,
1841                          tot_lat_max_usec,
1842                          last_tsc,
1843                          rte_get_tsc_hz(),
1844                          lcore_id,
1845                          task_id);
1846                 input->reply(input, buf, strlen(buf));
1847         }
1848         else {
1849                 plog_info("core: %u, task: %u, min: %"PRIu64", max: %"PRIu64", avg: %"PRIu64", min since reset: %"PRIu64", max since reset: %"PRIu64"\n",
1850                           lcore_id,
1851                           task_id,
1852                           lat_min_usec,
1853                           lat_max_usec,
1854                           lat_avg_usec,
1855                           tot_lat_min_usec,
1856                           tot_lat_max_usec);
1857         }
1858 }
1859
1860 #ifdef LATENCY_HISTOGRAM
1861 static void handle_latency_histogram(unsigned lcore_id, unsigned task_id, struct input *input)
1862 {
1863         uint64_t *buckets;
1864
1865         stats_core_lat_histogram(lcore_id, task_id, &buckets);
1866
1867         if (buckets == NULL) {
1868                 if (input->reply) {
1869                         char buf[128];
1870                         snprintf(buf, sizeof(buf), "error: unexpected NULL bucket\n");
1871                         input->reply(input, buf, strlen(buf));
1872                 }
1873                 return;
1874         }
1875
1876         if (input->reply) {
1877                 char buf[4096] = {0};
1878                 for (size_t i = 0; i < LAT_BUCKET_COUNT; i++)
1879                         sprintf(buf+strlen(buf), "Bucket [%zu]: %"PRIu64"\n", i, buckets[i]);
1880                 input->reply(input, buf, strlen(buf));
1881         }
1882         else {
1883                 for (size_t i = 0; i < LAT_BUCKET_COUNT; i++)
1884                         if (buckets[i])
1885                                 plog_info("Bucket [%zu]: %"PRIu64"\n", i, buckets[i]);
1886         }
1887 }
1888
1889 static void handle_stats_and_packets(unsigned lcore_id, unsigned task_id, struct input *input)
1890 {
1891         handle_lat_stats(lcore_id, task_id, input);
1892         handle_latency_histogram(lcore_id, task_id, input);
1893 }
1894 #endif
1895
1896 static int parse_cmd_dp_core_stats(const char *str, struct input *input)
1897 {
1898         handle_cores_tasks(str, input, NULL, NULL, handle_dp_core_stats);
1899         return 0;
1900 }
1901
1902 static int parse_cmd_lat_stats(const char *str, struct input *input)
1903 {
1904         handle_cores_tasks(str, input, "lat", "latency", handle_lat_stats);
1905         return 0;
1906 }
1907
1908 static int parse_cmd_lat_packets(const char *str, struct input *input)
1909 {
1910 #ifdef LATENCY_HISTOGRAM
1911         handle_cores_tasks(str, input, "lat", "latency", handle_latency_histogram);
1912 #else
1913         if (input->reply) {
1914                 char buf[128];
1915                 snprintf(buf, sizeof(buf), "error: invalid syntax (LATENCY_HISTOGRAM disabled)\n");
1916                 input->reply(input, buf, strlen(buf));
1917         } else {
1918                 plog_info("LATENCY_HISTOGRAMS disabled\n");
1919         }
1920 #endif
1921         return 0;
1922 }
1923
1924 static int parse_cmd_lat_stats_and_packets(const char *str, struct input *input)
1925 {
1926 #ifdef LATENCY_HISTOGRAM
1927         handle_cores_tasks(str, input, "lat", "latency", handle_stats_and_packets);
1928 #else
1929         if (input->reply) {
1930                 char buf[128];
1931                 snprintf(buf, sizeof(buf), "error: invalid syntax (LATENCY_HISTOGRAMS disabled)\n");
1932                 input->reply(input, buf, strlen(buf));
1933         } else {
1934                 plog_info("LATENCY_HISTOGRAMS disabled\n");
1935         }
1936 #endif
1937         return 0;
1938 }
1939
1940 static int parse_cmd_show_irq_buckets(const char *str, struct input *input)
1941 {
1942         char buf[4096] = {0};
1943         unsigned int i, c;
1944         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
1945
1946         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1947                 return -1;
1948
1949         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1950                 for (c = 0; c < nb_cores; c++) {
1951                         lcore_id = lcores[c];
1952                         get_irq_buckets_by_core_task(buf, lcore_id, task_id);
1953                         plog_info("%s", buf);
1954                         if (input->reply)
1955                                 input->reply(input, buf, strlen(buf));
1956                         buf[0] = 0;
1957                 }
1958         }
1959         return 0;
1960 }
1961
1962 static int parse_cmd_irq(const char *str, struct input *input)
1963 {
1964         unsigned int i, c;
1965         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
1966
1967         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1968                 return -1;
1969
1970         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1971                 for (c = 0; c < nb_cores; c++) {
1972                         lcore_id = lcores[c];
1973                         if (!task_is_mode(lcore_id, task_id, "irq")) {
1974                                 plog_err("Core %u task %u is not in irq mode\n", lcore_id, task_id);
1975                         } else {
1976                                 struct task_irq *task_irq = (struct task_irq *)(lcore_cfg[lcore_id].tasks_all[task_id]);
1977
1978                                 task_irq_show_stats(task_irq, input);
1979                         }
1980                 }
1981         }
1982         return 0;
1983 }
1984
1985 static int parse_cmd_cgnat_public_hash(const char *str, struct input *input)
1986 {
1987         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
1988
1989         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
1990                 return -1;
1991
1992         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
1993                 for (unsigned int i = 0; i < nb_cores; i++) {
1994                         lcore_id = lcores[i];
1995
1996                         if (!task_is_mode(lcore_id, task_id, "cgnat")) {
1997                                 plog_err("Core %u task %u is not cgnat\n", lcore_id, task_id);
1998                         }
1999                         else {
2000                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2001                                 task_cgnat_dump_public_hash((struct task_nat *)tbase);
2002                         }
2003                 }
2004         }
2005         return 0;
2006 }
2007
2008 static int parse_cmd_cgnat_private_hash(const char *str, struct input *input)
2009 {
2010         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2011         uint32_t val;
2012
2013         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2014                 return -1;
2015
2016         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2017                 for (unsigned int i = 0; i < nb_cores; i++) {
2018                         lcore_id = lcores[i];
2019
2020                         if (!task_is_mode(lcore_id, task_id, "cgnat")) {
2021                                 plog_err("Core %u task %u is not cgnat\n", lcore_id, task_id);
2022                         }
2023                         else {
2024                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2025                                 task_cgnat_dump_private_hash((struct task_nat *)tbase);
2026                         }
2027                 }
2028         }
2029         return 0;
2030 }
2031
2032 static int parse_cmd_accuracy(const char *str, struct input *input)
2033 {
2034         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2035         uint32_t val;
2036
2037         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2038                 return -1;
2039         if (!(str = strchr_skip_twice(str, ' ')))
2040                 return -1;
2041         if (sscanf(str, "%"PRIu32"", &val) != 1)
2042                 return -1;
2043
2044         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2045                 for (unsigned int i = 0; i < nb_cores; i++) {
2046                         lcore_id = lcores[i];
2047
2048                         if (!task_is_mode(lcore_id, task_id, "lat")) {
2049                                 plog_err("Core %u task %u is not measuring latency\n", lcore_id, task_id);
2050                         }
2051                         else {
2052                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2053
2054                                 task_lat_set_accuracy_limit((struct task_lat *)tbase, val);
2055                         }
2056                 }
2057         }
2058         return 0;
2059 }
2060
2061 static int parse_cmd_leave_igmp(const char *str, struct input *input)
2062 {
2063         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2064
2065         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2066                 return -1;
2067
2068         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2069                 for (unsigned int i = 0; i < nb_cores; i++) {
2070                         lcore_id = lcores[i];
2071
2072                         if (!task_is_mode(lcore_id, task_id, "swap")) {
2073                                 plog_err("Core %u task %u is not running swap\n", lcore_id, task_id);
2074                         }
2075                         else {
2076                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2077                                 igmp_leave_group(tbase);
2078                         }
2079                 }
2080         }
2081         return 0;
2082 }
2083
2084 static int parse_cmd_join_igmp(const char *str, struct input *input)
2085 {
2086         unsigned lcores[RTE_MAX_LCORE], lcore_id, task_id, nb_cores;
2087         uint32_t igmp_ip;
2088         uint8_t *igmp_bytes = (uint8_t *)&igmp_ip;
2089
2090         if (parse_cores_task(str, lcores, &task_id, &nb_cores))
2091                 return -1;
2092         if (!(str = strchr_skip_twice(str, ' ')))
2093                 return -1;
2094         if (sscanf(str, "%hhu.%hhu.%hhu.%hhu", igmp_bytes, igmp_bytes + 1, igmp_bytes + 2, igmp_bytes + 3) != 4) {
2095                 return -1;
2096         }
2097         if (cores_task_are_valid(lcores, task_id, nb_cores)) {
2098                 for (unsigned int i = 0; i < nb_cores; i++) {
2099                         lcore_id = lcores[i];
2100
2101                         if (!task_is_mode(lcore_id, task_id, "swap")) {
2102                                 plog_err("Core %u task %u is not running swap\n", lcore_id, task_id);
2103                         }
2104                         else {
2105                                 struct task_base *tbase = lcore_cfg[lcore_id].tasks_all[task_id];
2106                                 igmp_join_group(tbase, igmp_ip);
2107                         }
2108                 }
2109         }
2110         return 0;
2111 }
2112
2113 static int parse_cmd_rx_tx_info(const char *str, struct input *input)
2114 {
2115         if (strcmp(str, "") != 0) {
2116                 return -1;
2117         }
2118
2119         cmd_rx_tx_info();
2120         return 0;
2121 }
2122
2123 static int parse_cmd_version(const char *str, struct input *input)
2124 {
2125         if (strcmp(str, "") != 0) {
2126                 return -1;
2127         }
2128
2129         if (input->reply) {
2130                 uint64_t version =
2131                         ((uint64_t)VERSION_MAJOR) << 24 |
2132                         ((uint64_t)VERSION_MINOR) << 16 |
2133                         ((uint64_t)VERSION_REV) << 8;
2134
2135                 char buf[128];
2136                 snprintf(buf, sizeof(buf), "%"PRIu64",%"PRIu64"\n", version, (uint64_t)RTE_VERSION);
2137                 input->reply(input, buf, strlen(buf));
2138         }
2139         else {
2140                 plog_info("prox version: %d.%d, DPDK version: %s\n",
2141                           VERSION_MAJOR, VERSION_MINOR,
2142                           rte_version() + sizeof(RTE_VER_PREFIX));
2143         }
2144         return 0;
2145 }
2146
2147 struct cmd_str {
2148         const char *cmd;
2149         const char *args;
2150         const char *help;
2151         int (*parse)(const char *args, struct input *input);
2152 };
2153
2154 static int parse_cmd_help(const char *str, struct input *input);
2155
2156 static struct cmd_str cmd_strings[] = {
2157         {"history", "", "Print command history", parse_cmd_history},
2158         {"echo", "", "echo parameter, useful to resolving variables", parse_cmd_echo},
2159         {"quit", "", "Stop all cores and quit", parse_cmd_quit},
2160         {"quit_force", "", "Quit without waiting on cores to stop", parse_cmd_quit_force},
2161         {"help", "<substr>", "Show list of commands that have <substr> as a substring. If no substring is provided, all commands are shown.", parse_cmd_help},
2162         {"verbose", "<level>", "Set verbosity level", parse_cmd_verbose},
2163         {"thread info", "<core_id> <task_id>", "", parse_cmd_thread_info},
2164         {"mem info", "", "Show information about system memory (number of huge pages and addresses of these huge pages)", parse_cmd_mem_info},
2165         {"update interval", "<value>", "Update statistics refresh rate, in msec (must be >=10). Default is 1 second", parse_cmd_update_interval},
2166         {"rx tx info", "", "Print connections between tasks on all cores", parse_cmd_rx_tx_info},
2167         {"start", "<core list>|all <task_id>", "Start core <core_id> or all cores", parse_cmd_start},
2168         {"stop", "<core list>|all <task_id>", "Stop core <core id> or all cores", parse_cmd_stop},
2169
2170         {"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},
2171         {"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},
2172         {"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},
2173         {"rx distr start", "", "Start gathering statistical distribution of received packets", parse_cmd_rx_distr_start},
2174         {"rx distr stop", "", "Stop gathering statistical distribution of received packets", parse_cmd_rx_distr_stop},
2175         {"rx distr reset", "", "Reset gathered statistical distribution of received packets", parse_cmd_rx_distr_reset},
2176         {"rx distr show", "", "Display gathered statistical distribution of received packets", parse_cmd_rx_distr_show},
2177         {"tx distr start", "", "Start gathering statistical distribution of xmitted packets", parse_cmd_tx_distr_start},
2178         {"tx distr stop", "", "Stop gathering statistical distribution of xmitted packets", parse_cmd_tx_distr_stop},
2179         {"tx distr reset", "", "Reset gathered statistical distribution of xmitted packets", parse_cmd_tx_distr_reset},
2180         {"tx distr show", "", "Display gathered statistical distribution of xmitted packets", parse_cmd_tx_distr_show},
2181
2182         {"rate", "<port id> <queue id> <rate>", "rate does not include preamble, SFD and IFG", parse_cmd_rate},
2183         {"count","<core id> <task id> <count>", "Generate <count> packets", parse_cmd_count},
2184         {"bypass", "<core_id> <task_id>", "Bypass task", parse_cmd_bypass},
2185         {"reconnect", "<core_id> <task_id>", "Reconnect task", parse_cmd_reconnect},
2186         {"pkt_size", "<core_id> <task_id> <pkt_size>", "Set the packet size to <pkt_size>", parse_cmd_pkt_size},
2187         {"imix", "<core_id> <task_id> <pkt_size,pkt_size ... >", "Set the packet sizes to <pkt_size>", parse_cmd_imix},
2188         {"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},
2189         {"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},
2190         {"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},
2191         {"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},
2192         {"reset values all", "", "Undo all \"set value\" commands on all cores/tasks", parse_cmd_reset_values_all},
2193         {"reset randoms all", "", "Undo all \"set random\" commands on all cores/tasks", parse_cmd_reset_randoms_all},
2194         {"reset values", "<core id> <task id>", "Undo all \"set value\" commands on specified core/task", parse_cmd_reset_values},
2195
2196         {"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},
2197         {"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},
2198         {"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},
2199         {"gateway ip", "<core id> <task id> <ip>", "Define/Change IP address of destination gateway on core <core id> <task id>.", parse_cmd_gateway_ip},
2200         {"local ip", "<core id> <task id> <ip>", "Define/Change IP address of destination gateway on core <core id> <task id>.", parse_cmd_local_ip},
2201
2202         {"pps unit", "", "Change core stats pps unit", parse_cmd_pps_unit},
2203         {"reset stats", "", "Reset all statistics", parse_cmd_reset_stats},
2204         {"reset lat stats", "", "Reset all latency statistics", parse_cmd_reset_lat_stats},
2205         {"tot stats", "", "Print total RX and TX packets", parse_cmd_tot_stats},
2206         {"tot ierrors tot", "", "Print total number of ierrors since reset", parse_cmd_tot_ierrors_tot},
2207         {"tot imissed tot", "", "Print total number of imissed since reset", parse_cmd_tot_imissed_tot},
2208         {"lat stats", "<core id> <task id>", "Print min,max,avg latency as measured during last sampling interval", parse_cmd_lat_stats},
2209         {"irq stats", "<core id> <task id>", "Print irq related infos", parse_cmd_irq},
2210         {"show irq buckets", "<core id> <task id>", "Print irq buckets", parse_cmd_show_irq_buckets},
2211         {"lat packets", "<core id> <task id>", "Print the latency for each of the last set of packets", parse_cmd_lat_packets},
2212         {"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},
2213         {"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},
2214         {"core stats", "<core id> <task id>", "Print rx/tx/drop for task <task id> running on core <core id>", parse_cmd_core_stats},
2215         {"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},
2216         {"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},
2217         {"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},
2218         {"read reg", "", "Read register", parse_cmd_read_reg},
2219         {"write reg", "", "Read register", parse_cmd_write_reg},
2220         {"set vlan offload", "", "Set Vlan offload", parse_cmd_set_vlan_offload},
2221         {"set vlan filter", "", "Set Vlan filter", parse_cmd_set_vlan_filter},
2222         {"reset cache", "", "Reset cache", parse_cmd_cache_reset},
2223         {"set cache class mask", "<core id> <class> <mask>", "Set cache class mask for <core id>", parse_cmd_set_cache_class_mask},
2224         {"get cache class mask", "<core id> <class>", "Get cache class mask", parse_cmd_get_cache_class_mask},
2225         {"set cache class", "<core id> <class>", "Set cache class", parse_cmd_set_cache_class},
2226         {"get cache class", "<core id>", "Get cache class", parse_cmd_get_cache_class},
2227         {"get cache mask", "<core id>", "Get cache mask", parse_cmd_get_cache_mask},
2228         {"reset port", "<port id>", "Reset port", parse_cmd_reset_port},
2229         {"enable multicast", "<port id> <MAC>", "Enable multicast", parse_cmd_enable_multicast},
2230         {"disable multicast", "<port id> <MAC>", "Disable multicast", parse_cmd_disable_multicast},
2231         {"ring info all", "", "Get information about ring, such as ring size and number of elements in the ring", parse_cmd_ring_info_all},
2232         {"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},
2233         {"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},
2234         {"port up", "<port id>", "Set the port up", parse_cmd_port_up},
2235         {"port down", "<port id>", "Set the port down", parse_cmd_port_down},
2236         {"port link state", "<port id>", "Get link state (up or down) for port", parse_cmd_port_link_state},
2237         {"port xstats", "<port id>", "Get extra statistics for the port", parse_cmd_xstats},
2238         {"stats", "<stats_path>", "Get stats as specified by <stats_path>. A comma-separated list of <stats_path> can be supplied", parse_cmd_stats},
2239         {"cgnat dump public hash", "<core id> <task id>", "Dump cgnat public hash table", parse_cmd_cgnat_public_hash},
2240         {"cgnat dump private hash", "<core id> <task id>", "Dump cgnat private hash table", parse_cmd_cgnat_private_hash},
2241         {"delay_us", "<core_id> <task_id> <delay_us>", "Set the delay in usec for the impair mode to <delay_us>", parse_cmd_delay_us},
2242         {"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},
2243         {"probability", "<core_id> <task_id> <probability>", "Set the percent of forwarded packets for the impair mode", parse_cmd_set_probability},
2244         {"version", "", "Show version", parse_cmd_version},
2245         {"join igmp", "<core_id> <task_id> <ip>", "Send igmp membership report for group <ip>", parse_cmd_join_igmp},
2246         {"leave igmp", "<core_id> <task_id>", "Send igmp leave group", parse_cmd_leave_igmp},
2247         {0,0,0,0},
2248 };
2249
2250 static int parse_cmd_help(const char *str, struct input *input)
2251 {
2252         /* str contains the arguments, all commands that have str as a
2253            substring will be shown. */
2254         size_t len, len2, longest_cmd = 0;
2255         for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
2256                 if (longest_cmd <strlen(cmd_strings[i].cmd))
2257                         longest_cmd = strlen(cmd_strings[i].cmd);
2258         }
2259         /* A single call to log will be executed after the help string
2260            has been built. The reason for this is to make use of the
2261            built-in pager. */
2262         char buf[32768] = {0};
2263
2264         for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
2265                 int is_substr = 0;
2266                 const size_t cmd_len = strlen(cmd_strings[i].cmd);
2267                 for (size_t j = 0; j < cmd_len; ++j) {
2268                         is_substr = 1;
2269                         for (size_t k = 0; k < strlen(str); ++k) {
2270                                 if (str[k] != (cmd_strings[i].cmd + j)[k]) {
2271                                         is_substr = 0;
2272                                         break;
2273                                 }
2274                         }
2275                         if (is_substr)
2276                                 break;
2277                 }
2278                 if (!is_substr)
2279                         continue;
2280
2281                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s", cmd_strings[i].cmd);
2282                 len = strlen(cmd_strings[i].cmd);
2283                 while (len < longest_cmd) {
2284                         len++;
2285                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
2286                 }
2287
2288                 if (strlen(cmd_strings[i].args)) {
2289                         char tmp[256] = {0};
2290                         prox_strncpy(tmp, cmd_strings[i].args, 128);
2291                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "Arguments: %s\n", tmp);
2292                         len2 = len;
2293                         if (strlen(cmd_strings[i].help)) {
2294                                 while (len2) {
2295                                         len2--;
2296                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
2297                                 }
2298                         }
2299                 }
2300
2301                 if (strlen(cmd_strings[i].help)) {
2302                         int add = 0;
2303                         const char *h = cmd_strings[i].help;
2304                         do {
2305                                 if (add) {
2306                                         len2 = len;
2307                                         while (len2) {
2308                                                 len2--;
2309                                                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " ");
2310                                         }
2311                                 }
2312                                 char tmp[128] = {0};
2313                                 const size_t max_len = strlen(h) > 80? 80 : strlen(h);
2314                                 size_t len3 = max_len;
2315                                 if (len3 == 80) {
2316                                         while (len3 && h[len3] != ' ')
2317                                                 len3--;
2318                                         if (len3 == 0)
2319                                                 len3 = max_len;
2320                                 }
2321
2322                                 // Use strncpy here and not prox_strncpy. The dest (tmp) has been initialized with 0.
2323                                 // The fact that we are copying 80 characters potentially not null terminated is hence not an issue.
2324                                 // Using prox_strncpy here might cause a PROX_PANIC
2325                                 strncpy(tmp, h, len3);
2326                                 h += len3;
2327                                 while (h[0] == ' ' && strlen(h))
2328                                         h++;
2329
2330                                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%s\n", tmp);
2331                                 add = 1;
2332                         } while(strlen(h));
2333                 }
2334                 if (strlen(cmd_strings[i].help) == 0&& strlen(cmd_strings[i].args) == 0) {
2335                         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "\n");
2336                 }
2337         }
2338         plog_info("%s", buf);
2339
2340         return 0;
2341 }
2342
2343 const char *cmd_parser_cmd(size_t i)
2344 {
2345         i = i < cmd_parser_n_cmd()? i: cmd_parser_n_cmd();
2346         return cmd_strings[i].cmd;
2347 }
2348
2349 size_t cmd_parser_n_cmd(void)
2350 {
2351         return sizeof(cmd_strings)/sizeof(cmd_strings[0]) - 1;
2352 }
2353
2354 void cmd_parser_parse(const char *str, struct input *input)
2355 {
2356         size_t skip;
2357
2358         for (size_t i = 0; i < cmd_parser_n_cmd(); ++i) {
2359                 skip = strlen(cmd_strings[i].cmd);
2360                 if (strncmp(cmd_strings[i].cmd, str, skip) == 0 &&
2361                     (str[skip] == ' ' || str[skip] == 0)) {
2362                         while (str[skip] == ' ')
2363                                 skip++;
2364
2365                         if (cmd_strings[i].parse(str + skip, input) != 0) {
2366                                 plog_warn("Invalid syntax for command '%s': %s %s\n",
2367                                           cmd_strings[i].cmd, cmd_strings[i].args, cmd_strings[i].help);
2368                         }
2369                         return ;
2370                 }
2371         }
2372
2373         plog_err("Unknown command: '%s'\n", str);
2374 }