swconfig: support receiving SWITCH_TYPE_LINK from kernel
[openwrt/openwrt.git] / package / network / config / swconfig / src / swlib.c
index d7e570e..908e0fb 100644 (file)
@@ -51,6 +51,15 @@ static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = {
        [SWITCH_PORTMAP_VIRT] = { .type = NLA_U32 },
 };
 
        [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)
 {
 static inline void *
 swlib_alloc(size_t size)
 {
@@ -201,6 +210,37 @@ out:
        return err;
 }
 
        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)
 {
 static int
 store_val(struct nl_msg *msg, void *arg)
 {
@@ -221,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);
                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;
 
        val->err = 0;
        return 0;
@@ -529,10 +571,13 @@ struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
 static void
 swlib_priv_free(void)
 {
 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);
        if (cache)
                nl_cache_free(cache);
        if (handle)
                nl_socket_free(handle);
+       family = NULL;
        handle = NULL;
        cache = NULL;
 }
        handle = NULL;
        cache = NULL;
 }
@@ -749,6 +794,8 @@ swlib_free_attributes(struct switch_attr **head)
 
        while (a) {
                next = a->next;
 
        while (a) {
                next = a->next;
+               free(a->name);
+               free(a->description);
                free(a);
                a = next;
        }
                free(a);
                a = next;
        }