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_verror(td, errno);
131 memcpy(&addr.sin_addr, hent->h_addr, 4);
134 for_each_file(td, f, i) {
135 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
137 td_verror(td, errno);
141 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
142 td_verror(td, errno);
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, IPPROTO_TCP);
160 td_verror(td, errno);
165 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
166 td_verror(td, errno);
170 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
171 td_verror(td, errno);
176 memset(&addr, 0, sizeof(addr));
177 addr.sin_family = AF_INET;
178 addr.sin_addr.s_addr = htonl(INADDR_ANY);
179 addr.sin_port = htons(port);
181 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
182 td_verror(td, errno);
185 if (listen(fd, 1) < 0) {
186 td_verror(td, errno);
190 fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files);
192 socklen = sizeof(addr);
193 for_each_file(td, f, i) {
194 f->fd = accept(fd, (struct sockaddr *) &addr, &socklen);
196 td_verror(td, errno);
204 static int fio_netio_setup(struct thread_data *td)
206 char host[64], buf[128];
213 if (!td->total_file_size) {
214 log_err("fio: need size= set\n");
219 * work around for late init call
221 if (td->io_ops->init(td))
224 nd = td->io_ops->data;
227 log_err("fio: network connections must be read OR write\n");
231 strcpy(buf, td->filename);
233 sep = strchr(buf, ':');
235 log_err("fio: bad network host:port <<%s>>\n", td->filename);
244 if (td->ddir == READ) {
246 ret = fio_netio_setup_listen(td, port);
249 ret = fio_netio_setup_connect(td, host, port);
255 td->io_size = td->total_file_size;
256 td->total_io_size = td->io_size;
258 for_each_file(td, f, i) {
259 f->file_size = td->total_file_size / td->nr_files;
260 f->real_file_size = f->file_size;
266 static void fio_netio_cleanup(struct thread_data *td)
268 if (td->io_ops->data) {
269 free(td->io_ops->data);
270 td->io_ops->data = NULL;
274 static int fio_netio_init(struct thread_data *td)
279 * Hack to work-around the ->setup() function calling init on its
280 * own, since it needs ->io_ops->data to be set up.
282 if (td->io_ops->data)
285 nd = malloc(sizeof(*nd));
286 nd->last_io_u = NULL;
287 td->io_ops->data = nd;
291 static struct ioengine_ops ioengine = {
293 .version = FIO_IOOPS_VERSION,
294 .init = fio_netio_init,
295 .prep = fio_netio_prep,
296 .queue = fio_netio_queue,
297 .getevents = fio_netio_getevents,
298 .event = fio_netio_event,
299 .cleanup = fio_netio_cleanup,
300 .setup = fio_netio_setup,
301 .flags = FIO_SYNCIO | FIO_NETIO,
304 static void fio_init fio_netio_register(void)
306 register_ioengine(&ioengine);
309 static void fio_exit fio_netio_unregister(void)
311 unregister_ioengine(&ioengine);