swconfig: support sending SWITCH_TYPE_LINK to kernel
[openwrt.git] / package / network / config / swconfig / src / swlib.c
1 /*
2  * swlib.c: Switch configuration API (user space part)
3  *
4  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5  *
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.
9  *
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.
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <inttypes.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <linux/switch.h>
26 #include "swlib.h"
27 #include <netlink/netlink.h>
28 #include <netlink/genl/genl.h>
29 #include <netlink/genl/family.h>
30
31 //#define DEBUG 1
32 #ifdef DEBUG
33 #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
34 #else
35 #define DPRINTF(fmt, ...) do {} while (0)
36 #endif
37
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;
43
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 },
47 };
48
49 static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = {
50         [SWITCH_PORTMAP_SEGMENT] = { .type = NLA_STRING },
51         [SWITCH_PORTMAP_VIRT] = { .type = NLA_U32 },
52 };
53
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 },
61 };
62
63 static inline void *
64 swlib_alloc(size_t size)
65 {
66         void *ptr;
67
68         ptr = malloc(size);
69         if (!ptr)
70                 goto done;
71         memset(ptr, 0, size);
72
73 done:
74         return ptr;
75 }
76
77 static int
78 wait_handler(struct nl_msg *msg, void *arg)
79 {
80         int *finished = arg;
81
82         *finished = 1;
83         return NL_STOP;
84 }
85
86 /* helper function for performing netlink requests */
87 static int
88 swlib_call(int cmd, int (*call)(struct nl_msg *, void *),
89                 int (*data)(struct nl_msg *, void *), void *arg)
90 {
91         struct nl_msg *msg;
92         struct nl_cb *cb = NULL;
93         int finished;
94         int flags = 0;
95         int err;
96
97         msg = nlmsg_alloc();
98         if (!msg) {
99                 fprintf(stderr, "Out of memory!\n");
100                 exit(1);
101         }
102
103         if (!data)
104                 flags |= NLM_F_DUMP;
105
106         genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, genl_family_get_id(family), 0, flags, cmd, 0);
107         if (data) {
108                 if (data(msg, arg) < 0)
109                         goto nla_put_failure;
110         }
111
112         cb = nl_cb_alloc(NL_CB_CUSTOM);
113         if (!cb) {
114                 fprintf(stderr, "nl_cb_alloc failed.\n");
115                 exit(1);
116         }
117
118         err = nl_send_auto_complete(handle, msg);
119         if (err < 0) {
120                 fprintf(stderr, "nl_send_auto_complete failed: %d\n", err);
121                 goto out;
122         }
123
124         finished = 0;
125
126         if (call)
127                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, call, arg);
128
129         if (data)
130                 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
131         else
132                 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &finished);
133
134         err = nl_recvmsgs(handle, cb);
135         if (err < 0) {
136                 goto out;
137         }
138
139         if (!finished)
140                 err = nl_wait_for_ack(handle);
141
142 out:
143         if (cb)
144                 nl_cb_put(cb);
145 nla_put_failure:
146         nlmsg_free(msg);
147         return err;
148 }
149
150 static int
151 send_attr(struct nl_msg *msg, void *arg)
152 {
153         struct switch_val *val = arg;
154         struct switch_attr *attr = val->attr;
155
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);
161                 break;
162         case SWLIB_ATTR_GROUP_VLAN:
163                 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VLAN, val->port_vlan);
164                 break;
165         default:
166                 break;
167         }
168
169         return 0;
170
171 nla_put_failure:
172         return -1;
173 }
174
175 static int
176 store_port_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
177 {
178         struct nlattr *p;
179         int ports = val->attr->dev->ports;
180         int err = 0;
181         int remaining;
182
183         if (!val->value.ports)
184                 val->value.ports = malloc(sizeof(struct switch_port) * ports);
185
186         nla_for_each_nested(p, nla, remaining) {
187                 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
188                 struct switch_port *port;
189
190                 if (val->len >= ports)
191                         break;
192
193                 err = nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, p, port_policy);
194                 if (err < 0)
195                         goto out;
196
197                 if (!tb[SWITCH_PORT_ID])
198                         continue;
199
200                 port = &val->value.ports[val->len];
201                 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
202                 port->flags = 0;
203                 if (tb[SWITCH_PORT_FLAG_TAGGED])
204                         port->flags |= SWLIB_PORT_FLAG_TAGGED;
205
206                 val->len++;
207         }
208
209 out:
210         return err;
211 }
212
213 static int
214 store_link_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
215 {
216         struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1];
217         struct switch_port_link *link;
218         int err = 0;
219
220         if (!val->value.link)
221                 val->value.link = malloc(sizeof(struct switch_port_link));
222
223         err = nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy);
224         if (err < 0)
225                 goto out;
226
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]);
234         link->eee = 0;
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;
239
240 out:
241         return err;
242 }
243
244 static int
245 store_val(struct nl_msg *msg, void *arg)
246 {
247         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
248         struct switch_val *val = arg;
249
250         if (!val)
251                 goto error;
252
253         if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
254                         genlmsg_attrlen(gnlh, 0), NULL) < 0) {
255                 goto error;
256         }
257
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);
266
267         val->err = 0;
268         return 0;
269
270 error:
271         return NL_SKIP;
272 }
273
274 int
275 swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
276 {
277         int cmd;
278         int err;
279
280         switch(attr->atype) {
281         case SWLIB_ATTR_GROUP_GLOBAL:
282                 cmd = SWITCH_CMD_GET_GLOBAL;
283                 break;
284         case SWLIB_ATTR_GROUP_PORT:
285                 cmd = SWITCH_CMD_GET_PORT;
286                 break;
287         case SWLIB_ATTR_GROUP_VLAN:
288                 cmd = SWITCH_CMD_GET_VLAN;
289                 break;
290         default:
291                 return -EINVAL;
292         }
293
294         memset(&val->value, 0, sizeof(val->value));
295         val->len = 0;
296         val->attr = attr;
297         val->err = -EINVAL;
298         err = swlib_call(cmd, store_val, send_attr, val);
299         if (!err)
300                 err = val->err;
301
302         return err;
303 }
304
305 static int
306 send_attr_ports(struct nl_msg *msg, struct switch_val *val)
307 {
308         struct nlattr *n;
309         int i;
310
311         /* TODO implement multipart? */
312         if (val->len == 0)
313                 goto done;
314         n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_PORTS);
315         if (!n)
316                 goto nla_put_failure;
317         for (i = 0; i < val->len; i++) {
318                 struct switch_port *port = &val->value.ports[i];
319                 struct nlattr *np;
320
321                 np = nla_nest_start(msg, SWITCH_ATTR_PORT);
322                 if (!np)
323                         goto nla_put_failure;
324
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);
328
329                 nla_nest_end(msg, np);
330         }
331         nla_nest_end(msg, n);
332 done:
333         return 0;
334
335 nla_put_failure:
336         return -1;
337 }
338
339 static int
340 send_attr_link(struct nl_msg *msg, struct switch_val *val)
341 {
342         struct switch_port_link *link = val->value.link;
343         struct nlattr *n;
344
345         n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_LINK);
346         if (!n)
347                 goto nla_put_failure;
348
349         if (link->duplex)
350                 NLA_PUT_FLAG(msg, SWITCH_LINK_FLAG_DUPLEX);
351         if (link->aneg)
352                 NLA_PUT_FLAG(msg, SWITCH_LINK_FLAG_ANEG);
353         NLA_PUT_U32(msg, SWITCH_LINK_SPEED, link->speed);
354
355         nla_nest_end(msg, n);
356
357         return 0;
358
359 nla_put_failure:
360         return -1;
361 }
362
363 static int
364 send_attr_val(struct nl_msg *msg, void *arg)
365 {
366         struct switch_val *val = arg;
367         struct switch_attr *attr = val->attr;
368
369         if (send_attr(msg, arg))
370                 goto nla_put_failure;
371
372         switch(attr->type) {
373         case SWITCH_TYPE_NOVAL:
374                 break;
375         case SWITCH_TYPE_INT:
376                 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val->value.i);
377                 break;
378         case SWITCH_TYPE_STRING:
379                 if (!val->value.s)
380                         goto nla_put_failure;
381                 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val->value.s);
382                 break;
383         case SWITCH_TYPE_PORTS:
384                 if (send_attr_ports(msg, val) < 0)
385                         goto nla_put_failure;
386                 break;
387         case SWITCH_TYPE_LINK:
388                 if (send_attr_link(msg, val))
389                         goto nla_put_failure;
390                 break;
391         default:
392                 goto nla_put_failure;
393         }
394         return 0;
395
396 nla_put_failure:
397         return -1;
398 }
399
400 int
401 swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
402 {
403         int cmd;
404
405         switch(attr->atype) {
406         case SWLIB_ATTR_GROUP_GLOBAL:
407                 cmd = SWITCH_CMD_SET_GLOBAL;
408                 break;
409         case SWLIB_ATTR_GROUP_PORT:
410                 cmd = SWITCH_CMD_SET_PORT;
411                 break;
412         case SWLIB_ATTR_GROUP_VLAN:
413                 cmd = SWITCH_CMD_SET_VLAN;
414                 break;
415         default:
416                 return -EINVAL;
417         }
418
419         val->attr = attr;
420         return swlib_call(cmd, NULL, send_attr_val, val);
421 }
422
423 int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str)
424 {
425         struct switch_port *ports;
426         struct switch_val val;
427         char *ptr;
428
429         memset(&val, 0, sizeof(val));
430         val.port_vlan = port_vlan;
431         switch(a->type) {
432         case SWITCH_TYPE_INT:
433                 val.value.i = atoi(str);
434                 break;
435         case SWITCH_TYPE_STRING:
436                 val.value.s = (char *)str;
437                 break;
438         case SWITCH_TYPE_PORTS:
439                 ports = alloca(sizeof(struct switch_port) * dev->ports);
440                 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
441                 val.len = 0;
442                 ptr = (char *)str;
443                 while(ptr && *ptr)
444                 {
445                         while(*ptr && isspace(*ptr))
446                                 ptr++;
447
448                         if (!*ptr)
449                                 break;
450
451                         if (!isdigit(*ptr))
452                                 return -1;
453
454                         if (val.len >= dev->ports)
455                                 return -1;
456
457                         ports[val.len].flags = 0;
458                         ports[val.len].id = strtoul(ptr, &ptr, 10);
459                         while(*ptr && !isspace(*ptr)) {
460                                 if (*ptr == 't')
461                                         ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
462                                 else
463                                         return -1;
464
465                                 ptr++;
466                         }
467                         if (*ptr)
468                                 ptr++;
469                         val.len++;
470                 }
471                 val.value.ports = ports;
472                 break;
473         case SWITCH_TYPE_NOVAL:
474                 if (str && !strcmp(str, "0"))
475                         return 0;
476
477                 break;
478         default:
479                 return -1;
480         }
481         return swlib_set_attr(dev, a, &val);
482 }
483
484
485 struct attrlist_arg {
486         int id;
487         int atype;
488         struct switch_dev *dev;
489         struct switch_attr *prev;
490         struct switch_attr **head;
491 };
492
493 static int
494 add_id(struct nl_msg *msg, void *arg)
495 {
496         struct attrlist_arg *l = arg;
497
498         NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id);
499
500         return 0;
501 nla_put_failure:
502         return -1;
503 }
504
505 static int
506 add_attr(struct nl_msg *msg, void *ptr)
507 {
508         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
509         struct attrlist_arg *arg = ptr;
510         struct switch_attr *new;
511
512         if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
513                         genlmsg_attrlen(gnlh, 0), NULL) < 0)
514                 goto done;
515
516         new = swlib_alloc(sizeof(struct switch_attr));
517         if (!new)
518                 goto done;
519
520         new->dev = arg->dev;
521         new->atype = arg->atype;
522         if (arg->prev) {
523                 arg->prev->next = new;
524         } else {
525                 arg->prev = *arg->head;
526         }
527         *arg->head = new;
528         arg->head = &new->next;
529
530         if (tb[SWITCH_ATTR_OP_ID])
531                 new->id = nla_get_u32(tb[SWITCH_ATTR_OP_ID]);
532         if (tb[SWITCH_ATTR_OP_TYPE])
533                 new->type = nla_get_u32(tb[SWITCH_ATTR_OP_TYPE]);
534         if (tb[SWITCH_ATTR_OP_NAME])
535                 new->name = strdup(nla_get_string(tb[SWITCH_ATTR_OP_NAME]));
536         if (tb[SWITCH_ATTR_OP_DESCRIPTION])
537                 new->description = strdup(nla_get_string(tb[SWITCH_ATTR_OP_DESCRIPTION]));
538
539 done:
540         return NL_SKIP;
541 }
542
543 int
544 swlib_scan(struct switch_dev *dev)
545 {
546         struct attrlist_arg arg;
547
548         if (dev->ops || dev->port_ops || dev->vlan_ops)
549                 return 0;
550
551         arg.atype = SWLIB_ATTR_GROUP_GLOBAL;
552         arg.dev = dev;
553         arg.id = dev->id;
554         arg.prev = NULL;
555         arg.head = &dev->ops;
556         swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg);
557
558         arg.atype = SWLIB_ATTR_GROUP_PORT;
559         arg.prev = NULL;
560         arg.head = &dev->port_ops;
561         swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg);
562
563         arg.atype = SWLIB_ATTR_GROUP_VLAN;
564         arg.prev = NULL;
565         arg.head = &dev->vlan_ops;
566         swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg);
567
568         return 0;
569 }
570
571 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
572                 enum swlib_attr_group atype, const char *name)
573 {
574         struct switch_attr *head;
575
576         if (!name || !dev)
577                 return NULL;
578
579         switch(atype) {
580         case SWLIB_ATTR_GROUP_GLOBAL:
581                 head = dev->ops;
582                 break;
583         case SWLIB_ATTR_GROUP_PORT:
584                 head = dev->port_ops;
585                 break;
586         case SWLIB_ATTR_GROUP_VLAN:
587                 head = dev->vlan_ops;
588                 break;
589         }
590         while(head) {
591                 if (!strcmp(name, head->name))
592                         return head;
593                 head = head->next;
594         }
595
596         return NULL;
597 }
598
599 static void
600 swlib_priv_free(void)
601 {
602         if (family)
603                 nl_object_put((struct nl_object*)family);
604         if (cache)
605                 nl_cache_free(cache);
606         if (handle)
607                 nl_socket_free(handle);
608         family = NULL;
609         handle = NULL;
610         cache = NULL;
611 }
612
613 static int
614 swlib_priv_init(void)
615 {
616         int ret;
617
618         handle = nl_socket_alloc();
619         if (!handle) {
620                 DPRINTF("Failed to create handle\n");
621                 goto err;
622         }
623
624         if (genl_connect(handle)) {
625                 DPRINTF("Failed to connect to generic netlink\n");
626                 goto err;
627         }
628
629         ret = genl_ctrl_alloc_cache(handle, &cache);
630         if (ret < 0) {
631                 DPRINTF("Failed to allocate netlink cache\n");
632                 goto err;
633         }
634
635         family = genl_ctrl_search_by_name(cache, "switch");
636         if (!family) {
637                 DPRINTF("Switch API not present\n");
638                 goto err;
639         }
640         return 0;
641
642 err:
643         swlib_priv_free();
644         return -EINVAL;
645 }
646
647 struct swlib_scan_arg {
648         const char *name;
649         struct switch_dev *head;
650         struct switch_dev *ptr;
651 };
652
653 static int
654 add_port_map(struct switch_dev *dev, struct nlattr *nla)
655 {
656         struct nlattr *p;
657         int err = 0, idx = 0;
658         int remaining;
659
660         dev->maps = malloc(sizeof(struct switch_portmap) * dev->ports);
661         if (!dev->maps)
662                 return -1;
663         memset(dev->maps, 0, sizeof(struct switch_portmap) * dev->ports);
664
665         nla_for_each_nested(p, nla, remaining) {
666                 struct nlattr *tb[SWITCH_PORTMAP_MAX+1];
667
668                 if (idx >= dev->ports)
669                         continue;
670
671                 err = nla_parse_nested(tb, SWITCH_PORTMAP_MAX, p, portmap_policy);
672                 if (err < 0)
673                         continue;
674
675
676                 if (tb[SWITCH_PORTMAP_SEGMENT] && tb[SWITCH_PORTMAP_VIRT]) {
677                         dev->maps[idx].segment = strdup(nla_get_string(tb[SWITCH_PORTMAP_SEGMENT]));
678                         dev->maps[idx].virt = nla_get_u32(tb[SWITCH_PORTMAP_VIRT]);
679                 }
680                 idx++;
681         }
682
683 out:
684         return err;
685 }
686
687
688 static int
689 add_switch(struct nl_msg *msg, void *arg)
690 {
691         struct swlib_scan_arg *sa = arg;
692         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
693         struct switch_dev *dev;
694         const char *name;
695         const char *alias;
696
697         if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
698                 goto done;
699
700         if (!tb[SWITCH_ATTR_DEV_NAME])
701                 goto done;
702
703         name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
704         alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]);
705
706         if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0))
707                 goto done;
708
709         dev = swlib_alloc(sizeof(struct switch_dev));
710         if (!dev)
711                 goto done;
712
713         strncpy(dev->dev_name, name, IFNAMSIZ - 1);
714         dev->alias = strdup(alias);
715         if (tb[SWITCH_ATTR_ID])
716                 dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
717         if (tb[SWITCH_ATTR_NAME])
718                 dev->name = strdup(nla_get_string(tb[SWITCH_ATTR_NAME]));
719         if (tb[SWITCH_ATTR_PORTS])
720                 dev->ports = nla_get_u32(tb[SWITCH_ATTR_PORTS]);
721         if (tb[SWITCH_ATTR_VLANS])
722                 dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
723         if (tb[SWITCH_ATTR_CPU_PORT])
724                 dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]);
725         if (tb[SWITCH_ATTR_PORTMAP])
726                 add_port_map(dev, tb[SWITCH_ATTR_PORTMAP]);
727
728         if (!sa->head) {
729                 sa->head = dev;
730                 sa->ptr = dev;
731         } else {
732                 sa->ptr->next = dev;
733                 sa->ptr = dev;
734         }
735
736         refcount++;
737 done:
738         return NL_SKIP;
739 }
740
741 static int
742 list_switch(struct nl_msg *msg, void *arg)
743 {
744         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
745
746         if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
747                 goto done;
748
749         if (!tb[SWITCH_ATTR_DEV_NAME] || !tb[SWITCH_ATTR_NAME])
750                 goto done;
751
752         printf("Found: %s - %s\n", nla_get_string(tb[SWITCH_ATTR_DEV_NAME]),
753                 nla_get_string(tb[SWITCH_ATTR_ALIAS]));
754
755 done:
756         return NL_SKIP;
757 }
758
759 void
760 swlib_list(void)
761 {
762         if (swlib_priv_init() < 0)
763                 return;
764         swlib_call(SWITCH_CMD_GET_SWITCH, list_switch, NULL, NULL);
765         swlib_priv_free();
766 }
767
768 void
769 swlib_print_portmap(struct switch_dev *dev, char *segment)
770 {
771         int i;
772
773         if (segment) {
774                 if (!strcmp(segment, "cpu")) {
775                         printf("%d ", dev->cpu_port);
776                 } else if (!strcmp(segment, "disabled")) {
777                         for (i = 0; i < dev->ports; i++)
778                                 if (!dev->maps[i].segment)
779                                         printf("%d ", i);
780                 } else for (i = 0; i < dev->ports; i++) {
781                         if (dev->maps[i].segment && !strcmp(dev->maps[i].segment, segment))
782                                 printf("%d ", i);
783                 }
784         } else {
785                 printf("%s - %s\n", dev->dev_name, dev->name);
786                 for (i = 0; i < dev->ports; i++)
787                         if (i == dev->cpu_port)
788                                 printf("port%d:\tcpu\n", i);
789                         else if (dev->maps[i].segment)
790                                 printf("port%d:\t%s.%d\n", i, dev->maps[i].segment, dev->maps[i].virt);
791                         else
792                                 printf("port%d:\tdisabled\n", i);
793         }
794 }
795
796 struct switch_dev *
797 swlib_connect(const char *name)
798 {
799         struct swlib_scan_arg arg;
800
801         if (!refcount) {
802                 if (swlib_priv_init() < 0)
803                         return NULL;
804         };
805
806         arg.head = NULL;
807         arg.ptr = NULL;
808         arg.name = name;
809         swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg);
810
811         if (!refcount)
812                 swlib_priv_free();
813
814         return arg.head;
815 }
816
817 static void
818 swlib_free_attributes(struct switch_attr **head)
819 {
820         struct switch_attr *a = *head;
821         struct switch_attr *next;
822
823         while (a) {
824                 next = a->next;
825                 free(a->name);
826                 free(a->description);
827                 free(a);
828                 a = next;
829         }
830         *head = NULL;
831 }
832
833 static void
834 swlib_free_port_map(struct switch_dev *dev)
835 {
836         int i;
837
838         if (!dev || !dev->maps)
839                 return;
840
841         for (i = 0; i < dev->ports; i++)
842                 free(dev->maps[i].segment);
843         free(dev->maps);
844 }
845
846 void
847 swlib_free(struct switch_dev *dev)
848 {
849         swlib_free_attributes(&dev->ops);
850         swlib_free_attributes(&dev->port_ops);
851         swlib_free_attributes(&dev->vlan_ops);
852         swlib_free_port_map(dev);
853         free(dev->name);
854         free(dev->alias);
855         free(dev);
856
857         if (--refcount == 0)
858                 swlib_priv_free();
859 }
860
861 void
862 swlib_free_all(struct switch_dev *dev)
863 {
864         struct switch_dev *p;
865
866         while (dev) {
867                 p = dev->next;
868                 swlib_free(dev);
869                 dev = p;
870         }
871 }