examples/ethtool: remove dead code
[dpdk.git] / examples / ethtool / ethtool-app / ethapp.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <cmdline_parse.h>
35 #include <cmdline_parse_num.h>
36 #include <cmdline_parse_string.h>
37 #include <cmdline_parse_etheraddr.h>
38 #include <cmdline_socket.h>
39 #include <cmdline.h>
40
41 #include "rte_ethtool.h"
42 #include "ethapp.h"
43
44 #define EEPROM_DUMP_CHUNKSIZE 1024
45
46
47 struct pcmd_get_params {
48         cmdline_fixed_string_t cmd;
49 };
50 struct pcmd_int_params {
51         cmdline_fixed_string_t cmd;
52         uint16_t port;
53 };
54 struct pcmd_intstr_params {
55         cmdline_fixed_string_t cmd;
56         uint16_t port;
57         cmdline_fixed_string_t opt;
58 };
59 struct pcmd_intmac_params {
60         cmdline_fixed_string_t cmd;
61         uint16_t port;
62         struct ether_addr mac;
63 };
64 struct pcmd_str_params {
65         cmdline_fixed_string_t cmd;
66         cmdline_fixed_string_t opt;
67 };
68 struct pcmd_vlan_params {
69         cmdline_fixed_string_t cmd;
70         uint16_t port;
71         cmdline_fixed_string_t mode;
72         uint16_t vid;
73 };
74 struct pcmd_intintint_params {
75         cmdline_fixed_string_t cmd;
76         uint16_t port;
77         uint16_t tx;
78         uint16_t rx;
79 };
80
81
82 /* Parameter-less commands */
83 cmdline_parse_token_string_t pcmd_quit_token_cmd =
84         TOKEN_STRING_INITIALIZER(struct pcmd_get_params, cmd, "quit");
85 cmdline_parse_token_string_t pcmd_stats_token_cmd =
86         TOKEN_STRING_INITIALIZER(struct pcmd_get_params, cmd, "stats");
87 cmdline_parse_token_string_t pcmd_drvinfo_token_cmd =
88         TOKEN_STRING_INITIALIZER(struct pcmd_get_params, cmd, "drvinfo");
89 cmdline_parse_token_string_t pcmd_link_token_cmd =
90         TOKEN_STRING_INITIALIZER(struct pcmd_get_params, cmd, "link");
91
92 /* Commands taking just port id */
93 cmdline_parse_token_string_t pcmd_open_token_cmd =
94         TOKEN_STRING_INITIALIZER(struct pcmd_int_params, cmd, "open");
95 cmdline_parse_token_string_t pcmd_stop_token_cmd =
96         TOKEN_STRING_INITIALIZER(struct pcmd_int_params, cmd, "stop");
97 cmdline_parse_token_string_t pcmd_rxmode_token_cmd =
98         TOKEN_STRING_INITIALIZER(struct pcmd_int_params, cmd, "rxmode");
99 cmdline_parse_token_string_t pcmd_portstats_token_cmd =
100         TOKEN_STRING_INITIALIZER(struct pcmd_int_params, cmd, "portstats");
101 cmdline_parse_token_num_t pcmd_int_token_port =
102         TOKEN_NUM_INITIALIZER(struct pcmd_int_params, port, UINT16);
103
104 /* Commands taking port id and string */
105 cmdline_parse_token_string_t pcmd_eeprom_token_cmd =
106         TOKEN_STRING_INITIALIZER(struct pcmd_intstr_params, cmd, "eeprom");
107 cmdline_parse_token_string_t pcmd_mtu_token_cmd =
108         TOKEN_STRING_INITIALIZER(struct pcmd_intstr_params, cmd, "mtu");
109 cmdline_parse_token_string_t pcmd_regs_token_cmd =
110         TOKEN_STRING_INITIALIZER(struct pcmd_intstr_params, cmd, "regs");
111
112 cmdline_parse_token_num_t pcmd_intstr_token_port =
113         TOKEN_NUM_INITIALIZER(struct pcmd_intstr_params, port, UINT16);
114 cmdline_parse_token_string_t pcmd_intstr_token_opt =
115         TOKEN_STRING_INITIALIZER(struct pcmd_intstr_params, opt, NULL);
116
117 /* Commands taking port id and a MAC address string */
118 cmdline_parse_token_string_t pcmd_macaddr_token_cmd =
119         TOKEN_STRING_INITIALIZER(struct pcmd_intmac_params, cmd, "macaddr");
120 cmdline_parse_token_num_t pcmd_intmac_token_port =
121         TOKEN_NUM_INITIALIZER(struct pcmd_intmac_params, port, UINT16);
122 cmdline_parse_token_etheraddr_t pcmd_intmac_token_mac =
123         TOKEN_ETHERADDR_INITIALIZER(struct pcmd_intmac_params, mac);
124
125 /* Command taking just a MAC address */
126 cmdline_parse_token_string_t pcmd_validate_token_cmd =
127         TOKEN_STRING_INITIALIZER(struct pcmd_intmac_params, cmd, "validate");
128
129
130 /* Commands taking port id and two integers */
131 cmdline_parse_token_string_t pcmd_ringparam_token_cmd =
132         TOKEN_STRING_INITIALIZER(struct pcmd_intintint_params, cmd,
133                 "ringparam");
134 cmdline_parse_token_num_t pcmd_intintint_token_port =
135         TOKEN_NUM_INITIALIZER(struct pcmd_intintint_params, port, UINT16);
136 cmdline_parse_token_num_t pcmd_intintint_token_tx =
137         TOKEN_NUM_INITIALIZER(struct pcmd_intintint_params, tx, UINT16);
138 cmdline_parse_token_num_t pcmd_intintint_token_rx =
139         TOKEN_NUM_INITIALIZER(struct pcmd_intintint_params, rx, UINT16);
140
141
142 /* Pause commands */
143 cmdline_parse_token_string_t pcmd_pause_token_cmd =
144         TOKEN_STRING_INITIALIZER(struct pcmd_intstr_params, cmd, "pause");
145 cmdline_parse_token_num_t pcmd_pause_token_port =
146         TOKEN_NUM_INITIALIZER(struct pcmd_intstr_params, port, UINT16);
147 cmdline_parse_token_string_t pcmd_pause_token_opt =
148         TOKEN_STRING_INITIALIZER(struct pcmd_intstr_params,
149                 opt, "all#tx#rx#none");
150
151 /* VLAN commands */
152 cmdline_parse_token_string_t pcmd_vlan_token_cmd =
153         TOKEN_STRING_INITIALIZER(struct pcmd_vlan_params, cmd, "vlan");
154 cmdline_parse_token_num_t pcmd_vlan_token_port =
155         TOKEN_NUM_INITIALIZER(struct pcmd_vlan_params, port, UINT16);
156 cmdline_parse_token_string_t pcmd_vlan_token_mode =
157         TOKEN_STRING_INITIALIZER(struct pcmd_vlan_params, mode, "add#del");
158 cmdline_parse_token_num_t pcmd_vlan_token_vid =
159         TOKEN_NUM_INITIALIZER(struct pcmd_vlan_params, vid, UINT16);
160
161
162 static void
163 pcmd_quit_callback(__rte_unused void *ptr_params,
164         struct cmdline *ctx,
165         __rte_unused void *ptr_data)
166 {
167         cmdline_quit(ctx);
168 }
169
170
171 static void
172 pcmd_drvinfo_callback(__rte_unused void *ptr_params,
173         __rte_unused struct cmdline *ctx,
174         __rte_unused void *ptr_data)
175 {
176         struct ethtool_drvinfo info;
177         int id_port;
178
179         for (id_port = 0; id_port < rte_eth_dev_count(); id_port++) {
180                 if (rte_ethtool_get_drvinfo(id_port, &info)) {
181                         printf("Error getting info for port %i\n", id_port);
182                         return;
183                 }
184                 printf("Port %i driver: %s (ver: %s)\n",
185                         id_port, info.driver, info.version
186                       );
187         }
188 }
189
190
191 static void
192 pcmd_link_callback(__rte_unused void *ptr_params,
193         __rte_unused struct cmdline *ctx,
194         __rte_unused void *ptr_data)
195 {
196         int num_ports = rte_eth_dev_count();
197         int id_port, stat_port;
198
199         for (id_port = 0; id_port < num_ports; id_port++) {
200                 if (!rte_eth_dev_is_valid_port(id_port))
201                         continue;
202                 stat_port = rte_ethtool_get_link(id_port);
203                 switch (stat_port) {
204                 case 0:
205                         printf("Port %i: Down\n", id_port);
206                         break;
207                 case 1:
208                         printf("Port %i: Up\n", id_port);
209                         break;
210                 default:
211                         printf("Port %i: Error getting link status\n",
212                                 id_port
213                                 );
214                         break;
215                 }
216         }
217         printf("\n");
218 }
219
220
221 static void
222 pcmd_regs_callback(void *ptr_params,
223         __rte_unused struct cmdline *ctx,
224         __rte_unused void *ptr_data)
225 {
226         struct pcmd_intstr_params *params = ptr_params;
227         int len_regs;
228         struct ethtool_regs regs;
229         unsigned char *buf_data;
230         FILE *fp_regs;
231
232         if (!rte_eth_dev_is_valid_port(params->port)) {
233                 printf("Error: Invalid port number %i\n", params->port);
234                 return;
235         }
236         len_regs = rte_ethtool_get_regs_len(params->port);
237         if (len_regs > 0) {
238                 printf("Port %i: %i bytes\n", params->port, len_regs);
239                 buf_data = malloc(len_regs);
240                 if (buf_data == NULL) {
241                         printf("Error allocating %i bytes for buffer\n",
242                                 len_regs);
243                         return;
244                 }
245                 if (!rte_ethtool_get_regs(params->port, &regs, buf_data)) {
246                         fp_regs = fopen(params->opt, "wb");
247                         if (fp_regs == NULL) {
248                                 printf("Error opening '%s' for writing\n",
249                                         params->opt);
250                         } else {
251                                 if ((int)fwrite(buf_data,
252                                                 1, len_regs,
253                                                 fp_regs) != len_regs)
254                                         printf("Error writing '%s'\n",
255                                                 params->opt);
256                                 fclose(fp_regs);
257                         }
258                 }
259                 free(buf_data);
260         } else if (len_regs == -ENOTSUP)
261                 printf("Port %i: Operation not supported\n", params->port);
262         else
263                 printf("Port %i: Error getting registers\n", params->port);
264 }
265
266
267 static void
268 pcmd_eeprom_callback(void *ptr_params,
269         __rte_unused struct cmdline *ctx,
270         __rte_unused void *ptr_data)
271 {
272         struct pcmd_intstr_params *params = ptr_params;
273         struct ethtool_eeprom info_eeprom;
274         int len_eeprom;
275         int pos_eeprom;
276         int stat;
277         unsigned char bytes_eeprom[EEPROM_DUMP_CHUNKSIZE];
278         FILE *fp_eeprom;
279
280         if (!rte_eth_dev_is_valid_port(params->port)) {
281                 printf("Error: Invalid port number %i\n", params->port);
282                 return;
283         }
284         len_eeprom = rte_ethtool_get_eeprom_len(params->port);
285         if (len_eeprom > 0) {
286                 fp_eeprom = fopen(params->opt, "wb");
287                 if (fp_eeprom == NULL) {
288                         printf("Error opening '%s' for writing\n",
289                                 params->opt);
290                         return;
291                 }
292                 printf("Total EEPROM length: %i bytes\n", len_eeprom);
293                 info_eeprom.len = EEPROM_DUMP_CHUNKSIZE;
294                 for (pos_eeprom = 0;
295                                 pos_eeprom < len_eeprom;
296                                 pos_eeprom += EEPROM_DUMP_CHUNKSIZE) {
297                         info_eeprom.offset = pos_eeprom;
298                         if (pos_eeprom + EEPROM_DUMP_CHUNKSIZE > len_eeprom)
299                                 info_eeprom.len = len_eeprom - pos_eeprom;
300                         else
301                                 info_eeprom.len = EEPROM_DUMP_CHUNKSIZE;
302                         stat = rte_ethtool_get_eeprom(
303                                 params->port, &info_eeprom, bytes_eeprom
304                                 );
305                         if (stat != 0) {
306                                 printf("EEPROM read error %i\n", stat);
307                                 break;
308                         }
309                         if (fwrite(bytes_eeprom,
310                                         1, info_eeprom.len,
311                                         fp_eeprom) != info_eeprom.len) {
312                                 printf("Error writing '%s'\n", params->opt);
313                                 break;
314                         }
315                 }
316                 fclose(fp_eeprom);
317         } else if (len_eeprom == 0)
318                 printf("Port %i: Device does not have EEPROM\n", params->port);
319         else if (len_eeprom == -ENOTSUP)
320                 printf("Port %i: Operation not supported\n", params->port);
321         else
322                 printf("Port %i: Error getting EEPROM\n", params->port);
323 }
324
325
326 static void
327 pcmd_pause_callback(void *ptr_params,
328         __rte_unused struct cmdline *ctx,
329         void *ptr_data)
330 {
331         struct pcmd_intstr_params *params = ptr_params;
332         struct ethtool_pauseparam info;
333         int stat;
334
335         if (!rte_eth_dev_is_valid_port(params->port)) {
336                 printf("Error: Invalid port number %i\n", params->port);
337                 return;
338         }
339         if (ptr_data != NULL) {
340                 stat = rte_ethtool_get_pauseparam(params->port, &info);
341         } else {
342                 if (strcasecmp("all", params->opt) == 0) {
343                         info.tx_pause = 1;
344                         info.rx_pause = 1;
345                 } else if (strcasecmp("tx", params->opt) == 0) {
346                         info.tx_pause = 1;
347                         info.rx_pause = 0;
348                 } else if (strcasecmp("rx", params->opt) == 0) {
349                         info.tx_pause = 0;
350                         info.rx_pause = 1;
351                 } else {
352                         info.tx_pause = 0;
353                         info.rx_pause = 0;
354                 }
355                 stat = rte_ethtool_set_pauseparam(params->port, &info);
356         }
357         if (stat == 0) {
358                 if (info.rx_pause && info.tx_pause)
359                         printf("Port %i: Tx & Rx Paused\n", params->port);
360                 else if (info.rx_pause)
361                         printf("Port %i: Rx Paused\n", params->port);
362                 else if (info.tx_pause)
363                         printf("Port %i: Tx Paused\n", params->port);
364                 else
365                         printf("Port %i: Tx & Rx not paused\n", params->port);
366         } else if (stat == -ENOTSUP)
367                 printf("Port %i: Operation not supported\n", params->port);
368         else
369                 printf("Port %i: Error %i\n", params->port, stat);
370 }
371
372
373 static void
374 pcmd_open_callback(__rte_unused void *ptr_params,
375         __rte_unused struct cmdline *ctx,
376         __rte_unused void *ptr_data)
377 {
378         struct pcmd_int_params *params = ptr_params;
379         int stat;
380
381         if (!rte_eth_dev_is_valid_port(params->port)) {
382                 printf("Error: Invalid port number %i\n", params->port);
383                 return;
384         }
385         lock_port(params->port);
386         stat = rte_ethtool_net_open(params->port);
387         mark_port_active(params->port);
388         unlock_port(params->port);
389         if (stat == 0)
390                 return;
391         else if (stat == -ENOTSUP)
392                 printf("Port %i: Operation not supported\n", params->port);
393         else
394                 printf("Port %i: Error opening device\n", params->port);
395 }
396
397 static void
398 pcmd_stop_callback(__rte_unused void *ptr_params,
399         __rte_unused struct cmdline *ctx,
400         __rte_unused void *ptr_data)
401 {
402         struct pcmd_int_params *params = ptr_params;
403         int stat;
404
405         if (!rte_eth_dev_is_valid_port(params->port)) {
406                 printf("Error: Invalid port number %i\n", params->port);
407                 return;
408         }
409         lock_port(params->port);
410         stat = rte_ethtool_net_stop(params->port);
411         mark_port_inactive(params->port);
412         unlock_port(params->port);
413         if (stat == 0)
414                 return;
415         else if (stat == -ENOTSUP)
416                 printf("Port %i: Operation not supported\n", params->port);
417         else
418                 printf("Port %i: Error stopping device\n", params->port);
419 }
420
421
422 static void
423 pcmd_rxmode_callback(void *ptr_params,
424         __rte_unused struct cmdline *ctx,
425         __rte_unused void *ptr_data)
426 {
427         struct pcmd_intstr_params *params = ptr_params;
428         int stat;
429
430         if (!rte_eth_dev_is_valid_port(params->port)) {
431                 printf("Error: Invalid port number %i\n", params->port);
432                 return;
433         }
434         stat = rte_ethtool_net_set_rx_mode(params->port);
435         if (stat == 0)
436                 return;
437         else if (stat == -ENOTSUP)
438                 printf("Port %i: Operation not supported\n", params->port);
439         else
440                 printf("Port %i: Error setting rx mode\n", params->port);
441 }
442
443
444 static void
445 pcmd_macaddr_callback(void *ptr_params,
446         __rte_unused struct cmdline *ctx,
447         void *ptr_data)
448 {
449         struct pcmd_intmac_params *params = ptr_params;
450         struct ether_addr mac_addr;
451         int stat;
452
453         stat = 0;
454         if (!rte_eth_dev_is_valid_port(params->port)) {
455                 printf("Error: Invalid port number %i\n", params->port);
456                 return;
457         }
458         if (ptr_data != NULL) {
459                 lock_port(params->port);
460                 stat = rte_ethtool_net_set_mac_addr(params->port,
461                         &params->mac);
462                 mark_port_newmac(params->port);
463                 unlock_port(params->port);
464                 if (stat == 0) {
465                         printf("MAC address changed\n");
466                         return;
467                 }
468         } else {
469                 stat = rte_ethtool_net_get_mac_addr(params->port, &mac_addr);
470                 if (stat == 0) {
471                         printf(
472                                 "Port %i MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
473                                 params->port,
474                                 mac_addr.addr_bytes[0],
475                                 mac_addr.addr_bytes[1],
476                                 mac_addr.addr_bytes[2],
477                                 mac_addr.addr_bytes[3],
478                                 mac_addr.addr_bytes[4],
479                                 mac_addr.addr_bytes[5]);
480                         return;
481                 }
482         }
483
484         printf("Port %i: Error %s\n", params->port,
485                strerror(-stat));
486 }
487
488 static void
489 pcmd_mtu_callback(void *ptr_params,
490         __rte_unused struct cmdline *ctx,
491         __rte_unused void *ptr_data)
492 {
493         struct pcmd_intstr_params *params = ptr_params;
494         int stat;
495         int new_mtu;
496         char *ptr_parse_end;
497
498         if (!rte_eth_dev_is_valid_port(params->port)) {
499                 printf("Error: Invalid port number %i\n", params->port);
500                 return;
501         }
502         new_mtu = atoi(params->opt);
503         new_mtu = strtoul(params->opt, &ptr_parse_end, 10);
504         if (*ptr_parse_end != '\0' ||
505                         new_mtu < ETHER_MIN_MTU ||
506                         new_mtu > ETHER_MAX_JUMBO_FRAME_LEN) {
507                 printf("Port %i: Invalid MTU value\n", params->port);
508                 return;
509         }
510         stat = rte_ethtool_net_change_mtu(params->port, new_mtu);
511         if (stat == 0)
512                 printf("Port %i: MTU set to %i\n", params->port, new_mtu);
513         else if (stat == -ENOTSUP)
514                 printf("Port %i: Operation not supported\n", params->port);
515         else
516                 printf("Port %i: Error setting MTU\n", params->port);
517 }
518
519
520
521 static void pcmd_portstats_callback(__rte_unused void *ptr_params,
522         __rte_unused struct cmdline *ctx,
523         __rte_unused void *ptr_data)
524 {
525         struct pcmd_int_params *params = ptr_params;
526         struct rte_eth_stats stat_info;
527         int stat;
528
529         if (!rte_eth_dev_is_valid_port(params->port)) {
530                 printf("Error: Invalid port number %i\n", params->port);
531                 return;
532         }
533         stat = rte_ethtool_net_get_stats64(params->port, &stat_info);
534         if (stat == 0) {
535                 /* Most of rte_eth_stats is deprecated.. */
536                 printf("Port %i stats\n", params->port);
537                 printf("   In: %" PRIu64 " (%" PRIu64 " bytes)\n"
538                         "  Out: %"PRIu64" (%"PRIu64 " bytes)\n"
539                         "  Err: %"PRIu64"\n",
540                         stat_info.ipackets,
541                         stat_info.ibytes,
542                         stat_info.opackets,
543                         stat_info.obytes,
544                         stat_info.ierrors+stat_info.oerrors
545                       );
546         } else if (stat == -ENOTSUP)
547                 printf("Port %i: Operation not supported\n", params->port);
548         else
549                 printf("Port %i: Error fetching statistics\n", params->port);
550 }
551
552 static void pcmd_ringparam_callback(__rte_unused void *ptr_params,
553         __rte_unused struct cmdline *ctx,
554         void *ptr_data)
555 {
556         struct pcmd_intintint_params *params = ptr_params;
557         struct ethtool_ringparam ring_data;
558         struct ethtool_ringparam ring_params;
559         int stat;
560
561         if (!rte_eth_dev_is_valid_port(params->port)) {
562                 printf("Error: Invalid port number %i\n", params->port);
563                 return;
564         }
565         if (ptr_data == NULL) {
566                 stat = rte_ethtool_get_ringparam(params->port, &ring_data);
567                 if (stat == 0) {
568                         printf("Port %i ring parameters\n"
569                                 "  Rx Pending: %i (%i max)\n"
570                                 "  Tx Pending: %i (%i max)\n",
571                                 params->port,
572                                 ring_data.rx_pending,
573                                 ring_data.rx_max_pending,
574                                 ring_data.tx_pending,
575                                 ring_data.tx_max_pending);
576                 }
577         } else {
578                 if (params->tx < 1 || params->rx < 1) {
579                         printf("Error: Invalid parameters\n");
580                         return;
581                 }
582                 memset(&ring_params, 0, sizeof(struct ethtool_ringparam));
583                 ring_params.tx_pending = params->tx;
584                 ring_params.rx_pending = params->rx;
585                 lock_port(params->port);
586                 stat = rte_ethtool_set_ringparam(params->port, &ring_params);
587                 unlock_port(params->port);
588         }
589         if (stat == 0)
590                 return;
591         else if (stat == -ENOTSUP)
592                 printf("Port %i: Operation not supported\n", params->port);
593         else
594                 printf("Port %i: Error fetching statistics\n", params->port);
595 }
596
597 static void pcmd_validate_callback(void *ptr_params,
598         __rte_unused struct cmdline *ctx,
599         __rte_unused void *ptr_data)
600 {
601         struct pcmd_intmac_params *params = ptr_params;
602
603         if (rte_ethtool_net_validate_addr(0, &params->mac))
604                 printf("Address is unicast\n");
605         else
606                 printf("Address is not unicast\n");
607 }
608
609
610 static void pcmd_vlan_callback(__rte_unused void *ptr_params,
611         __rte_unused struct cmdline *ctx,
612         __rte_unused void *ptr_data)
613 {
614         struct pcmd_vlan_params *params = ptr_params;
615         int stat;
616
617         if (!rte_eth_dev_is_valid_port(params->port)) {
618                 printf("Error: Invalid port number %i\n", params->port);
619                 return;
620         }
621         stat = 0;
622
623         if (strcasecmp("add", params->mode) == 0) {
624                 stat = rte_ethtool_net_vlan_rx_add_vid(
625                         params->port, params->vid
626                         );
627                 if (stat == 0)
628                         printf("VLAN vid %i added\n", params->vid);
629
630         } else if (strcasecmp("del", params->mode) == 0) {
631                 stat = rte_ethtool_net_vlan_rx_kill_vid(
632                         params->port, params->vid
633                         );
634                 if (stat == 0)
635                         printf("VLAN vid %i removed\n", params->vid);
636         } else {
637                 /* Should not happen! */
638                 printf("Error: Bad mode %s\n", params->mode);
639         }
640         if (stat == -ENOTSUP)
641                 printf("Port %i: Operation not supported\n", params->port);
642         else if (stat == -ENOSYS)
643                 printf("Port %i: VLAN filtering disabled\n", params->port);
644         else if (stat != 0)
645                 printf("Port %i: Error changing VLAN setup (code %i)\n",
646                         params->port, -stat);
647 }
648
649
650 cmdline_parse_inst_t pcmd_quit = {
651         .f = pcmd_quit_callback,
652         .data = NULL,
653         .help_str = "quit\n     Exit program",
654         .tokens = {(void *)&pcmd_quit_token_cmd, NULL},
655 };
656 cmdline_parse_inst_t pcmd_drvinfo = {
657         .f = pcmd_drvinfo_callback,
658         .data = NULL,
659         .help_str = "drvinfo\n     Print driver info",
660         .tokens = {(void *)&pcmd_drvinfo_token_cmd, NULL},
661 };
662 cmdline_parse_inst_t pcmd_link = {
663         .f = pcmd_link_callback,
664         .data = NULL,
665         .help_str = "link\n     Print port link states",
666         .tokens = {(void *)&pcmd_link_token_cmd, NULL},
667 };
668 cmdline_parse_inst_t pcmd_regs = {
669         .f = pcmd_regs_callback,
670         .data = NULL,
671         .help_str = "regs <port_id> <filename>\n"
672                 "     Dump port register(s) to file",
673         .tokens = {
674                 (void *)&pcmd_regs_token_cmd,
675                 (void *)&pcmd_intstr_token_port,
676                 (void *)&pcmd_intstr_token_opt,
677                 NULL
678         },
679 };
680 cmdline_parse_inst_t pcmd_eeprom = {
681         .f = pcmd_eeprom_callback,
682         .data = NULL,
683         .help_str = "eeprom <port_id> <filename>\n    Dump EEPROM to file",
684         .tokens = {
685                 (void *)&pcmd_eeprom_token_cmd,
686                 (void *)&pcmd_intstr_token_port,
687                 (void *)&pcmd_intstr_token_opt,
688                 NULL
689         },
690 };
691 cmdline_parse_inst_t pcmd_pause_noopt = {
692         .f = pcmd_pause_callback,
693         .data = (void *)0x01,
694         .help_str = "pause <port_id>\n     Print port pause state",
695         .tokens = {
696                 (void *)&pcmd_pause_token_cmd,
697                 (void *)&pcmd_pause_token_port,
698                 NULL
699         },
700 };
701 cmdline_parse_inst_t pcmd_pause = {
702         .f = pcmd_pause_callback,
703         .data = NULL,
704         .help_str =
705                 "pause <port_id> <all|tx|rx|none>\n     Pause/unpause port",
706         .tokens = {
707                 (void *)&pcmd_pause_token_cmd,
708                 (void *)&pcmd_pause_token_port,
709                 (void *)&pcmd_pause_token_opt,
710                 NULL
711         },
712 };
713 cmdline_parse_inst_t pcmd_open = {
714         .f = pcmd_open_callback,
715         .data = NULL,
716         .help_str = "open <port_id>\n     Open port",
717         .tokens = {
718                 (void *)&pcmd_open_token_cmd,
719                 (void *)&pcmd_int_token_port,
720                 NULL
721         },
722 };
723 cmdline_parse_inst_t pcmd_stop = {
724         .f = pcmd_stop_callback,
725         .data = NULL,
726         .help_str = "stop <port_id>\n     Stop port",
727         .tokens = {
728                 (void *)&pcmd_stop_token_cmd,
729                 (void *)&pcmd_int_token_port,
730                 NULL
731         },
732 };
733 cmdline_parse_inst_t pcmd_rxmode = {
734         .f = pcmd_rxmode_callback,
735         .data = NULL,
736         .help_str = "rxmode <port_id>\n     Toggle port Rx mode",
737         .tokens = {
738                 (void *)&pcmd_rxmode_token_cmd,
739                 (void *)&pcmd_int_token_port,
740                 NULL
741         },
742 };
743 cmdline_parse_inst_t pcmd_macaddr_get = {
744         .f = pcmd_macaddr_callback,
745         .data = NULL,
746         .help_str = "macaddr <port_id>\n"
747                 "     Get MAC address",
748         .tokens = {
749                 (void *)&pcmd_macaddr_token_cmd,
750                 (void *)&pcmd_intstr_token_port,
751                 NULL
752         },
753 };
754 cmdline_parse_inst_t pcmd_macaddr = {
755         .f = pcmd_macaddr_callback,
756         .data = (void *)0x01,
757         .help_str =
758                 "macaddr <port_id> <mac_addr>\n"
759                 "     Set MAC address",
760         .tokens = {
761                 (void *)&pcmd_macaddr_token_cmd,
762                 (void *)&pcmd_intmac_token_port,
763                 (void *)&pcmd_intmac_token_mac,
764                 NULL
765         },
766 };
767 cmdline_parse_inst_t pcmd_mtu = {
768         .f = pcmd_mtu_callback,
769         .data = NULL,
770         .help_str = "mtu <port_id> <mtu_value>\n"
771                 "     Change MTU",
772         .tokens = {
773                 (void *)&pcmd_mtu_token_cmd,
774                 (void *)&pcmd_intstr_token_port,
775                 (void *)&pcmd_intstr_token_opt,
776                 NULL
777         },
778 };
779 cmdline_parse_inst_t pcmd_portstats = {
780         .f = pcmd_portstats_callback,
781         .data = NULL,
782         .help_str = "portstats <port_id>\n"
783                 "     Print port eth statistics",
784         .tokens = {
785                 (void *)&pcmd_portstats_token_cmd,
786                 (void *)&pcmd_int_token_port,
787                 NULL
788         },
789 };
790 cmdline_parse_inst_t pcmd_ringparam = {
791         .f = pcmd_ringparam_callback,
792         .data = NULL,
793         .help_str = "ringparam <port_id>\n"
794                 "     Print ring parameters",
795         .tokens = {
796                 (void *)&pcmd_ringparam_token_cmd,
797                 (void *)&pcmd_intintint_token_port,
798                 NULL
799         },
800 };
801 cmdline_parse_inst_t pcmd_ringparam_set = {
802         .f = pcmd_ringparam_callback,
803         .data = (void *)1,
804         .help_str = "ringparam <port_id> <tx_param> <rx_param>\n"
805                 "     Set ring parameters",
806         .tokens = {
807                 (void *)&pcmd_ringparam_token_cmd,
808                 (void *)&pcmd_intintint_token_port,
809                 (void *)&pcmd_intintint_token_tx,
810                 (void *)&pcmd_intintint_token_rx,
811                 NULL
812         },
813 };
814 cmdline_parse_inst_t pcmd_validate = {
815         .f = pcmd_validate_callback,
816         .data = NULL,
817         .help_str = "validate <mac_addr>\n"
818                 "     Check that MAC address is valid unicast address",
819         .tokens = {
820                 (void *)&pcmd_validate_token_cmd,
821                 (void *)&pcmd_intmac_token_mac,
822                 NULL
823         },
824 };
825 cmdline_parse_inst_t pcmd_vlan = {
826         .f = pcmd_vlan_callback,
827         .data = NULL,
828         .help_str = "vlan <port_id> <add|del> <vlan_id>\n"
829                 "     Add/remove VLAN id",
830         .tokens = {
831                 (void *)&pcmd_vlan_token_cmd,
832                 (void *)&pcmd_vlan_token_port,
833                 (void *)&pcmd_vlan_token_mode,
834                 (void *)&pcmd_vlan_token_vid,
835                 NULL
836         },
837 };
838
839
840 cmdline_parse_ctx_t list_prompt_commands[] = {
841         (cmdline_parse_inst_t *)&pcmd_drvinfo,
842         (cmdline_parse_inst_t *)&pcmd_eeprom,
843         (cmdline_parse_inst_t *)&pcmd_link,
844         (cmdline_parse_inst_t *)&pcmd_macaddr_get,
845         (cmdline_parse_inst_t *)&pcmd_macaddr,
846         (cmdline_parse_inst_t *)&pcmd_mtu,
847         (cmdline_parse_inst_t *)&pcmd_open,
848         (cmdline_parse_inst_t *)&pcmd_pause_noopt,
849         (cmdline_parse_inst_t *)&pcmd_pause,
850         (cmdline_parse_inst_t *)&pcmd_portstats,
851         (cmdline_parse_inst_t *)&pcmd_regs,
852         (cmdline_parse_inst_t *)&pcmd_ringparam,
853         (cmdline_parse_inst_t *)&pcmd_ringparam_set,
854         (cmdline_parse_inst_t *)&pcmd_rxmode,
855         (cmdline_parse_inst_t *)&pcmd_stop,
856         (cmdline_parse_inst_t *)&pcmd_validate,
857         (cmdline_parse_inst_t *)&pcmd_vlan,
858         (cmdline_parse_inst_t *)&pcmd_quit,
859         NULL
860 };
861
862
863 void ethapp_main(void)
864 {
865         struct cmdline *ctx_cmdline;
866
867         ctx_cmdline = cmdline_stdin_new(list_prompt_commands, "EthApp> ");
868         cmdline_interact(ctx_cmdline);
869         cmdline_stdin_exit(ctx_cmdline);
870 }