xenbus.c 29 KB

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