From 2f7ef7dc6cb542d5811f64fa7af97fb8619c9429 Mon Sep 17 00:00:00 2001 From: Hans Dedecker Date: Sun, 11 Nov 2018 21:15:56 +0100 Subject: interface: rework code to get rid of interface_set_dynamic Integrate dynamic interface creation code into interface_alloc and __interface_add so we can get rid of interface_set_dynamic Signed-off-by: Hans Dedecker --- config.c | 2 +- interface.c | 28 ++++++++++++++++++---------- interface.h | 6 ++---- ubus.c | 19 ++++++------------- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/config.c b/config.c index 536b7d3..a1267b0 100644 --- a/config.c +++ b/config.c @@ -102,7 +102,7 @@ config_parse_interface(struct uci_section *s, bool alias) uci_to_blob(&b, s, &interface_attr_list); - iface = interface_alloc(s->e.name, b.head); + iface = interface_alloc(s->e.name, b.head, false); if (!iface) return; diff --git a/interface.c b/interface.c index b508b10..36f5480 100644 --- a/interface.c +++ b/interface.c @@ -768,7 +768,7 @@ void interface_set_proto_state(struct interface *iface, struct interface_proto_s } struct interface * -interface_alloc(const char *name, struct blob_attr *config) +interface_alloc(const char *name, struct blob_attr *config, bool dynamic) { struct interface *iface; struct blob_attr *tb[IFACE_ATTR_MAX]; @@ -803,6 +803,7 @@ interface_alloc(const char *name, struct blob_attr *config) iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true); iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link); + iface->dynamic = dynamic; iface->proto_ip.no_defaultroute = !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true); iface->proto_ip.no_dns = @@ -877,17 +878,11 @@ interface_alloc(const char *name, struct blob_attr *config) return iface; } -void interface_set_dynamic(struct interface *iface) -{ - iface->dynamic = true; - iface->autostart = true; - iface->node.version = -1; // Don't delete on reload -} - static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias) { struct blob_attr *tb[IFACE_ATTR_MAX]; struct blob_attr *cur; + char *name = iface->dynamic ? strdup(iface->name) : NULL; blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(config), blob_len(config)); @@ -905,13 +900,25 @@ static bool __interface_add(struct interface *iface, struct blob_attr *config, b iface->config = config; vlist_add(&interfaces, &iface->node, iface->name); + + if (name) { + iface = vlist_find(&interfaces, name, iface, node); + free(name); + + if (!iface) + return false; + + /* Don't delete dynamic interface on reload */ + iface->node.version = -1; + } + return true; } -void +bool interface_add(struct interface *iface, struct blob_attr *config) { - __interface_add(iface, config, false); + return __interface_add(iface, config, false); } bool @@ -1220,6 +1227,7 @@ interface_change_config(struct interface *if_old, struct interface *if_new) if_old->config_autostart = if_new->config_autostart; if_old->ifname = if_new->ifname; if_old->parent_ifname = if_new->parent_ifname; + if_old->dynamic = if_new->dynamic; if_old->proto_handler = if_new->proto_handler; if_old->force_link = if_new->force_link; if_old->dns_metric = if_new->dns_metric; diff --git a/interface.h b/interface.h index ace6a5d..f92a35e 100644 --- a/interface.h +++ b/interface.h @@ -174,11 +174,9 @@ struct interface { extern struct vlist_tree interfaces; extern const struct uci_blob_param_list interface_attr_list; -struct interface *interface_alloc(const char *name, struct blob_attr *config); +struct interface *interface_alloc(const char *name, struct blob_attr *config, bool dynamic); -void interface_set_dynamic(struct interface *iface); - -void interface_add(struct interface *iface, struct blob_attr *config); +bool interface_add(struct interface *iface, struct blob_attr *config); bool interface_add_alias(struct interface *iface, struct blob_attr *config); void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state); diff --git a/ubus.c b/ubus.c index 9c5b8b1..f5e4997 100644 --- a/ubus.c +++ b/ubus.c @@ -137,29 +137,22 @@ netifd_add_dynamic(struct ubus_context *ctx, struct ubus_object *obj, const char *name = blobmsg_get_string(tb[DI_NAME]); - iface = interface_alloc(name, msg); + iface = interface_alloc(name, msg, true); if (!iface) return UBUS_STATUS_UNKNOWN_ERROR; config = blob_memdup(msg); if (!config) - goto error; - - interface_add(iface, config); - - // need to look up the interface name again, in case of config update - // the pointer will have changed - iface = vlist_find(&interfaces, name, iface, node); - if (!iface) - return UBUS_STATUS_UNKNOWN_ERROR; + goto error_free; - // Set interface as dynamic - interface_set_dynamic(iface); + if (!interface_add(iface, config)) + goto error; return UBUS_STATUS_OK; -error: +error_free: free(iface); +error: return UBUS_STATUS_UNKNOWN_ERROR; } -- cgit v1.2.3