2 * swconfig.c: Switch configuration API
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 General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
22 #include <linux/if_ether.h>
23 #include <linux/capability.h>
24 #include <linux/skbuff.h>
25 #include <linux/switch.h>
29 #define DPRINTF(format, ...) printk("%s: " format, __func__, ##__VA_ARGS__)
31 #define DPRINTF(...) do {} while(0)
34 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
35 MODULE_LICENSE("GPL");
37 static int swdev_id = 0;
38 static struct list_head swdevs;
39 static spinlock_t swdevs_lock = SPIN_LOCK_UNLOCKED;
40 struct swconfig_callback;
42 struct swconfig_callback
45 struct genlmsghdr *hdr;
46 struct genl_info *info;
49 /* callback for filling in the message data */
50 int (*fill)(struct swconfig_callback *cb, void *arg);
52 /* callback for closing the message before sending it */
53 int (*close)(struct swconfig_callback *cb, void *arg);
55 struct nlattr *nest[4];
62 swconfig_get_vlan_ports(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
65 if (val->port_vlan >= dev->vlans)
68 if (!dev->get_vlan_ports)
71 ret = dev->get_vlan_ports(dev, val);
76 swconfig_set_vlan_ports(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
80 if (val->port_vlan >= dev->vlans)
84 if (val->len > dev->ports)
87 for (i = 0; i < val->len; i++) {
88 if (val->value.ports[i].id >= dev->ports)
92 if (!dev->set_vlan_ports)
95 return dev->set_vlan_ports(dev, val);
99 swconfig_apply_config(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
101 /* don't complain if not supported by the switch driver */
102 if (!dev->apply_config)
105 return dev->apply_config(dev);
109 enum global_defaults {
121 static struct switch_attr default_global[] = {
123 .type = SWITCH_TYPE_NOVAL,
125 .description = "Activate changes in the hardware",
126 .set = swconfig_apply_config,
130 static struct switch_attr default_port[] = {
132 .type = SWITCH_TYPE_INT,
134 .description = "Current link speed",
138 static struct switch_attr default_vlan[] = {
140 .type = SWITCH_TYPE_PORTS,
142 .description = "VLAN port mapping",
143 .set = swconfig_set_vlan_ports,
144 .get = swconfig_get_vlan_ports,
149 static void swconfig_defaults_init(struct switch_dev *dev)
155 if (dev->get_vlan_ports || dev->set_vlan_ports)
156 set_bit(VLAN_PORTS, &dev->def_vlan);
158 /* always present, can be no-op */
159 set_bit(GLOBAL_APPLY, &dev->def_global);
163 static struct genl_family switch_fam = {
164 .id = GENL_ID_GENERATE,
168 .maxattr = SWITCH_ATTR_MAX,
171 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
172 [SWITCH_ATTR_ID] = { .type = NLA_U32 },
173 [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
174 [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
175 [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
176 [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
177 [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
178 [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
179 [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
182 static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
183 [SWITCH_PORT_ID] = { .type = NLA_U32 },
184 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
190 spin_lock(&swdevs_lock);
194 swconfig_unlock(void)
196 spin_unlock(&swdevs_lock);
199 static struct switch_dev *
200 swconfig_get_dev(struct genl_info *info)
202 struct switch_dev *dev = NULL;
203 struct switch_dev *p;
206 if (!info->attrs[SWITCH_ATTR_ID])
209 id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
211 list_for_each_entry(p, &swdevs, dev_list) {
219 spin_lock(&dev->lock);
221 DPRINTF("device %d not found\n", id);
228 swconfig_put_dev(struct switch_dev *dev)
230 spin_unlock(&dev->lock);
234 swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
236 struct switch_attr *op = arg;
237 struct genl_info *info = cb->info;
238 struct sk_buff *msg = cb->msg;
239 int id = cb->args[0];
242 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
243 NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
247 NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, id);
248 NLA_PUT_U32(msg, SWITCH_ATTR_OP_TYPE, op->type);
249 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_NAME, op->name);
251 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_DESCRIPTION,
254 return genlmsg_end(msg, hdr);
256 genlmsg_cancel(msg, hdr);
260 /* spread multipart messages across multiple message buffers */
262 swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
264 struct genl_info *info = cb->info;
270 cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
275 if (!(cb->fill(cb, arg) < 0))
278 /* fill failed, check if this was already the second attempt */
282 /* try again in a new message, send the current one */
285 if (cb->close(cb, arg) < 0)
288 err = genlmsg_unicast(cb->msg, info->snd_pid);
304 swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
306 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
307 const struct switch_attrlist *alist;
308 struct switch_dev *dev;
309 struct swconfig_callback cb;
314 struct switch_attr *def_list;
315 unsigned long *def_active;
318 dev = swconfig_get_dev(info);
323 case SWITCH_CMD_LIST_GLOBAL:
324 alist = &dev->attr_global;
325 def_list = default_global;
326 def_active = &dev->def_global;
327 n_def = ARRAY_SIZE(default_global);
329 case SWITCH_CMD_LIST_VLAN:
330 alist = &dev->attr_vlan;
331 def_list = default_vlan;
332 def_active = &dev->def_vlan;
333 n_def = ARRAY_SIZE(default_vlan);
335 case SWITCH_CMD_LIST_PORT:
336 alist = &dev->attr_port;
337 def_list = default_port;
338 def_active = &dev->def_port;
339 n_def = ARRAY_SIZE(default_port);
346 memset(&cb, 0, sizeof(cb));
348 cb.fill = swconfig_dump_attr;
349 for (i = 0; i < alist->n_attr; i++) {
350 if (alist->attr[i].disabled)
353 err = swconfig_send_multipart(&cb, &alist->attr[i]);
359 for (i = 0; i < n_def; i++) {
360 if (!test_bit(i, def_active))
362 cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
363 err = swconfig_send_multipart(&cb, &def_list[i]);
367 swconfig_put_dev(dev);
372 return genlmsg_unicast(cb.msg, info->snd_pid);
378 swconfig_put_dev(dev);
382 static struct switch_attr *
383 swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
384 struct switch_val *val)
386 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
387 const struct switch_attrlist *alist;
388 struct switch_attr *attr = NULL;
392 struct switch_attr *def_list;
393 unsigned long *def_active;
396 if (!info->attrs[SWITCH_ATTR_OP_ID])
400 case SWITCH_CMD_SET_GLOBAL:
401 case SWITCH_CMD_GET_GLOBAL:
402 alist = &dev->attr_global;
403 def_list = default_global;
404 def_active = &dev->def_global;
405 n_def = ARRAY_SIZE(default_global);
407 case SWITCH_CMD_SET_VLAN:
408 case SWITCH_CMD_GET_VLAN:
409 alist = &dev->attr_vlan;
410 def_list = default_vlan;
411 def_active = &dev->def_vlan;
412 n_def = ARRAY_SIZE(default_vlan);
413 if (!info->attrs[SWITCH_ATTR_OP_VLAN])
415 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
417 case SWITCH_CMD_SET_PORT:
418 case SWITCH_CMD_GET_PORT:
419 alist = &dev->attr_port;
420 def_list = default_port;
421 def_active = &dev->def_port;
422 n_def = ARRAY_SIZE(default_port);
423 if (!info->attrs[SWITCH_ATTR_OP_PORT])
425 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
435 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
436 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
437 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
438 if (attr_id >= n_def)
440 if (!test_bit(attr_id, def_active))
442 attr = &def_list[attr_id];
444 if (attr_id >= alist->n_attr)
446 attr = &alist->attr[attr_id];
454 DPRINTF("attribute lookup failed\n");
460 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
461 struct switch_val *val, int max)
467 nla_for_each_nested(nla, head, rem) {
468 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
469 struct switch_port *port = &val->value.ports[val->len];
474 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
478 if (!tb[SWITCH_PORT_ID])
481 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
482 if (tb[SWITCH_PORT_FLAG_TAGGED])
483 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
491 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
493 struct switch_attr *attr;
494 struct switch_dev *dev;
495 struct switch_val val;
498 dev = swconfig_get_dev(info);
502 memset(&val, 0, sizeof(val));
503 attr = swconfig_lookup_attr(dev, info, &val);
504 if (!attr || !attr->set)
509 case SWITCH_TYPE_NOVAL:
511 case SWITCH_TYPE_INT:
512 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
515 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
517 case SWITCH_TYPE_STRING:
518 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
521 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
523 case SWITCH_TYPE_PORTS:
524 val.value.ports = dev->portbuf;
525 memset(dev->portbuf, 0,
526 sizeof(struct switch_port) * dev->ports);
528 /* TODO: implement multipart? */
529 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
530 err = swconfig_parse_ports(skb,
531 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
543 err = attr->set(dev, attr, &val);
545 swconfig_put_dev(dev);
550 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
553 nla_nest_end(cb->msg, cb->nest[0]);
558 swconfig_send_port(struct swconfig_callback *cb, void *arg)
560 const struct switch_port *port = arg;
561 struct nlattr *p = NULL;
564 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
569 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
573 NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
574 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
575 NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
577 nla_nest_end(cb->msg, p);
581 nla_nest_cancel(cb->msg, p);
583 nla_nest_cancel(cb->msg, cb->nest[0]);
588 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
589 const struct switch_val *val)
591 struct swconfig_callback cb;
595 if (!val->value.ports)
598 memset(&cb, 0, sizeof(cb));
602 cb.fill = swconfig_send_port;
603 cb.close = swconfig_close_portlist;
605 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
606 for (i = 0; i < val->len; i++) {
607 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
612 swconfig_close_portlist(&cb, NULL);
620 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
622 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
623 struct switch_attr *attr;
624 struct switch_dev *dev;
625 struct sk_buff *msg = NULL;
626 struct switch_val val;
630 dev = swconfig_get_dev(info);
634 memset(&val, 0, sizeof(val));
635 attr = swconfig_lookup_attr(dev, info, &val);
636 if (!attr || !attr->get)
639 if (attr->type == SWITCH_TYPE_PORTS) {
640 val.value.ports = dev->portbuf;
641 memset(dev->portbuf, 0,
642 sizeof(struct switch_port) * dev->ports);
645 err = attr->get(dev, attr, &val);
649 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
653 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
656 goto nla_put_failure;
659 case SWITCH_TYPE_INT:
660 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
662 case SWITCH_TYPE_STRING:
663 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
665 case SWITCH_TYPE_PORTS:
666 err = swconfig_send_ports(&msg, info,
667 SWITCH_ATTR_OP_VALUE_PORTS, &val);
669 goto nla_put_failure;
672 DPRINTF("invalid type in attribute\n");
676 err = genlmsg_end(msg, hdr);
678 goto nla_put_failure;
680 swconfig_put_dev(dev);
681 return genlmsg_unicast(msg, info->snd_pid);
687 swconfig_put_dev(dev);
695 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
696 const struct switch_dev *dev)
700 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
701 SWITCH_CMD_NEW_ATTR);
705 NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
706 NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
707 NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
708 NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
709 NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
711 return genlmsg_end(msg, hdr);
713 genlmsg_cancel(msg, hdr);
717 static int swconfig_dump_switches(struct sk_buff *skb,
718 struct netlink_callback *cb)
720 struct switch_dev *dev;
721 int start = cb->args[0];
725 list_for_each_entry(dev, &swdevs, dev_list) {
728 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
729 cb->nlh->nlmsg_seq, NLM_F_MULTI,
740 swconfig_done(struct netlink_callback *cb)
745 static struct genl_ops swconfig_ops[] = {
747 .cmd = SWITCH_CMD_LIST_GLOBAL,
748 .doit = swconfig_list_attrs,
749 .policy = switch_policy,
752 .cmd = SWITCH_CMD_LIST_VLAN,
753 .doit = swconfig_list_attrs,
754 .policy = switch_policy,
757 .cmd = SWITCH_CMD_LIST_PORT,
758 .doit = swconfig_list_attrs,
759 .policy = switch_policy,
762 .cmd = SWITCH_CMD_GET_GLOBAL,
763 .doit = swconfig_get_attr,
764 .policy = switch_policy,
767 .cmd = SWITCH_CMD_GET_VLAN,
768 .doit = swconfig_get_attr,
769 .policy = switch_policy,
772 .cmd = SWITCH_CMD_GET_PORT,
773 .doit = swconfig_get_attr,
774 .policy = switch_policy,
777 .cmd = SWITCH_CMD_SET_GLOBAL,
778 .doit = swconfig_set_attr,
779 .policy = switch_policy,
782 .cmd = SWITCH_CMD_SET_VLAN,
783 .doit = swconfig_set_attr,
784 .policy = switch_policy,
787 .cmd = SWITCH_CMD_SET_PORT,
788 .doit = swconfig_set_attr,
789 .policy = switch_policy,
792 .cmd = SWITCH_CMD_GET_SWITCH,
793 .dumpit = swconfig_dump_switches,
794 .policy = switch_policy,
795 .done = swconfig_done,
800 register_switch(struct switch_dev *dev, struct net_device *netdev)
802 INIT_LIST_HEAD(&dev->dev_list);
804 dev->netdev = netdev;
806 dev->devname = netdev->name;
808 BUG_ON(!dev->devname);
810 if (dev->ports > 0) {
811 dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
816 dev->id = ++swdev_id;
817 swconfig_defaults_init(dev);
818 spin_lock_init(&dev->lock);
820 list_add(&dev->dev_list, &swdevs);
825 EXPORT_SYMBOL_GPL(register_switch);
828 unregister_switch(struct switch_dev *dev)
831 spin_lock(&dev->lock);
833 list_del(&dev->dev_list);
836 EXPORT_SYMBOL_GPL(unregister_switch);
844 INIT_LIST_HEAD(&swdevs);
845 err = genl_register_family(&switch_fam);
849 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
850 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
858 genl_unregister_family(&switch_fam);
865 genl_unregister_family(&switch_fam);
868 module_init(swconfig_init);
869 module_exit(swconfig_exit);