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