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
& (1 << 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
]);
466 if (val
->port_vlan
>= dev
->vlans
)
469 case SWITCH_CMD_SET_PORT
:
470 case SWITCH_CMD_GET_PORT
:
471 alist
= &dev
->attr_port
;
472 def_list
= default_port
;
473 def_active
= &dev
->def_port
;
474 n_def
= ARRAY_SIZE(default_port
);
475 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
477 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
478 if (val
->port_vlan
>= dev
->ports
)
489 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
490 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
491 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
492 if (attr_id
>= n_def
)
494 if (!test_bit(attr_id
, def_active
))
496 attr
= &def_list
[attr_id
];
498 if (attr_id
>= alist
->n_attr
)
500 attr
= &alist
->attr
[attr_id
];
508 DPRINTF("attribute lookup failed\n");
514 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
515 struct switch_val
*val
, int max
)
521 nla_for_each_nested(nla
, head
, rem
) {
522 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
523 struct switch_port
*port
= &val
->value
.ports
[val
->len
];
528 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
532 if (!tb
[SWITCH_PORT_ID
])
535 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
536 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
537 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
545 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
547 const struct switch_attr
*attr
;
548 struct switch_dev
*dev
;
549 struct switch_val val
;
552 dev
= swconfig_get_dev(info
);
556 memset(&val
, 0, sizeof(val
));
557 attr
= swconfig_lookup_attr(dev
, info
, &val
);
558 if (!attr
|| !attr
->set
)
563 case SWITCH_TYPE_NOVAL
:
565 case SWITCH_TYPE_INT
:
566 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
569 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
571 case SWITCH_TYPE_STRING
:
572 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
575 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
577 case SWITCH_TYPE_PORTS
:
578 val
.value
.ports
= dev
->portbuf
;
579 memset(dev
->portbuf
, 0,
580 sizeof(struct switch_port
) * dev
->ports
);
582 /* TODO: implement multipart? */
583 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
584 err
= swconfig_parse_ports(skb
,
585 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
], &val
, dev
->ports
);
597 err
= attr
->set(dev
, attr
, &val
);
599 swconfig_put_dev(dev
);
604 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
607 nla_nest_end(cb
->msg
, cb
->nest
[0]);
612 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
614 const struct switch_port
*port
= arg
;
615 struct nlattr
*p
= NULL
;
618 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
623 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
627 NLA_PUT_U32(cb
->msg
, SWITCH_PORT_ID
, port
->id
);
628 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
629 NLA_PUT_FLAG(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
);
631 nla_nest_end(cb
->msg
, p
);
635 nla_nest_cancel(cb
->msg
, p
);
637 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
642 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
643 const struct switch_val
*val
)
645 struct swconfig_callback cb
;
649 if (!val
->value
.ports
)
652 memset(&cb
, 0, sizeof(cb
));
656 cb
.fill
= swconfig_send_port
;
657 cb
.close
= swconfig_close_portlist
;
659 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
660 for (i
= 0; i
< val
->len
; i
++) {
661 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
666 swconfig_close_portlist(&cb
, NULL
);
674 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
676 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
677 const struct switch_attr
*attr
;
678 struct switch_dev
*dev
;
679 struct sk_buff
*msg
= NULL
;
680 struct switch_val val
;
684 dev
= swconfig_get_dev(info
);
688 memset(&val
, 0, sizeof(val
));
689 attr
= swconfig_lookup_attr(dev
, info
, &val
);
690 if (!attr
|| !attr
->get
)
693 if (attr
->type
== SWITCH_TYPE_PORTS
) {
694 val
.value
.ports
= dev
->portbuf
;
695 memset(dev
->portbuf
, 0,
696 sizeof(struct switch_port
) * dev
->ports
);
699 err
= attr
->get(dev
, attr
, &val
);
703 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
707 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
, &switch_fam
,
710 goto nla_put_failure
;
713 case SWITCH_TYPE_INT
:
714 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
);
716 case SWITCH_TYPE_STRING
:
717 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
);
719 case SWITCH_TYPE_PORTS
:
720 err
= swconfig_send_ports(&msg
, info
,
721 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
723 goto nla_put_failure
;
726 DPRINTF("invalid type in attribute\n");
730 err
= genlmsg_end(msg
, hdr
);
732 goto nla_put_failure
;
734 swconfig_put_dev(dev
);
735 return genlmsg_unicast(msg
, info
->snd_pid
);
741 swconfig_put_dev(dev
);
749 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
750 const struct switch_dev
*dev
)
754 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
755 SWITCH_CMD_NEW_ATTR
);
759 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, dev
->id
);
760 NLA_PUT_STRING(msg
, SWITCH_ATTR_NAME
, dev
->name
);
761 NLA_PUT_STRING(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
);
762 NLA_PUT_U32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
);
763 NLA_PUT_U32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
);
764 NLA_PUT_U32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
);
766 return genlmsg_end(msg
, hdr
);
768 genlmsg_cancel(msg
, hdr
);
772 static int swconfig_dump_switches(struct sk_buff
*skb
,
773 struct netlink_callback
*cb
)
775 struct switch_dev
*dev
;
776 int start
= cb
->args
[0];
780 list_for_each_entry(dev
, &swdevs
, dev_list
) {
783 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).pid
,
784 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
795 swconfig_done(struct netlink_callback
*cb
)
800 static struct genl_ops swconfig_ops
[] = {
802 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
803 .doit
= swconfig_list_attrs
,
804 .policy
= switch_policy
,
807 .cmd
= SWITCH_CMD_LIST_VLAN
,
808 .doit
= swconfig_list_attrs
,
809 .policy
= switch_policy
,
812 .cmd
= SWITCH_CMD_LIST_PORT
,
813 .doit
= swconfig_list_attrs
,
814 .policy
= switch_policy
,
817 .cmd
= SWITCH_CMD_GET_GLOBAL
,
818 .doit
= swconfig_get_attr
,
819 .policy
= switch_policy
,
822 .cmd
= SWITCH_CMD_GET_VLAN
,
823 .doit
= swconfig_get_attr
,
824 .policy
= switch_policy
,
827 .cmd
= SWITCH_CMD_GET_PORT
,
828 .doit
= swconfig_get_attr
,
829 .policy
= switch_policy
,
832 .cmd
= SWITCH_CMD_SET_GLOBAL
,
833 .doit
= swconfig_set_attr
,
834 .policy
= switch_policy
,
837 .cmd
= SWITCH_CMD_SET_VLAN
,
838 .doit
= swconfig_set_attr
,
839 .policy
= switch_policy
,
842 .cmd
= SWITCH_CMD_SET_PORT
,
843 .doit
= swconfig_set_attr
,
844 .policy
= switch_policy
,
847 .cmd
= SWITCH_CMD_GET_SWITCH
,
848 .dumpit
= swconfig_dump_switches
,
849 .policy
= switch_policy
,
850 .done
= swconfig_done
,
855 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
857 INIT_LIST_HEAD(&dev
->dev_list
);
859 dev
->netdev
= netdev
;
861 dev
->devname
= netdev
->name
;
863 BUG_ON(!dev
->devname
);
865 if (dev
->ports
> 0) {
866 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) * dev
->ports
,
871 dev
->id
= ++swdev_id
;
872 swconfig_defaults_init(dev
);
873 spin_lock_init(&dev
->lock
);
875 list_add(&dev
->dev_list
, &swdevs
);
880 EXPORT_SYMBOL_GPL(register_switch
);
883 unregister_switch(struct switch_dev
*dev
)
886 spin_lock(&dev
->lock
);
888 list_del(&dev
->dev_list
);
891 EXPORT_SYMBOL_GPL(unregister_switch
);
899 INIT_LIST_HEAD(&swdevs
);
900 err
= genl_register_family(&switch_fam
);
904 for (i
= 0; i
< ARRAY_SIZE(swconfig_ops
); i
++) {
905 err
= genl_register_ops(&switch_fam
, &swconfig_ops
[i
]);
913 genl_unregister_family(&switch_fam
);
920 genl_unregister_family(&switch_fam
);
923 module_init(swconfig_init
);
924 module_exit(swconfig_exit
);