+
+static enum dev_change_type
+device_set_config(struct device *dev, struct device_type *type,
+ struct blob_attr *attr)
+{
+ struct blob_attr *tb[__DEV_ATTR_MAX];
+ const struct uci_blob_param_list *cfg = type->config_params;
+
+ if (type != dev->type)
+ return DEV_CONFIG_RECREATE;
+
+ if (dev->type->reload)
+ return dev->type->reload(dev, attr);
+
+ if (uci_blob_check_equal(dev->config, attr, cfg))
+ return DEV_CONFIG_NO_CHANGE;
+
+ if (cfg == &device_attr_list) {
+ memset(tb, 0, sizeof(tb));
+
+ if (attr)
+ blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
+ blob_data(attr), blob_len(attr));
+
+ device_init_settings(dev, tb);
+ return DEV_CONFIG_RESTART;
+ } else
+ return DEV_CONFIG_RECREATE;
+}
+
+enum dev_change_type
+device_apply_config(struct device *dev, struct device_type *type,
+ struct blob_attr *config)
+{
+ enum dev_change_type change;
+
+ change = device_set_config(dev, type, config);
+ if (dev->external) {
+ system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
+ change = DEV_CONFIG_APPLIED;
+ }
+
+ switch (change) {
+ case DEV_CONFIG_RESTART:
+ case DEV_CONFIG_APPLIED:
+ D(DEVICE, "Device '%s': config applied\n", dev->ifname);
+ config = blob_memdup(config);
+ free(dev->config);
+ dev->config = config;
+ if (change == DEV_CONFIG_RESTART && dev->present) {
+ int ret = 0;
+
+ device_set_present(dev, false);
+ if (dev->active && !dev->external) {
+ ret = dev->set_state(dev, false);
+ if (!ret)
+ ret = dev->set_state(dev, true);
+ }
+ if (!ret)
+ device_set_present(dev, true);
+ }
+ break;
+ case DEV_CONFIG_NO_CHANGE:
+ D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
+ break;
+ case DEV_CONFIG_RECREATE:
+ break;
+ }
+
+ return change;
+}
+
+static void
+device_replace(struct device *dev, struct device *odev)
+{
+ struct device_user *dep, *tmp;
+
+ __devlock++;
+ if (odev->present)
+ device_set_present(odev, false);
+
+ list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
+ device_release(dep);
+ safe_list_del(&dep->list);
+ __device_add_user(dep, dev);
+ }
+ __devlock--;
+
+ device_free(odev);
+}
+
+void
+device_reset_config(void)
+{
+ struct device *dev;
+
+ avl_for_each_element(&devices, dev, avl)
+ dev->current_config = false;
+}
+
+void
+device_reset_old(void)
+{
+ struct device *dev, *tmp, *ndev;
+
+ avl_for_each_element_safe(&devices, dev, avl, tmp) {
+ if (dev->current_config || dev->default_config)
+ continue;
+
+ if (dev->type != &simple_device_type)
+ continue;
+
+ ndev = device_create_default(dev->ifname, dev->external);
+ if (!ndev)
+ continue;
+
+ device_replace(ndev, dev);
+ }
+}
+
+void
+device_set_default_ps(bool state)
+{
+ struct device *dev;
+
+ if (state == default_ps)
+ return;
+
+ default_ps = state;
+
+ avl_for_each_element(&devices, dev, avl) {
+ struct device_settings *s = &dev->settings;
+ unsigned int apply_mask = 0;
+
+ if (!(s->flags & DEV_OPT_RPS)) {
+ s->rps = default_ps;
+ apply_mask |= DEV_OPT_RPS;
+ }
+
+ if (!(s->flags & DEV_OPT_XPS)) {
+ s->xps = default_ps;
+ apply_mask |= DEV_OPT_XPS;
+ }
+
+ if (!apply_mask)
+ continue;
+
+ if (!(dev->external || (dev->present && dev->active)) ||
+ dev->config_pending)
+ continue;
+
+ system_if_apply_settings(dev, s, apply_mask);
+ }
+}
+
+struct device *
+device_create(const char *name, struct device_type *type,
+ struct blob_attr *config)
+{
+ struct device *odev = NULL, *dev;
+ enum dev_change_type change;
+
+ odev = device_find(name);
+ if (odev) {
+ odev->current_config = true;
+ change = device_apply_config(odev, type, config);
+ switch (change) {
+ case DEV_CONFIG_RECREATE:
+ D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
+ device_delete(odev);
+ break;
+ default:
+ return odev;
+ }
+ } else
+ D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
+
+ config = blob_memdup(config);
+ if (!config)
+ return NULL;
+
+ dev = type->create(name, type, config);
+ if (!dev)
+ return NULL;
+
+ dev->current_config = true;
+ dev->config = config;
+ if (odev)
+ device_replace(dev, odev);
+
+ if (!config_init && dev->config_pending) {
+ type->config_init(dev);
+ dev->config_pending = false;
+ }
+
+ return dev;
+}
+
+void
+device_dump_status(struct blob_buf *b, struct device *dev)
+{
+ struct device_settings st;
+ void *c, *s;
+
+ if (!dev) {
+ avl_for_each_element(&devices, dev, avl) {
+ if (!dev->present)
+ continue;
+ c = blobmsg_open_table(b, dev->ifname);
+ device_dump_status(b, dev);
+ blobmsg_close_table(b, c);
+ }
+
+ return;
+ }
+
+ blobmsg_add_u8(b, "external", dev->external);
+ blobmsg_add_u8(b, "present", dev->present);
+ blobmsg_add_string(b, "type", dev->type->name);
+
+ if (!dev->present)
+ return;
+
+ blobmsg_add_u8(b, "up", !!dev->active);
+ blobmsg_add_u8(b, "carrier", !!dev->link_active);
+
+ if (dev->type->dump_info)
+ dev->type->dump_info(dev, b);
+ else
+ system_if_dump_info(dev, b);
+
+ if (dev->active) {
+ device_merge_settings(dev, &st);
+ if (st.flags & DEV_OPT_MTU)
+ blobmsg_add_u32(b, "mtu", st.mtu);
+ if (st.flags & DEV_OPT_MTU6)
+ blobmsg_add_u32(b, "mtu6", st.mtu6);
+ if (st.flags & DEV_OPT_MACADDR)
+ blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
+ if (st.flags & DEV_OPT_TXQUEUELEN)
+ blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
+ if (st.flags & DEV_OPT_IPV6)
+ blobmsg_add_u8(b, "ipv6", st.ipv6);
+ if (st.flags & DEV_OPT_PROMISC)
+ blobmsg_add_u8(b, "promisc", st.promisc);
+ if (st.flags & DEV_OPT_RPFILTER)
+ blobmsg_add_u32(b, "rpfilter", st.rpfilter);
+ if (st.flags & DEV_OPT_ACCEPTLOCAL)
+ blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
+ if (st.flags & DEV_OPT_IGMPVERSION)
+ blobmsg_add_u32(b, "igmpversion", st.igmpversion);
+ if (st.flags & DEV_OPT_MLDVERSION)
+ blobmsg_add_u32(b, "mldversion", st.mldversion);
+ if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
+ blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
+ blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
+ }
+ if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
+ blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
+ blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
+ }
+ if (st.flags & DEV_OPT_DADTRANSMITS)
+ blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
+ if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
+ blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
+ if (st.flags & DEV_OPT_MULTICAST_ROUTER)
+ blobmsg_add_u32(b, "multicast_router", st.multicast_router);
+ if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE)
+ blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave);
+ if (st.flags & DEV_OPT_MULTICAST)
+ blobmsg_add_u8(b, "multicast", st.multicast);
+ if (st.flags & DEV_OPT_LEARNING)
+ blobmsg_add_u8(b, "learning", st.learning);
+ if (st.flags & DEV_OPT_UNICAST_FLOOD)
+ blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
+ if (st.flags & DEV_OPT_SENDREDIRECTS)
+ blobmsg_add_u8(b, "sendredirects", st.sendredirects);
+ }
+
+ s = blobmsg_open_table(b, "statistics");
+ if (dev->type->dump_stats)
+ dev->type->dump_stats(dev, b);
+ else
+ system_if_dump_stats(dev, b);
+ blobmsg_close_table(b, s);
+}
+
+static void __init simple_device_type_init(void)
+{
+ device_type_add(&simple_device_type);
+}