xenbus.c 30 KB

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