2 * swlib.c: Switch configuration API (user space part)
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 Lesser General Public License
8 * version 2.1 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <linux/switch.h>
27 #include <netlink/netlink.h>
28 #include <netlink/genl/genl.h>
29 #include <netlink/genl/family.h>
33 #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
35 #define DPRINTF(fmt, ...) do {} while (0)
38 static struct nl_sock
*handle
;
39 static struct nl_cache
*cache
;
40 static struct genl_family
*family
;
41 static struct nlattr
*tb
[SWITCH_ATTR_MAX
+ 1];
42 static int refcount
= 0;
44 static struct nla_policy port_policy
[SWITCH_ATTR_MAX
] = {
45 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
46 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
49 static struct nla_policy portmap_policy
[SWITCH_PORTMAP_MAX
] = {
50 [SWITCH_PORTMAP_SEGMENT
] = { .type
= NLA_STRING
},
51 [SWITCH_PORTMAP_VIRT
] = { .type
= NLA_U32
},
54 static struct nla_policy link_policy
[SWITCH_LINK_ATTR_MAX
] = {
55 [SWITCH_LINK_FLAG_LINK
] = { .type
= NLA_FLAG
},
56 [SWITCH_LINK_FLAG_DUPLEX
] = { .type
= NLA_FLAG
},
57 [SWITCH_LINK_FLAG_ANEG
] = { .type
= NLA_FLAG
},
58 [SWITCH_LINK_SPEED
] = { .type
= NLA_U32
},
59 [SWITCH_LINK_FLAG_EEE_100BASET
] = { .type
= NLA_FLAG
},
60 [SWITCH_LINK_FLAG_EEE_1000BASET
] = { .type
= NLA_FLAG
},
64 swlib_alloc(size_t size
)
78 wait_handler(struct nl_msg
*msg
, void *arg
)
86 /* helper function for performing netlink requests */
88 swlib_call(int cmd
, int (*call
)(struct nl_msg
*, void *),
89 int (*data
)(struct nl_msg
*, void *), void *arg
)
92 struct nl_cb
*cb
= NULL
;
99 fprintf(stderr
, "Out of memory!\n");
106 genlmsg_put(msg
, NL_AUTO_PID
, NL_AUTO_SEQ
, genl_family_get_id(family
), 0, flags
, cmd
, 0);
108 if (data(msg
, arg
) < 0)
109 goto nla_put_failure
;
112 cb
= nl_cb_alloc(NL_CB_CUSTOM
);
114 fprintf(stderr
, "nl_cb_alloc failed.\n");
118 err
= nl_send_auto_complete(handle
, msg
);
120 fprintf(stderr
, "nl_send_auto_complete failed: %d\n", err
);
127 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, call
, arg
);
130 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, wait_handler
, &finished
);
132 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, wait_handler
, &finished
);
134 err
= nl_recvmsgs(handle
, cb
);
140 err
= nl_wait_for_ack(handle
);
151 send_attr(struct nl_msg
*msg
, void *arg
)
153 struct switch_val
*val
= arg
;
154 struct switch_attr
*attr
= val
->attr
;
156 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, attr
->dev
->id
);
157 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_ID
, attr
->id
);
158 switch(attr
->atype
) {
159 case SWLIB_ATTR_GROUP_PORT
:
160 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_PORT
, val
->port_vlan
);
162 case SWLIB_ATTR_GROUP_VLAN
:
163 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VLAN
, val
->port_vlan
);
176 store_port_val(struct nl_msg
*msg
, struct nlattr
*nla
, struct switch_val
*val
)
179 int ports
= val
->attr
->dev
->ports
;
183 if (!val
->value
.ports
)
184 val
->value
.ports
= malloc(sizeof(struct switch_port
) * ports
);
186 nla_for_each_nested(p
, nla
, remaining
) {
187 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
188 struct switch_port
*port
;
190 if (val
->len
>= ports
)
193 err
= nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, p
, port_policy
);
197 if (!tb
[SWITCH_PORT_ID
])
200 port
= &val
->value
.ports
[val
->len
];
201 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
203 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
204 port
->flags
|= SWLIB_PORT_FLAG_TAGGED
;
214 store_link_val(struct nl_msg
*msg
, struct nlattr
*nla
, struct switch_val
*val
)
216 struct nlattr
*tb
[SWITCH_LINK_ATTR_MAX
+ 1];
217 struct switch_port_link
*link
;
220 if (!val
->value
.link
)
221 val
->value
.link
= malloc(sizeof(struct switch_port_link
));
223 err
= nla_parse_nested(tb
, SWITCH_LINK_ATTR_MAX
, nla
, link_policy
);
227 link
= val
->value
.link
;
228 link
->link
= !!tb
[SWITCH_LINK_FLAG_LINK
];
229 link
->duplex
= !!tb
[SWITCH_LINK_FLAG_DUPLEX
];
230 link
->aneg
= !!tb
[SWITCH_LINK_FLAG_ANEG
];
231 link
->tx_flow
= !!tb
[SWITCH_LINK_FLAG_TX_FLOW
];
232 link
->rx_flow
= !!tb
[SWITCH_LINK_FLAG_RX_FLOW
];
233 link
->speed
= nla_get_u32(tb
[SWITCH_LINK_SPEED
]);
235 if (tb
[SWITCH_LINK_FLAG_EEE_100BASET
])
236 link
->eee
|= SWLIB_LINK_FLAG_EEE_100BASET
;
237 if (tb
[SWITCH_LINK_FLAG_EEE_1000BASET
])
238 link
->eee
|= SWLIB_LINK_FLAG_EEE_1000BASET
;
245 store_val(struct nl_msg
*msg
, void *arg
)
247 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
248 struct switch_val
*val
= arg
;
253 if (nla_parse(tb
, SWITCH_ATTR_MAX
- 1, genlmsg_attrdata(gnlh
, 0),
254 genlmsg_attrlen(gnlh
, 0), NULL
) < 0) {
258 if (tb
[SWITCH_ATTR_OP_VALUE_INT
])
259 val
->value
.i
= nla_get_u32(tb
[SWITCH_ATTR_OP_VALUE_INT
]);
260 else if (tb
[SWITCH_ATTR_OP_VALUE_STR
])
261 val
->value
.s
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_VALUE_STR
]));
262 else if (tb
[SWITCH_ATTR_OP_VALUE_PORTS
])
263 val
->err
= store_port_val(msg
, tb
[SWITCH_ATTR_OP_VALUE_PORTS
], val
);
264 else if (tb
[SWITCH_ATTR_OP_VALUE_LINK
])
265 val
->err
= store_link_val(msg
, tb
[SWITCH_ATTR_OP_VALUE_LINK
], val
);
275 swlib_get_attr(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
280 switch(attr
->atype
) {
281 case SWLIB_ATTR_GROUP_GLOBAL
:
282 cmd
= SWITCH_CMD_GET_GLOBAL
;
284 case SWLIB_ATTR_GROUP_PORT
:
285 cmd
= SWITCH_CMD_GET_PORT
;
287 case SWLIB_ATTR_GROUP_VLAN
:
288 cmd
= SWITCH_CMD_GET_VLAN
;
294 memset(&val
->value
, 0, sizeof(val
->value
));
298 err
= swlib_call(cmd
, store_val
, send_attr
, val
);
306 send_attr_ports(struct nl_msg
*msg
, struct switch_val
*val
)
311 /* TODO implement multipart? */
314 n
= nla_nest_start(msg
, SWITCH_ATTR_OP_VALUE_PORTS
);
316 goto nla_put_failure
;
317 for (i
= 0; i
< val
->len
; i
++) {
318 struct switch_port
*port
= &val
->value
.ports
[i
];
321 np
= nla_nest_start(msg
, SWITCH_ATTR_PORT
);
323 goto nla_put_failure
;
325 NLA_PUT_U32(msg
, SWITCH_PORT_ID
, port
->id
);
326 if (port
->flags
& SWLIB_PORT_FLAG_TAGGED
)
327 NLA_PUT_FLAG(msg
, SWITCH_PORT_FLAG_TAGGED
);
329 nla_nest_end(msg
, np
);
331 nla_nest_end(msg
, n
);
340 send_attr_val(struct nl_msg
*msg
, void *arg
)
342 struct switch_val
*val
= arg
;
343 struct switch_attr
*attr
= val
->attr
;
345 if (send_attr(msg
, arg
))
346 goto nla_put_failure
;
349 case SWITCH_TYPE_NOVAL
:
351 case SWITCH_TYPE_INT
:
352 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
->value
.i
);
354 case SWITCH_TYPE_STRING
:
356 goto nla_put_failure
;
357 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
->value
.s
);
359 case SWITCH_TYPE_PORTS
:
360 if (send_attr_ports(msg
, val
) < 0)
361 goto nla_put_failure
;
364 goto nla_put_failure
;
373 swlib_set_attr(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
377 switch(attr
->atype
) {
378 case SWLIB_ATTR_GROUP_GLOBAL
:
379 cmd
= SWITCH_CMD_SET_GLOBAL
;
381 case SWLIB_ATTR_GROUP_PORT
:
382 cmd
= SWITCH_CMD_SET_PORT
;
384 case SWLIB_ATTR_GROUP_VLAN
:
385 cmd
= SWITCH_CMD_SET_VLAN
;
392 return swlib_call(cmd
, NULL
, send_attr_val
, val
);
395 int swlib_set_attr_string(struct switch_dev
*dev
, struct switch_attr
*a
, int port_vlan
, const char *str
)
397 struct switch_port
*ports
;
398 struct switch_val val
;
401 memset(&val
, 0, sizeof(val
));
402 val
.port_vlan
= port_vlan
;
404 case SWITCH_TYPE_INT
:
405 val
.value
.i
= atoi(str
);
407 case SWITCH_TYPE_STRING
:
408 val
.value
.s
= (char *)str
;
410 case SWITCH_TYPE_PORTS
:
411 ports
= alloca(sizeof(struct switch_port
) * dev
->ports
);
412 memset(ports
, 0, sizeof(struct switch_port
) * dev
->ports
);
417 while(*ptr
&& isspace(*ptr
))
426 if (val
.len
>= dev
->ports
)
429 ports
[val
.len
].flags
= 0;
430 ports
[val
.len
].id
= strtoul(ptr
, &ptr
, 10);
431 while(*ptr
&& !isspace(*ptr
)) {
433 ports
[val
.len
].flags
|= SWLIB_PORT_FLAG_TAGGED
;
443 val
.value
.ports
= ports
;
445 case SWITCH_TYPE_NOVAL
:
446 if (str
&& !strcmp(str
, "0"))
453 return swlib_set_attr(dev
, a
, &val
);
457 struct attrlist_arg
{
460 struct switch_dev
*dev
;
461 struct switch_attr
*prev
;
462 struct switch_attr
**head
;
466 add_id(struct nl_msg
*msg
, void *arg
)
468 struct attrlist_arg
*l
= arg
;
470 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, l
->id
);
478 add_attr(struct nl_msg
*msg
, void *ptr
)
480 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
481 struct attrlist_arg
*arg
= ptr
;
482 struct switch_attr
*new;
484 if (nla_parse(tb
, SWITCH_ATTR_MAX
- 1, genlmsg_attrdata(gnlh
, 0),
485 genlmsg_attrlen(gnlh
, 0), NULL
) < 0)
488 new = swlib_alloc(sizeof(struct switch_attr
));
493 new->atype
= arg
->atype
;
495 arg
->prev
->next
= new;
497 arg
->prev
= *arg
->head
;
500 arg
->head
= &new->next
;
502 if (tb
[SWITCH_ATTR_OP_ID
])
503 new->id
= nla_get_u32(tb
[SWITCH_ATTR_OP_ID
]);
504 if (tb
[SWITCH_ATTR_OP_TYPE
])
505 new->type
= nla_get_u32(tb
[SWITCH_ATTR_OP_TYPE
]);
506 if (tb
[SWITCH_ATTR_OP_NAME
])
507 new->name
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_NAME
]));
508 if (tb
[SWITCH_ATTR_OP_DESCRIPTION
])
509 new->description
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_DESCRIPTION
]));
516 swlib_scan(struct switch_dev
*dev
)
518 struct attrlist_arg arg
;
520 if (dev
->ops
|| dev
->port_ops
|| dev
->vlan_ops
)
523 arg
.atype
= SWLIB_ATTR_GROUP_GLOBAL
;
527 arg
.head
= &dev
->ops
;
528 swlib_call(SWITCH_CMD_LIST_GLOBAL
, add_attr
, add_id
, &arg
);
530 arg
.atype
= SWLIB_ATTR_GROUP_PORT
;
532 arg
.head
= &dev
->port_ops
;
533 swlib_call(SWITCH_CMD_LIST_PORT
, add_attr
, add_id
, &arg
);
535 arg
.atype
= SWLIB_ATTR_GROUP_VLAN
;
537 arg
.head
= &dev
->vlan_ops
;
538 swlib_call(SWITCH_CMD_LIST_VLAN
, add_attr
, add_id
, &arg
);
543 struct switch_attr
*swlib_lookup_attr(struct switch_dev
*dev
,
544 enum swlib_attr_group atype
, const char *name
)
546 struct switch_attr
*head
;
552 case SWLIB_ATTR_GROUP_GLOBAL
:
555 case SWLIB_ATTR_GROUP_PORT
:
556 head
= dev
->port_ops
;
558 case SWLIB_ATTR_GROUP_VLAN
:
559 head
= dev
->vlan_ops
;
563 if (!strcmp(name
, head
->name
))
572 swlib_priv_free(void)
575 nl_object_put((struct nl_object
*)family
);
577 nl_cache_free(cache
);
579 nl_socket_free(handle
);
586 swlib_priv_init(void)
590 handle
= nl_socket_alloc();
592 DPRINTF("Failed to create handle\n");
596 if (genl_connect(handle
)) {
597 DPRINTF("Failed to connect to generic netlink\n");
601 ret
= genl_ctrl_alloc_cache(handle
, &cache
);
603 DPRINTF("Failed to allocate netlink cache\n");
607 family
= genl_ctrl_search_by_name(cache
, "switch");
609 DPRINTF("Switch API not present\n");
619 struct swlib_scan_arg
{
621 struct switch_dev
*head
;
622 struct switch_dev
*ptr
;
626 add_port_map(struct switch_dev
*dev
, struct nlattr
*nla
)
629 int err
= 0, idx
= 0;
632 dev
->maps
= malloc(sizeof(struct switch_portmap
) * dev
->ports
);
635 memset(dev
->maps
, 0, sizeof(struct switch_portmap
) * dev
->ports
);
637 nla_for_each_nested(p
, nla
, remaining
) {
638 struct nlattr
*tb
[SWITCH_PORTMAP_MAX
+1];
640 if (idx
>= dev
->ports
)
643 err
= nla_parse_nested(tb
, SWITCH_PORTMAP_MAX
, p
, portmap_policy
);
648 if (tb
[SWITCH_PORTMAP_SEGMENT
] && tb
[SWITCH_PORTMAP_VIRT
]) {
649 dev
->maps
[idx
].segment
= strdup(nla_get_string(tb
[SWITCH_PORTMAP_SEGMENT
]));
650 dev
->maps
[idx
].virt
= nla_get_u32(tb
[SWITCH_PORTMAP_VIRT
]);
661 add_switch(struct nl_msg
*msg
, void *arg
)
663 struct swlib_scan_arg
*sa
= arg
;
664 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
665 struct switch_dev
*dev
;
669 if (nla_parse(tb
, SWITCH_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0), genlmsg_attrlen(gnlh
, 0), NULL
) < 0)
672 if (!tb
[SWITCH_ATTR_DEV_NAME
])
675 name
= nla_get_string(tb
[SWITCH_ATTR_DEV_NAME
]);
676 alias
= nla_get_string(tb
[SWITCH_ATTR_ALIAS
]);
678 if (sa
->name
&& (strcmp(name
, sa
->name
) != 0) && (strcmp(alias
, sa
->name
) != 0))
681 dev
= swlib_alloc(sizeof(struct switch_dev
));
685 strncpy(dev
->dev_name
, name
, IFNAMSIZ
- 1);
686 dev
->alias
= strdup(alias
);
687 if (tb
[SWITCH_ATTR_ID
])
688 dev
->id
= nla_get_u32(tb
[SWITCH_ATTR_ID
]);
689 if (tb
[SWITCH_ATTR_NAME
])
690 dev
->name
= strdup(nla_get_string(tb
[SWITCH_ATTR_NAME
]));
691 if (tb
[SWITCH_ATTR_PORTS
])
692 dev
->ports
= nla_get_u32(tb
[SWITCH_ATTR_PORTS
]);
693 if (tb
[SWITCH_ATTR_VLANS
])
694 dev
->vlans
= nla_get_u32(tb
[SWITCH_ATTR_VLANS
]);
695 if (tb
[SWITCH_ATTR_CPU_PORT
])
696 dev
->cpu_port
= nla_get_u32(tb
[SWITCH_ATTR_CPU_PORT
]);
697 if (tb
[SWITCH_ATTR_PORTMAP
])
698 add_port_map(dev
, tb
[SWITCH_ATTR_PORTMAP
]);
714 list_switch(struct nl_msg
*msg
, void *arg
)
716 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
718 if (nla_parse(tb
, SWITCH_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0), genlmsg_attrlen(gnlh
, 0), NULL
) < 0)
721 if (!tb
[SWITCH_ATTR_DEV_NAME
] || !tb
[SWITCH_ATTR_NAME
])
724 printf("Found: %s - %s\n", nla_get_string(tb
[SWITCH_ATTR_DEV_NAME
]),
725 nla_get_string(tb
[SWITCH_ATTR_ALIAS
]));
734 if (swlib_priv_init() < 0)
736 swlib_call(SWITCH_CMD_GET_SWITCH
, list_switch
, NULL
, NULL
);
741 swlib_print_portmap(struct switch_dev
*dev
, char *segment
)
746 if (!strcmp(segment
, "cpu")) {
747 printf("%d ", dev
->cpu_port
);
748 } else if (!strcmp(segment
, "disabled")) {
749 for (i
= 0; i
< dev
->ports
; i
++)
750 if (!dev
->maps
[i
].segment
)
752 } else for (i
= 0; i
< dev
->ports
; i
++) {
753 if (dev
->maps
[i
].segment
&& !strcmp(dev
->maps
[i
].segment
, segment
))
757 printf("%s - %s\n", dev
->dev_name
, dev
->name
);
758 for (i
= 0; i
< dev
->ports
; i
++)
759 if (i
== dev
->cpu_port
)
760 printf("port%d:\tcpu\n", i
);
761 else if (dev
->maps
[i
].segment
)
762 printf("port%d:\t%s.%d\n", i
, dev
->maps
[i
].segment
, dev
->maps
[i
].virt
);
764 printf("port%d:\tdisabled\n", i
);
769 swlib_connect(const char *name
)
771 struct swlib_scan_arg arg
;
774 if (swlib_priv_init() < 0)
781 swlib_call(SWITCH_CMD_GET_SWITCH
, add_switch
, NULL
, &arg
);
790 swlib_free_attributes(struct switch_attr
**head
)
792 struct switch_attr
*a
= *head
;
793 struct switch_attr
*next
;
798 free(a
->description
);
806 swlib_free_port_map(struct switch_dev
*dev
)
810 if (!dev
|| !dev
->maps
)
813 for (i
= 0; i
< dev
->ports
; i
++)
814 free(dev
->maps
[i
].segment
);
819 swlib_free(struct switch_dev
*dev
)
821 swlib_free_attributes(&dev
->ops
);
822 swlib_free_attributes(&dev
->port_ops
);
823 swlib_free_attributes(&dev
->vlan_ops
);
824 swlib_free_port_map(dev
);
834 swlib_free_all(struct switch_dev
*dev
)
836 struct switch_dev
*p
;