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