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