blob: 44f5fa019372666da7aca019096f09e2670345e6 [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboe50d16972011-09-29 17:45:28 -060011#include <sys/mman.h>
Jens Axboe50d16972011-09-29 17:45:28 -060012#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <netdb.h>
15
16#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060017#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060018#include "crc/crc16.h"
Jens Axboe50d16972011-09-29 17:45:28 -060019
Jens Axboe132159a2011-09-30 15:01:32 -060020int fio_net_port = 8765;
Jens Axboe50d16972011-09-29 17:45:28 -060021
Jens Axboe009b1be2011-09-29 18:27:02 -060022int exit_backend = 0;
23
Jens Axboe132159a2011-09-30 15:01:32 -060024static char *job_buf;
25static unsigned int job_cur_len;
26static unsigned int job_max_len;
27
Jens Axboe37db14f2011-09-30 17:00:42 -060028static int server_fd;
29
Jens Axboe132159a2011-09-30 15:01:32 -060030int fio_send_data(int sk, const void *p, unsigned int len)
31{
32 do {
33 int ret = send(sk, p, len, 0);
34
35 if (ret > 0) {
36 len -= ret;
37 if (!len)
38 break;
39 p += ret;
40 continue;
41 } else if (!ret)
42 break;
43 else if (errno == EAGAIN || errno == EINTR)
44 continue;
45 } while (!exit_backend);
46
47 if (!len)
48 return 0;
49
50 return 1;
51}
52
53int fio_recv_data(int sk, void *p, unsigned int len)
54{
55 do {
56 int ret = recv(sk, p, len, MSG_WAITALL);
57
58 if (ret > 0) {
59 len -= ret;
60 if (!len)
61 break;
62 p += ret;
63 continue;
64 } else if (!ret)
65 break;
66 else if (errno == EAGAIN || errno == EINTR)
67 continue;
68 } while (!exit_backend);
69
70 if (!len)
71 return 0;
72
73 return -1;
74}
75
76static int verify_convert_cmd(struct fio_net_cmd *cmd)
77{
Jens Axboefcee5ff2011-09-30 22:50:39 -060078 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -060079
Jens Axboefcee5ff2011-09-30 22:50:39 -060080 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
81 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -060082
Jens Axboefcee5ff2011-09-30 22:50:39 -060083 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
84 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -060085 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -060086 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -060087 return 1;
88 }
89
90 cmd->version = le16_to_cpu(cmd->version);
91 cmd->opcode = le16_to_cpu(cmd->opcode);
92 cmd->flags = le32_to_cpu(cmd->flags);
93 cmd->serial = le64_to_cpu(cmd->serial);
94 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
95
96 switch (cmd->version) {
97 case FIO_SERVER_VER1:
98 break;
99 default:
100 log_err("fio: bad server cmd version %d\n", cmd->version);
101 return 1;
102 }
103
104 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
105 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
106 return 1;
107 }
108
109 return 0;
110}
111
Jens Axboe37db14f2011-09-30 17:00:42 -0600112struct fio_net_cmd *fio_net_cmd_read(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600113{
114 struct fio_net_cmd cmd, *ret = NULL;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600115 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600116
117 if (fio_recv_data(sk, &cmd, sizeof(cmd)))
118 return NULL;
119
120 /* We have a command, verify it and swap if need be */
121 if (verify_convert_cmd(&cmd))
122 return NULL;
123
124 /* Command checks out, alloc real command and fill in */
125 ret = malloc(sizeof(cmd) + cmd.pdu_len);
126 memcpy(ret, &cmd, sizeof(cmd));
127
128 if (!ret->pdu_len)
129 return ret;
130
131 /* There's payload, get it */
132 if (fio_recv_data(sk, (void *) ret + sizeof(*ret), ret->pdu_len)) {
133 free(ret);
134 return NULL;
135 }
136
137 /* Verify payload crc */
Jens Axboefcee5ff2011-09-30 22:50:39 -0600138 crc = crc16(ret->payload, ret->pdu_len);
139 if (crc != ret->pdu_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600140 log_err("fio: server bad crc on payload (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600141 ret->pdu_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600142 free(ret);
143 return NULL;
144 }
145
Jens Axboe132159a2011-09-30 15:01:32 -0600146 return ret;
147}
148
149void fio_net_cmd_crc(struct fio_net_cmd *cmd)
150{
151 uint32_t pdu_len;
152
Jens Axboefcee5ff2011-09-30 22:50:39 -0600153 cmd->cmd_crc16 = cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600154
155 pdu_len = le32_to_cpu(cmd->pdu_len);
156 if (pdu_len)
Jens Axboefcee5ff2011-09-30 22:50:39 -0600157 cmd->pdu_crc16 = cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600158}
159
160static int send_simple_command(int sk, uint16_t opcode, uint64_t serial)
161{
162 struct fio_net_cmd cmd = {
163 .version = cpu_to_le16(FIO_SERVER_VER1),
164 .opcode = cpu_to_le16(opcode),
165 .serial = cpu_to_le16(serial),
166 };
167
168 fio_net_cmd_crc(&cmd);
169
170 return fio_send_data(sk, &cmd, sizeof(cmd));
171}
172
173/*
174 * Send an ack for this command
175 */
176static int ack_command(int sk, struct fio_net_cmd *cmd)
177{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600178#if 0
Jens Axboe132159a2011-09-30 15:01:32 -0600179 return send_simple_command(sk, FIO_NET_CMD_ACK, cmd->serial);
Jens Axboefcee5ff2011-09-30 22:50:39 -0600180#else
181 return 0;
182#endif
Jens Axboe132159a2011-09-30 15:01:32 -0600183}
184
185#if 0
186static int nak_command(int sk, struct fio_net_cmd *cmd)
187{
188 return send_simple_command(sk, FIO_NET_CMD_NAK, cmd->serial);
189}
190#endif
191
192static int handle_cur_job(struct fio_net_cmd *cmd, int done)
193{
194 unsigned int left = job_max_len - job_cur_len;
195 int ret = 0;
196
197 if (left < cmd->pdu_len) {
198 job_buf = realloc(job_buf, job_max_len + 2 * cmd->pdu_len);
199 job_max_len += 2 * cmd->pdu_len;
200 }
201
202 memcpy(job_buf + job_cur_len, cmd->payload, cmd->pdu_len);
203 job_cur_len += cmd->pdu_len;
204
205 if (done) {
206 parse_jobs_ini(job_buf, 1, 0);
207 ret = exec_run();
208 reset_fio_state();
209 free(job_buf);
210 job_buf = NULL;
211 job_cur_len = job_max_len = 0;
212 }
213
214 return ret;
215}
216
217static int handle_command(struct fio_net_cmd *cmd)
218{
219 int ret;
220
221 switch (cmd->opcode) {
222 case FIO_NET_CMD_QUIT:
223 exit_backend = 1;
224 return 1;
225 case FIO_NET_CMD_ACK:
226 return 0;
227 case FIO_NET_CMD_NAK:
228 return 1;
229 case FIO_NET_CMD_JOB:
230 ret = handle_cur_job(cmd, 0);
231 break;
232 case FIO_NET_CMD_JOB_END:
233 ret = handle_cur_job(cmd, 1);
234 break;
235 default:
236 log_err("fio: unknown opcode: %d\n", cmd->opcode);
237 ret = 1;
238 }
239
240 return ret;
241}
242
243static int handle_connection(int sk)
244{
245 struct fio_net_cmd *cmd = NULL;
246 int ret = 0;
247
248 /* read forever */
249 while (!exit_backend) {
Jens Axboe37db14f2011-09-30 17:00:42 -0600250 cmd = fio_net_cmd_read(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600251 if (!cmd) {
252 ret = 1;
253 break;
254 }
255
256 ret = ack_command(sk, cmd);
257 if (ret)
258 break;
259
260 ret = handle_command(cmd);
261 if (ret)
262 break;
263
264 free(cmd);
265 }
266
267 if (cmd)
268 free(cmd);
269
270 return ret;
271}
272
Jens Axboe50d16972011-09-29 17:45:28 -0600273static int accept_loop(int listen_sk)
274{
275 struct sockaddr addr;
276 unsigned int len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600277 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600278 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600279
Jens Axboe009b1be2011-09-29 18:27:02 -0600280 flags = fcntl(listen_sk, F_GETFL);
281 flags |= O_NONBLOCK;
282 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600283again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600284 pfd.fd = listen_sk;
285 pfd.events = POLLIN;
286 do {
287 ret = poll(&pfd, 1, 100);
288 if (ret < 0) {
289 if (errno == EINTR)
290 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600291 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600292 goto out;
293 } else if (!ret)
294 continue;
295
296 if (pfd.revents & POLLIN)
297 break;
298 } while (!exit_backend);
299
300 if (exit_backend)
301 goto out;
302
Jens Axboe50d16972011-09-29 17:45:28 -0600303 sk = accept(listen_sk, &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600304 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600305 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600306 return -1;
307 }
308
Jens Axboe37db14f2011-09-30 17:00:42 -0600309 server_fd = sk;
310
Jens Axboe132159a2011-09-30 15:01:32 -0600311 exitval = handle_connection(sk);
Jens Axboe50d16972011-09-29 17:45:28 -0600312
Jens Axboe37db14f2011-09-30 17:00:42 -0600313 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600314 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600315
Jens Axboe009b1be2011-09-29 18:27:02 -0600316 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600317 goto again;
318
Jens Axboe009b1be2011-09-29 18:27:02 -0600319out:
Jens Axboe132159a2011-09-30 15:01:32 -0600320 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600321}
322
323int fio_server(void)
324{
325 struct sockaddr_in saddr_in;
326 struct sockaddr addr;
327 unsigned int len;
Jens Axboeafcf7752011-09-29 18:28:46 -0600328 int sk, opt, ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600329
330 sk = socket(AF_INET, SOCK_STREAM, 0);
331 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600332 log_err("fio: socket: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600333 return -1;
334 }
335
336 opt = 1;
337 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600338 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600339 return -1;
340 }
341#ifdef SO_REUSEPORT
342 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600343 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600344 return 1;
345 }
346#endif
347
348 saddr_in.sin_family = AF_INET;
349 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
Jens Axboe132159a2011-09-30 15:01:32 -0600350 saddr_in.sin_port = htons(fio_net_port);
Jens Axboe50d16972011-09-29 17:45:28 -0600351
352 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600353 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600354 return -1;
355 }
356
357 if (listen(sk, 1) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600358 log_err("fio: listen: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600359 return -1;
360 }
361
362 len = sizeof(addr);
363 if (getsockname(sk, &addr, &len) < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600364 log_err("fio: getsockname: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600365 return -1;
366 }
367
Jens Axboeafcf7752011-09-29 18:28:46 -0600368 ret = accept_loop(sk);
369 close(sk);
370 return ret;
Jens Axboe50d16972011-09-29 17:45:28 -0600371}
Jens Axboe37db14f2011-09-30 17:00:42 -0600372
Jens Axboe142575e2011-09-30 17:35:45 -0600373int fio_server_text_output(const char *buf, unsigned int len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600374{
375 struct fio_net_cmd *cmd;
Jens Axboe142575e2011-09-30 17:35:45 -0600376 int size = sizeof(*cmd) + len;
Jens Axboe37db14f2011-09-30 17:00:42 -0600377
Jens Axboe142575e2011-09-30 17:35:45 -0600378 cmd = malloc(size);
Jens Axboe4d8f8782011-09-30 22:24:17 -0600379 fio_init_net_cmd(cmd, FIO_NET_CMD_TEXT, buf, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600380 fio_net_cmd_crc(cmd);
381
Jens Axboe142575e2011-09-30 17:35:45 -0600382 fio_send_data(server_fd, cmd, size);
Jens Axboe37db14f2011-09-30 17:00:42 -0600383 free(cmd);
Jens Axboe142575e2011-09-30 17:35:45 -0600384 return size;
385}
386
387int fio_server_log(const char *format, ...)
388{
389 char buffer[1024];
390 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600391 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600392
393 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600394 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600395 va_end(args);
396
Jens Axboe82fa6b22011-09-30 22:29:08 -0600397 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600398}