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 DEFINE_SPINLOCK(swdevs_lock
);
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
, const struct switch_attr
*attr
, struct switch_val
*val
)
65 if (val
->port_vlan
>= dev
->vlans
)
68 if (!dev
->ops
->get_vlan_ports
)
71 ret
= dev
->ops
->get_vlan_ports(dev
, val
);
76 swconfig_set_vlan_ports(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
78 struct switch_port
*ports
= val
->value
.ports
;
79 const struct switch_dev_ops
*ops
= dev
->ops
;
82 if (val
->port_vlan
>= dev
->vlans
)
86 if (val
->len
> dev
->ports
)
89 if (!ops
->set_vlan_ports
)
92 for (i
= 0; i
< val
->len
; i
++) {
93 if (ports
[i
].id
>= dev
->ports
)
96 if (ops
->set_port_pvid
&&
97 !(ports
[i
].flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)))
98 ops
->set_port_pvid(dev
, ports
[i
].id
, val
->port_vlan
);
101 return ops
->set_vlan_ports(dev
, val
);
105 swconfig_set_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
107 if (val
->port_vlan
>= dev
->ports
)
110 if (!dev
->ops
->set_port_pvid
)
113 return dev
->ops
->set_port_pvid(dev
, val
->port_vlan
, val
->value
.i
);
117 swconfig_get_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
119 if (val
->port_vlan
>= dev
->ports
)
122 if (!dev
->ops
->get_port_pvid
)
125 return dev
->ops
->get_port_pvid(dev
, val
->port_vlan
, &val
->value
.i
);
129 swconfig_apply_config(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
131 /* don't complain if not supported by the switch driver */
132 if (!dev
->ops
->apply_config
)
135 return dev
->ops
->apply_config(dev
);
139 swconfig_reset_switch(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
141 /* don't complain if not supported by the switch driver */
142 if (!dev
->ops
->reset_switch
)
145 return dev
->ops
->reset_switch(dev
);
148 enum global_defaults
{
161 static struct switch_attr default_global
[] = {
163 .type
= SWITCH_TYPE_NOVAL
,
165 .description
= "Activate changes in the hardware",
166 .set
= swconfig_apply_config
,
169 .type
= SWITCH_TYPE_NOVAL
,
171 .description
= "Reset the switch",
172 .set
= swconfig_reset_switch
,
176 static struct switch_attr default_port
[] = {
178 .type
= SWITCH_TYPE_INT
,
180 .description
= "Primary VLAN ID",
181 .set
= swconfig_set_pvid
,
182 .get
= swconfig_get_pvid
,
186 static struct switch_attr default_vlan
[] = {
188 .type
= SWITCH_TYPE_PORTS
,
190 .description
= "VLAN port mapping",
191 .set
= swconfig_set_vlan_ports
,
192 .get
= swconfig_get_vlan_ports
,
197 static void swconfig_defaults_init(struct switch_dev
*dev
)
199 const struct switch_dev_ops
*ops
= dev
->ops
;
205 if (ops
->get_vlan_ports
|| ops
->set_vlan_ports
)
206 set_bit(VLAN_PORTS
, &dev
->def_vlan
);
208 if (ops
->get_port_pvid
|| ops
->set_port_pvid
)
209 set_bit(PORT_PVID
, &dev
->def_port
);
211 /* always present, can be no-op */
212 set_bit(GLOBAL_APPLY
, &dev
->def_global
);
213 set_bit(GLOBAL_RESET
, &dev
->def_global
);
217 static struct genl_family switch_fam
= {
218 .id
= GENL_ID_GENERATE
,
222 .maxattr
= SWITCH_ATTR_MAX
,
225 static const struct nla_policy switch_policy
[SWITCH_ATTR_MAX
+1] = {
226 [SWITCH_ATTR_ID
] = { .type
= NLA_U32
},
227 [SWITCH_ATTR_OP_ID
] = { .type
= NLA_U32
},
228 [SWITCH_ATTR_OP_PORT
] = { .type
= NLA_U32
},
229 [SWITCH_ATTR_OP_VLAN
] = { .type
= NLA_U32
},
230 [SWITCH_ATTR_OP_VALUE_INT
] = { .type
= NLA_U32
},
231 [SWITCH_ATTR_OP_VALUE_STR
] = { .type
= NLA_NUL_STRING
},
232 [SWITCH_ATTR_OP_VALUE_PORTS
] = { .type
= NLA_NESTED
},
233 [SWITCH_ATTR_TYPE
] = { .type
= NLA_U32
},
236 static const struct nla_policy port_policy
[SWITCH_PORT_ATTR_MAX
+1] = {
237 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
238 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
244 spin_lock(&swdevs_lock
);
248 swconfig_unlock(void)
250 spin_unlock(&swdevs_lock
);
253 static struct switch_dev
*
254 swconfig_get_dev(struct genl_info
*info
)
256 struct switch_dev
*dev
= NULL
;
257 struct switch_dev
*p
;
260 if (!info
->attrs
[SWITCH_ATTR_ID
])
263 id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_ID
]);
265 list_for_each_entry(p
, &swdevs
, dev_list
) {
273 spin_lock(&dev
->lock
);
275 DPRINTF("device %d not found\n", id
);
282 swconfig_put_dev(struct switch_dev
*dev
)
284 spin_unlock(&dev
->lock
);
288 swconfig_dump_attr(struct swconfig_callback
*cb
, void *arg
)
290 struct switch_attr
*op
= arg
;
291 struct genl_info
*info
= cb
->info
;
292 struct sk_buff
*msg
= cb
->msg
;
293 int id
= cb
->args
[0];
296 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
, &switch_fam
,
297 NLM_F_MULTI
, SWITCH_CMD_NEW_ATTR
);
301 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_ID
, id
);
302 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_TYPE
, op
->type
);
303 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_NAME
, op
->name
);
305 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_DESCRIPTION
,
308 return genlmsg_end(msg
, hdr
);
310 genlmsg_cancel(msg
, hdr
);
314 /* spread multipart messages across multiple message buffers */
316 swconfig_send_multipart(struct swconfig_callback
*cb
, void *arg
)
318 struct genl_info
*info
= cb
->info
;
324 cb
->msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
329 if (!(cb
->fill(cb
, arg
) < 0))
332 /* fill failed, check if this was already the second attempt */
336 /* try again in a new message, send the current one */
339 if (cb
->close(cb
, arg
) < 0)
342 err
= genlmsg_reply(cb
->msg
, info
);
358 swconfig_list_attrs(struct sk_buff
*skb
, struct genl_info
*info
)
360 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
361 const struct switch_attrlist
*alist
;
362 struct switch_dev
*dev
;
363 struct swconfig_callback cb
;
368 struct switch_attr
*def_list
;
369 unsigned long *def_active
;
372 dev
= swconfig_get_dev(info
);
377 case SWITCH_CMD_LIST_GLOBAL
:
378 alist
= &dev
->ops
->attr_global
;
379 def_list
= default_global
;
380 def_active
= &dev
->def_global
;
381 n_def
= ARRAY_SIZE(default_global
);
383 case SWITCH_CMD_LIST_VLAN
:
384 alist
= &dev
->ops
->attr_vlan
;
385 def_list
= default_vlan
;
386 def_active
= &dev
->def_vlan
;
387 n_def
= ARRAY_SIZE(default_vlan
);
389 case SWITCH_CMD_LIST_PORT
:
390 alist
= &dev
->ops
->attr_port
;
391 def_list
= default_port
;
392 def_active
= &dev
->def_port
;
393 n_def
= ARRAY_SIZE(default_port
);
400 memset(&cb
, 0, sizeof(cb
));
402 cb
.fill
= swconfig_dump_attr
;
403 for (i
= 0; i
< alist
->n_attr
; i
++) {
404 if (alist
->attr
[i
].disabled
)
407 err
= swconfig_send_multipart(&cb
, (void *) &alist
->attr
[i
]);
413 for (i
= 0; i
< n_def
; i
++) {
414 if (!test_bit(i
, def_active
))
416 cb
.args
[0] = SWITCH_ATTR_DEFAULTS_OFFSET
+ i
;
417 err
= swconfig_send_multipart(&cb
, (void *) &def_list
[i
]);
421 swconfig_put_dev(dev
);
426 return genlmsg_reply(cb
.msg
, info
);
432 swconfig_put_dev(dev
);
436 static const struct switch_attr
*
437 swconfig_lookup_attr(struct switch_dev
*dev
, struct genl_info
*info
,
438 struct switch_val
*val
)
440 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
441 const struct switch_attrlist
*alist
;
442 const struct switch_attr
*attr
= NULL
;
446 struct switch_attr
*def_list
;
447 unsigned long *def_active
;
450 if (!info
->attrs
[SWITCH_ATTR_OP_ID
])
454 case SWITCH_CMD_SET_GLOBAL
:
455 case SWITCH_CMD_GET_GLOBAL
:
456 alist
= &dev
->ops
->attr_global
;
457 def_list
= default_global
;
458 def_active
= &dev
->def_global
;
459 n_def
= ARRAY_SIZE(default_global
);
461 case SWITCH_CMD_SET_VLAN
:
462 case SWITCH_CMD_GET_VLAN
:
463 alist
= &dev
->ops
->attr_vlan
;
464 def_list
= default_vlan
;
465 def_active
= &dev
->def_vlan
;
466 n_def
= ARRAY_SIZE(default_vlan
);
467 if (!info
->attrs
[SWITCH_ATTR_OP_VLAN
])
469 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VLAN
]);
470 if (val
->port_vlan
>= dev
->vlans
)
473 case SWITCH_CMD_SET_PORT
:
474 case SWITCH_CMD_GET_PORT
:
475 alist
= &dev
->ops
->attr_port
;
476 def_list
= default_port
;
477 def_active
= &dev
->def_port
;
478 n_def
= ARRAY_SIZE(default_port
);
479 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
481 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
482 if (val
->port_vlan
>= dev
->ports
)
493 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
494 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
495 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
496 if (attr_id
>= n_def
)
498 if (!test_bit(attr_id
, def_active
))
500 attr
= &def_list
[attr_id
];
502 if (attr_id
>= alist
->n_attr
)
504 attr
= &alist
->attr
[attr_id
];
512 DPRINTF("attribute lookup failed\n");
518 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
519 struct switch_val
*val
, int max
)
525 nla_for_each_nested(nla
, head
, rem
) {
526 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
527 struct switch_port
*port
= &val
->value
.ports
[val
->len
];
532 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
536 if (!tb
[SWITCH_PORT_ID
])
539 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
540 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
541 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
549 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
551 const struct switch_attr
*attr
;
552 struct switch_dev
*dev
;
553 struct switch_val val
;
556 dev
= swconfig_get_dev(info
);
560 memset(&val
, 0, sizeof(val
));
561 attr
= swconfig_lookup_attr(dev
, info
, &val
);
562 if (!attr
|| !attr
->set
)
567 case SWITCH_TYPE_NOVAL
:
569 case SWITCH_TYPE_INT
:
570 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
573 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
575 case SWITCH_TYPE_STRING
:
576 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
579 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
581 case SWITCH_TYPE_PORTS
:
582 val
.value
.ports
= dev
->portbuf
;
583 memset(dev
->portbuf
, 0,
584 sizeof(struct switch_port
) * dev
->ports
);
586 /* TODO: implement multipart? */
587 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
588 err
= swconfig_parse_ports(skb
,
589 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
], &val
, dev
->ports
);
601 err
= attr
->set(dev
, attr
, &val
);
603 swconfig_put_dev(dev
);
608 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
611 nla_nest_end(cb
->msg
, cb
->nest
[0]);
616 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
618 const struct switch_port
*port
= arg
;
619 struct nlattr
*p
= NULL
;
622 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
627 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
631 NLA_PUT_U32(cb
->msg
, SWITCH_PORT_ID
, port
->id
);
632 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
633 NLA_PUT_FLAG(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
);
635 nla_nest_end(cb
->msg
, p
);
639 nla_nest_cancel(cb
->msg
, p
);
641 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
646 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
647 const struct switch_val
*val
)
649 struct swconfig_callback cb
;
653 if (!val
->value
.ports
)
656 memset(&cb
, 0, sizeof(cb
));
660 cb
.fill
= swconfig_send_port
;
661 cb
.close
= swconfig_close_portlist
;
663 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
664 for (i
= 0; i
< val
->len
; i
++) {
665 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
670 swconfig_close_portlist(&cb
, NULL
);
678 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
680 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
681 const struct switch_attr
*attr
;
682 struct switch_dev
*dev
;
683 struct sk_buff
*msg
= NULL
;
684 struct switch_val val
;
688 dev
= swconfig_get_dev(info
);
692 memset(&val
, 0, sizeof(val
));
693 attr
= swconfig_lookup_attr(dev
, info
, &val
);
694 if (!attr
|| !attr
->get
)
697 if (attr
->type
== SWITCH_TYPE_PORTS
) {
698 val
.value
.ports
= dev
->portbuf
;
699 memset(dev
->portbuf
, 0,
700 sizeof(struct switch_port
) * dev
->ports
);
703 err
= attr
->get(dev
, attr
, &val
);
707 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
711 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
, &switch_fam
,
714 goto nla_put_failure
;
717 case SWITCH_TYPE_INT
:
718 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
);
720 case SWITCH_TYPE_STRING
:
721 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
);
723 case SWITCH_TYPE_PORTS
:
724 err
= swconfig_send_ports(&msg
, info
,
725 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
727 goto nla_put_failure
;
730 DPRINTF("invalid type in attribute\n");
734 err
= genlmsg_end(msg
, hdr
);
736 goto nla_put_failure
;
738 swconfig_put_dev(dev
);
739 return genlmsg_reply(msg
, info
);
745 swconfig_put_dev(dev
);
752 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
753 const struct switch_dev
*dev
)
757 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
758 SWITCH_CMD_NEW_ATTR
);
762 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, dev
->id
);
763 NLA_PUT_STRING(msg
, SWITCH_ATTR_NAME
, dev
->name
);
764 NLA_PUT_STRING(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
);
765 NLA_PUT_U32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
);
766 NLA_PUT_U32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
);
767 NLA_PUT_U32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
);
769 return genlmsg_end(msg
, hdr
);
771 genlmsg_cancel(msg
, hdr
);
775 static int swconfig_dump_switches(struct sk_buff
*skb
,
776 struct netlink_callback
*cb
)
778 struct switch_dev
*dev
;
779 int start
= cb
->args
[0];
783 list_for_each_entry(dev
, &swdevs
, dev_list
) {
786 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).pid
,
787 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
798 swconfig_done(struct netlink_callback
*cb
)
803 static struct genl_ops swconfig_ops
[] = {
805 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
806 .doit
= swconfig_list_attrs
,
807 .policy
= switch_policy
,
810 .cmd
= SWITCH_CMD_LIST_VLAN
,
811 .doit
= swconfig_list_attrs
,
812 .policy
= switch_policy
,
815 .cmd
= SWITCH_CMD_LIST_PORT
,
816 .doit
= swconfig_list_attrs
,
817 .policy
= switch_policy
,
820 .cmd
= SWITCH_CMD_GET_GLOBAL
,
821 .doit
= swconfig_get_attr
,
822 .policy
= switch_policy
,
825 .cmd
= SWITCH_CMD_GET_VLAN
,
826 .doit
= swconfig_get_attr
,
827 .policy
= switch_policy
,
830 .cmd
= SWITCH_CMD_GET_PORT
,
831 .doit
= swconfig_get_attr
,
832 .policy
= switch_policy
,
835 .cmd
= SWITCH_CMD_SET_GLOBAL
,
836 .doit
= swconfig_set_attr
,
837 .policy
= switch_policy
,
840 .cmd
= SWITCH_CMD_SET_VLAN
,
841 .doit
= swconfig_set_attr
,
842 .policy
= switch_policy
,
845 .cmd
= SWITCH_CMD_SET_PORT
,
846 .doit
= swconfig_set_attr
,
847 .policy
= switch_policy
,
850 .cmd
= SWITCH_CMD_GET_SWITCH
,
851 .dumpit
= swconfig_dump_switches
,
852 .policy
= switch_policy
,
853 .done
= swconfig_done
,
858 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
860 INIT_LIST_HEAD(&dev
->dev_list
);
862 dev
->netdev
= netdev
;
864 dev
->devname
= netdev
->name
;
866 BUG_ON(!dev
->devname
);
868 if (dev
->ports
> 0) {
869 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) * dev
->ports
,
874 dev
->id
= ++swdev_id
;
875 swconfig_defaults_init(dev
);
876 spin_lock_init(&dev
->lock
);
878 list_add(&dev
->dev_list
, &swdevs
);
883 EXPORT_SYMBOL_GPL(register_switch
);
886 unregister_switch(struct switch_dev
*dev
)
889 spin_lock(&dev
->lock
);
891 list_del(&dev
->dev_list
);
893 spin_unlock(&dev
->lock
);
895 EXPORT_SYMBOL_GPL(unregister_switch
);
903 INIT_LIST_HEAD(&swdevs
);
904 err
= genl_register_family(&switch_fam
);
908 for (i
= 0; i
< ARRAY_SIZE(swconfig_ops
); i
++) {
909 err
= genl_register_ops(&switch_fam
, &swconfig_ops
[i
]);
917 genl_unregister_family(&switch_fam
);
924 genl_unregister_family(&switch_fam
);
927 module_init(swconfig_init
);
928 module_exit(swconfig_exit
);