blob: 222624d946d091e017274ea8f2b3d854b3527fa6 [file] [log] [blame]
Jens Axboeed92ac02007-02-06 14:43:52 +01001/*
2 * Transfer data over the net. Pretty basic setup, will only support
3 * 1 file per thread/job.
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10#include <netinet/in.h>
11#include <arpa/inet.h>
12#include <netdb.h>
13
14#include "../fio.h"
15#include "../os.h"
16
17struct net_data {
18 int send_to_net;
19 struct io_u *last_io_u;
20};
21
22static int fio_netio_getevents(struct thread_data *td, int fio_unused min,
23 int max, struct timespec fio_unused *t)
24{
25 assert(max <= 1);
26
27 /*
28 * we can only have one finished io_u for sync io, since the depth
29 * is always 1
30 */
31 if (list_empty(&td->io_u_busylist))
32 return 0;
33
34 return 1;
35}
36
37static struct io_u *fio_netio_event(struct thread_data *td, int event)
38{
39 struct net_data *nd = td->io_ops->data;
40
41 assert(event == 0);
42
43 return nd->last_io_u;
44}
45
46static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
47{
48 struct net_data *nd = td->io_ops->data;
49 struct fio_file *f = io_u->file;
50
Jens Axboe7a6499d2007-02-07 09:35:29 +010051 /*
52 * Make sure we don't see spurious reads to a receiver, and vice versa
53 */
54 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
55 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
56 printf("boo!\n");
57 td_verror(td, EINVAL);
58 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010059 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010060
Jens Axboeed92ac02007-02-06 14:43:52 +010061 if (io_u->ddir == DDIR_SYNC)
62 return 0;
63 if (io_u->offset == f->last_completed_pos)
64 return 0;
65
Jens Axboee01547d2007-02-06 19:16:01 +010066 /*
67 * If offset is different from last end position, it's a seek.
68 * As network io is purely sequential, we don't allow seeks.
69 */
Jens Axboeed92ac02007-02-06 14:43:52 +010070 td_verror(td, EINVAL);
71 return 1;
72}
73
74static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
75{
76 struct net_data *nd = td->io_ops->data;
77 struct fio_file *f = io_u->file;
Jens Axboe7a6499d2007-02-07 09:35:29 +010078 int ret;
Jens Axboeed92ac02007-02-06 14:43:52 +010079
Jens Axboe7a6499d2007-02-07 09:35:29 +010080 if (io_u->ddir == DDIR_WRITE) {
81 int flags = 0;
82
83 /*
84 * if we are going to write more, set MSG_MORE
85 */
86 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
87 td->io_size)
88 flags = MSG_MORE;
89
90 ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
91 } else if (io_u->ddir == DDIR_READ)
92 ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, MSG_WAITALL);
93 else
94 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +010095
Jens Axboecec6b552007-02-06 20:15:38 +010096 if (ret != (int) io_u->xfer_buflen) {
Jens Axboeed92ac02007-02-06 14:43:52 +010097 if (ret > 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010098 io_u->resid = io_u->xfer_buflen - ret;
99 io_u->error = 0;
100 return ret;
Jens Axboeed92ac02007-02-06 14:43:52 +0100101 } else
102 io_u->error = errno;
103 }
104
105 if (!io_u->error)
106 nd->last_io_u = io_u;
107
108 return io_u->error;
109}
110
111static int fio_netio_setup_connect(struct thread_data *td, const char *host,
Jens Axboee01547d2007-02-06 19:16:01 +0100112 unsigned short port)
Jens Axboeed92ac02007-02-06 14:43:52 +0100113{
114 struct sockaddr_in addr;
115 struct fio_file *f;
Jens Axboe2fc26982007-02-06 19:27:27 +0100116 int i;
Jens Axboeed92ac02007-02-06 14:43:52 +0100117
118 memset(&addr, 0, sizeof(addr));
119 addr.sin_family = AF_INET;
Jens Axboee01547d2007-02-06 19:16:01 +0100120 addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100121
122 if (inet_aton(host, &addr.sin_addr) != 1) {
Jens Axboe7a6499d2007-02-07 09:35:29 +0100123 struct hostent *hent;
Jens Axboeed92ac02007-02-06 14:43:52 +0100124
Jens Axboe7a6499d2007-02-07 09:35:29 +0100125 hent = gethostbyname(host);
Jens Axboeed92ac02007-02-06 14:43:52 +0100126 if (!hent) {
127 td_vmsg(td, errno, "gethostbyname");
128 return 1;
129 }
130
131 memcpy(&addr.sin_addr, hent->h_addr, 4);
132 }
133
Jens Axboe2fc26982007-02-06 19:27:27 +0100134 for_each_file(td, f, i) {
135 f->fd = socket(AF_INET, SOCK_STREAM, 0);
136 if (f->fd < 0) {
137 td_vmsg(td, errno, "socket");
138 return 1;
139 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100140
Jens Axboe2fc26982007-02-06 19:27:27 +0100141 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
142 td_vmsg(td, errno, "connect");
143 return 1;
144 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100145 }
146
147 return 0;
148
149}
150
Jens Axboee01547d2007-02-06 19:16:01 +0100151static int fio_netio_setup_listen(struct thread_data *td, unsigned short port)
Jens Axboeed92ac02007-02-06 14:43:52 +0100152{
153 struct sockaddr_in addr;
154 socklen_t socklen;
Jens Axboe2fc26982007-02-06 19:27:27 +0100155 struct fio_file *f;
156 int fd, opt, i;
Jens Axboeed92ac02007-02-06 14:43:52 +0100157
158 fd = socket(AF_INET, SOCK_STREAM, 0);
159 if (fd < 0) {
160 td_vmsg(td, errno, "socket");
161 return 1;
162 }
163
164 opt = 1;
165 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
166 td_vmsg(td, errno, "setsockopt");
167 return 1;
168 }
169
170 memset(&addr, 0, sizeof(addr));
171 addr.sin_family = AF_INET;
172 addr.sin_addr.s_addr = htonl(INADDR_ANY);
Jens Axboee01547d2007-02-06 19:16:01 +0100173 addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100174
175 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
176 td_vmsg(td, errno, "bind");
177 return 1;
178 }
179 if (listen(fd, 1) < 0) {
180 td_vmsg(td, errno, "listen");
181 return 1;
182 }
183
Jens Axboe2fc26982007-02-06 19:27:27 +0100184 fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files);
185
Jens Axboeed92ac02007-02-06 14:43:52 +0100186 socklen = sizeof(addr);
Jens Axboe2fc26982007-02-06 19:27:27 +0100187 for_each_file(td, f, i) {
188 f->fd = accept(fd, (struct sockaddr *) &addr, &socklen);
189 if (f->fd < 0) {
190 td_vmsg(td, errno, "accept");
191 return 1;
192 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100193 }
194
195 return 0;
196}
197
198static int fio_netio_setup(struct thread_data *td)
199{
Jens Axboee01547d2007-02-06 19:16:01 +0100200 char host[64], buf[128];
Jens Axboeed92ac02007-02-06 14:43:52 +0100201 struct net_data *nd;
Jens Axboee01547d2007-02-06 19:16:01 +0100202 unsigned short port;
Jens Axboe2fc26982007-02-06 19:27:27 +0100203 struct fio_file *f;
Jens Axboeed92ac02007-02-06 14:43:52 +0100204 char *sep;
Jens Axboe2fc26982007-02-06 19:27:27 +0100205 int ret, i;
Jens Axboeed92ac02007-02-06 14:43:52 +0100206
Jens Axboe7a6499d2007-02-07 09:35:29 +0100207 if (!td->total_file_size) {
208 log_err("fio: need size= set\n");
209 return 1;
210 }
211
Jens Axboeed92ac02007-02-06 14:43:52 +0100212 /*
213 * work around for late init call
214 */
215 if (td->io_ops->init(td))
216 return 1;
217
218 nd = td->io_ops->data;
219
220 if (td->iomix) {
221 log_err("fio: network connections must be read OR write\n");
222 return 1;
223 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100224
225 strcpy(buf, td->filename);
226
227 sep = strchr(buf, ':');
228 if (!sep) {
229 log_err("fio: bad network host:port <<%s>>\n", td->filename);
230 return 1;
231 }
232
233 *sep = '\0';
234 sep++;
235 strcpy(host, buf);
Jens Axboee01547d2007-02-06 19:16:01 +0100236 port = atoi(sep);
Jens Axboeed92ac02007-02-06 14:43:52 +0100237
238 if (td->ddir == READ) {
239 nd->send_to_net = 0;
240 ret = fio_netio_setup_listen(td, port);
241 } else {
242 nd->send_to_net = 1;
243 ret = fio_netio_setup_connect(td, host, port);
244 }
245
Jens Axboe2fc26982007-02-06 19:27:27 +0100246 if (ret)
247 return ret;
248
249 td->io_size = td->total_file_size;
250 td->total_io_size = td->io_size;
251
252 for_each_file(td, f, i) {
253 f->file_size = td->total_file_size / td->nr_files;
254 f->real_file_size = f->file_size;
Jens Axboeed92ac02007-02-06 14:43:52 +0100255 }
256
Jens Axboe2fc26982007-02-06 19:27:27 +0100257 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100258}
259
260static void fio_netio_cleanup(struct thread_data *td)
261{
262 if (td->io_ops->data) {
263 free(td->io_ops->data);
264 td->io_ops->data = NULL;
265 }
266}
267
268static int fio_netio_init(struct thread_data *td)
269{
270 struct net_data *nd;
271
Jens Axboee01547d2007-02-06 19:16:01 +0100272 /*
273 * Hack to work-around the ->setup() function calling init on its
274 * own, since it needs ->io_ops->data to be set up.
275 */
Jens Axboeed92ac02007-02-06 14:43:52 +0100276 if (td->io_ops->data)
277 return 0;
278
279 nd = malloc(sizeof(*nd));
280 nd->last_io_u = NULL;
281 td->io_ops->data = nd;
282 return 0;
283}
284
285static struct ioengine_ops ioengine = {
286 .name = "net",
287 .version = FIO_IOOPS_VERSION,
288 .init = fio_netio_init,
289 .prep = fio_netio_prep,
290 .queue = fio_netio_queue,
291 .getevents = fio_netio_getevents,
292 .event = fio_netio_event,
293 .cleanup = fio_netio_cleanup,
294 .setup = fio_netio_setup,
295 .flags = FIO_SYNCIO | FIO_NETIO,
296};
297
298static void fio_init fio_netio_register(void)
299{
300 register_ioengine(&ioengine);
301}
302
303static void fio_exit fio_netio_unregister(void)
304{
305 unregister_ioengine(&ioengine);
306}