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