swlib.c 16 KB

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