blob: 419508e47a5ec037a33c2c00781973820cbdc752 [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>
10#include <errno.h>
11#include <assert.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
Jens Axboe5fdd1242007-02-11 04:00:37 +010015#include <sys/poll.h>
Jens Axboe72920562008-06-02 12:30:06 +020016#include <sys/types.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020017#include <sys/stat.h>
Jens Axboe72920562008-06-02 12:30:06 +020018#include <sys/socket.h>
Jens Axboe0fd666b2011-10-06 20:08:53 +020019#include <sys/un.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010020
21#include "../fio.h"
Jens Axboeed92ac02007-02-06 14:43:52 +010022
Jens Axboeb5af8292007-03-08 12:43:13 +010023struct netio_data {
24 int listenfd;
Jens Axboe9cce02e2007-06-22 15:42:21 +020025 int use_splice;
26 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010027 struct sockaddr_in addr;
Jens Axboe0fd666b2011-10-06 20:08:53 +020028 struct sockaddr_un addr_un;
Jens Axboeb5af8292007-03-08 12:43:13 +010029};
Jens Axboeed92ac02007-02-06 14:43:52 +010030
Steven Langde890a12011-11-09 14:03:34 +010031struct netio_options {
32 struct thread_data *td;
33 unsigned int port;
34 unsigned int proto;
35 unsigned int listen;
36};
37
Jens Axboe664fb3b2009-01-19 13:26:36 +010038struct udp_close_msg {
39 uint32_t magic;
40 uint32_t cmd;
41};
42
43enum {
44 FIO_LINK_CLOSE = 0x89,
45 FIO_LINK_CLOSE_MAGIC = 0x6c696e6b,
Jens Axboe0fd666b2011-10-06 20:08:53 +020046
47 FIO_TYPE_TCP = 1,
48 FIO_TYPE_UDP = 2,
49 FIO_TYPE_UNIX = 3,
Jens Axboe664fb3b2009-01-19 13:26:36 +010050};
51
Steven Langde890a12011-11-09 14:03:34 +010052static int str_hostname_cb(void *data, const char *input);
53static struct fio_option options[] = {
54 {
55 .name = "hostname",
56 .type = FIO_OPT_STR_STORE,
57 .cb = str_hostname_cb,
58 .help = "Hostname for net IO engine",
59 },
60 {
61 .name = "port",
62 .type = FIO_OPT_INT,
63 .off1 = offsetof(struct netio_options, port),
64 .minval = 1,
65 .maxval = 65535,
66 .help = "Port to use for TCP or UDP net connections",
67 },
68 {
69 .name = "protocol",
70 .alias = "proto",
71 .type = FIO_OPT_STR,
72 .off1 = offsetof(struct netio_options, proto),
73 .help = "Network protocol to use",
74 .def = "tcp",
75 .posval = {
76 { .ival = "tcp",
77 .oval = FIO_TYPE_TCP,
78 .help = "Transmission Control Protocol",
79 },
80 { .ival = "udp",
81 .oval = FIO_TYPE_UDP,
Bruce Cranf5cc3d02012-10-10 08:17:44 -060082 .help = "User Datagram Protocol",
Steven Langde890a12011-11-09 14:03:34 +010083 },
84 { .ival = "unix",
85 .oval = FIO_TYPE_UNIX,
86 .help = "UNIX domain socket",
87 },
88 },
89 },
90 {
91 .name = "listen",
92 .type = FIO_OPT_STR_SET,
93 .off1 = offsetof(struct netio_options, listen),
94 .help = "Listen for incoming TCP connections",
95 },
96 {
97 .name = NULL,
98 },
99};
100
Jens Axboe371d4562009-01-19 10:17:06 +0100101/*
102 * Return -1 for error and 'nr events' for a positive number
103 * of events
104 */
105static int poll_wait(struct thread_data *td, int fd, short events)
106{
107 struct pollfd pfd;
108 int ret;
109
110 while (!td->terminate) {
111 pfd.fd = fd;
112 pfd.events = events;
113 ret = poll(&pfd, 1, -1);
114 if (ret < 0) {
115 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +0100116 break;
Jens Axboe371d4562009-01-19 10:17:06 +0100117
118 td_verror(td, errno, "poll");
119 return -1;
120 } else if (!ret)
121 continue;
122
123 break;
124 }
125
126 if (pfd.revents & events)
127 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +0100128
129 return -1;
130}
131
Jens Axboeed92ac02007-02-06 14:43:52 +0100132static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
133{
Steven Langde890a12011-11-09 14:03:34 +0100134 struct netio_options *o = td->eo;
Jens Axboeed92ac02007-02-06 14:43:52 +0100135
Jens Axboe7a6499d2007-02-07 09:35:29 +0100136 /*
137 * Make sure we don't see spurious reads to a receiver, and vice versa
138 */
Steven Langde890a12011-11-09 14:03:34 +0100139 if (o->proto == FIO_TYPE_TCP)
140 return 0;
141
142 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
143 (!o->listen && io_u->ddir == DDIR_READ)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100144 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +0100145 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100146 }
Bruce Cran3f457be2012-10-10 13:37:41 +0100147
Jens Axboef85ac252008-03-01 18:09:49 +0100148 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100149}
150
Jens Axboe5921e802008-05-30 15:02:38 +0200151#ifdef FIO_HAVE_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +0200152static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200153{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200154 int bytes = 0;
155
156 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200157 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200158
159 if (ret < 0) {
160 if (!bytes)
161 bytes = ret;
162
163 break;
164 } else if (!ret)
165 break;
166
167 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200168 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200169 }
170
171 return bytes;
172}
173
174/*
Jens Axboecd963e12007-06-24 21:41:46 +0200175 * Receive bytes from a socket and fill them into the internal pipe
176 */
177static int splice_in(struct thread_data *td, struct io_u *io_u)
178{
179 struct netio_data *nd = td->io_ops->data;
180
181 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
182}
183
184/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200185 * Transmit 'len' bytes from the internal pipe
186 */
187static int splice_out(struct thread_data *td, struct io_u *io_u,
188 unsigned int len)
189{
190 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200191
192 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
193}
194
195static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
196{
197 struct iovec iov = {
198 .iov_base = io_u->xfer_buf,
199 .iov_len = len,
200 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200201 int bytes = 0;
202
Jens Axboecd963e12007-06-24 21:41:46 +0200203 while (iov.iov_len) {
204 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200205
206 if (ret < 0) {
207 if (!bytes)
208 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200209 break;
210 } else if (!ret)
211 break;
212
Jens Axboecd963e12007-06-24 21:41:46 +0200213 iov.iov_len -= ret;
214 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200215 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200216 }
217
218 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200219
Jens Axboe9cce02e2007-06-22 15:42:21 +0200220}
221
222/*
223 * vmsplice() pipe to io_u buffer
224 */
225static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
226 unsigned int len)
227{
228 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200229
Jens Axboecd963e12007-06-24 21:41:46 +0200230 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200231}
232
233/*
234 * vmsplice() io_u to pipe
235 */
236static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
237{
238 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200239
Jens Axboecd963e12007-06-24 21:41:46 +0200240 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200241}
242
Jens Axboecd963e12007-06-24 21:41:46 +0200243/*
244 * splice receive - transfer socket data into a pipe using splice, then map
245 * that pipe data into the io_u using vmsplice.
246 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200247static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
248{
249 int ret;
250
251 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200252 if (ret > 0)
253 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200254
Jens Axboecd963e12007-06-24 21:41:46 +0200255 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200256}
257
Jens Axboecd963e12007-06-24 21:41:46 +0200258/*
259 * splice transmit - map data from the io_u into a pipe by using vmsplice,
260 * then transfer that pipe to a socket using splice.
261 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200262static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
263{
264 int ret;
265
266 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200267 if (ret > 0)
268 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200269
Jens Axboecd963e12007-06-24 21:41:46 +0200270 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200271}
Jens Axboe5921e802008-05-30 15:02:38 +0200272#else
273static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
274{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200275 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200276 return -1;
277}
278
279static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
280{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200281 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200282 return -1;
283}
284#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200285
286static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
287{
Jens Axboe414c2a32009-01-16 13:21:15 +0100288 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100289 struct netio_options *o = td->eo;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400290 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100291
Jens Axboe664fb3b2009-01-19 13:26:36 +0100292 do {
Steven Langde890a12011-11-09 14:03:34 +0100293 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe62b38922009-05-11 10:37:33 +0200294 struct sockaddr *to = (struct sockaddr *) &nd->addr;
295
Jens Axboe664fb3b2009-01-19 13:26:36 +0100296 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200297 io_u->xfer_buflen, flags, to,
298 sizeof(*to));
Jens Axboe664fb3b2009-01-19 13:26:36 +0100299 } else {
300 /*
301 * if we are going to write more, set MSG_MORE
302 */
Jens Axboe5921e802008-05-30 15:02:38 +0200303#ifdef MSG_MORE
Jens Axboe664fb3b2009-01-19 13:26:36 +0100304 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
305 td->o.size)
306 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200307#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100308 ret = send(io_u->file->fd, io_u->xfer_buf,
309 io_u->xfer_buflen, flags);
310 }
311 if (ret > 0)
312 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200313
Jens Axboe664fb3b2009-01-19 13:26:36 +0100314 ret = poll_wait(td, io_u->file->fd, POLLOUT);
315 if (ret <= 0)
316 break;
317
Jens Axboe8e239ca2010-08-11 10:29:12 -0400318 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100319 } while (1);
320
321 return ret;
322}
323
324static int is_udp_close(struct io_u *io_u, int len)
325{
326 struct udp_close_msg *msg;
327
328 if (len != sizeof(struct udp_close_msg))
329 return 0;
330
331 msg = io_u->xfer_buf;
332 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
333 return 0;
334 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
335 return 0;
336
337 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200338}
339
Jens Axboe414c2a32009-01-16 13:21:15 +0100340static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200341{
Jens Axboe414c2a32009-01-16 13:21:15 +0100342 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100343 struct netio_options *o = td->eo;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400344 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100345
Jens Axboe664fb3b2009-01-19 13:26:36 +0100346 do {
Steven Langde890a12011-11-09 14:03:34 +0100347 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200348 fio_socklen_t len = sizeof(nd->addr);
Jens Axboe62b38922009-05-11 10:37:33 +0200349 struct sockaddr *from = (struct sockaddr *) &nd->addr;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200350
Jens Axboe664fb3b2009-01-19 13:26:36 +0100351 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200352 io_u->xfer_buflen, flags, from, &len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100353 if (is_udp_close(io_u, ret)) {
354 td->done = 1;
355 return 0;
356 }
357 } else {
358 ret = recv(io_u->file->fd, io_u->xfer_buf,
359 io_u->xfer_buflen, flags);
360 }
361 if (ret > 0)
362 break;
Jens Axboe7d988f62012-11-29 19:57:35 +0100363 else if (!ret && (flags & MSG_WAITALL))
364 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100365
Jens Axboe664fb3b2009-01-19 13:26:36 +0100366 ret = poll_wait(td, io_u->file->fd, POLLIN);
367 if (ret <= 0)
368 break;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400369 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100370 flags |= MSG_WAITALL;
371 } while (1);
372
373 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200374}
375
Jens Axboeed92ac02007-02-06 14:43:52 +0100376static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
377{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200378 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100379 struct netio_options *o = td->eo;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200380 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100381
Jens Axboe7101d9c2007-09-12 13:12:39 +0200382 fio_ro_check(td, io_u);
383
Jens Axboe7a6499d2007-02-07 09:35:29 +0100384 if (io_u->ddir == DDIR_WRITE) {
Steven Langde890a12011-11-09 14:03:34 +0100385 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
386 o->proto == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200387 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200388 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100389 ret = fio_netio_splice_out(td, io_u);
390 } else if (io_u->ddir == DDIR_READ) {
Steven Langde890a12011-11-09 14:03:34 +0100391 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
392 o->proto == FIO_TYPE_UNIX)
Jens Axboe414c2a32009-01-16 13:21:15 +0100393 ret = fio_netio_recv(td, io_u);
394 else
395 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100396 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100397 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100398
Jens Axboecec6b552007-02-06 20:15:38 +0100399 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100400 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100401 io_u->resid = io_u->xfer_buflen - ret;
402 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100403 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100404 } else {
405 int err = errno;
406
407 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
408 return FIO_Q_BUSY;
409
410 io_u->error = err;
411 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100412 }
413
Jens Axboe36167d82007-02-18 05:41:31 +0100414 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100415 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100416
Jens Axboe36167d82007-02-18 05:41:31 +0100417 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100418}
419
Jens Axboeb5af8292007-03-08 12:43:13 +0100420static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100421{
Jens Axboeb5af8292007-03-08 12:43:13 +0100422 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100423 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200424 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100425
Steven Langde890a12011-11-09 14:03:34 +0100426 if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200427 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100428 type = SOCK_STREAM;
Steven Langde890a12011-11-09 14:03:34 +0100429 } else if (o->proto == FIO_TYPE_UDP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200430 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100431 type = SOCK_DGRAM;
Steven Langde890a12011-11-09 14:03:34 +0100432 } else if (o->proto == FIO_TYPE_UNIX) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200433 domain = AF_UNIX;
434 type = SOCK_STREAM;
435 } else {
Steven Langde890a12011-11-09 14:03:34 +0100436 log_err("fio: bad network type %d\n", o->proto);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200437 f->fd = -1;
438 return 1;
439 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100440
Jens Axboe0fd666b2011-10-06 20:08:53 +0200441 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100442 if (f->fd < 0) {
443 td_verror(td, errno, "socket");
444 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100445 }
446
Steven Langde890a12011-11-09 14:03:34 +0100447 if (o->proto == FIO_TYPE_UDP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100448 return 0;
Steven Langde890a12011-11-09 14:03:34 +0100449 else if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200450 fio_socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100451
Jens Axboe0fd666b2011-10-06 20:08:53 +0200452 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
453 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200454 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200455 return 1;
456 }
457 } else {
458 struct sockaddr_un *addr = &nd->addr_un;
459 fio_socklen_t len;
460
461 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
462
463 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
464 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200465 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200466 return 1;
467 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100468 }
469
470 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100471}
472
Jens Axboeb5af8292007-03-08 12:43:13 +0100473static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100474{
Jens Axboeb5af8292007-03-08 12:43:13 +0100475 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100476 struct netio_options *o = td->eo;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200477 fio_socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100478
Steven Langde890a12011-11-09 14:03:34 +0100479 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100480 f->fd = nd->listenfd;
481 return 0;
482 }
483
Jens Axboe6d861442007-03-15 09:22:23 +0100484 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100485
Jens Axboe371d4562009-01-19 10:17:06 +0100486 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
487 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100488
Jens Axboe371d4562009-01-19 10:17:06 +0100489 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
490 if (f->fd < 0) {
491 td_verror(td, errno, "accept");
492 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100493 }
494
495 return 0;
496}
497
Jens Axboeb5af8292007-03-08 12:43:13 +0100498static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100499{
Jens Axboe0fd666b2011-10-06 20:08:53 +0200500 int ret;
Yufei Ren991802b2011-12-19 08:56:29 +0100501 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200502
Yufei Ren991802b2011-12-19 08:56:29 +0100503 if (o->listen)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200504 ret = fio_netio_accept(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100505 else
Jens Axboe0fd666b2011-10-06 20:08:53 +0200506 ret = fio_netio_connect(td, f);
507
508 if (ret)
509 f->fd = -1;
510 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +0100511}
512
Jens Axboe664fb3b2009-01-19 13:26:36 +0100513static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
514{
515 struct netio_data *nd = td->io_ops->data;
516 struct udp_close_msg msg;
Jens Axboe62b38922009-05-11 10:37:33 +0200517 struct sockaddr *to = (struct sockaddr *) &nd->addr;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100518 int ret;
519
520 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
521 msg.cmd = htonl(FIO_LINK_CLOSE);
522
Jens Axboe62b38922009-05-11 10:37:33 +0200523 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
Jens Axboe664fb3b2009-01-19 13:26:36 +0100524 sizeof(nd->addr));
525 if (ret < 0)
526 td_verror(td, errno, "sendto udp link close");
527}
528
529static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
530{
Steven Langde890a12011-11-09 14:03:34 +0100531 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100532
533 /*
534 * If this is an UDP connection, notify the receiver that we are
535 * closing down the link
536 */
Steven Langde890a12011-11-09 14:03:34 +0100537 if (o->proto == FIO_TYPE_UDP)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100538 fio_netio_udp_close(td, f);
539
540 return generic_close_file(td, f);
541}
542
Jens Axboe0fd666b2011-10-06 20:08:53 +0200543static int fio_netio_setup_connect_inet(struct thread_data *td,
544 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100545{
546 struct netio_data *nd = td->io_ops->data;
547
Jens Axboe166dce42012-11-29 14:35:33 +0100548 if (!host) {
549 log_err("fio: connect with no host to connect to.\n");
550 if (td_read(td))
551 log_err("fio: did you forget to set 'listen'?\n");
552
553 td_verror(td, EINVAL, "no hostname= set");
554 return 1;
555 }
556
Jens Axboeb5af8292007-03-08 12:43:13 +0100557 nd->addr.sin_family = AF_INET;
558 nd->addr.sin_port = htons(port);
559
560 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
561 struct hostent *hent;
562
563 hent = gethostbyname(host);
564 if (!hent) {
565 td_verror(td, errno, "gethostbyname");
566 return 1;
567 }
568
569 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
570 }
571
572 return 0;
573}
574
Jens Axboe0fd666b2011-10-06 20:08:53 +0200575static int fio_netio_setup_connect_unix(struct thread_data *td,
576 const char *path)
577{
578 struct netio_data *nd = td->io_ops->data;
579 struct sockaddr_un *soun = &nd->addr_un;
580
581 soun->sun_family = AF_UNIX;
582 strcpy(soun->sun_path, path);
583 return 0;
584}
585
Steven Langde890a12011-11-09 14:03:34 +0100586static int fio_netio_setup_connect(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200587{
Steven Langde890a12011-11-09 14:03:34 +0100588 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200589
Steven Langde890a12011-11-09 14:03:34 +0100590 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
591 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200592 else
Steven Langde890a12011-11-09 14:03:34 +0100593 return fio_netio_setup_connect_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200594}
595
596static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
597{
598 struct netio_data *nd = td->io_ops->data;
599 struct sockaddr_un *addr = &nd->addr_un;
600 mode_t mode;
601 int len, fd;
602
603 fd = socket(AF_UNIX, SOCK_STREAM, 0);
604 if (fd < 0) {
605 log_err("fio: socket: %s\n", strerror(errno));
606 return -1;
607 }
608
609 mode = umask(000);
610
611 memset(addr, 0, sizeof(*addr));
612 addr->sun_family = AF_UNIX;
613 strcpy(addr->sun_path, path);
614 unlink(path);
615
616 len = sizeof(addr->sun_family) + strlen(path) + 1;
617
618 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
619 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200620 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200621 return -1;
622 }
623
624 umask(mode);
625 nd->listenfd = fd;
626 return 0;
627}
628
629static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100630{
631 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100632 struct netio_options *o = td->eo;
Jens Axboe414c2a32009-01-16 13:21:15 +0100633 int fd, opt, type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100634
Steven Langde890a12011-11-09 14:03:34 +0100635 if (o->proto == FIO_TYPE_TCP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100636 type = SOCK_STREAM;
637 else
638 type = SOCK_DGRAM;
639
Jens Axboe0fd666b2011-10-06 20:08:53 +0200640 fd = socket(AF_INET, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +0100641 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100642 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100643 return 1;
644 }
645
646 opt = 1;
647 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100648 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100649 return 1;
650 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100651#ifdef SO_REUSEPORT
652 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100653 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100654 return 1;
655 }
656#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100657
Jens Axboeb5af8292007-03-08 12:43:13 +0100658 nd->addr.sin_family = AF_INET;
659 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
660 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100661
Jens Axboeb5af8292007-03-08 12:43:13 +0100662 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100663 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100664 return 1;
665 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200666
667 nd->listenfd = fd;
668 return 0;
669}
670
Steven Langde890a12011-11-09 14:03:34 +0100671static int fio_netio_setup_listen(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200672{
673 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100674 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200675 int ret;
676
Steven Langde890a12011-11-09 14:03:34 +0100677 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
678 ret = fio_netio_setup_listen_inet(td, o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200679 else
Steven Langde890a12011-11-09 14:03:34 +0100680 ret = fio_netio_setup_listen_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200681
682 if (ret)
683 return ret;
Steven Langde890a12011-11-09 14:03:34 +0100684 if (o->proto == FIO_TYPE_UDP)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200685 return 0;
686
687 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100688 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +0200689 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100690 return 1;
691 }
692
Jens Axboeb5af8292007-03-08 12:43:13 +0100693 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100694}
695
Jens Axboe9bec88e2007-03-02 08:55:48 +0100696static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100697{
Steven Langde890a12011-11-09 14:03:34 +0100698 struct netio_options *o = td->eo;
Jens Axboeaf52b342007-03-13 10:07:47 +0100699 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100700
Bruce Cran3f457be2012-10-10 13:37:41 +0100701#ifdef WIN32
702 WSADATA wsd;
703 WSAStartup(MAKEWORD(2,2), &wsd);
704#endif
705
Jens Axboe16d55aa2007-05-22 09:21:37 +0200706 if (td_random(td)) {
707 log_err("fio: network IO can't be random\n");
708 return 1;
709 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100710
Steven Langde890a12011-11-09 14:03:34 +0100711 if (o->proto == FIO_TYPE_UNIX && o->port) {
712 log_err("fio: network IO port not valid with unix socket\n");
713 return 1;
714 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
715 log_err("fio: network IO requires port for tcp or udp\n");
716 return 1;
Jens Axboe414c2a32009-01-16 13:21:15 +0100717 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200718
Steven Langde890a12011-11-09 14:03:34 +0100719 if (o->proto != FIO_TYPE_TCP) {
720 if (o->listen) {
Jens Axboe9b986062011-12-19 08:57:18 +0100721 log_err("fio: listen only valid for TCP proto IO\n");
722 return 1;
Steven Langde890a12011-11-09 14:03:34 +0100723 }
724 if (td_rw(td)) {
Jens Axboe9b986062011-12-19 08:57:18 +0100725 log_err("fio: datagram network connections must be"
Steven Langde890a12011-11-09 14:03:34 +0100726 " read OR write\n");
Jens Axboe9b986062011-12-19 08:57:18 +0100727 return 1;
728 }
729 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
730 log_err("fio: UNIX sockets need host/filename\n");
731 return 1;
Steven Langde890a12011-11-09 14:03:34 +0100732 }
733 o->listen = td_read(td);
734 }
735
736 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
737 log_err("fio: hostname not valid for inbound network IO\n");
738 return 1;
739 }
740
741 if (o->listen)
742 ret = fio_netio_setup_listen(td);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200743 else
Steven Langde890a12011-11-09 14:03:34 +0100744 ret = fio_netio_setup_connect(td);
Jens Axboeed92ac02007-02-06 14:43:52 +0100745
Jens Axboe7bb48f82007-03-27 15:30:28 +0200746 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100747}
748
Jens Axboeb5af8292007-03-08 12:43:13 +0100749static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100750{
Jens Axboeb5af8292007-03-08 12:43:13 +0100751 struct netio_data *nd = td->io_ops->data;
752
753 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +0200754 if (nd->listenfd != -1)
755 close(nd->listenfd);
756 if (nd->pipes[0] != -1)
757 close(nd->pipes[0]);
758 if (nd->pipes[1] != -1)
759 close(nd->pipes[1]);
760
Jens Axboeb5af8292007-03-08 12:43:13 +0100761 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +0100762 }
763}
764
765static int fio_netio_setup(struct thread_data *td)
766{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200767 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100768
Steven Langde890a12011-11-09 14:03:34 +0100769 if (!td->files_index) {
770 add_file(td, td->o.filename ?: "net");
771 td->o.nr_files = td->o.nr_files ?: 1;
772 }
773
Jens Axboe7bb48f82007-03-27 15:30:28 +0200774 if (!td->io_ops->data) {
775 nd = malloc(sizeof(*nd));;
776
777 memset(nd, 0, sizeof(*nd));
778 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +0200779 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200780 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200781 }
782
Jens Axboe9bec88e2007-03-02 08:55:48 +0100783 return 0;
784}
785
Jens Axboe5921e802008-05-30 15:02:38 +0200786#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200787static int fio_netio_setup_splice(struct thread_data *td)
788{
789 struct netio_data *nd;
790
791 fio_netio_setup(td);
792
793 nd = td->io_ops->data;
794 if (nd) {
795 if (pipe(nd->pipes) < 0)
796 return 1;
797
798 nd->use_splice = 1;
799 return 0;
800 }
801
802 return 1;
803}
804
Jens Axboe5921e802008-05-30 15:02:38 +0200805static struct ioengine_ops ioengine_splice = {
Steven Langde890a12011-11-09 14:03:34 +0100806 .name = "netsplice",
807 .version = FIO_IOOPS_VERSION,
808 .prep = fio_netio_prep,
809 .queue = fio_netio_queue,
810 .setup = fio_netio_setup_splice,
811 .init = fio_netio_init,
812 .cleanup = fio_netio_cleanup,
813 .open_file = fio_netio_open_file,
814 .close_file = generic_close_file,
815 .options = options,
816 .option_struct_size = sizeof(struct netio_options),
817 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
818 FIO_SIGTERM | FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +0200819};
820#endif
821
Jens Axboe9cce02e2007-06-22 15:42:21 +0200822static struct ioengine_ops ioengine_rw = {
Steven Langde890a12011-11-09 14:03:34 +0100823 .name = "net",
824 .version = FIO_IOOPS_VERSION,
825 .prep = fio_netio_prep,
826 .queue = fio_netio_queue,
827 .setup = fio_netio_setup,
828 .init = fio_netio_init,
829 .cleanup = fio_netio_cleanup,
830 .open_file = fio_netio_open_file,
831 .close_file = fio_netio_close_file,
832 .options = options,
833 .option_struct_size = sizeof(struct netio_options),
834 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
835 FIO_SIGTERM | FIO_PIPEIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100836};
837
Steven Langde890a12011-11-09 14:03:34 +0100838static int str_hostname_cb(void *data, const char *input)
839{
840 struct netio_options *o = data;
841
842 if (o->td->o.filename)
843 free(o->td->o.filename);
844 o->td->o.filename = strdup(input);
845 return 0;
846}
847
Jens Axboeed92ac02007-02-06 14:43:52 +0100848static void fio_init fio_netio_register(void)
849{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200850 register_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200851#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200852 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200853#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100854}
855
856static void fio_exit fio_netio_unregister(void)
857{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200858 unregister_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200859#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200860 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200861#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100862}