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);
581 fio_net_queue_entry(entry);
585 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
587 struct fio_net_cmd cmd;
589 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
590 fio_net_cmd_crc(&cmd);
592 return fio_send_data(sk, &cmd, sizeof(cmd));
596 * If 'list' is non-NULL, then allocate and store the sent command for
597 * later verification.
599 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
600 struct flist_head *list)
605 tag = alloc_reply(tag, opcode);
607 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
616 add_reply(tag, list);
621 static int fio_net_queue_quit(void)
623 dprint(FD_NET, "server: sending quit\n");
625 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, 0, SK_F_SIMPLE);
628 int fio_net_send_quit(int sk)
630 dprint(FD_NET, "server: sending quit\n");
632 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
635 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
637 struct cmd_end_pdu epdu;
643 epdu.error = __cpu_to_le32(error);
644 epdu.signal = __cpu_to_le32(signal);
645 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
648 static int fio_net_queue_stop(int error, int signal)
650 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
651 return fio_net_send_ack(NULL, error, signal);
654 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
656 struct fio_fork_item *ffi;
658 ffi = malloc(sizeof(*ffi));
663 flist_add_tail(&ffi->list, list);
666 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
668 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
669 fio_server_add_fork_item(pid, conn_list);
672 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
674 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
675 fio_server_add_fork_item(pid, job_list);
678 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
682 ret = waitpid(ffi->pid, &status, WNOHANG);
684 if (errno == ECHILD) {
685 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
688 log_err("fio: waitpid: %s\n", strerror(errno));
689 } else if (ret == ffi->pid) {
690 if (WIFSIGNALED(status)) {
691 ffi->signal = WTERMSIG(status);
694 if (WIFEXITED(status)) {
695 if (WEXITSTATUS(status))
696 ffi->exitval = WEXITSTATUS(status);
702 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
704 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
707 * Fold STOP and QUIT...
710 fio_net_queue_stop(ffi->exitval, ffi->signal);
711 fio_net_queue_quit();
714 flist_del(&ffi->list);
718 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
720 struct flist_head *entry, *tmp;
721 struct fio_fork_item *ffi;
723 flist_for_each_safe(entry, tmp, list) {
724 ffi = flist_entry(entry, struct fio_fork_item, list);
726 fio_server_check_fork_item(ffi);
729 fio_server_fork_item_done(ffi, stop);
733 static void fio_server_check_jobs(struct flist_head *job_list)
735 fio_server_check_fork_items(job_list, true);
738 static void fio_server_check_conns(struct flist_head *conn_list)
740 fio_server_check_fork_items(conn_list, false);
743 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
745 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
746 void *file_name = pdu->file;
747 struct cmd_start_pdu spdu;
749 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
751 pdu->name_len = le16_to_cpu(pdu->name_len);
752 pdu->client_type = le16_to_cpu(pdu->client_type);
754 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
755 fio_net_queue_quit();
759 spdu.jobs = cpu_to_le32(thread_number);
760 spdu.stat_outputs = cpu_to_le32(stat_number);
761 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
765 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
766 struct fio_net_cmd *cmd)
771 sk_out_assign(sk_out);
778 fio_server_add_job_pid(job_list, pid);
782 ret = fio_backend(sk_out);
788 static int handle_job_cmd(struct fio_net_cmd *cmd)
790 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
791 void *buf = pdu->buf;
792 struct cmd_start_pdu spdu;
794 pdu->buf_len = le32_to_cpu(pdu->buf_len);
795 pdu->client_type = le32_to_cpu(pdu->client_type);
797 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
798 fio_net_queue_quit();
802 spdu.jobs = cpu_to_le32(thread_number);
803 spdu.stat_outputs = cpu_to_le32(stat_number);
805 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
809 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
811 void *pdu = cmd->payload;
812 struct cmd_single_line_pdu *cslp;
813 struct cmd_line_pdu *clp;
814 unsigned long offset;
815 struct cmd_start_pdu spdu;
820 clp->lines = le16_to_cpu(clp->lines);
821 clp->client_type = le16_to_cpu(clp->client_type);
822 argv = malloc(clp->lines * sizeof(char *));
823 offset = sizeof(*clp);
825 dprint(FD_NET, "server: %d command line args\n", clp->lines);
827 for (i = 0; i < clp->lines; i++) {
829 argv[i] = (char *) cslp->text;
831 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
832 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
835 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
836 fio_net_queue_quit();
843 spdu.jobs = cpu_to_le32(thread_number);
844 spdu.stat_outputs = cpu_to_le32(stat_number);
846 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
850 static int handle_probe_cmd(struct fio_net_cmd *cmd)
852 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
853 struct cmd_probe_reply_pdu probe;
854 uint64_t tag = cmd->tag;
856 dprint(FD_NET, "server: sending probe reply\n");
858 strcpy(me, (char *) pdu->server);
860 memset(&probe, 0, sizeof(probe));
861 gethostname((char *) probe.hostname, sizeof(probe.hostname));
862 #ifdef CONFIG_BIG_ENDIAN
865 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
868 probe.arch = FIO_ARCH;
869 probe.bpp = sizeof(void *);
870 probe.cpus = __cpu_to_le32(cpus_online());
873 * If the client supports compression and we do too, then enable it
875 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
876 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
883 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
886 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
889 uint64_t tag = cmd->tag;
893 dprint(FD_NET, "server sending status\n");
896 * Fake ETA return if we don't have a local one, otherwise the client
897 * will end up timing out waiting for a response to the ETA request
899 je = get_jobs_eta(true, &size);
902 je = calloc(1, size);
904 je->nr_running = cpu_to_le32(je->nr_running);
905 je->nr_ramp = cpu_to_le32(je->nr_ramp);
906 je->nr_pending = cpu_to_le32(je->nr_pending);
907 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
908 je->files_open = cpu_to_le32(je->files_open);
910 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
911 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
912 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
913 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
914 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
915 je->rate[i] = cpu_to_le32(je->rate[i]);
916 je->iops[i] = cpu_to_le32(je->iops[i]);
919 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
920 je->eta_sec = cpu_to_le64(je->eta_sec);
921 je->nr_threads = cpu_to_le32(je->nr_threads);
922 je->is_pow2 = cpu_to_le32(je->is_pow2);
923 je->unit_base = cpu_to_le32(je->unit_base);
926 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
930 static int send_update_job_reply(uint64_t __tag, int error)
932 uint64_t tag = __tag;
935 pdu_error = __cpu_to_le32(error);
936 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
939 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
941 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
942 struct thread_data *td;
945 tnumber = le32_to_cpu(pdu->thread_number);
947 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
949 if (!tnumber || tnumber > thread_number) {
950 send_update_job_reply(cmd->tag, ENODEV);
954 td = &threads[tnumber - 1];
955 convert_thread_options_to_cpu(&td->o, &pdu->top);
956 send_update_job_reply(cmd->tag, 0);
960 static int handle_trigger_cmd(struct fio_net_cmd *cmd)
962 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
963 char *buf = (char *) pdu->cmd;
964 struct all_io_list *rep;
967 pdu->len = le16_to_cpu(pdu->len);
968 buf[pdu->len] = '\0';
970 rep = get_all_io_list(IO_LIST_ALL, &sz);
972 struct all_io_list state;
974 state.threads = cpu_to_le64((uint64_t) 0);
975 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY | SK_F_INLINE);
977 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE | SK_F_INLINE);
983 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
984 struct fio_net_cmd *cmd)
988 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
989 fio_server_op(cmd->opcode), cmd->pdu_len,
990 (unsigned long long) cmd->tag);
992 switch (cmd->opcode) {
993 case FIO_NET_CMD_QUIT:
994 fio_terminate_threads(TERMINATE_ALL);
997 case FIO_NET_CMD_EXIT:
1000 case FIO_NET_CMD_LOAD_FILE:
1001 ret = handle_load_file_cmd(cmd);
1003 case FIO_NET_CMD_JOB:
1004 ret = handle_job_cmd(cmd);
1006 case FIO_NET_CMD_JOBLINE:
1007 ret = handle_jobline_cmd(cmd);
1009 case FIO_NET_CMD_PROBE:
1010 ret = handle_probe_cmd(cmd);
1012 case FIO_NET_CMD_SEND_ETA:
1013 ret = handle_send_eta_cmd(cmd);
1015 case FIO_NET_CMD_RUN:
1016 ret = handle_run_cmd(sk_out, job_list, cmd);
1018 case FIO_NET_CMD_UPDATE_JOB:
1019 ret = handle_update_job_cmd(cmd);
1021 case FIO_NET_CMD_VTRIGGER:
1022 ret = handle_trigger_cmd(cmd);
1024 case FIO_NET_CMD_SENDFILE: {
1025 struct cmd_sendfile_reply *in;
1026 struct cmd_reply *rep;
1028 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
1030 in = (struct cmd_sendfile_reply *) cmd->payload;
1031 in->size = le32_to_cpu(in->size);
1032 in->error = le32_to_cpu(in->error);
1035 rep->error = in->error;
1038 rep->data = smalloc(in->size);
1041 rep->error = ENOMEM;
1043 rep->size = in->size;
1044 memcpy(rep->data, in->data, in->size);
1047 fio_mutex_up(&rep->lock);
1051 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
1059 * Send a command with a separate PDU, not inlined in the command
1061 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1062 off_t size, uint64_t tag, uint32_t flags)
1064 struct fio_net_cmd cmd;
1065 struct iovec iov[2];
1069 iov[0].iov_base = (void *) &cmd;
1070 iov[0].iov_len = sizeof(cmd);
1073 uint32_t this_flags = flags;
1076 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
1077 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
1079 if (this_len < size)
1080 this_flags |= FIO_NET_CMD_F_MORE;
1082 __fio_init_net_cmd(&cmd, opcode, this_len, tag);
1083 cmd.flags = __cpu_to_le32(this_flags);
1084 fio_net_cmd_crc_pdu(&cmd, buf);
1086 iov[1].iov_base = (void *) buf;
1087 iov[1].iov_len = this_len;
1089 ret = fio_sendv_data(sk, iov, 2);
1092 } while (!ret && size);
1097 static void finish_entry(struct sk_entry *entry)
1099 if (entry->flags & SK_F_FREE)
1101 else if (entry->flags & SK_F_COPY)
1107 static void entry_set_flags(struct sk_entry *entry, struct flist_head *list,
1108 unsigned int *flags)
1110 if (!flist_empty(list))
1111 *flags = FIO_NET_CMD_F_MORE;
1116 static int send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1121 entry_set_flags(first, &first->next, &flags);
1123 ret = fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf,
1124 first->size, first->tag, flags);
1126 while (!flist_empty(&first->next)) {
1127 struct sk_entry *next;
1129 next = flist_first_entry(&first->next, struct sk_entry, list);
1130 flist_del_init(&next->list);
1132 entry_set_flags(next, &first->next, &flags);
1134 ret += fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf,
1135 next->size, next->tag, flags);
1142 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1146 fio_mutex_down(&sk_out->xmit);
1148 if (entry->flags & SK_F_VEC)
1149 ret = send_vec_entry(sk_out, entry);
1150 else if (entry->flags & SK_F_SIMPLE) {
1151 ret = fio_net_send_simple_cmd(sk_out->sk, entry->opcode,
1154 ret = fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf,
1155 entry->size, &entry->tag, NULL);
1158 fio_mutex_up(&sk_out->xmit);
1161 log_err("fio: failed handling cmd %s\n", fio_server_op(entry->opcode));
1163 finish_entry(entry);
1167 static int handle_xmits(struct sk_out *sk_out)
1169 struct sk_entry *entry;
1174 if (flist_empty(&sk_out->list)) {
1179 flist_splice_init(&sk_out->list, &list);
1182 while (!flist_empty(&list)) {
1183 entry = flist_entry(list.next, struct sk_entry, list);
1184 flist_del(&entry->list);
1185 ret += handle_sk_entry(sk_out, entry);
1191 static int handle_connection(struct sk_out *sk_out)
1193 struct fio_net_cmd *cmd = NULL;
1194 FLIST_HEAD(job_list);
1200 while (!exit_backend) {
1201 struct pollfd pfd = {
1210 if (!flist_empty(&job_list))
1213 handle_xmits(sk_out);
1215 ret = poll(&pfd, 1, 0);
1219 log_err("fio: poll: %s\n", strerror(errno));
1222 fio_server_check_jobs(&job_list);
1223 fio_mutex_down_timeout(&sk_out->wait, timeout);
1227 if (pfd.revents & POLLIN)
1229 if (pfd.revents & (POLLERR|POLLHUP)) {
1233 } while (!exit_backend);
1235 fio_server_check_jobs(&job_list);
1240 cmd = fio_net_recv_cmd(sk_out->sk, true);
1246 ret = handle_command(sk_out, &job_list, cmd);
1257 handle_xmits(sk_out);
1261 __sk_out_drop(sk_out);
1265 /* get the address on this host bound by the input socket,
1266 * whether it is ipv6 or ipv4 */
1268 static int get_my_addr_str(int sk)
1270 struct sockaddr_in6 myaddr6 = { 0, };
1271 struct sockaddr_in myaddr4 = { 0, };
1272 struct sockaddr *sockaddr_p;
1278 len = sizeof(myaddr6);
1279 sockaddr_p = (struct sockaddr * )&myaddr6;
1280 net_addr = (char * )&myaddr6.sin6_addr;
1282 len = sizeof(myaddr4);
1283 sockaddr_p = (struct sockaddr * )&myaddr4;
1284 net_addr = (char * )&myaddr4.sin_addr;
1287 ret = getsockname(sk, sockaddr_p, &len);
1289 log_err("fio: getsockaddr: %s\n", strerror(errno));
1293 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1294 log_err("inet_ntop: failed to convert addr to string\n");
1298 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1302 static int accept_loop(int listen_sk)
1304 struct sockaddr_in addr;
1305 struct sockaddr_in6 addr6;
1306 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1308 int ret = 0, sk, exitval = 0;
1309 FLIST_HEAD(conn_list);
1311 dprint(FD_NET, "server enter accept loop\n");
1313 fio_set_fd_nonblocking(listen_sk, "server");
1315 while (!exit_backend) {
1316 struct sk_out *sk_out;
1322 pfd.events = POLLIN;
1326 if (!flist_empty(&conn_list))
1329 ret = poll(&pfd, 1, timeout);
1333 log_err("fio: poll: %s\n", strerror(errno));
1336 fio_server_check_conns(&conn_list);
1340 if (pfd.revents & POLLIN)
1342 } while (!exit_backend);
1344 fio_server_check_conns(&conn_list);
1346 if (exit_backend || ret < 0)
1350 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1352 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1355 log_err("fio: accept: %s\n", strerror(errno));
1360 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1362 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1364 dprint(FD_NET, "server: connect from %s\n", from);
1366 sk_out = smalloc(sizeof(*sk_out));
1368 INIT_FLIST_HEAD(&sk_out->list);
1369 __fio_mutex_init(&sk_out->lock, FIO_MUTEX_UNLOCKED);
1370 __fio_mutex_init(&sk_out->wait, FIO_MUTEX_LOCKED);
1371 __fio_mutex_init(&sk_out->xmit, FIO_MUTEX_UNLOCKED);
1376 fio_server_add_conn_pid(&conn_list, pid);
1380 /* if error, it's already logged, non-fatal */
1381 get_my_addr_str(sk);
1384 * Assign sk_out here, it'll be dropped in handle_connection()
1385 * since that function calls _exit() when done
1387 sk_out_assign(sk_out);
1388 handle_connection(sk_out);
1394 int fio_server_text_output(int level, const char *buf, size_t len)
1396 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1397 struct cmd_text_pdu *pdu;
1401 if (!sk_out || sk_out->sk == -1)
1404 tlen = sizeof(*pdu) + len;
1407 pdu->level = __cpu_to_le32(level);
1408 pdu->buf_len = __cpu_to_le32(len);
1410 gettimeofday(&tv, NULL);
1411 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1412 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1414 memcpy(pdu->buf, buf, len);
1416 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1421 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1423 dst->max_val = cpu_to_le64(src->max_val);
1424 dst->min_val = cpu_to_le64(src->min_val);
1425 dst->samples = cpu_to_le64(src->samples);
1428 * Encode to IEEE 754 for network transfer
1430 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1431 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1434 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1438 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1439 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1440 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1441 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1442 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1443 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
1444 dst->agg[i] = cpu_to_le64(src->agg[i]);
1447 dst->kb_base = cpu_to_le32(src->kb_base);
1448 dst->unit_base = cpu_to_le32(src->unit_base);
1449 dst->groupid = cpu_to_le32(src->groupid);
1450 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1454 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1455 * into a single payload.
1457 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1459 struct cmd_ts_pdu p;
1462 dprint(FD_NET, "server sending end stats\n");
1464 memset(&p, 0, sizeof(p));
1466 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1467 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1468 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1470 p.ts.error = cpu_to_le32(ts->error);
1471 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1472 p.ts.groupid = cpu_to_le32(ts->groupid);
1473 p.ts.pid = cpu_to_le32(ts->pid);
1474 p.ts.members = cpu_to_le32(ts->members);
1475 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1477 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1478 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1479 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1480 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1481 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1484 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1485 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1486 p.ts.ctx = cpu_to_le64(ts->ctx);
1487 p.ts.minf = cpu_to_le64(ts->minf);
1488 p.ts.majf = cpu_to_le64(ts->majf);
1489 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1490 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1492 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1493 fio_fp64_t *src = &ts->percentile_list[i];
1494 fio_fp64_t *dst = &p.ts.percentile_list[i];
1496 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1499 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1500 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1501 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1502 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1505 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1506 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1507 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1510 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1511 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1512 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1514 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1515 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1516 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1517 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1520 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1521 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1523 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1524 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1525 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1528 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1529 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1530 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1531 p.ts.first_error = cpu_to_le32(ts->first_error);
1532 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1533 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1535 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1536 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1537 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1538 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1540 p.ts.nr_block_infos = le64_to_cpu(ts->nr_block_infos);
1541 for (i = 0; i < p.ts.nr_block_infos; i++)
1542 p.ts.block_infos[i] = le32_to_cpu(ts->block_infos[i]);
1544 convert_gs(&p.rs, rs);
1546 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1549 void fio_server_send_gs(struct group_run_stats *rs)
1551 struct group_run_stats gs;
1553 dprint(FD_NET, "server sending group run stats\n");
1555 convert_gs(&gs, rs);
1556 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1559 void fio_server_send_job_options(struct flist_head *opt_list,
1560 unsigned int groupid)
1562 struct cmd_job_option pdu;
1563 struct flist_head *entry;
1565 if (flist_empty(opt_list))
1568 flist_for_each(entry, opt_list) {
1569 struct print_option *p;
1572 p = flist_entry(entry, struct print_option, list);
1573 memset(&pdu, 0, sizeof(pdu));
1575 if (groupid == -1U) {
1576 pdu.global = __cpu_to_le16(1);
1580 pdu.groupid = cpu_to_le32(groupid);
1582 len = strlen(p->name);
1583 if (len >= sizeof(pdu.name)) {
1584 len = sizeof(pdu.name) - 1;
1585 pdu.truncated = __cpu_to_le16(1);
1587 memcpy(pdu.name, p->name, len);
1589 len = strlen(p->value);
1590 if (len >= sizeof(pdu.value)) {
1591 len = sizeof(pdu.value) - 1;
1592 pdu.truncated = __cpu_to_le16(1);
1594 memcpy(pdu.value, p->value, len);
1596 fio_net_queue_cmd(FIO_NET_CMD_JOB_OPT, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1600 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1604 for (i = 0; i < 2; i++) {
1605 dst->ios[i] = cpu_to_le64(src->ios[i]);
1606 dst->merges[i] = cpu_to_le64(src->merges[i]);
1607 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1608 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1611 dst->io_ticks = cpu_to_le64(src->io_ticks);
1612 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1613 dst->slavecount = cpu_to_le32(src->slavecount);
1614 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1617 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1621 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1622 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1624 for (i = 0; i < 2; i++) {
1625 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1626 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1627 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1628 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1631 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1632 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1633 dst->s.msec = cpu_to_le64(src->s.msec);
1636 void fio_server_send_du(void)
1638 struct disk_util *du;
1639 struct flist_head *entry;
1640 struct cmd_du_pdu pdu;
1642 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1644 memset(&pdu, 0, sizeof(pdu));
1646 flist_for_each(entry, &disk_list) {
1647 du = flist_entry(entry, struct disk_util, list);
1649 convert_dus(&pdu.dus, &du->dus);
1650 convert_agg(&pdu.agg, &du->agg);
1652 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1657 static int __fio_append_iolog_gz(struct sk_entry *first, struct io_log *log,
1658 struct io_logs *cur_log, z_stream *stream)
1660 unsigned int this_len;
1664 stream->next_in = (void *) cur_log->log;
1665 stream->avail_in = cur_log->nr_samples * log_entry_sz(log);
1668 struct sk_entry *entry;
1671 * Dirty - since the log is potentially huge, compress it into
1672 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1673 * side defragment it.
1675 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1677 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1678 stream->next_out = out_pdu;
1679 ret = deflate(stream, Z_BLOCK);
1680 /* may be Z_OK, or Z_STREAM_END */
1686 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1688 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1689 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1690 flist_add_tail(&entry->list, &first->next);
1691 } while (stream->avail_in);
1696 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1701 memset(&stream, 0, sizeof(stream));
1702 stream.zalloc = Z_NULL;
1703 stream.zfree = Z_NULL;
1704 stream.opaque = Z_NULL;
1706 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK)
1709 while (!flist_empty(&log->io_logs)) {
1710 struct io_logs *cur_log;
1712 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1713 flist_del_init(&cur_log->list);
1715 ret = __fio_append_iolog_gz(first, log, cur_log, &stream);
1720 ret = deflate(&stream, Z_FINISH);
1722 while (ret != Z_STREAM_END) {
1723 struct sk_entry *entry;
1724 unsigned int this_len;
1727 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1728 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1729 stream.next_out = out_pdu;
1731 ret = deflate(&stream, Z_FINISH);
1732 /* may be Z_OK, or Z_STREAM_END */
1738 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1740 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1741 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1742 flist_add_tail(&entry->list, &first->next);
1743 } while (ret != Z_STREAM_END);
1745 ret = deflateEnd(&stream);
1752 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1758 static int fio_append_gz_chunks(struct sk_entry *first, struct io_log *log)
1760 struct sk_entry *entry;
1761 struct flist_head *node;
1763 pthread_mutex_lock(&log->chunk_lock);
1764 flist_for_each(node, &log->chunk_list) {
1765 struct iolog_compress *c;
1767 c = flist_entry(node, struct iolog_compress, list);
1768 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, c->buf, c->len,
1769 NULL, SK_F_VEC | SK_F_INLINE);
1770 flist_add_tail(&entry->list, &first->next);
1772 pthread_mutex_unlock(&log->chunk_lock);
1777 static int fio_append_text_log(struct sk_entry *first, struct io_log *log)
1779 struct sk_entry *entry;
1781 while (!flist_empty(&log->io_logs)) {
1782 struct io_logs *cur_log;
1785 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1786 flist_del_init(&cur_log->list);
1788 size = cur_log->nr_samples * log_entry_sz(log);
1790 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, cur_log->log, size,
1791 NULL, SK_F_VEC | SK_F_INLINE);
1792 flist_add_tail(&entry->list, &first->next);
1798 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1800 struct cmd_iolog_pdu pdu;
1801 struct sk_entry *first;
1802 struct flist_head *entry;
1805 pdu.nr_samples = cpu_to_le64(iolog_nr_samples(log));
1806 pdu.thread_number = cpu_to_le32(td->thread_number);
1807 pdu.log_type = cpu_to_le32(log->log_type);
1809 if (!flist_empty(&log->chunk_list))
1810 pdu.compressed = __cpu_to_le32(STORE_COMPRESSED);
1812 pdu.compressed = __cpu_to_le32(XMIT_COMPRESSED);
1816 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1817 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1820 * We can't do this for a pre-compressed log, but for that case,
1821 * log->nr_samples is zero anyway.
1823 flist_for_each(entry, &log->io_logs) {
1824 struct io_logs *cur_log;
1827 cur_log = flist_entry(entry, struct io_logs, list);
1829 for (i = 0; i < cur_log->nr_samples; i++) {
1830 struct io_sample *s = get_sample(log, cur_log, i);
1832 s->time = cpu_to_le64(s->time);
1833 s->val = cpu_to_le64(s->val);
1834 s->__ddir = cpu_to_le32(s->__ddir);
1835 s->bs = cpu_to_le32(s->bs);
1837 if (log->log_offset) {
1838 struct io_sample_offset *so = (void *) s;
1840 so->offset = cpu_to_le64(so->offset);
1846 * Assemble header entry first
1848 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_VEC | SK_F_INLINE | SK_F_COPY);
1851 * Now append actual log entries. If log compression was enabled on
1852 * the job, just send out the compressed chunks directly. If we
1853 * have a plain log, compress if we can, then send. Otherwise, send
1854 * the plain text output.
1856 if (!flist_empty(&log->chunk_list))
1857 ret = fio_append_gz_chunks(first, log);
1859 ret = fio_append_iolog_gz(first, log);
1861 ret = fio_append_text_log(first, log);
1863 fio_net_queue_entry(first);
1867 void fio_server_send_add_job(struct thread_data *td)
1869 struct cmd_add_job_pdu pdu;
1871 memset(&pdu, 0, sizeof(pdu));
1872 pdu.thread_number = cpu_to_le32(td->thread_number);
1873 pdu.groupid = cpu_to_le32(td->groupid);
1874 convert_thread_options_to_net(&pdu.top, &td->o);
1876 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL,
1880 void fio_server_send_start(struct thread_data *td)
1882 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1884 assert(sk_out->sk != -1);
1886 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, 0, SK_F_SIMPLE);
1889 int fio_server_get_verify_state(const char *name, int threadnumber,
1892 struct thread_io_list *s;
1893 struct cmd_sendfile out;
1894 struct cmd_reply *rep;
1899 dprint(FD_NET, "server: request verify state\n");
1901 rep = smalloc(sizeof(*rep));
1903 log_err("fio: smalloc pool too small\n");
1907 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1911 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1913 tag = (uint64_t) (uintptr_t) rep;
1914 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag,
1918 * Wait for the backend to receive the reply
1920 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
1921 log_err("fio: timed out waiting for reply\n");
1927 log_err("fio: failure on receiving state file %s: %s\n",
1928 out.path, strerror(rep->error));
1933 fio_net_queue_quit();
1938 * The format is verify_state_hdr, then thread_io_list. Verify
1939 * the header, and the thread_io_list checksum
1941 s = rep->data + sizeof(struct verify_state_hdr);
1942 if (verify_state_hdr(rep->data, s)) {
1948 * Don't need the header from now, copy just the thread_io_list
1951 rep->size -= sizeof(struct verify_state_hdr);
1952 data = malloc(rep->size);
1953 memcpy(data, s, rep->size);
1957 __fio_mutex_remove(&rep->lock);
1962 static int fio_init_server_ip(void)
1964 struct sockaddr *addr;
1971 sk = socket(AF_INET6, SOCK_STREAM, 0);
1973 sk = socket(AF_INET, SOCK_STREAM, 0);
1976 log_err("fio: socket: %s\n", strerror(errno));
1981 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1982 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
1988 * Not fatal if fails, so just ignore it if that happens
1990 setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
1994 const void *src = &saddr_in6.sin6_addr;
1996 addr = (struct sockaddr *) &saddr_in6;
1997 socklen = sizeof(saddr_in6);
1998 saddr_in6.sin6_family = AF_INET6;
1999 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
2001 const void *src = &saddr_in.sin_addr;
2003 addr = (struct sockaddr *) &saddr_in;
2004 socklen = sizeof(saddr_in);
2005 saddr_in.sin_family = AF_INET;
2006 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
2009 if (bind(sk, addr, socklen) < 0) {
2010 log_err("fio: bind: %s\n", strerror(errno));
2011 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
2019 static int fio_init_server_sock(void)
2021 struct sockaddr_un addr;
2026 sk = socket(AF_UNIX, SOCK_STREAM, 0);
2028 log_err("fio: socket: %s\n", strerror(errno));
2034 memset(&addr, 0, sizeof(addr));
2035 addr.sun_family = AF_UNIX;
2036 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
2038 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
2040 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
2041 log_err("fio: bind: %s\n", strerror(errno));
2050 static int fio_init_server_connection(void)
2055 dprint(FD_NET, "starting server\n");
2058 sk = fio_init_server_ip();
2060 sk = fio_init_server_sock();
2065 memset(bind_str, 0, sizeof(bind_str));
2074 src = &saddr_in6.sin6_addr;
2077 src = &saddr_in.sin_addr;
2080 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
2082 sprintf(port, ",%u", fio_net_port);
2086 strncpy(bind_str, port, sizeof(bind_str) - 1);
2088 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
2090 log_info("fio: server listening on %s\n", bind_str);
2092 if (listen(sk, 4) < 0) {
2093 log_err("fio: listen: %s\n", strerror(errno));
2101 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
2102 struct in6_addr *inp6)
2108 ret = inet_pton(AF_INET6, host, inp6);
2110 ret = inet_pton(AF_INET, host, inp);
2113 struct addrinfo hints, *res;
2115 memset(&hints, 0, sizeof(hints));
2116 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
2117 hints.ai_socktype = SOCK_STREAM;
2119 ret = getaddrinfo(host, NULL, &hints, &res);
2121 log_err("fio: failed to resolve <%s> (%s)\n", host,
2127 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
2129 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
2139 * Parse a host/ip/port string. Reads from 'str'.
2144 * *ptr is the host, *port is the port, inp is the destination.
2146 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
2147 * For local domain sockets:
2148 * *ptr is the filename, *is_sock is 1.
2150 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
2151 int *port, struct in_addr *inp,
2152 struct in6_addr *inp6, int *ipv6)
2154 const char *host = str;
2160 *port = fio_net_port;
2163 if (!strncmp(str, "sock:", 5)) {
2164 *ptr = strdup(str + 5);
2171 * Is it ip:<ip or host>:port
2173 if (!strncmp(host, "ip:", 3))
2175 else if (!strncmp(host, "ip4:", 4))
2177 else if (!strncmp(host, "ip6:", 4)) {
2180 } else if (host[0] == ':') {
2181 /* String is :port */
2184 if (!lport || lport > 65535) {
2185 log_err("fio: bad server port %u\n", lport);
2188 /* no hostname given, we are done */
2194 * If no port seen yet, check if there's a last ',' at the end
2197 portp = strchr(host, ',');
2201 lport = atoi(portp);
2202 if (!lport || lport > 65535) {
2203 log_err("fio: bad server port %u\n", lport);
2215 *ptr = strdup(host);
2217 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2224 *port = fio_net_port;
2230 * Server arg should be one of:
2232 * sock:/path/to/socket
2236 * Where sock uses unix domain sockets, and ip binds the server to
2237 * a specific interface. If no arguments are given to the server, it
2238 * uses IP and binds to 0.0.0.0.
2241 static int fio_handle_server_arg(void)
2243 int port = fio_net_port;
2244 int is_sock, ret = 0;
2246 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2248 if (!fio_server_arg)
2251 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2252 &port, &saddr_in.sin_addr,
2253 &saddr_in6.sin6_addr, &use_ipv6);
2255 if (!is_sock && bind_sock) {
2261 fio_net_port = port;
2262 saddr_in.sin_port = htons(port);
2263 saddr_in6.sin6_port = htons(port);
2267 static void sig_int(int sig)
2273 static void set_sig_handlers(void)
2275 struct sigaction act;
2277 memset(&act, 0, sizeof(act));
2278 act.sa_handler = sig_int;
2279 act.sa_flags = SA_RESTART;
2280 sigaction(SIGINT, &act, NULL);
2283 void fio_server_destroy_sk_key(void)
2285 pthread_key_delete(sk_out_key);
2288 int fio_server_create_sk_key(void)
2290 if (pthread_key_create(&sk_out_key, NULL)) {
2291 log_err("fio: can't create sk_out backend key\n");
2295 pthread_setspecific(sk_out_key, NULL);
2299 static int fio_server(void)
2303 dprint(FD_NET, "starting server\n");
2305 if (fio_handle_server_arg())
2308 sk = fio_init_server_connection();
2314 ret = accept_loop(sk);
2318 if (fio_server_arg) {
2319 free(fio_server_arg);
2320 fio_server_arg = NULL;
2328 void fio_server_got_signal(int signal)
2330 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2334 if (signal == SIGPIPE)
2337 log_info("\nfio: terminating on signal %d\n", signal);
2342 static int check_existing_pidfile(const char *pidfile)
2349 if (stat(pidfile, &sb))
2352 f = fopen(pidfile, "r");
2356 if (fread(buf, sb.st_size, 1, f) <= 0) {
2363 if (kill(pid, SIGCONT) < 0)
2364 return errno != ESRCH;
2369 static int write_pid(pid_t pid, const char *pidfile)
2373 fpid = fopen(pidfile, "w");
2375 log_err("fio: failed opening pid file %s\n", pidfile);
2379 fprintf(fpid, "%u\n", (unsigned int) pid);
2385 * If pidfile is specified, background us.
2387 int fio_start_server(char *pidfile)
2394 WSAStartup(MAKEWORD(2, 2), &wsd);
2398 return fio_server();
2400 if (check_existing_pidfile(pidfile)) {
2401 log_err("fio: pidfile %s exists and server appears alive\n",
2409 log_err("fio: failed server fork: %s", strerror(errno));
2413 ret = write_pid(pid, pidfile);
2419 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2421 close(STDIN_FILENO);
2422 close(STDOUT_FILENO);
2423 close(STDERR_FILENO);
2435 void fio_server_set_arg(const char *arg)
2437 fio_server_arg = strdup(arg);