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 uint64_t *ss_iops, *ss_bw;
1464 dprint(FD_NET, "server sending end stats\n");
1466 memset(&p, 0, sizeof(p));
1468 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1469 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1470 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1472 p.ts.error = cpu_to_le32(ts->error);
1473 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1474 p.ts.groupid = cpu_to_le32(ts->groupid);
1475 p.ts.pid = cpu_to_le32(ts->pid);
1476 p.ts.members = cpu_to_le32(ts->members);
1477 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1479 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1480 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1481 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1482 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1483 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1486 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1487 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1488 p.ts.ctx = cpu_to_le64(ts->ctx);
1489 p.ts.minf = cpu_to_le64(ts->minf);
1490 p.ts.majf = cpu_to_le64(ts->majf);
1491 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1492 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1494 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1495 fio_fp64_t *src = &ts->percentile_list[i];
1496 fio_fp64_t *dst = &p.ts.percentile_list[i];
1498 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1501 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1502 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1503 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1504 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1507 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1508 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1509 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1512 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1513 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1514 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1516 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1517 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1518 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1519 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1522 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1523 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1525 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1526 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1527 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1530 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1531 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1532 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1533 p.ts.first_error = cpu_to_le32(ts->first_error);
1534 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1535 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1537 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1538 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1539 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1540 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1542 p.ts.nr_block_infos = le64_to_cpu(ts->nr_block_infos);
1543 for (i = 0; i < p.ts.nr_block_infos; i++)
1544 p.ts.block_infos[i] = le32_to_cpu(ts->block_infos[i]);
1546 p.ts.ss_dur = cpu_to_le64(ts->ss_dur);
1547 p.ts.ss_state = cpu_to_le32(ts->ss_state);
1548 p.ts.ss_head = cpu_to_le32(ts->ss_head);
1549 p.ts.ss_limit.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_limit.u.f));
1550 p.ts.ss_slope.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_slope.u.f));
1551 p.ts.ss_deviation.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_deviation.u.f));
1552 p.ts.ss_criterion.u.i = cpu_to_le64(fio_double_to_uint64(ts->ss_criterion.u.f));
1554 convert_gs(&p.rs, rs);
1556 dprint(FD_NET, "ts->ss_state = %d\n", ts->ss_state);
1557 if (ts->ss_state & __FIO_SS_DATA) {
1558 dprint(FD_NET, "server sending steadystate ring buffers\n");
1560 ss_buf = malloc(sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t));
1562 memcpy(ss_buf, &p, sizeof(p));
1564 ss_iops = (uint64_t *) ((struct cmd_ts_pdu *)ss_buf + 1);
1565 ss_bw = ss_iops + (int) ts->ss_dur;
1566 for (i = 0; i < ts->ss_dur; i++) {
1567 ss_iops[i] = cpu_to_le64(ts->ss_iops_data[i]);
1568 ss_bw[i] = cpu_to_le64(ts->ss_bw_data[i]);
1571 fio_net_queue_cmd(FIO_NET_CMD_TS, ss_buf, sizeof(p) + 2*ts->ss_dur*sizeof(uint64_t), NULL, SK_F_COPY);
1576 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1579 void fio_server_send_gs(struct group_run_stats *rs)
1581 struct group_run_stats gs;
1583 dprint(FD_NET, "server sending group run stats\n");
1585 convert_gs(&gs, rs);
1586 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1589 void fio_server_send_job_options(struct flist_head *opt_list,
1590 unsigned int groupid)
1592 struct cmd_job_option pdu;
1593 struct flist_head *entry;
1595 if (flist_empty(opt_list))
1598 flist_for_each(entry, opt_list) {
1599 struct print_option *p;
1602 p = flist_entry(entry, struct print_option, list);
1603 memset(&pdu, 0, sizeof(pdu));
1605 if (groupid == -1U) {
1606 pdu.global = __cpu_to_le16(1);
1610 pdu.groupid = cpu_to_le32(groupid);
1612 len = strlen(p->name);
1613 if (len >= sizeof(pdu.name)) {
1614 len = sizeof(pdu.name) - 1;
1615 pdu.truncated = __cpu_to_le16(1);
1617 memcpy(pdu.name, p->name, len);
1619 len = strlen(p->value);
1620 if (len >= sizeof(pdu.value)) {
1621 len = sizeof(pdu.value) - 1;
1622 pdu.truncated = __cpu_to_le16(1);
1624 memcpy(pdu.value, p->value, len);
1626 fio_net_queue_cmd(FIO_NET_CMD_JOB_OPT, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1630 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1634 for (i = 0; i < 2; i++) {
1635 dst->ios[i] = cpu_to_le64(src->ios[i]);
1636 dst->merges[i] = cpu_to_le64(src->merges[i]);
1637 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1638 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1641 dst->io_ticks = cpu_to_le64(src->io_ticks);
1642 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1643 dst->slavecount = cpu_to_le32(src->slavecount);
1644 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1647 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1651 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1652 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1654 for (i = 0; i < 2; i++) {
1655 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1656 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1657 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1658 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1661 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1662 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1663 dst->s.msec = cpu_to_le64(src->s.msec);
1666 void fio_server_send_du(void)
1668 struct disk_util *du;
1669 struct flist_head *entry;
1670 struct cmd_du_pdu pdu;
1672 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1674 memset(&pdu, 0, sizeof(pdu));
1676 flist_for_each(entry, &disk_list) {
1677 du = flist_entry(entry, struct disk_util, list);
1679 convert_dus(&pdu.dus, &du->dus);
1680 convert_agg(&pdu.agg, &du->agg);
1682 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1687 static int __fio_append_iolog_gz(struct sk_entry *first, struct io_log *log,
1688 struct io_logs *cur_log, z_stream *stream)
1690 unsigned int this_len;
1694 stream->next_in = (void *) cur_log->log;
1695 stream->avail_in = cur_log->nr_samples * log_entry_sz(log);
1698 struct sk_entry *entry;
1701 * Dirty - since the log is potentially huge, compress it into
1702 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1703 * side defragment it.
1705 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1707 stream->avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1708 stream->next_out = out_pdu;
1709 ret = deflate(stream, Z_BLOCK);
1710 /* may be Z_OK, or Z_STREAM_END */
1716 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream->avail_out;
1718 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1719 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1720 flist_add_tail(&entry->list, &first->next);
1721 } while (stream->avail_in);
1726 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1731 memset(&stream, 0, sizeof(stream));
1732 stream.zalloc = Z_NULL;
1733 stream.zfree = Z_NULL;
1734 stream.opaque = Z_NULL;
1736 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK)
1739 while (!flist_empty(&log->io_logs)) {
1740 struct io_logs *cur_log;
1742 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1743 flist_del_init(&cur_log->list);
1745 ret = __fio_append_iolog_gz(first, log, cur_log, &stream);
1750 ret = deflate(&stream, Z_FINISH);
1752 while (ret != Z_STREAM_END) {
1753 struct sk_entry *entry;
1754 unsigned int this_len;
1757 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1758 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1759 stream.next_out = out_pdu;
1761 ret = deflate(&stream, Z_FINISH);
1762 /* may be Z_OK, or Z_STREAM_END */
1768 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1770 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1771 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1772 flist_add_tail(&entry->list, &first->next);
1773 } while (ret != Z_STREAM_END);
1775 ret = deflateEnd(&stream);
1782 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1788 static int fio_append_gz_chunks(struct sk_entry *first, struct io_log *log)
1790 struct sk_entry *entry;
1791 struct flist_head *node;
1793 pthread_mutex_lock(&log->chunk_lock);
1794 flist_for_each(node, &log->chunk_list) {
1795 struct iolog_compress *c;
1797 c = flist_entry(node, struct iolog_compress, list);
1798 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, c->buf, c->len,
1799 NULL, SK_F_VEC | SK_F_INLINE);
1800 flist_add_tail(&entry->list, &first->next);
1802 pthread_mutex_unlock(&log->chunk_lock);
1807 static int fio_append_text_log(struct sk_entry *first, struct io_log *log)
1809 struct sk_entry *entry;
1811 while (!flist_empty(&log->io_logs)) {
1812 struct io_logs *cur_log;
1815 cur_log = flist_first_entry(&log->io_logs, struct io_logs, list);
1816 flist_del_init(&cur_log->list);
1818 size = cur_log->nr_samples * log_entry_sz(log);
1820 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, cur_log->log, size,
1821 NULL, SK_F_VEC | SK_F_INLINE);
1822 flist_add_tail(&entry->list, &first->next);
1828 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1830 struct cmd_iolog_pdu pdu;
1831 struct sk_entry *first;
1832 struct flist_head *entry;
1835 pdu.nr_samples = cpu_to_le64(iolog_nr_samples(log));
1836 pdu.thread_number = cpu_to_le32(td->thread_number);
1837 pdu.log_type = cpu_to_le32(log->log_type);
1839 if (!flist_empty(&log->chunk_list))
1840 pdu.compressed = __cpu_to_le32(STORE_COMPRESSED);
1842 pdu.compressed = __cpu_to_le32(XMIT_COMPRESSED);
1846 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1847 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1850 * We can't do this for a pre-compressed log, but for that case,
1851 * log->nr_samples is zero anyway.
1853 flist_for_each(entry, &log->io_logs) {
1854 struct io_logs *cur_log;
1857 cur_log = flist_entry(entry, struct io_logs, list);
1859 for (i = 0; i < cur_log->nr_samples; i++) {
1860 struct io_sample *s = get_sample(log, cur_log, i);
1862 s->time = cpu_to_le64(s->time);
1863 s->val = cpu_to_le64(s->val);
1864 s->__ddir = cpu_to_le32(s->__ddir);
1865 s->bs = cpu_to_le32(s->bs);
1867 if (log->log_offset) {
1868 struct io_sample_offset *so = (void *) s;
1870 so->offset = cpu_to_le64(so->offset);
1876 * Assemble header entry first
1878 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_VEC | SK_F_INLINE | SK_F_COPY);
1881 * Now append actual log entries. If log compression was enabled on
1882 * the job, just send out the compressed chunks directly. If we
1883 * have a plain log, compress if we can, then send. Otherwise, send
1884 * the plain text output.
1886 if (!flist_empty(&log->chunk_list))
1887 ret = fio_append_gz_chunks(first, log);
1889 ret = fio_append_iolog_gz(first, log);
1891 ret = fio_append_text_log(first, log);
1893 fio_net_queue_entry(first);
1897 void fio_server_send_add_job(struct thread_data *td)
1899 struct cmd_add_job_pdu pdu;
1901 memset(&pdu, 0, sizeof(pdu));
1902 pdu.thread_number = cpu_to_le32(td->thread_number);
1903 pdu.groupid = cpu_to_le32(td->groupid);
1904 convert_thread_options_to_net(&pdu.top, &td->o);
1906 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL,
1910 void fio_server_send_start(struct thread_data *td)
1912 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1914 assert(sk_out->sk != -1);
1916 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, 0, SK_F_SIMPLE);
1919 int fio_server_get_verify_state(const char *name, int threadnumber,
1922 struct thread_io_list *s;
1923 struct cmd_sendfile out;
1924 struct cmd_reply *rep;
1929 dprint(FD_NET, "server: request verify state\n");
1931 rep = smalloc(sizeof(*rep));
1933 log_err("fio: smalloc pool too small\n");
1937 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1941 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1943 tag = (uint64_t) (uintptr_t) rep;
1944 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag,
1948 * Wait for the backend to receive the reply
1950 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
1951 log_err("fio: timed out waiting for reply\n");
1957 log_err("fio: failure on receiving state file %s: %s\n",
1958 out.path, strerror(rep->error));
1963 fio_net_queue_quit();
1968 * The format is verify_state_hdr, then thread_io_list. Verify
1969 * the header, and the thread_io_list checksum
1971 s = rep->data + sizeof(struct verify_state_hdr);
1972 if (verify_state_hdr(rep->data, s)) {
1978 * Don't need the header from now, copy just the thread_io_list
1981 rep->size -= sizeof(struct verify_state_hdr);
1982 data = malloc(rep->size);
1983 memcpy(data, s, rep->size);
1987 __fio_mutex_remove(&rep->lock);
1992 static int fio_init_server_ip(void)
1994 struct sockaddr *addr;
2001 sk = socket(AF_INET6, SOCK_STREAM, 0);
2003 sk = socket(AF_INET, SOCK_STREAM, 0);
2006 log_err("fio: socket: %s\n", strerror(errno));
2011 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
2012 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
2018 * Not fatal if fails, so just ignore it if that happens
2020 setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
2024 const void *src = &saddr_in6.sin6_addr;
2026 addr = (struct sockaddr *) &saddr_in6;
2027 socklen = sizeof(saddr_in6);
2028 saddr_in6.sin6_family = AF_INET6;
2029 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
2031 const void *src = &saddr_in.sin_addr;
2033 addr = (struct sockaddr *) &saddr_in;
2034 socklen = sizeof(saddr_in);
2035 saddr_in.sin_family = AF_INET;
2036 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
2039 if (bind(sk, addr, socklen) < 0) {
2040 log_err("fio: bind: %s\n", strerror(errno));
2041 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
2049 static int fio_init_server_sock(void)
2051 struct sockaddr_un addr;
2056 sk = socket(AF_UNIX, SOCK_STREAM, 0);
2058 log_err("fio: socket: %s\n", strerror(errno));
2064 memset(&addr, 0, sizeof(addr));
2065 addr.sun_family = AF_UNIX;
2066 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
2068 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
2070 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
2071 log_err("fio: bind: %s\n", strerror(errno));
2080 static int fio_init_server_connection(void)
2085 dprint(FD_NET, "starting server\n");
2088 sk = fio_init_server_ip();
2090 sk = fio_init_server_sock();
2095 memset(bind_str, 0, sizeof(bind_str));
2104 src = &saddr_in6.sin6_addr;
2107 src = &saddr_in.sin_addr;
2110 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
2112 sprintf(port, ",%u", fio_net_port);
2116 strncpy(bind_str, port, sizeof(bind_str) - 1);
2118 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
2120 log_info("fio: server listening on %s\n", bind_str);
2122 if (listen(sk, 4) < 0) {
2123 log_err("fio: listen: %s\n", strerror(errno));
2131 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
2132 struct in6_addr *inp6)
2138 ret = inet_pton(AF_INET6, host, inp6);
2140 ret = inet_pton(AF_INET, host, inp);
2143 struct addrinfo hints, *res;
2145 memset(&hints, 0, sizeof(hints));
2146 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
2147 hints.ai_socktype = SOCK_STREAM;
2149 ret = getaddrinfo(host, NULL, &hints, &res);
2151 log_err("fio: failed to resolve <%s> (%s)\n", host,
2157 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
2159 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
2169 * Parse a host/ip/port string. Reads from 'str'.
2174 * *ptr is the host, *port is the port, inp is the destination.
2176 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
2177 * For local domain sockets:
2178 * *ptr is the filename, *is_sock is 1.
2180 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
2181 int *port, struct in_addr *inp,
2182 struct in6_addr *inp6, int *ipv6)
2184 const char *host = str;
2190 *port = fio_net_port;
2193 if (!strncmp(str, "sock:", 5)) {
2194 *ptr = strdup(str + 5);
2201 * Is it ip:<ip or host>:port
2203 if (!strncmp(host, "ip:", 3))
2205 else if (!strncmp(host, "ip4:", 4))
2207 else if (!strncmp(host, "ip6:", 4)) {
2210 } else if (host[0] == ':') {
2211 /* String is :port */
2214 if (!lport || lport > 65535) {
2215 log_err("fio: bad server port %u\n", lport);
2218 /* no hostname given, we are done */
2224 * If no port seen yet, check if there's a last ',' at the end
2227 portp = strchr(host, ',');
2231 lport = atoi(portp);
2232 if (!lport || lport > 65535) {
2233 log_err("fio: bad server port %u\n", lport);
2245 *ptr = strdup(host);
2247 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2254 *port = fio_net_port;
2260 * Server arg should be one of:
2262 * sock:/path/to/socket
2266 * Where sock uses unix domain sockets, and ip binds the server to
2267 * a specific interface. If no arguments are given to the server, it
2268 * uses IP and binds to 0.0.0.0.
2271 static int fio_handle_server_arg(void)
2273 int port = fio_net_port;
2274 int is_sock, ret = 0;
2276 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2278 if (!fio_server_arg)
2281 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2282 &port, &saddr_in.sin_addr,
2283 &saddr_in6.sin6_addr, &use_ipv6);
2285 if (!is_sock && bind_sock) {
2291 fio_net_port = port;
2292 saddr_in.sin_port = htons(port);
2293 saddr_in6.sin6_port = htons(port);
2297 static void sig_int(int sig)
2303 static void set_sig_handlers(void)
2305 struct sigaction act;
2307 memset(&act, 0, sizeof(act));
2308 act.sa_handler = sig_int;
2309 act.sa_flags = SA_RESTART;
2310 sigaction(SIGINT, &act, NULL);
2313 void fio_server_destroy_sk_key(void)
2315 pthread_key_delete(sk_out_key);
2318 int fio_server_create_sk_key(void)
2320 if (pthread_key_create(&sk_out_key, NULL)) {
2321 log_err("fio: can't create sk_out backend key\n");
2325 pthread_setspecific(sk_out_key, NULL);
2329 static int fio_server(void)
2333 dprint(FD_NET, "starting server\n");
2335 if (fio_handle_server_arg())
2338 sk = fio_init_server_connection();
2344 ret = accept_loop(sk);
2348 if (fio_server_arg) {
2349 free(fio_server_arg);
2350 fio_server_arg = NULL;
2358 void fio_server_got_signal(int signal)
2360 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2364 if (signal == SIGPIPE)
2367 log_info("\nfio: terminating on signal %d\n", signal);
2372 static int check_existing_pidfile(const char *pidfile)
2379 if (stat(pidfile, &sb))
2382 f = fopen(pidfile, "r");
2386 if (fread(buf, sb.st_size, 1, f) <= 0) {
2393 if (kill(pid, SIGCONT) < 0)
2394 return errno != ESRCH;
2399 static int write_pid(pid_t pid, const char *pidfile)
2403 fpid = fopen(pidfile, "w");
2405 log_err("fio: failed opening pid file %s\n", pidfile);
2409 fprintf(fpid, "%u\n", (unsigned int) pid);
2415 * If pidfile is specified, background us.
2417 int fio_start_server(char *pidfile)
2424 WSAStartup(MAKEWORD(2, 2), &wsd);
2428 return fio_server();
2430 if (check_existing_pidfile(pidfile)) {
2431 log_err("fio: pidfile %s exists and server appears alive\n",
2439 log_err("fio: failed server fork: %s", strerror(errno));
2443 ret = write_pid(pid, pidfile);
2449 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2451 close(STDIN_FILENO);
2452 close(STDOUT_FILENO);
2453 close(STDERR_FILENO);
2465 void fio_server_set_arg(const char *arg)
2467 fio_server_arg = strdup(arg);