As device name is used as key in avl list a device name change will break the avl find logic.
Function device_set_ifname offers api to set the device name and re-inserts the avl node in the list
when the avl key value is changed.
Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
alias->dev.hidden = !dev;
if (dev) {
device_set_ifindex(&alias->dev, dev->ifindex);
- strcpy(alias->dev.ifname, dev->ifname);
- device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
+ device_set_ifname(&alias->dev, dev->ifname);
device_add_user(&alias->dep, dev);
- } else {
- alias->dev.ifname[0] = 0;
- device_broadcast_event(&alias->dev, DEV_EVENT_UPDATE_IFNAME);
- }
+ } else
+ device_set_ifname(&alias->dev, "");
}
static int
assert(dev);
assert(type);
- if (name)
- strncpy(dev->ifname, name, IFNAMSIZ);
-
- D(DEVICE, "Initialize device '%s'\n", dev->ifname);
+ D(DEVICE, "Initialize device '%s'\n", name ? name : "");
INIT_SAFE_LIST(&dev->users);
INIT_SAFE_LIST(&dev->aliases);
dev->type = type;
+ if (name)
+ device_set_ifname(dev, name);
+
if (!dev->set_state)
dev->set_state = set_device_state;
}
device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
}
+int device_set_ifname(struct device *dev, const char *name)
+{
+ int ret = 0;
+
+ if (!strcmp(dev->ifname, name))
+ return 0;
+
+ if (dev->avl.key)
+ avl_delete(&devices, &dev->avl);
+
+ strncpy(dev->ifname, name, IFNAMSIZ);
+
+ if (dev->avl.key)
+ ret = avl_insert(&devices, &dev->avl);
+
+ if (ret == 0)
+ device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
+
+ return ret;
+}
+
static int device_refcount(struct device *dev)
{
struct list_head *list;
void device_set_present(struct device *dev, bool state);
void device_set_link(struct device *dev, bool state);
void device_set_ifindex(struct device *dev, int ifindex);
+int device_set_ifname(struct device *dev, const char *name);
void device_refresh_present(struct device *dev);
int device_claim(struct device_user *dep);
void device_release(struct device_user *dep);
static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev)
{
+ char name[IFNAMSIZ];
+
vldev->dev.hidden = dev->hidden;
- snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, vldev->id);
+ snprintf(name, IFNAMSIZ, "%s.%d", dev->ifname, vldev->id);
+ device_set_ifname(&vldev->dev, name);
}
static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
break;
case DEV_EVENT_UPDATE_IFNAME:
vlan_dev_set_name(vldev, dep->dev);
- device_broadcast_event(&vldev->dev, ev);
break;
case DEV_EVENT_TOPO_CHANGE:
/* Propagate topo changes */
return NULL;
vldev->id = id;
- vlan_dev_set_name(vldev, dev);
- device_init(&vldev->dev, &vlan_type, vldev->dev.ifname);
+ device_init(&vldev->dev, &vlan_type, NULL);
+
+ vlan_dev_set_name(vldev, dev);
vldev->dev.default_config = true;
vldev->set_state = vldev->dev.set_state;