blob: 381c731c9bf3892c9261aabcb45bf0d5812f21a2 [file] [log] [blame]
Jens Axboeed92ac02007-02-06 14:43:52 +01001/*
Jens Axboed4f12dd2007-02-08 12:59:02 +01002 * Transfer data over the net.
Jens Axboeed92ac02007-02-06 14:43:52 +01003 */
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>
Jens Axboe5fdd1242007-02-11 04:00:37 +010012#include <sys/poll.h>
Jens Axboeed92ac02007-02-06 14:43:52 +010013
14#include "../fio.h"
15#include "../os.h"
16
Jens Axboe36167d82007-02-18 05:41:31 +010017#define send_to_net(td) ((td)->io_ops->priv)
Jens Axboeed92ac02007-02-06 14:43:52 +010018
19static int fio_netio_prep(struct thread_data *td, struct io_u *io_u)
20{
Jens Axboeed92ac02007-02-06 14:43:52 +010021 struct fio_file *f = io_u->file;
22
Jens Axboe7a6499d2007-02-07 09:35:29 +010023 /*
24 * Make sure we don't see spurious reads to a receiver, and vice versa
25 */
Jens Axboe36167d82007-02-18 05:41:31 +010026 if ((send_to_net(td) && io_u->ddir == DDIR_READ) ||
27 (!send_to_net(td) && io_u->ddir == DDIR_WRITE)) {
Jens Axboe7a6499d2007-02-07 09:35:29 +010028 td_verror(td, EINVAL);
29 return 1;
Jens Axboeed92ac02007-02-06 14:43:52 +010030 }
Jens Axboe7a6499d2007-02-07 09:35:29 +010031
Jens Axboeed92ac02007-02-06 14:43:52 +010032 if (io_u->ddir == DDIR_SYNC)
33 return 0;
34 if (io_u->offset == f->last_completed_pos)
35 return 0;
36
Jens Axboee01547d2007-02-06 19:16:01 +010037 /*
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 */
Jens Axboeed92ac02007-02-06 14:43:52 +010041 td_verror(td, EINVAL);
42 return 1;
43}
44
45static int fio_netio_queue(struct thread_data *td, struct io_u *io_u)
46{
Jens Axboeed92ac02007-02-06 14:43:52 +010047 struct fio_file *f = io_u->file;
Jens Axboed4f12dd2007-02-08 12:59:02 +010048 int ret, flags = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +010049
Jens Axboe7a6499d2007-02-07 09:35:29 +010050 if (io_u->ddir == DDIR_WRITE) {
Jens Axboe7a6499d2007-02-07 09:35:29 +010051 /*
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;
57
58 ret = send(f->fd, io_u->xfer_buf, io_u->xfer_buflen, flags);
Jens Axboed4f12dd2007-02-08 12:59:02 +010059 } 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
Jens Axboe7a6499d2007-02-07 09:35:29 +010063 ret = 0; /* must be a SYNC */
Jens Axboeed92ac02007-02-06 14:43:52 +010064
Jens Axboecec6b552007-02-06 20:15:38 +010065 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +010066 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010067 io_u->resid = io_u->xfer_buflen - ret;
68 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +010069 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010070 } else
71 io_u->error = errno;
72 }
73
Jens Axboe36167d82007-02-18 05:41:31 +010074 if (io_u->error)
Jens Axboe95bcd812007-02-11 01:01:57 +010075 td_verror(td, io_u->error);
Jens Axboeed92ac02007-02-06 14:43:52 +010076
Jens Axboe36167d82007-02-18 05:41:31 +010077 return FIO_Q_COMPLETED;
Jens Axboeed92ac02007-02-06 14:43:52 +010078}
79
80static int fio_netio_setup_connect(struct thread_data *td, const char *host,
Jens Axboee01547d2007-02-06 19:16:01 +010081 unsigned short port)
Jens Axboeed92ac02007-02-06 14:43:52 +010082{
83 struct sockaddr_in addr;
84 struct fio_file *f;
Jens Axboe2fc26982007-02-06 19:27:27 +010085 int i;
Jens Axboeed92ac02007-02-06 14:43:52 +010086
87 memset(&addr, 0, sizeof(addr));
88 addr.sin_family = AF_INET;
Jens Axboee01547d2007-02-06 19:16:01 +010089 addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +010090
91 if (inet_aton(host, &addr.sin_addr) != 1) {
Jens Axboe7a6499d2007-02-07 09:35:29 +010092 struct hostent *hent;
Jens Axboeed92ac02007-02-06 14:43:52 +010093
Jens Axboe7a6499d2007-02-07 09:35:29 +010094 hent = gethostbyname(host);
Jens Axboeed92ac02007-02-06 14:43:52 +010095 if (!hent) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +010096 td_verror(td, errno);
Jens Axboeed92ac02007-02-06 14:43:52 +010097 return 1;
98 }
99
100 memcpy(&addr.sin_addr, hent->h_addr, 4);
101 }
102
Jens Axboe2fc26982007-02-06 19:27:27 +0100103 for_each_file(td, f, i) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100104 f->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Jens Axboe2fc26982007-02-06 19:27:27 +0100105 if (f->fd < 0) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100106 td_verror(td, errno);
Jens Axboe2fc26982007-02-06 19:27:27 +0100107 return 1;
108 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100109
Jens Axboe2fc26982007-02-06 19:27:27 +0100110 if (connect(f->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100111 td_verror(td, errno);
Jens Axboe2fc26982007-02-06 19:27:27 +0100112 return 1;
113 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100114 }
115
116 return 0;
117
118}
119
Jens Axboe5fdd1242007-02-11 04:00:37 +0100120static 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
145 td_verror(td, errno);
146 break;
147 } else if (!ret)
148 continue;
149
Jens Axboe0c094422007-02-11 04:44:02 +0100150 /*
151 * should be impossible
152 */
153 if (!(pfd.revents & POLLIN))
154 continue;
155
Jens Axboe5fdd1242007-02-11 04:00:37 +0100156 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) {
162 td_verror(td, errno);
163 return 1;
164 }
165 accepts++;
166 break;
167 }
168 }
169
170 return 0;
171}
172
Jens Axboee01547d2007-02-06 19:16:01 +0100173static int fio_netio_setup_listen(struct thread_data *td, unsigned short port)
Jens Axboeed92ac02007-02-06 14:43:52 +0100174{
175 struct sockaddr_in addr;
Jens Axboe5fdd1242007-02-11 04:00:37 +0100176 int fd, opt;
Jens Axboeed92ac02007-02-06 14:43:52 +0100177
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100178 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
Jens Axboeed92ac02007-02-06 14:43:52 +0100179 if (fd < 0) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100180 td_verror(td, errno);
Jens Axboeed92ac02007-02-06 14:43:52 +0100181 return 1;
182 }
183
184 opt = 1;
185 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100186 td_verror(td, errno);
Jens Axboeed92ac02007-02-06 14:43:52 +0100187 return 1;
188 }
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100189#ifdef SO_REUSEPORT
190 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
191 td_verror(td, errno);
192 return 1;
193 }
194#endif
Jens Axboeed92ac02007-02-06 14:43:52 +0100195
196 memset(&addr, 0, sizeof(addr));
197 addr.sin_family = AF_INET;
198 addr.sin_addr.s_addr = htonl(INADDR_ANY);
Jens Axboee01547d2007-02-06 19:16:01 +0100199 addr.sin_port = htons(port);
Jens Axboeed92ac02007-02-06 14:43:52 +0100200
201 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100202 td_verror(td, errno);
Jens Axboeed92ac02007-02-06 14:43:52 +0100203 return 1;
204 }
205 if (listen(fd, 1) < 0) {
Jens Axboe6bedbfa2007-02-07 09:54:40 +0100206 td_verror(td, errno);
Jens Axboeed92ac02007-02-06 14:43:52 +0100207 return 1;
208 }
209
Jens Axboe5fdd1242007-02-11 04:00:37 +0100210 return fio_netio_accept_connections(td, fd, &addr);
Jens Axboeed92ac02007-02-06 14:43:52 +0100211}
212
213static int fio_netio_setup(struct thread_data *td)
214{
Jens Axboee01547d2007-02-06 19:16:01 +0100215 char host[64], buf[128];
Jens Axboee01547d2007-02-06 19:16:01 +0100216 unsigned short port;
Jens Axboe2fc26982007-02-06 19:27:27 +0100217 struct fio_file *f;
Jens Axboeed92ac02007-02-06 14:43:52 +0100218 char *sep;
Jens Axboe2fc26982007-02-06 19:27:27 +0100219 int ret, i;
Jens Axboeed92ac02007-02-06 14:43:52 +0100220
Jens Axboe7a6499d2007-02-07 09:35:29 +0100221 if (!td->total_file_size) {
222 log_err("fio: need size= set\n");
223 return 1;
224 }
225
Jens Axboeed92ac02007-02-06 14:43:52 +0100226 if (td->iomix) {
227 log_err("fio: network connections must be read OR write\n");
228 return 1;
229 }
Jens Axboeed92ac02007-02-06 14:43:52 +0100230
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);
Jens Axboee01547d2007-02-06 19:16:01 +0100242 port = atoi(sep);
Jens Axboeed92ac02007-02-06 14:43:52 +0100243
Jens Axboe85eb1d42007-02-09 09:30:25 +0100244 if (td->ddir == DDIR_READ) {
Jens Axboe36167d82007-02-18 05:41:31 +0100245 send_to_net(td) = 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100246 ret = fio_netio_setup_listen(td, port);
247 } else {
Jens Axboe36167d82007-02-18 05:41:31 +0100248 send_to_net(td) = 1;
Jens Axboeed92ac02007-02-06 14:43:52 +0100249 ret = fio_netio_setup_connect(td, host, port);
250 }
251
Jens Axboe2fc26982007-02-06 19:27:27 +0100252 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;
Jens Axboeed92ac02007-02-06 14:43:52 +0100261 }
262
Jens Axboe2fc26982007-02-06 19:27:27 +0100263 return 0;
Jens Axboeed92ac02007-02-06 14:43:52 +0100264}
265
Jens Axboeed92ac02007-02-06 14:43:52 +0100266static struct ioengine_ops ioengine = {
267 .name = "net",
268 .version = FIO_IOOPS_VERSION,
Jens Axboeed92ac02007-02-06 14:43:52 +0100269 .prep = fio_netio_prep,
270 .queue = fio_netio_queue,
Jens Axboeed92ac02007-02-06 14:43:52 +0100271 .setup = fio_netio_setup,
272 .flags = FIO_SYNCIO | FIO_NETIO,
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}