From dd5c70707e9c3779ce8d26dc505894b45ac42061 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Wed, 8 May 2024 18:17:08 +0200 Subject: [PATCH 1/2] ubus: add command to create devices dynamically Some kind of devices might become available after initial configuration, and other one like WDS stations or APuP peers can create unpredictably many linux interfaces like wlan0.peer1 wlan0.peer2... To use those linux interfaces as a base for other devices on top of those like 802.1ad devices we need to be able to create them at runtime ofter thos comes up, so an ubus command have been added for that. ubus call network add_dynamic_device \ '{"name":"NewDeviceName", "type":"8021ad", \ "ifname":"wlan0.peer1", "vid":"47"}' This will create the device that can be used for one or more interface with pre-existent commands ubus call network add_dynamic \ '{"name":"NewInterfaceName", "proto":"static", "auto":1, \ "device":"NewDeviceName", \ "ipaddr":"192.0.2.0", "netmask":"255.255.255.255"}' ubus call network.interface.NewInterfaceName up To inspect the newly created device status a pre-existent command can be used ubus call network.device status '{"name":"NewDeviceName"}' Signed-off-by: Gioacchino Mazzurco --- ubus.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/ubus.c b/ubus.c index f8662c2..5cdc116 100644 --- a/ubus.c +++ b/ubus.c @@ -163,6 +163,57 @@ netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj, return UBUS_STATUS_UNKNOWN_ERROR; } +enum { + DI_DEV_NAME, + DI_DEV_TYPE, + __DI_DEV_MAX +}; + +static const struct blobmsg_policy dynamic_device_policy[__DI_DEV_MAX] = { + [DI_DEV_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING }, + [DI_DEV_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING } +}; + +static int +netifd_add_dynamic_device( + struct ubus_context */*ctx*/, struct ubus_object */*obj*/, + struct ubus_request_data */*req*/, const char */*method*/, + struct blob_attr *msg ) +{ + struct blob_attr *tb[__DI_DEV_MAX]; + struct device *device; + struct device_type *type; + struct blob_attr *config; + + blobmsg_parse( dynamic_device_policy, __DI_DEV_MAX, tb, + blob_data(msg), blob_len(msg) ); + + if (!tb[DI_DEV_NAME] || !tb[DI_DEV_TYPE]) + return UBUS_STATUS_INVALID_ARGUMENT; + + const char *name = blobmsg_get_string(tb[DI_DEV_NAME]); + const char *type_name = blobmsg_get_string(tb[DI_DEV_TYPE]); + + type = device_type_get(type_name); + + if (!type) + return UBUS_STATUS_INVALID_ARGUMENT; + + config = blob_memdup(msg); + if (!config) + return UBUS_STATUS_UNKNOWN_ERROR; + + device = device_create(name, type, config); + if (!device) + goto error_free_config; + + return UBUS_STATUS_OK; + +error_free_config: + free(config); + return UBUS_STATUS_UNKNOWN_ERROR; +} + enum { NETNS_UPDOWN_JAIL, NETNS_UPDOWN_START, @@ -209,6 +260,7 @@ static struct ubus_method main_object_methods[] = { UBUS_METHOD("add_host_route", netifd_add_host_route, route_policy), { .name = "get_proto_handlers", .handler = netifd_get_proto_handlers }, UBUS_METHOD("add_dynamic", netifd_add_dynamic, dynamic_policy), + UBUS_METHOD("add_dynamic_device", netifd_add_dynamic_device, dynamic_device_policy), UBUS_METHOD("netns_updown", netifd_netns_updown, netns_updown_policy), }; From f4f5a1617504ed1809fa7228bb62b28f1f668ea3 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 18 May 2024 20:12:58 +0200 Subject: [PATCH 2/2] WIP: debugging ubus call add_dynamic_device --- device.c | 3 ++- netifd.h | 2 +- ubus.c | 2 ++ vlandev.c | 2 ++ 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/device.c b/device.c index 0ec3409..bd27b5f 100644 --- a/device.c +++ b/device.c @@ -1317,7 +1317,8 @@ device_create(const char *name, struct device_type *type, dev->config_pending = false; } - device_check_state(dev); + int checkret = device_check_state(dev); + D(DEVICE, "%s device_check_state return %d\n", __func__, checkret); return dev; } diff --git a/netifd.h b/netifd.h index a462abd..f1d805c 100644 --- a/netifd.h +++ b/netifd.h @@ -69,7 +69,7 @@ enum { DEBUG_WIRELESS = 3, }; -#ifdef DEBUG +#if 1 // def DEBUG #define DPRINTF(format, ...) fprintf(stderr, "%s(%d): " format, __func__, __LINE__, ## __VA_ARGS__) #define D(level, format, ...) do { \ netifd_udebug_printf("[" #level "] %s(%d): " format, __func__, __LINE__, ## __VA_ARGS__); \ diff --git a/ubus.c b/ubus.c index 5cdc116..9e4dbab 100644 --- a/ubus.c +++ b/ubus.c @@ -180,6 +180,8 @@ netifd_add_dynamic_device( struct ubus_request_data */*req*/, const char */*method*/, struct blob_attr *msg ) { + D(DEVICE, "%s\n", __func__); + struct blob_attr *tb[__DI_DEV_MAX]; struct device *device; struct device_type *type; diff --git a/vlandev.c b/vlandev.c index 4d6e76a..6f76122 100644 --- a/vlandev.c +++ b/vlandev.c @@ -384,6 +384,8 @@ static struct device * vlandev_create(const char *name, struct device_type *devtype, struct blob_attr *attr) { + D(DEVICE, "%s\n", __func__); + struct vlandev_device *mvdev; struct device *dev = NULL;