11 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
26 #include "crc/crc16.h"
27 #include "lib/ieee754.h"
29 int fio_net_port = FIO_NET_PORT;
33 static int server_fd = -1;
34 static char *fio_server_arg;
35 static char *bind_sock;
36 static struct sockaddr_in saddr_in;
37 static struct sockaddr_in6 saddr_in6;
40 static unsigned int has_zlib = 1;
42 static unsigned int has_zlib = 0;
44 static unsigned int use_zlib;
46 struct fio_fork_item {
47 struct flist_head list;
54 /* Created on fork on new connection */
55 static FLIST_HEAD(conn_list);
57 /* Created on job fork from connection */
58 static FLIST_HEAD(job_list);
60 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
81 const char *fio_server_op(unsigned int op)
85 if (op < FIO_NET_CMD_NR)
86 return fio_server_ops[op];
88 sprintf(buf, "UNKNOWN/%d", op);
92 static ssize_t iov_total_len(const struct iovec *iov, int count)
104 static int fio_sendv_data(int sk, struct iovec *iov, int count)
106 ssize_t total_len = iov_total_len(iov, count);
110 ret = writev(sk, iov, count);
117 if (ret >= iov->iov_len) {
122 iov->iov_base += ret;
128 else if (errno == EAGAIN || errno == EINTR)
132 } while (!exit_backend);
143 int fio_send_data(int sk, const void *p, unsigned int len)
145 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
147 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
149 return fio_sendv_data(sk, &iov, 1);
152 int fio_recv_data(int sk, void *p, unsigned int len)
155 int ret = recv(sk, p, len, MSG_WAITALL);
165 else if (errno == EAGAIN || errno == EINTR)
169 } while (!exit_backend);
177 static int verify_convert_cmd(struct fio_net_cmd *cmd)
181 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
182 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
184 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
185 if (crc != cmd->cmd_crc16) {
186 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
187 cmd->cmd_crc16, crc);
191 cmd->version = le16_to_cpu(cmd->version);
192 cmd->opcode = le16_to_cpu(cmd->opcode);
193 cmd->flags = le32_to_cpu(cmd->flags);
194 cmd->tag = le64_to_cpu(cmd->tag);
195 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
197 switch (cmd->version) {
201 log_err("fio: bad server cmd version %d\n", cmd->version);
205 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
206 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
214 * Read (and defragment, if necessary) incoming commands
216 struct fio_net_cmd *fio_net_recv_cmd(int sk)
218 struct fio_net_cmd cmd, *cmdret = NULL;
219 size_t cmd_size = 0, pdu_offset = 0;
225 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
229 /* We have a command, verify it and swap if need be */
230 ret = verify_convert_cmd(&cmd);
235 /* if this is text, add room for \0 at the end */
236 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
239 cmd_size += cmd.pdu_len;
241 cmdret = realloc(cmdret, cmd_size);
244 memcpy(cmdret, &cmd, sizeof(cmd));
245 else if (cmdret->opcode != cmd.opcode) {
246 log_err("fio: fragment opcode mismatch (%d != %d)\n",
247 cmdret->opcode, cmd.opcode);
255 /* There's payload, get it */
256 pdu = (void *) cmdret->payload + pdu_offset;
257 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
261 /* Verify payload crc */
262 crc = fio_crc16(pdu, cmd.pdu_len);
263 if (crc != cmd.pdu_crc16) {
264 log_err("fio: server bad crc on payload ");
265 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
270 pdu_offset += cmd.pdu_len;
272 cmdret->pdu_len += cmd.pdu_len;
274 } while (cmd.flags & FIO_NET_CMD_F_MORE);
280 /* zero-terminate text input */
281 if (cmdret->pdu_len) {
282 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
283 struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload;
284 char *buf = (char *) pdu->buf;
286 buf[pdu->buf_len] = '\0';
287 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
288 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload;
289 char *buf = (char *) pdu->buf;
290 int len = le32_to_cpu(pdu->buf_len);
296 /* frag flag is internal */
297 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
303 static void add_reply(uint64_t tag, struct flist_head *list)
305 struct fio_net_cmd_reply *reply;
307 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
308 flist_add_tail(&reply->list, list);
311 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
313 struct fio_net_cmd_reply *reply;
315 reply = calloc(1, sizeof(*reply));
316 INIT_FLIST_HEAD(&reply->list);
317 gettimeofday(&reply->tv, NULL);
318 reply->saved_tag = tag;
319 reply->opcode = opcode;
321 return (uintptr_t) reply;
324 static void free_reply(uint64_t tag)
326 struct fio_net_cmd_reply *reply;
328 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
332 void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
336 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
338 pdu_len = le32_to_cpu(cmd->pdu_len);
339 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
342 void fio_net_cmd_crc(struct fio_net_cmd *cmd)
344 fio_net_cmd_crc_pdu(cmd, cmd->payload);
347 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
348 uint64_t *tagptr, struct flist_head *list)
350 struct fio_net_cmd *cmd = NULL;
351 size_t this_len, cur_len = 0;
357 tag = *tagptr = alloc_reply(*tagptr, opcode);
359 tag = tagptr ? *tagptr : 0;
363 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
364 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
366 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
370 cur_len = sizeof(*cmd) + this_len;
371 cmd = malloc(cur_len);
374 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
377 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
379 fio_net_cmd_crc(cmd);
381 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
384 } while (!ret && size);
390 add_reply(tag, list);
399 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
401 struct fio_net_cmd cmd;
403 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
404 fio_net_cmd_crc(&cmd);
406 return fio_send_data(sk, &cmd, sizeof(cmd));
410 * If 'list' is non-NULL, then allocate and store the sent command for
411 * later verification.
413 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
414 struct flist_head *list)
419 tag = alloc_reply(tag, opcode);
421 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
430 add_reply(tag, list);
435 int fio_net_send_quit(int sk)
437 dprint(FD_NET, "server: sending quit\n");
439 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
442 static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
445 struct cmd_end_pdu epdu;
451 epdu.error = __cpu_to_le32(error);
452 epdu.signal = __cpu_to_le32(signal);
453 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
456 int fio_net_send_stop(int sk, int error, int signal)
458 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
459 return fio_net_send_ack(sk, NULL, error, signal);
462 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
464 struct fio_fork_item *ffi;
466 ffi = malloc(sizeof(*ffi));
471 flist_add_tail(&ffi->list, list);
474 static void fio_server_add_conn_pid(pid_t pid)
476 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", pid);
477 fio_server_add_fork_item(pid, &conn_list);
480 static void fio_server_add_job_pid(pid_t pid)
482 dprint(FD_NET, "server: forked off job job (pid=%u)\n", pid);
483 fio_server_add_fork_item(pid, &job_list);
486 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
490 ret = waitpid(ffi->pid, &status, WNOHANG);
492 if (errno == ECHILD) {
493 log_err("fio: connection pid %u disappeared\n", ffi->pid);
496 log_err("fio: waitpid: %s\n", strerror(errno));
497 } else if (ret == ffi->pid) {
498 if (WIFSIGNALED(status)) {
499 ffi->signal = WTERMSIG(status);
502 if (WIFEXITED(status)) {
503 if (WEXITSTATUS(status))
504 ffi->exitval = WEXITSTATUS(status);
510 static void fio_server_fork_item_done(struct fio_fork_item *ffi)
512 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", ffi->pid, ffi->signal, ffi->exitval);
515 * Fold STOP and QUIT...
517 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
518 fio_net_send_quit(server_fd);
519 flist_del(&ffi->list);
523 static void fio_server_check_fork_items(struct flist_head *list)
525 struct flist_head *entry, *tmp;
526 struct fio_fork_item *ffi;
528 flist_for_each_safe(entry, tmp, list) {
529 ffi = flist_entry(entry, struct fio_fork_item, list);
531 fio_server_check_fork_item(ffi);
534 fio_server_fork_item_done(ffi);
538 static void fio_server_check_jobs(void)
540 fio_server_check_fork_items(&job_list);
543 static void fio_server_check_conns(void)
545 fio_server_check_fork_items(&conn_list);
548 static int handle_run_cmd(struct fio_net_cmd *cmd)
557 fio_server_add_job_pid(pid);
566 static int handle_job_cmd(struct fio_net_cmd *cmd)
568 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
569 void *buf = pdu->buf;
570 struct cmd_start_pdu spdu;
572 pdu->buf_len = le32_to_cpu(pdu->buf_len);
573 pdu->client_type = le32_to_cpu(pdu->client_type);
575 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
576 fio_net_send_quit(server_fd);
580 spdu.jobs = cpu_to_le32(thread_number);
581 spdu.stat_outputs = cpu_to_le32(stat_number);
582 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
586 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
588 void *pdu = cmd->payload;
589 struct cmd_single_line_pdu *cslp;
590 struct cmd_line_pdu *clp;
591 unsigned long offset;
592 struct cmd_start_pdu spdu;
597 clp->lines = le16_to_cpu(clp->lines);
598 clp->client_type = le16_to_cpu(clp->client_type);
599 argv = malloc(clp->lines * sizeof(char *));
600 offset = sizeof(*clp);
602 dprint(FD_NET, "server: %d command line args\n", clp->lines);
604 for (i = 0; i < clp->lines; i++) {
606 argv[i] = (char *) cslp->text;
608 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
609 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
612 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
613 fio_net_send_quit(server_fd);
620 spdu.jobs = cpu_to_le32(thread_number);
621 spdu.stat_outputs = cpu_to_le32(stat_number);
622 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
626 static int handle_probe_cmd(struct fio_net_cmd *cmd)
628 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
629 struct cmd_probe_reply_pdu probe;
630 uint64_t tag = cmd->tag;
632 dprint(FD_NET, "server: sending probe reply\n");
634 memset(&probe, 0, sizeof(probe));
635 gethostname((char *) probe.hostname, sizeof(probe.hostname));
636 #ifdef CONFIG_BIG_ENDIAN
639 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
642 probe.arch = FIO_ARCH;
643 probe.bpp = sizeof(void *);
644 probe.cpus = __cpu_to_le32(cpus_online());
647 * If the client supports compression and we do too, then enable it
649 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
650 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
657 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
660 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
664 uint64_t tag = cmd->tag;
670 size = sizeof(*je) + thread_number * sizeof(char) + 1;
674 if (!calc_thread_status(je, 1)) {
679 dprint(FD_NET, "server sending status\n");
681 je->nr_running = cpu_to_le32(je->nr_running);
682 je->nr_ramp = cpu_to_le32(je->nr_ramp);
683 je->nr_pending = cpu_to_le32(je->nr_pending);
684 je->files_open = cpu_to_le32(je->files_open);
686 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
687 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
688 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
689 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
690 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
691 je->rate[i] = cpu_to_le32(je->rate[i]);
692 je->iops[i] = cpu_to_le32(je->iops[i]);
695 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
696 je->eta_sec = cpu_to_le64(je->eta_sec);
697 je->nr_threads = cpu_to_le32(je->nr_threads);
698 je->is_pow2 = cpu_to_le32(je->is_pow2);
699 je->unit_base = cpu_to_le32(je->unit_base);
701 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
706 static int send_update_job_reply(int fd, uint64_t __tag, int error)
708 uint64_t tag = __tag;
711 pdu_error = __cpu_to_le32(error);
712 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
715 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
717 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
718 struct thread_data *td;
721 tnumber = le32_to_cpu(pdu->thread_number);
723 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
725 if (!tnumber || tnumber > thread_number) {
726 send_update_job_reply(server_fd, cmd->tag, ENODEV);
730 td = &threads[tnumber - 1];
731 convert_thread_options_to_cpu(&td->o, &pdu->top);
732 send_update_job_reply(server_fd, cmd->tag, 0);
736 static int handle_command(struct fio_net_cmd *cmd)
740 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
741 fio_server_op(cmd->opcode), cmd->pdu_len,
742 (unsigned long long) cmd->tag);
744 switch (cmd->opcode) {
745 case FIO_NET_CMD_QUIT:
746 fio_terminate_threads(TERMINATE_ALL);
748 case FIO_NET_CMD_EXIT:
751 case FIO_NET_CMD_JOB:
752 ret = handle_job_cmd(cmd);
754 case FIO_NET_CMD_JOBLINE:
755 ret = handle_jobline_cmd(cmd);
757 case FIO_NET_CMD_PROBE:
758 ret = handle_probe_cmd(cmd);
760 case FIO_NET_CMD_SEND_ETA:
761 ret = handle_send_eta_cmd(cmd);
763 case FIO_NET_CMD_RUN:
764 ret = handle_run_cmd(cmd);
766 case FIO_NET_CMD_UPDATE_JOB:
767 ret = handle_update_job_cmd(cmd);
770 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
777 static int handle_connection(int sk)
779 struct fio_net_cmd *cmd = NULL;
783 INIT_FLIST_HEAD(&job_list);
787 while (!exit_backend) {
788 struct pollfd pfd = {
797 if (!flist_empty(&job_list))
800 ret = poll(&pfd, 1, timeout);
804 log_err("fio: poll: %s\n", strerror(errno));
807 fio_server_check_jobs();
811 if (pfd.revents & POLLIN)
813 if (pfd.revents & (POLLERR|POLLHUP)) {
817 } while (!exit_backend);
819 fio_server_check_jobs();
824 cmd = fio_net_recv_cmd(sk);
830 ret = handle_command(cmd);
845 static int accept_loop(int listen_sk)
847 struct sockaddr_in addr;
848 socklen_t len = sizeof(addr);
850 int ret = 0, sk, flags, exitval = 0;
852 dprint(FD_NET, "server enter accept loop\n");
854 flags = fcntl(listen_sk, F_GETFL);
856 fcntl(listen_sk, F_SETFL, flags);
858 while (!exit_backend) {
866 if (!flist_empty(&conn_list))
869 ret = poll(&pfd, 1, timeout);
873 log_err("fio: poll: %s\n", strerror(errno));
876 fio_server_check_conns();
880 if (pfd.revents & POLLIN)
882 } while (!exit_backend);
884 fio_server_check_conns();
886 if (exit_backend || ret < 0)
889 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
891 log_err("fio: accept: %s\n", strerror(errno));
895 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
900 fio_server_add_conn_pid(pid);
905 handle_connection(sk);
911 int fio_server_text_output(int level, const char *buf, size_t len)
913 struct cmd_text_pdu *pdu;
918 return log_local_buf(buf, len);
920 tlen = sizeof(*pdu) + len;
923 pdu->level = __cpu_to_le32(level);
924 pdu->buf_len = __cpu_to_le32(len);
926 gettimeofday(&tv, NULL);
927 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
928 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
930 memcpy(pdu->buf, buf, len);
932 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
937 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
939 dst->max_val = cpu_to_le64(src->max_val);
940 dst->min_val = cpu_to_le64(src->min_val);
941 dst->samples = cpu_to_le64(src->samples);
944 * Encode to IEEE 754 for network transfer
946 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
947 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
950 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
954 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
955 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
956 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
957 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
958 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
959 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
960 dst->agg[i] = cpu_to_le64(src->agg[i]);
963 dst->kb_base = cpu_to_le32(src->kb_base);
964 dst->unit_base = cpu_to_le32(src->unit_base);
965 dst->groupid = cpu_to_le32(src->groupid);
966 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
970 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
971 * into a single payload.
973 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
978 dprint(FD_NET, "server sending end stats\n");
980 memset(&p, 0, sizeof(p));
982 strcpy(p.ts.name, ts->name);
983 strcpy(p.ts.verror, ts->verror);
984 strcpy(p.ts.description, ts->description);
986 p.ts.error = cpu_to_le32(ts->error);
987 p.ts.thread_number = cpu_to_le32(ts->thread_number);
988 p.ts.groupid = cpu_to_le32(ts->groupid);
989 p.ts.pid = cpu_to_le32(ts->pid);
990 p.ts.members = cpu_to_le32(ts->members);
991 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
993 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
994 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
995 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
996 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
997 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1000 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1001 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1002 p.ts.ctx = cpu_to_le64(ts->ctx);
1003 p.ts.minf = cpu_to_le64(ts->minf);
1004 p.ts.majf = cpu_to_le64(ts->majf);
1005 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1007 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1008 fio_fp64_t *src = &ts->percentile_list[i];
1009 fio_fp64_t *dst = &p.ts.percentile_list[i];
1011 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
1014 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1015 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1016 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1017 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1020 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1021 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1022 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1025 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1026 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1027 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1029 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1030 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1031 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1034 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1035 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1037 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1038 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1039 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1042 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1043 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1044 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1045 p.ts.first_error = cpu_to_le32(ts->first_error);
1046 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1047 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1049 convert_gs(&p.rs, rs);
1051 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
1054 void fio_server_send_gs(struct group_run_stats *rs)
1056 struct group_run_stats gs;
1058 dprint(FD_NET, "server sending group run stats\n");
1060 convert_gs(&gs, rs);
1061 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
1064 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1068 for (i = 0; i < 2; i++) {
1069 dst->ios[i] = cpu_to_le32(src->ios[i]);
1070 dst->merges[i] = cpu_to_le32(src->merges[i]);
1071 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1072 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1075 dst->io_ticks = cpu_to_le32(src->io_ticks);
1076 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1077 dst->slavecount = cpu_to_le32(src->slavecount);
1078 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1081 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1085 strcpy((char *) dst->name, (char *) src->name);
1087 for (i = 0; i < 2; i++) {
1088 dst->ios[i] = cpu_to_le32(src->ios[i]);
1089 dst->merges[i] = cpu_to_le32(src->merges[i]);
1090 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1091 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
1094 dst->io_ticks = cpu_to_le32(src->io_ticks);
1095 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
1096 dst->msec = cpu_to_le64(src->msec);
1099 void fio_server_send_du(void)
1101 struct disk_util *du;
1102 struct flist_head *entry;
1103 struct cmd_du_pdu pdu;
1105 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1107 memset(&pdu, 0, sizeof(pdu));
1109 flist_for_each(entry, &disk_list) {
1110 du = flist_entry(entry, struct disk_util, list);
1112 convert_dus(&pdu.dus, &du->dus);
1113 convert_agg(&pdu.agg, &du->agg);
1115 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
1120 * Send a command with a separate PDU, not inlined in the command
1122 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1123 off_t size, uint64_t tag, uint32_t flags)
1125 struct fio_net_cmd cmd;
1126 struct iovec iov[2];
1128 iov[0].iov_base = &cmd;
1129 iov[0].iov_len = sizeof(cmd);
1130 iov[1].iov_base = (void *) buf;
1131 iov[1].iov_len = size;
1133 __fio_init_net_cmd(&cmd, opcode, size, tag);
1134 cmd.flags = __cpu_to_le32(flags);
1135 fio_net_cmd_crc_pdu(&cmd, buf);
1137 return fio_sendv_data(sk, iov, 2);
1140 static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
1148 * Dirty - since the log is potentially huge, compress it into
1149 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1150 * side defragment it.
1152 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1154 stream.zalloc = Z_NULL;
1155 stream.zfree = Z_NULL;
1156 stream.opaque = Z_NULL;
1158 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
1163 stream.next_in = (void *) log->log;
1164 stream.avail_in = log->nr_samples * sizeof(struct io_sample);
1167 unsigned int this_len, flags = 0;
1170 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1171 stream.next_out = out_pdu;
1172 ret = deflate(&stream, Z_FINISH);
1173 /* may be Z_OK, or Z_STREAM_END */
1177 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1179 if (stream.avail_in)
1180 flags = FIO_NET_CMD_F_MORE;
1182 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1183 out_pdu, this_len, 0, flags);
1186 } while (stream.avail_in);
1189 deflateEnd(&stream);
1196 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1198 struct cmd_iolog_pdu pdu;
1201 pdu.thread_number = cpu_to_le32(td->thread_number);
1202 pdu.nr_samples = __cpu_to_le32(log->nr_samples);
1203 pdu.log_type = cpu_to_le32(log->log_type);
1204 pdu.compressed = cpu_to_le32(use_zlib);
1205 strcpy((char *) pdu.name, name);
1207 for (i = 0; i < log->nr_samples; i++) {
1208 struct io_sample *s = &log->log[i];
1210 s->time = cpu_to_le64(s->time);
1211 s->val = cpu_to_le64(s->val);
1212 s->ddir = cpu_to_le32(s->ddir);
1213 s->bs = cpu_to_le32(s->bs);
1217 * Send header first, it's not compressed.
1219 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1220 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1225 * Now send actual log, compress if we can, otherwise just plain
1228 return fio_send_iolog_gz(&pdu, log);
1230 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
1231 log->nr_samples * sizeof(struct io_sample), 0, 0);
1234 void fio_server_send_add_job(struct thread_data *td)
1236 struct cmd_add_job_pdu pdu;
1238 memset(&pdu, 0, sizeof(pdu));
1239 pdu.thread_number = cpu_to_le32(td->thread_number);
1240 pdu.groupid = cpu_to_le32(td->groupid);
1241 convert_thread_options_to_net(&pdu.top, &td->o);
1243 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
1246 void fio_server_send_start(struct thread_data *td)
1248 assert(server_fd != -1);
1250 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1253 static int fio_init_server_ip(void)
1255 struct sockaddr *addr;
1260 sk = socket(AF_INET6, SOCK_STREAM, 0);
1262 sk = socket(AF_INET, SOCK_STREAM, 0);
1265 log_err("fio: socket: %s\n", strerror(errno));
1270 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1271 log_err("fio: setsockopt: %s\n", strerror(errno));
1276 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
1277 log_err("fio: setsockopt: %s\n", strerror(errno));
1284 addr = (struct sockaddr *) &saddr_in6;
1285 socklen = sizeof(saddr_in6);
1286 saddr_in6.sin6_family = AF_INET6;
1288 addr = (struct sockaddr *) &saddr_in;
1289 socklen = sizeof(saddr_in);
1290 saddr_in.sin_family = AF_INET;
1293 if (bind(sk, addr, socklen) < 0) {
1294 log_err("fio: bind: %s\n", strerror(errno));
1302 static int fio_init_server_sock(void)
1304 struct sockaddr_un addr;
1309 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1311 log_err("fio: socket: %s\n", strerror(errno));
1317 memset(&addr, 0, sizeof(addr));
1318 addr.sun_family = AF_UNIX;
1319 strcpy(addr.sun_path, bind_sock);
1322 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1324 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1325 log_err("fio: bind: %s\n", strerror(errno));
1334 static int fio_init_server_connection(void)
1339 dprint(FD_NET, "starting server\n");
1342 sk = fio_init_server_ip();
1344 sk = fio_init_server_sock();
1356 src = &saddr_in6.sin6_addr;
1359 src = &saddr_in.sin_addr;
1362 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1364 sprintf(port, ",%u", fio_net_port);
1368 strcpy(bind_str, port);
1370 strcpy(bind_str, bind_sock);
1372 log_info("fio: server listening on %s\n", bind_str);
1374 if (listen(sk, 0) < 0) {
1375 log_err("fio: listen: %s\n", strerror(errno));
1382 int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp,
1383 struct in6_addr *inp6)
1389 ret = inet_pton(AF_INET6, host, inp6);
1391 ret = inet_pton(AF_INET, host, inp);
1394 struct hostent *hent;
1396 hent = gethostbyname(host);
1398 log_err("fio: failed to resolve <%s>\n", host);
1403 if (hent->h_addrtype != AF_INET6) {
1404 log_info("fio: falling back to IPv4\n");
1407 memcpy(inp6, hent->h_addr_list[0], 16);
1410 if (hent->h_addrtype != AF_INET) {
1411 log_err("fio: lookup type mismatch\n");
1414 memcpy(inp, hent->h_addr_list[0], 4);
1423 * Parse a host/ip/port string. Reads from 'str'.
1428 * *ptr is the host, *port is the port, inp is the destination.
1430 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1431 * For local domain sockets:
1432 * *ptr is the filename, *is_sock is 1.
1434 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
1435 int *port, struct in_addr *inp,
1436 struct in6_addr *inp6, int *ipv6)
1438 const char *host = str;
1444 *port = fio_net_port;
1447 if (!strncmp(str, "sock:", 5)) {
1448 *ptr = strdup(str + 5);
1455 * Is it ip:<ip or host>:port
1457 if (!strncmp(host, "ip:", 3))
1459 else if (!strncmp(host, "ip4:", 4))
1461 else if (!strncmp(host, "ip6:", 4)) {
1464 } else if (host[0] == ':') {
1465 /* String is :port */
1468 if (!lport || lport > 65535) {
1469 log_err("fio: bad server port %u\n", port);
1472 /* no hostname given, we are done */
1478 * If no port seen yet, check if there's a last ':' at the end
1481 portp = strchr(host, ',');
1485 lport = atoi(portp);
1486 if (!lport || lport > 65535) {
1487 log_err("fio: bad server port %u\n", port);
1499 *ptr = strdup(host);
1501 if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) {
1508 *port = fio_net_port;
1514 * Server arg should be one of:
1516 * sock:/path/to/socket
1520 * Where sock uses unix domain sockets, and ip binds the server to
1521 * a specific interface. If no arguments are given to the server, it
1522 * uses IP and binds to 0.0.0.0.
1525 static int fio_handle_server_arg(void)
1527 int port = fio_net_port;
1528 int is_sock, ret = 0;
1530 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1532 if (!fio_server_arg)
1535 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
1536 &port, &saddr_in.sin_addr,
1537 &saddr_in6.sin6_addr, &use_ipv6);
1539 if (!is_sock && bind_sock) {
1545 fio_net_port = port;
1546 saddr_in.sin_port = htons(port);
1547 saddr_in6.sin6_port = htons(port);
1551 static int fio_server(void)
1555 dprint(FD_NET, "starting server\n");
1557 if (fio_handle_server_arg())
1560 sk = fio_init_server_connection();
1564 ret = accept_loop(sk);
1568 if (fio_server_arg) {
1569 free(fio_server_arg);
1570 fio_server_arg = NULL;
1578 void fio_server_got_signal(int signal)
1580 if (signal == SIGPIPE)
1583 log_info("\nfio: terminating on signal %d\n", signal);
1588 static int check_existing_pidfile(const char *pidfile)
1595 if (stat(pidfile, &sb))
1598 f = fopen(pidfile, "r");
1602 if (fread(buf, sb.st_size, 1, f) <= 0) {
1609 if (kill(pid, SIGCONT) < 0)
1610 return errno != ESRCH;
1615 static int write_pid(pid_t pid, const char *pidfile)
1619 fpid = fopen(pidfile, "w");
1621 log_err("fio: failed opening pid file %s\n", pidfile);
1625 fprintf(fpid, "%u\n", (unsigned int) pid);
1631 * If pidfile is specified, background us.
1633 int fio_start_server(char *pidfile)
1640 WSAStartup(MAKEWORD(2, 2), &wsd);
1644 return fio_server();
1646 if (check_existing_pidfile(pidfile)) {
1647 log_err("fio: pidfile %s exists and server appears alive\n",
1654 log_err("fio: failed server fork: %s", strerror(errno));
1658 int ret = write_pid(pid, pidfile);
1664 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1666 close(STDIN_FILENO);
1667 close(STDOUT_FILENO);
1668 close(STDERR_FILENO);
1680 void fio_server_set_arg(const char *arg)
1682 fio_server_arg = strdup(arg);