xenbus.c 11 KB

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