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>
30 #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
32 #define DPRINTF(fmt, ...) do {} while (0)
35 static struct nl_handle
*handle
;
36 static struct nl_cache
*cache
;
37 static struct genl_family
*family
;
38 static struct nlattr
*tb
[SWITCH_ATTR_MAX
];
39 static int refcount
= 0;
41 static struct nla_policy port_policy
[] = {
42 [SWITCH_PORT_ID
] = { .type
= NLA_U32
},
43 [SWITCH_PORT_FLAG_TAGGED
] = { .type
= NLA_FLAG
},
47 swlib_alloc(size_t size
)
61 wait_handler(struct nl_msg
*msg
, void *arg
)
69 /* helper function for performing netlink requests */
71 swlib_call(int cmd
, int (*call
)(struct nl_msg
*, void *),
72 int (*data
)(struct nl_msg
*, void *), void *arg
)
75 struct nl_cb
*cb
= NULL
;
82 fprintf(stderr
, "Out of memory!\n");
89 genlmsg_put(msg
, NL_AUTO_PID
, NL_AUTO_SEQ
, genl_family_get_id(family
), 0, flags
, cmd
, 0);
91 if (data(msg
, arg
) < 0)
95 cb
= nl_cb_alloc(NL_CB_CUSTOM
);
97 fprintf(stderr
, "nl_cb_alloc failed.\n");
101 err
= nl_send_auto_complete(handle
, msg
);
103 fprintf(stderr
, "nl_send_auto_complete failed: %d\n", err
);
110 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, call
, arg
);
113 nl_cb_set(cb
, NL_CB_ACK
, NL_CB_CUSTOM
, wait_handler
, &finished
);
115 nl_cb_set(cb
, NL_CB_FINISH
, NL_CB_CUSTOM
, wait_handler
, &finished
);
117 err
= nl_recvmsgs(handle
, cb
);
123 err
= nl_wait_for_ack(handle
);
134 send_attr(struct nl_msg
*msg
, void *arg
)
136 struct switch_val
*val
= arg
;
137 struct switch_attr
*attr
= val
->attr
;
139 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, attr
->dev
->id
);
140 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_ID
, attr
->id
);
141 switch(attr
->atype
) {
142 case SWLIB_ATTR_GROUP_PORT
:
143 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_PORT
, val
->port_vlan
);
145 case SWLIB_ATTR_GROUP_VLAN
:
146 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VLAN
, val
->port_vlan
);
159 store_port_val(struct nl_msg
*msg
, struct nlattr
*nla
, struct switch_val
*val
)
162 int ports
= val
->attr
->dev
->ports
;
166 if (!val
->value
.ports
)
167 val
->value
.ports
= malloc(sizeof(struct switch_port
) * ports
);
169 nla_for_each_nested(p
, nla
, remaining
) {
170 struct nlattr
*tb
[SWITCH_PORT_ATTR_MAX
+1];
171 struct switch_port
*port
;
173 if (val
->len
>= ports
)
176 err
= nla_parse_nested(tb
, SWITCH_PORT_ATTR_MAX
, p
, port_policy
);
180 if (!tb
[SWITCH_PORT_ID
])
183 port
= &val
->value
.ports
[val
->len
];
184 port
->id
= nla_get_u32(tb
[SWITCH_PORT_ID
]);
186 if (tb
[SWITCH_PORT_FLAG_TAGGED
])
187 port
->flags
|= SWLIB_PORT_FLAG_TAGGED
;
197 store_val(struct nl_msg
*msg
, void *arg
)
199 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
200 struct switch_val
*val
= arg
;
201 struct switch_attr
*attr
= val
->attr
;
206 if (nla_parse(tb
, SWITCH_ATTR_MAX
- 1, genlmsg_attrdata(gnlh
, 0),
207 genlmsg_attrlen(gnlh
, 0), NULL
) < 0) {
211 if (tb
[SWITCH_ATTR_OP_VALUE_INT
])
212 val
->value
.i
= nla_get_u32(tb
[SWITCH_ATTR_OP_VALUE_INT
]);
213 else if (tb
[SWITCH_ATTR_OP_VALUE_STR
])
214 val
->value
.s
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_VALUE_STR
]));
215 else if (tb
[SWITCH_ATTR_OP_VALUE_PORTS
])
216 val
->err
= store_port_val(msg
, tb
[SWITCH_ATTR_OP_VALUE_PORTS
], val
);
226 swlib_get_attr(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
231 switch(attr
->atype
) {
232 case SWLIB_ATTR_GROUP_GLOBAL
:
233 cmd
= SWITCH_CMD_GET_GLOBAL
;
235 case SWLIB_ATTR_GROUP_PORT
:
236 cmd
= SWITCH_CMD_GET_PORT
;
238 case SWLIB_ATTR_GROUP_VLAN
:
239 cmd
= SWITCH_CMD_GET_VLAN
;
245 memset(&val
->value
, 0, sizeof(val
->value
));
249 err
= swlib_call(cmd
, store_val
, send_attr
, val
);
257 send_attr_ports(struct nl_msg
*msg
, struct switch_val
*val
)
262 /* TODO implement multipart? */
265 n
= nla_nest_start(msg
, SWITCH_ATTR_OP_VALUE_PORTS
);
267 goto nla_put_failure
;
268 for (i
= 0; i
< val
->len
; i
++) {
269 struct switch_port
*port
= &val
->value
.ports
[i
];
272 np
= nla_nest_start(msg
, SWITCH_ATTR_PORT
);
274 goto nla_put_failure
;
276 NLA_PUT_U32(msg
, SWITCH_PORT_ID
, port
->id
);
277 if (port
->flags
& SWLIB_PORT_FLAG_TAGGED
)
278 NLA_PUT_FLAG(msg
, SWITCH_PORT_FLAG_TAGGED
);
280 nla_nest_end(msg
, np
);
282 nla_nest_end(msg
, n
);
291 send_attr_val(struct nl_msg
*msg
, void *arg
)
293 struct switch_val
*val
= arg
;
294 struct switch_attr
*attr
= val
->attr
;
296 if (send_attr(msg
, arg
))
297 goto nla_put_failure
;
300 case SWITCH_TYPE_NOVAL
:
302 case SWITCH_TYPE_INT
:
303 NLA_PUT_U32(msg
, SWITCH_ATTR_OP_VALUE_INT
, val
->value
.i
);
305 case SWITCH_TYPE_STRING
:
307 goto nla_put_failure
;
308 NLA_PUT_STRING(msg
, SWITCH_ATTR_OP_VALUE_STR
, val
->value
.s
);
310 case SWITCH_TYPE_PORTS
:
311 if (send_attr_ports(msg
, val
) < 0)
312 goto nla_put_failure
;
315 goto nla_put_failure
;
324 swlib_set_attr(struct switch_dev
*dev
, struct switch_attr
*attr
, struct switch_val
*val
)
328 switch(attr
->atype
) {
329 case SWLIB_ATTR_GROUP_GLOBAL
:
330 cmd
= SWITCH_CMD_SET_GLOBAL
;
332 case SWLIB_ATTR_GROUP_PORT
:
333 cmd
= SWITCH_CMD_SET_PORT
;
335 case SWLIB_ATTR_GROUP_VLAN
:
336 cmd
= SWITCH_CMD_SET_VLAN
;
343 return swlib_call(cmd
, NULL
, send_attr_val
, val
);
347 struct attrlist_arg
{
350 struct switch_dev
*dev
;
351 struct switch_attr
*prev
;
352 struct switch_attr
**head
;
356 add_id(struct nl_msg
*msg
, void *arg
)
358 struct attrlist_arg
*l
= arg
;
360 NLA_PUT_U32(msg
, SWITCH_ATTR_ID
, l
->id
);
368 add_attr(struct nl_msg
*msg
, void *ptr
)
370 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
371 struct attrlist_arg
*arg
= ptr
;
372 struct switch_attr
*new;
374 if (nla_parse(tb
, SWITCH_ATTR_MAX
- 1, genlmsg_attrdata(gnlh
, 0),
375 genlmsg_attrlen(gnlh
, 0), NULL
) < 0)
378 new = swlib_alloc(sizeof(struct switch_attr
));
383 new->atype
= arg
->atype
;
385 arg
->prev
->next
= new;
387 arg
->prev
= *arg
->head
;
390 arg
->head
= &new->next
;
392 if (tb
[SWITCH_ATTR_OP_ID
])
393 new->id
= nla_get_u32(tb
[SWITCH_ATTR_OP_ID
]);
394 if (tb
[SWITCH_ATTR_OP_TYPE
])
395 new->type
= nla_get_u32(tb
[SWITCH_ATTR_OP_TYPE
]);
396 if (tb
[SWITCH_ATTR_OP_NAME
])
397 new->name
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_NAME
]));
398 if (tb
[SWITCH_ATTR_OP_DESCRIPTION
])
399 new->description
= strdup(nla_get_string(tb
[SWITCH_ATTR_OP_DESCRIPTION
]));
406 swlib_scan(struct switch_dev
*dev
)
408 struct attrlist_arg arg
;
410 if (dev
->ops
|| dev
->port_ops
|| dev
->vlan_ops
)
413 arg
.atype
= SWLIB_ATTR_GROUP_GLOBAL
;
417 arg
.head
= &dev
->ops
;
418 swlib_call(SWITCH_CMD_LIST_GLOBAL
, add_attr
, add_id
, &arg
);
420 arg
.atype
= SWLIB_ATTR_GROUP_PORT
;
422 arg
.head
= &dev
->port_ops
;
423 swlib_call(SWITCH_CMD_LIST_PORT
, add_attr
, add_id
, &arg
);
425 arg
.atype
= SWLIB_ATTR_GROUP_VLAN
;
427 arg
.head
= &dev
->vlan_ops
;
428 swlib_call(SWITCH_CMD_LIST_VLAN
, add_attr
, add_id
, &arg
);
433 struct switch_attr
*swlib_lookup_attr(struct switch_dev
*dev
,
434 enum swlib_attr_group atype
, const char *name
)
436 struct switch_attr
*head
;
442 case SWLIB_ATTR_GROUP_GLOBAL
:
445 case SWLIB_ATTR_GROUP_PORT
:
446 head
= dev
->port_ops
;
448 case SWLIB_ATTR_GROUP_VLAN
:
449 head
= dev
->vlan_ops
;
453 if (!strcmp(name
, head
->name
))
462 swlib_priv_free(void)
465 nl_cache_free(cache
);
467 nl_handle_destroy(handle
);
473 swlib_priv_init(void)
475 handle
= nl_handle_alloc();
477 DPRINTF("Failed to create handle\n");
481 if (genl_connect(handle
)) {
482 DPRINTF("Failed to connect to generic netlink\n");
486 cache
= genl_ctrl_alloc_cache(handle
);
488 DPRINTF("Failed to allocate netlink cache\n");
492 family
= genl_ctrl_search_by_name(cache
, "switch");
494 DPRINTF("Switch API not present\n");
504 struct swlib_scan_arg
{
506 struct switch_dev
*head
;
507 struct switch_dev
*ptr
;
511 add_switch(struct nl_msg
*msg
, void *arg
)
513 struct swlib_scan_arg
*sa
= arg
;
514 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
515 struct switch_dev
*dev
;
518 if (nla_parse(tb
, SWITCH_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0), genlmsg_attrlen(gnlh
, 0), NULL
) < 0)
521 if (!tb
[SWITCH_ATTR_DEV_NAME
])
524 name
= nla_get_string(tb
[SWITCH_ATTR_DEV_NAME
]);
525 if (sa
->name
&& (strcmp(name
, sa
->name
) != 0))
528 dev
= swlib_alloc(sizeof(struct switch_dev
));
532 dev
->dev_name
= strdup(name
);
533 if (tb
[SWITCH_ATTR_ID
])
534 dev
->id
= nla_get_u32(tb
[SWITCH_ATTR_ID
]);
535 if (tb
[SWITCH_ATTR_NAME
])
536 dev
->name
= strdup(nla_get_string(tb
[SWITCH_ATTR_DEV_NAME
]));
537 if (tb
[SWITCH_ATTR_PORTS
])
538 dev
->ports
= nla_get_u32(tb
[SWITCH_ATTR_PORTS
]);
539 if (tb
[SWITCH_ATTR_VLANS
])
540 dev
->vlans
= nla_get_u32(tb
[SWITCH_ATTR_VLANS
]);
557 swlib_connect(const char *name
)
559 struct swlib_scan_arg arg
;
563 if (swlib_priv_init() < 0)
570 swlib_call(SWITCH_CMD_GET_SWITCH
, add_switch
, NULL
, &arg
);
579 swlib_free_attributes(struct switch_attr
**head
)
581 struct switch_attr
*a
= *head
;
582 struct switch_attr
*next
;
593 swlib_free(struct switch_dev
*dev
)
595 swlib_free_attributes(&dev
->ops
);
596 swlib_free_attributes(&dev
->port_ops
);
597 swlib_free_attributes(&dev
->vlan_ops
);
605 swlib_free_all(struct switch_dev
*dev
)
607 struct switch_dev
*p
;