blob: 1a403bdb95688319ef53c0324cd57b368a1741e4 [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"
Jens Axboeed92ac02007-02-06 14:43:52 +010018
Jens Axboeb5af8292007-03-08 12:43:13 +010019struct netio_data {
20 int listenfd;
21 int send_to_net;
22 char host[64];
23 struct sockaddr_in addr;
24};
Jens Axboeed92ac02007-02-06 14:43:52 +010025
26static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
27{
Jens Axboeb5af8292007-03-08 12:43:13 +010028 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010029 struct fio_file *f = io_u->file;
30
Jens Axboe7a6499d2007-02-07 09:35:29 +010031 /*
32 * Make sure we don't see spurious reads to a receiver, and vice versa
33 */
Jens Axboeb5af8292007-03-08 12:43:13 +010034 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
35 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
Jens Axboee1161c32007-02-22 19:36:48 +010036 td_verror(td, EINVAL, "bad direction");
Jens Axboe7a6499d2007-02-07 09:35:29 +010037 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010038 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010039
Jens Axboeed92ac02007-02-06 14:43:52 +010040 if (io_u->ddir == DDIR_SYNC)
41 return 0;
42 if (io_u->offset == f->last_completed_pos)
43 return 0;
44
Jens Axboee01547d2007-02-06 19:16:01 +010045 /*
46 * If offset is different from last end position, it's a seek.
47 * As network io is purely sequential, we don't allow seeks.
48 */
Jens Axboee1161c32007-02-22 19:36:48 +010049 td_verror(td, EINVAL, "cannot seek");
Jens Axboeed92ac02007-02-06 14:43:52 +010050 return 1;
51}
52
53static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
54{
Jens Axboeed92ac02007-02-06 14:43:52 +010055 struct fio_file *f = io_u->file;
Jens Axboed4f12dd2007-02-08 12:59:02 +010056 int ret, flags = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010057
Jens Axboe7a6499d2007-02-07 09:35:29 +010058 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe7a6499d2007-02-07 09:35:29 +010059 /*
60 * if we are going to write more, set MSG_MORE
61 */
62 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
Jens Axboe7bb48f82007-03-27 15:30:28 +020063 td->o.size)
Jens Axboe7a6499d2007-02-07 09:35:29 +010064 flags = MSG_MORE;
65
66 ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
Jens Axboed4f12dd2007-02-08 12:59:02 +010067 } else if (io_u->ddir == DDIR_READ) {
68 flags = MSG_WAITALL;
69 ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
70 } else
Jens Axboe7a6499d2007-02-07 09:35:29 +010071 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +010072
Jens Axboecec6b552007-02-06 20:15:38 +010073 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +010074 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010075 io_u->resid = io_u->xfer_buflen - ret;
76 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +010077 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010078 } else
79 io_u->error = errno;
80 }
81
Jens Axboe36167d82007-02-18 05:41:31 +010082 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +010083 td_verror(td, io_u->error, "xfer");
Jens Axboeed92ac02007-02-06 14:43:52 +010084
Jens Axboe36167d82007-02-18 05:41:31 +010085 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010086}
87
Jens Axboeb5af8292007-03-08 12:43:13 +010088static int fio_netio_connect(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +010089{
Jens Axboeb5af8292007-03-08 12:43:13 +010090 struct netio_data *nd = td->io_ops->data;
Jens Axboeed92ac02007-02-06 14:43:52 +010091
Jens Axboeb5af8292007-03-08 12:43:13 +010092 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
93 if (f->fd < 0) {
94 td_verror(td, errno, "socket");
95 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010096 }
97
Jens Axboeb5af8292007-03-08 12:43:13 +010098 if (connect(f->fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
99 td_verror(td, errno, "connect");
100 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100101 }
102
103 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100104}
105
Jens Axboeb5af8292007-03-08 12:43:13 +0100106static int fio_netio_accept(struct thread_data *td, struct fio_file *f)
Jens Axboe5fdd1242007-02-11 04:00:37 +0100107{
Jens Axboeb5af8292007-03-08 12:43:13 +0100108 struct netio_data *nd = td->io_ops->data;
109 socklen_t socklen = sizeof(nd->addr);
Jens Axboe5fdd1242007-02-11 04:00:37 +0100110 struct pollfd pfd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100111 int ret;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100112
Jens Axboe6d861442007-03-15 09:22:23 +0100113 log_info("fio: waiting for connection\n");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100114
115 /*
116 * Accept loop. poll for incoming events, accept them. Repeat until we
117 * have all connections.
118 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100119 while (!td->terminate) {
120 pfd.fd = nd->listenfd;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100121 pfd.events = POLLIN;
122
123 ret = poll(&pfd, 1, -1);
124 if (ret < 0) {
125 if (errno == EINTR)
126 continue;
127
Jens Axboee1161c32007-02-22 19:36:48 +0100128 td_verror(td, errno, "poll");
Jens Axboe5fdd1242007-02-11 04:00:37 +0100129 break;
130 } else if (!ret)
131 continue;
132
Jens Axboe0c094422007-02-11 04:44:02 +0100133 /*
134 * should be impossible
135 */
136 if (!(pfd.revents & POLLIN))
137 continue;
138
Jens Axboeb5af8292007-03-08 12:43:13 +0100139 f->fd = accept(nd->listenfd, (struct sockaddr *) &nd->addr, &socklen);
140 if (f->fd < 0) {
141 td_verror(td, errno, "accept");
142 return 1;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100143 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100144 break;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100145 }
146
147 return 0;
148}
149
Jens Axboeb5af8292007-03-08 12:43:13 +0100150
151static int fio_netio_open_file(struct thread_data *td, struct fio_file *f)
Jens Axboeed92ac02007-02-06 14:43:52 +0100152{
Jens Axboeb5af8292007-03-08 12:43:13 +0100153 if (td_read(td))
154 return fio_netio_accept(td, f);
155 else
156 return fio_netio_connect(td, f);
157}
158
159static int fio_netio_setup_connect(struct thread_data *td, const char *host,
160 unsigned short port)
161{
162 struct netio_data *nd = td->io_ops->data;
163
164 nd->addr.sin_family = AF_INET;
165 nd->addr.sin_port = htons(port);
166
167 if (inet_aton(host, &nd->addr.sin_addr) != 1) {
168 struct hostent *hent;
169
170 hent = gethostbyname(host);
171 if (!hent) {
172 td_verror(td, errno, "gethostbyname");
173 return 1;
174 }
175
176 memcpy(&nd->addr.sin_addr, hent->h_addr, 4);
177 }
178
179 return 0;
180}
181
182static int fio_netio_setup_listen(struct thread_data *td, short port)
183{
184 struct netio_data *nd = td->io_ops->data;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100185 int fd, opt;
Jens Axboeed92ac02007-02-06 14:43:52 +0100186
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100187 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Jens Axboeed92ac02007-02-06 14:43:52 +0100188 if (fd < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100189 td_verror(td, errno, "socket");
Jens Axboeed92ac02007-02-06 14:43:52 +0100190 return 1;
191 }
192
193 opt = 1;
194 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100195 td_verror(td, errno, "setsockopt");
Jens Axboeed92ac02007-02-06 14:43:52 +0100196 return 1;
197 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100198#ifdef SO_REUSEPORT
199 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100200 td_verror(td, errno, "setsockopt");
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100201 return 1;
202 }
203#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100204
Jens Axboeb5af8292007-03-08 12:43:13 +0100205 nd->addr.sin_family = AF_INET;
206 nd->addr.sin_addr.s_addr = htonl(INADDR_ANY);
207 nd->addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100208
Jens Axboeb5af8292007-03-08 12:43:13 +0100209 if (bind(fd, (struct sockaddr *) &nd->addr, sizeof(nd->addr)) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100210 td_verror(td, errno, "bind");
Jens Axboeed92ac02007-02-06 14:43:52 +0100211 return 1;
212 }
213 if (listen(fd, 1) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100214 td_verror(td, errno, "listen");
Jens Axboeed92ac02007-02-06 14:43:52 +0100215 return 1;
216 }
217
Jens Axboeb5af8292007-03-08 12:43:13 +0100218 nd->listenfd = fd;
219 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100220}
221
Jens Axboe9bec88e2007-03-02 08:55:48 +0100222static int fio_netio_init(struct thread_data *td)
Jens Axboeed92ac02007-02-06 14:43:52 +0100223{
Jens Axboeb5af8292007-03-08 12:43:13 +0100224 struct netio_data *nd = td->io_ops->data;
Jens Axboee01547d2007-02-06 19:16:01 +0100225 unsigned short port;
Jens Axboeb5af8292007-03-08 12:43:13 +0100226 char host[64], buf[128];
Jens Axboeed92ac02007-02-06 14:43:52 +0100227 char *sep;
Jens Axboeaf52b342007-03-13 10:07:47 +0100228 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100229
Jens Axboe413dd452007-02-23 09:26:09 +0100230 if (td_rw(td)) {
Jens Axboeed92ac02007-02-06 14:43:52 +0100231 log_err("fio: network connections must be read OR write\n");
232 return 1;
233 }
Jens Axboe16d55aa2007-05-22 09:21:37 +0200234 if (td_random(td)) {
235 log_err("fio: network IO can't be random\n");
236 return 1;
237 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100238
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100239 strcpy(buf, td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100240
Jens Axboe9f9214f2007-03-13 14:02:16 +0100241 sep = strchr(buf, '/');
Jens Axboeed92ac02007-02-06 14:43:52 +0100242 if (!sep) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100243 log_err("fio: bad network host/port <<%s>>\n", td->o.filename);
Jens Axboeed92ac02007-02-06 14:43:52 +0100244 return 1;
245 }
246
247 *sep = '\0';
248 sep++;
249 strcpy(host, buf);
Jens Axboee01547d2007-02-06 19:16:01 +0100250 port = atoi(sep);
Jens Axboeed92ac02007-02-06 14:43:52 +0100251
Jens Axboe413dd452007-02-23 09:26:09 +0100252 if (td_read(td)) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100253 nd->send_to_net = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100254 ret = fio_netio_setup_listen(td, port);
255 } else {
Jens Axboeb5af8292007-03-08 12:43:13 +0100256 nd->send_to_net = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100257 ret = fio_netio_setup_connect(td, host, port);
258 }
259
Jens Axboe7bb48f82007-03-27 15:30:28 +0200260 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100261}
262
Jens Axboeb5af8292007-03-08 12:43:13 +0100263static void fio_netio_cleanup(struct thread_data *td)
Jens Axboe9bec88e2007-03-02 08:55:48 +0100264{
Jens Axboeb5af8292007-03-08 12:43:13 +0100265 struct netio_data *nd = td->io_ops->data;
266
267 if (nd) {
268 free(nd);
269 td->io_ops->data = NULL;
270 }
271}
272
273static int fio_netio_setup(struct thread_data *td)
274{
Jens Axboe7bb48f82007-03-27 15:30:28 +0200275 struct netio_data *nd;
Jens Axboeb5af8292007-03-08 12:43:13 +0100276
Jens Axboe7bb48f82007-03-27 15:30:28 +0200277 if (!td->io_ops->data) {
278 nd = malloc(sizeof(*nd));;
279
280 memset(nd, 0, sizeof(*nd));
281 nd->listenfd = -1;
282 td->io_ops->data = nd;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200283 }
284
Jens Axboe9bec88e2007-03-02 08:55:48 +0100285 return 0;
286}
287
Jens Axboeed92ac02007-02-06 14:43:52 +0100288static struct ioengine_ops ioengine = {
289 .name = "net",
290 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100291 .prep = fio_netio_prep,
292 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100293 .setup = fio_netio_setup,
Jens Axboe9bec88e2007-03-02 08:55:48 +0100294 .init = fio_netio_init,
Jens Axboeb5af8292007-03-08 12:43:13 +0100295 .cleanup = fio_netio_cleanup,
296 .open_file = fio_netio_open_file,
297 .close_file = generic_close_file,
298 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
Jens Axboeed92ac02007-02-06 14:43:52 +0100299};
300
301static void fio_init fio_netio_register(void)
302{
303 register_ioengine(&ioengine);
304}
305
306static void fio_exit fio_netio_unregister(void)
307{
308 unregister_ioengine(&ioengine);
309}