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] = {
119 static void sk_lock(struct sk_out *sk_out)
121 fio_mutex_down(&sk_out->lock);
124 static void sk_unlock(struct sk_out *sk_out)
126 fio_mutex_up(&sk_out->lock);
129 void sk_out_assign(struct sk_out *sk_out)
137 pthread_setspecific(sk_out_key, sk_out);
140 static void sk_out_free(struct sk_out *sk_out)
142 __fio_mutex_remove(&sk_out->lock);
143 __fio_mutex_remove(&sk_out->wait);
144 __fio_mutex_remove(&sk_out->xmit);
148 static int __sk_out_drop(struct sk_out *sk_out)
154 assert(sk_out->refs != 0);
155 refs = --sk_out->refs;
160 pthread_setspecific(sk_out_key, NULL);
168 void sk_out_drop(void)
170 struct sk_out *sk_out;
172 sk_out = pthread_getspecific(sk_out_key);
173 __sk_out_drop(sk_out);
176 static void __fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
177 uint32_t pdu_len, uint64_t tag)
179 memset(cmd, 0, sizeof(*cmd));
181 cmd->version = __cpu_to_le16(FIO_SERVER_VER);
182 cmd->opcode = cpu_to_le16(opcode);
183 cmd->tag = cpu_to_le64(tag);
184 cmd->pdu_len = cpu_to_le32(pdu_len);
188 static void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode,
189 const void *pdu, uint32_t pdu_len, uint64_t tag)
191 __fio_init_net_cmd(cmd, opcode, pdu_len, tag);
194 memcpy(&cmd->payload, pdu, pdu_len);
197 const char *fio_server_op(unsigned int op)
201 if (op < FIO_NET_CMD_NR)
202 return fio_server_ops[op];
204 sprintf(buf, "UNKNOWN/%d", op);
208 static ssize_t iov_total_len(const struct iovec *iov, int count)
220 static int fio_sendv_data(int sk, struct iovec *iov, int count)
222 ssize_t total_len = iov_total_len(iov, count);
226 ret = writev(sk, iov, count);
233 if (ret >= iov->iov_len) {
238 iov->iov_base += ret;
244 else if (errno == EAGAIN || errno == EINTR)
248 } while (!exit_backend);
256 static int fio_send_data(int sk, const void *p, unsigned int len)
258 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
260 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
262 return fio_sendv_data(sk, &iov, 1);
265 static int fio_recv_data(int sk, void *p, unsigned int len)
268 int ret = recv(sk, p, len, MSG_WAITALL);
278 else if (errno == EAGAIN || errno == EINTR)
282 } while (!exit_backend);
290 static int verify_convert_cmd(struct fio_net_cmd *cmd)
294 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
295 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
297 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
298 if (crc != cmd->cmd_crc16) {
299 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
300 cmd->cmd_crc16, crc);
304 cmd->version = le16_to_cpu(cmd->version);
305 cmd->opcode = le16_to_cpu(cmd->opcode);
306 cmd->flags = le32_to_cpu(cmd->flags);
307 cmd->tag = le64_to_cpu(cmd->tag);
308 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
310 switch (cmd->version) {
314 log_err("fio: bad server cmd version %d\n", cmd->version);
318 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
319 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
327 * Read (and defragment, if necessary) incoming commands
329 struct fio_net_cmd *fio_net_recv_cmd(int sk)
331 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
332 size_t cmd_size = 0, pdu_offset = 0;
338 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
342 /* We have a command, verify it and swap if need be */
343 ret = verify_convert_cmd(&cmd);
348 /* if this is text, add room for \0 at the end */
349 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
352 cmd_size += cmd.pdu_len;
354 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
355 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
360 tmp = realloc(cmdret, cmd_size);
362 log_err("fio: server failed allocating cmd\n");
369 memcpy(cmdret, &cmd, sizeof(cmd));
370 else if (cmdret->opcode != cmd.opcode) {
371 log_err("fio: fragment opcode mismatch (%d != %d)\n",
372 cmdret->opcode, cmd.opcode);
380 /* There's payload, get it */
381 pdu = (void *) cmdret->payload + pdu_offset;
382 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
386 /* Verify payload crc */
387 crc = fio_crc16(pdu, cmd.pdu_len);
388 if (crc != cmd.pdu_crc16) {
389 log_err("fio: server bad crc on payload ");
390 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
395 pdu_offset += cmd.pdu_len;
397 cmdret->pdu_len += cmd.pdu_len;
399 } while (cmd.flags & FIO_NET_CMD_F_MORE);
405 /* zero-terminate text input */
406 if (cmdret->pdu_len) {
407 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
408 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
409 char *buf = (char *) __pdu->buf;
411 buf[__pdu->buf_len] = '\0';
412 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
413 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
414 char *buf = (char *) __pdu->buf;
415 int len = le32_to_cpu(__pdu->buf_len);
421 /* frag flag is internal */
422 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
428 static void add_reply(uint64_t tag, struct flist_head *list)
430 struct fio_net_cmd_reply *reply;
432 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
433 flist_add_tail(&reply->list, list);
436 static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
438 struct fio_net_cmd_reply *reply;
440 reply = calloc(1, sizeof(*reply));
441 INIT_FLIST_HEAD(&reply->list);
442 fio_gettime(&reply->tv, NULL);
443 reply->saved_tag = tag;
444 reply->opcode = opcode;
446 return (uintptr_t) reply;
449 static void free_reply(uint64_t tag)
451 struct fio_net_cmd_reply *reply;
453 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
457 static void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
461 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
463 pdu_len = le32_to_cpu(cmd->pdu_len);
464 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
467 static void fio_net_cmd_crc(struct fio_net_cmd *cmd)
469 fio_net_cmd_crc_pdu(cmd, cmd->payload);
472 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
473 uint64_t *tagptr, struct flist_head *list)
475 struct fio_net_cmd *cmd = NULL;
476 size_t this_len, cur_len = 0;
482 tag = *tagptr = alloc_reply(*tagptr, opcode);
484 tag = tagptr ? *tagptr : 0;
488 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
489 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
491 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
495 cur_len = sizeof(*cmd) + this_len;
496 cmd = malloc(cur_len);
499 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
502 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
504 fio_net_cmd_crc(cmd);
506 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
509 } while (!ret && size);
515 add_reply(tag, list);
524 static struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf,
525 size_t size, uint64_t *tagptr,
528 struct sk_entry *entry;
530 entry = smalloc(sizeof(*entry));
531 INIT_FLIST_HEAD(&entry->next);
532 entry->opcode = opcode;
533 if (flags & SK_F_COPY) {
534 entry->buf = smalloc(size);
535 memcpy(entry->buf, buf, size);
541 entry->tag = *tagptr;
544 entry->flags = flags;
548 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry);
550 static void fio_net_queue_entry(struct sk_entry *entry)
552 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
554 if (entry->flags & SK_F_INLINE)
555 handle_sk_entry(sk_out, entry);
558 flist_add_tail(&entry->list, &sk_out->list);
561 fio_mutex_up(&sk_out->wait);
565 static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
566 uint64_t *tagptr, int flags)
568 struct sk_entry *entry;
570 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
571 fio_net_queue_entry(entry);
575 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
577 struct fio_net_cmd cmd;
579 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
580 fio_net_cmd_crc(&cmd);
582 return fio_send_data(sk, &cmd, sizeof(cmd));
586 * If 'list' is non-NULL, then allocate and store the sent command for
587 * later verification.
589 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
590 struct flist_head *list)
595 tag = alloc_reply(tag, opcode);
597 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
606 add_reply(tag, list);
611 static int fio_net_queue_quit(void)
613 dprint(FD_NET, "server: sending quit\n");
615 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, 0, SK_F_SIMPLE);
618 int fio_net_send_quit(int sk)
620 dprint(FD_NET, "server: sending quit\n");
622 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
625 static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
627 struct cmd_end_pdu epdu;
633 epdu.error = __cpu_to_le32(error);
634 epdu.signal = __cpu_to_le32(signal);
635 return fio_net_queue_cmd(FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, SK_F_COPY);
638 static int fio_net_queue_stop(int error, int signal)
640 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
641 return fio_net_send_ack(NULL, error, signal);
644 static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
646 struct fio_fork_item *ffi;
648 ffi = malloc(sizeof(*ffi));
653 flist_add_tail(&ffi->list, list);
656 static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
658 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
659 fio_server_add_fork_item(pid, conn_list);
662 static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
664 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
665 fio_server_add_fork_item(pid, job_list);
668 static void fio_server_check_fork_item(struct fio_fork_item *ffi)
672 ret = waitpid(ffi->pid, &status, WNOHANG);
674 if (errno == ECHILD) {
675 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
678 log_err("fio: waitpid: %s\n", strerror(errno));
679 } else if (ret == ffi->pid) {
680 if (WIFSIGNALED(status)) {
681 ffi->signal = WTERMSIG(status);
684 if (WIFEXITED(status)) {
685 if (WEXITSTATUS(status))
686 ffi->exitval = WEXITSTATUS(status);
692 static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
694 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
697 * Fold STOP and QUIT...
700 fio_net_queue_stop(ffi->exitval, ffi->signal);
701 fio_net_queue_quit();
704 flist_del(&ffi->list);
708 static void fio_server_check_fork_items(struct flist_head *list, bool stop)
710 struct flist_head *entry, *tmp;
711 struct fio_fork_item *ffi;
713 flist_for_each_safe(entry, tmp, list) {
714 ffi = flist_entry(entry, struct fio_fork_item, list);
716 fio_server_check_fork_item(ffi);
719 fio_server_fork_item_done(ffi, stop);
723 static void fio_server_check_jobs(struct flist_head *job_list)
725 fio_server_check_fork_items(job_list, true);
728 static void fio_server_check_conns(struct flist_head *conn_list)
730 fio_server_check_fork_items(conn_list, false);
733 static int handle_load_file_cmd(struct fio_net_cmd *cmd)
735 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
736 void *file_name = pdu->file;
737 struct cmd_start_pdu spdu;
739 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
741 pdu->name_len = le16_to_cpu(pdu->name_len);
742 pdu->client_type = le16_to_cpu(pdu->client_type);
744 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
745 fio_net_queue_quit();
749 spdu.jobs = cpu_to_le32(thread_number);
750 spdu.stat_outputs = cpu_to_le32(stat_number);
751 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
755 static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
756 struct fio_net_cmd *cmd)
761 sk_out_assign(sk_out);
768 fio_server_add_job_pid(job_list, pid);
772 ret = fio_backend(sk_out);
778 static int handle_job_cmd(struct fio_net_cmd *cmd)
780 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
781 void *buf = pdu->buf;
782 struct cmd_start_pdu spdu;
784 pdu->buf_len = le32_to_cpu(pdu->buf_len);
785 pdu->client_type = le32_to_cpu(pdu->client_type);
787 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
788 fio_net_queue_quit();
792 spdu.jobs = cpu_to_le32(thread_number);
793 spdu.stat_outputs = cpu_to_le32(stat_number);
795 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
799 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
801 void *pdu = cmd->payload;
802 struct cmd_single_line_pdu *cslp;
803 struct cmd_line_pdu *clp;
804 unsigned long offset;
805 struct cmd_start_pdu spdu;
810 clp->lines = le16_to_cpu(clp->lines);
811 clp->client_type = le16_to_cpu(clp->client_type);
812 argv = malloc(clp->lines * sizeof(char *));
813 offset = sizeof(*clp);
815 dprint(FD_NET, "server: %d command line args\n", clp->lines);
817 for (i = 0; i < clp->lines; i++) {
819 argv[i] = (char *) cslp->text;
821 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
822 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
825 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
826 fio_net_queue_quit();
833 spdu.jobs = cpu_to_le32(thread_number);
834 spdu.stat_outputs = cpu_to_le32(stat_number);
836 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
840 static int handle_probe_cmd(struct fio_net_cmd *cmd)
842 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
843 struct cmd_probe_reply_pdu probe;
844 uint64_t tag = cmd->tag;
846 dprint(FD_NET, "server: sending probe reply\n");
848 strcpy(me, (char *) pdu->server);
850 memset(&probe, 0, sizeof(probe));
851 gethostname((char *) probe.hostname, sizeof(probe.hostname));
852 #ifdef CONFIG_BIG_ENDIAN
855 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
858 probe.arch = FIO_ARCH;
859 probe.bpp = sizeof(void *);
860 probe.cpus = __cpu_to_le32(cpus_online());
863 * If the client supports compression and we do too, then enable it
865 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
866 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
873 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
876 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
879 uint64_t tag = cmd->tag;
883 dprint(FD_NET, "server sending status\n");
886 * Fake ETA return if we don't have a local one, otherwise the client
887 * will end up timing out waiting for a response to the ETA request
889 je = get_jobs_eta(true, &size);
892 je = calloc(1, size);
894 je->nr_running = cpu_to_le32(je->nr_running);
895 je->nr_ramp = cpu_to_le32(je->nr_ramp);
896 je->nr_pending = cpu_to_le32(je->nr_pending);
897 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
898 je->files_open = cpu_to_le32(je->files_open);
900 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
901 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
902 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
903 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
904 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
905 je->rate[i] = cpu_to_le32(je->rate[i]);
906 je->iops[i] = cpu_to_le32(je->iops[i]);
909 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
910 je->eta_sec = cpu_to_le64(je->eta_sec);
911 je->nr_threads = cpu_to_le32(je->nr_threads);
912 je->is_pow2 = cpu_to_le32(je->is_pow2);
913 je->unit_base = cpu_to_le32(je->unit_base);
916 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
920 static int send_update_job_reply(uint64_t __tag, int error)
922 uint64_t tag = __tag;
925 pdu_error = __cpu_to_le32(error);
926 return fio_net_queue_cmd(FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, SK_F_COPY);
929 static int handle_update_job_cmd(struct fio_net_cmd *cmd)
931 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
932 struct thread_data *td;
935 tnumber = le32_to_cpu(pdu->thread_number);
937 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
939 if (!tnumber || tnumber > thread_number) {
940 send_update_job_reply(cmd->tag, ENODEV);
944 td = &threads[tnumber - 1];
945 convert_thread_options_to_cpu(&td->o, &pdu->top);
946 send_update_job_reply(cmd->tag, 0);
950 static int handle_trigger_cmd(struct fio_net_cmd *cmd)
952 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
953 char *buf = (char *) pdu->cmd;
954 struct all_io_list *rep;
957 pdu->len = le16_to_cpu(pdu->len);
958 buf[pdu->len] = '\0';
960 rep = get_all_io_list(IO_LIST_ALL, &sz);
962 struct all_io_list state;
964 state.threads = cpu_to_le64((uint64_t) 0);
965 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, SK_F_COPY);
967 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE);
973 static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
974 struct fio_net_cmd *cmd)
978 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
979 fio_server_op(cmd->opcode), cmd->pdu_len,
980 (unsigned long long) cmd->tag);
982 switch (cmd->opcode) {
983 case FIO_NET_CMD_QUIT:
984 fio_terminate_threads(TERMINATE_ALL);
987 case FIO_NET_CMD_EXIT:
990 case FIO_NET_CMD_LOAD_FILE:
991 ret = handle_load_file_cmd(cmd);
993 case FIO_NET_CMD_JOB:
994 ret = handle_job_cmd(cmd);
996 case FIO_NET_CMD_JOBLINE:
997 ret = handle_jobline_cmd(cmd);
999 case FIO_NET_CMD_PROBE:
1000 ret = handle_probe_cmd(cmd);
1002 case FIO_NET_CMD_SEND_ETA:
1003 ret = handle_send_eta_cmd(cmd);
1005 case FIO_NET_CMD_RUN:
1006 ret = handle_run_cmd(sk_out, job_list, cmd);
1008 case FIO_NET_CMD_UPDATE_JOB:
1009 ret = handle_update_job_cmd(cmd);
1011 case FIO_NET_CMD_VTRIGGER:
1012 ret = handle_trigger_cmd(cmd);
1014 case FIO_NET_CMD_SENDFILE: {
1015 struct cmd_sendfile_reply *in;
1016 struct cmd_reply *rep;
1018 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
1020 in = (struct cmd_sendfile_reply *) cmd->payload;
1021 in->size = le32_to_cpu(in->size);
1022 in->error = le32_to_cpu(in->error);
1025 rep->error = in->error;
1028 rep->data = smalloc(in->size);
1031 rep->error = ENOMEM;
1033 rep->size = in->size;
1034 memcpy(rep->data, in->data, in->size);
1037 fio_mutex_up(&rep->lock);
1041 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
1049 * Send a command with a separate PDU, not inlined in the command
1051 static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1052 off_t size, uint64_t tag, uint32_t flags)
1054 struct fio_net_cmd cmd;
1055 struct iovec iov[2];
1057 iov[0].iov_base = (void *) &cmd;
1058 iov[0].iov_len = sizeof(cmd);
1059 iov[1].iov_base = (void *) buf;
1060 iov[1].iov_len = size;
1062 __fio_init_net_cmd(&cmd, opcode, size, tag);
1063 cmd.flags = __cpu_to_le32(flags);
1064 fio_net_cmd_crc_pdu(&cmd, buf);
1066 return fio_sendv_data(sk, iov, 2);
1069 static void finish_entry(struct sk_entry *entry)
1071 if (entry->flags & SK_F_FREE)
1073 else if (entry->flags & SK_F_COPY)
1079 static void entry_set_flags(struct sk_entry *entry, struct flist_head *list,
1080 unsigned int *flags)
1082 if (!flist_empty(list))
1083 *flags = FIO_NET_CMD_F_MORE;
1088 static int send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1093 entry_set_flags(first, &first->next, &flags);
1095 ret = fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf,
1096 first->size, first->tag, flags);
1098 while (!flist_empty(&first->next)) {
1099 struct sk_entry *next;
1101 next = flist_first_entry(&first->next, struct sk_entry, list);
1102 flist_del_init(&next->list);
1104 entry_set_flags(next, &first->next, &flags);
1106 ret += fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf,
1107 next->size, next->tag, flags);
1114 static int handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1118 fio_mutex_down(&sk_out->xmit);
1120 if (entry->flags & SK_F_VEC)
1121 ret = send_vec_entry(sk_out, entry);
1122 else if (entry->flags & SK_F_SIMPLE) {
1123 ret = fio_net_send_simple_cmd(sk_out->sk, entry->opcode,
1126 ret = fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf,
1127 entry->size, &entry->tag, NULL);
1130 fio_mutex_up(&sk_out->xmit);
1133 log_err("fio: failed handling cmd %s\n", fio_server_op(entry->opcode));
1135 finish_entry(entry);
1139 static int handle_xmits(struct sk_out *sk_out)
1141 struct sk_entry *entry;
1146 if (flist_empty(&sk_out->list)) {
1151 flist_splice_init(&sk_out->list, &list);
1154 while (!flist_empty(&list)) {
1155 entry = flist_entry(list.next, struct sk_entry, list);
1156 flist_del(&entry->list);
1157 ret += handle_sk_entry(sk_out, entry);
1163 static int handle_connection(struct sk_out *sk_out)
1165 struct fio_net_cmd *cmd = NULL;
1166 FLIST_HEAD(job_list);
1172 while (!exit_backend) {
1173 struct pollfd pfd = {
1182 if (!flist_empty(&job_list))
1185 handle_xmits(sk_out);
1187 ret = poll(&pfd, 1, 0);
1191 log_err("fio: poll: %s\n", strerror(errno));
1194 fio_server_check_jobs(&job_list);
1195 fio_mutex_down_timeout(&sk_out->wait, timeout);
1199 if (pfd.revents & POLLIN)
1201 if (pfd.revents & (POLLERR|POLLHUP)) {
1205 } while (!exit_backend);
1207 fio_server_check_jobs(&job_list);
1212 cmd = fio_net_recv_cmd(sk_out->sk);
1218 ret = handle_command(sk_out, &job_list, cmd);
1229 handle_xmits(sk_out);
1233 __sk_out_drop(sk_out);
1237 /* get the address on this host bound by the input socket,
1238 * whether it is ipv6 or ipv4 */
1240 static int get_my_addr_str(int sk)
1242 struct sockaddr_in6 myaddr6 = { 0, };
1243 struct sockaddr_in myaddr4 = { 0, };
1244 struct sockaddr *sockaddr_p;
1250 len = sizeof(myaddr6);
1251 sockaddr_p = (struct sockaddr * )&myaddr6;
1252 net_addr = (char * )&myaddr6.sin6_addr;
1254 len = sizeof(myaddr4);
1255 sockaddr_p = (struct sockaddr * )&myaddr4;
1256 net_addr = (char * )&myaddr4.sin_addr;
1259 ret = getsockname(sk, sockaddr_p, &len);
1261 log_err("fio: getsockaddr: %s\n", strerror(errno));
1265 if (!inet_ntop(use_ipv6?AF_INET6:AF_INET, net_addr, client_sockaddr_str, INET6_ADDRSTRLEN - 1)) {
1266 log_err("inet_ntop: failed to convert addr to string\n");
1270 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1274 static int accept_loop(int listen_sk)
1276 struct sockaddr_in addr;
1277 struct sockaddr_in6 addr6;
1278 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1280 int ret = 0, sk, exitval = 0;
1281 FLIST_HEAD(conn_list);
1283 dprint(FD_NET, "server enter accept loop\n");
1285 fio_set_fd_nonblocking(listen_sk, "server");
1287 while (!exit_backend) {
1288 struct sk_out *sk_out;
1294 pfd.events = POLLIN;
1298 if (!flist_empty(&conn_list))
1301 ret = poll(&pfd, 1, timeout);
1305 log_err("fio: poll: %s\n", strerror(errno));
1308 fio_server_check_conns(&conn_list);
1312 if (pfd.revents & POLLIN)
1314 } while (!exit_backend);
1316 fio_server_check_conns(&conn_list);
1318 if (exit_backend || ret < 0)
1322 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1324 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1327 log_err("fio: accept: %s\n", strerror(errno));
1332 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1334 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1336 dprint(FD_NET, "server: connect from %s\n", from);
1338 sk_out = smalloc(sizeof(*sk_out));
1340 INIT_FLIST_HEAD(&sk_out->list);
1341 __fio_mutex_init(&sk_out->lock, FIO_MUTEX_UNLOCKED);
1342 __fio_mutex_init(&sk_out->wait, FIO_MUTEX_LOCKED);
1343 __fio_mutex_init(&sk_out->xmit, FIO_MUTEX_UNLOCKED);
1348 fio_server_add_conn_pid(&conn_list, pid);
1352 /* if error, it's already logged, non-fatal */
1353 get_my_addr_str(sk);
1356 * Assign sk_out here, it'll be dropped in handle_connection()
1357 * since that function calls _exit() when done
1359 sk_out_assign(sk_out);
1360 handle_connection(sk_out);
1366 int fio_server_text_output(int level, const char *buf, size_t len)
1368 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1369 struct cmd_text_pdu *pdu;
1373 if (!sk_out || sk_out->sk == -1)
1376 tlen = sizeof(*pdu) + len;
1379 pdu->level = __cpu_to_le32(level);
1380 pdu->buf_len = __cpu_to_le32(len);
1382 gettimeofday(&tv, NULL);
1383 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1384 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1386 memcpy(pdu->buf, buf, len);
1388 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1393 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1395 dst->max_val = cpu_to_le64(src->max_val);
1396 dst->min_val = cpu_to_le64(src->min_val);
1397 dst->samples = cpu_to_le64(src->samples);
1400 * Encode to IEEE 754 for network transfer
1402 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1403 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1406 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1410 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1411 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1412 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1413 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1414 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1415 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
1416 dst->agg[i] = cpu_to_le64(src->agg[i]);
1419 dst->kb_base = cpu_to_le32(src->kb_base);
1420 dst->unit_base = cpu_to_le32(src->unit_base);
1421 dst->groupid = cpu_to_le32(src->groupid);
1422 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1426 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1427 * into a single payload.
1429 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1431 struct cmd_ts_pdu p;
1434 dprint(FD_NET, "server sending end stats\n");
1436 memset(&p, 0, sizeof(p));
1438 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1439 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1440 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1442 p.ts.error = cpu_to_le32(ts->error);
1443 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1444 p.ts.groupid = cpu_to_le32(ts->groupid);
1445 p.ts.pid = cpu_to_le32(ts->pid);
1446 p.ts.members = cpu_to_le32(ts->members);
1447 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1449 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1450 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1451 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1452 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1453 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1456 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1457 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1458 p.ts.ctx = cpu_to_le64(ts->ctx);
1459 p.ts.minf = cpu_to_le64(ts->minf);
1460 p.ts.majf = cpu_to_le64(ts->majf);
1461 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1462 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1464 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1465 fio_fp64_t *src = &ts->percentile_list[i];
1466 fio_fp64_t *dst = &p.ts.percentile_list[i];
1468 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1471 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1472 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1473 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1474 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1477 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1478 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1479 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1482 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1483 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1484 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1486 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1487 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1488 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1489 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1492 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1493 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1495 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1496 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1497 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1500 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1501 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1502 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1503 p.ts.first_error = cpu_to_le32(ts->first_error);
1504 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1505 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1507 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1508 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1509 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1510 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1512 p.ts.nr_block_infos = le64_to_cpu(ts->nr_block_infos);
1513 for (i = 0; i < p.ts.nr_block_infos; i++)
1514 p.ts.block_infos[i] = le32_to_cpu(ts->block_infos[i]);
1516 convert_gs(&p.rs, rs);
1518 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1521 void fio_server_send_gs(struct group_run_stats *rs)
1523 struct group_run_stats gs;
1525 dprint(FD_NET, "server sending group run stats\n");
1527 convert_gs(&gs, rs);
1528 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1531 void fio_server_send_job_options(struct flist_head *opt_list,
1532 unsigned int groupid)
1534 struct cmd_job_option pdu;
1535 struct flist_head *entry;
1537 if (flist_empty(opt_list))
1540 flist_for_each(entry, opt_list) {
1541 struct print_option *p;
1544 p = flist_entry(entry, struct print_option, list);
1545 memset(&pdu, 0, sizeof(pdu));
1547 if (groupid == -1U) {
1548 pdu.global = __cpu_to_le16(1);
1552 pdu.groupid = cpu_to_le32(groupid);
1554 len = strlen(p->name);
1555 if (len >= sizeof(pdu.name)) {
1556 len = sizeof(pdu.name) - 1;
1557 pdu.truncated = __cpu_to_le16(1);
1559 memcpy(pdu.name, p->name, len);
1561 len = strlen(p->value);
1562 if (len >= sizeof(pdu.value)) {
1563 len = sizeof(pdu.value) - 1;
1564 pdu.truncated = __cpu_to_le16(1);
1566 memcpy(pdu.value, p->value, len);
1568 fio_net_queue_cmd(FIO_NET_CMD_JOB_OPT, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1572 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1576 for (i = 0; i < 2; i++) {
1577 dst->ios[i] = cpu_to_le64(src->ios[i]);
1578 dst->merges[i] = cpu_to_le64(src->merges[i]);
1579 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1580 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1583 dst->io_ticks = cpu_to_le64(src->io_ticks);
1584 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1585 dst->slavecount = cpu_to_le32(src->slavecount);
1586 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1589 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1593 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1594 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1596 for (i = 0; i < 2; i++) {
1597 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1598 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1599 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1600 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1603 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1604 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1605 dst->s.msec = cpu_to_le64(src->s.msec);
1608 void fio_server_send_du(void)
1610 struct disk_util *du;
1611 struct flist_head *entry;
1612 struct cmd_du_pdu pdu;
1614 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1616 memset(&pdu, 0, sizeof(pdu));
1618 flist_for_each(entry, &disk_list) {
1619 du = flist_entry(entry, struct disk_util, list);
1621 convert_dus(&pdu.dus, &du->dus);
1622 convert_agg(&pdu.agg, &du->agg);
1624 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1628 static int fio_append_iolog_gz(struct sk_entry *first, struct io_log *log)
1632 struct sk_entry *entry;
1637 * Dirty - since the log is potentially huge, compress it into
1638 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1639 * side defragment it.
1641 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1643 stream.zalloc = Z_NULL;
1644 stream.zfree = Z_NULL;
1645 stream.opaque = Z_NULL;
1647 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
1652 stream.next_in = (void *) log->log;
1653 stream.avail_in = log->nr_samples * log_entry_sz(log);
1656 unsigned int this_len;
1658 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1659 stream.next_out = out_pdu;
1660 ret = deflate(&stream, Z_FINISH);
1661 /* may be Z_OK, or Z_STREAM_END */
1665 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1667 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, out_pdu, this_len,
1668 NULL, SK_F_VEC | SK_F_INLINE | SK_F_FREE);
1670 flist_add_tail(&entry->list, &first->next);
1671 } while (stream.avail_in);
1674 deflateEnd(&stream);
1681 static int fio_append_gz_chunks(struct sk_entry *first, struct io_log *log)
1683 struct sk_entry *entry;
1684 struct flist_head *node;
1686 pthread_mutex_lock(&log->chunk_lock);
1687 flist_for_each(node, &log->chunk_list) {
1688 struct iolog_compress *c;
1690 c = flist_entry(node, struct iolog_compress, list);
1691 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, c->buf, c->len,
1692 NULL, SK_F_VEC | SK_F_INLINE);
1693 flist_add_tail(&entry->list, &first->next);
1695 pthread_mutex_unlock(&log->chunk_lock);
1700 static int fio_append_text_log(struct sk_entry *first, struct io_log *log)
1702 struct sk_entry *entry;
1703 size_t size = log->nr_samples * log_entry_sz(log);
1705 entry = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, log->log, size,
1706 NULL, SK_F_VEC | SK_F_INLINE);
1707 flist_add_tail(&entry->list, &first->next);
1711 int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1713 struct cmd_iolog_pdu pdu;
1714 struct sk_entry *first;
1717 pdu.nr_samples = cpu_to_le64(log->nr_samples);
1718 pdu.thread_number = cpu_to_le32(td->thread_number);
1719 pdu.log_type = cpu_to_le32(log->log_type);
1721 if (!flist_empty(&log->chunk_list))
1722 pdu.compressed = __cpu_to_le32(STORE_COMPRESSED);
1724 pdu.compressed = __cpu_to_le32(XMIT_COMPRESSED);
1728 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1729 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1732 * We can't do this for a pre-compressed log, but for that case,
1733 * log->nr_samples is zero anyway.
1735 for (i = 0; i < log->nr_samples; i++) {
1736 struct io_sample *s = get_sample(log, i);
1738 s->time = cpu_to_le64(s->time);
1739 s->val = cpu_to_le64(s->val);
1740 s->__ddir = cpu_to_le32(s->__ddir);
1741 s->bs = cpu_to_le32(s->bs);
1743 if (log->log_offset) {
1744 struct io_sample_offset *so = (void *) s;
1746 so->offset = cpu_to_le64(so->offset);
1751 * Assemble header entry first
1753 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_VEC | SK_F_INLINE | SK_F_COPY);
1756 * Now append actual log entries. If log compression was enabled on
1757 * the job, just send out the compressed chunks directly. If we
1758 * have a plain log, compress if we can, then send. Otherwise, send
1759 * the plain text output.
1761 if (!flist_empty(&log->chunk_list))
1762 ret = fio_append_gz_chunks(first, log);
1764 ret = fio_append_iolog_gz(first, log);
1766 ret = fio_append_text_log(first, log);
1768 fio_net_queue_entry(first);
1772 void fio_server_send_add_job(struct thread_data *td)
1774 struct cmd_add_job_pdu pdu;
1776 memset(&pdu, 0, sizeof(pdu));
1777 pdu.thread_number = cpu_to_le32(td->thread_number);
1778 pdu.groupid = cpu_to_le32(td->groupid);
1779 convert_thread_options_to_net(&pdu.top, &td->o);
1781 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL,
1785 void fio_server_send_start(struct thread_data *td)
1787 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1789 assert(sk_out->sk != -1);
1791 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, 0, SK_F_SIMPLE);
1794 int fio_server_get_verify_state(const char *name, int threadnumber,
1795 void **datap, int *version)
1797 struct thread_io_list *s;
1798 struct cmd_sendfile out;
1799 struct cmd_reply *rep;
1804 dprint(FD_NET, "server: request verify state\n");
1806 rep = smalloc(sizeof(*rep));
1808 log_err("fio: smalloc pool too small\n");
1812 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1816 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1818 tag = (uint64_t) (uintptr_t) rep;
1819 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag,
1823 * Wait for the backend to receive the reply
1825 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
1826 log_err("fio: timed out waiting for reply\n");
1832 log_err("fio: failure on receiving state file %s: %s\n",
1833 out.path, strerror(rep->error));
1838 fio_net_queue_quit();
1843 * The format is verify_state_hdr, then thread_io_list. Verify
1844 * the header, and the thread_io_list checksum
1846 s = rep->data + sizeof(struct verify_state_hdr);
1847 if (verify_state_hdr(rep->data, s, version)) {
1853 * Don't need the header from now, copy just the thread_io_list
1856 rep->size -= sizeof(struct verify_state_hdr);
1857 data = malloc(rep->size);
1858 memcpy(data, s, rep->size);
1862 __fio_mutex_remove(&rep->lock);
1867 static int fio_init_server_ip(void)
1869 struct sockaddr *addr;
1876 sk = socket(AF_INET6, SOCK_STREAM, 0);
1878 sk = socket(AF_INET, SOCK_STREAM, 0);
1881 log_err("fio: socket: %s\n", strerror(errno));
1886 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1887 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
1892 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
1893 log_err("fio: setsockopt(REUSEPORT): %s\n", strerror(errno));
1900 const void *src = &saddr_in6.sin6_addr;
1902 addr = (struct sockaddr *) &saddr_in6;
1903 socklen = sizeof(saddr_in6);
1904 saddr_in6.sin6_family = AF_INET6;
1905 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
1907 const void *src = &saddr_in.sin_addr;
1909 addr = (struct sockaddr *) &saddr_in;
1910 socklen = sizeof(saddr_in);
1911 saddr_in.sin_family = AF_INET;
1912 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
1915 if (bind(sk, addr, socklen) < 0) {
1916 log_err("fio: bind: %s\n", strerror(errno));
1917 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
1925 static int fio_init_server_sock(void)
1927 struct sockaddr_un addr;
1932 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1934 log_err("fio: socket: %s\n", strerror(errno));
1940 memset(&addr, 0, sizeof(addr));
1941 addr.sun_family = AF_UNIX;
1942 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
1944 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1946 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1947 log_err("fio: bind: %s\n", strerror(errno));
1956 static int fio_init_server_connection(void)
1961 dprint(FD_NET, "starting server\n");
1964 sk = fio_init_server_ip();
1966 sk = fio_init_server_sock();
1971 memset(bind_str, 0, sizeof(bind_str));
1980 src = &saddr_in6.sin6_addr;
1983 src = &saddr_in.sin_addr;
1986 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1988 sprintf(port, ",%u", fio_net_port);
1992 strncpy(bind_str, port, sizeof(bind_str) - 1);
1994 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
1996 log_info("fio: server listening on %s\n", bind_str);
1998 if (listen(sk, 4) < 0) {
1999 log_err("fio: listen: %s\n", strerror(errno));
2007 int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
2008 struct in6_addr *inp6)
2014 ret = inet_pton(AF_INET6, host, inp6);
2016 ret = inet_pton(AF_INET, host, inp);
2019 struct addrinfo hints, *res;
2021 memset(&hints, 0, sizeof(hints));
2022 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
2023 hints.ai_socktype = SOCK_STREAM;
2025 ret = getaddrinfo(host, NULL, &hints, &res);
2027 log_err("fio: failed to resolve <%s> (%s)\n", host,
2033 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
2035 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
2045 * Parse a host/ip/port string. Reads from 'str'.
2050 * *ptr is the host, *port is the port, inp is the destination.
2052 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
2053 * For local domain sockets:
2054 * *ptr is the filename, *is_sock is 1.
2056 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
2057 int *port, struct in_addr *inp,
2058 struct in6_addr *inp6, int *ipv6)
2060 const char *host = str;
2066 *port = fio_net_port;
2069 if (!strncmp(str, "sock:", 5)) {
2070 *ptr = strdup(str + 5);
2077 * Is it ip:<ip or host>:port
2079 if (!strncmp(host, "ip:", 3))
2081 else if (!strncmp(host, "ip4:", 4))
2083 else if (!strncmp(host, "ip6:", 4)) {
2086 } else if (host[0] == ':') {
2087 /* String is :port */
2090 if (!lport || lport > 65535) {
2091 log_err("fio: bad server port %u\n", lport);
2094 /* no hostname given, we are done */
2100 * If no port seen yet, check if there's a last ',' at the end
2103 portp = strchr(host, ',');
2107 lport = atoi(portp);
2108 if (!lport || lport > 65535) {
2109 log_err("fio: bad server port %u\n", lport);
2121 *ptr = strdup(host);
2123 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
2130 *port = fio_net_port;
2136 * Server arg should be one of:
2138 * sock:/path/to/socket
2142 * Where sock uses unix domain sockets, and ip binds the server to
2143 * a specific interface. If no arguments are given to the server, it
2144 * uses IP and binds to 0.0.0.0.
2147 static int fio_handle_server_arg(void)
2149 int port = fio_net_port;
2150 int is_sock, ret = 0;
2152 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
2154 if (!fio_server_arg)
2157 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
2158 &port, &saddr_in.sin_addr,
2159 &saddr_in6.sin6_addr, &use_ipv6);
2161 if (!is_sock && bind_sock) {
2167 fio_net_port = port;
2168 saddr_in.sin_port = htons(port);
2169 saddr_in6.sin6_port = htons(port);
2173 static void sig_int(int sig)
2179 static void set_sig_handlers(void)
2181 struct sigaction act;
2183 memset(&act, 0, sizeof(act));
2184 act.sa_handler = sig_int;
2185 act.sa_flags = SA_RESTART;
2186 sigaction(SIGINT, &act, NULL);
2189 static int fio_server(void)
2193 if (pthread_key_create(&sk_out_key, NULL)) {
2194 log_err("fio: can't create sk_out backend key\n");
2198 pthread_setspecific(sk_out_key, NULL);
2200 dprint(FD_NET, "starting server\n");
2202 if (fio_handle_server_arg())
2205 sk = fio_init_server_connection();
2211 ret = accept_loop(sk);
2215 if (fio_server_arg) {
2216 free(fio_server_arg);
2217 fio_server_arg = NULL;
2225 void fio_server_got_signal(int signal)
2227 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2231 if (signal == SIGPIPE)
2234 log_info("\nfio: terminating on signal %d\n", signal);
2239 static int check_existing_pidfile(const char *pidfile)
2246 if (stat(pidfile, &sb))
2249 f = fopen(pidfile, "r");
2253 if (fread(buf, sb.st_size, 1, f) <= 0) {
2260 if (kill(pid, SIGCONT) < 0)
2261 return errno != ESRCH;
2266 static int write_pid(pid_t pid, const char *pidfile)
2270 fpid = fopen(pidfile, "w");
2272 log_err("fio: failed opening pid file %s\n", pidfile);
2276 fprintf(fpid, "%u\n", (unsigned int) pid);
2282 * If pidfile is specified, background us.
2284 int fio_start_server(char *pidfile)
2291 WSAStartup(MAKEWORD(2, 2), &wsd);
2295 return fio_server();
2297 if (check_existing_pidfile(pidfile)) {
2298 log_err("fio: pidfile %s exists and server appears alive\n",
2306 log_err("fio: failed server fork: %s", strerror(errno));
2310 ret = write_pid(pid, pidfile);
2316 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2318 close(STDIN_FILENO);
2319 close(STDOUT_FILENO);
2320 close(STDERR_FILENO);
2332 void fio_server_set_arg(const char *arg)
2334 fio_server_arg = strdup(arg);