2
0

extdev.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2015 Arne Kappen <arne.kappen@hhi.fraunhofer.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. *
  15. * extdev - external device handler interface
  16. *
  17. * This allows to integrate external daemons that configure network devices
  18. * with netifd. At startup, netifd generates device handler stubs from
  19. * descriptions in /lib/netifd/extdev-config and adds them to the list of
  20. * device handlers. A device handler is an instance of struct device_type
  21. * The descriptions are in JSON format and specify
  22. * - names of the device type and of the external device handler on ubus,
  23. * - whether the device is bridge-like,
  24. * - a prefix for device names,
  25. * - the UCI config options for devices of this type, and
  26. * - the format of calls to dump() and info()
  27. * These device handlers stubs act as relays forwarding calls against the
  28. * device handler interface to the external daemon.
  29. */
  30. #include <libubox/blobmsg.h>
  31. #include <libubox/list.h>
  32. #include <libubus.h>
  33. #include <assert.h>
  34. #include "netifd.h"
  35. #include "handler.h"
  36. #include "device.h"
  37. #include "ubus.h"
  38. #include "extdev.h"
  39. #include "interface.h"
  40. #include "system.h"
  41. static struct blob_buf b;
  42. static int confdir_fd = -1;
  43. struct extdev_type {
  44. struct device_type handler;
  45. const char *name;
  46. uint32_t peer_id;
  47. struct ubus_subscriber ubus_sub;
  48. bool subscribed;
  49. struct ubus_event_handler obj_wait;
  50. struct uci_blob_param_list *config_params;
  51. char *config_strbuf;
  52. struct uci_blob_param_list *info_params;
  53. char *info_strbuf;
  54. struct uci_blob_param_list *stats_params;
  55. char *stats_strbuf;
  56. };
  57. struct extdev_device {
  58. struct device dev;
  59. struct extdev_type *etype;
  60. const char *dep_name;
  61. struct uloop_timeout retry;
  62. };
  63. struct extdev_bridge {
  64. struct extdev_device edev;
  65. device_state_cb set_state;
  66. struct blob_attr *config;
  67. bool empty;
  68. struct blob_attr *ifnames;
  69. bool active;
  70. bool force_active;
  71. struct uloop_timeout retry;
  72. struct vlist_tree members;
  73. int n_present;
  74. int n_failed;
  75. };
  76. struct extdev_bridge_member {
  77. struct vlist_node node;
  78. struct extdev_bridge *parent_br;
  79. struct device_user dev_usr;
  80. bool present;
  81. char *name;
  82. };
  83. static void __bridge_config_init(struct extdev_bridge *ebr);
  84. static enum dev_change_type __bridge_reload(struct extdev_bridge *ebr, struct blob_attr *config);
  85. enum {
  86. METHOD_CREATE,
  87. METHOD_CONFIG_INIT,
  88. METHOD_RELOAD,
  89. METHOD_DUMP_INFO,
  90. METHOD_DUMP_STATS,
  91. METHOD_CHECK_STATE,
  92. METHOD_FREE,
  93. METHOD_HOTPLUG_PREPARE,
  94. METHOD_HOTPLUG_ADD,
  95. METHOD_HOTPLUG_REMOVE,
  96. __METHODS_MAX
  97. };
  98. static const char *__extdev_methods[__METHODS_MAX] = {
  99. [METHOD_CREATE] = "create",
  100. [METHOD_CONFIG_INIT] = "config_init",
  101. [METHOD_RELOAD] = "reload",
  102. [METHOD_DUMP_INFO] = "dump_info",
  103. [METHOD_DUMP_STATS] = "dump_stats",
  104. [METHOD_CHECK_STATE] = "check_state",
  105. [METHOD_FREE] = "free",
  106. [METHOD_HOTPLUG_PREPARE] = "prepare",
  107. [METHOD_HOTPLUG_ADD] = "add",
  108. [METHOD_HOTPLUG_REMOVE] = "remove",
  109. };
  110. static inline int
  111. netifd_extdev_create(struct extdev_device *edev, struct blob_attr *msg)
  112. {
  113. D(DEVICE, "create %s '%s' at external device handler\n", edev->dev.type->name,
  114. edev->dev.ifname);
  115. return netifd_extdev_invoke(edev->etype->peer_id, __extdev_methods[METHOD_CREATE], msg,
  116. NULL, NULL);
  117. }
  118. static inline int
  119. netifd_extdev_reload(struct extdev_device *edev, struct blob_attr *msg)
  120. {
  121. D(DEVICE, "reload %s '%s' at external device handler\n", edev->dev.type->name,
  122. edev->dev.ifname);
  123. return netifd_extdev_invoke(edev->etype->peer_id, __extdev_methods[METHOD_RELOAD], msg,
  124. NULL, NULL);
  125. }
  126. static inline int
  127. netifd_extdev_free(struct extdev_device *edev, struct blob_attr *msg)
  128. {
  129. D(DEVICE, "delete %s '%s' with external device handler\n", edev->dev.type->name,
  130. edev->dev.ifname);
  131. return netifd_extdev_invoke(edev->etype->peer_id, __extdev_methods[METHOD_FREE], msg,
  132. NULL, NULL);
  133. }
  134. static inline int
  135. netifd_extdev_prepare(struct extdev_bridge *ebr, struct blob_attr *msg)
  136. {
  137. D(DEVICE, "prepare %s bridge '%s' at external device handler\n", ebr->edev.dev.type->name,
  138. ebr->edev.dev.ifname);
  139. return netifd_extdev_invoke(ebr->edev.etype->peer_id,
  140. __extdev_methods[METHOD_HOTPLUG_PREPARE], msg, NULL, NULL);
  141. }
  142. static inline int
  143. netifd_extdev_add(struct extdev_bridge *ebr, struct blob_attr *msg)
  144. {
  145. D(DEVICE, "add a member to %s bridge '%s' at external device handler\n",
  146. ebr->edev.dev.type->name, ebr->edev.dev.ifname);
  147. return netifd_extdev_invoke(ebr->edev.etype->peer_id,
  148. __extdev_methods[METHOD_HOTPLUG_ADD], msg,NULL, NULL);
  149. }
  150. static inline int
  151. netifd_extdev_remove(struct extdev_bridge *ebr, struct blob_attr *msg)
  152. {
  153. D(DEVICE, "remove a member from %s bridge '%s' at external device handler\n",
  154. ebr->edev.dev.type->name, ebr->edev.dev.ifname);
  155. return netifd_extdev_invoke(ebr->edev.etype->peer_id,
  156. __extdev_methods[METHOD_HOTPLUG_REMOVE], msg, NULL, NULL);
  157. }
  158. static inline void
  159. extdev_invocation_error(int error, const char *method, const char *devname)
  160. {
  161. netifd_log_message(L_CRIT, "'%s' failed for '%s': %s\n",
  162. method, devname, ubus_strerror(error));
  163. }
  164. static struct ubus_method extdev_ubus_obj_methods[] = {};
  165. static struct ubus_object_type extdev_ubus_object_type =
  166. UBUS_OBJECT_TYPE("netifd_extdev", extdev_ubus_obj_methods);
  167. static int
  168. extdev_lookup_id(struct extdev_type *etype)
  169. {
  170. int ret = UBUS_STATUS_UNKNOWN_ERROR;
  171. if (!etype || !etype->name)
  172. goto error;
  173. ret = ubus_lookup_id(ubus_ctx, etype->name, &etype->peer_id);
  174. if (ret)
  175. goto error;
  176. return 0;
  177. error:
  178. netifd_log_message(L_CRIT, "Could not find '%s' ubus ID: %s\n",
  179. etype->name, ubus_strerror(ret));
  180. return ret;
  181. }
  182. static int
  183. extdev_ext_ubus_obj_wait(struct ubus_event_handler *h)
  184. {
  185. return ubus_register_event_handler(ubus_ctx, h, "ubus.object.add");
  186. }
  187. static int
  188. extdev_subscribe(struct extdev_type *etype)
  189. {
  190. int ret;
  191. ret = extdev_lookup_id(etype);
  192. if (ret) {
  193. etype->subscribed = false;
  194. return ret;
  195. }
  196. ret = ubus_subscribe(ubus_ctx, &etype->ubus_sub, etype->peer_id);
  197. if (ret) {
  198. etype->subscribed = false;
  199. extdev_ext_ubus_obj_wait(&etype->obj_wait);
  200. } else {
  201. netifd_log_message(L_NOTICE, "subscribed to external device handler '%s'\n",
  202. etype->name);
  203. etype->subscribed = true;
  204. }
  205. return ret;
  206. }
  207. static void
  208. extdev_wait_ev_cb(struct ubus_context *ctx, struct ubus_event_handler *ev_handler,
  209. const char *type, struct blob_attr *msg)
  210. {
  211. static const struct blobmsg_policy wait_policy = {
  212. "path", BLOBMSG_TYPE_STRING
  213. };
  214. struct blob_attr *attr;
  215. const char *path;
  216. struct extdev_type *etype;
  217. etype = container_of(ev_handler, struct extdev_type, obj_wait);
  218. if (strcmp(type, "ubus.object.add"))
  219. return;
  220. blobmsg_parse(&wait_policy, 1, &attr, blob_data(msg), blob_len(msg));
  221. if (!attr)
  222. return;
  223. path = blobmsg_data(attr);
  224. if (strcmp(etype->name, path))
  225. return;
  226. extdev_subscribe(etype);
  227. }
  228. static int
  229. extdev_bridge_disable_interface(struct extdev_bridge *ebr)
  230. {
  231. int ret;
  232. if (!ebr->active)
  233. return 0;
  234. blob_buf_init(&b, 0);
  235. blobmsg_add_string(&b, "name", ebr->edev.dev.ifname);
  236. ret = netifd_extdev_free(&ebr->edev, b.head);
  237. if (ret && ret != UBUS_STATUS_NOT_FOUND)
  238. goto error;
  239. ebr->active = false;
  240. return 0;
  241. error:
  242. extdev_invocation_error(ret, __extdev_methods[METHOD_FREE], ebr->edev.dev.ifname);
  243. return ret;
  244. }
  245. static int
  246. extdev_bridge_enable_interface(struct extdev_bridge *ebr)
  247. {
  248. int ret;
  249. if (ebr->active)
  250. return 0;
  251. ret = netifd_extdev_create(&ebr->edev, ebr->config);
  252. if (ret)
  253. goto error;
  254. ebr->active = true;
  255. return 0;
  256. error:
  257. extdev_invocation_error(ret, __extdev_methods[METHOD_CREATE], ebr->edev.dev.ifname);
  258. return ret;
  259. }
  260. static int
  261. extdev_bridge_enable_member(struct extdev_bridge_member *ubm)
  262. {
  263. int ret;
  264. struct extdev_bridge *ebr = ubm->parent_br;
  265. D(DEVICE, "%s enable member %s\n", ebr->edev.dev.ifname, ubm->name);
  266. if (!ubm->present)
  267. return 0;
  268. ret = extdev_bridge_enable_interface(ebr);
  269. if (ret)
  270. goto error;
  271. ret = device_claim(&ubm->dev_usr);
  272. if (ret < 0)
  273. goto error;
  274. blob_buf_init(&b, 0);
  275. blobmsg_add_string(&b, "bridge", ebr->edev.dev.ifname);
  276. blobmsg_add_string(&b, "member", ubm->dev_usr.dev->ifname);
  277. /* use hotplug add as addif equivalent. Maybe we need a dedicated ubus
  278. * method on the external handler for this sort of operation. */
  279. ret = netifd_extdev_add(ebr, b.head);
  280. if (ret) {
  281. extdev_invocation_error(ret, __extdev_methods[METHOD_HOTPLUG_ADD],
  282. ubm->dev_usr.dev->ifname);
  283. goto error;
  284. }
  285. device_set_present(&ebr->edev.dev, true);
  286. device_broadcast_event(&ebr->edev.dev, DEV_EVENT_TOPO_CHANGE);
  287. return 0;
  288. error:
  289. D(DEVICE, "%s: failed to enable member '%s'\n", ebr->edev.dev.ifname, ubm->name);
  290. ebr->n_failed++;
  291. ubm->present = false;
  292. ebr->n_present--;
  293. return ret;
  294. }
  295. static int
  296. extdev_bridge_disable_member(struct extdev_bridge_member *ubm)
  297. {
  298. int ret;
  299. struct extdev_bridge *ebr = ubm->parent_br;
  300. if (!ubm->present)
  301. return 0;
  302. D(DEVICE, "%s disable member %s\n", ubm->parent_br->edev.dev.ifname, ubm->name);
  303. blob_buf_init(&b, 0);
  304. blobmsg_add_string(&b, "bridge", ebr->edev.dev.ifname);
  305. blobmsg_add_string(&b, "member", ubm->dev_usr.dev->ifname);
  306. /* use hotplug remove as delif equivalent. Maybe we need a dedicated
  307. * ubus method on the external handler for this sort of operation. */
  308. ret = netifd_extdev_remove(ebr, b.head);
  309. /* continue in case of NOT FOUND since we're trying to remove anyway */
  310. if (ret && ret != UBUS_STATUS_NOT_FOUND)
  311. goto error;
  312. device_release(&ubm->dev_usr);
  313. device_broadcast_event(&ebr->edev.dev, DEV_EVENT_TOPO_CHANGE);
  314. return 0;
  315. error:
  316. extdev_invocation_error(ret, __extdev_methods[METHOD_HOTPLUG_REMOVE],
  317. ubm->dev_usr.dev->ifname);
  318. return ret;
  319. }
  320. static int
  321. extdev_bridge_set_down(struct extdev_bridge *ebr)
  322. {
  323. D(DEVICE, "set %s bridge %s down\n", ebr->edev.dev.type->name, ebr->edev.dev.ifname);
  324. struct extdev_bridge_member *ubm;
  325. ebr->set_state(&ebr->edev.dev, false);
  326. vlist_for_each_element(&ebr->members, ubm, node)
  327. extdev_bridge_disable_member(ubm);
  328. extdev_bridge_disable_interface(ebr);
  329. return 0;
  330. }
  331. static void
  332. extdev_bridge_check_retry(struct extdev_bridge *ebr)
  333. {
  334. if (!ebr->n_failed)
  335. return;
  336. uloop_timeout_set(&ebr->retry, 200);
  337. }
  338. static int
  339. extdev_bridge_set_up(struct extdev_bridge *ebr)
  340. {
  341. D(DEVICE, "set %s bridge %s up\n", ebr->edev.dev.type->name, ebr->edev.dev.ifname);
  342. struct extdev_bridge_member *ubm;
  343. int ret;
  344. if (!ebr->n_present) {
  345. if (!ebr->force_active)
  346. return -ENOENT;
  347. ret = extdev_bridge_enable_interface(ebr);
  348. if (ret)
  349. return ret;
  350. }
  351. ebr->n_failed = 0;
  352. vlist_for_each_element(&ebr->members, ubm, node)
  353. extdev_bridge_enable_member(ubm);
  354. extdev_bridge_check_retry(ebr);
  355. if (!ebr->force_active && !ebr->n_present) {
  356. extdev_bridge_disable_interface(ebr);
  357. device_set_present(&ebr->edev.dev, false);
  358. return -ENOENT;
  359. }
  360. return 0;
  361. }
  362. static int
  363. extdev_bridge_set_state(struct device *dev, bool up)
  364. {
  365. struct extdev_bridge *ebr;
  366. if (!dev->type->bridge_capability)
  367. return -1;
  368. ebr = container_of(dev, struct extdev_bridge, edev.dev);
  369. if (up)
  370. return extdev_bridge_set_up(ebr);
  371. else
  372. return extdev_bridge_set_down(ebr);
  373. }
  374. static void
  375. extdev_bridge_remove_member(struct extdev_bridge_member *member)
  376. {
  377. struct extdev_bridge *ebr = member->parent_br;
  378. if (!member->present)
  379. return;
  380. if (ebr->edev.dev.active)
  381. extdev_bridge_disable_member(member);
  382. member->present = false;
  383. ebr->n_present--;
  384. if (ebr->empty)
  385. return;
  386. ebr->force_active = false;
  387. if (ebr->n_present == 0)
  388. device_set_present(&ebr->edev.dev, false);
  389. }
  390. static void
  391. extdev_bridge_member_cb(struct device_user *usr, enum device_event event)
  392. {
  393. int ret;
  394. struct extdev_bridge_member *ubm;
  395. struct extdev_bridge *ebr;
  396. ubm = container_of(usr, struct extdev_bridge_member, dev_usr);
  397. ebr = ubm->parent_br;
  398. switch (event) {
  399. case DEV_EVENT_ADD:
  400. assert(!ubm->present);
  401. ubm->present = true;
  402. ebr->n_present++;
  403. /* if this member is the first one that is brought up,
  404. * create the bridge at the external device handler */
  405. if (ebr->n_present == 1) {
  406. ret = netifd_extdev_create(&ebr->edev, ebr->config);
  407. if (ret)
  408. goto error;
  409. ebr->active = true;
  410. ret = ebr->set_state(&ebr->edev.dev, true);
  411. if (ret < 0)
  412. extdev_bridge_set_down(ebr);
  413. device_set_present(&ebr->edev.dev, true);
  414. }
  415. extdev_bridge_enable_member(ubm);
  416. break;
  417. case DEV_EVENT_REMOVE:
  418. if (usr->hotplug) {
  419. vlist_delete(&ebr->members, &ubm->node);
  420. return;
  421. }
  422. if (ubm->present)
  423. extdev_bridge_remove_member(ubm);
  424. break;
  425. default:
  426. break;
  427. }
  428. return;
  429. error:
  430. netifd_log_message(L_CRIT, "Failed to create %s bridge %s: %s\n",
  431. ebr->edev.dev.type->name, ebr->edev.dev.ifname, ubus_strerror(ret));
  432. ubm->present = false;
  433. ebr->n_present--;
  434. }
  435. static void
  436. __bridge_enable_members(struct extdev_bridge *ebr)
  437. {
  438. struct extdev_bridge_member *cur;
  439. ebr->n_failed = 0;
  440. vlist_for_each_element(&ebr->members, cur, node) {
  441. if (cur->present)
  442. continue;
  443. if (!cur->dev_usr.dev->present)
  444. continue;
  445. cur->present = true;
  446. ebr->n_present++;
  447. extdev_bridge_enable_member(cur);
  448. }
  449. }
  450. static void
  451. extdev_bridge_retry_enable_members(struct uloop_timeout *timeout)
  452. {
  453. struct extdev_bridge *ebr = container_of(timeout, struct extdev_bridge, retry);
  454. D(DEVICE, "%s retry enable members\n", ebr->edev.dev.ifname);
  455. __bridge_enable_members(ebr);
  456. }
  457. static struct extdev_bridge_member *
  458. extdev_bridge_create_member(struct extdev_bridge *ebr, struct device *dev)
  459. {
  460. struct extdev_bridge_member *ubm;
  461. char *name;
  462. ubm = calloc_a(sizeof(*ubm), &name, strlen(dev->ifname) + 1);
  463. if (!ubm)
  464. return NULL;
  465. ubm->parent_br = ebr;
  466. ubm->name = name;
  467. strcpy(name, dev->ifname);
  468. ubm->dev_usr.dev = dev;
  469. ubm->dev_usr.cb = extdev_bridge_member_cb;
  470. vlist_add(&ebr->members, &ubm->node, ubm->name);
  471. /* Need to look up the bridge member again as the above
  472. * created pointer will be freed in case the bridge member
  473. * already existed */
  474. ubm = vlist_find(&ebr->members, dev->ifname, ubm, node);
  475. if (!ubm)
  476. return NULL;
  477. return ubm;
  478. }
  479. static void
  480. extdev_bridge_add_member(struct extdev_bridge *ebr, const char *name)
  481. {
  482. D(DEVICE, "%s add member %s\n", ebr->edev.dev.ifname, name);
  483. struct device *dev;
  484. dev = device_get(name, 1);
  485. if (!dev)
  486. return;
  487. extdev_bridge_create_member(ebr, dev);
  488. }
  489. /* TODO: how to handle vlan arg? */
  490. static int
  491. extdev_hotplug_add(struct device *ebr_dev, struct device *ebm_dev, struct blob_attr *vlan)
  492. {
  493. D(DEVICE, "%s hotplug add member %s\n", ebr_dev->ifname, ebm_dev->ifname);
  494. struct extdev_bridge *ebr;
  495. struct extdev_bridge_member *ubm;
  496. if (!ebr_dev->type->bridge_capability)
  497. return UBUS_STATUS_NOT_SUPPORTED;
  498. ebr = container_of(ebr_dev, struct extdev_bridge, edev.dev);
  499. if (!ebr->edev.etype->subscribed)
  500. return UBUS_STATUS_NOT_FOUND;
  501. ubm = extdev_bridge_create_member(ebr, ebm_dev);
  502. if (!ubm)
  503. return UBUS_STATUS_UNKNOWN_ERROR;
  504. device_broadcast_event(&ebr->edev.dev, DEV_EVENT_TOPO_CHANGE);
  505. return 0;
  506. }
  507. static int
  508. extdev_hotplug_remove(struct device *dev, struct device *member, struct blob_attr *vlan)
  509. {
  510. struct extdev_bridge *ebr;
  511. struct extdev_bridge_member *ubm;
  512. if (!dev->type->bridge_capability)
  513. return UBUS_STATUS_NOT_SUPPORTED;
  514. ebr = container_of(dev, struct extdev_bridge, edev.dev);
  515. if (!ebr->edev.etype->subscribed)
  516. return UBUS_STATUS_NOT_FOUND;
  517. ubm = vlist_find(&ebr->members, member->ifname, ubm, node);
  518. if (!ubm)
  519. return UBUS_STATUS_NOT_FOUND;
  520. vlist_delete(&ebr->members, &ubm->node);
  521. extdev_bridge_remove_member(ubm);
  522. return 0;
  523. }
  524. static int
  525. extdev_hotplug_prepare(struct device *dev, struct device **bridge_dev)
  526. {
  527. struct extdev_bridge *ebr;
  528. int ret;
  529. if (!dev->type->bridge_capability)
  530. return UBUS_STATUS_NOT_SUPPORTED;
  531. if (bridge_dev)
  532. *bridge_dev = dev;
  533. ebr = container_of(dev, struct extdev_bridge, edev.dev);
  534. blob_buf_init(&b, 0);
  535. blobmsg_add_string(&b, "name", dev->ifname);
  536. ret = netifd_extdev_prepare(ebr, b.head);
  537. if (ret)
  538. goto error;
  539. ebr->force_active = true;
  540. device_set_present(&ebr->edev.dev, true);
  541. return 0;
  542. error:
  543. extdev_invocation_error(ret, __extdev_methods[METHOD_HOTPLUG_PREPARE], dev->ifname);
  544. return ret;
  545. }
  546. static void
  547. extdev_bridge_free_member(struct extdev_bridge_member *ubm)
  548. {
  549. struct device *dev = ubm->dev_usr.dev;
  550. extdev_bridge_remove_member(ubm);
  551. device_remove_user(&ubm->dev_usr);
  552. if (dev->present) {
  553. device_set_present(dev, false);
  554. device_set_present(dev, true);
  555. }
  556. free(ubm);
  557. }
  558. static void
  559. extdev_bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
  560. struct vlist_node *node_old)
  561. {
  562. struct extdev_bridge_member *ubm;
  563. struct device *dev;
  564. if (node_new) {
  565. ubm = container_of(node_new, struct extdev_bridge_member, node);
  566. if (node_old) {
  567. free(ubm);
  568. return;
  569. }
  570. dev = ubm->dev_usr.dev;
  571. ubm->dev_usr.dev = NULL;
  572. device_add_user(&ubm->dev_usr, dev);
  573. }
  574. if (node_old) {
  575. ubm = container_of(node_old, struct extdev_bridge_member, node);
  576. extdev_bridge_free_member(ubm);
  577. }
  578. }
  579. static void
  580. bridge_dependency_retry(struct uloop_timeout *timeout)
  581. {
  582. struct extdev_bridge *ebr;
  583. ebr = container_of(timeout, struct extdev_bridge, edev.retry);
  584. __bridge_reload(ebr, NULL);
  585. }
  586. static void
  587. __buf_add_all(struct blob_attr *attr)
  588. {
  589. struct blob_attr *cur;
  590. size_t rem;
  591. blobmsg_for_each_attr(cur, attr, rem)
  592. blobmsg_add_field(&b, blobmsg_type(cur), blobmsg_name(cur), blobmsg_data(cur),
  593. blobmsg_data_len(cur));
  594. }
  595. enum {
  596. BRIDGE_EMPTY,
  597. BRIDGE_IFNAMES,
  598. BRIDGE_DEPENDS_ON,
  599. __BRIDGE_MAX
  600. };
  601. static const struct blobmsg_policy brpol[__BRIDGE_MAX] = {
  602. [BRIDGE_EMPTY] = { "empty", BLOBMSG_TYPE_BOOL },
  603. [BRIDGE_IFNAMES] = { "ifname", BLOBMSG_TYPE_ARRAY },
  604. [BRIDGE_DEPENDS_ON] = { "depends_on", BLOBMSG_TYPE_STRING },
  605. };
  606. static enum dev_change_type
  607. __do_bridge_reload(struct extdev_bridge *ebr, struct blob_attr *config)
  608. {
  609. void *cfg_table;
  610. int ret;
  611. blob_buf_init(&b, 0);
  612. cfg_table = blobmsg_open_table(&b, "old");
  613. __buf_add_all(ebr->config);
  614. blobmsg_close_table(&b, cfg_table);
  615. cfg_table = blobmsg_open_table(&b, "new");
  616. __buf_add_all(config);
  617. blobmsg_close_table(&b, cfg_table);
  618. ret = netifd_extdev_reload(&ebr->edev, b.head);
  619. if (ret) {
  620. netifd_log_message(L_WARNING, "%s config reload failed: %s\n",
  621. ebr->edev.dev.ifname, ubus_strerror(ret));
  622. return DEV_CONFIG_RECREATE;
  623. } else {
  624. return DEV_CONFIG_RESTART;
  625. }
  626. }
  627. static enum dev_change_type
  628. __bridge_reload(struct extdev_bridge *ebr, struct blob_attr *config)
  629. {
  630. int n_params = ebr->edev.dev.type->config_params->n_params;
  631. struct blob_attr *tb[__BRIDGE_MAX];
  632. const struct uci_blob_param_list *config_params;
  633. const struct blobmsg_policy *pol;
  634. struct blob_attr *old_tb[n_params], *brtb[n_params];
  635. enum dev_change_type change = DEV_CONFIG_APPLIED;
  636. struct device *dev;
  637. unsigned long diff = 0;
  638. if (config) {
  639. config = blob_memdup(config);
  640. blobmsg_parse(brpol, __BRIDGE_MAX, tb, blobmsg_data(config), blobmsg_len(config));
  641. ebr->edev.dep_name = blobmsg_get_string(tb[BRIDGE_DEPENDS_ON]);
  642. if (tb[BRIDGE_EMPTY] && blobmsg_get_bool(tb[BRIDGE_EMPTY]))
  643. ebr->empty = true;
  644. if (ebr->config) {
  645. config_params = ebr->edev.dev.type->config_params;
  646. pol = config_params->params;
  647. blobmsg_parse(pol, n_params, old_tb, blobmsg_data(ebr->config),
  648. blobmsg_len(ebr->config));
  649. blobmsg_parse(pol, n_params, brtb, blobmsg_data(config), blobmsg_len
  650. (config));
  651. diff = 0;
  652. uci_blob_diff(brtb, old_tb, config_params, &diff);
  653. if (diff) {
  654. if (diff & ~(1 << BRIDGE_IFNAMES)) {
  655. change = DEV_CONFIG_RESTART;
  656. } else {
  657. change = __do_bridge_reload(ebr, config);
  658. }
  659. free(ebr->config);
  660. }
  661. }
  662. ebr->ifnames = tb[BRIDGE_IFNAMES];
  663. ebr->config = config;
  664. }
  665. if (ebr->edev.dep_name) {
  666. dev = device_get(ebr->edev.dep_name, 0);
  667. if (!(dev && dev->current_config)) {
  668. D(DEVICE, "%s: cannot yet init config since dependency '%s' is not ready\n",
  669. ebr->edev.dev.ifname, ebr->edev.dep_name);
  670. ebr->edev.retry.cb = bridge_dependency_retry;
  671. uloop_timeout_set(&ebr->edev.retry, 200);
  672. return DEV_CONFIG_RESTART;
  673. }
  674. }
  675. __bridge_config_init(ebr);
  676. ebr->edev.dev.config_pending = false;
  677. uloop_timeout_cancel(&ebr->edev.retry);
  678. return change;
  679. }
  680. static enum dev_change_type
  681. __reload(struct extdev_device *edev, struct blob_attr *config)
  682. {
  683. unsigned long diff = 0;
  684. struct uci_blob_param_list *params;
  685. params = edev->etype->config_params;
  686. struct blob_attr *tb[params->n_params];
  687. struct blob_attr *old_tb[params->n_params];
  688. blobmsg_parse(params->params, params->n_params, tb, blobmsg_data(config),
  689. blobmsg_len(config));
  690. blobmsg_parse(params->params, params->n_params, old_tb, blobmsg_data(edev->dev.config),
  691. blobmsg_len(edev->dev.config));
  692. uci_blob_diff(tb, old_tb, edev->etype->config_params, &diff);
  693. if (!diff)
  694. return DEV_CONFIG_NO_CHANGE;
  695. // TODO: make reload ubus call with old and new config
  696. device_set_present(&edev->dev, false);
  697. device_set_present(&edev->dev, true);
  698. return DEV_CONFIG_APPLIED;
  699. }
  700. static enum dev_change_type
  701. extdev_reload(struct device *dev, struct blob_attr *config)
  702. {
  703. struct extdev_type *etype;
  704. struct extdev_device *edev;
  705. struct extdev_bridge *ebr;
  706. etype = container_of(dev->type, struct extdev_type, handler);
  707. if (!etype->subscribed)
  708. return DEV_CONFIG_NO_CHANGE;
  709. edev = container_of(dev, struct extdev_device, dev);
  710. if (dev->type->bridge_capability) {
  711. ebr = container_of(edev, struct extdev_bridge, edev);
  712. return __bridge_reload(ebr, config);
  713. } else {
  714. return __reload(edev, config);
  715. }
  716. }
  717. static struct device*
  718. __create(const char *name, struct device_type *type, struct blob_attr *config)
  719. {
  720. struct extdev_device *edev;
  721. struct extdev_type *etype;
  722. int ret;
  723. etype = container_of(type, struct extdev_type, handler);
  724. edev = calloc(1, sizeof(struct extdev_device));
  725. if (!edev)
  726. return NULL;
  727. ret = device_init(&edev->dev, type, name);
  728. if (ret)
  729. goto error;
  730. edev->etype = etype;
  731. ret = netifd_extdev_create(edev, config);
  732. if (ret)
  733. goto inv_error;
  734. edev->dev.config_pending = false;
  735. return &edev->dev;
  736. inv_error:
  737. extdev_invocation_error(ret, __extdev_methods[METHOD_CREATE], name);
  738. error:
  739. free(edev->dev.config);
  740. device_cleanup(&edev->dev);
  741. free(edev);
  742. netifd_log_message(L_WARNING, "Failed to create %s %s\n", type->name, name);
  743. return NULL;
  744. }
  745. static const struct device_hotplug_ops extdev_hotplug_ops = {
  746. .prepare = extdev_hotplug_prepare,
  747. .add = extdev_hotplug_add,
  748. .del = extdev_hotplug_remove
  749. };
  750. static struct device*
  751. __bridge_create(const char *name, struct device_type *devtype, struct blob_attr *config)
  752. {
  753. struct extdev_bridge *ebr;
  754. ebr = calloc(1, sizeof(*ebr));
  755. if (!ebr)
  756. return NULL;
  757. device_init(&ebr->edev.dev, devtype, name);
  758. ebr->edev.dev.config_pending = true;
  759. ebr->retry.cb = extdev_bridge_retry_enable_members;
  760. ebr->edev.etype = container_of(devtype, struct extdev_type, handler);
  761. ebr->set_state = ebr->edev.dev.set_state;
  762. ebr->edev.dev.set_state = extdev_bridge_set_state;
  763. ebr->edev.dev.hotplug_ops = &extdev_hotplug_ops;
  764. vlist_init(&ebr->members, avl_strcmp, extdev_bridge_member_update);
  765. ebr->members.keep_old = true;
  766. __bridge_reload(ebr, config);
  767. return &ebr->edev.dev;
  768. }
  769. /* Device creation process:
  770. * For bridges without dependencies:
  771. * 1) The bridge state is initialized in netifd. Devices for the members are
  772. * created and added to the members vlist by config_init automatically.
  773. * 2) When the first bridge member device is brought up in
  774. * extdev_bridge_enable_member the 'create' call to the external device
  775. * handler is issued.
  776. * 3) After successful device creation the bridge is marked "present" and a
  777. * new attempt at adding the member is made.
  778. * For bridges with dependencies:
  779. * 1) The bridge state is initialized in netifd. If a dependency is expressed
  780. * via the 'depends_on' UCI option and the dependency is not ready (i.e. it
  781. * does not exist or config_pending == true) the call to
  782. * __bridge_config_init() is postponed and a retry timer is started. Retries
  783. * happen until the dependency is ready. Then, __bridge_config_init() gets
  784. * called and the process continues as with bridges without dependencies
  785. * For regular devices:
  786. * 1) The device structure is created in netifd.
  787. * 2) config_init is called automatically which issues the 'create' call to the
  788. * external device handler.
  789. */
  790. static struct device *
  791. extdev_create(const char *name, struct device_type *devtype, struct blob_attr *config)
  792. {
  793. struct extdev_type *etype = container_of(devtype, struct extdev_type, handler);
  794. if (!etype->subscribed)
  795. return NULL;
  796. if (devtype->bridge_capability)
  797. return __bridge_create(name, devtype, config);
  798. else
  799. return __create(name, devtype, config);
  800. }
  801. static void
  802. extdev_free(struct device *dev)
  803. {
  804. struct extdev_type *etype;
  805. struct extdev_device *edev;
  806. struct extdev_bridge *ebr;
  807. int ret;
  808. etype = container_of(dev->type, struct extdev_type, handler);
  809. edev = container_of(dev, struct extdev_device, dev);
  810. if (!etype->subscribed)
  811. return;
  812. blob_buf_init(&b, 0);
  813. blobmsg_add_string(&b, "name", dev->ifname);
  814. ret = netifd_extdev_free(edev, b.head);
  815. if (ret && ret != UBUS_STATUS_NOT_FOUND)
  816. goto error;
  817. if (dev->type->bridge_capability) {
  818. ebr = container_of(dev, struct extdev_bridge, edev.dev);
  819. vlist_flush_all(&ebr->members);
  820. // vlist_flush_all(&dev->vlans); TODO: do we need this?
  821. free(ebr->config);
  822. free(ebr);
  823. }
  824. return;
  825. error:
  826. extdev_invocation_error(ret, __extdev_methods[METHOD_FREE],
  827. dev->ifname);
  828. }
  829. static void
  830. __bridge_config_init(struct extdev_bridge *ebr)
  831. {
  832. int ret;
  833. size_t rem;
  834. struct blob_attr *cur;
  835. if (ebr->empty) {
  836. ebr->force_active = true;
  837. ret = netifd_extdev_create(&ebr->edev, ebr->config);
  838. if (ret)
  839. goto error;
  840. device_set_present(&ebr->edev.dev, true);
  841. }
  842. ebr->n_failed = 0;
  843. vlist_update(&ebr->members);
  844. if (ebr->ifnames) {
  845. blobmsg_for_each_attr(cur, ebr->ifnames, rem)
  846. extdev_bridge_add_member(ebr, blobmsg_data(cur));
  847. }
  848. vlist_flush(&ebr->members);
  849. extdev_bridge_check_retry(ebr);
  850. return;
  851. error:
  852. fprintf(stderr, "Failed to init config for '%s': %s\n", ebr->edev.dev.ifname,
  853. ubus_strerror(ret));
  854. }
  855. static void
  856. extdev_config_init(struct device *dev)
  857. {
  858. struct extdev_type *etype;
  859. struct extdev_bridge *ebr;
  860. etype = container_of(dev->type, struct extdev_type, handler);
  861. if (!etype->subscribed)
  862. return;
  863. if (dev->type->bridge_capability) {
  864. ebr = container_of(dev, struct extdev_bridge, edev.dev);
  865. __bridge_config_init(ebr);
  866. }
  867. }
  868. static void
  869. extdev_buf_add_list(struct blob_attr *attr, size_t len, const char *name,
  870. struct blob_buf *buf, bool array)
  871. {
  872. struct blob_attr *cur;
  873. struct blobmsg_hdr *hdr;
  874. void *list;
  875. int type;
  876. if (array)
  877. list = blobmsg_open_array(buf, name);
  878. else
  879. list = blobmsg_open_table(buf, name);
  880. blobmsg_for_each_attr(cur, attr, len) {
  881. hdr = blob_data(cur);
  882. type = blobmsg_type(cur);
  883. switch (type) {
  884. case BLOBMSG_TYPE_STRING:
  885. blobmsg_add_string(buf, (char *) hdr->name,
  886. blobmsg_get_string(cur));
  887. break;
  888. case BLOBMSG_TYPE_TABLE:
  889. case BLOBMSG_TYPE_ARRAY:
  890. extdev_buf_add_list(blobmsg_data(cur), blobmsg_data_len(cur),
  891. (char *) hdr->name, buf, type == BLOBMSG_TYPE_ARRAY);
  892. break;
  893. case BLOBMSG_TYPE_INT64:
  894. blobmsg_add_u64(buf, (char *) hdr->name, blobmsg_get_u64(cur));
  895. break;
  896. case BLOBMSG_TYPE_INT32:
  897. blobmsg_add_u32(buf, (char *) hdr->name, blobmsg_get_u32(cur));
  898. break;
  899. case BLOBMSG_TYPE_INT16:
  900. blobmsg_add_u16(buf, (char *) hdr->name, blobmsg_get_u16(cur));
  901. break;
  902. case BLOBMSG_TYPE_INT8:
  903. blobmsg_add_u8(buf, (char *) hdr->name, blobmsg_get_u8(cur));
  904. break;
  905. default:
  906. break;
  907. }
  908. }
  909. if (array)
  910. blobmsg_close_array(buf, list);
  911. else
  912. blobmsg_close_table(buf, list);
  913. }
  914. static void
  915. add_parsed_data(struct blob_attr **tb, const struct blobmsg_policy *policy, int n_params,
  916. struct blob_buf *buf)
  917. {
  918. for (int i = 0; i < n_params; i++) {
  919. if (!tb[i])
  920. continue;
  921. switch (policy[i].type) {
  922. case BLOBMSG_TYPE_STRING:
  923. blobmsg_add_string(buf, policy[i].name, blobmsg_get_string(tb[i]));
  924. break;
  925. case BLOBMSG_TYPE_ARRAY:
  926. case BLOBMSG_TYPE_TABLE:
  927. extdev_buf_add_list(blobmsg_data(tb[i]), blobmsg_data_len(tb[i]),
  928. policy[i].name, buf, policy[i].type == BLOBMSG_TYPE_ARRAY);
  929. break;
  930. case BLOBMSG_TYPE_INT64:
  931. blobmsg_add_u64(buf, policy[i].name, blobmsg_get_u64(tb[i]));
  932. break;
  933. case BLOBMSG_TYPE_INT32:
  934. blobmsg_add_u32(buf, policy[i].name, blobmsg_get_u32(tb[i]));
  935. break;
  936. case BLOBMSG_TYPE_INT16:
  937. blobmsg_add_u16(buf, policy[i].name, blobmsg_get_u16(tb[i]));
  938. break;
  939. case BLOBMSG_TYPE_INT8:
  940. blobmsg_add_u8(buf, policy[i].name, blobmsg_get_u8(tb[i]));
  941. break;
  942. default:
  943. break;
  944. }
  945. }
  946. }
  947. struct dump_data {
  948. const struct device *dev;
  949. struct blob_buf *buf;
  950. };
  951. static void
  952. dump_cb(struct ubus_request *req, int type, struct blob_attr *reply)
  953. {
  954. struct dump_data *data;
  955. struct extdev_type *etype;
  956. const struct blobmsg_policy *info_policy;
  957. int n_params;
  958. struct blob_buf *buf;
  959. data = req->priv;
  960. etype = container_of(data->dev->type, struct extdev_type, handler);
  961. info_policy = etype->info_params->params;
  962. n_params = etype->info_params->n_params;
  963. buf = data->buf;
  964. struct blob_attr *tb[n_params];
  965. blobmsg_parse(info_policy, n_params, tb, blobmsg_data(reply), blobmsg_len(reply));
  966. add_parsed_data(tb, info_policy, n_params, buf);
  967. }
  968. static void
  969. extdev_dump(const char *method, struct device *dev, struct blob_buf *buf)
  970. {
  971. static struct dump_data data;
  972. struct extdev_type *etype;
  973. etype = container_of(dev->type, struct extdev_type, handler);
  974. if (!etype->subscribed)
  975. return;
  976. data.dev = dev;
  977. data.buf = buf;
  978. blob_buf_init(&b, 0);
  979. blobmsg_add_string(&b, "name", dev->ifname);
  980. netifd_extdev_invoke(etype->peer_id, method, b.head, dump_cb, &data);
  981. }
  982. static void
  983. extdev_dump_info(struct device *dev, struct blob_buf *buf)
  984. {
  985. extdev_dump(__extdev_methods[METHOD_DUMP_INFO], dev, buf);
  986. }
  987. static void
  988. extdev_dump_stats(struct device *dev, struct blob_buf *buf)
  989. {
  990. extdev_dump(__extdev_methods[METHOD_DUMP_STATS], dev, buf);
  991. }
  992. static void
  993. extdev_ext_handler_remove_cb(struct ubus_context *ctx,
  994. struct ubus_subscriber *obj, uint32_t id)
  995. {
  996. struct extdev_type *etype;
  997. etype = container_of(obj, struct extdev_type, ubus_sub);
  998. netifd_log_message(L_NOTICE, "%s: external device handler "
  999. "'%s' disappeared. Waiting for it to re-appear.\n",
  1000. etype->handler.name, etype->name);
  1001. etype->peer_id = 0;
  1002. etype->subscribed = false;
  1003. extdev_ext_ubus_obj_wait(&etype->obj_wait);
  1004. }
  1005. static void
  1006. extdev_add_devtype(const char *cfg_file, const char *tname, const char *ubus_name,
  1007. bool bridge_capability, const char *br_prefix, json_object *cfg_obj,
  1008. json_object *info_obj, json_object *stats_obj)
  1009. {
  1010. static const char *OBJ_PREFIX = "network.device.";
  1011. struct extdev_type *etype;
  1012. struct device_type *devtype;
  1013. char *ubus_obj_name, *devtype_name, *ext_dev_handler_name, *name_prefix;
  1014. struct uci_blob_param_list *config_params, *info_params, *stats_params;
  1015. int ret;
  1016. etype = calloc_a(sizeof(*etype),
  1017. &ubus_obj_name, strlen(OBJ_PREFIX) + strlen(ubus_name) + 1,
  1018. &devtype_name, strlen(tname) + 1,
  1019. &ext_dev_handler_name, strlen(ubus_name) + 1,
  1020. &config_params, sizeof(struct uci_blob_param_list),
  1021. &info_params, sizeof(struct uci_blob_param_list),
  1022. &stats_params, sizeof(struct uci_blob_param_list));
  1023. if (!etype)
  1024. return;
  1025. etype->config_params = config_params;
  1026. etype->info_params = info_params;
  1027. etype->name = strcpy(ext_dev_handler_name, ubus_name);
  1028. devtype = &etype->handler;
  1029. devtype->name = strcpy(devtype_name, tname);
  1030. devtype->create = extdev_create;
  1031. devtype->free = extdev_free;
  1032. devtype->config_init = extdev_config_init;
  1033. devtype->reload = extdev_reload;
  1034. devtype->dump_info = extdev_dump_info;
  1035. devtype->dump_stats = extdev_dump_stats;
  1036. devtype->bridge_capability = bridge_capability;
  1037. devtype->config_params = etype->config_params;
  1038. if (bridge_capability) {
  1039. name_prefix = malloc(strlen(br_prefix) + 1);
  1040. if (!name_prefix)
  1041. goto error;
  1042. strcpy(name_prefix, br_prefix);
  1043. devtype->name_prefix = name_prefix;
  1044. }
  1045. /* subscribe to external device handler */
  1046. sprintf(ubus_obj_name, "%s%s", OBJ_PREFIX, ubus_name);
  1047. etype->ubus_sub.obj.name = ubus_obj_name;
  1048. etype->ubus_sub.obj.type = &extdev_ubus_object_type;
  1049. ret = ubus_register_subscriber(ubus_ctx, &etype->ubus_sub);
  1050. if (ret) {
  1051. fprintf(stderr, "Failed to register subscriber object '%s'\n",
  1052. etype->ubus_sub.obj.name);
  1053. goto error;
  1054. }
  1055. etype->obj_wait.cb = extdev_wait_ev_cb;
  1056. etype->ubus_sub.remove_cb = extdev_ext_handler_remove_cb;
  1057. extdev_subscribe(etype);
  1058. /* parse config params from JSON object */
  1059. etype->config_strbuf = netifd_handler_parse_config(etype->config_params, cfg_obj);
  1060. if (!etype->config_strbuf)
  1061. goto error;
  1062. /* parse info dump params from JSON object */
  1063. if (!info_obj) {
  1064. devtype->dump_info = NULL;
  1065. } else {
  1066. etype->info_strbuf = netifd_handler_parse_config(etype->info_params, info_obj);
  1067. if (!etype->info_strbuf)
  1068. devtype->dump_info = NULL;
  1069. }
  1070. /* parse statistics dump params from JSON object */
  1071. if (!stats_obj) {
  1072. devtype->dump_stats = NULL;
  1073. } else {
  1074. etype->stats_strbuf = netifd_handler_parse_config(etype->stats_params, stats_obj);
  1075. if (!etype->stats_strbuf)
  1076. devtype->dump_stats = NULL;
  1077. }
  1078. ret = device_type_add(devtype);
  1079. if (ret)
  1080. goto config_error;
  1081. return;
  1082. config_error:
  1083. free(etype->config_strbuf);
  1084. free(etype->info_strbuf);
  1085. free(etype->stats_strbuf);
  1086. error:
  1087. fprintf(stderr, "Failed to create device handler for device"
  1088. "type '%s' from file '%s'\n", tname, cfg_file);
  1089. free(ubus_obj_name);
  1090. free(devtype_name);
  1091. free(etype);
  1092. }
  1093. /* create extdev device handler stubs from JSON description */
  1094. void
  1095. extdev_init(void)
  1096. {
  1097. confdir_fd = netifd_open_subdir("extdev-config");
  1098. if (confdir_fd < 0)
  1099. return;
  1100. netifd_init_extdev_handlers(confdir_fd, extdev_add_devtype);
  1101. }