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