swconfig: Add generic switch identifiers
authorJonas Gorski <jogo@openwrt.org>
Wed, 27 Jul 2011 18:00:18 +0000 (18:00 +0000)
committerJonas Gorski <jogo@openwrt.org>
Wed, 27 Jul 2011 18:00:18 +0000 (18:00 +0000)
Also make switches available under a generic name "switch<num>" for device
name agnostic access. The old device name is used as an alias for backward
compatibility.

SVN-Revision: 27800

package/swconfig/Makefile
package/swconfig/src/cli.c
package/swconfig/src/swlib.c
package/swconfig/src/swlib.h
target/linux/generic/files/drivers/net/phy/rtl8366rb.c
target/linux/generic/files/drivers/net/phy/rtl8366s.c
target/linux/generic/files/drivers/net/phy/swconfig.c
target/linux/generic/files/include/linux/switch.h

index 363b3a01fd82ac3a7b562563bf8460822f597def..58ae75f46633edb3a31bf5c29c129cbe188a0e68 100644 (file)
@@ -8,7 +8,7 @@
 include $(TOPDIR)/rules.mk
 
 PKG_NAME:=swconfig
-PKG_RELEASE:=7
+PKG_RELEASE:=8
 
 include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/kernel.mk
index eea88d812fb87be3c99089b93c67000ec6a06a8b..eff34fb754fac7f6f2dfb5fab89183bf8fc475fe 100644 (file)
@@ -74,7 +74,7 @@ print_attrs(const struct switch_attr *attr)
 static void
 list_attributes(struct switch_dev *dev)
 {
-       printf("Switch %d: %s(%s), ports: %d (cpu @ %d), vlans: %d\n", dev->id, dev->dev_name, dev->name, dev->ports, dev->cpu_port, dev->vlans);
+       printf("%s: %s(%s), ports: %d (cpu @ %d), vlans: %d\n", dev->dev_name, dev->alias, dev->name, dev->ports, dev->cpu_port, dev->vlans);
        printf("     --switch\n");
        print_attrs(dev->ops);
        printf("     --vlan\n");
index 20e727eb5371c88bcf0c5d80c3eadc4cd98cf9f5..531a23a1ab00f68a7827c40682f72120a159641a 100644 (file)
@@ -580,6 +580,7 @@ add_switch(struct nl_msg *msg, void *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;
@@ -588,14 +589,17 @@ add_switch(struct nl_msg *msg, void *arg)
                goto done;
 
        name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
-       if (sa->name && (strcmp(name, sa->name) != 0))
+       alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]);
+
+       if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0))
                goto done;
 
        dev = swlib_alloc(sizeof(struct switch_dev));
        if (!dev)
                goto done;
 
-       dev->dev_name = strdup(name);
+       strncpy(dev->dev_name, name, IFNAMSIZ - 1);
+       dev->alias = strdup(alias);
        if (tb[SWITCH_ATTR_ID])
                dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
        if (tb[SWITCH_ATTR_NAME])
