2 * swconfig.c: Switch configuration API
4 * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
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>
27 #include <linux/version.h>
28 #include <uapi/linux/mii.h>
30 #define SWCONFIG_DEVNAME "switch%d"
32 #include "swconfig_leds.c"
34 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
35 MODULE_LICENSE("GPL");
38 static struct list_head swdevs
;
39 static DEFINE_MUTEX(swdevs_lock
);
40 struct swconfig_callback
;
42 struct swconfig_callback
{
44 struct genlmsghdr
*hdr
;
45 struct genl_info
*info
;
48 /* callback for filling in the message data */
49 int (*fill
)(struct swconfig_callback
*cb
, void *arg
);
51 /* callback for closing the message before sending it */
52 int (*close
)(struct swconfig_callback
*cb
, void *arg
);
54 struct nlattr
*nest
[4];
61 swconfig_get_vlan_ports(struct switch_dev
*dev
, const struct switch_attr
*attr
,
62 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
,
77 struct switch_val
*val
)
79 struct switch_port
*ports
= val
->value
.ports
;
80 const struct switch_dev_ops
*ops
= dev
->ops
;
83 if (val
->port_vlan
>= dev
->vlans
)
87 if (val
->len
> dev
->ports
)
90 if (!ops
->set_vlan_ports
)
93 for (i
= 0; i
< val
->len
; i
++) {
94 if (ports
[i
].id
>= dev
->ports
)
97 if (ops
->set_port_pvid
&&
98 !(ports
[i
].flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)))
99 ops
->set_port_pvid(dev
, ports
[i
].id
, val
->port_vlan
);
102 return ops
->set_vlan_ports(dev
, val
);
106 swconfig_set_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
107 struct switch_val
*val
)
109 if (val
->port_vlan
>= dev
->ports
)
112 if (!dev
->ops
->set_port_pvid
)
115 return dev
->ops
->set_port_pvid(dev
, val
->port_vlan
, val
->value
.i
);
119 swconfig_get_pvid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
120 struct switch_val
*val
)
122 if (val
->port_vlan
>= dev
->ports
)
125 if (!dev
->ops
->get_port_pvid
)
128 return dev
->ops
->get_port_pvid(dev
, val
->port_vlan
, &val
->value
.i
);
132 swconfig_set_link(struct switch_dev
*dev
, const struct switch_attr
*attr
,
133 struct switch_val
*val
)
135 if (!dev
->ops
->set_port_link
)
138 return dev
->ops
->set_port_link(dev
, val
->port_vlan
, val
->value
.link
);
142 swconfig_get_link(struct switch_dev
*dev
, const struct switch_attr
*attr
,
143 struct switch_val
*val
)
145 struct switch_port_link
*link
= val
->value
.link
;
147 if (val
->port_vlan
>= dev
->ports
)
150 if (!dev
->ops
->get_port_link
)
153 memset(link
, 0, sizeof(*link
));
154 return dev
->ops
->get_port_link(dev
, val
->port_vlan
, link
);
158 swconfig_apply_config(struct switch_dev
*dev
, const struct switch_attr
*attr
,
159 struct switch_val
*val
)
161 /* don't complain if not supported by the switch driver */
162 if (!dev
->ops
->apply_config
)
165 return dev
->ops
->apply_config(dev
);
169 swconfig_reset_switch(struct switch_dev
*dev
, const struct switch_attr
*attr
,
170 struct switch_val
*val
)
172 /* don't complain if not supported by the switch driver */
173 if (!dev
->ops
->reset_switch
)
176 return dev
->ops
->reset_switch(dev
);
179 enum global_defaults
{
193 static struct switch_attr default_global
[] = {
195 .type
= SWITCH_TYPE_NOVAL
,
197 .description
= "Activate changes in the hardware",
198 .set
= swconfig_apply_config
,
201 .type
= SWITCH_TYPE_NOVAL
,
203 .description
= "Reset the switch",
204 .set
= swconfig_reset_switch
,
208 static struct switch_attr default_port
[] = {
210 .type
= SWITCH_TYPE_INT
,
212 .description
= "Primary VLAN ID",
213 .set
= swconfig_set_pvid
,
214 .get
= swconfig_get_pvid
,
217 .type
= SWITCH_TYPE_LINK
,
219 .description
= "Get port link information",
220 .set
= swconfig_set_link
,
221 .get
= swconfig_get_link
,
225 static struct switch_attr default_vlan
[] = {
227 .type
= SWITCH_TYPE_PORTS
,
229 .description
= "VLAN port mapping",
230 .set
= swconfig_set_vlan_ports
,
231 .get
= swconfig_get_vlan_ports
,
235 static const struct switch_attr
*
236 swconfig_find_attr_by_name(const struct switch_attrlist
*alist
,
241 for (i
= 0; i
< alist
->n_attr
; i
++)
242 if (strcmp(name
, alist
->attr
[i
].name
) == 0)
243 return &alist
->attr
[i
];
248 static void swconfig_defaults_init(struct switch_dev
*dev
)
250 const struct switch_dev_ops
*ops
= dev
->ops
;
256 if (ops
->get_vlan_ports
|| ops
->set_vlan_ports
)
257 set_bit(VLAN_PORTS
, &dev
->def_vlan
);
259 if (ops
->get_port_pvid
|| ops
->set_port_pvid
)
260 set_bit(PORT_PVID
, &dev
->def_port
);
262 if (ops
->get_port_link
&&
263 !swconfig_find_attr_by_name(&ops
->attr_port
, "link"))
264 set_bit(PORT_LINK
, &dev
->def_port
);
266 /* always present, can be no-op */
267 set_bit(GLOBAL_APPLY
, &dev
->def_global
);
268 set_bit(GLOBAL_RESET
, &dev
->def_global
);
272 static struct genl_family switch_fam
;
274 static const struct nla_policy switch_policy
[SWITCH_ATTR_MAX
+1] = {
275 [SWITCH_ATTR_ID
] = { .type
= NLA_U32
},
276 [SWITCH_ATTR_OP_ID
] = { .type
= NLA_U32
},
277 [SWITCH_ATTR_OP_PORT
] = { .type
= NLA_U32
},
278 [SWITCH_ATTR_OP_VLAN
] = { .type
= NLA_U32
},
279 [SWITCH_ATTR_OP_VALUE_INT
] = { .type
= NLA_U32
},
280 [SWITCH_ATTR_OP_VALUE_STR
] = { .type
= NLA_NUL_STRING
},
281 [SWITCH_ATTR_OP_VALUE_PORTS
] = { .type
= NLA_NESTED
},
282 [SWITCH_ATTR_TYPE
] = { .type
= NLA_U32
},
285 static const struct nla_policy port_policy
[SWITCH_PORT_ATTR_MAX
+1] = {
286 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
287 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
290 static struct nla_policy link_policy
[SWITCH_LINK_ATTR_MAX
] = {
291 [SWITCH_LINK_FLAG_DUPLEX
] = { .type
= NLA_FLAG
},
292 [SWITCH_LINK_FLAG_ANEG
] = { .type
= NLA_FLAG
},
293 [SWITCH_LINK_SPEED
] = { .type
= NLA_U32
},
299 mutex_lock(&swdevs_lock
);
303 swconfig_unlock(void)
305 mutex_unlock(&swdevs_lock
);
308 static struct switch_dev
*
309 swconfig_get_dev(struct genl_info
*info
)
311 struct switch_dev
*dev
= NULL
;
312 struct switch_dev
*p
;
315 if (!info
->attrs
[SWITCH_ATTR_ID
])
318 id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_ID
]);
320 list_for_each_entry(p
, &swdevs
, dev_list
) {
328 mutex_lock(&dev
->sw_mutex
);
330 pr_debug("device %d not found\n", id
);
337 swconfig_put_dev(struct switch_dev
*dev
)
339 mutex_unlock(&dev
->sw_mutex
);
343 swconfig_dump_attr(struct swconfig_callback
*cb
, void *arg
)
345 struct switch_attr
*op
= arg
;
346 struct genl_info
*info
= cb
->info
;
347 struct sk_buff
*msg
= cb
->msg
;
348 int id
= cb
->args
[0];
351 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
352 NLM_F_MULTI
, SWITCH_CMD_NEW_ATTR
);
356 if (nla_put_u32(msg
, SWITCH_ATTR_OP_ID
, id
))
357 goto nla_put_failure
;
358 if (nla_put_u32(msg
, SWITCH_ATTR_OP_TYPE
, op
->type
))
359 goto nla_put_failure
;
360 if (nla_put_string(msg
, SWITCH_ATTR_OP_NAME
, op
->name
))
361 goto nla_put_failure
;
363 if (nla_put_string(msg
, SWITCH_ATTR_OP_DESCRIPTION
,
365 goto nla_put_failure
;
367 genlmsg_end(msg
, hdr
);
370 genlmsg_cancel(msg
, hdr
);
374 /* spread multipart messages across multiple message buffers */
376 swconfig_send_multipart(struct swconfig_callback
*cb
, void *arg
)
378 struct genl_info
*info
= cb
->info
;
384 cb
->msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
389 if (!(cb
->fill(cb
, arg
) < 0))
392 /* fill failed, check if this was already the second attempt */
396 /* try again in a new message, send the current one */
399 if (cb
->close(cb
, arg
) < 0)
402 err
= genlmsg_reply(cb
->msg
, info
);
418 swconfig_list_attrs(struct sk_buff
*skb
, struct genl_info
*info
)
420 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
421 const struct switch_attrlist
*alist
;
422 struct switch_dev
*dev
;
423 struct swconfig_callback cb
;
428 struct switch_attr
*def_list
;
429 unsigned long *def_active
;
432 dev
= swconfig_get_dev(info
);
437 case SWITCH_CMD_LIST_GLOBAL
:
438 alist
= &dev
->ops
->attr_global
;
439 def_list
= default_global
;
440 def_active
= &dev
->def_global
;
441 n_def
= ARRAY_SIZE(default_global
);
443 case SWITCH_CMD_LIST_VLAN
:
444 alist
= &dev
->ops
->attr_vlan
;
445 def_list
= default_vlan
;
446 def_active
= &dev
->def_vlan
;
447 n_def
= ARRAY_SIZE(default_vlan
);
449 case SWITCH_CMD_LIST_PORT
:
450 alist
= &dev
->ops
->attr_port
;
451 def_list
= default_port
;
452 def_active
= &dev
->def_port
;
453 n_def
= ARRAY_SIZE(default_port
);
460 memset(&cb
, 0, sizeof(cb
));
462 cb
.fill
= swconfig_dump_attr
;
463 for (i
= 0; i
< alist
->n_attr
; i
++) {
464 if (alist
->attr
[i
].disabled
)
467 err
= swconfig_send_multipart(&cb
, (void *) &alist
->attr
[i
]);
473 for (i
= 0; i
< n_def
; i
++) {
474 if (!test_bit(i
, def_active
))
476 cb
.args
[0] = SWITCH_ATTR_DEFAULTS_OFFSET
+ i
;
477 err
= swconfig_send_multipart(&cb
, (void *) &def_list
[i
]);
481 swconfig_put_dev(dev
);
486 return genlmsg_reply(cb
.msg
, info
);
492 swconfig_put_dev(dev
);
496 static const struct switch_attr
*
497 swconfig_lookup_attr(struct switch_dev
*dev
, struct genl_info
*info
,
498 struct switch_val
*val
)
500 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
501 const struct switch_attrlist
*alist
;
502 const struct switch_attr
*attr
= NULL
;
503 unsigned int attr_id
;
506 struct switch_attr
*def_list
;
507 unsigned long *def_active
;
510 if (!info
->attrs
[SWITCH_ATTR_OP_ID
])
514 case SWITCH_CMD_SET_GLOBAL
:
515 case SWITCH_CMD_GET_GLOBAL
:
516 alist
= &dev
->ops
->attr_global
;
517 def_list
= default_global
;
518 def_active
= &dev
->def_global
;
519 n_def
= ARRAY_SIZE(default_global
);
521 case SWITCH_CMD_SET_VLAN
:
522 case SWITCH_CMD_GET_VLAN
:
523 alist
= &dev
->ops
->attr_vlan
;
524 def_list
= default_vlan
;
525 def_active
= &dev
->def_vlan
;
526 n_def
= ARRAY_SIZE(default_vlan
);
527 if (!info
->attrs
[SWITCH_ATTR_OP_VLAN
])
529 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VLAN
]);
530 if (val
->port_vlan
>= dev
->vlans
)
533 case SWITCH_CMD_SET_PORT
:
534 case SWITCH_CMD_GET_PORT
:
535 alist
= &dev
->ops
->attr_port
;
536 def_list
= default_port
;
537 def_active
= &dev
->def_port
;
538 n_def
= ARRAY_SIZE(default_port
);
539 if (!info
->attrs
[SWITCH_ATTR_OP_PORT
])
541 val
->port_vlan
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_PORT
]);
542 if (val
->port_vlan
>= dev
->ports
)
553 attr_id
= nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_ID
]);
554 if (attr_id
>= SWITCH_ATTR_DEFAULTS_OFFSET
) {
555 attr_id
-= SWITCH_ATTR_DEFAULTS_OFFSET
;
556 if (attr_id
>= n_def
)
558 if (!test_bit(attr_id
, def_active
))
560 attr
= &def_list
[attr_id
];
562 if (attr_id
>= alist
->n_attr
)
564 attr
= &alist
->attr
[attr_id
];
572 pr_debug("attribute lookup failed\n");
578 swconfig_parse_ports(struct sk_buff
*msg
, struct nlattr
*head
,
579 struct switch_val
*val
, int max
)
585 nla_for_each_nested(nla
, head
, rem
) {
586 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
587 struct switch_port
*port
;
592 port
= &val
->value
.ports
[val
->len
];
594 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
595 if (nla_parse_nested_deprecated(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
597 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0)
598 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
601 if (nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, nla
,
606 if (!tb
[SWITCH_PORT_ID
])
609 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
610 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
611 port
->flags
|= (1 << SWITCH_PORT_FLAG_TAGGED
);
619 swconfig_parse_link(struct sk_buff
*msg
, struct nlattr
*nla
,
620 struct switch_port_link
*link
)
622 struct nlattr
*tb
[SWITCH_LINK_ATTR_MAX
+ 1];
624 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
625 if (nla_parse_nested_deprecated(tb
, SWITCH_LINK_ATTR_MAX
, nla
, link_policy
, NULL
))
626 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(4,12,0)
627 if (nla_parse_nested(tb
, SWITCH_LINK_ATTR_MAX
, nla
, link_policy
, NULL
))
629 if (nla_parse_nested(tb
, SWITCH_LINK_ATTR_MAX
, nla
, link_policy
))
633 link
->duplex
= !!tb
[SWITCH_LINK_FLAG_DUPLEX
];
634 link
->aneg
= !!tb
[SWITCH_LINK_FLAG_ANEG
];
635 link
->speed
= nla_get_u32(tb
[SWITCH_LINK_SPEED
]);
641 swconfig_set_attr(struct sk_buff
*skb
, struct genl_info
*info
)
643 const struct switch_attr
*attr
;
644 struct switch_dev
*dev
;
645 struct switch_val val
;
648 if (!capable(CAP_NET_ADMIN
))
651 dev
= swconfig_get_dev(info
);
655 memset(&val
, 0, sizeof(val
));
656 attr
= swconfig_lookup_attr(dev
, info
, &val
);
657 if (!attr
|| !attr
->set
)
661 switch (attr
->type
) {
662 case SWITCH_TYPE_NOVAL
:
664 case SWITCH_TYPE_INT
:
665 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
])
668 nla_get_u32(info
->attrs
[SWITCH_ATTR_OP_VALUE_INT
]);
670 case SWITCH_TYPE_STRING
:
671 if (!info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
])
674 nla_data(info
->attrs
[SWITCH_ATTR_OP_VALUE_STR
]);
676 case SWITCH_TYPE_PORTS
:
677 val
.value
.ports
= dev
->portbuf
;
678 memset(dev
->portbuf
, 0,
679 sizeof(struct switch_port
) * dev
->ports
);
681 /* TODO: implement multipart? */
682 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
]) {
683 err
= swconfig_parse_ports(skb
,
684 info
->attrs
[SWITCH_ATTR_OP_VALUE_PORTS
],
693 case SWITCH_TYPE_LINK
:
694 val
.value
.link
= &dev
->linkbuf
;
695 memset(&dev
->linkbuf
, 0, sizeof(struct switch_port_link
));
697 if (info
->attrs
[SWITCH_ATTR_OP_VALUE_LINK
]) {
698 err
= swconfig_parse_link(skb
,
699 info
->attrs
[SWITCH_ATTR_OP_VALUE_LINK
],
712 err
= attr
->set(dev
, attr
, &val
);
714 swconfig_put_dev(dev
);
719 swconfig_close_portlist(struct swconfig_callback
*cb
, void *arg
)
722 nla_nest_end(cb
->msg
, cb
->nest
[0]);
727 swconfig_send_port(struct swconfig_callback
*cb
, void *arg
)
729 const struct switch_port
*port
= arg
;
730 struct nlattr
*p
= NULL
;
733 cb
->nest
[0] = nla_nest_start(cb
->msg
, cb
->cmd
);
738 p
= nla_nest_start(cb
->msg
, SWITCH_ATTR_PORT
);
742 if (nla_put_u32(cb
->msg
, SWITCH_PORT_ID
, port
->id
))
743 goto nla_put_failure
;
744 if (port
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)) {
745 if (nla_put_flag(cb
->msg
, SWITCH_PORT_FLAG_TAGGED
))
746 goto nla_put_failure
;
749 nla_nest_end(cb
->msg
, p
);
753 nla_nest_cancel(cb
->msg
, p
);
755 nla_nest_cancel(cb
->msg
, cb
->nest
[0]);
760 swconfig_send_ports(struct sk_buff
**msg
, struct genl_info
*info
, int attr
,
761 const struct switch_val
*val
)
763 struct swconfig_callback cb
;
767 if (!val
->value
.ports
)
770 memset(&cb
, 0, sizeof(cb
));
774 cb
.fill
= swconfig_send_port
;
775 cb
.close
= swconfig_close_portlist
;
777 cb
.nest
[0] = nla_nest_start(cb
.msg
, cb
.cmd
);
778 for (i
= 0; i
< val
->len
; i
++) {
779 err
= swconfig_send_multipart(&cb
, &val
->value
.ports
[i
]);
784 swconfig_close_portlist(&cb
, NULL
);
792 swconfig_send_link(struct sk_buff
*msg
, struct genl_info
*info
, int attr
,
793 const struct switch_port_link
*link
)
795 struct nlattr
*p
= NULL
;
798 p
= nla_nest_start(msg
, attr
);
800 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_LINK
))
801 goto nla_put_failure
;
804 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_DUPLEX
))
805 goto nla_put_failure
;
808 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_ANEG
))
809 goto nla_put_failure
;
812 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_TX_FLOW
))
813 goto nla_put_failure
;
816 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_RX_FLOW
))
817 goto nla_put_failure
;
819 if (nla_put_u32(msg
, SWITCH_LINK_SPEED
, link
->speed
))
820 goto nla_put_failure
;
821 if (link
->eee
& ADVERTISED_100baseT_Full
) {
822 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_EEE_100BASET
))
823 goto nla_put_failure
;
825 if (link
->eee
& ADVERTISED_1000baseT_Full
) {
826 if (nla_put_flag(msg
, SWITCH_LINK_FLAG_EEE_1000BASET
))
827 goto nla_put_failure
;
829 nla_nest_end(msg
, p
);
834 nla_nest_cancel(msg
, p
);
839 swconfig_get_attr(struct sk_buff
*skb
, struct genl_info
*info
)
841 struct genlmsghdr
*hdr
= nlmsg_data(info
->nlhdr
);
842 const struct switch_attr
*attr
;
843 struct switch_dev
*dev
;
844 struct sk_buff
*msg
= NULL
;
845 struct switch_val val
;
849 dev
= swconfig_get_dev(info
);
853 memset(&val
, 0, sizeof(val
));
854 attr
= swconfig_lookup_attr(dev
, info
, &val
);
855 if (!attr
|| !attr
->get
)
858 if (attr
->type
== SWITCH_TYPE_PORTS
) {
859 val
.value
.ports
= dev
->portbuf
;
860 memset(dev
->portbuf
, 0,
861 sizeof(struct switch_port
) * dev
->ports
);
862 } else if (attr
->type
== SWITCH_TYPE_LINK
) {
863 val
.value
.link
= &dev
->linkbuf
;
864 memset(&dev
->linkbuf
, 0, sizeof(struct switch_port_link
));
867 err
= attr
->get(dev
, attr
, &val
);
871 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
875 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &switch_fam
,
878 goto nla_put_failure
;
880 switch (attr
->type
) {
881 case SWITCH_TYPE_INT
:
882 if (nla_put_u32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
.value
.i
))
883 goto nla_put_failure
;
885 case SWITCH_TYPE_STRING
:
886 if (nla_put_string(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
.value
.s
))
887 goto nla_put_failure
;
889 case SWITCH_TYPE_PORTS
:
890 err
= swconfig_send_ports(&msg
, info
,
891 SWITCH_ATTR_OP_VALUE_PORTS
, &val
);
893 goto nla_put_failure
;
895 case SWITCH_TYPE_LINK
:
896 err
= swconfig_send_link(msg
, info
,
897 SWITCH_ATTR_OP_VALUE_LINK
, val
.value
.link
);
899 goto nla_put_failure
;
902 pr_debug("invalid type in attribute\n");
904 goto nla_put_failure
;
906 genlmsg_end(msg
, hdr
);
909 goto nla_put_failure
;
911 swconfig_put_dev(dev
);
912 return genlmsg_reply(msg
, info
);
918 swconfig_put_dev(dev
);
925 swconfig_send_switch(struct sk_buff
*msg
, u32 pid
, u32 seq
, int flags
,
926 const struct switch_dev
*dev
)
928 struct nlattr
*p
= NULL
, *m
= NULL
;
932 hdr
= genlmsg_put(msg
, pid
, seq
, &switch_fam
, flags
,
933 SWITCH_CMD_NEW_ATTR
);
937 if (nla_put_u32(msg
, SWITCH_ATTR_ID
, dev
->id
))
938 goto nla_put_failure
;
939 if (nla_put_string(msg
, SWITCH_ATTR_DEV_NAME
, dev
->devname
))
940 goto nla_put_failure
;
941 if (nla_put_string(msg
, SWITCH_ATTR_ALIAS
, dev
->alias
))
942 goto nla_put_failure
;
943 if (nla_put_string(msg
, SWITCH_ATTR_NAME
, dev
->name
))
944 goto nla_put_failure
;
945 if (nla_put_u32(msg
, SWITCH_ATTR_VLANS
, dev
->vlans
))
946 goto nla_put_failure
;
947 if (nla_put_u32(msg
, SWITCH_ATTR_PORTS
, dev
->ports
))
948 goto nla_put_failure
;
949 if (nla_put_u32(msg
, SWITCH_ATTR_CPU_PORT
, dev
->cpu_port
))
950 goto nla_put_failure
;
952 m
= nla_nest_start(msg
, SWITCH_ATTR_PORTMAP
);
954 goto nla_put_failure
;
955 for (i
= 0; i
< dev
->ports
; i
++) {
956 p
= nla_nest_start(msg
, SWITCH_ATTR_PORTS
);
959 if (dev
->portmap
[i
].s
) {
960 if (nla_put_string(msg
, SWITCH_PORTMAP_SEGMENT
,
962 goto nla_put_failure
;
963 if (nla_put_u32(msg
, SWITCH_PORTMAP_VIRT
,
964 dev
->portmap
[i
].virt
))
965 goto nla_put_failure
;
967 nla_nest_end(msg
, p
);
969 nla_nest_end(msg
, m
);
970 genlmsg_end(msg
, hdr
);
973 genlmsg_cancel(msg
, hdr
);
977 static int swconfig_dump_switches(struct sk_buff
*skb
,
978 struct netlink_callback
*cb
)
980 struct switch_dev
*dev
;
981 int start
= cb
->args
[0];
985 list_for_each_entry(dev
, &swdevs
, dev_list
) {
988 if (swconfig_send_switch(skb
, NETLINK_CB(cb
->skb
).portid
,
989 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
1000 swconfig_done(struct netlink_callback
*cb
)
1005 static struct genl_ops swconfig_ops
[] = {
1007 .cmd
= SWITCH_CMD_LIST_GLOBAL
,
1008 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1009 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1011 .doit
= swconfig_list_attrs
,
1012 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1013 .policy
= switch_policy
,
1017 .cmd
= SWITCH_CMD_LIST_VLAN
,
1018 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1019 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1021 .doit
= swconfig_list_attrs
,
1022 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1023 .policy
= switch_policy
,
1027 .cmd
= SWITCH_CMD_LIST_PORT
,
1028 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1029 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1031 .doit
= swconfig_list_attrs
,
1032 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1033 .policy
= switch_policy
,
1037 .cmd
= SWITCH_CMD_GET_GLOBAL
,
1038 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1039 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1041 .doit
= swconfig_get_attr
,
1042 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1043 .policy
= switch_policy
,
1047 .cmd
= SWITCH_CMD_GET_VLAN
,
1048 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1049 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1051 .doit
= swconfig_get_attr
,
1052 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1053 .policy
= switch_policy
,
1057 .cmd
= SWITCH_CMD_GET_PORT
,
1058 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1059 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1061 .doit
= swconfig_get_attr
,
1062 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1063 .policy
= switch_policy
,
1067 .cmd
= SWITCH_CMD_SET_GLOBAL
,
1068 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1069 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1071 .flags
= GENL_ADMIN_PERM
,
1072 .doit
= swconfig_set_attr
,
1073 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1074 .policy
= switch_policy
,
1078 .cmd
= SWITCH_CMD_SET_VLAN
,
1079 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1080 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1082 .flags
= GENL_ADMIN_PERM
,
1083 .doit
= swconfig_set_attr
,
1084 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1085 .policy
= switch_policy
,
1089 .cmd
= SWITCH_CMD_SET_PORT
,
1090 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1091 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1093 .flags
= GENL_ADMIN_PERM
,
1094 .doit
= swconfig_set_attr
,
1095 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1096 .policy
= switch_policy
,
1100 .cmd
= SWITCH_CMD_GET_SWITCH
,
1101 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1102 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
1104 .dumpit
= swconfig_dump_switches
,
1105 #if LINUX_VERSION_CODE < KERNEL_VERSION(5,2,0)
1106 .policy
= switch_policy
,
1108 .done
= swconfig_done
,
1112 static struct genl_family switch_fam
= {
1113 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0)
1114 .id
= GENL_ID_GENERATE
,
1119 .maxattr
= SWITCH_ATTR_MAX
,
1120 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,2,0)
1121 .policy
= switch_policy
,
1123 .module
= THIS_MODULE
,
1124 .ops
= swconfig_ops
,
1125 .n_ops
= ARRAY_SIZE(swconfig_ops
),
1130 of_switch_load_portmap(struct switch_dev
*dev
)
1132 struct device_node
*port
;
1137 for_each_child_of_node(dev
->of_node
, port
) {
1139 const char *segment
;
1142 if (!of_device_is_compatible(port
, "swconfig,port"))
1145 if (of_property_read_string(port
, "swconfig,segment", &segment
))
1148 prop
= of_get_property(port
, "swconfig,portmap", &size
);
1152 if (size
!= (2 * sizeof(*prop
))) {
1153 pr_err("%s: failed to parse port mapping\n",
1158 phys
= be32_to_cpup(prop
++);
1159 if ((phys
< 0) | (phys
>= dev
->ports
)) {
1160 pr_err("%s: physical port index out of range\n",
1165 dev
->portmap
[phys
].s
= kstrdup(segment
, GFP_KERNEL
);
1166 dev
->portmap
[phys
].virt
= be32_to_cpup(prop
);
1167 pr_debug("Found port: %s, physical: %d, virtual: %d\n",
1168 segment
, phys
, dev
->portmap
[phys
].virt
);
1174 register_switch(struct switch_dev
*dev
, struct net_device
*netdev
)
1176 struct switch_dev
*sdev
;
1177 const int max_switches
= 8 * sizeof(unsigned long);
1178 unsigned long in_use
= 0;
1182 INIT_LIST_HEAD(&dev
->dev_list
);
1184 dev
->netdev
= netdev
;
1186 dev
->alias
= netdev
->name
;
1188 BUG_ON(!dev
->alias
);
1190 /* Make sure swdev_id doesn't overflow */
1191 if (swdev_id
== INT_MAX
) {
1195 if (dev
->ports
> 0) {
1196 dev
->portbuf
= kzalloc(sizeof(struct switch_port
) *
1197 dev
->ports
, GFP_KERNEL
);
1200 dev
->portmap
= kzalloc(sizeof(struct switch_portmap
) *
1201 dev
->ports
, GFP_KERNEL
);
1202 if (!dev
->portmap
) {
1203 kfree(dev
->portbuf
);
1207 swconfig_defaults_init(dev
);
1208 mutex_init(&dev
->sw_mutex
);
1210 dev
->id
= ++swdev_id
;
1212 list_for_each_entry(sdev
, &swdevs
, dev_list
) {
1213 if (!sscanf(sdev
->devname
, SWCONFIG_DEVNAME
, &i
))
1215 if (i
< 0 || i
> max_switches
)
1218 set_bit(i
, &in_use
);
1220 i
= find_first_zero_bit(&in_use
, max_switches
);
1222 if (i
== max_switches
) {
1229 of_switch_load_portmap(dev
);
1232 /* fill device name */
1233 snprintf(dev
->devname
, IFNAMSIZ
, SWCONFIG_DEVNAME
, i
);
1235 list_add_tail(&dev
->dev_list
, &swdevs
);
1238 err
= swconfig_create_led_trigger(dev
);
1244 EXPORT_SYMBOL_GPL(register_switch
);
1247 unregister_switch(struct switch_dev
*dev
)
1249 swconfig_destroy_led_trigger(dev
);
1250 kfree(dev
->portbuf
);
1251 mutex_lock(&dev
->sw_mutex
);
1253 list_del(&dev
->dev_list
);
1255 mutex_unlock(&dev
->sw_mutex
);
1257 EXPORT_SYMBOL_GPL(unregister_switch
);
1260 switch_generic_set_link(struct switch_dev
*dev
, int port
,
1261 struct switch_port_link
*link
)
1263 if (WARN_ON(!dev
->ops
->phy_write16
))
1266 /* Generic implementation */
1268 dev
->ops
->phy_write16(dev
, port
, MII_BMCR
, 0x0000);
1269 dev
->ops
->phy_write16(dev
, port
, MII_BMCR
, BMCR_ANENABLE
| BMCR_ANRESTART
);
1274 bmcr
|= BMCR_FULLDPLX
;
1276 switch (link
->speed
) {
1277 case SWITCH_PORT_SPEED_10
:
1279 case SWITCH_PORT_SPEED_100
:
1280 bmcr
|= BMCR_SPEED100
;
1282 case SWITCH_PORT_SPEED_1000
:
1283 bmcr
|= BMCR_SPEED1000
;
1289 dev
->ops
->phy_write16(dev
, port
, MII_BMCR
, bmcr
);
1298 INIT_LIST_HEAD(&swdevs
);
1300 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0)
1301 return genl_register_family_with_ops(&switch_fam
, swconfig_ops
);
1303 return genl_register_family(&switch_fam
);
1310 genl_unregister_family(&switch_fam
);
1313 module_init(swconfig_init
);
1314 module_exit(swconfig_exit
);