engines: don't use printf or stderr directly
[fio.git] / server.c
CommitLineData
50d16976
JA
1#include <stdio.h>
2#include <stdlib.h>
142575e6 3#include <stdarg.h>
50d16976
JA
4#include <unistd.h>
5#include <limits.h>
50d16976 6#include <errno.h>
50d16976 7#include <sys/poll.h>
50d16976
JA
8#include <sys/types.h>
9#include <sys/wait.h>
d05c4a03 10#include <sys/socket.h>
87aa8f19
JA
11#include <sys/stat.h>
12#include <sys/un.h>
1f51bba9 13#include <sys/uio.h>
50d16976
JA
14#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
e46d8091 17#include <syslog.h>
9e22ecb0 18#include <signal.h>
3989b143 19#ifdef CONFIG_ZLIB
1b42725f 20#include <zlib.h>
3989b143 21#endif
50d16976
JA
22
23#include "fio.h"
132159a5 24#include "server.h"
fcee5ff6 25#include "crc/crc16.h"
c7c6cb4c 26#include "lib/ieee754.h"
89cf1480 27
5adc2447 28int fio_net_port = FIO_NET_PORT;
50d16976 29
009b1be4
JA
30int exit_backend = 0;
31
46c48f1f 32static int server_fd = -1;
87aa8f19
JA
33static char *fio_server_arg;
34static char *bind_sock;
35static struct sockaddr_in saddr_in;
811826be 36static struct sockaddr_in6 saddr_in6;
811826be 37static int use_ipv6;
3989b143
JA
38#ifdef CONFIG_ZLIB
39static unsigned int has_zlib = 1;
40#else
41static unsigned int has_zlib = 0;
42#endif
43static unsigned int use_zlib;
37db14fe 44
122c7725
JA
45struct fio_fork_item {
46 struct flist_head list;
47 int exitval;
48 int signal;
49 int exited;
50 pid_t pid;
51};
52
89c1707c
JA
53static const char *fio_server_ops[FIO_NET_CMD_NR] = {
54 "",
55 "QUIT",
56 "EXIT",
57 "JOB",
58 "JOBLINE",
59 "TEXT",
60 "TS",
61 "GS",
62 "SEND_ETA",
63 "ETA",
64 "PROBE",
65 "START",
d09a64a0
JA
66 "STOP",
67 "DISK_UTIL",
b9d2f30a 68 "SERVER_START",
807f9971 69 "ADD_JOB",
8d7b618a 70 "CMD_RUN",
c70e63e5 71 "CMD_IOLOG",
323255cc
JA
72 "CMD_UPDATE_JOB",
73 "CMD_LOAD_FILE",
89c1707c
JA
74};
75
76const char *fio_server_op(unsigned int op)
77{
78 static char buf[32];
79
80 if (op < FIO_NET_CMD_NR)
81 return fio_server_ops[op];
82
83 sprintf(buf, "UNKNOWN/%d", op);
84 return buf;
85}
86
5235f626 87static ssize_t iov_total_len(const struct iovec *iov, int count)
132159a5 88{
5235f626 89 ssize_t ret = 0;
794d69ca 90
5235f626
JA
91 while (count--) {
92 ret += iov->iov_len;
93 iov++;
94 }
95
96 return ret;
97}
132159a5 98
5235f626
JA
99static int fio_sendv_data(int sk, struct iovec *iov, int count)
100{
101 ssize_t total_len = iov_total_len(iov, count);
102 ssize_t ret;
103
104 do {
105 ret = writev(sk, iov, count);
132159a5 106 if (ret > 0) {
5235f626
JA
107 total_len -= ret;
108 if (!total_len)
132159a5 109 break;
5235f626
JA
110
111 while (ret) {
112 if (ret >= iov->iov_len) {
113 ret -= iov->iov_len;
114 iov++;
115 continue;
116 }
117 iov->iov_base += ret;
118 iov->iov_len -= ret;
119 ret = 0;
120 }
132159a5
JA
121 } else if (!ret)
122 break;
123 else if (errno == EAGAIN || errno == EINTR)
124 continue;
7b821684
JA
125 else
126 break;
132159a5
JA
127 } while (!exit_backend);
128
5235f626 129 if (!total_len)
132159a5
JA
130 return 0;
131
8b4e61b2
JA
132 if (errno)
133 return -errno;
134
132159a5
JA
135 return 1;
136}
137
5235f626
JA
138int fio_send_data(int sk, const void *p, unsigned int len)
139{
140 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
141
142 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
143
144 return fio_sendv_data(sk, &iov, 1);
145}
146
132159a5
JA
147int fio_recv_data(int sk, void *p, unsigned int len)
148{
149 do {
150 int ret = recv(sk, p, len, MSG_WAITALL);
151
152 if (ret > 0) {
153 len -= ret;
154 if (!len)
155 break;
156 p += ret;
157 continue;
158 } else if (!ret)
159 break;
160 else if (errno == EAGAIN || errno == EINTR)
161 continue;
7b821684
JA
162 else
163 break;
132159a5
JA
164 } while (!exit_backend);
165
166 if (!len)
167 return 0;
168
169 return -1;
170}
171
172static int verify_convert_cmd(struct fio_net_cmd *cmd)
173{
fcee5ff6 174 uint16_t crc;
132159a5 175
fcee5ff6
JA
176 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
177 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
132159a5 178
25dfa848 179 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
fcee5ff6 180 if (crc != cmd->cmd_crc16) {
132159a5 181 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
fcee5ff6 182 cmd->cmd_crc16, crc);
132159a5
JA
183 return 1;
184 }
185
186 cmd->version = le16_to_cpu(cmd->version);
187 cmd->opcode = le16_to_cpu(cmd->opcode);
188 cmd->flags = le32_to_cpu(cmd->flags);
af9c9fb3 189 cmd->tag = le64_to_cpu(cmd->tag);
132159a5
JA
190 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
191
192 switch (cmd->version) {
fa2ea806 193 case FIO_SERVER_VER:
132159a5
JA
194 break;
195 default:
196 log_err("fio: bad server cmd version %d\n", cmd->version);
197 return 1;
198 }
199
b9d2f30a 200 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
132159a5
JA
201 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
202 return 1;
203 }
204
205 return 0;
206}
207
a64e88da
JA
208/*
209 * Read (and defragment, if necessary) incoming commands
210 */
e951bdc4 211struct fio_net_cmd *fio_net_recv_cmd(int sk)
132159a5 212{
dfbf1f6f 213 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
a64e88da 214 size_t cmd_size = 0, pdu_offset = 0;
fcee5ff6 215 uint16_t crc;
a64e88da
JA
216 int ret, first = 1;
217 void *pdu = NULL;
132159a5 218
a64e88da
JA
219 do {
220 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
221 if (ret)
222 break;
132159a5 223
a64e88da
JA
224 /* We have a command, verify it and swap if need be */
225 ret = verify_convert_cmd(&cmd);
226 if (ret)
227 break;
132159a5 228
0b8f30a5
JA
229 if (first) {
230 /* if this is text, add room for \0 at the end */
231 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
232 assert(!cmdret);
233 } else
a64e88da 234 cmd_size += cmd.pdu_len;
132159a5 235
dfbf1f6f
JA
236 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
237 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
238 ret = 1;
239 break;
240 }
241
242 tmp = realloc(cmdret, cmd_size);
243 if (!tmp) {
244 log_err("fio: server failed allocating cmd\n");
245 ret = 1;
246 break;
247 }
248 cmdret = tmp;
132159a5 249
a64e88da
JA
250 if (first)
251 memcpy(cmdret, &cmd, sizeof(cmd));
67f15dcf
JA
252 else if (cmdret->opcode != cmd.opcode) {
253 log_err("fio: fragment opcode mismatch (%d != %d)\n",
254 cmdret->opcode, cmd.opcode);
255 ret = 1;
256 break;
257 }
a64e88da
JA
258
259 if (!cmd.pdu_len)
260 break;
261
262 /* There's payload, get it */
263 pdu = (void *) cmdret->payload + pdu_offset;
264 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
265 if (ret)
266 break;
267
268 /* Verify payload crc */
25dfa848 269 crc = fio_crc16(pdu, cmd.pdu_len);
a64e88da
JA
270 if (crc != cmd.pdu_crc16) {
271 log_err("fio: server bad crc on payload ");
272 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
273 ret = 1;
274 break;
275 }
276
277 pdu_offset += cmd.pdu_len;
817f06bb
JA
278 if (!first)
279 cmdret->pdu_len += cmd.pdu_len;
a64e88da
JA
280 first = 0;
281 } while (cmd.flags & FIO_NET_CMD_F_MORE);
132159a5 282
a64e88da
JA
283 if (ret) {
284 free(cmdret);
285 cmdret = NULL;
0b8f30a5
JA
286 } else if (cmdret) {
287 /* zero-terminate text input */
084d1c6f
JA
288 if (cmdret->pdu_len) {
289 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
290 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
291 char *buf = (char *) pdu->buf;
0b8f30a5 292
3c3ed070 293 buf[pdu->buf_len] = '\0';
084d1c6f 294 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
46bcd498
JA
295 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
296 char *buf = (char *) pdu->buf;
297 int len = le32_to_cpu(pdu->buf_len);
084d1c6f 298
46bcd498 299 buf[len] = '\0';
084d1c6f 300 }
0b8f30a5 301 }
084d1c6f 302
0b8f30a5 303 /* frag flag is internal */
a64e88da 304 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
0b8f30a5 305 }
a64e88da
JA
306
307 return cmdret;
132159a5
JA
308}
309
40c60516
JA
310static void add_reply(uint64_t tag, struct flist_head *list)
311{
a572bbfb 312 struct fio_net_cmd_reply *reply;
40c60516 313
a572bbfb 314 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
40c60516
JA
315 flist_add_tail(&reply->list, list);
316}
317
318static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
319{
320 struct fio_net_cmd_reply *reply;
321
322 reply = calloc(1, sizeof(*reply));
323 INIT_FLIST_HEAD(&reply->list);
324 gettimeofday(&reply->tv, NULL);
325 reply->saved_tag = tag;
326 reply->opcode = opcode;
327
328 return (uintptr_t) reply;
329}
330
331static void free_reply(uint64_t tag)
332{
a572bbfb 333 struct fio_net_cmd_reply *reply;
40c60516 334
a572bbfb 335 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
40c60516
JA
336 free(reply);
337}
338
53bd8dbc 339void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
132159a5
JA
340{
341 uint32_t pdu_len;
342
25dfa848 343 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
132159a5
JA
344
345 pdu_len = le32_to_cpu(cmd->pdu_len);
1b42725f
JA
346 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
347}
348
349void fio_net_cmd_crc(struct fio_net_cmd *cmd)
350{
351 fio_net_cmd_crc_pdu(cmd, cmd->payload);
132159a5
JA
352}
353
af9c9fb3 354int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
40c60516 355 uint64_t *tagptr, struct flist_head *list)
794d69ca 356{
7f868316
JA
357 struct fio_net_cmd *cmd = NULL;
358 size_t this_len, cur_len = 0;
40c60516 359 uint64_t tag;
794d69ca
JA
360 int ret;
361
40c60516
JA
362 if (list) {
363 assert(tagptr);
364 tag = *tagptr = alloc_reply(*tagptr, opcode);
365 } else
366 tag = tagptr ? *tagptr : 0;
367
794d69ca
JA
368 do {
369 this_len = size;
b9d2f30a
JA
370 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
371 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
794d69ca 372
7f868316
JA
373 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
374 if (cmd)
375 free(cmd);
376
377 cur_len = sizeof(*cmd) + this_len;
378 cmd = malloc(cur_len);
379 }
794d69ca 380
af9c9fb3 381 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
794d69ca
JA
382
383 if (this_len < size)
ddcc0b69 384 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
794d69ca
JA
385
386 fio_net_cmd_crc(cmd);
387
388 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
794d69ca
JA
389 size -= this_len;
390 buf += this_len;
391 } while (!ret && size);
392
40c60516
JA
393 if (list) {
394 if (ret)
395 free_reply(tag);
396 else
397 add_reply(tag, list);
398 }
399
7f868316
JA
400 if (cmd)
401 free(cmd);
402
794d69ca
JA
403 return ret;
404}
405
89c1707c 406static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
132159a5 407{
178cde9f 408 struct fio_net_cmd cmd;
132159a5 409
af9c9fb3 410 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
132159a5
JA
411 fio_net_cmd_crc(&cmd);
412
413 return fio_send_data(sk, &cmd, sizeof(cmd));
414}
415
89c1707c
JA
416/*
417 * If 'list' is non-NULL, then allocate and store the sent command for
418 * later verification.
419 */
420int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
421 struct flist_head *list)
422{
89c1707c
JA
423 int ret;
424
40c60516
JA
425 if (list)
426 tag = alloc_reply(tag, opcode);
89c1707c 427
40c60516 428 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
89c1707c 429 if (ret) {
40c60516
JA
430 if (list)
431 free_reply(tag);
432
89c1707c
JA
433 return ret;
434 }
435
40c60516
JA
436 if (list)
437 add_reply(tag, list);
438
89c1707c
JA
439 return 0;
440}
441
122c7725 442int fio_net_send_quit(int sk)
437377e1 443{
46c48f1f 444 dprint(FD_NET, "server: sending quit\n");
122c7725 445
8c95307b 446 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
437377e1
JA
447}
448
f58bd2a4
JA
449static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
450 int signal)
132159a5 451{
11e950bd 452 struct cmd_end_pdu epdu;
f58bd2a4 453 uint64_t tag = 0;
122c7725 454
f58bd2a4
JA
455 if (cmd)
456 tag = cmd->tag;
122c7725
JA
457
458 epdu.error = __cpu_to_le32(error);
459 epdu.signal = __cpu_to_le32(signal);
40c60516 460 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
f58bd2a4
JA
461}
462
463int fio_net_send_stop(int sk, int error, int signal)
464{
465 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
466 return fio_net_send_ack(sk, NULL, error, signal);
122c7725
JA
467}
468
469static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
470{
471 struct fio_fork_item *ffi;
472
473 ffi = malloc(sizeof(*ffi));
474 ffi->exitval = 0;
475 ffi->signal = 0;
476 ffi->exited = 0;
477 ffi->pid = pid;
478 flist_add_tail(&ffi->list, list);
479}
480
6348b5de 481static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
122c7725 482{
a7533dbd 483 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
6348b5de 484 fio_server_add_fork_item(pid, conn_list);
122c7725
JA
485}
486
6348b5de 487static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
122c7725 488{
a7533dbd 489 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
6348b5de 490 fio_server_add_fork_item(pid, job_list);
122c7725
JA
491}
492
493static void fio_server_check_fork_item(struct fio_fork_item *ffi)
494{
495 int ret, status;
496
497 ret = waitpid(ffi->pid, &status, WNOHANG);
498 if (ret < 0) {
499 if (errno == ECHILD) {
a7533dbd 500 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
122c7725
JA
501 ffi->exited = 1;
502 } else
503 log_err("fio: waitpid: %s\n", strerror(errno));
504 } else if (ret == ffi->pid) {
505 if (WIFSIGNALED(status)) {
506 ffi->signal = WTERMSIG(status);
507 ffi->exited = 1;
508 }
509 if (WIFEXITED(status)) {
510 if (WEXITSTATUS(status))
511 ffi->exitval = WEXITSTATUS(status);
512 ffi->exited = 1;
513 }
514 }
515}
516
517static void fio_server_fork_item_done(struct fio_fork_item *ffi)
518{
a7533dbd 519 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
122c7725
JA
520
521 /*
522 * Fold STOP and QUIT...
523 */
524 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
525 fio_net_send_quit(server_fd);
526 flist_del(&ffi->list);
527 free(ffi);
528}
529
54163255 530static void fio_server_check_fork_items(struct flist_head *list)
122c7725
JA
531{
532 struct flist_head *entry, *tmp;
533 struct fio_fork_item *ffi;
534
535 flist_for_each_safe(entry, tmp, list) {
536 ffi = flist_entry(entry, struct fio_fork_item, list);
537
538 fio_server_check_fork_item(ffi);
539
540 if (ffi->exited)
541 fio_server_fork_item_done(ffi);
542 }
543}
544
6348b5de 545static void fio_server_check_jobs(struct flist_head *job_list)
122c7725 546{
6348b5de 547 fio_server_check_fork_items(job_list);
122c7725
JA
548}
549
6348b5de 550static void fio_server_check_conns(struct flist_head *conn_list)
122c7725 551{
6348b5de 552 fio_server_check_fork_items(conn_list);
122c7725
JA
553}
554
323255cc
JA
555static int handle_load_file_cmd(struct fio_net_cmd *cmd)
556{
557 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
558 void *file_name = pdu->file;
559 struct cmd_start_pdu spdu;
560
561 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
562
563 pdu->name_len = le16_to_cpu(pdu->name_len);
564 pdu->client_type = le16_to_cpu(pdu->client_type);
565
566 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
567 fio_net_send_quit(server_fd);
568 return -1;
569 }
570
571 spdu.jobs = cpu_to_le32(thread_number);
572 spdu.stat_outputs = cpu_to_le32(stat_number);
573 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
574 return 0;
575}
576
6348b5de 577static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd)
122c7725
JA
578{
579 pid_t pid;
a64e88da 580 int ret;
132159a5 581
cbb1303c 582 fio_time_init();
122c7725 583 set_genesis_time();
11e950bd 584
122c7725
JA
585 pid = fork();
586 if (pid) {
6348b5de 587 fio_server_add_job_pid(job_list, pid);
122c7725
JA
588 return 0;
589 }
11e950bd 590
122c7725 591 ret = fio_backend();
2bb3f0a7 592 free_threads_shm();
122c7725 593 _exit(ret);
81179eec
JA
594}
595
b9d2f30a
JA
596static int handle_job_cmd(struct fio_net_cmd *cmd)
597{
46bcd498
JA
598 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
599 void *buf = pdu->buf;
b9d2f30a
JA
600 struct cmd_start_pdu spdu;
601
46bcd498
JA
602 pdu->buf_len = le32_to_cpu(pdu->buf_len);
603 pdu->client_type = le32_to_cpu(pdu->client_type);
604
605 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
122c7725 606 fio_net_send_quit(server_fd);
b9d2f30a
JA
607 return -1;
608 }
609
610 spdu.jobs = cpu_to_le32(thread_number);
108fea77 611 spdu.stat_outputs = cpu_to_le32(stat_number);
40c60516 612 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
b9d2f30a
JA
613 return 0;
614}
615
81179eec
JA
616static int handle_jobline_cmd(struct fio_net_cmd *cmd)
617{
fa2ea806
JA
618 void *pdu = cmd->payload;
619 struct cmd_single_line_pdu *cslp;
620 struct cmd_line_pdu *clp;
621 unsigned long offset;
b9d2f30a 622 struct cmd_start_pdu spdu;
fa2ea806 623 char **argv;
b9d2f30a 624 int i;
81179eec 625
fa2ea806
JA
626 clp = pdu;
627 clp->lines = le16_to_cpu(clp->lines);
46bcd498 628 clp->client_type = le16_to_cpu(clp->client_type);
fa2ea806
JA
629 argv = malloc(clp->lines * sizeof(char *));
630 offset = sizeof(*clp);
81179eec 631
fa2ea806 632 dprint(FD_NET, "server: %d command line args\n", clp->lines);
39e8e016 633
fa2ea806
JA
634 for (i = 0; i < clp->lines; i++) {
635 cslp = pdu + offset;
636 argv[i] = (char *) cslp->text;
637
638 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
39e8e016
JA
639 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
640 }
81179eec 641
46bcd498 642 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
122c7725 643 fio_net_send_quit(server_fd);
fa2ea806 644 free(argv);
81179eec 645 return -1;
e6d1c668 646 }
81179eec 647
fa2ea806
JA
648 free(argv);
649
b9d2f30a 650 spdu.jobs = cpu_to_le32(thread_number);
1e5324e7 651 spdu.stat_outputs = cpu_to_le32(stat_number);
40c60516 652 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
b9d2f30a 653 return 0;
132159a5
JA
654}
655
c28e8e8c
JA
656static int handle_probe_cmd(struct fio_net_cmd *cmd)
657{
3989b143
JA
658 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
659 struct cmd_probe_reply_pdu probe;
40c60516 660 uint64_t tag = cmd->tag;
c28e8e8c 661
89c1707c
JA
662 dprint(FD_NET, "server: sending probe reply\n");
663
c28e8e8c
JA
664 memset(&probe, 0, sizeof(probe));
665 gethostname((char *) probe.hostname, sizeof(probe.hostname));
0dcebdf4 666#ifdef CONFIG_BIG_ENDIAN
6eb24791
JA
667 probe.bigendian = 1;
668#endif
750db473 669 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
c28e8e8c 670
cca84643
JA
671 probe.os = FIO_OS;
672 probe.arch = FIO_ARCH;
38fdef22 673 probe.bpp = sizeof(void *);
d31e26d0 674 probe.cpus = __cpu_to_le32(cpus_online());
3989b143
JA
675
676 /*
677 * If the client supports compression and we do too, then enable it
678 */
679 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
680 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
681 use_zlib = 1;
682 } else {
683 probe.flags = 0;
684 use_zlib = 0;
685 }
38fdef22 686
40c60516 687 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
af9c9fb3
JA
688}
689
690static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
691{
692 struct jobs_eta *je;
40c60516 693 uint64_t tag = cmd->tag;
723297c9 694 size_t size;
af9c9fb3
JA
695 int i;
696
723297c9
JA
697 je = get_jobs_eta(1, &size);
698 if (!je)
af9c9fb3 699 return 0;
af9c9fb3
JA
700
701 dprint(FD_NET, "server sending status\n");
702
703 je->nr_running = cpu_to_le32(je->nr_running);
704 je->nr_ramp = cpu_to_le32(je->nr_ramp);
705 je->nr_pending = cpu_to_le32(je->nr_pending);
714e85f3 706 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
af9c9fb3 707 je->files_open = cpu_to_le32(je->files_open);
af9c9fb3 708
298921d6 709 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
3e47bd25
JA
710 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
711 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
712 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
713 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
16725bb0
JA
714 je->rate[i] = cpu_to_le32(je->rate[i]);
715 je->iops[i] = cpu_to_le32(je->iops[i]);
af9c9fb3
JA
716 }
717
c1970b90 718 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
af9c9fb3 719 je->eta_sec = cpu_to_le64(je->eta_sec);
8c621fb2 720 je->nr_threads = cpu_to_le32(je->nr_threads);
b7f05eb0 721 je->is_pow2 = cpu_to_le32(je->is_pow2);
ed2c0a12 722 je->unit_base = cpu_to_le32(je->unit_base);
af9c9fb3 723
40c60516 724 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
af9c9fb3
JA
725 free(je);
726 return 0;
c28e8e8c
JA
727}
728
40c60516
JA
729static int send_update_job_reply(int fd, uint64_t __tag, int error)
730{
731 uint64_t tag = __tag;
732 uint32_t pdu_error;
733
734 pdu_error = __cpu_to_le32(error);
735 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
736}
737
f58bd2a4
JA
738static int handle_update_job_cmd(struct fio_net_cmd *cmd)
739{
740 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
741 struct thread_data *td;
742 uint32_t tnumber;
743
744 tnumber = le32_to_cpu(pdu->thread_number);
745
746 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
747
30ffacbf 748 if (!tnumber || tnumber > thread_number) {
40c60516 749 send_update_job_reply(server_fd, cmd->tag, ENODEV);
f58bd2a4
JA
750 return 0;
751 }
752
30ffacbf 753 td = &threads[tnumber - 1];
f58bd2a4 754 convert_thread_options_to_cpu(&td->o, &pdu->top);
40c60516 755 send_update_job_reply(server_fd, cmd->tag, 0);
f58bd2a4
JA
756 return 0;
757}
758
6348b5de 759static int handle_command(struct flist_head *job_list, struct fio_net_cmd *cmd)
132159a5
JA
760{
761 int ret;
762
4b91ee8f
JA
763 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
764 fio_server_op(cmd->opcode), cmd->pdu_len,
765 (unsigned long long) cmd->tag);
46c48f1f 766
132159a5
JA
767 switch (cmd->opcode) {
768 case FIO_NET_CMD_QUIT:
cc0df00a 769 fio_terminate_threads(TERMINATE_ALL);
c28e8e8c 770 return -1;
d7959186 771 case FIO_NET_CMD_EXIT:
132159a5 772 exit_backend = 1;
c28e8e8c 773 return -1;
323255cc
JA
774 case FIO_NET_CMD_LOAD_FILE:
775 ret = handle_load_file_cmd(cmd);
776 break;
132159a5 777 case FIO_NET_CMD_JOB:
0b8f30a5 778 ret = handle_job_cmd(cmd);
132159a5 779 break;
81179eec
JA
780 case FIO_NET_CMD_JOBLINE:
781 ret = handle_jobline_cmd(cmd);
782 break;
c28e8e8c
JA
783 case FIO_NET_CMD_PROBE:
784 ret = handle_probe_cmd(cmd);
785 break;
af9c9fb3
JA
786 case FIO_NET_CMD_SEND_ETA:
787 ret = handle_send_eta_cmd(cmd);
788 break;
b9d2f30a 789 case FIO_NET_CMD_RUN:
6348b5de 790 ret = handle_run_cmd(job_list, cmd);
b9d2f30a 791 break;
f58bd2a4
JA
792 case FIO_NET_CMD_UPDATE_JOB:
793 ret = handle_update_job_cmd(cmd);
794 break;
132159a5 795 default:
3c3ed070 796 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
132159a5
JA
797 ret = 1;
798 }
799
800 return ret;
801}
802
122c7725 803static int handle_connection(int sk)
132159a5
JA
804{
805 struct fio_net_cmd *cmd = NULL;
6348b5de 806 FLIST_HEAD(job_list);
132159a5
JA
807 int ret = 0;
808
122c7725 809 reset_fio_state();
122c7725
JA
810 server_fd = sk;
811
132159a5
JA
812 /* read forever */
813 while (!exit_backend) {
e951bdc4
JA
814 struct pollfd pfd = {
815 .fd = sk,
816 .events = POLLIN,
817 };
818
819 ret = 0;
820 do {
54163255
JA
821 int timeout = 1000;
822
823 if (!flist_empty(&job_list))
824 timeout = 100;
3c3ed070 825
54163255 826 ret = poll(&pfd, 1, timeout);
e951bdc4
JA
827 if (ret < 0) {
828 if (errno == EINTR)
829 break;
830 log_err("fio: poll: %s\n", strerror(errno));
831 break;
19c65179 832 } else if (!ret) {
6348b5de 833 fio_server_check_jobs(&job_list);
e951bdc4 834 continue;
19c65179 835 }
e951bdc4
JA
836
837 if (pfd.revents & POLLIN)
838 break;
839 if (pfd.revents & (POLLERR|POLLHUP)) {
840 ret = 1;
841 break;
842 }
19c65179 843 } while (!exit_backend);
e951bdc4 844
6348b5de 845 fio_server_check_jobs(&job_list);
122c7725 846
e951bdc4
JA
847 if (ret < 0)
848 break;
849
850 cmd = fio_net_recv_cmd(sk);
132159a5 851 if (!cmd) {
c28e8e8c 852 ret = -1;
132159a5
JA
853 break;
854 }
855
6348b5de 856 ret = handle_command(&job_list, cmd);
132159a5
JA
857 if (ret)
858 break;
859
860 free(cmd);
c77a99e7 861 cmd = NULL;
132159a5
JA
862 }
863
864 if (cmd)
865 free(cmd);
866
122c7725
JA
867 close(sk);
868 _exit(ret);
cc0df00a
JA
869}
870
50d16976
JA
871static int accept_loop(int listen_sk)
872{
bb447a27 873 struct sockaddr_in addr;
479471c4
JA
874 struct sockaddr_in6 addr6;
875 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
009b1be4 876 struct pollfd pfd;
4a851614 877 int ret = 0, sk, exitval = 0;
6348b5de 878 FLIST_HEAD(conn_list);
50d16976 879
60efd14e
JA
880 dprint(FD_NET, "server enter accept loop\n");
881
4a851614 882 fio_set_fd_nonblocking(listen_sk, "server");
009b1be4 883
122c7725 884 while (!exit_backend) {
479471c4
JA
885 const char *from;
886 char buf[64];
122c7725 887 pid_t pid;
009b1be4 888
122c7725
JA
889 pfd.fd = listen_sk;
890 pfd.events = POLLIN;
891 do {
54163255
JA
892 int timeout = 1000;
893
894 if (!flist_empty(&conn_list))
895 timeout = 100;
896
897 ret = poll(&pfd, 1, timeout);
122c7725
JA
898 if (ret < 0) {
899 if (errno == EINTR)
900 break;
901 log_err("fio: poll: %s\n", strerror(errno));
902 break;
903 } else if (!ret) {
6348b5de 904 fio_server_check_conns(&conn_list);
122c7725
JA
905 continue;
906 }
009b1be4 907
122c7725
JA
908 if (pfd.revents & POLLIN)
909 break;
910 } while (!exit_backend);
50d16976 911
6348b5de 912 fio_server_check_conns(&conn_list);
46c48f1f 913
122c7725
JA
914 if (exit_backend || ret < 0)
915 break;
916
479471c4
JA
917 if (use_ipv6)
918 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
919 else
920 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
921
122c7725
JA
922 if (sk < 0) {
923 log_err("fio: accept: %s\n", strerror(errno));
924 return -1;
925 }
37db14fe 926
479471c4
JA
927 if (use_ipv6)
928 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
929 else
930 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
931
932 dprint(FD_NET, "server: connect from %s\n", from);
50d16976 933
122c7725
JA
934 pid = fork();
935 if (pid) {
936 close(sk);
6348b5de 937 fio_server_add_conn_pid(&conn_list, pid);
122c7725
JA
938 continue;
939 }
5c341e9a 940
122c7725
JA
941 /* exits */
942 handle_connection(sk);
943 }
5c341e9a 944
132159a5 945 return exitval;
50d16976
JA
946}
947
084d1c6f 948int fio_server_text_output(int level, const char *buf, size_t len)
37db14fe 949{
084d1c6f
JA
950 struct cmd_text_pdu *pdu;
951 unsigned int tlen;
952 struct timeval tv;
953
954 if (server_fd == -1)
955 return log_local_buf(buf, len);
956
957 tlen = sizeof(*pdu) + len;
958 pdu = malloc(tlen);
959
960 pdu->level = __cpu_to_le32(level);
961 pdu->buf_len = __cpu_to_le32(len);
962
963 gettimeofday(&tv, NULL);
964 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
965 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
337d75a8 966
084d1c6f
JA
967 memcpy(pdu->buf, buf, len);
968
40c60516 969 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
084d1c6f
JA
970 free(pdu);
971 return len;
142575e6
JA
972}
973
a64e88da
JA
974static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
975{
976 dst->max_val = cpu_to_le64(src->max_val);
977 dst->min_val = cpu_to_le64(src->min_val);
978 dst->samples = cpu_to_le64(src->samples);
802ad4a8
JA
979
980 /*
981 * Encode to IEEE 754 for network transfer
982 */
983 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
984 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
a64e88da
JA
985}
986
987static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
988{
989 int i;
990
298921d6 991 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
992 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
993 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
994 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
995 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
996 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
997 dst->agg[i] = cpu_to_le64(src->agg[i]);
998 }
999
1000 dst->kb_base = cpu_to_le32(src->kb_base);
ad705bcb 1001 dst->unit_base = cpu_to_le32(src->unit_base);
a64e88da 1002 dst->groupid = cpu_to_le32(src->groupid);
771e58be 1003 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
a64e88da
JA
1004}
1005
1006/*
1007 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1008 * into a single payload.
1009 */
1010void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1011{
1012 struct cmd_ts_pdu p;
1013 int i, j;
1014
60efd14e
JA
1015 dprint(FD_NET, "server sending end stats\n");
1016
317b3c8b
JA
1017 memset(&p, 0, sizeof(p));
1018
4e59d0f3
JA
1019 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1020 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1021 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
a64e88da 1022
2f122b13
JA
1023 p.ts.error = cpu_to_le32(ts->error);
1024 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1025 p.ts.groupid = cpu_to_le32(ts->groupid);
1026 p.ts.pid = cpu_to_le32(ts->pid);
1027 p.ts.members = cpu_to_le32(ts->members);
771e58be 1028 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
a64e88da 1029
298921d6 1030 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
1031 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1032 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1033 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1034 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1035 }
1036
1037 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1038 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1039 p.ts.ctx = cpu_to_le64(ts->ctx);
1040 p.ts.minf = cpu_to_le64(ts->minf);
1041 p.ts.majf = cpu_to_le64(ts->majf);
1042 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
802ad4a8
JA
1043
1044 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
cfc03e46
JA
1045 fio_fp64_t *src = &ts->percentile_list[i];
1046 fio_fp64_t *dst = &p.ts.percentile_list[i];
802ad4a8 1047
cfc03e46 1048 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
802ad4a8 1049 }
a64e88da
JA
1050
1051 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1052 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1053 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1054 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1055 }
1056
1057 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1058 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1059 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1060 }
1061
298921d6 1062 for (i = 0; i < DDIR_RWDIR_CNT; i++)
a64e88da
JA
1063 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1064 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1065
78799deb 1066 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da 1067 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
93eee04a 1068 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
3bcb9d94 1069 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
a64e88da
JA
1070 }
1071
93eee04a 1072 p.ts.total_submit = cpu_to_le64(ts->total_submit);
a64e88da
JA
1073 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1074
298921d6 1075 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
a64e88da
JA
1076 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1077 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1078 }
1079
1080 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1081 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1082 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
ddcc0b69
JA
1083 p.ts.first_error = cpu_to_le32(ts->first_error);
1084 p.ts.kb_base = cpu_to_le32(ts->kb_base);
ad705bcb 1085 p.ts.unit_base = cpu_to_le32(ts->unit_base);
a64e88da 1086
3e260a46
JA
1087 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1088 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1089 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1090 p.ts.latency_percentile.u.i = __cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1091
a64e88da
JA
1092 convert_gs(&p.rs, rs);
1093
40c60516 1094 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
a64e88da
JA
1095}
1096
1097void fio_server_send_gs(struct group_run_stats *rs)
1098{
1099 struct group_run_stats gs;
1100
60efd14e
JA
1101 dprint(FD_NET, "server sending group run stats\n");
1102
a64e88da 1103 convert_gs(&gs, rs);
40c60516 1104 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
cf451d1e
JA
1105}
1106
d09a64a0
JA
1107static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1108{
1109 int i;
1110
1111 for (i = 0; i < 2; i++) {
1112 dst->ios[i] = cpu_to_le32(src->ios[i]);
1113 dst->merges[i] = cpu_to_le32(src->merges[i]);
1114 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1115 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1116 }
1117
1118 dst->io_ticks = cpu_to_le32(src->io_ticks);
1119 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1120 dst->slavecount = cpu_to_le32(src->slavecount);
1121 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1122}
1123
1124static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1125{
1126 int i;
1127
59140421
JA
1128 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1129 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
d09a64a0
JA
1130
1131 for (i = 0; i < 2; i++) {
a3b4cf7d
JA
1132 dst->s.ios[i] = cpu_to_le32(src->s.ios[i]);
1133 dst->s.merges[i] = cpu_to_le32(src->s.merges[i]);
1134 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1135 dst->s.ticks[i] = cpu_to_le32(src->s.ticks[i]);
d09a64a0
JA
1136 }
1137
a3b4cf7d
JA
1138 dst->s.io_ticks = cpu_to_le32(src->s.io_ticks);
1139 dst->s.time_in_queue = cpu_to_le32(src->s.time_in_queue);
1140 dst->s.msec = cpu_to_le64(src->s.msec);
d09a64a0
JA
1141}
1142
1143void fio_server_send_du(void)
1144{
1145 struct disk_util *du;
1146 struct flist_head *entry;
1147 struct cmd_du_pdu pdu;
1148
1149 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1150
0766d92e
JA
1151 memset(&pdu, 0, sizeof(pdu));
1152
d09a64a0
JA
1153 flist_for_each(entry, &disk_list) {
1154 du = flist_entry(entry, struct disk_util, list);
1155
1156 convert_dus(&pdu.dus, &du->dus);
1157 convert_agg(&pdu.agg, &du->agg);
1158
40c60516 1159 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
d09a64a0
JA
1160 }
1161}
1162
53bd8dbc
JA
1163/*
1164 * Send a command with a separate PDU, not inlined in the command
1165 */
1166static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1167 off_t size, uint64_t tag, uint32_t flags)
1168{
1169 struct fio_net_cmd cmd;
1170 struct iovec iov[2];
1171
9f247261 1172 iov[0].iov_base = (void *) &cmd;
53bd8dbc
JA
1173 iov[0].iov_len = sizeof(cmd);
1174 iov[1].iov_base = (void *) buf;
1175 iov[1].iov_len = size;
1176
1177 __fio_init_net_cmd(&cmd, opcode, size, tag);
1178 cmd.flags = __cpu_to_le32(flags);
1179 fio_net_cmd_crc_pdu(&cmd, buf);
1180
8c95307b 1181 return fio_sendv_data(sk, iov, 2);
53bd8dbc
JA
1182}
1183
3989b143 1184static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
1b42725f 1185{
3989b143
JA
1186 int ret = 0;
1187#ifdef CONFIG_ZLIB
1b42725f
JA
1188 z_stream stream;
1189 void *out_pdu;
1b42725f
JA
1190
1191 /*
1192 * Dirty - since the log is potentially huge, compress it into
1193 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1194 * side defragment it.
1195 */
1196 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1197
1198 stream.zalloc = Z_NULL;
1199 stream.zfree = Z_NULL;
1200 stream.opaque = Z_NULL;
1201
1202 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
53bd8dbc
JA
1203 ret = 1;
1204 goto err;
1b42725f
JA
1205 }
1206
f5ed765a 1207 stream.next_in = (void *) log->log;
ae588852 1208 stream.avail_in = log->nr_samples * log_entry_sz(log);
1b42725f
JA
1209
1210 do {
53bd8dbc
JA
1211 unsigned int this_len, flags = 0;
1212 int ret;
1b42725f
JA
1213
1214 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1215 stream.next_out = out_pdu;
3c547fe0
JA
1216 ret = deflate(&stream, Z_FINISH);
1217 /* may be Z_OK, or Z_STREAM_END */
1218 if (ret < 0)
1219 goto err_zlib;
1b42725f
JA
1220
1221 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1222
1b42725f 1223 if (stream.avail_in)
53bd8dbc 1224 flags = FIO_NET_CMD_F_MORE;
1b42725f 1225
53bd8dbc
JA
1226 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1227 out_pdu, this_len, 0, flags);
1228 if (ret)
1229 goto err_zlib;
1b42725f
JA
1230 } while (stream.avail_in);
1231
53bd8dbc 1232err_zlib:
1b42725f 1233 deflateEnd(&stream);
53bd8dbc
JA
1234err:
1235 free(out_pdu);
3989b143 1236#endif
53bd8dbc 1237 return ret;
1b42725f
JA
1238}
1239
3989b143
JA
1240int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1241{
1242 struct cmd_iolog_pdu pdu;
1243 int i, ret = 0;
1244
ae588852 1245 pdu.nr_samples = cpu_to_le64(log->nr_samples);
3989b143 1246 pdu.thread_number = cpu_to_le32(td->thread_number);
3989b143
JA
1247 pdu.log_type = cpu_to_le32(log->log_type);
1248 pdu.compressed = cpu_to_le32(use_zlib);
9bdb9265
JA
1249
1250 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1251 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
3989b143
JA
1252
1253 for (i = 0; i < log->nr_samples; i++) {
ae588852 1254 struct io_sample *s = get_sample(log, i);
3989b143 1255
b26317c9
JA
1256 s->time = cpu_to_le64(s->time);
1257 s->val = cpu_to_le64(s->val);
1258 s->__ddir = cpu_to_le32(s->__ddir);
1259 s->bs = cpu_to_le32(s->bs);
ae588852
JA
1260
1261 if (log->log_offset) {
1262 struct io_sample_offset *so = (void *) s;
1263
1264 so->offset = cpu_to_le64(so->offset);
1265 }
3989b143
JA
1266 }
1267
1268 /*
1269 * Send header first, it's not compressed.
1270 */
1271 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1272 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1273 if (ret)
1274 return ret;
1275
1276 /*
1277 * Now send actual log, compress if we can, otherwise just plain
1278 */
1279 if (use_zlib)
1280 return fio_send_iolog_gz(&pdu, log);
1281
1282 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
ae588852 1283 log->nr_samples * log_entry_sz(log), 0, 0);
3989b143
JA
1284}
1285
2f122b13 1286void fio_server_send_add_job(struct thread_data *td)
807f9971
JA
1287{
1288 struct cmd_add_job_pdu pdu;
807f9971 1289
731e30a2 1290 memset(&pdu, 0, sizeof(pdu));
2f122b13
JA
1291 pdu.thread_number = cpu_to_le32(td->thread_number);
1292 pdu.groupid = cpu_to_le32(td->groupid);
1293 convert_thread_options_to_net(&pdu.top, &td->o);
807f9971 1294
40c60516 1295 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
807f9971
JA
1296}
1297
122c7725
JA
1298void fio_server_send_start(struct thread_data *td)
1299{
1300 assert(server_fd != -1);
1301
1302 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1303}
1304
87aa8f19 1305static int fio_init_server_ip(void)
81179eec 1306{
811826be 1307 struct sockaddr *addr;
67bf9823 1308 socklen_t socklen;
17e3531a
JA
1309 char buf[80];
1310 const char *str;
87aa8f19 1311 int sk, opt;
81179eec 1312
811826be
JA
1313 if (use_ipv6)
1314 sk = socket(AF_INET6, SOCK_STREAM, 0);
1315 else
1316 sk = socket(AF_INET, SOCK_STREAM, 0);
1317
81179eec
JA
1318 if (sk < 0) {
1319 log_err("fio: socket: %s\n", strerror(errno));
1320 return -1;
1321 }
1322
1323 opt = 1;
1f81991e 1324 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
81179eec 1325 log_err("fio: setsockopt: %s\n", strerror(errno));
b94cba47 1326 close(sk);
81179eec
JA
1327 return -1;
1328 }
1329#ifdef SO_REUSEPORT
6eb24791 1330 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
81179eec 1331 log_err("fio: setsockopt: %s\n", strerror(errno));
b94cba47 1332 close(sk);
81179eec
JA
1333 return -1;
1334 }
1335#endif
1336
811826be 1337 if (use_ipv6) {
17e3531a
JA
1338 const void *src = &saddr_in6.sin6_addr;
1339
811826be
JA
1340 addr = (struct sockaddr *) &saddr_in6;
1341 socklen = sizeof(saddr_in6);
1342 saddr_in6.sin6_family = AF_INET6;
17e3531a 1343 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
811826be 1344 } else {
17e3531a
JA
1345 const void *src = &saddr_in.sin_addr;
1346
811826be
JA
1347 addr = (struct sockaddr *) &saddr_in;
1348 socklen = sizeof(saddr_in);
1349 saddr_in.sin_family = AF_INET;
17e3531a 1350 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
811826be 1351 }
81179eec 1352
811826be 1353 if (bind(sk, addr, socklen) < 0) {
81179eec 1354 log_err("fio: bind: %s\n", strerror(errno));
17e3531a 1355 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
b94cba47 1356 close(sk);
81179eec
JA
1357 return -1;
1358 }
1359
87aa8f19
JA
1360 return sk;
1361}
1362
1363static int fio_init_server_sock(void)
1364{
1365 struct sockaddr_un addr;
67bf9823 1366 socklen_t len;
87aa8f19
JA
1367 mode_t mode;
1368 int sk;
1369
1370 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1371 if (sk < 0) {
1372 log_err("fio: socket: %s\n", strerror(errno));
1373 return -1;
1374 }
1375
1376 mode = umask(000);
1377
1378 memset(&addr, 0, sizeof(addr));
1379 addr.sun_family = AF_UNIX;
a48fddbc 1380 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
87aa8f19
JA
1381
1382 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1383
1384 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1385 log_err("fio: bind: %s\n", strerror(errno));
b94cba47 1386 close(sk);
87aa8f19
JA
1387 return -1;
1388 }
1389
1390 umask(mode);
1391 return sk;
1392}
1393
1394static int fio_init_server_connection(void)
1395{
bebe6398 1396 char bind_str[128];
87aa8f19
JA
1397 int sk;
1398
1399 dprint(FD_NET, "starting server\n");
1400
1401 if (!bind_sock)
1402 sk = fio_init_server_ip();
1403 else
1404 sk = fio_init_server_sock();
1405
1406 if (sk < 0)
1407 return sk;
1408
afdcad23
JA
1409 memset(bind_str, 0, sizeof(bind_str));
1410
811826be
JA
1411 if (!bind_sock) {
1412 char *p, port[16];
1413 const void *src;
1414 int af;
1415
1416 if (use_ipv6) {
1417 af = AF_INET6;
1418 src = &saddr_in6.sin6_addr;
1419 } else {
1420 af = AF_INET;
1421 src = &saddr_in.sin_addr;
1422 }
1423
1424 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1425
1426 sprintf(port, ",%u", fio_net_port);
1427 if (p)
1428 strcat(p, port);
1429 else
afdcad23 1430 strncpy(bind_str, port, sizeof(bind_str) - 1);
811826be 1431 } else
afdcad23 1432 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
bebe6398
JA
1433
1434 log_info("fio: server listening on %s\n", bind_str);
1435
89c1707c 1436 if (listen(sk, 0) < 0) {
81179eec 1437 log_err("fio: listen: %s\n", strerror(errno));
2fd973c9 1438 close(sk);
81179eec
JA
1439 return -1;
1440 }
1441
87aa8f19
JA
1442 return sk;
1443}
1444
3aa3ceeb 1445int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
3ec62ec4
JA
1446 struct in6_addr *inp6)
1447
1448{
1449 int ret = 0;
1450
3aa3ceeb 1451 if (ipv6)
3ec62ec4
JA
1452 ret = inet_pton(AF_INET6, host, inp6);
1453 else
1454 ret = inet_pton(AF_INET, host, inp);
1455
1456 if (ret != 1) {
479471c4
JA
1457 struct addrinfo hints, *res;
1458
1459 memset(&hints, 0, sizeof(hints));
3aa3ceeb 1460 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
479471c4 1461 hints.ai_socktype = SOCK_STREAM;
3ec62ec4 1462
479471c4
JA
1463 ret = getaddrinfo(host, NULL, &hints, &res);
1464 if (ret) {
1465 log_err("fio: failed to resolve <%s> (%s)\n", host,
1466 gai_strerror(ret));
3caf43e3 1467 return 1;
3ec62ec4
JA
1468 }
1469
3aa3ceeb 1470 if (ipv6)
479471c4
JA
1471 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
1472 else
1473 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
1474
3ec62ec4 1475 ret = 1;
479471c4 1476 freeaddrinfo(res);
3ec62ec4
JA
1477 }
1478
1479 return !(ret == 1);
1480}
1481
660a2bfb
JA
1482/*
1483 * Parse a host/ip/port string. Reads from 'str'.
1484 *
1485 * Outputs:
1486 *
1487 * For IPv4:
1488 * *ptr is the host, *port is the port, inp is the destination.
1489 * For IPv6:
1490 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1491 * For local domain sockets:
1492 * *ptr is the filename, *is_sock is 1.
1493 */
bebe6398 1494int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
811826be
JA
1495 int *port, struct in_addr *inp,
1496 struct in6_addr *inp6, int *ipv6)
bebe6398 1497{
76867621
JA
1498 const char *host = str;
1499 char *portp;
3ec62ec4 1500 int lport = 0;
76867621 1501
bebe6398
JA
1502 *ptr = NULL;
1503 *is_sock = 0;
6d2cf394 1504 *port = fio_net_port;
811826be 1505 *ipv6 = 0;
bebe6398
JA
1506
1507 if (!strncmp(str, "sock:", 5)) {
1508 *ptr = strdup(str + 5);
1509 *is_sock = 1;
76867621
JA
1510
1511 return 0;
1512 }
1513
1514 /*
1515 * Is it ip:<ip or host>:port
1516 */
1517 if (!strncmp(host, "ip:", 3))
1518 host += 3;
1519 else if (!strncmp(host, "ip4:", 4))
1520 host += 4;
1521 else if (!strncmp(host, "ip6:", 4)) {
1522 host += 4;
1523 *ipv6 = 1;
1524 } else if (host[0] == ':') {
1525 /* String is :port */
1526 host++;
1527 lport = atoi(host);
1528 if (!lport || lport > 65535) {
4e0a8fa2 1529 log_err("fio: bad server port %u\n", lport);
76867621
JA
1530 return 1;
1531 }
1532 /* no hostname given, we are done */
1533 *port = lport;
1534 return 0;
1535 }
1536
1537 /*
b96b90c3 1538 * If no port seen yet, check if there's a last ',' at the end
76867621
JA
1539 */
1540 if (!lport) {
1541 portp = strchr(host, ',');
1542 if (portp) {
1543 *portp = '\0';
1544 portp++;
1545 lport = atoi(portp);
bebe6398 1546 if (!lport || lport > 65535) {
4e0a8fa2 1547 log_err("fio: bad server port %u\n", lport);
bebe6398
JA
1548 return 1;
1549 }
bebe6398 1550 }
76867621 1551 }
bebe6398 1552
76867621
JA
1553 if (lport)
1554 *port = lport;
bebe6398 1555
76867621
JA
1556 if (!strlen(host))
1557 return 0;
bebe6398 1558
76867621 1559 *ptr = strdup(host);
811826be 1560
3aa3ceeb 1561 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
3ec62ec4
JA
1562 free(*ptr);
1563 *ptr = NULL;
1564 return 1;
bebe6398
JA
1565 }
1566
1567 if (*port == 0)
1568 *port = fio_net_port;
1569
1570 return 0;
1571}
1572
87aa8f19
JA
1573/*
1574 * Server arg should be one of:
1575 *
1576 * sock:/path/to/socket
1577 * ip:1.2.3.4
1578 * 1.2.3.4
1579 *
1580 * Where sock uses unix domain sockets, and ip binds the server to
1581 * a specific interface. If no arguments are given to the server, it
1582 * uses IP and binds to 0.0.0.0.
1583 *
1584 */
1585static int fio_handle_server_arg(void)
1586{
6d2cf394 1587 int port = fio_net_port;
a7de0a11 1588 int is_sock, ret = 0;
bebe6398 1589
87aa8f19
JA
1590 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1591
1592 if (!fio_server_arg)
a7de0a11 1593 goto out;
87aa8f19 1594
4e5b8fb8 1595 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
811826be
JA
1596 &port, &saddr_in.sin_addr,
1597 &saddr_in6.sin6_addr, &use_ipv6);
4e5b8fb8
JA
1598
1599 if (!is_sock && bind_sock) {
1600 free(bind_sock);
1601 bind_sock = NULL;
1602 }
1603
a7de0a11 1604out:
6d2cf394
JA
1605 fio_net_port = port;
1606 saddr_in.sin_port = htons(port);
811826be 1607 saddr_in6.sin6_port = htons(port);
4e5b8fb8 1608 return ret;
87aa8f19
JA
1609}
1610
43cdea1d
JA
1611static void sig_int(int sig)
1612{
1613 if (bind_sock)
1614 unlink(bind_sock);
1615}
1616
1617static void set_sig_handlers(void)
1618{
1619 struct sigaction act;
1620
1621 memset(&act, 0, sizeof(act));
1622 act.sa_handler = sig_int;
1623 act.sa_flags = SA_RESTART;
1624 sigaction(SIGINT, &act, NULL);
1625}
1626
87aa8f19
JA
1627static int fio_server(void)
1628{
1629 int sk, ret;
1630
1631 dprint(FD_NET, "starting server\n");
1632
1633 if (fio_handle_server_arg())
1634 return -1;
1635
1636 sk = fio_init_server_connection();
1637 if (sk < 0)
1638 return -1;
81179eec 1639
43cdea1d
JA
1640 set_sig_handlers();
1641
81179eec 1642 ret = accept_loop(sk);
87aa8f19 1643
81179eec 1644 close(sk);
87aa8f19
JA
1645
1646 if (fio_server_arg) {
1647 free(fio_server_arg);
1648 fio_server_arg = NULL;
1649 }
bebe6398
JA
1650 if (bind_sock)
1651 free(bind_sock);
87aa8f19 1652
81179eec
JA
1653 return ret;
1654}
1655
7b821684 1656void fio_server_got_signal(int signal)
9abea48b 1657{
7b821684
JA
1658 if (signal == SIGPIPE)
1659 server_fd = -1;
1660 else {
1661 log_info("\nfio: terminating on signal %d\n", signal);
1662 exit_backend = 1;
1663 }
9abea48b
JA
1664}
1665
13755d94
JA
1666static int check_existing_pidfile(const char *pidfile)
1667{
1668 struct stat sb;
1669 char buf[16];
1670 pid_t pid;
1671 FILE *f;
1672
1673 if (stat(pidfile, &sb))
1674 return 0;
1675
1676 f = fopen(pidfile, "r");
1677 if (!f)
1678 return 0;
1679
bfc3b179 1680 if (fread(buf, sb.st_size, 1, f) <= 0) {
13755d94
JA
1681 fclose(f);
1682 return 1;
1683 }
1684 fclose(f);
1685
1686 pid = atoi(buf);
1687 if (kill(pid, SIGCONT) < 0)
ea5aa1be 1688 return errno != ESRCH;
13755d94
JA
1689
1690 return 1;
1691}
1692
1693static int write_pid(pid_t pid, const char *pidfile)
402668f3
JA
1694{
1695 FILE *fpid;
1696
1697 fpid = fopen(pidfile, "w");
1698 if (!fpid) {
1699 log_err("fio: failed opening pid file %s\n", pidfile);
13755d94 1700 return 1;
402668f3
JA
1701 }
1702
1703 fprintf(fpid, "%u\n", (unsigned int) pid);
13755d94
JA
1704 fclose(fpid);
1705 return 0;
402668f3
JA
1706}
1707
1708/*
1709 * If pidfile is specified, background us.
1710 */
1711int fio_start_server(char *pidfile)
e46d8091
JA
1712{
1713 pid_t pid;
402668f3 1714 int ret;
e46d8091 1715
93bcfd20 1716#if defined(WIN32)
905c78b9 1717 WSADATA wsd;
3c3ed070 1718 WSAStartup(MAKEWORD(2, 2), &wsd);
93bcfd20
BC
1719#endif
1720
402668f3 1721 if (!pidfile)
e46d8091
JA
1722 return fio_server();
1723
13755d94
JA
1724 if (check_existing_pidfile(pidfile)) {
1725 log_err("fio: pidfile %s exists and server appears alive\n",
1726 pidfile);
b8ba87ac 1727 free(pidfile);
13755d94
JA
1728 return -1;
1729 }
1730
e46d8091
JA
1731 pid = fork();
1732 if (pid < 0) {
13755d94 1733 log_err("fio: failed server fork: %s", strerror(errno));
402668f3 1734 free(pidfile);
c28e8e8c 1735 return -1;
402668f3 1736 } else if (pid) {
13755d94
JA
1737 int ret = write_pid(pid, pidfile);
1738
b8ba87ac 1739 free(pidfile);
44c8268d 1740 _exit(ret);
402668f3 1741 }
e46d8091
JA
1742
1743 setsid();
13755d94
JA
1744 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1745 log_syslog = 1;
e46d8091
JA
1746 close(STDIN_FILENO);
1747 close(STDOUT_FILENO);
1748 close(STDERR_FILENO);
1749 f_out = NULL;
1750 f_err = NULL;
402668f3
JA
1751
1752 ret = fio_server();
1753
1754 closelog();
1755 unlink(pidfile);
1756 free(pidfile);
1757 return ret;
e46d8091 1758}
87aa8f19 1759
bebe6398 1760void fio_server_set_arg(const char *arg)
87aa8f19
JA
1761{
1762 fio_server_arg = strdup(arg);
1763}