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