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)
68 td_verror(td, EINVAL);
72 static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
74 struct net_data *nd = td->io_ops->data;
75 struct fio_file *f = io_u->file;
78 if (io_u->ddir == DDIR_WRITE)
79 ret = write(f->fd, io_u->buf, io_u->buflen);
80 else if (io_u->ddir == DDIR_READ)
81 ret = read(f->fd, io_u->buf, io_u->buflen);
83 if (ret != io_u->buflen) {
85 io_u->resid = io_u->buflen - ret;
97 static int fio_netio_setup_connect(struct thread_data *td, const char *host,
100 struct sockaddr_in addr;
103 memset(&addr, 0, sizeof(addr));
104 addr.sin_family = AF_INET;
105 addr.sin_port = htons(atoi(port));
107 if (inet_aton(host, &addr.sin_addr) != 1) {
108 struct hostent *hent = gethostbyname(host);
111 td_vmsg(td, errno, "gethostbyname");
115 memcpy(&addr.sin_addr, hent->h_addr, 4);
120 f->fd = socket(AF_INET, SOCK_STREAM, 0);
122 td_vmsg(td, errno, "socket");
126 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
127 td_vmsg(td, errno, "connect");
135 static int fio_netio_setup_listen(struct thread_data *td, const char *port)
137 struct sockaddr_in addr;
141 fd = socket(AF_INET, SOCK_STREAM, 0);
143 td_vmsg(td, errno, "socket");
148 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
149 td_vmsg(td, errno, "setsockopt");
153 memset(&addr, 0, sizeof(addr));
154 addr.sin_family = AF_INET;
155 addr.sin_addr.s_addr = htonl(INADDR_ANY);
156 addr.sin_port = htons(atoi(port));
158 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
159 td_vmsg(td, errno, "bind");
162 if (listen(fd, 1) < 0) {
163 td_vmsg(td, errno, "listen");
167 socklen = sizeof(addr);
168 td->files[0].fd = accept(fd, (struct sockaddr *) &addr, &socklen);
169 if (td->files[0].fd < 0) {
170 td_vmsg(td, errno, "accept");
177 static int fio_netio_setup(struct thread_data *td)
179 char host[64], port[64], buf[128];
185 * work around for late init call
187 if (td->io_ops->init(td))
190 nd = td->io_ops->data;
193 log_err("fio: network connections must be read OR write\n");
196 if (td->nr_files > 1) {
197 log_err("fio: only one file supported for network\n");
201 strcpy(buf, td->filename);
203 sep = strchr(buf, ':');
205 log_err("fio: bad network host:port <<%s>>\n", td->filename);
214 if (td->ddir == READ) {
216 ret = fio_netio_setup_listen(td, port);
219 ret = fio_netio_setup_connect(td, host, port);
223 td->io_size = td->total_file_size;
224 td->total_io_size = td->io_size;
225 td->files[0].real_file_size = td->io_size;
231 static void fio_netio_cleanup(struct thread_data *td)
233 if (td->io_ops->data) {
234 free(td->io_ops->data);
235 td->io_ops->data = NULL;
239 static int fio_netio_init(struct thread_data *td)
243 if (td->io_ops->data)
246 nd = malloc(sizeof(*nd));
247 nd->last_io_u = NULL;
248 td->io_ops->data = nd;
252 static struct ioengine_ops ioengine = {
254 .version = FIO_IOOPS_VERSION,
255 .init = fio_netio_init,
256 .prep = fio_netio_prep,
257 .queue = fio_netio_queue,
258 .getevents = fio_netio_getevents,
259 .event = fio_netio_event,
260 .cleanup = fio_netio_cleanup,
261 .setup = fio_netio_setup,
262 .flags = FIO_SYNCIO | FIO_NETIO,
265 static void fio_init fio_netio_register(void)
267 register_ioengine(&ioengine);
270 static void fio_exit fio_netio_unregister(void)
272 unregister_ioengine(&ioengine);