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 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
37 MODULE_LICENSE("GPL");
39 static int swdev_id = 0;
40 static struct list_head swdevs;
41 static DEFINE_SPINLOCK(swdevs_lock);
42 struct swconfig_callback;
44 struct swconfig_callback
47 struct genlmsghdr *hdr;
48 struct genl_info *info;
51 /* callback for filling in the message data */
52 int (*fill)(struct swconfig_callback *cb, void *arg);
54 /* callback for closing the message before sending it */
55 int (*close)(struct swconfig_callback *cb, void *arg);
57 struct nlattr *nest[4];
64 swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
67 if (val->port_vlan >= dev->vlans)
70 if (!dev->ops->get_vlan_ports)
73 ret = dev->ops->get_vlan_ports(dev, val);
78 swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
80 struct switch_port *ports = val->value.ports;
81 const struct switch_dev_ops *ops = dev->ops;
84 if (val->port_vlan >= dev->vlans)
88 if (val->len > dev->ports)
91 if (!ops->set_vlan_ports)
94 for (i = 0; i < val->len; i++) {
95 if (ports[i].id >= dev->ports)
98 if (ops->set_port_pvid &&
99 !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
100 ops->set_port_pvid(dev, ports[i].id, val->port_vlan);
103 return ops->set_vlan_ports(dev, val);
107 swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
109 if (val->port_vlan >= dev->ports)
112 if (!dev->ops->set_port_pvid)
115 return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
119 swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
121 if (val->port_vlan >= dev->ports)
124 if (!dev->ops->get_port_pvid)
127 return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
131 swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
133 /* don't complain if not supported by the switch driver */
134 if (!dev->ops->apply_config)
137 return dev->ops->apply_config(dev);
141 swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val)
143 /* don't complain if not supported by the switch driver */
144 if (!dev->ops->reset_switch)
147 return dev->ops->reset_switch(dev);
150 enum global_defaults {
163 static struct switch_attr default_global[] = {
165 .type = SWITCH_TYPE_NOVAL,
167 .description = "Activate changes in the hardware",
168 .set = swconfig_apply_config,
171 .type = SWITCH_TYPE_NOVAL,
173 .description = "Reset the switch",
174 .set = swconfig_reset_switch,
178 static struct switch_attr default_port[] = {
180 .type = SWITCH_TYPE_INT,
182 .description = "Primary VLAN ID",
183 .set = swconfig_set_pvid,
184 .get = swconfig_get_pvid,
188 static struct switch_attr default_vlan[] = {
190 .type = SWITCH_TYPE_PORTS,
192 .description = "VLAN port mapping",
193 .set = swconfig_set_vlan_ports,
194 .get = swconfig_get_vlan_ports,
199 static void swconfig_defaults_init(struct switch_dev *dev)
201 const struct switch_dev_ops *ops = dev->ops;
207 if (ops->get_vlan_ports || ops->set_vlan_ports)
208 set_bit(VLAN_PORTS, &dev->def_vlan);
210 if (ops->get_port_pvid || ops->set_port_pvid)
211 set_bit(PORT_PVID, &dev->def_port);
213 /* always present, can be no-op */
214 set_bit(GLOBAL_APPLY, &dev->def_global);
215 set_bit(GLOBAL_RESET, &dev->def_global);
219 static struct genl_family switch_fam = {
220 .id = GENL_ID_GENERATE,
224 .maxattr = SWITCH_ATTR_MAX,
227 static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
228 [SWITCH_ATTR_ID] = { .type = NLA_U32 },
229 [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
230 [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
231 [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
232 [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
233 [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
234 [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
235 [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
238 static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
239 [SWITCH_PORT_ID] = { .type = NLA_U32 },
240 [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
246 spin_lock(&swdevs_lock);
250 swconfig_unlock(void)
252 spin_unlock(&swdevs_lock);
255 static struct switch_dev *
256 swconfig_get_dev(struct genl_info *info)
258 struct switch_dev *dev = NULL;
259 struct switch_dev *p;
262 if (!info->attrs[SWITCH_ATTR_ID])
265 id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
267 list_for_each_entry(p, &swdevs, dev_list) {
275 spin_lock(&dev->lock);
277 DPRINTF("device %d not found\n", id);
284 swconfig_put_dev(struct switch_dev *dev)
286 spin_unlock(&dev->lock);
290 swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
292 struct switch_attr *op = arg;
293 struct genl_info *info = cb->info;
294 struct sk_buff *msg = cb->msg;
295 int id = cb->args[0];
298 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
299 NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
303 NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, id);
304 NLA_PUT_U32(msg, SWITCH_ATTR_OP_TYPE, op->type);
305 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_NAME, op->name);
307 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_DESCRIPTION,
310 return genlmsg_end(msg, hdr);
312 genlmsg_cancel(msg, hdr);
316 /* spread multipart messages across multiple message buffers */
318 swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
320 struct genl_info *info = cb->info;
326 cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
331 if (!(cb->fill(cb, arg) < 0))
334 /* fill failed, check if this was already the second attempt */
338 /* try again in a new message, send the current one */
341 if (cb->close(cb, arg) < 0)
344 err = genlmsg_reply(cb->msg, info);
360 swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
362 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
363 const struct switch_attrlist *alist;
364 struct switch_dev *dev;
365 struct swconfig_callback cb;
370 struct switch_attr *def_list;
371 unsigned long *def_active;
374 dev = swconfig_get_dev(info);
379 case SWITCH_CMD_LIST_GLOBAL:
380 alist = &dev->ops->attr_global;
381 def_list = default_global;
382 def_active = &dev->def_global;
383 n_def = ARRAY_SIZE(default_global);
385 case SWITCH_CMD_LIST_VLAN:
386 alist = &dev->ops->attr_vlan;
387 def_list = default_vlan;
388 def_active = &dev->def_vlan;
389 n_def = ARRAY_SIZE(default_vlan);
391 case SWITCH_CMD_LIST_PORT:
392 alist = &dev->ops->attr_port;
393 def_list = default_port;
394 def_active = &dev->def_port;
395 n_def = ARRAY_SIZE(default_port);
402 memset(&cb, 0, sizeof(cb));
404 cb.fill = swconfig_dump_attr;
405 for (i = 0; i < alist->n_attr; i++) {
406 if (alist->attr[i].disabled)
409 err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
415 for (i = 0; i < n_def; i++) {
416 if (!test_bit(i, def_active))
418 cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
419 err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
423 swconfig_put_dev(dev);
428 return genlmsg_reply(cb.msg, info);
434 swconfig_put_dev(dev);
438 static const struct switch_attr *
439 swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
440 struct switch_val *val)
442 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
443 const struct switch_attrlist *alist;
444 const struct switch_attr *attr = NULL;
448 struct switch_attr *def_list;
449 unsigned long *def_active;
452 if (!info->attrs[SWITCH_ATTR_OP_ID])
456 case SWITCH_CMD_SET_GLOBAL:
457 case SWITCH_CMD_GET_GLOBAL:
458 alist = &dev->ops->attr_global;
459 def_list = default_global;
460 def_active = &dev->def_global;
461 n_def = ARRAY_SIZE(default_global);
463 case SWITCH_CMD_SET_VLAN:
464 case SWITCH_CMD_GET_VLAN:
465 alist = &dev->ops->attr_vlan;
466 def_list = default_vlan;
467 def_active = &dev->def_vlan;
468 n_def = ARRAY_SIZE(default_vlan);
469 if (!info->attrs[SWITCH_ATTR_OP_VLAN])
471 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
472 if (val->port_vlan >= dev->vlans)
475 case SWITCH_CMD_SET_PORT:
476 case SWITCH_CMD_GET_PORT:
477 alist = &dev->ops->attr_port;
478 def_list = default_port;
479 def_active = &dev->def_port;
480 n_def = ARRAY_SIZE(default_port);
481 if (!info->attrs[SWITCH_ATTR_OP_PORT])
483 val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
484 if (val->port_vlan >= dev->ports)
495 attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
496 if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
497 attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
498 if (attr_id >= n_def)
500 if (!test_bit(attr_id, def_active))
502 attr = &def_list[attr_id];
504 if (attr_id >= alist->n_attr)
506 attr = &alist->attr[attr_id];
514 DPRINTF("attribute lookup failed\n");
520 swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
521 struct switch_val *val, int max)
527 nla_for_each_nested(nla, head, rem) {
528 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
529 struct switch_port *port = &val->value.ports[val->len];
534 if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
538 if (!tb[SWITCH_PORT_ID])
541 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
542 if (tb[SWITCH_PORT_FLAG_TAGGED])
543 port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
551 swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
553 const struct switch_attr *attr;
554 struct switch_dev *dev;
555 struct switch_val val;
558 dev = swconfig_get_dev(info);
562 memset(&val, 0, sizeof(val));
563 attr = swconfig_lookup_attr(dev, info, &val);
564 if (!attr || !attr->set)
569 case SWITCH_TYPE_NOVAL:
571 case SWITCH_TYPE_INT:
572 if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
575 nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
577 case SWITCH_TYPE_STRING:
578 if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
581 nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
583 case SWITCH_TYPE_PORTS:
584 val.value.ports = dev->portbuf;
585 memset(dev->portbuf, 0,
586 sizeof(struct switch_port) * dev->ports);
588 /* TODO: implement multipart? */
589 if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
590 err = swconfig_parse_ports(skb,
591 info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], &val, dev->ports);
603 err = attr->set(dev, attr, &val);
605 swconfig_put_dev(dev);
610 swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
613 nla_nest_end(cb->msg, cb->nest[0]);
618 swconfig_send_port(struct swconfig_callback *cb, void *arg)
620 const struct switch_port *port = arg;
621 struct nlattr *p = NULL;
624 cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
629 p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
633 NLA_PUT_U32(cb->msg, SWITCH_PORT_ID, port->id);
634 if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
635 NLA_PUT_FLAG(cb->msg, SWITCH_PORT_FLAG_TAGGED);
637 nla_nest_end(cb->msg, p);
641 nla_nest_cancel(cb->msg, p);
643 nla_nest_cancel(cb->msg, cb->nest[0]);
648 swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
649 const struct switch_val *val)
651 struct swconfig_callback cb;
655 if (!val->value.ports)
658 memset(&cb, 0, sizeof(cb));
662 cb.fill = swconfig_send_port;
663 cb.close = swconfig_close_portlist;
665 cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
666 for (i = 0; i < val->len; i++) {
667 err = swconfig_send_multipart(&cb, &val->value.ports[i]);
672 swconfig_close_portlist(&cb, NULL);
680 swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
682 struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
683 const struct switch_attr *attr;
684 struct switch_dev *dev;
685 struct sk_buff *msg = NULL;
686 struct switch_val val;
690 dev = swconfig_get_dev(info);
694 memset(&val, 0, sizeof(val));
695 attr = swconfig_lookup_attr(dev, info, &val);
696 if (!attr || !attr->get)
699 if (attr->type == SWITCH_TYPE_PORTS) {
700 val.value.ports = dev->portbuf;
701 memset(dev->portbuf, 0,
702 sizeof(struct switch_port) * dev->ports);
705 err = attr->get(dev, attr, &val);
709 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
713 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq, &switch_fam,
716 goto nla_put_failure;
719 case SWITCH_TYPE_INT:
720 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i);
722 case SWITCH_TYPE_STRING:
723 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s);
725 case SWITCH_TYPE_PORTS:
726 err = swconfig_send_ports(&msg, info,
727 SWITCH_ATTR_OP_VALUE_PORTS, &val);
729 goto nla_put_failure;
732 DPRINTF("invalid type in attribute\n");
736 err = genlmsg_end(msg, hdr);
738 goto nla_put_failure;
740 swconfig_put_dev(dev);
741 return genlmsg_reply(msg, info);
747 swconfig_put_dev(dev);
754 swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
755 const struct switch_dev *dev)
759 hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
760 SWITCH_CMD_NEW_ATTR);
764 NLA_PUT_U32(msg, SWITCH_ATTR_ID, dev->id);
765 NLA_PUT_STRING(msg, SWITCH_ATTR_DEV_NAME, dev->devname);
766 NLA_PUT_STRING(msg, SWITCH_ATTR_ALIAS, dev->alias);
767 NLA_PUT_STRING(msg, SWITCH_ATTR_NAME, dev->name);
768 NLA_PUT_U32(msg, SWITCH_ATTR_VLANS, dev->vlans);
769 NLA_PUT_U32(msg, SWITCH_ATTR_PORTS, dev->ports);
770 NLA_PUT_U32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port);
772 return genlmsg_end(msg, hdr);
774 genlmsg_cancel(msg, hdr);
778 static int swconfig_dump_switches(struct sk_buff *skb,
779 struct netlink_callback *cb)
781 struct switch_dev *dev;
782 int start = cb->args[0];
786 list_for_each_entry(dev, &swdevs, dev_list) {
789 if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).pid,
790 cb->nlh->nlmsg_seq, NLM_F_MULTI,
801 swconfig_done(struct netlink_callback *cb)
806 static struct genl_ops swconfig_ops[] = {
808 .cmd = SWITCH_CMD_LIST_GLOBAL,
809 .doit = swconfig_list_attrs,
810 .policy = switch_policy,
813 .cmd = SWITCH_CMD_LIST_VLAN,
814 .doit = swconfig_list_attrs,
815 .policy = switch_policy,
818 .cmd = SWITCH_CMD_LIST_PORT,
819 .doit = swconfig_list_attrs,
820 .policy = switch_policy,
823 .cmd = SWITCH_CMD_GET_GLOBAL,
824 .doit = swconfig_get_attr,
825 .policy = switch_policy,
828 .cmd = SWITCH_CMD_GET_VLAN,
829 .doit = swconfig_get_attr,
830 .policy = switch_policy,
833 .cmd = SWITCH_CMD_GET_PORT,
834 .doit = swconfig_get_attr,
835 .policy = switch_policy,
838 .cmd = SWITCH_CMD_SET_GLOBAL,
839 .doit = swconfig_set_attr,
840 .policy = switch_policy,
843 .cmd = SWITCH_CMD_SET_VLAN,
844 .doit = swconfig_set_attr,
845 .policy = switch_policy,
848 .cmd = SWITCH_CMD_SET_PORT,
849 .doit = swconfig_set_attr,
850 .policy = switch_policy,
853 .cmd = SWITCH_CMD_GET_SWITCH,
854 .dumpit = swconfig_dump_switches,
855 .policy = switch_policy,
856 .done = swconfig_done,
861 register_switch(struct switch_dev *dev, struct net_device *netdev)
863 struct switch_dev *sdev;
864 const int max_switches = 8 * sizeof(unsigned long);
865 unsigned long in_use = 0;
868 INIT_LIST_HEAD(&dev->dev_list);
870 dev->netdev = netdev;
872 dev->alias = netdev->name;
876 if (dev->ports > 0) {
877 dev->portbuf = kzalloc(sizeof(struct switch_port) * dev->ports,
882 swconfig_defaults_init(dev);
883 spin_lock_init(&dev->lock);
885 dev->id = ++swdev_id;
887 list_for_each_entry(sdev, &swdevs, dev_list) {
888 if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
890 if (i < 0 || i > max_switches)
895 i = find_first_zero_bit(&in_use, max_switches);
897 if (i == max_switches) {
902 /* fill device name */
903 snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
905 list_add(&dev->dev_list, &swdevs);
910 EXPORT_SYMBOL_GPL(register_switch);
913 unregister_switch(struct switch_dev *dev)
916 spin_lock(&dev->lock);
918 list_del(&dev->dev_list);
920 spin_unlock(&dev->lock);
922 EXPORT_SYMBOL_GPL(unregister_switch);
930 INIT_LIST_HEAD(&swdevs);
931 err = genl_register_family(&switch_fam);
935 for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
936 err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
944 genl_unregister_family(&switch_fam);
951 genl_unregister_family(&switch_fam);
954 module_init(swconfig_init);
955 module_exit(swconfig_exit);