blob: eb72e2eee389857346c22cbe376f1c0c7fe36c9a [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;
29 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010030 struct sockaddr_in addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -080031 struct sockaddr_in6 addr6;
Jens Axboe0fd666b2011-10-06 20:08:53 +020032 struct sockaddr_un addr_un;
Jens Axboeda91d752014-10-09 13:10:14 -060033 uint64_t udp_lost;
34 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;
59};
60
Jens Axboe664fb3b2009-01-19 13:26:36 +010061enum {
62 FIO_LINK_CLOSE = 0x89,
Jens Axboeb96d2432012-11-30 08:27:46 +010063 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
64 FIO_LINK_OPEN = 0x98,
Jens Axboeda91d752014-10-09 13:10:14 -060065 FIO_UDP_SEQ_MAGIC = 0x657375716e556563ULL,
Jens Axboe0fd666b2011-10-06 20:08:53 +020066
67 FIO_TYPE_TCP = 1,
68 FIO_TYPE_UDP = 2,
69 FIO_TYPE_UNIX = 3,
Jens Axboe49ccb8c2014-01-23 16:49:37 -080070 FIO_TYPE_TCP_V6 = 4,
71 FIO_TYPE_UDP_V6 = 5,
Jens Axboe664fb3b2009-01-19 13:26:36 +010072};
73
Steven Langde890a12011-11-09 14:03:34 +010074static int str_hostname_cb(void *data, const char *input);
75static struct fio_option options[] = {
76 {
77 .name = "hostname",
Jens Axboee8b0e952012-03-19 14:37:08 +010078 .lname = "net engine hostname",
Steven Langde890a12011-11-09 14:03:34 +010079 .type = FIO_OPT_STR_STORE,
80 .cb = str_hostname_cb,
81 .help = "Hostname for net IO engine",
Jens Axboee90a0ad2013-04-10 19:43:59 +020082 .category = FIO_OPT_C_ENGINE,
83 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010084 },
85 {
86 .name = "port",
Jens Axboee8b0e952012-03-19 14:37:08 +010087 .lname = "net engine port",
Steven Langde890a12011-11-09 14:03:34 +010088 .type = FIO_OPT_INT,
89 .off1 = offsetof(struct netio_options, port),
90 .minval = 1,
91 .maxval = 65535,
92 .help = "Port to use for TCP or UDP net connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +020093 .category = FIO_OPT_C_ENGINE,
94 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010095 },
96 {
97 .name = "protocol",
Jens Axboee8b0e952012-03-19 14:37:08 +010098 .lname = "net engine protocol",
Steven Langde890a12011-11-09 14:03:34 +010099 .alias = "proto",
100 .type = FIO_OPT_STR,
101 .off1 = offsetof(struct netio_options, proto),
102 .help = "Network protocol to use",
103 .def = "tcp",
104 .posval = {
105 { .ival = "tcp",
106 .oval = FIO_TYPE_TCP,
107 .help = "Transmission Control Protocol",
108 },
Jens Axboeeb232312014-01-24 18:59:15 -0800109#ifdef CONFIG_IPV6
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800110 { .ival = "tcpv6",
111 .oval = FIO_TYPE_TCP_V6,
112 .help = "Transmission Control Protocol V6",
113 },
Jens Axboeeb232312014-01-24 18:59:15 -0800114#endif
Steven Langde890a12011-11-09 14:03:34 +0100115 { .ival = "udp",
116 .oval = FIO_TYPE_UDP,
Bruce Cranf5cc3d02012-10-10 08:17:44 -0600117 .help = "User Datagram Protocol",
Steven Langde890a12011-11-09 14:03:34 +0100118 },
Jens Axboeeb232312014-01-24 18:59:15 -0800119#ifdef CONFIG_IPV6
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800120 { .ival = "udpv6",
121 .oval = FIO_TYPE_UDP_V6,
122 .help = "User Datagram Protocol V6",
123 },
Jens Axboeeb232312014-01-24 18:59:15 -0800124#endif
Steven Langde890a12011-11-09 14:03:34 +0100125 { .ival = "unix",
126 .oval = FIO_TYPE_UNIX,
127 .help = "UNIX domain socket",
128 },
129 },
Jens Axboee90a0ad2013-04-10 19:43:59 +0200130 .category = FIO_OPT_C_ENGINE,
131 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100132 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100133#ifdef CONFIG_TCP_NODELAY
Steven Langde890a12011-11-09 14:03:34 +0100134 {
Steven Noonan70a78782012-11-28 14:52:36 -0800135 .name = "nodelay",
136 .type = FIO_OPT_BOOL,
137 .off1 = offsetof(struct netio_options, nodelay),
138 .help = "Use TCP_NODELAY on TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200139 .category = FIO_OPT_C_ENGINE,
140 .group = FIO_OPT_G_NETIO,
Steven Noonan70a78782012-11-28 14:52:36 -0800141 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100142#endif
Steven Langde890a12011-11-09 14:03:34 +0100143 {
144 .name = "listen",
Jens Axboee8b0e952012-03-19 14:37:08 +0100145 .lname = "net engine listen",
Steven Langde890a12011-11-09 14:03:34 +0100146 .type = FIO_OPT_STR_SET,
147 .off1 = offsetof(struct netio_options, listen),
148 .help = "Listen for incoming TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200149 .category = FIO_OPT_C_ENGINE,
150 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100151 },
152 {
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100153 .name = "pingpong",
154 .type = FIO_OPT_STR_SET,
155 .off1 = offsetof(struct netio_options, pingpong),
156 .help = "Ping-pong IO requests",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200157 .category = FIO_OPT_C_ENGINE,
158 .group = FIO_OPT_G_NETIO,
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100159 },
160 {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500161 .name = "interface",
162 .lname = "net engine interface",
163 .type = FIO_OPT_STR_STORE,
Bruce Cranf16b7402013-10-08 15:05:27 +0100164 .off1 = offsetof(struct netio_options, intfc),
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500165 .help = "Network interface to use",
166 .category = FIO_OPT_C_ENGINE,
167 .group = FIO_OPT_G_NETIO,
168 },
169 {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500170 .name = "ttl",
171 .lname = "net engine multicast ttl",
172 .type = FIO_OPT_INT,
173 .off1 = offsetof(struct netio_options, ttl),
174 .def = "1",
175 .minval = 0,
176 .help = "Time-to-live value for outgoing UDP multicast packets",
177 .category = FIO_OPT_C_ENGINE,
178 .group = FIO_OPT_G_NETIO,
179 },
Jens Axboe531e67a2014-10-09 11:55:16 -0600180#ifdef CONFIG_NET_WINDOWSIZE
181 {
182 .name = "window_size",
183 .lname = "Window Size",
184 .type = FIO_OPT_INT,
185 .off1 = offsetof(struct netio_options, window_size),
186 .minval = 0,
187 .help = "Set socket buffer window size",
188 .category = FIO_OPT_C_ENGINE,
189 .group = FIO_OPT_G_NETIO,
190 },
191#endif
Jens Axboe5e34cea2014-10-09 12:05:44 -0600192#ifdef CONFIG_NET_MSS
193 {
194 .name = "mss",
195 .lname = "Maximum segment size",
196 .type = FIO_OPT_INT,
197 .off1 = offsetof(struct netio_options, mss),
198 .minval = 0,
199 .help = "Set TCP maximum segment size",
200 .category = FIO_OPT_C_ENGINE,
201 .group = FIO_OPT_G_NETIO,
202 },
203#endif
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500204 {
Steven Langde890a12011-11-09 14:03:34 +0100205 .name = NULL,
206 },
207};
208
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800209static inline int is_udp(struct netio_options *o)
210{
211 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
212}
213
214static inline int is_tcp(struct netio_options *o)
215{
216 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
217}
218
219static inline int is_ipv6(struct netio_options *o)
220{
221 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
222}
223
Jens Axboe531e67a2014-10-09 11:55:16 -0600224static int set_window_size(struct thread_data *td, int fd)
225{
226#ifdef CONFIG_NET_WINDOWSIZE
227 struct netio_options *o = td->eo;
228 unsigned int wss;
229 int snd, rcv, ret;
230
231 if (!o->window_size)
232 return 0;
233
234 rcv = o->listen || o->pingpong;
235 snd = !o->listen || o->pingpong;
236 wss = o->window_size;
237 ret = 0;
238
239 if (rcv) {
240 ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &wss,
241 sizeof(wss));
242 if (ret < 0)
243 td_verror(td, errno, "rcvbuf window size");
244 }
245 if (snd && !ret) {
246 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &wss,
247 sizeof(wss));
248 if (ret < 0)
249 td_verror(td, errno, "sndbuf window size");
250 }
251
252 return ret;
253#else
254 td_verror(td, -EINVAL, "setsockopt window size");
255 return -1;
256#endif
257}
258
Jens Axboe5e34cea2014-10-09 12:05:44 -0600259static int set_mss(struct thread_data *td, int fd)
260{
261#ifdef CONFIG_NET_MSS
262 struct netio_options *o = td->eo;
263 unsigned int mss;
264 int ret;
265
266 if (!o->mss || !is_tcp(o))
267 return 0;
268
269 mss = o->mss;
270 ret = setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, (void *) &mss,
271 sizeof(mss));
272 if (ret < 0)
273 td_verror(td, errno, "setsockopt TCP_MAXSEG");
274
275 return ret;
276#else
277 td_verror(td, -EINVAL, "setsockopt TCP_MAXSEG");
278 return -1;
279#endif
280}
281
282
Jens Axboe371d4562009-01-19 10:17:06 +0100283/*
284 * Return -1 for error and 'nr events' for a positive number
285 * of events
286 */
287static int poll_wait(struct thread_data *td, int fd, short events)
288{
289 struct pollfd pfd;
290 int ret;
291
292 while (!td->terminate) {
293 pfd.fd = fd;
294 pfd.events = events;
295 ret = poll(&pfd, 1, -1);
296 if (ret < 0) {
297 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +0100298 break;
Jens Axboe371d4562009-01-19 10:17:06 +0100299
300 td_verror(td, errno, "poll");
301 return -1;
302 } else if (!ret)
303 continue;
304
305 break;
306 }
307
308 if (pfd.revents & events)
309 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +0100310
311 return -1;
312}
313
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500314static int fio_netio_is_multicast(const char *mcaddr)
315{
316 in_addr_t addr = inet_network(mcaddr);
317 if (addr == -1)
318 return 0;
319
320 if (inet_network("224.0.0.0") <= addr &&
321 inet_network("239.255.255.255") >= addr)
322 return 1;
323
324 return 0;
325}
326
327
Jens Axboeed92ac02007-02-06 14:43:52 +0100328static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
329{
Steven Langde890a12011-11-09 14:03:34 +0100330 struct netio_options *o = td->eo;
Jens Axboeed92ac02007-02-06 14:43:52 +0100331
Jens Axboe7a6499d2007-02-07 09:35:29 +0100332 /*
333 * Make sure we don't see spurious reads to a receiver, and vice versa
334 */
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800335 if (is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +0100336 return 0;
337
338 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
339 (!o->listen && io_u->ddir == DDIR_READ)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100340 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +0100341 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100342 }
Bruce Cran3f457be2012-10-10 13:37:41 +0100343
Jens Axboef85ac252008-03-01 18:09:49 +0100344 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100345}
346
Jens Axboe67bf9822013-01-10 11:23:19 +0100347#ifdef CONFIG_LINUX_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +0200348static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200349{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200350 int bytes = 0;
351
352 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200353 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200354
355 if (ret < 0) {
356 if (!bytes)
357 bytes = ret;
358
359 break;
360 } else if (!ret)
361 break;
362
363 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200364 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200365 }
366
367 return bytes;
368}
369
370/*
Jens Axboecd963e12007-06-24 21:41:46 +0200371 * Receive bytes from a socket and fill them into the internal pipe
372 */
373static int splice_in(struct thread_data *td, struct io_u *io_u)
374{
375 struct netio_data *nd = td->io_ops->data;
376
377 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
378}
379
380/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200381 * Transmit 'len' bytes from the internal pipe
382 */
383static int splice_out(struct thread_data *td, struct io_u *io_u,
384 unsigned int len)
385{
386 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200387
388 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
389}
390
391static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
392{
393 struct iovec iov = {
394 .iov_base = io_u->xfer_buf,
395 .iov_len = len,
396 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200397 int bytes = 0;
398
Jens Axboecd963e12007-06-24 21:41:46 +0200399 while (iov.iov_len) {
400 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200401
402 if (ret < 0) {
403 if (!bytes)
404 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200405 break;
406 } else if (!ret)
407 break;
408
Jens Axboecd963e12007-06-24 21:41:46 +0200409 iov.iov_len -= ret;
410 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200411 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200412 }
413
414 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200415
Jens Axboe9cce02e2007-06-22 15:42:21 +0200416}
417
418/*
419 * vmsplice() pipe to io_u buffer
420 */
421static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
422 unsigned int len)
423{
424 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200425
Jens Axboecd963e12007-06-24 21:41:46 +0200426 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200427}
428
429/*
430 * vmsplice() io_u to pipe
431 */
432static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
433{
434 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200435
Jens Axboecd963e12007-06-24 21:41:46 +0200436 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200437}
438
Jens Axboecd963e12007-06-24 21:41:46 +0200439/*
440 * splice receive - transfer socket data into a pipe using splice, then map
441 * that pipe data into the io_u using vmsplice.
442 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200443static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
444{
445 int ret;
446
447 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200448 if (ret > 0)
449 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200450
Jens Axboecd963e12007-06-24 21:41:46 +0200451 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200452}
453
Jens Axboecd963e12007-06-24 21:41:46 +0200454/*
455 * splice transmit - map data from the io_u into a pipe by using vmsplice,
456 * then transfer that pipe to a socket using splice.
457 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200458static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
459{
460 int ret;
461
462 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200463 if (ret > 0)
464 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200465
Jens Axboecd963e12007-06-24 21:41:46 +0200466 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200467}
Jens Axboe5921e802008-05-30 15:02:38 +0200468#else
469static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
470{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200471 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200472 return -1;
473}
474
475static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
476{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200477 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200478 return -1;
479}
480#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200481
Jens Axboeda91d752014-10-09 13:10:14 -0600482static void store_udp_seq(struct netio_data *nd, struct io_u *io_u)
483{
484 struct udp_seq *us;
485
486 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
487 us->magic = cpu_to_le64(FIO_UDP_SEQ_MAGIC);
488 us->seq = cpu_to_le64(nd->udp_send_seq++);
489}
490
491static void verify_udp_seq(struct netio_data *nd, struct io_u *io_u)
492{
493 struct udp_seq *us;
494 uint64_t seq;
495
496 us = io_u->xfer_buf + io_u->xfer_buflen - sizeof(*us);
497 if (le64_to_cpu(us->magic) != FIO_UDP_SEQ_MAGIC)
498 return;
499
500 seq = le64_to_cpu(us->seq);
501
502 if (seq != nd->udp_recv_seq)
503 nd->udp_lost += seq - nd->udp_recv_seq;
504
505 nd->udp_recv_seq = seq + 1;
506}
507
Jens Axboe9cce02e2007-06-22 15:42:21 +0200508static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
509{
Jens Axboe414c2a32009-01-16 13:21:15 +0100510 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100511 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100512 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100513
Jens Axboe664fb3b2009-01-19 13:26:36 +0100514 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800515 if (is_udp(o)) {
Jens Axboe10aa1362014-04-01 21:10:36 -0600516 const struct sockaddr *to;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800517 socklen_t len;
518
519 if (is_ipv6(o)) {
520 to = (struct sockaddr *) &nd->addr6;
521 len = sizeof(nd->addr6);
522 } else {
523 to = (struct sockaddr *) &nd->addr;
524 len = sizeof(nd->addr);
525 }
Jens Axboe62b38922009-05-11 10:37:33 +0200526
Jens Axboeda91d752014-10-09 13:10:14 -0600527 if (td->o.verify == VERIFY_NONE)
528 store_udp_seq(nd, io_u);
529
Jens Axboe664fb3b2009-01-19 13:26:36 +0100530 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800531 io_u->xfer_buflen, flags, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100532 } else {
533 /*
534 * if we are going to write more, set MSG_MORE
535 */
Jens Axboe5921e802008-05-30 15:02:38 +0200536#ifdef MSG_MORE
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100537 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
538 td->o.size) && !o->pingpong)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100539 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200540#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100541 ret = send(io_u->file->fd, io_u->xfer_buf,
542 io_u->xfer_buflen, flags);
543 }
544 if (ret > 0)
545 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200546
Jens Axboe664fb3b2009-01-19 13:26:36 +0100547 ret = poll_wait(td, io_u->file->fd, POLLOUT);
548 if (ret <= 0)
549 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100550 } while (1);
551
552 return ret;
553}
554
555static int is_udp_close(struct io_u *io_u, int len)
556{
557 struct udp_close_msg *msg;
558
559 if (len != sizeof(struct udp_close_msg))
560 return 0;
561
562 msg = io_u->xfer_buf;
Jens Axboeb96d2432012-11-30 08:27:46 +0100563 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100564 return 0;
565 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
566 return 0;
567
568 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200569}
570
Jens Axboe414c2a32009-01-16 13:21:15 +0100571static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200572{
Jens Axboe414c2a32009-01-16 13:21:15 +0100573 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100574 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100575 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100576
Jens Axboe664fb3b2009-01-19 13:26:36 +0100577 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800578 if (is_udp(o)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500579 struct sockaddr *from;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800580 socklen_t l, *len = &l;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500581
582 if (o->listen) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800583 if (!is_ipv6(o)) {
584 from = (struct sockaddr *) &nd->addr;
585 *len = sizeof(nd->addr);
586 } else {
587 from = (struct sockaddr *) &nd->addr6;
588 *len = sizeof(nd->addr6);
589 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500590 } else {
591 from = NULL;
592 len = NULL;
593 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200594
Jens Axboe664fb3b2009-01-19 13:26:36 +0100595 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500596 io_u->xfer_buflen, flags, from, len);
Jens Axboeda91d752014-10-09 13:10:14 -0600597
Jens Axboe664fb3b2009-01-19 13:26:36 +0100598 if (is_udp_close(io_u, ret)) {
599 td->done = 1;
600 return 0;
601 }
602 } else {
603 ret = recv(io_u->file->fd, io_u->xfer_buf,
604 io_u->xfer_buflen, flags);
605 }
606 if (ret > 0)
607 break;
Jens Axboe7d988f62012-11-29 19:57:35 +0100608 else if (!ret && (flags & MSG_WAITALL))
609 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100610
Jens Axboe664fb3b2009-01-19 13:26:36 +0100611 ret = poll_wait(td, io_u->file->fd, POLLIN);
612 if (ret <= 0)
613 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100614 flags |= MSG_WAITALL;
615 } while (1);
616
Jens Axboeda91d752014-10-09 13:10:14 -0600617 if (is_udp(o) && td->o.verify == VERIFY_NONE)
618 verify_udp_seq(nd, io_u);
619
Jens Axboe664fb3b2009-01-19 13:26:36 +0100620 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200621}
622
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100623static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
624 enum fio_ddir ddir)
Jens Axboeed92ac02007-02-06 14:43:52 +0100625{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200626 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100627 struct netio_options *o = td->eo;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200628 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100629
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100630 if (ddir == DDIR_WRITE) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800631 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100632 o->proto == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200633 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200634 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100635 ret = fio_netio_splice_out(td, io_u);
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100636 } else if (ddir == DDIR_READ) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800637 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100638 o->proto == FIO_TYPE_UNIX)
Jens Axboe414c2a32009-01-16 13:21:15 +0100639 ret = fio_netio_recv(td, io_u);
640 else
641 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100642 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100643 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100644
Jens Axboecec6b552007-02-06 20:15:38 +0100645 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe0ecdd7f2014-10-08 17:04:29 -0600646 if (ret > 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100647 io_u->resid = io_u->xfer_buflen - ret;
648 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100649 return FIO_Q_COMPLETED;
Jens Axboe0ecdd7f2014-10-08 17:04:29 -0600650 } else if (!ret)
651 return FIO_Q_BUSY;
652 else {
Jens Axboe414c2a32009-01-16 13:21:15 +0100653 int err = errno;
654
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100655 if (ddir == DDIR_WRITE && err == EMSGSIZE)
Jens Axboe414c2a32009-01-16 13:21:15 +0100656 return FIO_Q_BUSY;
657
658 io_u->error = err;
659 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100660 }
661
Jens Axboe36167d82007-02-18 05:41:31 +0100662 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100663 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100664
Jens Axboe36167d82007-02-18 05:41:31 +0100665 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100666}
667
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100668static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
669{
670 struct netio_options *o = td->eo;
671 int ret;
672
673 fio_ro_check(td, io_u);
674
675 ret = __fio_netio_queue(td, io_u, io_u->ddir);
676 if (!o->pingpong || ret != FIO_Q_COMPLETED)
677 return ret;
678
679 /*
680 * For ping-pong mode, receive or send reply as needed
681 */
682 if (td_read(td) && io_u->ddir == DDIR_READ)
683 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
684 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
685 ret = __fio_netio_queue(td, io_u, DDIR_READ);
686
687 return ret;
688}
689
Jens Axboeb5af8292007-03-08 12:43:13 +0100690static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100691{
Jens Axboeb5af8292007-03-08 12:43:13 +0100692 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100693 struct netio_options *o = td->eo;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100694 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100695
Steven Langde890a12011-11-09 14:03:34 +0100696 if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200697 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100698 type = SOCK_STREAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800699 } else if (o->proto == FIO_TYPE_TCP_V6) {
700 domain = AF_INET6;
701 type = SOCK_STREAM;
Steven Langde890a12011-11-09 14:03:34 +0100702 } else if (o->proto == FIO_TYPE_UDP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200703 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100704 type = SOCK_DGRAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800705 } else if (o->proto == FIO_TYPE_UDP_V6) {
706 domain = AF_INET6;
707 type = SOCK_DGRAM;
Steven Langde890a12011-11-09 14:03:34 +0100708 } else if (o->proto == FIO_TYPE_UNIX) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200709 domain = AF_UNIX;
710 type = SOCK_STREAM;
711 } else {
Steven Langde890a12011-11-09 14:03:34 +0100712 log_err("fio: bad network type %d\n", o->proto);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200713 f->fd = -1;
714 return 1;
715 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100716
Jens Axboe0fd666b2011-10-06 20:08:53 +0200717 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100718 if (f->fd < 0) {
719 td_verror(td, errno, "socket");
720 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100721 }
722
Jens Axboe1eafa372013-01-31 10:19:51 +0100723#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800724 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100725 int optval = 1;
726
Jens Axboe26e594a2013-01-30 21:52:37 +0100727 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800728 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
729 return 1;
730 }
731 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100732#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800733
Jens Axboe531e67a2014-10-09 11:55:16 -0600734 if (set_window_size(td, f->fd)) {
735 close(f->fd);
736 return 1;
737 }
Jens Axboe5e34cea2014-10-09 12:05:44 -0600738 if (set_mss(td, f->fd)) {
739 close(f->fd);
740 return 1;
741 }
Jens Axboe531e67a2014-10-09 11:55:16 -0600742
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800743 if (is_udp(o)) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500744 if (!fio_netio_is_multicast(td->o.filename))
745 return 0;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800746 if (is_ipv6(o)) {
747 log_err("fio: multicast not supported on IPv6\n");
748 close(f->fd);
749 return 1;
750 }
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500751
Bruce Cranf16b7402013-10-08 15:05:27 +0100752 if (o->intfc) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500753 struct in_addr interface_addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800754
Bruce Cranf16b7402013-10-08 15:05:27 +0100755 if (inet_aton(o->intfc, &interface_addr) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500756 log_err("fio: interface not valid interface IP\n");
757 close(f->fd);
758 return 1;
759 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100760 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500761 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
762 close(f->fd);
763 return 1;
764 }
765 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100766 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500767 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
768 close(f->fd);
769 return 1;
770 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100771 return 0;
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500772 } else if (o->proto == FIO_TYPE_TCP) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100773 socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100774
Jens Axboe0fd666b2011-10-06 20:08:53 +0200775 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
776 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200777 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200778 return 1;
779 }
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800780 } else if (o->proto == FIO_TYPE_TCP_V6) {
781 socklen_t len = sizeof(nd->addr6);
782
783 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
784 td_verror(td, errno, "connect");
785 close(f->fd);
786 return 1;
787 }
788
Jens Axboe0fd666b2011-10-06 20:08:53 +0200789 } else {
790 struct sockaddr_un *addr = &nd->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100791 socklen_t len;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200792
793 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
794
795 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
796 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200797 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200798 return 1;
799 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100800 }
801
802 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100803}
804
Jens Axboeb5af8292007-03-08 12:43:13 +0100805static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100806{
Jens Axboeb5af8292007-03-08 12:43:13 +0100807 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100808 struct netio_options *o = td->eo;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800809 socklen_t socklen;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100810 int state;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100811
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800812 if (is_udp(o)) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100813 f->fd = nd->listenfd;
814 return 0;
815 }
816
Jens Axboe859088d2012-11-29 20:02:50 +0100817 state = td->runstate;
818 td_set_runstate(td, TD_SETTING_UP);
819
Jens Axboe6d861442007-03-15 09:22:23 +0100820 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100821
Jens Axboe371d4562009-01-19 10:17:06 +0100822 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
Jens Axboe859088d2012-11-29 20:02:50 +0100823 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100824
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800825 if (o->proto == FIO_TYPE_TCP) {
826 socklen = sizeof(nd->addr);
827 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
828 } else {
829 socklen = sizeof(nd->addr6);
830 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
831 }
832
Jens Axboe371d4562009-01-19 10:17:06 +0100833 if (f->fd < 0) {
834 td_verror(td, errno, "accept");
Jens Axboe859088d2012-11-29 20:02:50 +0100835 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100836 }
837
Jens Axboe1eafa372013-01-31 10:19:51 +0100838#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800839 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100840 int optval = 1;
841
Jens Axboe26e594a2013-01-30 21:52:37 +0100842 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800843 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
844 return 1;
845 }
846 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100847#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800848
Jens Axboe0cae16f2012-11-30 16:22:31 +0100849 reset_all_stats(td);
Jens Axboe859088d2012-11-29 20:02:50 +0100850 td_set_runstate(td, state);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100851 return 0;
Jens Axboe859088d2012-11-29 20:02:50 +0100852err:
853 td_set_runstate(td, state);
854 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100855}
856
Jens Axboe664fb3b2009-01-19 13:26:36 +0100857static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
858{
859 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800860 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100861 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800862 struct sockaddr *to;
863 socklen_t len;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100864 int ret;
865
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800866 if (is_ipv6(o)) {
867 to = (struct sockaddr *) &nd->addr6;
868 len = sizeof(nd->addr6);
869 } else {
870 to = (struct sockaddr *) &nd->addr;
871 len = sizeof(nd->addr);
872 }
873
Jens Axboeb96d2432012-11-30 08:27:46 +0100874 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100875 msg.cmd = htonl(FIO_LINK_CLOSE);
876
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800877 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100878 if (ret < 0)
879 td_verror(td, errno, "sendto udp link close");
880}
881
882static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
883{
Steven Langde890a12011-11-09 14:03:34 +0100884 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100885
886 /*
887 * If this is an UDP connection, notify the receiver that we are
888 * closing down the link
889 */
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800890 if (is_udp(o))
Jens Axboe664fb3b2009-01-19 13:26:36 +0100891 fio_netio_udp_close(td, f);
892
893 return generic_close_file(td, f);
894}
895
Jens Axboeb96d2432012-11-30 08:27:46 +0100896static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
897{
898 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800899 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100900 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800901 struct sockaddr *to;
902 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100903 int ret;
904
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800905 if (is_ipv6(o)) {
906 len = sizeof(nd->addr6);
907 to = (struct sockaddr *) &nd->addr6;
908 } else {
909 len = sizeof(nd->addr);
910 to = (struct sockaddr *) &nd->addr;
911 }
912
Jens Axboe1f819912013-01-23 17:21:41 -0700913 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100914 if (ret < 0) {
Shawn Bohreree7062f2013-07-19 13:24:09 -0500915 td_verror(td, errno, "recvfrom udp link open");
Jens Axboeb96d2432012-11-30 08:27:46 +0100916 return ret;
917 }
918
919 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
920 ntohl(msg.cmd) != FIO_LINK_OPEN) {
921 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
922 ntohl(msg.cmd));
923 return -1;
924 }
925
Jens Axboeda91d752014-10-09 13:10:14 -0600926 fio_gettime(&td->start, NULL);
Jens Axboeb96d2432012-11-30 08:27:46 +0100927 return 0;
928}
929
930static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
931{
932 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800933 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100934 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800935 struct sockaddr *to;
936 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100937 int ret;
938
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800939 if (is_ipv6(o)) {
940 len = sizeof(nd->addr6);
941 to = (struct sockaddr *) &nd->addr6;
942 } else {
943 len = sizeof(nd->addr);
944 to = (struct sockaddr *) &nd->addr;
945 }
946
Jens Axboeb96d2432012-11-30 08:27:46 +0100947 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
948 msg.cmd = htonl(FIO_LINK_OPEN);
949
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800950 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100951 if (ret < 0) {
952 td_verror(td, errno, "sendto udp link open");
953 return ret;
954 }
955
956 return 0;
957}
958
959static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
960{
961 int ret;
962 struct netio_options *o = td->eo;
963
964 if (o->listen)
965 ret = fio_netio_accept(td, f);
966 else
967 ret = fio_netio_connect(td, f);
968
969 if (ret) {
970 f->fd = -1;
971 return ret;
972 }
973
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800974 if (is_udp(o)) {
Jens Axboeb96d2432012-11-30 08:27:46 +0100975 if (td_write(td))
976 ret = fio_netio_udp_send_open(td, f);
977 else {
978 int state;
979
980 state = td->runstate;
981 td_set_runstate(td, TD_SETTING_UP);
982 ret = fio_netio_udp_recv_open(td, f);
983 td_set_runstate(td, state);
984 }
985 }
986
987 if (ret)
988 fio_netio_close_file(td, f);
989
990 return ret;
991}
992
Jens Axboe0b783342014-01-23 20:19:17 -0800993static int fio_fill_addr(struct thread_data *td, const char *host, int af,
994 void *dst, struct addrinfo **res)
995{
996 struct netio_options *o = td->eo;
997 struct addrinfo hints;
998 int ret;
999
1000 if (inet_pton(af, host, dst))
1001 return 0;
1002
1003 memset(&hints, 0, sizeof(hints));
Jens Axboe0b783342014-01-23 20:19:17 -08001004
1005 if (is_tcp(o))
1006 hints.ai_socktype = SOCK_STREAM;
1007 else
1008 hints.ai_socktype = SOCK_DGRAM;
1009
Jens Axboeb1b1be22014-01-23 20:41:33 -08001010 if (is_ipv6(o))
1011 hints.ai_family = AF_INET6;
1012 else
1013 hints.ai_family = AF_INET;
1014
Jens Axboe0b783342014-01-23 20:19:17 -08001015 ret = getaddrinfo(host, NULL, &hints, res);
1016 if (ret) {
1017 int e = EINVAL;
1018 char str[128];
1019
1020 if (ret == EAI_SYSTEM)
1021 e = errno;
1022
1023 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
1024 td_verror(td, e, str);
1025 return 1;
1026 }
1027
1028 return 0;
1029}
1030
Jens Axboe0fd666b2011-10-06 20:08:53 +02001031static int fio_netio_setup_connect_inet(struct thread_data *td,
1032 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +01001033{
1034 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001035 struct netio_options *o = td->eo;
Jens Axboe0b783342014-01-23 20:19:17 -08001036 struct addrinfo *res = NULL;
1037 void *dst, *src;
1038 int af, len;
Jens Axboeb5af8292007-03-08 12:43:13 +01001039
Jens Axboe166dce42012-11-29 14:35:33 +01001040 if (!host) {
1041 log_err("fio: connect with no host to connect to.\n");
1042 if (td_read(td))
1043 log_err("fio: did you forget to set 'listen'?\n");
1044
1045 td_verror(td, EINVAL, "no hostname= set");
1046 return 1;
1047 }
1048
Jens Axboe0b783342014-01-23 20:19:17 -08001049 nd->addr.sin_family = AF_INET;
1050 nd->addr.sin_port = htons(port);
1051 nd->addr6.sin6_family = AF_INET6;
1052 nd->addr6.sin6_port = htons(port);
1053
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001054 if (is_ipv6(o)) {
Jens Axboe0b783342014-01-23 20:19:17 -08001055 af = AF_INET6;
Jens Axboe68631b32014-02-25 13:43:38 -08001056 dst = &nd->addr6.sin6_addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001057 } else {
Jens Axboe0b783342014-01-23 20:19:17 -08001058 af = AF_INET;
Jens Axboe68631b32014-02-25 13:43:38 -08001059 dst = &nd->addr.sin_addr;
Jens Axboeb5af8292007-03-08 12:43:13 +01001060 }
1061
Jens Axboe0b783342014-01-23 20:19:17 -08001062 if (fio_fill_addr(td, host, af, dst, &res))
1063 return 1;
1064
1065 if (!res)
1066 return 0;
1067
1068 if (is_ipv6(o)) {
1069 len = sizeof(nd->addr6.sin6_addr);
1070 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
1071 } else {
1072 len = sizeof(nd->addr.sin_addr);
1073 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
1074 }
1075
1076 memcpy(dst, src, len);
1077 freeaddrinfo(res);
Jens Axboeb5af8292007-03-08 12:43:13 +01001078 return 0;
1079}
1080
Jens Axboe0fd666b2011-10-06 20:08:53 +02001081static int fio_netio_setup_connect_unix(struct thread_data *td,
1082 const char *path)
1083{
1084 struct netio_data *nd = td->io_ops->data;
1085 struct sockaddr_un *soun = &nd->addr_un;
1086
1087 soun->sun_family = AF_UNIX;
Jens Axboe4b159fa2014-04-14 08:49:04 -06001088 memset(soun->sun_path, 0, sizeof(soun->sun_path));
1089 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001090 return 0;
1091}
1092
Steven Langde890a12011-11-09 14:03:34 +01001093static int fio_netio_setup_connect(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +02001094{
Steven Langde890a12011-11-09 14:03:34 +01001095 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +02001096
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001097 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +01001098 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001099 else
Steven Langde890a12011-11-09 14:03:34 +01001100 return fio_netio_setup_connect_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001101}
1102
1103static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
1104{
1105 struct netio_data *nd = td->io_ops->data;
1106 struct sockaddr_un *addr = &nd->addr_un;
1107 mode_t mode;
1108 int len, fd;
1109
1110 fd = socket(AF_UNIX, SOCK_STREAM, 0);
1111 if (fd < 0) {
1112 log_err("fio: socket: %s\n", strerror(errno));
1113 return -1;
1114 }
1115
1116 mode = umask(000);
1117
1118 memset(addr, 0, sizeof(*addr));
1119 addr->sun_family = AF_UNIX;
Jens Axboe4b159fa2014-04-14 08:49:04 -06001120 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001121 unlink(path);
1122
1123 len = sizeof(addr->sun_family) + strlen(path) + 1;
1124
1125 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
1126 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +02001127 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001128 return -1;
1129 }
1130
1131 umask(mode);
1132 nd->listenfd = fd;
1133 return 0;
1134}
1135
1136static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +01001137{
1138 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +01001139 struct netio_options *o = td->eo;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001140 struct ip_mreq mr;
1141 struct sockaddr_in sin;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001142 struct sockaddr *saddr;
1143 int fd, opt, type, domain;
1144 socklen_t len;
Jens Axboeed92ac02007-02-06 14:43:52 +01001145
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001146 memset(&sin, 0, sizeof(sin));
Jens Axboe414c2a32009-01-16 13:21:15 +01001147
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001148 if (o->proto == FIO_TYPE_TCP) {
1149 type = SOCK_STREAM;
1150 domain = AF_INET;
1151 } else if (o->proto == FIO_TYPE_TCP_V6) {
1152 type = SOCK_STREAM;
1153 domain = AF_INET6;
1154 } else if (o->proto == FIO_TYPE_UDP) {
1155 type = SOCK_DGRAM;
1156 domain = AF_INET;
1157 } else if (o->proto == FIO_TYPE_UDP_V6) {
1158 type = SOCK_DGRAM;
1159 domain = AF_INET6;
1160 } else {
1161 log_err("fio: unknown proto %d\n", o->proto);
1162 return 1;
1163 }
1164
1165 fd = socket(domain, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +01001166 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001167 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +01001168 return 1;
1169 }
1170
1171 opt = 1;
Jens Axboe26e594a2013-01-30 21:52:37 +01001172 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001173 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001174 close(fd);
Jens Axboeed92ac02007-02-06 14:43:52 +01001175 return 1;
1176 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001177#ifdef SO_REUSEPORT
Jens Axboe26e594a2013-01-30 21:52:37 +01001178 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001179 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001180 close(fd);
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001181 return 1;
1182 }
1183#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001184
Jens Axboe531e67a2014-10-09 11:55:16 -06001185 if (set_window_size(td, fd)) {
1186 close(fd);
1187 return 1;
1188 }
Jens Axboe5e34cea2014-10-09 12:05:44 -06001189 if (set_mss(td, fd)) {
1190 close(fd);
1191 return 1;
1192 }
Jens Axboe531e67a2014-10-09 11:55:16 -06001193
Jens Axboe6611e9c2014-01-24 07:36:43 -08001194 if (td->o.filename) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001195 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001196 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1197 close(fd);
1198 return 1;
1199 }
Jens Axboe6611e9c2014-01-24 07:36:43 -08001200 if (is_ipv6(o)) {
1201 log_err("fio: IPv6 not supported for multicast network IO");
1202 close(fd);
1203 return 1;
1204 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001205
1206 inet_aton(td->o.filename, &sin.sin_addr);
1207
1208 mr.imr_multiaddr = sin.sin_addr;
Bruce Cranf16b7402013-10-08 15:05:27 +01001209 if (o->intfc) {
1210 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -05001211 log_err("fio: interface not valid interface IP\n");
1212 close(fd);
1213 return 1;
1214 }
1215 } else {
1216 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1217 }
Jens Axboe6611e9c2014-01-24 07:36:43 -08001218
Bruce Cranf16b7402013-10-08 15:05:27 +01001219 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001220 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1221 close(fd);
1222 return 1;
1223 }
1224 }
1225
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001226 if (!is_ipv6(o)) {
1227 saddr = (struct sockaddr *) &nd->addr;
1228 len = sizeof(nd->addr);
Jens Axboeed92ac02007-02-06 14:43:52 +01001229
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001230 nd->addr.sin_family = AF_INET;
1231 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1232 nd->addr.sin_port = htons(port);
1233 } else {
1234 saddr = (struct sockaddr *) &nd->addr6;
1235 len = sizeof(nd->addr6);
1236
1237 nd->addr6.sin6_family = AF_INET6;
Jens Axboe66f7a562014-04-14 11:57:05 -06001238 nd->addr6.sin6_addr = in6addr_any;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001239 nd->addr6.sin6_port = htons(port);
1240 }
1241
1242 if (bind(fd, saddr, len) < 0) {
Jens Axboed19cedd2014-04-11 11:29:50 -06001243 close(fd);
Jens Axboee1161c32007-02-22 19:36:48 +01001244 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +01001245 return 1;
1246 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001247
1248 nd->listenfd = fd;
1249 return 0;
1250}
1251
Steven Langde890a12011-11-09 14:03:34 +01001252static int fio_netio_setup_listen(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +02001253{
1254 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +01001255 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +02001256 int ret;
1257
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001258 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +01001259 ret = fio_netio_setup_listen_inet(td, o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001260 else
Steven Langde890a12011-11-09 14:03:34 +01001261 ret = fio_netio_setup_listen_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001262
1263 if (ret)
1264 return ret;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001265 if (is_udp(o))
Jens Axboe0fd666b2011-10-06 20:08:53 +02001266 return 0;
1267
1268 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001269 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +02001270 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +01001271 return 1;
1272 }
1273
Jens Axboeb5af8292007-03-08 12:43:13 +01001274 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +01001275}
1276
Jens Axboe9bec88e2007-03-02 08:55:48 +01001277static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +01001278{
Steven Langde890a12011-11-09 14:03:34 +01001279 struct netio_options *o = td->eo;
Jens Axboeaf52b342007-03-13 10:07:47 +01001280 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001281
Bruce Cran3f457be2012-10-10 13:37:41 +01001282#ifdef WIN32
1283 WSADATA wsd;
1284 WSAStartup(MAKEWORD(2,2), &wsd);
1285#endif
1286
Jens Axboe16d55aa2007-05-22 09:21:37 +02001287 if (td_random(td)) {
1288 log_err("fio: network IO can't be random\n");
1289 return 1;
1290 }
Jens Axboeed92ac02007-02-06 14:43:52 +01001291
Steven Langde890a12011-11-09 14:03:34 +01001292 if (o->proto == FIO_TYPE_UNIX && o->port) {
1293 log_err("fio: network IO port not valid with unix socket\n");
1294 return 1;
1295 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1296 log_err("fio: network IO requires port for tcp or udp\n");
1297 return 1;
Jens Axboe414c2a32009-01-16 13:21:15 +01001298 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001299
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001300 if (!is_tcp(o)) {
Steven Langde890a12011-11-09 14:03:34 +01001301 if (o->listen) {
Jens Axboe9b986062011-12-19 08:57:18 +01001302 log_err("fio: listen only valid for TCP proto IO\n");
1303 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001304 }
1305 if (td_rw(td)) {
Jens Axboe9b986062011-12-19 08:57:18 +01001306 log_err("fio: datagram network connections must be"
Steven Langde890a12011-11-09 14:03:34 +01001307 " read OR write\n");
Jens Axboe9b986062011-12-19 08:57:18 +01001308 return 1;
1309 }
1310 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1311 log_err("fio: UNIX sockets need host/filename\n");
1312 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001313 }
1314 o->listen = td_read(td);
1315 }
1316
Steven Langde890a12011-11-09 14:03:34 +01001317 if (o->listen)
1318 ret = fio_netio_setup_listen(td);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001319 else
Steven Langde890a12011-11-09 14:03:34 +01001320 ret = fio_netio_setup_connect(td);
Jens Axboeed92ac02007-02-06 14:43:52 +01001321
Jens Axboe7bb48f82007-03-27 15:30:28 +02001322 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001323}
1324
Jens Axboeb5af8292007-03-08 12:43:13 +01001325static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +01001326{
Jens Axboeb5af8292007-03-08 12:43:13 +01001327 struct netio_data *nd = td->io_ops->data;
1328
1329 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +02001330 if (nd->listenfd != -1)
1331 close(nd->listenfd);
1332 if (nd->pipes[0] != -1)
1333 close(nd->pipes[0]);
1334 if (nd->pipes[1] != -1)
1335 close(nd->pipes[1]);
1336
Jens Axboeb5af8292007-03-08 12:43:13 +01001337 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +01001338 }
1339}
1340
1341static int fio_netio_setup(struct thread_data *td)
1342{
Jens Axboe7bb48f82007-03-27 15:30:28 +02001343 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +01001344
Steven Langde890a12011-11-09 14:03:34 +01001345 if (!td->files_index) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001346 add_file(td, td->o.filename ?: "net", 0, 0);
Steven Langde890a12011-11-09 14:03:34 +01001347 td->o.nr_files = td->o.nr_files ?: 1;
Jens Axboeb53f2c52014-04-08 21:07:12 -06001348 td->o.open_files++;
Steven Langde890a12011-11-09 14:03:34 +01001349 }
1350
Jens Axboe7bb48f82007-03-27 15:30:28 +02001351 if (!td->io_ops->data) {
1352 nd = malloc(sizeof(*nd));;
1353
1354 memset(nd, 0, sizeof(*nd));
1355 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +02001356 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001357 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001358 }
1359
Jens Axboe9bec88e2007-03-02 08:55:48 +01001360 return 0;
1361}
1362
Jens Axboe36d80bc2012-11-30 21:46:06 +01001363static void fio_netio_terminate(struct thread_data *td)
1364{
Jens Axboee5f7caa2014-10-08 14:14:05 -06001365 kill(td->pid, SIGTERM);
Jens Axboe36d80bc2012-11-30 21:46:06 +01001366}
1367
Jens Axboe67bf9822013-01-10 11:23:19 +01001368#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001369static int fio_netio_setup_splice(struct thread_data *td)
1370{
1371 struct netio_data *nd;
1372
1373 fio_netio_setup(td);
1374
1375 nd = td->io_ops->data;
1376 if (nd) {
1377 if (pipe(nd->pipes) < 0)
1378 return 1;
1379
1380 nd->use_splice = 1;
1381 return 0;
1382 }
1383
1384 return 1;
1385}
1386
Jens Axboe5921e802008-05-30 15:02:38 +02001387static struct ioengine_ops ioengine_splice = {
Steven Langde890a12011-11-09 14:03:34 +01001388 .name = "netsplice",
1389 .version = FIO_IOOPS_VERSION,
1390 .prep = fio_netio_prep,
1391 .queue = fio_netio_queue,
1392 .setup = fio_netio_setup_splice,
1393 .init = fio_netio_init,
1394 .cleanup = fio_netio_cleanup,
1395 .open_file = fio_netio_open_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001396 .close_file = fio_netio_close_file,
1397 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001398 .options = options,
1399 .option_struct_size = sizeof(struct netio_options),
1400 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Jens Axboe36d80bc2012-11-30 21:46:06 +01001401 FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +02001402};
1403#endif
1404
Jens Axboe9cce02e2007-06-22 15:42:21 +02001405static struct ioengine_ops ioengine_rw = {
Steven Langde890a12011-11-09 14:03:34 +01001406 .name = "net",
1407 .version = FIO_IOOPS_VERSION,
1408 .prep = fio_netio_prep,
1409 .queue = fio_netio_queue,
1410 .setup = fio_netio_setup,
1411 .init = fio_netio_init,
1412 .cleanup = fio_netio_cleanup,
1413 .open_file = fio_netio_open_file,
1414 .close_file = fio_netio_close_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001415 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001416 .options = options,
1417 .option_struct_size = sizeof(struct netio_options),
1418 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Steven Noonanad705bc2013-04-08 15:05:25 -07001419 FIO_PIPEIO | FIO_BIT_BASED,
Jens Axboeed92ac02007-02-06 14:43:52 +01001420};
1421
Steven Langde890a12011-11-09 14:03:34 +01001422static int str_hostname_cb(void *data, const char *input)
1423{
1424 struct netio_options *o = data;
1425
1426 if (o->td->o.filename)
1427 free(o->td->o.filename);
1428 o->td->o.filename = strdup(input);
1429 return 0;
1430}
1431
Jens Axboeed92ac02007-02-06 14:43:52 +01001432static void fio_init fio_netio_register(void)
1433{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001434 register_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001435#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001436 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001437#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001438}
1439
1440static void fio_exit fio_netio_unregister(void)
1441{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001442 unregister_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001443#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001444 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001445#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001446}