2 * Copyright 2015 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
6 * Ethernet Switch commands
12 #include <env_flags.h>
15 static const char *ethsw_name;
17 #define ETHSW_PORT_STATS_HELP "ethsw [port <port_no>] statistics " \
18 "{ [help] | [clear] } - show an l2 switch port's statistics"
20 static int ethsw_port_stats_help_key_func(struct ethsw_command_def *parsed_cmd)
22 printf(ETHSW_PORT_STATS_HELP"\n");
24 return CMD_RET_SUCCESS;
27 #define ETHSW_LEARN_HELP "ethsw [port <port_no>] learning " \
28 "{ [help] | show | auto | disable } " \
29 "- enable/disable/show learning configuration on a port"
31 static int ethsw_learn_help_key_func(struct ethsw_command_def *parsed_cmd)
33 printf(ETHSW_LEARN_HELP"\n");
35 return CMD_RET_SUCCESS;
38 #define ETHSW_FDB_HELP "ethsw [port <port_no>] [vlan <vid>] fdb " \
39 "{ [help] | show | flush | { add | del } <mac> } " \
40 "- Add/delete a mac entry in FDB; use show to see FDB entries; " \
41 "if vlan <vid> is missing, VID 1 will be used"
43 static int ethsw_fdb_help_key_func(struct ethsw_command_def *parsed_cmd)
45 printf(ETHSW_FDB_HELP"\n");
47 return CMD_RET_SUCCESS;
50 #define ETHSW_PVID_HELP "ethsw [port <port_no>] " \
51 "pvid { [help] | show | <pvid> } " \
52 "- set/show PVID (ingress and egress VLAN tagging) for a port"
54 static int ethsw_pvid_help_key_func(struct ethsw_command_def *parsed_cmd)
56 printf(ETHSW_PVID_HELP"\n");
58 return CMD_RET_SUCCESS;
61 #define ETHSW_VLAN_HELP "ethsw [port <port_no>] vlan " \
62 "{ [help] | show | add <vid> | del <vid> } " \
63 "- add a VLAN to a port (VLAN members)"
65 static int ethsw_vlan_help_key_func(struct ethsw_command_def *parsed_cmd)
67 printf(ETHSW_VLAN_HELP"\n");
69 return CMD_RET_SUCCESS;
72 #define ETHSW_PORT_UNTAG_HELP "ethsw [port <port_no>] untagged " \
73 "{ [help] | show | all | none | pvid } " \
74 " - set egress tagging mod for a port"
76 static int ethsw_port_untag_help_key_func(struct ethsw_command_def *parsed_cmd)
78 printf(ETHSW_PORT_UNTAG_HELP"\n");
80 return CMD_RET_SUCCESS;
83 #define ETHSW_EGR_VLAN_TAG_HELP "ethsw [port <port_no>] egress tag " \
84 "{ [help] | show | pvid | classified } " \
85 "- Configure VID source for egress tag. " \
86 "Tag's VID could be the frame's classified VID or the PVID of the port"
88 static int ethsw_egr_tag_help_key_func(struct ethsw_command_def *parsed_cmd)
90 printf(ETHSW_EGR_VLAN_TAG_HELP"\n");
92 return CMD_RET_SUCCESS;
95 #define ETHSW_VLAN_FDB_HELP "ethsw vlan fdb " \
96 "{ [help] | show | shared | private } " \
97 "- make VLAN learning shared or private"
99 static int ethsw_vlan_learn_help_key_func(struct ethsw_command_def *parsed_cmd)
101 printf(ETHSW_VLAN_FDB_HELP"\n");
103 return CMD_RET_SUCCESS;
106 #define ETHSW_PORT_INGR_FLTR_HELP "ethsw [port <port_no>] ingress filtering" \
107 " { [help] | show | enable | disable } " \
108 "- enable/disable VLAN ingress filtering on port"
110 static int ethsw_ingr_fltr_help_key_func(struct ethsw_command_def *parsed_cmd)
112 printf(ETHSW_PORT_INGR_FLTR_HELP"\n");
114 return CMD_RET_SUCCESS;
117 static struct keywords_to_function {
118 enum ethsw_keyword_id cmd_keyword[ETHSW_MAX_CMD_PARAMS];
120 int (*keyword_function)(struct ethsw_command_def *parsed_cmd);
121 } ethsw_cmd_def[] = {
127 .cmd_func_offset = offsetof(struct ethsw_command_func,
129 .keyword_function = NULL,
135 .cmd_func_offset = offsetof(struct ethsw_command_func,
137 .keyword_function = NULL,
143 .cmd_func_offset = offsetof(struct ethsw_command_func,
145 .keyword_function = NULL,
152 .cmd_func_offset = -1,
153 .keyword_function = ðsw_port_stats_help_key_func,
159 .cmd_func_offset = offsetof(struct ethsw_command_func,
161 .keyword_function = NULL,
168 .cmd_func_offset = offsetof(struct ethsw_command_func,
170 .keyword_function = NULL,
176 .cmd_func_offset = -1,
177 .keyword_function = ðsw_learn_help_key_func,
184 .cmd_func_offset = -1,
185 .keyword_function = ðsw_learn_help_key_func,
192 .cmd_func_offset = offsetof(struct ethsw_command_func,
194 .keyword_function = NULL,
201 .cmd_func_offset = offsetof(struct ethsw_command_func,
203 .keyword_function = NULL,
210 .cmd_func_offset = offsetof(struct ethsw_command_func,
212 .keyword_function = NULL,
218 .cmd_func_offset = -1,
219 .keyword_function = ðsw_fdb_help_key_func,
226 .cmd_func_offset = -1,
227 .keyword_function = ðsw_fdb_help_key_func,
234 .cmd_func_offset = offsetof(struct ethsw_command_func,
236 .keyword_function = NULL,
243 .cmd_func_offset = offsetof(struct ethsw_command_func,
245 .keyword_function = NULL,
250 ethsw_id_add_del_mac,
253 .cmd_func_offset = offsetof(struct ethsw_command_func,
255 .keyword_function = NULL,
260 ethsw_id_add_del_mac,
263 .cmd_func_offset = offsetof(struct ethsw_command_func,
265 .keyword_function = NULL,
271 .cmd_func_offset = -1,
272 .keyword_function = ðsw_pvid_help_key_func,
279 .cmd_func_offset = -1,
280 .keyword_function = ðsw_pvid_help_key_func,
287 .cmd_func_offset = offsetof(struct ethsw_command_func,
289 .keyword_function = NULL,
296 .cmd_func_offset = offsetof(struct ethsw_command_func,
298 .keyword_function = NULL,
304 .cmd_func_offset = -1,
305 .keyword_function = ðsw_vlan_help_key_func,
312 .cmd_func_offset = -1,
313 .keyword_function = ðsw_vlan_help_key_func,
320 .cmd_func_offset = offsetof(struct ethsw_command_func,
322 .keyword_function = NULL,
330 .cmd_func_offset = offsetof(struct ethsw_command_func,
332 .keyword_function = NULL,
340 .cmd_func_offset = offsetof(struct ethsw_command_func,
342 .keyword_function = NULL,
348 .cmd_func_offset = -1,
349 .keyword_function = ðsw_port_untag_help_key_func,
356 .cmd_func_offset = -1,
357 .keyword_function = ðsw_port_untag_help_key_func,
364 .cmd_func_offset = offsetof(struct ethsw_command_func,
366 .keyword_function = NULL,
373 .cmd_func_offset = offsetof(struct ethsw_command_func,
375 .keyword_function = NULL,
382 .cmd_func_offset = offsetof(struct ethsw_command_func,
384 .keyword_function = NULL,
391 .cmd_func_offset = offsetof(struct ethsw_command_func,
393 .keyword_function = NULL,
400 .cmd_func_offset = -1,
401 .keyword_function = ðsw_egr_tag_help_key_func,
409 .cmd_func_offset = -1,
410 .keyword_function = ðsw_egr_tag_help_key_func,
418 .cmd_func_offset = offsetof(struct ethsw_command_func,
420 .keyword_function = NULL,
428 .cmd_func_offset = offsetof(struct ethsw_command_func,
430 .keyword_function = NULL,
438 .cmd_func_offset = offsetof(struct ethsw_command_func,
440 .keyword_function = NULL,
447 .cmd_func_offset = -1,
448 .keyword_function = ðsw_vlan_learn_help_key_func,
456 .cmd_func_offset = -1,
457 .keyword_function = ðsw_vlan_learn_help_key_func,
465 .cmd_func_offset = offsetof(struct ethsw_command_func,
467 .keyword_function = NULL,
475 .cmd_func_offset = offsetof(struct ethsw_command_func,
477 .keyword_function = NULL,
485 .cmd_func_offset = offsetof(struct ethsw_command_func,
487 .keyword_function = NULL,
494 .cmd_func_offset = -1,
495 .keyword_function = ðsw_ingr_fltr_help_key_func,
503 .cmd_func_offset = -1,
504 .keyword_function = ðsw_ingr_fltr_help_key_func,
512 .cmd_func_offset = offsetof(struct ethsw_command_func,
513 port_ingr_filt_show),
514 .keyword_function = NULL,
522 .cmd_func_offset = offsetof(struct ethsw_command_func,
524 .keyword_function = NULL,
532 .cmd_func_offset = offsetof(struct ethsw_command_func,
534 .keyword_function = NULL,
538 struct keywords_optional {
539 int cmd_keyword[ETHSW_MAX_CMD_PARAMS];
564 static int keyword_match_gen(enum ethsw_keyword_id key_id, int argc, char
565 *const argv[], int *argc_nr,
566 struct ethsw_command_def *parsed_cmd);
567 static int keyword_match_port(enum ethsw_keyword_id key_id, int argc,
568 char *const argv[], int *argc_nr,
569 struct ethsw_command_def *parsed_cmd);
570 static int keyword_match_vlan(enum ethsw_keyword_id key_id, int argc,
571 char *const argv[], int *argc_nr,
572 struct ethsw_command_def *parsed_cmd);
573 static int keyword_match_pvid(enum ethsw_keyword_id key_id, int argc,
574 char *const argv[], int *argc_nr,
575 struct ethsw_command_def *parsed_cmd);
576 static int keyword_match_mac_addr(enum ethsw_keyword_id key_id, int argc,
577 char *const argv[], int *argc_nr,
578 struct ethsw_command_def *parsed_cmd);
581 * Define properties for each keyword;
582 * keep the order synced with enum ethsw_keyword_id
585 const char *keyword_name;
586 int (*match)(enum ethsw_keyword_id key_id, int argc, char *const argv[],
587 int *argc_nr, struct ethsw_command_def *parsed_cmd);
590 .keyword_name = "help",
591 .match = &keyword_match_gen,
593 .keyword_name = "show",
594 .match = &keyword_match_gen,
596 .keyword_name = "port",
597 .match = &keyword_match_port
599 .keyword_name = "enable",
600 .match = &keyword_match_gen,
602 .keyword_name = "disable",
603 .match = &keyword_match_gen,
605 .keyword_name = "statistics",
606 .match = &keyword_match_gen,
608 .keyword_name = "clear",
609 .match = &keyword_match_gen,
611 .keyword_name = "learning",
612 .match = &keyword_match_gen,
614 .keyword_name = "auto",
615 .match = &keyword_match_gen,
617 .keyword_name = "vlan",
618 .match = &keyword_match_vlan,
620 .keyword_name = "fdb",
621 .match = &keyword_match_gen,
623 .keyword_name = "add",
624 .match = &keyword_match_mac_addr,
626 .keyword_name = "del",
627 .match = &keyword_match_mac_addr,
629 .keyword_name = "flush",
630 .match = &keyword_match_gen,
632 .keyword_name = "pvid",
633 .match = &keyword_match_pvid,
635 .keyword_name = "untagged",
636 .match = &keyword_match_gen,
638 .keyword_name = "all",
639 .match = &keyword_match_gen,
641 .keyword_name = "none",
642 .match = &keyword_match_gen,
644 .keyword_name = "egress",
645 .match = &keyword_match_gen,
647 .keyword_name = "tag",
648 .match = &keyword_match_gen,
650 .keyword_name = "classified",
651 .match = &keyword_match_gen,
653 .keyword_name = "shared",
654 .match = &keyword_match_gen,
656 .keyword_name = "private",
657 .match = &keyword_match_gen,
659 .keyword_name = "ingress",
660 .match = &keyword_match_gen,
662 .keyword_name = "filtering",
663 .match = &keyword_match_gen,
668 * Function used by an Ethernet Switch driver to set the functions
669 * that must be called by the parser when an ethsw command is given
671 int ethsw_define_functions(const struct ethsw_command_func *cmd_func)
675 int (*cmd_func_aux)(struct ethsw_command_def *);
677 if (!cmd_func->ethsw_name)
680 ethsw_name = cmd_func->ethsw_name;
682 for (i = 0; i < ARRAY_SIZE(ethsw_cmd_def); i++) {
684 * get the pointer to the function send by the Ethernet Switch
685 * driver that corresponds to the proper ethsw command
687 if (ethsw_cmd_def[i].keyword_function)
690 aux_p = (void *)cmd_func + ethsw_cmd_def[i].cmd_func_offset;
692 cmd_func_aux = (int (*)(struct ethsw_command_def *)) *aux_p;
693 ethsw_cmd_def[i].keyword_function = cmd_func_aux;
699 /* Generic function used to match a keyword only by a string */
700 static int keyword_match_gen(enum ethsw_keyword_id key_id, int argc,
701 char *const argv[], int *argc_nr,
702 struct ethsw_command_def *parsed_cmd)
704 if (strcmp(argv[*argc_nr], keyword[key_id].keyword_name) == 0) {
705 parsed_cmd->cmd_to_keywords[*argc_nr] = key_id;
712 /* Function used to match the command's port */
713 static int keyword_match_port(enum ethsw_keyword_id key_id, int argc,
714 char *const argv[], int *argc_nr,
715 struct ethsw_command_def *parsed_cmd)
719 if (!keyword_match_gen(key_id, argc, argv, argc_nr, parsed_cmd))
722 if (*argc_nr + 1 >= argc)
725 if (strict_strtoul(argv[*argc_nr + 1], 10, &val) != -EINVAL) {
726 parsed_cmd->port = val;
728 parsed_cmd->cmd_to_keywords[*argc_nr] = ethsw_id_port_no;
735 /* Function used to match the command's vlan */
736 static int keyword_match_vlan(enum ethsw_keyword_id key_id, int argc,
737 char *const argv[], int *argc_nr,
738 struct ethsw_command_def *parsed_cmd)
743 if (!keyword_match_gen(key_id, argc, argv, argc_nr, parsed_cmd))
746 if (*argc_nr + 1 >= argc)
749 if (strict_strtoul(argv[*argc_nr + 1], 10, &val) != -EINVAL) {
750 parsed_cmd->vid = val;
752 parsed_cmd->cmd_to_keywords[*argc_nr] = ethsw_id_vlan_no;
758 if (keyword_match_gen(ethsw_id_add, argc, argv, &aux, parsed_cmd))
759 parsed_cmd->cmd_to_keywords[*argc_nr + 1] = ethsw_id_add;
760 else if (keyword_match_gen(ethsw_id_del, argc, argv, &aux, parsed_cmd))
761 parsed_cmd->cmd_to_keywords[*argc_nr + 1] = ethsw_id_del;
765 if (*argc_nr + 2 >= argc)
768 if (strict_strtoul(argv[*argc_nr + 2], 10, &val) != -EINVAL) {
769 parsed_cmd->vid = val;
771 parsed_cmd->cmd_to_keywords[*argc_nr] = ethsw_id_add_del_no;
778 /* Function used to match the command's pvid */
779 static int keyword_match_pvid(enum ethsw_keyword_id key_id, int argc,
780 char *const argv[], int *argc_nr,
781 struct ethsw_command_def *parsed_cmd)
785 if (!keyword_match_gen(key_id, argc, argv, argc_nr, parsed_cmd))
788 if (*argc_nr + 1 >= argc)
791 if (strict_strtoul(argv[*argc_nr + 1], 10, &val) != -EINVAL) {
792 parsed_cmd->vid = val;
794 parsed_cmd->cmd_to_keywords[*argc_nr] = ethsw_id_pvid_no;
800 /* Function used to match the command's MAC address */
801 static int keyword_match_mac_addr(enum ethsw_keyword_id key_id, int argc,
802 char *const argv[], int *argc_nr,
803 struct ethsw_command_def *parsed_cmd)
805 if (!keyword_match_gen(key_id, argc, argv, argc_nr, parsed_cmd))
808 if ((*argc_nr + 1 >= argc) ||
809 !is_broadcast_ethaddr(parsed_cmd->ethaddr))
812 if (eth_validate_ethaddr_str(argv[*argc_nr + 1])) {
813 printf("Invalid MAC address: %s\n", argv[*argc_nr + 1]);
817 eth_parse_enetaddr(argv[*argc_nr + 1], parsed_cmd->ethaddr);
819 if (is_broadcast_ethaddr(parsed_cmd->ethaddr)) {
820 memset(parsed_cmd->ethaddr, 0xFF, sizeof(parsed_cmd->ethaddr));
824 parsed_cmd->cmd_to_keywords[*argc_nr + 1] = ethsw_id_add_del_mac;
829 /* Finds optional keywords and modifies *argc_va to skip them */
830 static void cmd_keywords_opt_check(const struct ethsw_command_def *parsed_cmd,
834 int keyw_opt_matched;
836 int const *cmd_keyw_p;
837 int const *cmd_keyw_opt_p;
839 /* remember the best match */
840 argc_val_max = *argc_val;
843 * check if our command's optional keywords match the optional
844 * keywords of an available command
846 for (i = 0; i < ARRAY_SIZE(ethsw_cmd_def); i++) {
847 keyw_opt_matched = 0;
848 cmd_keyw_p = &parsed_cmd->cmd_to_keywords[keyw_opt_matched];
849 cmd_keyw_opt_p = &cmd_opt_def[i].cmd_keyword[keyw_opt_matched];
852 * increase the number of keywords that
853 * matched with a command
855 while (keyw_opt_matched + *argc_val <
856 parsed_cmd->cmd_keywords_nr &&
857 *cmd_keyw_opt_p != ethsw_id_key_end &&
858 *(cmd_keyw_p + *argc_val) == *cmd_keyw_opt_p) {
865 * if all our optional command's keywords perfectly match an
866 * optional pattern, then we can move to the next defined
867 * keywords in our command; remember the one that matched the
868 * greatest number of keywords
870 if (keyw_opt_matched + *argc_val <=
871 parsed_cmd->cmd_keywords_nr &&
872 *cmd_keyw_opt_p == ethsw_id_key_end &&
873 *argc_val + keyw_opt_matched > argc_val_max)
874 argc_val_max = *argc_val + keyw_opt_matched;
877 *argc_val = argc_val_max;
881 * Finds the function to call based on keywords and
882 * modifies *argc_va to skip them
884 static void cmd_keywords_check(struct ethsw_command_def *parsed_cmd,
893 * check if our command's keywords match the
894 * keywords of an available command
896 for (i = 0; i < ARRAY_SIZE(ethsw_cmd_def); i++) {
898 cmd_keyw_p = &parsed_cmd->cmd_to_keywords[keyw_matched];
899 cmd_keyw_def_p = ðsw_cmd_def[i].cmd_keyword[keyw_matched];
902 * increase the number of keywords that
903 * matched with a command
905 while (keyw_matched + *argc_val < parsed_cmd->cmd_keywords_nr &&
906 *cmd_keyw_def_p != ethsw_id_key_end &&
907 *(cmd_keyw_p + *argc_val) == *cmd_keyw_def_p) {
914 * if all our command's keywords perfectly match an
915 * available command, then we get the function we need to call
916 * to configure the Ethernet Switch
918 if (keyw_matched && keyw_matched + *argc_val ==
919 parsed_cmd->cmd_keywords_nr &&
920 *cmd_keyw_def_p == ethsw_id_key_end) {
921 *argc_val += keyw_matched;
922 parsed_cmd->cmd_function =
923 ethsw_cmd_def[i].keyword_function;
929 /* find all the keywords in the command */
930 static int keywords_find(int argc, char * const argv[],
931 struct ethsw_command_def *parsed_cmd)
936 int rc = CMD_RET_SUCCESS;
938 for (i = 1; i < argc; i++) {
939 for (j = 0; j < ethsw_id_count; j++) {
940 if (keyword[j].match(j, argc, argv, &i, parsed_cmd))
945 /* if there is no keyword match for a word, the command is invalid */
946 for (i = 1; i < argc; i++)
947 if (parsed_cmd->cmd_to_keywords[i] == ethsw_id_key_end)
950 parsed_cmd->cmd_keywords_nr = argc;
953 /* get optional parameters first */
954 cmd_keywords_opt_check(parsed_cmd, &argc_val);
956 if (argc_val == parsed_cmd->cmd_keywords_nr)
957 return CMD_RET_USAGE;
960 * check the keywords and if a match is found,
961 * get the function to call
963 cmd_keywords_check(parsed_cmd, &argc_val);
965 /* error if not all commands' parameters were matched */
966 if (argc_val == parsed_cmd->cmd_keywords_nr) {
967 if (!parsed_cmd->cmd_function) {
968 printf("Command not available for: %s\n", ethsw_name);
969 rc = CMD_RET_FAILURE;
978 static void command_def_init(struct ethsw_command_def *parsed_cmd)
982 for (i = 0; i < ETHSW_MAX_CMD_PARAMS; i++)
983 parsed_cmd->cmd_to_keywords[i] = ethsw_id_key_end;
985 parsed_cmd->port = ETHSW_CMD_PORT_ALL;
986 parsed_cmd->vid = ETHSW_CMD_VLAN_ALL;
987 parsed_cmd->cmd_function = NULL;
989 /* We initialize the MAC address with the Broadcast address */
990 memset(parsed_cmd->ethaddr, 0xff, sizeof(parsed_cmd->ethaddr));
993 /* function to interpret commands starting with "ethsw " */
994 static int do_ethsw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
996 struct ethsw_command_def parsed_cmd;
997 int rc = CMD_RET_SUCCESS;
999 if (argc == 1 || argc >= ETHSW_MAX_CMD_PARAMS)
1000 return CMD_RET_USAGE;
1002 command_def_init(&parsed_cmd);
1004 rc = keywords_find(argc, argv, &parsed_cmd);
1006 if (rc == CMD_RET_SUCCESS)
1007 rc = parsed_cmd.cmd_function(&parsed_cmd);
1012 #define ETHSW_PORT_CONF_HELP "[port <port_no>] { enable | disable | show } " \
1013 "- enable/disable a port; show shows a port's configuration"
1015 U_BOOT_CMD(ethsw, ETHSW_MAX_CMD_PARAMS, 0, do_ethsw,
1016 "Ethernet l2 switch commands",
1017 ETHSW_PORT_CONF_HELP"\n"
1018 ETHSW_PORT_STATS_HELP"\n"
1019 ETHSW_LEARN_HELP"\n"
1023 ETHSW_PORT_UNTAG_HELP"\n"
1024 ETHSW_EGR_VLAN_TAG_HELP"\n"
1025 ETHSW_VLAN_FDB_HELP"\n"
1026 ETHSW_PORT_INGR_FLTR_HELP"\n"