xenbus.c 26 KB

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