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