908e0fb496580c74e0e659f6c55771446790d6ed
[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_val(struct nl_msg *msg, void *arg)
341 {
342         struct switch_val *val = arg;
343         struct switch_attr *attr = val->attr;
344
345         if (send_attr(msg, arg))
346                 goto nla_put_failure;
347
348         switch(attr->type) {
349         case SWITCH_TYPE_NOVAL:
350                 break;
351         case SWITCH_TYPE_INT:
352                 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val->value.i);
353                 break;
354         case SWITCH_TYPE_STRING:
355                 if (!val->value.s)
356                         goto nla_put_failure;
357                 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val->value.s);
358                 break;
359         case SWITCH_TYPE_PORTS:
360                 if (send_attr_ports(msg, val) < 0)
361                         goto nla_put_failure;
362                 break;
363         default:
364                 goto nla_put_failure;
365         }
366         return 0;
367
368 nla_put_failure:
369         return -1;
370 }
371
372 int
373 swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
374 {
375         int cmd;
376
377         switch(attr->atype) {
378         case SWLIB_ATTR_GROUP_GLOBAL:
379                 cmd = SWITCH_CMD_SET_GLOBAL;
380                 break;
381         case SWLIB_ATTR_GROUP_PORT:
382                 cmd = SWITCH_CMD_SET_PORT;
383                 break;
384         case SWLIB_ATTR_GROUP_VLAN:
385                 cmd = SWITCH_CMD_SET_VLAN;
386                 break;
387         default:
388                 return -EINVAL;
389         }
390
391         val->attr = attr;
392         return swlib_call(cmd, NULL, send_attr_val, val);
393 }
394
395 int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str)
396 {
397         struct switch_port *ports;
398         struct switch_val val;
399         char *ptr;
400
401         memset(&val, 0, sizeof(val));
402         val.port_vlan = port_vlan;
403         switch(a->type) {
404         case SWITCH_TYPE_INT:
405                 val.value.i = atoi(str);
406                 break;
407         case SWITCH_TYPE_STRING:
408                 val.value.s = (char *)str;
409                 break;
410         case SWITCH_TYPE_PORTS:
411                 ports = alloca(sizeof(struct switch_port) * dev->ports);
412                 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
413                 val.len = 0;
414                 ptr = (char *)str;
415                 while(ptr && *ptr)
416                 {
417                         while(*ptr && isspace(*ptr))
418                                 ptr++;
419
420                         if (!*ptr)
421                                 break;
422
423                         if (!isdigit(*ptr))
424                                 return -1;
425
426                         if (val.len >= dev->ports)
427                                 return -1;
428
429                         ports[val.len].flags = 0;
430                         ports[val.len].id = strtoul(ptr, &ptr, 10);
431                         while(*ptr && !isspace(*ptr)) {
432                                 if (*ptr == 't')
433                                         ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
434                                 else
435                                         return -1;
436
437                                 ptr++;
438                         }
439                         if (*ptr)
440                                 ptr++;
441                         val.len++;
442                 }
443                 val.value.ports = ports;
444                 break;
445         case SWITCH_TYPE_NOVAL:
446                 if (str && !strcmp(str, "0"))
447                         return 0;
448
449                 break;
450         default:
451                 return -1;
452         }
453         return swlib_set_attr(dev, a, &val);
454 }
455
456
457 struct attrlist_arg {
458         int id;
459         int atype;
460         struct switch_dev *dev;
461         struct switch_attr *prev;
462         struct switch_attr **head;
463 };
464
465 static int
466 add_id(struct nl_msg *msg, void *arg)
467 {
468         struct attrlist_arg *l = arg;
469
470         NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id);
471
472         return 0;
473 nla_put_failure:
474         return -1;
475 }
476
477 static int
478 add_attr(struct nl_msg *msg, void *ptr)
479 {
480         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
481         struct attrlist_arg *arg = ptr;
482         struct switch_attr *new;
483
484         if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
485                         genlmsg_attrlen(gnlh, 0), NULL) < 0)
486                 goto done;
487
488         new = swlib_alloc(sizeof(struct switch_attr));
489         if (!new)
490                 goto done;
491
492         new->dev = arg->dev;
493         new->atype = arg->atype;
494         if (arg->prev) {
495                 arg->prev->next = new;
496         } else {
497                 arg->prev = *arg->head;
498         }
499         *arg->head = new;
500         arg->head = &new->next;
501
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]));
510
511 done:
512         return NL_SKIP;
513 }
514
515 int
516 swlib_scan(struct switch_dev *dev)
517 {
518         struct attrlist_arg arg;
519
520         if (dev->ops || dev->port_ops || dev->vlan_ops)
521                 return 0;
522
523         arg.atype = SWLIB_ATTR_GROUP_GLOBAL;
524         arg.dev = dev;
525         arg.id = dev->id;
526         arg.prev = NULL;
527         arg.head = &dev->ops;
528         swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg);
529
530         arg.atype = SWLIB_ATTR_GROUP_PORT;
531         arg.prev = NULL;
532         arg.head = &dev->port_ops;
533         swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg);
534
535         arg.atype = SWLIB_ATTR_GROUP_VLAN;
536         arg.prev = NULL;
537         arg.head = &dev->vlan_ops;
538         swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg);
539
540         return 0;
541 }
542
543 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
544                 enum swlib_attr_group atype, const char *name)
545 {
546         struct switch_attr *head;
547
548         if (!name || !dev)
549                 return NULL;
550
551         switch(atype) {
552         case SWLIB_ATTR_GROUP_GLOBAL:
553                 head = dev->ops;
554                 break;
555         case SWLIB_ATTR_GROUP_PORT:
556                 head = dev->port_ops;
557                 break;
558         case SWLIB_ATTR_GROUP_VLAN:
559                 head = dev->vlan_ops;
560                 break;
561         }
562         while(head) {
563                 if (!strcmp(name, head->name))
564                         return head;
565                 head = head->next;
566         }
567
568         return NULL;
569 }
570
571 static void
572 swlib_priv_free(void)
573 {
574         if (family)
575                 nl_object_put((struct nl_object*)family);
576         if (cache)
577                 nl_cache_free(cache);
578         if (handle)
579                 nl_socket_free(handle);
580         family = NULL;
581         handle = NULL;
582         cache = NULL;
583 }
584
585 static int
586 swlib_priv_init(void)
587 {
588         int ret;
589
590         handle = nl_socket_alloc();
591         if (!handle) {
592                 DPRINTF("Failed to create handle\n");
593                 goto err;
594         }
595
596         if (genl_connect(handle)) {
597                 DPRINTF("Failed to connect to generic netlink\n");
598                 goto err;
599         }
600
601         ret = genl_ctrl_alloc_cache(handle, &cache);
602         if (ret < 0) {
603                 DPRINTF("Failed to allocate netlink cache\n");
604                 goto err;
605         }
606
607         family = genl_ctrl_search_by_name(cache, "switch");
608         if (!family) {
609                 DPRINTF("Switch API not present\n");
610                 goto err;
611         }
612         return 0;
613
614 err:
615         swlib_priv_free();
616         return -EINVAL;
617 }
618
619 struct swlib_scan_arg {
620         const char *name;
621         struct switch_dev *head;
622         struct switch_dev *ptr;
623 };
624
625 static int
626 add_port_map(struct switch_dev *dev, struct nlattr *nla)
627 {
628         struct nlattr *p;
629         int err = 0, idx = 0;
630         int remaining;
631
632         dev->maps = malloc(sizeof(struct switch_portmap) * dev->ports);
633         if (!dev->maps)
634                 return -1;
635         memset(dev->maps, 0, sizeof(struct switch_portmap) * dev->ports);
636
637         nla_for_each_nested(p, nla, remaining) {
638                 struct nlattr *tb[SWITCH_PORTMAP_MAX+1];
639
640                 if (idx >= dev->ports)
641                         continue;
642
643                 err = nla_parse_nested(tb, SWITCH_PORTMAP_MAX, p, portmap_policy);
644                 if (err < 0)
645                         continue;
646
647
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]);
651                 }
652                 idx++;
653         }
654
655 out:
656         return err;
657 }
658
659
660 static int
661 add_switch(struct nl_msg *msg, void *arg)
662 {
663         struct swlib_scan_arg *sa = arg;
664         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
665         struct switch_dev *dev;
666         const char *name;
667         const char *alias;
668
669         if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
670                 goto done;
671
672         if (!tb[SWITCH_ATTR_DEV_NAME])
673                 goto done;
674
675         name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
676         alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]);
677
678         if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0))
679                 goto done;
680
681         dev = swlib_alloc(sizeof(struct switch_dev));
682         if (!dev)
683                 goto done;
684
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]);
699
700         if (!sa->head) {
701                 sa->head = dev;
702                 sa->ptr = dev;
703         } else {
704                 sa->ptr->next = dev;
705                 sa->ptr = dev;
706         }
707
708         refcount++;
709 done:
710         return NL_SKIP;
711 }
712
713 static int
714 list_switch(struct nl_msg *msg, void *arg)
715 {
716         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
717
718         if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
719                 goto done;
720
721         if (!tb[SWITCH_ATTR_DEV_NAME] || !tb[SWITCH_ATTR_NAME])
722                 goto done;
723
724         printf("Found: %s - %s\n", nla_get_string(tb[SWITCH_ATTR_DEV_NAME]),
725                 nla_get_string(tb[SWITCH_ATTR_ALIAS]));
726
727 done:
728         return NL_SKIP;
729 }
730
731 void
732 swlib_list(void)
733 {
734         if (swlib_priv_init() < 0)
735                 return;
736         swlib_call(SWITCH_CMD_GET_SWITCH, list_switch, NULL, NULL);
737         swlib_priv_free();
738 }
739
740 void
741 swlib_print_portmap(struct switch_dev *dev, char *segment)
742 {
743         int i;
744
745         if (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)
751                                         printf("%d ", i);
752                 } else for (i = 0; i < dev->ports; i++) {
753                         if (dev->maps[i].segment && !strcmp(dev->maps[i].segment, segment))
754                                 printf("%d ", i);
755                 }
756         } else {
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);
763                         else
764                                 printf("port%d:\tdisabled\n", i);
765         }
766 }
767
768 struct switch_dev *
769 swlib_connect(const char *name)
770 {
771         struct swlib_scan_arg arg;
772
773         if (!refcount) {
774                 if (swlib_priv_init() < 0)
775                         return NULL;
776         };
777
778         arg.head = NULL;
779         arg.ptr = NULL;
780         arg.name = name;
781         swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg);
782
783         if (!refcount)
784                 swlib_priv_free();
785
786         return arg.head;
787 }
788
789 static void
790 swlib_free_attributes(struct switch_attr **head)
791 {
792         struct switch_attr *a = *head;
793         struct switch_attr *next;
794
795         while (a) {
796                 next = a->next;
797                 free(a->name);
798                 free(a->description);
799                 free(a);
800                 a = next;
801         }
802         *head = NULL;
803 }
804
805 static void
806 swlib_free_port_map(struct switch_dev *dev)
807 {
808         int i;
809
810         if (!dev || !dev->maps)
811                 return;
812
813         for (i = 0; i < dev->ports; i++)
814                 free(dev->maps[i].segment);
815         free(dev->maps);
816 }
817
818 void
819 swlib_free(struct switch_dev *dev)
820 {
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);
825         free(dev->name);
826         free(dev->alias);
827         free(dev);
828
829         if (--refcount == 0)
830                 swlib_priv_free();
831 }
832
833 void
834 swlib_free_all(struct switch_dev *dev)
835 {
836         struct switch_dev *p;
837
838         while (dev) {
839                 p = dev->next;
840                 swlib_free(dev);
841                 dev = p;
842         }
843 }