blob: f7e4f2d0b748725c0613868e461c2a071d73f018 [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 Axboeed92ac02007-02-06 14:43:52 +010016
17#include "../fio.h"
18#include "../os.h"
19
Jens Axboeb5af8292007-03-08 12:43:13 +010020struct netio_data {
21 int listenfd;
22 int send_to_net;
23 char host[64];
24 struct sockaddr_in addr;
25};
Jens Axboeed92ac02007-02-06 14:43:52 +010026
27static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
28{
Jens Axboeb5af8292007-03-08 12:43:13 +010029 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010030 struct fio_file *f = io_u->file;
31
Jens Axboe7a6499d2007-02-07 09:35:29 +010032 /*
33 * Make sure we don't see spurious reads to a receiver, and vice versa
34 */
Jens Axboeb5af8292007-03-08 12:43:13 +010035 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
36 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010037 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010038 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010039 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010040
Jens Axboeed92ac02007-02-06 14:43:52 +010041 if (io_u->ddir == DDIR_SYNC)
42 return 0;
43 if (io_u->offset == f->last_completed_pos)
44 return 0;
45
Jens Axboee01547d2007-02-06 19:16:01 +010046 /*
47 * If offset is different from last end position, it's a seek.
48 * As network io is purely sequential, we don't allow seeks.
49 */
Jens Axboee1161c32007-02-22 19:36:48 +010050 td_verror(td, EINVAL, "cannot seek");
Jens Axboeed92ac02007-02-06 14:43:52 +010051 return 1;
52}
53
54static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
55{
Jens Axboeed92ac02007-02-06 14:43:52 +010056 struct fio_file *f = io_u->file;
Jens Axboed4f12dd2007-02-08 12:59:02 +010057 int ret, flags = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010058
Jens Axboe7a6499d2007-02-07 09:35:29 +010059 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe7a6499d2007-02-07 09:35:29 +010060 /*
61 * if we are going to write more, set MSG_MORE
62 */
63 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
64 td->io_size)
65 flags = MSG_MORE;
66
67 ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
Jens Axboed4f12dd2007-02-08 12:59:02 +010068 } else if (io_u->ddir == DDIR_READ) {
69 flags = MSG_WAITALL;
70 ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
71 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +010072 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +010073
Jens Axboecec6b552007-02-06 20:15:38 +010074 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +010075 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010076 io_u->resid = io_u->xfer_buflen - ret;
77 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +010078 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010079 } else
80 io_u->error = errno;
81 }
82
Jens Axboe36167d82007-02-18 05:41:31 +010083 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +010084 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +010085
Jens Axboe36167d82007-02-18 05:41:31 +010086 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010087}
88
Jens Axboeb5af8292007-03-08 12:43:13 +010089static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +010090{
Jens Axboeb5af8292007-03-08 12:43:13 +010091 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010092
Jens Axboeb5af8292007-03-08 12:43:13 +010093 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
94 if (f->fd < 0) {
95 td_verror(td, errno, "socket");
96 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010097 }
98
Jens Axboeb5af8292007-03-08 12:43:13 +010099 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
100 td_verror(td, errno, "connect");
101 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100102 }
103
104 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100105}
106
Jens Axboeb5af8292007-03-08 12:43:13 +0100107static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100108{
Jens Axboeb5af8292007-03-08 12:43:13 +0100109 struct netio_data *nd = td->io_ops->data;
110 socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100111 struct pollfd pfd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100112 int ret;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100113
Jens Axboe6d861442007-03-15 09:22:23 +0100114 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100115
116 /*
117 * Accept loop. poll for incoming events, accept them. Repeat until we
118 * have all connections.
119 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100120 while (!td->terminate) {
121 pfd.fd = nd->listenfd;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100122 pfd.events = POLLIN;
123
124 ret = poll(&pfd, 1, -1);
125 if (ret < 0) {
126 if (errno == EINTR)
127 continue;
128
Jens Axboee1161c32007-02-22 19:36:48 +0100129 td_verror(td, errno, "poll");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100130 break;
131 } else if (!ret)
132 continue;
133
Jens Axboe0c094422007-02-11 04:44:02 +0100134 /*
135 * should be impossible
136 */
137 if (!(pfd.revents & POLLIN))
138 continue;
139
Jens Axboeb5af8292007-03-08 12:43:13 +0100140 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
141 if (f->fd < 0) {
142 td_verror(td, errno, "accept");
143 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100144 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100145 break;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100146 }
147
148 return 0;
149}
150
Jens Axboeb5af8292007-03-08 12:43:13 +0100151
152static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100153{
Jens Axboeb5af8292007-03-08 12:43:13 +0100154 if (td_read(td))
155 return fio_netio_accept(td, f);
156 else
157 return fio_netio_connect(td, f);
158}
159
160static int fio_netio_setup_connect(struct thread_data *td, const char *host,
161 unsigned short port)
162{
163 struct netio_data *nd = td->io_ops->data;
164
165 nd->addr.sin_family = AF_INET;
166 nd->addr.sin_port = htons(port);
167
168 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
169 struct hostent *hent;
170
171 hent = gethostbyname(host);
172 if (!hent) {
173 td_verror(td, errno, "gethostbyname");
174 return 1;
175 }
176
177 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
178 }
179
180 return 0;
181}
182
183static int fio_netio_setup_listen(struct thread_data *td, short port)
184{
185 struct netio_data *nd = td->io_ops->data;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100186 int fd, opt;
Jens Axboeed92ac02007-02-06 14:43:52 +0100187
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100188 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Jens Axboeed92ac02007-02-06 14:43:52 +0100189 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100190 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100191 return 1;
192 }
193
194 opt = 1;
195 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100196 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100197 return 1;
198 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100199#ifdef SO_REUSEPORT
200 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100201 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100202 return 1;
203 }
204#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100205
Jens Axboeb5af8292007-03-08 12:43:13 +0100206 nd->addr.sin_family = AF_INET;
207 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
208 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100209
Jens Axboeb5af8292007-03-08 12:43:13 +0100210 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100211 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100212 return 1;
213 }
214 if (listen(fd, 1) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100215 td_verror(td, errno, "listen");
Jens Axboeed92ac02007-02-06 14:43:52 +0100216 return 1;
217 }
218
Jens Axboeb5af8292007-03-08 12:43:13 +0100219 nd->listenfd = fd;
220 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100221}
222
Jens Axboe9bec88e2007-03-02 08:55:48 +0100223static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100224{
Jens Axboeb5af8292007-03-08 12:43:13 +0100225 struct netio_data *nd = td->io_ops->data;
Jens Axboee01547d2007-02-06 19:16:01 +0100226 unsigned short port;
Jens Axboe2fc26982007-02-06 19:27:27 +0100227 struct fio_file *f;
Jens Axboeb5af8292007-03-08 12:43:13 +0100228 char host[64], buf[128];
Jens Axboeaf52b342007-03-13 10:07:47 +0100229 unsigned int i;
Jens Axboeed92ac02007-02-06 14:43:52 +0100230 char *sep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100231 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100232
Jens Axboe7a6499d2007-02-07 09:35:29 +0100233 if (!td->total_file_size) {
234 log_err("fio: need size= set\n");
235 return 1;
236 }
237
Jens Axboe413dd452007-02-23 09:26:09 +0100238 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100239 log_err("fio: network connections must be read OR write\n");
240 return 1;
241 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100242
243 strcpy(buf, td->filename);
244
Jens Axboe9f9214f2007-03-13 14:02:16 +0100245 sep = strchr(buf, '/');
Jens Axboeed92ac02007-02-06 14:43:52 +0100246 if (!sep) {
Jens Axboe9f9214f2007-03-13 14:02:16 +0100247 log_err("fio: bad network host/port <<%s>>\n", td->filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100248 return 1;
249 }
250
251 *sep = '\0';
252 sep++;
253 strcpy(host, buf);
Jens Axboee01547d2007-02-06 19:16:01 +0100254 port = atoi(sep);
Jens Axboeed92ac02007-02-06 14:43:52 +0100255
Jens Axboe413dd452007-02-23 09:26:09 +0100256 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100257 nd->send_to_net = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100258 ret = fio_netio_setup_listen(td, port);
259 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100260 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100261 ret = fio_netio_setup_connect(td, host, port);
262 }
263
Jens Axboe2fc26982007-02-06 19:27:27 +0100264 if (ret)
265 return ret;
266
267 td->io_size = td->total_file_size;
268 td->total_io_size = td->io_size;
269
270 for_each_file(td, f, i) {
271 f->file_size = td->total_file_size / td->nr_files;
272 f->real_file_size = f->file_size;
Jens Axboeed92ac02007-02-06 14:43:52 +0100273 }
274
Jens Axboe2fc26982007-02-06 19:27:27 +0100275 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100276}
277
Jens Axboeb5af8292007-03-08 12:43:13 +0100278static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100279{
Jens Axboeb5af8292007-03-08 12:43:13 +0100280 struct netio_data *nd = td->io_ops->data;
281
282 if (nd) {
283 free(nd);
284 td->io_ops->data = NULL;
285 }
286}
287
288static int fio_netio_setup(struct thread_data *td)
289{
290 struct netio_data *nd = malloc(sizeof(*nd));
291
292 memset(nd, 0, sizeof(*nd));
293 nd->listenfd = -1;
294 td->io_ops->data = nd;
Jens Axboe9bec88e2007-03-02 08:55:48 +0100295 return 0;
296}
297
Jens Axboeed92ac02007-02-06 14:43:52 +0100298static struct ioengine_ops ioengine = {
299 .name = "net",
300 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100301 .prep = fio_netio_prep,
302 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100303 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100304 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100305 .cleanup = fio_netio_cleanup,
306 .open_file = fio_netio_open_file,
307 .close_file = generic_close_file,
308 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100309};
310
311static void fio_init fio_netio_register(void)
312{
313 register_ioengine(&ioengine);
314}
315
316static void fio_exit fio_netio_unregister(void)
317{
318 unregister_ioengine(&ioengine);
319}