add support for updating route metrics
authorFelix Fietkau <nbd@openwrt.org>
Mon, 19 Mar 2012 22:26:44 +0000 (23:26 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 19 Mar 2012 22:26:44 +0000 (23:26 +0100)
interface-ip.c
interface-ip.h
interface.c
interface.h

index 56f3c0ce6468c7ff62808207eed70b0f40501504..8c4616896cc9aea9547a80c7fab8df4f0ce2f874 100644 (file)
@@ -377,9 +377,12 @@ void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
                if (route->enabled == _enabled)
                        continue;
 
-               if (_enabled)
+               if (_enabled) {
+                       if (!(route->flags & DEVROUTE_METRIC))
+                               route->metric = ip->iface->metric;
+
                        system_add_route(dev, route);
-               else
+               else
                        system_del_route(dev, route);
                route->enabled = _enabled;
        }
index 68bfdf647360a4aba319653f2ecc3adf9ec3437a..df5545d5fde60c1cf689f25f6e8afbe626663498 100644 (file)
@@ -76,6 +76,6 @@ void interface_ip_update_start(struct interface_ip_settings *ip);
 void interface_ip_update_complete(struct interface_ip_settings *ip);
 void interface_ip_flush(struct interface_ip_settings *ip);
 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled);
-
+void interface_ip_update_metric(struct interface_ip_settings *ip, int metric);
 
 #endif
index 4ad89f538a41328f006d49c49c3576f1348463da..f281a9b4bbf2d1dd63de31b5375a8d296e531561 100644 (file)
@@ -18,6 +18,7 @@ enum {
        IFACE_ATTR_PROTO,
        IFACE_ATTR_AUTO,
        IFACE_ATTR_DEFAULTROUTE,
+       IFACE_ATTR_METRIC,
        IFACE_ATTR_MAX
 };
 
@@ -26,6 +27,7 @@ static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
        [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
        [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
        [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
+       [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
 };
 
 const struct config_param_list interface_attr_list = {
@@ -516,11 +518,21 @@ interface_change_config(struct interface *if_old, struct interface *if_new)
                goto reload;
        }
 
-       if (if_old->proto_ip.no_defaultroute != if_new->proto_ip.no_defaultroute) {
-               if_old->proto_ip.no_defaultroute = if_new->proto_ip.no_defaultroute;
-               interface_ip_set_enabled(&if_old->proto_ip, if_old->proto_ip.enabled);
+#define UPDATE(field) ({                                               \
+               bool __changed = (if_old->field != if_new->field);      \
+               if_old->field = if_new->field;                          \
+               __changed;                                              \
+       })
+
+       if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
+               interface_ip_set_enabled(&if_old->config_ip, false);
+               interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
+               interface_ip_set_enabled(&if_old->proto_ip, false);
+               interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
        }
 
+#undef UPDATE
+
        goto out;
 
 reload:
index 750c0f2bb44b880a740b0de9cfdb51b575a8bdb9..f0b26f5405da0addfbe20f7788e6a8258f5e3012 100644 (file)
@@ -87,6 +87,8 @@ struct interface {
        struct interface_ip_settings proto_ip;
        struct interface_ip_settings config_ip;
 
+       int metric;
+
        /* errors/warnings while trying to bring up the interface */
        struct list_head errors;