blob: fb554a4808745f278054515b05c62370f36c154c [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,
Jens Axboeb96d2432012-11-30 08:27:46 +010045 FIO_LINK_OPEN_CLOSE_MAGIC = 0x6c696e6b,
46 FIO_LINK_OPEN = 0x98,
Jens Axboe0fd666b2011-10-06 20:08:53 +020047
48 FIO_TYPE_TCP = 1,
49 FIO_TYPE_UDP = 2,
50 FIO_TYPE_UNIX = 3,
Jens Axboe664fb3b2009-01-19 13:26:36 +010051};
52
Steven Langde890a12011-11-09 14:03:34 +010053static int str_hostname_cb(void *data, const char *input);
54static struct fio_option options[] = {
55 {
56 .name = "hostname",
57 .type = FIO_OPT_STR_STORE,
58 .cb = str_hostname_cb,
59 .help = "Hostname for net IO engine",
60 },
61 {
62 .name = "port",
63 .type = FIO_OPT_INT,
64 .off1 = offsetof(struct netio_options, port),
65 .minval = 1,
66 .maxval = 65535,
67 .help = "Port to use for TCP or UDP net connections",
68 },
69 {
70 .name = "protocol",
71 .alias = "proto",
72 .type = FIO_OPT_STR,
73 .off1 = offsetof(struct netio_options, proto),
74 .help = "Network protocol to use",
75 .def = "tcp",
76 .posval = {
77 { .ival = "tcp",
78 .oval = FIO_TYPE_TCP,
79 .help = "Transmission Control Protocol",
80 },
81 { .ival = "udp",
82 .oval = FIO_TYPE_UDP,
Bruce Cranf5cc3d02012-10-10 08:17:44 -060083 .help = "User Datagram Protocol",
Steven Langde890a12011-11-09 14:03:34 +010084 },
85 { .ival = "unix",
86 .oval = FIO_TYPE_UNIX,
87 .help = "UNIX domain socket",
88 },
89 },
90 },
91 {
92 .name = "listen",
93 .type = FIO_OPT_STR_SET,
94 .off1 = offsetof(struct netio_options, listen),
95 .help = "Listen for incoming TCP connections",
96 },
97 {
98 .name = NULL,
99 },
100};
101
Jens Axboe371d4562009-01-19 10:17:06 +0100102/*
103 * Return -1 for error and 'nr events' for a positive number
104 * of events
105 */
106static int poll_wait(struct thread_data *td, int fd, short events)
107{
108 struct pollfd pfd;
109 int ret;
110
111 while (!td->terminate) {
112 pfd.fd = fd;
113 pfd.events = events;
114 ret = poll(&pfd, 1, -1);
115 if (ret < 0) {
116 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +0100117 break;
Jens Axboe371d4562009-01-19 10:17:06 +0100118
119 td_verror(td, errno, "poll");
120 return -1;
121 } else if (!ret)
122 continue;
123
124 break;
125 }
126
127 if (pfd.revents & events)
128 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +0100129
130 return -1;
131}
132
Jens Axboeed92ac02007-02-06 14:43:52 +0100133static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
134{
Steven Langde890a12011-11-09 14:03:34 +0100135 struct netio_options *o = td->eo;
Jens Axboeed92ac02007-02-06 14:43:52 +0100136
Jens Axboe7a6499d2007-02-07 09:35:29 +0100137 /*
138 * Make sure we don't see spurious reads to a receiver, and vice versa
139 */
Steven Langde890a12011-11-09 14:03:34 +0100140 if (o->proto == FIO_TYPE_TCP)
141 return 0;
142
143 if ((o->listen && io_u->ddir == DDIR_WRITE) ||
144 (!o->listen && io_u->ddir == DDIR_READ)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100145 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +0100146 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100147 }
Bruce Cran3f457be2012-10-10 13:37:41 +0100148
Jens Axboef85ac252008-03-01 18:09:49 +0100149 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100150}
151
Jens Axboe5921e802008-05-30 15:02:38 +0200152#ifdef FIO_HAVE_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +0200153static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200154{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200155 int bytes = 0;
156
157 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200158 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200159
160 if (ret < 0) {
161 if (!bytes)
162 bytes = ret;
163
164 break;
165 } else if (!ret)
166 break;
167
168 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200169 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200170 }
171
172 return bytes;
173}
174
175/*
Jens Axboecd963e12007-06-24 21:41:46 +0200176 * Receive bytes from a socket and fill them into the internal pipe
177 */
178static int splice_in(struct thread_data *td, struct io_u *io_u)
179{
180 struct netio_data *nd = td->io_ops->data;
181
182 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
183}
184
185/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200186 * Transmit 'len' bytes from the internal pipe
187 */
188static int splice_out(struct thread_data *td, struct io_u *io_u,
189 unsigned int len)
190{
191 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200192
193 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
194}
195
196static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
197{
198 struct iovec iov = {
199 .iov_base = io_u->xfer_buf,
200 .iov_len = len,
201 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200202 int bytes = 0;
203
Jens Axboecd963e12007-06-24 21:41:46 +0200204 while (iov.iov_len) {
205 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200206
207 if (ret < 0) {
208 if (!bytes)
209 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200210 break;
211 } else if (!ret)
212 break;
213
Jens Axboecd963e12007-06-24 21:41:46 +0200214 iov.iov_len -= ret;
215 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200216 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200217 }
218
219 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200220
Jens Axboe9cce02e2007-06-22 15:42:21 +0200221}
222
223/*
224 * vmsplice() pipe to io_u buffer
225 */
226static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
227 unsigned int len)
228{
229 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200230
Jens Axboecd963e12007-06-24 21:41:46 +0200231 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200232}
233
234/*
235 * vmsplice() io_u to pipe
236 */
237static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
238{
239 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200240
Jens Axboecd963e12007-06-24 21:41:46 +0200241 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200242}
243
Jens Axboecd963e12007-06-24 21:41:46 +0200244/*
245 * splice receive - transfer socket data into a pipe using splice, then map
246 * that pipe data into the io_u using vmsplice.
247 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200248static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
249{
250 int ret;
251
252 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200253 if (ret > 0)
254 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200255
Jens Axboecd963e12007-06-24 21:41:46 +0200256 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200257}
258
Jens Axboecd963e12007-06-24 21:41:46 +0200259/*
260 * splice transmit - map data from the io_u into a pipe by using vmsplice,
261 * then transfer that pipe to a socket using splice.
262 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200263static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
264{
265 int ret;
266
267 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200268 if (ret > 0)
269 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200270
Jens Axboecd963e12007-06-24 21:41:46 +0200271 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200272}
Jens Axboe5921e802008-05-30 15:02:38 +0200273#else
274static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
275{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200276 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200277 return -1;
278}
279
280static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
281{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200282 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200283 return -1;
284}
285#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200286
287static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
288{
Jens Axboe414c2a32009-01-16 13:21:15 +0100289 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100290 struct netio_options *o = td->eo;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400291 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100292
Jens Axboe664fb3b2009-01-19 13:26:36 +0100293 do {
Steven Langde890a12011-11-09 14:03:34 +0100294 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe62b38922009-05-11 10:37:33 +0200295 struct sockaddr *to = (struct sockaddr *) &nd->addr;
296
Jens Axboe664fb3b2009-01-19 13:26:36 +0100297 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200298 io_u->xfer_buflen, flags, to,
299 sizeof(*to));
Jens Axboe664fb3b2009-01-19 13:26:36 +0100300 } else {
301 /*
302 * if we are going to write more, set MSG_MORE
303 */
Jens Axboe5921e802008-05-30 15:02:38 +0200304#ifdef MSG_MORE
Jens Axboe664fb3b2009-01-19 13:26:36 +0100305 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
306 td->o.size)
307 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200308#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100309 ret = send(io_u->file->fd, io_u->xfer_buf,
310 io_u->xfer_buflen, flags);
311 }
312 if (ret > 0)
313 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200314
Jens Axboe664fb3b2009-01-19 13:26:36 +0100315 ret = poll_wait(td, io_u->file->fd, POLLOUT);
316 if (ret <= 0)
317 break;
318
Jens Axboe8e239ca2010-08-11 10:29:12 -0400319 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100320 } while (1);
321
322 return ret;
323}
324
325static int is_udp_close(struct io_u *io_u, int len)
326{
327 struct udp_close_msg *msg;
328
329 if (len != sizeof(struct udp_close_msg))
330 return 0;
331
332 msg = io_u->xfer_buf;
Jens Axboeb96d2432012-11-30 08:27:46 +0100333 if (ntohl(msg->magic) != FIO_LINK_OPEN_CLOSE_MAGIC)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100334 return 0;
335 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
336 return 0;
337
338 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200339}
340
Jens Axboe414c2a32009-01-16 13:21:15 +0100341static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200342{
Jens Axboe414c2a32009-01-16 13:21:15 +0100343 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100344 struct netio_options *o = td->eo;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400345 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100346
Jens Axboe664fb3b2009-01-19 13:26:36 +0100347 do {
Steven Langde890a12011-11-09 14:03:34 +0100348 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200349 fio_socklen_t len = sizeof(nd->addr);
Jens Axboe62b38922009-05-11 10:37:33 +0200350 struct sockaddr *from = (struct sockaddr *) &nd->addr;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200351
Jens Axboe664fb3b2009-01-19 13:26:36 +0100352 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200353 io_u->xfer_buflen, flags, from, &len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100354 if (is_udp_close(io_u, ret)) {
355 td->done = 1;
356 return 0;
357 }
358 } else {
359 ret = recv(io_u->file->fd, io_u->xfer_buf,
360 io_u->xfer_buflen, flags);
361 }
362 if (ret > 0)
363 break;
Jens Axboe7d988f62012-11-29 19:57:35 +0100364 else if (!ret && (flags & MSG_WAITALL))
365 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100366
Jens Axboe664fb3b2009-01-19 13:26:36 +0100367 ret = poll_wait(td, io_u->file->fd, POLLIN);
368 if (ret <= 0)
369 break;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400370 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100371 flags |= MSG_WAITALL;
372 } while (1);
373
374 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200375}
376
Jens Axboeed92ac02007-02-06 14:43:52 +0100377static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
378{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200379 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100380 struct netio_options *o = td->eo;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200381 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100382
Jens Axboe7101d9c2007-09-12 13:12:39 +0200383 fio_ro_check(td, io_u);
384
Jens Axboe7a6499d2007-02-07 09:35:29 +0100385 if (io_u->ddir == DDIR_WRITE) {
Steven Langde890a12011-11-09 14:03:34 +0100386 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
387 o->proto == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200388 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200389 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100390 ret = fio_netio_splice_out(td, io_u);
391 } else if (io_u->ddir == DDIR_READ) {
Steven Langde890a12011-11-09 14:03:34 +0100392 if (!nd->use_splice || o->proto == FIO_TYPE_UDP ||
393 o->proto == FIO_TYPE_UNIX)
Jens Axboe414c2a32009-01-16 13:21:15 +0100394 ret = fio_netio_recv(td, io_u);
395 else
396 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100397 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100398 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100399
Jens Axboecec6b552007-02-06 20:15:38 +0100400 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100401 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100402 io_u->resid = io_u->xfer_buflen - ret;
403 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100404 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100405 } else {
406 int err = errno;
407
408 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
409 return FIO_Q_BUSY;
410
411 io_u->error = err;
412 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100413 }
414
Jens Axboe36167d82007-02-18 05:41:31 +0100415 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100416 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100417
Jens Axboe36167d82007-02-18 05:41:31 +0100418 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100419}
420
Jens Axboeb5af8292007-03-08 12:43:13 +0100421static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100422{
Jens Axboeb5af8292007-03-08 12:43:13 +0100423 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100424 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200425 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100426
Steven Langde890a12011-11-09 14:03:34 +0100427 if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200428 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100429 type = SOCK_STREAM;
Steven Langde890a12011-11-09 14:03:34 +0100430 } else if (o->proto == FIO_TYPE_UDP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200431 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100432 type = SOCK_DGRAM;
Steven Langde890a12011-11-09 14:03:34 +0100433 } else if (o->proto == FIO_TYPE_UNIX) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200434 domain = AF_UNIX;
435 type = SOCK_STREAM;
436 } else {
Steven Langde890a12011-11-09 14:03:34 +0100437 log_err("fio: bad network type %d\n", o->proto);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200438 f->fd = -1;
439 return 1;
440 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100441
Jens Axboe0fd666b2011-10-06 20:08:53 +0200442 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100443 if (f->fd < 0) {
444 td_verror(td, errno, "socket");
445 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100446 }
447
Steven Langde890a12011-11-09 14:03:34 +0100448 if (o->proto == FIO_TYPE_UDP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100449 return 0;
Steven Langde890a12011-11-09 14:03:34 +0100450 else if (o->proto == FIO_TYPE_TCP) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200451 fio_socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100452
Jens Axboe0fd666b2011-10-06 20:08:53 +0200453 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
454 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200455 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200456 return 1;
457 }
458 } else {
459 struct sockaddr_un *addr = &nd->addr_un;
460 fio_socklen_t len;
461
462 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
463
464 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
465 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200466 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200467 return 1;
468 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100469 }
470
471 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100472}
473
Jens Axboeb5af8292007-03-08 12:43:13 +0100474static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100475{
Jens Axboeb5af8292007-03-08 12:43:13 +0100476 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100477 struct netio_options *o = td->eo;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200478 fio_socklen_t socklen = sizeof(nd->addr);
Jens Axboe859088d2012-11-29 20:02:50 +0100479 int state;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100480
Steven Langde890a12011-11-09 14:03:34 +0100481 if (o->proto == FIO_TYPE_UDP) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100482 f->fd = nd->listenfd;
483 return 0;
484 }
485
Jens Axboe859088d2012-11-29 20:02:50 +0100486 state = td->runstate;
487 td_set_runstate(td, TD_SETTING_UP);
488
Jens Axboe6d861442007-03-15 09:22:23 +0100489 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100490
Jens Axboe371d4562009-01-19 10:17:06 +0100491 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
Jens Axboe859088d2012-11-29 20:02:50 +0100492 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100493
Jens Axboe371d4562009-01-19 10:17:06 +0100494 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
495 if (f->fd < 0) {
496 td_verror(td, errno, "accept");
Jens Axboe859088d2012-11-29 20:02:50 +0100497 goto err;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100498 }
499
Jens Axboe859088d2012-11-29 20:02:50 +0100500 td_set_runstate(td, state);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100501 return 0;
Jens Axboe859088d2012-11-29 20:02:50 +0100502err:
503 td_set_runstate(td, state);
504 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100505}
506
Jens Axboe664fb3b2009-01-19 13:26:36 +0100507static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
508{
509 struct netio_data *nd = td->io_ops->data;
510 struct udp_close_msg msg;
Jens Axboe62b38922009-05-11 10:37:33 +0200511 struct sockaddr *to = (struct sockaddr *) &nd->addr;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100512 int ret;
513
Jens Axboeb96d2432012-11-30 08:27:46 +0100514 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100515 msg.cmd = htonl(FIO_LINK_CLOSE);
516
Jens Axboe62b38922009-05-11 10:37:33 +0200517 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
Jens Axboe664fb3b2009-01-19 13:26:36 +0100518 sizeof(nd->addr));
519 if (ret < 0)
520 td_verror(td, errno, "sendto udp link close");
521}
522
523static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
524{
Steven Langde890a12011-11-09 14:03:34 +0100525 struct netio_options *o = td->eo;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100526
527 /*
528 * If this is an UDP connection, notify the receiver that we are
529 * closing down the link
530 */
Steven Langde890a12011-11-09 14:03:34 +0100531 if (o->proto == FIO_TYPE_UDP)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100532 fio_netio_udp_close(td, f);
533
534 return generic_close_file(td, f);
535}
536
Jens Axboeb96d2432012-11-30 08:27:46 +0100537static int fio_netio_udp_recv_open(struct thread_data *td, struct fio_file *f)
538{
539 struct netio_data *nd = td->io_ops->data;
540 struct udp_close_msg msg;
541 struct sockaddr *to = (struct sockaddr *) &nd->addr;
542 fio_socklen_t len = sizeof(nd->addr);
543 int ret;
544
545 ret = recvfrom(f->fd, &msg, sizeof(msg), MSG_WAITALL, to, &len);
546 if (ret < 0) {
547 td_verror(td, errno, "sendto udp link open");
548 return ret;
549 }
550
551 if (ntohl(msg.magic) != FIO_LINK_OPEN_CLOSE_MAGIC ||
552 ntohl(msg.cmd) != FIO_LINK_OPEN) {
553 log_err("fio: bad udp open magic %x/%x\n", ntohl(msg.magic),
554 ntohl(msg.cmd));
555 return -1;
556 }
557
558 return 0;
559}
560
561static int fio_netio_udp_send_open(struct thread_data *td, struct fio_file *f)
562{
563 struct netio_data *nd = td->io_ops->data;
564 struct udp_close_msg msg;
565 struct sockaddr *to = (struct sockaddr *) &nd->addr;
566 int ret;
567
568 msg.magic = htonl(FIO_LINK_OPEN_CLOSE_MAGIC);
569 msg.cmd = htonl(FIO_LINK_OPEN);
570
571 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
572 sizeof(nd->addr));
573 if (ret < 0) {
574 td_verror(td, errno, "sendto udp link open");
575 return ret;
576 }
577
578 return 0;
579}
580
581static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
582{
583 int ret;
584 struct netio_options *o = td->eo;
585
586 if (o->listen)
587 ret = fio_netio_accept(td, f);
588 else
589 ret = fio_netio_connect(td, f);
590
591 if (ret) {
592 f->fd = -1;
593 return ret;
594 }
595
596 if (o->proto == FIO_TYPE_UDP) {
597 if (td_write(td))
598 ret = fio_netio_udp_send_open(td, f);
599 else {
600 int state;
601
602 state = td->runstate;
603 td_set_runstate(td, TD_SETTING_UP);
604 ret = fio_netio_udp_recv_open(td, f);
605 td_set_runstate(td, state);
606 }
607 }
608
609 if (ret)
610 fio_netio_close_file(td, f);
611
612 return ret;
613}
614
Jens Axboe0fd666b2011-10-06 20:08:53 +0200615static int fio_netio_setup_connect_inet(struct thread_data *td,
616 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100617{
618 struct netio_data *nd = td->io_ops->data;
619
Jens Axboe166dce42012-11-29 14:35:33 +0100620 if (!host) {
621 log_err("fio: connect with no host to connect to.\n");
622 if (td_read(td))
623 log_err("fio: did you forget to set 'listen'?\n");
624
625 td_verror(td, EINVAL, "no hostname= set");
626 return 1;
627 }
628
Jens Axboeb5af8292007-03-08 12:43:13 +0100629 nd->addr.sin_family = AF_INET;
630 nd->addr.sin_port = htons(port);
631
632 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
633 struct hostent *hent;
634
635 hent = gethostbyname(host);
636 if (!hent) {
637 td_verror(td, errno, "gethostbyname");
638 return 1;
639 }
640
641 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
642 }
643
644 return 0;
645}
646
Jens Axboe0fd666b2011-10-06 20:08:53 +0200647static int fio_netio_setup_connect_unix(struct thread_data *td,
648 const char *path)
649{
650 struct netio_data *nd = td->io_ops->data;
651 struct sockaddr_un *soun = &nd->addr_un;
652
653 soun->sun_family = AF_UNIX;
654 strcpy(soun->sun_path, path);
655 return 0;
656}
657
Steven Langde890a12011-11-09 14:03:34 +0100658static int fio_netio_setup_connect(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200659{
Steven Langde890a12011-11-09 14:03:34 +0100660 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200661
Steven Langde890a12011-11-09 14:03:34 +0100662 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
663 return fio_netio_setup_connect_inet(td, td->o.filename,o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200664 else
Steven Langde890a12011-11-09 14:03:34 +0100665 return fio_netio_setup_connect_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200666}
667
668static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
669{
670 struct netio_data *nd = td->io_ops->data;
671 struct sockaddr_un *addr = &nd->addr_un;
672 mode_t mode;
673 int len, fd;
674
675 fd = socket(AF_UNIX, SOCK_STREAM, 0);
676 if (fd < 0) {
677 log_err("fio: socket: %s\n", strerror(errno));
678 return -1;
679 }
680
681 mode = umask(000);
682
683 memset(addr, 0, sizeof(*addr));
684 addr->sun_family = AF_UNIX;
685 strcpy(addr->sun_path, path);
686 unlink(path);
687
688 len = sizeof(addr->sun_family) + strlen(path) + 1;
689
690 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
691 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200692 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200693 return -1;
694 }
695
696 umask(mode);
697 nd->listenfd = fd;
698 return 0;
699}
700
701static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100702{
703 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100704 struct netio_options *o = td->eo;
Jens Axboe414c2a32009-01-16 13:21:15 +0100705 int fd, opt, type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100706
Steven Langde890a12011-11-09 14:03:34 +0100707 if (o->proto == FIO_TYPE_TCP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100708 type = SOCK_STREAM;
709 else
710 type = SOCK_DGRAM;
711
Jens Axboe0fd666b2011-10-06 20:08:53 +0200712 fd = socket(AF_INET, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +0100713 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100714 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100715 return 1;
716 }
717
718 opt = 1;
719 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100720 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100721 return 1;
722 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100723#ifdef SO_REUSEPORT
724 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100725 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100726 return 1;
727 }
728#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100729
Jens Axboeb5af8292007-03-08 12:43:13 +0100730 nd->addr.sin_family = AF_INET;
731 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
732 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100733
Jens Axboeb5af8292007-03-08 12:43:13 +0100734 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100735 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100736 return 1;
737 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200738
739 nd->listenfd = fd;
740 return 0;
741}
742
Steven Langde890a12011-11-09 14:03:34 +0100743static int fio_netio_setup_listen(struct thread_data *td)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200744{
745 struct netio_data *nd = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100746 struct netio_options *o = td->eo;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200747 int ret;
748
Steven Langde890a12011-11-09 14:03:34 +0100749 if (o->proto == FIO_TYPE_UDP || o->proto == FIO_TYPE_TCP)
750 ret = fio_netio_setup_listen_inet(td, o->port);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200751 else
Steven Langde890a12011-11-09 14:03:34 +0100752 ret = fio_netio_setup_listen_unix(td, td->o.filename);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200753
754 if (ret)
755 return ret;
Steven Langde890a12011-11-09 14:03:34 +0100756 if (o->proto == FIO_TYPE_UDP)
Jens Axboe0fd666b2011-10-06 20:08:53 +0200757 return 0;
758
759 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100760 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +0200761 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100762 return 1;
763 }
764
Jens Axboeb5af8292007-03-08 12:43:13 +0100765 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100766}
767
Jens Axboe9bec88e2007-03-02 08:55:48 +0100768static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100769{
Steven Langde890a12011-11-09 14:03:34 +0100770 struct netio_options *o = td->eo;
Jens Axboeaf52b342007-03-13 10:07:47 +0100771 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100772
Bruce Cran3f457be2012-10-10 13:37:41 +0100773#ifdef WIN32
774 WSADATA wsd;
775 WSAStartup(MAKEWORD(2,2), &wsd);
776#endif
777
Jens Axboe16d55aa2007-05-22 09:21:37 +0200778 if (td_random(td)) {
779 log_err("fio: network IO can't be random\n");
780 return 1;
781 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100782
Steven Langde890a12011-11-09 14:03:34 +0100783 if (o->proto == FIO_TYPE_UNIX && o->port) {
784 log_err("fio: network IO port not valid with unix socket\n");
785 return 1;
786 } else if (o->proto != FIO_TYPE_UNIX && !o->port) {
787 log_err("fio: network IO requires port for tcp or udp\n");
788 return 1;
Jens Axboe414c2a32009-01-16 13:21:15 +0100789 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200790
Steven Langde890a12011-11-09 14:03:34 +0100791 if (o->proto != FIO_TYPE_TCP) {
792 if (o->listen) {
Jens Axboe9b986062011-12-19 08:57:18 +0100793 log_err("fio: listen only valid for TCP proto IO\n");
794 return 1;
Steven Langde890a12011-11-09 14:03:34 +0100795 }
796 if (td_rw(td)) {
Jens Axboe9b986062011-12-19 08:57:18 +0100797 log_err("fio: datagram network connections must be"
Steven Langde890a12011-11-09 14:03:34 +0100798 " read OR write\n");
Jens Axboe9b986062011-12-19 08:57:18 +0100799 return 1;
800 }
801 if (o->proto == FIO_TYPE_UNIX && !td->o.filename) {
802 log_err("fio: UNIX sockets need host/filename\n");
803 return 1;
Steven Langde890a12011-11-09 14:03:34 +0100804 }
805 o->listen = td_read(td);
806 }
807
808 if (o->proto != FIO_TYPE_UNIX && o->listen && td->o.filename) {
809 log_err("fio: hostname not valid for inbound network IO\n");
810 return 1;
811 }
812
813 if (o->listen)
814 ret = fio_netio_setup_listen(td);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200815 else
Steven Langde890a12011-11-09 14:03:34 +0100816 ret = fio_netio_setup_connect(td);
Jens Axboeed92ac02007-02-06 14:43:52 +0100817
Jens Axboe7bb48f82007-03-27 15:30:28 +0200818 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100819}
820
Jens Axboeb5af8292007-03-08 12:43:13 +0100821static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100822{
Jens Axboeb5af8292007-03-08 12:43:13 +0100823 struct netio_data *nd = td->io_ops->data;
824
825 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +0200826 if (nd->listenfd != -1)
827 close(nd->listenfd);
828 if (nd->pipes[0] != -1)
829 close(nd->pipes[0]);
830 if (nd->pipes[1] != -1)
831 close(nd->pipes[1]);
832
Jens Axboeb5af8292007-03-08 12:43:13 +0100833 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +0100834 }
835}
836
837static int fio_netio_setup(struct thread_data *td)
838{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200839 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100840
Steven Langde890a12011-11-09 14:03:34 +0100841 if (!td->files_index) {
842 add_file(td, td->o.filename ?: "net");
843 td->o.nr_files = td->o.nr_files ?: 1;
844 }
845
Jens Axboe7bb48f82007-03-27 15:30:28 +0200846 if (!td->io_ops->data) {
847 nd = malloc(sizeof(*nd));;
848
849 memset(nd, 0, sizeof(*nd));
850 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +0200851 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200852 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200853 }
854
Jens Axboe9bec88e2007-03-02 08:55:48 +0100855 return 0;
856}
857
Jens Axboe5921e802008-05-30 15:02:38 +0200858#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200859static int fio_netio_setup_splice(struct thread_data *td)
860{
861 struct netio_data *nd;
862
863 fio_netio_setup(td);
864
865 nd = td->io_ops->data;
866 if (nd) {
867 if (pipe(nd->pipes) < 0)
868 return 1;
869
870 nd->use_splice = 1;
871 return 0;
872 }
873
874 return 1;
875}
876
Jens Axboe5921e802008-05-30 15:02:38 +0200877static struct ioengine_ops ioengine_splice = {
Steven Langde890a12011-11-09 14:03:34 +0100878 .name = "netsplice",
879 .version = FIO_IOOPS_VERSION,
880 .prep = fio_netio_prep,
881 .queue = fio_netio_queue,
882 .setup = fio_netio_setup_splice,
883 .init = fio_netio_init,
884 .cleanup = fio_netio_cleanup,
885 .open_file = fio_netio_open_file,
886 .close_file = generic_close_file,
887 .options = options,
888 .option_struct_size = sizeof(struct netio_options),
889 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
890 FIO_SIGTERM | FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +0200891};
892#endif
893
Jens Axboe9cce02e2007-06-22 15:42:21 +0200894static struct ioengine_ops ioengine_rw = {
Steven Langde890a12011-11-09 14:03:34 +0100895 .name = "net",
896 .version = FIO_IOOPS_VERSION,
897 .prep = fio_netio_prep,
898 .queue = fio_netio_queue,
899 .setup = fio_netio_setup,
900 .init = fio_netio_init,
901 .cleanup = fio_netio_cleanup,
902 .open_file = fio_netio_open_file,
903 .close_file = fio_netio_close_file,
904 .options = options,
905 .option_struct_size = sizeof(struct netio_options),
906 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
907 FIO_SIGTERM | FIO_PIPEIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100908};
909
Steven Langde890a12011-11-09 14:03:34 +0100910static int str_hostname_cb(void *data, const char *input)
911{
912 struct netio_options *o = data;
913
914 if (o->td->o.filename)
915 free(o->td->o.filename);
916 o->td->o.filename = strdup(input);
917 return 0;
918}
919
Jens Axboeed92ac02007-02-06 14:43:52 +0100920static void fio_init fio_netio_register(void)
921{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200922 register_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200923#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200924 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200925#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100926}
927
928static void fio_exit fio_netio_unregister(void)
929{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200930 unregister_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200931#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200932 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200933#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100934}