blob: 0c90e1cac74f77d80dbdae4280c691bda06ec864 [file] [log] [blame]
Jens Axboeed92ac02007-02-06 14:43:52 +01001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * net engine
3 *
4 * IO engine that reads/writes to/from sockets.
5 *
Jens Axboeed92ac02007-02-06 14:43:52 +01006 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
Steven Noonan842805f2012-12-01 08:16:55 +000010#include <signal.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010011#include <errno.h>
12#include <assert.h>
13#include <netinet/in.h>
Steven Noonan70a78782012-11-28 14:52:36 -080014#include <netinet/tcp.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010015#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboe5fdd1242007-02-11 04:00:37 +010017#include <sys/poll.h>
Jens Axboe72920562008-06-02 12:30:06 +020018#include <sys/types.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020019#include <sys/stat.h>
Jens Axboe72920562008-06-02 12:30:06 +020020#include <sys/socket.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020021#include <sys/un.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010022
23#include "../fio.h"
Jens Axboeed92ac02007-02-06 14:43:52 +010024
Jens Axboeb5af8292007-03-08 12:43:13 +010025struct netio_data {
26 int listenfd;
Jens Axboe9cce02e2007-06-22 15:42:21 +020027 int use_splice;
28 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010029 struct sockaddr_in addr;
Jens Axboe0fd666b2011-10-06 20:08:53 +020030 struct sockaddr_un addr_un;
Jens Axboeb5af8292007-03-08 12:43:13 +010031};
Jens Axboeed92ac02007-02-06 14:43:52 +010032
Steven Langde890a12011-11-09 14:03:34 +010033struct netio_options {
34 struct thread_data *td;
35 unsigned int port;
36 unsigned int proto;
37 unsigned int listen;
Jens Axboe6f73a7f2012-11-30 09:59:20 +010038 unsigned int pingpong;
Steven Noonan70a78782012-11-28 14:52:36 -080039 unsigned int nodelay;
Shawn Bohrerd3a623d2013-07-19 13:24:08 -050040 unsigned int ttl;
Shawn Bohrerb93b6a22013-07-19 13:24:07 -050041 char * interface;
Steven Langde890a12011-11-09 14:03:34 +010042};
43
Jens Axboe664fb3b2009-01-19 13:26:36 +010044struct udp_close_msg {
45 uint32_t magic;
46 uint32_t cmd;
47};
48
49enum {
50 FIO_LINK_CLOSE = 0x89,
Jens Axboeb96d2432012-11-30 08:27:46 +010051 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
52 FIO_LINK_OPEN = 0x98,
Jens Axboe0fd666b2011-10-06 20:08:53 +020053
54 FIO_TYPE_TCP = 1,
55 FIO_TYPE_UDP = 2,
56 FIO_TYPE_UNIX = 3,
Jens Axboe664fb3b2009-01-19 13:26:36 +010057};
58
Steven Langde890a12011-11-09 14:03:34 +010059static int str_hostname_cb(void *data, const char *input);
60static struct fio_option options[] = {
61 {
62 .name = "hostname",
Jens Axboee8b0e952012-03-19 14:37:08 +010063 .lname = "net engine hostname",
Steven Langde890a12011-11-09 14:03:34 +010064 .type = FIO_OPT_STR_STORE,
65 .cb = str_hostname_cb,
66 .help = "Hostname for net IO engine",
Jens Axboee90a0ad2013-04-10 19:43:59 +020067 .category = FIO_OPT_C_ENGINE,
68 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010069 },
70 {
71 .name = "port",
Jens Axboee8b0e952012-03-19 14:37:08 +010072 .lname = "net engine port",
Steven Langde890a12011-11-09 14:03:34 +010073 .type = FIO_OPT_INT,
74 .off1 = offsetof(struct netio_options, port),
75 .minval = 1,
76 .maxval = 65535,
77 .help = "Port to use for TCP or UDP net connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +020078 .category = FIO_OPT_C_ENGINE,
79 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010080 },
81 {
82 .name = "protocol",
Jens Axboee8b0e952012-03-19 14:37:08 +010083 .lname = "net engine protocol",
Steven Langde890a12011-11-09 14:03:34 +010084 .alias = "proto",
85 .type = FIO_OPT_STR,
86 .off1 = offsetof(struct netio_options, proto),
87 .help = "Network protocol to use",
88 .def = "tcp",
89 .posval = {
90 { .ival = "tcp",
91 .oval = FIO_TYPE_TCP,
92 .help = "Transmission Control Protocol",
93 },
94 { .ival = "udp",
95 .oval = FIO_TYPE_UDP,
Bruce Cranf5cc3d02012-10-10 08:17:44 -060096 .help = "User Datagram Protocol",
Steven Langde890a12011-11-09 14:03:34 +010097 },
98 { .ival = "unix",
99 .oval = FIO_TYPE_UNIX,
100 .help = "UNIX domain socket",
101 },
102 },
Jens Axboee90a0ad2013-04-10 19:43:59 +0200103 .category = FIO_OPT_C_ENGINE,
104 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100105 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100106#ifdef CONFIG_TCP_NODELAY
Steven Langde890a12011-11-09 14:03:34 +0100107 {
Steven Noonan70a78782012-11-28 14:52:36 -0800108 .name = "nodelay",
109 .type = FIO_OPT_BOOL,
110 .off1 = offsetof(struct netio_options, nodelay),
111 .help = "Use TCP_NODELAY on TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200112 .category = FIO_OPT_C_ENGINE,
113 .group = FIO_OPT_G_NETIO,
Steven Noonan70a78782012-11-28 14:52:36 -0800114 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100115#endif
Steven Langde890a12011-11-09 14:03:34 +0100116 {
117 .name = "listen",
Jens Axboee8b0e952012-03-19 14:37:08 +0100118 .lname = "net engine listen",
Steven Langde890a12011-11-09 14:03:34 +0100119 .type = FIO_OPT_STR_SET,
120 .off1 = offsetof(struct netio_options, listen),
121 .help = "Listen for incoming TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200122 .category = FIO_OPT_C_ENGINE,
123 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100124 },
125 {
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100126 .name = "pingpong",
127 .type = FIO_OPT_STR_SET,
128 .off1 = offsetof(struct netio_options, pingpong),
129 .help = "Ping-pong IO requests",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200130 .category = FIO_OPT_C_ENGINE,
131 .group = FIO_OPT_G_NETIO,
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100132 },
133 {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500134 .name = "interface",
135 .lname = "net engine interface",
136 .type = FIO_OPT_STR_STORE,
137 .off1 = offsetof(struct netio_options, interface),
138 .help = "Network interface to use",
139 .category = FIO_OPT_C_ENGINE,
140 .group = FIO_OPT_G_NETIO,
141 },
142 {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500143 .name = "ttl",
144 .lname = "net engine multicast ttl",
145 .type = FIO_OPT_INT,
146 .off1 = offsetof(struct netio_options, ttl),
147 .def = "1",
148 .minval = 0,
149 .help = "Time-to-live value for outgoing UDP multicast packets",
150 .category = FIO_OPT_C_ENGINE,
151 .group = FIO_OPT_G_NETIO,
152 },
153 {
Steven Langde890a12011-11-09 14:03:34 +0100154 .name = NULL,
155 },
156};
157
Jens Axboe371d4562009-01-19 10:17:06 +0100158/*
159 * Return -1 for error and 'nr events' for a positive number
160 * of events
161 */
162static int poll_wait(struct thread_data *td, int fd, short events)
163{
164 struct pollfd pfd;
165 int ret;
166
167 while (!td->terminate) {
168 pfd.fd = fd;
169 pfd.events = events;
170 ret = poll(&pfd, 1, -1);
171 if (ret < 0) {
172 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +0100173 break;
Jens Axboe371d4562009-01-19 10:17:06 +0100174
175 td_verror(td, errno, "poll");
176 return -1;
177 } else if (!ret)
178 continue;
179
180 break;
181 }
182
183 if (pfd.revents & events)
184 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +0100185
186 return -1;
187}
188
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500189static int fio_netio_is_multicast(const char *mcaddr)
190{
191 in_addr_t addr = inet_network(mcaddr);
192 if (addr == -1)
193 return 0;
194
195 if (inet_network("224.0.0.0") <= addr &&
196 inet_network("239.255.255.255") >= addr)
197 return 1;
198
199 return 0;
200}
201
202
Jens Axboeed92ac02007-02-06 14:43:52 +0100203static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
204{
Steven Langde890a12011-11-09 14:03:34 +0100205 struct netio_options *o = td->eo;
Jens Axboeed92ac02007-02-06 14:43:52 +0100206
Jens Axboe7a6499d2007-02-07 09:35:29 +0100207 /*
208 * Make sure we don't see spurious reads to a receiver, and vice versa
209 */
Steven Langde890a12011-11-09 14:03:34 +0100210 if (o->proto == FIO_TYPE_TCP)
211 return 0;
212
213 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
214 (!o->listen && io_u->ddir == DDIR_READ)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100215 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +0100216 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100217 }
Bruce Cran3f457be2012-10-10 13:37:41 +0100218
Jens Axboef85ac252008-03-01 18:09:49 +0100219 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100220}
221
Jens Axboe67bf9822013-01-10 11:23:19 +0100222#ifdef CONFIG_LINUX_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +0200223static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200224{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200225 int bytes = 0;
226
227 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200228 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200229
230 if (ret < 0) {
231 if (!bytes)
232 bytes = ret;
233
234 break;
235 } else if (!ret)
236 break;
237
238 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200239 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200240 }
241
242 return bytes;
243}
244
245/*
Jens Axboecd963e12007-06-24 21:41:46 +0200246 * Receive bytes from a socket and fill them into the internal pipe
247 */
248static int splice_in(struct thread_data *td, struct io_u *io_u)
249{
250 struct netio_data *nd = td->io_ops->data;
251
252 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
253}
254
255/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200256 * Transmit 'len' bytes from the internal pipe
257 */
258static int splice_out(struct thread_data *td, struct io_u *io_u,
259 unsigned int len)
260{
261 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200262
263 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
264}
265
266static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
267{
268 struct iovec iov = {
269 .iov_base = io_u->xfer_buf,
270 .iov_len = len,
271 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200272 int bytes = 0;
273
Jens Axboecd963e12007-06-24 21:41:46 +0200274 while (iov.iov_len) {
275 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200276
277 if (ret < 0) {
278 if (!bytes)
279 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200280 break;
281 } else if (!ret)
282 break;
283
Jens Axboecd963e12007-06-24 21:41:46 +0200284 iov.iov_len -= ret;
285 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200286 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200287 }
288
289 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200290
Jens Axboe9cce02e2007-06-22 15:42:21 +0200291}
292
293/*
294 * vmsplice() pipe to io_u buffer
295 */
296static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
297 unsigned int len)
298{
299 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200300
Jens Axboecd963e12007-06-24 21:41:46 +0200301 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200302}
303
304/*
305 * vmsplice() io_u to pipe
306 */
307static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
308{
309 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200310
Jens Axboecd963e12007-06-24 21:41:46 +0200311 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200312}
313
Jens Axboecd963e12007-06-24 21:41:46 +0200314/*
315 * splice receive - transfer socket data into a pipe using splice, then map
316 * that pipe data into the io_u using vmsplice.
317 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200318static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
319{
320 int ret;
321
322 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200323 if (ret > 0)
324 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200325
Jens Axboecd963e12007-06-24 21:41:46 +0200326 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200327}
328
Jens Axboecd963e12007-06-24 21:41:46 +0200329/*
330 * splice transmit - map data from the io_u into a pipe by using vmsplice,
331 * then transfer that pipe to a socket using splice.
332 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200333static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
334{
335 int ret;
336
337 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200338 if (ret > 0)
339 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200340
Jens Axboecd963e12007-06-24 21:41:46 +0200341 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200342}
Jens Axboe5921e802008-05-30 15:02:38 +0200343#else
344static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
345{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200346 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200347 return -1;
348}
349
350static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
351{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200352 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200353 return -1;
354}
355#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200356
357static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
358{
Jens Axboe414c2a32009-01-16 13:21:15 +0100359 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100360 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100361 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100362
Jens Axboe664fb3b2009-01-19 13:26:36 +0100363 do {
Steven Langde890a12011-11-09 14:03:34 +0100364 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe62b38922009-05-11 10:37:33 +0200365 struct sockaddr *to = (struct sockaddr *) &nd->addr;
366
Jens Axboe664fb3b2009-01-19 13:26:36 +0100367 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200368 io_u->xfer_buflen, flags, to,
369 sizeof(*to));
Jens Axboe664fb3b2009-01-19 13:26:36 +0100370 } else {
371 /*
372 * if we are going to write more, set MSG_MORE
373 */
Jens Axboe5921e802008-05-30 15:02:38 +0200374#ifdef MSG_MORE
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100375 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
376 td->o.size) && !o->pingpong)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100377 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200378#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100379 ret = send(io_u->file->fd, io_u->xfer_buf,
380 io_u->xfer_buflen, flags);
381 }
382 if (ret > 0)
383 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200384
Jens Axboe664fb3b2009-01-19 13:26:36 +0100385 ret = poll_wait(td, io_u->file->fd, POLLOUT);
386 if (ret <= 0)
387 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100388 } while (1);
389
390 return ret;
391}
392
393static int is_udp_close(struct io_u *io_u, int len)
394{
395 struct udp_close_msg *msg;
396
397 if (len != sizeof(struct udp_close_msg))
398 return 0;
399
400 msg = io_u->xfer_buf;
Jens Axboeb96d2432012-11-30 08:27:46 +0100401 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100402 return 0;
403 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
404 return 0;
405
406 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200407}
408
Jens Axboe414c2a32009-01-16 13:21:15 +0100409static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200410{
Jens Axboe414c2a32009-01-16 13:21:15 +0100411 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100412 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100413 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100414
Jens Axboe664fb3b2009-01-19 13:26:36 +0100415 do {
Steven Langde890a12011-11-09 14:03:34 +0100416 if (o->proto == FIO_TYPE_UDP) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500417 socklen_t l;
418 socklen_t *len = &l;
419 struct sockaddr *from;
420
421 if (o->listen) {
422 from = (struct sockaddr *) &nd->addr;
423 *len = sizeof(nd->addr);
424 } else {
425 from = NULL;
426 len = NULL;
427 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200428
Jens Axboe664fb3b2009-01-19 13:26:36 +0100429 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500430 io_u->xfer_buflen, flags, from, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100431 if (is_udp_close(io_u, ret)) {
432 td->done = 1;
433 return 0;
434 }
435 } else {
436 ret = recv(io_u->file->fd, io_u->xfer_buf,
437 io_u->xfer_buflen, flags);
438 }
439 if (ret > 0)
440 break;
Jens Axboe7d988f62012-11-29 19:57:35 +0100441 else if (!ret && (flags & MSG_WAITALL))
442 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100443
Jens Axboe664fb3b2009-01-19 13:26:36 +0100444 ret = poll_wait(td, io_u->file->fd, POLLIN);
445 if (ret <= 0)
446 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100447 flags |= MSG_WAITALL;
448 } while (1);
449
450 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200451}
452
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100453static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
454 enum fio_ddir ddir)
Jens Axboeed92ac02007-02-06 14:43:52 +0100455{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200456 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100457 struct netio_options *o = td->eo;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200458 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100459
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100460 if (ddir == DDIR_WRITE) {
Steven Langde890a12011-11-09 14:03:34 +0100461 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
462 o->proto == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200463 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200464 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100465 ret = fio_netio_splice_out(td, io_u);
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100466 } else if (ddir == DDIR_READ) {
Steven Langde890a12011-11-09 14:03:34 +0100467 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
468 o->proto == FIO_TYPE_UNIX)
Jens Axboe414c2a32009-01-16 13:21:15 +0100469 ret = fio_netio_recv(td, io_u);
470 else
471 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100472 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100473 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100474
Jens Axboecec6b552007-02-06 20:15:38 +0100475 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100476 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100477 io_u->resid = io_u->xfer_buflen - ret;
478 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100479 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100480 } else {
481 int err = errno;
482
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100483 if (ddir == DDIR_WRITE && err == EMSGSIZE)
Jens Axboe414c2a32009-01-16 13:21:15 +0100484 return FIO_Q_BUSY;
485
486 io_u->error = err;
487 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100488 }
489
Jens Axboe36167d82007-02-18 05:41:31 +0100490 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100491 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100492
Jens Axboe36167d82007-02-18 05:41:31 +0100493 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100494}
495
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100496static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
497{
498 struct netio_options *o = td->eo;
499 int ret;
500
501 fio_ro_check(td, io_u);
502
503 ret = __fio_netio_queue(td, io_u, io_u->ddir);
504 if (!o->pingpong || ret != FIO_Q_COMPLETED)
505 return ret;
506
507 /*
508 * For ping-pong mode, receive or send reply as needed
509 */
510 if (td_read(td) && io_u->ddir == DDIR_READ)
511 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
512 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
513 ret = __fio_netio_queue(td, io_u, DDIR_READ);
514
515 return ret;
516}
517
Jens Axboeb5af8292007-03-08 12:43:13 +0100518static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100519{
Jens Axboeb5af8292007-03-08 12:43:13 +0100520 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100521 struct netio_options *o = td->eo;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100522 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100523
Steven Langde890a12011-11-09 14:03:34 +0100524 if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200525 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100526 type = SOCK_STREAM;
Steven Langde890a12011-11-09 14:03:34 +0100527 } else if (o->proto == FIO_TYPE_UDP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200528 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100529 type = SOCK_DGRAM;
Steven Langde890a12011-11-09 14:03:34 +0100530 } else if (o->proto == FIO_TYPE_UNIX) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200531 domain = AF_UNIX;
532 type = SOCK_STREAM;
533 } else {
Steven Langde890a12011-11-09 14:03:34 +0100534 log_err("fio: bad network type %d\n", o->proto);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200535 f->fd = -1;
536 return 1;
537 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100538
Jens Axboe0fd666b2011-10-06 20:08:53 +0200539 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100540 if (f->fd < 0) {
541 td_verror(td, errno, "socket");
542 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100543 }
544
Jens Axboe1eafa372013-01-31 10:19:51 +0100545#ifdef CONFIG_TCP_NODELAY
Steven Noonan70a78782012-11-28 14:52:36 -0800546 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100547 int optval = 1;
548
Jens Axboe26e594a2013-01-30 21:52:37 +0100549 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800550 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
551 return 1;
552 }
553 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100554#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800555
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500556 if (o->proto == FIO_TYPE_UDP) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500557 if (!fio_netio_is_multicast(td->o.filename))
558 return 0;
559
560 if (o->interface) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500561 struct in_addr interface_addr;
562 if (inet_aton(o->interface, &interface_addr) == 0) {
563 log_err("fio: interface not valid interface IP\n");
564 close(f->fd);
565 return 1;
566 }
567 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, sizeof(interface_addr)) < 0) {
568 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
569 close(f->fd);
570 return 1;
571 }
572 }
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500573 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, &o->ttl, sizeof(o->ttl)) < 0) {
574 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
575 close(f->fd);
576 return 1;
577 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100578 return 0;
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500579 } else if (o->proto == FIO_TYPE_TCP) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100580 socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100581
Jens Axboe0fd666b2011-10-06 20:08:53 +0200582 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
583 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200584 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200585 return 1;
586 }
587 } else {
588 struct sockaddr_un *addr = &nd->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100589 socklen_t len;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200590
591 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
592
593 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
594 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200595 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200596 return 1;
597 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100598 }
599
600 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100601}
602
Jens Axboeb5af8292007-03-08 12:43:13 +0100603static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100604{
Jens Axboeb5af8292007-03-08 12:43:13 +0100605 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100606 struct netio_options *o = td->eo;
Jens Axboe67bf9822013-01-10 11:23:19 +0100607 socklen_t socklen = sizeof(nd->addr);
Jens Axboe6264c7a2013-02-28 08:24:23 +0100608 int state;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100609
Steven Langde890a12011-11-09 14:03:34 +0100610 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100611 f->fd = nd->listenfd;
612 return 0;
613 }
614
Jens Axboe859088d2012-11-29 20:02:50 +0100615 state = td->runstate;
616 td_set_runstate(td, TD_SETTING_UP);
617
Jens Axboe6d861442007-03-15 09:22:23 +0100618 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100619
Jens Axboe371d4562009-01-19 10:17:06 +0100620 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
Jens Axboe859088d2012-11-29 20:02:50 +0100621 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100622
Jens Axboe371d4562009-01-19 10:17:06 +0100623 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
624 if (f->fd < 0) {
625 td_verror(td, errno, "accept");
Jens Axboe859088d2012-11-29 20:02:50 +0100626 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100627 }
628
Jens Axboe1eafa372013-01-31 10:19:51 +0100629#ifdef CONFIG_TCP_NODELAY
Steven Noonan70a78782012-11-28 14:52:36 -0800630 if (o->nodelay && o->proto == FIO_TYPE_TCP) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100631 int optval = 1;
632
Jens Axboe26e594a2013-01-30 21:52:37 +0100633 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800634 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
635 return 1;
636 }
637 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100638#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800639
Jens Axboe0cae16f2012-11-30 16:22:31 +0100640 reset_all_stats(td);
Jens Axboe859088d2012-11-29 20:02:50 +0100641 td_set_runstate(td, state);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100642 return 0;
Jens Axboe859088d2012-11-29 20:02:50 +0100643err:
644 td_set_runstate(td, state);
645 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100646}
647
Jens Axboe664fb3b2009-01-19 13:26:36 +0100648static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
649{
650 struct netio_data *nd = td->io_ops->data;
651 struct udp_close_msg msg;
Jens Axboe62b38922009-05-11 10:37:33 +0200652 struct sockaddr *to = (struct sockaddr *) &nd->addr;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100653 int ret;
654
Jens Axboeb96d2432012-11-30 08:27:46 +0100655 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100656 msg.cmd = htonl(FIO_LINK_CLOSE);
657
Jens Axboe1f819912013-01-23 17:21:41 -0700658 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
Jens Axboe664fb3b2009-01-19 13:26:36 +0100659 sizeof(nd->addr));
660 if (ret < 0)
661 td_verror(td, errno, "sendto udp link close");
662}
663
664static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
665{
Steven Langde890a12011-11-09 14:03:34 +0100666 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100667
668 /*
669 * If this is an UDP connection, notify the receiver that we are
670 * closing down the link
671 */
Steven Langde890a12011-11-09 14:03:34 +0100672 if (o->proto == FIO_TYPE_UDP)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100673 fio_netio_udp_close(td, f);
674
675 return generic_close_file(td, f);
676}
677
Jens Axboeb96d2432012-11-30 08:27:46 +0100678static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
679{
680 struct netio_data *nd = td->io_ops->data;
681 struct udp_close_msg msg;
682 struct sockaddr *to = (struct sockaddr *) &nd->addr;
Jens Axboe67bf9822013-01-10 11:23:19 +0100683 socklen_t len = sizeof(nd->addr);
Jens Axboeb96d2432012-11-30 08:27:46 +0100684 int ret;
685
Jens Axboe1f819912013-01-23 17:21:41 -0700686 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100687 if (ret < 0) {
Shawn Bohreree7062f2013-07-19 13:24:09 -0500688 td_verror(td, errno, "recvfrom udp link open");
Jens Axboeb96d2432012-11-30 08:27:46 +0100689 return ret;
690 }
691
692 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
693 ntohl(msg.cmd) != FIO_LINK_OPEN) {
694 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
695 ntohl(msg.cmd));
696 return -1;
697 }
698
699 return 0;
700}
701
702static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
703{
704 struct netio_data *nd = td->io_ops->data;
705 struct udp_close_msg msg;
706 struct sockaddr *to = (struct sockaddr *) &nd->addr;
707 int ret;
708
709 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
710 msg.cmd = htonl(FIO_LINK_OPEN);
711
Jens Axboe1f819912013-01-23 17:21:41 -0700712 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to,
Jens Axboeb96d2432012-11-30 08:27:46 +0100713 sizeof(nd->addr));
714 if (ret < 0) {
715 td_verror(td, errno, "sendto udp link open");
716 return ret;
717 }
718
719 return 0;
720}
721
722static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
723{
724 int ret;
725 struct netio_options *o = td->eo;
726
727 if (o->listen)
728 ret = fio_netio_accept(td, f);
729 else
730 ret = fio_netio_connect(td, f);
731
732 if (ret) {
733 f->fd = -1;
734 return ret;
735 }
736
737 if (o->proto == FIO_TYPE_UDP) {
738 if (td_write(td))
739 ret = fio_netio_udp_send_open(td, f);
740 else {
741 int state;
742
743 state = td->runstate;
744 td_set_runstate(td, TD_SETTING_UP);
745 ret = fio_netio_udp_recv_open(td, f);
746 td_set_runstate(td, state);
747 }
748 }
749
750 if (ret)
751 fio_netio_close_file(td, f);
752
753 return ret;
754}
755
Jens Axboe0fd666b2011-10-06 20:08:53 +0200756static int fio_netio_setup_connect_inet(struct thread_data *td,
757 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100758{
759 struct netio_data *nd = td->io_ops->data;
760
Jens Axboe166dce42012-11-29 14:35:33 +0100761 if (!host) {
762 log_err("fio: connect with no host to connect to.\n");
763 if (td_read(td))
764 log_err("fio: did you forget to set 'listen'?\n");
765
766 td_verror(td, EINVAL, "no hostname= set");
767 return 1;
768 }
769
Jens Axboeb5af8292007-03-08 12:43:13 +0100770 nd->addr.sin_family = AF_INET;
771 nd->addr.sin_port = htons(port);
772
773 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
774 struct hostent *hent;
775
776 hent = gethostbyname(host);
777 if (!hent) {
778 td_verror(td, errno, "gethostbyname");
779 return 1;
780 }
781
782 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
783 }
784
785 return 0;
786}
787
Jens Axboe0fd666b2011-10-06 20:08:53 +0200788static int fio_netio_setup_connect_unix(struct thread_data *td,
789 const char *path)
790{
791 struct netio_data *nd = td->io_ops->data;
792 struct sockaddr_un *soun = &nd->addr_un;
793
794 soun->sun_family = AF_UNIX;
795 strcpy(soun->sun_path, path);
796 return 0;
797}
798
Steven Langde890a12011-11-09 14:03:34 +0100799static int fio_netio_setup_connect(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200800{
Steven Langde890a12011-11-09 14:03:34 +0100801 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200802
Steven Langde890a12011-11-09 14:03:34 +0100803 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
804 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200805 else
Steven Langde890a12011-11-09 14:03:34 +0100806 return fio_netio_setup_connect_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200807}
808
809static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
810{
811 struct netio_data *nd = td->io_ops->data;
812 struct sockaddr_un *addr = &nd->addr_un;
813 mode_t mode;
814 int len, fd;
815
816 fd = socket(AF_UNIX, SOCK_STREAM, 0);
817 if (fd < 0) {
818 log_err("fio: socket: %s\n", strerror(errno));
819 return -1;
820 }
821
822 mode = umask(000);
823
824 memset(addr, 0, sizeof(*addr));
825 addr->sun_family = AF_UNIX;
826 strcpy(addr->sun_path, path);
827 unlink(path);
828
829 len = sizeof(addr->sun_family) + strlen(path) + 1;
830
831 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
832 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200833 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200834 return -1;
835 }
836
837 umask(mode);
838 nd->listenfd = fd;
839 return 0;
840}
841
842static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100843{
844 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100845 struct netio_options *o = td->eo;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500846 struct ip_mreq mr;
847 struct sockaddr_in sin;
Jens Axboe414c2a32009-01-16 13:21:15 +0100848 int fd, opt, type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100849
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500850 memset(&sin, 0, sizeof(sin));
Steven Langde890a12011-11-09 14:03:34 +0100851 if (o->proto == FIO_TYPE_TCP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100852 type = SOCK_STREAM;
853 else
854 type = SOCK_DGRAM;
855
Jens Axboe0fd666b2011-10-06 20:08:53 +0200856 fd = socket(AF_INET, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +0100857 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100858 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100859 return 1;
860 }
861
862 opt = 1;
Jens Axboe26e594a2013-01-30 21:52:37 +0100863 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100864 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -0500865 close(fd);
Jens Axboeed92ac02007-02-06 14:43:52 +0100866 return 1;
867 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100868#ifdef SO_REUSEPORT
Jens Axboe26e594a2013-01-30 21:52:37 +0100869 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100870 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -0500871 close(fd);
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100872 return 1;
873 }
874#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100875
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500876 if (td->o.filename){
877 if(o->proto != FIO_TYPE_UDP ||
878 !fio_netio_is_multicast(td->o.filename)) {
879 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
880 close(fd);
881 return 1;
882 }
883
884 inet_aton(td->o.filename, &sin.sin_addr);
885
886 mr.imr_multiaddr = sin.sin_addr;
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500887 if (o->interface) {
888 if (inet_aton(o->interface, &mr.imr_interface) == 0) {
889 log_err("fio: interface not valid interface IP\n");
890 close(fd);
891 return 1;
892 }
893 } else {
894 mr.imr_interface.s_addr = htonl(INADDR_ANY);
895 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500896 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0) {
897 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
898 close(fd);
899 return 1;
900 }
901 }
902
Jens Axboeb5af8292007-03-08 12:43:13 +0100903 nd->addr.sin_family = AF_INET;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500904 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
Jens Axboeb5af8292007-03-08 12:43:13 +0100905 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100906
Jens Axboeb5af8292007-03-08 12:43:13 +0100907 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100908 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100909 return 1;
910 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200911
912 nd->listenfd = fd;
913 return 0;
914}
915
Steven Langde890a12011-11-09 14:03:34 +0100916static int fio_netio_setup_listen(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200917{
918 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100919 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200920 int ret;
921
Steven Langde890a12011-11-09 14:03:34 +0100922 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
923 ret = fio_netio_setup_listen_inet(td, o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200924 else
Steven Langde890a12011-11-09 14:03:34 +0100925 ret = fio_netio_setup_listen_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200926
927 if (ret)
928 return ret;
Steven Langde890a12011-11-09 14:03:34 +0100929 if (o->proto == FIO_TYPE_UDP)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200930 return 0;
931
932 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100933 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +0200934 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100935 return 1;
936 }
937
Jens Axboeb5af8292007-03-08 12:43:13 +0100938 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100939}
940
Jens Axboe9bec88e2007-03-02 08:55:48 +0100941static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100942{
Steven Langde890a12011-11-09 14:03:34 +0100943 struct netio_options *o = td->eo;
Jens Axboeaf52b342007-03-13 10:07:47 +0100944 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100945
Bruce Cran3f457be2012-10-10 13:37:41 +0100946#ifdef WIN32
947 WSADATA wsd;
948 WSAStartup(MAKEWORD(2,2), &wsd);
949#endif
950
Jens Axboe16d55aa2007-05-22 09:21:37 +0200951 if (td_random(td)) {
952 log_err("fio: network IO can't be random\n");
953 return 1;
954 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100955
Steven Langde890a12011-11-09 14:03:34 +0100956 if (o->proto == FIO_TYPE_UNIX && o->port) {
957 log_err("fio: network IO port not valid with unix socket\n");
958 return 1;
959 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
960 log_err("fio: network IO requires port for tcp or udp\n");
961 return 1;
Jens Axboe414c2a32009-01-16 13:21:15 +0100962 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200963
Steven Langde890a12011-11-09 14:03:34 +0100964 if (o->proto != FIO_TYPE_TCP) {
965 if (o->listen) {
Jens Axboe9b986062011-12-19 08:57:18 +0100966 log_err("fio: listen only valid for TCP proto IO\n");
967 return 1;
Steven Langde890a12011-11-09 14:03:34 +0100968 }
969 if (td_rw(td)) {
Jens Axboe9b986062011-12-19 08:57:18 +0100970 log_err("fio: datagram network connections must be"
Steven Langde890a12011-11-09 14:03:34 +0100971 " read OR write\n");
Jens Axboe9b986062011-12-19 08:57:18 +0100972 return 1;
973 }
974 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
975 log_err("fio: UNIX sockets need host/filename\n");
976 return 1;
Steven Langde890a12011-11-09 14:03:34 +0100977 }
978 o->listen = td_read(td);
979 }
980
Steven Langde890a12011-11-09 14:03:34 +0100981 if (o->listen)
982 ret = fio_netio_setup_listen(td);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200983 else
Steven Langde890a12011-11-09 14:03:34 +0100984 ret = fio_netio_setup_connect(td);
Jens Axboeed92ac02007-02-06 14:43:52 +0100985
Jens Axboe7bb48f82007-03-27 15:30:28 +0200986 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100987}
988
Jens Axboeb5af8292007-03-08 12:43:13 +0100989static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100990{
Jens Axboeb5af8292007-03-08 12:43:13 +0100991 struct netio_data *nd = td->io_ops->data;
992
993 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +0200994 if (nd->listenfd != -1)
995 close(nd->listenfd);
996 if (nd->pipes[0] != -1)
997 close(nd->pipes[0]);
998 if (nd->pipes[1] != -1)
999 close(nd->pipes[1]);
1000
Jens Axboeb5af8292007-03-08 12:43:13 +01001001 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +01001002 }
1003}
1004
1005static int fio_netio_setup(struct thread_data *td)
1006{
Jens Axboe7bb48f82007-03-27 15:30:28 +02001007 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +01001008
Steven Langde890a12011-11-09 14:03:34 +01001009 if (!td->files_index) {
1010 add_file(td, td->o.filename ?: "net");
1011 td->o.nr_files = td->o.nr_files ?: 1;
1012 }
1013
Jens Axboe7bb48f82007-03-27 15:30:28 +02001014 if (!td->io_ops->data) {
1015 nd = malloc(sizeof(*nd));;
1016
1017 memset(nd, 0, sizeof(*nd));
1018 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +02001019 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001020 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001021 }
1022
Jens Axboe9bec88e2007-03-02 08:55:48 +01001023 return 0;
1024}
1025
Jens Axboe36d80bc2012-11-30 21:46:06 +01001026static void fio_netio_terminate(struct thread_data *td)
1027{
1028 kill(td->pid, SIGUSR2);
1029}
1030
Jens Axboe67bf9822013-01-10 11:23:19 +01001031#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001032static int fio_netio_setup_splice(struct thread_data *td)
1033{
1034 struct netio_data *nd;
1035
1036 fio_netio_setup(td);
1037
1038 nd = td->io_ops->data;
1039 if (nd) {
1040 if (pipe(nd->pipes) < 0)
1041 return 1;
1042
1043 nd->use_splice = 1;
1044 return 0;
1045 }
1046
1047 return 1;
1048}
1049
Jens Axboe5921e802008-05-30 15:02:38 +02001050static struct ioengine_ops ioengine_splice = {
Steven Langde890a12011-11-09 14:03:34 +01001051 .name = "netsplice",
1052 .version = FIO_IOOPS_VERSION,
1053 .prep = fio_netio_prep,
1054 .queue = fio_netio_queue,
1055 .setup = fio_netio_setup_splice,
1056 .init = fio_netio_init,
1057 .cleanup = fio_netio_cleanup,
1058 .open_file = fio_netio_open_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001059 .close_file = fio_netio_close_file,
1060 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001061 .options = options,
1062 .option_struct_size = sizeof(struct netio_options),
1063 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Jens Axboe36d80bc2012-11-30 21:46:06 +01001064 FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +02001065};
1066#endif
1067
Jens Axboe9cce02e2007-06-22 15:42:21 +02001068static struct ioengine_ops ioengine_rw = {
Steven Langde890a12011-11-09 14:03:34 +01001069 .name = "net",
1070 .version = FIO_IOOPS_VERSION,
1071 .prep = fio_netio_prep,
1072 .queue = fio_netio_queue,
1073 .setup = fio_netio_setup,
1074 .init = fio_netio_init,
1075 .cleanup = fio_netio_cleanup,
1076 .open_file = fio_netio_open_file,
1077 .close_file = fio_netio_close_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001078 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001079 .options = options,
1080 .option_struct_size = sizeof(struct netio_options),
1081 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Steven Noonanad705bc2013-04-08 15:05:25 -07001082 FIO_PIPEIO | FIO_BIT_BASED,
Jens Axboeed92ac02007-02-06 14:43:52 +01001083};
1084
Steven Langde890a12011-11-09 14:03:34 +01001085static int str_hostname_cb(void *data, const char *input)
1086{
1087 struct netio_options *o = data;
1088
1089 if (o->td->o.filename)
1090 free(o->td->o.filename);
1091 o->td->o.filename = strdup(input);
1092 return 0;
1093}
1094
Jens Axboeed92ac02007-02-06 14:43:52 +01001095static void fio_init fio_netio_register(void)
1096{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001097 register_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001098#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001099 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001100#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001101}
1102
1103static void fio_exit fio_netio_unregister(void)
1104{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001105 unregister_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001106#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001107 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001108#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001109}