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