xenbus.c 26 KB

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