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. 0600,
  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. 0400,
  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 num_queues = vif->num_queues;
  420. unsigned int queue_index;
  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. /* At this point some of the handlers may still be active
  427. * so we need to have additional synchronization here.
  428. */
  429. vif->num_queues = 0;
  430. synchronize_net();
  431. for (queue_index = 0; queue_index < num_queues; ++queue_index)
  432. xenvif_deinit_queue(&vif->queues[queue_index]);
  433. vfree(vif->queues);
  434. vif->queues = NULL;
  435. xenvif_disconnect_ctrl(vif);
  436. }
  437. }
  438. static void backend_connect(struct backend_info *be)
  439. {
  440. if (be->vif)
  441. connect(be);
  442. }
  443. static inline void backend_switch_state(struct backend_info *be,
  444. enum xenbus_state state)
  445. {
  446. struct xenbus_device *dev = be->dev;
  447. pr_debug("%s -> %s\n", dev->nodename, xenbus_strstate(state));
  448. be->state = state;
  449. /* If we are waiting for a hotplug script then defer the
  450. * actual xenbus state change.
  451. */
  452. if (!be->have_hotplug_status_watch)
  453. xenbus_switch_state(dev, state);
  454. }
  455. /* Handle backend state transitions:
  456. *
  457. * The backend state starts in Initialising and the following transitions are
  458. * allowed.
  459. *
  460. * Initialising -> InitWait -> Connected
  461. * \
  462. * \ ^ \ |
  463. * \ | \ |
  464. * \ | \ |
  465. * \ | \ |
  466. * \ | \ |
  467. * \ | \ |
  468. * V | V V
  469. *
  470. * Closed <-> Closing
  471. *
  472. * The state argument specifies the eventual state of the backend and the
  473. * function transitions to that state via the shortest path.
  474. */
  475. static void set_backend_state(struct backend_info *be,
  476. enum xenbus_state state)
  477. {
  478. while (be->state != state) {
  479. switch (be->state) {
  480. case XenbusStateInitialising:
  481. switch (state) {
  482. case XenbusStateInitWait:
  483. case XenbusStateConnected:
  484. case XenbusStateClosing:
  485. backend_switch_state(be, XenbusStateInitWait);
  486. break;
  487. case XenbusStateClosed:
  488. backend_switch_state(be, XenbusStateClosed);
  489. break;
  490. default:
  491. BUG();
  492. }
  493. break;
  494. case XenbusStateClosed:
  495. switch (state) {
  496. case XenbusStateInitWait:
  497. case XenbusStateConnected:
  498. backend_switch_state(be, XenbusStateInitWait);
  499. break;
  500. case XenbusStateClosing:
  501. backend_switch_state(be, XenbusStateClosing);
  502. break;
  503. default:
  504. BUG();
  505. }
  506. break;
  507. case XenbusStateInitWait:
  508. switch (state) {
  509. case XenbusStateConnected:
  510. backend_connect(be);
  511. backend_switch_state(be, XenbusStateConnected);
  512. break;
  513. case XenbusStateClosing:
  514. case XenbusStateClosed:
  515. backend_switch_state(be, XenbusStateClosing);
  516. break;
  517. default:
  518. BUG();
  519. }
  520. break;
  521. case XenbusStateConnected:
  522. switch (state) {
  523. case XenbusStateInitWait:
  524. case XenbusStateClosing:
  525. case XenbusStateClosed:
  526. backend_disconnect(be);
  527. backend_switch_state(be, XenbusStateClosing);
  528. break;
  529. default:
  530. BUG();
  531. }
  532. break;
  533. case XenbusStateClosing:
  534. switch (state) {
  535. case XenbusStateInitWait:
  536. case XenbusStateConnected:
  537. case XenbusStateClosed:
  538. backend_switch_state(be, XenbusStateClosed);
  539. break;
  540. default:
  541. BUG();
  542. }
  543. break;
  544. default:
  545. BUG();
  546. }
  547. }
  548. }
  549. /**
  550. * Callback received when the frontend's state changes.
  551. */
  552. static void frontend_changed(struct xenbus_device *dev,
  553. enum xenbus_state frontend_state)
  554. {
  555. struct backend_info *be = dev_get_drvdata(&dev->dev);
  556. pr_debug("%s -> %s\n", dev->otherend, xenbus_strstate(frontend_state));
  557. be->frontend_state = frontend_state;
  558. switch (frontend_state) {
  559. case XenbusStateInitialising:
  560. set_backend_state(be, XenbusStateInitWait);
  561. break;
  562. case XenbusStateInitialised:
  563. break;
  564. case XenbusStateConnected:
  565. set_backend_state(be, XenbusStateConnected);
  566. break;
  567. case XenbusStateClosing:
  568. set_backend_state(be, XenbusStateClosing);
  569. break;
  570. case XenbusStateClosed:
  571. set_backend_state(be, XenbusStateClosed);
  572. if (xenbus_dev_is_online(dev))
  573. break;
  574. /* fall through if not online */
  575. case XenbusStateUnknown:
  576. set_backend_state(be, XenbusStateClosed);
  577. device_unregister(&dev->dev);
  578. break;
  579. default:
  580. xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
  581. frontend_state);
  582. break;
  583. }
  584. }
  585. static void xen_net_read_rate(struct xenbus_device *dev,
  586. unsigned long *bytes, unsigned long *usec)
  587. {
  588. char *s, *e;
  589. unsigned long b, u;
  590. char *ratestr;
  591. /* Default to unlimited bandwidth. */
  592. *bytes = ~0UL;
  593. *usec = 0;
  594. ratestr = xenbus_read(XBT_NIL, dev->nodename, "rate", NULL);
  595. if (IS_ERR(ratestr))
  596. return;
  597. s = ratestr;
  598. b = simple_strtoul(s, &e, 10);
  599. if ((s == e) || (*e != ','))
  600. goto fail;
  601. s = e + 1;
  602. u = simple_strtoul(s, &e, 10);
  603. if ((s == e) || (*e != '\0'))
  604. goto fail;
  605. *bytes = b;
  606. *usec = u;
  607. kfree(ratestr);
  608. return;
  609. fail:
  610. pr_warn("Failed to parse network rate limit. Traffic unlimited.\n");
  611. kfree(ratestr);
  612. }
  613. static int xen_net_read_mac(struct xenbus_device *dev, u8 mac[])
  614. {
  615. char *s, *e, *macstr;
  616. int i;
  617. macstr = s = xenbus_read(XBT_NIL, dev->nodename, "mac", NULL);
  618. if (IS_ERR(macstr))
  619. return PTR_ERR(macstr);
  620. for (i = 0; i < ETH_ALEN; i++) {
  621. mac[i] = simple_strtoul(s, &e, 16);
  622. if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
  623. kfree(macstr);
  624. return -ENOENT;
  625. }
  626. s = e+1;
  627. }
  628. kfree(macstr);
  629. return 0;
  630. }
  631. static void xen_net_rate_changed(struct xenbus_watch *watch,
  632. const char *path, const char *token)
  633. {
  634. struct xenvif *vif = container_of(watch, struct xenvif, credit_watch);
  635. struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
  636. unsigned long credit_bytes;
  637. unsigned long credit_usec;
  638. unsigned int queue_index;
  639. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  640. for (queue_index = 0; queue_index < vif->num_queues; queue_index++) {
  641. struct xenvif_queue *queue = &vif->queues[queue_index];
  642. queue->credit_bytes = credit_bytes;
  643. queue->credit_usec = credit_usec;
  644. if (!mod_timer_pending(&queue->credit_timeout, jiffies) &&
  645. queue->remaining_credit > queue->credit_bytes) {
  646. queue->remaining_credit = queue->credit_bytes;
  647. }
  648. }
  649. }
  650. static int xen_register_credit_watch(struct xenbus_device *dev,
  651. struct xenvif *vif)
  652. {
  653. int err = 0;
  654. char *node;
  655. unsigned maxlen = strlen(dev->nodename) + sizeof("/rate");
  656. if (vif->credit_watch.node)
  657. return -EADDRINUSE;
  658. node = kmalloc(maxlen, GFP_KERNEL);
  659. if (!node)
  660. return -ENOMEM;
  661. snprintf(node, maxlen, "%s/rate", dev->nodename);
  662. vif->credit_watch.node = node;
  663. vif->credit_watch.callback = xen_net_rate_changed;
  664. err = register_xenbus_watch(&vif->credit_watch);
  665. if (err) {
  666. pr_err("Failed to set watcher %s\n", vif->credit_watch.node);
  667. kfree(node);
  668. vif->credit_watch.node = NULL;
  669. vif->credit_watch.callback = NULL;
  670. }
  671. return err;
  672. }
  673. static void xen_unregister_credit_watch(struct xenvif *vif)
  674. {
  675. if (vif->credit_watch.node) {
  676. unregister_xenbus_watch(&vif->credit_watch);
  677. kfree(vif->credit_watch.node);
  678. vif->credit_watch.node = NULL;
  679. }
  680. }
  681. static void xen_mcast_ctrl_changed(struct xenbus_watch *watch,
  682. const char *path, const char *token)
  683. {
  684. struct xenvif *vif = container_of(watch, struct xenvif,
  685. mcast_ctrl_watch);
  686. struct xenbus_device *dev = xenvif_to_xenbus_device(vif);
  687. vif->multicast_control = !!xenbus_read_unsigned(dev->otherend,
  688. "request-multicast-control", 0);
  689. }
  690. static int xen_register_mcast_ctrl_watch(struct xenbus_device *dev,
  691. struct xenvif *vif)
  692. {
  693. int err = 0;
  694. char *node;
  695. unsigned maxlen = strlen(dev->otherend) +
  696. sizeof("/request-multicast-control");
  697. if (vif->mcast_ctrl_watch.node) {
  698. pr_err_ratelimited("Watch is already registered\n");
  699. return -EADDRINUSE;
  700. }
  701. node = kmalloc(maxlen, GFP_KERNEL);
  702. if (!node) {
  703. pr_err("Failed to allocate memory for watch\n");
  704. return -ENOMEM;
  705. }
  706. snprintf(node, maxlen, "%s/request-multicast-control",
  707. dev->otherend);
  708. vif->mcast_ctrl_watch.node = node;
  709. vif->mcast_ctrl_watch.callback = xen_mcast_ctrl_changed;
  710. err = register_xenbus_watch(&vif->mcast_ctrl_watch);
  711. if (err) {
  712. pr_err("Failed to set watcher %s\n",
  713. vif->mcast_ctrl_watch.node);
  714. kfree(node);
  715. vif->mcast_ctrl_watch.node = NULL;
  716. vif->mcast_ctrl_watch.callback = NULL;
  717. }
  718. return err;
  719. }
  720. static void xen_unregister_mcast_ctrl_watch(struct xenvif *vif)
  721. {
  722. if (vif->mcast_ctrl_watch.node) {
  723. unregister_xenbus_watch(&vif->mcast_ctrl_watch);
  724. kfree(vif->mcast_ctrl_watch.node);
  725. vif->mcast_ctrl_watch.node = NULL;
  726. }
  727. }
  728. static void xen_register_watchers(struct xenbus_device *dev,
  729. struct xenvif *vif)
  730. {
  731. xen_register_credit_watch(dev, vif);
  732. xen_register_mcast_ctrl_watch(dev, vif);
  733. }
  734. static void xen_unregister_watchers(struct xenvif *vif)
  735. {
  736. xen_unregister_mcast_ctrl_watch(vif);
  737. xen_unregister_credit_watch(vif);
  738. }
  739. static void unregister_hotplug_status_watch(struct backend_info *be)
  740. {
  741. if (be->have_hotplug_status_watch) {
  742. unregister_xenbus_watch(&be->hotplug_status_watch);
  743. kfree(be->hotplug_status_watch.node);
  744. }
  745. be->have_hotplug_status_watch = 0;
  746. }
  747. static void hotplug_status_changed(struct xenbus_watch *watch,
  748. const char *path,
  749. const char *token)
  750. {
  751. struct backend_info *be = container_of(watch,
  752. struct backend_info,
  753. hotplug_status_watch);
  754. char *str;
  755. unsigned int len;
  756. str = xenbus_read(XBT_NIL, be->dev->nodename, "hotplug-status", &len);
  757. if (IS_ERR(str))
  758. return;
  759. if (len == sizeof("connected")-1 && !memcmp(str, "connected", len)) {
  760. /* Complete any pending state change */
  761. xenbus_switch_state(be->dev, be->state);
  762. /* Not interested in this watch anymore. */
  763. unregister_hotplug_status_watch(be);
  764. }
  765. kfree(str);
  766. }
  767. static int connect_ctrl_ring(struct backend_info *be)
  768. {
  769. struct xenbus_device *dev = be->dev;
  770. struct xenvif *vif = be->vif;
  771. unsigned int val;
  772. grant_ref_t ring_ref;
  773. unsigned int evtchn;
  774. int err;
  775. err = xenbus_scanf(XBT_NIL, dev->otherend,
  776. "ctrl-ring-ref", "%u", &val);
  777. if (err < 0)
  778. goto done; /* The frontend does not have a control ring */
  779. ring_ref = val;
  780. err = xenbus_scanf(XBT_NIL, dev->otherend,
  781. "event-channel-ctrl", "%u", &val);
  782. if (err < 0) {
  783. xenbus_dev_fatal(dev, err,
  784. "reading %s/event-channel-ctrl",
  785. dev->otherend);
  786. goto fail;
  787. }
  788. evtchn = val;
  789. err = xenvif_connect_ctrl(vif, ring_ref, evtchn);
  790. if (err) {
  791. xenbus_dev_fatal(dev, err,
  792. "mapping shared-frame %u port %u",
  793. ring_ref, evtchn);
  794. goto fail;
  795. }
  796. done:
  797. return 0;
  798. fail:
  799. return err;
  800. }
  801. static void connect(struct backend_info *be)
  802. {
  803. int err;
  804. struct xenbus_device *dev = be->dev;
  805. unsigned long credit_bytes, credit_usec;
  806. unsigned int queue_index;
  807. unsigned int requested_num_queues;
  808. struct xenvif_queue *queue;
  809. /* Check whether the frontend requested multiple queues
  810. * and read the number requested.
  811. */
  812. requested_num_queues = xenbus_read_unsigned(dev->otherend,
  813. "multi-queue-num-queues", 1);
  814. if (requested_num_queues > xenvif_max_queues) {
  815. /* buggy or malicious guest */
  816. xenbus_dev_fatal(dev, -EINVAL,
  817. "guest requested %u queues, exceeding the maximum of %u.",
  818. requested_num_queues, xenvif_max_queues);
  819. return;
  820. }
  821. err = xen_net_read_mac(dev, be->vif->fe_dev_addr);
  822. if (err) {
  823. xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
  824. return;
  825. }
  826. xen_net_read_rate(dev, &credit_bytes, &credit_usec);
  827. xen_unregister_watchers(be->vif);
  828. xen_register_watchers(dev, be->vif);
  829. read_xenbus_vif_flags(be);
  830. err = connect_ctrl_ring(be);
  831. if (err) {
  832. xenbus_dev_fatal(dev, err, "connecting control ring");
  833. return;
  834. }
  835. /* Use the number of queues requested by the frontend */
  836. be->vif->queues = vzalloc(requested_num_queues *
  837. sizeof(struct xenvif_queue));
  838. if (!be->vif->queues) {
  839. xenbus_dev_fatal(dev, -ENOMEM,
  840. "allocating queues");
  841. return;
  842. }
  843. be->vif->num_queues = requested_num_queues;
  844. be->vif->stalled_queues = requested_num_queues;
  845. for (queue_index = 0; queue_index < requested_num_queues; ++queue_index) {
  846. queue = &be->vif->queues[queue_index];
  847. queue->vif = be->vif;
  848. queue->id = queue_index;
  849. snprintf(queue->name, sizeof(queue->name), "%s-q%u",
  850. be->vif->dev->name, queue->id);
  851. err = xenvif_init_queue(queue);
  852. if (err) {
  853. /* xenvif_init_queue() cleans up after itself on
  854. * failure, but we need to clean up any previously
  855. * initialised queues. Set num_queues to i so that
  856. * earlier queues can be destroyed using the regular
  857. * disconnect logic.
  858. */
  859. be->vif->num_queues = queue_index;
  860. goto err;
  861. }
  862. queue->credit_bytes = credit_bytes;
  863. queue->remaining_credit = credit_bytes;
  864. queue->credit_usec = credit_usec;
  865. err = connect_data_rings(be, queue);
  866. if (err) {
  867. /* connect_data_rings() cleans up after itself on
  868. * failure, but we need to clean up after
  869. * xenvif_init_queue() here, and also clean up any
  870. * previously initialised queues.
  871. */
  872. xenvif_deinit_queue(queue);
  873. be->vif->num_queues = queue_index;
  874. goto err;
  875. }
  876. }
  877. #ifdef CONFIG_DEBUG_FS
  878. xenvif_debugfs_addif(be->vif);
  879. #endif /* CONFIG_DEBUG_FS */
  880. /* Initialisation completed, tell core driver the number of
  881. * active queues.
  882. */
  883. rtnl_lock();
  884. netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
  885. netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
  886. rtnl_unlock();
  887. xenvif_carrier_on(be->vif);
  888. unregister_hotplug_status_watch(be);
  889. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  890. hotplug_status_changed,
  891. "%s/%s", dev->nodename, "hotplug-status");
  892. if (!err)
  893. be->have_hotplug_status_watch = 1;
  894. netif_tx_wake_all_queues(be->vif->dev);
  895. return;
  896. err:
  897. if (be->vif->num_queues > 0)
  898. xenvif_disconnect_data(be->vif); /* Clean up existing queues */
  899. for (queue_index = 0; queue_index < be->vif->num_queues; ++queue_index)
  900. xenvif_deinit_queue(&be->vif->queues[queue_index]);
  901. vfree(be->vif->queues);
  902. be->vif->queues = NULL;
  903. be->vif->num_queues = 0;
  904. xenvif_disconnect_ctrl(be->vif);
  905. return;
  906. }
  907. static int connect_data_rings(struct backend_info *be,
  908. struct xenvif_queue *queue)
  909. {
  910. struct xenbus_device *dev = be->dev;
  911. unsigned int num_queues = queue->vif->num_queues;
  912. unsigned long tx_ring_ref, rx_ring_ref;
  913. unsigned int tx_evtchn, rx_evtchn;
  914. int err;
  915. char *xspath;
  916. size_t xspathsize;
  917. const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
  918. /* If the frontend requested 1 queue, or we have fallen back
  919. * to single queue due to lack of frontend support for multi-
  920. * queue, expect the remaining XenStore keys in the toplevel
  921. * directory. Otherwise, expect them in a subdirectory called
  922. * queue-N.
  923. */
  924. if (num_queues == 1) {
  925. xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
  926. if (!xspath) {
  927. xenbus_dev_fatal(dev, -ENOMEM,
  928. "reading ring references");
  929. return -ENOMEM;
  930. }
  931. strcpy(xspath, dev->otherend);
  932. } else {
  933. xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
  934. xspath = kzalloc(xspathsize, GFP_KERNEL);
  935. if (!xspath) {
  936. xenbus_dev_fatal(dev, -ENOMEM,
  937. "reading ring references");
  938. return -ENOMEM;
  939. }
  940. snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
  941. queue->id);
  942. }
  943. err = xenbus_gather(XBT_NIL, xspath,
  944. "tx-ring-ref", "%lu", &tx_ring_ref,
  945. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  946. if (err) {
  947. xenbus_dev_fatal(dev, err,
  948. "reading %s/ring-ref",
  949. xspath);
  950. goto err;
  951. }
  952. /* Try split event channels first, then single event channel. */
  953. err = xenbus_gather(XBT_NIL, xspath,
  954. "event-channel-tx", "%u", &tx_evtchn,
  955. "event-channel-rx", "%u", &rx_evtchn, NULL);
  956. if (err < 0) {
  957. err = xenbus_scanf(XBT_NIL, xspath,
  958. "event-channel", "%u", &tx_evtchn);
  959. if (err < 0) {
  960. xenbus_dev_fatal(dev, err,
  961. "reading %s/event-channel(-tx/rx)",
  962. xspath);
  963. goto err;
  964. }
  965. rx_evtchn = tx_evtchn;
  966. }
  967. /* Map the shared frame, irq etc. */
  968. err = xenvif_connect_data(queue, tx_ring_ref, rx_ring_ref,
  969. tx_evtchn, rx_evtchn);
  970. if (err) {
  971. xenbus_dev_fatal(dev, err,
  972. "mapping shared-frames %lu/%lu port tx %u rx %u",
  973. tx_ring_ref, rx_ring_ref,
  974. tx_evtchn, rx_evtchn);
  975. goto err;
  976. }
  977. err = 0;
  978. err: /* Regular return falls through with err == 0 */
  979. kfree(xspath);
  980. return err;
  981. }
  982. static int read_xenbus_vif_flags(struct backend_info *be)
  983. {
  984. struct xenvif *vif = be->vif;
  985. struct xenbus_device *dev = be->dev;
  986. unsigned int rx_copy;
  987. int err;
  988. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  989. &rx_copy);
  990. if (err == -ENOENT) {
  991. err = 0;
  992. rx_copy = 0;
  993. }
  994. if (err < 0) {
  995. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  996. dev->otherend);
  997. return err;
  998. }
  999. if (!rx_copy)
  1000. return -EOPNOTSUPP;
  1001. if (!xenbus_read_unsigned(dev->otherend, "feature-rx-notify", 0)) {
  1002. /* - Reduce drain timeout to poll more frequently for
  1003. * Rx requests.
  1004. * - Disable Rx stall detection.
  1005. */
  1006. be->vif->drain_timeout = msecs_to_jiffies(30);
  1007. be->vif->stall_timeout = 0;
  1008. }
  1009. vif->can_sg = !!xenbus_read_unsigned(dev->otherend, "feature-sg", 0);
  1010. vif->gso_mask = 0;
  1011. if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv4", 0))
  1012. vif->gso_mask |= GSO_BIT(TCPV4);
  1013. if (xenbus_read_unsigned(dev->otherend, "feature-gso-tcpv6", 0))
  1014. vif->gso_mask |= GSO_BIT(TCPV6);
  1015. vif->ip_csum = !xenbus_read_unsigned(dev->otherend,
  1016. "feature-no-csum-offload", 0);
  1017. vif->ipv6_csum = !!xenbus_read_unsigned(dev->otherend,
  1018. "feature-ipv6-csum-offload", 0);
  1019. return 0;
  1020. }
  1021. static const struct xenbus_device_id netback_ids[] = {
  1022. { "vif" },
  1023. { "" }
  1024. };
  1025. static struct xenbus_driver netback_driver = {
  1026. .ids = netback_ids,
  1027. .probe = netback_probe,
  1028. .remove = netback_remove,
  1029. .uevent = netback_uevent,
  1030. .otherend_changed = frontend_changed,
  1031. };
  1032. int xenvif_xenbus_init(void)
  1033. {
  1034. return xenbus_register_backend(&netback_driver);
  1035. }
  1036. void xenvif_xenbus_fini(void)
  1037. {
  1038. return xenbus_unregister_driver(&netback_driver);
  1039. }