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
];
42 static int refcount
= 0;
44 static struct nla_policy port_policy
[] = {
45 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
46 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
50 swlib_alloc(size_t size
)
64 wait_handler(struct nl_msg
*msg
, void *arg
)
72 /* helper function for performing netlink requests */
74 swlib_call(int cmd
, int (*call
)(struct nl_msg
*, void *),
75 int (*data
)(struct nl_msg
*, void *), void *arg
)
78 struct nl_cb
*cb
= NULL
;
85 fprintf(stderr
, "Out of memory!\n");
92 genlmsg_put(msg
, NL_AUTO_PID
, NL_AUTO_SEQ
, genl_family_get_id(family
), 0, flags
, cmd
, 0);
94 if (data(msg
, arg
) < 0)
98 cb
= nl_cb_alloc(NL_CB_CUSTOM
);
100 fprintf(stderr
, "nl_cb_alloc failed.\n");
104 err
= nl_send_auto_complete(handle
, msg
);
106 fprintf(stderr
, "nl_send_auto_complete failed: %d\n", err
);
113 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, call
, arg
);
116 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, wait_handler
, &finished
);
118 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, wait_handler
, &finished
);
120 err
= nl_recvmsgs(handle
, cb
);
126 err
= nl_wait_for_ack(handle
);
137 send_attr(struct nl_msg
*msg
, void *arg
)
139 struct switch_val
*val
= arg
;
140 struct switch_attr
*attr
= val
->attr
;
142 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, attr
->dev
->id
);
143 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_ID
, attr
->id
);
144 switch(attr
->atype
) {
145 case SWLIB_ATTR_GROUP_PORT
:
146 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_PORT
, val
->port_vlan
);
148 case SWLIB_ATTR_GROUP_VLAN
:
149 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VLAN
, val
->port_vlan
);
162 store_port_val(struct nl_msg
*msg
, struct nlattr
*nla
, struct switch_val
*val
)
165 int ports
= val
->attr
->dev
->ports
;
169 if (!val
->value
.ports
)
170 val
->value
.ports
= malloc(sizeof(struct switch_port
) * ports
);
172 nla_for_each_nested(p
, nla
, remaining
) {
173 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
174 struct switch_port
*port
;
176 if (val
->len
>= ports
)
179 err
= nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, p
, port_policy
);
183 if (!tb
[SWITCH_PORT_ID
])
186 port
= &val
->value
.ports
[val
->len
];
187 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
189 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
190 port
->flags
|= SWLIB_PORT_FLAG_TAGGED
;
200 store_val(struct nl_msg
*msg
, void *arg
)
202 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
203 struct switch_val
*val
= arg
;
204 struct switch_attr
*attr
= val
->attr
;
209 if (nla_parse(tb
, SWITCH_ATTR_MAX
- 1, genlmsg_attrdata(gnlh
, 0),
210 genlmsg_attrlen(gnlh
, 0), NULL
) < 0) {
214 if (tb
[SWITCH_ATTR_OP_VALUE_INT
])
215 val
->value
.i
= nla_get_u32(tb
[SWITCH_ATTR_OP_VALUE_INT
]);
216 else if (tb
[SWITCH_ATTR_OP_VALUE_STR
])
217 val
->value
.s
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_VALUE_STR
]));
218 else if (tb
[SWITCH_ATTR_OP_VALUE_PORTS
])
219 val
->err
= store_port_val(msg
, tb
[SWITCH_ATTR_OP_VALUE_PORTS
], val
);
229 swlib_get_attr(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
234 switch(attr
->atype
) {
235 case SWLIB_ATTR_GROUP_GLOBAL
:
236 cmd
= SWITCH_CMD_GET_GLOBAL
;
238 case SWLIB_ATTR_GROUP_PORT
:
239 cmd
= SWITCH_CMD_GET_PORT
;
241 case SWLIB_ATTR_GROUP_VLAN
:
242 cmd
= SWITCH_CMD_GET_VLAN
;
248 memset(&val
->value
, 0, sizeof(val
->value
));
252 err
= swlib_call(cmd
, store_val
, send_attr
, val
);
260 send_attr_ports(struct nl_msg
*msg
, struct switch_val
*val
)
265 /* TODO implement multipart? */
268 n
= nla_nest_start(msg
, SWITCH_ATTR_OP_VALUE_PORTS
);
270 goto nla_put_failure
;
271 for (i
= 0; i
< val
->len
; i
++) {
272 struct switch_port
*port
= &val
->value
.ports
[i
];
275 np
= nla_nest_start(msg
, SWITCH_ATTR_PORT
);
277 goto nla_put_failure
;
279 NLA_PUT_U32(msg
, SWITCH_PORT_ID
, port
->id
);
280 if (port
->flags
& SWLIB_PORT_FLAG_TAGGED
)
281 NLA_PUT_FLAG(msg
, SWITCH_PORT_FLAG_TAGGED
);
283 nla_nest_end(msg
, np
);
285 nla_nest_end(msg
, n
);
294 send_attr_val(struct nl_msg
*msg
, void *arg
)
296 struct switch_val
*val
= arg
;
297 struct switch_attr
*attr
= val
->attr
;
299 if (send_attr(msg
, arg
))
300 goto nla_put_failure
;
303 case SWITCH_TYPE_NOVAL
:
305 case SWITCH_TYPE_INT
:
306 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
->value
.i
);
308 case SWITCH_TYPE_STRING
:
310 goto nla_put_failure
;
311 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
->value
.s
);
313 case SWITCH_TYPE_PORTS
:
314 if (send_attr_ports(msg
, val
) < 0)
315 goto nla_put_failure
;
318 goto nla_put_failure
;
327 swlib_set_attr(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
331 switch(attr
->atype
) {
332 case SWLIB_ATTR_GROUP_GLOBAL
:
333 cmd
= SWITCH_CMD_SET_GLOBAL
;
335 case SWLIB_ATTR_GROUP_PORT
:
336 cmd
= SWITCH_CMD_SET_PORT
;
338 case SWLIB_ATTR_GROUP_VLAN
:
339 cmd
= SWITCH_CMD_SET_VLAN
;
346 return swlib_call(cmd
, NULL
, send_attr_val
, val
);
349 int swlib_set_attr_string(struct switch_dev
*dev
, struct switch_attr
*a
, int port_vlan
, const char *str
)
351 struct switch_port
*ports
;
352 struct switch_val val
;
355 memset(&val
, 0, sizeof(val
));
356 val
.port_vlan
= port_vlan
;
358 case SWITCH_TYPE_INT
:
359 val
.value
.i
= atoi(str
);
361 case SWITCH_TYPE_STRING
:
364 case SWITCH_TYPE_PORTS
:
365 ports
= alloca(sizeof(struct switch_port
) * dev
->ports
);
366 memset(ports
, 0, sizeof(struct switch_port
) * dev
->ports
);
371 while(*ptr
&& isspace(*ptr
))
380 if (val
.len
>= dev
->ports
)
383 ports
[val
.len
].flags
= 0;
384 ports
[val
.len
].id
= strtoul(ptr
, &ptr
, 10);
385 while(*ptr
&& !isspace(*ptr
)) {
387 ports
[val
.len
].flags
|= SWLIB_PORT_FLAG_TAGGED
;
397 val
.value
.ports
= ports
;
399 case SWITCH_TYPE_NOVAL
:
404 return swlib_set_attr(dev
, a
, &val
);
408 struct attrlist_arg
{
411 struct switch_dev
*dev
;
412 struct switch_attr
*prev
;
413 struct switch_attr
**head
;
417 add_id(struct nl_msg
*msg
, void *arg
)
419 struct attrlist_arg
*l
= arg
;
421 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, l
->id
);
429 add_attr(struct nl_msg
*msg
, void *ptr
)
431 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
432 struct attrlist_arg
*arg
= ptr
;
433 struct switch_attr
*new;
435 if (nla_parse(tb
, SWITCH_ATTR_MAX
- 1, genlmsg_attrdata(gnlh
, 0),
436 genlmsg_attrlen(gnlh
, 0), NULL
) < 0)
439 new = swlib_alloc(sizeof(struct switch_attr
));
444 new->atype
= arg
->atype
;
446 arg
->prev
->next
= new;
448 arg
->prev
= *arg
->head
;
451 arg
->head
= &new->next
;
453 if (tb
[SWITCH_ATTR_OP_ID
])
454 new->id
= nla_get_u32(tb
[SWITCH_ATTR_OP_ID
]);
455 if (tb
[SWITCH_ATTR_OP_TYPE
])
456 new->type
= nla_get_u32(tb
[SWITCH_ATTR_OP_TYPE
]);
457 if (tb
[SWITCH_ATTR_OP_NAME
])
458 new->name
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_NAME
]));
459 if (tb
[SWITCH_ATTR_OP_DESCRIPTION
])
460 new->description
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_DESCRIPTION
]));
467 swlib_scan(struct switch_dev
*dev
)
469 struct attrlist_arg arg
;
471 if (dev
->ops
|| dev
->port_ops
|| dev
->vlan_ops
)
474 arg
.atype
= SWLIB_ATTR_GROUP_GLOBAL
;
478 arg
.head
= &dev
->ops
;
479 swlib_call(SWITCH_CMD_LIST_GLOBAL
, add_attr
, add_id
, &arg
);
481 arg
.atype
= SWLIB_ATTR_GROUP_PORT
;
483 arg
.head
= &dev
->port_ops
;
484 swlib_call(SWITCH_CMD_LIST_PORT
, add_attr
, add_id
, &arg
);
486 arg
.atype
= SWLIB_ATTR_GROUP_VLAN
;
488 arg
.head
= &dev
->vlan_ops
;
489 swlib_call(SWITCH_CMD_LIST_VLAN
, add_attr
, add_id
, &arg
);
494 struct switch_attr
*swlib_lookup_attr(struct switch_dev
*dev
,
495 enum swlib_attr_group atype
, const char *name
)
497 struct switch_attr
*head
;
503 case SWLIB_ATTR_GROUP_GLOBAL
:
506 case SWLIB_ATTR_GROUP_PORT
:
507 head
= dev
->port_ops
;
509 case SWLIB_ATTR_GROUP_VLAN
:
510 head
= dev
->vlan_ops
;
514 if (!strcmp(name
, head
->name
))
523 swlib_priv_free(void)
526 nl_cache_free(cache
);
528 nl_socket_free(handle
);
534 swlib_priv_init(void)
538 handle
= nl_socket_alloc();
540 DPRINTF("Failed to create handle\n");
544 if (genl_connect(handle
)) {
545 DPRINTF("Failed to connect to generic netlink\n");
549 ret
= genl_ctrl_alloc_cache(handle
, &cache
);
551 DPRINTF("Failed to allocate netlink cache\n");
555 family
= genl_ctrl_search_by_name(cache
, "switch");
557 DPRINTF("Switch API not present\n");
567 struct swlib_scan_arg
{
569 struct switch_dev
*head
;
570 struct switch_dev
*ptr
;
574 add_switch(struct nl_msg
*msg
, void *arg
)
576 struct swlib_scan_arg
*sa
= arg
;
577 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
578 struct switch_dev
*dev
;
581 if (nla_parse(tb
, SWITCH_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0), genlmsg_attrlen(gnlh
, 0), NULL
) < 0)
584 if (!tb
[SWITCH_ATTR_DEV_NAME
])
587 name
= nla_get_string(tb
[SWITCH_ATTR_DEV_NAME
]);
588 if (sa
->name
&& (strcmp(name
, sa
->name
) != 0))
591 dev
= swlib_alloc(sizeof(struct switch_dev
));
595 dev
->dev_name
= strdup(name
);
596 if (tb
[SWITCH_ATTR_ID
])
597 dev
->id
= nla_get_u32(tb
[SWITCH_ATTR_ID
]);
598 if (tb
[SWITCH_ATTR_NAME
])
599 dev
->name
= strdup(nla_get_string(tb
[SWITCH_ATTR_NAME
]));
600 if (tb
[SWITCH_ATTR_PORTS
])
601 dev
->ports
= nla_get_u32(tb
[SWITCH_ATTR_PORTS
]);
602 if (tb
[SWITCH_ATTR_VLANS
])
603 dev
->vlans
= nla_get_u32(tb
[SWITCH_ATTR_VLANS
]);
604 if (tb
[SWITCH_ATTR_CPU_PORT
])
605 dev
->cpu_port
= nla_get_u32(tb
[SWITCH_ATTR_CPU_PORT
]);
622 swlib_connect(const char *name
)
624 struct swlib_scan_arg arg
;
628 if (swlib_priv_init() < 0)
635 swlib_call(SWITCH_CMD_GET_SWITCH
, add_switch
, NULL
, &arg
);
644 swlib_free_attributes(struct switch_attr
**head
)
646 struct switch_attr
*a
= *head
;
647 struct switch_attr
*next
;
658 swlib_free(struct switch_dev
*dev
)
660 swlib_free_attributes(&dev
->ops
);
661 swlib_free_attributes(&dev
->port_ops
);
662 swlib_free_attributes(&dev
->vlan_ops
);
670 swlib_free_all(struct switch_dev
*dev
)
672 struct switch_dev
*p
;