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_speed_str(enum switch_port_speed speed
)
136 case SWITCH_PORT_SPEED_10
:
138 case SWITCH_PORT_SPEED_100
:
140 case SWITCH_PORT_SPEED_1000
:
150 swconfig_get_link(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
152 struct switch_port_link link
;
156 if (val
->port_vlan
>= dev
->ports
)
159 if (!dev
->ops
->get_port_link
)
162 memset(&link
, 0, sizeof(link
));
163 ret
= dev
->ops
->get_port_link(dev
, val
->port_vlan
, &link
);
167 memset(dev
->buf
, 0, sizeof(dev
->buf
));
170 len
= snprintf(dev
->buf
, sizeof(dev
->buf
),
171 "port:%d link:up speed:%s %s-duplex %s%s%s",
173 swconfig_speed_str(link
.speed
),
174 link
.duplex
? "full" : "half",
175 link
.tx_flow
? "txflow ": "",
176 link
.rx_flow
? "rxflow " : "",
177 link
.aneg
? "auto" : "");
179 len
= snprintf(dev
->buf
, sizeof(dev
->buf
), "port:%d link:down",
182 val
->value
.s
= dev
->buf
;
189 swconfig_apply_config(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
191 /* don't complain if not supported by the switch driver */
192 if (!dev
->ops
->apply_config
)
195 return dev
->ops
->apply_config(dev
);
199 swconfig_reset_switch(struct switch_dev
*dev
, const struct switch_attr
*attr
, struct switch_val
*val
)
201 /* don't complain if not supported by the switch driver */
202 if (!dev
->ops
->reset_switch
)
205 return dev
->ops
->reset_switch(dev
);
208 enum global_defaults
{
222 static struct switch_attr default_global
[] = {
224 .type
= SWITCH_TYPE_NOVAL
,
226 .description
= "Activate changes in the hardware",
227 .set
= swconfig_apply_config
,
230 .type
= SWITCH_TYPE_NOVAL
,
232 .description
= "Reset the switch",
233 .set
= swconfig_reset_switch
,
237 static struct switch_attr default_port
[] = {
239 .type
= SWITCH_TYPE_INT
,
241 .description
= "Primary VLAN ID",
242 .set
= swconfig_set_pvid
,
243 .get
= swconfig_get_pvid
,
246 .type
= SWITCH_TYPE_STRING
,
248 .description
= "Get port link information",
250 .get
= swconfig_get_link
,
254 static struct switch_attr default_vlan
[] = {
256 .type
= SWITCH_TYPE_PORTS
,
258 .description
= "VLAN port mapping",
259 .set
= swconfig_set_vlan_ports
,
260 .get
= swconfig_get_vlan_ports
,
264 static const struct switch_attr
*
265 swconfig_find_attr_by_name(const struct switch_attrlist
*alist
, const char *name
)
269 for (i
= 0; i
< alist
->n_attr
; i
++)
270 if (strcmp(name
, alist
->attr
[i
].name
) == 0)
271 return &alist
->attr
[i
];
276 static void swconfig_defaults_init(struct switch_dev
*dev
)
278 const struct switch_dev_ops
*ops
= dev
->ops
;
284 if (ops
->get_vlan_ports
|| ops
->set_vlan_ports
)
285 set_bit(VLAN_PORTS
, &dev
->def_vlan
);
287 if (ops
->get_port_pvid
|| ops
->set_port_pvid
)
288 set_bit(PORT_PVID
, &dev
->def_port
);
290 if (ops
->get_port_link
&&
291 !swconfig_find_attr_by_name(&ops
->attr_port
, "link"))
292 set_bit(PORT_LINK
, &dev
->def_port
);
294 /* always present, can be no-op */
295 set_bit(GLOBAL_APPLY
, &dev
->def_global
);
296 set_bit(GLOBAL_RESET
, &dev
->def_global
);
300 static struct genl_family switch_fam
= {
301 .id
= GENL_ID_GENERATE
,
305 .maxattr
= SWITCH_ATTR_MAX
,
308 static const struct nla_policy switch_policy
[SWITCH_ATTR_MAX
+1] = {
309 [SWITCH_ATTR_ID
] = { .type
= NLA_U32
},
310 [SWITCH_ATTR_OP_ID
] = { .type
= NLA_U32
},
311 [SWITCH_ATTR_OP_PORT
] = { .type
= NLA_U32
},
312 [SWITCH_ATTR_OP_VLAN
] = { .type
= NLA_U32
},
313 [SWITCH_ATTR_OP_VALUE_INT
] = { .type
= NLA_U32
},
314 [SWITCH_ATTR_OP_VALUE_STR
] = { .type
= NLA_NUL_STRING
},
315 [SWITCH_ATTR_OP_VALUE_PORTS
] = { .type
= NLA_NESTED
},
316 [SWITCH_ATTR_TYPE
] = { .type
= NLA_U32
},
319 static const struct nla_policy port_policy
[SWITCH_PORT_ATTR_MAX
+1] = {
320 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
321 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
327 spin_lock(&swdevs_lock
);
331 swconfig_unlock(void)
333 spin_unlock(&swdevs_lock
);
336 static struct switch_dev
*
337 swconfig_get_dev(struct genl_info
*info
)
339 struct switch_dev
*dev
= NULL
;
340 struct switch_dev
*p
;
343 if (!info
->attrs
[SWITCH_ATTR_ID
])
346 id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_ID
]);
348 list_for_each_entry(p
, &swdevs
, dev_list
) {
356 mutex_lock(&dev
->sw_mutex
);
358 DPRINTF("device %d not found\n", id
);
365 swconfig_put_dev(struct switch_dev
*dev
)
367 mutex_unlock(&dev
->sw_mutex
);
371 swconfig_dump_attr(struct swconfig_callback
*cb
, void *arg
)
373 struct switch_attr
*op
= arg
;
374 struct genl_info
*info
= cb
->info
;
375 struct sk_buff
*msg
= cb
->msg
;
376 int id
= cb
->args
[0];
379 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
380 NLM_F_MULTI
, SWITCH_CMD_NEW_ATTR
);
384 if (nla_put_u32(msg
, SWITCH_ATTR_OP_ID
, id
))
385 goto nla_put_failure
;
386 if (nla_put_u32(msg
, SWITCH_ATTR_OP_TYPE
, op
->type
))
387 goto nla_put_failure
;
388 if (nla_put_string(msg
, SWITCH_ATTR_OP_NAME
, op
->name
))
389 goto nla_put_failure
;
391 if (nla_put_string(msg
, SWITCH_ATTR_OP_DESCRIPTION
,
393 goto nla_put_failure
;
395 return genlmsg_end(msg
, hdr
);
397 genlmsg_cancel(msg
, hdr
);
401 /* spread multipart messages across multiple message buffers */
403 swconfig_send_multipart(struct swconfig_callback
*cb
, void *arg
)
405 struct genl_info
*info
= cb
->info
;
411 cb
->msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
416 if (!(cb
->fill(cb
, arg
) < 0))
419 /* fill failed, check if this was already the second attempt */
423 /* try again in a new message, send the current one */
426 if (cb
->close(cb
, arg
) < 0)
429 err
= genlmsg_reply(cb
->msg
, info
);
445 swconfig_list_attrs(struct sk_buff
*skb
, struct genl_info
*info
)
447 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
448 const struct switch_attrlist
*alist
;
449 struct switch_dev
*dev
;
450 struct swconfig_callback cb
;
455 struct switch_attr
*def_list
;
456 unsigned long *def_active
;
459 dev
= swconfig_get_dev(info
);
464 case SWITCH_CMD_LIST_GLOBAL
:
465 alist
= &dev
->ops
->attr_global
;
466 def_list
= default_global
;
467 def_active
= &dev
->def_global
;
468 n_def
= ARRAY_SIZE(default_global
);
470 case SWITCH_CMD_LIST_VLAN
:
471 alist
= &dev
->ops
->attr_vlan
;
472 def_list
= default_vlan
;
473 def_active
= &dev
->def_vlan
;
474 n_def
= ARRAY_SIZE(default_vlan
);
476 case SWITCH_CMD_LIST_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
);
487 memset(&cb
, 0, sizeof(cb
));
489 cb
.fill
= swconfig_dump_attr
;
490 for (i
= 0; i
< alist
->n_attr
; i
++) {
491 if (alist
->attr
[i
].disabled
)
494 err
= swconfig_send_multipart(&cb
, (void *) &alist
->attr
[i
]);
500 for (i
= 0; i
< n_def
; i
++) {
501 if (!test_bit(i
, def_active
))
503 cb
.args
[0] = SWITCH_ATTR_DEFAULTS_OFFSET
+ i
;
504 err
= swconfig_send_multipart(&cb
, (void *) &def_list
[i
]);
508 swconfig_put_dev(dev
);
513 return genlmsg_reply(cb
.msg
, info
);
519 swconfig_put_dev(dev
);
523 static const struct switch_attr
*
524 swconfig_lookup_attr(struct switch_dev
*dev
, struct genl_info
*info
,
525 struct switch_val
*val
)
527 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
528 const struct switch_attrlist
*alist
;
529 const struct switch_attr
*attr
= NULL
;
533 struct switch_attr
*def_list
;
534 unsigned long *def_active
;
537 if (!info
->attrs
[SWITCH_ATTR_OP_ID
])
541 case SWITCH_CMD_SET_GLOBAL
:
542 case SWITCH_CMD_GET_GLOBAL
:
543 alist
= &dev
->ops
->attr_global
;
544 def_list
= default_global
;
545 def_active
= &dev
->def_global
;
546 n_def
= ARRAY_SIZE(default_global
);
548 case SWITCH_CMD_SET_VLAN
:
549 case SWITCH_CMD_GET_VLAN
:
550 alist
= &dev
->ops
->attr_vlan
;
551 def_list
= default_vlan
;
552 def_active
= &dev
->def_vlan
;
553 n_def
= ARRAY_SIZE(default_vlan
);
554 if (!info
->attrs
[SWITCH_ATTR_OP_VLAN
])
556 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VLAN
]);
557 if (val
->port_vlan
>= dev
->vlans
)
560 case SWITCH_CMD_SET_PORT
:
561 case SWITCH_CMD_GET_PORT
:
562 alist
= &dev
->ops
->attr_port
;
563 def_list
= default_port
;
564 def_active
= &dev
->def_port
;
565 n_def
= ARRAY_SIZE(default_port
);
566 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
568 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
569 if (val
->port_vlan
>= dev
->ports
)
580 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
581 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
582 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
583 if (attr_id
>= n_def
)
585 if (!test_bit(attr_id
, def_active
))
587 attr
= &def_list
[attr_id
];
589 if (attr_id
>= alist
->n_attr
)
591 attr
= &alist
->attr
[attr_id
];
599 DPRINTF("attribute lookup failed\n");
605 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
606 struct switch_val
*val
, int max
)
612 nla_for_each_nested(nla
, head
, rem
) {
613 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
614 struct switch_port
*port
= &val
->value
.ports
[val
->len
];
619 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
623 if (!tb
[SWITCH_PORT_ID
])
626 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
627 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
628 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
636 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
638 const struct switch_attr
*attr
;
639 struct switch_dev
*dev
;
640 struct switch_val val
;
643 dev
= swconfig_get_dev(info
);
647 memset(&val
, 0, sizeof(val
));
648 attr
= swconfig_lookup_attr(dev
, info
, &val
);
649 if (!attr
|| !attr
->set
)
654 case SWITCH_TYPE_NOVAL
:
656 case SWITCH_TYPE_INT
:
657 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
660 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
662 case SWITCH_TYPE_STRING
:
663 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
666 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
668 case SWITCH_TYPE_PORTS
:
669 val
.value
.ports
= dev
->portbuf
;
670 memset(dev
->portbuf
, 0,
671 sizeof(struct switch_port
) * dev
->ports
);
673 /* TODO: implement multipart? */
674 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
675 err
= swconfig_parse_ports(skb
,
676 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
], &val
, dev
->ports
);
688 err
= attr
->set(dev
, attr
, &val
);
690 swconfig_put_dev(dev
);
695 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
698 nla_nest_end(cb
->msg
, cb
->nest
[0]);
703 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
705 const struct switch_port
*port
= arg
;
706 struct nlattr
*p
= NULL
;
709 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
714 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
718 if (nla_put_u32(cb
->msg
, SWITCH_PORT_ID
, port
->id
))
719 goto nla_put_failure
;
720 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)) {
721 if (nla_put_flag(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
))
722 goto nla_put_failure
;
725 nla_nest_end(cb
->msg
, p
);
729 nla_nest_cancel(cb
->msg
, p
);
731 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
736 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
737 const struct switch_val
*val
)
739 struct swconfig_callback cb
;
743 if (!val
->value
.ports
)
746 memset(&cb
, 0, sizeof(cb
));
750 cb
.fill
= swconfig_send_port
;
751 cb
.close
= swconfig_close_portlist
;
753 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
754 for (i
= 0; i
< val
->len
; i
++) {
755 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
760 swconfig_close_portlist(&cb
, NULL
);
768 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
770 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
771 const struct switch_attr
*attr
;
772 struct switch_dev
*dev
;
773 struct sk_buff
*msg
= NULL
;
774 struct switch_val val
;
778 dev
= swconfig_get_dev(info
);
782 memset(&val
, 0, sizeof(val
));
783 attr
= swconfig_lookup_attr(dev
, info
, &val
);
784 if (!attr
|| !attr
->get
)
787 if (attr
->type
== SWITCH_TYPE_PORTS
) {
788 val
.value
.ports
= dev
->portbuf
;
789 memset(dev
->portbuf
, 0,
790 sizeof(struct switch_port
) * dev
->ports
);
793 err
= attr
->get(dev
, attr
, &val
);
797 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
801 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
804 goto nla_put_failure
;
807 case SWITCH_TYPE_INT
:
808 if (nla_put_u32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
))
809 goto nla_put_failure
;
811 case SWITCH_TYPE_STRING
:
812 if (nla_put_string(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
))
813 goto nla_put_failure
;
815 case SWITCH_TYPE_PORTS
:
816 err
= swconfig_send_ports(&msg
, info
,
817 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
819 goto nla_put_failure
;
822 DPRINTF("invalid type in attribute\n");
826 err
= genlmsg_end(msg
, hdr
);
828 goto nla_put_failure
;
830 swconfig_put_dev(dev
);
831 return genlmsg_reply(msg
, info
);
837 swconfig_put_dev(dev
);
844 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
845 const struct switch_dev
*dev
)
849 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
850 SWITCH_CMD_NEW_ATTR
);
854 if (nla_put_u32(msg
, SWITCH_ATTR_ID
, dev
->id
))
855 goto nla_put_failure
;
856 if (nla_put_string(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
))
857 goto nla_put_failure
;
858 if (nla_put_string(msg
, SWITCH_ATTR_ALIAS
, dev
->alias
))
859 goto nla_put_failure
;
860 if (nla_put_string(msg
, SWITCH_ATTR_NAME
, dev
->name
))
861 goto nla_put_failure
;
862 if (nla_put_u32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
))
863 goto nla_put_failure
;
864 if (nla_put_u32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
))
865 goto nla_put_failure
;
866 if (nla_put_u32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
))
867 goto nla_put_failure
;
869 return genlmsg_end(msg
, hdr
);
871 genlmsg_cancel(msg
, hdr
);
875 static int swconfig_dump_switches(struct sk_buff
*skb
,
876 struct netlink_callback
*cb
)
878 struct switch_dev
*dev
;
879 int start
= cb
->args
[0];
883 list_for_each_entry(dev
, &swdevs
, dev_list
) {
886 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).portid
,
887 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
898 swconfig_done(struct netlink_callback
*cb
)
903 static struct genl_ops swconfig_ops
[] = {
905 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
906 .doit
= swconfig_list_attrs
,
907 .policy
= switch_policy
,
910 .cmd
= SWITCH_CMD_LIST_VLAN
,
911 .doit
= swconfig_list_attrs
,
912 .policy
= switch_policy
,
915 .cmd
= SWITCH_CMD_LIST_PORT
,
916 .doit
= swconfig_list_attrs
,
917 .policy
= switch_policy
,
920 .cmd
= SWITCH_CMD_GET_GLOBAL
,
921 .doit
= swconfig_get_attr
,
922 .policy
= switch_policy
,
925 .cmd
= SWITCH_CMD_GET_VLAN
,
926 .doit
= swconfig_get_attr
,
927 .policy
= switch_policy
,
930 .cmd
= SWITCH_CMD_GET_PORT
,
931 .doit
= swconfig_get_attr
,
932 .policy
= switch_policy
,
935 .cmd
= SWITCH_CMD_SET_GLOBAL
,
936 .doit
= swconfig_set_attr
,
937 .policy
= switch_policy
,
940 .cmd
= SWITCH_CMD_SET_VLAN
,
941 .doit
= swconfig_set_attr
,
942 .policy
= switch_policy
,
945 .cmd
= SWITCH_CMD_SET_PORT
,
946 .doit
= swconfig_set_attr
,
947 .policy
= switch_policy
,
950 .cmd
= SWITCH_CMD_GET_SWITCH
,
951 .dumpit
= swconfig_dump_switches
,
952 .policy
= switch_policy
,
953 .done
= swconfig_done
,
958 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
960 struct switch_dev
*sdev
;
961 const int max_switches
= 8 * sizeof(unsigned long);
962 unsigned long in_use
= 0;
966 INIT_LIST_HEAD(&dev
->dev_list
);
968 dev
->netdev
= netdev
;
970 dev
->alias
= netdev
->name
;
974 if (dev
->ports
> 0) {
975 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) * dev
->ports
,
980 swconfig_defaults_init(dev
);
981 mutex_init(&dev
->sw_mutex
);
983 dev
->id
= ++swdev_id
;
985 list_for_each_entry(sdev
, &swdevs
, dev_list
) {
986 if (!sscanf(sdev
->devname
, SWCONFIG_DEVNAME
, &i
))
988 if (i
< 0 || i
> max_switches
)
993 i
= find_first_zero_bit(&in_use
, max_switches
);
995 if (i
== max_switches
) {
1000 /* fill device name */
1001 snprintf(dev
->devname
, IFNAMSIZ
, SWCONFIG_DEVNAME
, i
);
1003 list_add(&dev
->dev_list
, &swdevs
);
1006 err
= swconfig_create_led_trigger(dev
);
1012 EXPORT_SYMBOL_GPL(register_switch
);
1015 unregister_switch(struct switch_dev
*dev
)
1017 swconfig_destroy_led_trigger(dev
);
1018 kfree(dev
->portbuf
);
1019 mutex_lock(&dev
->sw_mutex
);
1021 list_del(&dev
->dev_list
);
1023 mutex_unlock(&dev
->sw_mutex
);
1025 EXPORT_SYMBOL_GPL(unregister_switch
);
1033 INIT_LIST_HEAD(&swdevs
);
1034 err
= genl_register_family(&switch_fam
);
1038 for (i
= 0; i
< ARRAY_SIZE(swconfig_ops
); i
++) {
1039 err
= genl_register_ops(&switch_fam
, &swconfig_ops
[i
]);
1047 genl_unregister_family(&switch_fam
);
1054 genl_unregister_family(&switch_fam
);
1057 module_init(swconfig_init
);
1058 module_exit(swconfig_exit
);