swconfig: support receiving SWITCH_TYPE_LINK from kernel
[openwrt/openwrt.git] / package / network / config / swconfig / src / swlib.c
index de08717..908e0fb 100644 (file)
@@ -41,11 +41,25 @@ static struct genl_family *family;
 static struct nlattr *tb[SWITCH_ATTR_MAX + 1];
 static int refcount = 0;
 
 static struct nlattr *tb[SWITCH_ATTR_MAX + 1];
 static int refcount = 0;
 
-static struct nla_policy port_policy[] = {
+static struct nla_policy port_policy[SWITCH_ATTR_MAX] = {
        [SWITCH_PORT_ID] = { .type = NLA_U32 },
        [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
 };
 
        [SWITCH_PORT_ID] = { .type = NLA_U32 },
        [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
 };
 
+static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = {
+       [SWITCH_PORTMAP_SEGMENT] = { .type = NLA_STRING },
+       [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)
 {
@@ -196,12 +210,42 @@ 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)
 {
        struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
        struct switch_val *val = arg;
 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;
 
        if (!val)
                goto error;
@@ -217,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;
@@ -359,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.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);
                break;
        case SWITCH_TYPE_PORTS:
                ports = alloca(sizeof(struct switch_port) * dev->ports);
@@ -525,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;
 }
@@ -573,6 +622,41 @@ struct swlib_scan_arg {
        struct switch_dev *ptr;
 };
 
        struct switch_dev *ptr;
 };
 
+static int
+add_port_map(struct switch_dev *dev, struct nlattr *nla)
+{
+       struct nlattr *p;
+       int err = 0, idx = 0;
+       int remaining;
+
+       dev->maps = malloc(sizeof(struct switch_portmap) * dev->ports);
+       if (!dev->maps)
+               return -1;
+       memset(dev->maps, 0, sizeof(struct switch_portmap) * dev->ports);
+
+       nla_for_each_nested(p, nla, remaining) {
+               struct nlattr *tb[SWITCH_PORTMAP_MAX+1];
+
+               if (idx >= dev->ports)
+                       continue;
+
+               err = nla_parse_nested(tb, SWITCH_PORTMAP_MAX, p, portmap_policy);
+               if (err < 0)
+                       continue;
+
+
+               if (tb[SWITCH_PORTMAP_SEGMENT] && tb[SWITCH_PORTMAP_VIRT]) {
+                       dev->maps[idx].segment = strdup(nla_get_string(tb[SWITCH_PORTMAP_SEGMENT]));
+                       dev->maps[idx].virt = nla_get_u32(tb[SWITCH_PORTMAP_VIRT]);
+               }
+               idx++;
+       }
+
+out:
+       return err;
+}
+
+
 static int
 add_switch(struct nl_msg *msg, void *arg)
 {
 static int
 add_switch(struct nl_msg *msg, void *arg)
 {
@@ -610,6 +694,8 @@ add_switch(struct nl_msg *msg, void *arg)
                dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
        if (tb[SWITCH_ATTR_CPU_PORT])
                dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]);
                dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
        if (tb[SWITCH_ATTR_CPU_PORT])
                dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]);
+       if (tb[SWITCH_ATTR_PORTMAP])
+               add_port_map(dev, tb[SWITCH_ATTR_PORTMAP]);
 
        if (!sa->head) {
                sa->head = dev;
 
        if (!sa->head) {
                sa->head = dev;
@@ -624,12 +710,65 @@ done:
        return NL_SKIP;
 }
 
        return NL_SKIP;
 }
 
+static int
+list_switch(struct nl_msg *msg, void *arg)
+{
+       struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+
+       if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
+               goto done;
+
+       if (!tb[SWITCH_ATTR_DEV_NAME] || !tb[SWITCH_ATTR_NAME])
+               goto done;
+
+       printf("Found: %s - %s\n", nla_get_string(tb[SWITCH_ATTR_DEV_NAME]),
+               nla_get_string(tb[SWITCH_ATTR_ALIAS]));
+
+done:
+       return NL_SKIP;
+}
+
+void
+swlib_list(void)
+{
+       if (swlib_priv_init() < 0)
+               return;
+       swlib_call(SWITCH_CMD_GET_SWITCH, list_switch, NULL, NULL);
+       swlib_priv_free();
+}
+
+void
+swlib_print_portmap(struct switch_dev *dev, char *segment)
+{
+       int i;
+
+       if (segment) {
+               if (!strcmp(segment, "cpu")) {
+                       printf("%d ", dev->cpu_port);
+               } else if (!strcmp(segment, "disabled")) {
+                       for (i = 0; i < dev->ports; i++)
+                               if (!dev->maps[i].segment)
+                                       printf("%d ", i);
+               } else for (i = 0; i < dev->ports; i++) {
+                       if (dev->maps[i].segment && !strcmp(dev->maps[i].segment, segment))
+                               printf("%d ", i);
+               }
+       } else {
+               printf("%s - %s\n", dev->dev_name, dev->name);
+               for (i = 0; i < dev->ports; i++)
+                       if (i == dev->cpu_port)
+                               printf("port%d:\tcpu\n", i);
+                       else if (dev->maps[i].segment)
+                               printf("port%d:\t%s.%d\n", i, dev->maps[i].segment, dev->maps[i].virt);
+                       else
+                               printf("port%d:\tdisabled\n", i);
+       }
+}
 
 struct switch_dev *
 swlib_connect(const char *name)
 {
        struct swlib_scan_arg arg;
 
 struct switch_dev *
 swlib_connect(const char *name)
 {
        struct swlib_scan_arg arg;
-       int err;
 
        if (!refcount) {
                if (swlib_priv_init() < 0)
 
        if (!refcount) {
                if (swlib_priv_init() < 0)
@@ -655,18 +794,36 @@ 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;
        }
        *head = NULL;
 }
 
                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);
 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)
        free(dev);
 
        if (--refcount == 0)