2 * Transfer data over the net. Pretty basic setup, will only support
3 * 1 file per thread/job.
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
19 struct io_u *last_io_u;
22 static int fio_netio_getevents(struct thread_data *td, int fio_unused min,
23 int max, struct timespec fio_unused *t)
28 * we can only have one finished io_u for sync io, since the depth
31 if (list_empty(&td->io_u_busylist))
37 static struct io_u *fio_netio_event(struct thread_data *td, int event)
39 struct net_data *nd = td->io_ops->data;
46 static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
48 struct net_data *nd = td->io_ops->data;
49 struct fio_file *f = io_u->file;
52 * Make sure we don't see spurious reads to a receiver, and vice versa
54 if ((nd->send_to_net && io_u->ddir == DDIR_READ) ||
55 (!nd->send_to_net && io_u->ddir == DDIR_WRITE)) {
57 td_verror(td, EINVAL);
61 if (io_u->ddir == DDIR_SYNC)
63 if (io_u->offset == f->last_completed_pos)
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.
70 td_verror(td, EINVAL);
74 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
76 struct net_data *nd = td->io_ops->data;
77 struct fio_file *f = io_u->file;
80 if (io_u->ddir == DDIR_WRITE) {
84 * if we are going to write more, set MSG_MORE
86 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
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);
94 ret = 0; /* must be a SYNC */
96 if (ret != (int) io_u->xfer_buflen) {
98 io_u->resid = io_u->xfer_buflen - ret;
106 nd->last_io_u = io_u;
111 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
114 struct sockaddr_in addr;
118 memset(&addr, 0, sizeof(addr));
119 addr.sin_family = AF_INET;
120 addr.sin_port = htons(port);
122 if (inet_aton(host, &addr.sin_addr) != 1) {
123 struct hostent *hent;
125 hent = gethostbyname(host);
127 td_vmsg(td, errno, "gethostbyname");
131 memcpy(&addr.sin_addr, hent->h_addr, 4);
134 for_each_file(td, f, i) {
135 f->fd = socket(AF_INET, SOCK_STREAM, 0);
137 td_vmsg(td, errno, "socket");
141 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
142 td_vmsg(td, errno, "connect");
151 static int fio_netio_setup_listen(struct thread_data *td, unsigned short port)
153 struct sockaddr_in addr;
158 fd = socket(AF_INET, SOCK_STREAM, 0);
160 td_vmsg(td, errno, "socket");
165 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
166 td_vmsg(td, errno, "setsockopt");
170 memset(&addr, 0, sizeof(addr));
171 addr.sin_family = AF_INET;
172 addr.sin_addr.s_addr = htonl(INADDR_ANY);
173 addr.sin_port = htons(port);
175 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
176 td_vmsg(td, errno, "bind");
179 if (listen(fd, 1) < 0) {
180 td_vmsg(td, errno, "listen");
184 fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files);
186 socklen = sizeof(addr);
187 for_each_file(td, f, i) {
188 f->fd = accept(fd, (struct sockaddr *) &addr, &socklen);
190 td_vmsg(td, errno, "accept");
198 static int fio_netio_setup(struct thread_data *td)
200 char host[64], buf[128];
207 if (!td->total_file_size) {
208 log_err("fio: need size= set\n");
213 * work around for late init call
215 if (td->io_ops->init(td))
218 nd = td->io_ops->data;
221 log_err("fio: network connections must be read OR write\n");
225 strcpy(buf, td->filename);
227 sep = strchr(buf, ':');
229 log_err("fio: bad network host:port <<%s>>\n", td->filename);
238 if (td->ddir == READ) {
240 ret = fio_netio_setup_listen(td, port);
243 ret = fio_netio_setup_connect(td, host, port);
249 td->io_size = td->total_file_size;
250 td->total_io_size = td->io_size;
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;
260 static void fio_netio_cleanup(struct thread_data *td)
262 if (td->io_ops->data) {
263 free(td->io_ops->data);
264 td->io_ops->data = NULL;
268 static int fio_netio_init(struct thread_data *td)
273 * Hack to work-around the ->setup() function calling init on its
274 * own, since it needs ->io_ops->data to be set up.
276 if (td->io_ops->data)
279 nd = malloc(sizeof(*nd));
280 nd->last_io_u = NULL;
281 td->io_ops->data = nd;
285 static struct ioengine_ops ioengine = {
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,
298 static void fio_init fio_netio_register(void)
300 register_ioengine(&ioengine);
303 static void fio_exit fio_netio_unregister(void)
305 unregister_ioengine(&ioengine);