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;
49 struct flist_head next;
54 struct fio_mutex *lock;
55 struct flist_head list;
56 struct fio_mutex *wait;
59 static char *fio_server_arg;
60 static char *bind_sock;
61 static struct sockaddr_in saddr_in;
62 static struct sockaddr_in6 saddr_in6;
65 static unsigned int has_zlib = 1;
67 static unsigned int has_zlib = 0;
69 static unsigned int use_zlib;
72 static pthread_key_t sk_out_key;
74 struct fio_fork_item {
75 struct flist_head list;
83 struct fio_mutex lock;
89 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
114 static void sk_lock(struct sk_out *sk_out)
116 fio_mutex_down(sk_out->lock);
119 static void sk_unlock(struct sk_out *sk_out)
121 fio_mutex_up(sk_out->lock);
124 const char *fio_server_op(unsigned int op)
128 if (op < FIO_NET_CMD_NR)
129 return fio_server_ops[op];
131 sprintf(buf, "UNKNOWN/%d", op);
135 static ssize_t iov_total_len(const struct iovec *iov, int count)
147 static int fio_sendv_data(int sk, struct iovec *iov, int count)
149 ssize_t total_len = iov_total_len(iov, count);
153 ret = writev(sk, iov, count);
160 if (ret >= iov->iov_len) {
165 iov->iov_base += ret;
171 else if (errno == EAGAIN || errno == EINTR)
175 } while (!exit_backend);
186 int fio_send_data(int sk, const void *p, unsigned int len)
188 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
190 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
192 return fio_sendv_data(sk, &iov, 1);
195 int fio_recv_data(int sk, void *p, unsigned int len)
198 int ret = recv(sk, p, len, MSG_WAITALL);
208 else if (errno == EAGAIN || errno == EINTR)
212 } while (!exit_backend);
220 static int verify_convert_cmd(struct fio_net_cmd *cmd)
224 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
225 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
227 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
228 if (crc != cmd->cmd_crc16) {
229 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
230 cmd->cmd_crc16, crc);
234 cmd->version = le16_to_cpu(cmd->version);
235 cmd->opcode = le16_to_cpu(cmd->opcode);
236 cmd->flags = le32_to_cpu(cmd->flags);
237 cmd->tag = le64_to_cpu(cmd->tag);
238 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
240 switch (cmd->version) {
244 log_err("fio: bad server cmd version %d\n", cmd->version);
248 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
249 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
257 * Read (and defragment, if necessary) incoming commands
259 struct fio_net_cmd *fio_net_recv_cmd(int sk)
261 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
262 size_t cmd_size = 0, pdu_offset = 0;
268 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
272 /* We have a command, verify it and swap if need be */
273 ret = verify_convert_cmd(&cmd);
278 /* if this is text, add room for \0 at the end */
279 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
282 cmd_size += cmd.pdu_len;
284 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
285 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
290 tmp = realloc(cmdret, cmd_size);
292 log_err("fio: server failed allocating cmd\n");
299 memcpy(cmdret, &cmd, sizeof(cmd));
300 else if (cmdret->opcode != cmd.opcode) {
301 log_err("fio: fragment opcode mismatch (%d != %d)\n",
302 cmdret->opcode, cmd.opcode);
310 /* There's payload, get it */
311 pdu = (void *) cmdret->payload + pdu_offset;
312 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
316 /* Verify payload crc */
317 crc = fio_crc16(pdu, cmd.pdu_len);
318 if (crc != cmd.pdu_crc16) {
319 log_err("fio: server bad crc on payload ");
320 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
325 pdu_offset += cmd.pdu_len;
327 cmdret->pdu_len += cmd.pdu_len;
329 } while (cmd.flags & FIO_NET_CMD_F_MORE);
335 /* zero-terminate text input */
336 if (cmdret->pdu_len) {
337 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
338 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
339 char *buf = (char *) __pdu->buf;
341 buf[__pdu->buf_len] = '\0';
342 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
343 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
344 char *buf = (char *) __pdu->buf;
345 int len = le32_to_cpu(__pdu->buf_len);
351 /* frag flag is internal */
352 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
358 static void add_reply(uint64_t tag, struct flist_head *list)
360 struct fio_net_cmd_reply *reply;
362 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
363 flist_add_tail(&reply->list, list);
366 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
368 struct fio_net_cmd_reply *reply;
370 reply = calloc(1, sizeof(*reply));
371 INIT_FLIST_HEAD(&reply->list);
372 fio_gettime(&reply->tv, NULL);
373 reply->saved_tag = tag;
374 reply->opcode = opcode;
376 return (uintptr_t) reply;
379 static void free_reply(uint64_t tag)
381 struct fio_net_cmd_reply *reply;
383 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
387 void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
391 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
393 pdu_len = le32_to_cpu(cmd->pdu_len);
394 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
397 void fio_net_cmd_crc(struct fio_net_cmd *cmd)
399 fio_net_cmd_crc_pdu(cmd, cmd->payload);
402 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
403 uint64_t *tagptr, struct flist_head *list)
405 struct fio_net_cmd *cmd = NULL;
406 size_t this_len, cur_len = 0;
412 tag = *tagptr = alloc_reply(*tagptr, opcode);
414 tag = tagptr ? *tagptr : 0;
418 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
419 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
421 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
425 cur_len = sizeof(*cmd) + this_len;
426 cmd = malloc(cur_len);
429 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
432 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
434 fio_net_cmd_crc(cmd);
436 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
439 } while (!ret && size);
445 add_reply(tag, list);
454 struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf, off_t size,
455 uint64_t *tagptr, int flags)
457 struct sk_entry *entry;
459 entry = smalloc(sizeof(*entry));
460 INIT_FLIST_HEAD(&entry->next);
461 entry->opcode = opcode;
462 if (flags & SK_F_COPY) {
463 entry->buf = smalloc(size);
464 memcpy(entry->buf, buf, size);
468 entry->tagptr = tagptr;
469 entry->flags = flags;
474 static void fio_net_queue_entry(struct sk_entry *entry)
476 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
479 flist_add_tail(&entry->list, &sk_out->list);
482 fio_mutex_up(sk_out->wait);
485 static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
486 uint64_t *tagptr, int flags)
488 struct sk_entry *entry;
490 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
491 fio_net_queue_entry(entry);
495 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
497 struct fio_net_cmd cmd;
499 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
500 fio_net_cmd_crc(&cmd);
502 return fio_send_data(sk, &cmd, sizeof(cmd));
506 * If 'list' is non-NULL, then allocate and store the sent command for
507 * later verification.
509 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
510 struct flist_head *list)
515 tag = alloc_reply(tag, opcode);
517 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
526 add_reply(tag, list);
531 static int fio_net_queue_quit(void)
533 dprint(FD_NET, "server: sending quit\n");
535 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, 0, SK_F_SIMPLE);
538 int fio_net_send_quit(int sk)
540 dprint(FD_NET, "server: sending quit\n");
542 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
545 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
547 struct cmd_end_pdu epdu;
553 epdu.error = __cpu_to_le32(error);
554 epdu.signal = __cpu_to_le32(signal);
555 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
558 static int fio_net_queue_stop(int error, int signal)
560 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
561 return fio_net_send_ack(NULL, error, signal);
564 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
566 struct fio_fork_item *ffi;
568 ffi = malloc(sizeof(*ffi));
573 flist_add_tail(&ffi->list, list);
576 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
578 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
579 fio_server_add_fork_item(pid, conn_list);
582 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
584 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
585 fio_server_add_fork_item(pid, job_list);
588 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
592 ret = waitpid(ffi->pid, &status, WNOHANG);
594 if (errno == ECHILD) {
595 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
598 log_err("fio: waitpid: %s\n", strerror(errno));
599 } else if (ret == ffi->pid) {
600 if (WIFSIGNALED(status)) {
601 ffi->signal = WTERMSIG(status);
604 if (WIFEXITED(status)) {
605 if (WEXITSTATUS(status))
606 ffi->exitval = WEXITSTATUS(status);
612 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
614 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
617 * Fold STOP and QUIT...
620 fio_net_queue_stop(ffi->exitval, ffi->signal);
621 fio_net_queue_quit();
624 flist_del(&ffi->list);
628 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
630 struct flist_head *entry, *tmp;
631 struct fio_fork_item *ffi;
633 flist_for_each_safe(entry, tmp, list) {
634 ffi = flist_entry(entry, struct fio_fork_item, list);
636 fio_server_check_fork_item(ffi);
639 fio_server_fork_item_done(ffi, stop);
643 static void fio_server_check_jobs(struct flist_head *job_list)
645 fio_server_check_fork_items(job_list, true);
648 static void fio_server_check_conns(struct flist_head *conn_list)
650 fio_server_check_fork_items(conn_list, false);
653 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
655 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
656 void *file_name = pdu->file;
657 struct cmd_start_pdu spdu;
659 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
661 pdu->name_len = le16_to_cpu(pdu->name_len);
662 pdu->client_type = le16_to_cpu(pdu->client_type);
664 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
665 fio_net_queue_quit();
669 spdu.jobs = cpu_to_le32(thread_number);
670 spdu.stat_outputs = cpu_to_le32(stat_number);
671 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
675 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
676 struct fio_net_cmd *cmd)
678 struct backend_data data;
687 fio_server_add_job_pid(job_list, pid);
691 data.key = sk_out_key;
693 //pthread_setspecific(sk_out_key, sk_out);
694 ret = fio_backend(&data);
699 static int handle_job_cmd(struct fio_net_cmd *cmd)
701 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
702 void *buf = pdu->buf;
703 struct cmd_start_pdu spdu;
705 pdu->buf_len = le32_to_cpu(pdu->buf_len);
706 pdu->client_type = le32_to_cpu(pdu->client_type);
708 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
709 fio_net_queue_quit();
713 spdu.jobs = cpu_to_le32(thread_number);
714 spdu.stat_outputs = cpu_to_le32(stat_number);
716 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
720 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
722 void *pdu = cmd->payload;
723 struct cmd_single_line_pdu *cslp;
724 struct cmd_line_pdu *clp;
725 unsigned long offset;
726 struct cmd_start_pdu spdu;
731 clp->lines = le16_to_cpu(clp->lines);
732 clp->client_type = le16_to_cpu(clp->client_type);
733 argv = malloc(clp->lines * sizeof(char *));
734 offset = sizeof(*clp);
736 dprint(FD_NET, "server: %d command line args\n", clp->lines);
738 for (i = 0; i < clp->lines; i++) {
740 argv[i] = (char *) cslp->text;
742 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
743 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
746 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
747 fio_net_queue_quit();
754 spdu.jobs = cpu_to_le32(thread_number);
755 spdu.stat_outputs = cpu_to_le32(stat_number);
757 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
761 static int handle_probe_cmd(struct fio_net_cmd *cmd)
763 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
764 struct cmd_probe_reply_pdu probe;
765 uint64_t tag = cmd->tag;
767 dprint(FD_NET, "server: sending probe reply\n");
769 strcpy(me, (char *) pdu->server);
771 memset(&probe, 0, sizeof(probe));
772 gethostname((char *) probe.hostname, sizeof(probe.hostname));
773 #ifdef CONFIG_BIG_ENDIAN
776 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
779 probe.arch = FIO_ARCH;
780 probe.bpp = sizeof(void *);
781 probe.cpus = __cpu_to_le32(cpus_online());
784 * If the client supports compression and we do too, then enable it
786 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
787 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
794 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
797 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
800 uint64_t tag = cmd->tag;
804 dprint(FD_NET, "server sending status\n");
807 * Fake ETA return if we don't have a local one, otherwise the client
808 * will end up timing out waiting for a response to the ETA request
810 je = get_jobs_eta(true, &size);
813 je = calloc(1, size);
815 je->nr_running = cpu_to_le32(je->nr_running);
816 je->nr_ramp = cpu_to_le32(je->nr_ramp);
817 je->nr_pending = cpu_to_le32(je->nr_pending);
818 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
819 je->files_open = cpu_to_le32(je->files_open);
821 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
822 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
823 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
824 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
825 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
826 je->rate[i] = cpu_to_le32(je->rate[i]);
827 je->iops[i] = cpu_to_le32(je->iops[i]);
830 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
831 je->eta_sec = cpu_to_le64(je->eta_sec);
832 je->nr_threads = cpu_to_le32(je->nr_threads);
833 je->is_pow2 = cpu_to_le32(je->is_pow2);
834 je->unit_base = cpu_to_le32(je->unit_base);
837 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
841 static int send_update_job_reply(uint64_t __tag, int error)
843 uint64_t tag = __tag;
846 pdu_error = __cpu_to_le32(error);
847 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
850 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
852 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
853 struct thread_data *td;
856 tnumber = le32_to_cpu(pdu->thread_number);
858 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
860 if (!tnumber || tnumber > thread_number) {
861 send_update_job_reply(cmd->tag, ENODEV);
865 td = &threads[tnumber - 1];
866 convert_thread_options_to_cpu(&td->o, &pdu->top);
867 send_update_job_reply(cmd->tag, 0);
871 static int handle_trigger_cmd(struct fio_net_cmd *cmd)
873 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
874 char *buf = (char *) pdu->cmd;
875 struct all_io_list *rep;
878 pdu->len = le16_to_cpu(pdu->len);
879 buf[pdu->len] = '\0';
881 rep = get_all_io_list(IO_LIST_ALL, &sz);
883 struct all_io_list state;
885 state.threads = cpu_to_le64((uint64_t) 0);
886 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY);
888 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE);
894 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
895 struct fio_net_cmd *cmd)
899 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
900 fio_server_op(cmd->opcode), cmd->pdu_len,
901 (unsigned long long) cmd->tag);
903 switch (cmd->opcode) {
904 case FIO_NET_CMD_QUIT:
905 fio_terminate_threads(TERMINATE_ALL);
908 case FIO_NET_CMD_EXIT:
911 case FIO_NET_CMD_LOAD_FILE:
912 ret = handle_load_file_cmd(cmd);
914 case FIO_NET_CMD_JOB:
915 ret = handle_job_cmd(cmd);
917 case FIO_NET_CMD_JOBLINE:
918 ret = handle_jobline_cmd(cmd);
920 case FIO_NET_CMD_PROBE:
921 ret = handle_probe_cmd(cmd);
923 case FIO_NET_CMD_SEND_ETA:
924 ret = handle_send_eta_cmd(cmd);
926 case FIO_NET_CMD_RUN:
927 ret = handle_run_cmd(sk_out, job_list, cmd);
929 case FIO_NET_CMD_UPDATE_JOB:
930 ret = handle_update_job_cmd(cmd);
932 case FIO_NET_CMD_VTRIGGER:
933 ret = handle_trigger_cmd(cmd);
935 case FIO_NET_CMD_SENDFILE: {
936 struct cmd_sendfile_reply *in;
937 struct cmd_reply *rep;
939 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
941 in = (struct cmd_sendfile_reply *) cmd->payload;
942 in->size = le32_to_cpu(in->size);
943 in->error = le32_to_cpu(in->error);
946 rep->error = in->error;
949 rep->data = smalloc(in->size);
954 rep->size = in->size;
955 memcpy(rep->data, in->data, in->size);
958 fio_mutex_up(&rep->lock);
962 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
970 * Send a command with a separate PDU, not inlined in the command
972 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
973 off_t size, uint64_t tag, uint32_t flags)
975 struct fio_net_cmd cmd;
978 iov[0].iov_base = (void *) &cmd;
979 iov[0].iov_len = sizeof(cmd);
980 iov[1].iov_base = (void *) buf;
981 iov[1].iov_len = size;
983 __fio_init_net_cmd(&cmd, opcode, size, tag);
984 cmd.flags = __cpu_to_le32(flags);
985 fio_net_cmd_crc_pdu(&cmd, buf);
987 return fio_sendv_data(sk, iov, 2);
990 static void finish_entry(struct sk_entry *entry)
992 if (entry->flags & SK_F_FREE)
994 else if (entry->flags & SK_F_COPY)
1000 static void send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1005 if (!flist_empty(&first->next))
1006 flags = FIO_NET_CMD_F_MORE;
1011 tag = *first->tagptr;
1015 fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf, first->size, tag, flags);
1017 while (!flist_empty(&first->next)) {
1018 struct sk_entry *next;
1020 next = flist_first_entry(&first->next, struct sk_entry, list);
1021 flist_del_init(&next->list);
1022 if (flist_empty(&first->next))
1026 tag = *next->tagptr;
1030 fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf, next->size, tag, flags);
1035 static void handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1037 if (entry->flags & SK_F_VEC)
1038 send_vec_entry(sk_out, entry);
1039 if (entry->flags & SK_F_SIMPLE) {
1043 tag = *entry->tagptr;
1045 fio_net_send_simple_cmd(sk_out->sk, entry->opcode, tag, NULL);
1047 fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf, entry->size, entry->tagptr, NULL);
1049 finish_entry(entry);
1052 static void handle_xmits(struct sk_out *sk_out)
1054 struct sk_entry *entry;
1058 if (flist_empty(&sk_out->list)) {
1063 flist_splice_init(&sk_out->list, &list);
1066 while (!flist_empty(&list)) {
1067 entry = flist_entry(list.next, struct sk_entry, list);
1068 flist_del(&entry->list);
1069 handle_sk_entry(sk_out, entry);
1073 static int handle_connection(struct sk_out *sk_out)
1075 struct fio_net_cmd *cmd = NULL;
1076 FLIST_HEAD(job_list);
1082 while (!exit_backend) {
1083 struct pollfd pfd = {
1092 if (!flist_empty(&job_list))
1095 handle_xmits(sk_out);
1097 ret = poll(&pfd, 1, 0);
1101 log_err("fio: poll: %s\n", strerror(errno));
1104 fio_server_check_jobs(&job_list);
1105 fio_mutex_down_timeout(sk_out->wait, timeout);
1109 if (pfd.revents & POLLIN)
1111 if (pfd.revents & (POLLERR|POLLHUP)) {
1115 } while (!exit_backend);
1117 fio_server_check_jobs(&job_list);
1122 cmd = fio_net_recv_cmd(sk_out->sk);
1128 ret = handle_command(sk_out, &job_list, cmd);
1139 handle_xmits(sk_out);
1145 /* get the address on this host bound by the input socket,
1146 * whether it is ipv6 or ipv4 */
1148 int get_my_addr_str(int sk)
1150 struct sockaddr_in6 myaddr6 = { 0, };
1151 struct sockaddr_in myaddr4 = { 0, };
1152 struct sockaddr *sockaddr_p;
1158 len = sizeof(myaddr6);
1159 sockaddr_p = (struct sockaddr * )&myaddr6;
1160 net_addr = (char * )&myaddr6.sin6_addr;
1162 len = sizeof(myaddr4);
1163 sockaddr_p = (struct sockaddr * )&myaddr4;
1164 net_addr = (char * )&myaddr4.sin_addr;
1167 ret = getsockname(sk, sockaddr_p, &len);
1169 log_err("fio: getsockaddr: %s\n", strerror(errno));
1173 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1174 log_err("inet_ntop: failed to convert addr to string\n");
1178 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1182 static int accept_loop(int listen_sk)
1184 struct sockaddr_in addr;
1185 struct sockaddr_in6 addr6;
1186 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1188 int ret = 0, sk, exitval = 0;
1189 struct sk_out *sk_out;
1190 FLIST_HEAD(conn_list);
1192 dprint(FD_NET, "server enter accept loop\n");
1194 fio_set_fd_nonblocking(listen_sk, "server");
1196 sk_out = smalloc(sizeof(*sk_out));
1197 INIT_FLIST_HEAD(&sk_out->list);
1198 sk_out->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
1199 sk_out->wait = fio_mutex_init(FIO_MUTEX_LOCKED);
1201 pthread_setspecific(sk_out_key, sk_out);
1203 while (!exit_backend) {
1209 pfd.events = POLLIN;
1213 if (!flist_empty(&conn_list))
1216 ret = poll(&pfd, 1, timeout);
1220 log_err("fio: poll: %s\n", strerror(errno));
1223 fio_server_check_conns(&conn_list);
1227 if (pfd.revents & POLLIN)
1229 } while (!exit_backend);
1231 fio_server_check_conns(&conn_list);
1233 if (exit_backend || ret < 0)
1237 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1239 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1242 log_err("fio: accept: %s\n", strerror(errno));
1247 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1249 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1251 dprint(FD_NET, "server: connect from %s\n", from);
1258 fio_server_add_conn_pid(&conn_list, pid);
1259 pthread_setspecific(sk_out_key, sk_out);
1264 get_my_addr_str(sk); /* if error, it's already logged, non-fatal */
1265 handle_connection(sk_out);
1269 fio_mutex_remove(sk_out->lock);
1270 fio_mutex_remove(sk_out->wait);
1272 pthread_setspecific(sk_out_key, NULL);
1278 int fio_server_text_output(int level, const char *buf, size_t len)
1280 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1281 struct cmd_text_pdu *pdu;
1285 if (!sk_out || sk_out->sk == -1)
1288 tlen = sizeof(*pdu) + len;
1291 pdu->level = __cpu_to_le32(level);
1292 pdu->buf_len = __cpu_to_le32(len);
1294 gettimeofday(&tv, NULL);
1295 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1296 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1298 memcpy(pdu->buf, buf, len);
1300 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1305 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1307 dst->max_val = cpu_to_le64(src->max_val);
1308 dst->min_val = cpu_to_le64(src->min_val);
1309 dst->samples = cpu_to_le64(src->samples);
1312 * Encode to IEEE 754 for network transfer
1314 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1315 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1318 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1322 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1323 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1324 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1325 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1326 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1327 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
1328 dst->agg[i] = cpu_to_le64(src->agg[i]);
1331 dst->kb_base = cpu_to_le32(src->kb_base);
1332 dst->unit_base = cpu_to_le32(src->unit_base);
1333 dst->groupid = cpu_to_le32(src->groupid);
1334 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1338 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1339 * into a single payload.
1341 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1343 struct cmd_ts_pdu p;
1346 dprint(FD_NET, "server sending end stats\n");
1348 memset(&p, 0, sizeof(p));
1350 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1351 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1352 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1354 p.ts.error = cpu_to_le32(ts->error);
1355 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1356 p.ts.groupid = cpu_to_le32(ts->groupid);
1357 p.ts.pid = cpu_to_le32(ts->pid);
1358 p.ts.members = cpu_to_le32(ts->members);
1359 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1361 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1362 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1363 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1364 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1365 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1368 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1369 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1370 p.ts.ctx = cpu_to_le64(ts->ctx);
1371 p.ts.minf = cpu_to_le64(ts->minf);
1372 p.ts.majf = cpu_to_le64(ts->majf);
1373 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1374 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1376 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1377 fio_fp64_t *src = &ts->percentile_list[i];
1378 fio_fp64_t *dst = &p.ts.percentile_list[i];
1380 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1383 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1384 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1385 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1386 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1389 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1390 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1391 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1394 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1395 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1396 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1398 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1399 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1400 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1401 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1404 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1405 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1407 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1408 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1409 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1412 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1413 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1414 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1415 p.ts.first_error = cpu_to_le32(ts->first_error);
1416 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1417 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1419 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1420 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1421 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1422 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1424 p.ts.nr_block_infos = le64_to_cpu(ts->nr_block_infos);
1425 for (i = 0; i < p.ts.nr_block_infos; i++)
1426 p.ts.block_infos[i] = le32_to_cpu(ts->block_infos[i]);
1428 convert_gs(&p.rs, rs);
1430 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1433 void fio_server_send_gs(struct group_run_stats *rs)
1435 struct group_run_stats gs;
1437 dprint(FD_NET, "server sending group run stats\n");
1439 convert_gs(&gs, rs);
1440 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1443 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1447 for (i = 0; i < 2; i++) {
1448 dst->ios[i] = cpu_to_le64(src->ios[i]);
1449 dst->merges[i] = cpu_to_le64(src->merges[i]);
1450 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1451 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1454 dst->io_ticks = cpu_to_le64(src->io_ticks);
1455 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1456 dst->slavecount = cpu_to_le32(src->slavecount);
1457 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1460 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1464 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1465 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1467 for (i = 0; i < 2; i++) {
1468 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1469 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1470 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1471 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1474 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1475 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1476 dst->s.msec = cpu_to_le64(src->s.msec);
1479 void fio_server_send_du(void)
1481 struct disk_util *du;
1482 struct flist_head *entry;
1483 struct cmd_du_pdu pdu;
1485 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1487 memset(&pdu, 0, sizeof(pdu));
1489 flist_for_each(entry, &disk_list) {
1490 du = flist_entry(entry, struct disk_util, list);
1492 convert_dus(&pdu.dus, &du->dus);
1493 convert_agg(&pdu.agg, &du->agg);
1495 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1499 static int fio_send_iolog_gz(struct sk_entry *first, struct io_log *log)
1503 struct sk_entry *entry;
1508 * Dirty - since the log is potentially huge, compress it into
1509 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1510 * side defragment it.
1512 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1514 stream.zalloc = Z_NULL;
1515 stream.zfree = Z_NULL;
1516 stream.opaque = Z_NULL;
1518 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
1523 stream.next_in = (void *) log->log;
1524 stream.avail_in = log->nr_samples * log_entry_sz(log);
1527 unsigned int this_len;
1529 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1530 stream.next_out = out_pdu;
1531 ret = deflate(&stream, Z_FINISH);
1532 /* may be Z_OK, or Z_STREAM_END */
1536 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1538 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1539 NULL, SK_F_FREE | SK_F_VEC);
1540 flist_add_tail(&entry->list, &first->next);
1541 } while (stream.avail_in);
1544 deflateEnd(&stream);
1551 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1553 struct cmd_iolog_pdu pdu;
1554 struct sk_entry *first;
1557 pdu.nr_samples = cpu_to_le64(log->nr_samples);
1558 pdu.thread_number = cpu_to_le32(td->thread_number);
1559 pdu.log_type = cpu_to_le32(log->log_type);
1560 pdu.compressed = cpu_to_le32(use_zlib);
1562 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1563 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1565 for (i = 0; i < log->nr_samples; i++) {
1566 struct io_sample *s = get_sample(log, i);
1568 s->time = cpu_to_le64(s->time);
1569 s->val = cpu_to_le64(s->val);
1570 s->__ddir = cpu_to_le32(s->__ddir);
1571 s->bs = cpu_to_le32(s->bs);
1573 if (log->log_offset) {
1574 struct io_sample_offset *so = (void *) s;
1576 so->offset = cpu_to_le64(so->offset);
1581 * Assemble header entry first
1583 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_COPY | SK_F_VEC);
1586 * Now append actual log entries. Compress if we can, otherwise just
1587 * plain text output.
1590 ret = fio_send_iolog_gz(first, log);
1592 struct sk_entry *entry;
1594 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, log->log,
1595 log->nr_samples * log_entry_sz(log),
1596 NULL, SK_F_FREE | SK_F_VEC);
1597 flist_add_tail(&entry->list, &first->next);
1603 void fio_server_send_add_job(struct thread_data *td)
1605 struct cmd_add_job_pdu pdu;
1607 memset(&pdu, 0, sizeof(pdu));
1608 pdu.thread_number = cpu_to_le32(td->thread_number);
1609 pdu.groupid = cpu_to_le32(td->groupid);
1610 convert_thread_options_to_net(&pdu.top, &td->o);
1612 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1615 void fio_server_send_start(struct thread_data *td)
1617 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1619 assert(sk_out->sk != -1);
1621 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, 0, SK_F_SIMPLE);
1624 int fio_server_get_verify_state(const char *name, int threadnumber,
1625 void **datap, int *version)
1627 struct thread_io_list *s;
1628 struct cmd_sendfile out;
1629 struct cmd_reply *rep;
1633 dprint(FD_NET, "server: request verify state\n");
1635 rep = smalloc(sizeof(*rep));
1637 log_err("fio: smalloc pool too small\n");
1641 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1645 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1647 tag = (uint64_t) (uintptr_t) rep;
1648 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag, SK_F_COPY);
1651 * Wait for the backend to receive the reply
1653 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
1654 log_err("fio: timed out waiting for reply\n");
1659 log_err("fio: failure on receiving state file: %s\n", strerror(rep->error));
1663 fio_net_queue_quit();
1668 * The format is verify_state_hdr, then thread_io_list. Verify
1669 * the header, and the thread_io_list checksum
1671 s = rep->data + sizeof(struct verify_state_hdr);
1672 if (verify_state_hdr(rep->data, s, version))
1676 * Don't need the header from now, copy just the thread_io_list
1678 rep->size -= sizeof(struct verify_state_hdr);
1679 data = malloc(rep->size);
1680 memcpy(data, s, rep->size);
1684 __fio_mutex_remove(&rep->lock);
1689 static int fio_init_server_ip(void)
1691 struct sockaddr *addr;
1698 sk = socket(AF_INET6, SOCK_STREAM, 0);
1700 sk = socket(AF_INET, SOCK_STREAM, 0);
1703 log_err("fio: socket: %s\n", strerror(errno));
1708 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1709 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
1714 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
1715 log_err("fio: setsockopt(REUSEPORT): %s\n", strerror(errno));
1722 const void *src = &saddr_in6.sin6_addr;
1724 addr = (struct sockaddr *) &saddr_in6;
1725 socklen = sizeof(saddr_in6);
1726 saddr_in6.sin6_family = AF_INET6;
1727 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
1729 const void *src = &saddr_in.sin_addr;
1731 addr = (struct sockaddr *) &saddr_in;
1732 socklen = sizeof(saddr_in);
1733 saddr_in.sin_family = AF_INET;
1734 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
1737 if (bind(sk, addr, socklen) < 0) {
1738 log_err("fio: bind: %s\n", strerror(errno));
1739 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
1747 static int fio_init_server_sock(void)
1749 struct sockaddr_un addr;
1754 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1756 log_err("fio: socket: %s\n", strerror(errno));
1762 memset(&addr, 0, sizeof(addr));
1763 addr.sun_family = AF_UNIX;
1764 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
1766 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1768 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1769 log_err("fio: bind: %s\n", strerror(errno));
1778 static int fio_init_server_connection(void)
1783 dprint(FD_NET, "starting server\n");
1786 sk = fio_init_server_ip();
1788 sk = fio_init_server_sock();
1793 memset(bind_str, 0, sizeof(bind_str));
1802 src = &saddr_in6.sin6_addr;
1805 src = &saddr_in.sin_addr;
1808 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1810 sprintf(port, ",%u", fio_net_port);
1814 strncpy(bind_str, port, sizeof(bind_str) - 1);
1816 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
1818 log_info("fio: server listening on %s\n", bind_str);
1820 if (listen(sk, 0) < 0) {
1821 log_err("fio: listen: %s\n", strerror(errno));
1829 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
1830 struct in6_addr *inp6)
1836 ret = inet_pton(AF_INET6, host, inp6);
1838 ret = inet_pton(AF_INET, host, inp);
1841 struct addrinfo hints, *res;
1843 memset(&hints, 0, sizeof(hints));
1844 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
1845 hints.ai_socktype = SOCK_STREAM;
1847 ret = getaddrinfo(host, NULL, &hints, &res);
1849 log_err("fio: failed to resolve <%s> (%s)\n", host,
1855 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
1857 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
1867 * Parse a host/ip/port string. Reads from 'str'.
1872 * *ptr is the host, *port is the port, inp is the destination.
1874 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1875 * For local domain sockets:
1876 * *ptr is the filename, *is_sock is 1.
1878 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
1879 int *port, struct in_addr *inp,
1880 struct in6_addr *inp6, int *ipv6)
1882 const char *host = str;
1888 *port = fio_net_port;
1891 if (!strncmp(str, "sock:", 5)) {
1892 *ptr = strdup(str + 5);
1899 * Is it ip:<ip or host>:port
1901 if (!strncmp(host, "ip:", 3))
1903 else if (!strncmp(host, "ip4:", 4))
1905 else if (!strncmp(host, "ip6:", 4)) {
1908 } else if (host[0] == ':') {
1909 /* String is :port */
1912 if (!lport || lport > 65535) {
1913 log_err("fio: bad server port %u\n", lport);
1916 /* no hostname given, we are done */
1922 * If no port seen yet, check if there's a last ',' at the end
1925 portp = strchr(host, ',');
1929 lport = atoi(portp);
1930 if (!lport || lport > 65535) {
1931 log_err("fio: bad server port %u\n", lport);
1943 *ptr = strdup(host);
1945 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
1952 *port = fio_net_port;
1958 * Server arg should be one of:
1960 * sock:/path/to/socket
1964 * Where sock uses unix domain sockets, and ip binds the server to
1965 * a specific interface. If no arguments are given to the server, it
1966 * uses IP and binds to 0.0.0.0.
1969 static int fio_handle_server_arg(void)
1971 int port = fio_net_port;
1972 int is_sock, ret = 0;
1974 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1976 if (!fio_server_arg)
1979 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
1980 &port, &saddr_in.sin_addr,
1981 &saddr_in6.sin6_addr, &use_ipv6);
1983 if (!is_sock && bind_sock) {
1989 fio_net_port = port;
1990 saddr_in.sin_port = htons(port);
1991 saddr_in6.sin6_port = htons(port);
1995 static void sig_int(int sig)
2001 static void set_sig_handlers(void)
2003 struct sigaction act;
2005 memset(&act, 0, sizeof(act));
2006 act.sa_handler = sig_int;
2007 act.sa_flags = SA_RESTART;
2008 sigaction(SIGINT, &act, NULL);
2011 static int fio_server(void)
2015 dprint(FD_NET, "starting server\n");
2017 if (fio_handle_server_arg())
2020 sk = fio_init_server_connection();
2026 if (pthread_key_create(&sk_out_key, NULL))
2027 log_err("fio: can't create sk_out backend key\n");
2029 ret = accept_loop(sk);
2033 if (fio_server_arg) {
2034 free(fio_server_arg);
2035 fio_server_arg = NULL;
2043 void fio_server_got_signal(int signal)
2045 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2049 if (signal == SIGPIPE)
2052 log_info("\nfio: terminating on signal %d\n", signal);
2057 static int check_existing_pidfile(const char *pidfile)
2064 if (stat(pidfile, &sb))
2067 f = fopen(pidfile, "r");
2071 if (fread(buf, sb.st_size, 1, f) <= 0) {
2078 if (kill(pid, SIGCONT) < 0)
2079 return errno != ESRCH;
2084 static int write_pid(pid_t pid, const char *pidfile)
2088 fpid = fopen(pidfile, "w");
2090 log_err("fio: failed opening pid file %s\n", pidfile);
2094 fprintf(fpid, "%u\n", (unsigned int) pid);
2100 * If pidfile is specified, background us.
2102 int fio_start_server(char *pidfile)
2109 WSAStartup(MAKEWORD(2, 2), &wsd);
2113 return fio_server();
2115 if (check_existing_pidfile(pidfile)) {
2116 log_err("fio: pidfile %s exists and server appears alive\n",
2124 log_err("fio: failed server fork: %s", strerror(errno));
2128 ret = write_pid(pid, pidfile);
2134 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2136 close(STDIN_FILENO);
2137 close(STDOUT_FILENO);
2138 close(STDERR_FILENO);
2150 void fio_server_set_arg(const char *arg)
2152 fio_server_arg = strdup(arg);