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 },
54 static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = {
55 [SWITCH_LINK_FLAG_LINK] = { .type = NLA_FLAG },
56 [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG },
57 [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG },
58 [SWITCH_LINK_SPEED] = { .type = NLA_U32 },
59 [SWITCH_LINK_FLAG_EEE_100BASET] = { .type = NLA_FLAG },
60 [SWITCH_LINK_FLAG_EEE_1000BASET] = { .type = NLA_FLAG },
64 swlib_alloc(size_t size)
78 wait_handler(struct nl_msg *msg, void *arg)
86 /* helper function for performing netlink requests */
88 swlib_call(int cmd, int (*call)(struct nl_msg *, void *),
89 int (*data)(struct nl_msg *, void *), void *arg)
92 struct nl_cb *cb = NULL;
99 fprintf(stderr, "Out of memory!\n");
106 genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, genl_family_get_id(family), 0, flags, cmd, 0);
108 if (data(msg, arg) < 0)
109 goto nla_put_failure;
112 cb = nl_cb_alloc(NL_CB_CUSTOM);
114 fprintf(stderr, "nl_cb_alloc failed.\n");
118 err = nl_send_auto_complete(handle, msg);
120 fprintf(stderr, "nl_send_auto_complete failed: %d\n", err);
127 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, call, arg);
130 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
132 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &finished);
134 err = nl_recvmsgs(handle, cb);
140 err = nl_wait_for_ack(handle);
151 send_attr(struct nl_msg *msg, void *arg)
153 struct switch_val *val = arg;
154 struct switch_attr *attr = val->attr;
156 NLA_PUT_U32(msg, SWITCH_ATTR_ID, attr->dev->id);
157 NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, attr->id);
158 switch(attr->atype) {
159 case SWLIB_ATTR_GROUP_PORT:
160 NLA_PUT_U32(msg, SWITCH_ATTR_OP_PORT, val->port_vlan);
162 case SWLIB_ATTR_GROUP_VLAN:
163 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VLAN, val->port_vlan);
176 store_port_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
179 int ports = val->attr->dev->ports;
183 if (!val->value.ports)
184 val->value.ports = malloc(sizeof(struct switch_port) * ports);
186 nla_for_each_nested(p, nla, remaining) {
187 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
188 struct switch_port *port;
190 if (val->len >= ports)
193 err = nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, p, port_policy);
197 if (!tb[SWITCH_PORT_ID])
200 port = &val->value.ports[val->len];
201 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
203 if (tb[SWITCH_PORT_FLAG_TAGGED])
204 port->flags |= SWLIB_PORT_FLAG_TAGGED;
214 store_link_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
216 struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
217 struct switch_port_link *link;
220 if (!val->value.link)
221 val->value.link = malloc(sizeof(struct switch_port_link));
223 err = nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy);
227 link = val->value.link;
228 link->link = !!tb[SWITCH_LINK_FLAG_LINK];
229 link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX];
230 link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG];
231 link->tx_flow = !!tb[SWITCH_LINK_FLAG_TX_FLOW];
232 link->rx_flow = !!tb[SWITCH_LINK_FLAG_RX_FLOW];
233 link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]);
235 if (tb[SWITCH_LINK_FLAG_EEE_100BASET])
236 link->eee |= SWLIB_LINK_FLAG_EEE_100BASET;
237 if (tb[SWITCH_LINK_FLAG_EEE_1000BASET])
238 link->eee |= SWLIB_LINK_FLAG_EEE_1000BASET;
245 store_val(struct nl_msg *msg, void *arg)
247 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
248 struct switch_val *val = arg;
253 if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
254 genlmsg_attrlen(gnlh, 0), NULL) < 0) {
258 if (tb[SWITCH_ATTR_OP_VALUE_INT])
259 val->value.i = nla_get_u32(tb[SWITCH_ATTR_OP_VALUE_INT]);
260 else if (tb[SWITCH_ATTR_OP_VALUE_STR])
261 val->value.s = strdup(nla_get_string(tb[SWITCH_ATTR_OP_VALUE_STR]));
262 else if (tb[SWITCH_ATTR_OP_VALUE_PORTS])
263 val->err = store_port_val(msg, tb[SWITCH_ATTR_OP_VALUE_PORTS], val);
264 else if (tb[SWITCH_ATTR_OP_VALUE_LINK])
265 val->err = store_link_val(msg, tb[SWITCH_ATTR_OP_VALUE_LINK], val);
275 swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
280 switch(attr->atype) {
281 case SWLIB_ATTR_GROUP_GLOBAL:
282 cmd = SWITCH_CMD_GET_GLOBAL;
284 case SWLIB_ATTR_GROUP_PORT:
285 cmd = SWITCH_CMD_GET_PORT;
287 case SWLIB_ATTR_GROUP_VLAN:
288 cmd = SWITCH_CMD_GET_VLAN;
294 memset(&val->value, 0, sizeof(val->value));
298 err = swlib_call(cmd, store_val, send_attr, val);
306 send_attr_ports(struct nl_msg *msg, struct switch_val *val)
311 /* TODO implement multipart? */
314 n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_PORTS);
316 goto nla_put_failure;
317 for (i = 0; i < val->len; i++) {
318 struct switch_port *port = &val->value.ports[i];
321 np = nla_nest_start(msg, SWITCH_ATTR_PORT);
323 goto nla_put_failure;
325 NLA_PUT_U32(msg, SWITCH_PORT_ID, port->id);
326 if (port->flags & SWLIB_PORT_FLAG_TAGGED)
327 NLA_PUT_FLAG(msg, SWITCH_PORT_FLAG_TAGGED);
329 nla_nest_end(msg, np);
331 nla_nest_end(msg, n);
340 send_attr_link(struct nl_msg *msg, struct switch_val *val)
342 struct switch_port_link *link = val->value.link;
345 n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_LINK);
347 goto nla_put_failure;
350 NLA_PUT_FLAG(msg, SWITCH_LINK_FLAG_DUPLEX);
352 NLA_PUT_FLAG(msg, SWITCH_LINK_FLAG_ANEG);
353 NLA_PUT_U32(msg, SWITCH_LINK_SPEED, link->speed);
355 nla_nest_end(msg, n);
364 send_attr_val(struct nl_msg *msg, void *arg)
366 struct switch_val *val = arg;
367 struct switch_attr *attr = val->attr;
369 if (send_attr(msg, arg))
370 goto nla_put_failure;
373 case SWITCH_TYPE_NOVAL:
375 case SWITCH_TYPE_INT:
376 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val->value.i);
378 case SWITCH_TYPE_STRING:
380 goto nla_put_failure;
381 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val->value.s);
383 case SWITCH_TYPE_PORTS:
384 if (send_attr_ports(msg, val) < 0)
385 goto nla_put_failure;
387 case SWITCH_TYPE_LINK:
388 if (send_attr_link(msg, val))
389 goto nla_put_failure;
392 goto nla_put_failure;
401 swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
405 switch(attr->atype) {
406 case SWLIB_ATTR_GROUP_GLOBAL:
407 cmd = SWITCH_CMD_SET_GLOBAL;
409 case SWLIB_ATTR_GROUP_PORT:
410 cmd = SWITCH_CMD_SET_PORT;
412 case SWLIB_ATTR_GROUP_VLAN:
413 cmd = SWITCH_CMD_SET_VLAN;
420 return swlib_call(cmd, NULL, send_attr_val, val);
423 int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str)
425 struct switch_port *ports;
426 struct switch_val val;
429 memset(&val, 0, sizeof(val));
430 val.port_vlan = port_vlan;
432 case SWITCH_TYPE_INT:
433 val.value.i = atoi(str);
435 case SWITCH_TYPE_STRING:
436 val.value.s = (char *)str;
438 case SWITCH_TYPE_PORTS:
439 ports = alloca(sizeof(struct switch_port) * dev->ports);
440 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
445 while(*ptr && isspace(*ptr))
454 if (val.len >= dev->ports)
457 ports[val.len].flags = 0;
458 ports[val.len].id = strtoul(ptr, &ptr, 10);
459 while(*ptr && !isspace(*ptr)) {
461 ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
471 val.value.ports = ports;
473 case SWITCH_TYPE_NOVAL:
474 if (str && !strcmp(str, "0"))
481 return swlib_set_attr(dev, a, &val);
485 struct attrlist_arg {
488 struct switch_dev *dev;
489 struct switch_attr *prev;
490 struct switch_attr **head;
494 add_id(struct nl_msg *msg, void *arg)
496 struct attrlist_arg *l = arg;
498 NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id);
506 add_attr(struct nl_msg *msg, void *ptr)
508 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
509 struct attrlist_arg *arg = ptr;
510 struct switch_attr *new;
512 if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
513 genlmsg_attrlen(gnlh, 0), NULL) < 0)
516 new = swlib_alloc(sizeof(struct switch_attr));
521 new->atype = arg->atype;
523 arg->prev->next = new;
525 arg->prev = *arg->head;
528 arg->head = &new->next;
530 if (tb[SWITCH_ATTR_OP_ID])
531 new->id = nla_get_u32(tb[SWITCH_ATTR_OP_ID]);
532 if (tb[SWITCH_ATTR_OP_TYPE])
533 new->type = nla_get_u32(tb[SWITCH_ATTR_OP_TYPE]);
534 if (tb[SWITCH_ATTR_OP_NAME])
535 new->name = strdup(nla_get_string(tb[SWITCH_ATTR_OP_NAME]));
536 if (tb[SWITCH_ATTR_OP_DESCRIPTION])
537 new->description = strdup(nla_get_string(tb[SWITCH_ATTR_OP_DESCRIPTION]));
544 swlib_scan(struct switch_dev *dev)
546 struct attrlist_arg arg;
548 if (dev->ops || dev->port_ops || dev->vlan_ops)
551 arg.atype = SWLIB_ATTR_GROUP_GLOBAL;
555 arg.head = &dev->ops;
556 swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg);
558 arg.atype = SWLIB_ATTR_GROUP_PORT;
560 arg.head = &dev->port_ops;
561 swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg);
563 arg.atype = SWLIB_ATTR_GROUP_VLAN;
565 arg.head = &dev->vlan_ops;
566 swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg);
571 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
572 enum swlib_attr_group atype, const char *name)
574 struct switch_attr *head;
580 case SWLIB_ATTR_GROUP_GLOBAL:
583 case SWLIB_ATTR_GROUP_PORT:
584 head = dev->port_ops;
586 case SWLIB_ATTR_GROUP_VLAN:
587 head = dev->vlan_ops;
591 if (!strcmp(name, head->name))
600 swlib_priv_free(void)
603 nl_object_put((struct nl_object*)family);
605 nl_cache_free(cache);
607 nl_socket_free(handle);
614 swlib_priv_init(void)
618 handle = nl_socket_alloc();
620 DPRINTF("Failed to create handle\n");
624 if (genl_connect(handle)) {
625 DPRINTF("Failed to connect to generic netlink\n");
629 ret = genl_ctrl_alloc_cache(handle, &cache);
631 DPRINTF("Failed to allocate netlink cache\n");
635 family = genl_ctrl_search_by_name(cache, "switch");
637 DPRINTF("Switch API not present\n");
647 struct swlib_scan_arg {
649 struct switch_dev *head;
650 struct switch_dev *ptr;
654 add_port_map(struct switch_dev *dev, struct nlattr *nla)
657 int err = 0, idx = 0;
660 dev->maps = malloc(sizeof(struct switch_portmap) * dev->ports);
663 memset(dev->maps, 0, sizeof(struct switch_portmap) * dev->ports);
665 nla_for_each_nested(p, nla, remaining) {
666 struct nlattr *tb[SWITCH_PORTMAP_MAX+1];
668 if (idx >= dev->ports)
671 err = nla_parse_nested(tb, SWITCH_PORTMAP_MAX, p, portmap_policy);
676 if (tb[SWITCH_PORTMAP_SEGMENT] && tb[SWITCH_PORTMAP_VIRT]) {
677 dev->maps[idx].segment = strdup(nla_get_string(tb[SWITCH_PORTMAP_SEGMENT]));
678 dev->maps[idx].virt = nla_get_u32(tb[SWITCH_PORTMAP_VIRT]);
689 add_switch(struct nl_msg *msg, void *arg)
691 struct swlib_scan_arg *sa = arg;
692 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
693 struct switch_dev *dev;
697 if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
700 if (!tb[SWITCH_ATTR_DEV_NAME])
703 name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
704 alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]);
706 if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0))
709 dev = swlib_alloc(sizeof(struct switch_dev));
713 strncpy(dev->dev_name, name, IFNAMSIZ - 1);
714 dev->alias = strdup(alias);
715 if (tb[SWITCH_ATTR_ID])
716 dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
717 if (tb[SWITCH_ATTR_NAME])
718 dev->name = strdup(nla_get_string(tb[SWITCH_ATTR_NAME]));
719 if (tb[SWITCH_ATTR_PORTS])
720 dev->ports = nla_get_u32(tb[SWITCH_ATTR_PORTS]);
721 if (tb[SWITCH_ATTR_VLANS])
722 dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
723 if (tb[SWITCH_ATTR_CPU_PORT])
724 dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]);
725 if (tb[SWITCH_ATTR_PORTMAP])
726 add_port_map(dev, tb[SWITCH_ATTR_PORTMAP]);
742 list_switch(struct nl_msg *msg, void *arg)
744 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
746 if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
749 if (!tb[SWITCH_ATTR_DEV_NAME] || !tb[SWITCH_ATTR_NAME])
752 printf("Found: %s - %s\n", nla_get_string(tb[SWITCH_ATTR_DEV_NAME]),
753 nla_get_string(tb[SWITCH_ATTR_ALIAS]));
762 if (swlib_priv_init() < 0)
764 swlib_call(SWITCH_CMD_GET_SWITCH, list_switch, NULL, NULL);
769 swlib_print_portmap(struct switch_dev *dev, char *segment)
774 if (!strcmp(segment, "cpu")) {
775 printf("%d ", dev->cpu_port);
776 } else if (!strcmp(segment, "disabled")) {
777 for (i = 0; i < dev->ports; i++)
778 if (!dev->maps[i].segment)
780 } else for (i = 0; i < dev->ports; i++) {
781 if (dev->maps[i].segment && !strcmp(dev->maps[i].segment, segment))
785 printf("%s - %s\n", dev->dev_name, dev->name);
786 for (i = 0; i < dev->ports; i++)
787 if (i == dev->cpu_port)
788 printf("port%d:\tcpu\n", i);
789 else if (dev->maps[i].segment)
790 printf("port%d:\t%s.%d\n", i, dev->maps[i].segment, dev->maps[i].virt);
792 printf("port%d:\tdisabled\n", i);
797 swlib_connect(const char *name)
799 struct swlib_scan_arg arg;
802 if (swlib_priv_init() < 0)
809 swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg);
818 swlib_free_attributes(struct switch_attr **head)
820 struct switch_attr *a = *head;
821 struct switch_attr *next;
826 free(a->description);
834 swlib_free_port_map(struct switch_dev *dev)
838 if (!dev || !dev->maps)
841 for (i = 0; i < dev->ports; i++)
842 free(dev->maps[i].segment);
847 swlib_free(struct switch_dev *dev)
849 swlib_free_attributes(&dev->ops);
850 swlib_free_attributes(&dev->port_ops);
851 swlib_free_attributes(&dev->vlan_ops);
852 swlib_free_port_map(dev);
862 swlib_free_all(struct switch_dev *dev)
864 struct switch_dev *p;