interface: support "device" attribute and deprecate "ifname"
authorRafał Miłecki <rafal@milecki.pl>
Tue, 25 May 2021 15:17:26 +0000 (17:17 +0200)
committerRafał Miłecki <rafal@milecki.pl>
Wed, 26 May 2021 05:52:27 +0000 (07:52 +0200)
Interfaces need to be assigned to devices. For that purpose a "device"
option should be more accurate than "ifname" one.

For backward compatibility old option remains supported too.

Config example:

config device
option name 'br-lan'
option type 'bridge'
list ports 'lan1'
list ports 'lan2'

config interface 'lan'
option device 'br-lan'
option proto 'static'

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
interface.c
interface.h

index a91246a1ae03769f16e798af7f063b6cf264d187..1d1a5f8cb8db260f3e350e5d5e870f26694230a0 100644 (file)
@@ -30,7 +30,8 @@ struct vlist_tree interfaces;
 static LIST_HEAD(iface_all_users);
 
 enum {
-       IFACE_ATTR_IFNAME,
+       IFACE_ATTR_DEVICE,
+       IFACE_ATTR_IFNAME, /* Backward compatibility */
        IFACE_ATTR_PROTO,
        IFACE_ATTR_AUTO,
        IFACE_ATTR_JAIL,
@@ -55,6 +56,7 @@ enum {
 };
 
 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
+       [IFACE_ATTR_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
@@ -641,9 +643,9 @@ interface_claim_device(struct interface *iface)
                parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
                iface->parent_iface.cb = interface_alias_cb;
                interface_add_user(&iface->parent_iface, parent);
-       } else if (iface->ifname &&
+       } else if (iface->device &&
                !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
-               dev = device_get(iface->ifname, true);
+               dev = device_get(iface->device, true);
                interface_set_device_config(iface, dev);
        } else {
                dev = iface->ext_dev.dev;
@@ -939,8 +941,11 @@ static bool __interface_add(struct interface *iface, struct blob_attr *config, b
                if (!iface->parent_ifname)
                        return false;
        } else {
-               if ((cur = tb[IFACE_ATTR_IFNAME]))
-                       iface->ifname = blobmsg_data(cur);
+               cur = tb[IFACE_ATTR_DEVICE];
+               if (!cur)
+                       cur = tb[IFACE_ATTR_IFNAME];
+               if (cur)
+                       iface->device = blobmsg_data(cur);
        }
 
        if (iface->dynamic) {
@@ -1216,7 +1221,7 @@ interface_start_jail(const char *jail, const pid_t netns_pid)
                 * list, so we can mess with it :)
                 */
                if (iface->jail_ifname)
-                       iface->ifname = iface->jail_ifname;
+                       iface->device = iface->jail_ifname;
 
                interface_do_reload(iface);
                interface_set_up(iface);
@@ -1257,9 +1262,9 @@ interface_stop_jail(const char *jail, const pid_t netns_pid)
                if (!iface->jail || strcmp(iface->jail, jail))
                        continue;
 
-               orig_ifname = iface->ifname;
+               orig_ifname = iface->device;
                if (iface->jail_ifname)
-                       iface->ifname = iface->jail_ifname;
+                       iface->device = iface->jail_ifname;
 
                interface_do_reload(iface);
                interface_set_down(iface);
@@ -1352,7 +1357,7 @@ interface_change_config(struct interface *if_old, struct interface *if_new)
        if (!reload && interface_device_config_changed(if_old, if_new))
                reload = true;
 
-       if (FIELD_CHANGED_STR(ifname) ||
+       if (FIELD_CHANGED_STR(device) ||
            if_old->proto_handler != if_new->proto_handler)
                reload = true;
 
@@ -1391,7 +1396,7 @@ interface_change_config(struct interface *if_old, struct interface *if_new)
 
        if_old->jail_ifname = if_new->jail_ifname;
 
-       if_old->ifname = if_new->ifname;
+       if_old->device = if_new->device;
        if_old->parent_ifname = if_new->parent_ifname;
        if_old->dynamic = if_new->dynamic;
        if_old->proto_handler = if_new->proto_handler;
index 9c136b63e3774f6b146efc8636f7a3f76d0af299..1767bf1dd63c8fd5423f8b6118e05c2da87ebd38 100644 (file)
@@ -107,7 +107,7 @@ struct interface {
        enum interface_event hotplug_ev;
 
        const char *name;
-       const char *ifname;
+       const char *device;
        char *jail;
        char *jail_ifname;
        int netns_fd;