blob: fd01089caa8cbc6eda75ec676225447dc4293ac8 [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 Axboe371d4562009-01-19 10:17:06 +010031/*
32 * Return -1 for error and 'nr events' for a positive number
33 * of events
34 */
35static int poll_wait(struct thread_data *td, int fd, short events)
36{
37 struct pollfd pfd;
38 int ret;
39
40 while (!td->terminate) {
41 pfd.fd = fd;
42 pfd.events = events;
43 ret = poll(&pfd, 1, -1);
44 if (ret < 0) {
45 if (errno == EINTR)
46 continue;
47
48 td_verror(td, errno, "poll");
49 return -1;
50 } else if (!ret)
51 continue;
52
53 break;
54 }
55
56 if (pfd.revents & events)
57 return 1;
58 else if (td->terminate)
59 return 1;
60
61 return -1;
62}
63
Jens Axboeed92ac02007-02-06 14:43:52 +010064static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
65{
Jens Axboeb5af8292007-03-08 12:43:13 +010066 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010067
Jens Axboe7a6499d2007-02-07 09:35:29 +010068 /*
69 * Make sure we don't see spurious reads to a receiver, and vice versa
70 */
Jens Axboeb5af8292007-03-08 12:43:13 +010071 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
72 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010073 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010074 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010075 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010076
Jens Axboef85ac252008-03-01 18:09:49 +010077 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010078}
79
Jens Axboe5921e802008-05-30 15:02:38 +020080#ifdef FIO_HAVE_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +020081static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +020082{
Jens Axboe9cce02e2007-06-22 15:42:21 +020083 int bytes = 0;
84
85 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +020086 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +020087
88 if (ret < 0) {
89 if (!bytes)
90 bytes = ret;
91
92 break;
93 } else if (!ret)
94 break;
95
96 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +020097 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +020098 }
99
100 return bytes;
101}
102
103/*
Jens Axboecd963e12007-06-24 21:41:46 +0200104 * Receive bytes from a socket and fill them into the internal pipe
105 */
106static int splice_in(struct thread_data *td, struct io_u *io_u)
107{
108 struct netio_data *nd = td->io_ops->data;
109
110 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
111}
112
113/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200114 * Transmit 'len' bytes from the internal pipe
115 */
116static int splice_out(struct thread_data *td, struct io_u *io_u,
117 unsigned int len)
118{
119 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200120
121 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
122}
123
124static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
125{
126 struct iovec iov = {
127 .iov_base = io_u->xfer_buf,
128 .iov_len = len,
129 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200130 int bytes = 0;
131
Jens Axboecd963e12007-06-24 21:41:46 +0200132 while (iov.iov_len) {
133 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200134
135 if (ret < 0) {
136 if (!bytes)
137 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200138 break;
139 } else if (!ret)
140 break;
141
Jens Axboecd963e12007-06-24 21:41:46 +0200142 iov.iov_len -= ret;
143 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200144 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200145 }
146
147 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200148
Jens Axboe9cce02e2007-06-22 15:42:21 +0200149}
150
151/*
152 * vmsplice() pipe to io_u buffer
153 */
154static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
155 unsigned int len)
156{
157 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200158
Jens Axboecd963e12007-06-24 21:41:46 +0200159 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200160}
161
162/*
163 * vmsplice() io_u to pipe
164 */
165static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
166{
167 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200168
Jens Axboecd963e12007-06-24 21:41:46 +0200169 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200170}
171
Jens Axboecd963e12007-06-24 21:41:46 +0200172/*
173 * splice receive - transfer socket data into a pipe using splice, then map
174 * that pipe data into the io_u using vmsplice.
175 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200176static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
177{
178 int ret;
179
180 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200181 if (ret > 0)
182 return vmsplice_io_u_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200183
Jens Axboecd963e12007-06-24 21:41:46 +0200184 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200185}
186
Jens Axboecd963e12007-06-24 21:41:46 +0200187/*
188 * splice transmit - map data from the io_u into a pipe by using vmsplice,
189 * then transfer that pipe to a socket using splice.
190 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200191static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
192{
193 int ret;
194
195 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200196 if (ret > 0)
197 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200198
Jens Axboecd963e12007-06-24 21:41:46 +0200199 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200200}
Jens Axboe5921e802008-05-30 15:02:38 +0200201#else
202static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
203{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200204 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200205 return -1;
206}
207
208static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
209{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200210 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200211 return -1;
212}
213#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200214
215static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
216{
Jens Axboe414c2a32009-01-16 13:21:15 +0100217 struct netio_data *nd = td->io_ops->data;
Jens Axboe371d4562009-01-19 10:17:06 +0100218 int ret, flags = 0;
219
220 ret = poll_wait(td, io_u->file->fd, POLLOUT);
221 if (ret <= 0)
222 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200223
224 /*
225 * if we are going to write more, set MSG_MORE
226 */
Jens Axboe5921e802008-05-30 15:02:38 +0200227#ifdef MSG_MORE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200228 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen < td->o.size)
229 flags = MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200230#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200231
Jens Axboe414c2a32009-01-16 13:21:15 +0100232 if (nd->net_protocol == IPPROTO_UDP) {
233 return sendto(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
Jens Axboe371d4562009-01-19 10:17:06 +0100234 flags, &nd->addr, sizeof(nd->addr));
Jens Axboe414c2a32009-01-16 13:21:15 +0100235 } else {
236 return send(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
237 flags);
238 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200239}
240
Jens Axboe414c2a32009-01-16 13:21:15 +0100241static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200242{
Jens Axboe414c2a32009-01-16 13:21:15 +0100243 struct netio_data *nd = td->io_ops->data;
Jens Axboe371d4562009-01-19 10:17:06 +0100244 int ret, flags = MSG_WAITALL;
245
246 ret = poll_wait(td, io_u->file->fd, POLLIN);
247 if (ret <= 0)
248 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200249
Jens Axboe414c2a32009-01-16 13:21:15 +0100250 if (nd->net_protocol == IPPROTO_UDP) {
251 socklen_t len = sizeof(nd->addr);
252
253 return recvfrom(io_u->file->fd, io_u->xfer_buf,
254 io_u->xfer_buflen, 0, &nd->addr, &len);
255 } else {
256 return recv(io_u->file->fd, io_u->xfer_buf, io_u->xfer_buflen,
257 flags);
258 }
Jens Axboe9cce02e2007-06-22 15:42:21 +0200259}
260
Jens Axboeed92ac02007-02-06 14:43:52 +0100261static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
262{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200263 struct netio_data *nd = td->io_ops->data;
264 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100265
Jens Axboe7101d9c2007-09-12 13:12:39 +0200266 fio_ro_check(td, io_u);
267
Jens Axboe7a6499d2007-02-07 09:35:29 +0100268 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100269 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200270 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200271 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100272 ret = fio_netio_splice_out(td, io_u);
273 } else if (io_u->ddir == DDIR_READ) {
274 if (!nd->use_splice || nd->net_protocol == IPPROTO_UDP)
275 ret = fio_netio_recv(td, io_u);
276 else
277 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100278 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100279 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100280
Jens Axboecec6b552007-02-06 20:15:38 +0100281 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100282 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100283 io_u->resid = io_u->xfer_buflen - ret;
284 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100285 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100286 } else {
287 int err = errno;
288
289 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
290 return FIO_Q_BUSY;
291
292 io_u->error = err;
293 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100294 }
295
Jens Axboe36167d82007-02-18 05:41:31 +0100296 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100297 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100298
Jens Axboe36167d82007-02-18 05:41:31 +0100299 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100300}
301
Jens Axboeb5af8292007-03-08 12:43:13 +0100302static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100303{
Jens Axboeb5af8292007-03-08 12:43:13 +0100304 struct netio_data *nd = td->io_ops->data;
Jens Axboe414c2a32009-01-16 13:21:15 +0100305 int type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100306
Jens Axboe414c2a32009-01-16 13:21:15 +0100307 if (nd->net_protocol == IPPROTO_TCP)
308 type = SOCK_STREAM;
309 else
310 type = SOCK_DGRAM;
311
312 f->fd = socket(AF_INET, type, nd->net_protocol);
Jens Axboeb5af8292007-03-08 12:43:13 +0100313 if (f->fd < 0) {
314 td_verror(td, errno, "socket");
315 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100316 }
317
Jens Axboe414c2a32009-01-16 13:21:15 +0100318 if (nd->net_protocol == IPPROTO_UDP)
319 return 0;
320
Jens Axboeb5af8292007-03-08 12:43:13 +0100321 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
322 td_verror(td, errno, "connect");
323 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100324 }
325
326 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100327}
328
Jens Axboeb5af8292007-03-08 12:43:13 +0100329static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100330{
Jens Axboeb5af8292007-03-08 12:43:13 +0100331 struct netio_data *nd = td->io_ops->data;
332 socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100333
Jens Axboe414c2a32009-01-16 13:21:15 +0100334 if (nd->net_protocol == IPPROTO_UDP) {
335 f->fd = nd->listenfd;
336 return 0;
337 }
338
Jens Axboe6d861442007-03-15 09:22:23 +0100339 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100340
Jens Axboe371d4562009-01-19 10:17:06 +0100341 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
342 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100343
Jens Axboe371d4562009-01-19 10:17:06 +0100344 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
345 if (f->fd < 0) {
346 td_verror(td, errno, "accept");
347 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100348 }
349
350 return 0;
351}
352
Jens Axboeb5af8292007-03-08 12:43:13 +0100353static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100354{
Jens Axboeb5af8292007-03-08 12:43:13 +0100355 if (td_read(td))
356 return fio_netio_accept(td, f);
357 else
358 return fio_netio_connect(td, f);
359}
360
361static int fio_netio_setup_connect(struct thread_data *td, const char *host,
362 unsigned short port)
363{
364 struct netio_data *nd = td->io_ops->data;
365
366 nd->addr.sin_family = AF_INET;
367 nd->addr.sin_port = htons(port);
368
369 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
370 struct hostent *hent;
371
372 hent = gethostbyname(host);
373 if (!hent) {
374 td_verror(td, errno, "gethostbyname");
375 return 1;
376 }
377
378 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
379 }
380
381 return 0;
382}
383
384static int fio_netio_setup_listen(struct thread_data *td, short port)
385{
386 struct netio_data *nd = td->io_ops->data;
Jens Axboe414c2a32009-01-16 13:21:15 +0100387 int fd, opt, type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100388
Jens Axboe414c2a32009-01-16 13:21:15 +0100389 if (nd->net_protocol == IPPROTO_TCP)
390 type = SOCK_STREAM;
391 else
392 type = SOCK_DGRAM;
393
394 fd = socket(AF_INET, type, nd->net_protocol);
Jens Axboeed92ac02007-02-06 14:43:52 +0100395 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100396 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100397 return 1;
398 }
399
400 opt = 1;
401 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100402 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100403 return 1;
404 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100405#ifdef SO_REUSEPORT
406 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100407 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100408 return 1;
409 }
410#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100411
Jens Axboeb5af8292007-03-08 12:43:13 +0100412 nd->addr.sin_family = AF_INET;
413 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
414 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100415
Jens Axboeb5af8292007-03-08 12:43:13 +0100416 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100417 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100418 return 1;
419 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100420 if (nd->net_protocol == IPPROTO_TCP && listen(fd, 1) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100421 td_verror(td, errno, "listen");
Jens Axboeed92ac02007-02-06 14:43:52 +0100422 return 1;
423 }
424
Jens Axboeb5af8292007-03-08 12:43:13 +0100425 nd->listenfd = fd;
426 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100427}
428
Jens Axboe9bec88e2007-03-02 08:55:48 +0100429static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100430{
Jens Axboeb5af8292007-03-08 12:43:13 +0100431 struct netio_data *nd = td->io_ops->data;
Jens Axboe443662e2008-05-30 13:29:03 +0200432 unsigned int port;
Jens Axboeb5af8292007-03-08 12:43:13 +0100433 char host[64], buf[128];
Jens Axboe414c2a32009-01-16 13:21:15 +0100434 char *sep, *portp, *modep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100435 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100436
Jens Axboe413dd452007-02-23 09:26:09 +0100437 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100438 log_err("fio: network connections must be read OR write\n");
439 return 1;
440 }
Jens Axboe16d55aa2007-05-22 09:21:37 +0200441 if (td_random(td)) {
442 log_err("fio: network IO can't be random\n");
443 return 1;
444 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100445
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100446 strcpy(buf, td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100447
Jens Axboe9f9214f2007-03-13 14:02:16 +0100448 sep = strchr(buf, '/');
Jens Axboe443662e2008-05-30 13:29:03 +0200449 if (!sep)
450 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100451
452 *sep = '\0';
453 sep++;
454 strcpy(host, buf);
Jens Axboe443662e2008-05-30 13:29:03 +0200455 if (!strlen(host))
456 goto bad_host;
457
Jens Axboe414c2a32009-01-16 13:21:15 +0100458 modep = NULL;
459 portp = sep;
460 sep = strchr(portp, '/');
461 if (sep) {
462 *sep = '\0';
463 modep = sep + 1;
464 }
465
466 port = strtol(portp, NULL, 10);
Jens Axboe443662e2008-05-30 13:29:03 +0200467 if (!port || port > 65535)
468 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100469
Jens Axboe414c2a32009-01-16 13:21:15 +0100470 if (modep) {
Jens Axboe3f8fc5a2009-01-16 13:22:26 +0100471 if (!strncmp("tcp", modep, strlen(modep)) ||
472 !strncmp("TCP", modep, strlen(modep)))
Jens Axboe414c2a32009-01-16 13:21:15 +0100473 nd->net_protocol = IPPROTO_TCP;
Jens Axboe3f8fc5a2009-01-16 13:22:26 +0100474 else if (!strncmp("udp", modep, strlen(modep)) ||
475 !strncmp("UDP", modep, strlen(modep)))
Jens Axboe414c2a32009-01-16 13:21:15 +0100476 nd->net_protocol = IPPROTO_UDP;
477 else
478 goto bad_host;
479 } else
480 nd->net_protocol = IPPROTO_TCP;
481
Jens Axboe413dd452007-02-23 09:26:09 +0100482 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100483 nd->send_to_net = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100484 ret = fio_netio_setup_listen(td, port);
485 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100486 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100487 ret = fio_netio_setup_connect(td, host, port);
488 }
489
Jens Axboe7bb48f82007-03-27 15:30:28 +0200490 return ret;
Jens Axboe443662e2008-05-30 13:29:03 +0200491bad_host:
Jens Axboe414c2a32009-01-16 13:21:15 +0100492 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
Jens Axboe443662e2008-05-30 13:29:03 +0200493 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100494}
495
Jens Axboeb5af8292007-03-08 12:43:13 +0100496static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100497{
Jens Axboeb5af8292007-03-08 12:43:13 +0100498 struct netio_data *nd = td->io_ops->data;
499
500 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +0200501 if (nd->listenfd != -1)
502 close(nd->listenfd);
503 if (nd->pipes[0] != -1)
504 close(nd->pipes[0]);
505 if (nd->pipes[1] != -1)
506 close(nd->pipes[1]);
507
Jens Axboeb5af8292007-03-08 12:43:13 +0100508 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +0100509 }
510}
511
512static int fio_netio_setup(struct thread_data *td)
513{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200514 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100515
Jens Axboe7bb48f82007-03-27 15:30:28 +0200516 if (!td->io_ops->data) {
517 nd = malloc(sizeof(*nd));;
518
519 memset(nd, 0, sizeof(*nd));
520 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +0200521 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200522 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200523 }
524
Jens Axboe9bec88e2007-03-02 08:55:48 +0100525 return 0;
526}
527
Jens Axboe5921e802008-05-30 15:02:38 +0200528#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200529static int fio_netio_setup_splice(struct thread_data *td)
530{
531 struct netio_data *nd;
532
533 fio_netio_setup(td);
534
535 nd = td->io_ops->data;
536 if (nd) {
537 if (pipe(nd->pipes) < 0)
538 return 1;
539
540 nd->use_splice = 1;
541 return 0;
542 }
543
544 return 1;
545}
546
Jens Axboe5921e802008-05-30 15:02:38 +0200547static struct ioengine_ops ioengine_splice = {
548 .name = "netsplice",
549 .version = FIO_IOOPS_VERSION,
550 .prep = fio_netio_prep,
551 .queue = fio_netio_queue,
552 .setup = fio_netio_setup_splice,
553 .init = fio_netio_init,
554 .cleanup = fio_netio_cleanup,
555 .open_file = fio_netio_open_file,
556 .close_file = generic_close_file,
557 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
558 FIO_SIGQUIT,
559};
560#endif
561
Jens Axboe9cce02e2007-06-22 15:42:21 +0200562static struct ioengine_ops ioengine_rw = {
Jens Axboeed92ac02007-02-06 14:43:52 +0100563 .name = "net",
564 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100565 .prep = fio_netio_prep,
566 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100567 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100568 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100569 .cleanup = fio_netio_cleanup,
570 .open_file = fio_netio_open_file,
571 .close_file = generic_close_file,
Jens Axboead830ed2008-02-18 21:11:24 +0100572 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
573 FIO_SIGQUIT,
Jens Axboeed92ac02007-02-06 14:43:52 +0100574};
575
576static void fio_init fio_netio_register(void)
577{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200578 register_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200579#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200580 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200581#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100582}
583
584static void fio_exit fio_netio_unregister(void)
585{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200586 unregister_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200587#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200588 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200589#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100590}