xenbus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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");
  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");
  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", xenbus_strstate(frontend_state));
  195. be->frontend_state = frontend_state;
  196. switch (frontend_state) {
  197. case XenbusStateInitialising:
  198. if (dev->state == XenbusStateClosed) {
  199. printk(KERN_INFO "%s: %s: prepare for reconnect\n",
  200. __func__, dev->nodename);
  201. xenbus_switch_state(dev, XenbusStateInitWait);
  202. }
  203. break;
  204. case XenbusStateInitialised:
  205. break;
  206. case XenbusStateConnected:
  207. if (dev->state == XenbusStateConnected)
  208. break;
  209. backend_create_xenvif(be);
  210. if (be->vif)
  211. connect(be);
  212. break;
  213. case XenbusStateClosing:
  214. if (be->vif)
  215. kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
  216. disconnect_backend(dev);
  217. xenbus_switch_state(dev, XenbusStateClosing);
  218. break;
  219. case XenbusStateClosed:
  220. xenbus_switch_state(dev, XenbusStateClosed);
  221. if (xenbus_dev_is_online(dev))
  222. break;
  223. /* fall through if not online */
  224. case XenbusStateUnknown:
  225. device_unregister(&dev->dev);
  226. break;
  227. default:
  228. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  229. frontend_state);
  230. break;
  231. }
  232. }
  233. static void xen_net_read_rate(struct xenbus_device *dev,
  234. unsigned long *bytes, unsigned long *usec)
  235. {
  236. char *s, *e;
  237. unsigned long b, u;
  238. char *ratestr;
  239. /* Default to unlimited bandwidth. */
  240. *bytes = ~0UL;
  241. *usec = 0;
  242. ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
  243. if (IS_ERR(ratestr))
  244. return;
  245. s = ratestr;
  246. b = simple_strtoul(s, &e, 10);
  247. if ((s == e) || (*e != ','))
  248. goto fail;
  249. s = e + 1;
  250. u = simple_strtoul(s, &e, 10);
  251. if ((s == e) || (*e != '\0'))
  252. goto fail;
  253. *bytes = b;
  254. *usec = u;
  255. kfree(ratestr);
  256. return;
  257. fail:
  258. pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
  259. kfree(ratestr);
  260. }
  261. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  262. {
  263. char *s, *e, *macstr;
  264. int i;
  265. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  266. if (IS_ERR(macstr))
  267. return PTR_ERR(macstr);
  268. for (i = 0; i < ETH_ALEN; i++) {
  269. mac[i] = simple_strtoul(s, &e, 16);
  270. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  271. kfree(macstr);
  272. return -ENOENT;
  273. }
  274. s = e+1;
  275. }
  276. kfree(macstr);
  277. return 0;
  278. }
  279. static void unregister_hotplug_status_watch(struct backend_info *be)
  280. {
  281. if (be->have_hotplug_status_watch) {
  282. unregister_xenbus_watch(&be->hotplug_status_watch);
  283. kfree(be->hotplug_status_watch.node);
  284. }
  285. be->have_hotplug_status_watch = 0;
  286. }
  287. static void hotplug_status_changed(struct xenbus_watch *watch,
  288. const char **vec,
  289. unsigned int vec_size)
  290. {
  291. struct backend_info *be = container_of(watch,
  292. struct backend_info,
  293. hotplug_status_watch);
  294. char *str;
  295. unsigned int len;
  296. str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
  297. if (IS_ERR(str))
  298. return;
  299. if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
  300. xenbus_switch_state(be->dev, XenbusStateConnected);
  301. /* Not interested in this watch anymore. */
  302. unregister_hotplug_status_watch(be);
  303. }
  304. kfree(str);
  305. }
  306. static void connect(struct backend_info *be)
  307. {
  308. int err;
  309. struct xenbus_device *dev = be->dev;
  310. err = connect_rings(be);
  311. if (err)
  312. return;
  313. err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
  314. if (err) {
  315. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  316. return;
  317. }
  318. xen_net_read_rate(dev, &be->vif->credit_bytes,
  319. &be->vif->credit_usec);
  320. be->vif->remaining_credit = be->vif->credit_bytes;
  321. unregister_hotplug_status_watch(be);
  322. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  323. hotplug_status_changed,
  324. "%s/%s", dev->nodename, "hotplug-status");
  325. if (err) {
  326. /* Switch now, since we can't do a watch. */
  327. xenbus_switch_state(dev, XenbusStateConnected);
  328. } else {
  329. be->have_hotplug_status_watch = 1;
  330. }
  331. netif_wake_queue(be->vif->dev);
  332. }
  333. static int connect_rings(struct backend_info *be)
  334. {
  335. struct xenvif *vif = be->vif;
  336. struct xenbus_device *dev = be->dev;
  337. unsigned long tx_ring_ref, rx_ring_ref;
  338. unsigned int tx_evtchn, rx_evtchn, rx_copy;
  339. int err;
  340. int val;
  341. err = xenbus_gather(XBT_NIL, dev->otherend,
  342. "tx-ring-ref", "%lu", &tx_ring_ref,
  343. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  344. if (err) {
  345. xenbus_dev_fatal(dev, err,
  346. "reading %s/ring-ref",
  347. dev->otherend);
  348. return err;
  349. }
  350. /* Try split event channels first, then single event channel. */
  351. err = xenbus_gather(XBT_NIL, dev->otherend,
  352. "event-channel-tx", "%u", &tx_evtchn,
  353. "event-channel-rx", "%u", &rx_evtchn, NULL);
  354. if (err < 0) {
  355. err = xenbus_scanf(XBT_NIL, dev->otherend,
  356. "event-channel", "%u", &tx_evtchn);
  357. if (err < 0) {
  358. xenbus_dev_fatal(dev, err,
  359. "reading %s/event-channel(-tx/rx)",
  360. dev->otherend);
  361. return err;
  362. }
  363. rx_evtchn = tx_evtchn;
  364. }
  365. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  366. &rx_copy);
  367. if (err == -ENOENT) {
  368. err = 0;
  369. rx_copy = 0;
  370. }
  371. if (err < 0) {
  372. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  373. dev->otherend);
  374. return err;
  375. }
  376. if (!rx_copy)
  377. return -EOPNOTSUPP;
  378. if (vif->dev->tx_queue_len != 0) {
  379. if (xenbus_scanf(XBT_NIL, dev->otherend,
  380. "feature-rx-notify", "%d", &val) < 0)
  381. val = 0;
  382. if (val)
  383. vif->can_queue = 1;
  384. else
  385. /* Must be non-zero for pfifo_fast to work. */
  386. vif->dev->tx_queue_len = 1;
  387. }
  388. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
  389. "%d", &val) < 0)
  390. val = 0;
  391. vif->can_sg = !!val;
  392. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
  393. "%d", &val) < 0)
  394. val = 0;
  395. vif->gso = !!val;
  396. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
  397. "%d", &val) < 0)
  398. val = 0;
  399. vif->gso_prefix = !!val;
  400. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
  401. "%d", &val) < 0)
  402. val = 0;
  403. vif->csum = !val;
  404. /* Map the shared frame, irq etc. */
  405. err = xenvif_connect(vif, tx_ring_ref, rx_ring_ref,
  406. tx_evtchn, rx_evtchn);
  407. if (err) {
  408. xenbus_dev_fatal(dev, err,
  409. "mapping shared-frames %lu/%lu port tx %u rx %u",
  410. tx_ring_ref, rx_ring_ref,
  411. tx_evtchn, rx_evtchn);
  412. return err;
  413. }
  414. return 0;
  415. }
  416. /* ** Driver Registration ** */
  417. static const struct xenbus_device_id netback_ids[] = {
  418. { "vif" },
  419. { "" }
  420. };
  421. static DEFINE_XENBUS_DRIVER(netback, ,
  422. .probe = netback_probe,
  423. .remove = netback_remove,
  424. .uevent = netback_uevent,
  425. .otherend_changed = frontend_changed,
  426. );
  427. int xenvif_xenbus_init(void)
  428. {
  429. return xenbus_register_backend(&netback_driver);
  430. }
  431. void xenvif_xenbus_fini(void)
  432. {
  433. return xenbus_unregister_driver(&netback_driver);
  434. }