| 1 | /* |
| 2 | * Transfer data over the net. Pretty basic setup, will only support |
| 3 | * 1 file per thread/job. |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
| 10 | #include <netinet/in.h> |
| 11 | #include <arpa/inet.h> |
| 12 | #include <netdb.h> |
| 13 | |
| 14 | #include "../fio.h" |
| 15 | #include "../os.h" |
| 16 | |
| 17 | struct net_data { |
| 18 | int send_to_net; |
| 19 | struct io_u *last_io_u; |
| 20 | }; |
| 21 | |
| 22 | static int fio_netio_getevents(struct thread_data *td, int fio_unused min, |
| 23 | int max, struct timespec fio_unused *t) |
| 24 | { |
| 25 | assert(max <= 1); |
| 26 | |
| 27 | /* |
| 28 | * we can only have one finished io_u for sync io, since the depth |
| 29 | * is always 1 |
| 30 | */ |
| 31 | if (list_empty(&td->io_u_busylist)) |
| 32 | return 0; |
| 33 | |
| 34 | return 1; |
| 35 | } |
| 36 | |
| 37 | static struct io_u *fio_netio_event(struct thread_data *td, int event) |
| 38 | { |
| 39 | struct net_data *nd = td->io_ops->data; |
| 40 | |
| 41 | assert(event == 0); |
| 42 | |
| 43 | return nd->last_io_u; |
| 44 | } |
| 45 | |
| 46 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
| 47 | { |
| 48 | struct net_data *nd = td->io_ops->data; |
| 49 | struct fio_file *f = io_u->file; |
| 50 | |
| 51 | if (nd->send_to_net) { |
| 52 | if (io_u->ddir == DDIR_READ) { |
| 53 | td_verror(td, EINVAL); |
| 54 | return 1; |
| 55 | } |
| 56 | } else { |
| 57 | if (io_u->ddir == DDIR_WRITE) { |
| 58 | td_verror(td, EINVAL); |
| 59 | return 1; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (io_u->ddir == DDIR_SYNC) |
| 64 | return 0; |
| 65 | if (io_u->offset == f->last_completed_pos) |
| 66 | return 0; |
| 67 | |
| 68 | /* |
| 69 | * If offset is different from last end position, it's a seek. |
| 70 | * As network io is purely sequential, we don't allow seeks. |
| 71 | */ |
| 72 | td_verror(td, EINVAL); |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 77 | { |
| 78 | struct net_data *nd = td->io_ops->data; |
| 79 | struct fio_file *f = io_u->file; |
| 80 | unsigned int ret = 0; |
| 81 | |
| 82 | if (io_u->ddir == DDIR_WRITE) |
| 83 | ret = write(f->fd, io_u->buf, io_u->buflen); |
| 84 | else if (io_u->ddir == DDIR_READ) |
| 85 | ret = read(f->fd, io_u->buf, io_u->buflen); |
| 86 | |
| 87 | if (ret != io_u->buflen) { |
| 88 | if (ret > 0) { |
| 89 | io_u->resid = io_u->buflen - ret; |
| 90 | io_u->error = EIO; |
| 91 | } else |
| 92 | io_u->error = errno; |
| 93 | } |
| 94 | |
| 95 | if (!io_u->error) |
| 96 | nd->last_io_u = io_u; |
| 97 | |
| 98 | return io_u->error; |
| 99 | } |
| 100 | |
| 101 | static int fio_netio_setup_connect(struct thread_data *td, const char *host, |
| 102 | unsigned short port) |
| 103 | { |
| 104 | struct sockaddr_in addr; |
| 105 | struct fio_file *f; |
| 106 | int i; |
| 107 | |
| 108 | memset(&addr, 0, sizeof(addr)); |
| 109 | addr.sin_family = AF_INET; |
| 110 | addr.sin_port = htons(port); |
| 111 | |
| 112 | if (inet_aton(host, &addr.sin_addr) != 1) { |
| 113 | struct hostent *hent = gethostbyname(host); |
| 114 | |
| 115 | if (!hent) { |
| 116 | td_vmsg(td, errno, "gethostbyname"); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | memcpy(&addr.sin_addr, hent->h_addr, 4); |
| 121 | } |
| 122 | |
| 123 | for_each_file(td, f, i) { |
| 124 | f->fd = socket(AF_INET, SOCK_STREAM, 0); |
| 125 | if (f->fd < 0) { |
| 126 | td_vmsg(td, errno, "socket"); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 131 | td_vmsg(td, errno, "connect"); |
| 132 | return 1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | |
| 138 | } |
| 139 | |
| 140 | static int fio_netio_setup_listen(struct thread_data *td, unsigned short port) |
| 141 | { |
| 142 | struct sockaddr_in addr; |
| 143 | socklen_t socklen; |
| 144 | struct fio_file *f; |
| 145 | int fd, opt, i; |
| 146 | |
| 147 | fd = socket(AF_INET, SOCK_STREAM, 0); |
| 148 | if (fd < 0) { |
| 149 | td_vmsg(td, errno, "socket"); |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | opt = 1; |
| 154 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 155 | td_vmsg(td, errno, "setsockopt"); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | memset(&addr, 0, sizeof(addr)); |
| 160 | addr.sin_family = AF_INET; |
| 161 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 162 | addr.sin_port = htons(port); |
| 163 | |
| 164 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 165 | td_vmsg(td, errno, "bind"); |
| 166 | return 1; |
| 167 | } |
| 168 | if (listen(fd, 1) < 0) { |
| 169 | td_vmsg(td, errno, "listen"); |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files); |
| 174 | |
| 175 | socklen = sizeof(addr); |
| 176 | for_each_file(td, f, i) { |
| 177 | f->fd = accept(fd, (struct sockaddr *) &addr, &socklen); |
| 178 | if (f->fd < 0) { |
| 179 | td_vmsg(td, errno, "accept"); |
| 180 | return 1; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | static int fio_netio_setup(struct thread_data *td) |
| 188 | { |
| 189 | char host[64], buf[128]; |
| 190 | struct net_data *nd; |
| 191 | unsigned short port; |
| 192 | struct fio_file *f; |
| 193 | char *sep; |
| 194 | int ret, i; |
| 195 | |
| 196 | /* |
| 197 | * work around for late init call |
| 198 | */ |
| 199 | if (td->io_ops->init(td)) |
| 200 | return 1; |
| 201 | |
| 202 | nd = td->io_ops->data; |
| 203 | |
| 204 | if (td->iomix) { |
| 205 | log_err("fio: network connections must be read OR write\n"); |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | strcpy(buf, td->filename); |
| 210 | |
| 211 | sep = strchr(buf, ':'); |
| 212 | if (!sep) { |
| 213 | log_err("fio: bad network host:port <<%s>>\n", td->filename); |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | *sep = '\0'; |
| 218 | sep++; |
| 219 | strcpy(host, buf); |
| 220 | port = atoi(sep); |
| 221 | |
| 222 | if (td->ddir == READ) { |
| 223 | nd->send_to_net = 0; |
| 224 | ret = fio_netio_setup_listen(td, port); |
| 225 | } else { |
| 226 | nd->send_to_net = 1; |
| 227 | ret = fio_netio_setup_connect(td, host, port); |
| 228 | } |
| 229 | |
| 230 | if (ret) |
| 231 | return ret; |
| 232 | |
| 233 | td->io_size = td->total_file_size; |
| 234 | td->total_io_size = td->io_size; |
| 235 | |
| 236 | for_each_file(td, f, i) { |
| 237 | f->file_size = td->total_file_size / td->nr_files; |
| 238 | f->real_file_size = f->file_size; |
| 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static void fio_netio_cleanup(struct thread_data *td) |
| 245 | { |
| 246 | if (td->io_ops->data) { |
| 247 | free(td->io_ops->data); |
| 248 | td->io_ops->data = NULL; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static int fio_netio_init(struct thread_data *td) |
| 253 | { |
| 254 | struct net_data *nd; |
| 255 | |
| 256 | /* |
| 257 | * Hack to work-around the ->setup() function calling init on its |
| 258 | * own, since it needs ->io_ops->data to be set up. |
| 259 | */ |
| 260 | if (td->io_ops->data) |
| 261 | return 0; |
| 262 | |
| 263 | nd = malloc(sizeof(*nd)); |
| 264 | nd->last_io_u = NULL; |
| 265 | td->io_ops->data = nd; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static struct ioengine_ops ioengine = { |
| 270 | .name = "net", |
| 271 | .version = FIO_IOOPS_VERSION, |
| 272 | .init = fio_netio_init, |
| 273 | .prep = fio_netio_prep, |
| 274 | .queue = fio_netio_queue, |
| 275 | .getevents = fio_netio_getevents, |
| 276 | .event = fio_netio_event, |
| 277 | .cleanup = fio_netio_cleanup, |
| 278 | .setup = fio_netio_setup, |
| 279 | .flags = FIO_SYNCIO | FIO_NETIO, |
| 280 | }; |
| 281 | |
| 282 | static void fio_init fio_netio_register(void) |
| 283 | { |
| 284 | register_ioengine(&ioengine); |
| 285 | } |
| 286 | |
| 287 | static void fio_exit fio_netio_unregister(void) |
| 288 | { |
| 289 | unregister_ioengine(&ioengine); |
| 290 | } |