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