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[] = {
45 [SWITCH_PORT_ID] = { .type = NLA_U32 },
46 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
50 swlib_alloc(size_t size)
64 wait_handler(struct nl_msg *msg, void *arg)
72 /* helper function for performing netlink requests */
74 swlib_call(int cmd, int (*call)(struct nl_msg *, void *),
75 int (*data)(struct nl_msg *, void *), void *arg)
78 struct nl_cb *cb = NULL;
85 fprintf(stderr, "Out of memory!\n");
92 genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, genl_family_get_id(family), 0, flags, cmd, 0);
94 if (data(msg, arg) < 0)
98 cb = nl_cb_alloc(NL_CB_CUSTOM);
100 fprintf(stderr, "nl_cb_alloc failed.\n");
104 err = nl_send_auto_complete(handle, msg);
106 fprintf(stderr, "nl_send_auto_complete failed: %d\n", err);
113 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, call, arg);
116 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
118 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &finished);
120 err = nl_recvmsgs(handle, cb);
126 err = nl_wait_for_ack(handle);
137 send_attr(struct nl_msg *msg, void *arg)
139 struct switch_val *val = arg;
140 struct switch_attr *attr = val->attr;
142 NLA_PUT_U32(msg, SWITCH_ATTR_ID, attr->dev->id);
143 NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, attr->id);
144 switch(attr->atype) {
145 case SWLIB_ATTR_GROUP_PORT:
146 NLA_PUT_U32(msg, SWITCH_ATTR_OP_PORT, val->port_vlan);
148 case SWLIB_ATTR_GROUP_VLAN:
149 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VLAN, val->port_vlan);
162 store_port_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
165 int ports = val->attr->dev->ports;
169 if (!val->value.ports)
170 val->value.ports = malloc(sizeof(struct switch_port) * ports);
172 nla_for_each_nested(p, nla, remaining) {
173 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
174 struct switch_port *port;
176 if (val->len >= ports)
179 err = nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, p, port_policy);
183 if (!tb[SWITCH_PORT_ID])
186 port = &val->value.ports[val->len];
187 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
189 if (tb[SWITCH_PORT_FLAG_TAGGED])
190 port->flags |= SWLIB_PORT_FLAG_TAGGED;
200 store_val(struct nl_msg *msg, void *arg)
202 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
203 struct switch_val *val = arg;
204 struct switch_attr *attr = val->attr;
209 if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
210 genlmsg_attrlen(gnlh, 0), NULL) < 0) {
214 if (tb[SWITCH_ATTR_OP_VALUE_INT])
215 val->value.i = nla_get_u32(tb[SWITCH_ATTR_OP_VALUE_INT]);
216 else if (tb[SWITCH_ATTR_OP_VALUE_STR])
217 val->value.s = strdup(nla_get_string(tb[SWITCH_ATTR_OP_VALUE_STR]));
218 else if (tb[SWITCH_ATTR_OP_VALUE_PORTS])
219 val->err = store_port_val(msg, tb[SWITCH_ATTR_OP_VALUE_PORTS], val);
229 swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
234 switch(attr->atype) {
235 case SWLIB_ATTR_GROUP_GLOBAL:
236 cmd = SWITCH_CMD_GET_GLOBAL;
238 case SWLIB_ATTR_GROUP_PORT:
239 cmd = SWITCH_CMD_GET_PORT;
241 case SWLIB_ATTR_GROUP_VLAN:
242 cmd = SWITCH_CMD_GET_VLAN;
248 memset(&val->value, 0, sizeof(val->value));
252 err = swlib_call(cmd, store_val, send_attr, val);
260 send_attr_ports(struct nl_msg *msg, struct switch_val *val)
265 /* TODO implement multipart? */
268 n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_PORTS);
270 goto nla_put_failure;
271 for (i = 0; i < val->len; i++) {
272 struct switch_port *port = &val->value.ports[i];
275 np = nla_nest_start(msg, SWITCH_ATTR_PORT);
277 goto nla_put_failure;
279 NLA_PUT_U32(msg, SWITCH_PORT_ID, port->id);
280 if (port->flags & SWLIB_PORT_FLAG_TAGGED)
281 NLA_PUT_FLAG(msg, SWITCH_PORT_FLAG_TAGGED);
283 nla_nest_end(msg, np);
285 nla_nest_end(msg, n);
294 send_attr_val(struct nl_msg *msg, void *arg)
296 struct switch_val *val = arg;
297 struct switch_attr *attr = val->attr;
299 if (send_attr(msg, arg))
300 goto nla_put_failure;
303 case SWITCH_TYPE_NOVAL:
305 case SWITCH_TYPE_INT:
306 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val->value.i);
308 case SWITCH_TYPE_STRING:
310 goto nla_put_failure;
311 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val->value.s);
313 case SWITCH_TYPE_PORTS:
314 if (send_attr_ports(msg, val) < 0)
315 goto nla_put_failure;
318 goto nla_put_failure;
327 swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
331 switch(attr->atype) {
332 case SWLIB_ATTR_GROUP_GLOBAL:
333 cmd = SWITCH_CMD_SET_GLOBAL;
335 case SWLIB_ATTR_GROUP_PORT:
336 cmd = SWITCH_CMD_SET_PORT;
338 case SWLIB_ATTR_GROUP_VLAN:
339 cmd = SWITCH_CMD_SET_VLAN;
346 return swlib_call(cmd, NULL, send_attr_val, val);
349 int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str)
351 struct switch_port *ports;
352 struct switch_val val;
355 memset(&val, 0, sizeof(val));
356 val.port_vlan = port_vlan;
358 case SWITCH_TYPE_INT:
359 val.value.i = atoi(str);
361 case SWITCH_TYPE_STRING:
364 case SWITCH_TYPE_PORTS:
365 ports = alloca(sizeof(struct switch_port) * dev->ports);
366 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
371 while(*ptr && isspace(*ptr))
380 if (val.len >= dev->ports)
383 ports[val.len].flags = 0;
384 ports[val.len].id = strtoul(ptr, &ptr, 10);
385 while(*ptr && !isspace(*ptr)) {
387 ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
397 val.value.ports = ports;
399 case SWITCH_TYPE_NOVAL:
400 if (str && !strcmp(str, "0"))
407 return swlib_set_attr(dev, a, &val);
411 struct attrlist_arg {
414 struct switch_dev *dev;
415 struct switch_attr *prev;
416 struct switch_attr **head;
420 add_id(struct nl_msg *msg, void *arg)
422 struct attrlist_arg *l = arg;
424 NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id);
432 add_attr(struct nl_msg *msg, void *ptr)
434 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
435 struct attrlist_arg *arg = ptr;
436 struct switch_attr *new;
438 if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
439 genlmsg_attrlen(gnlh, 0), NULL) < 0)
442 new = swlib_alloc(sizeof(struct switch_attr));
447 new->atype = arg->atype;
449 arg->prev->next = new;
451 arg->prev = *arg->head;
454 arg->head = &new->next;
456 if (tb[SWITCH_ATTR_OP_ID])
457 new->id = nla_get_u32(tb[SWITCH_ATTR_OP_ID]);
458 if (tb[SWITCH_ATTR_OP_TYPE])
459 new->type = nla_get_u32(tb[SWITCH_ATTR_OP_TYPE]);
460 if (tb[SWITCH_ATTR_OP_NAME])
461 new->name = strdup(nla_get_string(tb[SWITCH_ATTR_OP_NAME]));
462 if (tb[SWITCH_ATTR_OP_DESCRIPTION])
463 new->description = strdup(nla_get_string(tb[SWITCH_ATTR_OP_DESCRIPTION]));
470 swlib_scan(struct switch_dev *dev)
472 struct attrlist_arg arg;
474 if (dev->ops || dev->port_ops || dev->vlan_ops)
477 arg.atype = SWLIB_ATTR_GROUP_GLOBAL;
481 arg.head = &dev->ops;
482 swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg);
484 arg.atype = SWLIB_ATTR_GROUP_PORT;
486 arg.head = &dev->port_ops;
487 swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg);
489 arg.atype = SWLIB_ATTR_GROUP_VLAN;
491 arg.head = &dev->vlan_ops;
492 swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg);
497 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
498 enum swlib_attr_group atype, const char *name)
500 struct switch_attr *head;
506 case SWLIB_ATTR_GROUP_GLOBAL:
509 case SWLIB_ATTR_GROUP_PORT:
510 head = dev->port_ops;
512 case SWLIB_ATTR_GROUP_VLAN:
513 head = dev->vlan_ops;
517 if (!strcmp(name, head->name))
526 swlib_priv_free(void)
529 nl_cache_free(cache);
531 nl_socket_free(handle);
537 swlib_priv_init(void)
541 handle = nl_socket_alloc();
543 DPRINTF("Failed to create handle\n");
547 if (genl_connect(handle)) {
548 DPRINTF("Failed to connect to generic netlink\n");
552 ret = genl_ctrl_alloc_cache(handle, &cache);
554 DPRINTF("Failed to allocate netlink cache\n");
558 family = genl_ctrl_search_by_name(cache, "switch");
560 DPRINTF("Switch API not present\n");
570 struct swlib_scan_arg {
572 struct switch_dev *head;
573 struct switch_dev *ptr;
577 add_switch(struct nl_msg *msg, void *arg)
579 struct swlib_scan_arg *sa = arg;
580 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
581 struct switch_dev *dev;
585 if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
588 if (!tb[SWITCH_ATTR_DEV_NAME])
591 name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
592 alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]);
594 if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0))
597 dev = swlib_alloc(sizeof(struct switch_dev));
601 strncpy(dev->dev_name, name, IFNAMSIZ - 1);
602 dev->alias = strdup(alias);
603 if (tb[SWITCH_ATTR_ID])
604 dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
605 if (tb[SWITCH_ATTR_NAME])
606 dev->name = strdup(nla_get_string(tb[SWITCH_ATTR_NAME]));
607 if (tb[SWITCH_ATTR_PORTS])
608 dev->ports = nla_get_u32(tb[SWITCH_ATTR_PORTS]);
609 if (tb[SWITCH_ATTR_VLANS])
610 dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
611 if (tb[SWITCH_ATTR_CPU_PORT])
612 dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]);
629 swlib_connect(const char *name)
631 struct swlib_scan_arg arg;
635 if (swlib_priv_init() < 0)
642 swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg);
651 swlib_free_attributes(struct switch_attr **head)
653 struct switch_attr *a = *head;
654 struct switch_attr *next;
665 swlib_free(struct switch_dev *dev)
667 swlib_free_attributes(&dev->ops);
668 swlib_free_attributes(&dev->port_ops);
669 swlib_free_attributes(&dev->vlan_ops);
677 swlib_free_all(struct switch_dev *dev)
679 struct switch_dev *p;