1 /* 2 * QEMU I/O channel test helpers 3 * 4 * Copyright (c) 2015 Red Hat, Inc. 5 * 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. 10 * 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. 15 * 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/>. 18 * 19 */ 20 21 #include "qemu/osdep.h" 22 #include "io-channel-helpers.h" 23 #include "qapi/error.h" 24 #include "qemu/iov.h" 25 26 struct QIOChannelTest { 27 QIOChannel *src; 28 QIOChannel *dst; 29 bool blocking; 30 size_t len; 31 size_t niov; 32 char *input; 33 struct iovec *inputv; 34 char *output; 35 struct iovec *outputv; 36 Error *writeerr; 37 Error *readerr; 38 }; 39 40 41 /* This thread sends all data using iovecs */ 42 static gpointer test_io_thread_writer(gpointer opaque) 43 { 44 QIOChannelTest *data = opaque; 45 46 qio_channel_set_blocking(data->src, data->blocking, NULL); 47 48 qio_channel_writev_all(data->src, 49 data->inputv, 50 data->niov, 51 &data->writeerr); 52 53 return NULL; 54 } 55 56 57 /* This thread receives all data using iovecs */ 58 static gpointer test_io_thread_reader(gpointer opaque) 59 { 60 QIOChannelTest *data = opaque; 61 62 qio_channel_set_blocking(data->dst, data->blocking, NULL); 63 64 qio_channel_readv_all(data->dst, 65 data->outputv, 66 data->niov, 67 &data->readerr); 68 69 return NULL; 70 } 71 72 73 QIOChannelTest *qio_channel_test_new(void) 74 { 75 QIOChannelTest *data = g_new0(QIOChannelTest, 1); 76 size_t i; 77 size_t offset; 78 79 80 /* We'll send 1 MB of data */ 81 #define CHUNK_COUNT 250 82 #define CHUNK_LEN 4194 83 84 data->len = CHUNK_COUNT * CHUNK_LEN; 85 data->input = g_new0(char, data->len); 86 data->output = g_new0(gchar, data->len); 87 88 /* Fill input with a pattern */ 89 for (i = 0; i < data->len; i += CHUNK_LEN) { 90 memset(data->input + i, (i / CHUNK_LEN), CHUNK_LEN); 91 } 92 93 /* We'll split the data across a bunch of IO vecs */ 94 data->niov = CHUNK_COUNT; 95 data->inputv = g_new0(struct iovec, data->niov); 96 data->outputv = g_new0(struct iovec, data->niov); 97 98 for (i = 0, offset = 0; i < data->niov; i++, offset += CHUNK_LEN) { 99 data->inputv[i].iov_base = data->input + offset; 100 data->outputv[i].iov_base = data->output + offset; 101 data->inputv[i].iov_len = CHUNK_LEN; 102 data->outputv[i].iov_len = CHUNK_LEN; 103 } 104 105 return data; 106 } 107 108 void qio_channel_test_run_threads(QIOChannelTest *test, 109 bool blocking, 110 QIOChannel *src, 111 QIOChannel *dst) 112 { 113 GThread *reader, *writer; 114 115 test->src = src; 116 test->dst = dst; 117 test->blocking = blocking; 118 119 reader = g_thread_new("reader", 120 test_io_thread_reader, 121 test); 122 writer = g_thread_new("writer", 123 test_io_thread_writer, 124 test); 125 126 g_thread_join(reader); 127 g_thread_join(writer); 128 129 test->dst = test->src = NULL; 130 } 131 132 133 void qio_channel_test_run_writer(QIOChannelTest *test, 134 QIOChannel *src) 135 { 136 test->src = src; 137 test_io_thread_writer(test); 138 test->src = NULL; 139 } 140 141 142 void qio_channel_test_run_reader(QIOChannelTest *test, 143 QIOChannel *dst) 144 { 145 test->dst = dst; 146 test_io_thread_reader(test); 147 test->dst = NULL; 148 } 149 150 151 void qio_channel_test_validate(QIOChannelTest *test) 152 { 153 g_assert(test->readerr == NULL); 154 g_assert(test->writeerr == NULL); 155 g_assert_cmpint(memcmp(test->input, 156 test->output, 157 test->len), ==, 0); 158 159 g_free(test->inputv); 160 g_free(test->outputv); 161 g_free(test->input); 162 g_free(test->output); 163 g_free(test); 164 } 165