xenbus.c 30 KB

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