blob: 4ba4a5f80b82edea9e30566d7fc48e5b309a900a [file] [log] [blame]
Jens Axboeed92ac02007-02-06 14:43:52 +01001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * net engine
3 *
4 * IO engine that reads/writes to/from sockets.
5 *
Jens Axboeed92ac02007-02-06 14:43:52 +01006 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
Steven Noonan842805f2012-12-01 08:16:55 +000010#include <signal.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010011#include <errno.h>
12#include <assert.h>
13#include <netinet/in.h>
Steven Noonan70a78782012-11-28 14:52:36 -080014#include <netinet/tcp.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010015#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboe5fdd1242007-02-11 04:00:37 +010017#include <sys/poll.h>
Jens Axboe72920562008-06-02 12:30:06 +020018#include <sys/types.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020019#include <sys/stat.h>
Jens Axboe72920562008-06-02 12:30:06 +020020#include <sys/socket.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020021#include <sys/un.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010022
23#include "../fio.h"
Jens Axboeed92ac02007-02-06 14:43:52 +010024
Jens Axboeb5af8292007-03-08 12:43:13 +010025struct netio_data {
26 int listenfd;
Jens Axboe9cce02e2007-06-22 15:42:21 +020027 int use_splice;
28 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010029 struct sockaddr_in addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -080030 struct sockaddr_in6 addr6;
Jens Axboe0fd666b2011-10-06 20:08:53 +020031 struct sockaddr_un addr_un;
Jens Axboeb5af8292007-03-08 12:43:13 +010032};
Jens Axboeed92ac02007-02-06 14:43:52 +010033
Steven Langde890a12011-11-09 14:03:34 +010034struct netio_options {
35 struct thread_data *td;
36 unsigned int port;
37 unsigned int proto;
38 unsigned int listen;
Jens Axboe6f73a7f2012-11-30 09:59:20 +010039 unsigned int pingpong;
Steven Noonan70a78782012-11-28 14:52:36 -080040 unsigned int nodelay;
Shawn Bohrerd3a623d2013-07-19 13:24:08 -050041 unsigned int ttl;
Bruce Cranf16b7402013-10-08 15:05:27 +010042 char *intfc;
Steven Langde890a12011-11-09 14:03:34 +010043};
44
Jens Axboe664fb3b2009-01-19 13:26:36 +010045struct udp_close_msg {
46 uint32_t magic;
47 uint32_t cmd;
48};
49
50enum {
51 FIO_LINK_CLOSE = 0x89,
Jens Axboeb96d2432012-11-30 08:27:46 +010052 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
53 FIO_LINK_OPEN = 0x98,
Jens Axboe0fd666b2011-10-06 20:08:53 +020054
55 FIO_TYPE_TCP = 1,
56 FIO_TYPE_UDP = 2,
57 FIO_TYPE_UNIX = 3,
Jens Axboe49ccb8c2014-01-23 16:49:37 -080058 FIO_TYPE_TCP_V6 = 4,
59 FIO_TYPE_UDP_V6 = 5,
Jens Axboe664fb3b2009-01-19 13:26:36 +010060};
61
Steven Langde890a12011-11-09 14:03:34 +010062static int str_hostname_cb(void *data, const char *input);
63static struct fio_option options[] = {
64 {
65 .name = "hostname",
Jens Axboee8b0e952012-03-19 14:37:08 +010066 .lname = "net engine hostname",
Steven Langde890a12011-11-09 14:03:34 +010067 .type = FIO_OPT_STR_STORE,
68 .cb = str_hostname_cb,
69 .help = "Hostname for net IO engine",
Jens Axboee90a0ad2013-04-10 19:43:59 +020070 .category = FIO_OPT_C_ENGINE,
71 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010072 },
73 {
74 .name = "port",
Jens Axboee8b0e952012-03-19 14:37:08 +010075 .lname = "net engine port",
Steven Langde890a12011-11-09 14:03:34 +010076 .type = FIO_OPT_INT,
77 .off1 = offsetof(struct netio_options, port),
78 .minval = 1,
79 .maxval = 65535,
80 .help = "Port to use for TCP or UDP net connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +020081 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010083 },
84 {
85 .name = "protocol",
Jens Axboee8b0e952012-03-19 14:37:08 +010086 .lname = "net engine protocol",
Steven Langde890a12011-11-09 14:03:34 +010087 .alias = "proto",
88 .type = FIO_OPT_STR,
89 .off1 = offsetof(struct netio_options, proto),
90 .help = "Network protocol to use",
91 .def = "tcp",
92 .posval = {
93 { .ival = "tcp",
94 .oval = FIO_TYPE_TCP,
95 .help = "Transmission Control Protocol",
96 },
Jens Axboe49ccb8c2014-01-23 16:49:37 -080097 { .ival = "tcpv6",
98 .oval = FIO_TYPE_TCP_V6,
99 .help = "Transmission Control Protocol V6",
100 },
Steven Langde890a12011-11-09 14:03:34 +0100101 { .ival = "udp",
102 .oval = FIO_TYPE_UDP,
Bruce Cranf5cc3d02012-10-10 08:17:44 -0600103 .help = "User Datagram Protocol",
Steven Langde890a12011-11-09 14:03:34 +0100104 },
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800105 { .ival = "udpv6",
106 .oval = FIO_TYPE_UDP_V6,
107 .help = "User Datagram Protocol V6",
108 },
Steven Langde890a12011-11-09 14:03:34 +0100109 { .ival = "unix",
110 .oval = FIO_TYPE_UNIX,
111 .help = "UNIX domain socket",
112 },
113 },
Jens Axboee90a0ad2013-04-10 19:43:59 +0200114 .category = FIO_OPT_C_ENGINE,
115 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100116 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100117#ifdef CONFIG_TCP_NODELAY
Steven Langde890a12011-11-09 14:03:34 +0100118 {
Steven Noonan70a78782012-11-28 14:52:36 -0800119 .name = "nodelay",
120 .type = FIO_OPT_BOOL,
121 .off1 = offsetof(struct netio_options, nodelay),
122 .help = "Use TCP_NODELAY on TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200123 .category = FIO_OPT_C_ENGINE,
124 .group = FIO_OPT_G_NETIO,
Steven Noonan70a78782012-11-28 14:52:36 -0800125 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100126#endif
Steven Langde890a12011-11-09 14:03:34 +0100127 {
128 .name = "listen",
Jens Axboee8b0e952012-03-19 14:37:08 +0100129 .lname = "net engine listen",
Steven Langde890a12011-11-09 14:03:34 +0100130 .type = FIO_OPT_STR_SET,
131 .off1 = offsetof(struct netio_options, listen),
132 .help = "Listen for incoming TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200133 .category = FIO_OPT_C_ENGINE,
134 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100135 },
136 {
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100137 .name = "pingpong",
138 .type = FIO_OPT_STR_SET,
139 .off1 = offsetof(struct netio_options, pingpong),
140 .help = "Ping-pong IO requests",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200141 .category = FIO_OPT_C_ENGINE,
142 .group = FIO_OPT_G_NETIO,
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100143 },
144 {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500145 .name = "interface",
146 .lname = "net engine interface",
147 .type = FIO_OPT_STR_STORE,
Bruce Cranf16b7402013-10-08 15:05:27 +0100148 .off1 = offsetof(struct netio_options, intfc),
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500149 .help = "Network interface to use",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_NETIO,
152 },
153 {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500154 .name = "ttl",
155 .lname = "net engine multicast ttl",
156 .type = FIO_OPT_INT,
157 .off1 = offsetof(struct netio_options, ttl),
158 .def = "1",
159 .minval = 0,
160 .help = "Time-to-live value for outgoing UDP multicast packets",
161 .category = FIO_OPT_C_ENGINE,
162 .group = FIO_OPT_G_NETIO,
163 },
164 {
Steven Langde890a12011-11-09 14:03:34 +0100165 .name = NULL,
166 },
167};
168
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800169static inline int is_udp(struct netio_options *o)
170{
171 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
172}
173
174static inline int is_tcp(struct netio_options *o)
175{
176 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
177}
178
179static inline int is_ipv6(struct netio_options *o)
180{
181 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
182}
183
Jens Axboe371d4562009-01-19 10:17:06 +0100184/*
185 * Return -1 for error and 'nr events' for a positive number
186 * of events
187 */
188static int poll_wait(struct thread_data *td, int fd, short events)
189{
190 struct pollfd pfd;
191 int ret;
192
193 while (!td->terminate) {
194 pfd.fd = fd;
195 pfd.events = events;
196 ret = poll(&pfd, 1, -1);
197 if (ret < 0) {
198 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +0100199 break;
Jens Axboe371d4562009-01-19 10:17:06 +0100200
201 td_verror(td, errno, "poll");
202 return -1;
203 } else if (!ret)
204 continue;
205
206 break;
207 }
208
209 if (pfd.revents & events)
210 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +0100211
212 return -1;
213}
214
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500215static int fio_netio_is_multicast(const char *mcaddr)
216{
217 in_addr_t addr = inet_network(mcaddr);
218 if (addr == -1)
219 return 0;
220
221 if (inet_network("224.0.0.0") <= addr &&
222 inet_network("239.255.255.255") >= addr)
223 return 1;
224
225 return 0;
226}
227
228
Jens Axboeed92ac02007-02-06 14:43:52 +0100229static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
230{
Steven Langde890a12011-11-09 14:03:34 +0100231 struct netio_options *o = td->eo;
Jens Axboeed92ac02007-02-06 14:43:52 +0100232
Jens Axboe7a6499d2007-02-07 09:35:29 +0100233 /*
234 * Make sure we don't see spurious reads to a receiver, and vice versa
235 */
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800236 if (is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +0100237 return 0;
238
239 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
240 (!o->listen && io_u->ddir == DDIR_READ)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100241 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +0100242 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100243 }
Bruce Cran3f457be2012-10-10 13:37:41 +0100244
Jens Axboef85ac252008-03-01 18:09:49 +0100245 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100246}
247
Jens Axboe67bf9822013-01-10 11:23:19 +0100248#ifdef CONFIG_LINUX_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +0200249static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200250{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200251 int bytes = 0;
252
253 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200254 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200255
256 if (ret < 0) {
257 if (!bytes)
258 bytes = ret;
259
260 break;
261 } else if (!ret)
262 break;
263
264 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200265 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200266 }
267
268 return bytes;
269}
270
271/*
Jens Axboecd963e12007-06-24 21:41:46 +0200272 * Receive bytes from a socket and fill them into the internal pipe
273 */
274static int splice_in(struct thread_data *td, struct io_u *io_u)
275{
276 struct netio_data *nd = td->io_ops->data;
277
278 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
279}
280
281/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200282 * Transmit 'len' bytes from the internal pipe
283 */
284static int splice_out(struct thread_data *td, struct io_u *io_u,
285 unsigned int len)
286{
287 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200288
289 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
290}
291
292static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
293{
294 struct iovec iov = {
295 .iov_base = io_u->xfer_buf,
296 .iov_len = len,
297 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200298 int bytes = 0;
299
Jens Axboecd963e12007-06-24 21:41:46 +0200300 while (iov.iov_len) {
301 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200302
303 if (ret < 0) {
304 if (!bytes)
305 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200306 break;
307 } else if (!ret)
308 break;
309
Jens Axboecd963e12007-06-24 21:41:46 +0200310 iov.iov_len -= ret;
311 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200312 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200313 }
314
315 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200316
Jens Axboe9cce02e2007-06-22 15:42:21 +0200317}
318
319/*
320 * vmsplice() pipe to io_u buffer
321 */
322static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
323 unsigned int len)
324{
325 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200326
Jens Axboecd963e12007-06-24 21:41:46 +0200327 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200328}
329
330/*
331 * vmsplice() io_u to pipe
332 */
333static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
334{
335 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200336
Jens Axboecd963e12007-06-24 21:41:46 +0200337 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200338}
339
Jens Axboecd963e12007-06-24 21:41:46 +0200340/*
341 * splice receive - transfer socket data into a pipe using splice, then map
342 * that pipe data into the io_u using vmsplice.
343 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200344static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
345{
346 int ret;
347
348 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200349 if (ret > 0)
350 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200351
Jens Axboecd963e12007-06-24 21:41:46 +0200352 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200353}
354
Jens Axboecd963e12007-06-24 21:41:46 +0200355/*
356 * splice transmit - map data from the io_u into a pipe by using vmsplice,
357 * then transfer that pipe to a socket using splice.
358 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200359static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
360{
361 int ret;
362
363 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200364 if (ret > 0)
365 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200366
Jens Axboecd963e12007-06-24 21:41:46 +0200367 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200368}
Jens Axboe5921e802008-05-30 15:02:38 +0200369#else
370static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
371{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200372 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200373 return -1;
374}
375
376static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
377{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200378 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200379 return -1;
380}
381#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200382
383static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
384{
Jens Axboe414c2a32009-01-16 13:21:15 +0100385 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100386 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100387 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100388
Jens Axboe664fb3b2009-01-19 13:26:36 +0100389 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800390 if (is_udp(o)) {
391 struct sockaddr *to;
392 socklen_t len;
393
394 if (is_ipv6(o)) {
395 to = (struct sockaddr *) &nd->addr6;
396 len = sizeof(nd->addr6);
397 } else {
398 to = (struct sockaddr *) &nd->addr;
399 len = sizeof(nd->addr);
400 }
Jens Axboe62b38922009-05-11 10:37:33 +0200401
Jens Axboe664fb3b2009-01-19 13:26:36 +0100402 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800403 io_u->xfer_buflen, flags, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100404 } else {
405 /*
406 * if we are going to write more, set MSG_MORE
407 */
Jens Axboe5921e802008-05-30 15:02:38 +0200408#ifdef MSG_MORE
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100409 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
410 td->o.size) && !o->pingpong)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100411 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200412#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100413 ret = send(io_u->file->fd, io_u->xfer_buf,
414 io_u->xfer_buflen, flags);
415 }
416 if (ret > 0)
417 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200418
Jens Axboe664fb3b2009-01-19 13:26:36 +0100419 ret = poll_wait(td, io_u->file->fd, POLLOUT);
420 if (ret <= 0)
421 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100422 } while (1);
423
424 return ret;
425}
426
427static int is_udp_close(struct io_u *io_u, int len)
428{
429 struct udp_close_msg *msg;
430
431 if (len != sizeof(struct udp_close_msg))
432 return 0;
433
434 msg = io_u->xfer_buf;
Jens Axboeb96d2432012-11-30 08:27:46 +0100435 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100436 return 0;
437 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
438 return 0;
439
440 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200441}
442
Jens Axboe414c2a32009-01-16 13:21:15 +0100443static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200444{
Jens Axboe414c2a32009-01-16 13:21:15 +0100445 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100446 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100447 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100448
Jens Axboe664fb3b2009-01-19 13:26:36 +0100449 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800450 if (is_udp(o)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500451 struct sockaddr *from;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800452 socklen_t l, *len = &l;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500453
454 if (o->listen) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800455 if (!is_ipv6(o)) {
456 from = (struct sockaddr *) &nd->addr;
457 *len = sizeof(nd->addr);
458 } else {
459 from = (struct sockaddr *) &nd->addr6;
460 *len = sizeof(nd->addr6);
461 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500462 } else {
463 from = NULL;
464 len = NULL;
465 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200466
Jens Axboe664fb3b2009-01-19 13:26:36 +0100467 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500468 io_u->xfer_buflen, flags, from, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100469 if (is_udp_close(io_u, ret)) {
470 td->done = 1;
471 return 0;
472 }
473 } else {
474 ret = recv(io_u->file->fd, io_u->xfer_buf,
475 io_u->xfer_buflen, flags);
476 }
477 if (ret > 0)
478 break;
Jens Axboe7d988f62012-11-29 19:57:35 +0100479 else if (!ret && (flags & MSG_WAITALL))
480 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100481
Jens Axboe664fb3b2009-01-19 13:26:36 +0100482 ret = poll_wait(td, io_u->file->fd, POLLIN);
483 if (ret <= 0)
484 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100485 flags |= MSG_WAITALL;
486 } while (1);
487
488 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200489}
490
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100491static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
492 enum fio_ddir ddir)
Jens Axboeed92ac02007-02-06 14:43:52 +0100493{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200494 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100495 struct netio_options *o = td->eo;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200496 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100497
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100498 if (ddir == DDIR_WRITE) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800499 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100500 o->proto == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200501 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200502 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100503 ret = fio_netio_splice_out(td, io_u);
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100504 } else if (ddir == DDIR_READ) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800505 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100506 o->proto == FIO_TYPE_UNIX)
Jens Axboe414c2a32009-01-16 13:21:15 +0100507 ret = fio_netio_recv(td, io_u);
508 else
509 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100510 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100511 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100512
Jens Axboecec6b552007-02-06 20:15:38 +0100513 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100514 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100515 io_u->resid = io_u->xfer_buflen - ret;
516 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100517 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100518 } else {
519 int err = errno;
520
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100521 if (ddir == DDIR_WRITE && err == EMSGSIZE)
Jens Axboe414c2a32009-01-16 13:21:15 +0100522 return FIO_Q_BUSY;
523
524 io_u->error = err;
525 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100526 }
527
Jens Axboe36167d82007-02-18 05:41:31 +0100528 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100529 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100530
Jens Axboe36167d82007-02-18 05:41:31 +0100531 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100532}
533
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100534static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
535{
536 struct netio_options *o = td->eo;
537 int ret;
538
539 fio_ro_check(td, io_u);
540
541 ret = __fio_netio_queue(td, io_u, io_u->ddir);
542 if (!o->pingpong || ret != FIO_Q_COMPLETED)
543 return ret;
544
545 /*
546 * For ping-pong mode, receive or send reply as needed
547 */
548 if (td_read(td) && io_u->ddir == DDIR_READ)
549 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
550 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
551 ret = __fio_netio_queue(td, io_u, DDIR_READ);
552
553 return ret;
554}
555
Jens Axboeb5af8292007-03-08 12:43:13 +0100556static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100557{
Jens Axboeb5af8292007-03-08 12:43:13 +0100558 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100559 struct netio_options *o = td->eo;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100560 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100561
Steven Langde890a12011-11-09 14:03:34 +0100562 if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200563 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100564 type = SOCK_STREAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800565 } else if (o->proto == FIO_TYPE_TCP_V6) {
566 domain = AF_INET6;
567 type = SOCK_STREAM;
Steven Langde890a12011-11-09 14:03:34 +0100568 } else if (o->proto == FIO_TYPE_UDP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200569 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100570 type = SOCK_DGRAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800571 } else if (o->proto == FIO_TYPE_UDP_V6) {
572 domain = AF_INET6;
573 type = SOCK_DGRAM;
Steven Langde890a12011-11-09 14:03:34 +0100574 } else if (o->proto == FIO_TYPE_UNIX) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200575 domain = AF_UNIX;
576 type = SOCK_STREAM;
577 } else {
Steven Langde890a12011-11-09 14:03:34 +0100578 log_err("fio: bad network type %d\n", o->proto);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200579 f->fd = -1;
580 return 1;
581 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100582
Jens Axboe0fd666b2011-10-06 20:08:53 +0200583 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100584 if (f->fd < 0) {
585 td_verror(td, errno, "socket");
586 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100587 }
588
Jens Axboe1eafa372013-01-31 10:19:51 +0100589#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800590 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100591 int optval = 1;
592
Jens Axboe26e594a2013-01-30 21:52:37 +0100593 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800594 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
595 return 1;
596 }
597 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100598#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800599
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800600 if (is_udp(o)) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500601 if (!fio_netio_is_multicast(td->o.filename))
602 return 0;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800603 if (is_ipv6(o)) {
604 log_err("fio: multicast not supported on IPv6\n");
605 close(f->fd);
606 return 1;
607 }
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500608
Bruce Cranf16b7402013-10-08 15:05:27 +0100609 if (o->intfc) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500610 struct in_addr interface_addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800611
Bruce Cranf16b7402013-10-08 15:05:27 +0100612 if (inet_aton(o->intfc, &interface_addr) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500613 log_err("fio: interface not valid interface IP\n");
614 close(f->fd);
615 return 1;
616 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100617 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500618 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
619 close(f->fd);
620 return 1;
621 }
622 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100623 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500624 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
625 close(f->fd);
626 return 1;
627 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100628 return 0;
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500629 } else if (o->proto == FIO_TYPE_TCP) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100630 socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100631
Jens Axboe0fd666b2011-10-06 20:08:53 +0200632 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
633 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200634 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200635 return 1;
636 }
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800637 } else if (o->proto == FIO_TYPE_TCP_V6) {
638 socklen_t len = sizeof(nd->addr6);
639
640 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
641 td_verror(td, errno, "connect");
642 close(f->fd);
643 return 1;
644 }
645
Jens Axboe0fd666b2011-10-06 20:08:53 +0200646 } else {
647 struct sockaddr_un *addr = &nd->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100648 socklen_t len;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200649
650 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
651
652 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
653 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200654 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200655 return 1;
656 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100657 }
658
659 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100660}
661
Jens Axboeb5af8292007-03-08 12:43:13 +0100662static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100663{
Jens Axboeb5af8292007-03-08 12:43:13 +0100664 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100665 struct netio_options *o = td->eo;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800666 socklen_t socklen;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100667 int state;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100668
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800669 if (is_udp(o)) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100670 f->fd = nd->listenfd;
671 return 0;
672 }
673
Jens Axboe859088d2012-11-29 20:02:50 +0100674 state = td->runstate;
675 td_set_runstate(td, TD_SETTING_UP);
676
Jens Axboe6d861442007-03-15 09:22:23 +0100677 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100678
Jens Axboe371d4562009-01-19 10:17:06 +0100679 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
Jens Axboe859088d2012-11-29 20:02:50 +0100680 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100681
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800682 if (o->proto == FIO_TYPE_TCP) {
683 socklen = sizeof(nd->addr);
684 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
685 } else {
686 socklen = sizeof(nd->addr6);
687 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
688 }
689
Jens Axboe371d4562009-01-19 10:17:06 +0100690 if (f->fd < 0) {
691 td_verror(td, errno, "accept");
Jens Axboe859088d2012-11-29 20:02:50 +0100692 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100693 }
694
Jens Axboe1eafa372013-01-31 10:19:51 +0100695#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800696 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100697 int optval = 1;
698
Jens Axboe26e594a2013-01-30 21:52:37 +0100699 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800700 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
701 return 1;
702 }
703 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100704#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800705
Jens Axboe0cae16f2012-11-30 16:22:31 +0100706 reset_all_stats(td);
Jens Axboe859088d2012-11-29 20:02:50 +0100707 td_set_runstate(td, state);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100708 return 0;
Jens Axboe859088d2012-11-29 20:02:50 +0100709err:
710 td_set_runstate(td, state);
711 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100712}
713
Jens Axboe664fb3b2009-01-19 13:26:36 +0100714static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
715{
716 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800717 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100718 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800719 struct sockaddr *to;
720 socklen_t len;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100721 int ret;
722
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800723 if (is_ipv6(o)) {
724 to = (struct sockaddr *) &nd->addr6;
725 len = sizeof(nd->addr6);
726 } else {
727 to = (struct sockaddr *) &nd->addr;
728 len = sizeof(nd->addr);
729 }
730
Jens Axboeb96d2432012-11-30 08:27:46 +0100731 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100732 msg.cmd = htonl(FIO_LINK_CLOSE);
733
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800734 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100735 if (ret < 0)
736 td_verror(td, errno, "sendto udp link close");
737}
738
739static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
740{
Steven Langde890a12011-11-09 14:03:34 +0100741 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100742
743 /*
744 * If this is an UDP connection, notify the receiver that we are
745 * closing down the link
746 */
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800747 if (is_udp(o))
Jens Axboe664fb3b2009-01-19 13:26:36 +0100748 fio_netio_udp_close(td, f);
749
750 return generic_close_file(td, f);
751}
752
Jens Axboeb96d2432012-11-30 08:27:46 +0100753static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
754{
755 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800756 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100757 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800758 struct sockaddr *to;
759 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100760 int ret;
761
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800762 if (is_ipv6(o)) {
763 len = sizeof(nd->addr6);
764 to = (struct sockaddr *) &nd->addr6;
765 } else {
766 len = sizeof(nd->addr);
767 to = (struct sockaddr *) &nd->addr;
768 }
769
Jens Axboe1f819912013-01-23 17:21:41 -0700770 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100771 if (ret < 0) {
Shawn Bohreree7062f2013-07-19 13:24:09 -0500772 td_verror(td, errno, "recvfrom udp link open");
Jens Axboeb96d2432012-11-30 08:27:46 +0100773 return ret;
774 }
775
776 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
777 ntohl(msg.cmd) != FIO_LINK_OPEN) {
778 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
779 ntohl(msg.cmd));
780 return -1;
781 }
782
783 return 0;
784}
785
786static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
787{
788 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800789 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100790 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800791 struct sockaddr *to;
792 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100793 int ret;
794
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800795 if (is_ipv6(o)) {
796 len = sizeof(nd->addr6);
797 to = (struct sockaddr *) &nd->addr6;
798 } else {
799 len = sizeof(nd->addr);
800 to = (struct sockaddr *) &nd->addr;
801 }
802
Jens Axboeb96d2432012-11-30 08:27:46 +0100803 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
804 msg.cmd = htonl(FIO_LINK_OPEN);
805
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800806 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100807 if (ret < 0) {
808 td_verror(td, errno, "sendto udp link open");
809 return ret;
810 }
811
812 return 0;
813}
814
815static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
816{
817 int ret;
818 struct netio_options *o = td->eo;
819
820 if (o->listen)
821 ret = fio_netio_accept(td, f);
822 else
823 ret = fio_netio_connect(td, f);
824
825 if (ret) {
826 f->fd = -1;
827 return ret;
828 }
829
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800830 if (is_udp(o)) {
Jens Axboeb96d2432012-11-30 08:27:46 +0100831 if (td_write(td))
832 ret = fio_netio_udp_send_open(td, f);
833 else {
834 int state;
835
836 state = td->runstate;
837 td_set_runstate(td, TD_SETTING_UP);
838 ret = fio_netio_udp_recv_open(td, f);
839 td_set_runstate(td, state);
840 }
841 }
842
843 if (ret)
844 fio_netio_close_file(td, f);
845
846 return ret;
847}
848
Jens Axboe0b783342014-01-23 20:19:17 -0800849static int fio_fill_addr(struct thread_data *td, const char *host, int af,
850 void *dst, struct addrinfo **res)
851{
852 struct netio_options *o = td->eo;
853 struct addrinfo hints;
854 int ret;
855
856 if (inet_pton(af, host, dst))
857 return 0;
858
859 memset(&hints, 0, sizeof(hints));
860 hints.ai_family = AF_UNSPEC;
861
862 if (is_tcp(o))
863 hints.ai_socktype = SOCK_STREAM;
864 else
865 hints.ai_socktype = SOCK_DGRAM;
866
867 ret = getaddrinfo(host, NULL, &hints, res);
868 if (ret) {
869 int e = EINVAL;
870 char str[128];
871
872 if (ret == EAI_SYSTEM)
873 e = errno;
874
875 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
876 td_verror(td, e, str);
877 return 1;
878 }
879
880 return 0;
881}
882
Jens Axboe0fd666b2011-10-06 20:08:53 +0200883static int fio_netio_setup_connect_inet(struct thread_data *td,
884 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100885{
886 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800887 struct netio_options *o = td->eo;
Jens Axboe0b783342014-01-23 20:19:17 -0800888 struct addrinfo *res = NULL;
889 void *dst, *src;
890 int af, len;
Jens Axboeb5af8292007-03-08 12:43:13 +0100891
Jens Axboe166dce42012-11-29 14:35:33 +0100892 if (!host) {
893 log_err("fio: connect with no host to connect to.\n");
894 if (td_read(td))
895 log_err("fio: did you forget to set 'listen'?\n");
896
897 td_verror(td, EINVAL, "no hostname= set");
898 return 1;
899 }
900
Jens Axboe0b783342014-01-23 20:19:17 -0800901 nd->addr.sin_family = AF_INET;
902 nd->addr.sin_port = htons(port);
903 nd->addr6.sin6_family = AF_INET6;
904 nd->addr6.sin6_port = htons(port);
905
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800906 if (is_ipv6(o)) {
Jens Axboe0b783342014-01-23 20:19:17 -0800907 af = AF_INET6;
908 dst = &nd->addr6;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800909 } else {
Jens Axboe0b783342014-01-23 20:19:17 -0800910 af = AF_INET;
911 dst = &nd->addr;
Jens Axboeb5af8292007-03-08 12:43:13 +0100912 }
913
Jens Axboe0b783342014-01-23 20:19:17 -0800914 if (fio_fill_addr(td, host, af, dst, &res))
915 return 1;
916
917 if (!res)
918 return 0;
919
920 if (is_ipv6(o)) {
921 len = sizeof(nd->addr6.sin6_addr);
922 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
923 } else {
924 len = sizeof(nd->addr.sin_addr);
925 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
926 }
927
928 memcpy(dst, src, len);
929 freeaddrinfo(res);
Jens Axboeb5af8292007-03-08 12:43:13 +0100930 return 0;
931}
932
Jens Axboe0fd666b2011-10-06 20:08:53 +0200933static int fio_netio_setup_connect_unix(struct thread_data *td,
934 const char *path)
935{
936 struct netio_data *nd = td->io_ops->data;
937 struct sockaddr_un *soun = &nd->addr_un;
938
939 soun->sun_family = AF_UNIX;
940 strcpy(soun->sun_path, path);
941 return 0;
942}
943
Steven Langde890a12011-11-09 14:03:34 +0100944static int fio_netio_setup_connect(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200945{
Steven Langde890a12011-11-09 14:03:34 +0100946 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200947
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800948 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +0100949 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200950 else
Steven Langde890a12011-11-09 14:03:34 +0100951 return fio_netio_setup_connect_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200952}
953
954static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
955{
956 struct netio_data *nd = td->io_ops->data;
957 struct sockaddr_un *addr = &nd->addr_un;
958 mode_t mode;
959 int len, fd;
960
961 fd = socket(AF_UNIX, SOCK_STREAM, 0);
962 if (fd < 0) {
963 log_err("fio: socket: %s\n", strerror(errno));
964 return -1;
965 }
966
967 mode = umask(000);
968
969 memset(addr, 0, sizeof(*addr));
970 addr->sun_family = AF_UNIX;
971 strcpy(addr->sun_path, path);
972 unlink(path);
973
974 len = sizeof(addr->sun_family) + strlen(path) + 1;
975
976 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
977 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200978 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200979 return -1;
980 }
981
982 umask(mode);
983 nd->listenfd = fd;
984 return 0;
985}
986
987static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100988{
989 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100990 struct netio_options *o = td->eo;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500991 struct ip_mreq mr;
992 struct sockaddr_in sin;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800993 struct sockaddr_in6 sin6;
994 struct sockaddr *saddr;
995 int fd, opt, type, domain;
996 socklen_t len;
Jens Axboeed92ac02007-02-06 14:43:52 +0100997
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500998 memset(&sin, 0, sizeof(sin));
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800999 memset(&sin6, 0, sizeof(sin6));
Jens Axboe414c2a32009-01-16 13:21:15 +01001000
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001001 if (o->proto == FIO_TYPE_TCP) {
1002 type = SOCK_STREAM;
1003 domain = AF_INET;
1004 } else if (o->proto == FIO_TYPE_TCP_V6) {
1005 type = SOCK_STREAM;
1006 domain = AF_INET6;
1007 } else if (o->proto == FIO_TYPE_UDP) {
1008 type = SOCK_DGRAM;
1009 domain = AF_INET;
1010 } else if (o->proto == FIO_TYPE_UDP_V6) {
1011 type = SOCK_DGRAM;
1012 domain = AF_INET6;
1013 } else {
1014 log_err("fio: unknown proto %d\n", o->proto);
1015 return 1;
1016 }
1017
1018 fd = socket(domain, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +01001019 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001020 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +01001021 return 1;
1022 }
1023
1024 opt = 1;
Jens Axboe26e594a2013-01-30 21:52:37 +01001025 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001026 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001027 close(fd);
Jens Axboeed92ac02007-02-06 14:43:52 +01001028 return 1;
1029 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001030#ifdef SO_REUSEPORT
Jens Axboe26e594a2013-01-30 21:52:37 +01001031 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001032 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001033 close(fd);
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001034 return 1;
1035 }
1036#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001037
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001038 if (td->o.filename){
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001039 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001040 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1041 close(fd);
1042 return 1;
1043 }
1044
1045 inet_aton(td->o.filename, &sin.sin_addr);
1046
1047 mr.imr_multiaddr = sin.sin_addr;
Bruce Cranf16b7402013-10-08 15:05:27 +01001048 if (o->intfc) {
1049 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -05001050 log_err("fio: interface not valid interface IP\n");
1051 close(fd);
1052 return 1;
1053 }
1054 } else {
1055 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1056 }
Bruce Cranf16b7402013-10-08 15:05:27 +01001057 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001058 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1059 close(fd);
1060 return 1;
1061 }
1062 }
1063
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001064 if (!is_ipv6(o)) {
1065 saddr = (struct sockaddr *) &nd->addr;
1066 len = sizeof(nd->addr);
Jens Axboeed92ac02007-02-06 14:43:52 +01001067
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001068 nd->addr.sin_family = AF_INET;
1069 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1070 nd->addr.sin_port = htons(port);
1071 } else {
1072 saddr = (struct sockaddr *) &nd->addr6;
1073 len = sizeof(nd->addr6);
1074
1075 nd->addr6.sin6_family = AF_INET6;
1076 nd->addr6.sin6_addr = sin6.sin6_addr.s6_addr ? sin6.sin6_addr : in6addr_any;
1077 nd->addr6.sin6_port = htons(port);
1078 }
1079
1080 if (bind(fd, saddr, len) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001081 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +01001082 return 1;
1083 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001084
1085 nd->listenfd = fd;
1086 return 0;
1087}
1088
Steven Langde890a12011-11-09 14:03:34 +01001089static int fio_netio_setup_listen(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +02001090{
1091 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +01001092 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +02001093 int ret;
1094
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001095 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +01001096 ret = fio_netio_setup_listen_inet(td, o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001097 else
Steven Langde890a12011-11-09 14:03:34 +01001098 ret = fio_netio_setup_listen_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001099
1100 if (ret)
1101 return ret;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001102 if (is_udp(o))
Jens Axboe0fd666b2011-10-06 20:08:53 +02001103 return 0;
1104
1105 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001106 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +02001107 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +01001108 return 1;
1109 }
1110
Jens Axboeb5af8292007-03-08 12:43:13 +01001111 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +01001112}
1113
Jens Axboe9bec88e2007-03-02 08:55:48 +01001114static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +01001115{
Steven Langde890a12011-11-09 14:03:34 +01001116 struct netio_options *o = td->eo;
Jens Axboeaf52b342007-03-13 10:07:47 +01001117 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001118
Bruce Cran3f457be2012-10-10 13:37:41 +01001119#ifdef WIN32
1120 WSADATA wsd;
1121 WSAStartup(MAKEWORD(2,2), &wsd);
1122#endif
1123
Jens Axboe16d55aa2007-05-22 09:21:37 +02001124 if (td_random(td)) {
1125 log_err("fio: network IO can't be random\n");
1126 return 1;
1127 }
Jens Axboeed92ac02007-02-06 14:43:52 +01001128
Steven Langde890a12011-11-09 14:03:34 +01001129 if (o->proto == FIO_TYPE_UNIX && o->port) {
1130 log_err("fio: network IO port not valid with unix socket\n");
1131 return 1;
1132 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1133 log_err("fio: network IO requires port for tcp or udp\n");
1134 return 1;
Jens Axboe414c2a32009-01-16 13:21:15 +01001135 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001136
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001137 if (!is_tcp(o)) {
Steven Langde890a12011-11-09 14:03:34 +01001138 if (o->listen) {
Jens Axboe9b986062011-12-19 08:57:18 +01001139 log_err("fio: listen only valid for TCP proto IO\n");
1140 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001141 }
1142 if (td_rw(td)) {
Jens Axboe9b986062011-12-19 08:57:18 +01001143 log_err("fio: datagram network connections must be"
Steven Langde890a12011-11-09 14:03:34 +01001144 " read OR write\n");
Jens Axboe9b986062011-12-19 08:57:18 +01001145 return 1;
1146 }
1147 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1148 log_err("fio: UNIX sockets need host/filename\n");
1149 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001150 }
1151 o->listen = td_read(td);
1152 }
1153
Steven Langde890a12011-11-09 14:03:34 +01001154 if (o->listen)
1155 ret = fio_netio_setup_listen(td);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001156 else
Steven Langde890a12011-11-09 14:03:34 +01001157 ret = fio_netio_setup_connect(td);
Jens Axboeed92ac02007-02-06 14:43:52 +01001158
Jens Axboe7bb48f82007-03-27 15:30:28 +02001159 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001160}
1161
Jens Axboeb5af8292007-03-08 12:43:13 +01001162static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +01001163{
Jens Axboeb5af8292007-03-08 12:43:13 +01001164 struct netio_data *nd = td->io_ops->data;
1165
1166 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +02001167 if (nd->listenfd != -1)
1168 close(nd->listenfd);
1169 if (nd->pipes[0] != -1)
1170 close(nd->pipes[0]);
1171 if (nd->pipes[1] != -1)
1172 close(nd->pipes[1]);
1173
Jens Axboeb5af8292007-03-08 12:43:13 +01001174 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +01001175 }
1176}
1177
1178static int fio_netio_setup(struct thread_data *td)
1179{
Jens Axboe7bb48f82007-03-27 15:30:28 +02001180 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +01001181
Steven Langde890a12011-11-09 14:03:34 +01001182 if (!td->files_index) {
1183 add_file(td, td->o.filename ?: "net");
1184 td->o.nr_files = td->o.nr_files ?: 1;
1185 }
1186
Jens Axboe7bb48f82007-03-27 15:30:28 +02001187 if (!td->io_ops->data) {
1188 nd = malloc(sizeof(*nd));;
1189
1190 memset(nd, 0, sizeof(*nd));
1191 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +02001192 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001193 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001194 }
1195
Jens Axboe9bec88e2007-03-02 08:55:48 +01001196 return 0;
1197}
1198
Jens Axboe36d80bc2012-11-30 21:46:06 +01001199static void fio_netio_terminate(struct thread_data *td)
1200{
1201 kill(td->pid, SIGUSR2);
1202}
1203
Jens Axboe67bf9822013-01-10 11:23:19 +01001204#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001205static int fio_netio_setup_splice(struct thread_data *td)
1206{
1207 struct netio_data *nd;
1208
1209 fio_netio_setup(td);
1210
1211 nd = td->io_ops->data;
1212 if (nd) {
1213 if (pipe(nd->pipes) < 0)
1214 return 1;
1215
1216 nd->use_splice = 1;
1217 return 0;
1218 }
1219
1220 return 1;
1221}
1222
Jens Axboe5921e802008-05-30 15:02:38 +02001223static struct ioengine_ops ioengine_splice = {
Steven Langde890a12011-11-09 14:03:34 +01001224 .name = "netsplice",
1225 .version = FIO_IOOPS_VERSION,
1226 .prep = fio_netio_prep,
1227 .queue = fio_netio_queue,
1228 .setup = fio_netio_setup_splice,
1229 .init = fio_netio_init,
1230 .cleanup = fio_netio_cleanup,
1231 .open_file = fio_netio_open_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001232 .close_file = fio_netio_close_file,
1233 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001234 .options = options,
1235 .option_struct_size = sizeof(struct netio_options),
1236 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Jens Axboe36d80bc2012-11-30 21:46:06 +01001237 FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +02001238};
1239#endif
1240
Jens Axboe9cce02e2007-06-22 15:42:21 +02001241static struct ioengine_ops ioengine_rw = {
Steven Langde890a12011-11-09 14:03:34 +01001242 .name = "net",
1243 .version = FIO_IOOPS_VERSION,
1244 .prep = fio_netio_prep,
1245 .queue = fio_netio_queue,
1246 .setup = fio_netio_setup,
1247 .init = fio_netio_init,
1248 .cleanup = fio_netio_cleanup,
1249 .open_file = fio_netio_open_file,
1250 .close_file = fio_netio_close_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001251 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001252 .options = options,
1253 .option_struct_size = sizeof(struct netio_options),
1254 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Steven Noonanad705bc2013-04-08 15:05:25 -07001255 FIO_PIPEIO | FIO_BIT_BASED,
Jens Axboeed92ac02007-02-06 14:43:52 +01001256};
1257
Steven Langde890a12011-11-09 14:03:34 +01001258static int str_hostname_cb(void *data, const char *input)
1259{
1260 struct netio_options *o = data;
1261
1262 if (o->td->o.filename)
1263 free(o->td->o.filename);
1264 o->td->o.filename = strdup(input);
1265 return 0;
1266}
1267
Jens Axboeed92ac02007-02-06 14:43:52 +01001268static void fio_init fio_netio_register(void)
1269{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001270 register_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001271#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001272 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001273#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001274}
1275
1276static void fio_exit fio_netio_unregister(void)
1277{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001278 unregister_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001279#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001280 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001281#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001282}