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-common.h"
26#include "qemu/sockets.h"
27#include "block/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 int err;
38 while (done < bytes) {
39 ret = iov_send_recv(sockfd, iov, iov_cnt,
40 offset + done, bytes - done, do_send);
41 if (ret > 0) {
42 done += ret;
43 } else if (ret < 0) {
44 err = socket_error();
45 if (err == EAGAIN || err == EWOULDBLOCK) {
46 qemu_coroutine_yield();
47 } else if (done == 0) {
48 return -1;
49 } else {
50 break;
51 }
52 } else if (ret == 0 && !do_send) {
53
54
55
56
57 break;
58 }
59 }
60 return done;
61}
62
63ssize_t coroutine_fn
64qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
65{
66 struct iovec iov = { .iov_base = buf, .iov_len = bytes };
67 return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
68}
69
70typedef struct {
71 Coroutine *co;
72 int fd;
73} FDYieldUntilData;
74
75static void fd_coroutine_enter(void *opaque)
76{
77 FDYieldUntilData *data = opaque;
78 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
79 qemu_coroutine_enter(data->co, NULL);
80}
81
82void coroutine_fn yield_until_fd_readable(int fd)
83{
84 FDYieldUntilData data;
85
86 assert(qemu_in_coroutine());
87 data.co = qemu_coroutine_self();
88 data.fd = fd;
89 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
90 qemu_coroutine_yield();
91}
92