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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/*
* Copyright 2015 Freescale Semiconductor, Inc.
*
* SPDX-License-Identifier: GPL-2.0+
*
* Ethernet Switch commands
*/
#include <common.h>
#include <command.h>
#include <errno.h>
#include <ethsw.h>
static const char *ethsw_name;
#define ETHSW_PORT_STATS_HELP "ethsw [port <port_no>] statistics " \
"{ [help] | [clear] } - show an l2 switch port's statistics"
static int ethsw_port_stats_help_key_func(struct ethsw_command_def *parsed_cmd)
{
printf(ETHSW_PORT_STATS_HELP"\n");
return CMD_RET_SUCCESS;
}
static struct keywords_to_function {
enum ethsw_keyword_id cmd_keyword[ETHSW_MAX_CMD_PARAMS];
int cmd_func_offset;
int (*keyword_function)(struct ethsw_command_def *parsed_cmd);
} ethsw_cmd_def[] = {
{
.cmd_keyword = {
ethsw_id_enable,
ethsw_id_key_end,
},
.cmd_func_offset = offsetof(struct ethsw_command_func,
port_enable),
.keyword_function = NULL,
}, {
.cmd_keyword = {
ethsw_id_disable,
ethsw_id_key_end,
},
.cmd_func_offset = offsetof(struct ethsw_command_func,
port_disable),
.keyword_function = NULL,
}, {
.cmd_keyword = {
ethsw_id_show,
ethsw_id_key_end,
},
.cmd_func_offset = offsetof(struct ethsw_command_func,
port_show),
.keyword_function = NULL,
}, {
.cmd_keyword = {
ethsw_id_statistics,
ethsw_id_help,
ethsw_id_key_end,
},
.cmd_func_offset = -1,
.keyword_function = ðsw_port_stats_help_key_func,
}, {
.cmd_keyword = {
ethsw_id_statistics,
ethsw_id_key_end,
},
.cmd_func_offset = offsetof(struct ethsw_command_func,
port_stats),
.keyword_function = NULL,
}, {
.cmd_keyword = {
ethsw_id_statistics,
ethsw_id_clear,
ethsw_id_key_end,
},
.cmd_func_offset = offsetof(struct ethsw_command_func,
port_stats_clear),
.keyword_function = NULL,
},
};
struct keywords_optional {
int cmd_keyword[ETHSW_MAX_CMD_PARAMS];
} cmd_opt_def[] = {
{
.cmd_keyword = {
ethsw_id_port,
ethsw_id_port_no,
ethsw_id_key_end,
},
},
};
static int keyword_match_gen(enum ethsw_keyword_id key_id, int argc, char
*const argv[], int *argc_nr,
struct ethsw_command_def *parsed_cmd);
static int keyword_match_port(enum ethsw_keyword_id key_id, int argc,
char *const argv[], int *argc_nr,
struct ethsw_command_def *parsed_cmd);
/*
* Define properties for each keyword;
* keep the order synced with enum ethsw_keyword_id
*/
struct keyword_def {
const char *keyword_name;
int (*match)(enum ethsw_keyword_id key_id, int argc, char *const argv[],
int *argc_nr, struct ethsw_command_def *parsed_cmd);
} keyword[] = {
{
.keyword_name = "help",
.match = &keyword_match_gen,
}, {
.keyword_name = "show",
.match = &keyword_match_gen,
}, {
.keyword_name = "port",
.match = &keyword_match_port
}, {
.keyword_name = "enable",
.match = &keyword_match_gen,
}, {
.keyword_name = "disable",
.match = &keyword_match_gen,
}, {
.keyword_name = "statistics",
.match = &keyword_match_gen,
}, {
.keyword_name = "clear",
.match = &keyword_match_gen,
},
};
/*
* Function used by an Ethernet Switch driver to set the functions
* that must be called by the parser when an ethsw command is given
*/
int ethsw_define_functions(const struct ethsw_command_func *cmd_func)
{
int i;
void **aux_p;
int (*cmd_func_aux)(struct ethsw_command_def *);
if (!cmd_func->ethsw_name)
return -EINVAL;
ethsw_name = cmd_func->ethsw_name;
for (i = 0; i < ARRAY_SIZE(ethsw_cmd_def); i++) {
/*
* get the pointer to the function send by the Ethernet Switch
* driver that corresponds to the proper ethsw command
*/
if (ethsw_cmd_def[i].keyword_function)
continue;
aux_p = (void *)cmd_func + ethsw_cmd_def[i].cmd_func_offset;
cmd_func_aux = (int (*)(struct ethsw_command_def *)) *aux_p;
ethsw_cmd_def[i].keyword_function = cmd_func_aux;
}
return 0;
}
/* Generic function used to match a keyword only by a string */
static int keyword_match_gen(enum ethsw_keyword_id key_id, int argc,
char *const argv[], int *argc_nr,
struct ethsw_command_def *parsed_cmd)
{
if (strcmp(argv[*argc_nr], keyword[key_id].keyword_name) == 0) {
parsed_cmd->cmd_to_keywords[*argc_nr] = key_id;
return 1;
}
return 0;
}
/* Function used to match the command's port */
static int keyword_match_port(enum ethsw_keyword_id key_id, int argc,
char *const argv[], int *argc_nr,
struct ethsw_command_def *parsed_cmd)
{
unsigned long val;
if (!keyword_match_gen(key_id, argc, argv, argc_nr, parsed_cmd))
return 0;
if (*argc_nr + 1 >= argc)
return 0;
if (strict_strtoul(argv[*argc_nr + 1], 10, &val) != -EINVAL) {
parsed_cmd->port = val;
(*argc_nr)++;
parsed_cmd->cmd_to_keywords[*argc_nr] = ethsw_id_port_no;
return 1;
}
return 0;
}
/* Finds optional keywords and modifies *argc_va to skip them */
static void cmd_keywords_opt_check(const struct ethsw_command_def *parsed_cmd,
int *argc_val)
{
int i;
int keyw_opt_matched;
int argc_val_max;
int const *cmd_keyw_p;
int const *cmd_keyw_opt_p;
/* remember the best match */
argc_val_max = *argc_val;
/*
* check if our command's optional keywords match the optional
* keywords of an available command
*/
for (i = 0; i < ARRAY_SIZE(ethsw_cmd_def); i++) {
keyw_opt_matched = 0;
cmd_keyw_p = &parsed_cmd->cmd_to_keywords[keyw_opt_matched];
cmd_keyw_opt_p = &cmd_opt_def[i].cmd_keyword[keyw_opt_matched];
/*
* increase the number of keywords that
* matched with a command
*/
while (keyw_opt_matched + *argc_val <
parsed_cmd->cmd_keywords_nr &&
*cmd_keyw_opt_p != ethsw_id_key_end &&
*(cmd_keyw_p + *argc_val) == *cmd_keyw_opt_p) {
keyw_opt_matched++;
cmd_keyw_p++;
cmd_keyw_opt_p++;
}
/*
* if all our optional command's keywords perfectly match an
* optional pattern, then we can move to the next defined
* keywords in our command; remember the one that matched the
* greatest number of keywords
*/
if (keyw_opt_matched + *argc_val <=
parsed_cmd->cmd_keywords_nr &&
*cmd_keyw_opt_p == ethsw_id_key_end &&
*argc_val + keyw_opt_matched > argc_val_max)
argc_val_max = *argc_val + keyw_opt_matched;
}
*argc_val = argc_val_max;
}
/*
* Finds the function to call based on keywords and
* modifies *argc_va to skip them
*/
static void cmd_keywords_check(struct ethsw_command_def *parsed_cmd,
int *argc_val)
{
int i;
int keyw_matched;
int *cmd_keyw_p;
int *cmd_keyw_def_p;
/*
* check if our command's keywords match the
* keywords of an available command
*/
for (i = 0; i < ARRAY_SIZE(ethsw_cmd_def); i++) {
keyw_matched = 0;
cmd_keyw_p = &parsed_cmd->cmd_to_keywords[keyw_matched];
cmd_keyw_def_p = ðsw_cmd_def[i].cmd_keyword[keyw_matched];
/*
* increase the number of keywords that
* matched with a command
*/
while (keyw_matched + *argc_val < parsed_cmd->cmd_keywords_nr &&
*cmd_keyw_def_p != ethsw_id_key_end &&
*(cmd_keyw_p + *argc_val) == *cmd_keyw_def_p) {
keyw_matched++;
cmd_keyw_p++;
cmd_keyw_def_p++;
}
/*
* if all our command's keywords perfectly match an
* available command, then we get the function we need to call
* to configure the Ethernet Switch
*/
if (keyw_matched && keyw_matched + *argc_val ==
parsed_cmd->cmd_keywords_nr &&
*cmd_keyw_def_p == ethsw_id_key_end) {
*argc_val += keyw_matched;
parsed_cmd->cmd_function =
ethsw_cmd_def[i].keyword_function;
return;
}
}
}
/* find all the keywords in the command */
static int keywords_find(int argc, char * const argv[],
struct ethsw_command_def *parsed_cmd)
{
int i;
int j;
int argc_val;
int rc = CMD_RET_SUCCESS;
for (i = 1; i < argc; i++) {
for (j = 0; j < ethsw_id_count; j++) {
if (keyword[j].match(j, argc, argv, &i, parsed_cmd))
break;
}
}
/* if there is no keyword match for a word, the command is invalid */
for (i = 1; i < argc; i++)
if (parsed_cmd->cmd_to_keywords[i] == ethsw_id_key_end)
rc = CMD_RET_USAGE;
parsed_cmd->cmd_keywords_nr = argc;
argc_val = 1;
/* get optional parameters first */
cmd_keywords_opt_check(parsed_cmd, &argc_val);
if (argc_val == parsed_cmd->cmd_keywords_nr)
return CMD_RET_USAGE;
/*
* check the keywords and if a match is found,
* get the function to call
*/
cmd_keywords_check(parsed_cmd, &argc_val);
/* error if not all commands' parameters were matched */
if (argc_val == parsed_cmd->cmd_keywords_nr) {
if (!parsed_cmd->cmd_function) {
printf("Command not available for: %s\n", ethsw_name);
rc = CMD_RET_FAILURE;
}
} else {
rc = CMD_RET_USAGE;
}
return rc;
}
static void command_def_init(struct ethsw_command_def *parsed_cmd)
{
int i;
for (i = 0; i < ETHSW_MAX_CMD_PARAMS; i++)
parsed_cmd->cmd_to_keywords[i] = ethsw_id_key_end;
parsed_cmd->port = ETHSW_CMD_PORT_ALL;
parsed_cmd->cmd_function = NULL;
}
/* function to interpret commands starting with "ethsw " */
static int do_ethsw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
struct ethsw_command_def parsed_cmd;
int rc = CMD_RET_SUCCESS;
if (argc == 1 || argc >= ETHSW_MAX_CMD_PARAMS)
return CMD_RET_USAGE;
command_def_init(&parsed_cmd);
rc = keywords_find(argc, argv, &parsed_cmd);
if (rc == CMD_RET_SUCCESS)
rc = parsed_cmd.cmd_function(&parsed_cmd);
return rc;
}
#define ETHSW_PORT_CONF_HELP "[port <port_no>] { enable | disable | show } " \
"- enable/disable a port; show shows a port's configuration"
U_BOOT_CMD(ethsw, ETHSW_MAX_CMD_PARAMS, 0, do_ethsw,
"Ethernet l2 switch commands",
ETHSW_PORT_CONF_HELP"\n"
ETHSW_PORT_STATS_HELP"\n"
);
|