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