xenbus.c 30 KB

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