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