swconfig: support receiving SWITCH_TYPE_LINK from kernel
[openwrt/openwrt.git] / package / network / config / swconfig / src / swlib.c
index 7de3a60..908e0fb 100644 (file)
@@ -51,6 +51,15 @@ static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = {
        [SWITCH_PORTMAP_VIRT] = { .type = NLA_U32 },
 };
 
+static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = {
+       [SWITCH_LINK_FLAG_LINK] = { .type = NLA_FLAG },
+       [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG },
+       [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG },
+       [SWITCH_LINK_SPEED] = { .type = NLA_U32 },
+       [SWITCH_LINK_FLAG_EEE_100BASET] = { .type = NLA_FLAG },
+       [SWITCH_LINK_FLAG_EEE_1000BASET] = { .type = NLA_FLAG },
+};
+
 static inline void *
 swlib_alloc(size_t size)
 {
@@ -201,12 +210,42 @@ out:
        return err;
 }
 
+static int
+store_link_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
+{
+       struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
+       struct switch_port_link *link;
+       int err = 0;
+
+       if (!val->value.link)
+               val->value.link = malloc(sizeof(struct switch_port_link));
+
+       err = nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy);
+       if (err < 0)
+               goto out;
+
+       link = val->value.link;
+       link->link = !!tb[SWITCH_LINK_FLAG_LINK];
+       link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX];
+       link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG];
+       link->tx_flow = !!tb[SWITCH_LINK_FLAG_TX_FLOW];
+       link->rx_flow = !!tb[SWITCH_LINK_FLAG_RX_FLOW];
+       link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]);
+       link->eee = 0;
+       if (tb[SWITCH_LINK_FLAG_EEE_100BASET])
+               link->eee |= SWLIB_LINK_FLAG_EEE_100BASET;
+       if (tb[SWITCH_LINK_FLAG_EEE_1000BASET])
+               link->eee |= SWLIB_LINK_FLAG_EEE_1000BASET;
+
+out:
+       return err;
+}
+
 static int
 store_val(struct nl_msg *msg, void *arg)
 {
        struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
        struct switch_val *val = arg;
-       struct switch_attr *attr = val->attr;
 
        if (!val)
                goto error;
@@ -222,6 +261,8 @@ store_val(struct nl_msg *msg, void *arg)
                val->value.s = strdup(nla_get_string(tb[SWITCH_ATTR_OP_VALUE_STR]));
        else if (tb[SWITCH_ATTR_OP_VALUE_PORTS])
                val->err = store_port_val(msg, tb[SWITCH_ATTR_OP_VALUE_PORTS], val);
+       else if (tb[SWITCH_ATTR_OP_VALUE_LINK])
+               val->err = store_link_val(msg, tb[SWITCH_ATTR_OP_VALUE_LINK], val);
 
        val->err = 0;
        return 0;
@@ -364,7 +405,7 @@ int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int por
                val.value.i = atoi(str);
                break;
        case SWITCH_TYPE_STRING:
-               val.value.s = str;
+               val.value.s = (char *)str;
                break;
        case SWITCH_TYPE_PORTS:
                ports = alloca(sizeof(struct switch_port) * dev->ports);
@@ -530,10 +571,13 @@ struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
 static void
 swlib_priv_free(void)
 {
+       if (family)
+               nl_object_put((struct nl_object*)family);
        if (cache)
                nl_cache_free(cache);
        if (handle)
                nl_socket_free(handle);
+       family = NULL;
        handle = NULL;
        cache = NULL;
 }
@@ -669,11 +713,7 @@ done:
 static int
 list_switch(struct nl_msg *msg, void *arg)
 {
-       struct swlib_scan_arg *sa = arg;
        struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
-       struct switch_dev *dev;
-       const char *name;
-       const char *alias;
 
        if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
                goto done;
@@ -729,7 +769,6 @@ struct switch_dev *
 swlib_connect(const char *name)
 {
        struct swlib_scan_arg arg;
-       int err;
 
        if (!refcount) {
                if (swlib_priv_init() < 0)
@@ -755,18 +794,36 @@ swlib_free_attributes(struct switch_attr **head)
 
        while (a) {
                next = a->next;
+               free(a->name);
+               free(a->description);
                free(a);
                a = next;
        }
        *head = NULL;
 }
 
+static void
+swlib_free_port_map(struct switch_dev *dev)
+{
+       int i;
+
+       if (!dev || !dev->maps)
+               return;
+
+       for (i = 0; i < dev->ports; i++)
+               free(dev->maps[i].segment);
+       free(dev->maps);
+}
+
 void
 swlib_free(struct switch_dev *dev)
 {
        swlib_free_attributes(&dev->ops);
        swlib_free_attributes(&dev->port_ops);
        swlib_free_attributes(&dev->vlan_ops);
+       swlib_free_port_map(dev);
+       free(dev->name);
+       free(dev->alias);
        free(dev);
 
        if (--refcount == 0)