xenbus.c 15 KB

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