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