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