xenbus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 partial checksum setup for IPv6 packets */
  93. err = xenbus_printf(xbt, dev->nodename,
  94. "feature-ipv6-csum-offload",
  95. "%d", 1);
  96. if (err) {
  97. message = "writing feature-ipv6-csum-offload";
  98. goto abort_transaction;
  99. }
  100. /* We support rx-copy path. */
  101. err = xenbus_printf(xbt, dev->nodename,
  102. "feature-rx-copy", "%d", 1);
  103. if (err) {
  104. message = "writing feature-rx-copy";
  105. goto abort_transaction;
  106. }
  107. /*
  108. * We don't support rx-flip path (except old guests who don't
  109. * grok this feature flag).
  110. */
  111. err = xenbus_printf(xbt, dev->nodename,
  112. "feature-rx-flip", "%d", 0);
  113. if (err) {
  114. message = "writing feature-rx-flip";
  115. goto abort_transaction;
  116. }
  117. err = xenbus_transaction_end(xbt, 0);
  118. } while (err == -EAGAIN);
  119. if (err) {
  120. xenbus_dev_fatal(dev, err, "completing transaction");
  121. goto fail;
  122. }
  123. /*
  124. * Split event channels support, this is optional so it is not
  125. * put inside the above loop.
  126. */
  127. err = xenbus_printf(XBT_NIL, dev->nodename,
  128. "feature-split-event-channels",
  129. "%u", separate_tx_rx_irq);
  130. if (err)
  131. pr_debug("Error writing feature-split-event-channels\n");
  132. err = xenbus_switch_state(dev, XenbusStateInitWait);
  133. if (err)
  134. goto fail;
  135. be->state = XenbusStateInitWait;
  136. /* This kicks hotplug scripts, so do it immediately. */
  137. backend_create_xenvif(be);
  138. return 0;
  139. abort_transaction:
  140. xenbus_transaction_end(xbt, 1);
  141. xenbus_dev_fatal(dev, err, "%s", message);
  142. fail:
  143. pr_debug("failed\n");
  144. netback_remove(dev);
  145. return err;
  146. }
  147. /*
  148. * Handle the creation of the hotplug script environment. We add the script
  149. * and vif variables to the environment, for the benefit of the vif-* hotplug
  150. * scripts.
  151. */
  152. static int netback_uevent(struct xenbus_device *xdev,
  153. struct kobj_uevent_env *env)
  154. {
  155. struct backend_info *be = dev_get_drvdata(&xdev->dev);
  156. char *val;
  157. val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
  158. if (IS_ERR(val)) {
  159. int err = PTR_ERR(val);
  160. xenbus_dev_fatal(xdev, err, "reading script");
  161. return err;
  162. } else {
  163. if (add_uevent_var(env, "script=%s", val)) {
  164. kfree(val);
  165. return -ENOMEM;
  166. }
  167. kfree(val);
  168. }
  169. if (!be || !be->vif)
  170. return 0;
  171. return add_uevent_var(env, "vif=%s", be->vif->dev->name);
  172. }
  173. static void backend_create_xenvif(struct backend_info *be)
  174. {
  175. int err;
  176. long handle;
  177. struct xenbus_device *dev = be->dev;
  178. if (be->vif != NULL)
  179. return;
  180. err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
  181. if (err != 1) {
  182. xenbus_dev_fatal(dev, err, "reading handle");
  183. return;
  184. }
  185. be->vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
  186. if (IS_ERR(be->vif)) {
  187. err = PTR_ERR(be->vif);
  188. be->vif = NULL;
  189. xenbus_dev_fatal(dev, err, "creating interface");
  190. return;
  191. }
  192. kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
  193. }
  194. static void backend_disconnect(struct backend_info *be)
  195. {
  196. if (be->vif)
  197. xenvif_disconnect(be->vif);
  198. }
  199. static void backend_connect(struct backend_info *be)
  200. {
  201. if (be->vif)
  202. connect(be);
  203. }
  204. static inline void backend_switch_state(struct backend_info *be,
  205. enum xenbus_state state)
  206. {
  207. struct xenbus_device *dev = be->dev;
  208. pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
  209. be->state = state;
  210. /* If we are waiting for a hotplug script then defer the
  211. * actual xenbus state change.
  212. */
  213. if (!be->have_hotplug_status_watch)
  214. xenbus_switch_state(dev, state);
  215. }
  216. /* Handle backend state transitions:
  217. *
  218. * The backend state starts in InitWait and the following transitions are
  219. * allowed.
  220. *
  221. * InitWait -> Connected
  222. *
  223. * ^ \ |
  224. * | \ |
  225. * | \ |
  226. * | \ |
  227. * | \ |
  228. * | \ |
  229. * | V V
  230. *
  231. * Closed <-> Closing
  232. *
  233. * The state argument specifies the eventual state of the backend and the
  234. * function transitions to that state via the shortest path.
  235. */
  236. static void set_backend_state(struct backend_info *be,
  237. enum xenbus_state state)
  238. {
  239. while (be->state != state) {
  240. switch (be->state) {
  241. case XenbusStateClosed:
  242. switch (state) {
  243. case XenbusStateInitWait:
  244. case XenbusStateConnected:
  245. pr_info("%s: prepare for reconnect\n",
  246. be->dev->nodename);
  247. backend_switch_state(be, XenbusStateInitWait);
  248. break;
  249. case XenbusStateClosing:
  250. backend_switch_state(be, XenbusStateClosing);
  251. break;
  252. default:
  253. BUG();
  254. }
  255. break;
  256. case XenbusStateInitWait:
  257. switch (state) {
  258. case XenbusStateConnected:
  259. backend_connect(be);
  260. backend_switch_state(be, XenbusStateConnected);
  261. break;
  262. case XenbusStateClosing:
  263. case XenbusStateClosed:
  264. backend_switch_state(be, XenbusStateClosing);
  265. break;
  266. default:
  267. BUG();
  268. }
  269. break;
  270. case XenbusStateConnected:
  271. switch (state) {
  272. case XenbusStateInitWait:
  273. case XenbusStateClosing:
  274. case XenbusStateClosed:
  275. backend_disconnect(be);
  276. backend_switch_state(be, XenbusStateClosing);
  277. break;
  278. default:
  279. BUG();
  280. }
  281. break;
  282. case XenbusStateClosing:
  283. switch (state) {
  284. case XenbusStateInitWait:
  285. case XenbusStateConnected:
  286. case XenbusStateClosed:
  287. backend_switch_state(be, XenbusStateClosed);
  288. break;
  289. default:
  290. BUG();
  291. }
  292. break;
  293. default:
  294. BUG();
  295. }
  296. }
  297. }
  298. /**
  299. * Callback received when the frontend's state changes.
  300. */
  301. static void frontend_changed(struct xenbus_device *dev,
  302. enum xenbus_state frontend_state)
  303. {
  304. struct backend_info *be = dev_get_drvdata(&dev->dev);
  305. pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
  306. be->frontend_state = frontend_state;
  307. switch (frontend_state) {
  308. case XenbusStateInitialising:
  309. set_backend_state(be, XenbusStateInitWait);
  310. break;
  311. case XenbusStateInitialised:
  312. break;
  313. case XenbusStateConnected:
  314. set_backend_state(be, XenbusStateConnected);
  315. break;
  316. case XenbusStateClosing:
  317. set_backend_state(be, XenbusStateClosing);
  318. break;
  319. case XenbusStateClosed:
  320. set_backend_state(be, XenbusStateClosed);
  321. if (xenbus_dev_is_online(dev))
  322. break;
  323. /* fall through if not online */
  324. case XenbusStateUnknown:
  325. set_backend_state(be, XenbusStateClosed);
  326. device_unregister(&dev->dev);
  327. break;
  328. default:
  329. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  330. frontend_state);
  331. break;
  332. }
  333. }
  334. static void xen_net_read_rate(struct xenbus_device *dev,
  335. unsigned long *bytes, unsigned long *usec)
  336. {
  337. char *s, *e;
  338. unsigned long b, u;
  339. char *ratestr;
  340. /* Default to unlimited bandwidth. */
  341. *bytes = ~0UL;
  342. *usec = 0;
  343. ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
  344. if (IS_ERR(ratestr))
  345. return;
  346. s = ratestr;
  347. b = simple_strtoul(s, &e, 10);
  348. if ((s == e) || (*e != ','))
  349. goto fail;
  350. s = e + 1;
  351. u = simple_strtoul(s, &e, 10);
  352. if ((s == e) || (*e != '\0'))
  353. goto fail;
  354. *bytes = b;
  355. *usec = u;
  356. kfree(ratestr);
  357. return;
  358. fail:
  359. pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
  360. kfree(ratestr);
  361. }
  362. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  363. {
  364. char *s, *e, *macstr;
  365. int i;
  366. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  367. if (IS_ERR(macstr))
  368. return PTR_ERR(macstr);
  369. for (i = 0; i < ETH_ALEN; i++) {
  370. mac[i] = simple_strtoul(s, &e, 16);
  371. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  372. kfree(macstr);
  373. return -ENOENT;
  374. }
  375. s = e+1;
  376. }
  377. kfree(macstr);
  378. return 0;
  379. }
  380. static void unregister_hotplug_status_watch(struct backend_info *be)
  381. {
  382. if (be->have_hotplug_status_watch) {
  383. unregister_xenbus_watch(&be->hotplug_status_watch);
  384. kfree(be->hotplug_status_watch.node);
  385. }
  386. be->have_hotplug_status_watch = 0;
  387. }
  388. static void hotplug_status_changed(struct xenbus_watch *watch,
  389. const char **vec,
  390. unsigned int vec_size)
  391. {
  392. struct backend_info *be = container_of(watch,
  393. struct backend_info,
  394. hotplug_status_watch);
  395. char *str;
  396. unsigned int len;
  397. str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
  398. if (IS_ERR(str))
  399. return;
  400. if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
  401. /* Complete any pending state change */
  402. xenbus_switch_state(be->dev, be->state);
  403. /* Not interested in this watch anymore. */
  404. unregister_hotplug_status_watch(be);
  405. }
  406. kfree(str);
  407. }
  408. static void connect(struct backend_info *be)
  409. {
  410. int err;
  411. struct xenbus_device *dev = be->dev;
  412. err = connect_rings(be);
  413. if (err)
  414. return;
  415. err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
  416. if (err) {
  417. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  418. return;
  419. }
  420. xen_net_read_rate(dev, &be->vif->credit_bytes,
  421. &be->vif->credit_usec);
  422. be->vif->remaining_credit = be->vif->credit_bytes;
  423. unregister_hotplug_status_watch(be);
  424. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  425. hotplug_status_changed,
  426. "%s/%s", dev->nodename, "hotplug-status");
  427. if (!err)
  428. be->have_hotplug_status_watch = 1;
  429. netif_wake_queue(be->vif->dev);
  430. }
  431. static int connect_rings(struct backend_info *be)
  432. {
  433. struct xenvif *vif = be->vif;
  434. struct xenbus_device *dev = be->dev;
  435. unsigned long tx_ring_ref, rx_ring_ref;
  436. unsigned int tx_evtchn, rx_evtchn, rx_copy;
  437. int err;
  438. int val;
  439. err = xenbus_gather(XBT_NIL, dev->otherend,
  440. "tx-ring-ref", "%lu", &tx_ring_ref,
  441. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  442. if (err) {
  443. xenbus_dev_fatal(dev, err,
  444. "reading %s/ring-ref",
  445. dev->otherend);
  446. return err;
  447. }
  448. /* Try split event channels first, then single event channel. */
  449. err = xenbus_gather(XBT_NIL, dev->otherend,
  450. "event-channel-tx", "%u", &tx_evtchn,
  451. "event-channel-rx", "%u", &rx_evtchn, NULL);
  452. if (err < 0) {
  453. err = xenbus_scanf(XBT_NIL, dev->otherend,
  454. "event-channel", "%u", &tx_evtchn);
  455. if (err < 0) {
  456. xenbus_dev_fatal(dev, err,
  457. "reading %s/event-channel(-tx/rx)",
  458. dev->otherend);
  459. return err;
  460. }
  461. rx_evtchn = tx_evtchn;
  462. }
  463. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  464. &rx_copy);
  465. if (err == -ENOENT) {
  466. err = 0;
  467. rx_copy = 0;
  468. }
  469. if (err < 0) {
  470. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  471. dev->otherend);
  472. return err;
  473. }
  474. if (!rx_copy)
  475. return -EOPNOTSUPP;
  476. if (vif->dev->tx_queue_len != 0) {
  477. if (xenbus_scanf(XBT_NIL, dev->otherend,
  478. "feature-rx-notify", "%d", &val) < 0)
  479. val = 0;
  480. if (val)
  481. vif->can_queue = 1;
  482. else
  483. /* Must be non-zero for pfifo_fast to work. */
  484. vif->dev->tx_queue_len = 1;
  485. }
  486. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
  487. "%d", &val) < 0)
  488. val = 0;
  489. vif->can_sg = !!val;
  490. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
  491. "%d", &val) < 0)
  492. val = 0;
  493. vif->gso = !!val;
  494. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
  495. "%d", &val) < 0)
  496. val = 0;
  497. vif->gso_prefix = !!val;
  498. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
  499. "%d", &val) < 0)
  500. val = 0;
  501. vif->ip_csum = !val;
  502. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
  503. "%d", &val) < 0)
  504. val = 0;
  505. vif->ipv6_csum = !!val;
  506. /* Map the shared frame, irq etc. */
  507. err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
  508. tx_evtchn, rx_evtchn);
  509. if (err) {
  510. xenbus_dev_fatal(dev, err,
  511. "mapping shared-frames %lu/%lu port tx %u rx %u",
  512. tx_ring_ref, rx_ring_ref,
  513. tx_evtchn, rx_evtchn);
  514. return err;
  515. }
  516. return 0;
  517. }
  518. /* ** Driver Registration ** */
  519. static const struct xenbus_device_id netback_ids[] = {
  520. { "vif" },
  521. { "" }
  522. };
  523. static DEFINE_XENBUS_DRIVER(netback, ,
  524. .probe = netback_probe,
  525. .remove = netback_remove,
  526. .uevent = netback_uevent,
  527. .otherend_changed = frontend_changed,
  528. );
  529. int xenvif_xenbus_init(void)
  530. {
  531. return xenbus_register_backend(&netback_driver);
  532. }
  533. void xenvif_xenbus_fini(void)
  534. {
  535. return xenbus_unregister_driver(&netback_driver);
  536. }