blob: d791932d63b98bc38f2d8931c7aa83e474f85aa6 [file] [log] [blame]
Paolo Bonzini8c5135f2011-09-08 13:46:25 +02001/*
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 */
Peter Maydellaafd7582016-01-29 17:49:55 +000025#include "qemu/osdep.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/sockets.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010027#include "qemu/coroutine.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010029#include "qemu/main-loop.h"
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020030
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040031ssize_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)
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020034{
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040035 size_t done = 0;
36 ssize_t ret;
37 while (done < bytes) {
Michael Tokarev25e5e4c2012-03-14 11:18:54 +040038 ret = iov_send_recv(sockfd, iov, iov_cnt,
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040039 offset + done, bytes - done, do_send);
40 if (ret > 0) {
41 done += ret;
42 } else if (ret < 0) {
Daniel P. Berrangeb16a44e2016-03-07 20:36:03 +000043 if (errno == EAGAIN || errno == EWOULDBLOCK) {
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020044 qemu_coroutine_yield();
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040045 } else if (done == 0) {
Daniel P. Berrangeb16a44e2016-03-07 20:36:03 +000046 return -errno;
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040047 } else {
48 break;
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020049 }
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040050 } 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 */
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020055 break;
56 }
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020057 }
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040058 return done;
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020059}
60
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040061ssize_t coroutine_fn
62qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020063{
Michael Tokarev2fc8ae12012-06-07 20:22:46 +040064 struct iovec iov = { .iov_base = buf, .iov_len = bytes };
65 return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
Paolo Bonzini8c5135f2011-09-08 13:46:25 +020066}
Michael R. Hines9f05d0c2013-06-25 21:35:29 -040067
68typedef struct {
Dietmar Maurerd154ef32019-10-24 06:56:10 +020069 AioContext *ctx;
Michael R. Hines9f05d0c2013-06-25 21:35:29 -040070 Coroutine *co;
71 int fd;
72} FDYieldUntilData;
73
74static void fd_coroutine_enter(void *opaque)
75{
76 FDYieldUntilData *data = opaque;
Stefan Hajnoczi826cc322021-12-07 13:23:31 +000077 aio_set_fd_handler(data->ctx, data->fd, false,
78 NULL, NULL, NULL, NULL, NULL);
Paolo Bonzini0b8b8752016-07-04 19:10:01 +020079 qemu_coroutine_enter(data->co);
Michael R. Hines9f05d0c2013-06-25 21:35:29 -040080}
81
82void coroutine_fn yield_until_fd_readable(int fd)
83{
84 FDYieldUntilData data;
85
86 assert(qemu_in_coroutine());
Dietmar Maurerd154ef32019-10-24 06:56:10 +020087 data.ctx = qemu_get_current_aio_context();
Michael R. Hines9f05d0c2013-06-25 21:35:29 -040088 data.co = qemu_coroutine_self();
89 data.fd = fd;
Dietmar Maurerd154ef32019-10-24 06:56:10 +020090 aio_set_fd_handler(
Stefan Hajnoczi826cc322021-12-07 13:23:31 +000091 data.ctx, fd, false, fd_coroutine_enter, NULL, NULL, NULL, &data);
Michael R. Hines9f05d0c2013-06-25 21:35:29 -040092 qemu_coroutine_yield();
93}