blob: 8dbc2a8560bfbcfb4fb2735b08c86c025d0e5a14 [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
31static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
32{
Jens Axboeb5af8292007-03-08 12:43:13 +010033 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010034
Jens Axboe7a6499d2007-02-07 09:35:29 +010035 /*
36 * Make sure we don't see spurious reads to a receiver, and vice versa
37 */
Jens Axboeb5af8292007-03-08 12:43:13 +010038 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
39 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010040 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010041 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010042 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010043
Jens Axboef85ac252008-03-01 18:09:49 +010044 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010045}
46
Jens Axboe5921e802008-05-30 15:02:38 +020047#ifdef FIO_HAVE_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +020048static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +020049{
Jens Axboe9cce02e2007-06-22 15:42:21 +020050 int bytes = 0;
51
52 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +020053 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +020054
55 if (ret < 0) {
56 if (!bytes)
57 bytes = ret;
58
59 break;
60 } else if (!ret)
61 break;
62
63 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +020064 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +020065 }
66
67 return bytes;
68}
69
70/*
Jens Axboecd963e12007-06-24 21:41:46 +020071 * Receive bytes from a socket and fill them into the internal pipe
72 */
73static int splice_in(struct thread_data *td, struct io_u *io_u)
74{
75 struct netio_data *nd = td->io_ops->data;
76
77 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
78}
79
80/*
Jens Axboe9cce02e2007-06-22 15:42:21 +020081 * Transmit 'len' bytes from the internal pipe
82 */
83static int splice_out(struct thread_data *td, struct io_u *io_u,
84 unsigned int len)
85{
86 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +020087
88 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
89}
90
91static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
92{
93 struct iovec iov = {
94 .iov_base = io_u->xfer_buf,
95 .iov_len = len,
96 };
Jens Axboe9cce02e2007-06-22 15:42:21 +020097 int bytes = 0;
98
Jens Axboecd963e12007-06-24 21:41:46 +020099 while (iov.iov_len) {
100 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200101
102 if (ret < 0) {
103 if (!bytes)
104 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200105 break;
106 } else if (!ret)
107 break;
108
Jens Axboecd963e12007-06-24 21:41:46 +0200109 iov.iov_len -= ret;
110 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200111 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200112 }
113
114 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200115
Jens Axboe9cce02e2007-06-22 15:42:21 +0200116}
117
118/*
119 * vmsplice() pipe to io_u buffer
120 */
121static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
122 unsigned int len)
123{
124 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200125
Jens Axboecd963e12007-06-24 21:41:46 +0200126 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200127}
128
129/*
130 * vmsplice() io_u to pipe
131 */
132static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
133{
134 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200135
Jens Axboecd963e12007-06-24 21:41:46 +0200136 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200137}
138
Jens Axboecd963e12007-06-24 21:41:46 +0200139/*
140 * splice receive - transfer socket data into a pipe using splice, then map
141 * that pipe data into the io_u using vmsplice.
142 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200143static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
144{
145 int ret;
146
147 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200148 if (ret > 0)
149 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200150
Jens Axboecd963e12007-06-24 21:41:46 +0200151 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200152}
153
Jens Axboecd963e12007-06-24 21:41:46 +0200154/*
155 * splice transmit - map data from the io_u into a pipe by using vmsplice,
156 * then transfer that pipe to a socket using splice.
157 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200158static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
159{
160 int ret;
161
162 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200163 if (ret > 0)
164 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200165
Jens Axboecd963e12007-06-24 21:41:46 +0200166 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200167}
Jens Axboe5921e802008-05-30 15:02:38 +0200168#else
169static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
170{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200171 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200172 return -1;
173}
174
175static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
176{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200177 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200178 return -1;
179}
180#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200181
182static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
183{
Jens Axboe414c2a32009-01-16 13:21:15 +0100184 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200185 int flags = 0;
186
187 /*
188 * if we are going to write more, set MSG_MORE
189 */
Jens Axboe5921e802008-05-30 15:02:38 +0200190#ifdef MSG_MORE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200191 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
192 flags = MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200193#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200194
Jens Axboe414c2a32009-01-16 13:21:15 +0100195 if (nd->net_protocol == IPPROTO_UDP) {
196 return sendto(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
197 0, &nd->addr, sizeof(nd->addr));
198 } else {
199 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
200 flags);
201 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200202}
203
Jens Axboe414c2a32009-01-16 13:21:15 +0100204static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200205{
Jens Axboe414c2a32009-01-16 13:21:15 +0100206 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200207 int flags = MSG_WAITALL;
208
Jens Axboe414c2a32009-01-16 13:21:15 +0100209 if (nd->net_protocol == IPPROTO_UDP) {
210 socklen_t len = sizeof(nd->addr);
211
212 return recvfrom(io_u->file->fd, io_u->xfer_buf,
213 io_u->xfer_buflen, 0, &nd->addr, &len);
214 } else {
215 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
216 flags);
217 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200218}
219
Jens Axboeed92ac02007-02-06 14:43:52 +0100220static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
221{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200222 struct netio_data *nd = td->io_ops->data;
223 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100224
Jens Axboe7101d9c2007-09-12 13:12:39 +0200225 fio_ro_check(td, io_u);
226
Jens Axboe7a6499d2007-02-07 09:35:29 +0100227 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100228 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200229 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200230 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100231 ret = fio_netio_splice_out(td, io_u);
232 } else if (io_u->ddir == DDIR_READ) {
233 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
234 ret = fio_netio_recv(td, io_u);
235 else
236 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100237 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100238 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100239
Jens Axboecec6b552007-02-06 20:15:38 +0100240 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100241 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100242 io_u->resid = io_u->xfer_buflen - ret;
243 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100244 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100245 } else {
246 int err = errno;
247
248 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
249 return FIO_Q_BUSY;
250
251 io_u->error = err;
252 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100253 }
254
Jens Axboe36167d82007-02-18 05:41:31 +0100255 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100256 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100257
Jens Axboe36167d82007-02-18 05:41:31 +0100258 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100259}
260
Jens Axboeb5af8292007-03-08 12:43:13 +0100261static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100262{
Jens Axboeb5af8292007-03-08 12:43:13 +0100263 struct netio_data *nd = td->io_ops->data;
Jens Axboe414c2a32009-01-16 13:21:15 +0100264 int type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100265
Jens Axboe414c2a32009-01-16 13:21:15 +0100266 if (nd->net_protocol == IPPROTO_TCP)
267 type = SOCK_STREAM;
268 else
269 type = SOCK_DGRAM;
270
271 f->fd = socket(AF_INET, type, nd->net_protocol);
Jens Axboeb5af8292007-03-08 12:43:13 +0100272 if (f->fd < 0) {
273 td_verror(td, errno, "socket");
274 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100275 }
276
Jens Axboe414c2a32009-01-16 13:21:15 +0100277 if (nd->net_protocol == IPPROTO_UDP)
278 return 0;
279
Jens Axboeb5af8292007-03-08 12:43:13 +0100280 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
281 td_verror(td, errno, "connect");
282 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100283 }
284
285 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100286}
287
Jens Axboeb5af8292007-03-08 12:43:13 +0100288static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100289{
Jens Axboeb5af8292007-03-08 12:43:13 +0100290 struct netio_data *nd = td->io_ops->data;
291 socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100292 struct pollfd pfd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100293 int ret;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100294
Jens Axboe414c2a32009-01-16 13:21:15 +0100295 if (nd->net_protocol == IPPROTO_UDP) {
296 f->fd = nd->listenfd;
297 return 0;
298 }
299
Jens Axboe6d861442007-03-15 09:22:23 +0100300 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100301
302 /*
303 * Accept loop. poll for incoming events, accept them. Repeat until we
304 * have all connections.
305 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100306 while (!td->terminate) {
307 pfd.fd = nd->listenfd;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100308 pfd.events = POLLIN;
309
310 ret = poll(&pfd, 1, -1);
311 if (ret < 0) {
312 if (errno == EINTR)
313 continue;
314
Jens Axboee1161c32007-02-22 19:36:48 +0100315 td_verror(td, errno, "poll");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100316 break;
317 } else if (!ret)
318 continue;
319
Jens Axboe0c094422007-02-11 04:44:02 +0100320 /*
321 * should be impossible
322 */
323 if (!(pfd.revents & POLLIN))
324 continue;
325
Jens Axboeb5af8292007-03-08 12:43:13 +0100326 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
327 if (f->fd < 0) {
328 td_verror(td, errno, "accept");
329 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100330 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100331 break;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100332 }
333
334 return 0;
335}
336
Jens Axboeb5af8292007-03-08 12:43:13 +0100337static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100338{
Jens Axboeb5af8292007-03-08 12:43:13 +0100339 if (td_read(td))
340 return fio_netio_accept(td, f);
341 else
342 return fio_netio_connect(td, f);
343}
344
345static int fio_netio_setup_connect(struct thread_data *td, const char *host,
346 unsigned short port)
347{
348 struct netio_data *nd = td->io_ops->data;
349
350 nd->addr.sin_family = AF_INET;
351 nd->addr.sin_port = htons(port);
352
353 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
354 struct hostent *hent;
355
356 hent = gethostbyname(host);
357 if (!hent) {
358 td_verror(td, errno, "gethostbyname");
359 return 1;
360 }
361
362 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
363 }
364
365 return 0;
366}
367
368static int fio_netio_setup_listen(struct thread_data *td, short port)
369{
370 struct netio_data *nd = td->io_ops->data;
Jens Axboe414c2a32009-01-16 13:21:15 +0100371 int fd, opt, type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100372
Jens Axboe414c2a32009-01-16 13:21:15 +0100373 if (nd->net_protocol == IPPROTO_TCP)
374 type = SOCK_STREAM;
375 else
376 type = SOCK_DGRAM;
377
378 fd = socket(AF_INET, type, nd->net_protocol);
Jens Axboeed92ac02007-02-06 14:43:52 +0100379 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100380 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100381 return 1;
382 }
383
384 opt = 1;
385 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100386 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100387 return 1;
388 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100389#ifdef SO_REUSEPORT
390 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100391 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100392 return 1;
393 }
394#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100395
Jens Axboeb5af8292007-03-08 12:43:13 +0100396 nd->addr.sin_family = AF_INET;
397 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
398 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100399
Jens Axboeb5af8292007-03-08 12:43:13 +0100400 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100401 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100402 return 1;
403 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100404 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100405 td_verror(td, errno, "listen");
Jens Axboeed92ac02007-02-06 14:43:52 +0100406 return 1;
407 }
408
Jens Axboeb5af8292007-03-08 12:43:13 +0100409 nd->listenfd = fd;
410 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100411}
412
Jens Axboe9bec88e2007-03-02 08:55:48 +0100413static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100414{
Jens Axboeb5af8292007-03-08 12:43:13 +0100415 struct netio_data *nd = td->io_ops->data;
Jens Axboe443662e2008-05-30 13:29:03 +0200416 unsigned int port;
Jens Axboeb5af8292007-03-08 12:43:13 +0100417 char host[64], buf[128];
Jens Axboe414c2a32009-01-16 13:21:15 +0100418 char *sep, *portp, *modep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100419 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100420
Jens Axboe413dd452007-02-23 09:26:09 +0100421 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100422 log_err("fio: network connections must be read OR write\n");
423 return 1;
424 }
Jens Axboe16d55aa2007-05-22 09:21:37 +0200425 if (td_random(td)) {
426 log_err("fio: network IO can't be random\n");
427 return 1;
428 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100429
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100430 strcpy(buf, td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100431
Jens Axboe9f9214f2007-03-13 14:02:16 +0100432 sep = strchr(buf, '/');
Jens Axboe443662e2008-05-30 13:29:03 +0200433 if (!sep)
434 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100435
436 *sep = '\0';
437 sep++;
438 strcpy(host, buf);
Jens Axboe443662e2008-05-30 13:29:03 +0200439 if (!strlen(host))
440 goto bad_host;
441
Jens Axboe414c2a32009-01-16 13:21:15 +0100442 modep = NULL;
443 portp = sep;
444 sep = strchr(portp, '/');
445 if (sep) {
446 *sep = '\0';
447 modep = sep + 1;
448 }
449
450 port = strtol(portp, NULL, 10);
Jens Axboe443662e2008-05-30 13:29:03 +0200451 if (!port || port > 65535)
452 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100453
Jens Axboe414c2a32009-01-16 13:21:15 +0100454 if (modep) {
Jens Axboe3f8fc5a2009-01-16 13:22:26 +0100455 if (!strncmp("tcp", modep, strlen(modep)) ||
456 !strncmp("TCP", modep, strlen(modep)))
Jens Axboe414c2a32009-01-16 13:21:15 +0100457 nd->net_protocol = IPPROTO_TCP;
Jens Axboe3f8fc5a2009-01-16 13:22:26 +0100458 else if (!strncmp("udp", modep, strlen(modep)) ||
459 !strncmp("UDP", modep, strlen(modep)))
Jens Axboe414c2a32009-01-16 13:21:15 +0100460 nd->net_protocol = IPPROTO_UDP;
461 else
462 goto bad_host;
463 } else
464 nd->net_protocol = IPPROTO_TCP;
465
Jens Axboe413dd452007-02-23 09:26:09 +0100466 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100467 nd->send_to_net = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100468 ret = fio_netio_setup_listen(td, port);
469 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100470 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100471 ret = fio_netio_setup_connect(td, host, port);
472 }
473
Jens Axboe7bb48f82007-03-27 15:30:28 +0200474 return ret;
Jens Axboe443662e2008-05-30 13:29:03 +0200475bad_host:
Jens Axboe414c2a32009-01-16 13:21:15 +0100476 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
Jens Axboe443662e2008-05-30 13:29:03 +0200477 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100478}
479
Jens Axboeb5af8292007-03-08 12:43:13 +0100480static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100481{
Jens Axboeb5af8292007-03-08 12:43:13 +0100482 struct netio_data *nd = td->io_ops->data;
483
484 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +0200485 if (nd->listenfd != -1)
486 close(nd->listenfd);
487 if (nd->pipes[0] != -1)
488 close(nd->pipes[0]);
489 if (nd->pipes[1] != -1)
490 close(nd->pipes[1]);
491
Jens Axboeb5af8292007-03-08 12:43:13 +0100492 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +0100493 }
494}
495
496static int fio_netio_setup(struct thread_data *td)
497{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200498 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100499
Jens Axboe7bb48f82007-03-27 15:30:28 +0200500 if (!td->io_ops->data) {
501 nd = malloc(sizeof(*nd));;
502
503 memset(nd, 0, sizeof(*nd));
504 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +0200505 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200506 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200507 }
508
Jens Axboe9bec88e2007-03-02 08:55:48 +0100509 return 0;
510}
511
Jens Axboe5921e802008-05-30 15:02:38 +0200512#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200513static int fio_netio_setup_splice(struct thread_data *td)
514{
515 struct netio_data *nd;
516
517 fio_netio_setup(td);
518
519 nd = td->io_ops->data;
520 if (nd) {
521 if (pipe(nd->pipes) < 0)
522 return 1;
523
524 nd->use_splice = 1;
525 return 0;
526 }
527
528 return 1;
529}
530
Jens Axboe5921e802008-05-30 15:02:38 +0200531static struct ioengine_ops ioengine_splice = {
532 .name = "netsplice",
533 .version = FIO_IOOPS_VERSION,
534 .prep = fio_netio_prep,
535 .queue = fio_netio_queue,
536 .setup = fio_netio_setup_splice,
537 .init = fio_netio_init,
538 .cleanup = fio_netio_cleanup,
539 .open_file = fio_netio_open_file,
540 .close_file = generic_close_file,
541 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
542 FIO_SIGQUIT,
543};
544#endif
545
Jens Axboe9cce02e2007-06-22 15:42:21 +0200546static struct ioengine_ops ioengine_rw = {
Jens Axboeed92ac02007-02-06 14:43:52 +0100547 .name = "net",
548 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100549 .prep = fio_netio_prep,
550 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100551 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100552 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100553 .cleanup = fio_netio_cleanup,
554 .open_file = fio_netio_open_file,
555 .close_file = generic_close_file,
Jens Axboead830ed2008-02-18 21:11:24 +0100556 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
557 FIO_SIGQUIT,
Jens Axboeed92ac02007-02-06 14:43:52 +0100558};
559
560static void fio_init fio_netio_register(void)
561{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200562 register_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200563#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200564 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200565#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100566}
567
568static void fio_exit fio_netio_unregister(void)
569{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200570 unregister_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200571#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200572 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200573#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100574}