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