blob: 0566a618254638f77bedc5d46c2ec80c3eb8ac73 [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>
17#include <sys/socket.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010018
19#include "../fio.h"
Jens Axboeed92ac02007-02-06 14:43:52 +010020
Jens Axboeb5af8292007-03-08 12:43:13 +010021struct netio_data {
22 int listenfd;
23 int send_to_net;
Jens Axboe9cce02e2007-06-22 15:42:21 +020024 int use_splice;
Jens Axboe414c2a32009-01-16 13:21:15 +010025 int net_protocol;
Jens Axboe9cce02e2007-06-22 15:42:21 +020026 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010027 char host[64];
28 struct sockaddr_in addr;
29};
Jens Axboeed92ac02007-02-06 14:43:52 +010030
Jens Axboe664fb3b2009-01-19 13:26:36 +010031struct udp_close_msg {
32 uint32_t magic;
33 uint32_t cmd;
34};
35
36enum {
37 FIO_LINK_CLOSE = 0x89,
38 FIO_LINK_CLOSE_MAGIC = 0x6c696e6b,
39};
40
Jens Axboe371d4562009-01-19 10:17:06 +010041/*
42 * Return -1 for error and 'nr events' for a positive number
43 * of events
44 */
45static int poll_wait(struct thread_data *td, int fd, short events)
46{
47 struct pollfd pfd;
48 int ret;
49
50 while (!td->terminate) {
51 pfd.fd = fd;
52 pfd.events = events;
53 ret = poll(&pfd, 1, -1);
54 if (ret < 0) {
55 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +010056 break;
Jens Axboe371d4562009-01-19 10:17:06 +010057
58 td_verror(td, errno, "poll");
59 return -1;
60 } else if (!ret)
61 continue;
62
63 break;
64 }
65
66 if (pfd.revents & events)
67 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +010068
69 return -1;
70}
71
Jens Axboeed92ac02007-02-06 14:43:52 +010072static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
73{
Jens Axboeb5af8292007-03-08 12:43:13 +010074 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010075
Jens Axboe7a6499d2007-02-07 09:35:29 +010076 /*
77 * Make sure we don't see spurious reads to a receiver, and vice versa
78 */
Jens Axboeb5af8292007-03-08 12:43:13 +010079 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
80 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010081 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010082 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010083 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010084
Jens Axboef85ac252008-03-01 18:09:49 +010085 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010086}
87
Jens Axboe5921e802008-05-30 15:02:38 +020088#ifdef FIO_HAVE_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +020089static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +020090{
Jens Axboe9cce02e2007-06-22 15:42:21 +020091 int bytes = 0;
92
93 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +020094 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +020095
96 if (ret < 0) {
97 if (!bytes)
98 bytes = ret;
99
100 break;
101 } else if (!ret)
102 break;
103
104 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200105 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200106 }
107
108 return bytes;
109}
110
111/*
Jens Axboecd963e12007-06-24 21:41:46 +0200112 * Receive bytes from a socket and fill them into the internal pipe
113 */
114static int splice_in(struct thread_data *td, struct io_u *io_u)
115{
116 struct netio_data *nd = td->io_ops->data;
117
118 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
119}
120
121/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200122 * Transmit 'len' bytes from the internal pipe
123 */
124static int splice_out(struct thread_data *td, struct io_u *io_u,
125 unsigned int len)
126{
127 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200128
129 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
130}
131
132static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
133{
134 struct iovec iov = {
135 .iov_base = io_u->xfer_buf,
136 .iov_len = len,
137 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200138 int bytes = 0;
139
Jens Axboecd963e12007-06-24 21:41:46 +0200140 while (iov.iov_len) {
141 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200142
143 if (ret < 0) {
144 if (!bytes)
145 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200146 break;
147 } else if (!ret)
148 break;
149
Jens Axboecd963e12007-06-24 21:41:46 +0200150 iov.iov_len -= ret;
151 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200152 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200153 }
154
155 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200156
Jens Axboe9cce02e2007-06-22 15:42:21 +0200157}
158
159/*
160 * vmsplice() pipe to io_u buffer
161 */
162static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
163 unsigned int len)
164{
165 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200166
Jens Axboecd963e12007-06-24 21:41:46 +0200167 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200168}
169
170/*
171 * vmsplice() io_u to pipe
172 */
173static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
174{
175 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200176
Jens Axboecd963e12007-06-24 21:41:46 +0200177 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200178}
179
Jens Axboecd963e12007-06-24 21:41:46 +0200180/*
181 * splice receive - transfer socket data into a pipe using splice, then map
182 * that pipe data into the io_u using vmsplice.
183 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200184static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
185{
186 int ret;
187
188 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200189 if (ret > 0)
190 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200191
Jens Axboecd963e12007-06-24 21:41:46 +0200192 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200193}
194
Jens Axboecd963e12007-06-24 21:41:46 +0200195/*
196 * splice transmit - map data from the io_u into a pipe by using vmsplice,
197 * then transfer that pipe to a socket using splice.
198 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200199static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
200{
201 int ret;
202
203 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200204 if (ret > 0)
205 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200206
Jens Axboecd963e12007-06-24 21:41:46 +0200207 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200208}
Jens Axboe5921e802008-05-30 15:02:38 +0200209#else
210static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
211{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200212 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200213 return -1;
214}
215
216static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
217{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200218 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200219 return -1;
220}
221#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200222
223static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
224{
Jens Axboe414c2a32009-01-16 13:21:15 +0100225 struct netio_data *nd = td->io_ops->data;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400226 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100227
Jens Axboe664fb3b2009-01-19 13:26:36 +0100228 do {
229 if (nd->net_protocol == IPPROTO_UDP) {
Jens Axboe62b38922009-05-11 10:37:33 +0200230 struct sockaddr *to = (struct sockaddr *) &nd->addr;
231
Jens Axboe664fb3b2009-01-19 13:26:36 +0100232 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200233 io_u->xfer_buflen, flags, to,
234 sizeof(*to));
Jens Axboe664fb3b2009-01-19 13:26:36 +0100235 } else {
236 /*
237 * if we are going to write more, set MSG_MORE
238 */
Jens Axboe5921e802008-05-30 15:02:38 +0200239#ifdef MSG_MORE
Jens Axboe664fb3b2009-01-19 13:26:36 +0100240 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
241 td->o.size)
242 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200243#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100244 ret = send(io_u->file->fd, io_u->xfer_buf,
245 io_u->xfer_buflen, flags);
246 }
247 if (ret > 0)
248 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200249
Jens Axboe664fb3b2009-01-19 13:26:36 +0100250 ret = poll_wait(td, io_u->file->fd, POLLOUT);
251 if (ret <= 0)
252 break;
253
Jens Axboe8e239ca2010-08-11 10:29:12 -0400254 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100255 } while (1);
256
257 return ret;
258}
259
260static int is_udp_close(struct io_u *io_u, int len)
261{
262 struct udp_close_msg *msg;
263
264 if (len != sizeof(struct udp_close_msg))
265 return 0;
266
267 msg = io_u->xfer_buf;
268 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
269 return 0;
270 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
271 return 0;
272
273 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200274}
275
Jens Axboe414c2a32009-01-16 13:21:15 +0100276static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200277{
Jens Axboe414c2a32009-01-16 13:21:15 +0100278 struct netio_data *nd = td->io_ops->data;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400279 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100280
Jens Axboe664fb3b2009-01-19 13:26:36 +0100281 do {
282 if (nd->net_protocol == IPPROTO_UDP) {
283 socklen_t len = sizeof(nd->addr);
Jens Axboe62b38922009-05-11 10:37:33 +0200284 struct sockaddr *from = (struct sockaddr *) &nd->addr;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200285
Jens Axboe664fb3b2009-01-19 13:26:36 +0100286 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200287 io_u->xfer_buflen, flags, from, &len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100288 if (is_udp_close(io_u, ret)) {
289 td->done = 1;
290 return 0;
291 }
292 } else {
293 ret = recv(io_u->file->fd, io_u->xfer_buf,
294 io_u->xfer_buflen, flags);
295 }
296 if (ret > 0)
297 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100298
Jens Axboe664fb3b2009-01-19 13:26:36 +0100299 ret = poll_wait(td, io_u->file->fd, POLLIN);
300 if (ret <= 0)
301 break;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400302 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100303 flags |= MSG_WAITALL;
304 } while (1);
305
306 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200307}
308
Jens Axboeed92ac02007-02-06 14:43:52 +0100309static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
310{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200311 struct netio_data *nd = td->io_ops->data;
312 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100313
Jens Axboe7101d9c2007-09-12 13:12:39 +0200314 fio_ro_check(td, io_u);
315
Jens Axboe7a6499d2007-02-07 09:35:29 +0100316 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100317 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200318 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200319 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100320 ret = fio_netio_splice_out(td, io_u);
321 } else if (io_u->ddir == DDIR_READ) {
322 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
323 ret = fio_netio_recv(td, io_u);
324 else
325 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100326 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100327 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100328
Jens Axboecec6b552007-02-06 20:15:38 +0100329 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100330 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100331 io_u->resid = io_u->xfer_buflen - ret;
332 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100333 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100334 } else {
335 int err = errno;
336
337 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
338 return FIO_Q_BUSY;
339
340 io_u->error = err;
341 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100342 }
343
Jens Axboe36167d82007-02-18 05:41:31 +0100344 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100345 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100346
Jens Axboe36167d82007-02-18 05:41:31 +0100347 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100348}
349
Jens Axboeb5af8292007-03-08 12:43:13 +0100350static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100351{
Jens Axboeb5af8292007-03-08 12:43:13 +0100352 struct netio_data *nd = td->io_ops->data;
Jens Axboe414c2a32009-01-16 13:21:15 +0100353 int type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100354
Jens Axboe414c2a32009-01-16 13:21:15 +0100355 if (nd->net_protocol == IPPROTO_TCP)
356 type = SOCK_STREAM;
357 else
358 type = SOCK_DGRAM;
359
360 f->fd = socket(AF_INET, type, nd->net_protocol);
Jens Axboeb5af8292007-03-08 12:43:13 +0100361 if (f->fd < 0) {
362 td_verror(td, errno, "socket");
363 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100364 }
365
Jens Axboe414c2a32009-01-16 13:21:15 +0100366 if (nd->net_protocol == IPPROTO_UDP)
367 return 0;
368
Jens Axboeb5af8292007-03-08 12:43:13 +0100369 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
370 td_verror(td, errno, "connect");
371 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100372 }
373
374 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100375}
376
Jens Axboeb5af8292007-03-08 12:43:13 +0100377static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100378{
Jens Axboeb5af8292007-03-08 12:43:13 +0100379 struct netio_data *nd = td->io_ops->data;
380 socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100381
Jens Axboe414c2a32009-01-16 13:21:15 +0100382 if (nd->net_protocol == IPPROTO_UDP) {
383 f->fd = nd->listenfd;
384 return 0;
385 }
386
Jens Axboe6d861442007-03-15 09:22:23 +0100387 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100388
Jens Axboe371d4562009-01-19 10:17:06 +0100389 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
390 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100391
Jens Axboe371d4562009-01-19 10:17:06 +0100392 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
393 if (f->fd < 0) {
394 td_verror(td, errno, "accept");
395 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100396 }
397
398 return 0;
399}
400
Jens Axboeb5af8292007-03-08 12:43:13 +0100401static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100402{
Jens Axboeb5af8292007-03-08 12:43:13 +0100403 if (td_read(td))
404 return fio_netio_accept(td, f);
405 else
406 return fio_netio_connect(td, f);
407}
408
Jens Axboe664fb3b2009-01-19 13:26:36 +0100409static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
410{
411 struct netio_data *nd = td->io_ops->data;
412 struct udp_close_msg msg;
Jens Axboe62b38922009-05-11 10:37:33 +0200413 struct sockaddr *to = (struct sockaddr *) &nd->addr;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100414 int ret;
415
416 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
417 msg.cmd = htonl(FIO_LINK_CLOSE);
418
Jens Axboe62b38922009-05-11 10:37:33 +0200419 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
Jens Axboe664fb3b2009-01-19 13:26:36 +0100420 sizeof(nd->addr));
421 if (ret < 0)
422 td_verror(td, errno, "sendto udp link close");
423}
424
425static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
426{
427 struct netio_data *nd = td->io_ops->data;
428
429 /*
430 * If this is an UDP connection, notify the receiver that we are
431 * closing down the link
432 */
433 if (nd->net_protocol == IPPROTO_UDP)
434 fio_netio_udp_close(td, f);
435
436 return generic_close_file(td, f);
437}
438
Jens Axboeb5af8292007-03-08 12:43:13 +0100439static int fio_netio_setup_connect(struct thread_data *td, const char *host,
440 unsigned short port)
441{
442 struct netio_data *nd = td->io_ops->data;
443
444 nd->addr.sin_family = AF_INET;
445 nd->addr.sin_port = htons(port);
446
447 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
448 struct hostent *hent;
449
450 hent = gethostbyname(host);
451 if (!hent) {
452 td_verror(td, errno, "gethostbyname");
453 return 1;
454 }
455
456 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
457 }
458
459 return 0;
460}
461
462static int fio_netio_setup_listen(struct thread_data *td, short port)
463{
464 struct netio_data *nd = td->io_ops->data;
Jens Axboe414c2a32009-01-16 13:21:15 +0100465 int fd, opt, type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100466
Jens Axboe414c2a32009-01-16 13:21:15 +0100467 if (nd->net_protocol == IPPROTO_TCP)
468 type = SOCK_STREAM;
469 else
470 type = SOCK_DGRAM;
471
472 fd = socket(AF_INET, type, nd->net_protocol);
Jens Axboeed92ac02007-02-06 14:43:52 +0100473 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100474 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100475 return 1;
476 }
477
478 opt = 1;
479 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100480 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100481 return 1;
482 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100483#ifdef SO_REUSEPORT
484 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100485 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100486 return 1;
487 }
488#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100489
Jens Axboeb5af8292007-03-08 12:43:13 +0100490 nd->addr.sin_family = AF_INET;
491 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
492 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100493
Jens Axboeb5af8292007-03-08 12:43:13 +0100494 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100495 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100496 return 1;
497 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100498 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100499 td_verror(td, errno, "listen");
Jens Axboeed92ac02007-02-06 14:43:52 +0100500 return 1;
501 }
502
Jens Axboeb5af8292007-03-08 12:43:13 +0100503 nd->listenfd = fd;
504 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100505}
506
Jens Axboe9bec88e2007-03-02 08:55:48 +0100507static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100508{
Jens Axboeb5af8292007-03-08 12:43:13 +0100509 struct netio_data *nd = td->io_ops->data;
Jens Axboe443662e2008-05-30 13:29:03 +0200510 unsigned int port;
Jens Axboeb5af8292007-03-08 12:43:13 +0100511 char host[64], buf[128];
Jens Axboe414c2a32009-01-16 13:21:15 +0100512 char *sep, *portp, *modep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100513 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100514
Jens Axboe413dd452007-02-23 09:26:09 +0100515 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100516 log_err("fio: network connections must be read OR write\n");
517 return 1;
518 }
Jens Axboe16d55aa2007-05-22 09:21:37 +0200519 if (td_random(td)) {
520 log_err("fio: network IO can't be random\n");
521 return 1;
522 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100523
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100524 strcpy(buf, td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100525
Jens Axboe9f9214f2007-03-13 14:02:16 +0100526 sep = strchr(buf, '/');
Jens Axboe443662e2008-05-30 13:29:03 +0200527 if (!sep)
528 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100529
530 *sep = '\0';
531 sep++;
532 strcpy(host, buf);
Jens Axboe443662e2008-05-30 13:29:03 +0200533 if (!strlen(host))
534 goto bad_host;
535
Jens Axboe414c2a32009-01-16 13:21:15 +0100536 modep = NULL;
537 portp = sep;
538 sep = strchr(portp, '/');
539 if (sep) {
540 *sep = '\0';
541 modep = sep + 1;
542 }
543
544 port = strtol(portp, NULL, 10);
Jens Axboe443662e2008-05-30 13:29:03 +0200545 if (!port || port > 65535)
546 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100547
Jens Axboe414c2a32009-01-16 13:21:15 +0100548 if (modep) {
Jens Axboe3f8fc5a2009-01-16 13:22:26 +0100549 if (!strncmp("tcp", modep, strlen(modep)) ||
550 !strncmp("TCP", modep, strlen(modep)))
Jens Axboe414c2a32009-01-16 13:21:15 +0100551 nd->net_protocol = IPPROTO_TCP;
Jens Axboe3f8fc5a2009-01-16 13:22:26 +0100552 else if (!strncmp("udp", modep, strlen(modep)) ||
553 !strncmp("UDP", modep, strlen(modep)))
Jens Axboe414c2a32009-01-16 13:21:15 +0100554 nd->net_protocol = IPPROTO_UDP;
555 else
556 goto bad_host;
557 } else
558 nd->net_protocol = IPPROTO_TCP;
559
Jens Axboe413dd452007-02-23 09:26:09 +0100560 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100561 nd->send_to_net = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100562 ret = fio_netio_setup_listen(td, port);
563 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100564 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100565 ret = fio_netio_setup_connect(td, host, port);
566 }
567
Jens Axboe7bb48f82007-03-27 15:30:28 +0200568 return ret;
Jens Axboe443662e2008-05-30 13:29:03 +0200569bad_host:
Jens Axboe414c2a32009-01-16 13:21:15 +0100570 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
Jens Axboe443662e2008-05-30 13:29:03 +0200571 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100572}
573
Jens Axboeb5af8292007-03-08 12:43:13 +0100574static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100575{
Jens Axboeb5af8292007-03-08 12:43:13 +0100576 struct netio_data *nd = td->io_ops->data;
577
578 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +0200579 if (nd->listenfd != -1)
580 close(nd->listenfd);
581 if (nd->pipes[0] != -1)
582 close(nd->pipes[0]);
583 if (nd->pipes[1] != -1)
584 close(nd->pipes[1]);
585
Jens Axboeb5af8292007-03-08 12:43:13 +0100586 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +0100587 }
588}
589
590static int fio_netio_setup(struct thread_data *td)
591{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200592 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100593
Jens Axboe7bb48f82007-03-27 15:30:28 +0200594 if (!td->io_ops->data) {
595 nd = malloc(sizeof(*nd));;
596
597 memset(nd, 0, sizeof(*nd));
598 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +0200599 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200600 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200601 }
602
Jens Axboe9bec88e2007-03-02 08:55:48 +0100603 return 0;
604}
605
Jens Axboe5921e802008-05-30 15:02:38 +0200606#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200607static int fio_netio_setup_splice(struct thread_data *td)
608{
609 struct netio_data *nd;
610
611 fio_netio_setup(td);
612
613 nd = td->io_ops->data;
614 if (nd) {
615 if (pipe(nd->pipes) < 0)
616 return 1;
617
618 nd->use_splice = 1;
619 return 0;
620 }
621
622 return 1;
623}
624
Jens Axboe5921e802008-05-30 15:02:38 +0200625static struct ioengine_ops ioengine_splice = {
626 .name = "netsplice",
627 .version = FIO_IOOPS_VERSION,
628 .prep = fio_netio_prep,
629 .queue = fio_netio_queue,
630 .setup = fio_netio_setup_splice,
631 .init = fio_netio_init,
632 .cleanup = fio_netio_cleanup,
633 .open_file = fio_netio_open_file,
634 .close_file = generic_close_file,
635 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Jens Axboe9c0d2242009-07-01 12:26:28 +0200636 FIO_SIGQUIT | FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +0200637};
638#endif
639
Jens Axboe9cce02e2007-06-22 15:42:21 +0200640static struct ioengine_ops ioengine_rw = {
Jens Axboeed92ac02007-02-06 14:43:52 +0100641 .name = "net",
642 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100643 .prep = fio_netio_prep,
644 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100645 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100646 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100647 .cleanup = fio_netio_cleanup,
648 .open_file = fio_netio_open_file,
Jens Axboe664fb3b2009-01-19 13:26:36 +0100649 .close_file = fio_netio_close_file,
Jens Axboead830ed2008-02-18 21:11:24 +0100650 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Jens Axboe9c0d2242009-07-01 12:26:28 +0200651 FIO_SIGQUIT | FIO_PIPEIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100652};
653
654static void fio_init fio_netio_register(void)
655{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200656 register_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200657#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200658 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200659#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100660}
661
662static void fio_exit fio_netio_unregister(void)
663{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200664 unregister_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200665#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200666 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200667#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100668}