xenbus.c 24 KB

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