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
, const 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
, const struct switch_attr
*attr
, struct switch_val
*val
)
78 struct switch_port
*ports
= val
->value
.ports
;
81 if (val
->port_vlan
>= dev
->vlans
)
85 if (val
->len
> dev
->ports
)
88 if (!dev
->set_vlan_ports
)
91 for (i
= 0; i
< val
->len
; i
++) {
92 if (ports
[i
].id
>= dev
->ports
)
95 if (dev
->set_port_pvid
&& !(ports
[i
].flags
& SWITCH_PORT_FLAG_TAGGED
))
96 dev
->set_port_pvid(dev
, ports
[i
].id
, val
->port_vlan
);
99 return dev
->set_vlan_ports(dev
, val
);
103 swconfig_set_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
105 if (val
->port_vlan
>= dev
->ports
)
108 if (!dev
->set_port_pvid
)
111 return dev
->set_port_pvid(dev
, val
->port_vlan
, val
->value
.i
);
115 swconfig_get_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
117 if (val
->port_vlan
>= dev
->ports
)
120 if (!dev
->get_port_pvid
)
123 return dev
->get_port_pvid(dev
, val
->port_vlan
, &val
->value
.i
);
127 swconfig_apply_config(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
129 /* don't complain if not supported by the switch driver */
130 if (!dev
->apply_config
)
133 return dev
->apply_config(dev
);
137 swconfig_reset_switch(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
139 /* don't complain if not supported by the switch driver */
140 if (!dev
->reset_switch
)
143 return dev
->reset_switch(dev
);
146 enum global_defaults
{
159 static struct switch_attr default_global
[] = {
161 .type
= SWITCH_TYPE_NOVAL
,
163 .description
= "Activate changes in the hardware",
164 .set
= swconfig_apply_config
,
167 .type
= SWITCH_TYPE_NOVAL
,
169 .description
= "Reset the switch",
170 .set
= swconfig_reset_switch
,
174 static struct switch_attr default_port
[] = {
176 .type
= SWITCH_TYPE_INT
,
178 .description
= "Primary VLAN ID",
179 .set
= swconfig_set_pvid
,
180 .get
= swconfig_get_pvid
,
184 static struct switch_attr default_vlan
[] = {
186 .type
= SWITCH_TYPE_PORTS
,
188 .description
= "VLAN port mapping",
189 .set
= swconfig_set_vlan_ports
,
190 .get
= swconfig_get_vlan_ports
,
195 static void swconfig_defaults_init(struct switch_dev
*dev
)
201 if (dev
->get_vlan_ports
|| dev
->set_vlan_ports
)
202 set_bit(VLAN_PORTS
, &dev
->def_vlan
);
204 if (dev
->get_port_pvid
|| dev
->set_port_pvid
)
205 set_bit(PORT_PVID
, &dev
->def_port
);
207 /* always present, can be no-op */
208 set_bit(GLOBAL_APPLY
, &dev
->def_global
);
209 set_bit(GLOBAL_RESET
, &dev
->def_global
);
213 static struct genl_family switch_fam
= {
214 .id
= GENL_ID_GENERATE
,
218 .maxattr
= SWITCH_ATTR_MAX
,
221 static const struct nla_policy switch_policy
[SWITCH_ATTR_MAX
+1] = {
222 [SWITCH_ATTR_ID
] = { .type
= NLA_U32
},
223 [SWITCH_ATTR_OP_ID
] = { .type
= NLA_U32
},
224 [SWITCH_ATTR_OP_PORT
] = { .type
= NLA_U32
},
225 [SWITCH_ATTR_OP_VLAN
] = { .type
= NLA_U32
},
226 [SWITCH_ATTR_OP_VALUE_INT
] = { .type
= NLA_U32
},
227 [SWITCH_ATTR_OP_VALUE_STR
] = { .type
= NLA_NUL_STRING
},
228 [SWITCH_ATTR_OP_VALUE_PORTS
] = { .type
= NLA_NESTED
},
229 [SWITCH_ATTR_TYPE
] = { .type
= NLA_U32
},
232 static const struct nla_policy port_policy
[SWITCH_PORT_ATTR_MAX
+1] = {
233 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
234 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
240 spin_lock(&swdevs_lock
);
244 swconfig_unlock(void)
246 spin_unlock(&swdevs_lock
);
249 static struct switch_dev
*
250 swconfig_get_dev(struct genl_info
*info
)
252 struct switch_dev
*dev
= NULL
;
253 struct switch_dev
*p
;
256 if (!info
->attrs
[SWITCH_ATTR_ID
])
259 id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_ID
]);
261 list_for_each_entry(p
, &swdevs
, dev_list
) {
269 spin_lock(&dev
->lock
);
271 DPRINTF("device %d not found\n", id
);
278 swconfig_put_dev(struct switch_dev
*dev
)
280 spin_unlock(&dev
->lock
);
284 swconfig_dump_attr(struct swconfig_callback
*cb
, void *arg
)
286 struct switch_attr
*op
= arg
;
287 struct genl_info
*info
= cb
->info
;
288 struct sk_buff
*msg
= cb
->msg
;
289 int id
= cb
->args
[0];
292 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
, &switch_fam
,
293 NLM_F_MULTI
, SWITCH_CMD_NEW_ATTR
);
297 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_ID
, id
);
298 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_TYPE
, op
->type
);
299 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_NAME
, op
->name
);
301 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_DESCRIPTION
,
304 return genlmsg_end(msg
, hdr
);
306 genlmsg_cancel(msg
, hdr
);
310 /* spread multipart messages across multiple message buffers */
312 swconfig_send_multipart(struct swconfig_callback
*cb
, void *arg
)
314 struct genl_info
*info
= cb
->info
;
320 cb
->msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
325 if (!(cb
->fill(cb
, arg
) < 0))
328 /* fill failed, check if this was already the second attempt */
332 /* try again in a new message, send the current one */
335 if (cb
->close(cb
, arg
) < 0)
338 err
= genlmsg_unicast(cb
->msg
, info
->snd_pid
);
354 swconfig_list_attrs(struct sk_buff
*skb
, struct genl_info
*info
)
356 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
357 const struct switch_attrlist
*alist
;
358 struct switch_dev
*dev
;
359 struct swconfig_callback cb
;
364 struct switch_attr
*def_list
;
365 unsigned long *def_active
;
368 dev
= swconfig_get_dev(info
);
373 case SWITCH_CMD_LIST_GLOBAL
:
374 alist
= &dev
->attr_global
;
375 def_list
= default_global
;
376 def_active
= &dev
->def_global
;
377 n_def
= ARRAY_SIZE(default_global
);
379 case SWITCH_CMD_LIST_VLAN
:
380 alist
= &dev
->attr_vlan
;
381 def_list
= default_vlan
;
382 def_active
= &dev
->def_vlan
;
383 n_def
= ARRAY_SIZE(default_vlan
);
385 case SWITCH_CMD_LIST_PORT
:
386 alist
= &dev
->attr_port
;
387 def_list
= default_port
;
388 def_active
= &dev
->def_port
;
389 n_def
= ARRAY_SIZE(default_port
);
396 memset(&cb
, 0, sizeof(cb
));
398 cb
.fill
= swconfig_dump_attr
;
399 for (i
= 0; i
< alist
->n_attr
; i
++) {
400 if (alist
->attr
[i
].disabled
)
403 err
= swconfig_send_multipart(&cb
, (void *) &alist
->attr
[i
]);
409 for (i
= 0; i
< n_def
; i
++) {
410 if (!test_bit(i
, def_active
))
412 cb
.args
[0] = SWITCH_ATTR_DEFAULTS_OFFSET
+ i
;
413 err
= swconfig_send_multipart(&cb
, (void *) &def_list
[i
]);
417 swconfig_put_dev(dev
);
422 return genlmsg_unicast(cb
.msg
, info
->snd_pid
);
428 swconfig_put_dev(dev
);
432 static const struct switch_attr
*
433 swconfig_lookup_attr(struct switch_dev
*dev
, struct genl_info
*info
,
434 struct switch_val
*val
)
436 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
437 const struct switch_attrlist
*alist
;
438 const struct switch_attr
*attr
= NULL
;
442 struct switch_attr
*def_list
;
443 unsigned long *def_active
;
446 if (!info
->attrs
[SWITCH_ATTR_OP_ID
])
450 case SWITCH_CMD_SET_GLOBAL
:
451 case SWITCH_CMD_GET_GLOBAL
:
452 alist
= &dev
->attr_global
;
453 def_list
= default_global
;
454 def_active
= &dev
->def_global
;
455 n_def
= ARRAY_SIZE(default_global
);
457 case SWITCH_CMD_SET_VLAN
:
458 case SWITCH_CMD_GET_VLAN
:
459 alist
= &dev
->attr_vlan
;
460 def_list
= default_vlan
;
461 def_active
= &dev
->def_vlan
;
462 n_def
= ARRAY_SIZE(default_vlan
);
463 if (!info
->attrs
[SWITCH_ATTR_OP_VLAN
])
465 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VLAN
]);
467 case SWITCH_CMD_SET_PORT
:
468 case SWITCH_CMD_GET_PORT
:
469 alist
= &dev
->attr_port
;
470 def_list
= default_port
;
471 def_active
= &dev
->def_port
;
472 n_def
= ARRAY_SIZE(default_port
);
473 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
475 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
485 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
486 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
487 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
488 if (attr_id
>= n_def
)
490 if (!test_bit(attr_id
, def_active
))
492 attr
= &def_list
[attr_id
];
494 if (attr_id
>= alist
->n_attr
)
496 attr
= &alist
->attr
[attr_id
];
504 DPRINTF("attribute lookup failed\n");
510 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
511 struct switch_val
*val
, int max
)
517 nla_for_each_nested(nla
, head
, rem
) {
518 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
519 struct switch_port
*port
= &val
->value
.ports
[val
->len
];
524 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
528 if (!tb
[SWITCH_PORT_ID
])
531 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
532 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
533 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
541 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
543 const struct switch_attr
*attr
;
544 struct switch_dev
*dev
;
545 struct switch_val val
;
548 dev
= swconfig_get_dev(info
);
552 memset(&val
, 0, sizeof(val
));
553 attr
= swconfig_lookup_attr(dev
, info
, &val
);
554 if (!attr
|| !attr
->set
)
559 case SWITCH_TYPE_NOVAL
:
561 case SWITCH_TYPE_INT
:
562 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
565 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
567 case SWITCH_TYPE_STRING
:
568 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
571 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
573 case SWITCH_TYPE_PORTS
:
574 val
.value
.ports
= dev
->portbuf
;
575 memset(dev
->portbuf
, 0,
576 sizeof(struct switch_port
) * dev
->ports
);
578 /* TODO: implement multipart? */
579 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
580 err
= swconfig_parse_ports(skb
,
581 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
], &val
, dev
->ports
);
593 err
= attr
->set(dev
, attr
, &val
);
595 swconfig_put_dev(dev
);
600 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
603 nla_nest_end(cb
->msg
, cb
->nest
[0]);
608 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
610 const struct switch_port
*port
= arg
;
611 struct nlattr
*p
= NULL
;
614 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
619 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
623 NLA_PUT_U32(cb
->msg
, SWITCH_PORT_ID
, port
->id
);
624 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
625 NLA_PUT_FLAG(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
);
627 nla_nest_end(cb
->msg
, p
);
631 nla_nest_cancel(cb
->msg
, p
);
633 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
638 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
639 const struct switch_val
*val
)
641 struct swconfig_callback cb
;
645 if (!val
->value
.ports
)
648 memset(&cb
, 0, sizeof(cb
));
652 cb
.fill
= swconfig_send_port
;
653 cb
.close
= swconfig_close_portlist
;
655 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
656 for (i
= 0; i
< val
->len
; i
++) {
657 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
662 swconfig_close_portlist(&cb
, NULL
);
670 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
672 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
673 const struct switch_attr
*attr
;
674 struct switch_dev
*dev
;
675 struct sk_buff
*msg
= NULL
;
676 struct switch_val val
;
680 dev
= swconfig_get_dev(info
);
684 memset(&val
, 0, sizeof(val
));
685 attr
= swconfig_lookup_attr(dev
, info
, &val
);
686 if (!attr
|| !attr
->get
)
689 if (attr
->type
== SWITCH_TYPE_PORTS
) {
690 val
.value
.ports
= dev
->portbuf
;
691 memset(dev
->portbuf
, 0,
692 sizeof(struct switch_port
) * dev
->ports
);
695 err
= attr
->get(dev
, attr
, &val
);
699 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
703 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
, &switch_fam
,
706 goto nla_put_failure
;
709 case SWITCH_TYPE_INT
:
710 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
);
712 case SWITCH_TYPE_STRING
:
713 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
);
715 case SWITCH_TYPE_PORTS
:
716 err
= swconfig_send_ports(&msg
, info
,
717 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
719 goto nla_put_failure
;
722 DPRINTF("invalid type in attribute\n");
726 err
= genlmsg_end(msg
, hdr
);
728 goto nla_put_failure
;
730 swconfig_put_dev(dev
);
731 return genlmsg_unicast(msg
, info
->snd_pid
);
737 swconfig_put_dev(dev
);
745 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
746 const struct switch_dev
*dev
)
750 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
751 SWITCH_CMD_NEW_ATTR
);
755 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, dev
->id
);
756 NLA_PUT_STRING(msg
, SWITCH_ATTR_NAME
, dev
->name
);
757 NLA_PUT_STRING(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
);
758 NLA_PUT_U32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
);
759 NLA_PUT_U32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
);
761 return genlmsg_end(msg
, hdr
);
763 genlmsg_cancel(msg
, hdr
);
767 static int swconfig_dump_switches(struct sk_buff
*skb
,
768 struct netlink_callback
*cb
)
770 struct switch_dev
*dev
;
771 int start
= cb
->args
[0];
775 list_for_each_entry(dev
, &swdevs
, dev_list
) {
778 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).pid
,
779 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
790 swconfig_done(struct netlink_callback
*cb
)
795 static struct genl_ops swconfig_ops
[] = {
797 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
798 .doit
= swconfig_list_attrs
,
799 .policy
= switch_policy
,
802 .cmd
= SWITCH_CMD_LIST_VLAN
,
803 .doit
= swconfig_list_attrs
,
804 .policy
= switch_policy
,
807 .cmd
= SWITCH_CMD_LIST_PORT
,
808 .doit
= swconfig_list_attrs
,
809 .policy
= switch_policy
,
812 .cmd
= SWITCH_CMD_GET_GLOBAL
,
813 .doit
= swconfig_get_attr
,
814 .policy
= switch_policy
,
817 .cmd
= SWITCH_CMD_GET_VLAN
,
818 .doit
= swconfig_get_attr
,
819 .policy
= switch_policy
,
822 .cmd
= SWITCH_CMD_GET_PORT
,
823 .doit
= swconfig_get_attr
,
824 .policy
= switch_policy
,
827 .cmd
= SWITCH_CMD_SET_GLOBAL
,
828 .doit
= swconfig_set_attr
,
829 .policy
= switch_policy
,
832 .cmd
= SWITCH_CMD_SET_VLAN
,
833 .doit
= swconfig_set_attr
,
834 .policy
= switch_policy
,
837 .cmd
= SWITCH_CMD_SET_PORT
,
838 .doit
= swconfig_set_attr
,
839 .policy
= switch_policy
,
842 .cmd
= SWITCH_CMD_GET_SWITCH
,
843 .dumpit
= swconfig_dump_switches
,
844 .policy
= switch_policy
,
845 .done
= swconfig_done
,
850 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
852 INIT_LIST_HEAD(&dev
->dev_list
);
854 dev
->netdev
= netdev
;
856 dev
->devname
= netdev
->name
;
858 BUG_ON(!dev
->devname
);
860 if (dev
->ports
> 0) {
861 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) * dev
->ports
,
866 dev
->id
= ++swdev_id
;
867 swconfig_defaults_init(dev
);
868 spin_lock_init(&dev
->lock
);
870 list_add(&dev
->dev_list
, &swdevs
);
875 EXPORT_SYMBOL_GPL(register_switch
);
878 unregister_switch(struct switch_dev
*dev
)
881 spin_lock(&dev
->lock
);
883 list_del(&dev
->dev_list
);
886 EXPORT_SYMBOL_GPL(unregister_switch
);
894 INIT_LIST_HEAD(&swdevs
);
895 err
= genl_register_family(&switch_fam
);
899 for (i
= 0; i
< ARRAY_SIZE(swconfig_ops
); i
++) {
900 err
= genl_register_ops(&switch_fam
, &swconfig_ops
[i
]);
908 genl_unregister_family(&switch_fam
);
915 genl_unregister_family(&switch_fam
);
918 module_init(swconfig_init
);
919 module_exit(swconfig_exit
);