|
8 | 8 | #include <zephyr/logging/log.h>
|
9 | 9 | LOG_MODULE_DECLARE(net_shell);
|
10 | 10 |
|
| 11 | +#include <stdlib.h> |
| 12 | + |
11 | 13 | #if defined(CONFIG_NET_L2_VIRTUAL)
|
12 | 14 | #include <zephyr/net/virtual.h>
|
13 | 15 | #endif
|
@@ -84,7 +86,7 @@ static void attached_iface_cb(struct net_if *iface, void *user_data)
|
84 | 86 | }
|
85 | 87 | #endif /* CONFIG_NET_L2_VIRTUAL */
|
86 | 88 |
|
87 |
| -static int cmd_net_virtual(const struct shell *sh, size_t argc, char *argv[]) |
| 89 | +static int cmd_virtual_show(const struct shell *sh, size_t argc, char *argv[]) |
88 | 90 | {
|
89 | 91 | ARG_UNUSED(argc);
|
90 | 92 | ARG_UNUSED(argv);
|
@@ -113,6 +115,76 @@ static int cmd_net_virtual(const struct shell *sh, size_t argc, char *argv[])
|
113 | 115 | return 0;
|
114 | 116 | }
|
115 | 117 |
|
116 |
| -SHELL_SUBCMD_ADD((net), virtual, NULL, |
117 |
| - "Show virtual network interfaces.", |
118 |
| - cmd_net_virtual, 1, 0); |
| 118 | +static int cmd_virtual_attach(const struct shell *sh, size_t argc, char *argv[]) |
| 119 | +{ |
| 120 | +#if defined(CONFIG_NET_L2_VIRTUAL) |
| 121 | + struct net_if *virtual_iface, *lower_iface; |
| 122 | + int ret; |
| 123 | + |
| 124 | + virtual_iface = net_if_get_by_index(atoi(argv[1])); |
| 125 | + if (virtual_iface == NULL) { |
| 126 | + PR("No %s interface %s found.\n", "virtual", argv[1]); |
| 127 | + return -ENOENT; |
| 128 | + } |
| 129 | + |
| 130 | + lower_iface = net_if_get_by_index(atoi(argv[2])); |
| 131 | + if (lower_iface == NULL) { |
| 132 | + PR("No %s interface %s found.\n", "such", argv[2]); |
| 133 | + return -ENOENT; |
| 134 | + } |
| 135 | + |
| 136 | + ret = net_virtual_interface_attach(virtual_iface, lower_iface); |
| 137 | + if (ret < 0) { |
| 138 | + PR("Cannot attach interface %s to %s (%d)\n", argv[1], argv[2], ret); |
| 139 | + return -ENOENT; |
| 140 | + } |
| 141 | +#else |
| 142 | + PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_L2_VIRTUAL", |
| 143 | + "virtual network interface"); |
| 144 | +#endif |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +static int cmd_virtual_detach(const struct shell *sh, size_t argc, char *argv[]) |
| 149 | +{ |
| 150 | +#if defined(CONFIG_NET_L2_VIRTUAL) |
| 151 | + struct net_if *virtual_iface; |
| 152 | + int ret; |
| 153 | + |
| 154 | + virtual_iface = net_if_get_by_index(atoi(argv[1])); |
| 155 | + if (virtual_iface == NULL) { |
| 156 | + PR("No %s interface %s found.\n", "virtual", argv[1]); |
| 157 | + return -ENOENT; |
| 158 | + } |
| 159 | + |
| 160 | + ret = net_virtual_interface_attach(virtual_iface, NULL); |
| 161 | + if (ret < 0) { |
| 162 | + PR("Cannot detach interface %s (%d)\n", argv[1], ret); |
| 163 | + return -ENOENT; |
| 164 | + } |
| 165 | +#else |
| 166 | + PR_INFO("Set %s to enable %s support.\n", "CONFIG_NET_L2_VIRTUAL", |
| 167 | + "virtual network interface"); |
| 168 | +#endif |
| 169 | + return 0; |
| 170 | +} |
| 171 | + |
| 172 | +SHELL_STATIC_SUBCMD_SET_CREATE(virtual_commands, |
| 173 | + SHELL_CMD_ARG(attach, NULL, |
| 174 | + "Attach a network interface to another interface.\n" |
| 175 | + "'virtual attach <upper virtual iface index> <lower iface index>'", |
| 176 | + cmd_virtual_attach, 3, 0), |
| 177 | + SHELL_CMD_ARG(detach, NULL, |
| 178 | + "Detach a network interface from another interface.\n" |
| 179 | + "'virtual detach <upper virtual iface index>'", |
| 180 | + cmd_virtual_detach, 2, 0), |
| 181 | + SHELL_CMD_ARG(show, NULL, |
| 182 | + "Show virtual interface information.\n" |
| 183 | + "'virtual show'", |
| 184 | + cmd_virtual_show, 1, 1), |
| 185 | + SHELL_SUBCMD_SET_END |
| 186 | +); |
| 187 | + |
| 188 | +SHELL_SUBCMD_ADD((net), virtual, &virtual_commands, |
| 189 | + "Show/manipulate virtual network interfaces.", |
| 190 | + cmd_virtual_show, 1, 1); |
0 commit comments