10 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
26 #include "crc/crc16.h"
27 #include "lib/ieee754.h"
31 int fio_net_port = FIO_NET_PORT;
44 struct flist_head list; /* link on sk_out->list */
45 int flags; /* SK_F_* */
46 int opcode; /* Actual command fields */
50 struct flist_head next; /* Other sk_entry's, if linked command */
53 static char *fio_server_arg;
54 static char *bind_sock;
55 static struct sockaddr_in saddr_in;
56 static struct sockaddr_in6 saddr_in6;
59 static unsigned int has_zlib = 1;
61 static unsigned int has_zlib = 0;
63 static unsigned int use_zlib;
66 static pthread_key_t sk_out_key;
68 struct fio_fork_item {
69 struct flist_head list;
77 struct fio_mutex lock;
83 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
109 static void sk_lock(struct sk_out *sk_out)
111 fio_mutex_down(&sk_out->lock);
114 static void sk_unlock(struct sk_out *sk_out)
116 fio_mutex_up(&sk_out->lock);
119 void sk_out_assign(struct sk_out *sk_out)
127 pthread_setspecific(sk_out_key, sk_out);
130 static void sk_out_free(struct sk_out *sk_out)
132 __fio_mutex_remove(&sk_out->lock);
133 __fio_mutex_remove(&sk_out->wait);
134 __fio_mutex_remove(&sk_out->xmit);
138 static int __sk_out_drop(struct sk_out *sk_out)
144 assert(sk_out->refs != 0);
145 refs = --sk_out->refs;
150 pthread_setspecific(sk_out_key, NULL);
158 void sk_out_drop(void)
160 struct sk_out *sk_out;
162 sk_out = pthread_getspecific(sk_out_key);
163 __sk_out_drop(sk_out);
166 static void __fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
167 uint32_t pdu_len, uint64_t tag)
169 memset(cmd, 0, sizeof(*cmd));
171 cmd->version = __cpu_to_le16(FIO_SERVER_VER);
172 cmd->opcode = cpu_to_le16(opcode);
173 cmd->tag = cpu_to_le64(tag);
174 cmd->pdu_len = cpu_to_le32(pdu_len);
178 static void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
179 const void *pdu, uint32_t pdu_len, uint64_t tag)
181 __fio_init_net_cmd(cmd, opcode, pdu_len, tag);
184 memcpy(&cmd->payload, pdu, pdu_len);
187 const char *fio_server_op(unsigned int op)
191 if (op < FIO_NET_CMD_NR)
192 return fio_server_ops[op];
194 sprintf(buf, "UNKNOWN/%d", op);
198 static ssize_t iov_total_len(const struct iovec *iov, int count)
210 static int fio_sendv_data(int sk, struct iovec *iov, int count)
212 ssize_t total_len = iov_total_len(iov, count);
216 ret = writev(sk, iov, count);
223 if (ret >= iov->iov_len) {
228 iov->iov_base += ret;
234 else if (errno == EAGAIN || errno == EINTR)
238 } while (!exit_backend);
246 static int fio_send_data(int sk, const void *p, unsigned int len)
248 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
250 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
252 return fio_sendv_data(sk, &iov, 1);
255 static int fio_recv_data(int sk, void *buf, unsigned int len, bool wait)
263 flags = OS_MSG_DONTWAIT;
266 int ret = recv(sk, p, len, flags);
276 else if (errno == EAGAIN || errno == EINTR) {
282 } while (!exit_backend);
290 static int verify_convert_cmd(struct fio_net_cmd *cmd)
294 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
295 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
297 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
298 if (crc != cmd->cmd_crc16) {
299 log_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);
318 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
319 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
327 * Read (and defragment, if necessary) incoming commands
329 struct fio_net_cmd *fio_net_recv_cmd(int sk, bool wait)
331 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
332 size_t cmd_size = 0, pdu_offset = 0;
338 ret = fio_recv_data(sk, &cmd, sizeof(cmd), wait);
342 /* We have a command, verify it and swap if need be */
343 ret = verify_convert_cmd(&cmd);
348 /* if this is text, add room for \0 at the end */
349 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
352 cmd_size += cmd.pdu_len;
354 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
355 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
360 tmp = realloc(cmdret, cmd_size);
362 log_err("fio: server failed allocating cmd\n");
369 memcpy(cmdret, &cmd, sizeof(cmd));
370 else if (cmdret->opcode != cmd.opcode) {
371 log_err("fio: fragment opcode mismatch (%d != %d)\n",
372 cmdret->opcode, cmd.opcode);
380 /* There's payload, get it */
381 pdu = (char *) cmdret->payload + pdu_offset;
382 ret = fio_recv_data(sk, pdu, cmd.pdu_len, wait);
386 /* Verify payload crc */
387 crc = fio_crc16(pdu, cmd.pdu_len);
388 if (crc != cmd.pdu_crc16) {
389 log_err("fio: server bad crc on payload ");
390 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
395 pdu_offset += cmd.pdu_len;
397 cmdret->pdu_len += cmd.pdu_len;
399 } while (cmd.flags & FIO_NET_CMD_F_MORE);
405 /* zero-terminate text input */
406 if (cmdret->pdu_len) {
407 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
408 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
409 char *buf = (char *) __pdu->buf;
411 buf[__pdu->buf_len] = '\0';
412 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
413 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
414 char *buf = (char *) __pdu->buf;
415 int len = le32_to_cpu(__pdu->buf_len);
421 /* frag flag is internal */
422 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
428 static void add_reply(uint64_t tag, struct flist_head *list)
430 struct fio_net_cmd_reply *reply;
432 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
433 flist_add_tail(&reply->list, list);
436 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
438 struct fio_net_cmd_reply *reply;
440 reply = calloc(1, sizeof(*reply));
441 INIT_FLIST_HEAD(&reply->list);
442 fio_gettime(&reply->ts, NULL);
443 reply->saved_tag = tag;
444 reply->opcode = opcode;
446 return (uintptr_t) reply;
449 static void free_reply(uint64_t tag)
451 struct fio_net_cmd_reply *reply;
453 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
457 static void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
461 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
463 pdu_len = le32_to_cpu(cmd->pdu_len);
464 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
467 static void fio_net_cmd_crc(struct fio_net_cmd *cmd)
469 fio_net_cmd_crc_pdu(cmd, cmd->payload);
472 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
473 uint64_t *tagptr, struct flist_head *list)
475 struct fio_net_cmd *cmd = NULL;
476 size_t this_len, cur_len = 0;
482 tag = *tagptr = alloc_reply(*tagptr, opcode);
484 tag = tagptr ? *tagptr : 0;
488 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
489 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
491 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
495 cur_len = sizeof(*cmd) + this_len;
496 cmd = malloc(cur_len);
499 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
502 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
504 fio_net_cmd_crc(cmd);
506 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
509 } while (!ret && size);
515 add_reply(tag, list);
524 static struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf,
525 size_t size, uint64_t *tagptr,
528 struct sk_entry *entry;
530 entry = smalloc(sizeof(*entry));
531 INIT_FLIST_HEAD(&entry->next);
532 entry->opcode = opcode;
533 if (flags & SK_F_COPY) {
534 entry->buf = smalloc(size);
535 memcpy(entry->buf, buf, size);
541 entry->tag = *tagptr;
544 entry->flags = flags;
548 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry);
550 static void fio_net_queue_entry(struct sk_entry *entry)
552 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
554 if (entry->flags & SK_F_INLINE)
555 handle_sk_entry(sk_out, entry);
558 flist_add_tail(&entry->list, &sk_out->list);
561 fio_mutex_up(&sk_out->wait);
565 static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
566 uint64_t *tagptr, int flags)
568 struct sk_entry *entry;
570 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
572 fio_net_queue_entry(entry);
579 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
581 struct fio_net_cmd cmd;
583 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
584 fio_net_cmd_crc(&cmd);
586 return fio_send_data(sk, &cmd, sizeof(cmd));
590 * If 'list' is non-NULL, then allocate and store the sent command for
591 * later verification.
593 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
594 struct flist_head *list)
599 tag = alloc_reply(tag, opcode);
601 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
610 add_reply(tag, list);
615 static int fio_net_queue_quit(void)
617 dprint(FD_NET, "server: sending quit\n");
619 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, NULL, SK_F_SIMPLE);
622 int fio_net_send_quit(int sk)
624 dprint(FD_NET, "server: sending quit\n");
626 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
629 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
631 struct cmd_end_pdu epdu;
637 epdu.error = __cpu_to_le32(error);
638 epdu.signal = __cpu_to_le32(signal);
639 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
642 static int fio_net_queue_stop(int error, int signal)
644 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
645 return fio_net_send_ack(NULL, error, signal);
648 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
650 struct fio_fork_item *ffi;
652 ffi = malloc(sizeof(*ffi));
657 flist_add_tail(&ffi->list, list);
660 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
662 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
663 fio_server_add_fork_item(pid, conn_list);
666 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
668 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
669 fio_server_add_fork_item(pid, job_list);
672 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
676 ret = waitpid(ffi->pid, &status, WNOHANG);
678 if (errno == ECHILD) {
679 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
682 log_err("fio: waitpid: %s\n", strerror(errno));
683 } else if (ret == ffi->pid) {
684 if (WIFSIGNALED(status)) {
685 ffi->signal = WTERMSIG(status);
688 if (WIFEXITED(status)) {
689 if (WEXITSTATUS(status))
690 ffi->exitval = WEXITSTATUS(status);
696 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
698 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
701 * Fold STOP and QUIT...
704 fio_net_queue_stop(ffi->exitval, ffi->signal);
705 fio_net_queue_quit();
708 flist_del(&ffi->list);
712 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
714 struct flist_head *entry, *tmp;
715 struct fio_fork_item *ffi;
717 flist_for_each_safe(entry, tmp, list) {
718 ffi = flist_entry(entry, struct fio_fork_item, list);
720 fio_server_check_fork_item(ffi);
723 fio_server_fork_item_done(ffi, stop);
727 static void fio_server_check_jobs(struct flist_head *job_list)
729 fio_server_check_fork_items(job_list, true);
732 static void fio_server_check_conns(struct flist_head *conn_list)
734 fio_server_check_fork_items(conn_list, false);
737 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
739 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
740 void *file_name = pdu->file;
741 struct cmd_start_pdu spdu;
743 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
745 pdu->name_len = le16_to_cpu(pdu->name_len);
746 pdu->client_type = le16_to_cpu(pdu->client_type);
748 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
749 fio_net_queue_quit();
753 spdu.jobs = cpu_to_le32(thread_number);
754 spdu.stat_outputs = cpu_to_le32(stat_number);
755 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
759 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
760 struct fio_net_cmd *cmd)
765 sk_out_assign(sk_out);
772 fio_server_add_job_pid(job_list, pid);
776 ret = fio_backend(sk_out);
782 static int handle_job_cmd(struct fio_net_cmd *cmd)
784 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
785 void *buf = pdu->buf;
786 struct cmd_start_pdu spdu;
788 pdu->buf_len = le32_to_cpu(pdu->buf_len);
789 pdu->client_type = le32_to_cpu(pdu->client_type);
791 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
792 fio_net_queue_quit();
796 spdu.jobs = cpu_to_le32(thread_number);
797 spdu.stat_outputs = cpu_to_le32(stat_number);
799 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
803 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
805 void *pdu = cmd->payload;
806 struct cmd_single_line_pdu *cslp;
807 struct cmd_line_pdu *clp;
808 unsigned long offset;
809 struct cmd_start_pdu spdu;
814 clp->lines = le16_to_cpu(clp->lines);
815 clp->client_type = le16_to_cpu(clp->client_type);
816 argv = malloc(clp->lines * sizeof(char *));
817 offset = sizeof(*clp);
819 dprint(FD_NET, "server: %d command line args\n", clp->lines);
821 for (i = 0; i < clp->lines; i++) {
823 argv[i] = (char *) cslp->text;
825 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
826 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
829 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
830 fio_net_queue_quit();
837 spdu.jobs = cpu_to_le32(thread_number);
838 spdu.stat_outputs = cpu_to_le32(stat_number);
840 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
844 static int handle_probe_cmd(struct fio_net_cmd *cmd)
846 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
847 struct cmd_probe_reply_pdu probe;
848 uint64_t tag = cmd->tag;
850 dprint(FD_NET, "server: sending probe reply\n");
852 strcpy(me, (char *) pdu->server);
854 memset(&probe, 0, sizeof(probe));
855 gethostname((char *) probe.hostname, sizeof(probe.hostname));
856 #ifdef CONFIG_BIG_ENDIAN
859 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version) - 1);
862 probe.arch = FIO_ARCH;
863 probe.bpp = sizeof(void *);
864 probe.cpus = __cpu_to_le32(cpus_online());
867 * If the client supports compression and we do too, then enable it
869 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
870 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
877 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
880 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
883 uint64_t tag = cmd->tag;
887 dprint(FD_NET, "server sending status\n");
890 * Fake ETA return if we don't have a local one, otherwise the client
891 * will end up timing out waiting for a response to the ETA request
893 je = get_jobs_eta(true, &size);
896 je = calloc(1, size);
898 je->nr_running = cpu_to_le32(je->nr_running);
899 je->nr_ramp = cpu_to_le32(je->nr_ramp);
900 je->nr_pending = cpu_to_le32(je->nr_pending);
901 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
902 je->files_open = cpu_to_le32(je->files_open);
904 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
905 je->m_rate[i] = cpu_to_le64(je->m_rate[i]);
906 je->t_rate[i] = cpu_to_le64(je->t_rate[i]);
907 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
908 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
909 je->rate[i] = cpu_to_le64(je->rate[i]);
910 je->iops[i] = cpu_to_le32(je->iops[i]);
913 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
914 je->eta_sec = cpu_to_le64(je->eta_sec);
915 je->nr_threads = cpu_to_le32(je->nr_threads);
916 je->is_pow2 = cpu_to_le32(je->is_pow2);
917 je->unit_base = cpu_to_le32(je->unit_base);
920 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
924 static int send_update_job_reply(uint64_t __tag, int error)
926 uint64_t tag = __tag;
929 pdu_error = __cpu_to_le32(error);
930 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
933 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
935 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
936 struct thread_data *td;
939 tnumber = le32_to_cpu(pdu->thread_number);
941 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
943 if (!tnumber || tnumber > thread_number) {
944 send_update_job_reply(cmd->tag, ENODEV);
948 td = &threads[tnumber - 1];
949 convert_thread_options_to_cpu(&td->o, &pdu->top);
950 send_update_job_reply(cmd->tag, 0);
954 static int handle_trigger_cmd(struct fio_net_cmd *cmd)
956 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
957 char *buf = (char *) pdu->cmd;
958 struct all_io_list *rep;
961 pdu->len = le16_to_cpu(pdu->len);
962 buf[pdu->len] = '\0';
964 rep = get_all_io_list(IO_LIST_ALL, &sz);
966 struct all_io_list state;
968 state.threads = cpu_to_le64((uint64_t) 0);
969 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY | SK_F_INLINE);
971 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE | SK_F_INLINE);
973 fio_terminate_threads(TERMINATE_ALL);
978 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
979 struct fio_net_cmd *cmd)
983 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
984 fio_server_op(cmd->opcode), cmd->pdu_len,
985 (unsigned long long) cmd->tag);
987 switch (cmd->opcode) {
988 case FIO_NET_CMD_QUIT:
989 fio_terminate_threads(TERMINATE_ALL);
992 case FIO_NET_CMD_EXIT:
995 case FIO_NET_CMD_LOAD_FILE:
996 ret = handle_load_file_cmd(cmd);
998 case FIO_NET_CMD_JOB:
999 ret = handle_job_cmd(cmd);
1001 case FIO_NET_CMD_JOBLINE:
1002 ret = handle_jobline_cmd(cmd);
1004 case FIO_NET_CMD_PROBE:
1005 ret = handle_probe_cmd(cmd);
1007 case FIO_NET_CMD_SEND_ETA:
1008 ret = handle_send_eta_cmd(cmd);
1010 case FIO_NET_CMD_RUN:
1011 ret = handle_run_cmd(sk_out, job_list, cmd);
1013 case FIO_NET_CMD_UPDATE_JOB:
1014 ret = handle_update_job_cmd(cmd);
1016 case FIO_NET_CMD_VTRIGGER:
1017 ret = handle_trigger_cmd(cmd);
1019 case FIO_NET_CMD_SENDFILE: {
1020 struct cmd_sendfile_reply *in;
1021 struct cmd_reply *rep;
1023 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
1025 in = (struct cmd_sendfile_reply *) cmd->payload;
1026 in->size = le32_to_cpu(in->size);
1027 in->error = le32_to_cpu(in->error);
1030 rep->error = in->error;
1033 rep->data = smalloc(in->size);
1036 rep->error = ENOMEM;
1038 rep->size = in->size;
1039 memcpy(rep->data, in->data, in->size);
1042 fio_mutex_up(&rep->lock);
1046 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
1054 * Send a command with a separate PDU, not inlined in the command
1056 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1057 off_t size, uint64_t tag, uint32_t flags)
1059 struct fio_net_cmd cmd;
1060 struct iovec iov[2];
1064 iov[0].iov_base = (void *) &cmd;
1065 iov[0].iov_len = sizeof(cmd);
1068 uint32_t this_flags = flags;
1071 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
1072 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
1074 if (this_len < size)
1075 this_flags |= FIO_NET_CMD_F_MORE;
1077 __fio_init_net_cmd(&cmd, opcode, this_len, tag);
1078 cmd.flags = __cpu_to_le32(this_flags);
1079 fio_net_cmd_crc_pdu(&cmd, buf);
1081 iov[1].iov_base = (void *) buf;
1082 iov[1].iov_len = this_len;
1084 ret = fio_sendv_data(sk, iov, 2);
1087 } while (!ret && size);
1092 static void finish_entry(struct sk_entry *entry)
1094 if (entry->flags & SK_F_FREE)
1096 else if (entry->flags & SK_F_COPY)
1102 static void entry_set_flags(struct sk_entry *entry, struct flist_head *list,
1103 unsigned int *flags)
1105 if (!flist_empty(list))
1106 *flags = FIO_NET_CMD_F_MORE;
1111 static int send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1116 entry_set_flags(first, &first->next, &flags);
1118 ret = fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf,
1119 first->size, first->tag, flags);
1121 while (!flist_empty(&first->next)) {
1122 struct sk_entry *next;
1124 next = flist_first_entry(&first->next, struct sk_entry, list);
1125 flist_del_init(&next->list);
1127 entry_set_flags(next, &first->next, &flags);
1129 ret += fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf,
1130 next->size, next->tag, flags);
1137 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1141 fio_mutex_down(&sk_out->xmit);
1143 if (entry->flags & SK_F_VEC)
1144 ret = send_vec_entry(sk_out, entry);
1145 else if (entry->flags & SK_F_SIMPLE) {
1146 ret = fio_net_send_simple_cmd(sk_out->sk, entry->opcode,
1149 ret = fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf,
1150 entry->size, &entry->tag, NULL);
1153 fio_mutex_up(&sk_out->xmit);
1156 log_err("fio: failed handling cmd %s\n", fio_server_op(entry->opcode));
1158 finish_entry(entry);
1162 static int handle_xmits(struct sk_out *sk_out)
1164 struct sk_entry *entry;
1169 if (flist_empty(&sk_out->list)) {
1174 flist_splice_init(&sk_out->list, &list);
1177 while (!flist_empty(&list)) {
1178 entry = flist_entry(list.next, struct sk_entry, list);
1179 flist_del(&entry->list);
1180 ret += handle_sk_entry(sk_out, entry);
1186 static int handle_connection(struct sk_out *sk_out)
1188 struct fio_net_cmd *cmd = NULL;
1189 FLIST_HEAD(job_list);
1195 while (!exit_backend) {
1196 struct pollfd pfd = {
1205 if (!flist_empty(&job_list))
1208 handle_xmits(sk_out);
1210 ret = poll(&pfd, 1, 0);
1214 log_err("fio: poll: %s\n", strerror(errno));
1217 fio_server_check_jobs(&job_list);
1218 fio_mutex_down_timeout(&sk_out->wait, timeout);
1222 if (pfd.revents & POLLIN)
1224 if (pfd.revents & (POLLERR|POLLHUP)) {
1228 } while (!exit_backend);
1230 fio_server_check_jobs(&job_list);
1235 cmd = fio_net_recv_cmd(sk_out->sk, true);
1241 ret = handle_command(sk_out, &job_list, cmd);
1252 handle_xmits(sk_out);
1256 __sk_out_drop(sk_out);
1260 /* get the address on this host bound by the input socket,
1261 * whether it is ipv6 or ipv4 */
1263 static int get_my_addr_str(int sk)
1265 struct sockaddr_in6 myaddr6 = { 0, };
1266 struct sockaddr_in myaddr4 = { 0, };
1267 struct sockaddr *sockaddr_p;
1273 len = sizeof(myaddr6);
1274 sockaddr_p = (struct sockaddr * )&myaddr6;
1275 net_addr = (char * )&myaddr6.sin6_addr;
1277 len = sizeof(myaddr4);
1278 sockaddr_p = (struct sockaddr * )&myaddr4;
1279 net_addr = (char * )&myaddr4.sin_addr;
1282 ret = getsockname(sk, sockaddr_p, &len);
1284 log_err("fio: getsockname: %s\n", strerror(errno));
1288 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1289 log_err("inet_ntop: failed to convert addr to string\n");
1293 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1297 static int accept_loop(int listen_sk)
1299 struct sockaddr_in addr;
1300 struct sockaddr_in6 addr6;
1301 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1303 int ret = 0, sk, exitval = 0;
1304 FLIST_HEAD(conn_list);
1306 dprint(FD_NET, "server enter accept loop\n");
1308 fio_set_fd_nonblocking(listen_sk, "server");
1310 while (!exit_backend) {
1311 struct sk_out *sk_out;
1317 pfd.events = POLLIN;
1321 if (!flist_empty(&conn_list))
1324 ret = poll(&pfd, 1, timeout);
1328 log_err("fio: poll: %s\n", strerror(errno));
1331 fio_server_check_conns(&conn_list);
1335 if (pfd.revents & POLLIN)
1337 } while (!exit_backend);
1339 fio_server_check_conns(&conn_list);
1341 if (exit_backend || ret < 0)
1345 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1347 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1350 log_err("fio: accept: %s\n", strerror(errno));
1355 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1357 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1359 dprint(FD_NET, "server: connect from %s\n", from);
1361 sk_out = smalloc(sizeof(*sk_out));
1363 INIT_FLIST_HEAD(&sk_out->list);
1364 __fio_mutex_init(&sk_out->lock, FIO_MUTEX_UNLOCKED);
1365 __fio_mutex_init(&sk_out->wait, FIO_MUTEX_LOCKED);
1366 __fio_mutex_init(&sk_out->xmit, FIO_MUTEX_UNLOCKED);
1371 fio_server_add_conn_pid(&conn_list, pid);
1375 /* if error, it's already logged, non-fatal */
1376 get_my_addr_str(sk);
1379 * Assign sk_out here, it'll be dropped in handle_connection()
1380 * since that function calls _exit() when done
1382 sk_out_assign(sk_out);
1383 handle_connection(sk_out);
1389 int fio_server_text_output(int level, const char *buf, size_t len)
1391 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1392 struct cmd_text_pdu *pdu;
1396 if (!sk_out || sk_out->sk == -1)
1399 tlen = sizeof(*pdu) + len;
1402 pdu->level = __cpu_to_le32(level);
1403 pdu->buf_len = __cpu_to_le32(len);
1405 gettimeofday(&tv, NULL);
1406 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1407 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1409 memcpy(pdu->buf, buf, len);
1411 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1416 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1418 dst->max_val = cpu_to_le64(src->max_val);
1419 dst->min_val = cpu_to_le64(src->min_val);
1420 dst->samples = cpu_to_le64(src->samples);
1423 * Encode to IEEE 754 for network transfer
1425 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1426 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1429 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1433 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1434 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1435 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1436 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1437 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1438 dst->iobytes[i] = cpu_to_le64(src->iobytes[i]);
1439 dst->agg[i] = cpu_to_le64(src->agg[i]);
1442 dst->kb_base = cpu_to_le32(src->kb_base);
1443 dst->unit_base = cpu_to_le32(src->unit_base);
1444 dst->groupid = cpu_to_le32(src->groupid);
1445 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1449 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1450 * into a single payload.
1452 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1454 struct cmd_ts_pdu p;
1457 uint64_t *ss_iops, *ss_bw;
1459 dprint(FD_NET, "server sending end stats\n");
1461 memset(&p, 0, sizeof(p));
1463 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1464 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1465 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1467 p.ts.error = cpu_to_le32(ts->error);
1468 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1469 p.ts.groupid = cpu_to_le32(ts->groupid);
1470 p.ts.pid = cpu_to_le32(ts->pid);
1471 p.ts.members = cpu_to_le32(ts->members);
1472 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1474 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1475 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1476 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1477 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1478 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1479 convert_io_stat(&p.ts.iops_stat[i], &ts->iops_stat[i]);
1482 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1483 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1484 p.ts.ctx = cpu_to_le64(ts->ctx);
1485 p.ts.minf = cpu_to_le64(ts->minf);
1486 p.ts.majf = cpu_to_le64(ts->majf);
1487 p.ts.clat_percentiles = cpu_to_le32(ts->clat_percentiles);
1488 p.ts.lat_percentiles = cpu_to_le32(ts->lat_percentiles);
1489 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1491 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1492 fio_fp64_t *src = &ts->percentile_list[i];
1493 fio_fp64_t *dst = &p.ts.percentile_list[i];
1495 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1498 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1499 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1500 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1501 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1504 for (i = 0; i < FIO_IO_U_LAT_N_NR; i++)
1505 p.ts.io_u_lat_n[i] = cpu_to_le32(ts->io_u_lat_n[i]);
1506 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++)
1507 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1508 for (i = 0; i < FIO_IO_U_LAT_M_NR; i++)
1509 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1511 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1512 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1513 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1515 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1516 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1517 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1518 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1521 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1522 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1524 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1525 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1526 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1529 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1530 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1531 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1532 p.ts.first_error = cpu_to_le32(ts->first_error);
1533 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1534 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1536 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1537 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1538 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1539 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1541 p.ts.nr_block_infos = cpu_to_le64(ts->nr_block_infos);
1542 for (i = 0; i < p.ts.nr_block_infos; i++)
1543 p.ts.block_infos[i] = cpu_to_le32(ts->block_infos[i]);
1545 p.ts.ss_dur = cpu_to_le64(ts->ss_dur);
1546 p.ts.ss_state = cpu_to_le32(ts->ss_state);
1547 p.ts.ss_head = cpu_to_le32(ts->ss_head);
1548 p.ts.ss_limit.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_limit.u.f));
1549 p.ts.ss_slope.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_slope.u.f));
1550 p.ts.ss_deviation.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_deviation.u.f));
1551 p.ts.ss_criterion.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_criterion.u.f));
1553 convert_gs(&p.rs, rs);
1555 dprint(FD_NET, "ts->ss_state = %d\n", ts->ss_state);
1556 if (ts->ss_state & __FIO_SS_DATA) {
1557 dprint(FD_NET, "server sending steadystate ring buffers\n");
1559 ss_buf = malloc(sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t));
1561 memcpy(ss_buf, &p, sizeof(p));
1563 ss_iops = (uint64_t *) ((struct cmd_ts_pdu *)ss_buf + 1);
1564 ss_bw = ss_iops + (int) ts->ss_dur;
1565 for (i = 0; i < ts->ss_dur; i++) {
1566 ss_iops[i] = cpu_to_le64(ts->ss_iops_data[i]);
1567 ss_bw[i] = cpu_to_le64(ts->ss_bw_data[i]);
1570 fio_net_queue_cmd(FIO_NET_CMD_TS, ss_buf, sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t), NULL, SK_F_COPY);
1575 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1578 void fio_server_send_gs(struct group_run_stats *rs)
1580 struct group_run_stats gs;
1582 dprint(FD_NET, "server sending group run stats\n");
1584 convert_gs(&gs, rs);
1585 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1588 void fio_server_send_job_options(struct flist_head *opt_list,
1589 unsigned int groupid)
1591 struct cmd_job_option pdu;
1592 struct flist_head *entry;
1594 if (flist_empty(opt_list))
1597 flist_for_each(entry, opt_list) {
1598 struct print_option *p;
1601 p = flist_entry(entry, struct print_option, list);
1602 memset(&pdu, 0, sizeof(pdu));
1604 if (groupid == -1U) {
1605 pdu.global = __cpu_to_le16(1);
1609 pdu.groupid = cpu_to_le32(groupid);
1611 len = strlen(p->name);
1612 if (len >= sizeof(pdu.name)) {
1613 len = sizeof(pdu.name) - 1;
1614 pdu.truncated = __cpu_to_le16(1);
1616 memcpy(pdu.name, p->name, len);
1618 len = strlen(p->value);
1619 if (len >= sizeof(pdu.value)) {
1620 len = sizeof(pdu.value) - 1;
1621 pdu.truncated = __cpu_to_le16(1);
1623 memcpy(pdu.value, p->value, len);
1625 fio_net_queue_cmd(FIO_NET_CMD_JOB_OPT, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1629 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1633 for (i = 0; i < 2; i++) {
1634 dst->ios[i] = cpu_to_le64(src->ios[i]);
1635 dst->merges[i] = cpu_to_le64(src->merges[i]);
1636 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1637 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1640 dst->io_ticks = cpu_to_le64(src->io_ticks);
1641 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1642 dst->slavecount = cpu_to_le32(src->slavecount);
1643 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1646 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1650 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1651 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1653 for (i = 0; i < 2; i++) {
1654 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1655 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1656 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1657 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1660 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1661 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1662 dst->s.msec = cpu_to_le64(src->s.msec);
1665 void fio_server_send_du(void)
1667 struct disk_util *du;
1668 struct flist_head *entry;
1669 struct cmd_du_pdu pdu;
1671 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1673 memset(&pdu, 0, sizeof(pdu));
1675 flist_for_each(entry, &disk_list) {
1676 du = flist_entry(entry, struct disk_util, list);
1678 convert_dus(&pdu.dus, &du->dus);
1679 convert_agg(&pdu.agg, &du->agg);
1681 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1687 static inline void __fio_net_prep_tail(z_stream *stream, void *out_pdu,
1688 struct sk_entry **last_entry,
1689 struct sk_entry *first)
1691 unsigned int this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1693 *last_entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1694 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1695 flist_add_tail(&(*last_entry)->list, &first->next);
1700 * Deflates the next input given, creating as many new packets in the
1701 * linked list as necessary.
1703 static int __deflate_pdu_buffer(void *next_in, unsigned int next_sz, void **out_pdu,
1704 struct sk_entry **last_entry, z_stream *stream,
1705 struct sk_entry *first)
1709 stream->next_in = next_in;
1710 stream->avail_in = next_sz;
1712 if (! stream->avail_out) {
1714 __fio_net_prep_tail(stream, *out_pdu, last_entry, first);
1716 *out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1718 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1719 stream->next_out = *out_pdu;
1722 ret = deflate(stream, Z_BLOCK);
1728 } while (stream->avail_in);
1733 static int __fio_append_iolog_gz_hist(struct sk_entry *first, struct io_log *log,
1734 struct io_logs *cur_log, z_stream *stream)
1736 struct sk_entry *entry;
1739 int sample_sz = log_entry_sz(log);
1741 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1742 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1743 stream->next_out = out_pdu;
1745 for (i = 0; i < cur_log->nr_samples; i++) {
1746 struct io_sample *s;
1747 struct io_u_plat_entry *cur_plat_entry, *prev_plat_entry;
1748 unsigned int *cur_plat, *prev_plat;
1750 s = get_sample(log, cur_log, i);
1751 ret = __deflate_pdu_buffer(s, sample_sz, &out_pdu, &entry, stream, first);
1755 /* Do the subtraction on server side so that client doesn't have to
1756 * reconstruct our linked list from packets.
1758 cur_plat_entry = s->data.plat_entry;
1759 prev_plat_entry = flist_first_entry(&cur_plat_entry->list, struct io_u_plat_entry, list);
1760 cur_plat = cur_plat_entry->io_u_plat;
1761 prev_plat = prev_plat_entry->io_u_plat;
1763 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
1764 cur_plat[j] -= prev_plat[j];
1767 flist_del(&prev_plat_entry->list);
1768 free(prev_plat_entry);
1770 ret = __deflate_pdu_buffer(cur_plat_entry, sizeof(*cur_plat_entry),
1771 &out_pdu, &entry, stream, first);
1777 __fio_net_prep_tail(stream, out_pdu, &entry, first);
1782 static int __fio_append_iolog_gz(struct sk_entry *first, struct io_log *log,
1783 struct io_logs *cur_log, z_stream *stream)
1785 unsigned int this_len;
1789 if (log->log_type == IO_LOG_TYPE_HIST)
1790 return __fio_append_iolog_gz_hist(first, log, cur_log, stream);
1792 stream->next_in = (void *) cur_log->log;
1793 stream->avail_in = cur_log->nr_samples * log_entry_sz(log);
1796 struct sk_entry *entry;
1799 * Dirty - since the log is potentially huge, compress it into
1800 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1801 * side defragment it.
1803 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1805 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1806 stream->next_out = out_pdu;
1807 ret = deflate(stream, Z_BLOCK);
1808 /* may be Z_OK, or Z_STREAM_END */
1814 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1816 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1817 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1818 flist_add_tail(&entry->list, &first->next);
1819 } while (stream->avail_in);
1824 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1829 memset(&stream, 0, sizeof(stream));
1830 stream.zalloc = Z_NULL;
1831 stream.zfree = Z_NULL;
1832 stream.opaque = Z_NULL;
1834 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK)
1837 while (!flist_empty(&log->io_logs)) {
1838 struct io_logs *cur_log;
1840 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1841 flist_del_init(&cur_log->list);
1843 ret = __fio_append_iolog_gz(first, log, cur_log, &stream);
1848 ret = deflate(&stream, Z_FINISH);
1850 while (ret != Z_STREAM_END) {
1851 struct sk_entry *entry;
1852 unsigned int this_len;
1855 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1856 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1857 stream.next_out = out_pdu;
1859 ret = deflate(&stream, Z_FINISH);
1860 /* may be Z_OK, or Z_STREAM_END */
1866 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1868 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1869 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1870 flist_add_tail(&entry->list, &first->next);
1871 } while (ret != Z_STREAM_END);
1873 ret = deflateEnd(&stream);
1880 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1886 static int fio_append_gz_chunks(struct sk_entry *first, struct io_log *log)
1888 struct sk_entry *entry;
1889 struct flist_head *node;
1891 pthread_mutex_lock(&log->chunk_lock);
1892 flist_for_each(node, &log->chunk_list) {
1893 struct iolog_compress *c;
1895 c = flist_entry(node, struct iolog_compress, list);
1896 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, c->buf, c->len,
1897 NULL, SK_F_VEC | SK_F_INLINE);
1898 flist_add_tail(&entry->list, &first->next);
1900 pthread_mutex_unlock(&log->chunk_lock);
1905 static int fio_append_text_log(struct sk_entry *first, struct io_log *log)
1907 struct sk_entry *entry;
1909 while (!flist_empty(&log->io_logs)) {
1910 struct io_logs *cur_log;
1913 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1914 flist_del_init(&cur_log->list);
1916 size = cur_log->nr_samples * log_entry_sz(log);
1918 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, cur_log->log, size,
1919 NULL, SK_F_VEC | SK_F_INLINE);
1920 flist_add_tail(&entry->list, &first->next);
1926 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1928 struct cmd_iolog_pdu pdu;
1929 struct sk_entry *first;
1930 struct flist_head *entry;
1933 pdu.nr_samples = cpu_to_le64(iolog_nr_samples(log));
1934 pdu.thread_number = cpu_to_le32(td->thread_number);
1935 pdu.log_type = cpu_to_le32(log->log_type);
1936 pdu.log_hist_coarseness = cpu_to_le32(log->hist_coarseness);
1938 if (!flist_empty(&log->chunk_list))
1939 pdu.compressed = __cpu_to_le32(STORE_COMPRESSED);
1941 pdu.compressed = __cpu_to_le32(XMIT_COMPRESSED);
1945 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1946 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1949 * We can't do this for a pre-compressed log, but for that case,
1950 * log->nr_samples is zero anyway.
1952 flist_for_each(entry, &log->io_logs) {
1953 struct io_logs *cur_log;
1956 cur_log = flist_entry(entry, struct io_logs, list);
1958 for (i = 0; i < cur_log->nr_samples; i++) {
1959 struct io_sample *s = get_sample(log, cur_log, i);
1961 s->time = cpu_to_le64(s->time);
1962 s->data.val = cpu_to_le64(s->data.val);
1963 s->__ddir = cpu_to_le32(s->__ddir);
1964 s->bs = cpu_to_le32(s->bs);
1966 if (log->log_offset) {
1967 struct io_sample_offset *so = (void *) s;
1969 so->offset = cpu_to_le64(so->offset);
1975 * Assemble header entry first
1977 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_VEC | SK_F_INLINE | SK_F_COPY);
1980 * Now append actual log entries. If log compression was enabled on
1981 * the job, just send out the compressed chunks directly. If we
1982 * have a plain log, compress if we can, then send. Otherwise, send
1983 * the plain text output.
1985 if (!flist_empty(&log->chunk_list))
1986 ret = fio_append_gz_chunks(first, log);
1988 ret = fio_append_iolog_gz(first, log);
1990 ret = fio_append_text_log(first, log);
1992 fio_net_queue_entry(first);
1996 void fio_server_send_add_job(struct thread_data *td)
1998 struct cmd_add_job_pdu pdu;
2000 memset(&pdu, 0, sizeof(pdu));
2001 pdu.thread_number = cpu_to_le32(td->thread_number);
2002 pdu.groupid = cpu_to_le32(td->groupid);
2003 convert_thread_options_to_net(&pdu.top, &td->o);
2005 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL,
2009 void fio_server_send_start(struct thread_data *td)
2011 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2013 assert(sk_out->sk != -1);
2015 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, NULL, SK_F_SIMPLE);
2018 int fio_server_get_verify_state(const char *name, int threadnumber,
2021 struct thread_io_list *s;
2022 struct cmd_sendfile out;
2023 struct cmd_reply *rep;
2028 dprint(FD_NET, "server: request verify state\n");
2030 rep = smalloc(sizeof(*rep));
2034 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
2038 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
2040 tag = (uint64_t) (uintptr_t) rep;
2041 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag,
2045 * Wait for the backend to receive the reply
2047 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
2048 log_err("fio: timed out waiting for reply\n");
2054 log_err("fio: failure on receiving state file %s: %s\n",
2055 out.path, strerror(rep->error));
2060 fio_net_queue_quit();
2065 * The format is verify_state_hdr, then thread_io_list. Verify
2066 * the header, and the thread_io_list checksum
2068 s = rep->data + sizeof(struct verify_state_hdr);
2069 if (verify_state_hdr(rep->data, s)) {
2075 * Don't need the header from now, copy just the thread_io_list
2078 rep->size -= sizeof(struct verify_state_hdr);
2079 data = malloc(rep->size);
2080 memcpy(data, s, rep->size);
2084 __fio_mutex_remove(&rep->lock);
2089 static int fio_init_server_ip(void)
2091 struct sockaddr *addr;
2098 sk = socket(AF_INET6, SOCK_STREAM, 0);
2100 sk = socket(AF_INET, SOCK_STREAM, 0);
2103 log_err("fio: socket: %s\n", strerror(errno));
2108 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
2109 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
2115 * Not fatal if fails, so just ignore it if that happens
2117 setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
2121 const void *src = &saddr_in6.sin6_addr;
2123 addr = (struct sockaddr *) &saddr_in6;
2124 socklen = sizeof(saddr_in6);
2125 saddr_in6.sin6_family = AF_INET6;
2126 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
2128 const void *src = &saddr_in.sin_addr;
2130 addr = (struct sockaddr *) &saddr_in;
2131 socklen = sizeof(saddr_in);
2132 saddr_in.sin_family = AF_INET;
2133 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
2136 if (bind(sk, addr, socklen) < 0) {
2137 log_err("fio: bind: %s\n", strerror(errno));
2138 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
2146 static int fio_init_server_sock(void)
2148 struct sockaddr_un addr;
2153 sk = socket(AF_UNIX, SOCK_STREAM, 0);
2155 log_err("fio: socket: %s\n", strerror(errno));
2161 memset(&addr, 0, sizeof(addr));
2162 addr.sun_family = AF_UNIX;
2163 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
2165 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
2167 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
2168 log_err("fio: bind: %s\n", strerror(errno));
2177 static int fio_init_server_connection(void)
2182 dprint(FD_NET, "starting server\n");
2185 sk = fio_init_server_ip();
2187 sk = fio_init_server_sock();
2192 memset(bind_str, 0, sizeof(bind_str));
2201 src = &saddr_in6.sin6_addr;
2204 src = &saddr_in.sin_addr;
2207 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
2209 sprintf(port, ",%u", fio_net_port);
2213 strncpy(bind_str, port, sizeof(bind_str) - 1);
2215 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
2217 log_info("fio: server listening on %s\n", bind_str);
2219 if (listen(sk, 4) < 0) {
2220 log_err("fio: listen: %s\n", strerror(errno));
2228 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
2229 struct in6_addr *inp6)
2235 ret = inet_pton(AF_INET6, host, inp6);
2237 ret = inet_pton(AF_INET, host, inp);
2240 struct addrinfo hints, *res;
2242 memset(&hints, 0, sizeof(hints));
2243 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
2244 hints.ai_socktype = SOCK_STREAM;
2246 ret = getaddrinfo(host, NULL, &hints, &res);
2248 log_err("fio: failed to resolve <%s> (%s)\n", host,
2254 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
2256 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
2266 * Parse a host/ip/port string. Reads from 'str'.
2271 * *ptr is the host, *port is the port, inp is the destination.
2273 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
2274 * For local domain sockets:
2275 * *ptr is the filename, *is_sock is 1.
2277 int fio_server_parse_string(const char *str, char **ptr, bool *is_sock,
2278 int *port, struct in_addr *inp,
2279 struct in6_addr *inp6, int *ipv6)
2281 const char *host = str;
2287 *port = fio_net_port;
2290 if (!strncmp(str, "sock:", 5)) {
2291 *ptr = strdup(str + 5);
2298 * Is it ip:<ip or host>:port
2300 if (!strncmp(host, "ip:", 3))
2302 else if (!strncmp(host, "ip4:", 4))
2304 else if (!strncmp(host, "ip6:", 4)) {
2307 } else if (host[0] == ':') {
2308 /* String is :port */
2311 if (!lport || lport > 65535) {
2312 log_err("fio: bad server port %u\n", lport);
2315 /* no hostname given, we are done */
2321 * If no port seen yet, check if there's a last ',' at the end
2324 portp = strchr(host, ',');
2328 lport = atoi(portp);
2329 if (!lport || lport > 65535) {
2330 log_err("fio: bad server port %u\n", lport);
2342 *ptr = strdup(host);
2344 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2351 *port = fio_net_port;
2357 * Server arg should be one of:
2359 * sock:/path/to/socket
2363 * Where sock uses unix domain sockets, and ip binds the server to
2364 * a specific interface. If no arguments are given to the server, it
2365 * uses IP and binds to 0.0.0.0.
2368 static int fio_handle_server_arg(void)
2370 int port = fio_net_port;
2374 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2376 if (!fio_server_arg)
2379 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2380 &port, &saddr_in.sin_addr,
2381 &saddr_in6.sin6_addr, &use_ipv6);
2383 if (!is_sock && bind_sock) {
2389 fio_net_port = port;
2390 saddr_in.sin_port = htons(port);
2391 saddr_in6.sin6_port = htons(port);
2395 static void sig_int(int sig)
2401 static void set_sig_handlers(void)
2403 struct sigaction act;
2405 memset(&act, 0, sizeof(act));
2406 act.sa_handler = sig_int;
2407 act.sa_flags = SA_RESTART;
2408 sigaction(SIGINT, &act, NULL);
2411 void fio_server_destroy_sk_key(void)
2413 pthread_key_delete(sk_out_key);
2416 int fio_server_create_sk_key(void)
2418 if (pthread_key_create(&sk_out_key, NULL)) {
2419 log_err("fio: can't create sk_out backend key\n");
2423 pthread_setspecific(sk_out_key, NULL);
2427 static int fio_server(void)
2431 dprint(FD_NET, "starting server\n");
2433 if (fio_handle_server_arg())
2436 sk = fio_init_server_connection();
2442 ret = accept_loop(sk);
2446 if (fio_server_arg) {
2447 free(fio_server_arg);
2448 fio_server_arg = NULL;
2456 void fio_server_got_signal(int signal)
2458 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2462 if (signal == SIGPIPE)
2465 log_info("\nfio: terminating on signal %d\n", signal);
2470 static int check_existing_pidfile(const char *pidfile)
2477 if (stat(pidfile, &sb))
2480 f = fopen(pidfile, "r");
2484 if (fread(buf, sb.st_size, 1, f) <= 0) {
2491 if (kill(pid, SIGCONT) < 0)
2492 return errno != ESRCH;
2497 static int write_pid(pid_t pid, const char *pidfile)
2501 fpid = fopen(pidfile, "w");
2503 log_err("fio: failed opening pid file %s\n", pidfile);
2507 fprintf(fpid, "%u\n", (unsigned int) pid);
2513 * If pidfile is specified, background us.
2515 int fio_start_server(char *pidfile)
2522 WSAStartup(MAKEWORD(2, 2), &wsd);
2526 return fio_server();
2528 if (check_existing_pidfile(pidfile)) {
2529 log_err("fio: pidfile %s exists and server appears alive\n",
2537 log_err("fio: failed server fork: %s\n", strerror(errno));
2541 ret = write_pid(pid, pidfile);
2547 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2549 close(STDIN_FILENO);
2550 close(STDOUT_FILENO);
2551 close(STDERR_FILENO);
2563 void fio_server_set_arg(const char *arg)
2565 fio_server_arg = strdup(arg);