Skip to content

Commit 7e4a2c6

Browse files
jukkarfabiobaltieri
authored andcommitted
net: shell: virtual: Add attach/detach commands
Allow "net virtual" command to attach or detach virtual interfaces. This is useful for device management. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 21558df commit 7e4a2c6

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

subsys/net/lib/shell/virtual.c

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <zephyr/logging/log.h>
99
LOG_MODULE_DECLARE(net_shell);
1010

11+
#include <stdlib.h>
12+
1113
#if defined(CONFIG_NET_L2_VIRTUAL)
1214
#include <zephyr/net/virtual.h>
1315
#endif
@@ -84,7 +86,7 @@ static void attached_iface_cb(struct net_if *iface, void *user_data)
8486
}
8587
#endif /* CONFIG_NET_L2_VIRTUAL */
8688

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[])
8890
{
8991
ARG_UNUSED(argc);
9092
ARG_UNUSED(argv);
@@ -113,6 +115,76 @@ static int cmd_net_virtual(const struct shell *sh, size_t argc, char *argv[])
113115
return 0;
114116
}
115117

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

Comments
 (0)