/*
* swlib.c: Switch configuration API (user space part)
*
- * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
[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 },
return -1;
}
+static int
+send_attr_link(struct nl_msg *msg, struct switch_val *val)
+{
+ struct switch_port_link *link = val->value.link;
+ struct nlattr *n;
+
+ n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_LINK);
+ if (!n)
+ goto nla_put_failure;
+
+ if (link->duplex)
+ NLA_PUT_FLAG(msg, SWITCH_LINK_FLAG_DUPLEX);
+ if (link->aneg)
+ NLA_PUT_FLAG(msg, SWITCH_LINK_FLAG_ANEG);
+ NLA_PUT_U32(msg, SWITCH_LINK_SPEED, link->speed);
+
+ nla_nest_end(msg, n);
+
+ return 0;
+
+nla_put_failure:
+ return -1;
+}
+
static int
send_attr_val(struct nl_msg *msg, void *arg)
{
if (send_attr_ports(msg, val) < 0)
goto nla_put_failure;
break;
+ case SWITCH_TYPE_LINK:
+ if (send_attr_link(msg, val))
+ goto nla_put_failure;
+ break;
default:
goto nla_put_failure;
}
return swlib_call(cmd, NULL, send_attr_val, val);
}
+enum {
+ CMD_NONE,
+ CMD_DUPLEX,
+ CMD_ANEG,
+ CMD_SPEED,
+};
+
int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str)
{
struct switch_port *ports;
+ struct switch_port_link *link;
struct switch_val val;
char *ptr;
+ int cmd = CMD_NONE;
memset(&val, 0, sizeof(val));
val.port_vlan = port_vlan;
}
val.value.ports = ports;
break;
+ case SWITCH_TYPE_LINK:
+ link = malloc(sizeof(struct switch_port_link));
+ memset(link, 0, sizeof(struct switch_port_link));
+ ptr = (char *)str;
+ for (ptr = strtok(ptr," "); ptr; ptr = strtok(NULL, " ")) {
+ switch (cmd) {
+ case CMD_NONE:
+ if (!strcmp(ptr, "duplex"))
+ cmd = CMD_DUPLEX;
+ else if (!strcmp(ptr, "autoneg"))
+ cmd = CMD_ANEG;
+ else if (!strcmp(ptr, "speed"))
+ cmd = CMD_SPEED;
+ else
+ fprintf(stderr, "Unsupported option %s\n", ptr);
+ break;
+ case CMD_DUPLEX:
+ if (!strcmp(ptr, "half"))
+ link->duplex = 0;
+ else if (!strcmp(ptr, "full"))
+ link->duplex = 1;
+ else
+ fprintf(stderr, "Unsupported value %s\n", ptr);
+ cmd = CMD_NONE;
+ break;
+ case CMD_ANEG:
+ if (!strcmp(ptr, "on"))
+ link->aneg = 1;
+ else if (!strcmp(ptr, "off"))
+ link->aneg = 0;
+ else
+ fprintf(stderr, "Unsupported value %s\n", ptr);
+ cmd = CMD_NONE;
+ break;
+ case CMD_SPEED:
+ link->speed = atoi(ptr);
+ cmd = CMD_NONE;
+ break;
+ }
+ }
+ val.value.link = link;
+ break;
case SWITCH_TYPE_NOVAL:
if (str && !strcmp(str, "0"))
return 0;
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)
{
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;
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)
{
*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);