Accept -foo as well as --foo for options
[fio.git] / engines / net.c
CommitLineData
ed92ac0c 1/*
d4f12dd0 2 * Transfer data over the net.
ed92ac0c
JA
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <errno.h>
8#include <assert.h>
9#include <netinet/in.h>
10#include <arpa/inet.h>
11#include <netdb.h>
5fdd124a 12#include <sys/poll.h>
ed92ac0c
JA
13
14#include "../fio.h"
15#include "../os.h"
16
36167d82 17#define send_to_net(td) ((td)->io_ops->priv)
ed92ac0c
JA
18
19static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
20{
ed92ac0c
JA
21 struct fio_file *f = io_u->file;
22
7a6499da
JA
23 /*
24 * Make sure we don't see spurious reads to a receiver, and vice versa
25 */
36167d82
JA
26 if ((send_to_net(td) && io_u->ddir == DDIR_READ) ||
27 (!send_to_net(td) && io_u->ddir == DDIR_WRITE)) {
e1161c32 28 td_verror(td, EINVAL, "bad direction");
7a6499da 29 return 1;
ed92ac0c 30 }
7a6499da 31
ed92ac0c
JA
32 if (io_u->ddir == DDIR_SYNC)
33 return 0;
34 if (io_u->offset == f->last_completed_pos)
35 return 0;
36
e01547d2
JA
37 /*
38 * If offset is different from last end position, it's a seek.
39 * As network io is purely sequential, we don't allow seeks.
40 */
e1161c32 41 td_verror(td, EINVAL, "cannot seek");
ed92ac0c
JA
42 return 1;
43}
44
45static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
46{
ed92ac0c 47 struct fio_file *f = io_u->file;
d4f12dd0 48 int ret, flags = 0;
7a6499da
JA
49
50 if (io_u->ddir == DDIR_WRITE) {
7a6499da
JA
51 /*
52 * if we are going to write more, set MSG_MORE
53 */
54 if (td->this_io_bytes[DDIR_WRITE] + io_u->xfer_buflen <
55 td->io_size)
56 flags = MSG_MORE;
ed92ac0c 57
7a6499da 58 ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
d4f12dd0
JA
59 } else if (io_u->ddir == DDIR_READ) {
60 flags = MSG_WAITALL;
61 ret = recv(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
62 } else
7a6499da 63 ret = 0; /* must be a SYNC */
ed92ac0c 64
cec6b55d 65 if (ret != (int) io_u->xfer_buflen) {
22819ec2 66 if (ret >= 0) {
cec6b55d
JA
67 io_u->resid = io_u->xfer_buflen - ret;
68 io_u->error = 0;
36167d82 69 return FIO_Q_COMPLETED;
ed92ac0c
JA
70 } else
71 io_u->error = errno;
72 }
73
36167d82 74 if (io_u->error)
e1161c32 75 td_verror(td, io_u->error, "xfer");
ed92ac0c 76
36167d82 77 return FIO_Q_COMPLETED;
ed92ac0c
JA
78}
79
80static int fio_netio_setup_connect(struct thread_data *td, const char *host,
e01547d2 81 unsigned short port)
ed92ac0c
JA
82{
83 struct sockaddr_in addr;
84 struct fio_file *f;
2fc26981 85 int i;
ed92ac0c
JA
86
87 memset(&addr, 0, sizeof(addr));
88 addr.sin_family = AF_INET;
e01547d2 89 addr.sin_port = htons(port);
ed92ac0c
JA
90
91 if (inet_aton(host, &addr.sin_addr) != 1) {
7a6499da 92 struct hostent *hent;
ed92ac0c 93
7a6499da 94 hent = gethostbyname(host);
ed92ac0c 95 if (!hent) {
e1161c32 96 td_verror(td, errno, "gethostbyname");
ed92ac0c
JA
97 return 1;
98 }
99
100 memcpy(&addr.sin_addr, hent->h_addr, 4);
101 }
102
2fc26981 103 for_each_file(td, f, i) {
6bedbfaf 104 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
2fc26981 105 if (f->fd < 0) {
e1161c32 106 td_verror(td, errno, "socket");
2fc26981
JA
107 return 1;
108 }
ed92ac0c 109
2fc26981 110 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
e1161c32 111 td_verror(td, errno, "connect");
2fc26981
JA
112 return 1;
113 }
ed92ac0c
JA
114 }
115
116 return 0;
117
118}
119
5fdd124a
JA
120static int fio_netio_accept_connections(struct thread_data *td, int fd,
121 struct sockaddr_in *addr)
122{
123 socklen_t socklen = sizeof(*addr);
124 unsigned int accepts = 0;
125 struct pollfd pfd;
126
127 fprintf(f_out, "fio: waiting for %u connections\n", td->nr_files);
128
129 /*
130 * Accept loop. poll for incoming events, accept them. Repeat until we
131 * have all connections.
132 */
133 while (!td->terminate && accepts < td->nr_files) {
134 struct fio_file *f;
135 int ret, i;
136
137 pfd.fd = fd;
138 pfd.events = POLLIN;
139
140 ret = poll(&pfd, 1, -1);
141 if (ret < 0) {
142 if (errno == EINTR)
143 continue;
144
e1161c32 145 td_verror(td, errno, "poll");
5fdd124a
JA
146 break;
147 } else if (!ret)
148 continue;
149
0c09442b
JA
150 /*
151 * should be impossible
152 */
153 if (!(pfd.revents & POLLIN))
154 continue;
155
5fdd124a
JA
156 for_each_file(td, f, i) {
157 if (f->fd != -1)
158 continue;
159
160 f->fd = accept(fd, (struct sockaddr *) addr, &socklen);
161 if (f->fd < 0) {
e1161c32 162 td_verror(td, errno, "accept");
5fdd124a
JA
163 return 1;
164 }
165 accepts++;
166 break;
167 }
168 }
169
170 return 0;
171}
172
e01547d2 173static int fio_netio_setup_listen(struct thread_data *td, unsigned short port)
ed92ac0c
JA
174{
175 struct sockaddr_in addr;
5fdd124a 176 int fd, opt;
ed92ac0c 177
6bedbfaf 178 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ed92ac0c 179 if (fd < 0) {
e1161c32 180 td_verror(td, errno, "socket");
ed92ac0c
JA
181 return 1;
182 }
183
184 opt = 1;
185 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
e1161c32 186 td_verror(td, errno, "setsockopt");
ed92ac0c
JA
187 return 1;
188 }
6bedbfaf
JA
189#ifdef SO_REUSEPORT
190 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
e1161c32 191 td_verror(td, errno, "setsockopt");
6bedbfaf
JA
192 return 1;
193 }
194#endif
ed92ac0c
JA
195
196 memset(&addr, 0, sizeof(addr));
197 addr.sin_family = AF_INET;
198 addr.sin_addr.s_addr = htonl(INADDR_ANY);
e01547d2 199 addr.sin_port = htons(port);
ed92ac0c
JA
200
201 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
e1161c32 202 td_verror(td, errno, "bind");
ed92ac0c
JA
203 return 1;
204 }
205 if (listen(fd, 1) < 0) {
e1161c32 206 td_verror(td, errno, "listen");
ed92ac0c
JA
207 return 1;
208 }
209
5fdd124a 210 return fio_netio_accept_connections(td, fd, &addr);
ed92ac0c
JA
211}
212
213static int fio_netio_setup(struct thread_data *td)
214{
e01547d2 215 char host[64], buf[128];
e01547d2 216 unsigned short port;
2fc26981 217 struct fio_file *f;
ed92ac0c 218 char *sep;
2fc26981 219 int ret, i;
ed92ac0c 220
7a6499da
JA
221 if (!td->total_file_size) {
222 log_err("fio: need size= set\n");
223 return 1;
224 }
225
413dd459 226 if (td_rw(td)) {
ed92ac0c
JA
227 log_err("fio: network connections must be read OR write\n");
228 return 1;
229 }
ed92ac0c
JA
230
231 strcpy(buf, td->filename);
232
233 sep = strchr(buf, ':');
234 if (!sep) {
235 log_err("fio: bad network host:port <<%s>>\n", td->filename);
236 return 1;
237 }
238
239 *sep = '\0';
240 sep++;
241 strcpy(host, buf);
e01547d2 242 port = atoi(sep);
ed92ac0c 243
413dd459 244 if (td_read(td)) {
36167d82 245 send_to_net(td) = 0;
ed92ac0c
JA
246 ret = fio_netio_setup_listen(td, port);
247 } else {
36167d82 248 send_to_net(td) = 1;
ed92ac0c
JA
249 ret = fio_netio_setup_connect(td, host, port);
250 }
251
2fc26981
JA
252 if (ret)
253 return ret;
254
255 td->io_size = td->total_file_size;
256 td->total_io_size = td->io_size;
257
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;
ed92ac0c
JA
261 }
262
2fc26981 263 return 0;
ed92ac0c
JA
264}
265
ed92ac0c
JA
266static struct ioengine_ops ioengine = {
267 .name = "net",
268 .version = FIO_IOOPS_VERSION,
ed92ac0c
JA
269 .prep = fio_netio_prep,
270 .queue = fio_netio_queue,
ed92ac0c 271 .setup = fio_netio_setup,
a9defc9a 272 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_SELFOPEN,
ed92ac0c
JA
273};
274
275static void fio_init fio_netio_register(void)
276{
277 register_ioengine(&ioengine);
278}
279
280static void fio_exit fio_netio_unregister(void)
281{
282 unregister_ioengine(&ioengine);
283}