Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1 | /* |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 2 | * Transfer data over the net. |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <unistd.h> |
| 7 | #include <errno.h> |
| 8 | #include <assert.h> |
| 9 | #include <netinet/in.h> |
| 10 | #include <arpa/inet.h> |
| 11 | #include <netdb.h> |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame^] | 12 | #include <sys/poll.h> |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 51 | /* |
| 52 | * Make sure we don't see spurious reads to a receiver, and vice versa |
| 53 | */ |
| 54 | if ((nd->send_to_net && io_u->ddir == DDIR_READ) || |
| 55 | (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) { |
| 56 | printf("boo!\n"); |
| 57 | td_verror(td, EINVAL); |
| 58 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 59 | } |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 60 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 61 | if (io_u->ddir == DDIR_SYNC) |
| 62 | return 0; |
| 63 | if (io_u->offset == f->last_completed_pos) |
| 64 | return 0; |
| 65 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 66 | /* |
| 67 | * If offset is different from last end position, it's a seek. |
| 68 | * As network io is purely sequential, we don't allow seeks. |
| 69 | */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 70 | td_verror(td, EINVAL); |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 75 | { |
| 76 | struct net_data *nd = td->io_ops->data; |
| 77 | struct fio_file *f = io_u->file; |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 78 | int ret, flags = 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 79 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 80 | if (io_u->ddir == DDIR_WRITE) { |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 81 | /* |
| 82 | * if we are going to write more, set MSG_MORE |
| 83 | */ |
| 84 | if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < |
| 85 | td->io_size) |
| 86 | flags = MSG_MORE; |
| 87 | |
| 88 | ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags); |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 89 | } else if (io_u->ddir == DDIR_READ) { |
| 90 | flags = MSG_WAITALL; |
| 91 | ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags); |
| 92 | } else |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 93 | ret = 0; /* must be a SYNC */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 94 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 95 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 96 | if (ret > 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 97 | io_u->resid = io_u->xfer_buflen - ret; |
| 98 | io_u->error = 0; |
| 99 | return ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 100 | } else |
| 101 | io_u->error = errno; |
| 102 | } |
| 103 | |
| 104 | if (!io_u->error) |
| 105 | nd->last_io_u = io_u; |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 106 | else |
| 107 | td_verror(td, io_u->error); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 108 | |
| 109 | return io_u->error; |
| 110 | } |
| 111 | |
| 112 | static int fio_netio_setup_connect(struct thread_data *td, const char *host, |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 113 | unsigned short port) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 114 | { |
| 115 | struct sockaddr_in addr; |
| 116 | struct fio_file *f; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 117 | int i; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 118 | |
| 119 | memset(&addr, 0, sizeof(addr)); |
| 120 | addr.sin_family = AF_INET; |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 121 | addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 122 | |
| 123 | if (inet_aton(host, &addr.sin_addr) != 1) { |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 124 | struct hostent *hent; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 125 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 126 | hent = gethostbyname(host); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 127 | if (!hent) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 128 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | memcpy(&addr.sin_addr, hent->h_addr, 4); |
| 133 | } |
| 134 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 135 | for_each_file(td, f, i) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 136 | f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 137 | if (f->fd < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 138 | td_verror(td, errno); |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 139 | return 1; |
| 140 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 141 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 142 | if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 143 | td_verror(td, errno); |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 144 | return 1; |
| 145 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | |
| 150 | } |
| 151 | |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame^] | 152 | static int fio_netio_accept_connections(struct thread_data *td, int fd, |
| 153 | struct sockaddr_in *addr) |
| 154 | { |
| 155 | socklen_t socklen = sizeof(*addr); |
| 156 | unsigned int accepts = 0; |
| 157 | struct pollfd pfd; |
| 158 | |
| 159 | fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files); |
| 160 | |
| 161 | /* |
| 162 | * Accept loop. poll for incoming events, accept them. Repeat until we |
| 163 | * have all connections. |
| 164 | */ |
| 165 | while (!td->terminate && accepts < td->nr_files) { |
| 166 | struct fio_file *f; |
| 167 | int ret, i; |
| 168 | |
| 169 | pfd.fd = fd; |
| 170 | pfd.events = POLLIN; |
| 171 | |
| 172 | ret = poll(&pfd, 1, -1); |
| 173 | if (ret < 0) { |
| 174 | if (errno == EINTR) |
| 175 | continue; |
| 176 | |
| 177 | td_verror(td, errno); |
| 178 | break; |
| 179 | } else if (!ret) |
| 180 | continue; |
| 181 | |
| 182 | for_each_file(td, f, i) { |
| 183 | if (f->fd != -1) |
| 184 | continue; |
| 185 | |
| 186 | f->fd = accept(fd, (struct sockaddr *) addr, &socklen); |
| 187 | if (f->fd < 0) { |
| 188 | td_verror(td, errno); |
| 189 | return 1; |
| 190 | } |
| 191 | accepts++; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 199 | static int fio_netio_setup_listen(struct thread_data *td, unsigned short port) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 200 | { |
| 201 | struct sockaddr_in addr; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame^] | 202 | int fd, opt; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 203 | |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 204 | fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 205 | if (fd < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 206 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | opt = 1; |
| 211 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 212 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 213 | return 1; |
| 214 | } |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 215 | #ifdef SO_REUSEPORT |
| 216 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
| 217 | td_verror(td, errno); |
| 218 | return 1; |
| 219 | } |
| 220 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 221 | |
| 222 | memset(&addr, 0, sizeof(addr)); |
| 223 | addr.sin_family = AF_INET; |
| 224 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 225 | addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 226 | |
| 227 | if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 228 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 229 | return 1; |
| 230 | } |
| 231 | if (listen(fd, 1) < 0) { |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 232 | td_verror(td, errno); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 233 | return 1; |
| 234 | } |
| 235 | |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame^] | 236 | return fio_netio_accept_connections(td, fd, &addr); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static int fio_netio_setup(struct thread_data *td) |
| 240 | { |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 241 | char host[64], buf[128]; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 242 | struct net_data *nd; |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 243 | unsigned short port; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 244 | struct fio_file *f; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 245 | char *sep; |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 246 | int ret, i; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 247 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 248 | if (!td->total_file_size) { |
| 249 | log_err("fio: need size= set\n"); |
| 250 | return 1; |
| 251 | } |
| 252 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 253 | /* |
| 254 | * work around for late init call |
| 255 | */ |
| 256 | if (td->io_ops->init(td)) |
| 257 | return 1; |
| 258 | |
| 259 | nd = td->io_ops->data; |
| 260 | |
| 261 | if (td->iomix) { |
| 262 | log_err("fio: network connections must be read OR write\n"); |
| 263 | return 1; |
| 264 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 265 | |
| 266 | strcpy(buf, td->filename); |
| 267 | |
| 268 | sep = strchr(buf, ':'); |
| 269 | if (!sep) { |
| 270 | log_err("fio: bad network host:port <<%s>>\n", td->filename); |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | *sep = '\0'; |
| 275 | sep++; |
| 276 | strcpy(host, buf); |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 277 | port = atoi(sep); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 278 | |
Jens Axboe | 85eb1d4 | 2007-02-09 09:30:25 +0100 | [diff] [blame] | 279 | if (td->ddir == DDIR_READ) { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 280 | nd->send_to_net = 0; |
| 281 | ret = fio_netio_setup_listen(td, port); |
| 282 | } else { |
| 283 | nd->send_to_net = 1; |
| 284 | ret = fio_netio_setup_connect(td, host, port); |
| 285 | } |
| 286 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 287 | if (ret) |
| 288 | return ret; |
| 289 | |
| 290 | td->io_size = td->total_file_size; |
| 291 | td->total_io_size = td->io_size; |
| 292 | |
| 293 | for_each_file(td, f, i) { |
| 294 | f->file_size = td->total_file_size / td->nr_files; |
| 295 | f->real_file_size = f->file_size; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 296 | } |
| 297 | |
Jens Axboe | 2fc2698 | 2007-02-06 19:27:27 +0100 | [diff] [blame] | 298 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static void fio_netio_cleanup(struct thread_data *td) |
| 302 | { |
| 303 | if (td->io_ops->data) { |
| 304 | free(td->io_ops->data); |
| 305 | td->io_ops->data = NULL; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static int fio_netio_init(struct thread_data *td) |
| 310 | { |
| 311 | struct net_data *nd; |
| 312 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 313 | /* |
| 314 | * Hack to work-around the ->setup() function calling init on its |
| 315 | * own, since it needs ->io_ops->data to be set up. |
| 316 | */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 317 | if (td->io_ops->data) |
| 318 | return 0; |
| 319 | |
| 320 | nd = malloc(sizeof(*nd)); |
| 321 | nd->last_io_u = NULL; |
| 322 | td->io_ops->data = nd; |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static struct ioengine_ops ioengine = { |
| 327 | .name = "net", |
| 328 | .version = FIO_IOOPS_VERSION, |
| 329 | .init = fio_netio_init, |
| 330 | .prep = fio_netio_prep, |
| 331 | .queue = fio_netio_queue, |
| 332 | .getevents = fio_netio_getevents, |
| 333 | .event = fio_netio_event, |
| 334 | .cleanup = fio_netio_cleanup, |
| 335 | .setup = fio_netio_setup, |
| 336 | .flags = FIO_SYNCIO | FIO_NETIO, |
| 337 | }; |
| 338 | |
| 339 | static void fio_init fio_netio_register(void) |
| 340 | { |
| 341 | register_ioengine(&ioengine); |
| 342 | } |
| 343 | |
| 344 | static void fio_exit fio_netio_unregister(void) |
| 345 | { |
| 346 | unregister_ioengine(&ioengine); |
| 347 | } |