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 | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 24 | #include "../verify.h" |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 25 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 26 | struct netio_data { |
| 27 | int listenfd; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 28 | int use_splice; |
| 29 | int pipes[2]; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 30 | struct sockaddr_in addr; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 31 | struct sockaddr_in6 addr6; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 32 | struct sockaddr_un addr_un; |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 33 | uint64_t udp_send_seq; |
| 34 | uint64_t udp_recv_seq; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 35 | }; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 36 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 37 | struct netio_options { |
| 38 | struct thread_data *td; |
| 39 | unsigned int port; |
| 40 | unsigned int proto; |
| 41 | unsigned int listen; |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 42 | unsigned int pingpong; |
Steven Noonan | 70a7878 | 2012-11-28 14:52:36 -0800 | [diff] [blame] | 43 | unsigned int nodelay; |
Shawn Bohrer | d3a623d | 2013-07-19 13:24:08 -0500 | [diff] [blame] | 44 | unsigned int ttl; |
Jens Axboe | 531e67a | 2014-10-09 11:55:16 -0600 | [diff] [blame] | 45 | unsigned int window_size; |
Jens Axboe | 5e34cea | 2014-10-09 12:05:44 -0600 | [diff] [blame] | 46 | unsigned int mss; |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 47 | char *intfc; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 50 | struct udp_close_msg { |
| 51 | uint32_t magic; |
| 52 | uint32_t cmd; |
| 53 | }; |
| 54 | |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 55 | struct udp_seq { |
| 56 | uint64_t magic; |
| 57 | uint64_t seq; |
| 58 | }; |
| 59 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 60 | enum { |
| 61 | FIO_LINK_CLOSE = 0x89, |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 62 | FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b, |
| 63 | FIO_LINK_OPEN = 0x98, |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 64 | FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL, |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 65 | |
| 66 | FIO_TYPE_TCP = 1, |
| 67 | FIO_TYPE_UDP = 2, |
| 68 | FIO_TYPE_UNIX = 3, |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 69 | FIO_TYPE_TCP_V6 = 4, |
| 70 | FIO_TYPE_UDP_V6 = 5, |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 73 | static int str_hostname_cb(void *data, const char *input); |
| 74 | static struct fio_option options[] = { |
| 75 | { |
| 76 | .name = "hostname", |
Jens Axboe | e8b0e95 | 2012-03-19 14:37:08 +0100 | [diff] [blame] | 77 | .lname = "net engine hostname", |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 78 | .type = FIO_OPT_STR_STORE, |
| 79 | .cb = str_hostname_cb, |
| 80 | .help = "Hostname for net IO engine", |
Jens Axboe | e90a0ad | 2013-04-10 19:43:59 +0200 | [diff] [blame] | 81 | .category = FIO_OPT_C_ENGINE, |
| 82 | .group = FIO_OPT_G_NETIO, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 83 | }, |
| 84 | { |
| 85 | .name = "port", |
Jens Axboe | e8b0e95 | 2012-03-19 14:37:08 +0100 | [diff] [blame] | 86 | .lname = "net engine port", |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 87 | .type = FIO_OPT_INT, |
| 88 | .off1 = offsetof(struct netio_options, port), |
| 89 | .minval = 1, |
| 90 | .maxval = 65535, |
| 91 | .help = "Port to use for TCP or UDP net connections", |
Jens Axboe | e90a0ad | 2013-04-10 19:43:59 +0200 | [diff] [blame] | 92 | .category = FIO_OPT_C_ENGINE, |
| 93 | .group = FIO_OPT_G_NETIO, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 94 | }, |
| 95 | { |
| 96 | .name = "protocol", |
Jens Axboe | e8b0e95 | 2012-03-19 14:37:08 +0100 | [diff] [blame] | 97 | .lname = "net engine protocol", |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 98 | .alias = "proto", |
| 99 | .type = FIO_OPT_STR, |
| 100 | .off1 = offsetof(struct netio_options, proto), |
| 101 | .help = "Network protocol to use", |
| 102 | .def = "tcp", |
| 103 | .posval = { |
| 104 | { .ival = "tcp", |
| 105 | .oval = FIO_TYPE_TCP, |
| 106 | .help = "Transmission Control Protocol", |
| 107 | }, |
Jens Axboe | eb23231 | 2014-01-24 18:59:15 -0800 | [diff] [blame] | 108 | #ifdef CONFIG_IPV6 |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 109 | { .ival = "tcpv6", |
| 110 | .oval = FIO_TYPE_TCP_V6, |
| 111 | .help = "Transmission Control Protocol V6", |
| 112 | }, |
Jens Axboe | eb23231 | 2014-01-24 18:59:15 -0800 | [diff] [blame] | 113 | #endif |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 114 | { .ival = "udp", |
| 115 | .oval = FIO_TYPE_UDP, |
Bruce Cran | f5cc3d0 | 2012-10-10 08:17:44 -0600 | [diff] [blame] | 116 | .help = "User Datagram Protocol", |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 117 | }, |
Jens Axboe | eb23231 | 2014-01-24 18:59:15 -0800 | [diff] [blame] | 118 | #ifdef CONFIG_IPV6 |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 119 | { .ival = "udpv6", |
| 120 | .oval = FIO_TYPE_UDP_V6, |
| 121 | .help = "User Datagram Protocol V6", |
| 122 | }, |
Jens Axboe | eb23231 | 2014-01-24 18:59:15 -0800 | [diff] [blame] | 123 | #endif |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 124 | { .ival = "unix", |
| 125 | .oval = FIO_TYPE_UNIX, |
| 126 | .help = "UNIX domain socket", |
| 127 | }, |
| 128 | }, |
Jens Axboe | e90a0ad | 2013-04-10 19:43:59 +0200 | [diff] [blame] | 129 | .category = FIO_OPT_C_ENGINE, |
| 130 | .group = FIO_OPT_G_NETIO, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 131 | }, |
Jens Axboe | 1eafa37 | 2013-01-31 10:19:51 +0100 | [diff] [blame] | 132 | #ifdef CONFIG_TCP_NODELAY |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 133 | { |
Steven Noonan | 70a7878 | 2012-11-28 14:52:36 -0800 | [diff] [blame] | 134 | .name = "nodelay", |
| 135 | .type = FIO_OPT_BOOL, |
| 136 | .off1 = offsetof(struct netio_options, nodelay), |
| 137 | .help = "Use TCP_NODELAY on TCP connections", |
Jens Axboe | e90a0ad | 2013-04-10 19:43:59 +0200 | [diff] [blame] | 138 | .category = FIO_OPT_C_ENGINE, |
| 139 | .group = FIO_OPT_G_NETIO, |
Steven Noonan | 70a7878 | 2012-11-28 14:52:36 -0800 | [diff] [blame] | 140 | }, |
Jens Axboe | 1eafa37 | 2013-01-31 10:19:51 +0100 | [diff] [blame] | 141 | #endif |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 142 | { |
| 143 | .name = "listen", |
Jens Axboe | e8b0e95 | 2012-03-19 14:37:08 +0100 | [diff] [blame] | 144 | .lname = "net engine listen", |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 145 | .type = FIO_OPT_STR_SET, |
| 146 | .off1 = offsetof(struct netio_options, listen), |
| 147 | .help = "Listen for incoming TCP connections", |
Jens Axboe | e90a0ad | 2013-04-10 19:43:59 +0200 | [diff] [blame] | 148 | .category = FIO_OPT_C_ENGINE, |
| 149 | .group = FIO_OPT_G_NETIO, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 150 | }, |
| 151 | { |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 152 | .name = "pingpong", |
| 153 | .type = FIO_OPT_STR_SET, |
| 154 | .off1 = offsetof(struct netio_options, pingpong), |
| 155 | .help = "Ping-pong IO requests", |
Jens Axboe | e90a0ad | 2013-04-10 19:43:59 +0200 | [diff] [blame] | 156 | .category = FIO_OPT_C_ENGINE, |
| 157 | .group = FIO_OPT_G_NETIO, |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 158 | }, |
| 159 | { |
Shawn Bohrer | b93b6a2 | 2013-07-19 13:24:07 -0500 | [diff] [blame] | 160 | .name = "interface", |
| 161 | .lname = "net engine interface", |
| 162 | .type = FIO_OPT_STR_STORE, |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 163 | .off1 = offsetof(struct netio_options, intfc), |
Shawn Bohrer | b93b6a2 | 2013-07-19 13:24:07 -0500 | [diff] [blame] | 164 | .help = "Network interface to use", |
| 165 | .category = FIO_OPT_C_ENGINE, |
| 166 | .group = FIO_OPT_G_NETIO, |
| 167 | }, |
| 168 | { |
Shawn Bohrer | d3a623d | 2013-07-19 13:24:08 -0500 | [diff] [blame] | 169 | .name = "ttl", |
| 170 | .lname = "net engine multicast ttl", |
| 171 | .type = FIO_OPT_INT, |
| 172 | .off1 = offsetof(struct netio_options, ttl), |
| 173 | .def = "1", |
| 174 | .minval = 0, |
| 175 | .help = "Time-to-live value for outgoing UDP multicast packets", |
| 176 | .category = FIO_OPT_C_ENGINE, |
| 177 | .group = FIO_OPT_G_NETIO, |
| 178 | }, |
Jens Axboe | 531e67a | 2014-10-09 11:55:16 -0600 | [diff] [blame] | 179 | #ifdef CONFIG_NET_WINDOWSIZE |
| 180 | { |
| 181 | .name = "window_size", |
| 182 | .lname = "Window Size", |
| 183 | .type = FIO_OPT_INT, |
| 184 | .off1 = offsetof(struct netio_options, window_size), |
| 185 | .minval = 0, |
| 186 | .help = "Set socket buffer window size", |
| 187 | .category = FIO_OPT_C_ENGINE, |
| 188 | .group = FIO_OPT_G_NETIO, |
| 189 | }, |
| 190 | #endif |
Jens Axboe | 5e34cea | 2014-10-09 12:05:44 -0600 | [diff] [blame] | 191 | #ifdef CONFIG_NET_MSS |
| 192 | { |
| 193 | .name = "mss", |
| 194 | .lname = "Maximum segment size", |
| 195 | .type = FIO_OPT_INT, |
| 196 | .off1 = offsetof(struct netio_options, mss), |
| 197 | .minval = 0, |
| 198 | .help = "Set TCP maximum segment size", |
| 199 | .category = FIO_OPT_C_ENGINE, |
| 200 | .group = FIO_OPT_G_NETIO, |
| 201 | }, |
| 202 | #endif |
Shawn Bohrer | d3a623d | 2013-07-19 13:24:08 -0500 | [diff] [blame] | 203 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 204 | .name = NULL, |
| 205 | }, |
| 206 | }; |
| 207 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 208 | static inline int is_udp(struct netio_options *o) |
| 209 | { |
| 210 | return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6; |
| 211 | } |
| 212 | |
| 213 | static inline int is_tcp(struct netio_options *o) |
| 214 | { |
| 215 | return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6; |
| 216 | } |
| 217 | |
| 218 | static inline int is_ipv6(struct netio_options *o) |
| 219 | { |
| 220 | return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6; |
| 221 | } |
| 222 | |
Jens Axboe | 531e67a | 2014-10-09 11:55:16 -0600 | [diff] [blame] | 223 | static int set_window_size(struct thread_data *td, int fd) |
| 224 | { |
| 225 | #ifdef CONFIG_NET_WINDOWSIZE |
| 226 | struct netio_options *o = td->eo; |
| 227 | unsigned int wss; |
| 228 | int snd, rcv, ret; |
| 229 | |
| 230 | if (!o->window_size) |
| 231 | return 0; |
| 232 | |
| 233 | rcv = o->listen || o->pingpong; |
| 234 | snd = !o->listen || o->pingpong; |
| 235 | wss = o->window_size; |
| 236 | ret = 0; |
| 237 | |
| 238 | if (rcv) { |
| 239 | ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss, |
| 240 | sizeof(wss)); |
| 241 | if (ret < 0) |
| 242 | td_verror(td, errno, "rcvbuf window size"); |
| 243 | } |
| 244 | if (snd && !ret) { |
| 245 | ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss, |
| 246 | sizeof(wss)); |
| 247 | if (ret < 0) |
| 248 | td_verror(td, errno, "sndbuf window size"); |
| 249 | } |
| 250 | |
| 251 | return ret; |
| 252 | #else |
| 253 | td_verror(td, -EINVAL, "setsockopt window size"); |
| 254 | return -1; |
| 255 | #endif |
| 256 | } |
| 257 | |
Jens Axboe | 5e34cea | 2014-10-09 12:05:44 -0600 | [diff] [blame] | 258 | static int set_mss(struct thread_data *td, int fd) |
| 259 | { |
| 260 | #ifdef CONFIG_NET_MSS |
| 261 | struct netio_options *o = td->eo; |
| 262 | unsigned int mss; |
| 263 | int ret; |
| 264 | |
| 265 | if (!o->mss || !is_tcp(o)) |
| 266 | return 0; |
| 267 | |
| 268 | mss = o->mss; |
| 269 | ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss, |
| 270 | sizeof(mss)); |
| 271 | if (ret < 0) |
| 272 | td_verror(td, errno, "setsockopt TCP_MAXSEG"); |
| 273 | |
| 274 | return ret; |
| 275 | #else |
| 276 | td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG"); |
| 277 | return -1; |
| 278 | #endif |
| 279 | } |
| 280 | |
| 281 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 282 | /* |
| 283 | * Return -1 for error and 'nr events' for a positive number |
| 284 | * of events |
| 285 | */ |
| 286 | static int poll_wait(struct thread_data *td, int fd, short events) |
| 287 | { |
| 288 | struct pollfd pfd; |
| 289 | int ret; |
| 290 | |
| 291 | while (!td->terminate) { |
| 292 | pfd.fd = fd; |
| 293 | pfd.events = events; |
| 294 | ret = poll(&pfd, 1, -1); |
| 295 | if (ret < 0) { |
| 296 | if (errno == EINTR) |
Jens Axboe | d5b388a | 2009-01-19 12:38:27 +0100 | [diff] [blame] | 297 | break; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 298 | |
| 299 | td_verror(td, errno, "poll"); |
| 300 | return -1; |
| 301 | } else if (!ret) |
| 302 | continue; |
| 303 | |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | if (pfd.revents & events) |
| 308 | return 1; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 309 | |
| 310 | return -1; |
| 311 | } |
| 312 | |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 313 | static int fio_netio_is_multicast(const char *mcaddr) |
| 314 | { |
| 315 | in_addr_t addr = inet_network(mcaddr); |
| 316 | if (addr == -1) |
| 317 | return 0; |
| 318 | |
| 319 | if (inet_network("224.0.0.0") <= addr && |
| 320 | inet_network("239.255.255.255") >= addr) |
| 321 | return 1; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 327 | static int fio_netio_prep(struct thread_data *td, struct io_u *io_u) |
| 328 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 329 | struct netio_options *o = td->eo; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 330 | |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 331 | /* |
| 332 | * Make sure we don't see spurious reads to a receiver, and vice versa |
| 333 | */ |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 334 | if (is_tcp(o)) |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 335 | return 0; |
| 336 | |
| 337 | if ((o->listen && io_u->ddir == DDIR_WRITE) || |
| 338 | (!o->listen && io_u->ddir == DDIR_READ)) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 339 | td_verror(td, EINVAL, "bad direction"); |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 340 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 341 | } |
Bruce Cran | 3f457be | 2012-10-10 13:37:41 +0100 | [diff] [blame] | 342 | |
Jens Axboe | f85ac25 | 2008-03-01 18:09:49 +0100 | [diff] [blame] | 343 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 346 | #ifdef CONFIG_LINUX_SPLICE |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 347 | static int splice_io_u(int fdin, int fdout, unsigned int len) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 348 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 349 | int bytes = 0; |
| 350 | |
| 351 | while (len) { |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 352 | int ret = splice(fdin, NULL, fdout, NULL, len, 0); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 353 | |
| 354 | if (ret < 0) { |
| 355 | if (!bytes) |
| 356 | bytes = ret; |
| 357 | |
| 358 | break; |
| 359 | } else if (!ret) |
| 360 | break; |
| 361 | |
| 362 | bytes += ret; |
Jens Axboe | f657a2f | 2007-06-22 20:40:10 +0200 | [diff] [blame] | 363 | len -= ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | return bytes; |
| 367 | } |
| 368 | |
| 369 | /* |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 370 | * Receive bytes from a socket and fill them into the internal pipe |
| 371 | */ |
| 372 | static int splice_in(struct thread_data *td, struct io_u *io_u) |
| 373 | { |
| 374 | struct netio_data *nd = td->io_ops->data; |
| 375 | |
| 376 | return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen); |
| 377 | } |
| 378 | |
| 379 | /* |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 380 | * Transmit 'len' bytes from the internal pipe |
| 381 | */ |
| 382 | static int splice_out(struct thread_data *td, struct io_u *io_u, |
| 383 | unsigned int len) |
| 384 | { |
| 385 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 386 | |
| 387 | return splice_io_u(nd->pipes[0], io_u->file->fd, len); |
| 388 | } |
| 389 | |
| 390 | static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len) |
| 391 | { |
| 392 | struct iovec iov = { |
| 393 | .iov_base = io_u->xfer_buf, |
| 394 | .iov_len = len, |
| 395 | }; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 396 | int bytes = 0; |
| 397 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 398 | while (iov.iov_len) { |
| 399 | int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 400 | |
| 401 | if (ret < 0) { |
| 402 | if (!bytes) |
| 403 | bytes = ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 404 | break; |
| 405 | } else if (!ret) |
| 406 | break; |
| 407 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 408 | iov.iov_len -= ret; |
| 409 | iov.iov_base += ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 410 | bytes += ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | return bytes; |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 414 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* |
| 418 | * vmsplice() pipe to io_u buffer |
| 419 | */ |
| 420 | static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u, |
| 421 | unsigned int len) |
| 422 | { |
| 423 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 424 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 425 | return vmsplice_io_u(io_u, nd->pipes[0], len); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* |
| 429 | * vmsplice() io_u to pipe |
| 430 | */ |
| 431 | static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u) |
| 432 | { |
| 433 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 434 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 435 | 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] | 436 | } |
| 437 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 438 | /* |
| 439 | * splice receive - transfer socket data into a pipe using splice, then map |
| 440 | * that pipe data into the io_u using vmsplice. |
| 441 | */ |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 442 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
| 443 | { |
| 444 | int ret; |
| 445 | |
| 446 | ret = splice_in(td, io_u); |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 447 | if (ret > 0) |
| 448 | return vmsplice_io_u_out(td, io_u, ret); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 449 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 450 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 453 | /* |
| 454 | * splice transmit - map data from the io_u into a pipe by using vmsplice, |
| 455 | * then transfer that pipe to a socket using splice. |
| 456 | */ |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 457 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
| 458 | { |
| 459 | int ret; |
| 460 | |
| 461 | ret = vmsplice_io_u_in(td, io_u); |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 462 | if (ret > 0) |
| 463 | return splice_out(td, io_u, ret); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 464 | |
Jens Axboe | cd963e1 | 2007-06-24 21:41:46 +0200 | [diff] [blame] | 465 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 466 | } |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 467 | #else |
| 468 | static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u) |
| 469 | { |
Jens Axboe | af8771b | 2008-05-30 22:58:28 +0200 | [diff] [blame] | 470 | errno = EOPNOTSUPP; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u) |
| 475 | { |
Jens Axboe | af8771b | 2008-05-30 22:58:28 +0200 | [diff] [blame] | 476 | errno = EOPNOTSUPP; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 477 | return -1; |
| 478 | } |
| 479 | #endif |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 480 | |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 481 | static void store_udp_seq(struct netio_data *nd, struct io_u *io_u) |
| 482 | { |
| 483 | struct udp_seq *us; |
| 484 | |
| 485 | us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us); |
| 486 | us->magic = cpu_to_le64(FIO_UDP_SEQ_MAGIC); |
| 487 | us->seq = cpu_to_le64(nd->udp_send_seq++); |
| 488 | } |
| 489 | |
Jens Axboe | 67e149c | 2014-10-09 13:27:44 -0600 | [diff] [blame^] | 490 | static void verify_udp_seq(struct thread_data *td, struct netio_data *nd, |
| 491 | struct io_u *io_u) |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 492 | { |
| 493 | struct udp_seq *us; |
| 494 | uint64_t seq; |
| 495 | |
| 496 | us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us); |
| 497 | if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC) |
| 498 | return; |
| 499 | |
| 500 | seq = le64_to_cpu(us->seq); |
| 501 | |
| 502 | if (seq != nd->udp_recv_seq) |
Jens Axboe | 67e149c | 2014-10-09 13:27:44 -0600 | [diff] [blame^] | 503 | td->ts.drop_io_u[io_u->ddir] += seq - nd->udp_recv_seq; |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 504 | |
| 505 | nd->udp_recv_seq = seq + 1; |
| 506 | } |
| 507 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 508 | static int fio_netio_send(struct thread_data *td, struct io_u *io_u) |
| 509 | { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 510 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 511 | struct netio_options *o = td->eo; |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 512 | int ret, flags = 0; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 513 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 514 | do { |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 515 | if (is_udp(o)) { |
Jens Axboe | 10aa136 | 2014-04-01 21:10:36 -0600 | [diff] [blame] | 516 | const struct sockaddr *to; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 517 | socklen_t len; |
| 518 | |
| 519 | if (is_ipv6(o)) { |
| 520 | to = (struct sockaddr *) &nd->addr6; |
| 521 | len = sizeof(nd->addr6); |
| 522 | } else { |
| 523 | to = (struct sockaddr *) &nd->addr; |
| 524 | len = sizeof(nd->addr); |
| 525 | } |
Jens Axboe | 62b3892 | 2009-05-11 10:37:33 +0200 | [diff] [blame] | 526 | |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 527 | if (td->o.verify == VERIFY_NONE) |
| 528 | store_udp_seq(nd, io_u); |
| 529 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 530 | ret = sendto(io_u->file->fd, io_u->xfer_buf, |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 531 | io_u->xfer_buflen, flags, to, len); |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 532 | } else { |
| 533 | /* |
| 534 | * if we are going to write more, set MSG_MORE |
| 535 | */ |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 536 | #ifdef MSG_MORE |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 537 | if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < |
| 538 | td->o.size) && !o->pingpong) |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 539 | flags |= MSG_MORE; |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 540 | #endif |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 541 | ret = send(io_u->file->fd, io_u->xfer_buf, |
| 542 | io_u->xfer_buflen, flags); |
| 543 | } |
| 544 | if (ret > 0) |
| 545 | break; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 546 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 547 | ret = poll_wait(td, io_u->file->fd, POLLOUT); |
| 548 | if (ret <= 0) |
| 549 | break; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 550 | } while (1); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | static int is_udp_close(struct io_u *io_u, int len) |
| 556 | { |
| 557 | struct udp_close_msg *msg; |
| 558 | |
| 559 | if (len != sizeof(struct udp_close_msg)) |
| 560 | return 0; |
| 561 | |
| 562 | msg = io_u->xfer_buf; |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 563 | if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC) |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 564 | return 0; |
| 565 | if (ntohl(msg->cmd) != FIO_LINK_CLOSE) |
| 566 | return 0; |
| 567 | |
| 568 | return 1; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 571 | 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] | 572 | { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 573 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 574 | struct netio_options *o = td->eo; |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 575 | int ret, flags = 0; |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 576 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 577 | do { |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 578 | if (is_udp(o)) { |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 579 | struct sockaddr *from; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 580 | socklen_t l, *len = &l; |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 581 | |
| 582 | if (o->listen) { |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 583 | if (!is_ipv6(o)) { |
| 584 | from = (struct sockaddr *) &nd->addr; |
| 585 | *len = sizeof(nd->addr); |
| 586 | } else { |
| 587 | from = (struct sockaddr *) &nd->addr6; |
| 588 | *len = sizeof(nd->addr6); |
| 589 | } |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 590 | } else { |
| 591 | from = NULL; |
| 592 | len = NULL; |
| 593 | } |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 594 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 595 | ret = recvfrom(io_u->file->fd, io_u->xfer_buf, |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 596 | io_u->xfer_buflen, flags, from, len); |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 597 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 598 | if (is_udp_close(io_u, ret)) { |
| 599 | td->done = 1; |
| 600 | return 0; |
| 601 | } |
| 602 | } else { |
| 603 | ret = recv(io_u->file->fd, io_u->xfer_buf, |
| 604 | io_u->xfer_buflen, flags); |
| 605 | } |
| 606 | if (ret > 0) |
| 607 | break; |
Jens Axboe | 7d988f6 | 2012-11-29 19:57:35 +0100 | [diff] [blame] | 608 | else if (!ret && (flags & MSG_WAITALL)) |
| 609 | break; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 610 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 611 | ret = poll_wait(td, io_u->file->fd, POLLIN); |
| 612 | if (ret <= 0) |
| 613 | break; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 614 | flags |= MSG_WAITALL; |
| 615 | } while (1); |
| 616 | |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 617 | if (is_udp(o) && td->o.verify == VERIFY_NONE) |
Jens Axboe | 67e149c | 2014-10-09 13:27:44 -0600 | [diff] [blame^] | 618 | verify_udp_seq(td, nd, io_u); |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 619 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 620 | return ret; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 623 | static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u, |
| 624 | enum fio_ddir ddir) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 625 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 626 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 627 | struct netio_options *o = td->eo; |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 628 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 629 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 630 | if (ddir == DDIR_WRITE) { |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 631 | if (!nd->use_splice || is_udp(o) || |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 632 | o->proto == FIO_TYPE_UNIX) |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 633 | ret = fio_netio_send(td, io_u); |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 634 | else |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 635 | ret = fio_netio_splice_out(td, io_u); |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 636 | } else if (ddir == DDIR_READ) { |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 637 | if (!nd->use_splice || is_udp(o) || |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 638 | o->proto == FIO_TYPE_UNIX) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 639 | ret = fio_netio_recv(td, io_u); |
| 640 | else |
| 641 | ret = fio_netio_splice_in(td, io_u); |
Jens Axboe | d4f12dd | 2007-02-08 12:59:02 +0100 | [diff] [blame] | 642 | } else |
Jens Axboe | 7a6499d | 2007-02-07 09:35:29 +0100 | [diff] [blame] | 643 | ret = 0; /* must be a SYNC */ |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 644 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 645 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | 0ecdd7f | 2014-10-08 17:04:29 -0600 | [diff] [blame] | 646 | if (ret > 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 647 | io_u->resid = io_u->xfer_buflen - ret; |
| 648 | io_u->error = 0; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 649 | return FIO_Q_COMPLETED; |
Jens Axboe | 0ecdd7f | 2014-10-08 17:04:29 -0600 | [diff] [blame] | 650 | } else if (!ret) |
| 651 | return FIO_Q_BUSY; |
| 652 | else { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 653 | int err = errno; |
| 654 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 655 | if (ddir == DDIR_WRITE && err == EMSGSIZE) |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 656 | return FIO_Q_BUSY; |
| 657 | |
| 658 | io_u->error = err; |
| 659 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 660 | } |
| 661 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 662 | if (io_u->error) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 663 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 664 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 665 | return FIO_Q_COMPLETED; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 666 | } |
| 667 | |
Jens Axboe | 6f73a7f | 2012-11-30 09:59:20 +0100 | [diff] [blame] | 668 | static int fio_netio_queue(struct thread_data *td, struct io_u *io_u) |
| 669 | { |
| 670 | struct netio_options *o = td->eo; |
| 671 | int ret; |
| 672 | |
| 673 | fio_ro_check(td, io_u); |
| 674 | |
| 675 | ret = __fio_netio_queue(td, io_u, io_u->ddir); |
| 676 | if (!o->pingpong || ret != FIO_Q_COMPLETED) |
| 677 | return ret; |
| 678 | |
| 679 | /* |
| 680 | * For ping-pong mode, receive or send reply as needed |
| 681 | */ |
| 682 | if (td_read(td) && io_u->ddir == DDIR_READ) |
| 683 | ret = __fio_netio_queue(td, io_u, DDIR_WRITE); |
| 684 | else if (td_write(td) && io_u->ddir == DDIR_WRITE) |
| 685 | ret = __fio_netio_queue(td, io_u, DDIR_READ); |
| 686 | |
| 687 | return ret; |
| 688 | } |
| 689 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 690 | 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] | 691 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 692 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 693 | struct netio_options *o = td->eo; |
Jens Axboe | 6264c7a | 2013-02-28 08:24:23 +0100 | [diff] [blame] | 694 | int type, domain; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 695 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 696 | if (o->proto == FIO_TYPE_TCP) { |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 697 | domain = AF_INET; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 698 | type = SOCK_STREAM; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 699 | } else if (o->proto == FIO_TYPE_TCP_V6) { |
| 700 | domain = AF_INET6; |
| 701 | type = SOCK_STREAM; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 702 | } else if (o->proto == FIO_TYPE_UDP) { |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 703 | domain = AF_INET; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 704 | type = SOCK_DGRAM; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 705 | } else if (o->proto == FIO_TYPE_UDP_V6) { |
| 706 | domain = AF_INET6; |
| 707 | type = SOCK_DGRAM; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 708 | } else if (o->proto == FIO_TYPE_UNIX) { |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 709 | domain = AF_UNIX; |
| 710 | type = SOCK_STREAM; |
| 711 | } else { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 712 | log_err("fio: bad network type %d\n", o->proto); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 713 | f->fd = -1; |
| 714 | return 1; |
| 715 | } |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 716 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 717 | f->fd = socket(domain, type, 0); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 718 | if (f->fd < 0) { |
| 719 | td_verror(td, errno, "socket"); |
| 720 | return 1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Jens Axboe | 1eafa37 | 2013-01-31 10:19:51 +0100 | [diff] [blame] | 723 | #ifdef CONFIG_TCP_NODELAY |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 724 | if (o->nodelay && is_tcp(o)) { |
Jens Axboe | 6264c7a | 2013-02-28 08:24:23 +0100 | [diff] [blame] | 725 | int optval = 1; |
| 726 | |
Jens Axboe | 26e594a | 2013-01-30 21:52:37 +0100 | [diff] [blame] | 727 | 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] | 728 | log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno)); |
| 729 | return 1; |
| 730 | } |
| 731 | } |
Jens Axboe | 1eafa37 | 2013-01-31 10:19:51 +0100 | [diff] [blame] | 732 | #endif |
Steven Noonan | 70a7878 | 2012-11-28 14:52:36 -0800 | [diff] [blame] | 733 | |
Jens Axboe | 531e67a | 2014-10-09 11:55:16 -0600 | [diff] [blame] | 734 | if (set_window_size(td, f->fd)) { |
| 735 | close(f->fd); |
| 736 | return 1; |
| 737 | } |
Jens Axboe | 5e34cea | 2014-10-09 12:05:44 -0600 | [diff] [blame] | 738 | if (set_mss(td, f->fd)) { |
| 739 | close(f->fd); |
| 740 | return 1; |
| 741 | } |
Jens Axboe | 531e67a | 2014-10-09 11:55:16 -0600 | [diff] [blame] | 742 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 743 | if (is_udp(o)) { |
Shawn Bohrer | d3a623d | 2013-07-19 13:24:08 -0500 | [diff] [blame] | 744 | if (!fio_netio_is_multicast(td->o.filename)) |
| 745 | return 0; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 746 | if (is_ipv6(o)) { |
| 747 | log_err("fio: multicast not supported on IPv6\n"); |
| 748 | close(f->fd); |
| 749 | return 1; |
| 750 | } |
Shawn Bohrer | d3a623d | 2013-07-19 13:24:08 -0500 | [diff] [blame] | 751 | |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 752 | if (o->intfc) { |
Shawn Bohrer | b93b6a2 | 2013-07-19 13:24:07 -0500 | [diff] [blame] | 753 | struct in_addr interface_addr; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 754 | |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 755 | if (inet_aton(o->intfc, &interface_addr) == 0) { |
Shawn Bohrer | b93b6a2 | 2013-07-19 13:24:07 -0500 | [diff] [blame] | 756 | log_err("fio: interface not valid interface IP\n"); |
| 757 | close(f->fd); |
| 758 | return 1; |
| 759 | } |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 760 | if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) { |
Shawn Bohrer | b93b6a2 | 2013-07-19 13:24:07 -0500 | [diff] [blame] | 761 | td_verror(td, errno, "setsockopt IP_MULTICAST_IF"); |
| 762 | close(f->fd); |
| 763 | return 1; |
| 764 | } |
| 765 | } |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 766 | if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) { |
Shawn Bohrer | d3a623d | 2013-07-19 13:24:08 -0500 | [diff] [blame] | 767 | td_verror(td, errno, "setsockopt IP_MULTICAST_TTL"); |
| 768 | close(f->fd); |
| 769 | return 1; |
| 770 | } |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 771 | return 0; |
Shawn Bohrer | b93b6a2 | 2013-07-19 13:24:07 -0500 | [diff] [blame] | 772 | } else if (o->proto == FIO_TYPE_TCP) { |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 773 | socklen_t len = sizeof(nd->addr); |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 774 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 775 | if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) { |
| 776 | td_verror(td, errno, "connect"); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 777 | close(f->fd); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 778 | return 1; |
| 779 | } |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 780 | } else if (o->proto == FIO_TYPE_TCP_V6) { |
| 781 | socklen_t len = sizeof(nd->addr6); |
| 782 | |
| 783 | if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) { |
| 784 | td_verror(td, errno, "connect"); |
| 785 | close(f->fd); |
| 786 | return 1; |
| 787 | } |
| 788 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 789 | } else { |
| 790 | struct sockaddr_un *addr = &nd->addr_un; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 791 | socklen_t len; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 792 | |
| 793 | len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1; |
| 794 | |
| 795 | if (connect(f->fd, (struct sockaddr *) addr, len) < 0) { |
| 796 | td_verror(td, errno, "connect"); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 797 | close(f->fd); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 798 | return 1; |
| 799 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 805 | 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] | 806 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 807 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 808 | struct netio_options *o = td->eo; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 809 | socklen_t socklen; |
Jens Axboe | 6264c7a | 2013-02-28 08:24:23 +0100 | [diff] [blame] | 810 | int state; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 811 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 812 | if (is_udp(o)) { |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 813 | f->fd = nd->listenfd; |
| 814 | return 0; |
| 815 | } |
| 816 | |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 817 | state = td->runstate; |
| 818 | td_set_runstate(td, TD_SETTING_UP); |
| 819 | |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 820 | log_info("fio: waiting for connection\n"); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 821 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 822 | if (poll_wait(td, nd->listenfd, POLLIN) < 0) |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 823 | goto err; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 824 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 825 | if (o->proto == FIO_TYPE_TCP) { |
| 826 | socklen = sizeof(nd->addr); |
| 827 | f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen); |
| 828 | } else { |
| 829 | socklen = sizeof(nd->addr6); |
| 830 | f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen); |
| 831 | } |
| 832 | |
Jens Axboe | 371d456 | 2009-01-19 10:17:06 +0100 | [diff] [blame] | 833 | if (f->fd < 0) { |
| 834 | td_verror(td, errno, "accept"); |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 835 | goto err; |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 836 | } |
| 837 | |
Jens Axboe | 1eafa37 | 2013-01-31 10:19:51 +0100 | [diff] [blame] | 838 | #ifdef CONFIG_TCP_NODELAY |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 839 | if (o->nodelay && is_tcp(o)) { |
Jens Axboe | 6264c7a | 2013-02-28 08:24:23 +0100 | [diff] [blame] | 840 | int optval = 1; |
| 841 | |
Jens Axboe | 26e594a | 2013-01-30 21:52:37 +0100 | [diff] [blame] | 842 | 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] | 843 | log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno)); |
| 844 | return 1; |
| 845 | } |
| 846 | } |
Jens Axboe | 1eafa37 | 2013-01-31 10:19:51 +0100 | [diff] [blame] | 847 | #endif |
Steven Noonan | 70a7878 | 2012-11-28 14:52:36 -0800 | [diff] [blame] | 848 | |
Jens Axboe | 0cae16f | 2012-11-30 16:22:31 +0100 | [diff] [blame] | 849 | reset_all_stats(td); |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 850 | td_set_runstate(td, state); |
Jens Axboe | 5fdd124 | 2007-02-11 04:00:37 +0100 | [diff] [blame] | 851 | return 0; |
Jens Axboe | 859088d | 2012-11-29 20:02:50 +0100 | [diff] [blame] | 852 | err: |
| 853 | td_set_runstate(td, state); |
| 854 | return 1; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 857 | static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f) |
| 858 | { |
| 859 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 860 | struct netio_options *o = td->eo; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 861 | struct udp_close_msg msg; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 862 | struct sockaddr *to; |
| 863 | socklen_t len; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 864 | int ret; |
| 865 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 866 | if (is_ipv6(o)) { |
| 867 | to = (struct sockaddr *) &nd->addr6; |
| 868 | len = sizeof(nd->addr6); |
| 869 | } else { |
| 870 | to = (struct sockaddr *) &nd->addr; |
| 871 | len = sizeof(nd->addr); |
| 872 | } |
| 873 | |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 874 | msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC); |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 875 | msg.cmd = htonl(FIO_LINK_CLOSE); |
| 876 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 877 | ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len); |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 878 | if (ret < 0) |
| 879 | td_verror(td, errno, "sendto udp link close"); |
| 880 | } |
| 881 | |
| 882 | static int fio_netio_close_file(struct thread_data *td, struct fio_file *f) |
| 883 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 884 | struct netio_options *o = td->eo; |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 885 | |
| 886 | /* |
| 887 | * If this is an UDP connection, notify the receiver that we are |
| 888 | * closing down the link |
| 889 | */ |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 890 | if (is_udp(o)) |
Jens Axboe | 664fb3b | 2009-01-19 13:26:36 +0100 | [diff] [blame] | 891 | fio_netio_udp_close(td, f); |
| 892 | |
| 893 | return generic_close_file(td, f); |
| 894 | } |
| 895 | |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 896 | static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f) |
| 897 | { |
| 898 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 899 | struct netio_options *o = td->eo; |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 900 | struct udp_close_msg msg; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 901 | struct sockaddr *to; |
| 902 | socklen_t len; |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 903 | int ret; |
| 904 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 905 | if (is_ipv6(o)) { |
| 906 | len = sizeof(nd->addr6); |
| 907 | to = (struct sockaddr *) &nd->addr6; |
| 908 | } else { |
| 909 | len = sizeof(nd->addr); |
| 910 | to = (struct sockaddr *) &nd->addr; |
| 911 | } |
| 912 | |
Jens Axboe | 1f81991 | 2013-01-23 17:21:41 -0700 | [diff] [blame] | 913 | ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len); |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 914 | if (ret < 0) { |
Shawn Bohrer | ee7062f | 2013-07-19 13:24:09 -0500 | [diff] [blame] | 915 | td_verror(td, errno, "recvfrom udp link open"); |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC || |
| 920 | ntohl(msg.cmd) != FIO_LINK_OPEN) { |
| 921 | log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic), |
| 922 | ntohl(msg.cmd)); |
| 923 | return -1; |
| 924 | } |
| 925 | |
Jens Axboe | da91d75 | 2014-10-09 13:10:14 -0600 | [diff] [blame] | 926 | fio_gettime(&td->start, NULL); |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f) |
| 931 | { |
| 932 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 933 | struct netio_options *o = td->eo; |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 934 | struct udp_close_msg msg; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 935 | struct sockaddr *to; |
| 936 | socklen_t len; |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 937 | int ret; |
| 938 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 939 | if (is_ipv6(o)) { |
| 940 | len = sizeof(nd->addr6); |
| 941 | to = (struct sockaddr *) &nd->addr6; |
| 942 | } else { |
| 943 | len = sizeof(nd->addr); |
| 944 | to = (struct sockaddr *) &nd->addr; |
| 945 | } |
| 946 | |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 947 | msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC); |
| 948 | msg.cmd = htonl(FIO_LINK_OPEN); |
| 949 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 950 | ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len); |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 951 | if (ret < 0) { |
| 952 | td_verror(td, errno, "sendto udp link open"); |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static int fio_netio_open_file(struct thread_data *td, struct fio_file *f) |
| 960 | { |
| 961 | int ret; |
| 962 | struct netio_options *o = td->eo; |
| 963 | |
| 964 | if (o->listen) |
| 965 | ret = fio_netio_accept(td, f); |
| 966 | else |
| 967 | ret = fio_netio_connect(td, f); |
| 968 | |
| 969 | if (ret) { |
| 970 | f->fd = -1; |
| 971 | return ret; |
| 972 | } |
| 973 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 974 | if (is_udp(o)) { |
Jens Axboe | b96d243 | 2012-11-30 08:27:46 +0100 | [diff] [blame] | 975 | if (td_write(td)) |
| 976 | ret = fio_netio_udp_send_open(td, f); |
| 977 | else { |
| 978 | int state; |
| 979 | |
| 980 | state = td->runstate; |
| 981 | td_set_runstate(td, TD_SETTING_UP); |
| 982 | ret = fio_netio_udp_recv_open(td, f); |
| 983 | td_set_runstate(td, state); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | if (ret) |
| 988 | fio_netio_close_file(td, f); |
| 989 | |
| 990 | return ret; |
| 991 | } |
| 992 | |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 993 | static int fio_fill_addr(struct thread_data *td, const char *host, int af, |
| 994 | void *dst, struct addrinfo **res) |
| 995 | { |
| 996 | struct netio_options *o = td->eo; |
| 997 | struct addrinfo hints; |
| 998 | int ret; |
| 999 | |
| 1000 | if (inet_pton(af, host, dst)) |
| 1001 | return 0; |
| 1002 | |
| 1003 | memset(&hints, 0, sizeof(hints)); |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 1004 | |
| 1005 | if (is_tcp(o)) |
| 1006 | hints.ai_socktype = SOCK_STREAM; |
| 1007 | else |
| 1008 | hints.ai_socktype = SOCK_DGRAM; |
| 1009 | |
Jens Axboe | b1b1be2 | 2014-01-23 20:41:33 -0800 | [diff] [blame] | 1010 | if (is_ipv6(o)) |
| 1011 | hints.ai_family = AF_INET6; |
| 1012 | else |
| 1013 | hints.ai_family = AF_INET; |
| 1014 | |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 1015 | ret = getaddrinfo(host, NULL, &hints, res); |
| 1016 | if (ret) { |
| 1017 | int e = EINVAL; |
| 1018 | char str[128]; |
| 1019 | |
| 1020 | if (ret == EAI_SYSTEM) |
| 1021 | e = errno; |
| 1022 | |
| 1023 | snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret)); |
| 1024 | td_verror(td, e, str); |
| 1025 | return 1; |
| 1026 | } |
| 1027 | |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1031 | static int fio_netio_setup_connect_inet(struct thread_data *td, |
| 1032 | const char *host, unsigned short port) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1033 | { |
| 1034 | struct netio_data *nd = td->io_ops->data; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1035 | struct netio_options *o = td->eo; |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 1036 | struct addrinfo *res = NULL; |
| 1037 | void *dst, *src; |
| 1038 | int af, len; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1039 | |
Jens Axboe | 166dce4 | 2012-11-29 14:35:33 +0100 | [diff] [blame] | 1040 | if (!host) { |
| 1041 | log_err("fio: connect with no host to connect to.\n"); |
| 1042 | if (td_read(td)) |
| 1043 | log_err("fio: did you forget to set 'listen'?\n"); |
| 1044 | |
| 1045 | td_verror(td, EINVAL, "no hostname= set"); |
| 1046 | return 1; |
| 1047 | } |
| 1048 | |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 1049 | nd->addr.sin_family = AF_INET; |
| 1050 | nd->addr.sin_port = htons(port); |
| 1051 | nd->addr6.sin6_family = AF_INET6; |
| 1052 | nd->addr6.sin6_port = htons(port); |
| 1053 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1054 | if (is_ipv6(o)) { |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 1055 | af = AF_INET6; |
Jens Axboe | 68631b3 | 2014-02-25 13:43:38 -0800 | [diff] [blame] | 1056 | dst = &nd->addr6.sin6_addr; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1057 | } else { |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 1058 | af = AF_INET; |
Jens Axboe | 68631b3 | 2014-02-25 13:43:38 -0800 | [diff] [blame] | 1059 | dst = &nd->addr.sin_addr; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1060 | } |
| 1061 | |
Jens Axboe | 0b78334 | 2014-01-23 20:19:17 -0800 | [diff] [blame] | 1062 | if (fio_fill_addr(td, host, af, dst, &res)) |
| 1063 | return 1; |
| 1064 | |
| 1065 | if (!res) |
| 1066 | return 0; |
| 1067 | |
| 1068 | if (is_ipv6(o)) { |
| 1069 | len = sizeof(nd->addr6.sin6_addr); |
| 1070 | src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr; |
| 1071 | } else { |
| 1072 | len = sizeof(nd->addr.sin_addr); |
| 1073 | src = &((struct sockaddr_in *) res->ai_addr)->sin_addr; |
| 1074 | } |
| 1075 | |
| 1076 | memcpy(dst, src, len); |
| 1077 | freeaddrinfo(res); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1078 | return 0; |
| 1079 | } |
| 1080 | |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1081 | static int fio_netio_setup_connect_unix(struct thread_data *td, |
| 1082 | const char *path) |
| 1083 | { |
| 1084 | struct netio_data *nd = td->io_ops->data; |
| 1085 | struct sockaddr_un *soun = &nd->addr_un; |
| 1086 | |
| 1087 | soun->sun_family = AF_UNIX; |
Jens Axboe | 4b159fa | 2014-04-14 08:49:04 -0600 | [diff] [blame] | 1088 | memset(soun->sun_path, 0, sizeof(soun->sun_path)); |
| 1089 | strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1090 | return 0; |
| 1091 | } |
| 1092 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1093 | static int fio_netio_setup_connect(struct thread_data *td) |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1094 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1095 | struct netio_options *o = td->eo; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1096 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1097 | if (is_udp(o) || is_tcp(o)) |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1098 | return fio_netio_setup_connect_inet(td, td->o.filename,o->port); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1099 | else |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1100 | return fio_netio_setup_connect_unix(td, td->o.filename); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path) |
| 1104 | { |
| 1105 | struct netio_data *nd = td->io_ops->data; |
| 1106 | struct sockaddr_un *addr = &nd->addr_un; |
| 1107 | mode_t mode; |
| 1108 | int len, fd; |
| 1109 | |
| 1110 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1111 | if (fd < 0) { |
| 1112 | log_err("fio: socket: %s\n", strerror(errno)); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | |
| 1116 | mode = umask(000); |
| 1117 | |
| 1118 | memset(addr, 0, sizeof(*addr)); |
| 1119 | addr->sun_family = AF_UNIX; |
Jens Axboe | 4b159fa | 2014-04-14 08:49:04 -0600 | [diff] [blame] | 1120 | strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1121 | unlink(path); |
| 1122 | |
| 1123 | len = sizeof(addr->sun_family) + strlen(path) + 1; |
| 1124 | |
| 1125 | if (bind(fd, (struct sockaddr *) addr, len) < 0) { |
| 1126 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1127 | close(fd); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1128 | return -1; |
| 1129 | } |
| 1130 | |
| 1131 | umask(mode); |
| 1132 | nd->listenfd = fd; |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
| 1136 | 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] | 1137 | { |
| 1138 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1139 | struct netio_options *o = td->eo; |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 1140 | struct ip_mreq mr; |
| 1141 | struct sockaddr_in sin; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1142 | struct sockaddr *saddr; |
| 1143 | int fd, opt, type, domain; |
| 1144 | socklen_t len; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1145 | |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 1146 | memset(&sin, 0, sizeof(sin)); |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 1147 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1148 | if (o->proto == FIO_TYPE_TCP) { |
| 1149 | type = SOCK_STREAM; |
| 1150 | domain = AF_INET; |
| 1151 | } else if (o->proto == FIO_TYPE_TCP_V6) { |
| 1152 | type = SOCK_STREAM; |
| 1153 | domain = AF_INET6; |
| 1154 | } else if (o->proto == FIO_TYPE_UDP) { |
| 1155 | type = SOCK_DGRAM; |
| 1156 | domain = AF_INET; |
| 1157 | } else if (o->proto == FIO_TYPE_UDP_V6) { |
| 1158 | type = SOCK_DGRAM; |
| 1159 | domain = AF_INET6; |
| 1160 | } else { |
| 1161 | log_err("fio: unknown proto %d\n", o->proto); |
| 1162 | return 1; |
| 1163 | } |
| 1164 | |
| 1165 | fd = socket(domain, type, 0); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1166 | if (fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 1167 | td_verror(td, errno, "socket"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1168 | return 1; |
| 1169 | } |
| 1170 | |
| 1171 | opt = 1; |
Jens Axboe | 26e594a | 2013-01-30 21:52:37 +0100 | [diff] [blame] | 1172 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 1173 | td_verror(td, errno, "setsockopt"); |
Shawn Bohrer | 4a93dec | 2013-07-19 13:24:10 -0500 | [diff] [blame] | 1174 | close(fd); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1175 | return 1; |
| 1176 | } |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 1177 | #ifdef SO_REUSEPORT |
Jens Axboe | 26e594a | 2013-01-30 21:52:37 +0100 | [diff] [blame] | 1178 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 1179 | td_verror(td, errno, "setsockopt"); |
Shawn Bohrer | 4a93dec | 2013-07-19 13:24:10 -0500 | [diff] [blame] | 1180 | close(fd); |
Jens Axboe | 6bedbfa | 2007-02-07 09:54:40 +0100 | [diff] [blame] | 1181 | return 1; |
| 1182 | } |
| 1183 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1184 | |
Jens Axboe | 531e67a | 2014-10-09 11:55:16 -0600 | [diff] [blame] | 1185 | if (set_window_size(td, fd)) { |
| 1186 | close(fd); |
| 1187 | return 1; |
| 1188 | } |
Jens Axboe | 5e34cea | 2014-10-09 12:05:44 -0600 | [diff] [blame] | 1189 | if (set_mss(td, fd)) { |
| 1190 | close(fd); |
| 1191 | return 1; |
| 1192 | } |
Jens Axboe | 531e67a | 2014-10-09 11:55:16 -0600 | [diff] [blame] | 1193 | |
Jens Axboe | 6611e9c | 2014-01-24 07:36:43 -0800 | [diff] [blame] | 1194 | if (td->o.filename) { |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1195 | if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) { |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 1196 | log_err("fio: hostname not valid for non-multicast inbound network IO\n"); |
| 1197 | close(fd); |
| 1198 | return 1; |
| 1199 | } |
Jens Axboe | 6611e9c | 2014-01-24 07:36:43 -0800 | [diff] [blame] | 1200 | if (is_ipv6(o)) { |
| 1201 | log_err("fio: IPv6 not supported for multicast network IO"); |
| 1202 | close(fd); |
| 1203 | return 1; |
| 1204 | } |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 1205 | |
| 1206 | inet_aton(td->o.filename, &sin.sin_addr); |
| 1207 | |
| 1208 | mr.imr_multiaddr = sin.sin_addr; |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 1209 | if (o->intfc) { |
| 1210 | if (inet_aton(o->intfc, &mr.imr_interface) == 0) { |
Shawn Bohrer | b93b6a2 | 2013-07-19 13:24:07 -0500 | [diff] [blame] | 1211 | log_err("fio: interface not valid interface IP\n"); |
| 1212 | close(fd); |
| 1213 | return 1; |
| 1214 | } |
| 1215 | } else { |
| 1216 | mr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 1217 | } |
Jens Axboe | 6611e9c | 2014-01-24 07:36:43 -0800 | [diff] [blame] | 1218 | |
Bruce Cran | f16b740 | 2013-10-08 15:05:27 +0100 | [diff] [blame] | 1219 | if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) { |
Shawn Bohrer | b511c9a | 2013-07-19 13:24:06 -0500 | [diff] [blame] | 1220 | td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP"); |
| 1221 | close(fd); |
| 1222 | return 1; |
| 1223 | } |
| 1224 | } |
| 1225 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1226 | if (!is_ipv6(o)) { |
| 1227 | saddr = (struct sockaddr *) &nd->addr; |
| 1228 | len = sizeof(nd->addr); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1229 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1230 | nd->addr.sin_family = AF_INET; |
| 1231 | nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY); |
| 1232 | nd->addr.sin_port = htons(port); |
| 1233 | } else { |
| 1234 | saddr = (struct sockaddr *) &nd->addr6; |
| 1235 | len = sizeof(nd->addr6); |
| 1236 | |
| 1237 | nd->addr6.sin6_family = AF_INET6; |
Jens Axboe | 66f7a56 | 2014-04-14 11:57:05 -0600 | [diff] [blame] | 1238 | nd->addr6.sin6_addr = in6addr_any; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1239 | nd->addr6.sin6_port = htons(port); |
| 1240 | } |
| 1241 | |
| 1242 | if (bind(fd, saddr, len) < 0) { |
Jens Axboe | d19cedd | 2014-04-11 11:29:50 -0600 | [diff] [blame] | 1243 | close(fd); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 1244 | td_verror(td, errno, "bind"); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1245 | return 1; |
| 1246 | } |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1247 | |
| 1248 | nd->listenfd = fd; |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1252 | static int fio_netio_setup_listen(struct thread_data *td) |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1253 | { |
| 1254 | struct netio_data *nd = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1255 | struct netio_options *o = td->eo; |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1256 | int ret; |
| 1257 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1258 | if (is_udp(o) || is_tcp(o)) |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1259 | ret = fio_netio_setup_listen_inet(td, o->port); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1260 | else |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1261 | ret = fio_netio_setup_listen_unix(td, td->o.filename); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1262 | |
| 1263 | if (ret) |
| 1264 | return ret; |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1265 | if (is_udp(o)) |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1266 | return 0; |
| 1267 | |
| 1268 | if (listen(nd->listenfd, 10) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 1269 | td_verror(td, errno, "listen"); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1270 | nd->listenfd = -1; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1271 | return 1; |
| 1272 | } |
| 1273 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1274 | return 0; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 1277 | static int fio_netio_init(struct thread_data *td) |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1278 | { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1279 | struct netio_options *o = td->eo; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1280 | int ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1281 | |
Bruce Cran | 3f457be | 2012-10-10 13:37:41 +0100 | [diff] [blame] | 1282 | #ifdef WIN32 |
| 1283 | WSADATA wsd; |
| 1284 | WSAStartup(MAKEWORD(2,2), &wsd); |
| 1285 | #endif |
| 1286 | |
Jens Axboe | 16d55aa | 2007-05-22 09:21:37 +0200 | [diff] [blame] | 1287 | if (td_random(td)) { |
| 1288 | log_err("fio: network IO can't be random\n"); |
| 1289 | return 1; |
| 1290 | } |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1291 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1292 | if (o->proto == FIO_TYPE_UNIX && o->port) { |
| 1293 | log_err("fio: network IO port not valid with unix socket\n"); |
| 1294 | return 1; |
| 1295 | } else if (o->proto != FIO_TYPE_UNIX && !o->port) { |
| 1296 | log_err("fio: network IO requires port for tcp or udp\n"); |
| 1297 | return 1; |
Jens Axboe | 414c2a3 | 2009-01-16 13:21:15 +0100 | [diff] [blame] | 1298 | } |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1299 | |
Jens Axboe | 49ccb8c | 2014-01-23 16:49:37 -0800 | [diff] [blame] | 1300 | if (!is_tcp(o)) { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1301 | if (o->listen) { |
Jens Axboe | 9b98606 | 2011-12-19 08:57:18 +0100 | [diff] [blame] | 1302 | log_err("fio: listen only valid for TCP proto IO\n"); |
| 1303 | return 1; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1304 | } |
| 1305 | if (td_rw(td)) { |
Jens Axboe | 9b98606 | 2011-12-19 08:57:18 +0100 | [diff] [blame] | 1306 | log_err("fio: datagram network connections must be" |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1307 | " read OR write\n"); |
Jens Axboe | 9b98606 | 2011-12-19 08:57:18 +0100 | [diff] [blame] | 1308 | return 1; |
| 1309 | } |
| 1310 | if (o->proto == FIO_TYPE_UNIX && !td->o.filename) { |
| 1311 | log_err("fio: UNIX sockets need host/filename\n"); |
| 1312 | return 1; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1313 | } |
| 1314 | o->listen = td_read(td); |
| 1315 | } |
| 1316 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1317 | if (o->listen) |
| 1318 | ret = fio_netio_setup_listen(td); |
Jens Axboe | 0fd666b | 2011-10-06 20:08:53 +0200 | [diff] [blame] | 1319 | else |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1320 | ret = fio_netio_setup_connect(td); |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1321 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 1322 | return ret; |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1323 | } |
| 1324 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1325 | static void fio_netio_cleanup(struct thread_data *td) |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 1326 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1327 | struct netio_data *nd = td->io_ops->data; |
| 1328 | |
| 1329 | if (nd) { |
Jens Axboe | 64b24cd | 2007-06-24 21:28:39 +0200 | [diff] [blame] | 1330 | if (nd->listenfd != -1) |
| 1331 | close(nd->listenfd); |
| 1332 | if (nd->pipes[0] != -1) |
| 1333 | close(nd->pipes[0]); |
| 1334 | if (nd->pipes[1] != -1) |
| 1335 | close(nd->pipes[1]); |
| 1336 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1337 | free(nd); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
| 1341 | static int fio_netio_setup(struct thread_data *td) |
| 1342 | { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 1343 | struct netio_data *nd; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 1344 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1345 | if (!td->files_index) { |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 1346 | add_file(td, td->o.filename ?: "net", 0, 0); |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1347 | td->o.nr_files = td->o.nr_files ?: 1; |
Jens Axboe | b53f2c5 | 2014-04-08 21:07:12 -0600 | [diff] [blame] | 1348 | td->o.open_files++; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1349 | } |
| 1350 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 1351 | if (!td->io_ops->data) { |
| 1352 | nd = malloc(sizeof(*nd));; |
| 1353 | |
| 1354 | memset(nd, 0, sizeof(*nd)); |
| 1355 | nd->listenfd = -1; |
Jens Axboe | 64b24cd | 2007-06-24 21:28:39 +0200 | [diff] [blame] | 1356 | nd->pipes[0] = nd->pipes[1] = -1; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 1357 | td->io_ops->data = nd; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
Jens Axboe | 9bec88e | 2007-03-02 08:55:48 +0100 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 1363 | static void fio_netio_terminate(struct thread_data *td) |
| 1364 | { |
Jens Axboe | e5f7caa | 2014-10-08 14:14:05 -0600 | [diff] [blame] | 1365 | kill(td->pid, SIGTERM); |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 1366 | } |
| 1367 | |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 1368 | #ifdef CONFIG_LINUX_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 1369 | static int fio_netio_setup_splice(struct thread_data *td) |
| 1370 | { |
| 1371 | struct netio_data *nd; |
| 1372 | |
| 1373 | fio_netio_setup(td); |
| 1374 | |
| 1375 | nd = td->io_ops->data; |
| 1376 | if (nd) { |
| 1377 | if (pipe(nd->pipes) < 0) |
| 1378 | return 1; |
| 1379 | |
| 1380 | nd->use_splice = 1; |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | return 1; |
| 1385 | } |
| 1386 | |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1387 | static struct ioengine_ops ioengine_splice = { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1388 | .name = "netsplice", |
| 1389 | .version = FIO_IOOPS_VERSION, |
| 1390 | .prep = fio_netio_prep, |
| 1391 | .queue = fio_netio_queue, |
| 1392 | .setup = fio_netio_setup_splice, |
| 1393 | .init = fio_netio_init, |
| 1394 | .cleanup = fio_netio_cleanup, |
| 1395 | .open_file = fio_netio_open_file, |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 1396 | .close_file = fio_netio_close_file, |
| 1397 | .terminate = fio_netio_terminate, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1398 | .options = options, |
| 1399 | .option_struct_size = sizeof(struct netio_options), |
| 1400 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 1401 | FIO_PIPEIO, |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1402 | }; |
| 1403 | #endif |
| 1404 | |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 1405 | static struct ioengine_ops ioengine_rw = { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1406 | .name = "net", |
| 1407 | .version = FIO_IOOPS_VERSION, |
| 1408 | .prep = fio_netio_prep, |
| 1409 | .queue = fio_netio_queue, |
| 1410 | .setup = fio_netio_setup, |
| 1411 | .init = fio_netio_init, |
| 1412 | .cleanup = fio_netio_cleanup, |
| 1413 | .open_file = fio_netio_open_file, |
| 1414 | .close_file = fio_netio_close_file, |
Jens Axboe | 36d80bc | 2012-11-30 21:46:06 +0100 | [diff] [blame] | 1415 | .terminate = fio_netio_terminate, |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1416 | .options = options, |
| 1417 | .option_struct_size = sizeof(struct netio_options), |
| 1418 | .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR | |
Steven Noonan | ad705bc | 2013-04-08 15:05:25 -0700 | [diff] [blame] | 1419 | FIO_PIPEIO | FIO_BIT_BASED, |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1420 | }; |
| 1421 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 1422 | static int str_hostname_cb(void *data, const char *input) |
| 1423 | { |
| 1424 | struct netio_options *o = data; |
| 1425 | |
| 1426 | if (o->td->o.filename) |
| 1427 | free(o->td->o.filename); |
| 1428 | o->td->o.filename = strdup(input); |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1432 | static void fio_init fio_netio_register(void) |
| 1433 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 1434 | register_ioengine(&ioengine_rw); |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 1435 | #ifdef CONFIG_LINUX_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 1436 | register_ioengine(&ioengine_splice); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1437 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static void fio_exit fio_netio_unregister(void) |
| 1441 | { |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 1442 | unregister_ioengine(&ioengine_rw); |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 1443 | #ifdef CONFIG_LINUX_SPLICE |
Jens Axboe | 9cce02e | 2007-06-22 15:42:21 +0200 | [diff] [blame] | 1444 | unregister_ioengine(&ioengine_splice); |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 1445 | #endif |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 1446 | } |