1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "qemu/sockets.h"
27#include "qemu/coroutine.h"
28#include "qemu/iov.h"
29#include "qemu/main-loop.h"
30
31ssize_t coroutine_fn
32qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
33 size_t offset, size_t bytes, bool do_send)
34{
35 size_t done = 0;
36 ssize_t ret;
37 while (done < bytes) {
38 ret = iov_send_recv(sockfd, iov, iov_cnt,
39 offset + done, bytes - done, do_send);
40 if (ret > 0) {
41 done += ret;
42 } else if (ret < 0) {
43 if (errno == EAGAIN || errno == EWOULDBLOCK) {
44 qemu_coroutine_yield();
45 } else if (done == 0) {
46 return -errno;
47 } else {
48 break;
49 }
50 } else if (ret == 0 && !do_send) {
51
52
53
54
55 break;
56 }
57 }
58 return done;
59}
60
61ssize_t coroutine_fn
62qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
63{
64 struct iovec iov = { .iov_base = buf, .iov_len = bytes };
65 return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
66}
67
68typedef struct {
69 AioContext *ctx;
70 Coroutine *co;
71 int fd;
72} FDYieldUntilData;
73
74static void fd_coroutine_enter(void *opaque)
75{
76 FDYieldUntilData *data = opaque;
77 aio_set_fd_handler(data->ctx, data->fd, false,
78 NULL, NULL, NULL, NULL, NULL);
79 qemu_coroutine_enter(data->co);
80}
81
82void coroutine_fn yield_until_fd_readable(int fd)
83{
84 FDYieldUntilData data;
85
86 assert(qemu_in_coroutine());
87 data.ctx = qemu_get_current_aio_context();
88 data.co = qemu_coroutine_self();
89 data.fd = fd;
90 aio_set_fd_handler(
91 data.ctx, fd, false, fd_coroutine_enter, NULL, NULL, NULL, &data);
92 qemu_coroutine_yield();
93}
94