Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1 | /* |
Jens Axboe | da751ca | 2007-03-14 10:59:33 +0100 | [diff] [blame] | 2 | * net engine |
| 3 | * |
| 4 | * IO engine that reads/writes to/from sockets. |
| 5 | * |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
| 11 | #include <assert.h> |
| 12 | #include <netinet/in.h> |
| 13 | #include <arpa/inet.h> |
| 14 | #include <netdb.h> |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 15 | #include <sys/poll.h> |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 16 | |
| 17 | #include "../fio.h" |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 18 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 19 | struct netio_data { |
| 20 | int listenfd; |
| 21 | int send_to_net; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 22 | int use_splice; |
| 23 | int pipes[2]; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 24 | char host[64]; |
| 25 | struct sockaddr_in addr; |
| 26 | }; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 27 | |
| 28 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
| 29 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 30 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 31 | struct fio_file *f = io_u->file; |
| 32 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Make sure we don't see spurious reads to a receiver, and vice versa |
| 35 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 36 | if ((nd->send_to_net && io_u->ddir == DDIR_READ) || |
| 37 | (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 38 | td_verror(td, EINVAL, "bad direction"); |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 39 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 40 | } |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 41 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 42 | if (io_u->ddir == DDIR_SYNC) |
| 43 | return 0; |
| 44 | if (io_u->offset == f->last_completed_pos) |
| 45 | return 0; |
| 46 | |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 47 | /* |
| 48 | * If offset is different from last end position, it's a seek. |
| 49 | * As network io is purely sequential, we don't allow seeks. |
| 50 | */ |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 51 | td_verror(td, EINVAL, "cannot seek"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 52 | return 1; |
| 53 | } |
| 54 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 55 | /* |
| 56 | * Receive bytes from a socket and fill them into the internal pipe |
| 57 | */ |
| 58 | static int splice_in(struct thread_data *td, struct io_u *io_u) |
| 59 | { |
| 60 | struct netio_data *nd = td->io_ops->data; |
| 61 | unsigned int len = io_u->xfer_buflen; |
| 62 | struct fio_file *f = io_u->file; |
| 63 | int bytes = 0; |
| 64 | |
| 65 | while (len) { |
Jens Axboe | f657a2f | 2007-06-22 20:40:10 +0200 | [diff] [blame^] | 66 | int ret = splice(f->fd, NULL, nd->pipes[1], NULL, len, 0); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 67 | |
| 68 | if (ret < 0) { |
| 69 | if (!bytes) |
| 70 | bytes = ret; |
| 71 | |
| 72 | break; |
| 73 | } else if (!ret) |
| 74 | break; |
| 75 | |
| 76 | bytes += ret; |
Jens Axboe | f657a2f | 2007-06-22 20:40:10 +0200 | [diff] [blame^] | 77 | len -= ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return bytes; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Transmit 'len' bytes from the internal pipe |
| 85 | */ |
| 86 | static int splice_out(struct thread_data *td, struct io_u *io_u, |
| 87 | unsigned int len) |
| 88 | { |
| 89 | struct netio_data *nd = td->io_ops->data; |
| 90 | struct fio_file *f = io_u->file; |
| 91 | int bytes = 0; |
| 92 | |
| 93 | while (len) { |
| 94 | int ret = splice(nd->pipes[0], NULL, f->fd, NULL, len, 0); |
| 95 | |
| 96 | if (ret < 0) { |
| 97 | if (!bytes) |
| 98 | bytes = ret; |
| 99 | |
| 100 | break; |
| 101 | } else if (!ret) |
| 102 | break; |
| 103 | |
| 104 | bytes += ret; |
| 105 | len -= ret; |
| 106 | } |
| 107 | |
| 108 | return bytes; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * vmsplice() pipe to io_u buffer |
| 113 | */ |
| 114 | static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u, |
| 115 | unsigned int len) |
| 116 | { |
| 117 | struct netio_data *nd = td->io_ops->data; |
| 118 | struct iovec iov = { |
| 119 | .iov_base = io_u->xfer_buf, |
| 120 | .iov_len = len, |
| 121 | }; |
| 122 | int bytes = 0; |
| 123 | |
| 124 | while (iov.iov_len) { |
| 125 | int ret = vmsplice(nd->pipes[0], &iov, 1, 0); |
| 126 | |
| 127 | if (ret < 0) { |
| 128 | if (!bytes) |
| 129 | bytes = ret; |
| 130 | break; |
| 131 | } else if (!ret) |
| 132 | break; |
| 133 | |
| 134 | iov.iov_len -= ret; |
Jens Axboe | f657a2f | 2007-06-22 20:40:10 +0200 | [diff] [blame^] | 135 | bytes += ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 136 | if (iov.iov_len) |
| 137 | iov.iov_base += ret; |
| 138 | } |
| 139 | |
| 140 | return bytes; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * vmsplice() io_u to pipe |
| 145 | */ |
| 146 | static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u) |
| 147 | { |
| 148 | struct netio_data *nd = td->io_ops->data; |
| 149 | struct iovec iov = { |
| 150 | .iov_base = io_u->xfer_buf, |
| 151 | .iov_len = io_u->xfer_buflen, |
| 152 | }; |
| 153 | unsigned int bytes = 0; |
| 154 | |
| 155 | while (iov.iov_len) { |
| 156 | int ret = vmsplice(nd->pipes[1], &iov, 1, 0); |
| 157 | |
| 158 | if (ret < 0) |
| 159 | return -1; |
| 160 | else if (!ret) |
| 161 | return bytes; |
| 162 | |
| 163 | iov.iov_len -= ret; |
| 164 | bytes += ret; |
| 165 | if (iov.iov_len) |
| 166 | iov.iov_base += ret; |
| 167 | } |
| 168 | |
| 169 | return bytes; |
| 170 | } |
| 171 | |
| 172 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
| 173 | { |
| 174 | int ret; |
| 175 | |
| 176 | ret = splice_in(td, io_u); |
| 177 | if (ret <= 0) |
| 178 | return ret; |
| 179 | |
| 180 | return vmsplice_io_u_out(td, io_u, ret); |
| 181 | } |
| 182 | |
| 183 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
| 184 | { |
| 185 | int ret; |
| 186 | |
| 187 | ret = vmsplice_io_u_in(td, io_u); |
| 188 | if (ret <= 0) |
| 189 | return ret; |
| 190 | |
| 191 | return splice_out(td, io_u, ret); |
| 192 | } |
| 193 | |
| 194 | static int fio_netio_send(struct thread_data *td, struct io_u *io_u) |
| 195 | { |
| 196 | int flags = 0; |
| 197 | |
| 198 | /* |
| 199 | * if we are going to write more, set MSG_MORE |
| 200 | */ |
| 201 | if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size) |
| 202 | flags = MSG_MORE; |
| 203 | |
| 204 | return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags); |
| 205 | } |
| 206 | |
| 207 | static int fio_netio_recv(struct io_u *io_u) |
| 208 | { |
| 209 | int flags = MSG_WAITALL; |
| 210 | |
| 211 | return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen, flags); |
| 212 | } |
| 213 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 214 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 215 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 216 | struct netio_data *nd = td->io_ops->data; |
| 217 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 218 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 219 | if (io_u->ddir == DDIR_WRITE) { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 220 | if (nd->use_splice) |
| 221 | ret = fio_netio_splice_out(td, io_u); |
| 222 | else |
| 223 | ret = fio_netio_send(td, io_u); |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 224 | } else if (io_u->ddir == DDIR_READ) { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 225 | if (nd->use_splice) |
| 226 | ret = fio_netio_splice_in(td, io_u); |
| 227 | else |
| 228 | ret = fio_netio_recv(io_u); |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 229 | } else |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 230 | ret = 0; /* must be a SYNC */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 231 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 232 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 233 | if (ret >= 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 234 | io_u->resid = io_u->xfer_buflen - ret; |
| 235 | io_u->error = 0; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 236 | return FIO_Q_COMPLETED; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 237 | } else |
| 238 | io_u->error = errno; |
| 239 | } |
| 240 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 241 | if (io_u->error) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 242 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 243 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 244 | return FIO_Q_COMPLETED; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 247 | static int fio_netio_connect(struct thread_data *td, struct fio_file *f) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 248 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 249 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 250 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 251 | f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 252 | if (f->fd < 0) { |
| 253 | td_verror(td, errno, "socket"); |
| 254 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 257 | if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) { |
| 258 | td_verror(td, errno, "connect"); |
| 259 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 265 | static int fio_netio_accept(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 266 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 267 | struct netio_data *nd = td->io_ops->data; |
| 268 | socklen_t socklen = sizeof(nd->addr); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 269 | struct pollfd pfd; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 270 | int ret; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 271 | |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 272 | log_info("fio: waiting for connection\n"); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * Accept loop. poll for incoming events, accept them. Repeat until we |
| 276 | * have all connections. |
| 277 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 278 | while (!td->terminate) { |
| 279 | pfd.fd = nd->listenfd; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 280 | pfd.events = POLLIN; |
| 281 | |
| 282 | ret = poll(&pfd, 1, -1); |
| 283 | if (ret < 0) { |
| 284 | if (errno == EINTR) |
| 285 | continue; |
| 286 | |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 287 | td_verror(td, errno, "poll"); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 288 | break; |
| 289 | } else if (!ret) |
| 290 | continue; |
| 291 | |
Jens Axboe | 0c09442 | 2007-02-11 04:44:02 +0100 | [diff] [blame] | 292 | /* |
| 293 | * should be impossible |
| 294 | */ |
| 295 | if (!(pfd.revents & POLLIN)) |
| 296 | continue; |
| 297 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 298 | f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen); |
| 299 | if (f->fd < 0) { |
| 300 | td_verror(td, errno, "accept"); |
| 301 | return 1; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 302 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 303 | break; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 309 | |
| 310 | static int fio_netio_open_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 311 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 312 | if (td_read(td)) |
| 313 | return fio_netio_accept(td, f); |
| 314 | else |
| 315 | return fio_netio_connect(td, f); |
| 316 | } |
| 317 | |
| 318 | static int fio_netio_setup_connect(struct thread_data *td, const char *host, |
| 319 | unsigned short port) |
| 320 | { |
| 321 | struct netio_data *nd = td->io_ops->data; |
| 322 | |
| 323 | nd->addr.sin_family = AF_INET; |
| 324 | nd->addr.sin_port = htons(port); |
| 325 | |
| 326 | if (inet_aton(host, &nd->addr.sin_addr) != 1) { |
| 327 | struct hostent *hent; |
| 328 | |
| 329 | hent = gethostbyname(host); |
| 330 | if (!hent) { |
| 331 | td_verror(td, errno, "gethostbyname"); |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | memcpy(&nd->addr.sin_addr, hent->h_addr, 4); |
| 336 | } |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int fio_netio_setup_listen(struct thread_data *td, short port) |
| 342 | { |
| 343 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 344 | int fd, opt; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 345 | |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 346 | fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 347 | if (fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 348 | td_verror(td, errno, "socket"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | opt = 1; |
| 353 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 354 | td_verror(td, errno, "setsockopt"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 355 | return 1; |
| 356 | } |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 357 | #ifdef SO_REUSEPORT |
| 358 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 359 | td_verror(td, errno, "setsockopt"); |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 360 | return 1; |
| 361 | } |
| 362 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 363 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 364 | nd->addr.sin_family = AF_INET; |
| 365 | nd->addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 366 | nd->addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 367 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 368 | if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 369 | td_verror(td, errno, "bind"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 370 | return 1; |
| 371 | } |
| 372 | if (listen(fd, 1) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 373 | td_verror(td, errno, "listen"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 374 | return 1; |
| 375 | } |
| 376 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 377 | nd->listenfd = fd; |
| 378 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 379 | } |
| 380 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 381 | static int fio_netio_init(struct thread_data *td) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 382 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 383 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 384 | unsigned short port; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 385 | char host[64], buf[128]; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 386 | char *sep; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 387 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 388 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 389 | if (td_rw(td)) { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 390 | log_err("fio: network connections must be read OR write\n"); |
| 391 | return 1; |
| 392 | } |
Jens Axboe | 16d55aa | 2007-05-22 09:21:37 +0200 | [diff] [blame] | 393 | if (td_random(td)) { |
| 394 | log_err("fio: network IO can't be random\n"); |
| 395 | return 1; |
| 396 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 397 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 398 | strcpy(buf, td->o.filename); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 399 | |
Jens Axboe | 9f9214f | 2007-03-13 14:02:16 +0100 | [diff] [blame] | 400 | sep = strchr(buf, '/'); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 401 | if (!sep) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 402 | log_err("fio: bad network host/port <<%s>>\n", td->o.filename); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | *sep = '\0'; |
| 407 | sep++; |
| 408 | strcpy(host, buf); |
Jens Axboe | e01547d | 2007-02-06 19:16:01 +0100 | [diff] [blame] | 409 | port = atoi(sep); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 410 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 411 | if (td_read(td)) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 412 | nd->send_to_net = 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 413 | ret = fio_netio_setup_listen(td, port); |
| 414 | } else { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 415 | nd->send_to_net = 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 416 | ret = fio_netio_setup_connect(td, host, port); |
| 417 | } |
| 418 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 419 | return ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 422 | static void fio_netio_cleanup(struct thread_data *td) |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 423 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 424 | struct netio_data *nd = td->io_ops->data; |
| 425 | |
| 426 | if (nd) { |
| 427 | free(nd); |
| 428 | td->io_ops->data = NULL; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static int fio_netio_setup(struct thread_data *td) |
| 433 | { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 434 | struct netio_data *nd; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 435 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 436 | if (!td->io_ops->data) { |
| 437 | nd = malloc(sizeof(*nd));; |
| 438 | |
| 439 | memset(nd, 0, sizeof(*nd)); |
| 440 | nd->listenfd = -1; |
| 441 | td->io_ops->data = nd; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 447 | static int fio_netio_setup_splice(struct thread_data *td) |
| 448 | { |
| 449 | struct netio_data *nd; |
| 450 | |
| 451 | fio_netio_setup(td); |
| 452 | |
| 453 | nd = td->io_ops->data; |
| 454 | if (nd) { |
| 455 | if (pipe(nd->pipes) < 0) |
| 456 | return 1; |
| 457 | |
| 458 | nd->use_splice = 1; |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | return 1; |
| 463 | } |
| 464 | |
| 465 | static struct ioengine_ops ioengine_rw = { |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 466 | .name = "net", |
| 467 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 468 | .prep = fio_netio_prep, |
| 469 | .queue = fio_netio_queue, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 470 | .setup = fio_netio_setup, |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 471 | .init = fio_netio_init, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 472 | .cleanup = fio_netio_cleanup, |
| 473 | .open_file = fio_netio_open_file, |
| 474 | .close_file = generic_close_file, |
| 475 | .flags = FIO_SYNCIO | FIO_DISKLESSIO, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 476 | }; |
| 477 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 478 | static struct ioengine_ops ioengine_splice = { |
| 479 | .name = "netsplice", |
| 480 | .version = FIO_IOOPS_VERSION, |
| 481 | .prep = fio_netio_prep, |
| 482 | .queue = fio_netio_queue, |
| 483 | .setup = fio_netio_setup_splice, |
| 484 | .init = fio_netio_init, |
| 485 | .cleanup = fio_netio_cleanup, |
| 486 | .open_file = fio_netio_open_file, |
| 487 | .close_file = generic_close_file, |
| 488 | .flags = FIO_SYNCIO | FIO_DISKLESSIO, |
| 489 | }; |
| 490 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 491 | static void fio_init fio_netio_register(void) |
| 492 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 493 | register_ioengine(&ioengine_rw); |
| 494 | register_ioengine(&ioengine_splice); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static void fio_exit fio_netio_unregister(void) |
| 498 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 499 | unregister_ioengine(&ioengine_rw); |
| 500 | unregister_ioengine(&ioengine_splice); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 501 | } |