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