summaryrefslogtreecommitdiff
path: root/examples/cli/main.c
blob: 84e89593d32448ffbc4b331f9e3ff2bb287009f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*-
 *   BSD LICENSE
 *
 *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Intel Corporation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* Created 2016 by Keith Wiles @ intel.com */

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <libgen.h>
#include <getopt.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <fcntl.h>

#include <rte_version.h>
#include <rte_cycles.h>
#include <rte_ether.h>
#include <rte_string_fns.h>

#include <cli.h>
#include <cli_file.h>
#include <cli_map.h>
#include <cli_help.h>
#include <cli_string_fns.h>

#include "main.h"

static int
hello_cmd(int argc, char **argv)
{
	int i, opt;

	optind = 1;
	while ((opt = getopt(argc, argv, "?")) != -1) {
		switch (opt) {
		case '?':
			cli_usage();
			return 0;
		default:
			break;
		}
	}

	cli_printf("Hello command said: Hello World!! ");
	for (i = 1; i < argc; i++)
		cli_printf("%s ", argv[i]);
	cli_printf("\n");

	return 0;
}

static int
pci_file(struct cli_node *node, char *buff, int len, uint32_t opt)
{
	if (is_file_open(opt)) {
		FILE *f;

		if (node->file_data && (node->fflags & CLI_FREE_DATA))
			free(node->file_data);

		node->file_data = malloc(32 * 1024);
		if (!node->file_data)
			return -1;
		node->file_size = 32 * 1024;
		node->fflags = CLI_DATA_RDONLY | CLI_FREE_DATA;

		f = popen("lspci", "r");
		if (!f)
			return -1;

		node->file_size = fread(node->file_data, 1, node->file_size, f);

		pclose(f);
		return 0;
	}
	return cli_file_handler(node, buff, len, opt);
}

static struct cli_map show_map[] = {
    {10, "show %P"},
    {20, "show %P mac %m"},
    {30, "show %P vlan %d mac %m"},
    {40, "show %P %|vlan|mac"},
    {-1, NULL}};

static const char *show_help[] = {
    "show <portlist>",
    "show <portlist> mac <ether_addr>",
    "show <portlist> vlan <vlanid> mac <ether_addr>",
    "show <portlist> [vlan|mac]",
    CLI_HELP_PAUSE,
    NULL};

static int
show_cmd(int argc, char **argv)
{
	struct cli_map *m;
	portlist_t portlist;
	struct ether_addr mac;

	m = cli_mapping(show_map, argc, argv);
	if (!m)
		return -1;

	switch (m->index) {
	case 10:
		rte_parse_portlist(argv[1], &portlist);
		cli_printf("   Show Portlist: %16lx\n", portlist);
		break;
	case 20:
		rte_parse_portlist(argv[1], &portlist);
		rte_ether_aton(argv[3], &mac);
		cli_printf("   Show Portlist: %016lx, MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
			   portlist,
			   mac.addr_bytes[0],
			   mac.addr_bytes[1],
			   mac.addr_bytes[2],
			   mac.addr_bytes[3],
			   mac.addr_bytes[4],
			   mac.addr_bytes[5]);
		break;
	case 30:
		rte_parse_portlist(argv[1], &portlist);
		rte_ether_aton(argv[5], &mac);
		cli_printf("   Show Portlist: %016lx vlan %d MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
			   portlist,
			   atoi(argv[3]),
			   mac.addr_bytes[0],
			   mac.addr_bytes[1],
			   mac.addr_bytes[2],
			   mac.addr_bytes[3],
			   mac.addr_bytes[4],
			   mac.addr_bytes[5]);
		break;
	case 40:
		rte_parse_portlist(argv[1], &portlist);
		rte_ether_aton("1234:4567:8901", &mac);
		cli_printf("   Show Portlist: %016lx %s: ",
			   portlist, argv[2]);
		if (argv[2][0] == 'm')
			cli_printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
				   mac.addr_bytes[0],
				   mac.addr_bytes[1],
				   mac.addr_bytes[2],
				   mac.addr_bytes[3],
				   mac.addr_bytes[4],
				   mac.addr_bytes[5]);
		else
			cli_printf("%d\n", 101);
		break;
	default:
		cli_help_show_group("Show");
		return -1;
	}
	return 0;
}

static struct cli_tree my_tree[] = {
    c_dir("/data"),
    c_file("pci", pci_file, "display lspci information"),
    c_dir("/bin"),
    c_cmd("show", show_cmd, "show mapping options"),
    c_cmd("hello", hello_cmd, "Hello-World!!"),
    c_alias("h", "history", "display history commands"),
    c_alias("ll", "ls -l", "long directory listing alias"),
    c_end()};

static int
init_tree(void)
{
	/*
	 * Root is created already and using system default cmds and dirs, the
	 * developer is not required to use the system default cmds/dirs.
	 */
	if (cli_default_tree_init())
		return -1;

	/* Using NULL here to start at root directory */
	if (cli_add_tree(NULL, my_tree))
		return -1;

	cli_help_add("Show", show_map, show_help);

	return cli_add_bin_path("/bin");
}

int rte_eal_timer_init(void);

int
main(int argc __rte_unused, char *argv[] __rte_unused)
{
	rte_eal_timer_init();

	/* Can use cli_create() or cli_create_with_defaults() */
	if (cli_create_with_tree(init_tree) == 0) {

		cli_start(NULL); /* Start collecting user input */

		cli_destroy(); /* Destroy the CLI data */
	}

	return 0;
}