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 spin_lock(&dev
->lock
);
358 DPRINTF("device %d not found\n", id
);
365 swconfig_put_dev(struct switch_dev
*dev
)
367 spin_unlock(&dev
->lock
);
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_pid
, info
->snd_seq
, &switch_fam
,
380 NLM_F_MULTI
, SWITCH_CMD_NEW_ATTR
);
384 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_ID
, id
);
385 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_TYPE
, op
->type
);
386 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_NAME
, op
->name
);
388 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_DESCRIPTION
,
391 return genlmsg_end(msg
, hdr
);
393 genlmsg_cancel(msg
, hdr
);
397 /* spread multipart messages across multiple message buffers */
399 swconfig_send_multipart(struct swconfig_callback
*cb
, void *arg
)
401 struct genl_info
*info
= cb
->info
;
407 cb
->msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
412 if (!(cb
->fill(cb
, arg
) < 0))
415 /* fill failed, check if this was already the second attempt */
419 /* try again in a new message, send the current one */
422 if (cb
->close(cb
, arg
) < 0)
425 err
= genlmsg_reply(cb
->msg
, info
);
441 swconfig_list_attrs(struct sk_buff
*skb
, struct genl_info
*info
)
443 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
444 const struct switch_attrlist
*alist
;
445 struct switch_dev
*dev
;
446 struct swconfig_callback cb
;
451 struct switch_attr
*def_list
;
452 unsigned long *def_active
;
455 dev
= swconfig_get_dev(info
);
460 case SWITCH_CMD_LIST_GLOBAL
:
461 alist
= &dev
->ops
->attr_global
;
462 def_list
= default_global
;
463 def_active
= &dev
->def_global
;
464 n_def
= ARRAY_SIZE(default_global
);
466 case SWITCH_CMD_LIST_VLAN
:
467 alist
= &dev
->ops
->attr_vlan
;
468 def_list
= default_vlan
;
469 def_active
= &dev
->def_vlan
;
470 n_def
= ARRAY_SIZE(default_vlan
);
472 case SWITCH_CMD_LIST_PORT
:
473 alist
= &dev
->ops
->attr_port
;
474 def_list
= default_port
;
475 def_active
= &dev
->def_port
;
476 n_def
= ARRAY_SIZE(default_port
);
483 memset(&cb
, 0, sizeof(cb
));
485 cb
.fill
= swconfig_dump_attr
;
486 for (i
= 0; i
< alist
->n_attr
; i
++) {
487 if (alist
->attr
[i
].disabled
)
490 err
= swconfig_send_multipart(&cb
, (void *) &alist
->attr
[i
]);
496 for (i
= 0; i
< n_def
; i
++) {
497 if (!test_bit(i
, def_active
))
499 cb
.args
[0] = SWITCH_ATTR_DEFAULTS_OFFSET
+ i
;
500 err
= swconfig_send_multipart(&cb
, (void *) &def_list
[i
]);
504 swconfig_put_dev(dev
);
509 return genlmsg_reply(cb
.msg
, info
);
515 swconfig_put_dev(dev
);
519 static const struct switch_attr
*
520 swconfig_lookup_attr(struct switch_dev
*dev
, struct genl_info
*info
,
521 struct switch_val
*val
)
523 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
524 const struct switch_attrlist
*alist
;
525 const struct switch_attr
*attr
= NULL
;
529 struct switch_attr
*def_list
;
530 unsigned long *def_active
;
533 if (!info
->attrs
[SWITCH_ATTR_OP_ID
])
537 case SWITCH_CMD_SET_GLOBAL
:
538 case SWITCH_CMD_GET_GLOBAL
:
539 alist
= &dev
->ops
->attr_global
;
540 def_list
= default_global
;
541 def_active
= &dev
->def_global
;
542 n_def
= ARRAY_SIZE(default_global
);
544 case SWITCH_CMD_SET_VLAN
:
545 case SWITCH_CMD_GET_VLAN
:
546 alist
= &dev
->ops
->attr_vlan
;
547 def_list
= default_vlan
;
548 def_active
= &dev
->def_vlan
;
549 n_def
= ARRAY_SIZE(default_vlan
);
550 if (!info
->attrs
[SWITCH_ATTR_OP_VLAN
])
552 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VLAN
]);
553 if (val
->port_vlan
>= dev
->vlans
)
556 case SWITCH_CMD_SET_PORT
:
557 case SWITCH_CMD_GET_PORT
:
558 alist
= &dev
->ops
->attr_port
;
559 def_list
= default_port
;
560 def_active
= &dev
->def_port
;
561 n_def
= ARRAY_SIZE(default_port
);
562 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
564 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
565 if (val
->port_vlan
>= dev
->ports
)
576 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
577 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
578 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
579 if (attr_id
>= n_def
)
581 if (!test_bit(attr_id
, def_active
))
583 attr
= &def_list
[attr_id
];
585 if (attr_id
>= alist
->n_attr
)
587 attr
= &alist
->attr
[attr_id
];
595 DPRINTF("attribute lookup failed\n");
601 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
602 struct switch_val
*val
, int max
)
608 nla_for_each_nested(nla
, head
, rem
) {
609 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
610 struct switch_port
*port
= &val
->value
.ports
[val
->len
];
615 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
619 if (!tb
[SWITCH_PORT_ID
])
622 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
623 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
624 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
632 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
634 const struct switch_attr
*attr
;
635 struct switch_dev
*dev
;
636 struct switch_val val
;
639 dev
= swconfig_get_dev(info
);
643 memset(&val
, 0, sizeof(val
));
644 attr
= swconfig_lookup_attr(dev
, info
, &val
);
645 if (!attr
|| !attr
->set
)
650 case SWITCH_TYPE_NOVAL
:
652 case SWITCH_TYPE_INT
:
653 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
656 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
658 case SWITCH_TYPE_STRING
:
659 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
662 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
664 case SWITCH_TYPE_PORTS
:
665 val
.value
.ports
= dev
->portbuf
;
666 memset(dev
->portbuf
, 0,
667 sizeof(struct switch_port
) * dev
->ports
);
669 /* TODO: implement multipart? */
670 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
671 err
= swconfig_parse_ports(skb
,
672 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
], &val
, dev
->ports
);
684 err
= attr
->set(dev
, attr
, &val
);
686 swconfig_put_dev(dev
);
691 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
694 nla_nest_end(cb
->msg
, cb
->nest
[0]);
699 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
701 const struct switch_port
*port
= arg
;
702 struct nlattr
*p
= NULL
;
705 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
710 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
714 NLA_PUT_U32(cb
->msg
, SWITCH_PORT_ID
, port
->id
);
715 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
716 NLA_PUT_FLAG(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
);
718 nla_nest_end(cb
->msg
, p
);
722 nla_nest_cancel(cb
->msg
, p
);
724 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
729 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
730 const struct switch_val
*val
)
732 struct swconfig_callback cb
;
736 if (!val
->value
.ports
)
739 memset(&cb
, 0, sizeof(cb
));
743 cb
.fill
= swconfig_send_port
;
744 cb
.close
= swconfig_close_portlist
;
746 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
747 for (i
= 0; i
< val
->len
; i
++) {
748 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
753 swconfig_close_portlist(&cb
, NULL
);
761 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
763 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
764 const struct switch_attr
*attr
;
765 struct switch_dev
*dev
;
766 struct sk_buff
*msg
= NULL
;
767 struct switch_val val
;
771 dev
= swconfig_get_dev(info
);
775 memset(&val
, 0, sizeof(val
));
776 attr
= swconfig_lookup_attr(dev
, info
, &val
);
777 if (!attr
|| !attr
->get
)
780 if (attr
->type
== SWITCH_TYPE_PORTS
) {
781 val
.value
.ports
= dev
->portbuf
;
782 memset(dev
->portbuf
, 0,
783 sizeof(struct switch_port
) * dev
->ports
);
786 err
= attr
->get(dev
, attr
, &val
);
790 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
794 hdr
= genlmsg_put(msg
, info
->snd_pid
, info
->snd_seq
, &switch_fam
,
797 goto nla_put_failure
;
800 case SWITCH_TYPE_INT
:
801 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
);
803 case SWITCH_TYPE_STRING
:
804 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
);
806 case SWITCH_TYPE_PORTS
:
807 err
= swconfig_send_ports(&msg
, info
,
808 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
810 goto nla_put_failure
;
813 DPRINTF("invalid type in attribute\n");
817 err
= genlmsg_end(msg
, hdr
);
819 goto nla_put_failure
;
821 swconfig_put_dev(dev
);
822 return genlmsg_reply(msg
, info
);
828 swconfig_put_dev(dev
);
835 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
836 const struct switch_dev
*dev
)
840 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
841 SWITCH_CMD_NEW_ATTR
);
845 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, dev
->id
);
846 NLA_PUT_STRING(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
);
847 NLA_PUT_STRING(msg
, SWITCH_ATTR_ALIAS
, dev
->alias
);
848 NLA_PUT_STRING(msg
, SWITCH_ATTR_NAME
, dev
->name
);
849 NLA_PUT_U32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
);
850 NLA_PUT_U32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
);
851 NLA_PUT_U32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
);
853 return genlmsg_end(msg
, hdr
);
855 genlmsg_cancel(msg
, hdr
);
859 static int swconfig_dump_switches(struct sk_buff
*skb
,
860 struct netlink_callback
*cb
)
862 struct switch_dev
*dev
;
863 int start
= cb
->args
[0];
867 list_for_each_entry(dev
, &swdevs
, dev_list
) {
870 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).pid
,
871 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
882 swconfig_done(struct netlink_callback
*cb
)
887 static struct genl_ops swconfig_ops
[] = {
889 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
890 .doit
= swconfig_list_attrs
,
891 .policy
= switch_policy
,
894 .cmd
= SWITCH_CMD_LIST_VLAN
,
895 .doit
= swconfig_list_attrs
,
896 .policy
= switch_policy
,
899 .cmd
= SWITCH_CMD_LIST_PORT
,
900 .doit
= swconfig_list_attrs
,
901 .policy
= switch_policy
,
904 .cmd
= SWITCH_CMD_GET_GLOBAL
,
905 .doit
= swconfig_get_attr
,
906 .policy
= switch_policy
,
909 .cmd
= SWITCH_CMD_GET_VLAN
,
910 .doit
= swconfig_get_attr
,
911 .policy
= switch_policy
,
914 .cmd
= SWITCH_CMD_GET_PORT
,
915 .doit
= swconfig_get_attr
,
916 .policy
= switch_policy
,
919 .cmd
= SWITCH_CMD_SET_GLOBAL
,
920 .doit
= swconfig_set_attr
,
921 .policy
= switch_policy
,
924 .cmd
= SWITCH_CMD_SET_VLAN
,
925 .doit
= swconfig_set_attr
,
926 .policy
= switch_policy
,
929 .cmd
= SWITCH_CMD_SET_PORT
,
930 .doit
= swconfig_set_attr
,
931 .policy
= switch_policy
,
934 .cmd
= SWITCH_CMD_GET_SWITCH
,
935 .dumpit
= swconfig_dump_switches
,
936 .policy
= switch_policy
,
937 .done
= swconfig_done
,
942 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
944 struct switch_dev
*sdev
;
945 const int max_switches
= 8 * sizeof(unsigned long);
946 unsigned long in_use
= 0;
950 INIT_LIST_HEAD(&dev
->dev_list
);
952 dev
->netdev
= netdev
;
954 dev
->alias
= netdev
->name
;
958 if (dev
->ports
> 0) {
959 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) * dev
->ports
,
964 swconfig_defaults_init(dev
);
965 spin_lock_init(&dev
->lock
);
967 dev
->id
= ++swdev_id
;
969 list_for_each_entry(sdev
, &swdevs
, dev_list
) {
970 if (!sscanf(sdev
->devname
, SWCONFIG_DEVNAME
, &i
))
972 if (i
< 0 || i
> max_switches
)
977 i
= find_first_zero_bit(&in_use
, max_switches
);
979 if (i
== max_switches
) {
984 /* fill device name */
985 snprintf(dev
->devname
, IFNAMSIZ
, SWCONFIG_DEVNAME
, i
);
987 list_add(&dev
->dev_list
, &swdevs
);
990 err
= swconfig_create_led_trigger(dev
);
996 EXPORT_SYMBOL_GPL(register_switch
);
999 unregister_switch(struct switch_dev
*dev
)
1001 swconfig_destroy_led_trigger(dev
);
1002 kfree(dev
->portbuf
);
1003 spin_lock(&dev
->lock
);
1005 list_del(&dev
->dev_list
);
1007 spin_unlock(&dev
->lock
);
1009 EXPORT_SYMBOL_GPL(unregister_switch
);
1017 INIT_LIST_HEAD(&swdevs
);
1018 err
= genl_register_family(&switch_fam
);
1022 for (i
= 0; i
< ARRAY_SIZE(swconfig_ops
); i
++) {
1023 err
= genl_register_ops(&switch_fam
, &swconfig_ops
[i
]);
1031 genl_unregister_family(&switch_fam
);
1038 genl_unregister_family(&switch_fam
);
1041 module_init(swconfig_init
);
1042 module_exit(swconfig_exit
);