xenbus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * Xenbus code for netif backend
  3. *
  4. * Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
  5. * Copyright (C) 2005 XenSource Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "common.h"
  22. struct backend_info {
  23. struct xenbus_device *dev;
  24. struct xenvif *vif;
  25. /* This is the state that will be reflected in xenstore when any
  26. * active hotplug script completes.
  27. */
  28. enum xenbus_state state;
  29. enum xenbus_state frontend_state;
  30. struct xenbus_watch hotplug_status_watch;
  31. u8 have_hotplug_status_watch:1;
  32. };
  33. static int connect_rings(struct backend_info *);
  34. static void connect(struct backend_info *);
  35. static void backend_create_xenvif(struct backend_info *be);
  36. static void unregister_hotplug_status_watch(struct backend_info *be);
  37. static void set_backend_state(struct backend_info *be,
  38. enum xenbus_state state);
  39. static int netback_remove(struct xenbus_device *dev)
  40. {
  41. struct backend_info *be = dev_get_drvdata(&dev->dev);
  42. set_backend_state(be, XenbusStateClosed);
  43. unregister_hotplug_status_watch(be);
  44. if (be->vif) {
  45. kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
  46. xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
  47. xenvif_free(be->vif);
  48. be->vif = NULL;
  49. }
  50. kfree(be);
  51. dev_set_drvdata(&dev->dev, NULL);
  52. return 0;
  53. }
  54. /**
  55. * Entry point to this code when a new device is created. Allocate the basic
  56. * structures and switch to InitWait.
  57. */
  58. static int netback_probe(struct xenbus_device *dev,
  59. const struct xenbus_device_id *id)
  60. {
  61. const char *message;
  62. struct xenbus_transaction xbt;
  63. int err;
  64. int sg;
  65. struct backend_info *be = kzalloc(sizeof(struct backend_info),
  66. GFP_KERNEL);
  67. if (!be) {
  68. xenbus_dev_fatal(dev, -ENOMEM,
  69. "allocating backend structure");
  70. return -ENOMEM;
  71. }
  72. be->dev = dev;
  73. dev_set_drvdata(&dev->dev, be);
  74. sg = 1;
  75. do {
  76. err = xenbus_transaction_start(&xbt);
  77. if (err) {
  78. xenbus_dev_fatal(dev, err, "starting transaction");
  79. goto fail;
  80. }
  81. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
  82. if (err) {
  83. message = "writing feature-sg";
  84. goto abort_transaction;
  85. }
  86. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
  87. "%d", sg);
  88. if (err) {
  89. message = "writing feature-gso-tcpv4";
  90. goto abort_transaction;
  91. }
  92. /* We support rx-copy path. */
  93. err = xenbus_printf(xbt, dev->nodename,
  94. "feature-rx-copy", "%d", 1);
  95. if (err) {
  96. message = "writing feature-rx-copy";
  97. goto abort_transaction;
  98. }
  99. /*
  100. * We don't support rx-flip path (except old guests who don't
  101. * grok this feature flag).
  102. */
  103. err = xenbus_printf(xbt, dev->nodename,
  104. "feature-rx-flip", "%d", 0);
  105. if (err) {
  106. message = "writing feature-rx-flip";
  107. goto abort_transaction;
  108. }
  109. err = xenbus_transaction_end(xbt, 0);
  110. } while (err == -EAGAIN);
  111. if (err) {
  112. xenbus_dev_fatal(dev, err, "completing transaction");
  113. goto fail;
  114. }
  115. /*
  116. * Split event channels support, this is optional so it is not
  117. * put inside the above loop.
  118. */
  119. err = xenbus_printf(XBT_NIL, dev->nodename,
  120. "feature-split-event-channels",
  121. "%u", separate_tx_rx_irq);
  122. if (err)
  123. pr_debug("Error writing feature-split-event-channels\n");
  124. err = xenbus_switch_state(dev, XenbusStateInitWait);
  125. if (err)
  126. goto fail;
  127. be->state = XenbusStateInitWait;
  128. /* This kicks hotplug scripts, so do it immediately. */
  129. backend_create_xenvif(be);
  130. return 0;
  131. abort_transaction:
  132. xenbus_transaction_end(xbt, 1);
  133. xenbus_dev_fatal(dev, err, "%s", message);
  134. fail:
  135. pr_debug("failed\n");
  136. netback_remove(dev);
  137. return err;
  138. }
  139. /*
  140. * Handle the creation of the hotplug script environment. We add the script
  141. * and vif variables to the environment, for the benefit of the vif-* hotplug
  142. * scripts.
  143. */
  144. static int netback_uevent(struct xenbus_device *xdev,
  145. struct kobj_uevent_env *env)
  146. {
  147. struct backend_info *be = dev_get_drvdata(&xdev->dev);
  148. char *val;
  149. val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
  150. if (IS_ERR(val)) {
  151. int err = PTR_ERR(val);
  152. xenbus_dev_fatal(xdev, err, "reading script");
  153. return err;
  154. } else {
  155. if (add_uevent_var(env, "script=%s", val)) {
  156. kfree(val);
  157. return -ENOMEM;
  158. }
  159. kfree(val);
  160. }
  161. if (!be || !be->vif)
  162. return 0;
  163. return add_uevent_var(env, "vif=%s", be->vif->dev->name);
  164. }
  165. static void backend_create_xenvif(struct backend_info *be)
  166. {
  167. int err;
  168. long handle;
  169. struct xenbus_device *dev = be->dev;
  170. if (be->vif != NULL)
  171. return;
  172. err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
  173. if (err != 1) {
  174. xenbus_dev_fatal(dev, err, "reading handle");
  175. return;
  176. }
  177. be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
  178. if (IS_ERR(be->vif)) {
  179. err = PTR_ERR(be->vif);
  180. be->vif = NULL;
  181. xenbus_dev_fatal(dev, err, "creating interface");
  182. return;
  183. }
  184. kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
  185. }
  186. static void backend_disconnect(struct backend_info *be)
  187. {
  188. if (be->vif)
  189. xenvif_disconnect(be->vif);
  190. }
  191. static void backend_connect(struct backend_info *be)
  192. {
  193. if (be->vif)
  194. connect(be);
  195. }
  196. static inline void backend_switch_state(struct backend_info *be,
  197. enum xenbus_state state)
  198. {
  199. struct xenbus_device *dev = be->dev;
  200. pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
  201. be->state = state;
  202. /* If we are waiting for a hotplug script then defer the
  203. * actual xenbus state change.
  204. */
  205. if (!be->have_hotplug_status_watch)
  206. xenbus_switch_state(dev, state);
  207. }
  208. /* Handle backend state transitions:
  209. *
  210. * The backend state starts in InitWait and the following transitions are
  211. * allowed.
  212. *
  213. * InitWait -> Connected
  214. *
  215. * ^ \ |
  216. * | \ |
  217. * | \ |
  218. * | \ |
  219. * | \ |
  220. * | \ |
  221. * | V V
  222. *
  223. * Closed <-> Closing
  224. *
  225. * The state argument specifies the eventual state of the backend and the
  226. * function transitions to that state via the shortest path.
  227. */
  228. static void set_backend_state(struct backend_info *be,
  229. enum xenbus_state state)
  230. {
  231. while (be->state != state) {
  232. switch (be->state) {
  233. case XenbusStateClosed:
  234. switch (state) {
  235. case XenbusStateInitWait:
  236. case XenbusStateConnected:
  237. pr_info("%s: prepare for reconnect\n",
  238. be->dev->nodename);
  239. backend_switch_state(be, XenbusStateInitWait);
  240. break;
  241. case XenbusStateClosing:
  242. backend_switch_state(be, XenbusStateClosing);
  243. break;
  244. default:
  245. BUG();
  246. }
  247. break;
  248. case XenbusStateInitWait:
  249. switch (state) {
  250. case XenbusStateConnected:
  251. backend_connect(be);
  252. backend_switch_state(be, XenbusStateConnected);
  253. break;
  254. case XenbusStateClosing:
  255. case XenbusStateClosed:
  256. backend_switch_state(be, XenbusStateClosing);
  257. break;
  258. default:
  259. BUG();
  260. }
  261. break;
  262. case XenbusStateConnected:
  263. switch (state) {
  264. case XenbusStateInitWait:
  265. case XenbusStateClosing:
  266. case XenbusStateClosed:
  267. backend_disconnect(be);
  268. backend_switch_state(be, XenbusStateClosing);
  269. break;
  270. default:
  271. BUG();
  272. }
  273. break;
  274. case XenbusStateClosing:
  275. switch (state) {
  276. case XenbusStateInitWait:
  277. case XenbusStateConnected:
  278. case XenbusStateClosed:
  279. backend_switch_state(be, XenbusStateClosed);
  280. break;
  281. default:
  282. BUG();
  283. }
  284. break;
  285. default:
  286. BUG();
  287. }
  288. }
  289. }
  290. /**
  291. * Callback received when the frontend's state changes.
  292. */
  293. static void frontend_changed(struct xenbus_device *dev,
  294. enum xenbus_state frontend_state)
  295. {
  296. struct backend_info *be = dev_get_drvdata(&dev->dev);
  297. pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
  298. be->frontend_state = frontend_state;
  299. switch (frontend_state) {
  300. case XenbusStateInitialising:
  301. set_backend_state(be, XenbusStateInitWait);
  302. break;
  303. case XenbusStateInitialised:
  304. break;
  305. case XenbusStateConnected:
  306. set_backend_state(be, XenbusStateConnected);
  307. break;
  308. case XenbusStateClosing:
  309. set_backend_state(be, XenbusStateClosing);
  310. break;
  311. case XenbusStateClosed:
  312. set_backend_state(be, XenbusStateClosed);
  313. if (xenbus_dev_is_online(dev))
  314. break;
  315. /* fall through if not online */
  316. case XenbusStateUnknown:
  317. set_backend_state(be, XenbusStateClosed);
  318. device_unregister(&dev->dev);
  319. break;
  320. default:
  321. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  322. frontend_state);
  323. break;
  324. }
  325. }
  326. static void xen_net_read_rate(struct xenbus_device *dev,
  327. unsigned long *bytes, unsigned long *usec)
  328. {
  329. char *s, *e;
  330. unsigned long b, u;
  331. char *ratestr;
  332. /* Default to unlimited bandwidth. */
  333. *bytes = ~0UL;
  334. *usec = 0;
  335. ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
  336. if (IS_ERR(ratestr))
  337. return;
  338. s = ratestr;
  339. b = simple_strtoul(s, &e, 10);
  340. if ((s == e) || (*e != ','))
  341. goto fail;
  342. s = e + 1;
  343. u = simple_strtoul(s, &e, 10);
  344. if ((s == e) || (*e != '\0'))
  345. goto fail;
  346. *bytes = b;
  347. *usec = u;
  348. kfree(ratestr);
  349. return;
  350. fail:
  351. pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
  352. kfree(ratestr);
  353. }
  354. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  355. {
  356. char *s, *e, *macstr;
  357. int i;
  358. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  359. if (IS_ERR(macstr))
  360. return PTR_ERR(macstr);
  361. for (i = 0; i < ETH_ALEN; i++) {
  362. mac[i] = simple_strtoul(s, &e, 16);
  363. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  364. kfree(macstr);
  365. return -ENOENT;
  366. }
  367. s = e+1;
  368. }
  369. kfree(macstr);
  370. return 0;
  371. }
  372. static void unregister_hotplug_status_watch(struct backend_info *be)
  373. {
  374. if (be->have_hotplug_status_watch) {
  375. unregister_xenbus_watch(&be->hotplug_status_watch);
  376. kfree(be->hotplug_status_watch.node);
  377. }
  378. be->have_hotplug_status_watch = 0;
  379. }
  380. static void hotplug_status_changed(struct xenbus_watch *watch,
  381. const char **vec,
  382. unsigned int vec_size)
  383. {
  384. struct backend_info *be = container_of(watch,
  385. struct backend_info,
  386. hotplug_status_watch);
  387. char *str;
  388. unsigned int len;
  389. str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
  390. if (IS_ERR(str))
  391. return;
  392. if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
  393. /* Complete any pending state change */
  394. xenbus_switch_state(be->dev, be->state);
  395. /* Not interested in this watch anymore. */
  396. unregister_hotplug_status_watch(be);
  397. }
  398. kfree(str);
  399. }
  400. static void connect(struct backend_info *be)
  401. {
  402. int err;
  403. struct xenbus_device *dev = be->dev;
  404. err = connect_rings(be);
  405. if (err)
  406. return;
  407. err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
  408. if (err) {
  409. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  410. return;
  411. }
  412. xen_net_read_rate(dev, &be->vif->credit_bytes,
  413. &be->vif->credit_usec);
  414. be->vif->remaining_credit = be->vif->credit_bytes;
  415. unregister_hotplug_status_watch(be);
  416. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  417. hotplug_status_changed,
  418. "%s/%s", dev->nodename, "hotplug-status");
  419. if (!err)
  420. be->have_hotplug_status_watch = 1;
  421. netif_wake_queue(be->vif->dev);
  422. }
  423. static int connect_rings(struct backend_info *be)
  424. {
  425. struct xenvif *vif = be->vif;
  426. struct xenbus_device *dev = be->dev;
  427. unsigned long tx_ring_ref, rx_ring_ref;
  428. unsigned int tx_evtchn, rx_evtchn, rx_copy;
  429. int err;
  430. int val;
  431. err = xenbus_gather(XBT_NIL, dev->otherend,
  432. "tx-ring-ref", "%lu", &tx_ring_ref,
  433. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  434. if (err) {
  435. xenbus_dev_fatal(dev, err,
  436. "reading %s/ring-ref",
  437. dev->otherend);
  438. return err;
  439. }
  440. /* Try split event channels first, then single event channel. */
  441. err = xenbus_gather(XBT_NIL, dev->otherend,
  442. "event-channel-tx", "%u", &tx_evtchn,
  443. "event-channel-rx", "%u", &rx_evtchn, NULL);
  444. if (err < 0) {
  445. err = xenbus_scanf(XBT_NIL, dev->otherend,
  446. "event-channel", "%u", &tx_evtchn);
  447. if (err < 0) {
  448. xenbus_dev_fatal(dev, err,
  449. "reading %s/event-channel(-tx/rx)",
  450. dev->otherend);
  451. return err;
  452. }
  453. rx_evtchn = tx_evtchn;
  454. }
  455. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  456. &rx_copy);
  457. if (err == -ENOENT) {
  458. err = 0;
  459. rx_copy = 0;
  460. }
  461. if (err < 0) {
  462. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  463. dev->otherend);
  464. return err;
  465. }
  466. if (!rx_copy)
  467. return -EOPNOTSUPP;
  468. if (vif->dev->tx_queue_len != 0) {
  469. if (xenbus_scanf(XBT_NIL, dev->otherend,
  470. "feature-rx-notify", "%d", &val) < 0)
  471. val = 0;
  472. if (val)
  473. vif->can_queue = 1;
  474. else
  475. /* Must be non-zero for pfifo_fast to work. */
  476. vif->dev->tx_queue_len = 1;
  477. }
  478. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
  479. "%d", &val) < 0)
  480. val = 0;
  481. vif->can_sg = !!val;
  482. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
  483. "%d", &val) < 0)
  484. val = 0;
  485. vif->gso = !!val;
  486. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
  487. "%d", &val) < 0)
  488. val = 0;
  489. vif->gso_prefix = !!val;
  490. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
  491. "%d", &val) < 0)
  492. val = 0;
  493. vif->csum = !val;
  494. /* Map the shared frame, irq etc. */
  495. err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
  496. tx_evtchn, rx_evtchn);
  497. if (err) {
  498. xenbus_dev_fatal(dev, err,
  499. "mapping shared-frames %lu/%lu port tx %u rx %u",
  500. tx_ring_ref, rx_ring_ref,
  501. tx_evtchn, rx_evtchn);
  502. return err;
  503. }
  504. return 0;
  505. }
  506. /* ** Driver Registration ** */
  507. static const struct xenbus_device_id netback_ids[] = {
  508. { "vif" },
  509. { "" }
  510. };
  511. static DEFINE_XENBUS_DRIVER(netback, ,
  512. .probe = netback_probe,
  513. .remove = netback_remove,
  514. .uevent = netback_uevent,
  515. .otherend_changed = frontend_changed,
  516. );
  517. int xenvif_xenbus_init(void)
  518. {
  519. return xenbus_register_backend(&netback_driver);
  520. }
  521. void xenvif_xenbus_fini(void)
  522. {
  523. return xenbus_unregister_driver(&netback_driver);
  524. }