xenbus.c 30 KB

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