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