index 2c2fccd3943f8d87c5d76e49495a26e8e4e9dc32..ff73969c8718cc0edcc1c7010669b44f24da0b14 100644 (file)
@@ -114,8 +114,9 @@ struct uci_package;
 
 struct switch_dev {
        int id;
+       char dev_name[IFNAMSIZ];
        const char *name;
-       const char *dev_name;
+       const char *alias;
        int ports;
        int vlans;
        int cpu_port;
index c95368148d356c2cf4b4d9a0cf5ec5f9e6e0c273..b337408b5a72b4181dc2f91c30779ca772a30795 100644 (file)
@@ -1116,7 +1116,7 @@ static int rtl8366rb_switch_init(struct rtl8366_smi *smi)
        dev->ports = RTL8366RB_NUM_PORTS;
        dev->vlans = RTL8366RB_NUM_VIDS;
        dev->ops = &rtl8366_ops;
-       dev->devname = dev_name(smi->parent);
+       dev->alias = dev_name(smi->parent);
 
        err = register_switch(dev, NULL);
        if (err)
index 8fa1ed186a69e8c5fe6346a2b51b726e49085889..e2d8402cf67f33a1b76d3fb03859c8b628281f08 100644 (file)
@@ -945,7 +945,7 @@ static int rtl8366s_switch_init(struct rtl8366_smi *smi)
        dev->ports = RTL8366S_NUM_PORTS;
        dev->vlans = RTL8366S_NUM_VIDS;
        dev->ops = &rtl8366_ops;
-       dev->devname = dev_name(smi->parent);
+       dev->alias = dev_name(smi->parent);
 
        err = register_switch(dev, NULL);
        if (err)
index 16afea0af86d45d08257e606da4d40b1f9dcf9ee..88fa244bc89c081702161de003f2477e3a624203 100644 (file)
@@ -31,6 +31,8 @@
 #define DPRINTF(...) do {} while(0)
 #endif
 
+#define SWCONFIG_DEVNAME       "switch%d"
+
 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
 MODULE_LICENSE("GPL");
 
@@ -760,8 +762,9 @@ swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
                return -1;
 
        NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
-       NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
        NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
+       NLA_PUT_STRING(msg, SWITCH_ATTR_ALIAS, dev->alias);
+       NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
        NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
        NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
        NLA_PUT_U32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port);
@@ -857,13 +860,18 @@ static struct genl_ops swconfig_ops[] = {
 int
 register_switch(struct switch_dev *dev, struct net_device *netdev)
 {
+       struct switch_dev *sdev;
+       const int max_switches = 8 * sizeof(unsigned long);
+       unsigned long in_use = 0;
+       int i;
+
        INIT_LIST_HEAD(&dev->dev_list);
        if (netdev) {
                dev->netdev = netdev;
-               if (!dev->devname)
-                       dev->devname = netdev->name;
+               if (!dev->alias)
+                       dev->alias = netdev->name;
        }
-       BUG_ON(!dev->devname);
+       BUG_ON(!dev->alias);
 
        if (dev->ports > 0) {
                dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
@@ -871,10 +879,27 @@ register_switch(struct switch_dev *dev, struct net_device *netdev)
                if (!dev->portbuf)
                        return -ENOMEM;
        }
-       dev->id = ++swdev_id;
        swconfig_defaults_init(dev);
        spin_lock_init(&dev->lock);
        swconfig_lock();
+       dev->id = ++swdev_id;
+
+       list_for_each_entry(sdev, &swdevs, dev_list) {
+               if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
+                       continue;
+               if (i < 0 || i > max_switches)
+                       continue;
+
+               set_bit(i, &in_use);
+       }
+       i = find_first_zero_bit(&in_use, max_switches);
+
+       if (i == max_switches)
+               return -ENFILE;
+
+       /* fill device name */
+       snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
+
        list_add(&dev->dev_list, &swdevs);
        swconfig_unlock();
 
index ef2dc3ad9fe1de38a66346ab39025ba97d41aa51..6eefb7fe6b0bb2ec93b0e2e323282bf7601e87cc 100644 (file)
@@ -36,8 +36,9 @@ enum {
        SWITCH_ATTR_TYPE,
        /* device */
        SWITCH_ATTR_ID,
-       SWITCH_ATTR_NAME,
        SWITCH_ATTR_DEV_NAME,
+       SWITCH_ATTR_ALIAS,
+       SWITCH_ATTR_NAME,
        SWITCH_ATTR_VLANS,
        SWITCH_ATTR_PORTS,
        SWITCH_ATTR_CPU_PORT,
@@ -146,10 +147,12 @@ struct switch_dev_ops {
 
 struct switch_dev {
        const struct switch_dev_ops *ops;
-       const char *name;
+       /* will be automatically filled */
+       char devname[IFNAMSIZ];
 
-       /* NB: either devname or netdev must be set */
-       const char *devname;
+       const char *name;
+       /* NB: either alias or netdev must be set */
+       const char *alias;
        struct net_device *netdev;
 
        int ports;