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 #define SWCONFIG_DEVNAME "switch%d"
36 #include "swconfig_leds.c"
38 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
39 MODULE_LICENSE("GPL");
41 static int swdev_id = 0;
42 static struct list_head swdevs;
43 static DEFINE_SPINLOCK(swdevs_lock);
44 struct swconfig_callback;
46 struct swconfig_callback
49 struct genlmsghdr *hdr;
50 struct genl_info *info;
53 /* callback for filling in the message data */
54 int (*fill)(struct swconfig_callback *cb, void *arg);
56 /* callback for closing the message before sending it */
57 int (*close)(struct swconfig_callback *cb, void *arg);
59 struct nlattr *nest[4];
66 swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
69 if (val->port_vlan >= dev->vlans)
72 if (!dev->ops->get_vlan_ports)
75 ret = dev->ops->get_vlan_ports(dev, val);
80 swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
82 struct switch_port *ports = val->value.ports;
83 const struct switch_dev_ops *ops = dev->ops;
86 if (val->port_vlan >= dev->vlans)
90 if (val->len > dev->ports)
93 if (!ops->set_vlan_ports)
96 for (i = 0; i < val->len; i++) {
97 if (ports[i].id >= dev->ports)
100 if (ops->set_port_pvid &&
101 !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
102 ops->set_port_pvid(dev, ports[i].id, val->port_vlan);
105 return ops->set_vlan_ports(dev, val);
109 swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
111 if (val->port_vlan >= dev->ports)
114 if (!dev->ops->set_port_pvid)
117 return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
121 swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
123 if (val->port_vlan >= dev->ports)
126 if (!dev->ops->get_port_pvid)
129 return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
133 swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
135 /* don't complain if not supported by the switch driver */
136 if (!dev->ops->apply_config)
139 return dev->ops->apply_config(dev);
143 swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
145 /* don't complain if not supported by the switch driver */
146 if (!dev->ops->reset_switch)
149 return dev->ops->reset_switch(dev);
152 enum global_defaults {
165 static struct switch_attr default_global[] = {
167 .type = SWITCH_TYPE_NOVAL,
169 .description = "Activate changes in the hardware",
170 .set = swconfig_apply_config,
173 .type = SWITCH_TYPE_NOVAL,
175 .description = "Reset the switch",
176 .set = swconfig_reset_switch,
180 static struct switch_attr default_port[] = {
182 .type = SWITCH_TYPE_INT,
184 .description = "Primary VLAN ID",
185 .set = swconfig_set_pvid,
186 .get = swconfig_get_pvid,
190 static struct switch_attr default_vlan[] = {
192 .type = SWITCH_TYPE_PORTS,
194 .description = "VLAN port mapping",
195 .set = swconfig_set_vlan_ports,
196 .get = swconfig_get_vlan_ports,
201 static void swconfig_defaults_init(struct switch_dev *dev)
203 const struct switch_dev_ops *ops = dev->ops;
209 if (ops->get_vlan_ports || ops->set_vlan_ports)
210 set_bit(VLAN_PORTS, &dev->def_vlan);
212 if (ops->get_port_pvid || ops->set_port_pvid)
213 set_bit(PORT_PVID, &dev->def_port);
215 /* always present, can be no-op */
216 set_bit(GLOBAL_APPLY, &dev->def_global);
217 set_bit(GLOBAL_RESET, &dev->def_global);
221 static struct genl_family switch_fam = {
222 .id = GENL_ID_GENERATE,
226 .maxattr = SWITCH_ATTR_MAX,
229 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
230 [SWITCH_ATTR_ID] = { .type = NLA_U32 },
231 [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
232 [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
233 [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
234 [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
235 [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
236 [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
237 [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
240 static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
241 [SWITCH_PORT_ID] = { .type = NLA_U32 },
242 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
248 spin_lock(&swdevs_lock);
252 swconfig_unlock(void)
254 spin_unlock(&swdevs_lock);
257 static struct switch_dev *
258 swconfig_get_dev(struct genl_info *info)
260 struct switch_dev *dev = NULL;
261 struct switch_dev *p;
264 if (!info->attrs[SWITCH_ATTR_ID])
267 id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
269 list_for_each_entry(p, &swdevs, dev_list) {
277 spin_lock(&dev->lock);
279 DPRINTF("device %d not found\n", id);
286 swconfig_put_dev(struct switch_dev *dev)
288 spin_unlock(&dev->lock);
292 swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
294 struct switch_attr *op = arg;
295 struct genl_info *info = cb->info;
296 struct sk_buff *msg = cb->msg;
297 int id = cb->args[0];
300 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
301 NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
305 NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, id);
306 NLA_PUT_U32(msg, SWITCH_ATTR_OP_TYPE, op->type);
307 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_NAME, op->name);
309 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_DESCRIPTION,
312 return genlmsg_end(msg, hdr);
314 genlmsg_cancel(msg, hdr);
318 /* spread multipart messages across multiple message buffers */
320 swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
322 struct genl_info *info = cb->info;
328 cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
333 if (!(cb->fill(cb, arg) < 0))
336 /* fill failed, check if this was already the second attempt */
340 /* try again in a new message, send the current one */
343 if (cb->close(cb, arg) < 0)
346 err = genlmsg_reply(cb->msg, info);
362 swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
364 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
365 const struct switch_attrlist *alist;
366 struct switch_dev *dev;
367 struct swconfig_callback cb;
372 struct switch_attr *def_list;
373 unsigned long *def_active;
376 dev = swconfig_get_dev(info);
381 case SWITCH_CMD_LIST_GLOBAL:
382 alist = &dev->ops->attr_global;
383 def_list = default_global;
384 def_active = &dev->def_global;
385 n_def = ARRAY_SIZE(default_global);
387 case SWITCH_CMD_LIST_VLAN:
388 alist = &dev->ops->attr_vlan;
389 def_list = default_vlan;
390 def_active = &dev->def_vlan;
391 n_def = ARRAY_SIZE(default_vlan);
393 case SWITCH_CMD_LIST_PORT:
394 alist = &dev->ops->attr_port;
395 def_list = default_port;
396 def_active = &dev->def_port;
397 n_def = ARRAY_SIZE(default_port);
404 memset(&cb, 0, sizeof(cb));
406 cb.fill = swconfig_dump_attr;
407 for (i = 0; i < alist->n_attr; i++) {
408 if (alist->attr[i].disabled)
411 err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
417 for (i = 0; i < n_def; i++) {
418 if (!test_bit(i, def_active))
420 cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
421 err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
425 swconfig_put_dev(dev);
430 return genlmsg_reply(cb.msg, info);
436 swconfig_put_dev(dev);
440 static const struct switch_attr *
441 swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
442 struct switch_val *val)
444 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
445 const struct switch_attrlist *alist;
446 const struct switch_attr *attr = NULL;
450 struct switch_attr *def_list;
451 unsigned long *def_active;
454 if (!info->attrs[SWITCH_ATTR_OP_ID])
458 case SWITCH_CMD_SET_GLOBAL:
459 case SWITCH_CMD_GET_GLOBAL:
460 alist = &dev->ops->attr_global;
461 def_list = default_global;
462 def_active = &dev->def_global;
463 n_def = ARRAY_SIZE(default_global);
465 case SWITCH_CMD_SET_VLAN:
466 case SWITCH_CMD_GET_VLAN:
467 alist = &dev->ops->attr_vlan;
468 def_list = default_vlan;
469 def_active = &dev->def_vlan;
470 n_def = ARRAY_SIZE(default_vlan);
471 if (!info->attrs[SWITCH_ATTR_OP_VLAN])
473 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
474 if (val->port_vlan >= dev->vlans)
477 case SWITCH_CMD_SET_PORT:
478 case SWITCH_CMD_GET_PORT:
479 alist = &dev->ops->attr_port;
480 def_list = default_port;
481 def_active = &dev->def_port;
482 n_def = ARRAY_SIZE(default_port);
483 if (!info->attrs[SWITCH_ATTR_OP_PORT])
485 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
486 if (val->port_vlan >= dev->ports)
497 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
498 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
499 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
500 if (attr_id >= n_def)
502 if (!test_bit(attr_id, def_active))
504 attr = &def_list[attr_id];
506 if (attr_id >= alist->n_attr)
508 attr = &alist->attr[attr_id];
516 DPRINTF("attribute lookup failed\n");
522 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
523 struct switch_val *val, int max)
529 nla_for_each_nested(nla, head, rem) {
530 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
531 struct switch_port *port = &val->value.ports[val->len];
536 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
540 if (!tb[SWITCH_PORT_ID])
543 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
544 if (tb[SWITCH_PORT_FLAG_TAGGED])
545 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
553 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
555 const struct switch_attr *attr;
556 struct switch_dev *dev;
557 struct switch_val val;
560 dev = swconfig_get_dev(info);
564 memset(&val, 0, sizeof(val));
565 attr = swconfig_lookup_attr(dev, info, &val);
566 if (!attr || !attr->set)
571 case SWITCH_TYPE_NOVAL:
573 case SWITCH_TYPE_INT:
574 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
577 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
579 case SWITCH_TYPE_STRING:
580 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
583 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
585 case SWITCH_TYPE_PORTS:
586 val.value.ports = dev->portbuf;
587 memset(dev->portbuf, 0,
588 sizeof(struct switch_port) * dev->ports);
590 /* TODO: implement multipart? */
591 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
592 err = swconfig_parse_ports(skb,
593 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
605 err = attr->set(dev, attr, &val);
607 swconfig_put_dev(dev);
612 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
615 nla_nest_end(cb->msg, cb->nest[0]);
620 swconfig_send_port(struct swconfig_callback *cb, void *arg)
622 const struct switch_port *port = arg;
623 struct nlattr *p = NULL;
626 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
631 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
635 NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
636 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
637 NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
639 nla_nest_end(cb->msg, p);
643 nla_nest_cancel(cb->msg, p);
645 nla_nest_cancel(cb->msg, cb->nest[0]);
650 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
651 const struct switch_val *val)
653 struct swconfig_callback cb;
657 if (!val->value.ports)
660 memset(&cb, 0, sizeof(cb));
664 cb.fill = swconfig_send_port;
665 cb.close = swconfig_close_portlist;
667 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
668 for (i = 0; i < val->len; i++) {
669 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
674 swconfig_close_portlist(&cb, NULL);
682 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
684 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
685 const struct switch_attr *attr;
686 struct switch_dev *dev;
687 struct sk_buff *msg = NULL;
688 struct switch_val val;
692 dev = swconfig_get_dev(info);
696 memset(&val, 0, sizeof(val));
697 attr = swconfig_lookup_attr(dev, info, &val);
698 if (!attr || !attr->get)
701 if (attr->type == SWITCH_TYPE_PORTS) {
702 val.value.ports = dev->portbuf;
703 memset(dev->portbuf, 0,
704 sizeof(struct switch_port) * dev->ports);
707 err = attr->get(dev, attr, &val);
711 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
715 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
718 goto nla_put_failure;
721 case SWITCH_TYPE_INT:
722 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
724 case SWITCH_TYPE_STRING:
725 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
727 case SWITCH_TYPE_PORTS:
728 err = swconfig_send_ports(&msg, info,
729 SWITCH_ATTR_OP_VALUE_PORTS, &val);
731 goto nla_put_failure;
734 DPRINTF("invalid type in attribute\n");
738 err = genlmsg_end(msg, hdr);
740 goto nla_put_failure;
742 swconfig_put_dev(dev);
743 return genlmsg_reply(msg, info);
749 swconfig_put_dev(dev);
756 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
757 const struct switch_dev *dev)
761 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
762 SWITCH_CMD_NEW_ATTR);
766 NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
767 NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
768 NLA_PUT_STRING(msg, SWITCH_ATTR_ALIAS, dev->alias);
769 NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
770 NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
771 NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
772 NLA_PUT_U32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port);
774 return genlmsg_end(msg, hdr);
776 genlmsg_cancel(msg, hdr);
780 static int swconfig_dump_switches(struct sk_buff *skb,
781 struct netlink_callback *cb)
783 struct switch_dev *dev;
784 int start = cb->args[0];
788 list_for_each_entry(dev, &swdevs, dev_list) {
791 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
792 cb->nlh->nlmsg_seq, NLM_F_MULTI,
803 swconfig_done(struct netlink_callback *cb)
808 static struct genl_ops swconfig_ops[] = {
810 .cmd = SWITCH_CMD_LIST_GLOBAL,
811 .doit = swconfig_list_attrs,
812 .policy = switch_policy,
815 .cmd = SWITCH_CMD_LIST_VLAN,
816 .doit = swconfig_list_attrs,
817 .policy = switch_policy,
820 .cmd = SWITCH_CMD_LIST_PORT,
821 .doit = swconfig_list_attrs,
822 .policy = switch_policy,
825 .cmd = SWITCH_CMD_GET_GLOBAL,
826 .doit = swconfig_get_attr,
827 .policy = switch_policy,
830 .cmd = SWITCH_CMD_GET_VLAN,
831 .doit = swconfig_get_attr,
832 .policy = switch_policy,
835 .cmd = SWITCH_CMD_GET_PORT,
836 .doit = swconfig_get_attr,
837 .policy = switch_policy,
840 .cmd = SWITCH_CMD_SET_GLOBAL,
841 .doit = swconfig_set_attr,
842 .policy = switch_policy,
845 .cmd = SWITCH_CMD_SET_VLAN,
846 .doit = swconfig_set_attr,
847 .policy = switch_policy,
850 .cmd = SWITCH_CMD_SET_PORT,
851 .doit = swconfig_set_attr,
852 .policy = switch_policy,
855 .cmd = SWITCH_CMD_GET_SWITCH,
856 .dumpit = swconfig_dump_switches,
857 .policy = switch_policy,
858 .done = swconfig_done,
863 register_switch(struct switch_dev *dev, struct net_device *netdev)
865 struct switch_dev *sdev;
866 const int max_switches = 8 * sizeof(unsigned long);
867 unsigned long in_use = 0;
871 INIT_LIST_HEAD(&dev->dev_list);
873 dev->netdev = netdev;
875 dev->alias = netdev->name;
879 if (dev->ports > 0) {
880 dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
885 swconfig_defaults_init(dev);
886 spin_lock_init(&dev->lock);
888 dev->id = ++swdev_id;
890 list_for_each_entry(sdev, &swdevs, dev_list) {
891 if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
893 if (i < 0 || i > max_switches)
898 i = find_first_zero_bit(&in_use, max_switches);
900 if (i == max_switches) {
905 /* fill device name */
906 snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
908 list_add(&dev->dev_list, &swdevs);
911 err = swconfig_create_led_trigger(dev);
917 EXPORT_SYMBOL_GPL(register_switch);
920 unregister_switch(struct switch_dev *dev)
922 swconfig_destroy_led_trigger(dev);
924 spin_lock(&dev->lock);
926 list_del(&dev->dev_list);
928 spin_unlock(&dev->lock);
930 EXPORT_SYMBOL_GPL(unregister_switch);
938 INIT_LIST_HEAD(&swdevs);
939 err = genl_register_family(&switch_fam);
943 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
944 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
952 genl_unregister_family(&switch_fam);
959 genl_unregister_family(&switch_fam);
962 module_init(swconfig_init);
963 module_exit(swconfig_exit);