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 */
54 unsigned int refs; /* frees sk_out when it drops to zero.
55 * protected by below ->lock */
57 int sk; /* socket fd to talk to client */
58 struct fio_mutex lock; /* protects ref and below list */
59 struct flist_head list; /* list of pending transmit work */
60 struct fio_mutex wait; /* wake backend when items added to list */
61 struct fio_mutex xmit; /* held while sending data */
64 static char *fio_server_arg;
65 static char *bind_sock;
66 static struct sockaddr_in saddr_in;
67 static struct sockaddr_in6 saddr_in6;
70 static unsigned int has_zlib = 1;
72 static unsigned int has_zlib = 0;
74 static unsigned int use_zlib;
77 static pthread_key_t sk_out_key;
79 struct fio_fork_item {
80 struct flist_head list;
88 struct fio_mutex lock;
94 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
120 static void sk_lock(struct sk_out *sk_out)
122 fio_mutex_down(&sk_out->lock);
125 static void sk_unlock(struct sk_out *sk_out)
127 fio_mutex_up(&sk_out->lock);
130 void sk_out_assign(struct sk_out *sk_out)
138 pthread_setspecific(sk_out_key, sk_out);
141 static void sk_out_free(struct sk_out *sk_out)
143 __fio_mutex_remove(&sk_out->lock);
144 __fio_mutex_remove(&sk_out->wait);
145 __fio_mutex_remove(&sk_out->xmit);
149 static int __sk_out_drop(struct sk_out *sk_out)
155 assert(sk_out->refs != 0);
156 refs = --sk_out->refs;
161 pthread_setspecific(sk_out_key, NULL);
169 void sk_out_drop(void)
171 struct sk_out *sk_out;
173 sk_out = pthread_getspecific(sk_out_key);
174 __sk_out_drop(sk_out);
177 static void __fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
178 uint32_t pdu_len, uint64_t tag)
180 memset(cmd, 0, sizeof(*cmd));
182 cmd->version = __cpu_to_le16(FIO_SERVER_VER);
183 cmd->opcode = cpu_to_le16(opcode);
184 cmd->tag = cpu_to_le64(tag);
185 cmd->pdu_len = cpu_to_le32(pdu_len);
189 static void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
190 const void *pdu, uint32_t pdu_len, uint64_t tag)
192 __fio_init_net_cmd(cmd, opcode, pdu_len, tag);
195 memcpy(&cmd->payload, pdu, pdu_len);
198 const char *fio_server_op(unsigned int op)
202 if (op < FIO_NET_CMD_NR)
203 return fio_server_ops[op];
205 sprintf(buf, "UNKNOWN/%d", op);
209 static ssize_t iov_total_len(const struct iovec *iov, int count)
221 static int fio_sendv_data(int sk, struct iovec *iov, int count)
223 ssize_t total_len = iov_total_len(iov, count);
227 ret = writev(sk, iov, count);
234 if (ret >= iov->iov_len) {
239 iov->iov_base += ret;
245 else if (errno == EAGAIN || errno == EINTR)
249 } while (!exit_backend);
257 static int fio_send_data(int sk, const void *p, unsigned int len)
259 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
261 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
263 return fio_sendv_data(sk, &iov, 1);
266 static int fio_recv_data(int sk, void *p, unsigned int len, bool wait)
273 flags = OS_MSG_DONTWAIT;
276 int ret = recv(sk, p, len, flags);
286 else if (errno == EAGAIN || errno == EINTR) {
292 } while (!exit_backend);
300 static int verify_convert_cmd(struct fio_net_cmd *cmd)
304 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
305 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
307 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
308 if (crc != cmd->cmd_crc16) {
309 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
310 cmd->cmd_crc16, crc);
314 cmd->version = le16_to_cpu(cmd->version);
315 cmd->opcode = le16_to_cpu(cmd->opcode);
316 cmd->flags = le32_to_cpu(cmd->flags);
317 cmd->tag = le64_to_cpu(cmd->tag);
318 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
320 switch (cmd->version) {
324 log_err("fio: bad server cmd version %d\n", cmd->version);
328 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
329 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
337 * Read (and defragment, if necessary) incoming commands
339 struct fio_net_cmd *fio_net_recv_cmd(int sk, bool wait)
341 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
342 size_t cmd_size = 0, pdu_offset = 0;
348 ret = fio_recv_data(sk, &cmd, sizeof(cmd), wait);
352 /* We have a command, verify it and swap if need be */
353 ret = verify_convert_cmd(&cmd);
358 /* if this is text, add room for \0 at the end */
359 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
362 cmd_size += cmd.pdu_len;
364 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
365 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
370 tmp = realloc(cmdret, cmd_size);
372 log_err("fio: server failed allocating cmd\n");
379 memcpy(cmdret, &cmd, sizeof(cmd));
380 else if (cmdret->opcode != cmd.opcode) {
381 log_err("fio: fragment opcode mismatch (%d != %d)\n",
382 cmdret->opcode, cmd.opcode);
390 /* There's payload, get it */
391 pdu = (void *) cmdret->payload + pdu_offset;
392 ret = fio_recv_data(sk, pdu, cmd.pdu_len, wait);
396 /* Verify payload crc */
397 crc = fio_crc16(pdu, cmd.pdu_len);
398 if (crc != cmd.pdu_crc16) {
399 log_err("fio: server bad crc on payload ");
400 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
405 pdu_offset += cmd.pdu_len;
407 cmdret->pdu_len += cmd.pdu_len;
409 } while (cmd.flags & FIO_NET_CMD_F_MORE);
415 /* zero-terminate text input */
416 if (cmdret->pdu_len) {
417 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
418 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
419 char *buf = (char *) __pdu->buf;
421 buf[__pdu->buf_len] = '\0';
422 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
423 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
424 char *buf = (char *) __pdu->buf;
425 int len = le32_to_cpu(__pdu->buf_len);
431 /* frag flag is internal */
432 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
438 static void add_reply(uint64_t tag, struct flist_head *list)
440 struct fio_net_cmd_reply *reply;
442 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
443 flist_add_tail(&reply->list, list);
446 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
448 struct fio_net_cmd_reply *reply;
450 reply = calloc(1, sizeof(*reply));
451 INIT_FLIST_HEAD(&reply->list);
452 fio_gettime(&reply->tv, NULL);
453 reply->saved_tag = tag;
454 reply->opcode = opcode;
456 return (uintptr_t) reply;
459 static void free_reply(uint64_t tag)
461 struct fio_net_cmd_reply *reply;
463 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
467 static void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
471 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
473 pdu_len = le32_to_cpu(cmd->pdu_len);
474 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
477 static void fio_net_cmd_crc(struct fio_net_cmd *cmd)
479 fio_net_cmd_crc_pdu(cmd, cmd->payload);
482 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
483 uint64_t *tagptr, struct flist_head *list)
485 struct fio_net_cmd *cmd = NULL;
486 size_t this_len, cur_len = 0;
492 tag = *tagptr = alloc_reply(*tagptr, opcode);
494 tag = tagptr ? *tagptr : 0;
498 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
499 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
501 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
505 cur_len = sizeof(*cmd) + this_len;
506 cmd = malloc(cur_len);
509 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
512 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
514 fio_net_cmd_crc(cmd);
516 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
519 } while (!ret && size);
525 add_reply(tag, list);
534 static struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf,
535 size_t size, uint64_t *tagptr,
538 struct sk_entry *entry;
540 entry = smalloc(sizeof(*entry));
541 INIT_FLIST_HEAD(&entry->next);
542 entry->opcode = opcode;
543 if (flags & SK_F_COPY) {
544 entry->buf = smalloc(size);
545 memcpy(entry->buf, buf, size);
551 entry->tag = *tagptr;
554 entry->flags = flags;
558 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry);
560 static void fio_net_queue_entry(struct sk_entry *entry)
562 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
564 if (entry->flags & SK_F_INLINE)
565 handle_sk_entry(sk_out, entry);
568 flist_add_tail(&entry->list, &sk_out->list);
571 fio_mutex_up(&sk_out->wait);
575 static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
576 uint64_t *tagptr, int flags)
578 struct sk_entry *entry;
580 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
582 fio_net_queue_entry(entry);
589 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
591 struct fio_net_cmd cmd;
593 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
594 fio_net_cmd_crc(&cmd);
596 return fio_send_data(sk, &cmd, sizeof(cmd));
600 * If 'list' is non-NULL, then allocate and store the sent command for
601 * later verification.
603 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
604 struct flist_head *list)
609 tag = alloc_reply(tag, opcode);
611 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
620 add_reply(tag, list);
625 static int fio_net_queue_quit(void)
627 dprint(FD_NET, "server: sending quit\n");
629 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, NULL, SK_F_SIMPLE);
632 int fio_net_send_quit(int sk)
634 dprint(FD_NET, "server: sending quit\n");
636 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
639 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
641 struct cmd_end_pdu epdu;
647 epdu.error = __cpu_to_le32(error);
648 epdu.signal = __cpu_to_le32(signal);
649 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
652 static int fio_net_queue_stop(int error, int signal)
654 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
655 return fio_net_send_ack(NULL, error, signal);
658 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
660 struct fio_fork_item *ffi;
662 ffi = malloc(sizeof(*ffi));
667 flist_add_tail(&ffi->list, list);
670 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
672 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
673 fio_server_add_fork_item(pid, conn_list);
676 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
678 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
679 fio_server_add_fork_item(pid, job_list);
682 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
686 ret = waitpid(ffi->pid, &status, WNOHANG);
688 if (errno == ECHILD) {
689 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
692 log_err("fio: waitpid: %s\n", strerror(errno));
693 } else if (ret == ffi->pid) {
694 if (WIFSIGNALED(status)) {
695 ffi->signal = WTERMSIG(status);
698 if (WIFEXITED(status)) {
699 if (WEXITSTATUS(status))
700 ffi->exitval = WEXITSTATUS(status);
706 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
708 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
711 * Fold STOP and QUIT...
714 fio_net_queue_stop(ffi->exitval, ffi->signal);
715 fio_net_queue_quit();
718 flist_del(&ffi->list);
722 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
724 struct flist_head *entry, *tmp;
725 struct fio_fork_item *ffi;
727 flist_for_each_safe(entry, tmp, list) {
728 ffi = flist_entry(entry, struct fio_fork_item, list);
730 fio_server_check_fork_item(ffi);
733 fio_server_fork_item_done(ffi, stop);
737 static void fio_server_check_jobs(struct flist_head *job_list)
739 fio_server_check_fork_items(job_list, true);
742 static void fio_server_check_conns(struct flist_head *conn_list)
744 fio_server_check_fork_items(conn_list, false);
747 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
749 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
750 void *file_name = pdu->file;
751 struct cmd_start_pdu spdu;
753 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
755 pdu->name_len = le16_to_cpu(pdu->name_len);
756 pdu->client_type = le16_to_cpu(pdu->client_type);
758 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
759 fio_net_queue_quit();
763 spdu.jobs = cpu_to_le32(thread_number);
764 spdu.stat_outputs = cpu_to_le32(stat_number);
765 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
769 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
770 struct fio_net_cmd *cmd)
775 sk_out_assign(sk_out);
782 fio_server_add_job_pid(job_list, pid);
786 ret = fio_backend(sk_out);
792 static int handle_job_cmd(struct fio_net_cmd *cmd)
794 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
795 void *buf = pdu->buf;
796 struct cmd_start_pdu spdu;
798 pdu->buf_len = le32_to_cpu(pdu->buf_len);
799 pdu->client_type = le32_to_cpu(pdu->client_type);
801 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
802 fio_net_queue_quit();
806 spdu.jobs = cpu_to_le32(thread_number);
807 spdu.stat_outputs = cpu_to_le32(stat_number);
809 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
813 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
815 void *pdu = cmd->payload;
816 struct cmd_single_line_pdu *cslp;
817 struct cmd_line_pdu *clp;
818 unsigned long offset;
819 struct cmd_start_pdu spdu;
824 clp->lines = le16_to_cpu(clp->lines);
825 clp->client_type = le16_to_cpu(clp->client_type);
826 argv = malloc(clp->lines * sizeof(char *));
827 offset = sizeof(*clp);
829 dprint(FD_NET, "server: %d command line args\n", clp->lines);
831 for (i = 0; i < clp->lines; i++) {
833 argv[i] = (char *) cslp->text;
835 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
836 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
839 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
840 fio_net_queue_quit();
847 spdu.jobs = cpu_to_le32(thread_number);
848 spdu.stat_outputs = cpu_to_le32(stat_number);
850 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
854 static int handle_probe_cmd(struct fio_net_cmd *cmd)
856 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
857 struct cmd_probe_reply_pdu probe;
858 uint64_t tag = cmd->tag;
860 dprint(FD_NET, "server: sending probe reply\n");
862 strcpy(me, (char *) pdu->server);
864 memset(&probe, 0, sizeof(probe));
865 gethostname((char *) probe.hostname, sizeof(probe.hostname));
866 #ifdef CONFIG_BIG_ENDIAN
869 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
872 probe.arch = FIO_ARCH;
873 probe.bpp = sizeof(void *);
874 probe.cpus = __cpu_to_le32(cpus_online());
877 * If the client supports compression and we do too, then enable it
879 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
880 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
887 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
890 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
893 uint64_t tag = cmd->tag;
897 dprint(FD_NET, "server sending status\n");
900 * Fake ETA return if we don't have a local one, otherwise the client
901 * will end up timing out waiting for a response to the ETA request
903 je = get_jobs_eta(true, &size);
906 je = calloc(1, size);
908 je->nr_running = cpu_to_le32(je->nr_running);
909 je->nr_ramp = cpu_to_le32(je->nr_ramp);
910 je->nr_pending = cpu_to_le32(je->nr_pending);
911 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
912 je->files_open = cpu_to_le32(je->files_open);
914 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
915 je->m_rate[i] = cpu_to_le64(je->m_rate[i]);
916 je->t_rate[i] = cpu_to_le64(je->t_rate[i]);
917 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
918 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
919 je->rate[i] = cpu_to_le64(je->rate[i]);
920 je->iops[i] = cpu_to_le32(je->iops[i]);
923 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
924 je->eta_sec = cpu_to_le64(je->eta_sec);
925 je->nr_threads = cpu_to_le32(je->nr_threads);
926 je->is_pow2 = cpu_to_le32(je->is_pow2);
927 je->unit_base = cpu_to_le32(je->unit_base);
930 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
934 static int send_update_job_reply(uint64_t __tag, int error)
936 uint64_t tag = __tag;
939 pdu_error = __cpu_to_le32(error);
940 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
943 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
945 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
946 struct thread_data *td;
949 tnumber = le32_to_cpu(pdu->thread_number);
951 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
953 if (!tnumber || tnumber > thread_number) {
954 send_update_job_reply(cmd->tag, ENODEV);
958 td = &threads[tnumber - 1];
959 convert_thread_options_to_cpu(&td->o, &pdu->top);
960 send_update_job_reply(cmd->tag, 0);
964 static int handle_trigger_cmd(struct fio_net_cmd *cmd)
966 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
967 char *buf = (char *) pdu->cmd;
968 struct all_io_list *rep;
971 pdu->len = le16_to_cpu(pdu->len);
972 buf[pdu->len] = '\0';
974 rep = get_all_io_list(IO_LIST_ALL, &sz);
976 struct all_io_list state;
978 state.threads = cpu_to_le64((uint64_t) 0);
979 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY | SK_F_INLINE);
981 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE | SK_F_INLINE);
987 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
988 struct fio_net_cmd *cmd)
992 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
993 fio_server_op(cmd->opcode), cmd->pdu_len,
994 (unsigned long long) cmd->tag);
996 switch (cmd->opcode) {
997 case FIO_NET_CMD_QUIT:
998 fio_terminate_threads(TERMINATE_ALL);
1001 case FIO_NET_CMD_EXIT:
1004 case FIO_NET_CMD_LOAD_FILE:
1005 ret = handle_load_file_cmd(cmd);
1007 case FIO_NET_CMD_JOB:
1008 ret = handle_job_cmd(cmd);
1010 case FIO_NET_CMD_JOBLINE:
1011 ret = handle_jobline_cmd(cmd);
1013 case FIO_NET_CMD_PROBE:
1014 ret = handle_probe_cmd(cmd);
1016 case FIO_NET_CMD_SEND_ETA:
1017 ret = handle_send_eta_cmd(cmd);
1019 case FIO_NET_CMD_RUN:
1020 ret = handle_run_cmd(sk_out, job_list, cmd);
1022 case FIO_NET_CMD_UPDATE_JOB:
1023 ret = handle_update_job_cmd(cmd);
1025 case FIO_NET_CMD_VTRIGGER:
1026 ret = handle_trigger_cmd(cmd);
1028 case FIO_NET_CMD_SENDFILE: {
1029 struct cmd_sendfile_reply *in;
1030 struct cmd_reply *rep;
1032 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
1034 in = (struct cmd_sendfile_reply *) cmd->payload;
1035 in->size = le32_to_cpu(in->size);
1036 in->error = le32_to_cpu(in->error);
1039 rep->error = in->error;
1042 rep->data = smalloc(in->size);
1045 rep->error = ENOMEM;
1047 rep->size = in->size;
1048 memcpy(rep->data, in->data, in->size);
1051 fio_mutex_up(&rep->lock);
1055 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
1063 * Send a command with a separate PDU, not inlined in the command
1065 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1066 off_t size, uint64_t tag, uint32_t flags)
1068 struct fio_net_cmd cmd;
1069 struct iovec iov[2];
1073 iov[0].iov_base = (void *) &cmd;
1074 iov[0].iov_len = sizeof(cmd);
1077 uint32_t this_flags = flags;
1080 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
1081 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
1083 if (this_len < size)
1084 this_flags |= FIO_NET_CMD_F_MORE;
1086 __fio_init_net_cmd(&cmd, opcode, this_len, tag);
1087 cmd.flags = __cpu_to_le32(this_flags);
1088 fio_net_cmd_crc_pdu(&cmd, buf);
1090 iov[1].iov_base = (void *) buf;
1091 iov[1].iov_len = this_len;
1093 ret = fio_sendv_data(sk, iov, 2);
1096 } while (!ret && size);
1101 static void finish_entry(struct sk_entry *entry)
1103 if (entry->flags & SK_F_FREE)
1105 else if (entry->flags & SK_F_COPY)
1111 static void entry_set_flags(struct sk_entry *entry, struct flist_head *list,
1112 unsigned int *flags)
1114 if (!flist_empty(list))
1115 *flags = FIO_NET_CMD_F_MORE;
1120 static int send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1125 entry_set_flags(first, &first->next, &flags);
1127 ret = fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf,
1128 first->size, first->tag, flags);
1130 while (!flist_empty(&first->next)) {
1131 struct sk_entry *next;
1133 next = flist_first_entry(&first->next, struct sk_entry, list);
1134 flist_del_init(&next->list);
1136 entry_set_flags(next, &first->next, &flags);
1138 ret += fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf,
1139 next->size, next->tag, flags);
1146 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1150 fio_mutex_down(&sk_out->xmit);
1152 if (entry->flags & SK_F_VEC)
1153 ret = send_vec_entry(sk_out, entry);
1154 else if (entry->flags & SK_F_SIMPLE) {
1155 ret = fio_net_send_simple_cmd(sk_out->sk, entry->opcode,
1158 ret = fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf,
1159 entry->size, &entry->tag, NULL);
1162 fio_mutex_up(&sk_out->xmit);
1165 log_err("fio: failed handling cmd %s\n", fio_server_op(entry->opcode));
1167 finish_entry(entry);
1171 static int handle_xmits(struct sk_out *sk_out)
1173 struct sk_entry *entry;
1178 if (flist_empty(&sk_out->list)) {
1183 flist_splice_init(&sk_out->list, &list);
1186 while (!flist_empty(&list)) {
1187 entry = flist_entry(list.next, struct sk_entry, list);
1188 flist_del(&entry->list);
1189 ret += handle_sk_entry(sk_out, entry);
1195 static int handle_connection(struct sk_out *sk_out)
1197 struct fio_net_cmd *cmd = NULL;
1198 FLIST_HEAD(job_list);
1204 while (!exit_backend) {
1205 struct pollfd pfd = {
1214 if (!flist_empty(&job_list))
1217 handle_xmits(sk_out);
1219 ret = poll(&pfd, 1, 0);
1223 log_err("fio: poll: %s\n", strerror(errno));
1226 fio_server_check_jobs(&job_list);
1227 fio_mutex_down_timeout(&sk_out->wait, timeout);
1231 if (pfd.revents & POLLIN)
1233 if (pfd.revents & (POLLERR|POLLHUP)) {
1237 } while (!exit_backend);
1239 fio_server_check_jobs(&job_list);
1244 cmd = fio_net_recv_cmd(sk_out->sk, true);
1250 ret = handle_command(sk_out, &job_list, cmd);
1261 handle_xmits(sk_out);
1265 __sk_out_drop(sk_out);
1269 /* get the address on this host bound by the input socket,
1270 * whether it is ipv6 or ipv4 */
1272 static int get_my_addr_str(int sk)
1274 struct sockaddr_in6 myaddr6 = { 0, };
1275 struct sockaddr_in myaddr4 = { 0, };
1276 struct sockaddr *sockaddr_p;
1282 len = sizeof(myaddr6);
1283 sockaddr_p = (struct sockaddr * )&myaddr6;
1284 net_addr = (char * )&myaddr6.sin6_addr;
1286 len = sizeof(myaddr4);
1287 sockaddr_p = (struct sockaddr * )&myaddr4;
1288 net_addr = (char * )&myaddr4.sin_addr;
1291 ret = getsockname(sk, sockaddr_p, &len);
1293 log_err("fio: getsockaddr: %s\n", strerror(errno));
1297 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1298 log_err("inet_ntop: failed to convert addr to string\n");
1302 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1306 static int accept_loop(int listen_sk)
1308 struct sockaddr_in addr;
1309 struct sockaddr_in6 addr6;
1310 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1312 int ret = 0, sk, exitval = 0;
1313 FLIST_HEAD(conn_list);
1315 dprint(FD_NET, "server enter accept loop\n");
1317 fio_set_fd_nonblocking(listen_sk, "server");
1319 while (!exit_backend) {
1320 struct sk_out *sk_out;
1326 pfd.events = POLLIN;
1330 if (!flist_empty(&conn_list))
1333 ret = poll(&pfd, 1, timeout);
1337 log_err("fio: poll: %s\n", strerror(errno));
1340 fio_server_check_conns(&conn_list);
1344 if (pfd.revents & POLLIN)
1346 } while (!exit_backend);
1348 fio_server_check_conns(&conn_list);
1350 if (exit_backend || ret < 0)
1354 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1356 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1359 log_err("fio: accept: %s\n", strerror(errno));
1364 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1366 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1368 dprint(FD_NET, "server: connect from %s\n", from);
1370 sk_out = smalloc(sizeof(*sk_out));
1372 INIT_FLIST_HEAD(&sk_out->list);
1373 __fio_mutex_init(&sk_out->lock, FIO_MUTEX_UNLOCKED);
1374 __fio_mutex_init(&sk_out->wait, FIO_MUTEX_LOCKED);
1375 __fio_mutex_init(&sk_out->xmit, FIO_MUTEX_UNLOCKED);
1380 fio_server_add_conn_pid(&conn_list, pid);
1384 /* if error, it's already logged, non-fatal */
1385 get_my_addr_str(sk);
1388 * Assign sk_out here, it'll be dropped in handle_connection()
1389 * since that function calls _exit() when done
1391 sk_out_assign(sk_out);
1392 handle_connection(sk_out);
1398 int fio_server_text_output(int level, const char *buf, size_t len)
1400 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1401 struct cmd_text_pdu *pdu;
1405 if (!sk_out || sk_out->sk == -1)
1408 tlen = sizeof(*pdu) + len;
1411 pdu->level = __cpu_to_le32(level);
1412 pdu->buf_len = __cpu_to_le32(len);
1414 gettimeofday(&tv, NULL);
1415 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1416 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1418 memcpy(pdu->buf, buf, len);
1420 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1425 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1427 dst->max_val = cpu_to_le64(src->max_val);
1428 dst->min_val = cpu_to_le64(src->min_val);
1429 dst->samples = cpu_to_le64(src->samples);
1432 * Encode to IEEE 754 for network transfer
1434 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1435 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1438 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1442 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1443 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1444 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1445 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1446 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1447 dst->iobytes[i] = cpu_to_le64(src->iobytes[i]);
1448 dst->agg[i] = cpu_to_le64(src->agg[i]);
1451 dst->kb_base = cpu_to_le32(src->kb_base);
1452 dst->unit_base = cpu_to_le32(src->unit_base);
1453 dst->groupid = cpu_to_le32(src->groupid);
1454 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1458 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1459 * into a single payload.
1461 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1463 struct cmd_ts_pdu p;
1466 uint64_t *ss_iops, *ss_bw;
1468 dprint(FD_NET, "server sending end stats\n");
1470 memset(&p, 0, sizeof(p));
1472 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1473 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1474 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1476 p.ts.error = cpu_to_le32(ts->error);
1477 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1478 p.ts.groupid = cpu_to_le32(ts->groupid);
1479 p.ts.pid = cpu_to_le32(ts->pid);
1480 p.ts.members = cpu_to_le32(ts->members);
1481 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1483 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1484 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1485 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1486 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1487 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1490 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1491 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1492 p.ts.ctx = cpu_to_le64(ts->ctx);
1493 p.ts.minf = cpu_to_le64(ts->minf);
1494 p.ts.majf = cpu_to_le64(ts->majf);
1495 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1496 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1498 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1499 fio_fp64_t *src = &ts->percentile_list[i];
1500 fio_fp64_t *dst = &p.ts.percentile_list[i];
1502 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1505 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1506 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1507 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1508 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1511 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1512 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1513 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1516 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1517 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1518 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1520 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1521 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1522 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1523 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1526 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1527 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1529 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1530 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1531 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1534 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1535 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1536 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1537 p.ts.first_error = cpu_to_le32(ts->first_error);
1538 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1539 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1541 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1542 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1543 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1544 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1546 p.ts.nr_block_infos = cpu_to_le64(ts->nr_block_infos);
1547 for (i = 0; i < p.ts.nr_block_infos; i++)
1548 p.ts.block_infos[i] = cpu_to_le32(ts->block_infos[i]);
1550 p.ts.ss_dur = cpu_to_le64(ts->ss_dur);
1551 p.ts.ss_state = cpu_to_le32(ts->ss_state);
1552 p.ts.ss_head = cpu_to_le32(ts->ss_head);
1553 p.ts.ss_limit.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_limit.u.f));
1554 p.ts.ss_slope.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_slope.u.f));
1555 p.ts.ss_deviation.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_deviation.u.f));
1556 p.ts.ss_criterion.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_criterion.u.f));
1558 convert_gs(&p.rs, rs);
1560 dprint(FD_NET, "ts->ss_state = %d\n", ts->ss_state);
1561 if (ts->ss_state & __FIO_SS_DATA) {
1562 dprint(FD_NET, "server sending steadystate ring buffers\n");
1564 ss_buf = malloc(sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t));
1566 memcpy(ss_buf, &p, sizeof(p));
1568 ss_iops = (uint64_t *) ((struct cmd_ts_pdu *)ss_buf + 1);
1569 ss_bw = ss_iops + (int) ts->ss_dur;
1570 for (i = 0; i < ts->ss_dur; i++) {
1571 ss_iops[i] = cpu_to_le64(ts->ss_iops_data[i]);
1572 ss_bw[i] = cpu_to_le64(ts->ss_bw_data[i]);
1575 fio_net_queue_cmd(FIO_NET_CMD_TS, ss_buf, sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t), NULL, SK_F_COPY);
1580 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1583 void fio_server_send_gs(struct group_run_stats *rs)
1585 struct group_run_stats gs;
1587 dprint(FD_NET, "server sending group run stats\n");
1589 convert_gs(&gs, rs);
1590 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1593 void fio_server_send_job_options(struct flist_head *opt_list,
1594 unsigned int groupid)
1596 struct cmd_job_option pdu;
1597 struct flist_head *entry;
1599 if (flist_empty(opt_list))
1602 flist_for_each(entry, opt_list) {
1603 struct print_option *p;
1606 p = flist_entry(entry, struct print_option, list);
1607 memset(&pdu, 0, sizeof(pdu));
1609 if (groupid == -1U) {
1610 pdu.global = __cpu_to_le16(1);
1614 pdu.groupid = cpu_to_le32(groupid);
1616 len = strlen(p->name);
1617 if (len >= sizeof(pdu.name)) {
1618 len = sizeof(pdu.name) - 1;
1619 pdu.truncated = __cpu_to_le16(1);
1621 memcpy(pdu.name, p->name, len);
1623 len = strlen(p->value);
1624 if (len >= sizeof(pdu.value)) {
1625 len = sizeof(pdu.value) - 1;
1626 pdu.truncated = __cpu_to_le16(1);
1628 memcpy(pdu.value, p->value, len);
1630 fio_net_queue_cmd(FIO_NET_CMD_JOB_OPT, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1634 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1638 for (i = 0; i < 2; i++) {
1639 dst->ios[i] = cpu_to_le64(src->ios[i]);
1640 dst->merges[i] = cpu_to_le64(src->merges[i]);
1641 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1642 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1645 dst->io_ticks = cpu_to_le64(src->io_ticks);
1646 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1647 dst->slavecount = cpu_to_le32(src->slavecount);
1648 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1651 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1655 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1656 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1658 for (i = 0; i < 2; i++) {
1659 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1660 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1661 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1662 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1665 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1666 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1667 dst->s.msec = cpu_to_le64(src->s.msec);
1670 void fio_server_send_du(void)
1672 struct disk_util *du;
1673 struct flist_head *entry;
1674 struct cmd_du_pdu pdu;
1676 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1678 memset(&pdu, 0, sizeof(pdu));
1680 flist_for_each(entry, &disk_list) {
1681 du = flist_entry(entry, struct disk_util, list);
1683 convert_dus(&pdu.dus, &du->dus);
1684 convert_agg(&pdu.agg, &du->agg);
1686 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1692 static inline void __fio_net_prep_tail(z_stream *stream, void *out_pdu,
1693 struct sk_entry **last_entry,
1694 struct sk_entry *first)
1696 unsigned int this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1698 *last_entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1699 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1700 flist_add_tail(&(*last_entry)->list, &first->next);
1705 * Deflates the next input given, creating as many new packets in the
1706 * linked list as necessary.
1708 static int __deflate_pdu_buffer(void *next_in, unsigned int next_sz, void **out_pdu,
1709 struct sk_entry **last_entry, z_stream *stream,
1710 struct sk_entry *first)
1714 stream->next_in = next_in;
1715 stream->avail_in = next_sz;
1717 if (! stream->avail_out) {
1719 __fio_net_prep_tail(stream, *out_pdu, last_entry, first);
1721 *out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1723 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1724 stream->next_out = *out_pdu;
1727 ret = deflate(stream, Z_BLOCK);
1733 } while (stream->avail_in);
1738 static int __fio_append_iolog_gz_hist(struct sk_entry *first, struct io_log *log,
1739 struct io_logs *cur_log, z_stream *stream)
1741 struct sk_entry *entry;
1744 int sample_sz = log_entry_sz(log);
1746 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1747 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1748 stream->next_out = out_pdu;
1750 for (i = 0; i < cur_log->nr_samples; i++) {
1751 struct io_sample *s;
1752 struct io_u_plat_entry *cur_plat_entry, *prev_plat_entry;
1753 unsigned int *cur_plat, *prev_plat;
1755 s = get_sample(log, cur_log, i);
1756 ret = __deflate_pdu_buffer(s, sample_sz, &out_pdu, &entry, stream, first);
1760 /* Do the subtraction on server side so that client doesn't have to
1761 * reconstruct our linked list from packets.
1763 cur_plat_entry = s->data.plat_entry;
1764 prev_plat_entry = flist_first_entry(&cur_plat_entry->list, struct io_u_plat_entry, list);
1765 cur_plat = cur_plat_entry->io_u_plat;
1766 prev_plat = prev_plat_entry->io_u_plat;
1768 for (j = 0; j < FIO_IO_U_PLAT_NR; j++) {
1769 cur_plat[j] -= prev_plat[j];
1772 flist_del(&prev_plat_entry->list);
1773 free(prev_plat_entry);
1775 ret = __deflate_pdu_buffer(cur_plat_entry, sizeof(*cur_plat_entry),
1776 &out_pdu, &entry, stream, first);
1782 __fio_net_prep_tail(stream, out_pdu, &entry, first);
1787 static int __fio_append_iolog_gz(struct sk_entry *first, struct io_log *log,
1788 struct io_logs *cur_log, z_stream *stream)
1790 unsigned int this_len;
1794 if (log->log_type == IO_LOG_TYPE_HIST)
1795 return __fio_append_iolog_gz_hist(first, log, cur_log, stream);
1797 stream->next_in = (void *) cur_log->log;
1798 stream->avail_in = cur_log->nr_samples * log_entry_sz(log);
1801 struct sk_entry *entry;
1804 * Dirty - since the log is potentially huge, compress it into
1805 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1806 * side defragment it.
1808 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1810 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1811 stream->next_out = out_pdu;
1812 ret = deflate(stream, Z_BLOCK);
1813 /* may be Z_OK, or Z_STREAM_END */
1819 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1821 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1822 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1823 flist_add_tail(&entry->list, &first->next);
1824 } while (stream->avail_in);
1829 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1834 memset(&stream, 0, sizeof(stream));
1835 stream.zalloc = Z_NULL;
1836 stream.zfree = Z_NULL;
1837 stream.opaque = Z_NULL;
1839 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK)
1842 while (!flist_empty(&log->io_logs)) {
1843 struct io_logs *cur_log;
1845 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1846 flist_del_init(&cur_log->list);
1848 ret = __fio_append_iolog_gz(first, log, cur_log, &stream);
1853 ret = deflate(&stream, Z_FINISH);
1855 while (ret != Z_STREAM_END) {
1856 struct sk_entry *entry;
1857 unsigned int this_len;
1860 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1861 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1862 stream.next_out = out_pdu;
1864 ret = deflate(&stream, Z_FINISH);
1865 /* may be Z_OK, or Z_STREAM_END */
1871 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1873 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1874 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1875 flist_add_tail(&entry->list, &first->next);
1876 } while (ret != Z_STREAM_END);
1878 ret = deflateEnd(&stream);
1885 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1891 static int fio_append_gz_chunks(struct sk_entry *first, struct io_log *log)
1893 struct sk_entry *entry;
1894 struct flist_head *node;
1896 pthread_mutex_lock(&log->chunk_lock);
1897 flist_for_each(node, &log->chunk_list) {
1898 struct iolog_compress *c;
1900 c = flist_entry(node, struct iolog_compress, list);
1901 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, c->buf, c->len,
1902 NULL, SK_F_VEC | SK_F_INLINE);
1903 flist_add_tail(&entry->list, &first->next);
1905 pthread_mutex_unlock(&log->chunk_lock);
1910 static int fio_append_text_log(struct sk_entry *first, struct io_log *log)
1912 struct sk_entry *entry;
1914 while (!flist_empty(&log->io_logs)) {
1915 struct io_logs *cur_log;
1918 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1919 flist_del_init(&cur_log->list);
1921 size = cur_log->nr_samples * log_entry_sz(log);
1923 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, cur_log->log, size,
1924 NULL, SK_F_VEC | SK_F_INLINE);
1925 flist_add_tail(&entry->list, &first->next);
1931 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1933 struct cmd_iolog_pdu pdu;
1934 struct sk_entry *first;
1935 struct flist_head *entry;
1938 pdu.nr_samples = cpu_to_le64(iolog_nr_samples(log));
1939 pdu.thread_number = cpu_to_le32(td->thread_number);
1940 pdu.log_type = cpu_to_le32(log->log_type);
1941 pdu.log_hist_coarseness = cpu_to_le32(log->hist_coarseness);
1943 if (!flist_empty(&log->chunk_list))
1944 pdu.compressed = __cpu_to_le32(STORE_COMPRESSED);
1946 pdu.compressed = __cpu_to_le32(XMIT_COMPRESSED);
1950 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1951 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1954 * We can't do this for a pre-compressed log, but for that case,
1955 * log->nr_samples is zero anyway.
1957 flist_for_each(entry, &log->io_logs) {
1958 struct io_logs *cur_log;
1961 cur_log = flist_entry(entry, struct io_logs, list);
1963 for (i = 0; i < cur_log->nr_samples; i++) {
1964 struct io_sample *s = get_sample(log, cur_log, i);
1966 s->time = cpu_to_le64(s->time);
1967 s->data.val = cpu_to_le64(s->data.val);
1968 s->__ddir = cpu_to_le32(s->__ddir);
1969 s->bs = cpu_to_le32(s->bs);
1971 if (log->log_offset) {
1972 struct io_sample_offset *so = (void *) s;
1974 so->offset = cpu_to_le64(so->offset);
1980 * Assemble header entry first
1982 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_VEC | SK_F_INLINE | SK_F_COPY);
1985 * Now append actual log entries. If log compression was enabled on
1986 * the job, just send out the compressed chunks directly. If we
1987 * have a plain log, compress if we can, then send. Otherwise, send
1988 * the plain text output.
1990 if (!flist_empty(&log->chunk_list))
1991 ret = fio_append_gz_chunks(first, log);
1993 ret = fio_append_iolog_gz(first, log);
1995 ret = fio_append_text_log(first, log);
1997 fio_net_queue_entry(first);
2001 void fio_server_send_add_job(struct thread_data *td)
2003 struct cmd_add_job_pdu pdu;
2005 memset(&pdu, 0, sizeof(pdu));
2006 pdu.thread_number = cpu_to_le32(td->thread_number);
2007 pdu.groupid = cpu_to_le32(td->groupid);
2008 convert_thread_options_to_net(&pdu.top, &td->o);
2010 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL,
2014 void fio_server_send_start(struct thread_data *td)
2016 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2018 assert(sk_out->sk != -1);
2020 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, NULL, SK_F_SIMPLE);
2023 int fio_server_get_verify_state(const char *name, int threadnumber,
2026 struct thread_io_list *s;
2027 struct cmd_sendfile out;
2028 struct cmd_reply *rep;
2033 dprint(FD_NET, "server: request verify state\n");
2035 rep = smalloc(sizeof(*rep));
2039 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
2043 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
2045 tag = (uint64_t) (uintptr_t) rep;
2046 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag,
2050 * Wait for the backend to receive the reply
2052 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
2053 log_err("fio: timed out waiting for reply\n");
2059 log_err("fio: failure on receiving state file %s: %s\n",
2060 out.path, strerror(rep->error));
2065 fio_net_queue_quit();
2070 * The format is verify_state_hdr, then thread_io_list. Verify
2071 * the header, and the thread_io_list checksum
2073 s = rep->data + sizeof(struct verify_state_hdr);
2074 if (verify_state_hdr(rep->data, s)) {
2080 * Don't need the header from now, copy just the thread_io_list
2083 rep->size -= sizeof(struct verify_state_hdr);
2084 data = malloc(rep->size);
2085 memcpy(data, s, rep->size);
2089 __fio_mutex_remove(&rep->lock);
2094 static int fio_init_server_ip(void)
2096 struct sockaddr *addr;
2103 sk = socket(AF_INET6, SOCK_STREAM, 0);
2105 sk = socket(AF_INET, SOCK_STREAM, 0);
2108 log_err("fio: socket: %s\n", strerror(errno));
2113 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
2114 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
2120 * Not fatal if fails, so just ignore it if that happens
2122 setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
2126 const void *src = &saddr_in6.sin6_addr;
2128 addr = (struct sockaddr *) &saddr_in6;
2129 socklen = sizeof(saddr_in6);
2130 saddr_in6.sin6_family = AF_INET6;
2131 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
2133 const void *src = &saddr_in.sin_addr;
2135 addr = (struct sockaddr *) &saddr_in;
2136 socklen = sizeof(saddr_in);
2137 saddr_in.sin_family = AF_INET;
2138 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
2141 if (bind(sk, addr, socklen) < 0) {
2142 log_err("fio: bind: %s\n", strerror(errno));
2143 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
2151 static int fio_init_server_sock(void)
2153 struct sockaddr_un addr;
2158 sk = socket(AF_UNIX, SOCK_STREAM, 0);
2160 log_err("fio: socket: %s\n", strerror(errno));
2166 memset(&addr, 0, sizeof(addr));
2167 addr.sun_family = AF_UNIX;
2168 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
2170 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
2172 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
2173 log_err("fio: bind: %s\n", strerror(errno));
2182 static int fio_init_server_connection(void)
2187 dprint(FD_NET, "starting server\n");
2190 sk = fio_init_server_ip();
2192 sk = fio_init_server_sock();
2197 memset(bind_str, 0, sizeof(bind_str));
2206 src = &saddr_in6.sin6_addr;
2209 src = &saddr_in.sin_addr;
2212 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
2214 sprintf(port, ",%u", fio_net_port);
2218 strncpy(bind_str, port, sizeof(bind_str) - 1);
2220 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
2222 log_info("fio: server listening on %s\n", bind_str);
2224 if (listen(sk, 4) < 0) {
2225 log_err("fio: listen: %s\n", strerror(errno));
2233 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
2234 struct in6_addr *inp6)
2240 ret = inet_pton(AF_INET6, host, inp6);
2242 ret = inet_pton(AF_INET, host, inp);
2245 struct addrinfo hints, *res;
2247 memset(&hints, 0, sizeof(hints));
2248 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
2249 hints.ai_socktype = SOCK_STREAM;
2251 ret = getaddrinfo(host, NULL, &hints, &res);
2253 log_err("fio: failed to resolve <%s> (%s)\n", host,
2259 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
2261 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
2271 * Parse a host/ip/port string. Reads from 'str'.
2276 * *ptr is the host, *port is the port, inp is the destination.
2278 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
2279 * For local domain sockets:
2280 * *ptr is the filename, *is_sock is 1.
2282 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
2283 int *port, struct in_addr *inp,
2284 struct in6_addr *inp6, int *ipv6)
2286 const char *host = str;
2292 *port = fio_net_port;
2295 if (!strncmp(str, "sock:", 5)) {
2296 *ptr = strdup(str + 5);
2303 * Is it ip:<ip or host>:port
2305 if (!strncmp(host, "ip:", 3))
2307 else if (!strncmp(host, "ip4:", 4))
2309 else if (!strncmp(host, "ip6:", 4)) {
2312 } else if (host[0] == ':') {
2313 /* String is :port */
2316 if (!lport || lport > 65535) {
2317 log_err("fio: bad server port %u\n", lport);
2320 /* no hostname given, we are done */
2326 * If no port seen yet, check if there's a last ',' at the end
2329 portp = strchr(host, ',');
2333 lport = atoi(portp);
2334 if (!lport || lport > 65535) {
2335 log_err("fio: bad server port %u\n", lport);
2347 *ptr = strdup(host);
2349 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2356 *port = fio_net_port;
2362 * Server arg should be one of:
2364 * sock:/path/to/socket
2368 * Where sock uses unix domain sockets, and ip binds the server to
2369 * a specific interface. If no arguments are given to the server, it
2370 * uses IP and binds to 0.0.0.0.
2373 static int fio_handle_server_arg(void)
2375 int port = fio_net_port;
2376 int is_sock, ret = 0;
2378 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2380 if (!fio_server_arg)
2383 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2384 &port, &saddr_in.sin_addr,
2385 &saddr_in6.sin6_addr, &use_ipv6);
2387 if (!is_sock && bind_sock) {
2393 fio_net_port = port;
2394 saddr_in.sin_port = htons(port);
2395 saddr_in6.sin6_port = htons(port);
2399 static void sig_int(int sig)
2405 static void set_sig_handlers(void)
2407 struct sigaction act;
2409 memset(&act, 0, sizeof(act));
2410 act.sa_handler = sig_int;
2411 act.sa_flags = SA_RESTART;
2412 sigaction(SIGINT, &act, NULL);
2415 void fio_server_destroy_sk_key(void)
2417 pthread_key_delete(sk_out_key);
2420 int fio_server_create_sk_key(void)
2422 if (pthread_key_create(&sk_out_key, NULL)) {
2423 log_err("fio: can't create sk_out backend key\n");
2427 pthread_setspecific(sk_out_key, NULL);
2431 static int fio_server(void)
2435 dprint(FD_NET, "starting server\n");
2437 if (fio_handle_server_arg())
2440 sk = fio_init_server_connection();
2446 ret = accept_loop(sk);
2450 if (fio_server_arg) {
2451 free(fio_server_arg);
2452 fio_server_arg = NULL;
2460 void fio_server_got_signal(int signal)
2462 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2466 if (signal == SIGPIPE)
2469 log_info("\nfio: terminating on signal %d\n", signal);
2474 static int check_existing_pidfile(const char *pidfile)
2481 if (stat(pidfile, &sb))
2484 f = fopen(pidfile, "r");
2488 if (fread(buf, sb.st_size, 1, f) <= 0) {
2495 if (kill(pid, SIGCONT) < 0)
2496 return errno != ESRCH;
2501 static int write_pid(pid_t pid, const char *pidfile)
2505 fpid = fopen(pidfile, "w");
2507 log_err("fio: failed opening pid file %s\n", pidfile);
2511 fprintf(fpid, "%u\n", (unsigned int) pid);
2517 * If pidfile is specified, background us.
2519 int fio_start_server(char *pidfile)
2526 WSAStartup(MAKEWORD(2, 2), &wsd);
2530 return fio_server();
2532 if (check_existing_pidfile(pidfile)) {
2533 log_err("fio: pidfile %s exists and server appears alive\n",
2541 log_err("fio: failed server fork: %s\n", strerror(errno));
2545 ret = write_pid(pid, pidfile);
2551 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2553 close(STDIN_FILENO);
2554 close(STDOUT_FILENO);
2555 close(STDERR_FILENO);
2567 void fio_server_set_arg(const char *arg)
2569 fio_server_arg = strdup(arg);