11 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
25 #include "crc/crc16.h"
26 #include "lib/ieee754.h"
28 int fio_net_port = FIO_NET_PORT;
32 static int server_fd = -1;
33 static char *fio_server_arg;
34 static char *bind_sock;
35 static struct sockaddr_in saddr_in;
36 static struct sockaddr_in6 saddr_in6;
39 static unsigned int has_zlib = 1;
41 static unsigned int has_zlib = 0;
43 static unsigned int use_zlib;
45 struct fio_fork_item {
46 struct flist_head list;
53 /* Created on fork on new connection */
54 static FLIST_HEAD(conn_list);
56 /* Created on job fork from connection */
57 static FLIST_HEAD(job_list);
59 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
80 const char *fio_server_op(unsigned int op)
84 if (op < FIO_NET_CMD_NR)
85 return fio_server_ops[op];
87 sprintf(buf, "UNKNOWN/%d", op);
91 static ssize_t iov_total_len(const struct iovec *iov, int count)
103 static int fio_sendv_data(int sk, struct iovec *iov, int count)
105 ssize_t total_len = iov_total_len(iov, count);
109 ret = writev(sk, iov, count);
116 if (ret >= iov->iov_len) {
121 iov->iov_base += ret;
127 else if (errno == EAGAIN || errno == EINTR)
131 } while (!exit_backend);
142 int fio_send_data(int sk, const void *p, unsigned int len)
144 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
146 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
148 return fio_sendv_data(sk, &iov, 1);
151 int fio_recv_data(int sk, void *p, unsigned int len)
154 int ret = recv(sk, p, len, MSG_WAITALL);
164 else if (errno == EAGAIN || errno == EINTR)
168 } while (!exit_backend);
176 static int verify_convert_cmd(struct fio_net_cmd *cmd)
180 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
181 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
183 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
184 if (crc != cmd->cmd_crc16) {
185 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
186 cmd->cmd_crc16, crc);
190 cmd->version = le16_to_cpu(cmd->version);
191 cmd->opcode = le16_to_cpu(cmd->opcode);
192 cmd->flags = le32_to_cpu(cmd->flags);
193 cmd->tag = le64_to_cpu(cmd->tag);
194 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
196 switch (cmd->version) {
200 log_err("fio: bad server cmd version %d\n", cmd->version);
204 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
205 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
213 * Read (and defragment, if necessary) incoming commands
215 struct fio_net_cmd *fio_net_recv_cmd(int sk)
217 struct fio_net_cmd cmd, *cmdret = NULL;
218 size_t cmd_size = 0, pdu_offset = 0;
224 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
228 /* We have a command, verify it and swap if need be */
229 ret = verify_convert_cmd(&cmd);
234 /* if this is text, add room for \0 at the end */
235 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
238 cmd_size += cmd.pdu_len;
240 cmdret = realloc(cmdret, cmd_size);
243 memcpy(cmdret, &cmd, sizeof(cmd));
244 else if (cmdret->opcode != cmd.opcode) {
245 log_err("fio: fragment opcode mismatch (%d != %d)\n",
246 cmdret->opcode, cmd.opcode);
254 /* There's payload, get it */
255 pdu = (void *) cmdret->payload + pdu_offset;
256 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
260 /* Verify payload crc */
261 crc = fio_crc16(pdu, cmd.pdu_len);
262 if (crc != cmd.pdu_crc16) {
263 log_err("fio: server bad crc on payload ");
264 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
269 pdu_offset += cmd.pdu_len;
271 cmdret->pdu_len += cmd.pdu_len;
273 } while (cmd.flags & FIO_NET_CMD_F_MORE);
279 /* zero-terminate text input */
280 if (cmdret->pdu_len) {
281 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
282 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
283 char *buf = (char *) pdu->buf;
285 buf[pdu->buf_len] = '\0';
286 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
287 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
288 char *buf = (char *) pdu->buf;
289 int len = le32_to_cpu(pdu->buf_len);
295 /* frag flag is internal */
296 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
302 static void add_reply(uint64_t tag, struct flist_head *list)
304 struct fio_net_cmd_reply *reply;
306 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
307 flist_add_tail(&reply->list, list);
310 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
312 struct fio_net_cmd_reply *reply;
314 reply = calloc(1, sizeof(*reply));
315 INIT_FLIST_HEAD(&reply->list);
316 gettimeofday(&reply->tv, NULL);
317 reply->saved_tag = tag;
318 reply->opcode = opcode;
320 return (uintptr_t) reply;
323 static void free_reply(uint64_t tag)
325 struct fio_net_cmd_reply *reply;
327 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
331 void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
335 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
337 pdu_len = le32_to_cpu(cmd->pdu_len);
338 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
341 void fio_net_cmd_crc(struct fio_net_cmd *cmd)
343 fio_net_cmd_crc_pdu(cmd, cmd->payload);
346 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
347 uint64_t *tagptr, struct flist_head *list)
349 struct fio_net_cmd *cmd = NULL;
350 size_t this_len, cur_len = 0;
356 tag = *tagptr = alloc_reply(*tagptr, opcode);
358 tag = tagptr ? *tagptr : 0;
362 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
363 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
365 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
369 cur_len = sizeof(*cmd) + this_len;
370 cmd = malloc(cur_len);
373 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
376 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
378 fio_net_cmd_crc(cmd);
380 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
383 } while (!ret && size);
389 add_reply(tag, list);
398 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
400 struct fio_net_cmd cmd;
402 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
403 fio_net_cmd_crc(&cmd);
405 return fio_send_data(sk, &cmd, sizeof(cmd));
409 * If 'list' is non-NULL, then allocate and store the sent command for
410 * later verification.
412 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
413 struct flist_head *list)
418 tag = alloc_reply(tag, opcode);
420 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
429 add_reply(tag, list);
434 int fio_net_send_quit(int sk)
436 dprint(FD_NET, "server: sending quit\n");
438 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
441 static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
444 struct cmd_end_pdu epdu;
450 epdu.error = __cpu_to_le32(error);
451 epdu.signal = __cpu_to_le32(signal);
452 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
455 int fio_net_send_stop(int sk, int error, int signal)
457 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
458 return fio_net_send_ack(sk, NULL, error, signal);
461 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
463 struct fio_fork_item *ffi;
465 ffi = malloc(sizeof(*ffi));
470 flist_add_tail(&ffi->list, list);
473 static void fio_server_add_conn_pid(pid_t pid)
475 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", pid);
476 fio_server_add_fork_item(pid, &conn_list);
479 static void fio_server_add_job_pid(pid_t pid)
481 dprint(FD_NET, "server: forked off job job (pid=%u)\n", pid);
482 fio_server_add_fork_item(pid, &job_list);
485 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
489 ret = waitpid(ffi->pid, &status, WNOHANG);
491 if (errno == ECHILD) {
492 log_err("fio: connection pid %u disappeared\n", ffi->pid);
495 log_err("fio: waitpid: %s\n", strerror(errno));
496 } else if (ret == ffi->pid) {
497 if (WIFSIGNALED(status)) {
498 ffi->signal = WTERMSIG(status);
501 if (WIFEXITED(status)) {
502 if (WEXITSTATUS(status))
503 ffi->exitval = WEXITSTATUS(status);
509 static void fio_server_fork_item_done(struct fio_fork_item *ffi)
511 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", ffi->pid, ffi->signal, ffi->exitval);
514 * Fold STOP and QUIT...
516 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
517 fio_net_send_quit(server_fd);
518 flist_del(&ffi->list);
522 static void fio_server_check_fork_items(struct flist_head *list)
524 struct flist_head *entry, *tmp;
525 struct fio_fork_item *ffi;
527 flist_for_each_safe(entry, tmp, list) {
528 ffi = flist_entry(entry, struct fio_fork_item, list);
530 fio_server_check_fork_item(ffi);
533 fio_server_fork_item_done(ffi);
537 static void fio_server_check_jobs(void)
539 fio_server_check_fork_items(&job_list);
542 static void fio_server_check_conns(void)
544 fio_server_check_fork_items(&conn_list);
547 static int handle_run_cmd(struct fio_net_cmd *cmd)
556 fio_server_add_job_pid(pid);
565 static int handle_job_cmd(struct fio_net_cmd *cmd)
567 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
568 void *buf = pdu->buf;
569 struct cmd_start_pdu spdu;
571 pdu->buf_len = le32_to_cpu(pdu->buf_len);
572 pdu->client_type = le32_to_cpu(pdu->client_type);
574 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
575 fio_net_send_quit(server_fd);
579 spdu.jobs = cpu_to_le32(thread_number);
580 spdu.stat_outputs = cpu_to_le32(stat_number);
581 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
585 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
587 void *pdu = cmd->payload;
588 struct cmd_single_line_pdu *cslp;
589 struct cmd_line_pdu *clp;
590 unsigned long offset;
591 struct cmd_start_pdu spdu;
596 clp->lines = le16_to_cpu(clp->lines);
597 clp->client_type = le16_to_cpu(clp->client_type);
598 argv = malloc(clp->lines * sizeof(char *));
599 offset = sizeof(*clp);
601 dprint(FD_NET, "server: %d command line args\n", clp->lines);
603 for (i = 0; i < clp->lines; i++) {
605 argv[i] = (char *) cslp->text;
607 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
608 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
611 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
612 fio_net_send_quit(server_fd);
619 spdu.jobs = cpu_to_le32(thread_number);
620 spdu.stat_outputs = cpu_to_le32(stat_number);
621 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
625 static int handle_probe_cmd(struct fio_net_cmd *cmd)
627 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
628 struct cmd_probe_reply_pdu probe;
629 uint64_t tag = cmd->tag;
631 dprint(FD_NET, "server: sending probe reply\n");
633 memset(&probe, 0, sizeof(probe));
634 gethostname((char *) probe.hostname, sizeof(probe.hostname));
635 #ifdef CONFIG_BIG_ENDIAN
638 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
641 probe.arch = FIO_ARCH;
642 probe.bpp = sizeof(void *);
643 probe.cpus = __cpu_to_le32(cpus_online());
646 * If the client supports compression and we do too, then enable it
648 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
649 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
656 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
659 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
663 uint64_t tag = cmd->tag;
669 size = sizeof(*je) + thread_number * sizeof(char) + 1;
673 if (!calc_thread_status(je, 1)) {
678 dprint(FD_NET, "server sending status\n");
680 je->nr_running = cpu_to_le32(je->nr_running);
681 je->nr_ramp = cpu_to_le32(je->nr_ramp);
682 je->nr_pending = cpu_to_le32(je->nr_pending);
683 je->files_open = cpu_to_le32(je->files_open);
685 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
686 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
687 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
688 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
689 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
690 je->rate[i] = cpu_to_le32(je->rate[i]);
691 je->iops[i] = cpu_to_le32(je->iops[i]);
694 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
695 je->eta_sec = cpu_to_le64(je->eta_sec);
696 je->nr_threads = cpu_to_le32(je->nr_threads);
697 je->is_pow2 = cpu_to_le32(je->is_pow2);
698 je->unit_base = cpu_to_le32(je->unit_base);
700 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
705 static int send_update_job_reply(int fd, uint64_t __tag, int error)
707 uint64_t tag = __tag;
710 pdu_error = __cpu_to_le32(error);
711 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
714 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
716 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
717 struct thread_data *td;
720 tnumber = le32_to_cpu(pdu->thread_number);
722 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
724 if (!tnumber || tnumber > thread_number) {
725 send_update_job_reply(server_fd, cmd->tag, ENODEV);
729 td = &threads[tnumber - 1];
730 convert_thread_options_to_cpu(&td->o, &pdu->top);
731 send_update_job_reply(server_fd, cmd->tag, 0);
735 static int handle_command(struct fio_net_cmd *cmd)
739 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
740 fio_server_op(cmd->opcode), cmd->pdu_len,
741 (unsigned long long) cmd->tag);
743 switch (cmd->opcode) {
744 case FIO_NET_CMD_QUIT:
745 fio_terminate_threads(TERMINATE_ALL);
747 case FIO_NET_CMD_EXIT:
750 case FIO_NET_CMD_JOB:
751 ret = handle_job_cmd(cmd);
753 case FIO_NET_CMD_JOBLINE:
754 ret = handle_jobline_cmd(cmd);
756 case FIO_NET_CMD_PROBE:
757 ret = handle_probe_cmd(cmd);
759 case FIO_NET_CMD_SEND_ETA:
760 ret = handle_send_eta_cmd(cmd);
762 case FIO_NET_CMD_RUN:
763 ret = handle_run_cmd(cmd);
765 case FIO_NET_CMD_UPDATE_JOB:
766 ret = handle_update_job_cmd(cmd);
769 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
776 static int handle_connection(int sk)
778 struct fio_net_cmd *cmd = NULL;
782 INIT_FLIST_HEAD(&job_list);
786 while (!exit_backend) {
787 struct pollfd pfd = {
796 if (!flist_empty(&job_list))
799 ret = poll(&pfd, 1, timeout);
803 log_err("fio: poll: %s\n", strerror(errno));
806 fio_server_check_jobs();
810 if (pfd.revents & POLLIN)
812 if (pfd.revents & (POLLERR|POLLHUP)) {
816 } while (!exit_backend);
818 fio_server_check_jobs();
823 cmd = fio_net_recv_cmd(sk);
829 ret = handle_command(cmd);
844 static int accept_loop(int listen_sk)
846 struct sockaddr_in addr;
847 socklen_t len = sizeof(addr);
849 int ret = 0, sk, flags, exitval = 0;
851 dprint(FD_NET, "server enter accept loop\n");
853 flags = fcntl(listen_sk, F_GETFL);
855 fcntl(listen_sk, F_SETFL, flags);
857 while (!exit_backend) {
865 if (!flist_empty(&conn_list))
868 ret = poll(&pfd, 1, timeout);
872 log_err("fio: poll: %s\n", strerror(errno));
875 fio_server_check_conns();
879 if (pfd.revents & POLLIN)
881 } while (!exit_backend);
883 fio_server_check_conns();
885 if (exit_backend || ret < 0)
888 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
890 log_err("fio: accept: %s\n", strerror(errno));
894 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
899 fio_server_add_conn_pid(pid);
904 handle_connection(sk);
910 int fio_server_text_output(int level, const char *buf, size_t len)
912 struct cmd_text_pdu *pdu;
917 return log_local_buf(buf, len);
919 tlen = sizeof(*pdu) + len;
922 pdu->level = __cpu_to_le32(level);
923 pdu->buf_len = __cpu_to_le32(len);
925 gettimeofday(&tv, NULL);
926 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
927 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
929 memcpy(pdu->buf, buf, len);
931 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
936 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
938 dst->max_val = cpu_to_le64(src->max_val);
939 dst->min_val = cpu_to_le64(src->min_val);
940 dst->samples = cpu_to_le64(src->samples);
943 * Encode to IEEE 754 for network transfer
945 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
946 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
949 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
953 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
954 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
955 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
956 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
957 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
958 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
959 dst->agg[i] = cpu_to_le64(src->agg[i]);
962 dst->kb_base = cpu_to_le32(src->kb_base);
963 dst->unit_base = cpu_to_le32(src->unit_base);
964 dst->groupid = cpu_to_le32(src->groupid);
965 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
969 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
970 * into a single payload.
972 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
977 dprint(FD_NET, "server sending end stats\n");
979 memset(&p, 0, sizeof(p));
981 strcpy(p.ts.name, ts->name);
982 strcpy(p.ts.verror, ts->verror);
983 strcpy(p.ts.description, ts->description);
985 p.ts.error = cpu_to_le32(ts->error);
986 p.ts.thread_number = cpu_to_le32(ts->thread_number);
987 p.ts.groupid = cpu_to_le32(ts->groupid);
988 p.ts.pid = cpu_to_le32(ts->pid);
989 p.ts.members = cpu_to_le32(ts->members);
990 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
992 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
993 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
994 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
995 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
996 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
999 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1000 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1001 p.ts.ctx = cpu_to_le64(ts->ctx);
1002 p.ts.minf = cpu_to_le64(ts->minf);
1003 p.ts.majf = cpu_to_le64(ts->majf);
1004 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1006 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1007 fio_fp64_t *src = &ts->percentile_list[i];
1008 fio_fp64_t *dst = &p.ts.percentile_list[i];
1010 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
1013 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1014 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1015 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1016 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1019 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1020 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1021 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1024 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1025 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1026 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1028 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1029 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1030 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1033 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1034 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1036 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1037 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1038 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1041 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1042 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1043 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1044 p.ts.first_error = cpu_to_le32(ts->first_error);
1045 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1046 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1048 convert_gs(&p.rs, rs);
1050 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
1053 void fio_server_send_gs(struct group_run_stats *rs)
1055 struct group_run_stats gs;
1057 dprint(FD_NET, "server sending group run stats\n");
1059 convert_gs(&gs, rs);
1060 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
1063 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1067 for (i = 0; i < 2; i++) {
1068 dst->ios[i] = cpu_to_le32(src->ios[i]);
1069 dst->merges[i] = cpu_to_le32(src->merges[i]);
1070 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1071 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1074 dst->io_ticks = cpu_to_le32(src->io_ticks);
1075 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1076 dst->slavecount = cpu_to_le32(src->slavecount);
1077 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1080 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1084 strcpy((char *) dst->name, (char *) src->name);
1086 for (i = 0; i < 2; i++) {
1087 dst->ios[i] = cpu_to_le32(src->ios[i]);
1088 dst->merges[i] = cpu_to_le32(src->merges[i]);
1089 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1090 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1093 dst->io_ticks = cpu_to_le32(src->io_ticks);
1094 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1095 dst->msec = cpu_to_le64(src->msec);
1098 void fio_server_send_du(void)
1100 struct disk_util *du;
1101 struct flist_head *entry;
1102 struct cmd_du_pdu pdu;
1104 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1106 memset(&pdu, 0, sizeof(pdu));
1108 flist_for_each(entry, &disk_list) {
1109 du = flist_entry(entry, struct disk_util, list);
1111 convert_dus(&pdu.dus, &du->dus);
1112 convert_agg(&pdu.agg, &du->agg);
1114 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
1119 * Send a command with a separate PDU, not inlined in the command
1121 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1122 off_t size, uint64_t tag, uint32_t flags)
1124 struct fio_net_cmd cmd;
1125 struct iovec iov[2];
1127 iov[0].iov_base = &cmd;
1128 iov[0].iov_len = sizeof(cmd);
1129 iov[1].iov_base = (void *) buf;
1130 iov[1].iov_len = size;
1132 __fio_init_net_cmd(&cmd, opcode, size, tag);
1133 cmd.flags = __cpu_to_le32(flags);
1134 fio_net_cmd_crc_pdu(&cmd, buf);
1136 return fio_sendv_data(sk, iov, 2);
1139 static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
1147 * Dirty - since the log is potentially huge, compress it into
1148 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1149 * side defragment it.
1151 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1153 stream.zalloc = Z_NULL;
1154 stream.zfree = Z_NULL;
1155 stream.opaque = Z_NULL;
1157 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
1162 stream.next_in = (void *) log->log;
1163 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
1166 unsigned int this_len, flags = 0;
1169 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1170 stream.next_out = out_pdu;
1171 ret = deflate(&stream, Z_FINISH);
1172 /* may be Z_OK, or Z_STREAM_END */
1176 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1178 if (stream.avail_in)
1179 flags = FIO_NET_CMD_F_MORE;
1181 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1182 out_pdu, this_len, 0, flags);
1185 } while (stream.avail_in);
1188 deflateEnd(&stream);
1195 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1197 struct cmd_iolog_pdu pdu;
1200 pdu.thread_number = cpu_to_le32(td->thread_number);
1201 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1202 pdu.log_type = cpu_to_le32(log->log_type);
1203 pdu.compressed = cpu_to_le32(use_zlib);
1204 strcpy((char *) pdu.name, name);
1206 for (i = 0; i < log->nr_samples; i++) {
1207 struct io_sample *s = &log->log[i];
1209 s->time = cpu_to_le64(s->time);
1210 s->val = cpu_to_le64(s->val);
1211 s->ddir = cpu_to_le32(s->ddir);
1212 s->bs = cpu_to_le32(s->bs);
1216 * Send header first, it's not compressed.
1218 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1219 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1224 * Now send actual log, compress if we can, otherwise just plain
1227 return fio_send_iolog_gz(&pdu, log);
1229 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
1230 log->nr_samples * sizeof(struct io_sample), 0, 0);
1233 void fio_server_send_add_job(struct thread_data *td)
1235 struct cmd_add_job_pdu pdu;
1237 memset(&pdu, 0, sizeof(pdu));
1238 pdu.thread_number = cpu_to_le32(td->thread_number);
1239 pdu.groupid = cpu_to_le32(td->groupid);
1240 convert_thread_options_to_net(&pdu.top, &td->o);
1242 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
1245 void fio_server_send_start(struct thread_data *td)
1247 assert(server_fd != -1);
1249 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1252 static int fio_init_server_ip(void)
1254 struct sockaddr *addr;
1259 sk = socket(AF_INET6, SOCK_STREAM, 0);
1261 sk = socket(AF_INET, SOCK_STREAM, 0);
1264 log_err("fio: socket: %s\n", strerror(errno));
1269 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1270 log_err("fio: setsockopt: %s\n", strerror(errno));
1275 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
1276 log_err("fio: setsockopt: %s\n", strerror(errno));
1283 addr = (struct sockaddr *) &saddr_in6;
1284 socklen = sizeof(saddr_in6);
1285 saddr_in6.sin6_family = AF_INET6;
1287 addr = (struct sockaddr *) &saddr_in;
1288 socklen = sizeof(saddr_in);
1289 saddr_in.sin_family = AF_INET;
1292 if (bind(sk, addr, socklen) < 0) {
1293 log_err("fio: bind: %s\n", strerror(errno));
1301 static int fio_init_server_sock(void)
1303 struct sockaddr_un addr;
1308 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1310 log_err("fio: socket: %s\n", strerror(errno));
1316 memset(&addr, 0, sizeof(addr));
1317 addr.sun_family = AF_UNIX;
1318 strcpy(addr.sun_path, bind_sock);
1321 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1323 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1324 log_err("fio: bind: %s\n", strerror(errno));
1333 static int fio_init_server_connection(void)
1338 dprint(FD_NET, "starting server\n");
1341 sk = fio_init_server_ip();
1343 sk = fio_init_server_sock();
1355 src = &saddr_in6.sin6_addr;
1358 src = &saddr_in.sin_addr;
1361 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1363 sprintf(port, ",%u", fio_net_port);
1367 strcpy(bind_str, port);
1369 strcpy(bind_str, bind_sock);
1371 log_info("fio: server listening on %s\n", bind_str);
1373 if (listen(sk, 0) < 0) {
1374 log_err("fio: listen: %s\n", strerror(errno));
1381 int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1382 struct in6_addr *inp6)
1388 ret = inet_pton(AF_INET6, host, inp6);
1390 ret = inet_pton(AF_INET, host, inp);
1393 struct hostent *hent;
1395 hent = gethostbyname(host);
1397 log_err("fio: failed to resolve <%s>\n", host);
1402 if (hent->h_addrtype != AF_INET6) {
1403 log_info("fio: falling back to IPv4\n");
1406 memcpy(inp6, hent->h_addr_list[0], 16);
1409 if (hent->h_addrtype != AF_INET) {
1410 log_err("fio: lookup type mismatch\n");
1413 memcpy(inp, hent->h_addr_list[0], 4);
1422 * Parse a host/ip/port string. Reads from 'str'.
1427 * *ptr is the host, *port is the port, inp is the destination.
1429 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1430 * For local domain sockets:
1431 * *ptr is the filename, *is_sock is 1.
1433 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
1434 int *port, struct in_addr *inp,
1435 struct in6_addr *inp6, int *ipv6)
1437 const char *host = str;
1443 *port = fio_net_port;
1446 if (!strncmp(str, "sock:", 5)) {
1447 *ptr = strdup(str + 5);
1454 * Is it ip:<ip or host>:port
1456 if (!strncmp(host, "ip:", 3))
1458 else if (!strncmp(host, "ip4:", 4))
1460 else if (!strncmp(host, "ip6:", 4)) {
1463 } else if (host[0] == ':') {
1464 /* String is :port */
1467 if (!lport || lport > 65535) {
1468 log_err("fio: bad server port %u\n", port);
1471 /* no hostname given, we are done */
1477 * If no port seen yet, check if there's a last ':' at the end
1480 portp = strchr(host, ',');
1484 lport = atoi(portp);
1485 if (!lport || lport > 65535) {
1486 log_err("fio: bad server port %u\n", port);
1498 *ptr = strdup(host);
1500 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1507 *port = fio_net_port;
1513 * Server arg should be one of:
1515 * sock:/path/to/socket
1519 * Where sock uses unix domain sockets, and ip binds the server to
1520 * a specific interface. If no arguments are given to the server, it
1521 * uses IP and binds to 0.0.0.0.
1524 static int fio_handle_server_arg(void)
1526 int port = fio_net_port;
1527 int is_sock, ret = 0;
1529 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1531 if (!fio_server_arg)
1534 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
1535 &port, &saddr_in.sin_addr,
1536 &saddr_in6.sin6_addr, &use_ipv6);
1538 if (!is_sock && bind_sock) {
1544 fio_net_port = port;
1545 saddr_in.sin_port = htons(port);
1546 saddr_in6.sin6_port = htons(port);
1550 static int fio_server(void)
1554 dprint(FD_NET, "starting server\n");
1556 if (fio_handle_server_arg())
1559 sk = fio_init_server_connection();
1563 ret = accept_loop(sk);
1567 if (fio_server_arg) {
1568 free(fio_server_arg);
1569 fio_server_arg = NULL;
1577 void fio_server_got_signal(int signal)
1579 if (signal == SIGPIPE)
1582 log_info("\nfio: terminating on signal %d\n", signal);
1587 static int check_existing_pidfile(const char *pidfile)
1594 if (stat(pidfile, &sb))
1597 f = fopen(pidfile, "r");
1601 if (fread(buf, sb.st_size, 1, f) <= 0) {
1608 if (kill(pid, SIGCONT) < 0)
1609 return errno != ESRCH;
1614 static int write_pid(pid_t pid, const char *pidfile)
1618 fpid = fopen(pidfile, "w");
1620 log_err("fio: failed opening pid file %s\n", pidfile);
1624 fprintf(fpid, "%u\n", (unsigned int) pid);
1630 * If pidfile is specified, background us.
1632 int fio_start_server(char *pidfile)
1639 WSAStartup(MAKEWORD(2, 2), &wsd);
1643 return fio_server();
1645 if (check_existing_pidfile(pidfile)) {
1646 log_err("fio: pidfile %s exists and server appears alive\n",
1653 log_err("fio: failed server fork: %s", strerror(errno));
1657 int ret = write_pid(pid, pidfile);
1663 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1665 close(STDIN_FILENO);
1666 close(STDOUT_FILENO);
1667 close(STDERR_FILENO);
1679 void fio_server_set_arg(const char *arg)
1681 fio_server_arg = strdup(arg);