xenbus.c 24 KB

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