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