xenbus.c 11 KB

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