xenbus.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 int 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. err = backend_create_xenvif(be);
  305. if (err)
  306. goto fail;
  307. return 0;
  308. abort_transaction:
  309. xenbus_transaction_end(xbt, 1);
  310. xenbus_dev_fatal(dev, err, "%s", message);
  311. fail:
  312. pr_debug("failed\n");
  313. netback_remove(dev);
  314. return err;
  315. }
  316. /*
  317. * Handle the creation of the hotplug script environment. We add the script
  318. * and vif variables to the environment, for the benefit of the vif-* hotplug
  319. * scripts.
  320. */
  321. static int netback_uevent(struct xenbus_device *xdev,
  322. struct kobj_uevent_env *env)
  323. {
  324. struct backend_info *be = dev_get_drvdata(&xdev->dev);
  325. char *val;
  326. val = xenbus_read(XBT_NIL, xdev->nodename, "script", NULL);
  327. if (IS_ERR(val)) {
  328. int err = PTR_ERR(val);
  329. xenbus_dev_fatal(xdev, err, "reading script");
  330. return err;
  331. } else {
  332. if (add_uevent_var(env, "script=%s", val)) {
  333. kfree(val);
  334. return -ENOMEM;
  335. }
  336. kfree(val);
  337. }
  338. if (!be || !be->vif)
  339. return 0;
  340. return add_uevent_var(env, "vif=%s", be->vif->dev->name);
  341. }
  342. static int backend_create_xenvif(struct backend_info *be)
  343. {
  344. int err;
  345. long handle;
  346. struct xenbus_device *dev = be->dev;
  347. struct xenvif *vif;
  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. vif = xenvif_alloc(&dev->dev, dev->otherend_id, handle);
  356. if (IS_ERR(vif)) {
  357. err = PTR_ERR(vif);
  358. xenbus_dev_fatal(dev, err, "creating interface");
  359. return err;
  360. }
  361. be->vif = vif;
  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->remaining_credit = credit_bytes;
  636. queue->credit_usec = credit_usec;
  637. err = connect_rings(be, queue);
  638. if (err) {
  639. /* connect_rings() cleans up after itself on failure,
  640. * but we need to clean up after xenvif_init_queue() here,
  641. * and also clean up any previously initialised queues.
  642. */
  643. xenvif_deinit_queue(queue);
  644. be->vif->num_queues = queue_index;
  645. goto err;
  646. }
  647. }
  648. #ifdef CONFIG_DEBUG_FS
  649. xenvif_debugfs_addif(be->vif);
  650. #endif /* CONFIG_DEBUG_FS */
  651. /* Initialisation completed, tell core driver the number of
  652. * active queues.
  653. */
  654. rtnl_lock();
  655. netif_set_real_num_tx_queues(be->vif->dev, requested_num_queues);
  656. netif_set_real_num_rx_queues(be->vif->dev, requested_num_queues);
  657. rtnl_unlock();
  658. xenvif_carrier_on(be->vif);
  659. unregister_hotplug_status_watch(be);
  660. err = xenbus_watch_pathfmt(dev, &be->hotplug_status_watch,
  661. hotplug_status_changed,
  662. "%s/%s", dev->nodename, "hotplug-status");
  663. if (!err)
  664. be->have_hotplug_status_watch = 1;
  665. netif_tx_wake_all_queues(be->vif->dev);
  666. return;
  667. err:
  668. if (be->vif->num_queues > 0)
  669. xenvif_disconnect(be->vif); /* Clean up existing queues */
  670. vfree(be->vif->queues);
  671. be->vif->queues = NULL;
  672. be->vif->num_queues = 0;
  673. return;
  674. }
  675. static int connect_rings(struct backend_info *be, struct xenvif_queue *queue)
  676. {
  677. struct xenbus_device *dev = be->dev;
  678. unsigned int num_queues = queue->vif->num_queues;
  679. unsigned long tx_ring_ref, rx_ring_ref;
  680. unsigned int tx_evtchn, rx_evtchn;
  681. int err;
  682. char *xspath;
  683. size_t xspathsize;
  684. const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
  685. /* If the frontend requested 1 queue, or we have fallen back
  686. * to single queue due to lack of frontend support for multi-
  687. * queue, expect the remaining XenStore keys in the toplevel
  688. * directory. Otherwise, expect them in a subdirectory called
  689. * queue-N.
  690. */
  691. if (num_queues == 1) {
  692. xspath = kzalloc(strlen(dev->otherend) + 1, GFP_KERNEL);
  693. if (!xspath) {
  694. xenbus_dev_fatal(dev, -ENOMEM,
  695. "reading ring references");
  696. return -ENOMEM;
  697. }
  698. strcpy(xspath, dev->otherend);
  699. } else {
  700. xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
  701. xspath = kzalloc(xspathsize, GFP_KERNEL);
  702. if (!xspath) {
  703. xenbus_dev_fatal(dev, -ENOMEM,
  704. "reading ring references");
  705. return -ENOMEM;
  706. }
  707. snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend,
  708. queue->id);
  709. }
  710. err = xenbus_gather(XBT_NIL, xspath,
  711. "tx-ring-ref", "%lu", &tx_ring_ref,
  712. "rx-ring-ref", "%lu", &rx_ring_ref, NULL);
  713. if (err) {
  714. xenbus_dev_fatal(dev, err,
  715. "reading %s/ring-ref",
  716. xspath);
  717. goto err;
  718. }
  719. /* Try split event channels first, then single event channel. */
  720. err = xenbus_gather(XBT_NIL, xspath,
  721. "event-channel-tx", "%u", &tx_evtchn,
  722. "event-channel-rx", "%u", &rx_evtchn, NULL);
  723. if (err < 0) {
  724. err = xenbus_scanf(XBT_NIL, xspath,
  725. "event-channel", "%u", &tx_evtchn);
  726. if (err < 0) {
  727. xenbus_dev_fatal(dev, err,
  728. "reading %s/event-channel(-tx/rx)",
  729. xspath);
  730. goto err;
  731. }
  732. rx_evtchn = tx_evtchn;
  733. }
  734. /* Map the shared frame, irq etc. */
  735. err = xenvif_connect(queue, tx_ring_ref, rx_ring_ref,
  736. tx_evtchn, rx_evtchn);
  737. if (err) {
  738. xenbus_dev_fatal(dev, err,
  739. "mapping shared-frames %lu/%lu port tx %u rx %u",
  740. tx_ring_ref, rx_ring_ref,
  741. tx_evtchn, rx_evtchn);
  742. goto err;
  743. }
  744. err = 0;
  745. err: /* Regular return falls through with err == 0 */
  746. kfree(xspath);
  747. return err;
  748. }
  749. static int read_xenbus_vif_flags(struct backend_info *be)
  750. {
  751. struct xenvif *vif = be->vif;
  752. struct xenbus_device *dev = be->dev;
  753. unsigned int rx_copy;
  754. int err, val;
  755. err = xenbus_scanf(XBT_NIL, dev->otherend, "request-rx-copy", "%u",
  756. &rx_copy);
  757. if (err == -ENOENT) {
  758. err = 0;
  759. rx_copy = 0;
  760. }
  761. if (err < 0) {
  762. xenbus_dev_fatal(dev, err, "reading %s/request-rx-copy",
  763. dev->otherend);
  764. return err;
  765. }
  766. if (!rx_copy)
  767. return -EOPNOTSUPP;
  768. if (xenbus_scanf(XBT_NIL, dev->otherend,
  769. "feature-rx-notify", "%d", &val) < 0)
  770. val = 0;
  771. if (!val) {
  772. /* - Reduce drain timeout to poll more frequently for
  773. * Rx requests.
  774. * - Disable Rx stall detection.
  775. */
  776. be->vif->drain_timeout = msecs_to_jiffies(30);
  777. be->vif->stall_timeout = 0;
  778. }
  779. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg",
  780. "%d", &val) < 0)
  781. val = 0;
  782. vif->can_sg = !!val;
  783. vif->gso_mask = 0;
  784. vif->gso_prefix_mask = 0;
  785. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4",
  786. "%d", &val) < 0)
  787. val = 0;
  788. if (val)
  789. vif->gso_mask |= GSO_BIT(TCPV4);
  790. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv4-prefix",
  791. "%d", &val) < 0)
  792. val = 0;
  793. if (val)
  794. vif->gso_prefix_mask |= GSO_BIT(TCPV4);
  795. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6",
  796. "%d", &val) < 0)
  797. val = 0;
  798. if (val)
  799. vif->gso_mask |= GSO_BIT(TCPV6);
  800. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-gso-tcpv6-prefix",
  801. "%d", &val) < 0)
  802. val = 0;
  803. if (val)
  804. vif->gso_prefix_mask |= GSO_BIT(TCPV6);
  805. if (vif->gso_mask & vif->gso_prefix_mask) {
  806. xenbus_dev_fatal(dev, err,
  807. "%s: gso and gso prefix flags are not "
  808. "mutually exclusive",
  809. dev->otherend);
  810. return -EOPNOTSUPP;
  811. }
  812. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-no-csum-offload",
  813. "%d", &val) < 0)
  814. val = 0;
  815. vif->ip_csum = !val;
  816. if (xenbus_scanf(XBT_NIL, dev->otherend, "feature-ipv6-csum-offload",
  817. "%d", &val) < 0)
  818. val = 0;
  819. vif->ipv6_csum = !!val;
  820. return 0;
  821. }
  822. static const struct xenbus_device_id netback_ids[] = {
  823. { "vif" },
  824. { "" }
  825. };
  826. static struct xenbus_driver netback_driver = {
  827. .ids = netback_ids,
  828. .probe = netback_probe,
  829. .remove = netback_remove,
  830. .uevent = netback_uevent,
  831. .otherend_changed = frontend_changed,
  832. };
  833. int xenvif_xenbus_init(void)
  834. {
  835. return xenbus_register_backend(&netback_driver);
  836. }
  837. void xenvif_xenbus_fini(void)
  838. {
  839. return xenbus_unregister_driver(&netback_driver);
  840. }