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> |
Steven Noonan | 842805f | 2012-12-01 08:16:55 +0000 | [diff] [blame^] | 10 | #include <signal.h> |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <assert.h> |
| 13 | #include <netinet/in.h> |
| 14 | #include <arpa/inet.h> |
| 15 | #include <netdb.h> |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 16 | #include <sys/poll.h> |
Jens Axboe | 7292056 | 2008-06-02 12:30:06 +0200 | [diff] [blame] | 17 | #include <sys/types.h> |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 18 | #include <sys/stat.h> |
Jens Axboe | 7292056 | 2008-06-02 12:30:06 +0200 | [diff] [blame] | 19 | #include <sys/socket.h> |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 20 | #include <sys/un.h> |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 21 | |
| 22 | #include "../fio.h" |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 23 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 24 | struct netio_data { |
| 25 | int listenfd; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 26 | int use_splice; |
| 27 | int pipes[2]; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 28 | struct sockaddr_in addr; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 29 | struct sockaddr_un addr_un; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 30 | }; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 31 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 32 | struct netio_options { |
| 33 | struct thread_data *td; |
| 34 | unsigned int port; |
| 35 | unsigned int proto; |
| 36 | unsigned int listen; |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 37 | unsigned int pingpong; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 38 | }; |
| 39 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 40 | struct udp_close_msg { |
| 41 | uint32_t magic; |
| 42 | uint32_t cmd; |
| 43 | }; |
| 44 | |
| 45 | enum { |
| 46 | FIO_LINK_CLOSE = 0x89, |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 47 | FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b, |
| 48 | FIO_LINK_OPEN = 0x98, |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 49 | |
| 50 | FIO_TYPE_TCP = 1, |
| 51 | FIO_TYPE_UDP = 2, |
| 52 | FIO_TYPE_UNIX = 3, |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 55 | static int str_hostname_cb(void *data, const char *input); |
| 56 | static struct fio_option options[] = { |
| 57 | { |
| 58 | .name = "hostname", |
| 59 | .type = FIO_OPT_STR_STORE, |
| 60 | .cb = str_hostname_cb, |
| 61 | .help = "Hostname for net IO engine", |
| 62 | }, |
| 63 | { |
| 64 | .name = "port", |
| 65 | .type = FIO_OPT_INT, |
| 66 | .off1 = offsetof(struct netio_options, port), |
| 67 | .minval = 1, |
| 68 | .maxval = 65535, |
| 69 | .help = "Port to use for TCP or UDP net connections", |
| 70 | }, |
| 71 | { |
| 72 | .name = "protocol", |
| 73 | .alias = "proto", |
| 74 | .type = FIO_OPT_STR, |
| 75 | .off1 = offsetof(struct netio_options, proto), |
| 76 | .help = "Network protocol to use", |
| 77 | .def = "tcp", |
| 78 | .posval = { |
| 79 | { .ival = "tcp", |
| 80 | .oval = FIO_TYPE_TCP, |
| 81 | .help = "Transmission Control Protocol", |
| 82 | }, |
| 83 | { .ival = "udp", |
| 84 | .oval = FIO_TYPE_UDP, |
Bruce Cran | f5cc3d0 | 2012-10-10 08:17:44 -0600 | [diff] [blame] | 85 | .help = "User Datagram Protocol", |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 86 | }, |
| 87 | { .ival = "unix", |
| 88 | .oval = FIO_TYPE_UNIX, |
| 89 | .help = "UNIX domain socket", |
| 90 | }, |
| 91 | }, |
| 92 | }, |
| 93 | { |
| 94 | .name = "listen", |
| 95 | .type = FIO_OPT_STR_SET, |
| 96 | .off1 = offsetof(struct netio_options, listen), |
| 97 | .help = "Listen for incoming TCP connections", |
| 98 | }, |
| 99 | { |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 100 | .name = "pingpong", |
| 101 | .type = FIO_OPT_STR_SET, |
| 102 | .off1 = offsetof(struct netio_options, pingpong), |
| 103 | .help = "Ping-pong IO requests", |
| 104 | }, |
| 105 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 106 | .name = NULL, |
| 107 | }, |
| 108 | }; |
| 109 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 110 | /* |
| 111 | * Return -1 for error and 'nr events' for a positive number |
| 112 | * of events |
| 113 | */ |
| 114 | static int poll_wait(struct thread_data *td, int fd, short events) |
| 115 | { |
| 116 | struct pollfd pfd; |
| 117 | int ret; |
| 118 | |
| 119 | while (!td->terminate) { |
| 120 | pfd.fd = fd; |
| 121 | pfd.events = events; |
| 122 | ret = poll(&pfd, 1, -1); |
| 123 | if (ret < 0) { |
| 124 | if (errno == EINTR) |
Jens Axboe | d5b388a | 2009-01-19 12:38:27 +0100 | [diff] [blame] | 125 | break; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 126 | |
| 127 | td_verror(td, errno, "poll"); |
| 128 | return -1; |
| 129 | } else if (!ret) |
| 130 | continue; |
| 131 | |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | if (pfd.revents & events) |
| 136 | return 1; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 137 | |
| 138 | return -1; |
| 139 | } |
| 140 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 141 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
| 142 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 143 | struct netio_options *o = td->eo; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 144 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 145 | /* |
| 146 | * Make sure we don't see spurious reads to a receiver, and vice versa |
| 147 | */ |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 148 | if (o->proto == FIO_TYPE_TCP) |
| 149 | return 0; |
| 150 | |
| 151 | if ((o->listen && io_u->ddir == DDIR_WRITE) || |
| 152 | (!o->listen && io_u->ddir == DDIR_READ)) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 153 | td_verror(td, EINVAL, "bad direction"); |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 154 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 155 | } |
Bruce Cran | 3f457be | 2012-10-10 13:37:41 +0100 | [diff] [blame] | 156 | |
Jens Axboe | f85ac25 | 2008-03-01 18:09:49 +0100 | [diff] [blame] | 157 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 160 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 161 | static int splice_io_u(int fdin, int fdout, unsigned int len) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 162 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 163 | int bytes = 0; |
| 164 | |
| 165 | while (len) { |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 166 | int ret = splice(fdin, NULL, fdout, NULL, len, 0); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 167 | |
| 168 | if (ret < 0) { |
| 169 | if (!bytes) |
| 170 | bytes = ret; |
| 171 | |
| 172 | break; |
| 173 | } else if (!ret) |
| 174 | break; |
| 175 | |
| 176 | bytes += ret; |
Jens Axboe | f657a2f | 2007-06-22 20:40:10 +0200 | [diff] [blame] | 177 | len -= ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return bytes; |
| 181 | } |
| 182 | |
| 183 | /* |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 184 | * Receive bytes from a socket and fill them into the internal pipe |
| 185 | */ |
| 186 | static int splice_in(struct thread_data *td, struct io_u *io_u) |
| 187 | { |
| 188 | struct netio_data *nd = td->io_ops->data; |
| 189 | |
| 190 | return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen); |
| 191 | } |
| 192 | |
| 193 | /* |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 194 | * Transmit 'len' bytes from the internal pipe |
| 195 | */ |
| 196 | static int splice_out(struct thread_data *td, struct io_u *io_u, |
| 197 | unsigned int len) |
| 198 | { |
| 199 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 200 | |
| 201 | return splice_io_u(nd->pipes[0], io_u->file->fd, len); |
| 202 | } |
| 203 | |
| 204 | static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len) |
| 205 | { |
| 206 | struct iovec iov = { |
| 207 | .iov_base = io_u->xfer_buf, |
| 208 | .iov_len = len, |
| 209 | }; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 210 | int bytes = 0; |
| 211 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 212 | while (iov.iov_len) { |
| 213 | int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 214 | |
| 215 | if (ret < 0) { |
| 216 | if (!bytes) |
| 217 | bytes = ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 218 | break; |
| 219 | } else if (!ret) |
| 220 | break; |
| 221 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 222 | iov.iov_len -= ret; |
| 223 | iov.iov_base += ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 224 | bytes += ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return bytes; |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 228 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * vmsplice() pipe to io_u buffer |
| 233 | */ |
| 234 | static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u, |
| 235 | unsigned int len) |
| 236 | { |
| 237 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 238 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 239 | return vmsplice_io_u(io_u, nd->pipes[0], len); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | * vmsplice() io_u to pipe |
| 244 | */ |
| 245 | static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u) |
| 246 | { |
| 247 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 248 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 249 | 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] | 250 | } |
| 251 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 252 | /* |
| 253 | * splice receive - transfer socket data into a pipe using splice, then map |
| 254 | * that pipe data into the io_u using vmsplice. |
| 255 | */ |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 256 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
| 257 | { |
| 258 | int ret; |
| 259 | |
| 260 | ret = splice_in(td, io_u); |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 261 | if (ret > 0) |
| 262 | return vmsplice_io_u_out(td, io_u, ret); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 263 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 264 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 267 | /* |
| 268 | * splice transmit - map data from the io_u into a pipe by using vmsplice, |
| 269 | * then transfer that pipe to a socket using splice. |
| 270 | */ |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 271 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
| 272 | { |
| 273 | int ret; |
| 274 | |
| 275 | ret = vmsplice_io_u_in(td, io_u); |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 276 | if (ret > 0) |
| 277 | return splice_out(td, io_u, ret); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 278 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 279 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 280 | } |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 281 | #else |
| 282 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
| 283 | { |
Jens Axboe | af8771b | 2008-05-30 22:58:28 +0200 | [diff] [blame] | 284 | errno = EOPNOTSUPP; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
| 289 | { |
Jens Axboe | af8771b | 2008-05-30 22:58:28 +0200 | [diff] [blame] | 290 | errno = EOPNOTSUPP; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 291 | return -1; |
| 292 | } |
| 293 | #endif |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 294 | |
| 295 | static int fio_netio_send(struct thread_data *td, struct io_u *io_u) |
| 296 | { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 297 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 298 | struct netio_options *o = td->eo; |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 299 | int ret, flags = 0; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 300 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 301 | do { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 302 | if (o->proto == FIO_TYPE_UDP) { |
Jens Axboe | 62b3892 | 2009-05-11 10:37:33 +0200 | [diff] [blame] | 303 | struct sockaddr *to = (struct sockaddr *) &nd->addr; |
| 304 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 305 | ret = sendto(io_u->file->fd, io_u->xfer_buf, |
Jens Axboe | 62b3892 | 2009-05-11 10:37:33 +0200 | [diff] [blame] | 306 | io_u->xfer_buflen, flags, to, |
| 307 | sizeof(*to)); |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 308 | } else { |
| 309 | /* |
| 310 | * if we are going to write more, set MSG_MORE |
| 311 | */ |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 312 | #ifdef MSG_MORE |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 313 | if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < |
| 314 | td->o.size) && !o->pingpong) |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 315 | flags |= MSG_MORE; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 316 | #endif |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 317 | ret = send(io_u->file->fd, io_u->xfer_buf, |
| 318 | io_u->xfer_buflen, flags); |
| 319 | } |
| 320 | if (ret > 0) |
| 321 | break; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 322 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 323 | ret = poll_wait(td, io_u->file->fd, POLLOUT); |
| 324 | if (ret <= 0) |
| 325 | break; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 326 | } while (1); |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | static int is_udp_close(struct io_u *io_u, int len) |
| 332 | { |
| 333 | struct udp_close_msg *msg; |
| 334 | |
| 335 | if (len != sizeof(struct udp_close_msg)) |
| 336 | return 0; |
| 337 | |
| 338 | msg = io_u->xfer_buf; |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 339 | if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC) |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | if (ntohl(msg->cmd) != FIO_LINK_CLOSE) |
| 342 | return 0; |
| 343 | |
| 344 | return 1; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 347 | static int fio_netio_recv(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 348 | { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 349 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 350 | struct netio_options *o = td->eo; |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 351 | int ret, flags = 0; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 352 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 353 | do { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 354 | if (o->proto == FIO_TYPE_UDP) { |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 355 | fio_socklen_t len = sizeof(nd->addr); |
Jens Axboe | 62b3892 | 2009-05-11 10:37:33 +0200 | [diff] [blame] | 356 | struct sockaddr *from = (struct sockaddr *) &nd->addr; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 357 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 358 | ret = recvfrom(io_u->file->fd, io_u->xfer_buf, |
Jens Axboe | 62b3892 | 2009-05-11 10:37:33 +0200 | [diff] [blame] | 359 | io_u->xfer_buflen, flags, from, &len); |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 360 | if (is_udp_close(io_u, ret)) { |
| 361 | td->done = 1; |
| 362 | return 0; |
| 363 | } |
| 364 | } else { |
| 365 | ret = recv(io_u->file->fd, io_u->xfer_buf, |
| 366 | io_u->xfer_buflen, flags); |
| 367 | } |
| 368 | if (ret > 0) |
| 369 | break; |
Jens Axboe | 7d988f6 | 2012-11-29 19:57:35 +0100 | [diff] [blame] | 370 | else if (!ret && (flags & MSG_WAITALL)) |
| 371 | break; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 372 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 373 | ret = poll_wait(td, io_u->file->fd, POLLIN); |
| 374 | if (ret <= 0) |
| 375 | break; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 376 | flags |= MSG_WAITALL; |
| 377 | } while (1); |
| 378 | |
| 379 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 382 | static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u, |
| 383 | enum fio_ddir ddir) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 384 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 385 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 386 | struct netio_options *o = td->eo; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 387 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 388 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 389 | if (ddir == DDIR_WRITE) { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 390 | if (!nd->use_splice || o->proto == FIO_TYPE_UDP || |
| 391 | o->proto == FIO_TYPE_UNIX) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 392 | ret = fio_netio_send(td, io_u); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 393 | else |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 394 | ret = fio_netio_splice_out(td, io_u); |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 395 | } else if (ddir == DDIR_READ) { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 396 | if (!nd->use_splice || o->proto == FIO_TYPE_UDP || |
| 397 | o->proto == FIO_TYPE_UNIX) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 398 | ret = fio_netio_recv(td, io_u); |
| 399 | else |
| 400 | ret = fio_netio_splice_in(td, io_u); |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 401 | } else |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 402 | ret = 0; /* must be a SYNC */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 403 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 404 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 405 | if (ret >= 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 406 | io_u->resid = io_u->xfer_buflen - ret; |
| 407 | io_u->error = 0; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 408 | return FIO_Q_COMPLETED; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 409 | } else { |
| 410 | int err = errno; |
| 411 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 412 | if (ddir == DDIR_WRITE && err == EMSGSIZE) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 413 | return FIO_Q_BUSY; |
| 414 | |
| 415 | io_u->error = err; |
| 416 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 419 | if (io_u->error) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 420 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 421 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 422 | return FIO_Q_COMPLETED; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 425 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 426 | { |
| 427 | struct netio_options *o = td->eo; |
| 428 | int ret; |
| 429 | |
| 430 | fio_ro_check(td, io_u); |
| 431 | |
| 432 | ret = __fio_netio_queue(td, io_u, io_u->ddir); |
| 433 | if (!o->pingpong || ret != FIO_Q_COMPLETED) |
| 434 | return ret; |
| 435 | |
| 436 | /* |
| 437 | * For ping-pong mode, receive or send reply as needed |
| 438 | */ |
| 439 | if (td_read(td) && io_u->ddir == DDIR_READ) |
| 440 | ret = __fio_netio_queue(td, io_u, DDIR_WRITE); |
| 441 | else if (td_write(td) && io_u->ddir == DDIR_WRITE) |
| 442 | ret = __fio_netio_queue(td, io_u, DDIR_READ); |
| 443 | |
| 444 | return ret; |
| 445 | } |
| 446 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 447 | 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] | 448 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 449 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 450 | struct netio_options *o = td->eo; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 451 | int type, domain; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 452 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 453 | if (o->proto == FIO_TYPE_TCP) { |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 454 | domain = AF_INET; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 455 | type = SOCK_STREAM; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 456 | } else if (o->proto == FIO_TYPE_UDP) { |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 457 | domain = AF_INET; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 458 | type = SOCK_DGRAM; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 459 | } else if (o->proto == FIO_TYPE_UNIX) { |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 460 | domain = AF_UNIX; |
| 461 | type = SOCK_STREAM; |
| 462 | } else { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 463 | log_err("fio: bad network type %d\n", o->proto); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 464 | f->fd = -1; |
| 465 | return 1; |
| 466 | } |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 467 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 468 | f->fd = socket(domain, type, 0); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 469 | if (f->fd < 0) { |
| 470 | td_verror(td, errno, "socket"); |
| 471 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 474 | if (o->proto == FIO_TYPE_UDP) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 475 | return 0; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 476 | else if (o->proto == FIO_TYPE_TCP) { |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 477 | fio_socklen_t len = sizeof(nd->addr); |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 478 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 479 | if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) { |
| 480 | td_verror(td, errno, "connect"); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 481 | close(f->fd); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 482 | return 1; |
| 483 | } |
| 484 | } else { |
| 485 | struct sockaddr_un *addr = &nd->addr_un; |
| 486 | fio_socklen_t len; |
| 487 | |
| 488 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; |
| 489 | |
| 490 | if (connect(f->fd, (struct sockaddr *) addr, len) < 0) { |
| 491 | td_verror(td, errno, "connect"); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 492 | close(f->fd); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 493 | return 1; |
| 494 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 500 | 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] | 501 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 502 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 503 | struct netio_options *o = td->eo; |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 504 | fio_socklen_t socklen = sizeof(nd->addr); |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 505 | int state; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 506 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 507 | if (o->proto == FIO_TYPE_UDP) { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 508 | f->fd = nd->listenfd; |
| 509 | return 0; |
| 510 | } |
| 511 | |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 512 | state = td->runstate; |
| 513 | td_set_runstate(td, TD_SETTING_UP); |
| 514 | |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 515 | log_info("fio: waiting for connection\n"); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 516 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 517 | if (poll_wait(td, nd->listenfd, POLLIN) < 0) |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 518 | goto err; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 519 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 520 | f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen); |
| 521 | if (f->fd < 0) { |
| 522 | td_verror(td, errno, "accept"); |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 523 | goto err; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Jens Axboe | 0cae16f | 2012-11-30 16:22:31 +0100 | [diff] [blame] | 526 | reset_all_stats(td); |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 527 | td_set_runstate(td, state); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 528 | return 0; |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 529 | err: |
| 530 | td_set_runstate(td, state); |
| 531 | return 1; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 534 | static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f) |
| 535 | { |
| 536 | struct netio_data *nd = td->io_ops->data; |
| 537 | struct udp_close_msg msg; |
Jens Axboe | 62b3892 | 2009-05-11 10:37:33 +0200 | [diff] [blame] | 538 | struct sockaddr *to = (struct sockaddr *) &nd->addr; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 539 | int ret; |
| 540 | |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 541 | msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC); |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 542 | msg.cmd = htonl(FIO_LINK_CLOSE); |
| 543 | |
Jens Axboe | 62b3892 | 2009-05-11 10:37:33 +0200 | [diff] [blame] | 544 | ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to, |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 545 | sizeof(nd->addr)); |
| 546 | if (ret < 0) |
| 547 | td_verror(td, errno, "sendto udp link close"); |
| 548 | } |
| 549 | |
| 550 | static int fio_netio_close_file(struct thread_data *td, struct fio_file *f) |
| 551 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 552 | struct netio_options *o = td->eo; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * If this is an UDP connection, notify the receiver that we are |
| 556 | * closing down the link |
| 557 | */ |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 558 | if (o->proto == FIO_TYPE_UDP) |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 559 | fio_netio_udp_close(td, f); |
| 560 | |
| 561 | return generic_close_file(td, f); |
| 562 | } |
| 563 | |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 564 | static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f) |
| 565 | { |
| 566 | struct netio_data *nd = td->io_ops->data; |
| 567 | struct udp_close_msg msg; |
| 568 | struct sockaddr *to = (struct sockaddr *) &nd->addr; |
| 569 | fio_socklen_t len = sizeof(nd->addr); |
| 570 | int ret; |
| 571 | |
| 572 | ret = recvfrom(f->fd, &msg, sizeof(msg), MSG_WAITALL, to, &len); |
| 573 | if (ret < 0) { |
| 574 | td_verror(td, errno, "sendto udp link open"); |
| 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC || |
| 579 | ntohl(msg.cmd) != FIO_LINK_OPEN) { |
| 580 | log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic), |
| 581 | ntohl(msg.cmd)); |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f) |
| 589 | { |
| 590 | struct netio_data *nd = td->io_ops->data; |
| 591 | struct udp_close_msg msg; |
| 592 | struct sockaddr *to = (struct sockaddr *) &nd->addr; |
| 593 | int ret; |
| 594 | |
| 595 | msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC); |
| 596 | msg.cmd = htonl(FIO_LINK_OPEN); |
| 597 | |
| 598 | ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to, |
| 599 | sizeof(nd->addr)); |
| 600 | if (ret < 0) { |
| 601 | td_verror(td, errno, "sendto udp link open"); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int fio_netio_open_file(struct thread_data *td, struct fio_file *f) |
| 609 | { |
| 610 | int ret; |
| 611 | struct netio_options *o = td->eo; |
| 612 | |
| 613 | if (o->listen) |
| 614 | ret = fio_netio_accept(td, f); |
| 615 | else |
| 616 | ret = fio_netio_connect(td, f); |
| 617 | |
| 618 | if (ret) { |
| 619 | f->fd = -1; |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | if (o->proto == FIO_TYPE_UDP) { |
| 624 | if (td_write(td)) |
| 625 | ret = fio_netio_udp_send_open(td, f); |
| 626 | else { |
| 627 | int state; |
| 628 | |
| 629 | state = td->runstate; |
| 630 | td_set_runstate(td, TD_SETTING_UP); |
| 631 | ret = fio_netio_udp_recv_open(td, f); |
| 632 | td_set_runstate(td, state); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | if (ret) |
| 637 | fio_netio_close_file(td, f); |
| 638 | |
| 639 | return ret; |
| 640 | } |
| 641 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 642 | static int fio_netio_setup_connect_inet(struct thread_data *td, |
| 643 | const char *host, unsigned short port) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 644 | { |
| 645 | struct netio_data *nd = td->io_ops->data; |
| 646 | |
Jens Axboe | 166dce4 | 2012-11-29 14:35:33 +0100 | [diff] [blame] | 647 | if (!host) { |
| 648 | log_err("fio: connect with no host to connect to.\n"); |
| 649 | if (td_read(td)) |
| 650 | log_err("fio: did you forget to set 'listen'?\n"); |
| 651 | |
| 652 | td_verror(td, EINVAL, "no hostname= set"); |
| 653 | return 1; |
| 654 | } |
| 655 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 656 | nd->addr.sin_family = AF_INET; |
| 657 | nd->addr.sin_port = htons(port); |
| 658 | |
| 659 | if (inet_aton(host, &nd->addr.sin_addr) != 1) { |
| 660 | struct hostent *hent; |
| 661 | |
| 662 | hent = gethostbyname(host); |
| 663 | if (!hent) { |
| 664 | td_verror(td, errno, "gethostbyname"); |
| 665 | return 1; |
| 666 | } |
| 667 | |
| 668 | memcpy(&nd->addr.sin_addr, hent->h_addr, 4); |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 674 | static int fio_netio_setup_connect_unix(struct thread_data *td, |
| 675 | const char *path) |
| 676 | { |
| 677 | struct netio_data *nd = td->io_ops->data; |
| 678 | struct sockaddr_un *soun = &nd->addr_un; |
| 679 | |
| 680 | soun->sun_family = AF_UNIX; |
| 681 | strcpy(soun->sun_path, path); |
| 682 | return 0; |
| 683 | } |
| 684 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 685 | static int fio_netio_setup_connect(struct thread_data *td) |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 686 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 687 | struct netio_options *o = td->eo; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 688 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 689 | if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP) |
| 690 | return fio_netio_setup_connect_inet(td, td->o.filename,o->port); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 691 | else |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 692 | return fio_netio_setup_connect_unix(td, td->o.filename); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path) |
| 696 | { |
| 697 | struct netio_data *nd = td->io_ops->data; |
| 698 | struct sockaddr_un *addr = &nd->addr_un; |
| 699 | mode_t mode; |
| 700 | int len, fd; |
| 701 | |
| 702 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 703 | if (fd < 0) { |
| 704 | log_err("fio: socket: %s\n", strerror(errno)); |
| 705 | return -1; |
| 706 | } |
| 707 | |
| 708 | mode = umask(000); |
| 709 | |
| 710 | memset(addr, 0, sizeof(*addr)); |
| 711 | addr->sun_family = AF_UNIX; |
| 712 | strcpy(addr->sun_path, path); |
| 713 | unlink(path); |
| 714 | |
| 715 | len = sizeof(addr->sun_family) + strlen(path) + 1; |
| 716 | |
| 717 | if (bind(fd, (struct sockaddr *) addr, len) < 0) { |
| 718 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 719 | close(fd); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 720 | return -1; |
| 721 | } |
| 722 | |
| 723 | umask(mode); |
| 724 | nd->listenfd = fd; |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int fio_netio_setup_listen_inet(struct thread_data *td, short port) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 729 | { |
| 730 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 731 | struct netio_options *o = td->eo; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 732 | int fd, opt, type; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 733 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 734 | if (o->proto == FIO_TYPE_TCP) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 735 | type = SOCK_STREAM; |
| 736 | else |
| 737 | type = SOCK_DGRAM; |
| 738 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 739 | fd = socket(AF_INET, type, 0); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 740 | if (fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 741 | td_verror(td, errno, "socket"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 742 | return 1; |
| 743 | } |
| 744 | |
| 745 | opt = 1; |
| 746 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 747 | td_verror(td, errno, "setsockopt"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 748 | return 1; |
| 749 | } |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 750 | #ifdef SO_REUSEPORT |
| 751 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 752 | td_verror(td, errno, "setsockopt"); |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 753 | return 1; |
| 754 | } |
| 755 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 756 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 757 | nd->addr.sin_family = AF_INET; |
| 758 | nd->addr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 759 | nd->addr.sin_port = htons(port); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 760 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 761 | if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 762 | td_verror(td, errno, "bind"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 763 | return 1; |
| 764 | } |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 765 | |
| 766 | nd->listenfd = fd; |
| 767 | return 0; |
| 768 | } |
| 769 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 770 | static int fio_netio_setup_listen(struct thread_data *td) |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 771 | { |
| 772 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 773 | struct netio_options *o = td->eo; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 774 | int ret; |
| 775 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 776 | if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP) |
| 777 | ret = fio_netio_setup_listen_inet(td, o->port); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 778 | else |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 779 | ret = fio_netio_setup_listen_unix(td, td->o.filename); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 780 | |
| 781 | if (ret) |
| 782 | return ret; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 783 | if (o->proto == FIO_TYPE_UDP) |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 784 | return 0; |
| 785 | |
| 786 | if (listen(nd->listenfd, 10) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 787 | td_verror(td, errno, "listen"); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 788 | nd->listenfd = -1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 789 | return 1; |
| 790 | } |
| 791 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 792 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 793 | } |
| 794 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 795 | static int fio_netio_init(struct thread_data *td) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 796 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 797 | struct netio_options *o = td->eo; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 798 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 799 | |
Bruce Cran | 3f457be | 2012-10-10 13:37:41 +0100 | [diff] [blame] | 800 | #ifdef WIN32 |
| 801 | WSADATA wsd; |
| 802 | WSAStartup(MAKEWORD(2,2), &wsd); |
| 803 | #endif |
| 804 | |
Jens Axboe | 16d55aa | 2007-05-22 09:21:37 +0200 | [diff] [blame] | 805 | if (td_random(td)) { |
| 806 | log_err("fio: network IO can't be random\n"); |
| 807 | return 1; |
| 808 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 809 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 810 | if (o->proto == FIO_TYPE_UNIX && o->port) { |
| 811 | log_err("fio: network IO port not valid with unix socket\n"); |
| 812 | return 1; |
| 813 | } else if (o->proto != FIO_TYPE_UNIX && !o->port) { |
| 814 | log_err("fio: network IO requires port for tcp or udp\n"); |
| 815 | return 1; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 816 | } |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 817 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 818 | if (o->proto != FIO_TYPE_TCP) { |
| 819 | if (o->listen) { |
Jens Axboe | 9b98606 | 2011-12-19 08:57:18 +0100 | [diff] [blame] | 820 | log_err("fio: listen only valid for TCP proto IO\n"); |
| 821 | return 1; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 822 | } |
| 823 | if (td_rw(td)) { |
Jens Axboe | 9b98606 | 2011-12-19 08:57:18 +0100 | [diff] [blame] | 824 | log_err("fio: datagram network connections must be" |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 825 | " read OR write\n"); |
Jens Axboe | 9b98606 | 2011-12-19 08:57:18 +0100 | [diff] [blame] | 826 | return 1; |
| 827 | } |
| 828 | if (o->proto == FIO_TYPE_UNIX && !td->o.filename) { |
| 829 | log_err("fio: UNIX sockets need host/filename\n"); |
| 830 | return 1; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 831 | } |
| 832 | o->listen = td_read(td); |
| 833 | } |
| 834 | |
| 835 | if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) { |
| 836 | log_err("fio: hostname not valid for inbound network IO\n"); |
| 837 | return 1; |
| 838 | } |
| 839 | |
| 840 | if (o->listen) |
| 841 | ret = fio_netio_setup_listen(td); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 842 | else |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 843 | ret = fio_netio_setup_connect(td); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 844 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 845 | return ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 846 | } |
| 847 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 848 | static void fio_netio_cleanup(struct thread_data *td) |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 849 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 850 | struct netio_data *nd = td->io_ops->data; |
| 851 | |
| 852 | if (nd) { |
Jens Axboe | 64b24cd | 2007-06-24 21:28:39 +0200 | [diff] [blame] | 853 | if (nd->listenfd != -1) |
| 854 | close(nd->listenfd); |
| 855 | if (nd->pipes[0] != -1) |
| 856 | close(nd->pipes[0]); |
| 857 | if (nd->pipes[1] != -1) |
| 858 | close(nd->pipes[1]); |
| 859 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 860 | free(nd); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
| 864 | static int fio_netio_setup(struct thread_data *td) |
| 865 | { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 866 | struct netio_data *nd; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 867 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 868 | if (!td->files_index) { |
| 869 | add_file(td, td->o.filename ?: "net"); |
| 870 | td->o.nr_files = td->o.nr_files ?: 1; |
| 871 | } |
| 872 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 873 | if (!td->io_ops->data) { |
| 874 | nd = malloc(sizeof(*nd));; |
| 875 | |
| 876 | memset(nd, 0, sizeof(*nd)); |
| 877 | nd->listenfd = -1; |
Jens Axboe | 64b24cd | 2007-06-24 21:28:39 +0200 | [diff] [blame] | 878 | nd->pipes[0] = nd->pipes[1] = -1; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 879 | td->io_ops->data = nd; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 880 | } |
| 881 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 882 | return 0; |
| 883 | } |
| 884 | |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 885 | static void fio_netio_terminate(struct thread_data *td) |
| 886 | { |
| 887 | kill(td->pid, SIGUSR2); |
| 888 | } |
| 889 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 890 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 891 | static int fio_netio_setup_splice(struct thread_data *td) |
| 892 | { |
| 893 | struct netio_data *nd; |
| 894 | |
| 895 | fio_netio_setup(td); |
| 896 | |
| 897 | nd = td->io_ops->data; |
| 898 | if (nd) { |
| 899 | if (pipe(nd->pipes) < 0) |
| 900 | return 1; |
| 901 | |
| 902 | nd->use_splice = 1; |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | return 1; |
| 907 | } |
| 908 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 909 | static struct ioengine_ops ioengine_splice = { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 910 | .name = "netsplice", |
| 911 | .version = FIO_IOOPS_VERSION, |
| 912 | .prep = fio_netio_prep, |
| 913 | .queue = fio_netio_queue, |
| 914 | .setup = fio_netio_setup_splice, |
| 915 | .init = fio_netio_init, |
| 916 | .cleanup = fio_netio_cleanup, |
| 917 | .open_file = fio_netio_open_file, |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 918 | .close_file = fio_netio_close_file, |
| 919 | .terminate = fio_netio_terminate, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 920 | .options = options, |
| 921 | .option_struct_size = sizeof(struct netio_options), |
| 922 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 923 | FIO_PIPEIO, |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 924 | }; |
| 925 | #endif |
| 926 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 927 | static struct ioengine_ops ioengine_rw = { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 928 | .name = "net", |
| 929 | .version = FIO_IOOPS_VERSION, |
| 930 | .prep = fio_netio_prep, |
| 931 | .queue = fio_netio_queue, |
| 932 | .setup = fio_netio_setup, |
| 933 | .init = fio_netio_init, |
| 934 | .cleanup = fio_netio_cleanup, |
| 935 | .open_file = fio_netio_open_file, |
| 936 | .close_file = fio_netio_close_file, |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 937 | .terminate = fio_netio_terminate, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 938 | .options = options, |
| 939 | .option_struct_size = sizeof(struct netio_options), |
| 940 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 941 | FIO_PIPEIO, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 942 | }; |
| 943 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 944 | static int str_hostname_cb(void *data, const char *input) |
| 945 | { |
| 946 | struct netio_options *o = data; |
| 947 | |
| 948 | if (o->td->o.filename) |
| 949 | free(o->td->o.filename); |
| 950 | o->td->o.filename = strdup(input); |
| 951 | return 0; |
| 952 | } |
| 953 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 954 | static void fio_init fio_netio_register(void) |
| 955 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 956 | register_ioengine(&ioengine_rw); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 957 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 958 | register_ioengine(&ioengine_splice); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 959 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static void fio_exit fio_netio_unregister(void) |
| 963 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 964 | unregister_ioengine(&ioengine_rw); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 965 | #ifdef FIO_HAVE_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 966 | unregister_ioengine(&ioengine_splice); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 967 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 968 | } |