8 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <arpa/inet.h>
24 #include "crc/crc16.h"
25 #include "lib/ieee754.h"
26 #include "verify-state.h"
29 int fio_net_port = FIO_NET_PORT;
31 bool exit_backend = false;
42 struct flist_head list; /* link on sk_out->list */
43 int flags; /* SK_F_* */
44 int opcode; /* Actual command fields */
48 struct flist_head next; /* Other sk_entry's, if linked command */
51 static char *fio_server_arg;
52 static char *bind_sock;
53 static struct sockaddr_in saddr_in;
54 static struct sockaddr_in6 saddr_in6;
57 static unsigned int has_zlib = 1;
59 static unsigned int has_zlib = 0;
61 static unsigned int use_zlib;
64 static pthread_key_t sk_out_key;
66 struct fio_fork_item {
67 struct flist_head list;
81 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
107 static void sk_lock(struct sk_out *sk_out)
109 fio_sem_down(&sk_out->lock);
112 static void sk_unlock(struct sk_out *sk_out)
114 fio_sem_up(&sk_out->lock);
117 void sk_out_assign(struct sk_out *sk_out)
125 pthread_setspecific(sk_out_key, sk_out);
128 static void sk_out_free(struct sk_out *sk_out)
130 __fio_sem_remove(&sk_out->lock);
131 __fio_sem_remove(&sk_out->wait);
132 __fio_sem_remove(&sk_out->xmit);
136 static int __sk_out_drop(struct sk_out *sk_out)
142 assert(sk_out->refs != 0);
143 refs = --sk_out->refs;
148 pthread_setspecific(sk_out_key, NULL);
156 void sk_out_drop(void)
158 struct sk_out *sk_out;
160 sk_out = pthread_getspecific(sk_out_key);
161 __sk_out_drop(sk_out);
164 static void __fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
165 uint32_t pdu_len, uint64_t tag)
167 memset(cmd, 0, sizeof(*cmd));
169 cmd->version = __cpu_to_le16(FIO_SERVER_VER);
170 cmd->opcode = cpu_to_le16(opcode);
171 cmd->tag = cpu_to_le64(tag);
172 cmd->pdu_len = cpu_to_le32(pdu_len);
176 static void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
177 const void *pdu, uint32_t pdu_len, uint64_t tag)
179 __fio_init_net_cmd(cmd, opcode, pdu_len, tag);
182 memcpy(&cmd->payload, pdu, pdu_len);
185 const char *fio_server_op(unsigned int op)
189 if (op < FIO_NET_CMD_NR)
190 return fio_server_ops[op];
192 sprintf(buf, "UNKNOWN/%d", op);
196 static ssize_t iov_total_len(const struct iovec *iov, int count)
208 static int fio_sendv_data(int sk, struct iovec *iov, int count)
210 ssize_t total_len = iov_total_len(iov, count);
214 ret = writev(sk, iov, count);
221 if (ret >= iov->iov_len) {
226 iov->iov_base += ret;
232 else if (errno == EAGAIN || errno == EINTR)
236 } while (!exit_backend);
244 static int fio_send_data(int sk, const void *p, unsigned int len)
246 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
248 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
250 return fio_sendv_data(sk, &iov, 1);
253 static int fio_recv_data(int sk, void *buf, unsigned int len, bool wait)
261 flags = OS_MSG_DONTWAIT;
264 int ret = recv(sk, p, len, flags);
274 else if (errno == EAGAIN || errno == EINTR) {
280 } while (!exit_backend);
288 static int verify_convert_cmd(struct fio_net_cmd *cmd)
292 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
293 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
295 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
296 if (crc != cmd->cmd_crc16) {
297 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
298 cmd->cmd_crc16, crc);
299 fprintf(f_err, "fio: server bad crc on command (got %x, wanted %x)\n",
300 cmd->cmd_crc16, crc);
304 cmd->version = le16_to_cpu(cmd->version);
305 cmd->opcode = le16_to_cpu(cmd->opcode);
306 cmd->flags = le32_to_cpu(cmd->flags);
307 cmd->tag = le64_to_cpu(cmd->tag);
308 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
310 switch (cmd->version) {
314 log_err("fio: bad server cmd version %d\n", cmd->version);
315 fprintf(f_err, "fio: client/server version mismatch (%d != %d)\n",
316 cmd->version, FIO_SERVER_VER);
320 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
321 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
329 * Read (and defragment, if necessary) incoming commands
331 struct fio_net_cmd *fio_net_recv_cmd(int sk, bool wait)
333 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
334 size_t cmd_size = 0, pdu_offset = 0;
340 ret = fio_recv_data(sk, &cmd, sizeof(cmd), wait);
344 /* We have a command, verify it and swap if need be */
345 ret = verify_convert_cmd(&cmd);
350 /* if this is text, add room for \0 at the end */
351 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
354 cmd_size += cmd.pdu_len;
356 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
357 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
362 tmp = realloc(cmdret, cmd_size);
364 log_err("fio: server failed allocating cmd\n");
371 memcpy(cmdret, &cmd, sizeof(cmd));
372 else if (cmdret->opcode != cmd.opcode) {
373 log_err("fio: fragment opcode mismatch (%d != %d)\n",
374 cmdret->opcode, cmd.opcode);
382 /* There's payload, get it */
383 pdu = (char *) cmdret->payload + pdu_offset;
384 ret = fio_recv_data(sk, pdu, cmd.pdu_len, wait);
388 /* Verify payload crc */
389 crc = fio_crc16(pdu, cmd.pdu_len);
390 if (crc != cmd.pdu_crc16) {
391 log_err("fio: server bad crc on payload ");
392 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
397 pdu_offset += cmd.pdu_len;
399 cmdret->pdu_len += cmd.pdu_len;
401 } while (cmd.flags & FIO_NET_CMD_F_MORE);
407 /* zero-terminate text input */
408 if (cmdret->pdu_len) {
409 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
410 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
411 char *buf = (char *) __pdu->buf;
413 buf[__pdu->buf_len] = '\0';
414 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
415 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
416 char *buf = (char *) __pdu->buf;
417 int len = le32_to_cpu(__pdu->buf_len);
423 /* frag flag is internal */
424 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
430 static void add_reply(uint64_t tag, struct flist_head *list)
432 struct fio_net_cmd_reply *reply;
434 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
435 flist_add_tail(&reply->list, list);
438 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
440 struct fio_net_cmd_reply *reply;
442 reply = calloc(1, sizeof(*reply));
443 INIT_FLIST_HEAD(&reply->list);
444 fio_gettime(&reply->ts, NULL);
445 reply->saved_tag = tag;
446 reply->opcode = opcode;
448 return (uintptr_t) reply;
451 static void free_reply(uint64_t tag)
453 struct fio_net_cmd_reply *reply;
455 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
459 static void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
463 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
465 pdu_len = le32_to_cpu(cmd->pdu_len);
466 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
469 static void fio_net_cmd_crc(struct fio_net_cmd *cmd)
471 fio_net_cmd_crc_pdu(cmd, cmd->payload);
474 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
475 uint64_t *tagptr, struct flist_head *list)
477 struct fio_net_cmd *cmd = NULL;
478 size_t this_len, cur_len = 0;
484 tag = *tagptr = alloc_reply(*tagptr, opcode);
486 tag = tagptr ? *tagptr : 0;
490 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
491 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
493 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
497 cur_len = sizeof(*cmd) + this_len;
498 cmd = malloc(cur_len);
501 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
504 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
506 fio_net_cmd_crc(cmd);
508 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
511 } while (!ret && size);
517 add_reply(tag, list);
526 static struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf,
527 size_t size, uint64_t *tagptr,
530 struct sk_entry *entry;
532 entry = smalloc(sizeof(*entry));
536 INIT_FLIST_HEAD(&entry->next);
537 entry->opcode = opcode;
538 if (flags & SK_F_COPY) {
539 entry->buf = smalloc(size);
540 memcpy(entry->buf, buf, size);
546 entry->tag = *tagptr;
549 entry->flags = flags;
553 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry);
555 static void fio_net_queue_entry(struct sk_entry *entry)
557 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
559 if (entry->flags & SK_F_INLINE)
560 handle_sk_entry(sk_out, entry);
563 flist_add_tail(&entry->list, &sk_out->list);
566 fio_sem_up(&sk_out->wait);
570 static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
571 uint64_t *tagptr, int flags)
573 struct sk_entry *entry;
575 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
577 fio_net_queue_entry(entry);
584 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
586 struct fio_net_cmd cmd;
588 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
589 fio_net_cmd_crc(&cmd);
591 return fio_send_data(sk, &cmd, sizeof(cmd));
595 * If 'list' is non-NULL, then allocate and store the sent command for
596 * later verification.
598 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
599 struct flist_head *list)
604 tag = alloc_reply(tag, opcode);
606 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
615 add_reply(tag, list);
620 static int fio_net_queue_quit(void)
622 dprint(FD_NET, "server: sending quit\n");
624 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, NULL, SK_F_SIMPLE);
627 int fio_net_send_quit(int sk)
629 dprint(FD_NET, "server: sending quit\n");
631 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
634 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
636 struct cmd_end_pdu epdu;
642 epdu.error = __cpu_to_le32(error);
643 epdu.signal = __cpu_to_le32(signal);
644 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
647 static int fio_net_queue_stop(int error, int signal)
649 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
650 return fio_net_send_ack(NULL, error, signal);
653 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
655 struct fio_fork_item *ffi;
657 ffi = malloc(sizeof(*ffi));
662 flist_add_tail(&ffi->list, list);
665 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
667 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
668 fio_server_add_fork_item(pid, conn_list);
671 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
673 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
674 fio_server_add_fork_item(pid, job_list);
677 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
681 ret = waitpid(ffi->pid, &status, WNOHANG);
683 if (errno == ECHILD) {
684 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
687 log_err("fio: waitpid: %s\n", strerror(errno));
688 } else if (ret == ffi->pid) {
689 if (WIFSIGNALED(status)) {
690 ffi->signal = WTERMSIG(status);
693 if (WIFEXITED(status)) {
694 if (WEXITSTATUS(status))
695 ffi->exitval = WEXITSTATUS(status);
701 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
703 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
706 * Fold STOP and QUIT...
709 fio_net_queue_stop(ffi->exitval, ffi->signal);
710 fio_net_queue_quit();
713 flist_del(&ffi->list);
717 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
719 struct flist_head *entry, *tmp;
720 struct fio_fork_item *ffi;
722 flist_for_each_safe(entry, tmp, list) {
723 ffi = flist_entry(entry, struct fio_fork_item, list);
725 fio_server_check_fork_item(ffi);
728 fio_server_fork_item_done(ffi, stop);
732 static void fio_server_check_jobs(struct flist_head *job_list)
734 fio_server_check_fork_items(job_list, true);
737 static void fio_server_check_conns(struct flist_head *conn_list)
739 fio_server_check_fork_items(conn_list, false);
742 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
744 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
745 void *file_name = pdu->file;
746 struct cmd_start_pdu spdu;
748 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
750 pdu->name_len = le16_to_cpu(pdu->name_len);
751 pdu->client_type = le16_to_cpu(pdu->client_type);
753 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
754 fio_net_queue_quit();
758 spdu.jobs = cpu_to_le32(thread_number);
759 spdu.stat_outputs = cpu_to_le32(stat_number);
760 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
764 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
765 struct fio_net_cmd *cmd)
770 sk_out_assign(sk_out);
777 fio_server_add_job_pid(job_list, pid);
781 ret = fio_backend(sk_out);
787 static int handle_job_cmd(struct fio_net_cmd *cmd)
789 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
790 void *buf = pdu->buf;
791 struct cmd_start_pdu spdu;
793 pdu->buf_len = le32_to_cpu(pdu->buf_len);
794 pdu->client_type = le32_to_cpu(pdu->client_type);
796 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
797 fio_net_queue_quit();
801 spdu.jobs = cpu_to_le32(thread_number);
802 spdu.stat_outputs = cpu_to_le32(stat_number);
804 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
808 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
810 void *pdu = cmd->payload;
811 struct cmd_single_line_pdu *cslp;
812 struct cmd_line_pdu *clp;
813 unsigned long offset;
814 struct cmd_start_pdu spdu;
819 clp->lines = le16_to_cpu(clp->lines);
820 clp->client_type = le16_to_cpu(clp->client_type);
821 argv = malloc(clp->lines * sizeof(char *));
822 offset = sizeof(*clp);
824 dprint(FD_NET, "server: %d command line args\n", clp->lines);
826 for (i = 0; i < clp->lines; i++) {
828 argv[i] = (char *) cslp->text;
830 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
831 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
834 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
835 fio_net_queue_quit();
842 spdu.jobs = cpu_to_le32(thread_number);
843 spdu.stat_outputs = cpu_to_le32(stat_number);
845 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
849 static int handle_probe_cmd(struct fio_net_cmd *cmd)
851 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
852 uint64_t tag = cmd->tag;
853 struct cmd_probe_reply_pdu probe = {
854 #ifdef CONFIG_BIG_ENDIAN
859 .bpp = sizeof(void *),
860 .cpus = __cpu_to_le32(cpus_online()),
863 dprint(FD_NET, "server: sending probe reply\n");
865 strcpy(me, (char *) pdu->server);
867 gethostname((char *) probe.hostname, sizeof(probe.hostname));
868 snprintf((char *) probe.fio_version, sizeof(probe.fio_version), "%s",
872 * If the client supports compression and we do too, then enable it
874 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
875 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
882 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
885 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
888 uint64_t tag = cmd->tag;
892 dprint(FD_NET, "server sending status\n");
895 * Fake ETA return if we don't have a local one, otherwise the client
896 * will end up timing out waiting for a response to the ETA request
898 je = get_jobs_eta(true, &size);
901 je = calloc(1, size);
903 je->nr_running = cpu_to_le32(je->nr_running);
904 je->nr_ramp = cpu_to_le32(je->nr_ramp);
905 je->nr_pending = cpu_to_le32(je->nr_pending);
906 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
907 je->files_open = cpu_to_le32(je->files_open);
909 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
910 je->m_rate[i] = cpu_to_le64(je->m_rate[i]);
911 je->t_rate[i] = cpu_to_le64(je->t_rate[i]);
912 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
913 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
914 je->rate[i] = cpu_to_le64(je->rate[i]);
915 je->iops[i] = cpu_to_le32(je->iops[i]);
918 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
919 je->eta_sec = cpu_to_le64(je->eta_sec);
920 je->nr_threads = cpu_to_le32(je->nr_threads);
921 je->is_pow2 = cpu_to_le32(je->is_pow2);
922 je->unit_base = cpu_to_le32(je->unit_base);
925 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
929 static int send_update_job_reply(uint64_t __tag, int error)
931 uint64_t tag = __tag;
934 pdu_error = __cpu_to_le32(error);
935 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
938 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
940 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
941 struct thread_data *td;
944 tnumber = le32_to_cpu(pdu->thread_number);
946 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
948 if (!tnumber || tnumber > thread_number) {
949 send_update_job_reply(cmd->tag, ENODEV);
953 td = &threads[tnumber - 1];
954 convert_thread_options_to_cpu(&td->o, &pdu->top);
955 send_update_job_reply(cmd->tag, 0);
959 static int handle_trigger_cmd(struct fio_net_cmd *cmd, struct flist_head *job_list)
961 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
962 char *buf = (char *) pdu->cmd;
963 struct all_io_list *rep;
966 pdu->len = le16_to_cpu(pdu->len);
967 buf[pdu->len] = '\0';
969 rep = get_all_io_list(IO_LIST_ALL, &sz);
971 struct all_io_list state;
973 state.threads = cpu_to_le64((uint64_t) 0);
974 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY | SK_F_INLINE);
976 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE | SK_F_INLINE);
978 fio_terminate_threads(TERMINATE_ALL);
979 fio_server_check_jobs(job_list);
984 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
985 struct fio_net_cmd *cmd)
989 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
990 fio_server_op(cmd->opcode), cmd->pdu_len,
991 (unsigned long long) cmd->tag);
993 switch (cmd->opcode) {
994 case FIO_NET_CMD_QUIT:
995 fio_terminate_threads(TERMINATE_ALL);
998 case FIO_NET_CMD_EXIT:
1001 case FIO_NET_CMD_LOAD_FILE:
1002 ret = handle_load_file_cmd(cmd);
1004 case FIO_NET_CMD_JOB:
1005 ret = handle_job_cmd(cmd);
1007 case FIO_NET_CMD_JOBLINE:
1008 ret = handle_jobline_cmd(cmd);
1010 case FIO_NET_CMD_PROBE:
1011 ret = handle_probe_cmd(cmd);
1013 case FIO_NET_CMD_SEND_ETA:
1014 ret = handle_send_eta_cmd(cmd);
1016 case FIO_NET_CMD_RUN:
1017 ret = handle_run_cmd(sk_out, job_list, cmd);
1019 case FIO_NET_CMD_UPDATE_JOB:
1020 ret = handle_update_job_cmd(cmd);
1022 case FIO_NET_CMD_VTRIGGER:
1023 ret = handle_trigger_cmd(cmd, job_list);
1025 case FIO_NET_CMD_SENDFILE: {
1026 struct cmd_sendfile_reply *in;
1027 struct cmd_reply *rep;
1029 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
1031 in = (struct cmd_sendfile_reply *) cmd->payload;
1032 in->size = le32_to_cpu(in->size);
1033 in->error = le32_to_cpu(in->error);
1036 rep->error = in->error;
1039 rep->data = smalloc(in->size);
1042 rep->error = ENOMEM;
1044 rep->size = in->size;
1045 memcpy(rep->data, in->data, in->size);
1048 fio_sem_up(&rep->lock);
1052 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
1060 * Send a command with a separate PDU, not inlined in the command
1062 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1063 off_t size, uint64_t tag, uint32_t flags)
1065 struct fio_net_cmd cmd;
1066 struct iovec iov[2];
1070 iov[0].iov_base = (void *) &cmd;
1071 iov[0].iov_len = sizeof(cmd);
1074 uint32_t this_flags = flags;
1077 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
1078 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
1080 if (this_len < size)
1081 this_flags |= FIO_NET_CMD_F_MORE;
1083 __fio_init_net_cmd(&cmd, opcode, this_len, tag);
1084 cmd.flags = __cpu_to_le32(this_flags);
1085 fio_net_cmd_crc_pdu(&cmd, buf);
1087 iov[1].iov_base = (void *) buf;
1088 iov[1].iov_len = this_len;
1090 ret = fio_sendv_data(sk, iov, 2);
1093 } while (!ret && size);
1098 static void finish_entry(struct sk_entry *entry)
1100 if (entry->flags & SK_F_FREE)
1102 else if (entry->flags & SK_F_COPY)
1108 static void entry_set_flags(struct sk_entry *entry, struct flist_head *list,
1109 unsigned int *flags)
1111 if (!flist_empty(list))
1112 *flags = FIO_NET_CMD_F_MORE;
1117 static int send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1122 entry_set_flags(first, &first->next, &flags);
1124 ret = fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf,
1125 first->size, first->tag, flags);
1127 while (!flist_empty(&first->next)) {
1128 struct sk_entry *next;
1130 next = flist_first_entry(&first->next, struct sk_entry, list);
1131 flist_del_init(&next->list);
1133 entry_set_flags(next, &first->next, &flags);
1135 ret += fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf,
1136 next->size, next->tag, flags);
1143 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1147 fio_sem_down(&sk_out->xmit);
1149 if (entry->flags & SK_F_VEC)
1150 ret = send_vec_entry(sk_out, entry);
1151 else if (entry->flags & SK_F_SIMPLE) {
1152 ret = fio_net_send_simple_cmd(sk_out->sk, entry->opcode,
1155 ret = fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf,
1156 entry->size, &entry->tag, NULL);
1159 fio_sem_up(&sk_out->xmit);
1162 log_err("fio: failed handling cmd %s\n", fio_server_op(entry->opcode));
1164 finish_entry(entry);
1168 static int handle_xmits(struct sk_out *sk_out)
1170 struct sk_entry *entry;
1175 if (flist_empty(&sk_out->list)) {
1180 flist_splice_init(&sk_out->list, &list);
1183 while (!flist_empty(&list)) {
1184 entry = flist_entry(list.next, struct sk_entry, list);
1185 flist_del(&entry->list);
1186 ret += handle_sk_entry(sk_out, entry);
1192 static int handle_connection(struct sk_out *sk_out)
1194 struct fio_net_cmd *cmd = NULL;
1195 FLIST_HEAD(job_list);
1201 while (!exit_backend) {
1202 struct pollfd pfd = {
1210 if (!flist_empty(&job_list))
1213 handle_xmits(sk_out);
1215 ret = poll(&pfd, 1, 0);
1219 log_err("fio: poll: %s\n", strerror(errno));
1222 fio_server_check_jobs(&job_list);
1223 fio_sem_down_timeout(&sk_out->wait, timeout);
1227 if (pfd.revents & POLLIN)
1229 if (pfd.revents & (POLLERR|POLLHUP)) {
1233 } while (!exit_backend);
1235 fio_server_check_jobs(&job_list);
1240 cmd = fio_net_recv_cmd(sk_out->sk, true);
1246 ret = handle_command(sk_out, &job_list, cmd);
1257 handle_xmits(sk_out);
1261 __sk_out_drop(sk_out);
1265 /* get the address on this host bound by the input socket,
1266 * whether it is ipv6 or ipv4 */
1268 static int get_my_addr_str(int sk)
1270 struct sockaddr_in6 myaddr6 = { 0, };
1271 struct sockaddr_in myaddr4 = { 0, };
1272 struct sockaddr *sockaddr_p;
1278 len = sizeof(myaddr6);
1279 sockaddr_p = (struct sockaddr * )&myaddr6;
1280 net_addr = (char * )&myaddr6.sin6_addr;
1282 len = sizeof(myaddr4);
1283 sockaddr_p = (struct sockaddr * )&myaddr4;
1284 net_addr = (char * )&myaddr4.sin_addr;
1287 ret = getsockname(sk, sockaddr_p, &len);
1289 log_err("fio: getsockname: %s\n", strerror(errno));
1293 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1294 log_err("inet_ntop: failed to convert addr to string\n");
1298 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1302 static int accept_loop(int listen_sk)
1304 struct sockaddr_in addr;
1305 struct sockaddr_in6 addr6;
1306 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1308 int ret = 0, sk, exitval = 0;
1309 FLIST_HEAD(conn_list);
1311 dprint(FD_NET, "server enter accept loop\n");
1313 fio_set_fd_nonblocking(listen_sk, "server");
1315 while (!exit_backend) {
1316 struct sk_out *sk_out;
1322 pfd.events = POLLIN;
1326 if (!flist_empty(&conn_list))
1329 ret = poll(&pfd, 1, timeout);
1333 log_err("fio: poll: %s\n", strerror(errno));
1336 fio_server_check_conns(&conn_list);
1340 if (pfd.revents & POLLIN)
1342 } while (!exit_backend);
1344 fio_server_check_conns(&conn_list);
1346 if (exit_backend || ret < 0)
1350 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1352 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1355 log_err("fio: accept: %s\n", strerror(errno));
1360 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1362 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1364 dprint(FD_NET, "server: connect from %s\n", from);
1366 sk_out = scalloc(1, sizeof(*sk_out));
1373 INIT_FLIST_HEAD(&sk_out->list);
1374 __fio_sem_init(&sk_out->lock, FIO_SEM_UNLOCKED);
1375 __fio_sem_init(&sk_out->wait, FIO_SEM_LOCKED);
1376 __fio_sem_init(&sk_out->xmit, FIO_SEM_UNLOCKED);
1381 fio_server_add_conn_pid(&conn_list, pid);
1385 /* if error, it's already logged, non-fatal */
1386 get_my_addr_str(sk);
1389 * Assign sk_out here, it'll be dropped in handle_connection()
1390 * since that function calls _exit() when done
1392 sk_out_assign(sk_out);
1393 handle_connection(sk_out);
1399 int fio_server_text_output(int level, const char *buf, size_t len)
1401 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1402 struct cmd_text_pdu *pdu;
1406 if (!sk_out || sk_out->sk == -1)
1409 tlen = sizeof(*pdu) + len;
1412 pdu->level = __cpu_to_le32(level);
1413 pdu->buf_len = __cpu_to_le32(len);
1415 gettimeofday(&tv, NULL);
1416 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1417 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1419 memcpy(pdu->buf, buf, len);
1421 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1426 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1428 dst->max_val = cpu_to_le64(src->max_val);
1429 dst->min_val = cpu_to_le64(src->min_val);
1430 dst->samples = cpu_to_le64(src->samples);
1433 * Encode to IEEE 754 for network transfer
1435 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1436 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1439 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1443 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1444 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1445 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1446 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1447 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1448 dst->iobytes[i] = cpu_to_le64(src->iobytes[i]);
1449 dst->agg[i] = cpu_to_le64(src->agg[i]);
1452 dst->kb_base = cpu_to_le32(src->kb_base);
1453 dst->unit_base = cpu_to_le32(src->unit_base);
1454 dst->groupid = cpu_to_le32(src->groupid);
1455 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1456 dst->sig_figs = cpu_to_le32(src->sig_figs);
1460 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1461 * into a single payload.
1463 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1465 struct cmd_ts_pdu p;
1468 uint64_t *ss_iops, *ss_bw;
1470 dprint(FD_NET, "server sending end stats\n");
1472 memset(&p, 0, sizeof(p));
1474 snprintf(p.ts.name, sizeof(p.ts.name), "%s", ts->name);
1475 snprintf(p.ts.verror, sizeof(p.ts.verror), "%s", ts->verror);
1476 snprintf(p.ts.description, sizeof(p.ts.description), "%s",
1479 p.ts.error = cpu_to_le32(ts->error);
1480 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1481 p.ts.groupid = cpu_to_le32(ts->groupid);
1482 p.ts.pid = cpu_to_le32(ts->pid);
1483 p.ts.members = cpu_to_le32(ts->members);
1484 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1486 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1487 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1488 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1489 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1490 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1491 convert_io_stat(&p.ts.iops_stat[i], &ts->iops_stat[i]);
1494 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1495 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1496 p.ts.ctx = cpu_to_le64(ts->ctx);
1497 p.ts.minf = cpu_to_le64(ts->minf);
1498 p.ts.majf = cpu_to_le64(ts->majf);
1499 p.ts.clat_percentiles = cpu_to_le32(ts->clat_percentiles);
1500 p.ts.lat_percentiles = cpu_to_le32(ts->lat_percentiles);
1501 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1503 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1504 fio_fp64_t *src = &ts->percentile_list[i];
1505 fio_fp64_t *dst = &p.ts.percentile_list[i];
1507 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1510 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1511 p.ts.io_u_map[i] = cpu_to_le64(ts->io_u_map[i]);
1512 p.ts.io_u_submit[i] = cpu_to_le64(ts->io_u_submit[i]);
1513 p.ts.io_u_complete[i] = cpu_to_le64(ts->io_u_complete[i]);
1516 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
1517 p.ts.io_u_lat_n[i] = cpu_to_le64(ts->io_u_lat_n[i]);
1518 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
1519 p.ts.io_u_lat_u[i] = cpu_to_le64(ts->io_u_lat_u[i]);
1520 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
1521 p.ts.io_u_lat_m[i] = cpu_to_le64(ts->io_u_lat_m[i]);
1523 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1524 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1525 p.ts.io_u_plat[i][j] = cpu_to_le64(ts->io_u_plat[i][j]);
1527 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1528 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1529 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1530 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1533 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1534 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1535 p.ts.nr_zone_resets = cpu_to_le64(ts->nr_zone_resets);
1537 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1538 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1539 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1542 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1543 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1544 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1545 p.ts.first_error = cpu_to_le32(ts->first_error);
1546 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1547 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1549 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1550 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1551 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1552 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1554 p.ts.sig_figs = cpu_to_le32(ts->sig_figs);
1556 p.ts.nr_block_infos = cpu_to_le64(ts->nr_block_infos);
1557 for (i = 0; i < p.ts.nr_block_infos; i++)
1558 p.ts.block_infos[i] = cpu_to_le32(ts->block_infos[i]);
1560 p.ts.ss_dur = cpu_to_le64(ts->ss_dur);
1561 p.ts.ss_state = cpu_to_le32(ts->ss_state);
1562 p.ts.ss_head = cpu_to_le32(ts->ss_head);
1563 p.ts.ss_limit.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_limit.u.f));
1564 p.ts.ss_slope.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_slope.u.f));
1565 p.ts.ss_deviation.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_deviation.u.f));
1566 p.ts.ss_criterion.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_criterion.u.f));
1568 p.ts.cachehit = cpu_to_le64(ts->cachehit);
1569 p.ts.cachemiss = cpu_to_le64(ts->cachemiss);
1571 convert_gs(&p.rs, rs);
1573 dprint(FD_NET, "ts->ss_state = %d\n", ts->ss_state);
1574 if (ts->ss_state & FIO_SS_DATA) {
1575 dprint(FD_NET, "server sending steadystate ring buffers\n");
1577 ss_buf = malloc(sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t));
1579 memcpy(ss_buf, &p, sizeof(p));
1581 ss_iops = (uint64_t *) ((struct cmd_ts_pdu *)ss_buf + 1);
1582 ss_bw = ss_iops + (int) ts->ss_dur;
1583 for (i = 0; i < ts->ss_dur; i++) {
1584 ss_iops[i] = cpu_to_le64(ts->ss_iops_data[i]);
1585 ss_bw[i] = cpu_to_le64(ts->ss_bw_data[i]);
1588 fio_net_queue_cmd(FIO_NET_CMD_TS, ss_buf, sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t), NULL, SK_F_COPY);
1593 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1596 void fio_server_send_gs(struct group_run_stats *rs)
1598 struct group_run_stats gs;
1600 dprint(FD_NET, "server sending group run stats\n");
1602 convert_gs(&gs, rs);
1603 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1606 void fio_server_send_job_options(struct flist_head *opt_list,
1609 struct cmd_job_option pdu;
1610 struct flist_head *entry;
1612 if (flist_empty(opt_list))
1615 flist_for_each(entry, opt_list) {
1616 struct print_option *p;
1619 p = flist_entry(entry, struct print_option, list);
1620 memset(&pdu, 0, sizeof(pdu));
1623 pdu.global = __cpu_to_le16(1);
1627 pdu.groupid = cpu_to_le32(gid);
1629 len = strlen(p->name);
1630 if (len >= sizeof(pdu.name)) {
1631 len = sizeof(pdu.name) - 1;
1632 pdu.truncated = __cpu_to_le16(1);
1634 memcpy(pdu.name, p->name, len);
1636 len = strlen(p->value);
1637 if (len >= sizeof(pdu.value)) {
1638 len = sizeof(pdu.value) - 1;
1639 pdu.truncated = __cpu_to_le16(1);
1641 memcpy(pdu.value, p->value, len);
1643 fio_net_queue_cmd(FIO_NET_CMD_JOB_OPT, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1647 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1651 for (i = 0; i < 2; i++) {
1652 dst->ios[i] = cpu_to_le64(src->ios[i]);
1653 dst->merges[i] = cpu_to_le64(src->merges[i]);
1654 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1655 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1658 dst->io_ticks = cpu_to_le64(src->io_ticks);
1659 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1660 dst->slavecount = cpu_to_le32(src->slavecount);
1661 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1664 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1668 snprintf((char *) dst->name, sizeof(dst->name), "%s", src->name);
1670 for (i = 0; i < 2; i++) {
1671 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1672 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1673 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1674 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1677 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1678 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1679 dst->s.msec = cpu_to_le64(src->s.msec);
1682 void fio_server_send_du(void)
1684 struct disk_util *du;
1685 struct flist_head *entry;
1686 struct cmd_du_pdu pdu;
1688 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1690 memset(&pdu, 0, sizeof(pdu));
1692 flist_for_each(entry, &disk_list) {
1693 du = flist_entry(entry, struct disk_util, list);
1695 convert_dus(&pdu.dus, &du->dus);
1696 convert_agg(&pdu.agg, &du->agg);
1698 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1704 static inline void __fio_net_prep_tail(z_stream *stream, void *out_pdu,
1705 struct sk_entry **last_entry,
1706 struct sk_entry *first)
1708 unsigned int this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1710 *last_entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1711 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1713 flist_add_tail(&(*last_entry)->list, &first->next);
1717 * Deflates the next input given, creating as many new packets in the
1718 * linked list as necessary.
1720 static int __deflate_pdu_buffer(void *next_in, unsigned int next_sz, void **out_pdu,
1721 struct sk_entry **last_entry, z_stream *stream,
1722 struct sk_entry *first)
1726 stream->next_in = next_in;
1727 stream->avail_in = next_sz;
1729 if (!stream->avail_out) {
1730 __fio_net_prep_tail(stream, *out_pdu, last_entry, first);
1731 if (*last_entry == NULL)
1734 *out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1736 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1737 stream->next_out = *out_pdu;
1740 ret = deflate(stream, Z_BLOCK);
1746 } while (stream->avail_in);
1751 static int __fio_append_iolog_gz_hist(struct sk_entry *first, struct io_log *log,
1752 struct io_logs *cur_log, z_stream *stream)
1754 struct sk_entry *entry;
1757 int sample_sz = log_entry_sz(log);
1759 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1760 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1761 stream->next_out = out_pdu;
1763 for (i = 0; i < cur_log->nr_samples; i++) {
1764 struct io_sample *s;
1765 struct io_u_plat_entry *cur_plat_entry, *prev_plat_entry;
1766 uint64_t *cur_plat, *prev_plat;
1768 s = get_sample(log, cur_log, i);
1769 ret = __deflate_pdu_buffer(s, sample_sz, &out_pdu, &entry, stream, first);
1773 /* Do the subtraction on server side so that client doesn't have to
1774 * reconstruct our linked list from packets.
1776 cur_plat_entry = s->data.plat_entry;
1777 prev_plat_entry = flist_first_entry(&cur_plat_entry->list, struct io_u_plat_entry, list);
1778 cur_plat = cur_plat_entry->io_u_plat;
1779 prev_plat = prev_plat_entry->io_u_plat;
1781 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
1782 cur_plat[j] -= prev_plat[j];
1785 flist_del(&prev_plat_entry->list);
1786 free(prev_plat_entry);
1788 ret = __deflate_pdu_buffer(cur_plat_entry, sizeof(*cur_plat_entry),
1789 &out_pdu, &entry, stream, first);
1795 __fio_net_prep_tail(stream, out_pdu, &entry, first);
1796 return entry == NULL;
1799 static int __fio_append_iolog_gz(struct sk_entry *first, struct io_log *log,
1800 struct io_logs *cur_log, z_stream *stream)
1802 unsigned int this_len;
1806 if (log->log_type == IO_LOG_TYPE_HIST)
1807 return __fio_append_iolog_gz_hist(first, log, cur_log, stream);
1809 stream->next_in = (void *) cur_log->log;
1810 stream->avail_in = cur_log->nr_samples * log_entry_sz(log);
1813 struct sk_entry *entry;
1816 * Dirty - since the log is potentially huge, compress it into
1817 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1818 * side defragment it.
1820 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1822 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1823 stream->next_out = out_pdu;
1824 ret = deflate(stream, Z_BLOCK);
1825 /* may be Z_OK, or Z_STREAM_END */
1831 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1833 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1834 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1839 flist_add_tail(&entry->list, &first->next);
1840 } while (stream->avail_in);
1845 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1854 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK)
1857 while (!flist_empty(&log->io_logs)) {
1858 struct io_logs *cur_log;
1860 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1861 flist_del_init(&cur_log->list);
1863 ret = __fio_append_iolog_gz(first, log, cur_log, &stream);
1868 ret = deflate(&stream, Z_FINISH);
1870 while (ret != Z_STREAM_END) {
1871 struct sk_entry *entry;
1872 unsigned int this_len;
1875 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1876 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1877 stream.next_out = out_pdu;
1879 ret = deflate(&stream, Z_FINISH);
1880 /* may be Z_OK, or Z_STREAM_END */
1886 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1888 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1889 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1894 flist_add_tail(&entry->list, &first->next);
1895 } while (ret != Z_STREAM_END);
1897 ret = deflateEnd(&stream);
1904 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1910 static int fio_append_gz_chunks(struct sk_entry *first, struct io_log *log)
1912 struct sk_entry *entry;
1913 struct flist_head *node;
1916 pthread_mutex_lock(&log->chunk_lock);
1917 flist_for_each(node, &log->chunk_list) {
1918 struct iolog_compress *c;
1920 c = flist_entry(node, struct iolog_compress, list);
1921 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, c->buf, c->len,
1922 NULL, SK_F_VEC | SK_F_INLINE);
1927 flist_add_tail(&entry->list, &first->next);
1929 pthread_mutex_unlock(&log->chunk_lock);
1933 static int fio_append_text_log(struct sk_entry *first, struct io_log *log)
1935 struct sk_entry *entry;
1938 while (!flist_empty(&log->io_logs)) {
1939 struct io_logs *cur_log;
1942 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1943 flist_del_init(&cur_log->list);
1945 size = cur_log->nr_samples * log_entry_sz(log);
1947 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, cur_log->log, size,
1948 NULL, SK_F_VEC | SK_F_INLINE);
1953 flist_add_tail(&entry->list, &first->next);
1959 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1961 struct cmd_iolog_pdu pdu = {
1962 .nr_samples = cpu_to_le64(iolog_nr_samples(log)),
1963 .thread_number = cpu_to_le32(td->thread_number),
1964 .log_type = cpu_to_le32(log->log_type),
1965 .log_hist_coarseness = cpu_to_le32(log->hist_coarseness),
1967 struct sk_entry *first;
1968 struct flist_head *entry;
1971 if (!flist_empty(&log->chunk_list))
1972 pdu.compressed = __cpu_to_le32(STORE_COMPRESSED);
1974 pdu.compressed = __cpu_to_le32(XMIT_COMPRESSED);
1978 snprintf((char *) pdu.name, sizeof(pdu.name), "%s", name);
1981 * We can't do this for a pre-compressed log, but for that case,
1982 * log->nr_samples is zero anyway.
1984 flist_for_each(entry, &log->io_logs) {
1985 struct io_logs *cur_log;
1988 cur_log = flist_entry(entry, struct io_logs, list);
1990 for (i = 0; i < cur_log->nr_samples; i++) {
1991 struct io_sample *s = get_sample(log, cur_log, i);
1993 s->time = cpu_to_le64(s->time);
1994 s->data.val = cpu_to_le64(s->data.val);
1995 s->__ddir = cpu_to_le32(s->__ddir);
1996 s->bs = cpu_to_le64(s->bs);
1998 if (log->log_offset) {
1999 struct io_sample_offset *so = (void *) s;
2001 so->offset = cpu_to_le64(so->offset);
2007 * Assemble header entry first
2009 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_VEC | SK_F_INLINE | SK_F_COPY);
2014 * Now append actual log entries. If log compression was enabled on
2015 * the job, just send out the compressed chunks directly. If we
2016 * have a plain log, compress if we can, then send. Otherwise, send
2017 * the plain text output.
2019 if (!flist_empty(&log->chunk_list))
2020 ret = fio_append_gz_chunks(first, log);
2022 ret = fio_append_iolog_gz(first, log);
2024 ret = fio_append_text_log(first, log);
2026 fio_net_queue_entry(first);
2030 void fio_server_send_add_job(struct thread_data *td)
2032 struct cmd_add_job_pdu pdu = {
2033 .thread_number = cpu_to_le32(td->thread_number),
2034 .groupid = cpu_to_le32(td->groupid),
2037 convert_thread_options_to_net(&pdu.top, &td->o);
2039 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL,
2043 void fio_server_send_start(struct thread_data *td)
2045 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2047 assert(sk_out->sk != -1);
2049 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, NULL, SK_F_SIMPLE);
2052 int fio_server_get_verify_state(const char *name, int threadnumber,
2055 struct thread_io_list *s;
2056 struct cmd_sendfile out;
2057 struct cmd_reply *rep;
2062 dprint(FD_NET, "server: request verify state\n");
2064 rep = smalloc(sizeof(*rep));
2068 __fio_sem_init(&rep->lock, FIO_SEM_LOCKED);
2072 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
2074 tag = (uint64_t) (uintptr_t) rep;
2075 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag,
2079 * Wait for the backend to receive the reply
2081 if (fio_sem_down_timeout(&rep->lock, 10000)) {
2082 log_err("fio: timed out waiting for reply\n");
2088 log_err("fio: failure on receiving state file %s: %s\n",
2089 out.path, strerror(rep->error));
2094 fio_net_queue_quit();
2099 * The format is verify_state_hdr, then thread_io_list. Verify
2100 * the header, and the thread_io_list checksum
2102 s = rep->data + sizeof(struct verify_state_hdr);
2103 if (verify_state_hdr(rep->data, s)) {
2109 * Don't need the header from now, copy just the thread_io_list
2112 rep->size -= sizeof(struct verify_state_hdr);
2113 data = malloc(rep->size);
2114 memcpy(data, s, rep->size);
2118 __fio_sem_remove(&rep->lock);
2123 static int fio_init_server_ip(void)
2125 struct sockaddr *addr;
2132 sk = socket(AF_INET6, SOCK_STREAM, 0);
2134 sk = socket(AF_INET, SOCK_STREAM, 0);
2137 log_err("fio: socket: %s\n", strerror(errno));
2142 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
2143 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
2149 * Not fatal if fails, so just ignore it if that happens
2151 setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
2155 void *src = &saddr_in6.sin6_addr;
2157 addr = (struct sockaddr *) &saddr_in6;
2158 socklen = sizeof(saddr_in6);
2159 saddr_in6.sin6_family = AF_INET6;
2160 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
2162 void *src = &saddr_in.sin_addr;
2164 addr = (struct sockaddr *) &saddr_in;
2165 socklen = sizeof(saddr_in);
2166 saddr_in.sin_family = AF_INET;
2167 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
2170 if (bind(sk, addr, socklen) < 0) {
2171 log_err("fio: bind: %s\n", strerror(errno));
2172 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
2180 static int fio_init_server_sock(void)
2182 struct sockaddr_un addr;
2187 sk = socket(AF_UNIX, SOCK_STREAM, 0);
2189 log_err("fio: socket: %s\n", strerror(errno));
2195 addr.sun_family = AF_UNIX;
2196 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", bind_sock);
2198 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
2200 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
2201 log_err("fio: bind: %s\n", strerror(errno));
2210 static int fio_init_server_connection(void)
2215 dprint(FD_NET, "starting server\n");
2218 sk = fio_init_server_ip();
2220 sk = fio_init_server_sock();
2225 memset(bind_str, 0, sizeof(bind_str));
2234 src = &saddr_in6.sin6_addr;
2237 src = &saddr_in.sin_addr;
2240 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
2242 sprintf(port, ",%u", fio_net_port);
2246 snprintf(bind_str, sizeof(bind_str), "%s", port);
2248 snprintf(bind_str, sizeof(bind_str), "%s", bind_sock);
2250 log_info("fio: server listening on %s\n", bind_str);
2252 if (listen(sk, 4) < 0) {
2253 log_err("fio: listen: %s\n", strerror(errno));
2261 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
2262 struct in6_addr *inp6)
2268 ret = inet_pton(AF_INET6, host, inp6);
2270 ret = inet_pton(AF_INET, host, inp);
2273 struct addrinfo *res, hints = {
2274 .ai_family = ipv6 ? AF_INET6 : AF_INET,
2275 .ai_socktype = SOCK_STREAM,
2278 ret = getaddrinfo(host, NULL, &hints, &res);
2280 log_err("fio: failed to resolve <%s> (%s)\n", host,
2286 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
2288 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
2298 * Parse a host/ip/port string. Reads from 'str'.
2303 * *ptr is the host, *port is the port, inp is the destination.
2305 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
2306 * For local domain sockets:
2307 * *ptr is the filename, *is_sock is 1.
2309 int fio_server_parse_string(const char *str, char **ptr, bool *is_sock,
2310 int *port, struct in_addr *inp,
2311 struct in6_addr *inp6, int *ipv6)
2313 const char *host = str;
2319 *port = fio_net_port;
2322 if (!strncmp(str, "sock:", 5)) {
2323 *ptr = strdup(str + 5);
2330 * Is it ip:<ip or host>:port
2332 if (!strncmp(host, "ip:", 3))
2334 else if (!strncmp(host, "ip4:", 4))
2336 else if (!strncmp(host, "ip6:", 4)) {
2339 } else if (host[0] == ':') {
2340 /* String is :port */
2343 if (!lport || lport > 65535) {
2344 log_err("fio: bad server port %u\n", lport);
2347 /* no hostname given, we are done */
2353 * If no port seen yet, check if there's a last ',' at the end
2356 portp = strchr(host, ',');
2360 lport = atoi(portp);
2361 if (!lport || lport > 65535) {
2362 log_err("fio: bad server port %u\n", lport);
2374 *ptr = strdup(host);
2376 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2383 *port = fio_net_port;
2389 * Server arg should be one of:
2391 * sock:/path/to/socket
2395 * Where sock uses unix domain sockets, and ip binds the server to
2396 * a specific interface. If no arguments are given to the server, it
2397 * uses IP and binds to 0.0.0.0.
2400 static int fio_handle_server_arg(void)
2402 int port = fio_net_port;
2406 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2408 if (!fio_server_arg)
2411 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2412 &port, &saddr_in.sin_addr,
2413 &saddr_in6.sin6_addr, &use_ipv6);
2415 if (!is_sock && bind_sock) {
2421 fio_net_port = port;
2422 saddr_in.sin_port = htons(port);
2423 saddr_in6.sin6_port = htons(port);
2427 static void sig_int(int sig)
2433 static void set_sig_handlers(void)
2435 struct sigaction act = {
2436 .sa_handler = sig_int,
2437 .sa_flags = SA_RESTART,
2440 sigaction(SIGINT, &act, NULL);
2443 void fio_server_destroy_sk_key(void)
2445 pthread_key_delete(sk_out_key);
2448 int fio_server_create_sk_key(void)
2450 if (pthread_key_create(&sk_out_key, NULL)) {
2451 log_err("fio: can't create sk_out backend key\n");
2455 pthread_setspecific(sk_out_key, NULL);
2459 static int fio_server(void)
2463 dprint(FD_NET, "starting server\n");
2465 if (fio_handle_server_arg())
2468 sk = fio_init_server_connection();
2474 ret = accept_loop(sk);
2478 if (fio_server_arg) {
2479 free(fio_server_arg);
2480 fio_server_arg = NULL;
2488 void fio_server_got_signal(int signal)
2490 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2494 if (signal == SIGPIPE)
2497 log_info("\nfio: terminating on signal %d\n", signal);
2498 exit_backend = true;
2502 static int check_existing_pidfile(const char *pidfile)
2509 if (stat(pidfile, &sb))
2512 f = fopen(pidfile, "r");
2516 if (fread(buf, sb.st_size, 1, f) <= 0) {
2523 if (kill(pid, SIGCONT) < 0)
2524 return errno != ESRCH;
2529 static int write_pid(pid_t pid, const char *pidfile)
2533 fpid = fopen(pidfile, "w");
2535 log_err("fio: failed opening pid file %s\n", pidfile);
2539 fprintf(fpid, "%u\n", (unsigned int) pid);
2545 * If pidfile is specified, background us.
2547 int fio_start_server(char *pidfile)
2554 WSAStartup(MAKEWORD(2, 2), &wsd);
2558 return fio_server();
2560 if (check_existing_pidfile(pidfile)) {
2561 log_err("fio: pidfile %s exists and server appears alive\n",
2569 log_err("fio: failed server fork: %s\n", strerror(errno));
2573 ret = write_pid(pid, pidfile);
2579 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2581 close(STDIN_FILENO);
2582 close(STDOUT_FILENO);
2583 close(STDERR_FILENO);
2595 void fio_server_set_arg(const char *arg)
2597 fio_server_arg = strdup(arg);