#include "config.h"
static struct avl_tree devices;
+static struct avl_tree aliases;
+
+struct alias_device {
+ struct avl_node avl;
+ struct device dev;
+ struct device_user dep;
+ bool cleanup;
+ char name[];
+};
+
+static const struct device_type alias_device_type;
static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
[DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
.free = simple_device_free,
};
+static int
+alias_device_set_state(struct device *dev, bool state)
+{
+ struct alias_device *alias;
+
+ alias = container_of(dev, struct alias_device, dev);
+ if (!alias->dep.dev)
+ return -1;
+
+ if (state)
+ return device_claim(&alias->dep);
+
+ device_release(&alias->dep);
+ if (alias->cleanup)
+ device_remove_user(&alias->dep);
+ return 0;
+}
+
+static struct device *
+alias_device_create(const char *name, struct blob_attr *attr)
+{
+ struct alias_device *alias;
+
+ alias = calloc(1, sizeof(*alias) + strlen(name) + 1);
+ strcpy(alias->name, name);
+ alias->dev.set_state = alias_device_set_state;
+ device_init_virtual(&alias->dev, &alias_device_type, NULL);
+ alias->avl.key = alias->name;
+ avl_insert(&aliases, &alias->avl);
+
+ return &alias->dev;
+}
+
+static void alias_device_free(struct device *dev)
+{
+ struct alias_device *alias;
+
+ device_cleanup(dev);
+
+ alias = container_of(dev, struct alias_device, dev);
+ avl_delete(&aliases, &alias->avl);
+ free(alias);
+}
+
+static const struct device_type alias_device_type = {
+ .name = "Network alias",
+ .create = alias_device_create,
+ .free = alias_device_free,
+};
+
void
device_init_settings(struct device *dev, struct blob_attr **tb)
{
static void __init dev_init(void)
{
avl_init(&devices, avl_strcmp, true, NULL);
+ avl_init(&aliases, avl_strcmp, false, NULL);
}
static void device_broadcast_event(struct device *dev, enum device_event ev)
}
}
+void
+alias_notify_device(const char *name, struct device *dev)
+{
+ struct alias_device *alias;
+
+ alias = avl_find_element(&aliases, name, alias, avl);
+ if (!alias)
+ return;
+
+ alias->cleanup = !dev;
+ if (dev) {
+ if (dev != alias->dep.dev) {
+ if (alias->dep.dev)
+ device_remove_user(&alias->dep);
+ strcpy(alias->dev.ifname, dev->ifname);
+ device_add_user(&alias->dep, dev);
+ }
+ }
+
+ device_set_present(&alias->dev, !!dev);
+
+ if (!dev && alias->dep.dev && !alias->dep.dev->active)
+ device_remove_user(&alias->dep);
+}
+
static int set_device_state(struct device *dev, bool state)
{
if (state)
return 0;
dep->claimed = true;
- D(DEVICE, "claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1);
+ D(DEVICE, "Claim %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active + 1);
if (++dev->active != 1)
return 0;
dep->claimed = false;
dev->active--;
- D(DEVICE, "release device %s, new refcount: %d\n", dev->ifname, dev->active);
+ D(DEVICE, "Release %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active);
assert(dev->active >= 0);
if (dev->active)
return dev;
}
+static struct device *
+device_alias_get(const char *name)
+{
+ struct alias_device *alias;
+
+ alias = avl_find_element(&aliases, name, alias, avl);
+ if (alias)
+ return &alias->dev;
+
+ return alias_device_create(name, NULL);
+}
+
struct device *
device_get(const char *name, bool create)
{
if (strchr(name, '.'))
return get_vlan_device_chain(name, create);
+ if (name[0] == '@')
+ return device_alias_get(name + 1);
+
dev = avl_find_element(&devices, name, dev, avl);
if (dev)
return dev;
if (dev->present == state)
return;
- D(DEVICE, "Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" );
+ D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
dev->present = state;
device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
}
return 0;
}
+enum {
+ ALIAS_ATTR_ALIAS,
+ ALIAS_ATTR_DEV,
+ __ALIAS_ATTR_MAX,
+};
+
+static const struct blobmsg_policy alias_attrs[__ALIAS_ATTR_MAX] = {
+ [ALIAS_ATTR_ALIAS] = { "alias", BLOBMSG_TYPE_ARRAY },
+ [ALIAS_ATTR_DEV] = { "device", BLOBMSG_TYPE_STRING },
+};
+
+static int
+netifd_handle_alias(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct device *dev = NULL;
+ struct blob_attr *tb[__ALIAS_ATTR_MAX];
+ struct blob_attr *cur;
+ int rem;
+
+ blobmsg_parse(alias_attrs, __ALIAS_ATTR_MAX, tb, blob_data(msg), blob_len(msg));
+
+ if (!tb[ALIAS_ATTR_ALIAS])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ if ((cur = tb[ALIAS_ATTR_DEV]) != NULL) {
+ dev = device_get(blobmsg_data(cur), true);
+ if (!dev)
+ return UBUS_STATUS_NOT_FOUND;
+ }
+
+ blobmsg_for_each_attr(cur, tb[ALIAS_ATTR_ALIAS], rem) {
+ if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
+ goto error;
+
+ if (!blobmsg_check_attr(cur, NULL))
+ goto error;
+
+ alias_notify_device(blobmsg_data(cur), dev);
+ }
+ return 0;
+
+error:
+ device_free_unused(dev);
+ return UBUS_STATUS_INVALID_ARGUMENT;
+}
+
static struct ubus_method dev_object_methods[] = {
- UBUS_METHOD("status", netifd_dev_status, dev_policy)
+ UBUS_METHOD("status", netifd_dev_status, dev_policy),
+ UBUS_METHOD("set_alias", netifd_handle_alias, alias_attrs),
};
static struct ubus_object_type dev_object_type =