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