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