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