xenbus.c 30 KB

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