2
0

xen-9p-backend.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Xen 9p backend
  3. *
  4. * Copyright Aporeto 2017
  5. *
  6. * Authors:
  7. * Stefano Stabellini <stefano@aporeto.com>
  8. *
  9. */
  10. /*
  11. * Not so fast! You might want to read the 9p developer docs first:
  12. * https://wiki.qemu.org/Documentation/9p
  13. */
  14. #include "qemu/osdep.h"
  15. #include "hw/9pfs/9p.h"
  16. #include "hw/xen/xen-legacy-backend.h"
  17. #include "hw/9pfs/xen-9pfs.h"
  18. #include "qapi/error.h"
  19. #include "qemu/config-file.h"
  20. #include "qemu/main-loop.h"
  21. #include "qemu/option.h"
  22. #include "qemu/iov.h"
  23. #include "fsdev/qemu-fsdev.h"
  24. #include "trace.h"
  25. #define VERSIONS "1"
  26. #define MAX_RINGS 8
  27. #define MAX_RING_ORDER 9
  28. typedef struct Xen9pfsRing {
  29. struct Xen9pfsDev *priv;
  30. int ref;
  31. xenevtchn_handle *evtchndev;
  32. int evtchn;
  33. int local_port;
  34. int ring_order;
  35. struct xen_9pfs_data_intf *intf;
  36. unsigned char *data;
  37. struct xen_9pfs_data ring;
  38. struct iovec *sg;
  39. QEMUBH *bh;
  40. Coroutine *co;
  41. /* local copies, so that we can read/write PDU data directly from
  42. * the ring */
  43. RING_IDX out_cons, out_size, in_cons;
  44. bool inprogress;
  45. } Xen9pfsRing;
  46. typedef struct Xen9pfsDev {
  47. struct XenLegacyDevice xendev; /* must be first */
  48. V9fsState state;
  49. char *path;
  50. char *security_model;
  51. char *tag;
  52. char *id;
  53. int num_rings;
  54. Xen9pfsRing *rings;
  55. MemReentrancyGuard mem_reentrancy_guard;
  56. } Xen9pfsDev;
  57. static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev);
  58. static void xen_9pfs_in_sg(Xen9pfsRing *ring,
  59. struct iovec *in_sg,
  60. int *num,
  61. uint32_t idx,
  62. uint32_t size)
  63. {
  64. RING_IDX cons, prod, masked_prod, masked_cons;
  65. cons = ring->intf->in_cons;
  66. prod = ring->intf->in_prod;
  67. xen_rmb();
  68. masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
  69. masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
  70. if (masked_prod < masked_cons) {
  71. in_sg[0].iov_base = ring->ring.in + masked_prod;
  72. in_sg[0].iov_len = masked_cons - masked_prod;
  73. *num = 1;
  74. } else {
  75. in_sg[0].iov_base = ring->ring.in + masked_prod;
  76. in_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) - masked_prod;
  77. in_sg[1].iov_base = ring->ring.in;
  78. in_sg[1].iov_len = masked_cons;
  79. *num = 2;
  80. }
  81. }
  82. static void xen_9pfs_out_sg(Xen9pfsRing *ring,
  83. struct iovec *out_sg,
  84. int *num,
  85. uint32_t idx)
  86. {
  87. RING_IDX cons, prod, masked_prod, masked_cons;
  88. cons = ring->intf->out_cons;
  89. prod = ring->intf->out_prod;
  90. xen_rmb();
  91. masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
  92. masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
  93. if (masked_cons < masked_prod) {
  94. out_sg[0].iov_base = ring->ring.out + masked_cons;
  95. out_sg[0].iov_len = ring->out_size;
  96. *num = 1;
  97. } else {
  98. if (ring->out_size >
  99. (XEN_FLEX_RING_SIZE(ring->ring_order) - masked_cons)) {
  100. out_sg[0].iov_base = ring->ring.out + masked_cons;
  101. out_sg[0].iov_len = XEN_FLEX_RING_SIZE(ring->ring_order) -
  102. masked_cons;
  103. out_sg[1].iov_base = ring->ring.out;
  104. out_sg[1].iov_len = ring->out_size -
  105. (XEN_FLEX_RING_SIZE(ring->ring_order) -
  106. masked_cons);
  107. *num = 2;
  108. } else {
  109. out_sg[0].iov_base = ring->ring.out + masked_cons;
  110. out_sg[0].iov_len = ring->out_size;
  111. *num = 1;
  112. }
  113. }
  114. }
  115. static ssize_t xen_9pfs_pdu_vmarshal(V9fsPDU *pdu,
  116. size_t offset,
  117. const char *fmt,
  118. va_list ap)
  119. {
  120. Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
  121. struct iovec in_sg[2];
  122. int num;
  123. ssize_t ret;
  124. xen_9pfs_in_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
  125. in_sg, &num, pdu->idx, ROUND_UP(offset + 128, 512));
  126. ret = v9fs_iov_vmarshal(in_sg, num, offset, 0, fmt, ap);
  127. if (ret < 0) {
  128. xen_pv_printf(&xen_9pfs->xendev, 0,
  129. "Failed to encode VirtFS reply type %d\n",
  130. pdu->id + 1);
  131. xen_be_set_state(&xen_9pfs->xendev, XenbusStateClosing);
  132. xen_9pfs_disconnect(&xen_9pfs->xendev);
  133. }
  134. return ret;
  135. }
  136. static ssize_t xen_9pfs_pdu_vunmarshal(V9fsPDU *pdu,
  137. size_t offset,
  138. const char *fmt,
  139. va_list ap)
  140. {
  141. Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
  142. struct iovec out_sg[2];
  143. int num;
  144. ssize_t ret;
  145. xen_9pfs_out_sg(&xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings],
  146. out_sg, &num, pdu->idx);
  147. ret = v9fs_iov_vunmarshal(out_sg, num, offset, 0, fmt, ap);
  148. if (ret < 0) {
  149. xen_pv_printf(&xen_9pfs->xendev, 0,
  150. "Failed to decode VirtFS request type %d\n", pdu->id);
  151. xen_be_set_state(&xen_9pfs->xendev, XenbusStateClosing);
  152. xen_9pfs_disconnect(&xen_9pfs->xendev);
  153. }
  154. return ret;
  155. }
  156. static void xen_9pfs_init_out_iov_from_pdu(V9fsPDU *pdu,
  157. struct iovec **piov,
  158. unsigned int *pniov,
  159. size_t size)
  160. {
  161. Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
  162. Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
  163. int num;
  164. g_free(ring->sg);
  165. ring->sg = g_new0(struct iovec, 2);
  166. xen_9pfs_out_sg(ring, ring->sg, &num, pdu->idx);
  167. *piov = ring->sg;
  168. *pniov = num;
  169. }
  170. static void xen_9pfs_init_in_iov_from_pdu(V9fsPDU *pdu,
  171. struct iovec **piov,
  172. unsigned int *pniov,
  173. size_t size)
  174. {
  175. Xen9pfsDev *xen_9pfs = container_of(pdu->s, Xen9pfsDev, state);
  176. Xen9pfsRing *ring = &xen_9pfs->rings[pdu->tag % xen_9pfs->num_rings];
  177. int num;
  178. size_t buf_size;
  179. g_free(ring->sg);
  180. ring->sg = g_new0(struct iovec, 2);
  181. ring->co = qemu_coroutine_self();
  182. /* make sure other threads see ring->co changes before continuing */
  183. smp_wmb();
  184. again:
  185. xen_9pfs_in_sg(ring, ring->sg, &num, pdu->idx, size);
  186. buf_size = iov_size(ring->sg, num);
  187. if (buf_size < size) {
  188. qemu_coroutine_yield();
  189. goto again;
  190. }
  191. ring->co = NULL;
  192. /* make sure other threads see ring->co changes before continuing */
  193. smp_wmb();
  194. *piov = ring->sg;
  195. *pniov = num;
  196. }
  197. static void xen_9pfs_push_and_notify(V9fsPDU *pdu)
  198. {
  199. RING_IDX prod;
  200. Xen9pfsDev *priv = container_of(pdu->s, Xen9pfsDev, state);
  201. Xen9pfsRing *ring = &priv->rings[pdu->tag % priv->num_rings];
  202. g_free(ring->sg);
  203. ring->sg = NULL;
  204. ring->intf->out_cons = ring->out_cons;
  205. xen_wmb();
  206. prod = ring->intf->in_prod;
  207. xen_rmb();
  208. ring->intf->in_prod = prod + pdu->size;
  209. xen_wmb();
  210. ring->inprogress = false;
  211. qemu_xen_evtchn_notify(ring->evtchndev, ring->local_port);
  212. qemu_bh_schedule(ring->bh);
  213. }
  214. static const V9fsTransport xen_9p_transport = {
  215. .pdu_vmarshal = xen_9pfs_pdu_vmarshal,
  216. .pdu_vunmarshal = xen_9pfs_pdu_vunmarshal,
  217. .init_in_iov_from_pdu = xen_9pfs_init_in_iov_from_pdu,
  218. .init_out_iov_from_pdu = xen_9pfs_init_out_iov_from_pdu,
  219. .push_and_notify = xen_9pfs_push_and_notify,
  220. };
  221. static int xen_9pfs_init(struct XenLegacyDevice *xendev)
  222. {
  223. return 0;
  224. }
  225. static int xen_9pfs_receive(Xen9pfsRing *ring)
  226. {
  227. P9MsgHeader h;
  228. RING_IDX cons, prod, masked_prod, masked_cons, queued;
  229. V9fsPDU *pdu;
  230. if (ring->inprogress) {
  231. return 0;
  232. }
  233. cons = ring->intf->out_cons;
  234. prod = ring->intf->out_prod;
  235. xen_rmb();
  236. queued = xen_9pfs_queued(prod, cons, XEN_FLEX_RING_SIZE(ring->ring_order));
  237. if (queued < sizeof(h)) {
  238. return 0;
  239. }
  240. ring->inprogress = true;
  241. masked_prod = xen_9pfs_mask(prod, XEN_FLEX_RING_SIZE(ring->ring_order));
  242. masked_cons = xen_9pfs_mask(cons, XEN_FLEX_RING_SIZE(ring->ring_order));
  243. xen_9pfs_read_packet((uint8_t *) &h, ring->ring.out, sizeof(h),
  244. masked_prod, &masked_cons,
  245. XEN_FLEX_RING_SIZE(ring->ring_order));
  246. if (queued < le32_to_cpu(h.size_le)) {
  247. return 0;
  248. }
  249. /* cannot fail, because we only handle one request per ring at a time */
  250. pdu = pdu_alloc(&ring->priv->state);
  251. ring->out_size = le32_to_cpu(h.size_le);
  252. ring->out_cons = cons + le32_to_cpu(h.size_le);
  253. pdu_submit(pdu, &h);
  254. return 0;
  255. }
  256. static void xen_9pfs_bh(void *opaque)
  257. {
  258. Xen9pfsRing *ring = opaque;
  259. bool wait;
  260. again:
  261. wait = ring->co != NULL && qemu_coroutine_entered(ring->co);
  262. /* paired with the smb_wmb barriers in xen_9pfs_init_in_iov_from_pdu */
  263. smp_rmb();
  264. if (wait) {
  265. cpu_relax();
  266. goto again;
  267. }
  268. if (ring->co != NULL) {
  269. qemu_coroutine_enter_if_inactive(ring->co);
  270. }
  271. xen_9pfs_receive(ring);
  272. }
  273. static void xen_9pfs_evtchn_event(void *opaque)
  274. {
  275. Xen9pfsRing *ring = opaque;
  276. evtchn_port_t port;
  277. port = qemu_xen_evtchn_pending(ring->evtchndev);
  278. qemu_xen_evtchn_unmask(ring->evtchndev, port);
  279. qemu_bh_schedule(ring->bh);
  280. }
  281. static void xen_9pfs_disconnect(struct XenLegacyDevice *xendev)
  282. {
  283. Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
  284. int i;
  285. trace_xen_9pfs_disconnect(xendev->name);
  286. for (i = 0; i < xen_9pdev->num_rings; i++) {
  287. if (xen_9pdev->rings[i].evtchndev != NULL) {
  288. qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
  289. NULL, NULL, NULL);
  290. qemu_xen_evtchn_unbind(xen_9pdev->rings[i].evtchndev,
  291. xen_9pdev->rings[i].local_port);
  292. xen_9pdev->rings[i].evtchndev = NULL;
  293. }
  294. if (xen_9pdev->rings[i].data != NULL) {
  295. xen_be_unmap_grant_refs(&xen_9pdev->xendev,
  296. xen_9pdev->rings[i].data,
  297. xen_9pdev->rings[i].intf->ref,
  298. (1 << xen_9pdev->rings[i].ring_order));
  299. xen_9pdev->rings[i].data = NULL;
  300. }
  301. if (xen_9pdev->rings[i].intf != NULL) {
  302. xen_be_unmap_grant_ref(&xen_9pdev->xendev,
  303. xen_9pdev->rings[i].intf,
  304. xen_9pdev->rings[i].ref);
  305. xen_9pdev->rings[i].intf = NULL;
  306. }
  307. if (xen_9pdev->rings[i].bh != NULL) {
  308. qemu_bh_delete(xen_9pdev->rings[i].bh);
  309. xen_9pdev->rings[i].bh = NULL;
  310. }
  311. }
  312. g_free(xen_9pdev->id);
  313. xen_9pdev->id = NULL;
  314. g_free(xen_9pdev->tag);
  315. xen_9pdev->tag = NULL;
  316. g_free(xen_9pdev->path);
  317. xen_9pdev->path = NULL;
  318. g_free(xen_9pdev->security_model);
  319. xen_9pdev->security_model = NULL;
  320. g_free(xen_9pdev->rings);
  321. xen_9pdev->rings = NULL;
  322. }
  323. static int xen_9pfs_free(struct XenLegacyDevice *xendev)
  324. {
  325. trace_xen_9pfs_free(xendev->name);
  326. return 0;
  327. }
  328. static int xen_9pfs_connect(struct XenLegacyDevice *xendev)
  329. {
  330. Error *err = NULL;
  331. int i;
  332. Xen9pfsDev *xen_9pdev = container_of(xendev, Xen9pfsDev, xendev);
  333. V9fsState *s = &xen_9pdev->state;
  334. QemuOpts *fsdev;
  335. trace_xen_9pfs_connect(xendev->name);
  336. if (xenstore_read_fe_int(&xen_9pdev->xendev, "num-rings",
  337. &xen_9pdev->num_rings) == -1 ||
  338. xen_9pdev->num_rings > MAX_RINGS || xen_9pdev->num_rings < 1) {
  339. return -1;
  340. }
  341. xen_9pdev->rings = g_new0(Xen9pfsRing, xen_9pdev->num_rings);
  342. for (i = 0; i < xen_9pdev->num_rings; i++) {
  343. char *str;
  344. int ring_order;
  345. xen_9pdev->rings[i].priv = xen_9pdev;
  346. xen_9pdev->rings[i].evtchn = -1;
  347. xen_9pdev->rings[i].local_port = -1;
  348. str = g_strdup_printf("ring-ref%u", i);
  349. if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
  350. &xen_9pdev->rings[i].ref) == -1) {
  351. g_free(str);
  352. goto out;
  353. }
  354. g_free(str);
  355. str = g_strdup_printf("event-channel-%u", i);
  356. if (xenstore_read_fe_int(&xen_9pdev->xendev, str,
  357. &xen_9pdev->rings[i].evtchn) == -1) {
  358. g_free(str);
  359. goto out;
  360. }
  361. g_free(str);
  362. xen_9pdev->rings[i].intf =
  363. xen_be_map_grant_ref(&xen_9pdev->xendev,
  364. xen_9pdev->rings[i].ref,
  365. PROT_READ | PROT_WRITE);
  366. if (!xen_9pdev->rings[i].intf) {
  367. goto out;
  368. }
  369. ring_order = xen_9pdev->rings[i].intf->ring_order;
  370. if (ring_order > MAX_RING_ORDER) {
  371. goto out;
  372. }
  373. xen_9pdev->rings[i].ring_order = ring_order;
  374. xen_9pdev->rings[i].data =
  375. xen_be_map_grant_refs(&xen_9pdev->xendev,
  376. xen_9pdev->rings[i].intf->ref,
  377. (1 << ring_order),
  378. PROT_READ | PROT_WRITE);
  379. if (!xen_9pdev->rings[i].data) {
  380. goto out;
  381. }
  382. xen_9pdev->rings[i].ring.in = xen_9pdev->rings[i].data;
  383. xen_9pdev->rings[i].ring.out = xen_9pdev->rings[i].data +
  384. XEN_FLEX_RING_SIZE(ring_order);
  385. xen_9pdev->rings[i].bh = qemu_bh_new_guarded(xen_9pfs_bh,
  386. &xen_9pdev->rings[i],
  387. &xen_9pdev->mem_reentrancy_guard);
  388. xen_9pdev->rings[i].out_cons = 0;
  389. xen_9pdev->rings[i].out_size = 0;
  390. xen_9pdev->rings[i].inprogress = false;
  391. xen_9pdev->rings[i].evtchndev = qemu_xen_evtchn_open();
  392. if (xen_9pdev->rings[i].evtchndev == NULL) {
  393. goto out;
  394. }
  395. qemu_set_cloexec(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev));
  396. xen_9pdev->rings[i].local_port = qemu_xen_evtchn_bind_interdomain
  397. (xen_9pdev->rings[i].evtchndev,
  398. xendev->dom,
  399. xen_9pdev->rings[i].evtchn);
  400. if (xen_9pdev->rings[i].local_port == -1) {
  401. xen_pv_printf(xendev, 0,
  402. "xenevtchn_bind_interdomain failed port=%d\n",
  403. xen_9pdev->rings[i].evtchn);
  404. goto out;
  405. }
  406. xen_pv_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
  407. qemu_set_fd_handler(qemu_xen_evtchn_fd(xen_9pdev->rings[i].evtchndev),
  408. xen_9pfs_evtchn_event, NULL, &xen_9pdev->rings[i]);
  409. }
  410. xen_9pdev->security_model = xenstore_read_be_str(xendev, "security_model");
  411. xen_9pdev->path = xenstore_read_be_str(xendev, "path");
  412. xen_9pdev->id = s->fsconf.fsdev_id =
  413. g_strdup_printf("xen9p%d", xendev->dev);
  414. xen_9pdev->tag = s->fsconf.tag = xenstore_read_fe_str(xendev, "tag");
  415. fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
  416. s->fsconf.tag,
  417. 1, NULL);
  418. qemu_opt_set(fsdev, "fsdriver", "local", NULL);
  419. qemu_opt_set(fsdev, "path", xen_9pdev->path, NULL);
  420. qemu_opt_set(fsdev, "security_model", xen_9pdev->security_model, NULL);
  421. qemu_opts_set_id(fsdev, s->fsconf.fsdev_id);
  422. qemu_fsdev_add(fsdev, &err);
  423. if (err) {
  424. error_report_err(err);
  425. }
  426. v9fs_device_realize_common(s, &xen_9p_transport, NULL);
  427. return 0;
  428. out:
  429. xen_9pfs_free(xendev);
  430. return -1;
  431. }
  432. static void xen_9pfs_alloc(struct XenLegacyDevice *xendev)
  433. {
  434. trace_xen_9pfs_alloc(xendev->name);
  435. xenstore_write_be_str(xendev, "versions", VERSIONS);
  436. xenstore_write_be_int(xendev, "max-rings", MAX_RINGS);
  437. xenstore_write_be_int(xendev, "max-ring-page-order", MAX_RING_ORDER);
  438. }
  439. static const struct XenDevOps xen_9pfs_ops = {
  440. .size = sizeof(Xen9pfsDev),
  441. .flags = DEVOPS_FLAG_NEED_GNTDEV,
  442. .alloc = xen_9pfs_alloc,
  443. .init = xen_9pfs_init,
  444. .initialise = xen_9pfs_connect,
  445. .disconnect = xen_9pfs_disconnect,
  446. .free = xen_9pfs_free,
  447. };
  448. static void xen_9pfs_register_backend(void)
  449. {
  450. xen_be_register("9pfs", &xen_9pfs_ops);
  451. }
  452. xen_backend_init(xen_9pfs_register_backend);