xenbus.c 15 KB

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