blob: 8087207e505bc69ac67e0321aff922a35207d9dc [file] [log] [blame]
Jens Axboeed92ac02007-02-06 14:43:52 +01001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * net engine
3 *
4 * IO engine that reads/writes to/from sockets.
5 *
Jens Axboeed92ac02007-02-06 14:43:52 +01006 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
Steven Noonan842805f2012-12-01 08:16:55 +000010#include <signal.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010011#include <errno.h>
12#include <assert.h>
13#include <netinet/in.h>
Steven Noonan70a78782012-11-28 14:52:36 -080014#include <netinet/tcp.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010015#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboe5fdd1242007-02-11 04:00:37 +010017#include <sys/poll.h>
Jens Axboe72920562008-06-02 12:30:06 +020018#include <sys/types.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020019#include <sys/stat.h>
Jens Axboe72920562008-06-02 12:30:06 +020020#include <sys/socket.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020021#include <sys/un.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010022
23#include "../fio.h"
Jens Axboeed92ac02007-02-06 14:43:52 +010024
Jens Axboeb5af8292007-03-08 12:43:13 +010025struct netio_data {
26 int listenfd;
Jens Axboe9cce02e2007-06-22 15:42:21 +020027 int use_splice;
28 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010029 struct sockaddr_in addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -080030 struct sockaddr_in6 addr6;
Jens Axboe0fd666b2011-10-06 20:08:53 +020031 struct sockaddr_un addr_un;
Jens Axboeb5af8292007-03-08 12:43:13 +010032};
Jens Axboeed92ac02007-02-06 14:43:52 +010033
Steven Langde890a12011-11-09 14:03:34 +010034struct netio_options {
35 struct thread_data *td;
36 unsigned int port;
37 unsigned int proto;
38 unsigned int listen;
Jens Axboe6f73a7f2012-11-30 09:59:20 +010039 unsigned int pingpong;
Steven Noonan70a78782012-11-28 14:52:36 -080040 unsigned int nodelay;
Shawn Bohrerd3a623d2013-07-19 13:24:08 -050041 unsigned int ttl;
Bruce Cranf16b7402013-10-08 15:05:27 +010042 char *intfc;
Steven Langde890a12011-11-09 14:03:34 +010043};
44
Jens Axboe664fb3b2009-01-19 13:26:36 +010045struct udp_close_msg {
46 uint32_t magic;
47 uint32_t cmd;
48};
49
50enum {
51 FIO_LINK_CLOSE = 0x89,
Jens Axboeb96d2432012-11-30 08:27:46 +010052 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
53 FIO_LINK_OPEN = 0x98,
Jens Axboe0fd666b2011-10-06 20:08:53 +020054
55 FIO_TYPE_TCP = 1,
56 FIO_TYPE_UDP = 2,
57 FIO_TYPE_UNIX = 3,
Jens Axboe49ccb8c2014-01-23 16:49:37 -080058 FIO_TYPE_TCP_V6 = 4,
59 FIO_TYPE_UDP_V6 = 5,
Jens Axboe664fb3b2009-01-19 13:26:36 +010060};
61
Steven Langde890a12011-11-09 14:03:34 +010062static int str_hostname_cb(void *data, const char *input);
63static struct fio_option options[] = {
64 {
65 .name = "hostname",
Jens Axboee8b0e952012-03-19 14:37:08 +010066 .lname = "net engine hostname",
Steven Langde890a12011-11-09 14:03:34 +010067 .type = FIO_OPT_STR_STORE,
68 .cb = str_hostname_cb,
69 .help = "Hostname for net IO engine",
Jens Axboee90a0ad2013-04-10 19:43:59 +020070 .category = FIO_OPT_C_ENGINE,
71 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010072 },
73 {
74 .name = "port",
Jens Axboee8b0e952012-03-19 14:37:08 +010075 .lname = "net engine port",
Steven Langde890a12011-11-09 14:03:34 +010076 .type = FIO_OPT_INT,
77 .off1 = offsetof(struct netio_options, port),
78 .minval = 1,
79 .maxval = 65535,
80 .help = "Port to use for TCP or UDP net connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +020081 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +010083 },
84 {
85 .name = "protocol",
Jens Axboee8b0e952012-03-19 14:37:08 +010086 .lname = "net engine protocol",
Steven Langde890a12011-11-09 14:03:34 +010087 .alias = "proto",
88 .type = FIO_OPT_STR,
89 .off1 = offsetof(struct netio_options, proto),
90 .help = "Network protocol to use",
91 .def = "tcp",
92 .posval = {
93 { .ival = "tcp",
94 .oval = FIO_TYPE_TCP,
95 .help = "Transmission Control Protocol",
96 },
Jens Axboeeb232312014-01-24 18:59:15 -080097#ifdef CONFIG_IPV6
Jens Axboe49ccb8c2014-01-23 16:49:37 -080098 { .ival = "tcpv6",
99 .oval = FIO_TYPE_TCP_V6,
100 .help = "Transmission Control Protocol V6",
101 },
Jens Axboeeb232312014-01-24 18:59:15 -0800102#endif
Steven Langde890a12011-11-09 14:03:34 +0100103 { .ival = "udp",
104 .oval = FIO_TYPE_UDP,
Bruce Cranf5cc3d02012-10-10 08:17:44 -0600105 .help = "User Datagram Protocol",
Steven Langde890a12011-11-09 14:03:34 +0100106 },
Jens Axboeeb232312014-01-24 18:59:15 -0800107#ifdef CONFIG_IPV6
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800108 { .ival = "udpv6",
109 .oval = FIO_TYPE_UDP_V6,
110 .help = "User Datagram Protocol V6",
111 },
Jens Axboeeb232312014-01-24 18:59:15 -0800112#endif
Steven Langde890a12011-11-09 14:03:34 +0100113 { .ival = "unix",
114 .oval = FIO_TYPE_UNIX,
115 .help = "UNIX domain socket",
116 },
117 },
Jens Axboee90a0ad2013-04-10 19:43:59 +0200118 .category = FIO_OPT_C_ENGINE,
119 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100120 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100121#ifdef CONFIG_TCP_NODELAY
Steven Langde890a12011-11-09 14:03:34 +0100122 {
Steven Noonan70a78782012-11-28 14:52:36 -0800123 .name = "nodelay",
124 .type = FIO_OPT_BOOL,
125 .off1 = offsetof(struct netio_options, nodelay),
126 .help = "Use TCP_NODELAY on TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_NETIO,
Steven Noonan70a78782012-11-28 14:52:36 -0800129 },
Jens Axboe1eafa372013-01-31 10:19:51 +0100130#endif
Steven Langde890a12011-11-09 14:03:34 +0100131 {
132 .name = "listen",
Jens Axboee8b0e952012-03-19 14:37:08 +0100133 .lname = "net engine listen",
Steven Langde890a12011-11-09 14:03:34 +0100134 .type = FIO_OPT_STR_SET,
135 .off1 = offsetof(struct netio_options, listen),
136 .help = "Listen for incoming TCP connections",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200137 .category = FIO_OPT_C_ENGINE,
138 .group = FIO_OPT_G_NETIO,
Steven Langde890a12011-11-09 14:03:34 +0100139 },
140 {
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100141 .name = "pingpong",
142 .type = FIO_OPT_STR_SET,
143 .off1 = offsetof(struct netio_options, pingpong),
144 .help = "Ping-pong IO requests",
Jens Axboee90a0ad2013-04-10 19:43:59 +0200145 .category = FIO_OPT_C_ENGINE,
146 .group = FIO_OPT_G_NETIO,
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100147 },
148 {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500149 .name = "interface",
150 .lname = "net engine interface",
151 .type = FIO_OPT_STR_STORE,
Bruce Cranf16b7402013-10-08 15:05:27 +0100152 .off1 = offsetof(struct netio_options, intfc),
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500153 .help = "Network interface to use",
154 .category = FIO_OPT_C_ENGINE,
155 .group = FIO_OPT_G_NETIO,
156 },
157 {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500158 .name = "ttl",
159 .lname = "net engine multicast ttl",
160 .type = FIO_OPT_INT,
161 .off1 = offsetof(struct netio_options, ttl),
162 .def = "1",
163 .minval = 0,
164 .help = "Time-to-live value for outgoing UDP multicast packets",
165 .category = FIO_OPT_C_ENGINE,
166 .group = FIO_OPT_G_NETIO,
167 },
168 {
Steven Langde890a12011-11-09 14:03:34 +0100169 .name = NULL,
170 },
171};
172
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800173static inline int is_udp(struct netio_options *o)
174{
175 return o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_UDP_V6;
176}
177
178static inline int is_tcp(struct netio_options *o)
179{
180 return o->proto == FIO_TYPE_TCP || o->proto == FIO_TYPE_TCP_V6;
181}
182
183static inline int is_ipv6(struct netio_options *o)
184{
185 return o->proto == FIO_TYPE_UDP_V6 || o->proto == FIO_TYPE_TCP_V6;
186}
187
Jens Axboe371d4562009-01-19 10:17:06 +0100188/*
189 * Return -1 for error and 'nr events' for a positive number
190 * of events
191 */
192static int poll_wait(struct thread_data *td, int fd, short events)
193{
194 struct pollfd pfd;
195 int ret;
196
197 while (!td->terminate) {
198 pfd.fd = fd;
199 pfd.events = events;
200 ret = poll(&pfd, 1, -1);
201 if (ret < 0) {
202 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +0100203 break;
Jens Axboe371d4562009-01-19 10:17:06 +0100204
205 td_verror(td, errno, "poll");
206 return -1;
207 } else if (!ret)
208 continue;
209
210 break;
211 }
212
213 if (pfd.revents & events)
214 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +0100215
216 return -1;
217}
218
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500219static int fio_netio_is_multicast(const char *mcaddr)
220{
221 in_addr_t addr = inet_network(mcaddr);
222 if (addr == -1)
223 return 0;
224
225 if (inet_network("224.0.0.0") <= addr &&
226 inet_network("239.255.255.255") >= addr)
227 return 1;
228
229 return 0;
230}
231
232
Jens Axboeed92ac02007-02-06 14:43:52 +0100233static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
234{
Steven Langde890a12011-11-09 14:03:34 +0100235 struct netio_options *o = td->eo;
Jens Axboeed92ac02007-02-06 14:43:52 +0100236
Jens Axboe7a6499d2007-02-07 09:35:29 +0100237 /*
238 * Make sure we don't see spurious reads to a receiver, and vice versa
239 */
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800240 if (is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +0100241 return 0;
242
243 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
244 (!o->listen && io_u->ddir == DDIR_READ)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100245 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +0100246 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100247 }
Bruce Cran3f457be2012-10-10 13:37:41 +0100248
Jens Axboef85ac252008-03-01 18:09:49 +0100249 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100250}
251
Jens Axboe67bf9822013-01-10 11:23:19 +0100252#ifdef CONFIG_LINUX_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +0200253static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200254{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200255 int bytes = 0;
256
257 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200258 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200259
260 if (ret < 0) {
261 if (!bytes)
262 bytes = ret;
263
264 break;
265 } else if (!ret)
266 break;
267
268 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200269 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200270 }
271
272 return bytes;
273}
274
275/*
Jens Axboecd963e12007-06-24 21:41:46 +0200276 * Receive bytes from a socket and fill them into the internal pipe
277 */
278static int splice_in(struct thread_data *td, struct io_u *io_u)
279{
280 struct netio_data *nd = td->io_ops->data;
281
282 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
283}
284
285/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200286 * Transmit 'len' bytes from the internal pipe
287 */
288static int splice_out(struct thread_data *td, struct io_u *io_u,
289 unsigned int len)
290{
291 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200292
293 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
294}
295
296static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
297{
298 struct iovec iov = {
299 .iov_base = io_u->xfer_buf,
300 .iov_len = len,
301 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200302 int bytes = 0;
303
Jens Axboecd963e12007-06-24 21:41:46 +0200304 while (iov.iov_len) {
305 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200306
307 if (ret < 0) {
308 if (!bytes)
309 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200310 break;
311 } else if (!ret)
312 break;
313
Jens Axboecd963e12007-06-24 21:41:46 +0200314 iov.iov_len -= ret;
315 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200316 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200317 }
318
319 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200320
Jens Axboe9cce02e2007-06-22 15:42:21 +0200321}
322
323/*
324 * vmsplice() pipe to io_u buffer
325 */
326static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
327 unsigned int len)
328{
329 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200330
Jens Axboecd963e12007-06-24 21:41:46 +0200331 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200332}
333
334/*
335 * vmsplice() io_u to pipe
336 */
337static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
338{
339 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200340
Jens Axboecd963e12007-06-24 21:41:46 +0200341 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200342}
343
Jens Axboecd963e12007-06-24 21:41:46 +0200344/*
345 * splice receive - transfer socket data into a pipe using splice, then map
346 * that pipe data into the io_u using vmsplice.
347 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200348static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
349{
350 int ret;
351
352 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200353 if (ret > 0)
354 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200355
Jens Axboecd963e12007-06-24 21:41:46 +0200356 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200357}
358
Jens Axboecd963e12007-06-24 21:41:46 +0200359/*
360 * splice transmit - map data from the io_u into a pipe by using vmsplice,
361 * then transfer that pipe to a socket using splice.
362 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200363static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
364{
365 int ret;
366
367 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200368 if (ret > 0)
369 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200370
Jens Axboecd963e12007-06-24 21:41:46 +0200371 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200372}
Jens Axboe5921e802008-05-30 15:02:38 +0200373#else
374static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
375{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200376 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200377 return -1;
378}
379
380static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
381{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200382 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200383 return -1;
384}
385#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200386
387static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
388{
Jens Axboe414c2a32009-01-16 13:21:15 +0100389 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100390 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100391 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100392
Jens Axboe664fb3b2009-01-19 13:26:36 +0100393 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800394 if (is_udp(o)) {
Jens Axboe10aa1362014-04-01 21:10:36 -0600395 const struct sockaddr *to;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800396 socklen_t len;
397
398 if (is_ipv6(o)) {
399 to = (struct sockaddr *) &nd->addr6;
400 len = sizeof(nd->addr6);
401 } else {
402 to = (struct sockaddr *) &nd->addr;
403 len = sizeof(nd->addr);
404 }
Jens Axboe62b38922009-05-11 10:37:33 +0200405
Jens Axboe664fb3b2009-01-19 13:26:36 +0100406 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800407 io_u->xfer_buflen, flags, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100408 } else {
409 /*
410 * if we are going to write more, set MSG_MORE
411 */
Jens Axboe5921e802008-05-30 15:02:38 +0200412#ifdef MSG_MORE
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100413 if ((td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
414 td->o.size) && !o->pingpong)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100415 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200416#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100417 ret = send(io_u->file->fd, io_u->xfer_buf,
418 io_u->xfer_buflen, flags);
419 }
420 if (ret > 0)
421 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200422
Jens Axboe664fb3b2009-01-19 13:26:36 +0100423 ret = poll_wait(td, io_u->file->fd, POLLOUT);
424 if (ret <= 0)
425 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100426 } while (1);
427
428 return ret;
429}
430
431static int is_udp_close(struct io_u *io_u, int len)
432{
433 struct udp_close_msg *msg;
434
435 if (len != sizeof(struct udp_close_msg))
436 return 0;
437
438 msg = io_u->xfer_buf;
Jens Axboeb96d2432012-11-30 08:27:46 +0100439 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100440 return 0;
441 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
442 return 0;
443
444 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200445}
446
Jens Axboe414c2a32009-01-16 13:21:15 +0100447static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200448{
Jens Axboe414c2a32009-01-16 13:21:15 +0100449 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100450 struct netio_options *o = td->eo;
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100451 int ret, flags = 0;
Jens Axboe371d4562009-01-19 10:17:06 +0100452
Jens Axboe664fb3b2009-01-19 13:26:36 +0100453 do {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800454 if (is_udp(o)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500455 struct sockaddr *from;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800456 socklen_t l, *len = &l;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500457
458 if (o->listen) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800459 if (!is_ipv6(o)) {
460 from = (struct sockaddr *) &nd->addr;
461 *len = sizeof(nd->addr);
462 } else {
463 from = (struct sockaddr *) &nd->addr6;
464 *len = sizeof(nd->addr6);
465 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500466 } else {
467 from = NULL;
468 len = NULL;
469 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200470
Jens Axboe664fb3b2009-01-19 13:26:36 +0100471 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Shawn Bohrerb511c9a2013-07-19 13:24:06 -0500472 io_u->xfer_buflen, flags, from, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100473 if (is_udp_close(io_u, ret)) {
474 td->done = 1;
475 return 0;
476 }
477 } else {
478 ret = recv(io_u->file->fd, io_u->xfer_buf,
479 io_u->xfer_buflen, flags);
480 }
481 if (ret > 0)
482 break;
Jens Axboe7d988f62012-11-29 19:57:35 +0100483 else if (!ret && (flags & MSG_WAITALL))
484 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100485
Jens Axboe664fb3b2009-01-19 13:26:36 +0100486 ret = poll_wait(td, io_u->file->fd, POLLIN);
487 if (ret <= 0)
488 break;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100489 flags |= MSG_WAITALL;
490 } while (1);
491
492 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200493}
494
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100495static int __fio_netio_queue(struct thread_data *td, struct io_u *io_u,
496 enum fio_ddir ddir)
Jens Axboeed92ac02007-02-06 14:43:52 +0100497{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200498 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100499 struct netio_options *o = td->eo;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200500 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100501
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100502 if (ddir == DDIR_WRITE) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800503 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100504 o->proto == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200505 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200506 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100507 ret = fio_netio_splice_out(td, io_u);
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100508 } else if (ddir == DDIR_READ) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800509 if (!nd->use_splice || is_udp(o) ||
Steven Langde890a12011-11-09 14:03:34 +0100510 o->proto == FIO_TYPE_UNIX)
Jens Axboe414c2a32009-01-16 13:21:15 +0100511 ret = fio_netio_recv(td, io_u);
512 else
513 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100514 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100515 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100516
Jens Axboecec6b552007-02-06 20:15:38 +0100517 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100518 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100519 io_u->resid = io_u->xfer_buflen - ret;
520 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100521 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100522 } else {
523 int err = errno;
524
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100525 if (ddir == DDIR_WRITE && err == EMSGSIZE)
Jens Axboe414c2a32009-01-16 13:21:15 +0100526 return FIO_Q_BUSY;
527
528 io_u->error = err;
529 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100530 }
531
Jens Axboe36167d82007-02-18 05:41:31 +0100532 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100533 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100534
Jens Axboe36167d82007-02-18 05:41:31 +0100535 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100536}
537
Jens Axboe6f73a7f2012-11-30 09:59:20 +0100538static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
539{
540 struct netio_options *o = td->eo;
541 int ret;
542
543 fio_ro_check(td, io_u);
544
545 ret = __fio_netio_queue(td, io_u, io_u->ddir);
546 if (!o->pingpong || ret != FIO_Q_COMPLETED)
547 return ret;
548
549 /*
550 * For ping-pong mode, receive or send reply as needed
551 */
552 if (td_read(td) && io_u->ddir == DDIR_READ)
553 ret = __fio_netio_queue(td, io_u, DDIR_WRITE);
554 else if (td_write(td) && io_u->ddir == DDIR_WRITE)
555 ret = __fio_netio_queue(td, io_u, DDIR_READ);
556
557 return ret;
558}
559
Jens Axboeb5af8292007-03-08 12:43:13 +0100560static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100561{
Jens Axboeb5af8292007-03-08 12:43:13 +0100562 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100563 struct netio_options *o = td->eo;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100564 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100565
Steven Langde890a12011-11-09 14:03:34 +0100566 if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200567 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100568 type = SOCK_STREAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800569 } else if (o->proto == FIO_TYPE_TCP_V6) {
570 domain = AF_INET6;
571 type = SOCK_STREAM;
Steven Langde890a12011-11-09 14:03:34 +0100572 } else if (o->proto == FIO_TYPE_UDP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200573 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100574 type = SOCK_DGRAM;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800575 } else if (o->proto == FIO_TYPE_UDP_V6) {
576 domain = AF_INET6;
577 type = SOCK_DGRAM;
Steven Langde890a12011-11-09 14:03:34 +0100578 } else if (o->proto == FIO_TYPE_UNIX) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200579 domain = AF_UNIX;
580 type = SOCK_STREAM;
581 } else {
Steven Langde890a12011-11-09 14:03:34 +0100582 log_err("fio: bad network type %d\n", o->proto);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200583 f->fd = -1;
584 return 1;
585 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100586
Jens Axboe0fd666b2011-10-06 20:08:53 +0200587 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100588 if (f->fd < 0) {
589 td_verror(td, errno, "socket");
590 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100591 }
592
Jens Axboe1eafa372013-01-31 10:19:51 +0100593#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800594 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100595 int optval = 1;
596
Jens Axboe26e594a2013-01-30 21:52:37 +0100597 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800598 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
599 return 1;
600 }
601 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100602#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800603
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800604 if (is_udp(o)) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500605 if (!fio_netio_is_multicast(td->o.filename))
606 return 0;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800607 if (is_ipv6(o)) {
608 log_err("fio: multicast not supported on IPv6\n");
609 close(f->fd);
610 return 1;
611 }
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500612
Bruce Cranf16b7402013-10-08 15:05:27 +0100613 if (o->intfc) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500614 struct in_addr interface_addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800615
Bruce Cranf16b7402013-10-08 15:05:27 +0100616 if (inet_aton(o->intfc, &interface_addr) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500617 log_err("fio: interface not valid interface IP\n");
618 close(f->fd);
619 return 1;
620 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100621 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_IF, (const char*)&interface_addr, sizeof(interface_addr)) < 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500622 td_verror(td, errno, "setsockopt IP_MULTICAST_IF");
623 close(f->fd);
624 return 1;
625 }
626 }
Bruce Cranf16b7402013-10-08 15:05:27 +0100627 if (setsockopt(f->fd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&o->ttl, sizeof(o->ttl)) < 0) {
Shawn Bohrerd3a623d2013-07-19 13:24:08 -0500628 td_verror(td, errno, "setsockopt IP_MULTICAST_TTL");
629 close(f->fd);
630 return 1;
631 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100632 return 0;
Shawn Bohrerb93b6a22013-07-19 13:24:07 -0500633 } else if (o->proto == FIO_TYPE_TCP) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100634 socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100635
Jens Axboe0fd666b2011-10-06 20:08:53 +0200636 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
637 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200638 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200639 return 1;
640 }
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800641 } else if (o->proto == FIO_TYPE_TCP_V6) {
642 socklen_t len = sizeof(nd->addr6);
643
644 if (connect(f->fd, (struct sockaddr *) &nd->addr6, len) < 0) {
645 td_verror(td, errno, "connect");
646 close(f->fd);
647 return 1;
648 }
649
Jens Axboe0fd666b2011-10-06 20:08:53 +0200650 } else {
651 struct sockaddr_un *addr = &nd->addr_un;
Jens Axboe67bf9822013-01-10 11:23:19 +0100652 socklen_t len;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200653
654 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
655
656 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
657 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200658 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200659 return 1;
660 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100661 }
662
663 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100664}
665
Jens Axboeb5af8292007-03-08 12:43:13 +0100666static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100667{
Jens Axboeb5af8292007-03-08 12:43:13 +0100668 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100669 struct netio_options *o = td->eo;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800670 socklen_t socklen;
Jens Axboe6264c7a2013-02-28 08:24:23 +0100671 int state;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100672
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800673 if (is_udp(o)) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100674 f->fd = nd->listenfd;
675 return 0;
676 }
677
Jens Axboe859088d2012-11-29 20:02:50 +0100678 state = td->runstate;
679 td_set_runstate(td, TD_SETTING_UP);
680
Jens Axboe6d861442007-03-15 09:22:23 +0100681 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100682
Jens Axboe371d4562009-01-19 10:17:06 +0100683 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
Jens Axboe859088d2012-11-29 20:02:50 +0100684 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100685
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800686 if (o->proto == FIO_TYPE_TCP) {
687 socklen = sizeof(nd->addr);
688 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
689 } else {
690 socklen = sizeof(nd->addr6);
691 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr6, &socklen);
692 }
693
Jens Axboe371d4562009-01-19 10:17:06 +0100694 if (f->fd < 0) {
695 td_verror(td, errno, "accept");
Jens Axboe859088d2012-11-29 20:02:50 +0100696 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100697 }
698
Jens Axboe1eafa372013-01-31 10:19:51 +0100699#ifdef CONFIG_TCP_NODELAY
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800700 if (o->nodelay && is_tcp(o)) {
Jens Axboe6264c7a2013-02-28 08:24:23 +0100701 int optval = 1;
702
Jens Axboe26e594a2013-01-30 21:52:37 +0100703 if (setsockopt(f->fd, IPPROTO_TCP, TCP_NODELAY, (void *) &optval, sizeof(int)) < 0) {
Steven Noonan70a78782012-11-28 14:52:36 -0800704 log_err("fio: cannot set TCP_NODELAY option on socket (%s), disable with 'nodelay=0'\n", strerror(errno));
705 return 1;
706 }
707 }
Jens Axboe1eafa372013-01-31 10:19:51 +0100708#endif
Steven Noonan70a78782012-11-28 14:52:36 -0800709
Jens Axboe0cae16f2012-11-30 16:22:31 +0100710 reset_all_stats(td);
Jens Axboe859088d2012-11-29 20:02:50 +0100711 td_set_runstate(td, state);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100712 return 0;
Jens Axboe859088d2012-11-29 20:02:50 +0100713err:
714 td_set_runstate(td, state);
715 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100716}
717
Jens Axboe664fb3b2009-01-19 13:26:36 +0100718static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
719{
720 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800721 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100722 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800723 struct sockaddr *to;
724 socklen_t len;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100725 int ret;
726
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800727 if (is_ipv6(o)) {
728 to = (struct sockaddr *) &nd->addr6;
729 len = sizeof(nd->addr6);
730 } else {
731 to = (struct sockaddr *) &nd->addr;
732 len = sizeof(nd->addr);
733 }
734
Jens Axboeb96d2432012-11-30 08:27:46 +0100735 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100736 msg.cmd = htonl(FIO_LINK_CLOSE);
737
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800738 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100739 if (ret < 0)
740 td_verror(td, errno, "sendto udp link close");
741}
742
743static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
744{
Steven Langde890a12011-11-09 14:03:34 +0100745 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100746
747 /*
748 * If this is an UDP connection, notify the receiver that we are
749 * closing down the link
750 */
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800751 if (is_udp(o))
Jens Axboe664fb3b2009-01-19 13:26:36 +0100752 fio_netio_udp_close(td, f);
753
754 return generic_close_file(td, f);
755}
756
Jens Axboeb96d2432012-11-30 08:27:46 +0100757static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
758{
759 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800760 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100761 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800762 struct sockaddr *to;
763 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100764 int ret;
765
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800766 if (is_ipv6(o)) {
767 len = sizeof(nd->addr6);
768 to = (struct sockaddr *) &nd->addr6;
769 } else {
770 len = sizeof(nd->addr);
771 to = (struct sockaddr *) &nd->addr;
772 }
773
Jens Axboe1f819912013-01-23 17:21:41 -0700774 ret = recvfrom(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, &len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100775 if (ret < 0) {
Shawn Bohreree7062f2013-07-19 13:24:09 -0500776 td_verror(td, errno, "recvfrom udp link open");
Jens Axboeb96d2432012-11-30 08:27:46 +0100777 return ret;
778 }
779
780 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
781 ntohl(msg.cmd) != FIO_LINK_OPEN) {
782 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
783 ntohl(msg.cmd));
784 return -1;
785 }
786
787 return 0;
788}
789
790static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
791{
792 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800793 struct netio_options *o = td->eo;
Jens Axboeb96d2432012-11-30 08:27:46 +0100794 struct udp_close_msg msg;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800795 struct sockaddr *to;
796 socklen_t len;
Jens Axboeb96d2432012-11-30 08:27:46 +0100797 int ret;
798
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800799 if (is_ipv6(o)) {
800 len = sizeof(nd->addr6);
801 to = (struct sockaddr *) &nd->addr6;
802 } else {
803 len = sizeof(nd->addr);
804 to = (struct sockaddr *) &nd->addr;
805 }
806
Jens Axboeb96d2432012-11-30 08:27:46 +0100807 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
808 msg.cmd = htonl(FIO_LINK_OPEN);
809
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800810 ret = sendto(f->fd, (void *) &msg, sizeof(msg), MSG_WAITALL, to, len);
Jens Axboeb96d2432012-11-30 08:27:46 +0100811 if (ret < 0) {
812 td_verror(td, errno, "sendto udp link open");
813 return ret;
814 }
815
816 return 0;
817}
818
819static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
820{
821 int ret;
822 struct netio_options *o = td->eo;
823
824 if (o->listen)
825 ret = fio_netio_accept(td, f);
826 else
827 ret = fio_netio_connect(td, f);
828
829 if (ret) {
830 f->fd = -1;
831 return ret;
832 }
833
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800834 if (is_udp(o)) {
Jens Axboeb96d2432012-11-30 08:27:46 +0100835 if (td_write(td))
836 ret = fio_netio_udp_send_open(td, f);
837 else {
838 int state;
839
840 state = td->runstate;
841 td_set_runstate(td, TD_SETTING_UP);
842 ret = fio_netio_udp_recv_open(td, f);
843 td_set_runstate(td, state);
844 }
845 }
846
847 if (ret)
848 fio_netio_close_file(td, f);
849
850 return ret;
851}
852
Jens Axboe0b783342014-01-23 20:19:17 -0800853static int fio_fill_addr(struct thread_data *td, const char *host, int af,
854 void *dst, struct addrinfo **res)
855{
856 struct netio_options *o = td->eo;
857 struct addrinfo hints;
858 int ret;
859
860 if (inet_pton(af, host, dst))
861 return 0;
862
863 memset(&hints, 0, sizeof(hints));
Jens Axboe0b783342014-01-23 20:19:17 -0800864
865 if (is_tcp(o))
866 hints.ai_socktype = SOCK_STREAM;
867 else
868 hints.ai_socktype = SOCK_DGRAM;
869
Jens Axboeb1b1be22014-01-23 20:41:33 -0800870 if (is_ipv6(o))
871 hints.ai_family = AF_INET6;
872 else
873 hints.ai_family = AF_INET;
874
Jens Axboe0b783342014-01-23 20:19:17 -0800875 ret = getaddrinfo(host, NULL, &hints, res);
876 if (ret) {
877 int e = EINVAL;
878 char str[128];
879
880 if (ret == EAI_SYSTEM)
881 e = errno;
882
883 snprintf(str, sizeof(str), "getaddrinfo: %s", gai_strerror(ret));
884 td_verror(td, e, str);
885 return 1;
886 }
887
888 return 0;
889}
890
Jens Axboe0fd666b2011-10-06 20:08:53 +0200891static int fio_netio_setup_connect_inet(struct thread_data *td,
892 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100893{
894 struct netio_data *nd = td->io_ops->data;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800895 struct netio_options *o = td->eo;
Jens Axboe0b783342014-01-23 20:19:17 -0800896 struct addrinfo *res = NULL;
897 void *dst, *src;
898 int af, len;
Jens Axboeb5af8292007-03-08 12:43:13 +0100899
Jens Axboe166dce42012-11-29 14:35:33 +0100900 if (!host) {
901 log_err("fio: connect with no host to connect to.\n");
902 if (td_read(td))
903 log_err("fio: did you forget to set 'listen'?\n");
904
905 td_verror(td, EINVAL, "no hostname= set");
906 return 1;
907 }
908
Jens Axboe0b783342014-01-23 20:19:17 -0800909 nd->addr.sin_family = AF_INET;
910 nd->addr.sin_port = htons(port);
911 nd->addr6.sin6_family = AF_INET6;
912 nd->addr6.sin6_port = htons(port);
913
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800914 if (is_ipv6(o)) {
Jens Axboe0b783342014-01-23 20:19:17 -0800915 af = AF_INET6;
Jens Axboe68631b32014-02-25 13:43:38 -0800916 dst = &nd->addr6.sin6_addr;
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800917 } else {
Jens Axboe0b783342014-01-23 20:19:17 -0800918 af = AF_INET;
Jens Axboe68631b32014-02-25 13:43:38 -0800919 dst = &nd->addr.sin_addr;
Jens Axboeb5af8292007-03-08 12:43:13 +0100920 }
921
Jens Axboe0b783342014-01-23 20:19:17 -0800922 if (fio_fill_addr(td, host, af, dst, &res))
923 return 1;
924
925 if (!res)
926 return 0;
927
928 if (is_ipv6(o)) {
929 len = sizeof(nd->addr6.sin6_addr);
930 src = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
931 } else {
932 len = sizeof(nd->addr.sin_addr);
933 src = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
934 }
935
936 memcpy(dst, src, len);
937 freeaddrinfo(res);
Jens Axboeb5af8292007-03-08 12:43:13 +0100938 return 0;
939}
940
Jens Axboe0fd666b2011-10-06 20:08:53 +0200941static int fio_netio_setup_connect_unix(struct thread_data *td,
942 const char *path)
943{
944 struct netio_data *nd = td->io_ops->data;
945 struct sockaddr_un *soun = &nd->addr_un;
946
947 soun->sun_family = AF_UNIX;
Jens Axboe4b159fa2014-04-14 08:49:04 -0600948 memset(soun->sun_path, 0, sizeof(soun->sun_path));
949 strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200950 return 0;
951}
952
Steven Langde890a12011-11-09 14:03:34 +0100953static int fio_netio_setup_connect(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200954{
Steven Langde890a12011-11-09 14:03:34 +0100955 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200956
Jens Axboe49ccb8c2014-01-23 16:49:37 -0800957 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +0100958 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200959 else
Steven Langde890a12011-11-09 14:03:34 +0100960 return fio_netio_setup_connect_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200961}
962
963static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
964{
965 struct netio_data *nd = td->io_ops->data;
966 struct sockaddr_un *addr = &nd->addr_un;
967 mode_t mode;
968 int len, fd;
969
970 fd = socket(AF_UNIX, SOCK_STREAM, 0);
971 if (fd < 0) {
972 log_err("fio: socket: %s\n", strerror(errno));
973 return -1;
974 }
975
976 mode = umask(000);
977
978 memset(addr, 0, sizeof(*addr));
979 addr->sun_family = AF_UNIX;
Jens Axboe4b159fa2014-04-14 08:49:04 -0600980 strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200981 unlink(path);
982
983 len = sizeof(addr->sun_family) + strlen(path) + 1;
984
985 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
986 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200987 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200988 return -1;
989 }
990
991 umask(mode);
992 nd->listenfd = fd;
993 return 0;
994}
995
996static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100997{
998 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100999 struct netio_options *o = td->eo;
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001000 struct ip_mreq mr;
1001 struct sockaddr_in sin;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001002 struct sockaddr *saddr;
1003 int fd, opt, type, domain;
1004 socklen_t len;
Jens Axboeed92ac02007-02-06 14:43:52 +01001005
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001006 memset(&sin, 0, sizeof(sin));
Jens Axboe414c2a32009-01-16 13:21:15 +01001007
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001008 if (o->proto == FIO_TYPE_TCP) {
1009 type = SOCK_STREAM;
1010 domain = AF_INET;
1011 } else if (o->proto == FIO_TYPE_TCP_V6) {
1012 type = SOCK_STREAM;
1013 domain = AF_INET6;
1014 } else if (o->proto == FIO_TYPE_UDP) {
1015 type = SOCK_DGRAM;
1016 domain = AF_INET;
1017 } else if (o->proto == FIO_TYPE_UDP_V6) {
1018 type = SOCK_DGRAM;
1019 domain = AF_INET6;
1020 } else {
1021 log_err("fio: unknown proto %d\n", o->proto);
1022 return 1;
1023 }
1024
1025 fd = socket(domain, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +01001026 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001027 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +01001028 return 1;
1029 }
1030
1031 opt = 1;
Jens Axboe26e594a2013-01-30 21:52:37 +01001032 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001033 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001034 close(fd);
Jens Axboeed92ac02007-02-06 14:43:52 +01001035 return 1;
1036 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001037#ifdef SO_REUSEPORT
Jens Axboe26e594a2013-01-30 21:52:37 +01001038 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *) &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001039 td_verror(td, errno, "setsockopt");
Shawn Bohrer4a93dec2013-07-19 13:24:10 -05001040 close(fd);
Jens Axboe6bedbfa2007-02-07 09:54:40 +01001041 return 1;
1042 }
1043#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001044
Jens Axboe6611e9c2014-01-24 07:36:43 -08001045 if (td->o.filename) {
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001046 if (!is_udp(o) || !fio_netio_is_multicast(td->o.filename)) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001047 log_err("fio: hostname not valid for non-multicast inbound network IO\n");
1048 close(fd);
1049 return 1;
1050 }
Jens Axboe6611e9c2014-01-24 07:36:43 -08001051 if (is_ipv6(o)) {
1052 log_err("fio: IPv6 not supported for multicast network IO");
1053 close(fd);
1054 return 1;
1055 }
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001056
1057 inet_aton(td->o.filename, &sin.sin_addr);
1058
1059 mr.imr_multiaddr = sin.sin_addr;
Bruce Cranf16b7402013-10-08 15:05:27 +01001060 if (o->intfc) {
1061 if (inet_aton(o->intfc, &mr.imr_interface) == 0) {
Shawn Bohrerb93b6a22013-07-19 13:24:07 -05001062 log_err("fio: interface not valid interface IP\n");
1063 close(fd);
1064 return 1;
1065 }
1066 } else {
1067 mr.imr_interface.s_addr = htonl(INADDR_ANY);
1068 }
Jens Axboe6611e9c2014-01-24 07:36:43 -08001069
Bruce Cranf16b7402013-10-08 15:05:27 +01001070 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mr, sizeof(mr)) < 0) {
Shawn Bohrerb511c9a2013-07-19 13:24:06 -05001071 td_verror(td, errno, "setsockopt IP_ADD_MEMBERSHIP");
1072 close(fd);
1073 return 1;
1074 }
1075 }
1076
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001077 if (!is_ipv6(o)) {
1078 saddr = (struct sockaddr *) &nd->addr;
1079 len = sizeof(nd->addr);
Jens Axboeed92ac02007-02-06 14:43:52 +01001080
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001081 nd->addr.sin_family = AF_INET;
1082 nd->addr.sin_addr.s_addr = sin.sin_addr.s_addr ? sin.sin_addr.s_addr : htonl(INADDR_ANY);
1083 nd->addr.sin_port = htons(port);
1084 } else {
1085 saddr = (struct sockaddr *) &nd->addr6;
1086 len = sizeof(nd->addr6);
1087
1088 nd->addr6.sin6_family = AF_INET6;
Jens Axboe66f7a562014-04-14 11:57:05 -06001089 nd->addr6.sin6_addr = in6addr_any;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001090 nd->addr6.sin6_port = htons(port);
1091 }
1092
1093 if (bind(fd, saddr, len) < 0) {
Jens Axboed19cedd2014-04-11 11:29:50 -06001094 close(fd);
Jens Axboee1161c32007-02-22 19:36:48 +01001095 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +01001096 return 1;
1097 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001098
1099 nd->listenfd = fd;
1100 return 0;
1101}
1102
Steven Langde890a12011-11-09 14:03:34 +01001103static int fio_netio_setup_listen(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +02001104{
1105 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +01001106 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +02001107 int ret;
1108
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001109 if (is_udp(o) || is_tcp(o))
Steven Langde890a12011-11-09 14:03:34 +01001110 ret = fio_netio_setup_listen_inet(td, o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001111 else
Steven Langde890a12011-11-09 14:03:34 +01001112 ret = fio_netio_setup_listen_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001113
1114 if (ret)
1115 return ret;
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001116 if (is_udp(o))
Jens Axboe0fd666b2011-10-06 20:08:53 +02001117 return 0;
1118
1119 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +01001120 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +02001121 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +01001122 return 1;
1123 }
1124
Jens Axboeb5af8292007-03-08 12:43:13 +01001125 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +01001126}
1127
Jens Axboe9bec88e2007-03-02 08:55:48 +01001128static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +01001129{
Steven Langde890a12011-11-09 14:03:34 +01001130 struct netio_options *o = td->eo;
Jens Axboeaf52b342007-03-13 10:07:47 +01001131 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001132
Bruce Cran3f457be2012-10-10 13:37:41 +01001133#ifdef WIN32
1134 WSADATA wsd;
1135 WSAStartup(MAKEWORD(2,2), &wsd);
1136#endif
1137
Jens Axboe16d55aa2007-05-22 09:21:37 +02001138 if (td_random(td)) {
1139 log_err("fio: network IO can't be random\n");
1140 return 1;
1141 }
Jens Axboeed92ac02007-02-06 14:43:52 +01001142
Steven Langde890a12011-11-09 14:03:34 +01001143 if (o->proto == FIO_TYPE_UNIX && o->port) {
1144 log_err("fio: network IO port not valid with unix socket\n");
1145 return 1;
1146 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
1147 log_err("fio: network IO requires port for tcp or udp\n");
1148 return 1;
Jens Axboe414c2a32009-01-16 13:21:15 +01001149 }
Jens Axboe0fd666b2011-10-06 20:08:53 +02001150
Jens Axboe49ccb8c2014-01-23 16:49:37 -08001151 if (!is_tcp(o)) {
Steven Langde890a12011-11-09 14:03:34 +01001152 if (o->listen) {
Jens Axboe9b986062011-12-19 08:57:18 +01001153 log_err("fio: listen only valid for TCP proto IO\n");
1154 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001155 }
1156 if (td_rw(td)) {
Jens Axboe9b986062011-12-19 08:57:18 +01001157 log_err("fio: datagram network connections must be"
Steven Langde890a12011-11-09 14:03:34 +01001158 " read OR write\n");
Jens Axboe9b986062011-12-19 08:57:18 +01001159 return 1;
1160 }
1161 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
1162 log_err("fio: UNIX sockets need host/filename\n");
1163 return 1;
Steven Langde890a12011-11-09 14:03:34 +01001164 }
1165 o->listen = td_read(td);
1166 }
1167
Steven Langde890a12011-11-09 14:03:34 +01001168 if (o->listen)
1169 ret = fio_netio_setup_listen(td);
Jens Axboe0fd666b2011-10-06 20:08:53 +02001170 else
Steven Langde890a12011-11-09 14:03:34 +01001171 ret = fio_netio_setup_connect(td);
Jens Axboeed92ac02007-02-06 14:43:52 +01001172
Jens Axboe7bb48f82007-03-27 15:30:28 +02001173 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +01001174}
1175
Jens Axboeb5af8292007-03-08 12:43:13 +01001176static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +01001177{
Jens Axboeb5af8292007-03-08 12:43:13 +01001178 struct netio_data *nd = td->io_ops->data;
1179
1180 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +02001181 if (nd->listenfd != -1)
1182 close(nd->listenfd);
1183 if (nd->pipes[0] != -1)
1184 close(nd->pipes[0]);
1185 if (nd->pipes[1] != -1)
1186 close(nd->pipes[1]);
1187
Jens Axboeb5af8292007-03-08 12:43:13 +01001188 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +01001189 }
1190}
1191
1192static int fio_netio_setup(struct thread_data *td)
1193{
Jens Axboe7bb48f82007-03-27 15:30:28 +02001194 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +01001195
Steven Langde890a12011-11-09 14:03:34 +01001196 if (!td->files_index) {
Jens Axboe5903e7b2014-02-26 13:42:13 -08001197 add_file(td, td->o.filename ?: "net", 0, 0);
Steven Langde890a12011-11-09 14:03:34 +01001198 td->o.nr_files = td->o.nr_files ?: 1;
Jens Axboeb53f2c52014-04-08 21:07:12 -06001199 td->o.open_files++;
Steven Langde890a12011-11-09 14:03:34 +01001200 }
1201
Jens Axboe7bb48f82007-03-27 15:30:28 +02001202 if (!td->io_ops->data) {
1203 nd = malloc(sizeof(*nd));;
1204
1205 memset(nd, 0, sizeof(*nd));
1206 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +02001207 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001208 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +02001209 }
1210
Jens Axboe9bec88e2007-03-02 08:55:48 +01001211 return 0;
1212}
1213
Jens Axboe36d80bc2012-11-30 21:46:06 +01001214static void fio_netio_terminate(struct thread_data *td)
1215{
1216 kill(td->pid, SIGUSR2);
1217}
1218
Jens Axboe67bf9822013-01-10 11:23:19 +01001219#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001220static int fio_netio_setup_splice(struct thread_data *td)
1221{
1222 struct netio_data *nd;
1223
1224 fio_netio_setup(td);
1225
1226 nd = td->io_ops->data;
1227 if (nd) {
1228 if (pipe(nd->pipes) < 0)
1229 return 1;
1230
1231 nd->use_splice = 1;
1232 return 0;
1233 }
1234
1235 return 1;
1236}
1237
Jens Axboe5921e802008-05-30 15:02:38 +02001238static struct ioengine_ops ioengine_splice = {
Steven Langde890a12011-11-09 14:03:34 +01001239 .name = "netsplice",
1240 .version = FIO_IOOPS_VERSION,
1241 .prep = fio_netio_prep,
1242 .queue = fio_netio_queue,
1243 .setup = fio_netio_setup_splice,
1244 .init = fio_netio_init,
1245 .cleanup = fio_netio_cleanup,
1246 .open_file = fio_netio_open_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001247 .close_file = fio_netio_close_file,
1248 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001249 .options = options,
1250 .option_struct_size = sizeof(struct netio_options),
1251 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Jens Axboe36d80bc2012-11-30 21:46:06 +01001252 FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +02001253};
1254#endif
1255
Jens Axboe9cce02e2007-06-22 15:42:21 +02001256static struct ioengine_ops ioengine_rw = {
Steven Langde890a12011-11-09 14:03:34 +01001257 .name = "net",
1258 .version = FIO_IOOPS_VERSION,
1259 .prep = fio_netio_prep,
1260 .queue = fio_netio_queue,
1261 .setup = fio_netio_setup,
1262 .init = fio_netio_init,
1263 .cleanup = fio_netio_cleanup,
1264 .open_file = fio_netio_open_file,
1265 .close_file = fio_netio_close_file,
Jens Axboe36d80bc2012-11-30 21:46:06 +01001266 .terminate = fio_netio_terminate,
Steven Langde890a12011-11-09 14:03:34 +01001267 .options = options,
1268 .option_struct_size = sizeof(struct netio_options),
1269 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Steven Noonanad705bc2013-04-08 15:05:25 -07001270 FIO_PIPEIO | FIO_BIT_BASED,
Jens Axboeed92ac02007-02-06 14:43:52 +01001271};
1272
Steven Langde890a12011-11-09 14:03:34 +01001273static int str_hostname_cb(void *data, const char *input)
1274{
1275 struct netio_options *o = data;
1276
1277 if (o->td->o.filename)
1278 free(o->td->o.filename);
1279 o->td->o.filename = strdup(input);
1280 return 0;
1281}
1282
Jens Axboeed92ac02007-02-06 14:43:52 +01001283static void fio_init fio_netio_register(void)
1284{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001285 register_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001286#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001287 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001288#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001289}
1290
1291static void fio_exit fio_netio_unregister(void)
1292{
Jens Axboe9cce02e2007-06-22 15:42:21 +02001293 unregister_ioengine(&ioengine_rw);
Jens Axboe67bf9822013-01-10 11:23:19 +01001294#ifdef CONFIG_LINUX_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +02001295 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +02001296#endif
Jens Axboeed92ac02007-02-06 14:43:52 +01001297}