]> git.proxmox.com Git - qemu.git/blame - qemu-coroutine-io.c
smc91c111: Fix receive starvation
[qemu.git] / qemu-coroutine-io.c
CommitLineData
8c5135f9
PB
1/*
2 * Coroutine-aware I/O functions
3 *
4 * Copyright (C) 2009-2010 Nippon Telegraph and Telephone Corporation.
5 * Copyright (c) 2011, Red Hat, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25#include "qemu-common.h"
1de7afc9 26#include "qemu/sockets.h"
737e150e 27#include "block/coroutine.h"
1de7afc9 28#include "qemu/iov.h"
6a1751b7 29#include "qemu/main-loop.h"
8c5135f9 30
2fc8ae1d
MT
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)
8c5135f9 34{
2fc8ae1d
MT
35 size_t done = 0;
36 ssize_t ret;
37 while (done < bytes) {
25e5e4c7 38 ret = iov_send_recv(sockfd, iov, iov_cnt,
2fc8ae1d
MT
39 offset + done, bytes - done, do_send);
40 if (ret > 0) {
41 done += ret;
42 } else if (ret < 0) {
8c5135f9
PB
43 if (errno == EAGAIN) {
44 qemu_coroutine_yield();
2fc8ae1d
MT
45 } else if (done == 0) {
46 return -1;
47 } else {
48 break;
8c5135f9 49 }
2fc8ae1d
MT
50 } else if (ret == 0 && !do_send) {
51 /* write (send) should never return 0.
52 * read (recv) returns 0 for end-of-file (-data).
53 * In both cases there's little point retrying,
54 * but we do for write anyway, just in case */
8c5135f9
PB
55 break;
56 }
8c5135f9 57 }
2fc8ae1d 58 return done;
8c5135f9
PB
59}
60
2fc8ae1d
MT
61ssize_t coroutine_fn
62qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
8c5135f9 63{
2fc8ae1d
MT
64 struct iovec iov = { .iov_base = buf, .iov_len = bytes };
65 return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
8c5135f9 66}
9f05d0c3
MH
67
68typedef struct {
69 Coroutine *co;
70 int fd;
71} FDYieldUntilData;
72
73static void fd_coroutine_enter(void *opaque)
74{
75 FDYieldUntilData *data = opaque;
76 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
77 qemu_coroutine_enter(data->co, NULL);
78}
79
80void coroutine_fn yield_until_fd_readable(int fd)
81{
82 FDYieldUntilData data;
83
84 assert(qemu_in_coroutine());
85 data.co = qemu_coroutine_self();
86 data.fd = fd;
87 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
88 qemu_coroutine_yield();
89}