xenbus.c 12 KB

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