extdev.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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. int 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 rem, ret;
  833. struct blob_attr *cur;
  834. if (ebr->empty) {
  835. ebr->force_active = true;
  836. ret = netifd_extdev_create(&ebr->edev, ebr->config);
  837. if (ret)
  838. goto error;
  839. device_set_present(&ebr->edev.dev, true);
  840. }
  841. ebr->n_failed = 0;
  842. vlist_update(&ebr->members);
  843. if (ebr->ifnames) {
  844. blobmsg_for_each_attr(cur, ebr->ifnames, rem)
  845. extdev_bridge_add_member(ebr, blobmsg_data(cur));
  846. }
  847. vlist_flush(&ebr->members);
  848. extdev_bridge_check_retry(ebr);
  849. return;
  850. error:
  851. fprintf(stderr, "Failed to init config for '%s': %s\n", ebr->edev.dev.ifname,
  852. ubus_strerror(ret));
  853. }
  854. static void
  855. extdev_config_init(struct device *dev)
  856. {
  857. struct extdev_type *etype;
  858. struct extdev_bridge *ebr;
  859. etype = container_of(dev->type, struct extdev_type, handler);
  860. if (!etype->subscribed)
  861. return;
  862. if (dev->type->bridge_capability) {
  863. ebr = container_of(dev, struct extdev_bridge, edev.dev);
  864. __bridge_config_init(ebr);
  865. }
  866. }
  867. static void
  868. extdev_buf_add_list(struct blob_attr *attr, int len, const char *name,
  869. struct blob_buf *buf, bool array)
  870. {
  871. struct blob_attr *cur;
  872. struct blobmsg_hdr *hdr;
  873. void *list;
  874. int type;
  875. if (array)
  876. list = blobmsg_open_array(buf, name);
  877. else
  878. list = blobmsg_open_table(buf, name);
  879. blobmsg_for_each_attr(cur, attr, len) {
  880. hdr = blob_data(cur);
  881. type = blobmsg_type(cur);
  882. switch (type) {
  883. case BLOBMSG_TYPE_STRING:
  884. blobmsg_add_string(buf, (char *) hdr->name,
  885. blobmsg_get_string(cur));
  886. break;
  887. case BLOBMSG_TYPE_TABLE:
  888. case BLOBMSG_TYPE_ARRAY:
  889. extdev_buf_add_list(blobmsg_data(cur), blobmsg_data_len(cur),
  890. (char *) hdr->name, buf, type == BLOBMSG_TYPE_ARRAY);
  891. break;
  892. case BLOBMSG_TYPE_INT64:
  893. blobmsg_add_u64(buf, (char *) hdr->name, blobmsg_get_u64(cur));
  894. break;
  895. case BLOBMSG_TYPE_INT32:
  896. blobmsg_add_u32(buf, (char *) hdr->name, blobmsg_get_u32(cur));
  897. break;
  898. case BLOBMSG_TYPE_INT16:
  899. blobmsg_add_u16(buf, (char *) hdr->name, blobmsg_get_u16(cur));
  900. break;
  901. case BLOBMSG_TYPE_INT8:
  902. blobmsg_add_u8(buf, (char *) hdr->name, blobmsg_get_u8(cur));
  903. break;
  904. default:
  905. break;
  906. }
  907. }
  908. if (array)
  909. blobmsg_close_array(buf, list);
  910. else
  911. blobmsg_close_table(buf, list);
  912. }
  913. static void
  914. add_parsed_data(struct blob_attr **tb, const struct blobmsg_policy *policy, int n_params,
  915. struct blob_buf *buf)
  916. {
  917. for (int i = 0; i < n_params; i++) {
  918. if (!tb[i])
  919. continue;
  920. switch (policy[i].type) {
  921. case BLOBMSG_TYPE_STRING:
  922. blobmsg_add_string(buf, policy[i].name, blobmsg_get_string(tb[i]));
  923. break;
  924. case BLOBMSG_TYPE_ARRAY:
  925. case BLOBMSG_TYPE_TABLE:
  926. extdev_buf_add_list(blobmsg_data(tb[i]), blobmsg_data_len(tb[i]),
  927. policy[i].name, buf, policy[i].type == BLOBMSG_TYPE_ARRAY);
  928. break;
  929. case BLOBMSG_TYPE_INT64:
  930. blobmsg_add_u64(buf, policy[i].name, blobmsg_get_u64(tb[i]));
  931. break;
  932. case BLOBMSG_TYPE_INT32:
  933. blobmsg_add_u32(buf, policy[i].name, blobmsg_get_u32(tb[i]));
  934. break;
  935. case BLOBMSG_TYPE_INT16:
  936. blobmsg_add_u16(buf, policy[i].name, blobmsg_get_u16(tb[i]));
  937. break;
  938. case BLOBMSG_TYPE_INT8:
  939. blobmsg_add_u8(buf, policy[i].name, blobmsg_get_u8(tb[i]));
  940. break;
  941. default:
  942. break;
  943. }
  944. }
  945. }
  946. struct dump_data {
  947. const struct device *dev;
  948. struct blob_buf *buf;
  949. };
  950. static void
  951. dump_cb(struct ubus_request *req, int type, struct blob_attr *reply)
  952. {
  953. struct dump_data *data;
  954. struct extdev_type *etype;
  955. const struct blobmsg_policy *info_policy;
  956. int n_params;
  957. struct blob_buf *buf;
  958. data = req->priv;
  959. etype = container_of(data->dev->type, struct extdev_type, handler);
  960. info_policy = etype->info_params->params;
  961. n_params = etype->info_params->n_params;
  962. buf = data->buf;
  963. struct blob_attr *tb[n_params];
  964. blobmsg_parse(info_policy, n_params, tb, blobmsg_data(reply), blobmsg_len(reply));
  965. add_parsed_data(tb, info_policy, n_params, buf);
  966. }
  967. static void
  968. extdev_dump(const char *method, struct device *dev, struct blob_buf *buf)
  969. {
  970. static struct dump_data data;
  971. struct extdev_type *etype;
  972. etype = container_of(dev->type, struct extdev_type, handler);
  973. if (!etype->subscribed)
  974. return;
  975. data.dev = dev;
  976. data.buf = buf;
  977. blob_buf_init(&b, 0);
  978. blobmsg_add_string(&b, "name", dev->ifname);
  979. netifd_extdev_invoke(etype->peer_id, method, b.head, dump_cb, &data);
  980. }
  981. static void
  982. extdev_dump_info(struct device *dev, struct blob_buf *buf)
  983. {
  984. extdev_dump(__extdev_methods[METHOD_DUMP_INFO], dev, buf);
  985. }
  986. static void
  987. extdev_dump_stats(struct device *dev, struct blob_buf *buf)
  988. {
  989. extdev_dump(__extdev_methods[METHOD_DUMP_STATS], dev, buf);
  990. }
  991. static void
  992. extdev_ext_handler_remove_cb(struct ubus_context *ctx,
  993. struct ubus_subscriber *obj, uint32_t id)
  994. {
  995. struct extdev_type *etype;
  996. etype = container_of(obj, struct extdev_type, ubus_sub);
  997. netifd_log_message(L_NOTICE, "%s: external device handler "
  998. "'%s' disappeared. Waiting for it to re-appear.\n",
  999. etype->handler.name, etype->name);
  1000. etype->peer_id = 0;
  1001. etype->subscribed = false;
  1002. extdev_ext_ubus_obj_wait(&etype->obj_wait);
  1003. }
  1004. static void
  1005. extdev_add_devtype(const char *cfg_file, const char *tname, const char *ubus_name,
  1006. bool bridge_capability, const char *br_prefix, json_object *cfg_obj,
  1007. json_object *info_obj, json_object *stats_obj)
  1008. {
  1009. static const char *OBJ_PREFIX = "network.device.";
  1010. struct extdev_type *etype;
  1011. struct device_type *devtype;
  1012. char *ubus_obj_name, *devtype_name, *ext_dev_handler_name, *name_prefix;
  1013. struct uci_blob_param_list *config_params, *info_params, *stats_params;
  1014. int ret;
  1015. etype = calloc_a(sizeof(*etype),
  1016. &ubus_obj_name, strlen(OBJ_PREFIX) + strlen(ubus_name) + 1,
  1017. &devtype_name, strlen(tname) + 1,
  1018. &ext_dev_handler_name, strlen(ubus_name) + 1,
  1019. &config_params, sizeof(struct uci_blob_param_list),
  1020. &info_params, sizeof(struct uci_blob_param_list),
  1021. &stats_params, sizeof(struct uci_blob_param_list));
  1022. if (!etype)
  1023. return;
  1024. etype->config_params = config_params;
  1025. etype->info_params = info_params;
  1026. etype->name = strcpy(ext_dev_handler_name, ubus_name);
  1027. devtype = &etype->handler;
  1028. devtype->name = strcpy(devtype_name, tname);
  1029. devtype->create = extdev_create;
  1030. devtype->free = extdev_free;
  1031. devtype->config_init = extdev_config_init;
  1032. devtype->reload = extdev_reload;
  1033. devtype->dump_info = extdev_dump_info;
  1034. devtype->dump_stats = extdev_dump_stats;
  1035. devtype->bridge_capability = bridge_capability;
  1036. devtype->config_params = etype->config_params;
  1037. if (bridge_capability) {
  1038. name_prefix = malloc(strlen(br_prefix) + 1);
  1039. if (!name_prefix)
  1040. goto error;
  1041. strcpy(name_prefix, br_prefix);
  1042. devtype->name_prefix = name_prefix;
  1043. }
  1044. /* subscribe to external device handler */
  1045. sprintf(ubus_obj_name, "%s%s", OBJ_PREFIX, ubus_name);
  1046. etype->ubus_sub.obj.name = ubus_obj_name;
  1047. etype->ubus_sub.obj.type = &extdev_ubus_object_type;
  1048. ret = ubus_register_subscriber(ubus_ctx, &etype->ubus_sub);
  1049. if (ret) {
  1050. fprintf(stderr, "Failed to register subscriber object '%s'\n",
  1051. etype->ubus_sub.obj.name);
  1052. goto error;
  1053. }
  1054. etype->obj_wait.cb = extdev_wait_ev_cb;
  1055. etype->ubus_sub.remove_cb = extdev_ext_handler_remove_cb;
  1056. extdev_subscribe(etype);
  1057. /* parse config params from JSON object */
  1058. etype->config_strbuf = netifd_handler_parse_config(etype->config_params, cfg_obj);
  1059. if (!etype->config_strbuf)
  1060. goto error;
  1061. /* parse info dump params from JSON object */
  1062. if (!info_obj) {
  1063. devtype->dump_info = NULL;
  1064. } else {
  1065. etype->info_strbuf = netifd_handler_parse_config(etype->info_params, info_obj);
  1066. if (!etype->info_strbuf)
  1067. devtype->dump_info = NULL;
  1068. }
  1069. /* parse statistics dump params from JSON object */
  1070. if (!stats_obj) {
  1071. devtype->dump_stats = NULL;
  1072. } else {
  1073. etype->stats_strbuf = netifd_handler_parse_config(etype->stats_params, stats_obj);
  1074. if (!etype->stats_strbuf)
  1075. devtype->dump_stats = NULL;
  1076. }
  1077. ret = device_type_add(devtype);
  1078. if (ret)
  1079. goto config_error;
  1080. return;
  1081. config_error:
  1082. free(etype->config_strbuf);
  1083. free(etype->info_strbuf);
  1084. free(etype->stats_strbuf);
  1085. error:
  1086. fprintf(stderr, "Failed to create device handler for device"
  1087. "type '%s' from file '%s'\n", tname, cfg_file);
  1088. free(ubus_obj_name);
  1089. free(devtype_name);
  1090. free(etype);
  1091. }
  1092. /* create extdev device handler stubs from JSON description */
  1093. void
  1094. extdev_init(void)
  1095. {
  1096. confdir_fd = netifd_open_subdir("extdev-config");
  1097. if (confdir_fd < 0)
  1098. return;
  1099. netifd_init_extdev_handlers(confdir_fd, extdev_add_devtype);
  1100. }