Merge branch 'req4' of git://github.com/kusumi/fio
[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 "server.h"
25#include "crc/crc16.h"
26#include "lib/ieee754.h"
27#include "verify.h"
28#include "smalloc.h"
29
30int fio_net_port = FIO_NET_PORT;
31
32int exit_backend = 0;
33
34static int server_fd = -1;
35static char *fio_server_arg;
36static char *bind_sock;
37static struct sockaddr_in saddr_in;
38static struct sockaddr_in6 saddr_in6;
39static int use_ipv6;
40#ifdef CONFIG_ZLIB
41static unsigned int has_zlib = 1;
42#else
43static unsigned int has_zlib = 0;
44#endif
45static unsigned int use_zlib;
46static char me[128];
47
48struct fio_fork_item {
49 struct flist_head list;
50 int exitval;
51 int signal;
52 int exited;
53 pid_t pid;
54};
55
56struct cmd_reply {
57 struct fio_mutex lock;
58 void *data;
59 size_t size;
60 int error;
61};
62
63static const char *fio_server_ops[FIO_NET_CMD_NR] = {
64 "",
65 "QUIT",
66 "EXIT",
67 "JOB",
68 "JOBLINE",
69 "TEXT",
70 "TS",
71 "GS",
72 "SEND_ETA",
73 "ETA",
74 "PROBE",
75 "START",
76 "STOP",
77 "DISK_UTIL",
78 "SERVER_START",
79 "ADD_JOB",
80 "RUN",
81 "IOLOG",
82 "UPDATE_JOB",
83 "LOAD_FILE",
84 "VTRIGGER",
85 "SENDFILE",
86};
87
88const char *fio_server_op(unsigned int op)
89{
90 static char buf[32];
91
92 if (op < FIO_NET_CMD_NR)
93 return fio_server_ops[op];
94
95 sprintf(buf, "UNKNOWN/%d", op);
96 return buf;
97}
98
99static ssize_t iov_total_len(const struct iovec *iov, int count)
100{
101 ssize_t ret = 0;
102
103 while (count--) {
104 ret += iov->iov_len;
105 iov++;
106 }
107
108 return ret;
109}
110
111static int fio_sendv_data(int sk, struct iovec *iov, int count)
112{
113 ssize_t total_len = iov_total_len(iov, count);
114 ssize_t ret;
115
116 do {
117 ret = writev(sk, iov, count);
118 if (ret > 0) {
119 total_len -= ret;
120 if (!total_len)
121 break;
122
123 while (ret) {
124 if (ret >= iov->iov_len) {
125 ret -= iov->iov_len;
126 iov++;
127 continue;
128 }
129 iov->iov_base += ret;
130 iov->iov_len -= ret;
131 ret = 0;
132 }
133 } else if (!ret)
134 break;
135 else if (errno == EAGAIN || errno == EINTR)
136 continue;
137 else
138 break;
139 } while (!exit_backend);
140
141 if (!total_len)
142 return 0;
143
144 if (errno)
145 return -errno;
146
147 return 1;
148}
149
150int fio_send_data(int sk, const void *p, unsigned int len)
151{
152 struct iovec iov = { .iov_base = (void *) p, .iov_len = len };
153
154 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU);
155
156 return fio_sendv_data(sk, &iov, 1);
157}
158
159int fio_recv_data(int sk, void *p, unsigned int len)
160{
161 do {
162 int ret = recv(sk, p, len, MSG_WAITALL);
163
164 if (ret > 0) {
165 len -= ret;
166 if (!len)
167 break;
168 p += ret;
169 continue;
170 } else if (!ret)
171 break;
172 else if (errno == EAGAIN || errno == EINTR)
173 continue;
174 else
175 break;
176 } while (!exit_backend);
177
178 if (!len)
179 return 0;
180
181 return -1;
182}
183
184static int verify_convert_cmd(struct fio_net_cmd *cmd)
185{
186 uint16_t crc;
187
188 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
189 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
190
191 crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ);
192 if (crc != cmd->cmd_crc16) {
193 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
194 cmd->cmd_crc16, crc);
195 return 1;
196 }
197
198 cmd->version = le16_to_cpu(cmd->version);
199 cmd->opcode = le16_to_cpu(cmd->opcode);
200 cmd->flags = le32_to_cpu(cmd->flags);
201 cmd->tag = le64_to_cpu(cmd->tag);
202 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
203
204 switch (cmd->version) {
205 case FIO_SERVER_VER:
206 break;
207 default:
208 log_err("fio: bad server cmd version %d\n", cmd->version);
209 return 1;
210 }
211
212 if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) {
213 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
214 return 1;
215 }
216
217 return 0;
218}
219
220/*
221 * Read (and defragment, if necessary) incoming commands
222 */
223struct fio_net_cmd *fio_net_recv_cmd(int sk)
224{
225 struct fio_net_cmd cmd, *tmp, *cmdret = NULL;
226 size_t cmd_size = 0, pdu_offset = 0;
227 uint16_t crc;
228 int ret, first = 1;
229 void *pdu = NULL;
230
231 do {
232 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
233 if (ret)
234 break;
235
236 /* We have a command, verify it and swap if need be */
237 ret = verify_convert_cmd(&cmd);
238 if (ret)
239 break;
240
241 if (first) {
242 /* if this is text, add room for \0 at the end */
243 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
244 assert(!cmdret);
245 } else
246 cmd_size += cmd.pdu_len;
247
248 if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) {
249 log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size);
250 ret = 1;
251 break;
252 }
253
254 tmp = realloc(cmdret, cmd_size);
255 if (!tmp) {
256 log_err("fio: server failed allocating cmd\n");
257 ret = 1;
258 break;
259 }
260 cmdret = tmp;
261
262 if (first)
263 memcpy(cmdret, &cmd, sizeof(cmd));
264 else if (cmdret->opcode != cmd.opcode) {
265 log_err("fio: fragment opcode mismatch (%d != %d)\n",
266 cmdret->opcode, cmd.opcode);
267 ret = 1;
268 break;
269 }
270
271 if (!cmd.pdu_len)
272 break;
273
274 /* There's payload, get it */
275 pdu = (void *) cmdret->payload + pdu_offset;
276 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
277 if (ret)
278 break;
279
280 /* Verify payload crc */
281 crc = fio_crc16(pdu, cmd.pdu_len);
282 if (crc != cmd.pdu_crc16) {
283 log_err("fio: server bad crc on payload ");
284 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
285 ret = 1;
286 break;
287 }
288
289 pdu_offset += cmd.pdu_len;
290 if (!first)
291 cmdret->pdu_len += cmd.pdu_len;
292 first = 0;
293 } while (cmd.flags & FIO_NET_CMD_F_MORE);
294
295 if (ret) {
296 free(cmdret);
297 cmdret = NULL;
298 } else if (cmdret) {
299 /* zero-terminate text input */
300 if (cmdret->pdu_len) {
301 if (cmdret->opcode == FIO_NET_CMD_TEXT) {
302 struct cmd_text_pdu *__pdu = (struct cmd_text_pdu *) cmdret->payload;
303 char *buf = (char *) __pdu->buf;
304
305 buf[__pdu->buf_len] = '\0';
306 } else if (cmdret->opcode == FIO_NET_CMD_JOB) {
307 struct cmd_job_pdu *__pdu = (struct cmd_job_pdu *) cmdret->payload;
308 char *buf = (char *) __pdu->buf;
309 int len = le32_to_cpu(__pdu->buf_len);
310
311 buf[len] = '\0';
312 }
313 }
314
315 /* frag flag is internal */
316 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
317 }
318
319 return cmdret;
320}
321
322static void add_reply(uint64_t tag, struct flist_head *list)
323{
324 struct fio_net_cmd_reply *reply;
325
326 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
327 flist_add_tail(&reply->list, list);
328}
329
330static uint64_t alloc_reply(uint64_t tag, uint16_t opcode)
331{
332 struct fio_net_cmd_reply *reply;
333
334 reply = calloc(1, sizeof(*reply));
335 INIT_FLIST_HEAD(&reply->list);
336 fio_gettime(&reply->tv, NULL);
337 reply->saved_tag = tag;
338 reply->opcode = opcode;
339
340 return (uintptr_t) reply;
341}
342
343static void free_reply(uint64_t tag)
344{
345 struct fio_net_cmd_reply *reply;
346
347 reply = (struct fio_net_cmd_reply *) (uintptr_t) tag;
348 free(reply);
349}
350
351void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu)
352{
353 uint32_t pdu_len;
354
355 cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ));
356
357 pdu_len = le32_to_cpu(cmd->pdu_len);
358 cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len));
359}
360
361void fio_net_cmd_crc(struct fio_net_cmd *cmd)
362{
363 fio_net_cmd_crc_pdu(cmd, cmd->payload);
364}
365
366int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
367 uint64_t *tagptr, struct flist_head *list)
368{
369 struct fio_net_cmd *cmd = NULL;
370 size_t this_len, cur_len = 0;
371 uint64_t tag;
372 int ret;
373
374 if (list) {
375 assert(tagptr);
376 tag = *tagptr = alloc_reply(*tagptr, opcode);
377 } else
378 tag = tagptr ? *tagptr : 0;
379
380 do {
381 this_len = size;
382 if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU)
383 this_len = FIO_SERVER_MAX_FRAGMENT_PDU;
384
385 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
386 if (cmd)
387 free(cmd);
388
389 cur_len = sizeof(*cmd) + this_len;
390 cmd = malloc(cur_len);
391 }
392
393 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
394
395 if (this_len < size)
396 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
397
398 fio_net_cmd_crc(cmd);
399
400 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
401 size -= this_len;
402 buf += this_len;
403 } while (!ret && size);
404
405 if (list) {
406 if (ret)
407 free_reply(tag);
408 else
409 add_reply(tag, list);
410 }
411
412 if (cmd)
413 free(cmd);
414
415 return ret;
416}
417
418static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
419{
420 struct fio_net_cmd cmd;
421
422 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
423 fio_net_cmd_crc(&cmd);
424
425 return fio_send_data(sk, &cmd, sizeof(cmd));
426}
427
428/*
429 * If 'list' is non-NULL, then allocate and store the sent command for
430 * later verification.
431 */
432int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
433 struct flist_head *list)
434{
435 int ret;
436
437 if (list)
438 tag = alloc_reply(tag, opcode);
439
440 ret = fio_net_send_simple_stack_cmd(sk, opcode, tag);
441 if (ret) {
442 if (list)
443 free_reply(tag);
444
445 return ret;
446 }
447
448 if (list)
449 add_reply(tag, list);
450
451 return 0;
452}
453
454int fio_net_send_quit(int sk)
455{
456 dprint(FD_NET, "server: sending quit\n");
457
458 return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL);
459}
460
461static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error,
462 int signal)
463{
464 struct cmd_end_pdu epdu;
465 uint64_t tag = 0;
466
467 if (cmd)
468 tag = cmd->tag;
469
470 epdu.error = __cpu_to_le32(error);
471 epdu.signal = __cpu_to_le32(signal);
472 return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL);
473}
474
475int fio_net_send_stop(int sk, int error, int signal)
476{
477 dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal);
478 return fio_net_send_ack(sk, NULL, error, signal);
479}
480
481static void fio_server_add_fork_item(pid_t pid, struct flist_head *list)
482{
483 struct fio_fork_item *ffi;
484
485 ffi = malloc(sizeof(*ffi));
486 ffi->exitval = 0;
487 ffi->signal = 0;
488 ffi->exited = 0;
489 ffi->pid = pid;
490 flist_add_tail(&ffi->list, list);
491}
492
493static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid)
494{
495 dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid);
496 fio_server_add_fork_item(pid, conn_list);
497}
498
499static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid)
500{
501 dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid);
502 fio_server_add_fork_item(pid, job_list);
503}
504
505static void fio_server_check_fork_item(struct fio_fork_item *ffi)
506{
507 int ret, status;
508
509 ret = waitpid(ffi->pid, &status, WNOHANG);
510 if (ret < 0) {
511 if (errno == ECHILD) {
512 log_err("fio: connection pid %u disappeared\n", (int) ffi->pid);
513 ffi->exited = 1;
514 } else
515 log_err("fio: waitpid: %s\n", strerror(errno));
516 } else if (ret == ffi->pid) {
517 if (WIFSIGNALED(status)) {
518 ffi->signal = WTERMSIG(status);
519 ffi->exited = 1;
520 }
521 if (WIFEXITED(status)) {
522 if (WEXITSTATUS(status))
523 ffi->exitval = WEXITSTATUS(status);
524 ffi->exited = 1;
525 }
526 }
527}
528
529static void fio_server_fork_item_done(struct fio_fork_item *ffi)
530{
531 dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval);
532
533 /*
534 * Fold STOP and QUIT...
535 */
536 fio_net_send_stop(server_fd, ffi->exitval, ffi->signal);
537 fio_net_send_quit(server_fd);
538 flist_del(&ffi->list);
539 free(ffi);
540}
541
542static void fio_server_check_fork_items(struct flist_head *list)
543{
544 struct flist_head *entry, *tmp;
545 struct fio_fork_item *ffi;
546
547 flist_for_each_safe(entry, tmp, list) {
548 ffi = flist_entry(entry, struct fio_fork_item, list);
549
550 fio_server_check_fork_item(ffi);
551
552 if (ffi->exited)
553 fio_server_fork_item_done(ffi);
554 }
555}
556
557static void fio_server_check_jobs(struct flist_head *job_list)
558{
559 fio_server_check_fork_items(job_list);
560}
561
562static void fio_server_check_conns(struct flist_head *conn_list)
563{
564 fio_server_check_fork_items(conn_list);
565}
566
567static int handle_load_file_cmd(struct fio_net_cmd *cmd)
568{
569 struct cmd_load_file_pdu *pdu = (struct cmd_load_file_pdu *) cmd->payload;
570 void *file_name = pdu->file;
571 struct cmd_start_pdu spdu;
572
573 dprint(FD_NET, "server: loading local file %s\n", (char *) file_name);
574
575 pdu->name_len = le16_to_cpu(pdu->name_len);
576 pdu->client_type = le16_to_cpu(pdu->client_type);
577
578 if (parse_jobs_ini(file_name, 0, 0, pdu->client_type)) {
579 fio_net_send_quit(server_fd);
580 return -1;
581 }
582
583 spdu.jobs = cpu_to_le32(thread_number);
584 spdu.stat_outputs = cpu_to_le32(stat_number);
585 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
586 return 0;
587}
588
589static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd)
590{
591 pid_t pid;
592 int ret;
593
594 fio_time_init();
595 set_genesis_time();
596
597 pid = fork();
598 if (pid) {
599 fio_server_add_job_pid(job_list, pid);
600 return 0;
601 }
602
603 ret = fio_backend();
604 free_threads_shm();
605 _exit(ret);
606}
607
608static int handle_job_cmd(struct fio_net_cmd *cmd)
609{
610 struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload;
611 void *buf = pdu->buf;
612 struct cmd_start_pdu spdu;
613
614 pdu->buf_len = le32_to_cpu(pdu->buf_len);
615 pdu->client_type = le32_to_cpu(pdu->client_type);
616
617 if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) {
618 fio_net_send_quit(server_fd);
619 return -1;
620 }
621
622 spdu.jobs = cpu_to_le32(thread_number);
623 spdu.stat_outputs = cpu_to_le32(stat_number);
624 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
625 return 0;
626}
627
628static int handle_jobline_cmd(struct fio_net_cmd *cmd)
629{
630 void *pdu = cmd->payload;
631 struct cmd_single_line_pdu *cslp;
632 struct cmd_line_pdu *clp;
633 unsigned long offset;
634 struct cmd_start_pdu spdu;
635 char **argv;
636 int i;
637
638 clp = pdu;
639 clp->lines = le16_to_cpu(clp->lines);
640 clp->client_type = le16_to_cpu(clp->client_type);
641 argv = malloc(clp->lines * sizeof(char *));
642 offset = sizeof(*clp);
643
644 dprint(FD_NET, "server: %d command line args\n", clp->lines);
645
646 for (i = 0; i < clp->lines; i++) {
647 cslp = pdu + offset;
648 argv[i] = (char *) cslp->text;
649
650 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
651 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
652 }
653
654 if (parse_cmd_line(clp->lines, argv, clp->client_type)) {
655 fio_net_send_quit(server_fd);
656 free(argv);
657 return -1;
658 }
659
660 free(argv);
661
662 spdu.jobs = cpu_to_le32(thread_number);
663 spdu.stat_outputs = cpu_to_le32(stat_number);
664 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL);
665 return 0;
666}
667
668static int handle_probe_cmd(struct fio_net_cmd *cmd)
669{
670 struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload;
671 struct cmd_probe_reply_pdu probe;
672 uint64_t tag = cmd->tag;
673
674 dprint(FD_NET, "server: sending probe reply\n");
675
676 strcpy(me, (char *) pdu->server);
677
678 memset(&probe, 0, sizeof(probe));
679 gethostname((char *) probe.hostname, sizeof(probe.hostname));
680#ifdef CONFIG_BIG_ENDIAN
681 probe.bigendian = 1;
682#endif
683 strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version));
684
685 probe.os = FIO_OS;
686 probe.arch = FIO_ARCH;
687 probe.bpp = sizeof(void *);
688 probe.cpus = __cpu_to_le32(cpus_online());
689
690 /*
691 * If the client supports compression and we do too, then enable it
692 */
693 if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) {
694 probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB);
695 use_zlib = 1;
696 } else {
697 probe.flags = 0;
698 use_zlib = 0;
699 }
700
701 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL);
702}
703
704static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
705{
706 struct jobs_eta *je;
707 uint64_t tag = cmd->tag;
708 size_t size;
709 int i;
710
711 je = get_jobs_eta(1, &size);
712 if (!je)
713 return 0;
714
715 dprint(FD_NET, "server sending status\n");
716
717 je->nr_running = cpu_to_le32(je->nr_running);
718 je->nr_ramp = cpu_to_le32(je->nr_ramp);
719 je->nr_pending = cpu_to_le32(je->nr_pending);
720 je->nr_setting_up = cpu_to_le32(je->nr_setting_up);
721 je->files_open = cpu_to_le32(je->files_open);
722
723 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
724 je->m_rate[i] = cpu_to_le32(je->m_rate[i]);
725 je->t_rate[i] = cpu_to_le32(je->t_rate[i]);
726 je->m_iops[i] = cpu_to_le32(je->m_iops[i]);
727 je->t_iops[i] = cpu_to_le32(je->t_iops[i]);
728 je->rate[i] = cpu_to_le32(je->rate[i]);
729 je->iops[i] = cpu_to_le32(je->iops[i]);
730 }
731
732 je->elapsed_sec = cpu_to_le64(je->elapsed_sec);
733 je->eta_sec = cpu_to_le64(je->eta_sec);
734 je->nr_threads = cpu_to_le32(je->nr_threads);
735 je->is_pow2 = cpu_to_le32(je->is_pow2);
736 je->unit_base = cpu_to_le32(je->unit_base);
737
738 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL);
739 free(je);
740 return 0;
741}
742
743static int send_update_job_reply(int fd, uint64_t __tag, int error)
744{
745 uint64_t tag = __tag;
746 uint32_t pdu_error;
747
748 pdu_error = __cpu_to_le32(error);
749 return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL);
750}
751
752static int handle_update_job_cmd(struct fio_net_cmd *cmd)
753{
754 struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload;
755 struct thread_data *td;
756 uint32_t tnumber;
757
758 tnumber = le32_to_cpu(pdu->thread_number);
759
760 dprint(FD_NET, "server: updating options for job %u\n", tnumber);
761
762 if (!tnumber || tnumber > thread_number) {
763 send_update_job_reply(server_fd, cmd->tag, ENODEV);
764 return 0;
765 }
766
767 td = &threads[tnumber - 1];
768 convert_thread_options_to_cpu(&td->o, &pdu->top);
769 send_update_job_reply(server_fd, cmd->tag, 0);
770 return 0;
771}
772
773static int handle_trigger_cmd(struct fio_net_cmd *cmd)
774{
775 struct cmd_vtrigger_pdu *pdu = (struct cmd_vtrigger_pdu *) cmd->payload;
776 char *buf = (char *) pdu->cmd;
777 struct all_io_list *rep;
778 size_t sz;
779
780 pdu->len = le16_to_cpu(pdu->len);
781 buf[pdu->len] = '\0';
782
783 rep = get_all_io_list(IO_LIST_ALL, &sz);
784 if (!rep) {
785 struct all_io_list state;
786
787 state.threads = cpu_to_le64((uint64_t) 0);
788 fio_net_send_cmd(server_fd, FIO_NET_CMD_VTRIGGER, &state, sizeof(state), NULL, NULL);
789 } else {
790 fio_net_send_cmd(server_fd, FIO_NET_CMD_VTRIGGER, rep, sz, NULL, NULL);
791 free(rep);
792 }
793
794 exec_trigger(buf);
795 return 0;
796}
797
798static int handle_command(struct flist_head *job_list, struct fio_net_cmd *cmd)
799{
800 int ret;
801
802 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n",
803 fio_server_op(cmd->opcode), cmd->pdu_len,
804 (unsigned long long) cmd->tag);
805
806 switch (cmd->opcode) {
807 case FIO_NET_CMD_QUIT:
808 fio_terminate_threads(TERMINATE_ALL);
809 return -1;
810 case FIO_NET_CMD_EXIT:
811 exit_backend = 1;
812 return -1;
813 case FIO_NET_CMD_LOAD_FILE:
814 ret = handle_load_file_cmd(cmd);
815 break;
816 case FIO_NET_CMD_JOB:
817 ret = handle_job_cmd(cmd);
818 break;
819 case FIO_NET_CMD_JOBLINE:
820 ret = handle_jobline_cmd(cmd);
821 break;
822 case FIO_NET_CMD_PROBE:
823 ret = handle_probe_cmd(cmd);
824 break;
825 case FIO_NET_CMD_SEND_ETA:
826 ret = handle_send_eta_cmd(cmd);
827 break;
828 case FIO_NET_CMD_RUN:
829 ret = handle_run_cmd(job_list, cmd);
830 break;
831 case FIO_NET_CMD_UPDATE_JOB:
832 ret = handle_update_job_cmd(cmd);
833 break;
834 case FIO_NET_CMD_VTRIGGER:
835 ret = handle_trigger_cmd(cmd);
836 break;
837 case FIO_NET_CMD_SENDFILE: {
838 struct cmd_sendfile_reply *in;
839 struct cmd_reply *rep;
840
841 rep = (struct cmd_reply *) (uintptr_t) cmd->tag;
842
843 in = (struct cmd_sendfile_reply *) cmd->payload;
844 in->size = le32_to_cpu(in->size);
845 in->error = le32_to_cpu(in->error);
846 if (in->error) {
847 ret = 1;
848 rep->error = in->error;
849 } else {
850 ret = 0;
851 rep->data = smalloc(in->size);
852 if (!rep->data) {
853 ret = 1;
854 rep->error = ENOMEM;
855 } else {
856 rep->size = in->size;
857 memcpy(rep->data, in->data, in->size);
858 }
859 }
860 fio_mutex_up(&rep->lock);
861 break;
862 }
863 default:
864 log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode));
865 ret = 1;
866 }
867
868 return ret;
869}
870
871static int handle_connection(int sk)
872{
873 struct fio_net_cmd *cmd = NULL;
874 FLIST_HEAD(job_list);
875 int ret = 0;
876
877 reset_fio_state();
878 server_fd = sk;
879
880 /* read forever */
881 while (!exit_backend) {
882 struct pollfd pfd = {
883 .fd = sk,
884 .events = POLLIN,
885 };
886
887 ret = 0;
888 do {
889 int timeout = 1000;
890
891 if (!flist_empty(&job_list))
892 timeout = 100;
893
894 ret = poll(&pfd, 1, timeout);
895 if (ret < 0) {
896 if (errno == EINTR)
897 break;
898 log_err("fio: poll: %s\n", strerror(errno));
899 break;
900 } else if (!ret) {
901 fio_server_check_jobs(&job_list);
902 continue;
903 }
904
905 if (pfd.revents & POLLIN)
906 break;
907 if (pfd.revents & (POLLERR|POLLHUP)) {
908 ret = 1;
909 break;
910 }
911 } while (!exit_backend);
912
913 fio_server_check_jobs(&job_list);
914
915 if (ret < 0)
916 break;
917
918 cmd = fio_net_recv_cmd(sk);
919 if (!cmd) {
920 ret = -1;
921 break;
922 }
923
924 ret = handle_command(&job_list, cmd);
925 if (ret)
926 break;
927
928 free(cmd);
929 cmd = NULL;
930 }
931
932 if (cmd)
933 free(cmd);
934
935 close(sk);
936 _exit(ret);
937}
938
939static int accept_loop(int listen_sk)
940{
941 struct sockaddr_in addr;
942 struct sockaddr_in6 addr6;
943 socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr);
944 struct pollfd pfd;
945 int ret = 0, sk, exitval = 0;
946 FLIST_HEAD(conn_list);
947
948 dprint(FD_NET, "server enter accept loop\n");
949
950 fio_set_fd_nonblocking(listen_sk, "server");
951
952 while (!exit_backend) {
953 const char *from;
954 char buf[64];
955 pid_t pid;
956
957 pfd.fd = listen_sk;
958 pfd.events = POLLIN;
959 do {
960 int timeout = 1000;
961
962 if (!flist_empty(&conn_list))
963 timeout = 100;
964
965 ret = poll(&pfd, 1, timeout);
966 if (ret < 0) {
967 if (errno == EINTR)
968 break;
969 log_err("fio: poll: %s\n", strerror(errno));
970 break;
971 } else if (!ret) {
972 fio_server_check_conns(&conn_list);
973 continue;
974 }
975
976 if (pfd.revents & POLLIN)
977 break;
978 } while (!exit_backend);
979
980 fio_server_check_conns(&conn_list);
981
982 if (exit_backend || ret < 0)
983 break;
984
985 if (use_ipv6)
986 sk = accept(listen_sk, (struct sockaddr *) &addr6, &len);
987 else
988 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
989
990 if (sk < 0) {
991 log_err("fio: accept: %s\n", strerror(errno));
992 return -1;
993 }
994
995 if (use_ipv6)
996 from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf));
997 else
998 from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf));
999
1000 dprint(FD_NET, "server: connect from %s\n", from);
1001
1002 pid = fork();
1003 if (pid) {
1004 close(sk);
1005 fio_server_add_conn_pid(&conn_list, pid);
1006 continue;
1007 }
1008
1009 /* exits */
1010 strncpy(client_sockaddr_str, from, INET6_ADDRSTRLEN);
1011 handle_connection(sk);
1012 }
1013
1014 return exitval;
1015}
1016
1017int fio_server_text_output(int level, const char *buf, size_t len)
1018{
1019 struct cmd_text_pdu *pdu;
1020 unsigned int tlen;
1021 struct timeval tv;
1022
1023 if (server_fd == -1)
1024 return log_local_buf(buf, len);
1025
1026 tlen = sizeof(*pdu) + len;
1027 pdu = malloc(tlen);
1028
1029 pdu->level = __cpu_to_le32(level);
1030 pdu->buf_len = __cpu_to_le32(len);
1031
1032 gettimeofday(&tv, NULL);
1033 pdu->log_sec = __cpu_to_le64(tv.tv_sec);
1034 pdu->log_usec = __cpu_to_le64(tv.tv_usec);
1035
1036 memcpy(pdu->buf, buf, len);
1037
1038 fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL);
1039 free(pdu);
1040 return len;
1041}
1042
1043static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
1044{
1045 dst->max_val = cpu_to_le64(src->max_val);
1046 dst->min_val = cpu_to_le64(src->min_val);
1047 dst->samples = cpu_to_le64(src->samples);
1048
1049 /*
1050 * Encode to IEEE 754 for network transfer
1051 */
1052 dst->mean.u.i = cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
1053 dst->S.u.i = cpu_to_le64(fio_double_to_uint64(src->S.u.f));
1054}
1055
1056static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
1057{
1058 int i;
1059
1060 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1061 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
1062 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
1063 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
1064 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
1065 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
1066 dst->agg[i] = cpu_to_le64(src->agg[i]);
1067 }
1068
1069 dst->kb_base = cpu_to_le32(src->kb_base);
1070 dst->unit_base = cpu_to_le32(src->unit_base);
1071 dst->groupid = cpu_to_le32(src->groupid);
1072 dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep);
1073}
1074
1075/*
1076 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
1077 * into a single payload.
1078 */
1079void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
1080{
1081 struct cmd_ts_pdu p;
1082 int i, j;
1083
1084 dprint(FD_NET, "server sending end stats\n");
1085
1086 memset(&p, 0, sizeof(p));
1087
1088 strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1);
1089 strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1);
1090 strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1);
1091
1092 p.ts.error = cpu_to_le32(ts->error);
1093 p.ts.thread_number = cpu_to_le32(ts->thread_number);
1094 p.ts.groupid = cpu_to_le32(ts->groupid);
1095 p.ts.pid = cpu_to_le32(ts->pid);
1096 p.ts.members = cpu_to_le32(ts->members);
1097 p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep);
1098
1099 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1100 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
1101 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
1102 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
1103 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
1104 }
1105
1106 p.ts.usr_time = cpu_to_le64(ts->usr_time);
1107 p.ts.sys_time = cpu_to_le64(ts->sys_time);
1108 p.ts.ctx = cpu_to_le64(ts->ctx);
1109 p.ts.minf = cpu_to_le64(ts->minf);
1110 p.ts.majf = cpu_to_le64(ts->majf);
1111 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
1112 p.ts.percentile_precision = cpu_to_le64(ts->percentile_precision);
1113
1114 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
1115 fio_fp64_t *src = &ts->percentile_list[i];
1116 fio_fp64_t *dst = &p.ts.percentile_list[i];
1117
1118 dst->u.i = cpu_to_le64(fio_double_to_uint64(src->u.f));
1119 }
1120
1121 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
1122 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
1123 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
1124 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
1125 }
1126
1127 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
1128 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
1129 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
1130 }
1131
1132 for (i = 0; i < DDIR_RWDIR_CNT; i++)
1133 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
1134 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
1135
1136 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1137 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
1138 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
1139 p.ts.drop_io_u[i] = cpu_to_le64(ts->drop_io_u[i]);
1140 }
1141
1142 p.ts.total_submit = cpu_to_le64(ts->total_submit);
1143 p.ts.total_complete = cpu_to_le64(ts->total_complete);
1144
1145 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1146 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
1147 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
1148 }
1149
1150 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
1151 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
1152 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
1153 p.ts.first_error = cpu_to_le32(ts->first_error);
1154 p.ts.kb_base = cpu_to_le32(ts->kb_base);
1155 p.ts.unit_base = cpu_to_le32(ts->unit_base);
1156
1157 p.ts.latency_depth = cpu_to_le32(ts->latency_depth);
1158 p.ts.latency_target = cpu_to_le64(ts->latency_target);
1159 p.ts.latency_window = cpu_to_le64(ts->latency_window);
1160 p.ts.latency_percentile.u.i = cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f));
1161
1162 p.ts.nr_block_infos = le64_to_cpu(ts->nr_block_infos);
1163 for (i = 0; i < p.ts.nr_block_infos; i++)
1164 p.ts.block_infos[i] = le32_to_cpu(ts->block_infos[i]);
1165
1166 convert_gs(&p.rs, rs);
1167
1168 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL);
1169}
1170
1171void fio_server_send_gs(struct group_run_stats *rs)
1172{
1173 struct group_run_stats gs;
1174
1175 dprint(FD_NET, "server sending group run stats\n");
1176
1177 convert_gs(&gs, rs);
1178 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL);
1179}
1180
1181static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
1182{
1183 int i;
1184
1185 for (i = 0; i < 2; i++) {
1186 dst->ios[i] = cpu_to_le64(src->ios[i]);
1187 dst->merges[i] = cpu_to_le64(src->merges[i]);
1188 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
1189 dst->ticks[i] = cpu_to_le64(src->ticks[i]);
1190 }
1191
1192 dst->io_ticks = cpu_to_le64(src->io_ticks);
1193 dst->time_in_queue = cpu_to_le64(src->time_in_queue);
1194 dst->slavecount = cpu_to_le32(src->slavecount);
1195 dst->max_util.u.i = cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
1196}
1197
1198static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
1199{
1200 int i;
1201
1202 dst->name[FIO_DU_NAME_SZ - 1] = '\0';
1203 strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1);
1204
1205 for (i = 0; i < 2; i++) {
1206 dst->s.ios[i] = cpu_to_le64(src->s.ios[i]);
1207 dst->s.merges[i] = cpu_to_le64(src->s.merges[i]);
1208 dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]);
1209 dst->s.ticks[i] = cpu_to_le64(src->s.ticks[i]);
1210 }
1211
1212 dst->s.io_ticks = cpu_to_le64(src->s.io_ticks);
1213 dst->s.time_in_queue = cpu_to_le64(src->s.time_in_queue);
1214 dst->s.msec = cpu_to_le64(src->s.msec);
1215}
1216
1217void fio_server_send_du(void)
1218{
1219 struct disk_util *du;
1220 struct flist_head *entry;
1221 struct cmd_du_pdu pdu;
1222
1223 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
1224
1225 memset(&pdu, 0, sizeof(pdu));
1226
1227 flist_for_each(entry, &disk_list) {
1228 du = flist_entry(entry, struct disk_util, list);
1229
1230 convert_dus(&pdu.dus, &du->dus);
1231 convert_agg(&pdu.agg, &du->agg);
1232
1233 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL);
1234 }
1235}
1236
1237/*
1238 * Send a command with a separate PDU, not inlined in the command
1239 */
1240static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf,
1241 off_t size, uint64_t tag, uint32_t flags)
1242{
1243 struct fio_net_cmd cmd;
1244 struct iovec iov[2];
1245
1246 iov[0].iov_base = (void *) &cmd;
1247 iov[0].iov_len = sizeof(cmd);
1248 iov[1].iov_base = (void *) buf;
1249 iov[1].iov_len = size;
1250
1251 __fio_init_net_cmd(&cmd, opcode, size, tag);
1252 cmd.flags = __cpu_to_le32(flags);
1253 fio_net_cmd_crc_pdu(&cmd, buf);
1254
1255 return fio_sendv_data(sk, iov, 2);
1256}
1257
1258static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log)
1259{
1260 int ret = 0;
1261#ifdef CONFIG_ZLIB
1262 z_stream stream;
1263 void *out_pdu;
1264
1265 /*
1266 * Dirty - since the log is potentially huge, compress it into
1267 * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving
1268 * side defragment it.
1269 */
1270 out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU);
1271
1272 stream.zalloc = Z_NULL;
1273 stream.zfree = Z_NULL;
1274 stream.opaque = Z_NULL;
1275
1276 if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
1277 ret = 1;
1278 goto err;
1279 }
1280
1281 stream.next_in = (void *) log->log;
1282 stream.avail_in = log->nr_samples * log_entry_sz(log);
1283
1284 do {
1285 unsigned int this_len, flags = 0;
1286
1287 stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU;
1288 stream.next_out = out_pdu;
1289 ret = deflate(&stream, Z_FINISH);
1290 /* may be Z_OK, or Z_STREAM_END */
1291 if (ret < 0)
1292 goto err_zlib;
1293
1294 this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out;
1295
1296 if (stream.avail_in)
1297 flags = FIO_NET_CMD_F_MORE;
1298
1299 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG,
1300 out_pdu, this_len, 0, flags);
1301 if (ret)
1302 goto err_zlib;
1303 } while (stream.avail_in);
1304
1305err_zlib:
1306 deflateEnd(&stream);
1307err:
1308 free(out_pdu);
1309#endif
1310 return ret;
1311}
1312
1313int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name)
1314{
1315 struct cmd_iolog_pdu pdu;
1316 int i, ret = 0;
1317
1318 pdu.nr_samples = cpu_to_le64(log->nr_samples);
1319 pdu.thread_number = cpu_to_le32(td->thread_number);
1320 pdu.log_type = cpu_to_le32(log->log_type);
1321 pdu.compressed = cpu_to_le32(use_zlib);
1322
1323 strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX);
1324 pdu.name[FIO_NET_NAME_MAX - 1] = '\0';
1325
1326 for (i = 0; i < log->nr_samples; i++) {
1327 struct io_sample *s = get_sample(log, i);
1328
1329 s->time = cpu_to_le64(s->time);
1330 s->val = cpu_to_le64(s->val);
1331 s->__ddir = cpu_to_le32(s->__ddir);
1332 s->bs = cpu_to_le32(s->bs);
1333
1334 if (log->log_offset) {
1335 struct io_sample_offset *so = (void *) s;
1336
1337 so->offset = cpu_to_le64(so->offset);
1338 }
1339 }
1340
1341 /*
1342 * Send header first, it's not compressed.
1343 */
1344 ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu,
1345 sizeof(pdu), 0, FIO_NET_CMD_F_MORE);
1346 if (ret)
1347 return ret;
1348
1349 /*
1350 * Now send actual log, compress if we can, otherwise just plain
1351 */
1352 if (use_zlib)
1353 return fio_send_iolog_gz(&pdu, log);
1354
1355 return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log,
1356 log->nr_samples * log_entry_sz(log), 0, 0);
1357}
1358
1359void fio_server_send_add_job(struct thread_data *td)
1360{
1361 struct cmd_add_job_pdu pdu;
1362
1363 memset(&pdu, 0, sizeof(pdu));
1364 pdu.thread_number = cpu_to_le32(td->thread_number);
1365 pdu.groupid = cpu_to_le32(td->groupid);
1366 convert_thread_options_to_net(&pdu.top, &td->o);
1367
1368 fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL);
1369}
1370
1371void fio_server_send_start(struct thread_data *td)
1372{
1373 assert(server_fd != -1);
1374
1375 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL);
1376}
1377
1378int fio_server_get_verify_state(const char *name, int threadnumber,
1379 void **datap)
1380{
1381 struct thread_io_list *s;
1382 struct cmd_sendfile out;
1383 struct cmd_reply *rep;
1384 uint64_t tag;
1385 void *data;
1386
1387 dprint(FD_NET, "server: request verify state\n");
1388
1389 rep = smalloc(sizeof(*rep));
1390 if (!rep) {
1391 log_err("fio: smalloc pool too small\n");
1392 return 1;
1393 }
1394
1395 __fio_mutex_init(&rep->lock, FIO_MUTEX_LOCKED);
1396 rep->data = NULL;
1397 rep->error = 0;
1398
1399 verify_state_gen_name((char *) out.path, sizeof(out.path), name, me,
1400 threadnumber);
1401 tag = (uint64_t) (uintptr_t) rep;
1402 fio_net_send_cmd(server_fd, FIO_NET_CMD_SENDFILE, &out, sizeof(out),
1403 &tag, NULL);
1404
1405 /*
1406 * Wait for the backend to receive the reply
1407 */
1408 if (fio_mutex_down_timeout(&rep->lock, 10)) {
1409 log_err("fio: timed out waiting for reply\n");
1410 goto fail;
1411 }
1412
1413 if (rep->error) {
1414 log_err("fio: failure on receiving state file: %s\n", strerror(rep->error));
1415fail:
1416 *datap = NULL;
1417 sfree(rep);
1418 fio_net_send_quit(server_fd);
1419 return 1;
1420 }
1421
1422 /*
1423 * The format is verify_state_hdr, then thread_io_list. Verify
1424 * the header, and the thread_io_list checksum
1425 */
1426 s = rep->data + sizeof(struct verify_state_hdr);
1427 if (verify_state_hdr(rep->data, s))
1428 goto fail;
1429
1430 /*
1431 * Don't need the header from now, copy just the thread_io_list
1432 */
1433 rep->size -= sizeof(struct verify_state_hdr);
1434 data = malloc(rep->size);
1435 memcpy(data, s, rep->size);
1436 *datap = data;
1437
1438 sfree(rep->data);
1439 __fio_mutex_remove(&rep->lock);
1440 sfree(rep);
1441 return 0;
1442}
1443
1444static int fio_init_server_ip(void)
1445{
1446 struct sockaddr *addr;
1447 socklen_t socklen;
1448 char buf[80];
1449 const char *str;
1450 int sk, opt;
1451
1452 if (use_ipv6)
1453 sk = socket(AF_INET6, SOCK_STREAM, 0);
1454 else
1455 sk = socket(AF_INET, SOCK_STREAM, 0);
1456
1457 if (sk < 0) {
1458 log_err("fio: socket: %s\n", strerror(errno));
1459 return -1;
1460 }
1461
1462 opt = 1;
1463 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) {
1464 log_err("fio: setsockopt: %s\n", strerror(errno));
1465 close(sk);
1466 return -1;
1467 }
1468#ifdef SO_REUSEPORT
1469 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
1470 log_err("fio: setsockopt: %s\n", strerror(errno));
1471 close(sk);
1472 return -1;
1473 }
1474#endif
1475
1476 if (use_ipv6) {
1477 const void *src = &saddr_in6.sin6_addr;
1478
1479 addr = (struct sockaddr *) &saddr_in6;
1480 socklen = sizeof(saddr_in6);
1481 saddr_in6.sin6_family = AF_INET6;
1482 str = inet_ntop(AF_INET6, src, buf, sizeof(buf));
1483 } else {
1484 const void *src = &saddr_in.sin_addr;
1485
1486 addr = (struct sockaddr *) &saddr_in;
1487 socklen = sizeof(saddr_in);
1488 saddr_in.sin_family = AF_INET;
1489 str = inet_ntop(AF_INET, src, buf, sizeof(buf));
1490 }
1491
1492 if (bind(sk, addr, socklen) < 0) {
1493 log_err("fio: bind: %s\n", strerror(errno));
1494 log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str);
1495 close(sk);
1496 return -1;
1497 }
1498
1499 return sk;
1500}
1501
1502static int fio_init_server_sock(void)
1503{
1504 struct sockaddr_un addr;
1505 socklen_t len;
1506 mode_t mode;
1507 int sk;
1508
1509 sk = socket(AF_UNIX, SOCK_STREAM, 0);
1510 if (sk < 0) {
1511 log_err("fio: socket: %s\n", strerror(errno));
1512 return -1;
1513 }
1514
1515 mode = umask(000);
1516
1517 memset(&addr, 0, sizeof(addr));
1518 addr.sun_family = AF_UNIX;
1519 strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1);
1520
1521 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
1522
1523 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
1524 log_err("fio: bind: %s\n", strerror(errno));
1525 close(sk);
1526 return -1;
1527 }
1528
1529 umask(mode);
1530 return sk;
1531}
1532
1533static int fio_init_server_connection(void)
1534{
1535 char bind_str[128];
1536 int sk;
1537
1538 dprint(FD_NET, "starting server\n");
1539
1540 if (!bind_sock)
1541 sk = fio_init_server_ip();
1542 else
1543 sk = fio_init_server_sock();
1544
1545 if (sk < 0)
1546 return sk;
1547
1548 memset(bind_str, 0, sizeof(bind_str));
1549
1550 if (!bind_sock) {
1551 char *p, port[16];
1552 const void *src;
1553 int af;
1554
1555 if (use_ipv6) {
1556 af = AF_INET6;
1557 src = &saddr_in6.sin6_addr;
1558 } else {
1559 af = AF_INET;
1560 src = &saddr_in.sin_addr;
1561 }
1562
1563 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
1564
1565 sprintf(port, ",%u", fio_net_port);
1566 if (p)
1567 strcat(p, port);
1568 else
1569 strncpy(bind_str, port, sizeof(bind_str) - 1);
1570 } else
1571 strncpy(bind_str, bind_sock, sizeof(bind_str) - 1);
1572
1573 log_info("fio: server listening on %s\n", bind_str);
1574
1575 if (listen(sk, 0) < 0) {
1576 log_err("fio: listen: %s\n", strerror(errno));
1577 close(sk);
1578 return -1;
1579 }
1580
1581 return sk;
1582}
1583
1584int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp,
1585 struct in6_addr *inp6)
1586
1587{
1588 int ret = 0;
1589
1590 if (ipv6)
1591 ret = inet_pton(AF_INET6, host, inp6);
1592 else
1593 ret = inet_pton(AF_INET, host, inp);
1594
1595 if (ret != 1) {
1596 struct addrinfo hints, *res;
1597
1598 memset(&hints, 0, sizeof(hints));
1599 hints.ai_family = ipv6 ? AF_INET6 : AF_INET;
1600 hints.ai_socktype = SOCK_STREAM;
1601
1602 ret = getaddrinfo(host, NULL, &hints, &res);
1603 if (ret) {
1604 log_err("fio: failed to resolve <%s> (%s)\n", host,
1605 gai_strerror(ret));
1606 return 1;
1607 }
1608
1609 if (ipv6)
1610 memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6));
1611 else
1612 memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp));
1613
1614 ret = 1;
1615 freeaddrinfo(res);
1616 }
1617
1618 return !(ret == 1);
1619}
1620
1621/*
1622 * Parse a host/ip/port string. Reads from 'str'.
1623 *
1624 * Outputs:
1625 *
1626 * For IPv4:
1627 * *ptr is the host, *port is the port, inp is the destination.
1628 * For IPv6:
1629 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
1630 * For local domain sockets:
1631 * *ptr is the filename, *is_sock is 1.
1632 */
1633int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
1634 int *port, struct in_addr *inp,
1635 struct in6_addr *inp6, int *ipv6)
1636{
1637 const char *host = str;
1638 char *portp;
1639 int lport = 0;
1640
1641 *ptr = NULL;
1642 *is_sock = 0;
1643 *port = fio_net_port;
1644 *ipv6 = 0;
1645
1646 if (!strncmp(str, "sock:", 5)) {
1647 *ptr = strdup(str + 5);
1648 *is_sock = 1;
1649
1650 return 0;
1651 }
1652
1653 /*
1654 * Is it ip:<ip or host>:port
1655 */
1656 if (!strncmp(host, "ip:", 3))
1657 host += 3;
1658 else if (!strncmp(host, "ip4:", 4))
1659 host += 4;
1660 else if (!strncmp(host, "ip6:", 4)) {
1661 host += 4;
1662 *ipv6 = 1;
1663 } else if (host[0] == ':') {
1664 /* String is :port */
1665 host++;
1666 lport = atoi(host);
1667 if (!lport || lport > 65535) {
1668 log_err("fio: bad server port %u\n", lport);
1669 return 1;
1670 }
1671 /* no hostname given, we are done */
1672 *port = lport;
1673 return 0;
1674 }
1675
1676 /*
1677 * If no port seen yet, check if there's a last ',' at the end
1678 */
1679 if (!lport) {
1680 portp = strchr(host, ',');
1681 if (portp) {
1682 *portp = '\0';
1683 portp++;
1684 lport = atoi(portp);
1685 if (!lport || lport > 65535) {
1686 log_err("fio: bad server port %u\n", lport);
1687 return 1;
1688 }
1689 }
1690 }
1691
1692 if (lport)
1693 *port = lport;
1694
1695 if (!strlen(host))
1696 return 0;
1697
1698 *ptr = strdup(host);
1699
1700 if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) {
1701 free(*ptr);
1702 *ptr = NULL;
1703 return 1;
1704 }
1705
1706 if (*port == 0)
1707 *port = fio_net_port;
1708
1709 return 0;
1710}
1711
1712/*
1713 * Server arg should be one of:
1714 *
1715 * sock:/path/to/socket
1716 * ip:1.2.3.4
1717 * 1.2.3.4
1718 *
1719 * Where sock uses unix domain sockets, and ip binds the server to
1720 * a specific interface. If no arguments are given to the server, it
1721 * uses IP and binds to 0.0.0.0.
1722 *
1723 */
1724static int fio_handle_server_arg(void)
1725{
1726 int port = fio_net_port;
1727 int is_sock, ret = 0;
1728
1729 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1730
1731 if (!fio_server_arg)
1732 goto out;
1733
1734 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
1735 &port, &saddr_in.sin_addr,
1736 &saddr_in6.sin6_addr, &use_ipv6);
1737
1738 if (!is_sock && bind_sock) {
1739 free(bind_sock);
1740 bind_sock = NULL;
1741 }
1742
1743out:
1744 fio_net_port = port;
1745 saddr_in.sin_port = htons(port);
1746 saddr_in6.sin6_port = htons(port);
1747 return ret;
1748}
1749
1750static void sig_int(int sig)
1751{
1752 if (bind_sock)
1753 unlink(bind_sock);
1754}
1755
1756static void set_sig_handlers(void)
1757{
1758 struct sigaction act;
1759
1760 memset(&act, 0, sizeof(act));
1761 act.sa_handler = sig_int;
1762 act.sa_flags = SA_RESTART;
1763 sigaction(SIGINT, &act, NULL);
1764}
1765
1766static int fio_server(void)
1767{
1768 int sk, ret;
1769
1770 dprint(FD_NET, "starting server\n");
1771
1772 if (fio_handle_server_arg())
1773 return -1;
1774
1775 sk = fio_init_server_connection();
1776 if (sk < 0)
1777 return -1;
1778
1779 set_sig_handlers();
1780
1781 ret = accept_loop(sk);
1782
1783 close(sk);
1784
1785 if (fio_server_arg) {
1786 free(fio_server_arg);
1787 fio_server_arg = NULL;
1788 }
1789 if (bind_sock)
1790 free(bind_sock);
1791
1792 return ret;
1793}
1794
1795void fio_server_got_signal(int signal)
1796{
1797 if (signal == SIGPIPE)
1798 server_fd = -1;
1799 else {
1800 log_info("\nfio: terminating on signal %d\n", signal);
1801 exit_backend = 1;
1802 }
1803}
1804
1805static int check_existing_pidfile(const char *pidfile)
1806{
1807 struct stat sb;
1808 char buf[16];
1809 pid_t pid;
1810 FILE *f;
1811
1812 if (stat(pidfile, &sb))
1813 return 0;
1814
1815 f = fopen(pidfile, "r");
1816 if (!f)
1817 return 0;
1818
1819 if (fread(buf, sb.st_size, 1, f) <= 0) {
1820 fclose(f);
1821 return 1;
1822 }
1823 fclose(f);
1824
1825 pid = atoi(buf);
1826 if (kill(pid, SIGCONT) < 0)
1827 return errno != ESRCH;
1828
1829 return 1;
1830}
1831
1832static int write_pid(pid_t pid, const char *pidfile)
1833{
1834 FILE *fpid;
1835
1836 fpid = fopen(pidfile, "w");
1837 if (!fpid) {
1838 log_err("fio: failed opening pid file %s\n", pidfile);
1839 return 1;
1840 }
1841
1842 fprintf(fpid, "%u\n", (unsigned int) pid);
1843 fclose(fpid);
1844 return 0;
1845}
1846
1847/*
1848 * If pidfile is specified, background us.
1849 */
1850int fio_start_server(char *pidfile)
1851{
1852 pid_t pid;
1853 int ret;
1854
1855#if defined(WIN32)
1856 WSADATA wsd;
1857 WSAStartup(MAKEWORD(2, 2), &wsd);
1858#endif
1859
1860 if (!pidfile)
1861 return fio_server();
1862
1863 if (check_existing_pidfile(pidfile)) {
1864 log_err("fio: pidfile %s exists and server appears alive\n",
1865 pidfile);
1866 free(pidfile);
1867 return -1;
1868 }
1869
1870 pid = fork();
1871 if (pid < 0) {
1872 log_err("fio: failed server fork: %s", strerror(errno));
1873 free(pidfile);
1874 return -1;
1875 } else if (pid) {
1876 ret = write_pid(pid, pidfile);
1877 free(pidfile);
1878 _exit(ret);
1879 }
1880
1881 setsid();
1882 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1883 log_syslog = 1;
1884 close(STDIN_FILENO);
1885 close(STDOUT_FILENO);
1886 close(STDERR_FILENO);
1887 f_out = NULL;
1888 f_err = NULL;
1889
1890 ret = fio_server();
1891
1892 closelog();
1893 unlink(pidfile);
1894 free(pidfile);
1895 return ret;
1896}
1897
1898void fio_server_set_arg(const char *arg)
1899{
1900 fio_server_arg = strdup(arg);
1901}