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