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.
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@27800
3c298f89-4303-0410-b956-
a3cf2f4a3e73
include $(TOPDIR)/rules.mk
PKG_NAME:=swconfig
include $(TOPDIR)/rules.mk
PKG_NAME:=swconfig
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/kernel.mk
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/kernel.mk
static void
list_attributes(struct switch_dev *dev)
{
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");
printf(" --switch\n");
print_attrs(dev->ops);
printf(" --vlan\n");
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct switch_dev *dev;
const char *name;
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
struct switch_dev *dev;
const char *name;
if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
goto done;
if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
goto done;
goto done;
name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
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;
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])
if (tb[SWITCH_ATTR_ID])
dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
if (tb[SWITCH_ATTR_NAME])
struct switch_dev {
int id;
struct switch_dev {
int id;
+ char dev_name[IFNAMSIZ];
int ports;
int vlans;
int cpu_port;
int ports;
int vlans;
int cpu_port;
dev->ports = RTL8366RB_NUM_PORTS;
dev->vlans = RTL8366RB_NUM_VIDS;
dev->ops = &rtl8366_ops;
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)
err = register_switch(dev, NULL);
if (err)
dev->ports = RTL8366S_NUM_PORTS;
dev->vlans = RTL8366S_NUM_VIDS;
dev->ops = &rtl8366_ops;
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)
err = register_switch(dev, NULL);
if (err)
#define DPRINTF(...) do {} while(0)
#endif
#define DPRINTF(...) do {} while(0)
#endif
+#define SWCONFIG_DEVNAME "switch%d"
+
MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
MODULE_LICENSE("GPL");
return -1;
NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
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_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);
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);
int
register_switch(struct switch_dev *dev, struct net_device *netdev)
{
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;
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;
if (dev->ports > 0) {
dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
if (dev->ports > 0) {
dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
if (!dev->portbuf)
return -ENOMEM;
}
if (!dev->portbuf)
return -ENOMEM;
}
swconfig_defaults_init(dev);
spin_lock_init(&dev->lock);
swconfig_lock();
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();
list_add(&dev->dev_list, &swdevs);
swconfig_unlock();
SWITCH_ATTR_TYPE,
/* device */
SWITCH_ATTR_ID,
SWITCH_ATTR_TYPE,
/* device */
SWITCH_ATTR_ID,
+ SWITCH_ATTR_ALIAS,
+ SWITCH_ATTR_NAME,
SWITCH_ATTR_VLANS,
SWITCH_ATTR_PORTS,
SWITCH_ATTR_CPU_PORT,
SWITCH_ATTR_VLANS,
SWITCH_ATTR_PORTS,
SWITCH_ATTR_CPU_PORT,
struct switch_dev {
const struct switch_dev_ops *ops;
struct switch_dev {
const struct switch_dev_ops *ops;
+ /* 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;
struct net_device *netdev;
int ports;