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;
43 struct flist_head list; /* link on sk_out->list */
44 int flags; /* SK_F_* */
45 int opcode; /* Actual command fields */
49 struct flist_head next; /* Other sk_entry's, if linked command */
53 unsigned int refs; /* frees sk_out when it drops to zero.
54 * protected by below ->lock */
56 int sk; /* socket fd to talk to client */
57 struct fio_mutex *lock; /* protects ref and below list */
58 struct flist_head list; /* list of pending transmit work */
59 struct fio_mutex *wait; /* wake backend when items added to list */
62 static char *fio_server_arg;
63 static char *bind_sock;
64 static struct sockaddr_in saddr_in;
65 static struct sockaddr_in6 saddr_in6;
68 static unsigned int has_zlib = 1;
70 static unsigned int has_zlib = 0;
72 static unsigned int use_zlib;
75 static pthread_key_t sk_out_key;
77 struct fio_fork_item {
78 struct flist_head list;
86 struct fio_mutex lock;
92 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
117 static void sk_lock(struct sk_out *sk_out)
119 fio_mutex_down(sk_out->lock);
122 static void sk_unlock(struct sk_out *sk_out)
124 fio_mutex_up(sk_out->lock);
127 void sk_out_assign(struct sk_out *sk_out)
135 pthread_setspecific(sk_out_key, sk_out);
138 static void sk_out_free(struct sk_out *sk_out)
140 fio_mutex_remove(sk_out->lock);
141 fio_mutex_remove(sk_out->wait);
145 static int __sk_out_drop(struct sk_out *sk_out)
151 refs = --sk_out->refs;
163 void sk_out_drop(void)
165 struct sk_out *sk_out;
167 sk_out = pthread_getspecific(sk_out_key);
168 if (!__sk_out_drop(sk_out))
169 pthread_setspecific(sk_out_key, NULL);
172 static void __fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
173 uint32_t pdu_len, uint64_t tag)
175 memset(cmd, 0, sizeof(*cmd));
177 cmd->version = __cpu_to_le16(FIO_SERVER_VER);
178 cmd->opcode = cpu_to_le16(opcode);
179 cmd->tag = cpu_to_le64(tag);
180 cmd->pdu_len = cpu_to_le32(pdu_len);
184 static void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
185 const void *pdu, uint32_t pdu_len, uint64_t tag)
187 __fio_init_net_cmd(cmd, opcode, pdu_len, tag);
190 memcpy(&cmd->payload, pdu, pdu_len);
193 const char *fio_server_op(unsigned int op)
197 if (op < FIO_NET_CMD_NR)
198 return fio_server_ops[op];
200 sprintf(buf, "UNKNOWN/%d", op);
204 static ssize_t iov_total_len(const struct iovec *iov, int count)
216 static int fio_sendv_data(int sk, struct iovec *iov, int count)
218 ssize_t total_len = iov_total_len(iov, count);
222 ret = writev(sk, iov, count);
229 if (ret >= iov->iov_len) {
234 iov->iov_base += ret;
240 else if (errno == EAGAIN || errno == EINTR)
244 } while (!exit_backend);
252 static int fio_send_data(int sk, const void *p, unsigned int len)
254 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
256 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
258 return fio_sendv_data(sk, &iov, 1);
261 static int fio_recv_data(int sk, void *p, unsigned int len)
264 int ret = recv(sk, p, len, MSG_WAITALL);
274 else if (errno == EAGAIN || errno == EINTR)
278 } while (!exit_backend);
286 static int verify_convert_cmd(struct fio_net_cmd *cmd)
290 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
291 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
293 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
294 if (crc != cmd->cmd_crc16) {
295 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
296 cmd->cmd_crc16, crc);
300 cmd->version = le16_to_cpu(cmd->version);
301 cmd->opcode = le16_to_cpu(cmd->opcode);
302 cmd->flags = le32_to_cpu(cmd->flags);
303 cmd->tag = le64_to_cpu(cmd->tag);
304 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
306 switch (cmd->version) {
310 log_err("fio: bad server cmd version %d\n", cmd->version);
314 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
315 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
323 * Read (and defragment, if necessary) incoming commands
325 struct fio_net_cmd *fio_net_recv_cmd(int sk)
327 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
328 size_t cmd_size = 0, pdu_offset = 0;
334 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
338 /* We have a command, verify it and swap if need be */
339 ret = verify_convert_cmd(&cmd);
344 /* if this is text, add room for \0 at the end */
345 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
348 cmd_size += cmd.pdu_len;
350 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
351 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
356 tmp = realloc(cmdret, cmd_size);
358 log_err("fio: server failed allocating cmd\n");
365 memcpy(cmdret, &cmd, sizeof(cmd));
366 else if (cmdret->opcode != cmd.opcode) {
367 log_err("fio: fragment opcode mismatch (%d != %d)\n",
368 cmdret->opcode, cmd.opcode);
376 /* There's payload, get it */
377 pdu = (void *) cmdret->payload + pdu_offset;
378 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
382 /* Verify payload crc */
383 crc = fio_crc16(pdu, cmd.pdu_len);
384 if (crc != cmd.pdu_crc16) {
385 log_err("fio: server bad crc on payload ");
386 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
391 pdu_offset += cmd.pdu_len;
393 cmdret->pdu_len += cmd.pdu_len;
395 } while (cmd.flags & FIO_NET_CMD_F_MORE);
401 /* zero-terminate text input */
402 if (cmdret->pdu_len) {
403 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
404 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
405 char *buf = (char *) __pdu->buf;
407 buf[__pdu->buf_len] = '\0';
408 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
409 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
410 char *buf = (char *) __pdu->buf;
411 int len = le32_to_cpu(__pdu->buf_len);
417 /* frag flag is internal */
418 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
424 static void add_reply(uint64_t tag, struct flist_head *list)
426 struct fio_net_cmd_reply *reply;
428 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
429 flist_add_tail(&reply->list, list);
432 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
434 struct fio_net_cmd_reply *reply;
436 reply = calloc(1, sizeof(*reply));
437 INIT_FLIST_HEAD(&reply->list);
438 fio_gettime(&reply->tv, NULL);
439 reply->saved_tag = tag;
440 reply->opcode = opcode;
442 return (uintptr_t) reply;
445 static void free_reply(uint64_t tag)
447 struct fio_net_cmd_reply *reply;
449 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
453 static void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
457 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
459 pdu_len = le32_to_cpu(cmd->pdu_len);
460 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
463 static void fio_net_cmd_crc(struct fio_net_cmd *cmd)
465 fio_net_cmd_crc_pdu(cmd, cmd->payload);
468 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
469 uint64_t *tagptr, struct flist_head *list)
471 struct fio_net_cmd *cmd = NULL;
472 size_t this_len, cur_len = 0;
478 tag = *tagptr = alloc_reply(*tagptr, opcode);
480 tag = tagptr ? *tagptr : 0;
484 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
485 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
487 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
491 cur_len = sizeof(*cmd) + this_len;
492 cmd = malloc(cur_len);
495 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
498 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
500 fio_net_cmd_crc(cmd);
502 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
505 } while (!ret && size);
511 add_reply(tag, list);
520 static struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf, off_t size,
521 uint64_t *tagptr, int flags)
523 struct sk_entry *entry;
525 entry = smalloc(sizeof(*entry));
526 INIT_FLIST_HEAD(&entry->next);
527 entry->opcode = opcode;
528 if (flags & SK_F_COPY) {
529 entry->buf = smalloc(size);
530 memcpy(entry->buf, buf, size);
534 entry->tagptr = tagptr;
535 entry->flags = flags;
540 static void fio_net_queue_entry(struct sk_entry *entry)
542 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
545 flist_add_tail(&entry->list, &sk_out->list);
548 fio_mutex_up(sk_out->wait);
551 static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
552 uint64_t *tagptr, int flags)
554 struct sk_entry *entry;
556 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
557 fio_net_queue_entry(entry);
561 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
563 struct fio_net_cmd cmd;
565 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
566 fio_net_cmd_crc(&cmd);
568 return fio_send_data(sk, &cmd, sizeof(cmd));
572 * If 'list' is non-NULL, then allocate and store the sent command for
573 * later verification.
575 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
576 struct flist_head *list)
581 tag = alloc_reply(tag, opcode);
583 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
592 add_reply(tag, list);
597 static int fio_net_queue_quit(void)
599 dprint(FD_NET, "server: sending quit\n");
601 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, 0, SK_F_SIMPLE);
604 int fio_net_send_quit(int sk)
606 dprint(FD_NET, "server: sending quit\n");
608 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
611 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
613 struct cmd_end_pdu epdu;
619 epdu.error = __cpu_to_le32(error);
620 epdu.signal = __cpu_to_le32(signal);
621 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
624 static int fio_net_queue_stop(int error, int signal)
626 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
627 return fio_net_send_ack(NULL, error, signal);
630 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
632 struct fio_fork_item *ffi;
634 ffi = malloc(sizeof(*ffi));
639 flist_add_tail(&ffi->list, list);
642 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
644 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
645 fio_server_add_fork_item(pid, conn_list);
648 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
650 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
651 fio_server_add_fork_item(pid, job_list);
654 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
658 ret = waitpid(ffi->pid, &status, WNOHANG);
660 if (errno == ECHILD) {
661 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
664 log_err("fio: waitpid: %s\n", strerror(errno));
665 } else if (ret == ffi->pid) {
666 if (WIFSIGNALED(status)) {
667 ffi->signal = WTERMSIG(status);
670 if (WIFEXITED(status)) {
671 if (WEXITSTATUS(status))
672 ffi->exitval = WEXITSTATUS(status);
678 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
680 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
683 * Fold STOP and QUIT...
686 fio_net_queue_stop(ffi->exitval, ffi->signal);
687 fio_net_queue_quit();
690 flist_del(&ffi->list);
694 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
696 struct flist_head *entry, *tmp;
697 struct fio_fork_item *ffi;
699 flist_for_each_safe(entry, tmp, list) {
700 ffi = flist_entry(entry, struct fio_fork_item, list);
702 fio_server_check_fork_item(ffi);
705 fio_server_fork_item_done(ffi, stop);
709 static void fio_server_check_jobs(struct flist_head *job_list)
711 fio_server_check_fork_items(job_list, true);
714 static void fio_server_check_conns(struct flist_head *conn_list)
716 fio_server_check_fork_items(conn_list, false);
719 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
721 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
722 void *file_name = pdu->file;
723 struct cmd_start_pdu spdu;
725 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
727 pdu->name_len = le16_to_cpu(pdu->name_len);
728 pdu->client_type = le16_to_cpu(pdu->client_type);
730 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
731 fio_net_queue_quit();
735 spdu.jobs = cpu_to_le32(thread_number);
736 spdu.stat_outputs = cpu_to_le32(stat_number);
737 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
741 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
742 struct fio_net_cmd *cmd)
752 fio_server_add_job_pid(job_list, pid);
756 ret = fio_backend(sk_out);
761 static int handle_job_cmd(struct fio_net_cmd *cmd)
763 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
764 void *buf = pdu->buf;
765 struct cmd_start_pdu spdu;
767 pdu->buf_len = le32_to_cpu(pdu->buf_len);
768 pdu->client_type = le32_to_cpu(pdu->client_type);
770 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
771 fio_net_queue_quit();
775 spdu.jobs = cpu_to_le32(thread_number);
776 spdu.stat_outputs = cpu_to_le32(stat_number);
778 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
782 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
784 void *pdu = cmd->payload;
785 struct cmd_single_line_pdu *cslp;
786 struct cmd_line_pdu *clp;
787 unsigned long offset;
788 struct cmd_start_pdu spdu;
793 clp->lines = le16_to_cpu(clp->lines);
794 clp->client_type = le16_to_cpu(clp->client_type);
795 argv = malloc(clp->lines * sizeof(char *));
796 offset = sizeof(*clp);
798 dprint(FD_NET, "server: %d command line args\n", clp->lines);
800 for (i = 0; i < clp->lines; i++) {
802 argv[i] = (char *) cslp->text;
804 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
805 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
808 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
809 fio_net_queue_quit();
816 spdu.jobs = cpu_to_le32(thread_number);
817 spdu.stat_outputs = cpu_to_le32(stat_number);
819 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
823 static int handle_probe_cmd(struct fio_net_cmd *cmd)
825 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
826 struct cmd_probe_reply_pdu probe;
827 uint64_t tag = cmd->tag;
829 dprint(FD_NET, "server: sending probe reply\n");
831 strcpy(me, (char *) pdu->server);
833 memset(&probe, 0, sizeof(probe));
834 gethostname((char *) probe.hostname, sizeof(probe.hostname));
835 #ifdef CONFIG_BIG_ENDIAN
838 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
841 probe.arch = FIO_ARCH;
842 probe.bpp = sizeof(void *);
843 probe.cpus = __cpu_to_le32(cpus_online());
846 * If the client supports compression and we do too, then enable it
848 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
849 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
856 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
859 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
862 uint64_t tag = cmd->tag;
866 dprint(FD_NET, "server sending status\n");
869 * Fake ETA return if we don't have a local one, otherwise the client
870 * will end up timing out waiting for a response to the ETA request
872 je = get_jobs_eta(true, &size);
875 je = calloc(1, size);
877 je->nr_running = cpu_to_le32(je->nr_running);
878 je->nr_ramp = cpu_to_le32(je->nr_ramp);
879 je->nr_pending = cpu_to_le32(je->nr_pending);
880 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
881 je->files_open = cpu_to_le32(je->files_open);
883 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
884 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
885 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
886 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
887 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
888 je->rate[i] = cpu_to_le32(je->rate[i]);
889 je->iops[i] = cpu_to_le32(je->iops[i]);
892 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
893 je->eta_sec = cpu_to_le64(je->eta_sec);
894 je->nr_threads = cpu_to_le32(je->nr_threads);
895 je->is_pow2 = cpu_to_le32(je->is_pow2);
896 je->unit_base = cpu_to_le32(je->unit_base);
899 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
903 static int send_update_job_reply(uint64_t __tag, int error)
905 uint64_t tag = __tag;
908 pdu_error = __cpu_to_le32(error);
909 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
912 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
914 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
915 struct thread_data *td;
918 tnumber = le32_to_cpu(pdu->thread_number);
920 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
922 if (!tnumber || tnumber > thread_number) {
923 send_update_job_reply(cmd->tag, ENODEV);
927 td = &threads[tnumber - 1];
928 convert_thread_options_to_cpu(&td->o, &pdu->top);
929 send_update_job_reply(cmd->tag, 0);
933 static int handle_trigger_cmd(struct fio_net_cmd *cmd)
935 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
936 char *buf = (char *) pdu->cmd;
937 struct all_io_list *rep;
940 pdu->len = le16_to_cpu(pdu->len);
941 buf[pdu->len] = '\0';
943 rep = get_all_io_list(IO_LIST_ALL, &sz);
945 struct all_io_list state;
947 state.threads = cpu_to_le64((uint64_t) 0);
948 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY);
950 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE);
956 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
957 struct fio_net_cmd *cmd)
961 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
962 fio_server_op(cmd->opcode), cmd->pdu_len,
963 (unsigned long long) cmd->tag);
965 switch (cmd->opcode) {
966 case FIO_NET_CMD_QUIT:
967 fio_terminate_threads(TERMINATE_ALL);
970 case FIO_NET_CMD_EXIT:
973 case FIO_NET_CMD_LOAD_FILE:
974 ret = handle_load_file_cmd(cmd);
976 case FIO_NET_CMD_JOB:
977 ret = handle_job_cmd(cmd);
979 case FIO_NET_CMD_JOBLINE:
980 ret = handle_jobline_cmd(cmd);
982 case FIO_NET_CMD_PROBE:
983 ret = handle_probe_cmd(cmd);
985 case FIO_NET_CMD_SEND_ETA:
986 ret = handle_send_eta_cmd(cmd);
988 case FIO_NET_CMD_RUN:
989 ret = handle_run_cmd(sk_out, job_list, cmd);
991 case FIO_NET_CMD_UPDATE_JOB:
992 ret = handle_update_job_cmd(cmd);
994 case FIO_NET_CMD_VTRIGGER:
995 ret = handle_trigger_cmd(cmd);
997 case FIO_NET_CMD_SENDFILE: {
998 struct cmd_sendfile_reply *in;
999 struct cmd_reply *rep;
1001 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
1003 in = (struct cmd_sendfile_reply *) cmd->payload;
1004 in->size = le32_to_cpu(in->size);
1005 in->error = le32_to_cpu(in->error);
1008 rep->error = in->error;
1011 rep->data = smalloc(in->size);
1014 rep->error = ENOMEM;
1016 rep->size = in->size;
1017 memcpy(rep->data, in->data, in->size);
1020 fio_mutex_up(&rep->lock);
1024 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
1032 * Send a command with a separate PDU, not inlined in the command
1034 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1035 off_t size, uint64_t tag, uint32_t flags)
1037 struct fio_net_cmd cmd;
1038 struct iovec iov[2];
1040 iov[0].iov_base = (void *) &cmd;
1041 iov[0].iov_len = sizeof(cmd);
1042 iov[1].iov_base = (void *) buf;
1043 iov[1].iov_len = size;
1045 __fio_init_net_cmd(&cmd, opcode, size, tag);
1046 cmd.flags = __cpu_to_le32(flags);
1047 fio_net_cmd_crc_pdu(&cmd, buf);
1049 return fio_sendv_data(sk, iov, 2);
1052 static void finish_entry(struct sk_entry *entry)
1054 if (entry->flags & SK_F_FREE)
1056 else if (entry->flags & SK_F_COPY)
1062 static void entry_set_flags_tag(struct sk_entry *entry, struct flist_head *list,
1063 unsigned int *flags, uint64_t *tag)
1065 if (!flist_empty(list))
1066 *flags = FIO_NET_CMD_F_MORE;
1071 *tag = *entry->tagptr;
1076 static int send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1082 entry_set_flags_tag(first, &first->next, &flags, &tag);
1084 ret = fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf, first->size, tag, flags);
1086 while (!flist_empty(&first->next)) {
1087 struct sk_entry *next;
1089 next = flist_first_entry(&first->next, struct sk_entry, list);
1090 flist_del_init(&next->list);
1092 entry_set_flags_tag(next, &first->next, &flags, &tag);
1094 ret += fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf, next->size, tag, flags);
1101 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1105 if (entry->flags & SK_F_VEC)
1106 ret = send_vec_entry(sk_out, entry);
1107 if (entry->flags & SK_F_SIMPLE) {
1111 tag = *entry->tagptr;
1113 ret = fio_net_send_simple_cmd(sk_out->sk, entry->opcode, tag, NULL);
1115 ret = fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf, entry->size, entry->tagptr, NULL);
1118 log_err("fio: failed handling cmd %s\n", fio_server_op(entry->opcode));
1120 finish_entry(entry);
1124 static int handle_xmits(struct sk_out *sk_out)
1126 struct sk_entry *entry;
1131 if (flist_empty(&sk_out->list)) {
1136 flist_splice_init(&sk_out->list, &list);
1139 while (!flist_empty(&list)) {
1140 entry = flist_entry(list.next, struct sk_entry, list);
1141 flist_del(&entry->list);
1142 ret += handle_sk_entry(sk_out, entry);
1148 static int handle_connection(struct sk_out *sk_out)
1150 struct fio_net_cmd *cmd = NULL;
1151 FLIST_HEAD(job_list);
1157 while (!exit_backend) {
1158 struct pollfd pfd = {
1167 if (!flist_empty(&job_list))
1170 handle_xmits(sk_out);
1172 ret = poll(&pfd, 1, 0);
1176 log_err("fio: poll: %s\n", strerror(errno));
1179 fio_server_check_jobs(&job_list);
1180 fio_mutex_down_timeout(sk_out->wait, timeout);
1184 if (pfd.revents & POLLIN)
1186 if (pfd.revents & (POLLERR|POLLHUP)) {
1190 } while (!exit_backend);
1192 fio_server_check_jobs(&job_list);
1197 cmd = fio_net_recv_cmd(sk_out->sk);
1203 ret = handle_command(sk_out, &job_list, cmd);
1214 handle_xmits(sk_out);
1218 __sk_out_drop(sk_out);
1222 /* get the address on this host bound by the input socket,
1223 * whether it is ipv6 or ipv4 */
1225 static int get_my_addr_str(int sk)
1227 struct sockaddr_in6 myaddr6 = { 0, };
1228 struct sockaddr_in myaddr4 = { 0, };
1229 struct sockaddr *sockaddr_p;
1235 len = sizeof(myaddr6);
1236 sockaddr_p = (struct sockaddr * )&myaddr6;
1237 net_addr = (char * )&myaddr6.sin6_addr;
1239 len = sizeof(myaddr4);
1240 sockaddr_p = (struct sockaddr * )&myaddr4;
1241 net_addr = (char * )&myaddr4.sin_addr;
1244 ret = getsockname(sk, sockaddr_p, &len);
1246 log_err("fio: getsockaddr: %s\n", strerror(errno));
1250 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1251 log_err("inet_ntop: failed to convert addr to string\n");
1255 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1259 static int accept_loop(int listen_sk)
1261 struct sockaddr_in addr;
1262 struct sockaddr_in6 addr6;
1263 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1265 int ret = 0, sk, exitval = 0;
1266 FLIST_HEAD(conn_list);
1268 dprint(FD_NET, "server enter accept loop\n");
1270 fio_set_fd_nonblocking(listen_sk, "server");
1272 while (!exit_backend) {
1273 struct sk_out *sk_out;
1279 pfd.events = POLLIN;
1283 if (!flist_empty(&conn_list))
1286 ret = poll(&pfd, 1, timeout);
1290 log_err("fio: poll: %s\n", strerror(errno));
1293 fio_server_check_conns(&conn_list);
1297 if (pfd.revents & POLLIN)
1299 } while (!exit_backend);
1301 fio_server_check_conns(&conn_list);
1303 if (exit_backend || ret < 0)
1307 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1309 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1312 log_err("fio: accept: %s\n", strerror(errno));
1317 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1319 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1321 dprint(FD_NET, "server: connect from %s\n", from);
1323 sk_out = smalloc(sizeof(*sk_out));
1325 INIT_FLIST_HEAD(&sk_out->list);
1326 sk_out->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1327 sk_out->wait = fio_mutex_init(FIO_MUTEX_LOCKED);
1332 fio_server_add_conn_pid(&conn_list, pid);
1336 /* if error, it's already logged, non-fatal */
1337 get_my_addr_str(sk);
1340 * Assign sk_out here, it'll be dropped in handle_connection()
1341 * since that function calls _exit() when done
1343 sk_out_assign(sk_out);
1344 handle_connection(sk_out);
1350 int fio_server_text_output(int level, const char *buf, size_t len)
1352 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1353 struct cmd_text_pdu *pdu;
1357 if (!sk_out || sk_out->sk == -1)
1360 tlen = sizeof(*pdu) + len;
1363 pdu->level = __cpu_to_le32(level);
1364 pdu->buf_len = __cpu_to_le32(len);
1366 gettimeofday(&tv, NULL);
1367 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1368 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1370 memcpy(pdu->buf, buf, len);
1372 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1377 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1379 dst->max_val = cpu_to_le64(src->max_val);
1380 dst->min_val = cpu_to_le64(src->min_val);
1381 dst->samples = cpu_to_le64(src->samples);
1384 * Encode to IEEE 754 for network transfer
1386 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1387 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1390 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1394 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1395 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1396 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1397 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1398 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1399 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
1400 dst->agg[i] = cpu_to_le64(src->agg[i]);
1403 dst->kb_base = cpu_to_le32(src->kb_base);
1404 dst->unit_base = cpu_to_le32(src->unit_base);
1405 dst->groupid = cpu_to_le32(src->groupid);
1406 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1410 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1411 * into a single payload.
1413 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1415 struct cmd_ts_pdu p;
1418 dprint(FD_NET, "server sending end stats\n");
1420 memset(&p, 0, sizeof(p));
1422 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1423 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1424 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1426 p.ts.error = cpu_to_le32(ts->error);
1427 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1428 p.ts.groupid = cpu_to_le32(ts->groupid);
1429 p.ts.pid = cpu_to_le32(ts->pid);
1430 p.ts.members = cpu_to_le32(ts->members);
1431 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1433 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1434 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1435 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1436 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1437 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1440 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1441 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1442 p.ts.ctx = cpu_to_le64(ts->ctx);
1443 p.ts.minf = cpu_to_le64(ts->minf);
1444 p.ts.majf = cpu_to_le64(ts->majf);
1445 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1446 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1448 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1449 fio_fp64_t *src = &ts->percentile_list[i];
1450 fio_fp64_t *dst = &p.ts.percentile_list[i];
1452 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1455 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1456 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1457 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1458 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1461 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1462 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1463 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1466 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1467 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1468 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1470 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1471 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1472 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1473 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1476 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1477 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1479 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1480 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1481 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1484 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1485 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1486 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1487 p.ts.first_error = cpu_to_le32(ts->first_error);
1488 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1489 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1491 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1492 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1493 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1494 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1496 p.ts.nr_block_infos = le64_to_cpu(ts->nr_block_infos);
1497 for (i = 0; i < p.ts.nr_block_infos; i++)
1498 p.ts.block_infos[i] = le32_to_cpu(ts->block_infos[i]);
1500 convert_gs(&p.rs, rs);
1502 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1505 void fio_server_send_gs(struct group_run_stats *rs)
1507 struct group_run_stats gs;
1509 dprint(FD_NET, "server sending group run stats\n");
1511 convert_gs(&gs, rs);
1512 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1515 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1519 for (i = 0; i < 2; i++) {
1520 dst->ios[i] = cpu_to_le64(src->ios[i]);
1521 dst->merges[i] = cpu_to_le64(src->merges[i]);
1522 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1523 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1526 dst->io_ticks = cpu_to_le64(src->io_ticks);
1527 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1528 dst->slavecount = cpu_to_le32(src->slavecount);
1529 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1532 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1536 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1537 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1539 for (i = 0; i < 2; i++) {
1540 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1541 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1542 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1543 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1546 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1547 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1548 dst->s.msec = cpu_to_le64(src->s.msec);
1551 void fio_server_send_du(void)
1553 struct disk_util *du;
1554 struct flist_head *entry;
1555 struct cmd_du_pdu pdu;
1557 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1559 memset(&pdu, 0, sizeof(pdu));
1561 flist_for_each(entry, &disk_list) {
1562 du = flist_entry(entry, struct disk_util, list);
1564 convert_dus(&pdu.dus, &du->dus);
1565 convert_agg(&pdu.agg, &du->agg);
1567 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1571 static int fio_send_iolog_gz(struct sk_entry *first, struct io_log *log)
1575 struct sk_entry *entry;
1580 * Dirty - since the log is potentially huge, compress it into
1581 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1582 * side defragment it.
1584 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1586 stream.zalloc = Z_NULL;
1587 stream.zfree = Z_NULL;
1588 stream.opaque = Z_NULL;
1590 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
1595 stream.next_in = (void *) log->log;
1596 stream.avail_in = log->nr_samples * log_entry_sz(log);
1599 unsigned int this_len;
1601 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1602 stream.next_out = out_pdu;
1603 ret = deflate(&stream, Z_FINISH);
1604 /* may be Z_OK, or Z_STREAM_END */
1608 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1610 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1611 NULL, SK_F_FREE | SK_F_VEC);
1612 flist_add_tail(&entry->list, &first->next);
1613 } while (stream.avail_in);
1616 deflateEnd(&stream);
1623 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1625 struct cmd_iolog_pdu pdu;
1626 struct sk_entry *first;
1629 pdu.nr_samples = cpu_to_le64(log->nr_samples);
1630 pdu.thread_number = cpu_to_le32(td->thread_number);
1631 pdu.log_type = cpu_to_le32(log->log_type);
1632 pdu.compressed = cpu_to_le32(use_zlib);
1634 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1635 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1637 for (i = 0; i < log->nr_samples; i++) {
1638 struct io_sample *s = get_sample(log, i);
1640 s->time = cpu_to_le64(s->time);
1641 s->val = cpu_to_le64(s->val);
1642 s->__ddir = cpu_to_le32(s->__ddir);
1643 s->bs = cpu_to_le32(s->bs);
1645 if (log->log_offset) {
1646 struct io_sample_offset *so = (void *) s;
1648 so->offset = cpu_to_le64(so->offset);
1653 * Assemble header entry first
1655 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_COPY | SK_F_VEC);
1658 * Now append actual log entries. Compress if we can, otherwise just
1659 * plain text output.
1662 ret = fio_send_iolog_gz(first, log);
1664 struct sk_entry *entry;
1666 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, log->log,
1667 log->nr_samples * log_entry_sz(log),
1668 NULL, SK_F_FREE | SK_F_VEC);
1669 flist_add_tail(&entry->list, &first->next);
1675 void fio_server_send_add_job(struct thread_data *td)
1677 struct cmd_add_job_pdu pdu;
1679 memset(&pdu, 0, sizeof(pdu));
1680 pdu.thread_number = cpu_to_le32(td->thread_number);
1681 pdu.groupid = cpu_to_le32(td->groupid);
1682 convert_thread_options_to_net(&pdu.top, &td->o);
1684 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1687 void fio_server_send_start(struct thread_data *td)
1689 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1691 assert(sk_out->sk != -1);
1693 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, 0, SK_F_SIMPLE);
1696 int fio_server_get_verify_state(const char *name, int threadnumber,
1697 void **datap, int *version)
1699 struct thread_io_list *s;
1700 struct cmd_sendfile out;
1701 struct cmd_reply *rep;
1705 dprint(FD_NET, "server: request verify state\n");
1707 rep = smalloc(sizeof(*rep));
1709 log_err("fio: smalloc pool too small\n");
1713 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1717 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1719 tag = (uint64_t) (uintptr_t) rep;
1720 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag, SK_F_COPY);
1723 * Wait for the backend to receive the reply
1725 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
1726 log_err("fio: timed out waiting for reply\n");
1731 log_err("fio: failure on receiving state file: %s\n", strerror(rep->error));
1735 fio_net_queue_quit();
1740 * The format is verify_state_hdr, then thread_io_list. Verify
1741 * the header, and the thread_io_list checksum
1743 s = rep->data + sizeof(struct verify_state_hdr);
1744 if (verify_state_hdr(rep->data, s, version))
1748 * Don't need the header from now, copy just the thread_io_list
1750 rep->size -= sizeof(struct verify_state_hdr);
1751 data = malloc(rep->size);
1752 memcpy(data, s, rep->size);
1756 __fio_mutex_remove(&rep->lock);
1761 static int fio_init_server_ip(void)
1763 struct sockaddr *addr;
1770 sk = socket(AF_INET6, SOCK_STREAM, 0);
1772 sk = socket(AF_INET, SOCK_STREAM, 0);
1775 log_err("fio: socket: %s\n", strerror(errno));
1780 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1781 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
1786 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
1787 log_err("fio: setsockopt(REUSEPORT): %s\n", strerror(errno));
1794 const void *src = &saddr_in6.sin6_addr;
1796 addr = (struct sockaddr *) &saddr_in6;
1797 socklen = sizeof(saddr_in6);
1798 saddr_in6.sin6_family = AF_INET6;
1799 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
1801 const void *src = &saddr_in.sin_addr;
1803 addr = (struct sockaddr *) &saddr_in;
1804 socklen = sizeof(saddr_in);
1805 saddr_in.sin_family = AF_INET;
1806 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
1809 if (bind(sk, addr, socklen) < 0) {
1810 log_err("fio: bind: %s\n", strerror(errno));
1811 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
1819 static int fio_init_server_sock(void)
1821 struct sockaddr_un addr;
1826 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1828 log_err("fio: socket: %s\n", strerror(errno));
1834 memset(&addr, 0, sizeof(addr));
1835 addr.sun_family = AF_UNIX;
1836 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
1838 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1840 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1841 log_err("fio: bind: %s\n", strerror(errno));
1850 static int fio_init_server_connection(void)
1855 dprint(FD_NET, "starting server\n");
1858 sk = fio_init_server_ip();
1860 sk = fio_init_server_sock();
1865 memset(bind_str, 0, sizeof(bind_str));
1874 src = &saddr_in6.sin6_addr;
1877 src = &saddr_in.sin_addr;
1880 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1882 sprintf(port, ",%u", fio_net_port);
1886 strncpy(bind_str, port, sizeof(bind_str) - 1);
1888 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
1890 log_info("fio: server listening on %s\n", bind_str);
1892 if (listen(sk, 4) < 0) {
1893 log_err("fio: listen: %s\n", strerror(errno));
1901 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
1902 struct in6_addr *inp6)
1908 ret = inet_pton(AF_INET6, host, inp6);
1910 ret = inet_pton(AF_INET, host, inp);
1913 struct addrinfo hints, *res;
1915 memset(&hints, 0, sizeof(hints));
1916 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
1917 hints.ai_socktype = SOCK_STREAM;
1919 ret = getaddrinfo(host, NULL, &hints, &res);
1921 log_err("fio: failed to resolve <%s> (%s)\n", host,
1927 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
1929 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
1939 * Parse a host/ip/port string. Reads from 'str'.
1944 * *ptr is the host, *port is the port, inp is the destination.
1946 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1947 * For local domain sockets:
1948 * *ptr is the filename, *is_sock is 1.
1950 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
1951 int *port, struct in_addr *inp,
1952 struct in6_addr *inp6, int *ipv6)
1954 const char *host = str;
1960 *port = fio_net_port;
1963 if (!strncmp(str, "sock:", 5)) {
1964 *ptr = strdup(str + 5);
1971 * Is it ip:<ip or host>:port
1973 if (!strncmp(host, "ip:", 3))
1975 else if (!strncmp(host, "ip4:", 4))
1977 else if (!strncmp(host, "ip6:", 4)) {
1980 } else if (host[0] == ':') {
1981 /* String is :port */
1984 if (!lport || lport > 65535) {
1985 log_err("fio: bad server port %u\n", lport);
1988 /* no hostname given, we are done */
1994 * If no port seen yet, check if there's a last ',' at the end
1997 portp = strchr(host, ',');
2001 lport = atoi(portp);
2002 if (!lport || lport > 65535) {
2003 log_err("fio: bad server port %u\n", lport);
2015 *ptr = strdup(host);
2017 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2024 *port = fio_net_port;
2030 * Server arg should be one of:
2032 * sock:/path/to/socket
2036 * Where sock uses unix domain sockets, and ip binds the server to
2037 * a specific interface. If no arguments are given to the server, it
2038 * uses IP and binds to 0.0.0.0.
2041 static int fio_handle_server_arg(void)
2043 int port = fio_net_port;
2044 int is_sock, ret = 0;
2046 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2048 if (!fio_server_arg)
2051 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2052 &port, &saddr_in.sin_addr,
2053 &saddr_in6.sin6_addr, &use_ipv6);
2055 if (!is_sock && bind_sock) {
2061 fio_net_port = port;
2062 saddr_in.sin_port = htons(port);
2063 saddr_in6.sin6_port = htons(port);
2067 static void sig_int(int sig)
2073 static void set_sig_handlers(void)
2075 struct sigaction act;
2077 memset(&act, 0, sizeof(act));
2078 act.sa_handler = sig_int;
2079 act.sa_flags = SA_RESTART;
2080 sigaction(SIGINT, &act, NULL);
2083 static int fio_server(void)
2087 if (pthread_key_create(&sk_out_key, NULL)) {
2088 log_err("fio: can't create sk_out backend key\n");
2092 pthread_setspecific(sk_out_key, NULL);
2094 dprint(FD_NET, "starting server\n");
2096 if (fio_handle_server_arg())
2099 sk = fio_init_server_connection();
2105 ret = accept_loop(sk);
2109 if (fio_server_arg) {
2110 free(fio_server_arg);
2111 fio_server_arg = NULL;
2119 void fio_server_got_signal(int signal)
2121 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2125 if (signal == SIGPIPE)
2128 log_info("\nfio: terminating on signal %d\n", signal);
2133 static int check_existing_pidfile(const char *pidfile)
2140 if (stat(pidfile, &sb))
2143 f = fopen(pidfile, "r");
2147 if (fread(buf, sb.st_size, 1, f) <= 0) {
2154 if (kill(pid, SIGCONT) < 0)
2155 return errno != ESRCH;
2160 static int write_pid(pid_t pid, const char *pidfile)
2164 fpid = fopen(pidfile, "w");
2166 log_err("fio: failed opening pid file %s\n", pidfile);
2170 fprintf(fpid, "%u\n", (unsigned int) pid);
2176 * If pidfile is specified, background us.
2178 int fio_start_server(char *pidfile)
2185 WSAStartup(MAKEWORD(2, 2), &wsd);
2189 return fio_server();
2191 if (check_existing_pidfile(pidfile)) {
2192 log_err("fio: pidfile %s exists and server appears alive\n",
2200 log_err("fio: failed server fork: %s", strerror(errno));
2204 ret = write_pid(pid, pidfile);
2210 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2212 close(STDIN_FILENO);
2213 close(STDOUT_FILENO);
2214 close(STDERR_FILENO);
2226 void fio_server_set_arg(const char *arg)
2228 fio_server_arg = strdup(arg);