xenbus.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "common.h"
  21. #include <linux/vmalloc.h>
  22. #include <linux/rtnetlink.h>
  23. struct backend_info {
  24. struct xenbus_device *dev;
  25. struct xenvif *vif;
  26. /* This is the state that will be reflected in xenstore when any
  27. * active hotplug script completes.
  28. */
  29. enum xenbus_state state;
  30. enum xenbus_state frontend_state;
  31. struct xenbus_watch hotplug_status_watch;
  32. u8 have_hotplug_status_watch:1;
  33. const char *hotplug_script;
  34. };
  35. static int connect_rings(struct backend_info *be, struct xenvif_queue *queue);
  36. static void connect(struct backend_info *be);
  37. static int read_xenbus_vif_flags(struct backend_info *be);
  38. static int backend_create_xenvif(struct backend_info *be);
  39. static void unregister_hotplug_status_watch(struct backend_info *be);
  40. static void xen_unregister_watchers(struct xenvif *vif);
  41. static void set_backend_state(struct backend_info *be,
  42. enum xenbus_state state);
  43. #ifdef CONFIG_DEBUG_FS
  44. struct dentry *xen_netback_dbg_root = NULL;
  45. static int xenvif_read_io_ring(struct seq_file *m, void *v)
  46. {
  47. struct xenvif_queue *queue = m->private;
  48. struct xen_netif_tx_back_ring *tx_ring = &queue->tx;
  49. struct xen_netif_rx_back_ring *rx_ring = &queue->rx;
  50. struct netdev_queue *dev_queue;
  51. if (tx_ring->sring) {
  52. struct xen_netif_tx_sring *sring = tx_ring->sring;
  53. seq_printf(m, "Queue %d\nTX: nr_ents %u\n", queue->id,
  54. tx_ring->nr_ents);
  55. seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
  56. sring->req_prod,
  57. sring->req_prod - sring->rsp_prod,
  58. tx_ring->req_cons,
  59. tx_ring->req_cons - sring->rsp_prod,
  60. sring->req_event,
  61. sring->req_event - sring->rsp_prod);
  62. seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n",
  63. sring->rsp_prod,
  64. tx_ring->rsp_prod_pvt,
  65. tx_ring->rsp_prod_pvt - sring->rsp_prod,
  66. sring->rsp_event,
  67. sring->rsp_event - sring->rsp_prod);
  68. seq_printf(m, "pending prod %u pending cons %u nr_pending_reqs %u\n",
  69. queue->pending_prod,
  70. queue->pending_cons,
  71. nr_pending_reqs(queue));
  72. seq_printf(m, "dealloc prod %u dealloc cons %u dealloc_queue %u\n\n",
  73. queue->dealloc_prod,
  74. queue->dealloc_cons,
  75. queue->dealloc_prod - queue->dealloc_cons);
  76. }
  77. if (rx_ring->sring) {
  78. struct xen_netif_rx_sring *sring = rx_ring->sring;
  79. seq_printf(m, "RX: nr_ents %u\n", rx_ring->nr_ents);
  80. seq_printf(m, "req prod %u (%d) cons %u (%d) event %u (%d)\n",
  81. sring->req_prod,
  82. sring->req_prod - sring->rsp_prod,
  83. rx_ring->req_cons,
  84. rx_ring->req_cons - sring->rsp_prod,
  85. sring->req_event,
  86. sring->req_event - sring->rsp_prod);
  87. seq_printf(m, "rsp prod %u (base) pvt %u (%d) event %u (%d)\n\n",
  88. sring->rsp_prod,
  89. rx_ring->rsp_prod_pvt,
  90. rx_ring->rsp_prod_pvt - sring->rsp_prod,
  91. sring->rsp_event,
  92. sring->rsp_event - sring->rsp_prod);
  93. }
  94. seq_printf(m, "NAPI state: %lx NAPI weight: %d TX queue len %u\n"
  95. "Credit timer_pending: %d, credit: %lu, usec: %lu\n"
  96. "remaining: %lu, expires: %lu, now: %lu\n",
  97. queue->napi.state, queue->napi.weight,
  98. skb_queue_len(&queue->tx_queue),
  99. timer_pending(&queue->credit_timeout),
  100. queue->credit_bytes,
  101. queue->credit_usec,
  102. queue->remaining_credit,
  103. queue->credit_timeout.expires,
  104. jiffies);
  105. dev_queue = netdev_get_tx_queue(queue->vif->dev, queue->id);
  106. seq_printf(m, "\nRx internal queue: len %u max %u pkts %u %s\n",
  107. queue->rx_queue_len, queue->rx_queue_max,
  108. skb_queue_len(&queue->rx_queue),
  109. netif_tx_queue_stopped(dev_queue) ? "stopped" : "running");
  110. return 0;
  111. }
  112. #define XENVIF_KICK_STR "kick"
  113. #define BUFFER_SIZE 32
  114. static ssize_t
  115. xenvif_write_io_ring(struct file *filp, const char __user *buf, size_t count,
  116. loff_t *ppos)
  117. {
  118. struct xenvif_queue *queue =
  119. ((struct seq_file *)filp->private_data)->private;
  120. int len;
  121. char write[BUFFER_SIZE];
  122. /* don't allow partial writes and check the length */
  123. if (*ppos != 0)
  124. return 0;
  125. if (count >= sizeof(write))
  126. return -ENOSPC;
  127. len = simple_write_to_buffer(write,
  128. sizeof(write) - 1,
  129. ppos,
  130. buf,
  131. count);
  132. if (len < 0)
  133. return len;
  134. write[len] = '\0';
  135. if (!strncmp(write, XENVIF_KICK_STR, sizeof(XENVIF_KICK_STR) - 1))
  136. xenvif_interrupt(0, (void *)queue);
  137. else {
  138. pr_warn("Unknown command to io_ring_q%d. Available: kick\n",
  139. queue->id);
  140. count = -EINVAL;
  141. }
  142. return count;
  143. }
  144. static int xenvif_dump_open(struct inode *inode, struct file *filp)
  145. {
  146. int ret;
  147. void *queue = NULL;
  148. if (inode->i_private)
  149. queue = inode->i_private;
  150. ret = single_open(filp, xenvif_read_io_ring, queue);
  151. filp->f_mode |= FMODE_PWRITE;
  152. return ret;
  153. }
  154. static const struct file_operations xenvif_dbg_io_ring_ops_fops = {
  155. .owner = THIS_MODULE,
  156. .open = xenvif_dump_open,
  157. .read = seq_read,
  158. .llseek = seq_lseek,
  159. .release = single_release,
  160. .write = xenvif_write_io_ring,
  161. };
  162. static void xenvif_debugfs_addif(struct xenvif *vif)
  163. {
  164. struct dentry *pfile;
  165. int i;
  166. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  167. return;
  168. vif->xenvif_dbg_root = debugfs_create_dir(vif->dev->name,
  169. xen_netback_dbg_root);
  170. if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root)) {
  171. for (i = 0; i < vif->num_queues; ++i) {
  172. char filename[sizeof("io_ring_q") + 4];
  173. snprintf(filename, sizeof(filename), "io_ring_q%d", i);
  174. pfile = debugfs_create_file(filename,
  175. S_IRUSR | S_IWUSR,
  176. vif->xenvif_dbg_root,
  177. &vif->queues[i],
  178. &xenvif_dbg_io_ring_ops_fops);
  179. if (IS_ERR_OR_NULL(pfile))
  180. pr_warn("Creation of io_ring file returned %ld!\n",
  181. PTR_ERR(pfile));
  182. }
  183. } else
  184. netdev_warn(vif->dev,
  185. "Creation of vif debugfs dir returned %ld!\n",
  186. PTR_ERR(vif->xenvif_dbg_root));
  187. }
  188. static void xenvif_debugfs_delif(struct xenvif *vif)
  189. {
  190. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  191. return;
  192. if (!IS_ERR_OR_NULL(vif->xenvif_dbg_root))
  193. debugfs_remove_recursive(vif->xenvif_dbg_root);
  194. vif->xenvif_dbg_root = NULL;
  195. }
  196. #endif /* CONFIG_DEBUG_FS */
  197. static int netback_remove(struct xenbus_device *dev)
  198. {
  199. struct backend_info *be = dev_get_drvdata(&dev->dev);
  200. set_backend_state(be, XenbusStateClosed);
  201. unregister_hotplug_status_watch(be);
  202. if (be->vif) {
  203. kobject_uevent(&dev->dev.kobj, KOBJ_OFFLINE);
  204. xen_unregister_watchers(be->vif);
  205. xenbus_rm(XBT_NIL, dev->nodename, "hotplug-status");
  206. xenvif_free(be->vif);
  207. be->vif = NULL;
  208. }
  209. kfree(be->hotplug_script);
  210. kfree(be);
  211. dev_set_drvdata(&dev->dev, NULL);
  212. return 0;
  213. }
  214. /**
  215. * Entry point to this code when a new device is created. Allocate the basic
  216. * structures and switch to InitWait.
  217. */
  218. static int netback_probe(struct xenbus_device *dev,
  219. const struct xenbus_device_id *id)
  220. {
  221. const char *message;
  222. struct xenbus_transaction xbt;
  223. int err;
  224. int sg;
  225. const char *script;
  226. struct backend_info *be = kzalloc(sizeof(struct backend_info),
  227. GFP_KERNEL);
  228. if (!be) {
  229. xenbus_dev_fatal(dev, -ENOMEM,
  230. "allocating backend structure");
  231. return -ENOMEM;
  232. }
  233. be->dev = dev;
  234. dev_set_drvdata(&dev->dev, be);
  235. sg = 1;
  236. do {
  237. err = xenbus_transaction_start(&xbt);
  238. if (err) {
  239. xenbus_dev_fatal(dev, err, "starting transaction");
  240. goto fail;
  241. }
  242. err = xenbus_printf(xbt, dev->nodename, "feature-sg", "%d", sg);
  243. if (err) {
  244. message = "writing feature-sg";
  245. goto abort_transaction;
  246. }
  247. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv4",
  248. "%d", sg);
  249. if (err) {
  250. message = "writing feature-gso-tcpv4";
  251. goto abort_transaction;
  252. }
  253. err = xenbus_printf(xbt, dev->nodename, "feature-gso-tcpv6",
  254. "%d", sg);
  255. if (err) {
  256. message = "writing feature-gso-tcpv6";
  257. goto abort_transaction;
  258. }
  259. /* We support partial checksum setup for IPv6 packets */
  260. err = xenbus_printf(xbt, dev->nodename,
  261. "feature-ipv6-csum-offload",
  262. "%d", 1);
  263. if (err) {
  264. message = "writing feature-ipv6-csum-offload";
  265. goto abort_transaction;
  266. }
  267. /* We support rx-copy path. */
  268. err = xenbus_printf(xbt, dev->nodename,
  269. "feature-rx-copy", "%d", 1);
  270. if (err) {
  271. message = "writing feature-rx-copy";
  272. goto abort_transaction;
  273. }
  274. /*
  275. * We don't support rx-flip path (except old guests who don't
  276. * grok this feature flag).
  277. */
  278. err = xenbus_printf(xbt, dev->nodename,
  279. "feature-rx-flip", "%d", 0);
  280. if (err) {
  281. message = "writing feature-rx-flip";
  282. goto abort_transaction;
  283. }
  284. err = xenbus_transaction_end(xbt, 0);
  285. } while (err == -EAGAIN);
  286. if (err) {
  287. xenbus_dev_fatal(dev, err, "completing transaction");
  288. goto fail;
  289. }
  290. /*
  291. * Split event channels support, this is optional so it is not
  292. * put inside the above loop.
  293. */
  294. err = xenbus_printf(XBT_NIL, dev->nodename,
  295. "feature-split-event-channels",
  296. "%u", separate_tx_rx_irq);
  297. if (err)
  298. pr_debug("Error writing feature-split-event-channels\n");
  299. /* Multi-queue support: This is an optional feature. */
  300. err = xenbus_printf(XBT_NIL, dev->nodename,
  301. "multi-queue-max-queues", "%u", xenvif_max_queues);
  302. if (err)
  303. pr_debug("Error writing multi-queue-max-queues\n");
  304. script = xenbus_read(XBT_NIL, dev->nodename, "script", NULL);
  305. if (IS_ERR(script)) {
  306. err = PTR_ERR(script);
  307. xenbus_dev_fatal(dev, err, "reading script");
  308. goto fail;
  309. }
  310. be->hotplug_script = script;
  311. err = xenbus_switch_state(dev, XenbusStateInitWait);
  312. if (err)
  313. goto fail;
  314. be->state = XenbusStateInitWait;
  315. /* This kicks hotplug scripts, so do it immediately. */
  316. err = backend_create_xenvif(be);
  317. if (err)
  318. goto fail;
  319. return 0;
  320. abort_transaction:
  321. xenbus_transaction_end(xbt, 1);
  322. xenbus_dev_fatal(dev, err, "%s", message);
  323. fail:
  324. pr_debug("failed\n");
  325. netback_remove(dev);
  326. return err;
  327. }
  328. /*
  329. * Handle the creation of the hotplug script environment. We add the script
  330. * and vif variables to the environment, for the benefit of the vif-* hotplug
  331. * scripts.
  332. */
  333. static int netback_uevent(struct xenbus_device *xdev,
  334. struct kobj_uevent_env *env)
  335. {
  336. struct backend_info *be = dev_get_drvdata(&xdev->dev);
  337. if (!be)
  338. return 0;
  339. if (add_uevent_var(env, "script=%s", be->hotplug_script))
  340. return -ENOMEM;
  341. if (!be->vif)
  342. return 0;
  343. return add_uevent_var(env, "vif=%s", be->vif->dev->name);
  344. }
  345. static int backend_create_xenvif(struct backend_info *be)
  346. {
  347. int err;
  348. long handle;
  349. struct xenbus_device *dev = be->dev;
  350. struct xenvif *vif;
  351. if (be->vif != NULL)
  352. return 0;
  353. err = xenbus_scanf(XBT_NIL, dev->nodename, "handle", "%li", &handle);
  354. if (err != 1) {
  355. xenbus_dev_fatal(dev, err, "reading handle");
  356. return (err < 0) ? err : -EINVAL;
  357. }
  358. vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
  359. if (IS_ERR(vif)) {
  360. err = PTR_ERR(vif);
  361. xenbus_dev_fatal(dev, err, "creating interface");
  362. return err;
  363. }
  364. be->vif = vif;
  365. kobject_uevent(&dev->dev.kobj, KOBJ_ONLINE);
  366. return 0;
  367. }
  368. static void backend_disconnect(struct backend_info *be)
  369. {
  370. if (be->vif) {
  371. xen_unregister_watchers(be->vif);
  372. #ifdef CONFIG_DEBUG_FS
  373. xenvif_debugfs_delif(be->vif);
  374. #endif /* CONFIG_DEBUG_FS */
  375. xenvif_disconnect(be->vif);
  376. }
  377. }
  378. static void backend_connect(struct backend_info *be)
  379. {
  380. if (be->vif)
  381. connect(be);
  382. }
  383. static inline void backend_switch_state(struct backend_info *be,
  384. enum xenbus_state state)
  385. {
  386. struct xenbus_device *dev = be->dev;
  387. pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
  388. be->state = state;
  389. /* If we are waiting for a hotplug script then defer the
  390. * actual xenbus state change.
  391. */
  392. if (!be->have_hotplug_status_watch)
  393. xenbus_switch_state(dev, state);
  394. }
  395. /* Handle backend state transitions:
  396. *
  397. * The backend state starts in InitWait and the following transitions are
  398. * allowed.
  399. *
  400. * InitWait -> Connected
  401. *
  402. * ^ \ |
  403. * | \ |
  404. * | \ |
  405. * | \ |
  406. * | \ |
  407. * | \ |
  408. * | V V
  409. *
  410. * Closed <-> Closing
  411. *
  412. * The state argument specifies the eventual state of the backend and the
  413. * function transitions to that state via the shortest path.
  414. */
  415. static void set_backend_state(struct backend_info *be,
  416. enum xenbus_state state)
  417. {
  418. while (be->state != state) {
  419. switch (be->state) {
  420. case XenbusStateClosed:
  421. switch (state) {
  422. case XenbusStateInitWait:
  423. case XenbusStateConnected:
  424. pr_info("%s: prepare for reconnect\n",
  425. be->dev->nodename);
  426. backend_switch_state(be, XenbusStateInitWait);
  427. break;
  428. case XenbusStateClosing:
  429. backend_switch_state(be, XenbusStateClosing);
  430. break;
  431. default:
  432. BUG();
  433. }
  434. break;
  435. case XenbusStateInitWait:
  436. switch (state) {
  437. case XenbusStateConnected:
  438. backend_connect(be);
  439. backend_switch_state(be, XenbusStateConnected);
  440. break;
  441. case XenbusStateClosing:
  442. case XenbusStateClosed:
  443. backend_switch_state(be, XenbusStateClosing);
  444. break;
  445. default:
  446. BUG();
  447. }
  448. break;
  449. case XenbusStateConnected:
  450. switch (state) {
  451. case XenbusStateInitWait:
  452. case XenbusStateClosing:
  453. case XenbusStateClosed:
  454. backend_disconnect(be);
  455. backend_switch_state(be, XenbusStateClosing);
  456. break;
  457. default:
  458. BUG();
  459. }
  460. break;
  461. case XenbusStateClosing:
  462. switch (state) {
  463. case XenbusStateInitWait:
  464. case XenbusStateConnected:
  465. case XenbusStateClosed:
  466. backend_switch_state(be, XenbusStateClosed);
  467. break;
  468. default:
  469. BUG();
  470. }
  471. break;
  472. default:
  473. BUG();
  474. }
  475. }
  476. }
  477. /**
  478. * Callback received when the frontend's state changes.
  479. */
  480. static void frontend_changed(struct xenbus_device *dev,
  481. enum xenbus_state frontend_state)
  482. {
  483. struct backend_info *be = dev_get_drvdata(&dev->dev);
  484. pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
  485. be->frontend_state = frontend_state;
  486. switch (frontend_state) {
  487. case XenbusStateInitialising:
  488. set_backend_state(be, XenbusStateInitWait);
  489. break;
  490. case XenbusStateInitialised:
  491. break;
  492. case XenbusStateConnected:
  493. set_backend_state(be, XenbusStateConnected);
  494. break;
  495. case XenbusStateClosing:
  496. set_backend_state(be, XenbusStateClosing);
  497. break;
  498. case XenbusStateClosed:
  499. set_backend_state(be, XenbusStateClosed);
  500. if (xenbus_dev_is_online(dev))
  501. break;
  502. /* fall through if not online */
  503. case XenbusStateUnknown:
  504. set_backend_state(be, XenbusStateClosed);
  505. device_unregister(&dev->dev);
  506. break;
  507. default:
  508. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  509. frontend_state);
  510. break;
  511. }
  512. }
  513. static void xen_net_read_rate(struct xenbus_device *dev,
  514. unsigned long *bytes, unsigned long *usec)
  515. {
  516. char *s, *e;
  517. unsigned long b, u;
  518. char *ratestr;
  519. /* Default to unlimited bandwidth. */
  520. *bytes = ~0UL;
  521. *usec = 0;
  522. ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
  523. if (IS_ERR(ratestr))
  524. return;
  525. s = ratestr;
  526. b = simple_strtoul(s, &e, 10);
  527. if ((s == e) || (*e != ','))
  528. goto fail;
  529. s = e + 1;
  530. u = simple_strtoul(s, &e, 10);
  531. if ((s == e) || (*e != '\0'))
  532. goto fail;
  533. *bytes = b;
  534. *usec = u;
  535. kfree(ratestr);
  536. return;
  537. fail:
  538. pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
  539. kfree(ratestr);
  540. }
  541. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  542. {
  543. char *s, *e, *macstr;
  544. int i;
  545. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  546. if (IS_ERR(macstr))
  547. return PTR_ERR(macstr);
  548. for (i = 0; i < ETH_ALEN; i++) {
  549. mac[i] = simple_strtoul(s, &e, 16);
  550. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  551. kfree(macstr);
  552. return -ENOENT;
  553. }
  554. s = e+1;
  555. }
  556. kfree(macstr);
  557. return 0;
  558. }
  559. static void xen_net_rate_changed(struct xenbus_watch *watch,
  560. const char **vec, unsigned int len)
  561. {
  562. struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
  563. struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
  564. unsigned long credit_bytes;
  565. unsigned long credit_usec;
  566. unsigned int queue_index;
  567. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  568. for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
  569. struct xenvif_queue *queue = &vif->queues[queue_index];
  570. queue->credit_bytes = credit_bytes;
  571. queue->credit_usec = credit_usec;
  572. if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
  573. queue->remaining_credit > queue->credit_bytes) {
  574. queue->remaining_credit = queue->credit_bytes;
  575. }
  576. }
  577. }
  578. static int xen_register_watchers(struct xenbus_device *dev, struct xenvif *vif)
  579. {
  580. int err = 0;
  581. char *node;
  582. unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
  583. node = kmalloc(maxlen, GFP_KERNEL);
  584. if (!node)
  585. return -ENOMEM;
  586. snprintf(node, maxlen, "%s/rate", dev->nodename);
  587. vif->credit_watch.node = node;
  588. vif->credit_watch.callback = xen_net_rate_changed;
  589. err = register_xenbus_watch(&vif->credit_watch);
  590. if (err) {
  591. pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
  592. kfree(node);
  593. vif->credit_watch.node = NULL;
  594. vif->credit_watch.callback = NULL;
  595. }
  596. return err;
  597. }
  598. static void xen_unregister_watchers(struct xenvif *vif)
  599. {
  600. if (vif->credit_watch.node) {
  601. unregister_xenbus_watch(&vif->credit_watch);
  602. kfree(vif->credit_watch.node);
  603. vif->credit_watch.node = NULL;
  604. }
  605. }
  606. static void unregister_hotplug_status_watch(struct backend_info *be)
  607. {
  608. if (be->have_hotplug_status_watch) {
  609. unregister_xenbus_watch(&be->hotplug_status_watch);
  610. kfree(be->hotplug_status_watch.node);
  611. }
  612. be->have_hotplug_status_watch = 0;
  613. }
  614. static void hotplug_status_changed(struct xenbus_watch *watch,
  615. const char **vec,
  616. unsigned int vec_size)
  617. {
  618. struct backend_info *be = container_of(watch,
  619. struct backend_info,
  620. hotplug_status_watch);
  621. char *str;
  622. unsigned int len;
  623. str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
  624. if (IS_ERR(str))
  625. return;
  626. if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
  627. /* Complete any pending state change */
  628. xenbus_switch_state(be->dev, be->state);
  629. /* Not interested in this watch anymore. */
  630. unregister_hotplug_status_watch(be);
  631. }
  632. kfree(str);
  633. }
  634. static void connect(struct backend_info *be)
  635. {
  636. int err;
  637. struct xenbus_device *dev = be->dev;
  638. unsigned long credit_bytes, credit_usec;
  639. unsigned int queue_index;
  640. unsigned int requested_num_queues;
  641. struct xenvif_queue *queue;
  642. /* Check whether the frontend requested multiple queues
  643. * and read the number requested.
  644. */
  645. err = xenbus_scanf(XBT_NIL, dev->otherend,
  646. "multi-queue-num-queues",
  647. "%u", &requested_num_queues);
  648. if (err < 0) {
  649. requested_num_queues = 1; /* Fall back to single queue */
  650. } else if (requested_num_queues > xenvif_max_queues) {
  651. /* buggy or malicious guest */
  652. xenbus_dev_fatal(dev, err,
  653. "guest requested %u queues, exceeding the maximum of %u.",
  654. requested_num_queues, xenvif_max_queues);
  655. return;
  656. }
  657. err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
  658. if (err) {
  659. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  660. return;
  661. }
  662. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  663. xen_register_watchers(dev, be->vif);
  664. read_xenbus_vif_flags(be);
  665. /* Use the number of queues requested by the frontend */
  666. be->vif->queues = vzalloc(requested_num_queues *
  667. sizeof(struct xenvif_queue));
  668. be->vif->num_queues = requested_num_queues;
  669. be->vif->stalled_queues = requested_num_queues;
  670. for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
  671. queue = &be->vif->queues[queue_index];
  672. queue->vif = be->vif;
  673. queue->id = queue_index;
  674. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  675. be->vif->dev->name, queue->id);
  676. err = xenvif_init_queue(queue);
  677. if (err) {
  678. /* xenvif_init_queue() cleans up after itself on
  679. * failure, but we need to clean up any previously
  680. * initialised queues. Set num_queues to i so that
  681. * earlier queues can be destroyed using the regular
  682. * disconnect logic.
  683. */
  684. be->vif->num_queues = queue_index;
  685. goto err;
  686. }
  687. queue->credit_bytes = credit_bytes;
  688. queue->remaining_credit = credit_bytes;
  689. queue->credit_usec = credit_usec;
  690. err = connect_rings(be, queue);
  691. if (err) {
  692. /* connect_rings() cleans up after itself on failure,
  693. * but we need to clean up after xenvif_init_queue() here,
  694. * and also clean up any previously initialised queues.
  695. */
  696. xenvif_deinit_queue(queue);
  697. be->vif->num_queues = queue_index;
  698. goto err;
  699. }
  700. }
  701. #ifdef CONFIG_DEBUG_FS
  702. xenvif_debugfs_addif(be->vif);
  703. #endif /* CONFIG_DEBUG_FS */
  704. /* Initialisation completed, tell core driver the number of
  705. * active queues.
  706. */
  707. rtnl_lock();
  708. netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
  709. netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
  710. rtnl_unlock();
  711. xenvif_carrier_on(be->vif);
  712. unregister_hotplug_status_watch(be);
  713. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  714. hotplug_status_changed,
  715. "%s/%s", dev->nodename, "hotplug-status");
  716. if (!err)
  717. be->have_hotplug_status_watch = 1;
  718. netif_tx_wake_all_queues(be->vif->dev);
  719. return;
  720. err:
  721. if (be->vif->num_queues > 0)
  722. xenvif_disconnect(be->vif); /* Clean up existing queues */
  723. vfree(be->vif->queues);
  724. be->vif->queues = NULL;
  725. be->vif->num_queues = 0;
  726. return;
  727. }
  728. static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
  729. {
  730. struct xenbus_device *dev = be->dev;
  731. unsigned int num_queues = queue->vif->num_queues;
  732. unsigned long tx_ring_ref, rx_ring_ref;
  733. unsigned int tx_evtchn, rx_evtchn;
  734. int err;
  735. char *xspath;
  736. size_t xspathsize;
  737. const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
  738. /* If the frontend requested 1 queue, or we have fallen back
  739. * to single queue due to lack of frontend support for multi-
  740. * queue, expect the remaining XenStore keys in the toplevel
  741. * directory. Otherwise, expect them in a subdirectory called
  742. * queue-N.
  743. */
  744. if (num_queues == 1) {
  745. xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
  746. if (!xspath) {
  747. xenbus_dev_fatal(dev, -ENOMEM,
  748. "reading ring references");
  749. return -ENOMEM;
  750. }
  751. strcpy(xspath, dev->otherend);
  752. } else {
  753. xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
  754. xspath = kzalloc(xspathsize, GFP_KERNEL);
  755. if (!xspath) {
  756. xenbus_dev_fatal(dev, -ENOMEM,
  757. "reading ring references");
  758. return -ENOMEM;
  759. }
  760. snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
  761. queue->id);
  762. }
  763. err = xenbus_gather(XBT_NIL, xspath,
  764. "tx-ring-ref", "%lu", &tx_ring_ref,
  765. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  766. if (err) {
  767. xenbus_dev_fatal(dev, err,
  768. "reading %s/ring-ref",
  769. xspath);
  770. goto err;
  771. }
  772. /* Try split event channels first, then single event channel. */
  773. err = xenbus_gather(XBT_NIL, xspath,
  774. "event-channel-tx", "%u", &tx_evtchn,
  775. "event-channel-rx", "%u", &rx_evtchn, NULL);
  776. if (err < 0) {
  777. err = xenbus_scanf(XBT_NIL, xspath,
  778. "event-channel", "%u", &tx_evtchn);
  779. if (err < 0) {
  780. xenbus_dev_fatal(dev, err,
  781. "reading %s/event-channel(-tx/rx)",
  782. xspath);
  783. goto err;
  784. }
  785. rx_evtchn = tx_evtchn;
  786. }
  787. /* Map the shared frame, irq etc. */
  788. err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
  789. tx_evtchn, rx_evtchn);
  790. if (err) {
  791. xenbus_dev_fatal(dev, err,
  792. "mapping shared-frames %lu/%lu port tx %u rx %u",
  793. tx_ring_ref, rx_ring_ref,
  794. tx_evtchn, rx_evtchn);
  795. goto err;
  796. }
  797. err = 0;
  798. err: /* Regular return falls through with err == 0 */
  799. kfree(xspath);
  800. return err;
  801. }
  802. static int read_xenbus_vif_flags(struct backend_info *be)
  803. {
  804. struct xenvif *vif = be->vif;
  805. struct xenbus_device *dev = be->dev;
  806. unsigned int rx_copy;
  807. int err, val;
  808. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  809. &rx_copy);
  810. if (err == -ENOENT) {
  811. err = 0;
  812. rx_copy = 0;
  813. }
  814. if (err < 0) {
  815. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  816. dev->otherend);
  817. return err;
  818. }
  819. if (!rx_copy)
  820. return -EOPNOTSUPP;
  821. if (xenbus_scanf(XBT_NIL, dev->otherend,
  822. "feature-rx-notify", "%d", &val) < 0)
  823. val = 0;
  824. if (!val) {
  825. /* - Reduce drain timeout to poll more frequently for
  826. * Rx requests.
  827. * - Disable Rx stall detection.
  828. */
  829. be->vif->drain_timeout = msecs_to_jiffies(30);
  830. be->vif->stall_timeout = 0;
  831. }
  832. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
  833. "%d", &val) < 0)
  834. val = 0;
  835. vif->can_sg = !!val;
  836. vif->gso_mask = 0;
  837. vif->gso_prefix_mask = 0;
  838. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
  839. "%d", &val) < 0)
  840. val = 0;
  841. if (val)
  842. vif->gso_mask |= GSO_BIT(TCPV4);
  843. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
  844. "%d", &val) < 0)
  845. val = 0;
  846. if (val)
  847. vif->gso_prefix_mask |= GSO_BIT(TCPV4);
  848. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
  849. "%d", &val) < 0)
  850. val = 0;
  851. if (val)
  852. vif->gso_mask |= GSO_BIT(TCPV6);
  853. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
  854. "%d", &val) < 0)
  855. val = 0;
  856. if (val)
  857. vif->gso_prefix_mask |= GSO_BIT(TCPV6);
  858. if (vif->gso_mask & vif->gso_prefix_mask) {
  859. xenbus_dev_fatal(dev, err,
  860. "%s: gso and gso prefix flags are not "
  861. "mutually exclusive",
  862. dev->otherend);
  863. return -EOPNOTSUPP;
  864. }
  865. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
  866. "%d", &val) < 0)
  867. val = 0;
  868. vif->ip_csum = !val;
  869. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
  870. "%d", &val) < 0)
  871. val = 0;
  872. vif->ipv6_csum = !!val;
  873. return 0;
  874. }
  875. static const struct xenbus_device_id netback_ids[] = {
  876. { "vif" },
  877. { "" }
  878. };
  879. static struct xenbus_driver netback_driver = {
  880. .ids = netback_ids,
  881. .probe = netback_probe,
  882. .remove = netback_remove,
  883. .uevent = netback_uevent,
  884. .otherend_changed = frontend_changed,
  885. };
  886. int xenvif_xenbus_init(void)
  887. {
  888. return xenbus_register_backend(&netback_driver);
  889. }
  890. void xenvif_xenbus_fini(void)
  891. {
  892. return xenbus_unregister_driver(&netback_driver);
  893. }