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