xenbus.c 30 KB

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