blob: d6821a4092e7898aa0fea956a6069931fde3a8b2 [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;
25 int send_to_net;
Jens Axboe9cce02e2007-06-22 15:42:21 +020026 int use_splice;
Jens Axboe0fd666b2011-10-06 20:08:53 +020027 int type;
Jens Axboe9cce02e2007-06-22 15:42:21 +020028 int pipes[2];
Jens Axboeb5af8292007-03-08 12:43:13 +010029 char host[64];
30 struct sockaddr_in addr;
Jens Axboe0fd666b2011-10-06 20:08:53 +020031 struct sockaddr_un addr_un;
Jens Axboeb5af8292007-03-08 12:43:13 +010032};
Jens Axboeed92ac02007-02-06 14:43:52 +010033
Jens Axboe664fb3b2009-01-19 13:26:36 +010034struct udp_close_msg {
35 uint32_t magic;
36 uint32_t cmd;
37};
38
39enum {
40 FIO_LINK_CLOSE = 0x89,
41 FIO_LINK_CLOSE_MAGIC = 0x6c696e6b,
Jens Axboe0fd666b2011-10-06 20:08:53 +020042
43 FIO_TYPE_TCP = 1,
44 FIO_TYPE_UDP = 2,
45 FIO_TYPE_UNIX = 3,
Jens Axboe664fb3b2009-01-19 13:26:36 +010046};
47
Jens Axboe371d4562009-01-19 10:17:06 +010048/*
49 * Return -1 for error and 'nr events' for a positive number
50 * of events
51 */
52static int poll_wait(struct thread_data *td, int fd, short events)
53{
54 struct pollfd pfd;
55 int ret;
56
57 while (!td->terminate) {
58 pfd.fd = fd;
59 pfd.events = events;
60 ret = poll(&pfd, 1, -1);
61 if (ret < 0) {
62 if (errno == EINTR)
Jens Axboed5b388a2009-01-19 12:38:27 +010063 break;
Jens Axboe371d4562009-01-19 10:17:06 +010064
65 td_verror(td, errno, "poll");
66 return -1;
67 } else if (!ret)
68 continue;
69
70 break;
71 }
72
73 if (pfd.revents & events)
74 return 1;
Jens Axboe371d4562009-01-19 10:17:06 +010075
76 return -1;
77}
78
Jens Axboeed92ac02007-02-06 14:43:52 +010079static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
80{
Jens Axboeb5af8292007-03-08 12:43:13 +010081 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010082
Jens Axboe7a6499d2007-02-07 09:35:29 +010083 /*
84 * Make sure we don't see spurious reads to a receiver, and vice versa
85 */
Jens Axboeb5af8292007-03-08 12:43:13 +010086 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
87 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010088 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010089 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010090 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010091
Jens Axboef85ac252008-03-01 18:09:49 +010092 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010093}
94
Jens Axboe5921e802008-05-30 15:02:38 +020095#ifdef FIO_HAVE_SPLICE
Jens Axboecd963e12007-06-24 21:41:46 +020096static int splice_io_u(int fdin, int fdout, unsigned int len)
Jens Axboe9cce02e2007-06-22 15:42:21 +020097{
Jens Axboe9cce02e2007-06-22 15:42:21 +020098 int bytes = 0;
99
100 while (len) {
Jens Axboecd963e12007-06-24 21:41:46 +0200101 int ret = splice(fdin, NULL, fdout, NULL, len, 0);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200102
103 if (ret < 0) {
104 if (!bytes)
105 bytes = ret;
106
107 break;
108 } else if (!ret)
109 break;
110
111 bytes += ret;
Jens Axboef657a2f2007-06-22 20:40:10 +0200112 len -= ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200113 }
114
115 return bytes;
116}
117
118/*
Jens Axboecd963e12007-06-24 21:41:46 +0200119 * Receive bytes from a socket and fill them into the internal pipe
120 */
121static int splice_in(struct thread_data *td, struct io_u *io_u)
122{
123 struct netio_data *nd = td->io_ops->data;
124
125 return splice_io_u(io_u->file->fd, nd->pipes[1], io_u->xfer_buflen);
126}
127
128/*
Jens Axboe9cce02e2007-06-22 15:42:21 +0200129 * Transmit 'len' bytes from the internal pipe
130 */
131static int splice_out(struct thread_data *td, struct io_u *io_u,
132 unsigned int len)
133{
134 struct netio_data *nd = td->io_ops->data;
Jens Axboecd963e12007-06-24 21:41:46 +0200135
136 return splice_io_u(nd->pipes[0], io_u->file->fd, len);
137}
138
139static int vmsplice_io_u(struct io_u *io_u, int fd, unsigned int len)
140{
141 struct iovec iov = {
142 .iov_base = io_u->xfer_buf,
143 .iov_len = len,
144 };
Jens Axboe9cce02e2007-06-22 15:42:21 +0200145 int bytes = 0;
146
Jens Axboecd963e12007-06-24 21:41:46 +0200147 while (iov.iov_len) {
148 int ret = vmsplice(fd, &iov, 1, SPLICE_F_MOVE);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200149
150 if (ret < 0) {
151 if (!bytes)
152 bytes = ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200153 break;
154 } else if (!ret)
155 break;
156
Jens Axboecd963e12007-06-24 21:41:46 +0200157 iov.iov_len -= ret;
158 iov.iov_base += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200159 bytes += ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200160 }
161
162 return bytes;
Jens Axboecd963e12007-06-24 21:41:46 +0200163
Jens Axboe9cce02e2007-06-22 15:42:21 +0200164}
165
166/*
167 * vmsplice() pipe to io_u buffer
168 */
169static int vmsplice_io_u_out(struct thread_data *td, struct io_u *io_u,
170 unsigned int len)
171{
172 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200173
Jens Axboecd963e12007-06-24 21:41:46 +0200174 return vmsplice_io_u(io_u, nd->pipes[0], len);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200175}
176
177/*
178 * vmsplice() io_u to pipe
179 */
180static int vmsplice_io_u_in(struct thread_data *td, struct io_u *io_u)
181{
182 struct netio_data *nd = td->io_ops->data;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200183
Jens Axboecd963e12007-06-24 21:41:46 +0200184 return vmsplice_io_u(io_u, nd->pipes[1], io_u->xfer_buflen);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200185}
186
Jens Axboecd963e12007-06-24 21:41:46 +0200187/*
188 * splice receive - transfer socket data into a pipe using splice, then map
189 * that pipe data into the io_u using vmsplice.
190 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200191static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
192{
193 int ret;
194
195 ret = splice_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200196 if (ret > 0)
197 return vmsplice_io_u_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}
201
Jens Axboecd963e12007-06-24 21:41:46 +0200202/*
203 * splice transmit - map data from the io_u into a pipe by using vmsplice,
204 * then transfer that pipe to a socket using splice.
205 */
Jens Axboe9cce02e2007-06-22 15:42:21 +0200206static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
207{
208 int ret;
209
210 ret = vmsplice_io_u_in(td, io_u);
Jens Axboecd963e12007-06-24 21:41:46 +0200211 if (ret > 0)
212 return splice_out(td, io_u, ret);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200213
Jens Axboecd963e12007-06-24 21:41:46 +0200214 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200215}
Jens Axboe5921e802008-05-30 15:02:38 +0200216#else
217static int fio_netio_splice_in(struct thread_data *td, struct io_u *io_u)
218{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200219 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200220 return -1;
221}
222
223static int fio_netio_splice_out(struct thread_data *td, struct io_u *io_u)
224{
Jens Axboeaf8771b2008-05-30 22:58:28 +0200225 errno = EOPNOTSUPP;
Jens Axboe5921e802008-05-30 15:02:38 +0200226 return -1;
227}
228#endif
Jens Axboe9cce02e2007-06-22 15:42:21 +0200229
230static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
231{
Jens Axboe414c2a32009-01-16 13:21:15 +0100232 struct netio_data *nd = td->io_ops->data;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400233 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100234
Jens Axboe664fb3b2009-01-19 13:26:36 +0100235 do {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200236 if (nd->type == FIO_TYPE_UDP) {
Jens Axboe62b38922009-05-11 10:37:33 +0200237 struct sockaddr *to = (struct sockaddr *) &nd->addr;
238
Jens Axboe664fb3b2009-01-19 13:26:36 +0100239 ret = sendto(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200240 io_u->xfer_buflen, flags, to,
241 sizeof(*to));
Jens Axboe664fb3b2009-01-19 13:26:36 +0100242 } else {
243 /*
244 * if we are going to write more, set MSG_MORE
245 */
Jens Axboe5921e802008-05-30 15:02:38 +0200246#ifdef MSG_MORE
Jens Axboe664fb3b2009-01-19 13:26:36 +0100247 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
248 td->o.size)
249 flags |= MSG_MORE;
Jens Axboe5921e802008-05-30 15:02:38 +0200250#endif
Jens Axboe664fb3b2009-01-19 13:26:36 +0100251 ret = send(io_u->file->fd, io_u->xfer_buf,
252 io_u->xfer_buflen, flags);
253 }
254 if (ret > 0)
255 break;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200256
Jens Axboe664fb3b2009-01-19 13:26:36 +0100257 ret = poll_wait(td, io_u->file->fd, POLLOUT);
258 if (ret <= 0)
259 break;
260
Jens Axboe8e239ca2010-08-11 10:29:12 -0400261 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100262 } while (1);
263
264 return ret;
265}
266
267static int is_udp_close(struct io_u *io_u, int len)
268{
269 struct udp_close_msg *msg;
270
271 if (len != sizeof(struct udp_close_msg))
272 return 0;
273
274 msg = io_u->xfer_buf;
275 if (ntohl(msg->magic) != FIO_LINK_CLOSE_MAGIC)
276 return 0;
277 if (ntohl(msg->cmd) != FIO_LINK_CLOSE)
278 return 0;
279
280 return 1;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200281}
282
Jens Axboe414c2a32009-01-16 13:21:15 +0100283static int fio_netio_recv(struct thread_data *td, struct io_u *io_u)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200284{
Jens Axboe414c2a32009-01-16 13:21:15 +0100285 struct netio_data *nd = td->io_ops->data;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400286 int ret, flags = OS_MSG_DONTWAIT;
Jens Axboe371d4562009-01-19 10:17:06 +0100287
Jens Axboe664fb3b2009-01-19 13:26:36 +0100288 do {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200289 if (nd->type == FIO_TYPE_UDP) {
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200290 fio_socklen_t len = sizeof(nd->addr);
Jens Axboe62b38922009-05-11 10:37:33 +0200291 struct sockaddr *from = (struct sockaddr *) &nd->addr;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200292
Jens Axboe664fb3b2009-01-19 13:26:36 +0100293 ret = recvfrom(io_u->file->fd, io_u->xfer_buf,
Jens Axboe62b38922009-05-11 10:37:33 +0200294 io_u->xfer_buflen, flags, from, &len);
Jens Axboe664fb3b2009-01-19 13:26:36 +0100295 if (is_udp_close(io_u, ret)) {
296 td->done = 1;
297 return 0;
298 }
299 } else {
300 ret = recv(io_u->file->fd, io_u->xfer_buf,
301 io_u->xfer_buflen, flags);
302 }
303 if (ret > 0)
304 break;
Jens Axboe414c2a32009-01-16 13:21:15 +0100305
Jens Axboe664fb3b2009-01-19 13:26:36 +0100306 ret = poll_wait(td, io_u->file->fd, POLLIN);
307 if (ret <= 0)
308 break;
Jens Axboe8e239ca2010-08-11 10:29:12 -0400309 flags &= ~OS_MSG_DONTWAIT;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100310 flags |= MSG_WAITALL;
311 } while (1);
312
313 return ret;
Jens Axboe9cce02e2007-06-22 15:42:21 +0200314}
315
Jens Axboeed92ac02007-02-06 14:43:52 +0100316static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
317{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200318 struct netio_data *nd = td->io_ops->data;
319 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100320
Jens Axboe7101d9c2007-09-12 13:12:39 +0200321 fio_ro_check(td, io_u);
322
Jens Axboe7a6499d2007-02-07 09:35:29 +0100323 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200324 if (!nd->use_splice || nd->type == FIO_TYPE_UDP ||
325 nd->type == FIO_TYPE_UNIX)
Jens Axboe9cce02e2007-06-22 15:42:21 +0200326 ret = fio_netio_send(td, io_u);
Jens Axboe9cce02e2007-06-22 15:42:21 +0200327 else
Jens Axboe414c2a32009-01-16 13:21:15 +0100328 ret = fio_netio_splice_out(td, io_u);
329 } else if (io_u->ddir == DDIR_READ) {
Jens Axboe0fd666b2011-10-06 20:08:53 +0200330 if (!nd->use_splice || nd->type == FIO_TYPE_UDP ||
331 nd->type == FIO_TYPE_UDP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100332 ret = fio_netio_recv(td, io_u);
333 else
334 ret = fio_netio_splice_in(td, io_u);
Jens Axboed4f12dd2007-02-08 12:59:02 +0100335 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +0100336 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +0100337
Jens Axboecec6b552007-02-06 20:15:38 +0100338 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100339 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100340 io_u->resid = io_u->xfer_buflen - ret;
341 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100342 return FIO_Q_COMPLETED;
Jens Axboe414c2a32009-01-16 13:21:15 +0100343 } else {
344 int err = errno;
345
346 if (io_u->ddir == DDIR_WRITE && err == EMSGSIZE)
347 return FIO_Q_BUSY;
348
349 io_u->error = err;
350 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100351 }
352
Jens Axboe36167d82007-02-18 05:41:31 +0100353 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +0100354 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +0100355
Jens Axboe36167d82007-02-18 05:41:31 +0100356 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +0100357}
358
Jens Axboeb5af8292007-03-08 12:43:13 +0100359static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100360{
Jens Axboeb5af8292007-03-08 12:43:13 +0100361 struct netio_data *nd = td->io_ops->data;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200362 int type, domain;
Jens Axboeed92ac02007-02-06 14:43:52 +0100363
Jens Axboe0fd666b2011-10-06 20:08:53 +0200364 if (nd->type == FIO_TYPE_TCP) {
365 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100366 type = SOCK_STREAM;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200367 } else if (nd->type == FIO_TYPE_UDP) {
368 domain = AF_INET;
Jens Axboe414c2a32009-01-16 13:21:15 +0100369 type = SOCK_DGRAM;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200370 } else if (nd->type == FIO_TYPE_UNIX) {
371 domain = AF_UNIX;
372 type = SOCK_STREAM;
373 } else {
374 log_err("fio: bad network type %d\n", nd->type);
375 f->fd = -1;
376 return 1;
377 }
Jens Axboe414c2a32009-01-16 13:21:15 +0100378
Jens Axboe0fd666b2011-10-06 20:08:53 +0200379 f->fd = socket(domain, type, 0);
Jens Axboeb5af8292007-03-08 12:43:13 +0100380 if (f->fd < 0) {
381 td_verror(td, errno, "socket");
382 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100383 }
384
Jens Axboe0fd666b2011-10-06 20:08:53 +0200385 if (nd->type == FIO_TYPE_UDP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100386 return 0;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200387 else if (nd->type == FIO_TYPE_TCP) {
388 fio_socklen_t len = sizeof(nd->addr);
Jens Axboe414c2a32009-01-16 13:21:15 +0100389
Jens Axboe0fd666b2011-10-06 20:08:53 +0200390 if (connect(f->fd, (struct sockaddr *) &nd->addr, len) < 0) {
391 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200392 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200393 return 1;
394 }
395 } else {
396 struct sockaddr_un *addr = &nd->addr_un;
397 fio_socklen_t len;
398
399 len = sizeof(addr->sun_family) + strlen(addr->sun_path) + 1;
400
401 if (connect(f->fd, (struct sockaddr *) addr, len) < 0) {
402 td_verror(td, errno, "connect");
Jens Axboeb94cba42011-10-06 21:27:10 +0200403 close(f->fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200404 return 1;
405 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100406 }
407
408 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100409}
410
Jens Axboeb5af8292007-03-08 12:43:13 +0100411static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100412{
Jens Axboeb5af8292007-03-08 12:43:13 +0100413 struct netio_data *nd = td->io_ops->data;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200414 fio_socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100415
Jens Axboe0fd666b2011-10-06 20:08:53 +0200416 if (nd->type == FIO_TYPE_UDP) {
Jens Axboe414c2a32009-01-16 13:21:15 +0100417 f->fd = nd->listenfd;
418 return 0;
419 }
420
Jens Axboe6d861442007-03-15 09:22:23 +0100421 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100422
Jens Axboe371d4562009-01-19 10:17:06 +0100423 if (poll_wait(td, nd->listenfd, POLLIN) < 0)
424 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100425
Jens Axboe371d4562009-01-19 10:17:06 +0100426 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
427 if (f->fd < 0) {
428 td_verror(td, errno, "accept");
429 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100430 }
431
432 return 0;
433}
434
Jens Axboeb5af8292007-03-08 12:43:13 +0100435static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100436{
Jens Axboe0fd666b2011-10-06 20:08:53 +0200437 int ret;
438
Jens Axboeb5af8292007-03-08 12:43:13 +0100439 if (td_read(td))
Jens Axboe0fd666b2011-10-06 20:08:53 +0200440 ret = fio_netio_accept(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100441 else
Jens Axboe0fd666b2011-10-06 20:08:53 +0200442 ret = fio_netio_connect(td, f);
443
444 if (ret)
445 f->fd = -1;
446 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +0100447}
448
Jens Axboe664fb3b2009-01-19 13:26:36 +0100449static void fio_netio_udp_close(struct thread_data *td, struct fio_file *f)
450{
451 struct netio_data *nd = td->io_ops->data;
452 struct udp_close_msg msg;
Jens Axboe62b38922009-05-11 10:37:33 +0200453 struct sockaddr *to = (struct sockaddr *) &nd->addr;
Jens Axboe664fb3b2009-01-19 13:26:36 +0100454 int ret;
455
456 msg.magic = htonl(FIO_LINK_CLOSE_MAGIC);
457 msg.cmd = htonl(FIO_LINK_CLOSE);
458
Jens Axboe62b38922009-05-11 10:37:33 +0200459 ret = sendto(f->fd, &msg, sizeof(msg), MSG_WAITALL, to,
Jens Axboe664fb3b2009-01-19 13:26:36 +0100460 sizeof(nd->addr));
461 if (ret < 0)
462 td_verror(td, errno, "sendto udp link close");
463}
464
465static int fio_netio_close_file(struct thread_data *td, struct fio_file *f)
466{
467 struct netio_data *nd = td->io_ops->data;
468
469 /*
470 * If this is an UDP connection, notify the receiver that we are
471 * closing down the link
472 */
Jens Axboe0fd666b2011-10-06 20:08:53 +0200473 if (nd->type == FIO_TYPE_UDP)
Jens Axboe664fb3b2009-01-19 13:26:36 +0100474 fio_netio_udp_close(td, f);
475
476 return generic_close_file(td, f);
477}
478
Jens Axboe0fd666b2011-10-06 20:08:53 +0200479static int fio_netio_setup_connect_inet(struct thread_data *td,
480 const char *host, unsigned short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100481{
482 struct netio_data *nd = td->io_ops->data;
483
484 nd->addr.sin_family = AF_INET;
485 nd->addr.sin_port = htons(port);
486
487 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
488 struct hostent *hent;
489
490 hent = gethostbyname(host);
491 if (!hent) {
492 td_verror(td, errno, "gethostbyname");
493 return 1;
494 }
495
496 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
497 }
498
499 return 0;
500}
501
Jens Axboe0fd666b2011-10-06 20:08:53 +0200502static int fio_netio_setup_connect_unix(struct thread_data *td,
503 const char *path)
504{
505 struct netio_data *nd = td->io_ops->data;
506 struct sockaddr_un *soun = &nd->addr_un;
507
508 soun->sun_family = AF_UNIX;
509 strcpy(soun->sun_path, path);
510 return 0;
511}
512
513static int fio_netio_setup_connect(struct thread_data *td, const char *host,
514 unsigned short port)
515{
516 struct netio_data *nd = td->io_ops->data;
517
518 if (nd->type == FIO_TYPE_UDP || nd->type == FIO_TYPE_TCP)
519 return fio_netio_setup_connect_inet(td, host, port);
520 else
521 return fio_netio_setup_connect_unix(td, host);
522}
523
524static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
525{
526 struct netio_data *nd = td->io_ops->data;
527 struct sockaddr_un *addr = &nd->addr_un;
528 mode_t mode;
529 int len, fd;
530
531 fd = socket(AF_UNIX, SOCK_STREAM, 0);
532 if (fd < 0) {
533 log_err("fio: socket: %s\n", strerror(errno));
534 return -1;
535 }
536
537 mode = umask(000);
538
539 memset(addr, 0, sizeof(*addr));
540 addr->sun_family = AF_UNIX;
541 strcpy(addr->sun_path, path);
542 unlink(path);
543
544 len = sizeof(addr->sun_family) + strlen(path) + 1;
545
546 if (bind(fd, (struct sockaddr *) addr, len) < 0) {
547 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200548 close(fd);
Jens Axboe0fd666b2011-10-06 20:08:53 +0200549 return -1;
550 }
551
552 umask(mode);
553 nd->listenfd = fd;
554 return 0;
555}
556
557static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
Jens Axboeb5af8292007-03-08 12:43:13 +0100558{
559 struct netio_data *nd = td->io_ops->data;
Jens Axboe414c2a32009-01-16 13:21:15 +0100560 int fd, opt, type;
Jens Axboeed92ac02007-02-06 14:43:52 +0100561
Jens Axboe0fd666b2011-10-06 20:08:53 +0200562 if (nd->type == FIO_TYPE_TCP)
Jens Axboe414c2a32009-01-16 13:21:15 +0100563 type = SOCK_STREAM;
564 else
565 type = SOCK_DGRAM;
566
Jens Axboe0fd666b2011-10-06 20:08:53 +0200567 fd = socket(AF_INET, type, 0);
Jens Axboeed92ac02007-02-06 14:43:52 +0100568 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100569 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100570 return 1;
571 }
572
573 opt = 1;
574 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100575 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100576 return 1;
577 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100578#ifdef SO_REUSEPORT
579 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100580 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100581 return 1;
582 }
583#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100584
Jens Axboeb5af8292007-03-08 12:43:13 +0100585 nd->addr.sin_family = AF_INET;
586 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
587 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100588
Jens Axboeb5af8292007-03-08 12:43:13 +0100589 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100590 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100591 return 1;
592 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200593
594 nd->listenfd = fd;
595 return 0;
596}
597
598static int fio_netio_setup_listen(struct thread_data *td, const char *path,
599 short port)
600{
601 struct netio_data *nd = td->io_ops->data;
602 int ret;
603
604 if (nd->type == FIO_TYPE_UDP || nd->type == FIO_TYPE_TCP)
605 ret = fio_netio_setup_listen_inet(td, port);
606 else
607 ret = fio_netio_setup_listen_unix(td, path);
608
609 if (ret)
610 return ret;
611 if (nd->type == FIO_TYPE_UDP)
612 return 0;
613
614 if (listen(nd->listenfd, 10) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100615 td_verror(td, errno, "listen");
Jens Axboe0fd666b2011-10-06 20:08:53 +0200616 nd->listenfd = -1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100617 return 1;
618 }
619
Jens Axboeb5af8292007-03-08 12:43:13 +0100620 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100621}
622
Jens Axboe9bec88e2007-03-02 08:55:48 +0100623static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100624{
Jens Axboeb5af8292007-03-08 12:43:13 +0100625 struct netio_data *nd = td->io_ops->data;
Jens Axboe443662e2008-05-30 13:29:03 +0200626 unsigned int port;
Jens Axboeb5af8292007-03-08 12:43:13 +0100627 char host[64], buf[128];
Jens Axboe414c2a32009-01-16 13:21:15 +0100628 char *sep, *portp, *modep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100629 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100630
Jens Axboe413dd452007-02-23 09:26:09 +0100631 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100632 log_err("fio: network connections must be read OR write\n");
633 return 1;
634 }
Jens Axboe16d55aa2007-05-22 09:21:37 +0200635 if (td_random(td)) {
636 log_err("fio: network IO can't be random\n");
637 return 1;
638 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100639
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100640 strcpy(buf, td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100641
Jens Axboe0fd666b2011-10-06 20:08:53 +0200642 sep = strchr(buf, ',');
Jens Axboe443662e2008-05-30 13:29:03 +0200643 if (!sep)
644 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100645
646 *sep = '\0';
647 sep++;
648 strcpy(host, buf);
Jens Axboe443662e2008-05-30 13:29:03 +0200649 if (!strlen(host))
650 goto bad_host;
651
Jens Axboe414c2a32009-01-16 13:21:15 +0100652 modep = NULL;
653 portp = sep;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200654 sep = strchr(portp, ',');
Jens Axboe414c2a32009-01-16 13:21:15 +0100655 if (sep) {
656 *sep = '\0';
657 modep = sep + 1;
658 }
Jens Axboe0fd666b2011-10-06 20:08:53 +0200659
660 if (!strncmp("tcp", modep, strlen(modep)) ||
661 !strncmp("TCP", modep, strlen(modep)))
662 nd->type = FIO_TYPE_TCP;
663 else if (!strncmp("udp", modep, strlen(modep)) ||
664 !strncmp("UDP", modep, strlen(modep)))
665 nd->type = FIO_TYPE_UDP;
666 else if (!strncmp("unix", modep, strlen(modep)) ||
667 !strncmp("UNIX", modep, strlen(modep)))
668 nd->type = FIO_TYPE_UNIX;
669 else
Jens Axboe443662e2008-05-30 13:29:03 +0200670 goto bad_host;
Jens Axboeed92ac02007-02-06 14:43:52 +0100671
Jens Axboe0fd666b2011-10-06 20:08:53 +0200672 if (nd->type != FIO_TYPE_UNIX) {
673 port = strtol(portp, NULL, 10);
674 if (!port || port > 65535)
Jens Axboe414c2a32009-01-16 13:21:15 +0100675 goto bad_host;
676 } else
Jens Axboe0fd666b2011-10-06 20:08:53 +0200677 port = 0;
Jens Axboe414c2a32009-01-16 13:21:15 +0100678
Jens Axboe413dd452007-02-23 09:26:09 +0100679 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100680 nd->send_to_net = 0;
Jens Axboe0fd666b2011-10-06 20:08:53 +0200681 ret = fio_netio_setup_listen(td, host, port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100682 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100683 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100684 ret = fio_netio_setup_connect(td, host, port);
685 }
686
Jens Axboe7bb48f82007-03-27 15:30:28 +0200687 return ret;
Jens Axboe443662e2008-05-30 13:29:03 +0200688bad_host:
Jens Axboe414c2a32009-01-16 13:21:15 +0100689 log_err("fio: bad network host/port/protocol: %s\n", td->o.filename);
Jens Axboe443662e2008-05-30 13:29:03 +0200690 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100691}
692
Jens Axboeb5af8292007-03-08 12:43:13 +0100693static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100694{
Jens Axboeb5af8292007-03-08 12:43:13 +0100695 struct netio_data *nd = td->io_ops->data;
696
697 if (nd) {
Jens Axboe64b24cd2007-06-24 21:28:39 +0200698 if (nd->listenfd != -1)
699 close(nd->listenfd);
700 if (nd->pipes[0] != -1)
701 close(nd->pipes[0]);
702 if (nd->pipes[1] != -1)
703 close(nd->pipes[1]);
704
Jens Axboeb5af8292007-03-08 12:43:13 +0100705 free(nd);
Jens Axboeb5af8292007-03-08 12:43:13 +0100706 }
707}
708
709static int fio_netio_setup(struct thread_data *td)
710{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200711 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100712
Jens Axboe7bb48f82007-03-27 15:30:28 +0200713 if (!td->io_ops->data) {
714 nd = malloc(sizeof(*nd));;
715
716 memset(nd, 0, sizeof(*nd));
717 nd->listenfd = -1;
Jens Axboe64b24cd2007-06-24 21:28:39 +0200718 nd->pipes[0] = nd->pipes[1] = -1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200719 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200720 }
721
Jens Axboe9bec88e2007-03-02 08:55:48 +0100722 return 0;
723}
724
Jens Axboe5921e802008-05-30 15:02:38 +0200725#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200726static int fio_netio_setup_splice(struct thread_data *td)
727{
728 struct netio_data *nd;
729
730 fio_netio_setup(td);
731
732 nd = td->io_ops->data;
733 if (nd) {
734 if (pipe(nd->pipes) < 0)
735 return 1;
736
737 nd->use_splice = 1;
738 return 0;
739 }
740
741 return 1;
742}
743
Jens Axboe5921e802008-05-30 15:02:38 +0200744static struct ioengine_ops ioengine_splice = {
745 .name = "netsplice",
746 .version = FIO_IOOPS_VERSION,
747 .prep = fio_netio_prep,
748 .queue = fio_netio_queue,
749 .setup = fio_netio_setup_splice,
750 .init = fio_netio_init,
751 .cleanup = fio_netio_cleanup,
752 .open_file = fio_netio_open_file,
753 .close_file = generic_close_file,
754 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Bruce Cran03e20d62011-01-02 20:14:54 +0100755 FIO_SIGTERM | FIO_PIPEIO,
Jens Axboe5921e802008-05-30 15:02:38 +0200756};
757#endif
758
Jens Axboe9cce02e2007-06-22 15:42:21 +0200759static struct ioengine_ops ioengine_rw = {
Jens Axboeed92ac02007-02-06 14:43:52 +0100760 .name = "net",
761 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100762 .prep = fio_netio_prep,
763 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100764 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100765 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100766 .cleanup = fio_netio_cleanup,
767 .open_file = fio_netio_open_file,
Jens Axboe664fb3b2009-01-19 13:26:36 +0100768 .close_file = fio_netio_close_file,
Jens Axboead830ed2008-02-18 21:11:24 +0100769 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_UNIDIR |
Bruce Cran03e20d62011-01-02 20:14:54 +0100770 FIO_SIGTERM | FIO_PIPEIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100771};
772
773static void fio_init fio_netio_register(void)
774{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200775 register_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200776#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200777 register_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200778#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100779}
780
781static void fio_exit fio_netio_unregister(void)
782{
Jens Axboe9cce02e2007-06-22 15:42:21 +0200783 unregister_ioengine(&ioengine_rw);
Jens Axboe5921e802008-05-30 15:02:38 +0200784#ifdef FIO_HAVE_SPLICE
Jens Axboe9cce02e2007-06-22 15:42:21 +0200785 unregister_ioengine(&ioengine_splice);
Jens Axboe5921e802008-05-30 15:02:38 +0200786#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100787}