From 65dcba6303e8d0ae940dfc0ffe6b1e48f9114159 Mon Sep 17 00:00:00 2001 From: blogic Date: Tue, 9 Apr 2013 14:19:13 +0000 Subject: [PATCH] [swconfig] add portmap support to userland Signed-off-by: John Crispin git-svn-id: svn://svn.openwrt.org/openwrt/trunk@36284 3c298f89-4303-0410-b956-a3cf2f4a3e73 --- package/network/config/swconfig/Makefile | 3 +- package/network/config/swconfig/src/cli.c | 9 ++++ package/network/config/swconfig/src/swlib.c | 72 ++++++++++++++++++++++++++++- package/network/config/swconfig/src/swlib.h | 13 ++++++ 4 files changed, 95 insertions(+), 2 deletions(-) diff --git a/package/network/config/swconfig/Makefile b/package/network/config/swconfig/Makefile index 76817f0dbf..6d1c9db780 100644 --- a/package/network/config/swconfig/Makefile +++ b/package/network/config/swconfig/Makefile @@ -26,7 +26,8 @@ TARGET_CPPFLAGS := \ -D_GNU_SOURCE \ -I$(STAGING_DIR)/usr/include/libnl-tiny \ -I$(PKG_BUILD_DIR) \ - $(TARGET_CPPFLAGS) + $(TARGET_CPPFLAGS) \ + -I$(LINUX_DIR)/user_headers/include define Build/Prepare mkdir -p $(PKG_BUILD_DIR) diff --git a/package/network/config/swconfig/src/cli.c b/package/network/config/swconfig/src/cli.c index 5f9e532bc0..2eb73bea38 100644 --- a/package/network/config/swconfig/src/cli.c +++ b/package/network/config/swconfig/src/cli.c @@ -41,6 +41,7 @@ enum { CMD_LOAD, CMD_HELP, CMD_SHOW, + CMD_PORTMAP, }; static void @@ -214,6 +215,7 @@ int main(int argc, char **argv) int cvlan = -1; char *ckey = NULL; char *cvalue = NULL; + char *csegment = NULL; if((argc == 2) && !strcmp(argv[1], "list")) { swlib_list(); @@ -252,6 +254,10 @@ int main(int argc, char **argv) print_usage(); cmd = CMD_LOAD; ckey = argv[++i]; + } else if (!strcmp(arg, "portmap")) { + if (i + 1 < argc) + csegment = argv[++i]; + cmd = CMD_PORTMAP; } else if (!strcmp(arg, "show")) { cmd = CMD_SHOW; } else { @@ -324,6 +330,9 @@ int main(int argc, char **argv) case CMD_HELP: list_attributes(dev); break; + case CMD_PORTMAP: + swlib_print_portmap(dev, csegment); + break; case CMD_SHOW: if (cport >= 0 || cvlan >= 0) { if (cport >= 0) diff --git a/package/network/config/swconfig/src/swlib.c b/package/network/config/swconfig/src/swlib.c index a867d2e842..7de3a604e0 100644 --- a/package/network/config/swconfig/src/swlib.c +++ b/package/network/config/swconfig/src/swlib.c @@ -41,11 +41,16 @@ static struct genl_family *family; 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 }, }; +static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = { + [SWITCH_PORTMAP_SEGMENT] = { .type = NLA_STRING }, + [SWITCH_PORTMAP_VIRT] = { .type = NLA_U32 }, +}; + static inline void * swlib_alloc(size_t size) { @@ -574,6 +579,41 @@ struct swlib_scan_arg { }; 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) { struct swlib_scan_arg *sa = arg; @@ -610,6 +650,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]); + if (tb[SWITCH_ATTR_PORTMAP]) + add_port_map(dev, tb[SWITCH_ATTR_PORTMAP]); if (!sa->head) { sa->head = dev; @@ -655,6 +697,34 @@ swlib_list(void) 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) { diff --git a/package/network/config/swconfig/src/swlib.h b/package/network/config/swconfig/src/swlib.h index 02fa45610c..016f74b4fd 100644 --- a/package/network/config/swconfig/src/swlib.h +++ b/package/network/config/swconfig/src/swlib.h @@ -109,6 +109,7 @@ enum swlib_port_flags { struct switch_dev; struct switch_attr; struct switch_port; +struct switch_port_map; struct switch_val; struct uci_package; @@ -123,6 +124,7 @@ struct switch_dev { struct switch_attr *ops; struct switch_attr *port_ops; struct switch_attr *vlan_ops; + struct switch_portmap *maps; struct switch_dev *next; void *priv; }; @@ -154,12 +156,23 @@ struct switch_port { unsigned int flags; }; +struct switch_portmap { + unsigned int virt; + const char *segment; +}; + /** * swlib_list: list all switches */ void swlib_list(void); /** + * swlib_print_portmap: get portmap + * @dev: switch device struct + */ +void swlib_print_portmap(struct switch_dev *dev, char *segment); + +/** * swlib_connect: connect to the switch through netlink * @name: name of the ethernet interface, * -- 2.11.0