xenbus.c 14 KB

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