server: make the io log transmit use the new infrastructure
[fio.git] / server.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdarg.h>
4#include <unistd.h>
5#include <limits.h>
6#include <errno.h>
7#include <sys/poll.h>
8#include <sys/types.h>
9#include <sys/wait.h>
10#include <sys/socket.h>
11#include <sys/stat.h>
12#include <sys/un.h>
13#include <sys/uio.h>
14#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
17#include <syslog.h>
18#include <signal.h>
19#ifdef CONFIG_ZLIB
20#include <zlib.h>
21#endif
22
23#include "fio.h"
24#include "options.h"
25#include "server.h"
26#include "crc/crc16.h"
27#include "lib/ieee754.h"
28#include "verify.h"
29#include "smalloc.h"
30
31int fio_net_port = FIO_NET_PORT;
32
33int exit_backend = 0;
34
35enum {
36 SK_F_FREE = 1,
37 SK_F_COPY = 2,
38 SK_F_SIMPLE = 4,
39 SK_F_VEC = 8,
40};
41
42struct sk_entry {
43 struct flist_head list;
44 int opcode;
45 void *buf;
46 off_t size;
47 uint64_t *tagptr;
48 int flags;
49 struct flist_head next;
50};
51
52struct sk_out {
53 int sk;
54 struct fio_mutex *lock;
55 struct flist_head list;
56 struct fio_mutex *wait;
57};
58
59static char *fio_server_arg;
60static char *bind_sock;
61static struct sockaddr_in saddr_in;
62static struct sockaddr_in6 saddr_in6;
63static int use_ipv6;
64#ifdef CONFIG_ZLIB
65static unsigned int has_zlib = 1;
66#else
67static unsigned int has_zlib = 0;
68#endif
69static unsigned int use_zlib;
70static char me[128];
71
72static pthread_key_t sk_out_key;
73
74struct fio_fork_item {
75 struct flist_head list;
76 int exitval;
77 int signal;
78 int exited;
79 pid_t pid;
80};
81
82struct cmd_reply {
83 struct fio_mutex lock;
84 void *data;
85 size_t size;
86 int error;
87};
88
89static const char *fio_server_ops[FIO_NET_CMD_NR] = {
90 "",
91 "QUIT",
92 "EXIT",
93 "JOB",
94 "JOBLINE",
95 "TEXT",
96 "TS",
97 "GS",
98 "SEND_ETA",
99 "ETA",
100 "PROBE",
101 "START",
102 "STOP",
103 "DISK_UTIL",
104 "SERVER_START",
105 "ADD_JOB",
106 "RUN",
107 "IOLOG",
108 "UPDATE_JOB",
109 "LOAD_FILE",
110 "VTRIGGER",
111 "SENDFILE",
112};
113
114static void sk_lock(struct sk_out *sk_out)
115{
116 fio_mutex_down(sk_out->lock);
117}
118
119static void sk_unlock(struct sk_out *sk_out)
120{
121 fio_mutex_up(sk_out->lock);
122}
123
124const char *fio_server_op(unsigned int op)
125{
126 static char buf[32];
127
128 if (op < FIO_NET_CMD_NR)
129 return fio_server_ops[op];
130
131 sprintf(buf, "UNKNOWN/%d", op);
132 return buf;
133}
134
135static ssize_t iov_total_len(const struct iovec *iov, int count)
136{
137 ssize_t ret = 0;
138
139 while (count--) {
140 ret += iov->iov_len;
141 iov++;
142 }
143
144 return ret;
145}
146
147static int fio_sendv_data(int sk, struct iovec *iov, int count)
148{
149 ssize_t total_len = iov_total_len(iov, count);
150 ssize_t ret;
151
152 do {
153 ret = writev(sk, iov, count);
154 if (ret > 0) {
155 total_len -= ret;
156 if (!total_len)
157 break;
158
159 while (ret) {
160 if (ret >= iov->iov_len) {
161 ret -= iov->iov_len;
162 iov++;
163 continue;
164 }
165 iov->iov_base += ret;
166 iov->iov_len -= ret;
167 ret = 0;
168 }
169 } else if (!ret)
170 break;
171 else if (errno == EAGAIN || errno == EINTR)
172 continue;
173 else
174 break;
175 } while (!exit_backend);
176
177 if (!total_len)
178 return 0;
179
180 if (errno)
181 return -errno;
182
183 return 1;
184}
185
186int fio_send_data(int sk, const void *p, unsigned int len)
187{
188 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
189
190 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
191
192 return fio_sendv_data(sk, &iov, 1);
193}
194
195int fio_recv_data(int sk, void *p, unsigned int len)
196{
197 do {
198 int ret = recv(sk, p, len, MSG_WAITALL);
199
200 if (ret > 0) {
201 len -= ret;
202 if (!len)
203 break;
204 p += ret;
205 continue;
206 } else if (!ret)
207 break;
208 else if (errno == EAGAIN || errno == EINTR)
209 continue;
210 else
211 break;
212 } while (!exit_backend);
213
214 if (!len)
215 return 0;
216
217 return -1;
218}
219
220static int verify_convert_cmd(struct fio_net_cmd *cmd)
221{
222 uint16_t crc;
223
224 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
225 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
226
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);
231 return 1;
232 }
233
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);
239
240 switch (cmd->version) {
241 case FIO_SERVER_VER:
242 break;
243 default:
244 log_err("fio: bad server cmd version %d\n", cmd->version);
245 return 1;
246 }
247
248 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
249 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
250 return 1;
251 }
252
253 return 0;
254}
255
256/*
257 * Read (and defragment, if necessary) incoming commands
258 */
259struct fio_net_cmd *fio_net_recv_cmd(int sk)
260{
261 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
262 size_t cmd_size = 0, pdu_offset = 0;
263 uint16_t crc;
264 int ret, first = 1;
265 void *pdu = NULL;
266
267 do {
268 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
269 if (ret)
270 break;
271
272 /* We have a command, verify it and swap if need be */
273 ret = verify_convert_cmd(&cmd);
274 if (ret)
275 break;
276
277 if (first) {
278 /* if this is text, add room for \0 at the end */
279 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
280 assert(!cmdret);
281 } else
282 cmd_size += cmd.pdu_len;
283
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);
286 ret = 1;
287 break;
288 }
289
290 tmp = realloc(cmdret, cmd_size);
291 if (!tmp) {
292 log_err("fio: server failed allocating cmd\n");
293 ret = 1;
294 break;
295 }
296 cmdret = tmp;
297
298 if (first)
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);
303 ret = 1;
304 break;
305 }
306
307 if (!cmd.pdu_len)
308 break;
309
310 /* There's payload, get it */
311 pdu = (void *) cmdret->payload + pdu_offset;
312 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
313 if (ret)
314 break;
315
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);
321 ret = 1;
322 break;
323 }
324
325 pdu_offset += cmd.pdu_len;
326 if (!first)
327 cmdret->pdu_len += cmd.pdu_len;
328 first = 0;
329 } while (cmd.flags & FIO_NET_CMD_F_MORE);
330
331 if (ret) {
332 free(cmdret);
333 cmdret = NULL;
334 } else if (cmdret) {
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;
340
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);
346
347 buf[len] = '\0';
348 }
349 }
350
351 /* frag flag is internal */
352 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
353 }
354
355 return cmdret;
356}
357
358static void add_reply(uint64_t tag, struct flist_head *list)
359{
360 struct fio_net_cmd_reply *reply;
361
362 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
363 flist_add_tail(&reply->list, list);
364}
365
366static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
367{
368 struct fio_net_cmd_reply *reply;
369
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;
375
376 return (uintptr_t) reply;
377}
378
379static void free_reply(uint64_t tag)
380{
381 struct fio_net_cmd_reply *reply;
382
383 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
384 free(reply);
385}
386
387void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
388{
389 uint32_t pdu_len;
390
391 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
392
393 pdu_len = le32_to_cpu(cmd->pdu_len);
394 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
395}
396
397void fio_net_cmd_crc(struct fio_net_cmd *cmd)
398{
399 fio_net_cmd_crc_pdu(cmd, cmd->payload);
400}
401
402int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
403 uint64_t *tagptr, struct flist_head *list)
404{
405 struct fio_net_cmd *cmd = NULL;
406 size_t this_len, cur_len = 0;
407 uint64_t tag;
408 int ret;
409
410 if (list) {
411 assert(tagptr);
412 tag = *tagptr = alloc_reply(*tagptr, opcode);
413 } else
414 tag = tagptr ? *tagptr : 0;
415
416 do {
417 this_len = size;
418 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
419 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
420
421 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
422 if (cmd)
423 free(cmd);
424
425 cur_len = sizeof(*cmd) + this_len;
426 cmd = malloc(cur_len);
427 }
428
429 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
430
431 if (this_len < size)
432 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
433
434 fio_net_cmd_crc(cmd);
435
436 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
437 size -= this_len;
438 buf += this_len;
439 } while (!ret && size);
440
441 if (list) {
442 if (ret)
443 free_reply(tag);
444 else
445 add_reply(tag, list);
446 }
447
448 if (cmd)
449 free(cmd);
450
451 return ret;
452}
453
454struct sk_entry *fio_net_prep_cmd(uint16_t opcode, void *buf, off_t size,
455 uint64_t *tagptr, int flags)
456{
457 struct sk_entry *entry;
458
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);
465 } else
466 entry->buf = buf;
467 entry->size = size;
468 entry->tagptr = tagptr;
469 entry->flags = flags;
470
471 return entry;
472}
473
474static void fio_net_queue_entry(struct sk_entry *entry)
475{
476 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
477
478 sk_lock(sk_out);
479 flist_add_tail(&entry->list, &sk_out->list);
480 sk_unlock(sk_out);
481
482 fio_mutex_up(sk_out->wait);
483}
484
485static int fio_net_queue_cmd(uint16_t opcode, void *buf, off_t size,
486 uint64_t *tagptr, int flags)
487{
488 struct sk_entry *entry;
489
490 entry = fio_net_prep_cmd(opcode, buf, size, tagptr, flags);
491 fio_net_queue_entry(entry);
492 return 0;
493}
494
495static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
496{
497 struct fio_net_cmd cmd;
498
499 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
500 fio_net_cmd_crc(&cmd);
501
502 return fio_send_data(sk, &cmd, sizeof(cmd));
503}
504
505/*
506 * If 'list' is non-NULL, then allocate and store the sent command for
507 * later verification.
508 */
509int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
510 struct flist_head *list)
511{
512 int ret;
513
514 if (list)
515 tag = alloc_reply(tag, opcode);
516
517 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
518 if (ret) {
519 if (list)
520 free_reply(tag);
521
522 return ret;
523 }
524
525 if (list)
526 add_reply(tag, list);
527
528 return 0;
529}
530
531static int fio_net_queue_quit(void)
532{
533 dprint(FD_NET, "server: sending quit\n");
534
535 return fio_net_queue_cmd(FIO_NET_CMD_QUIT, NULL, 0, 0, SK_F_SIMPLE);
536}
537
538int fio_net_send_quit(int sk)
539{
540 dprint(FD_NET, "server: sending quit\n");
541
542 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
543}
544
545static int fio_net_send_ack(struct fio_net_cmd *cmd, int error, int signal)
546{
547 struct cmd_end_pdu epdu;
548 uint64_t tag = 0;
549
550 if (cmd)
551 tag = cmd->tag;
552
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);
556}
557
558static int fio_net_queue_stop(int error, int signal)
559{
560 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
561 return fio_net_send_ack(NULL, error, signal);
562}
563
564static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
565{
566 struct fio_fork_item *ffi;
567
568 ffi = malloc(sizeof(*ffi));
569 ffi->exitval = 0;
570 ffi->signal = 0;
571 ffi->exited = 0;
572 ffi->pid = pid;
573 flist_add_tail(&ffi->list, list);
574}
575
576static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
577{
578 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
579 fio_server_add_fork_item(pid, conn_list);
580}
581
582static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
583{
584 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
585 fio_server_add_fork_item(pid, job_list);
586}
587
588static void fio_server_check_fork_item(struct fio_fork_item *ffi)
589{
590 int ret, status;
591
592 ret = waitpid(ffi->pid, &status, WNOHANG);
593 if (ret < 0) {
594 if (errno == ECHILD) {
595 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
596 ffi->exited = 1;
597 } else
598 log_err("fio: waitpid: %s\n", strerror(errno));
599 } else if (ret == ffi->pid) {
600 if (WIFSIGNALED(status)) {
601 ffi->signal = WTERMSIG(status);
602 ffi->exited = 1;
603 }
604 if (WIFEXITED(status)) {
605 if (WEXITSTATUS(status))
606 ffi->exitval = WEXITSTATUS(status);
607 ffi->exited = 1;
608 }
609 }
610}
611
612static void fio_server_fork_item_done(struct fio_fork_item *ffi, bool stop)
613{
614 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
615
616 /*
617 * Fold STOP and QUIT...
618 */
619 if (stop) {
620 fio_net_queue_stop(ffi->exitval, ffi->signal);
621 fio_net_queue_quit();
622 }
623
624 flist_del(&ffi->list);
625 free(ffi);
626}
627
628static void fio_server_check_fork_items(struct flist_head *list, bool stop)
629{
630 struct flist_head *entry, *tmp;
631 struct fio_fork_item *ffi;
632
633 flist_for_each_safe(entry, tmp, list) {
634 ffi = flist_entry(entry, struct fio_fork_item, list);
635
636 fio_server_check_fork_item(ffi);
637
638 if (ffi->exited)
639 fio_server_fork_item_done(ffi, stop);
640 }
641}
642
643static void fio_server_check_jobs(struct flist_head *job_list)
644{
645 fio_server_check_fork_items(job_list, true);
646}
647
648static void fio_server_check_conns(struct flist_head *conn_list)
649{
650 fio_server_check_fork_items(conn_list, false);
651}
652
653static int handle_load_file_cmd(struct fio_net_cmd *cmd)
654{
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;
658
659 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
660
661 pdu->name_len = le16_to_cpu(pdu->name_len);
662 pdu->client_type = le16_to_cpu(pdu->client_type);
663
664 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
665 fio_net_queue_quit();
666 return -1;
667 }
668
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);
672 return 0;
673}
674
675static int handle_run_cmd(struct sk_out *sk_out, struct flist_head *job_list,
676 struct fio_net_cmd *cmd)
677{
678 struct backend_data data;
679 pid_t pid;
680 int ret;
681
682 fio_time_init();
683 set_genesis_time();
684
685 pid = fork();
686 if (pid) {
687 fio_server_add_job_pid(job_list, pid);
688 return 0;
689 }
690
691 data.key = sk_out_key;
692 data.ptr = sk_out;
693 //pthread_setspecific(sk_out_key, sk_out);
694 ret = fio_backend(&data);
695 free_threads_shm();
696 _exit(ret);
697}
698
699static int handle_job_cmd(struct fio_net_cmd *cmd)
700{
701 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
702 void *buf = pdu->buf;
703 struct cmd_start_pdu spdu;
704
705 pdu->buf_len = le32_to_cpu(pdu->buf_len);
706 pdu->client_type = le32_to_cpu(pdu->client_type);
707
708 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
709 fio_net_queue_quit();
710 return -1;
711 }
712
713 spdu.jobs = cpu_to_le32(thread_number);
714 spdu.stat_outputs = cpu_to_le32(stat_number);
715
716 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
717 return 0;
718}
719
720static int handle_jobline_cmd(struct fio_net_cmd *cmd)
721{
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;
727 char **argv;
728 int i;
729
730 clp = pdu;
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);
735
736 dprint(FD_NET, "server: %d command line args\n", clp->lines);
737
738 for (i = 0; i < clp->lines; i++) {
739 cslp = pdu + offset;
740 argv[i] = (char *) cslp->text;
741
742 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
743 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
744 }
745
746 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
747 fio_net_queue_quit();
748 free(argv);
749 return -1;
750 }
751
752 free(argv);
753
754 spdu.jobs = cpu_to_le32(thread_number);
755 spdu.stat_outputs = cpu_to_le32(stat_number);
756
757 fio_net_queue_cmd(FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, SK_F_COPY);
758 return 0;
759}
760
761static int handle_probe_cmd(struct fio_net_cmd *cmd)
762{
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;
766
767 dprint(FD_NET, "server: sending probe reply\n");
768
769 strcpy(me, (char *) pdu->server);
770
771 memset(&probe, 0, sizeof(probe));
772 gethostname((char *) probe.hostname, sizeof(probe.hostname));
773#ifdef CONFIG_BIG_ENDIAN
774 probe.bigendian = 1;
775#endif
776 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
777
778 probe.os = FIO_OS;
779 probe.arch = FIO_ARCH;
780 probe.bpp = sizeof(void *);
781 probe.cpus = __cpu_to_le32(cpus_online());
782
783 /*
784 * If the client supports compression and we do too, then enable it
785 */
786 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
787 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
788 use_zlib = 1;
789 } else {
790 probe.flags = 0;
791 use_zlib = 0;
792 }
793
794 return fio_net_queue_cmd(FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, SK_F_COPY);
795}
796
797static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
798{
799 struct jobs_eta *je;
800 uint64_t tag = cmd->tag;
801 size_t size;
802 int i;
803
804 dprint(FD_NET, "server sending status\n");
805
806 /*
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
809 */
810 je = get_jobs_eta(true, &size);
811 if (!je) {
812 size = sizeof(*je);
813 je = calloc(1, size);
814 } else {
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);
820
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]);
828 }
829
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);
835 }
836
837 fio_net_queue_cmd(FIO_NET_CMD_ETA, je, size, &tag, SK_F_FREE);
838 return 0;
839}
840
841static int send_update_job_reply(uint64_t __tag, int error)
842{
843 uint64_t tag = __tag;
844 uint32_t pdu_error;
845
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);
848}
849
850static int handle_update_job_cmd(struct fio_net_cmd *cmd)
851{
852 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
853 struct thread_data *td;
854 uint32_t tnumber;
855
856 tnumber = le32_to_cpu(pdu->thread_number);
857
858 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
859
860 if (!tnumber || tnumber > thread_number) {
861 send_update_job_reply(cmd->tag, ENODEV);
862 return 0;
863 }
864
865 td = &threads[tnumber - 1];
866 convert_thread_options_to_cpu(&td->o, &pdu->top);
867 send_update_job_reply(cmd->tag, 0);
868 return 0;
869}
870
871static int handle_trigger_cmd(struct fio_net_cmd *cmd)
872{
873 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
874 char *buf = (char *) pdu->cmd;
875 struct all_io_list *rep;
876 size_t sz;
877
878 pdu->len = le16_to_cpu(pdu->len);
879 buf[pdu->len] = '\0';
880
881 rep = get_all_io_list(IO_LIST_ALL, &sz);
882 if (!rep) {
883 struct all_io_list state;
884
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);
887 } else
888 fio_net_queue_cmd(FIO_NET_CMD_VTRIGGER, rep, sz, NULL, SK_F_FREE);
889
890 exec_trigger(buf);
891 return 0;
892}
893
894static int handle_command(struct sk_out *sk_out, struct flist_head *job_list,
895 struct fio_net_cmd *cmd)
896{
897 int ret;
898
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);
902
903 switch (cmd->opcode) {
904 case FIO_NET_CMD_QUIT:
905 fio_terminate_threads(TERMINATE_ALL);
906 ret = 0;
907 break;
908 case FIO_NET_CMD_EXIT:
909 exit_backend = 1;
910 return -1;
911 case FIO_NET_CMD_LOAD_FILE:
912 ret = handle_load_file_cmd(cmd);
913 break;
914 case FIO_NET_CMD_JOB:
915 ret = handle_job_cmd(cmd);
916 break;
917 case FIO_NET_CMD_JOBLINE:
918 ret = handle_jobline_cmd(cmd);
919 break;
920 case FIO_NET_CMD_PROBE:
921 ret = handle_probe_cmd(cmd);
922 break;
923 case FIO_NET_CMD_SEND_ETA:
924 ret = handle_send_eta_cmd(cmd);
925 break;
926 case FIO_NET_CMD_RUN:
927 ret = handle_run_cmd(sk_out, job_list, cmd);
928 break;
929 case FIO_NET_CMD_UPDATE_JOB:
930 ret = handle_update_job_cmd(cmd);
931 break;
932 case FIO_NET_CMD_VTRIGGER:
933 ret = handle_trigger_cmd(cmd);
934 break;
935 case FIO_NET_CMD_SENDFILE: {
936 struct cmd_sendfile_reply *in;
937 struct cmd_reply *rep;
938
939 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
940
941 in = (struct cmd_sendfile_reply *) cmd->payload;
942 in->size = le32_to_cpu(in->size);
943 in->error = le32_to_cpu(in->error);
944 if (in->error) {
945 ret = 1;
946 rep->error = in->error;
947 } else {
948 ret = 0;
949 rep->data = smalloc(in->size);
950 if (!rep->data) {
951 ret = 1;
952 rep->error = ENOMEM;
953 } else {
954 rep->size = in->size;
955 memcpy(rep->data, in->data, in->size);
956 }
957 }
958 fio_mutex_up(&rep->lock);
959 break;
960 }
961 default:
962 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
963 ret = 1;
964 }
965
966 return ret;
967}
968
969/*
970 * Send a command with a separate PDU, not inlined in the command
971 */
972static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
973 off_t size, uint64_t tag, uint32_t flags)
974{
975 struct fio_net_cmd cmd;
976 struct iovec iov[2];
977
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;
982
983 __fio_init_net_cmd(&cmd, opcode, size, tag);
984 cmd.flags = __cpu_to_le32(flags);
985 fio_net_cmd_crc_pdu(&cmd, buf);
986
987 return fio_sendv_data(sk, iov, 2);
988}
989
990static void finish_entry(struct sk_entry *entry)
991{
992 if (entry->flags & SK_F_FREE)
993 free(entry->buf);
994 else if (entry->flags & SK_F_COPY)
995 sfree(entry->buf);
996
997 sfree(entry);
998}
999
1000static void send_vec_entry(struct sk_out *sk_out, struct sk_entry *first)
1001{
1002 uint64_t tag;
1003 int flags;
1004
1005 if (!flist_empty(&first->next))
1006 flags = FIO_NET_CMD_F_MORE;
1007 else
1008 flags = 0;
1009
1010 if (first->tagptr)
1011 tag = *first->tagptr;
1012 else
1013 tag = 0;
1014
1015 fio_send_cmd_ext_pdu(sk_out->sk, first->opcode, first->buf, first->size, tag, flags);
1016
1017 while (!flist_empty(&first->next)) {
1018 struct sk_entry *next;
1019
1020 next = flist_first_entry(&first->next, struct sk_entry, list);
1021 flist_del_init(&next->list);
1022 if (flist_empty(&first->next))
1023 flags = 0;
1024
1025 if (next->tagptr)
1026 tag = *next->tagptr;
1027 else
1028 tag = 0;
1029
1030 fio_send_cmd_ext_pdu(sk_out->sk, next->opcode, next->buf, next->size, tag, flags);
1031 finish_entry(next);
1032 }
1033}
1034
1035static void handle_sk_entry(struct sk_out *sk_out, struct sk_entry *entry)
1036{
1037 if (entry->flags & SK_F_VEC)
1038 send_vec_entry(sk_out, entry);
1039 if (entry->flags & SK_F_SIMPLE) {
1040 uint64_t tag = 0;
1041
1042 if (entry->tagptr)
1043 tag = *entry->tagptr;
1044
1045 fio_net_send_simple_cmd(sk_out->sk, entry->opcode, tag, NULL);
1046 } else
1047 fio_net_send_cmd(sk_out->sk, entry->opcode, entry->buf, entry->size, entry->tagptr, NULL);
1048
1049 finish_entry(entry);
1050}
1051
1052static void handle_xmits(struct sk_out *sk_out)
1053{
1054 struct sk_entry *entry;
1055 FLIST_HEAD(list);
1056
1057 sk_lock(sk_out);
1058 if (flist_empty(&sk_out->list)) {
1059 sk_unlock(sk_out);
1060 return;
1061 }
1062
1063 flist_splice_init(&sk_out->list, &list);
1064 sk_unlock(sk_out);
1065
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);
1070 }
1071}
1072
1073static int handle_connection(struct sk_out *sk_out)
1074{
1075 struct fio_net_cmd *cmd = NULL;
1076 FLIST_HEAD(job_list);
1077 int ret = 0;
1078
1079 reset_fio_state();
1080
1081 /* read forever */
1082 while (!exit_backend) {
1083 struct pollfd pfd = {
1084 .fd = sk_out->sk,
1085 .events = POLLIN,
1086 };
1087
1088 ret = 0;
1089 do {
1090 int timeout = 1000;
1091
1092 if (!flist_empty(&job_list))
1093 timeout = 100;
1094
1095 handle_xmits(sk_out);
1096
1097 ret = poll(&pfd, 1, 0);
1098 if (ret < 0) {
1099 if (errno == EINTR)
1100 break;
1101 log_err("fio: poll: %s\n", strerror(errno));
1102 break;
1103 } else if (!ret) {
1104 fio_server_check_jobs(&job_list);
1105 fio_mutex_down_timeout(sk_out->wait, timeout);
1106 continue;
1107 }
1108
1109 if (pfd.revents & POLLIN)
1110 break;
1111 if (pfd.revents & (POLLERR|POLLHUP)) {
1112 ret = 1;
1113 break;
1114 }
1115 } while (!exit_backend);
1116
1117 fio_server_check_jobs(&job_list);
1118
1119 if (ret < 0)
1120 break;
1121
1122 cmd = fio_net_recv_cmd(sk_out->sk);
1123 if (!cmd) {
1124 ret = -1;
1125 break;
1126 }
1127
1128 ret = handle_command(sk_out, &job_list, cmd);
1129 if (ret)
1130 break;
1131
1132 free(cmd);
1133 cmd = NULL;
1134 }
1135
1136 if (cmd)
1137 free(cmd);
1138
1139 handle_xmits(sk_out);
1140
1141 close(sk_out->sk);
1142 _exit(ret);
1143}
1144
1145/* get the address on this host bound by the input socket,
1146 * whether it is ipv6 or ipv4 */
1147
1148int get_my_addr_str(int sk)
1149{
1150 struct sockaddr_in6 myaddr6 = { 0, };
1151 struct sockaddr_in myaddr4 = { 0, };
1152 struct sockaddr *sockaddr_p;
1153 char *net_addr;
1154 socklen_t len;
1155 int ret;
1156
1157 if (use_ipv6) {
1158 len = sizeof(myaddr6);
1159 sockaddr_p = (struct sockaddr * )&myaddr6;
1160 net_addr = (char * )&myaddr6.sin6_addr;
1161 } else {
1162 len = sizeof(myaddr4);
1163 sockaddr_p = (struct sockaddr * )&myaddr4;
1164 net_addr = (char * )&myaddr4.sin_addr;
1165 }
1166
1167 ret = getsockname(sk, sockaddr_p, &len);
1168 if (ret) {
1169 log_err("fio: getsockaddr: %s\n", strerror(errno));
1170 return -1;
1171 }
1172
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");
1175 return -1;
1176 }
1177
1178 dprint(FD_NET, "fio server bound to addr %s\n", client_sockaddr_str);
1179 return 0;
1180}
1181
1182static int accept_loop(int listen_sk)
1183{
1184 struct sockaddr_in addr;
1185 struct sockaddr_in6 addr6;
1186 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
1187 struct pollfd pfd;
1188 int ret = 0, sk, exitval = 0;
1189 struct sk_out *sk_out;
1190 FLIST_HEAD(conn_list);
1191
1192 dprint(FD_NET, "server enter accept loop\n");
1193
1194 fio_set_fd_nonblocking(listen_sk, "server");
1195
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);
1200
1201 pthread_setspecific(sk_out_key, sk_out);
1202
1203 while (!exit_backend) {
1204 const char *from;
1205 char buf[64];
1206 pid_t pid;
1207
1208 pfd.fd = listen_sk;
1209 pfd.events = POLLIN;
1210 do {
1211 int timeout = 1000;
1212
1213 if (!flist_empty(&conn_list))
1214 timeout = 100;
1215
1216 ret = poll(&pfd, 1, timeout);
1217 if (ret < 0) {
1218 if (errno == EINTR)
1219 break;
1220 log_err("fio: poll: %s\n", strerror(errno));
1221 break;
1222 } else if (!ret) {
1223 fio_server_check_conns(&conn_list);
1224 continue;
1225 }
1226
1227 if (pfd.revents & POLLIN)
1228 break;
1229 } while (!exit_backend);
1230
1231 fio_server_check_conns(&conn_list);
1232
1233 if (exit_backend || ret < 0)
1234 break;
1235
1236 if (use_ipv6)
1237 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
1238 else
1239 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
1240
1241 if (sk < 0) {
1242 log_err("fio: accept: %s\n", strerror(errno));
1243 return -1;
1244 }
1245
1246 if (use_ipv6)
1247 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
1248 else
1249 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
1250
1251 dprint(FD_NET, "server: connect from %s\n", from);
1252
1253 sk_out->sk = sk;
1254
1255 pid = fork();
1256 if (pid) {
1257 close(sk);
1258 fio_server_add_conn_pid(&conn_list, pid);
1259 pthread_setspecific(sk_out_key, sk_out);
1260 continue;
1261 }
1262
1263 /* exits */
1264 get_my_addr_str(sk); /* if error, it's already logged, non-fatal */
1265 handle_connection(sk_out);
1266 }
1267
1268#if 0
1269 fio_mutex_remove(sk_out->lock);
1270 fio_mutex_remove(sk_out->wait);
1271 sfree(sk_out);
1272 pthread_setspecific(sk_out_key, NULL);
1273#endif
1274
1275 return exitval;
1276}
1277
1278int fio_server_text_output(int level, const char *buf, size_t len)
1279{
1280 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1281 struct cmd_text_pdu *pdu;
1282 unsigned int tlen;
1283 struct timeval tv;
1284
1285 if (!sk_out || sk_out->sk == -1)
1286 return -1;
1287
1288 tlen = sizeof(*pdu) + len;
1289 pdu = malloc(tlen);
1290
1291 pdu->level = __cpu_to_le32(level);
1292 pdu->buf_len = __cpu_to_le32(len);
1293
1294 gettimeofday(&tv, NULL);
1295 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1296 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1297
1298 memcpy(pdu->buf, buf, len);
1299
1300 fio_net_queue_cmd(FIO_NET_CMD_TEXT, pdu, tlen, NULL, SK_F_COPY);
1301 free(pdu);
1302 return len;
1303}
1304
1305static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1306{
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);
1310
1311 /*
1312 * Encode to IEEE 754 for network transfer
1313 */
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));
1316}
1317
1318static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1319{
1320 int i;
1321
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]);
1329 }
1330
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);
1335}
1336
1337/*
1338 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1339 * into a single payload.
1340 */
1341void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1342{
1343 struct cmd_ts_pdu p;
1344 int i, j;
1345
1346 dprint(FD_NET, "server sending end stats\n");
1347
1348 memset(&p, 0, sizeof(p));
1349
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);
1353
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);
1360
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]);
1366 }
1367
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);
1375
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];
1379
1380 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1381 }
1382
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]);
1387 }
1388
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]);
1392 }
1393
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]);
1397
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]);
1402 }
1403
1404 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1405 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1406
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]);
1410 }
1411
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);
1418
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));
1423
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]);
1427
1428 convert_gs(&p.rs, rs);
1429
1430 fio_net_queue_cmd(FIO_NET_CMD_TS, &p, sizeof(p), NULL, SK_F_COPY);
1431}
1432
1433void fio_server_send_gs(struct group_run_stats *rs)
1434{
1435 struct group_run_stats gs;
1436
1437 dprint(FD_NET, "server sending group run stats\n");
1438
1439 convert_gs(&gs, rs);
1440 fio_net_queue_cmd(FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, SK_F_COPY);
1441}
1442
1443static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1444{
1445 int i;
1446
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]);
1452 }
1453
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));
1458}
1459
1460static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1461{
1462 int i;
1463
1464 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1465 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1466
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]);
1472 }
1473
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);
1477}
1478
1479void fio_server_send_du(void)
1480{
1481 struct disk_util *du;
1482 struct flist_head *entry;
1483 struct cmd_du_pdu pdu;
1484
1485 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1486
1487 memset(&pdu, 0, sizeof(pdu));
1488
1489 flist_for_each(entry, &disk_list) {
1490 du = flist_entry(entry, struct disk_util, list);
1491
1492 convert_dus(&pdu.dus, &du->dus);
1493 convert_agg(&pdu.agg, &du->agg);
1494
1495 fio_net_queue_cmd(FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1496 }
1497}
1498
1499static int fio_send_iolog_gz(struct sk_entry *first, struct io_log *log)
1500{
1501 int ret = 0;
1502#ifdef CONFIG_ZLIB
1503 struct sk_entry *entry;
1504 z_stream stream;
1505 void *out_pdu;
1506
1507 /*
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.
1511 */
1512 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1513
1514 stream.zalloc = Z_NULL;
1515 stream.zfree = Z_NULL;
1516 stream.opaque = Z_NULL;
1517
1518 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
1519 ret = 1;
1520 goto err;
1521 }
1522
1523 stream.next_in = (void *) log->log;
1524 stream.avail_in = log->nr_samples * log_entry_sz(log);
1525
1526 do {
1527 unsigned int this_len;
1528
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 */
1533 if (ret < 0)
1534 goto err_zlib;
1535
1536 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1537
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);
1542
1543err_zlib:
1544 deflateEnd(&stream);
1545err:
1546 free(out_pdu);
1547#endif
1548 return ret;
1549}
1550
1551int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1552{
1553 struct cmd_iolog_pdu pdu;
1554 struct sk_entry *first;
1555 int i, ret = 0;
1556
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);
1561
1562 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1563 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1564
1565 for (i = 0; i < log->nr_samples; i++) {
1566 struct io_sample *s = get_sample(log, i);
1567
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);
1572
1573 if (log->log_offset) {
1574 struct io_sample_offset *so = (void *) s;
1575
1576 so->offset = cpu_to_le64(so->offset);
1577 }
1578 }
1579
1580 /*
1581 * Assemble header entry first
1582 */
1583 first = fio_net_prep_cmd(FIO_NET_CMD_IOLOG, &pdu, sizeof(pdu), NULL, SK_F_COPY | SK_F_VEC);
1584
1585 /*
1586 * Now append actual log entries. Compress if we can, otherwise just
1587 * plain text output.
1588 */
1589 if (use_zlib)
1590 ret = fio_send_iolog_gz(first, log);
1591 else {
1592 struct sk_entry *entry;
1593
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);
1598 }
1599
1600 return ret;
1601}
1602
1603void fio_server_send_add_job(struct thread_data *td)
1604{
1605 struct cmd_add_job_pdu pdu;
1606
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);
1611
1612 fio_net_queue_cmd(FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, SK_F_COPY);
1613}
1614
1615void fio_server_send_start(struct thread_data *td)
1616{
1617 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
1618
1619 assert(sk_out->sk != -1);
1620
1621 fio_net_queue_cmd(FIO_NET_CMD_SERVER_START, NULL, 0, 0, SK_F_SIMPLE);
1622}
1623
1624int fio_server_get_verify_state(const char *name, int threadnumber,
1625 void **datap, int *version)
1626{
1627 struct thread_io_list *s;
1628 struct cmd_sendfile out;
1629 struct cmd_reply *rep;
1630 uint64_t tag;
1631 void *data;
1632
1633 dprint(FD_NET, "server: request verify state\n");
1634
1635 rep = smalloc(sizeof(*rep));
1636 if (!rep) {
1637 log_err("fio: smalloc pool too small\n");
1638 return 1;
1639 }
1640
1641 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1642 rep->data = NULL;
1643 rep->error = 0;
1644
1645 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1646 threadnumber);
1647 tag = (uint64_t) (uintptr_t) rep;
1648 fio_net_queue_cmd(FIO_NET_CMD_SENDFILE, &out, sizeof(out), &tag, SK_F_COPY);
1649
1650 /*
1651 * Wait for the backend to receive the reply
1652 */
1653 if (fio_mutex_down_timeout(&rep->lock, 10000)) {
1654 log_err("fio: timed out waiting for reply\n");
1655 goto fail;
1656 }
1657
1658 if (rep->error) {
1659 log_err("fio: failure on receiving state file: %s\n", strerror(rep->error));
1660fail:
1661 *datap = NULL;
1662 sfree(rep);
1663 fio_net_queue_quit();
1664 return 1;
1665 }
1666
1667 /*
1668 * The format is verify_state_hdr, then thread_io_list. Verify
1669 * the header, and the thread_io_list checksum
1670 */
1671 s = rep->data + sizeof(struct verify_state_hdr);
1672 if (verify_state_hdr(rep->data, s, version))
1673 goto fail;
1674
1675 /*
1676 * Don't need the header from now, copy just the thread_io_list
1677 */
1678 rep->size -= sizeof(struct verify_state_hdr);
1679 data = malloc(rep->size);
1680 memcpy(data, s, rep->size);
1681 *datap = data;
1682
1683 sfree(rep->data);
1684 __fio_mutex_remove(&rep->lock);
1685 sfree(rep);
1686 return 0;
1687}
1688
1689static int fio_init_server_ip(void)
1690{
1691 struct sockaddr *addr;
1692 socklen_t socklen;
1693 char buf[80];
1694 const char *str;
1695 int sk, opt;
1696
1697 if (use_ipv6)
1698 sk = socket(AF_INET6, SOCK_STREAM, 0);
1699 else
1700 sk = socket(AF_INET, SOCK_STREAM, 0);
1701
1702 if (sk < 0) {
1703 log_err("fio: socket: %s\n", strerror(errno));
1704 return -1;
1705 }
1706
1707 opt = 1;
1708 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1709 log_err("fio: setsockopt(REUSEADDR): %s\n", strerror(errno));
1710 close(sk);
1711 return -1;
1712 }
1713#ifdef SO_REUSEPORT
1714 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
1715 log_err("fio: setsockopt(REUSEPORT): %s\n", strerror(errno));
1716 close(sk);
1717 return -1;
1718 }
1719#endif
1720
1721 if (use_ipv6) {
1722 const void *src = &saddr_in6.sin6_addr;
1723
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));
1728 } else {
1729 const void *src = &saddr_in.sin_addr;
1730
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));
1735 }
1736
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);
1740 close(sk);
1741 return -1;
1742 }
1743
1744 return sk;
1745}
1746
1747static int fio_init_server_sock(void)
1748{
1749 struct sockaddr_un addr;
1750 socklen_t len;
1751 mode_t mode;
1752 int sk;
1753
1754 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1755 if (sk < 0) {
1756 log_err("fio: socket: %s\n", strerror(errno));
1757 return -1;
1758 }
1759
1760 mode = umask(000);
1761
1762 memset(&addr, 0, sizeof(addr));
1763 addr.sun_family = AF_UNIX;
1764 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
1765
1766 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1767
1768 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1769 log_err("fio: bind: %s\n", strerror(errno));
1770 close(sk);
1771 return -1;
1772 }
1773
1774 umask(mode);
1775 return sk;
1776}
1777
1778static int fio_init_server_connection(void)
1779{
1780 char bind_str[128];
1781 int sk;
1782
1783 dprint(FD_NET, "starting server\n");
1784
1785 if (!bind_sock)
1786 sk = fio_init_server_ip();
1787 else
1788 sk = fio_init_server_sock();
1789
1790 if (sk < 0)
1791 return sk;
1792
1793 memset(bind_str, 0, sizeof(bind_str));
1794
1795 if (!bind_sock) {
1796 char *p, port[16];
1797 const void *src;
1798 int af;
1799
1800 if (use_ipv6) {
1801 af = AF_INET6;
1802 src = &saddr_in6.sin6_addr;
1803 } else {
1804 af = AF_INET;
1805 src = &saddr_in.sin_addr;
1806 }
1807
1808 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1809
1810 sprintf(port, ",%u", fio_net_port);
1811 if (p)
1812 strcat(p, port);
1813 else
1814 strncpy(bind_str, port, sizeof(bind_str) - 1);
1815 } else
1816 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
1817
1818 log_info("fio: server listening on %s\n", bind_str);
1819
1820 if (listen(sk, 0) < 0) {
1821 log_err("fio: listen: %s\n", strerror(errno));
1822 close(sk);
1823 return -1;
1824 }
1825
1826 return sk;
1827}
1828
1829int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
1830 struct in6_addr *inp6)
1831
1832{
1833 int ret = 0;
1834
1835 if (ipv6)
1836 ret = inet_pton(AF_INET6, host, inp6);
1837 else
1838 ret = inet_pton(AF_INET, host, inp);
1839
1840 if (ret != 1) {
1841 struct addrinfo hints, *res;
1842
1843 memset(&hints, 0, sizeof(hints));
1844 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
1845 hints.ai_socktype = SOCK_STREAM;
1846
1847 ret = getaddrinfo(host, NULL, &hints, &res);
1848 if (ret) {
1849 log_err("fio: failed to resolve <%s> (%s)\n", host,
1850 gai_strerror(ret));
1851 return 1;
1852 }
1853
1854 if (ipv6)
1855 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
1856 else
1857 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
1858
1859 ret = 1;
1860 freeaddrinfo(res);
1861 }
1862
1863 return !(ret == 1);
1864}
1865
1866/*
1867 * Parse a host/ip/port string. Reads from 'str'.
1868 *
1869 * Outputs:
1870 *
1871 * For IPv4:
1872 * *ptr is the host, *port is the port, inp is the destination.
1873 * For IPv6:
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.
1877 */
1878int 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)
1881{
1882 const char *host = str;
1883 char *portp;
1884 int lport = 0;
1885
1886 *ptr = NULL;
1887 *is_sock = 0;
1888 *port = fio_net_port;
1889 *ipv6 = 0;
1890
1891 if (!strncmp(str, "sock:", 5)) {
1892 *ptr = strdup(str + 5);
1893 *is_sock = 1;
1894
1895 return 0;
1896 }
1897
1898 /*
1899 * Is it ip:<ip or host>:port
1900 */
1901 if (!strncmp(host, "ip:", 3))
1902 host += 3;
1903 else if (!strncmp(host, "ip4:", 4))
1904 host += 4;
1905 else if (!strncmp(host, "ip6:", 4)) {
1906 host += 4;
1907 *ipv6 = 1;
1908 } else if (host[0] == ':') {
1909 /* String is :port */
1910 host++;
1911 lport = atoi(host);
1912 if (!lport || lport > 65535) {
1913 log_err("fio: bad server port %u\n", lport);
1914 return 1;
1915 }
1916 /* no hostname given, we are done */
1917 *port = lport;
1918 return 0;
1919 }
1920
1921 /*
1922 * If no port seen yet, check if there's a last ',' at the end
1923 */
1924 if (!lport) {
1925 portp = strchr(host, ',');
1926 if (portp) {
1927 *portp = '\0';
1928 portp++;
1929 lport = atoi(portp);
1930 if (!lport || lport > 65535) {
1931 log_err("fio: bad server port %u\n", lport);
1932 return 1;
1933 }
1934 }
1935 }
1936
1937 if (lport)
1938 *port = lport;
1939
1940 if (!strlen(host))
1941 return 0;
1942
1943 *ptr = strdup(host);
1944
1945 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
1946 free(*ptr);
1947 *ptr = NULL;
1948 return 1;
1949 }
1950
1951 if (*port == 0)
1952 *port = fio_net_port;
1953
1954 return 0;
1955}
1956
1957/*
1958 * Server arg should be one of:
1959 *
1960 * sock:/path/to/socket
1961 * ip:1.2.3.4
1962 * 1.2.3.4
1963 *
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.
1967 *
1968 */
1969static int fio_handle_server_arg(void)
1970{
1971 int port = fio_net_port;
1972 int is_sock, ret = 0;
1973
1974 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1975
1976 if (!fio_server_arg)
1977 goto out;
1978
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);
1982
1983 if (!is_sock && bind_sock) {
1984 free(bind_sock);
1985 bind_sock = NULL;
1986 }
1987
1988out:
1989 fio_net_port = port;
1990 saddr_in.sin_port = htons(port);
1991 saddr_in6.sin6_port = htons(port);
1992 return ret;
1993}
1994
1995static void sig_int(int sig)
1996{
1997 if (bind_sock)
1998 unlink(bind_sock);
1999}
2000
2001static void set_sig_handlers(void)
2002{
2003 struct sigaction act;
2004
2005 memset(&act, 0, sizeof(act));
2006 act.sa_handler = sig_int;
2007 act.sa_flags = SA_RESTART;
2008 sigaction(SIGINT, &act, NULL);
2009}
2010
2011static int fio_server(void)
2012{
2013 int sk, ret;
2014
2015 dprint(FD_NET, "starting server\n");
2016
2017 if (fio_handle_server_arg())
2018 return -1;
2019
2020 sk = fio_init_server_connection();
2021 if (sk < 0)
2022 return -1;
2023
2024 set_sig_handlers();
2025
2026 if (pthread_key_create(&sk_out_key, NULL))
2027 log_err("fio: can't create sk_out backend key\n");
2028
2029 ret = accept_loop(sk);
2030
2031 close(sk);
2032
2033 if (fio_server_arg) {
2034 free(fio_server_arg);
2035 fio_server_arg = NULL;
2036 }
2037 if (bind_sock)
2038 free(bind_sock);
2039
2040 return ret;
2041}
2042
2043void fio_server_got_signal(int signal)
2044{
2045 struct sk_out *sk_out = pthread_getspecific(sk_out_key);
2046
2047 assert(sk_out);
2048
2049 if (signal == SIGPIPE)
2050 sk_out->sk = -1;
2051 else {
2052 log_info("\nfio: terminating on signal %d\n", signal);
2053 exit_backend = 1;
2054 }
2055}
2056
2057static int check_existing_pidfile(const char *pidfile)
2058{
2059 struct stat sb;
2060 char buf[16];
2061 pid_t pid;
2062 FILE *f;
2063
2064 if (stat(pidfile, &sb))
2065 return 0;
2066
2067 f = fopen(pidfile, "r");
2068 if (!f)
2069 return 0;
2070
2071 if (fread(buf, sb.st_size, 1, f) <= 0) {
2072 fclose(f);
2073 return 1;
2074 }
2075 fclose(f);
2076
2077 pid = atoi(buf);
2078 if (kill(pid, SIGCONT) < 0)
2079 return errno != ESRCH;
2080
2081 return 1;
2082}
2083
2084static int write_pid(pid_t pid, const char *pidfile)
2085{
2086 FILE *fpid;
2087
2088 fpid = fopen(pidfile, "w");
2089 if (!fpid) {
2090 log_err("fio: failed opening pid file %s\n", pidfile);
2091 return 1;
2092 }
2093
2094 fprintf(fpid, "%u\n", (unsigned int) pid);
2095 fclose(fpid);
2096 return 0;
2097}
2098
2099/*
2100 * If pidfile is specified, background us.
2101 */
2102int fio_start_server(char *pidfile)
2103{
2104 pid_t pid;
2105 int ret;
2106
2107#if defined(WIN32)
2108 WSADATA wsd;
2109 WSAStartup(MAKEWORD(2, 2), &wsd);
2110#endif
2111
2112 if (!pidfile)
2113 return fio_server();
2114
2115 if (check_existing_pidfile(pidfile)) {
2116 log_err("fio: pidfile %s exists and server appears alive\n",
2117 pidfile);
2118 free(pidfile);
2119 return -1;
2120 }
2121
2122 pid = fork();
2123 if (pid < 0) {
2124 log_err("fio: failed server fork: %s", strerror(errno));
2125 free(pidfile);
2126 return -1;
2127 } else if (pid) {
2128 ret = write_pid(pid, pidfile);
2129 free(pidfile);
2130 _exit(ret);
2131 }
2132
2133 setsid();
2134 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
2135 log_syslog = 1;
2136 close(STDIN_FILENO);
2137 close(STDOUT_FILENO);
2138 close(STDERR_FILENO);
2139 f_out = NULL;
2140 f_err = NULL;
2141
2142 ret = fio_server();
2143
2144 closelog();
2145 unlink(pidfile);
2146 free(pidfile);
2147 return ret;
2148}
2149
2150void fio_server_set_arg(const char *arg)
2151{
2152 fio_server_arg = strdup(arg);
2153}