blob: cd19535294a083eff56c97036aef75f8c4615541 [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 Axboeda91d752014-10-09 13:10:14 -060024#include "../verify.h"
Jens Axboeed92ac02007-02-06 14:43:52 +010025
Jens Axboeb5af8292007-03-08 12:43:13 +010026struct netio_data {
27 int listenfd;
Jens Axboe9cce02e2007-06-22 15:42:21 +020028 int use_splice;
Jens Axboe97909a12014-10-09 13:38:06 -060029 int seq_off;
Jens Axboe9cce02e2007-06-22 15:42:21 +020030 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010031 struct sockaddr_in addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -080032 struct sockaddr_in6 addr6;
Jens Axboe0fd666b2011-10-06 20:08:53 +020033 struct sockaddr_un addr_un;
Jens Axboeda91d752014-10-09 13:10:14 -060034 uint64_t udp_send_seq;
35 uint64_t udp_recv_seq;
Jens Axboeb5af8292007-03-08 12:43:13 +010036};
Jens Axboeed92ac02007-02-06 14:43:52 +010037
Steven Langde890a12011-11-09 14:03:34 +010038struct netio_options {
39 struct thread_data *td;
40 unsigned int port;
41 unsigned int proto;
42 unsigned int listen;
Jens Axboe6f73a7f2012-11-30 09:59:20 +010043 unsigned int pingpong;
Steven Noonan70a78782012-11-28 14:52:36 -080044 unsigned int nodelay;
Shawn Bohrerd3a623d2013-07-19 13:24:08 -050045 unsigned int ttl;
Jens Axboe531e67a2014-10-09 11:55:16 -060046 unsigned int window_size;
Jens Axboe5e34cea2014-10-09 12:05:44 -060047 unsigned int mss;
Bruce Cranf16b7402013-10-08 15:05:27 +010048 char *intfc;
Steven Langde890a12011-11-09 14:03:34 +010049};
50
Jens Axboe664fb3b2009-01-19 13:26:36 +010051struct udp_close_msg {
52 uint32_t magic;
53 uint32_t cmd;
54};
55
Jens Axboeda91d752014-10-09 13:10:14 -060056struct udp_seq {
57 uint64_t magic;
58 uint64_t seq;
Jens Axboe97909a12014-10-09 13:38:06 -060059 uint64_t bs;
Jens Axboeda91d752014-10-09 13:10:14 -060060};
61
Jens Axboe664fb3b2009-01-19 13:26:36 +010062enum {
63 FIO_LINK_CLOSE = 0x89,
Jens Axboeb96d2432012-11-30 08:27:46 +010064 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
65 FIO_LINK_OPEN = 0x98,
Jens Axboeda91d752014-10-09 13:10:14 -060066 FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
Jens Axboe0fd666b2011-10-06 20:08:53 +020067
68 FIO_TYPE_TCP = 1,
69 FIO_TYPE_UDP = 2,
70 FIO_TYPE_UNIX = 3,
Jens Axboe49ccb8c2014-01-23 16:49:37 -080071 FIO_TYPE_TCP_V6 = 4,
72 FIO_TYPE_UDP_V6 = 5,
Jens Axboe664fb3b2009-01-19 13:26:36 +010073};
74
Steven Langde890a12011-11-09 14:03:34 +010075static int str_hostname_cb(void *data, const char *input);
76static struct fio_option options[] = {
77 {
78 .name = "hostname",
Jens Axboee8b0e952012-03-19 14:37:08 +010079 .lname = "net engine hostname",
Steven Langde890a12011-11-09 14:03:34 +010080 .type = FIO_OPT_STR_STORE,
81 .cb = str_hostname_cb,
82 .help = "Hostname for net IO engine",
Jens Axboee90a0ad2013-04-10 19:43:59 +020083 .category = FIO_OPT_C_ENGINE,
84 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010085 },
86 {
87 .name = "port",
Jens Axboee8b0e952012-03-19 14:37:08 +010088 .lname = "net engine port",
Steven Langde890a12011-11-09 14:03:34 +010089 .type = FIO_OPT_INT,
90 .off1 = offsetof(struct netio_options, port),
91 .minval = 1,
92 .maxval = 65535,
93 .help = "Port to use for TCP or UDP net connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +020094 .category = FIO_OPT_C_ENGINE,
95 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010096 },
97 {
98 .name = "protocol",
Jens Axboee8b0e952012-03-19 14:37:08 +010099 .lname = "net engine protocol",
Steven Langde890a12011-11-09 14:03:34 +0100100 .alias = "proto",
101 .type = FIO_OPT_STR,
102 .off1 = offsetof(struct netio_options, proto),
103 .help = "Network protocol to use",
104 .def = "tcp",
105 .posval = {
106 { .ival = "tcp",
107 .oval = FIO_TYPE_TCP,
108 .help = "Transmission Control Protocol",
109 },
Jens Axboeeb232312014-01-24 18:59:15 -0800110#ifdef CONFIG_IPV6
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800111 { .ival = "tcpv6",
112 .oval = FIO_TYPE_TCP_V6,
113 .help = "Transmission Control Protocol V6",
114 },
Jens Axboeeb232312014-01-24 18:59:15 -0800115#endif
Steven Langde890a12011-11-09 14:03:34 +0100116 { .ival = "udp",
117 .oval = FIO_TYPE_UDP,
Bruce Cranf5cc3d02012-10-10 08:17:44 -0600118 .help = "User Datagram Protocol",
Steven Langde890a12011-11-09 14:03:34 +0100119 },
Jens Axboeeb232312014-01-24 18:59:15 -0800120#ifdef CONFIG_IPV6
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800121 { .ival = "udpv6",
122 .oval = FIO_TYPE_UDP_V6,
123 .help = "User Datagram Protocol V6",
124 },
Jens Axboeeb232312014-01-24 18:59:15 -0800125#endif
Steven Langde890a12011-11-09 14:03:34 +0100126 { .ival = "unix",
127 .oval = FIO_TYPE_UNIX,
128 .help = "UNIX domain socket",
129 },
130 },
Jens Axboee90a0ad2013-04-10 19:43:59 +0200131 .category = FIO_OPT_C_ENGINE,
132 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100133 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100134#ifdef CONFIG_TCP_NODELAY
Steven Langde890a12011-11-09 14:03:34 +0100135 {
Steven Noonan70a78782012-11-28 14:52:36 -0800136 .name = "nodelay",
137 .type = FIO_OPT_BOOL,
138 .off1 = offsetof(struct netio_options, nodelay),
139 .help = "Use TCP_NODELAY on TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200140 .category = FIO_OPT_C_ENGINE,
141 .group = FIO_OPT_G_NETIO,
Steven Noonan70a78782012-11-28 14:52:36 -0800142 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100143#endif
Steven Langde890a12011-11-09 14:03:34 +0100144 {
145 .name = "listen",
Jens Axboee8b0e952012-03-19 14:37:08 +0100146 .lname = "net engine listen",
Steven Langde890a12011-11-09 14:03:34 +0100147 .type = FIO_OPT_STR_SET,
148 .off1 = offsetof(struct netio_options, listen),
149 .help = "Listen for incoming TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100152 },
153 {
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100154 .name = "pingpong",
155 .type = FIO_OPT_STR_SET,
156 .off1 = offsetof(struct netio_options, pingpong),
157 .help = "Ping-pong IO requests",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200158 .category = FIO_OPT_C_ENGINE,
159 .group = FIO_OPT_G_NETIO,
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100160 },
161 {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500162 .name = "interface",
163 .lname = "net engine interface",
164 .type = FIO_OPT_STR_STORE,
Bruce Cranf16b7402013-10-08 15:05:27 +0100165 .off1 = offsetof(struct netio_options, intfc),
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500166 .help = "Network interface to use",
167 .category = FIO_OPT_C_ENGINE,
168 .group = FIO_OPT_G_NETIO,
169 },
170 {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500171 .name = "ttl",
172 .lname = "net engine multicast ttl",
173 .type = FIO_OPT_INT,
174 .off1 = offsetof(struct netio_options, ttl),
175 .def = "1",
176 .minval = 0,
177 .help = "Time-to-live value for outgoing UDP multicast packets",
178 .category = FIO_OPT_C_ENGINE,
179 .group = FIO_OPT_G_NETIO,
180 },
Jens Axboe531e67a2014-10-09 11:55:16 -0600181#ifdef CONFIG_NET_WINDOWSIZE
182 {
183 .name = "window_size",
184 .lname = "Window Size",
185 .type = FIO_OPT_INT,
186 .off1 = offsetof(struct netio_options, window_size),
187 .minval = 0,
188 .help = "Set socket buffer window size",
189 .category = FIO_OPT_C_ENGINE,
190 .group = FIO_OPT_G_NETIO,
191 },
192#endif
Jens Axboe5e34cea2014-10-09 12:05:44 -0600193#ifdef CONFIG_NET_MSS
194 {
195 .name = "mss",
196 .lname = "Maximum segment size",
197 .type = FIO_OPT_INT,
198 .off1 = offsetof(struct netio_options, mss),
199 .minval = 0,
200 .help = "Set TCP maximum segment size",
201 .category = FIO_OPT_C_ENGINE,
202 .group = FIO_OPT_G_NETIO,
203 },
204#endif
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500205 {
Steven Langde890a12011-11-09 14:03:34 +0100206 .name = NULL,
207 },
208};
209
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800210static inline int is_udp(struct netio_options *o)
211{
212 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
213}
214
215static inline int is_tcp(struct netio_options *o)
216{
217 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
218}
219
220static inline int is_ipv6(struct netio_options *o)
221{
222 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
223}
224
Jens Axboe531e67a2014-10-09 11:55:16 -0600225static int set_window_size(struct thread_data *td, int fd)
226{
227#ifdef CONFIG_NET_WINDOWSIZE
228 struct netio_options *o = td->eo;
229 unsigned int wss;
230 int snd, rcv, ret;
231
232 if (!o->window_size)
233 return 0;
234
235 rcv = o->listen || o->pingpong;
236 snd = !o->listen || o->pingpong;
237 wss = o->window_size;
238 ret = 0;
239
240 if (rcv) {
241 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
242 sizeof(wss));
243 if (ret < 0)
244 td_verror(td, errno, "rcvbuf window size");
245 }
246 if (snd && !ret) {
247 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
248 sizeof(wss));
249 if (ret < 0)
250 td_verror(td, errno, "sndbuf window size");
251 }
252
253 return ret;
254#else
255 td_verror(td, -EINVAL, "setsockopt window size");
256 return -1;
257#endif
258}
259
Jens Axboe5e34cea2014-10-09 12:05:44 -0600260static int set_mss(struct thread_data *td, int fd)
261{
262#ifdef CONFIG_NET_MSS
263 struct netio_options *o = td->eo;
264 unsigned int mss;
265 int ret;
266
267 if (!o->mss || !is_tcp(o))
268 return 0;
269
270 mss = o->mss;
271 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
272 sizeof(mss));
273 if (ret < 0)
274 td_verror(td, errno, "setsockopt TCP_MAXSEG");
275
276 return ret;
277#else
278 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
279 return -1;
280#endif
281}
282
283
Jens Axboe371d4562009-01-19 10:17:06 +0100284/*
285 * Return -1 for error and 'nr events' for a positive number
286 * of events
287 */
288static int poll_wait(struct thread_data *td, int fd, short events)
289{
290 struct pollfd pfd;
291 int ret;
292
293 while (!td->terminate) {
294 pfd.fd = fd;
295 pfd.events = events;
296 ret = poll(&pfd, 1, -1);
297 if (ret < 0) {
298 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +0100299 break;
Jens Axboe371d4562009-01-19 10:17:06 +0100300
301 td_verror(td, errno, "poll");
302 return -1;
303 } else if (!ret)
304 continue;
305
306 break;
307 }
308
309 if (pfd.revents & events)
310 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +0100311
312 return -1;
313}
314
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500315static int fio_netio_is_multicast(const char *mcaddr)
316{
317 in_addr_t addr = inet_network(mcaddr);
318 if (addr == -1)
319 return 0;
320
321 if (inet_network("224.0.0.0") <= addr &&
322 inet_network("239.255.255.255") >= addr)
323 return 1;
324
325 return 0;
326}
327
328
Jens Axboeed92ac02007-02-06 14:43:52 +0100329static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
330{
Steven Langde890a12011-11-09 14:03:34 +0100331 struct netio_options *o = td->eo;
Jens Axboeed92ac02007-02-06 14:43:52 +0100332
Jens Axboe7a6499d2007-02-07 09:35:29 +0100333 /*
334 * Make sure we don't see spurious reads to a receiver, and vice versa
335 */
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800336 if (is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +0100337 return 0;
338
339 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
340 (!o->listen && io_u->ddir == DDIR_READ)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100341 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +0100342 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100343 }
Bruce Cran3f457be2012-10-10 13:37:41 +0100344
Jens Axboef85ac252008-03-01 18:09:49 +0100345 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100346}
347
Jens Axboe67bf9822013-01-10 11:23:19 +0100348#ifdef CONFIG_LINUX_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +0200349static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200350{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200351 int bytes = 0;
352
353 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200354 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200355
356 if (ret < 0) {
357 if (!bytes)
358 bytes = ret;
359
360 break;
361 } else if (!ret)
362 break;
363
364 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200365 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200366 }
367
368 return bytes;
369}
370
371/*
Jens Axboecd963e12007-06-24 21:41:46 +0200372 * Receive bytes from a socket and fill them into the internal pipe
373 */
374static int splice_in(struct thread_data *td, struct io_u *io_u)
375{
376 struct netio_data *nd = td->io_ops->data;
377
378 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
379}
380
381/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200382 * Transmit 'len' bytes from the internal pipe
383 */
384static int splice_out(struct thread_data *td, struct io_u *io_u,
385 unsigned int len)
386{
387 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200388
389 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
390}
391
392static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
393{
394 struct iovec iov = {
395 .iov_base = io_u->xfer_buf,
396 .iov_len = len,
397 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200398 int bytes = 0;
399
Jens Axboecd963e12007-06-24 21:41:46 +0200400 while (iov.iov_len) {
401 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200402
403 if (ret < 0) {
404 if (!bytes)
405 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200406 break;
407 } else if (!ret)
408 break;
409
Jens Axboecd963e12007-06-24 21:41:46 +0200410 iov.iov_len -= ret;
411 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200412 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200413 }
414
415 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200416
Jens Axboe9cce02e2007-06-22 15:42:21 +0200417}
418
419/*
420 * vmsplice() pipe to io_u buffer
421 */
422static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
423 unsigned int len)
424{
425 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200426
Jens Axboecd963e12007-06-24 21:41:46 +0200427 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200428}
429
430/*
431 * vmsplice() io_u to pipe
432 */
433static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
434{
435 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200436
Jens Axboecd963e12007-06-24 21:41:46 +0200437 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200438}
439
Jens Axboecd963e12007-06-24 21:41:46 +0200440/*
441 * splice receive - transfer socket data into a pipe using splice, then map
442 * that pipe data into the io_u using vmsplice.
443 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200444static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
445{
446 int ret;
447
448 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200449 if (ret > 0)
450 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200451
Jens Axboecd963e12007-06-24 21:41:46 +0200452 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200453}
454
Jens Axboecd963e12007-06-24 21:41:46 +0200455/*
456 * splice transmit - map data from the io_u into a pipe by using vmsplice,
457 * then transfer that pipe to a socket using splice.
458 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200459static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
460{
461 int ret;
462
463 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200464 if (ret > 0)
465 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200466
Jens Axboecd963e12007-06-24 21:41:46 +0200467 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200468}
Jens Axboe5921e802008-05-30 15:02:38 +0200469#else
470static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
471{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200472 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200473 return -1;
474}
475
476static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
477{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200478 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200479 return -1;
480}
481#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200482
Jens Axboeda91d752014-10-09 13:10:14 -0600483static void store_udp_seq(struct netio_data *nd, struct io_u *io_u)
484{
485 struct udp_seq *us;
486
Steven Noonan868902d2015-01-16 16:46:11 -0800487 if (io_u->xfer_buflen < sizeof(*us))
488 return;
489
Jens Axboeda91d752014-10-09 13:10:14 -0600490 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
Jens Axboe768ae052014-10-15 08:51:43 -0600491 us->magic = cpu_to_le64((uint64_t) FIO_UDP_SEQ_MAGIC);
Jens Axboe97909a12014-10-09 13:38:06 -0600492 us->bs = cpu_to_le64((uint64_t) io_u->xfer_buflen);
Jens Axboeda91d752014-10-09 13:10:14 -0600493 us->seq = cpu_to_le64(nd->udp_send_seq++);
494}
495
Jens Axboe67e149c2014-10-09 13:27:44 -0600496static void verify_udp_seq(struct thread_data *td, struct netio_data *nd,
497 struct io_u *io_u)
Jens Axboeda91d752014-10-09 13:10:14 -0600498{
499 struct udp_seq *us;
500 uint64_t seq;
501
Steven Noonan868902d2015-01-16 16:46:11 -0800502 if (io_u->xfer_buflen < sizeof(*us))
503 return;
504
Jens Axboe97909a12014-10-09 13:38:06 -0600505 if (nd->seq_off)
506 return;
507
Jens Axboeda91d752014-10-09 13:10:14 -0600508 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
509 if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC)
510 return;
Jens Axboe97909a12014-10-09 13:38:06 -0600511 if (le64_to_cpu(us->bs) != io_u->xfer_buflen) {
512 nd->seq_off = 1;
513 return;
514 }
Jens Axboeda91d752014-10-09 13:10:14 -0600515
516 seq = le64_to_cpu(us->seq);
517
518 if (seq != nd->udp_recv_seq)
Jens Axboe67e149c2014-10-09 13:27:44 -0600519 td->ts.drop_io_u[io_u->ddir] += seq - nd->udp_recv_seq;
Jens Axboeda91d752014-10-09 13:10:14 -0600520
521 nd->udp_recv_seq = seq + 1;
522}
523
Jens Axboe9cce02e2007-06-22 15:42:21 +0200524static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
525{
Jens Axboe414c2a32009-01-16 13:21:15 +0100526 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100527 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100528 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100529
Jens Axboe664fb3b2009-01-19 13:26:36 +0100530 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800531 if (is_udp(o)) {
Jens Axboe10aa1362014-04-01 21:10:36 -0600532 const struct sockaddr *to;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800533 socklen_t len;
534
535 if (is_ipv6(o)) {
536 to = (struct sockaddr *) &nd->addr6;
537 len = sizeof(nd->addr6);
538 } else {
539 to = (struct sockaddr *) &nd->addr;
540 len = sizeof(nd->addr);
541 }
Jens Axboe62b38922009-05-11 10:37:33 +0200542
Jens Axboeda91d752014-10-09 13:10:14 -0600543 if (td->o.verify == VERIFY_NONE)
544 store_udp_seq(nd, io_u);
545
Jens Axboe664fb3b2009-01-19 13:26:36 +0100546 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800547 io_u->xfer_buflen, flags, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100548 } else {
549 /*
550 * if we are going to write more, set MSG_MORE
551 */
Jens Axboe5921e802008-05-30 15:02:38 +0200552#ifdef MSG_MORE
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100553 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
554 td->o.size) && !o->pingpong)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100555 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200556#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100557 ret = send(io_u->file->fd, io_u->xfer_buf,
558 io_u->xfer_buflen, flags);
559 }
560 if (ret > 0)
561 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200562
Jens Axboe664fb3b2009-01-19 13:26:36 +0100563 ret = poll_wait(td, io_u->file->fd, POLLOUT);
564 if (ret <= 0)
565 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100566 } while (1);
567
568 return ret;
569}
570
Jens Axboe80436e12014-10-09 18:10:11 -0600571static int is_close_msg(struct io_u *io_u, int len)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100572{
573 struct udp_close_msg *msg;
574
575 if (len != sizeof(struct udp_close_msg))
576 return 0;
577
578 msg = io_u->xfer_buf;
Jens Axboe80436e12014-10-09 18:10:11 -0600579 if (le32_to_cpu(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100580 return 0;
Jens Axboe80436e12014-10-09 18:10:11 -0600581 if (le32_to_cpu(msg->cmd) != FIO_LINK_CLOSE)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100582 return 0;
583
584 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200585}
586
Jens Axboe414c2a32009-01-16 13:21:15 +0100587static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200588{
Jens Axboe414c2a32009-01-16 13:21:15 +0100589 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100590 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100591 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100592
Jens Axboe664fb3b2009-01-19 13:26:36 +0100593 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800594 if (is_udp(o)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500595 struct sockaddr *from;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800596 socklen_t l, *len = &l;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500597
598 if (o->listen) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800599 if (!is_ipv6(o)) {
600 from = (struct sockaddr *) &nd->addr;
601 *len = sizeof(nd->addr);
602 } else {
603 from = (struct sockaddr *) &nd->addr6;
604 *len = sizeof(nd->addr6);
605 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500606 } else {
607 from = NULL;
608 len = NULL;
609 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200610
Jens Axboe664fb3b2009-01-19 13:26:36 +0100611 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500612 io_u->xfer_buflen, flags, from, len);
Jens Axboeda91d752014-10-09 13:10:14 -0600613
Jens Axboe80436e12014-10-09 18:10:11 -0600614 if (is_close_msg(io_u, ret)) {
Jens Axboe664fb3b2009-01-19 13:26:36 +0100615 td->done = 1;
616 return 0;
617 }
618 } else {
619 ret = recv(io_u->file->fd, io_u->xfer_buf,
620 io_u->xfer_buflen, flags);
Jens Axboe80436e12014-10-09 18:10:11 -0600621
622 if (is_close_msg(io_u, ret)) {
623 td->done = 1;
624 return 0;
625 }
Jens Axboe664fb3b2009-01-19 13:26:36 +0100626 }
627 if (ret > 0)
628 break;
Jens Axboe7d988f62012-11-29 19:57:35 +0100629 else if (!ret && (flags & MSG_WAITALL))
630 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100631
Jens Axboe664fb3b2009-01-19 13:26:36 +0100632 ret = poll_wait(td, io_u->file->fd, POLLIN);
633 if (ret <= 0)
634 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100635 flags |= MSG_WAITALL;
636 } while (1);
637
Jens Axboeda91d752014-10-09 13:10:14 -0600638 if (is_udp(o) && td->o.verify == VERIFY_NONE)
Jens Axboe67e149c2014-10-09 13:27:44 -0600639 verify_udp_seq(td, nd, io_u);
Jens Axboeda91d752014-10-09 13:10:14 -0600640
Jens Axboe664fb3b2009-01-19 13:26:36 +0100641 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200642}
643
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100644static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
645 enum fio_ddir ddir)
Jens Axboeed92ac02007-02-06 14:43:52 +0100646{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200647 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100648 struct netio_options *o = td->eo;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200649 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100650
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100651 if (ddir == DDIR_WRITE) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800652 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100653 o->proto == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200654 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200655 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100656 ret = fio_netio_splice_out(td, io_u);
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100657 } else if (ddir == DDIR_READ) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800658 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100659 o->proto == FIO_TYPE_UNIX)
Jens Axboe414c2a32009-01-16 13:21:15 +0100660 ret = fio_netio_recv(td, io_u);
661 else
662 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100663 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100664 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100665
Jens Axboecec6b552007-02-06 20:15:38 +0100666 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe0ecdd7f2014-10-08 17:04:29 -0600667 if (ret > 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100668 io_u->resid = io_u->xfer_buflen - ret;
669 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100670 return FIO_Q_COMPLETED;
Jens Axboe0ecdd7f2014-10-08 17:04:29 -0600671 } else if (!ret)
672 return FIO_Q_BUSY;
673 else {
Jens Axboe414c2a32009-01-16 13:21:15 +0100674 int err = errno;
675
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100676 if (ddir == DDIR_WRITE && err == EMSGSIZE)
Jens Axboe414c2a32009-01-16 13:21:15 +0100677 return FIO_Q_BUSY;
678
679 io_u->error = err;
680 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100681 }
682
Jens Axboe36167d82007-02-18 05:41:31 +0100683 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100684 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100685
Jens Axboe36167d82007-02-18 05:41:31 +0100686 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100687}
688
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100689static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
690{
691 struct netio_options *o = td->eo;
692 int ret;
693
694 fio_ro_check(td, io_u);
695
696 ret = __fio_netio_queue(td, io_u, io_u->ddir);
697 if (!o->pingpong || ret != FIO_Q_COMPLETED)
698 return ret;
699
700 /*
701 * For ping-pong mode, receive or send reply as needed
702 */
703 if (td_read(td) && io_u->ddir == DDIR_READ)
704 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
705 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
706 ret = __fio_netio_queue(td, io_u, DDIR_READ);
707
708 return ret;
709}
710
Jens Axboeb5af8292007-03-08 12:43:13 +0100711static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100712{
Jens Axboeb5af8292007-03-08 12:43:13 +0100713 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100714 struct netio_options *o = td->eo;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100715 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100716
Steven Langde890a12011-11-09 14:03:34 +0100717 if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200718 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100719 type = SOCK_STREAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800720 } else if (o->proto == FIO_TYPE_TCP_V6) {
721 domain = AF_INET6;
722 type = SOCK_STREAM;
Steven Langde890a12011-11-09 14:03:34 +0100723 } else if (o->proto == FIO_TYPE_UDP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200724 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100725 type = SOCK_DGRAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800726 } else if (o->proto == FIO_TYPE_UDP_V6) {
727 domain = AF_INET6;
728 type = SOCK_DGRAM;
Steven Langde890a12011-11-09 14:03:34 +0100729 } else if (o->proto == FIO_TYPE_UNIX) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200730 domain = AF_UNIX;
731 type = SOCK_STREAM;
732 } else {
Steven Langde890a12011-11-09 14:03:34 +0100733 log_err("fio: bad network type %d\n", o->proto);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200734 f->fd = -1;
735 return 1;
736 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100737
Jens Axboe0fd666b2011-10-06 20:08:53 +0200738 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100739 if (f->fd < 0) {
740 td_verror(td, errno, "socket");
741 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100742 }
743
Jens Axboe1eafa372013-01-31 10:19:51 +0100744#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800745 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100746 int optval = 1;
747
Jens Axboe26e594a2013-01-30 21:52:37 +0100748 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800749 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
750 return 1;
751 }
752 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100753#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800754
Jens Axboe531e67a2014-10-09 11:55:16 -0600755 if (set_window_size(td, f->fd)) {
756 close(f->fd);
757 return 1;
758 }
Jens Axboe5e34cea2014-10-09 12:05:44 -0600759 if (set_mss(td, f->fd)) {
760 close(f->fd);
761 return 1;
762 }
Jens Axboe531e67a2014-10-09 11:55:16 -0600763
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800764 if (is_udp(o)) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500765 if (!fio_netio_is_multicast(td->o.filename))
766 return 0;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800767 if (is_ipv6(o)) {
768 log_err("fio: multicast not supported on IPv6\n");
769 close(f->fd);
770 return 1;
771 }
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500772
Bruce Cranf16b7402013-10-08 15:05:27 +0100773 if (o->intfc) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500774 struct in_addr interface_addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800775
Bruce Cranf16b7402013-10-08 15:05:27 +0100776 if (inet_aton(o->intfc, &interface_addr) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500777 log_err("fio: interface not valid interface IP\n");
778 close(f->fd);
779 return 1;
780 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100781 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500782 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
783 close(f->fd);
784 return 1;
785 }
786 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100787 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500788 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
789 close(f->fd);
790 return 1;
791 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100792 return 0;
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500793 } else if (o->proto == FIO_TYPE_TCP) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100794 socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100795
Jens Axboe0fd666b2011-10-06 20:08:53 +0200796 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
797 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200798 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200799 return 1;
800 }
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800801 } else if (o->proto == FIO_TYPE_TCP_V6) {
802 socklen_t len = sizeof(nd->addr6);
803
804 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
805 td_verror(td, errno, "connect");
806 close(f->fd);
807 return 1;
808 }
809
Jens Axboe0fd666b2011-10-06 20:08:53 +0200810 } else {
811 struct sockaddr_un *addr = &nd->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100812 socklen_t len;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200813
814 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
815
816 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
817 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200818 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200819 return 1;
820 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100821 }
822
823 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100824}
825
Jens Axboeb5af8292007-03-08 12:43:13 +0100826static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100827{
Jens Axboeb5af8292007-03-08 12:43:13 +0100828 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100829 struct netio_options *o = td->eo;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800830 socklen_t socklen;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100831 int state;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100832
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800833 if (is_udp(o)) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100834 f->fd = nd->listenfd;
835 return 0;
836 }
837
Jens Axboe859088d2012-11-29 20:02:50 +0100838 state = td->runstate;
839 td_set_runstate(td, TD_SETTING_UP);
840
Jens Axboe6d861442007-03-15 09:22:23 +0100841 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100842
Jens Axboe371d4562009-01-19 10:17:06 +0100843 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
Jens Axboe859088d2012-11-29 20:02:50 +0100844 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100845
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800846 if (o->proto == FIO_TYPE_TCP) {
847 socklen = sizeof(nd->addr);
848 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
849 } else {
850 socklen = sizeof(nd->addr6);
851 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
852 }
853
Jens Axboe371d4562009-01-19 10:17:06 +0100854 if (f->fd < 0) {
855 td_verror(td, errno, "accept");
Jens Axboe859088d2012-11-29 20:02:50 +0100856 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100857 }
858
Jens Axboe1eafa372013-01-31 10:19:51 +0100859#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800860 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100861 int optval = 1;
862
Jens Axboe26e594a2013-01-30 21:52:37 +0100863 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800864 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
865 return 1;
866 }
867 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100868#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800869
Jens Axboe0cae16f2012-11-30 16:22:31 +0100870 reset_all_stats(td);
Jens Axboe859088d2012-11-29 20:02:50 +0100871 td_set_runstate(td, state);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100872 return 0;
Jens Axboe859088d2012-11-29 20:02:50 +0100873err:
874 td_set_runstate(td, state);
875 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100876}
877
Jens Axboe80436e12014-10-09 18:10:11 -0600878static void fio_netio_send_close(struct thread_data *td, struct fio_file *f)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100879{
880 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800881 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100882 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800883 struct sockaddr *to;
884 socklen_t len;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100885 int ret;
886
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800887 if (is_ipv6(o)) {
888 to = (struct sockaddr *) &nd->addr6;
889 len = sizeof(nd->addr6);
890 } else {
891 to = (struct sockaddr *) &nd->addr;
892 len = sizeof(nd->addr);
893 }
894
Jens Axboe80436e12014-10-09 18:10:11 -0600895 msg.magic = cpu_to_le32((uint32_t) FIO_LINK_OPEN_CLOSE_MAGIC);
896 msg.cmd = cpu_to_le32((uint32_t) FIO_LINK_CLOSE);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100897
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800898 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100899 if (ret < 0)
900 td_verror(td, errno, "sendto udp link close");
901}
902
903static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
904{
Jens Axboe664fb3b2009-01-19 13:26:36 +0100905 /*
Jens Axboe80436e12014-10-09 18:10:11 -0600906 * Notify the receiver that we are closing down the link
Jens Axboe664fb3b2009-01-19 13:26:36 +0100907 */
Jens Axboe80436e12014-10-09 18:10:11 -0600908 fio_netio_send_close(td, f);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100909
910 return generic_close_file(td, f);
911}
912
Jens Axboeb96d2432012-11-30 08:27:46 +0100913static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
914{
915 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800916 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100917 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800918 struct sockaddr *to;
919 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100920 int ret;
921
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800922 if (is_ipv6(o)) {
923 len = sizeof(nd->addr6);
924 to = (struct sockaddr *) &nd->addr6;
925 } else {
926 len = sizeof(nd->addr);
927 to = (struct sockaddr *) &nd->addr;
928 }
929
Jens Axboe1f819912013-01-23 17:21:41 -0700930 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100931 if (ret < 0) {
Shawn Bohreree7062f2013-07-19 13:24:09 -0500932 td_verror(td, errno, "recvfrom udp link open");
Jens Axboeb96d2432012-11-30 08:27:46 +0100933 return ret;
934 }
935
936 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
937 ntohl(msg.cmd) != FIO_LINK_OPEN) {
938 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
939 ntohl(msg.cmd));
940 return -1;
941 }
942
Jens Axboeda91d752014-10-09 13:10:14 -0600943 fio_gettime(&td->start, NULL);
Jens Axboeb96d2432012-11-30 08:27:46 +0100944 return 0;
945}
946
Jens Axboe80436e12014-10-09 18:10:11 -0600947static int fio_netio_send_open(struct thread_data *td, struct fio_file *f)
Jens Axboeb96d2432012-11-30 08:27:46 +0100948{
949 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800950 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100951 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800952 struct sockaddr *to;
953 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100954 int ret;
955
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800956 if (is_ipv6(o)) {
957 len = sizeof(nd->addr6);
958 to = (struct sockaddr *) &nd->addr6;
959 } else {
960 len = sizeof(nd->addr);
961 to = (struct sockaddr *) &nd->addr;
962 }
963
Jens Axboeb96d2432012-11-30 08:27:46 +0100964 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
965 msg.cmd = htonl(FIO_LINK_OPEN);
966
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800967 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100968 if (ret < 0) {
969 td_verror(td, errno, "sendto udp link open");
970 return ret;
971 }
972
973 return 0;
974}
975
976static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
977{
978 int ret;
979 struct netio_options *o = td->eo;
980
981 if (o->listen)
982 ret = fio_netio_accept(td, f);
983 else
984 ret = fio_netio_connect(td, f);
985
986 if (ret) {
987 f->fd = -1;
988 return ret;
989 }
990
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800991 if (is_udp(o)) {
Jens Axboeb96d2432012-11-30 08:27:46 +0100992 if (td_write(td))
Jens Axboe80436e12014-10-09 18:10:11 -0600993 ret = fio_netio_send_open(td, f);
Jens Axboeb96d2432012-11-30 08:27:46 +0100994 else {
995 int state;
996
997 state = td->runstate;
998 td_set_runstate(td, TD_SETTING_UP);
999 ret = fio_netio_udp_recv_open(td, f);
1000 td_set_runstate(td, state);
1001 }
1002 }
1003
1004 if (ret)
1005 fio_netio_close_file(td, f);
1006
1007 return ret;
1008}
1009
Jens Axboe0b783342014-01-23 20:19:17 -08001010static int fio_fill_addr(struct thread_data *td, const char *host, int af,
1011 void *dst, struct addrinfo **res)
1012{
1013 struct netio_options *o = td->eo;
1014 struct addrinfo hints;
1015 int ret;
1016
1017 if (inet_pton(af, host, dst))
1018 return 0;
1019
1020 memset(&hints, 0, sizeof(hints));
Jens Axboe0b783342014-01-23 20:19:17 -08001021
1022 if (is_tcp(o))
1023 hints.ai_socktype = SOCK_STREAM;
1024 else
1025 hints.ai_socktype = SOCK_DGRAM;
1026
Jens Axboeb1b1be22014-01-23 20:41:33 -08001027 if (is_ipv6(o))
1028 hints.ai_family = AF_INET6;
1029 else
1030 hints.ai_family = AF_INET;
1031
Jens Axboe0b783342014-01-23 20:19:17 -08001032 ret = getaddrinfo(host, NULL, &hints, res);
1033 if (ret) {
1034 int e = EINVAL;
1035 char str[128];
1036
1037 if (ret == EAI_SYSTEM)
1038 e = errno;
1039
1040 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1041 td_verror(td, e, str);
1042 return 1;
1043 }
1044
1045 return 0;
1046}
1047
Jens Axboe0fd666b2011-10-06 20:08:53 +02001048static int fio_netio_setup_connect_inet(struct thread_data *td,
1049 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +01001050{
1051 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001052 struct netio_options *o = td->eo;
Jens Axboe0b783342014-01-23 20:19:17 -08001053 struct addrinfo *res = NULL;
1054 void *dst, *src;
1055 int af, len;
Jens Axboeb5af8292007-03-08 12:43:13 +01001056
Jens Axboe166dce42012-11-29 14:35:33 +01001057 if (!host) {
1058 log_err("fio: connect with no host to connect to.\n");
1059 if (td_read(td))
1060 log_err("fio: did you forget to set 'listen'?\n");
1061
1062 td_verror(td, EINVAL, "no hostname= set");
1063 return 1;
1064 }
1065
Jens Axboe0b783342014-01-23 20:19:17 -08001066 nd->addr.sin_family = AF_INET;
1067 nd->addr.sin_port = htons(port);
1068 nd->addr6.sin6_family = AF_INET6;
1069 nd->addr6.sin6_port = htons(port);
1070
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001071 if (is_ipv6(o)) {
Jens Axboe0b783342014-01-23 20:19:17 -08001072 af = AF_INET6;
Jens Axboe68631b32014-02-25 13:43:38 -08001073 dst = &nd->addr6.sin6_addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001074 } else {
Jens Axboe0b783342014-01-23 20:19:17 -08001075 af = AF_INET;
Jens Axboe68631b32014-02-25 13:43:38 -08001076 dst = &nd->addr.sin_addr;
Jens Axboeb5af8292007-03-08 12:43:13 +01001077 }
1078
Jens Axboe0b783342014-01-23 20:19:17 -08001079 if (fio_fill_addr(td, host, af, dst, &res))
1080 return 1;
1081
1082 if (!res)
1083 return 0;
1084
1085 if (is_ipv6(o)) {
1086 len = sizeof(nd->addr6.sin6_addr);
1087 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1088 } else {
1089 len = sizeof(nd->addr.sin_addr);
1090 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1091 }
1092
1093 memcpy(dst, src, len);
1094 freeaddrinfo(res);
Jens Axboeb5af8292007-03-08 12:43:13 +01001095 return 0;
1096}
1097
Jens Axboe0fd666b2011-10-06 20:08:53 +02001098static int fio_netio_setup_connect_unix(struct thread_data *td,
1099 const char *path)
1100{
1101 struct netio_data *nd = td->io_ops->data;
1102 struct sockaddr_un *soun = &nd->addr_un;
1103
1104 soun->sun_family = AF_UNIX;
Jens Axboe4b159fa2014-04-14 08:49:04 -06001105 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1106 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001107 return 0;
1108}
1109
Steven Langde890a12011-11-09 14:03:34 +01001110static int fio_netio_setup_connect(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +02001111{
Steven Langde890a12011-11-09 14:03:34 +01001112 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +02001113
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001114 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +01001115 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001116 else
Steven Langde890a12011-11-09 14:03:34 +01001117 return fio_netio_setup_connect_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001118}
1119
1120static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1121{
1122 struct netio_data *nd = td->io_ops->data;
1123 struct sockaddr_un *addr = &nd->addr_un;
1124 mode_t mode;
1125 int len, fd;
1126
1127 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1128 if (fd < 0) {
1129 log_err("fio: socket: %s\n", strerror(errno));
1130 return -1;
1131 }
1132
1133 mode = umask(000);
1134
1135 memset(addr, 0, sizeof(*addr));
1136 addr->sun_family = AF_UNIX;
Jens Axboe4b159fa2014-04-14 08:49:04 -06001137 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001138 unlink(path);
1139
1140 len = sizeof(addr->sun_family) + strlen(path) + 1;
1141
1142 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1143 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001144 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001145 return -1;
1146 }
1147
1148 umask(mode);
1149 nd->listenfd = fd;
1150 return 0;
1151}
1152
1153static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +01001154{
1155 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +01001156 struct netio_options *o = td->eo;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001157 struct ip_mreq mr;
1158 struct sockaddr_in sin;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001159 struct sockaddr *saddr;
1160 int fd, opt, type, domain;
1161 socklen_t len;
Jens Axboeed92ac02007-02-06 14:43:52 +01001162
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001163 memset(&sin, 0, sizeof(sin));
Jens Axboe414c2a32009-01-16 13:21:15 +01001164
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001165 if (o->proto == FIO_TYPE_TCP) {
1166 type = SOCK_STREAM;
1167 domain = AF_INET;
1168 } else if (o->proto == FIO_TYPE_TCP_V6) {
1169 type = SOCK_STREAM;
1170 domain = AF_INET6;
1171 } else if (o->proto == FIO_TYPE_UDP) {
1172 type = SOCK_DGRAM;
1173 domain = AF_INET;
1174 } else if (o->proto == FIO_TYPE_UDP_V6) {
1175 type = SOCK_DGRAM;
1176 domain = AF_INET6;
1177 } else {
1178 log_err("fio: unknown proto %d\n", o->proto);
1179 return 1;
1180 }
1181
1182 fd = socket(domain, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +01001183 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001184 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +01001185 return 1;
1186 }
1187
1188 opt = 1;
Jens Axboe26e594a2013-01-30 21:52:37 +01001189 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001190 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001191 close(fd);
Jens Axboeed92ac02007-02-06 14:43:52 +01001192 return 1;
1193 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001194#ifdef SO_REUSEPORT
Jens Axboe26e594a2013-01-30 21:52:37 +01001195 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001196 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001197 close(fd);
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001198 return 1;
1199 }
1200#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001201
Jens Axboe531e67a2014-10-09 11:55:16 -06001202 if (set_window_size(td, fd)) {
1203 close(fd);
1204 return 1;
1205 }
Jens Axboe5e34cea2014-10-09 12:05:44 -06001206 if (set_mss(td, fd)) {
1207 close(fd);
1208 return 1;
1209 }
Jens Axboe531e67a2014-10-09 11:55:16 -06001210
Jens Axboe6611e9c2014-01-24 07:36:43 -08001211 if (td->o.filename) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001212 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001213 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1214 close(fd);
1215 return 1;
1216 }
Jens Axboe6611e9c2014-01-24 07:36:43 -08001217 if (is_ipv6(o)) {
1218 log_err("fio: IPv6 not supported for multicast network IO");
1219 close(fd);
1220 return 1;
1221 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001222
1223 inet_aton(td->o.filename, &sin.sin_addr);
1224
1225 mr.imr_multiaddr = sin.sin_addr;
Bruce Cranf16b7402013-10-08 15:05:27 +01001226 if (o->intfc) {
1227 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -05001228 log_err("fio: interface not valid interface IP\n");
1229 close(fd);
1230 return 1;
1231 }
1232 } else {
1233 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1234 }
Jens Axboe6611e9c2014-01-24 07:36:43 -08001235
Bruce Cranf16b7402013-10-08 15:05:27 +01001236 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001237 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1238 close(fd);
1239 return 1;
1240 }
1241 }
1242
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001243 if (!is_ipv6(o)) {
1244 saddr = (struct sockaddr *) &nd->addr;
1245 len = sizeof(nd->addr);
Jens Axboeed92ac02007-02-06 14:43:52 +01001246
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001247 nd->addr.sin_family = AF_INET;
1248 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1249 nd->addr.sin_port = htons(port);
1250 } else {
1251 saddr = (struct sockaddr *) &nd->addr6;
1252 len = sizeof(nd->addr6);
1253
1254 nd->addr6.sin6_family = AF_INET6;
Jens Axboe66f7a562014-04-14 11:57:05 -06001255 nd->addr6.sin6_addr = in6addr_any;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001256 nd->addr6.sin6_port = htons(port);
1257 }
1258
1259 if (bind(fd, saddr, len) < 0) {
Jens Axboed19cedd2014-04-11 11:29:50 -06001260 close(fd);
Jens Axboee1161c32007-02-22 19:36:48 +01001261 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +01001262 return 1;
1263 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001264
1265 nd->listenfd = fd;
1266 return 0;
1267}
1268
Steven Langde890a12011-11-09 14:03:34 +01001269static int fio_netio_setup_listen(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +02001270{
1271 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +01001272 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +02001273 int ret;
1274
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001275 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +01001276 ret = fio_netio_setup_listen_inet(td, o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001277 else
Steven Langde890a12011-11-09 14:03:34 +01001278 ret = fio_netio_setup_listen_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001279
1280 if (ret)
1281 return ret;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001282 if (is_udp(o))
Jens Axboe0fd666b2011-10-06 20:08:53 +02001283 return 0;
1284
1285 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001286 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +02001287 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +01001288 return 1;
1289 }
1290
Jens Axboeb5af8292007-03-08 12:43:13 +01001291 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +01001292}
1293
Jens Axboe9bec88e2007-03-02 08:55:48 +01001294static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +01001295{
Steven Langde890a12011-11-09 14:03:34 +01001296 struct netio_options *o = td->eo;
Jens Axboeaf52b342007-03-13 10:07:47 +01001297 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001298
Bruce Cran3f457be2012-10-10 13:37:41 +01001299#ifdef WIN32
1300 WSADATA wsd;
1301 WSAStartup(MAKEWORD(2,2), &wsd);
1302#endif
1303
Jens Axboe16d55aa2007-05-22 09:21:37 +02001304 if (td_random(td)) {
1305 log_err("fio: network IO can't be random\n");
1306 return 1;
1307 }
Jens Axboeed92ac02007-02-06 14:43:52 +01001308
Steven Langde890a12011-11-09 14:03:34 +01001309 if (o->proto == FIO_TYPE_UNIX && o->port) {
1310 log_err("fio: network IO port not valid with unix socket\n");
1311 return 1;
1312 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1313 log_err("fio: network IO requires port for tcp or udp\n");
1314 return 1;
Jens Axboe414c2a32009-01-16 13:21:15 +01001315 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001316
Jens Axboea9b3c692014-10-09 19:55:21 -06001317 o->port += td->subjob_number;
1318
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001319 if (!is_tcp(o)) {
Steven Langde890a12011-11-09 14:03:34 +01001320 if (o->listen) {
Jens Axboe9b986062011-12-19 08:57:18 +01001321 log_err("fio: listen only valid for TCP proto IO\n");
1322 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001323 }
1324 if (td_rw(td)) {
Jens Axboe9b986062011-12-19 08:57:18 +01001325 log_err("fio: datagram network connections must be"
Steven Langde890a12011-11-09 14:03:34 +01001326 " read OR write\n");
Jens Axboe9b986062011-12-19 08:57:18 +01001327 return 1;
1328 }
1329 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1330 log_err("fio: UNIX sockets need host/filename\n");
1331 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001332 }
1333 o->listen = td_read(td);
1334 }
1335
Steven Langde890a12011-11-09 14:03:34 +01001336 if (o->listen)
1337 ret = fio_netio_setup_listen(td);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001338 else
Steven Langde890a12011-11-09 14:03:34 +01001339 ret = fio_netio_setup_connect(td);
Jens Axboeed92ac02007-02-06 14:43:52 +01001340
Jens Axboe7bb48f82007-03-27 15:30:28 +02001341 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001342}
1343
Jens Axboeb5af8292007-03-08 12:43:13 +01001344static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +01001345{
Jens Axboeb5af8292007-03-08 12:43:13 +01001346 struct netio_data *nd = td->io_ops->data;
1347
1348 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +02001349 if (nd->listenfd != -1)
1350 close(nd->listenfd);
1351 if (nd->pipes[0] != -1)
1352 close(nd->pipes[0]);
1353 if (nd->pipes[1] != -1)
1354 close(nd->pipes[1]);
1355
Jens Axboeb5af8292007-03-08 12:43:13 +01001356 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +01001357 }
1358}
1359
1360static int fio_netio_setup(struct thread_data *td)
1361{
Jens Axboe7bb48f82007-03-27 15:30:28 +02001362 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +01001363
Steven Langde890a12011-11-09 14:03:34 +01001364 if (!td->files_index) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001365 add_file(td, td->o.filename ?: "net", 0, 0);
Steven Langde890a12011-11-09 14:03:34 +01001366 td->o.nr_files = td->o.nr_files ?: 1;
Jens Axboeb53f2c52014-04-08 21:07:12 -06001367 td->o.open_files++;
Steven Langde890a12011-11-09 14:03:34 +01001368 }
1369
Jens Axboe7bb48f82007-03-27 15:30:28 +02001370 if (!td->io_ops->data) {
1371 nd = malloc(sizeof(*nd));;
1372
1373 memset(nd, 0, sizeof(*nd));
1374 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +02001375 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001376 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001377 }
1378
Jens Axboe9bec88e2007-03-02 08:55:48 +01001379 return 0;
1380}
1381
Jens Axboe36d80bc2012-11-30 21:46:06 +01001382static void fio_netio_terminate(struct thread_data *td)
1383{
Jens Axboee5f7caa2014-10-08 14:14:05 -06001384 kill(td->pid, SIGTERM);
Jens Axboe36d80bc2012-11-30 21:46:06 +01001385}
1386
Jens Axboe67bf9822013-01-10 11:23:19 +01001387#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001388static int fio_netio_setup_splice(struct thread_data *td)
1389{
1390 struct netio_data *nd;
1391
1392 fio_netio_setup(td);
1393
1394 nd = td->io_ops->data;
1395 if (nd) {
1396 if (pipe(nd->pipes) < 0)
1397 return 1;
1398
1399 nd->use_splice = 1;
1400 return 0;
1401 }
1402
1403 return 1;
1404}
1405
Jens Axboe5921e802008-05-30 15:02:38 +02001406static struct ioengine_ops ioengine_splice = {
Steven Langde890a12011-11-09 14:03:34 +01001407 .name = "netsplice",
1408 .version = FIO_IOOPS_VERSION,
1409 .prep = fio_netio_prep,
1410 .queue = fio_netio_queue,
1411 .setup = fio_netio_setup_splice,
1412 .init = fio_netio_init,
1413 .cleanup = fio_netio_cleanup,
1414 .open_file = fio_netio_open_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001415 .close_file = fio_netio_close_file,
1416 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001417 .options = options,
1418 .option_struct_size = sizeof(struct netio_options),
1419 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Jens Axboe36d80bc2012-11-30 21:46:06 +01001420 FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +02001421};
1422#endif
1423
Jens Axboe9cce02e2007-06-22 15:42:21 +02001424static struct ioengine_ops ioengine_rw = {
Steven Langde890a12011-11-09 14:03:34 +01001425 .name = "net",
1426 .version = FIO_IOOPS_VERSION,
1427 .prep = fio_netio_prep,
1428 .queue = fio_netio_queue,
1429 .setup = fio_netio_setup,
1430 .init = fio_netio_init,
1431 .cleanup = fio_netio_cleanup,
1432 .open_file = fio_netio_open_file,
1433 .close_file = fio_netio_close_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001434 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001435 .options = options,
1436 .option_struct_size = sizeof(struct netio_options),
1437 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Steven Noonanad705bc2013-04-08 15:05:25 -07001438 FIO_PIPEIO | FIO_BIT_BASED,
Jens Axboeed92ac02007-02-06 14:43:52 +01001439};
1440
Steven Langde890a12011-11-09 14:03:34 +01001441static int str_hostname_cb(void *data, const char *input)
1442{
1443 struct netio_options *o = data;
1444
1445 if (o->td->o.filename)
1446 free(o->td->o.filename);
1447 o->td->o.filename = strdup(input);
1448 return 0;
1449}
1450
Jens Axboeed92ac02007-02-06 14:43:52 +01001451static void fio_init fio_netio_register(void)
1452{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001453 register_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001454#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001455 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001456#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001457}
1458
1459static void fio_exit fio_netio_unregister(void)
1460{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001461 unregister_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001462#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001463 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001464#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001465}