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];
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 ports[val.len].flags = 0;
372 ports[val.len].id = strtoul(ptr, &ptr, 10);
373 while(*ptr && !isspace(*ptr)) {
375 ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
382 val.value.ports = ports;
384 case SWITCH_TYPE_NOVAL:
389 return swlib_set_attr(dev, a, &val);
393 struct attrlist_arg {
396 struct switch_dev *dev;
397 struct switch_attr *prev;
398 struct switch_attr **head;
402 add_id(struct nl_msg *msg, void *arg)
404 struct attrlist_arg *l = arg;
406 NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id);
414 add_attr(struct nl_msg *msg, void *ptr)
416 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
417 struct attrlist_arg *arg = ptr;
418 struct switch_attr *new;
420 if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
421 genlmsg_attrlen(gnlh, 0), NULL) < 0)
424 new = swlib_alloc(sizeof(struct switch_attr));
429 new->atype = arg->atype;
431 arg->prev->next = new;
433 arg->prev = *arg->head;
436 arg->head = &new->next;
438 if (tb[SWITCH_ATTR_OP_ID])
439 new->id = nla_get_u32(tb[SWITCH_ATTR_OP_ID]);
440 if (tb[SWITCH_ATTR_OP_TYPE])
441 new->type = nla_get_u32(tb[SWITCH_ATTR_OP_TYPE]);
442 if (tb[SWITCH_ATTR_OP_NAME])
443 new->name = strdup(nla_get_string(tb[SWITCH_ATTR_OP_NAME]));
444 if (tb[SWITCH_ATTR_OP_DESCRIPTION])
445 new->description = strdup(nla_get_string(tb[SWITCH_ATTR_OP_DESCRIPTION]));
452 swlib_scan(struct switch_dev *dev)
454 struct attrlist_arg arg;
456 if (dev->ops || dev->port_ops || dev->vlan_ops)
459 arg.atype = SWLIB_ATTR_GROUP_GLOBAL;
463 arg.head = &dev->ops;
464 swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg);
466 arg.atype = SWLIB_ATTR_GROUP_PORT;
468 arg.head = &dev->port_ops;
469 swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg);
471 arg.atype = SWLIB_ATTR_GROUP_VLAN;
473 arg.head = &dev->vlan_ops;
474 swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg);
479 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
480 enum swlib_attr_group atype, const char *name)
482 struct switch_attr *head;
488 case SWLIB_ATTR_GROUP_GLOBAL:
491 case SWLIB_ATTR_GROUP_PORT:
492 head = dev->port_ops;
494 case SWLIB_ATTR_GROUP_VLAN:
495 head = dev->vlan_ops;
499 if (!strcmp(name, head->name))
508 swlib_priv_free(void)
511 nl_cache_free(cache);
513 nl_socket_free(handle);
519 swlib_priv_init(void)
523 handle = nl_socket_alloc();
525 DPRINTF("Failed to create handle\n");
529 if (genl_connect(handle)) {
530 DPRINTF("Failed to connect to generic netlink\n");
534 ret = genl_ctrl_alloc_cache(handle, &cache);
536 DPRINTF("Failed to allocate netlink cache\n");
540 family = genl_ctrl_search_by_name(cache, "switch");
542 DPRINTF("Switch API not present\n");
552 struct swlib_scan_arg {
554 struct switch_dev *head;
555 struct switch_dev *ptr;
559 add_switch(struct nl_msg *msg, void *arg)
561 struct swlib_scan_arg *sa = arg;
562 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
563 struct switch_dev *dev;
566 if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
569 if (!tb[SWITCH_ATTR_DEV_NAME])
572 name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
573 if (sa->name && (strcmp(name, sa->name) != 0))
576 dev = swlib_alloc(sizeof(struct switch_dev));
580 dev->dev_name = strdup(name);
581 if (tb[SWITCH_ATTR_ID])
582 dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
583 if (tb[SWITCH_ATTR_NAME])
584 dev->name = strdup(nla_get_string(tb[SWITCH_ATTR_DEV_NAME]));
585 if (tb[SWITCH_ATTR_PORTS])
586 dev->ports = nla_get_u32(tb[SWITCH_ATTR_PORTS]);
587 if (tb[SWITCH_ATTR_VLANS])
588 dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
605 swlib_connect(const char *name)
607 struct swlib_scan_arg arg;
611 if (swlib_priv_init() < 0)
618 swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg);
627 swlib_free_attributes(struct switch_attr **head)
629 struct switch_attr *a = *head;
630 struct switch_attr *next;
641 swlib_free(struct switch_dev *dev)
643 swlib_free_attributes(&dev->ops);
644 swlib_free_attributes(&dev->port_ops);
645 swlib_free_attributes(&dev->vlan_ops);
653 swlib_free_all(struct switch_dev *dev)
655 struct switch_dev *p;