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;
51 if (nd->send_to_net) {
52 if (io_u->ddir == DDIR_READ) {
53 td_verror(td, EINVAL);
57 if (io_u->ddir == DDIR_WRITE) {
58 td_verror(td, EINVAL);
63 if (io_u->ddir == DDIR_SYNC)
65 if (io_u->offset == f->last_completed_pos)
69 * If offset is different from last end position, it's a seek.
70 * As network io is purely sequential, we don't allow seeks.
72 td_verror(td, EINVAL);
76 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
78 struct net_data *nd = td->io_ops->data;
79 struct fio_file *f = io_u->file;
82 if (io_u->ddir == DDIR_WRITE)
83 ret = write(f->fd, io_u->buf, io_u->buflen);
84 else if (io_u->ddir == DDIR_READ)
85 ret = read(f->fd, io_u->buf, io_u->buflen);
87 if (ret != io_u->buflen) {
89 io_u->resid = io_u->buflen - ret;
101 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
104 struct sockaddr_in addr;
108 memset(&addr, 0, sizeof(addr));
109 addr.sin_family = AF_INET;
110 addr.sin_port = htons(port);
112 if (inet_aton(host, &addr.sin_addr) != 1) {
113 struct hostent *hent = gethostbyname(host);
116 td_vmsg(td, errno, "gethostbyname");
120 memcpy(&addr.sin_addr, hent->h_addr, 4);
123 for_each_file(td, f, i) {
124 f->fd = socket(AF_INET, SOCK_STREAM, 0);
126 td_vmsg(td, errno, "socket");
130 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
131 td_vmsg(td, errno, "connect");
140 static int fio_netio_setup_listen(struct thread_data *td, unsigned short port)
142 struct sockaddr_in addr;
147 fd = socket(AF_INET, SOCK_STREAM, 0);
149 td_vmsg(td, errno, "socket");
154 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
155 td_vmsg(td, errno, "setsockopt");
159 memset(&addr, 0, sizeof(addr));
160 addr.sin_family = AF_INET;
161 addr.sin_addr.s_addr = htonl(INADDR_ANY);
162 addr.sin_port = htons(port);
164 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
165 td_vmsg(td, errno, "bind");
168 if (listen(fd, 1) < 0) {
169 td_vmsg(td, errno, "listen");
173 fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files);
175 socklen = sizeof(addr);
176 for_each_file(td, f, i) {
177 f->fd = accept(fd, (struct sockaddr *) &addr, &socklen);
179 td_vmsg(td, errno, "accept");
187 static int fio_netio_setup(struct thread_data *td)
189 char host[64], buf[128];
197 * work around for late init call
199 if (td->io_ops->init(td))
202 nd = td->io_ops->data;
205 log_err("fio: network connections must be read OR write\n");
209 strcpy(buf, td->filename);
211 sep = strchr(buf, ':');
213 log_err("fio: bad network host:port <<%s>>\n", td->filename);
222 if (td->ddir == READ) {
224 ret = fio_netio_setup_listen(td, port);
227 ret = fio_netio_setup_connect(td, host, port);
233 td->io_size = td->total_file_size;
234 td->total_io_size = td->io_size;
236 for_each_file(td, f, i) {
237 f->file_size = td->total_file_size / td->nr_files;
238 f->real_file_size = f->file_size;
244 static void fio_netio_cleanup(struct thread_data *td)
246 if (td->io_ops->data) {
247 free(td->io_ops->data);
248 td->io_ops->data = NULL;
252 static int fio_netio_init(struct thread_data *td)
257 * Hack to work-around the ->setup() function calling init on its
258 * own, since it needs ->io_ops->data to be set up.
260 if (td->io_ops->data)
263 nd = malloc(sizeof(*nd));
264 nd->last_io_u = NULL;
265 td->io_ops->data = nd;
269 static struct ioengine_ops ioengine = {
271 .version = FIO_IOOPS_VERSION,
272 .init = fio_netio_init,
273 .prep = fio_netio_prep,
274 .queue = fio_netio_queue,
275 .getevents = fio_netio_getevents,
276 .event = fio_netio_event,
277 .cleanup = fio_netio_cleanup,
278 .setup = fio_netio_setup,
279 .flags = FIO_SYNCIO | FIO_NETIO,
282 static void fio_init fio_netio_register(void)
284 register_ioengine(&ioengine);
287 static void fio_exit fio_netio_unregister(void)
289 unregister_ioengine(&ioengine);