2 * swlib.c: Switch configuration API (user space part)
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * version 2.1 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <linux/switch.h>
27 #include <netlink/netlink.h>
28 #include <netlink/genl/genl.h>
29 #include <netlink/genl/family.h>
33 #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
35 #define DPRINTF(fmt, ...) do {} while (0)
38 static struct nl_sock *handle;
39 static struct nl_cache *cache;
40 static struct genl_family *family;
41 static struct nlattr *tb[SWITCH_ATTR_MAX + 1];
42 static int refcount = 0;
44 static struct nla_policy port_policy[SWITCH_ATTR_MAX] = {
45 [SWITCH_PORT_ID] = { .type = NLA_U32 },
46 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
49 static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = {
50 [SWITCH_PORTMAP_SEGMENT] = { .type = NLA_STRING },
51 [SWITCH_PORTMAP_VIRT] = { .type = NLA_U32 },
55 swlib_alloc(size_t size)
69 wait_handler(struct nl_msg *msg, void *arg)
77 /* helper function for performing netlink requests */
79 swlib_call(int cmd, int (*call)(struct nl_msg *, void *),
80 int (*data)(struct nl_msg *, void *), void *arg)
83 struct nl_cb *cb = NULL;
90 fprintf(stderr, "Out of memory!\n");
97 genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, genl_family_get_id(family), 0, flags, cmd, 0);
99 if (data(msg, arg) < 0)
100 goto nla_put_failure;
103 cb = nl_cb_alloc(NL_CB_CUSTOM);
105 fprintf(stderr, "nl_cb_alloc failed.\n");
109 err = nl_send_auto_complete(handle, msg);
111 fprintf(stderr, "nl_send_auto_complete failed: %d\n", err);
118 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, call, arg);
121 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
123 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &finished);
125 err = nl_recvmsgs(handle, cb);
131 err = nl_wait_for_ack(handle);
142 send_attr(struct nl_msg *msg, void *arg)
144 struct switch_val *val = arg;
145 struct switch_attr *attr = val->attr;
147 NLA_PUT_U32(msg, SWITCH_ATTR_ID, attr->dev->id);
148 NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, attr->id);
149 switch(attr->atype) {
150 case SWLIB_ATTR_GROUP_PORT:
151 NLA_PUT_U32(msg, SWITCH_ATTR_OP_PORT, val->port_vlan);
153 case SWLIB_ATTR_GROUP_VLAN:
154 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VLAN, val->port_vlan);
167 store_port_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
170 int ports = val->attr->dev->ports;
174 if (!val->value.ports)
175 val->value.ports = malloc(sizeof(struct switch_port) * ports);
177 nla_for_each_nested(p, nla, remaining) {
178 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
179 struct switch_port *port;
181 if (val->len >= ports)
184 err = nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, p, port_policy);
188 if (!tb[SWITCH_PORT_ID])
191 port = &val->value.ports[val->len];
192 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
194 if (tb[SWITCH_PORT_FLAG_TAGGED])
195 port->flags |= SWLIB_PORT_FLAG_TAGGED;
205 store_val(struct nl_msg *msg, void *arg)
207 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
208 struct switch_val *val = arg;
213 if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
214 genlmsg_attrlen(gnlh, 0), NULL) < 0) {
218 if (tb[SWITCH_ATTR_OP_VALUE_INT])
219 val->value.i = nla_get_u32(tb[SWITCH_ATTR_OP_VALUE_INT]);
220 else if (tb[SWITCH_ATTR_OP_VALUE_STR])
221 val->value.s = strdup(nla_get_string(tb[SWITCH_ATTR_OP_VALUE_STR]));
222 else if (tb[SWITCH_ATTR_OP_VALUE_PORTS])
223 val->err = store_port_val(msg, tb[SWITCH_ATTR_OP_VALUE_PORTS], val);
233 swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
238 switch(attr->atype) {
239 case SWLIB_ATTR_GROUP_GLOBAL:
240 cmd = SWITCH_CMD_GET_GLOBAL;
242 case SWLIB_ATTR_GROUP_PORT:
243 cmd = SWITCH_CMD_GET_PORT;
245 case SWLIB_ATTR_GROUP_VLAN:
246 cmd = SWITCH_CMD_GET_VLAN;
252 memset(&val->value, 0, sizeof(val->value));
256 err = swlib_call(cmd, store_val, send_attr, val);
264 send_attr_ports(struct nl_msg *msg, struct switch_val *val)
269 /* TODO implement multipart? */
272 n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_PORTS);
274 goto nla_put_failure;
275 for (i = 0; i < val->len; i++) {
276 struct switch_port *port = &val->value.ports[i];
279 np = nla_nest_start(msg, SWITCH_ATTR_PORT);
281 goto nla_put_failure;
283 NLA_PUT_U32(msg, SWITCH_PORT_ID, port->id);
284 if (port->flags & SWLIB_PORT_FLAG_TAGGED)
285 NLA_PUT_FLAG(msg, SWITCH_PORT_FLAG_TAGGED);
287 nla_nest_end(msg, np);
289 nla_nest_end(msg, n);
298 send_attr_val(struct nl_msg *msg, void *arg)
300 struct switch_val *val = arg;
301 struct switch_attr *attr = val->attr;
303 if (send_attr(msg, arg))
304 goto nla_put_failure;
307 case SWITCH_TYPE_NOVAL:
309 case SWITCH_TYPE_INT:
310 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val->value.i);
312 case SWITCH_TYPE_STRING:
314 goto nla_put_failure;
315 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val->value.s);
317 case SWITCH_TYPE_PORTS:
318 if (send_attr_ports(msg, val) < 0)
319 goto nla_put_failure;
322 goto nla_put_failure;
331 swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
335 switch(attr->atype) {
336 case SWLIB_ATTR_GROUP_GLOBAL:
337 cmd = SWITCH_CMD_SET_GLOBAL;
339 case SWLIB_ATTR_GROUP_PORT:
340 cmd = SWITCH_CMD_SET_PORT;
342 case SWLIB_ATTR_GROUP_VLAN:
343 cmd = SWITCH_CMD_SET_VLAN;
350 return swlib_call(cmd, NULL, send_attr_val, val);
353 int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str)
355 struct switch_port *ports;
356 struct switch_val val;
359 memset(&val, 0, sizeof(val));
360 val.port_vlan = port_vlan;
362 case SWITCH_TYPE_INT:
363 val.value.i = atoi(str);
365 case SWITCH_TYPE_STRING:
368 case SWITCH_TYPE_PORTS:
369 ports = alloca(sizeof(struct switch_port) * dev->ports);
370 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
375 while(*ptr && isspace(*ptr))
384 if (val.len >= dev->ports)
387 ports[val.len].flags = 0;
388 ports[val.len].id = strtoul(ptr, &ptr, 10);
389 while(*ptr && !isspace(*ptr)) {
391 ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
401 val.value.ports = ports;
403 case SWITCH_TYPE_NOVAL:
404 if (str && !strcmp(str, "0"))
411 return swlib_set_attr(dev, a, &val);
415 struct attrlist_arg {
418 struct switch_dev *dev;
419 struct switch_attr *prev;
420 struct switch_attr **head;
424 add_id(struct nl_msg *msg, void *arg)
426 struct attrlist_arg *l = arg;
428 NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id);
436 add_attr(struct nl_msg *msg, void *ptr)
438 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
439 struct attrlist_arg *arg = ptr;
440 struct switch_attr *new;
442 if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
443 genlmsg_attrlen(gnlh, 0), NULL) < 0)
446 new = swlib_alloc(sizeof(struct switch_attr));
451 new->atype = arg->atype;
453 arg->prev->next = new;
455 arg->prev = *arg->head;
458 arg->head = &new->next;
460 if (tb[SWITCH_ATTR_OP_ID])
461 new->id = nla_get_u32(tb[SWITCH_ATTR_OP_ID]);
462 if (tb[SWITCH_ATTR_OP_TYPE])
463 new->type = nla_get_u32(tb[SWITCH_ATTR_OP_TYPE]);
464 if (tb[SWITCH_ATTR_OP_NAME])
465 new->name = strdup(nla_get_string(tb[SWITCH_ATTR_OP_NAME]));
466 if (tb[SWITCH_ATTR_OP_DESCRIPTION])
467 new->description = strdup(nla_get_string(tb[SWITCH_ATTR_OP_DESCRIPTION]));
474 swlib_scan(struct switch_dev *dev)
476 struct attrlist_arg arg;
478 if (dev->ops || dev->port_ops || dev->vlan_ops)
481 arg.atype = SWLIB_ATTR_GROUP_GLOBAL;
485 arg.head = &dev->ops;
486 swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg);
488 arg.atype = SWLIB_ATTR_GROUP_PORT;
490 arg.head = &dev->port_ops;
491 swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg);
493 arg.atype = SWLIB_ATTR_GROUP_VLAN;
495 arg.head = &dev->vlan_ops;
496 swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg);
501 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
502 enum swlib_attr_group atype, const char *name)
504 struct switch_attr *head;
510 case SWLIB_ATTR_GROUP_GLOBAL:
513 case SWLIB_ATTR_GROUP_PORT:
514 head = dev->port_ops;
516 case SWLIB_ATTR_GROUP_VLAN:
517 head = dev->vlan_ops;
521 if (!strcmp(name, head->name))
530 swlib_priv_free(void)
533 nl_cache_free(cache);
535 nl_socket_free(handle);
541 swlib_priv_init(void)
545 handle = nl_socket_alloc();
547 DPRINTF("Failed to create handle\n");
551 if (genl_connect(handle)) {
552 DPRINTF("Failed to connect to generic netlink\n");
556 ret = genl_ctrl_alloc_cache(handle, &cache);
558 DPRINTF("Failed to allocate netlink cache\n");
562 family = genl_ctrl_search_by_name(cache, "switch");
564 DPRINTF("Switch API not present\n");
574 struct swlib_scan_arg {
576 struct switch_dev *head;
577 struct switch_dev *ptr;
581 add_port_map(struct switch_dev *dev, struct nlattr *nla)
584 int err = 0, idx = 0;
587 dev->maps = malloc(sizeof(struct switch_portmap) * dev->ports);
590 memset(dev->maps, 0, sizeof(struct switch_portmap) * dev->ports);
592 nla_for_each_nested(p, nla, remaining) {
593 struct nlattr *tb[SWITCH_PORTMAP_MAX+1];
595 if (idx >= dev->ports)
598 err = nla_parse_nested(tb, SWITCH_PORTMAP_MAX, p, portmap_policy);
603 if (tb[SWITCH_PORTMAP_SEGMENT] && tb[SWITCH_PORTMAP_VIRT]) {
604 dev->maps[idx].segment = strdup(nla_get_string(tb[SWITCH_PORTMAP_SEGMENT]));
605 dev->maps[idx].virt = nla_get_u32(tb[SWITCH_PORTMAP_VIRT]);
616 add_switch(struct nl_msg *msg, void *arg)
618 struct swlib_scan_arg *sa = arg;
619 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
620 struct switch_dev *dev;
624 if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
627 if (!tb[SWITCH_ATTR_DEV_NAME])
630 name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
631 alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]);
633 if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0))
636 dev = swlib_alloc(sizeof(struct switch_dev));
640 strncpy(dev->dev_name, name, IFNAMSIZ - 1);
641 dev->alias = strdup(alias);
642 if (tb[SWITCH_ATTR_ID])
643 dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
644 if (tb[SWITCH_ATTR_NAME])
645 dev->name = strdup(nla_get_string(tb[SWITCH_ATTR_NAME]));
646 if (tb[SWITCH_ATTR_PORTS])
647 dev->ports = nla_get_u32(tb[SWITCH_ATTR_PORTS]);
648 if (tb[SWITCH_ATTR_VLANS])
649 dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
650 if (tb[SWITCH_ATTR_CPU_PORT])
651 dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]);
652 if (tb[SWITCH_ATTR_PORTMAP])
653 add_port_map(dev, tb[SWITCH_ATTR_PORTMAP]);
669 list_switch(struct nl_msg *msg, void *arg)
671 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
673 if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
676 if (!tb[SWITCH_ATTR_DEV_NAME] || !tb[SWITCH_ATTR_NAME])
679 printf("Found: %s - %s\n", nla_get_string(tb[SWITCH_ATTR_DEV_NAME]),
680 nla_get_string(tb[SWITCH_ATTR_ALIAS]));
689 if (swlib_priv_init() < 0)
691 swlib_call(SWITCH_CMD_GET_SWITCH, list_switch, NULL, NULL);
696 swlib_print_portmap(struct switch_dev *dev, char *segment)
701 if (!strcmp(segment, "cpu")) {
702 printf("%d ", dev->cpu_port);
703 } else if (!strcmp(segment, "disabled")) {
704 for (i = 0; i < dev->ports; i++)
705 if (!dev->maps[i].segment)
707 } else for (i = 0; i < dev->ports; i++) {
708 if (dev->maps[i].segment && !strcmp(dev->maps[i].segment, segment))
712 printf("%s - %s\n", dev->dev_name, dev->name);
713 for (i = 0; i < dev->ports; i++)
714 if (i == dev->cpu_port)
715 printf("port%d:\tcpu\n", i);
716 else if (dev->maps[i].segment)
717 printf("port%d:\t%s.%d\n", i, dev->maps[i].segment, dev->maps[i].virt);
719 printf("port%d:\tdisabled\n", i);
724 swlib_connect(const char *name)
726 struct swlib_scan_arg arg;
729 if (swlib_priv_init() < 0)
736 swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg);
745 swlib_free_attributes(struct switch_attr **head)
747 struct switch_attr *a = *head;
748 struct switch_attr *next;
759 swlib_free(struct switch_dev *dev)
761 swlib_free_attributes(&dev->ops);
762 swlib_free_attributes(&dev->port_ops);
763 swlib_free_attributes(&dev->vlan_ops);
773 swlib_free_all(struct switch_dev *dev)
775 struct switch_dev *p;