2 * QEMU I/O channel test helpers
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io-channel-helpers.h"
23 #include "qapi/error.h"
25 struct QIOChannelTest
{
34 struct iovec
*outputv
;
40 static void test_skip_iovec(struct iovec
**iov
,
48 for (i
= 0; i
< *niov
; i
++) {
49 if (skip
< (*iov
)[i
].iov_len
) {
50 old
->iov_len
= (*iov
)[i
].iov_len
;
51 old
->iov_base
= (*iov
)[i
].iov_base
;
53 (*iov
)[i
].iov_len
-= skip
;
54 (*iov
)[i
].iov_base
+= skip
;
57 skip
-= (*iov
)[i
].iov_len
;
59 if (i
== 0 && old
->iov_base
) {
60 (*iov
)[i
].iov_len
= old
->iov_len
;
61 (*iov
)[i
].iov_base
= old
->iov_base
;
75 /* This thread sends all data using iovecs */
76 static gpointer
test_io_thread_writer(gpointer opaque
)
78 QIOChannelTest
*data
= opaque
;
79 struct iovec
*iov
= data
->inputv
;
80 size_t niov
= data
->niov
;
81 struct iovec old
= { 0 };
83 qio_channel_set_blocking(data
->src
, data
->blocking
, NULL
);
87 ret
= qio_channel_writev(data
->src
,
91 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
93 error_setg(&data
->writeerr
,
94 "Unexpected I/O blocking");
97 qio_channel_wait(data
->src
,
101 } else if (ret
< 0) {
103 } else if (ret
== 0) {
104 error_setg(&data
->writeerr
,
105 "Unexpected zero length write");
109 test_skip_iovec(&iov
, &niov
, ret
, &old
);
116 /* This thread receives all data using iovecs */
117 static gpointer
test_io_thread_reader(gpointer opaque
)
119 QIOChannelTest
*data
= opaque
;
120 struct iovec
*iov
= data
->outputv
;
121 size_t niov
= data
->niov
;
122 struct iovec old
= { 0 };
124 qio_channel_set_blocking(data
->dst
, data
->blocking
, NULL
);
129 ret
= qio_channel_readv(data
->dst
,
134 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
135 if (data
->blocking
) {
136 error_setg(&data
->readerr
,
137 "Unexpected I/O blocking");
140 qio_channel_wait(data
->dst
,
144 } else if (ret
< 0) {
146 } else if (ret
== 0) {
150 test_skip_iovec(&iov
, &niov
, ret
, &old
);
157 QIOChannelTest
*qio_channel_test_new(void)
159 QIOChannelTest
*data
= g_new0(QIOChannelTest
, 1);
164 /* We'll send 1 MB of data */
165 #define CHUNK_COUNT 250
166 #define CHUNK_LEN 4194
168 data
->len
= CHUNK_COUNT
* CHUNK_LEN
;
169 data
->input
= g_new0(char, data
->len
);
170 data
->output
= g_new0(gchar
, data
->len
);
172 /* Fill input with a pattern */
173 for (i
= 0; i
< data
->len
; i
+= CHUNK_LEN
) {
174 memset(data
->input
+ i
, (i
/ CHUNK_LEN
), CHUNK_LEN
);
177 /* We'll split the data across a bunch of IO vecs */
178 data
->niov
= CHUNK_COUNT
;
179 data
->inputv
= g_new0(struct iovec
, data
->niov
);
180 data
->outputv
= g_new0(struct iovec
, data
->niov
);
182 for (i
= 0, offset
= 0; i
< data
->niov
; i
++, offset
+= CHUNK_LEN
) {
183 data
->inputv
[i
].iov_base
= data
->input
+ offset
;
184 data
->outputv
[i
].iov_base
= data
->output
+ offset
;
185 data
->inputv
[i
].iov_len
= CHUNK_LEN
;
186 data
->outputv
[i
].iov_len
= CHUNK_LEN
;
192 void qio_channel_test_run_threads(QIOChannelTest
*test
,
197 GThread
*reader
, *writer
;
201 test
->blocking
= blocking
;
203 reader
= g_thread_new("reader",
204 test_io_thread_reader
,
206 writer
= g_thread_new("writer",
207 test_io_thread_writer
,
210 g_thread_join(reader
);
211 g_thread_join(writer
);
213 test
->dst
= test
->src
= NULL
;
217 void qio_channel_test_run_writer(QIOChannelTest
*test
,
221 test_io_thread_writer(test
);
226 void qio_channel_test_run_reader(QIOChannelTest
*test
,
230 test_io_thread_reader(test
);
235 void qio_channel_test_validate(QIOChannelTest
*test
)
237 g_assert(test
->readerr
== NULL
);
238 g_assert(test
->writeerr
== NULL
);
239 g_assert_cmpint(memcmp(test
->input
,
243 g_free(test
->inputv
);
244 g_free(test
->outputv
);
246 g_free(test
->output
);