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