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