Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 3 | #include <stdarg.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <limits.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/poll.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/wait.h> |
Jens Axboe | d05c4a0 | 2011-10-05 00:16:11 +0200 | [diff] [blame] | 11 | #include <sys/socket.h> |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 12 | #include <sys/stat.h> |
| 13 | #include <sys/un.h> |
Bruce Cran | 1f51bba | 2013-04-12 15:40:21 +0200 | [diff] [blame] | 14 | #include <sys/uio.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 15 | #include <netinet/in.h> |
| 16 | #include <arpa/inet.h> |
| 17 | #include <netdb.h> |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 18 | #include <syslog.h> |
Jens Axboe | 9e22ecb | 2011-10-04 14:50:21 +0200 | [diff] [blame] | 19 | #include <signal.h> |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 20 | #ifdef CONFIG_ZLIB |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 21 | #include <zlib.h> |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 22 | #endif |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 23 | |
| 24 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 25 | #include "server.h" |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 26 | #include "crc/crc16.h" |
Jens Axboe | c7c6cb4 | 2011-10-13 14:12:40 +0200 | [diff] [blame] | 27 | #include "lib/ieee754.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 28 | |
Stephen M. Cameron | 5adc244 | 2012-02-24 08:17:31 +0100 | [diff] [blame] | 29 | int fio_net_port = FIO_NET_PORT; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 30 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 31 | int exit_backend = 0; |
| 32 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 33 | static int server_fd = -1; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 34 | static char *fio_server_arg; |
| 35 | static char *bind_sock; |
| 36 | static struct sockaddr_in saddr_in; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 37 | static struct sockaddr_in6 saddr_in6; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 38 | static int use_ipv6; |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 39 | #ifdef CONFIG_ZLIB |
| 40 | static unsigned int has_zlib = 1; |
| 41 | #else |
| 42 | static unsigned int has_zlib = 0; |
| 43 | #endif |
| 44 | static unsigned int use_zlib; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 45 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 46 | struct fio_fork_item { |
| 47 | struct flist_head list; |
| 48 | int exitval; |
| 49 | int signal; |
| 50 | int exited; |
| 51 | pid_t pid; |
| 52 | }; |
| 53 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 54 | static const char *fio_server_ops[FIO_NET_CMD_NR] = { |
| 55 | "", |
| 56 | "QUIT", |
| 57 | "EXIT", |
| 58 | "JOB", |
| 59 | "JOBLINE", |
| 60 | "TEXT", |
| 61 | "TS", |
| 62 | "GS", |
| 63 | "SEND_ETA", |
| 64 | "ETA", |
| 65 | "PROBE", |
| 66 | "START", |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 67 | "STOP", |
| 68 | "DISK_UTIL", |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 69 | "SERVER_START", |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 70 | "ADD_JOB", |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 71 | "CMD_RUN" |
Jens Axboe | c70e63e | 2012-03-15 08:26:18 +0100 | [diff] [blame] | 72 | "CMD_IOLOG", |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | const char *fio_server_op(unsigned int op) |
| 76 | { |
| 77 | static char buf[32]; |
| 78 | |
| 79 | if (op < FIO_NET_CMD_NR) |
| 80 | return fio_server_ops[op]; |
| 81 | |
| 82 | sprintf(buf, "UNKNOWN/%d", op); |
| 83 | return buf; |
| 84 | } |
| 85 | |
Jens Axboe | 5235f62 | 2012-03-14 21:25:51 +0100 | [diff] [blame] | 86 | static ssize_t iov_total_len(const struct iovec *iov, int count) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 87 | { |
Jens Axboe | 5235f62 | 2012-03-14 21:25:51 +0100 | [diff] [blame] | 88 | ssize_t ret = 0; |
| 89 | |
| 90 | while (count--) { |
| 91 | ret += iov->iov_len; |
| 92 | iov++; |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | static int fio_sendv_data(int sk, struct iovec *iov, int count) |
| 99 | { |
| 100 | ssize_t total_len = iov_total_len(iov, count); |
| 101 | ssize_t ret; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 102 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 103 | do { |
Jens Axboe | 5235f62 | 2012-03-14 21:25:51 +0100 | [diff] [blame] | 104 | ret = writev(sk, iov, count); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 105 | if (ret > 0) { |
Jens Axboe | 5235f62 | 2012-03-14 21:25:51 +0100 | [diff] [blame] | 106 | total_len -= ret; |
| 107 | if (!total_len) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 108 | break; |
Jens Axboe | 5235f62 | 2012-03-14 21:25:51 +0100 | [diff] [blame] | 109 | |
| 110 | while (ret) { |
| 111 | if (ret >= iov->iov_len) { |
| 112 | ret -= iov->iov_len; |
| 113 | iov++; |
| 114 | continue; |
| 115 | } |
| 116 | iov->iov_base += ret; |
| 117 | iov->iov_len -= ret; |
| 118 | ret = 0; |
| 119 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 120 | } else if (!ret) |
| 121 | break; |
| 122 | else if (errno == EAGAIN || errno == EINTR) |
| 123 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 124 | else |
| 125 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 126 | } while (!exit_backend); |
| 127 | |
Jens Axboe | 5235f62 | 2012-03-14 21:25:51 +0100 | [diff] [blame] | 128 | if (!total_len) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 129 | return 0; |
| 130 | |
Jens Axboe | 8b4e61b | 2012-03-09 17:10:54 +0100 | [diff] [blame] | 131 | if (errno) |
| 132 | return -errno; |
| 133 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 134 | return 1; |
| 135 | } |
| 136 | |
Jens Axboe | 5235f62 | 2012-03-14 21:25:51 +0100 | [diff] [blame] | 137 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 138 | { |
| 139 | struct iovec iov = { .iov_base = (void *) p, .iov_len = len }; |
| 140 | |
| 141 | assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_FRAGMENT_PDU); |
| 142 | |
| 143 | return fio_sendv_data(sk, &iov, 1); |
| 144 | } |
| 145 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 146 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 147 | { |
| 148 | do { |
| 149 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 150 | |
| 151 | if (ret > 0) { |
| 152 | len -= ret; |
| 153 | if (!len) |
| 154 | break; |
| 155 | p += ret; |
| 156 | continue; |
| 157 | } else if (!ret) |
| 158 | break; |
| 159 | else if (errno == EAGAIN || errno == EINTR) |
| 160 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 161 | else |
| 162 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 163 | } while (!exit_backend); |
| 164 | |
| 165 | if (!len) |
| 166 | return 0; |
| 167 | |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 172 | { |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 173 | uint16_t crc; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 174 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 175 | cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); |
| 176 | cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 177 | |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 178 | crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ); |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 179 | if (crc != cmd->cmd_crc16) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 180 | log_err("fio: server bad crc on command (got %x, wanted %x)\n", |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 181 | cmd->cmd_crc16, crc); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | cmd->version = le16_to_cpu(cmd->version); |
| 186 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 187 | cmd->flags = le32_to_cpu(cmd->flags); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 188 | cmd->tag = le64_to_cpu(cmd->tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 189 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 190 | |
| 191 | switch (cmd->version) { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 192 | case FIO_SERVER_VER: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 193 | break; |
| 194 | default: |
| 195 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 196 | return 1; |
| 197 | } |
| 198 | |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 199 | if (cmd->pdu_len > FIO_SERVER_MAX_FRAGMENT_PDU) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 200 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 207 | /* |
| 208 | * Read (and defragment, if necessary) incoming commands |
| 209 | */ |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 210 | struct fio_net_cmd *fio_net_recv_cmd(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 211 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 212 | struct fio_net_cmd cmd, *cmdret = NULL; |
| 213 | size_t cmd_size = 0, pdu_offset = 0; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 214 | uint16_t crc; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 215 | int ret, first = 1; |
| 216 | void *pdu = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 217 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 218 | do { |
| 219 | ret = fio_recv_data(sk, &cmd, sizeof(cmd)); |
| 220 | if (ret) |
| 221 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 222 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 223 | /* We have a command, verify it and swap if need be */ |
| 224 | ret = verify_convert_cmd(&cmd); |
| 225 | if (ret) |
| 226 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 227 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 228 | if (first) { |
| 229 | /* if this is text, add room for \0 at the end */ |
| 230 | cmd_size = sizeof(cmd) + cmd.pdu_len + 1; |
| 231 | assert(!cmdret); |
| 232 | } else |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 233 | cmd_size += cmd.pdu_len; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 234 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 235 | cmdret = realloc(cmdret, cmd_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 236 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 237 | if (first) |
| 238 | memcpy(cmdret, &cmd, sizeof(cmd)); |
Jens Axboe | 67f15dc | 2011-10-15 16:07:40 +0200 | [diff] [blame] | 239 | else if (cmdret->opcode != cmd.opcode) { |
| 240 | log_err("fio: fragment opcode mismatch (%d != %d)\n", |
| 241 | cmdret->opcode, cmd.opcode); |
| 242 | ret = 1; |
| 243 | break; |
| 244 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 245 | |
| 246 | if (!cmd.pdu_len) |
| 247 | break; |
| 248 | |
| 249 | /* There's payload, get it */ |
| 250 | pdu = (void *) cmdret->payload + pdu_offset; |
| 251 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 252 | if (ret) |
| 253 | break; |
| 254 | |
| 255 | /* Verify payload crc */ |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 256 | crc = fio_crc16(pdu, cmd.pdu_len); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 257 | if (crc != cmd.pdu_crc16) { |
| 258 | log_err("fio: server bad crc on payload "); |
| 259 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 260 | ret = 1; |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 265 | if (!first) |
| 266 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 267 | first = 0; |
| 268 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 269 | |
| 270 | if (ret) { |
| 271 | free(cmdret); |
| 272 | cmdret = NULL; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 273 | } else if (cmdret) { |
| 274 | /* zero-terminate text input */ |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 275 | if (cmdret->pdu_len) { |
| 276 | if (cmdret->opcode == FIO_NET_CMD_TEXT) { |
| 277 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload; |
| 278 | char *buf = (char *) pdu->buf; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 279 | |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 280 | buf[pdu->buf_len] = '\0'; |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 281 | } else if (cmdret->opcode == FIO_NET_CMD_JOB) { |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 282 | struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload; |
| 283 | char *buf = (char *) pdu->buf; |
| 284 | int len = le32_to_cpu(pdu->buf_len); |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 285 | |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 286 | buf[len] = '\0'; |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 287 | } |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 288 | } |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 289 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 290 | /* frag flag is internal */ |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 291 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 292 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 293 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 294 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 295 | } |
| 296 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 297 | static void add_reply(uint64_t tag, struct flist_head *list) |
| 298 | { |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 299 | struct fio_net_cmd_reply *reply; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 300 | |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 301 | reply = (struct fio_net_cmd_reply *) (uintptr_t) tag; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 302 | flist_add_tail(&reply->list, list); |
| 303 | } |
| 304 | |
| 305 | static uint64_t alloc_reply(uint64_t tag, uint16_t opcode) |
| 306 | { |
| 307 | struct fio_net_cmd_reply *reply; |
| 308 | |
| 309 | reply = calloc(1, sizeof(*reply)); |
| 310 | INIT_FLIST_HEAD(&reply->list); |
| 311 | gettimeofday(&reply->tv, NULL); |
| 312 | reply->saved_tag = tag; |
| 313 | reply->opcode = opcode; |
| 314 | |
| 315 | return (uintptr_t) reply; |
| 316 | } |
| 317 | |
| 318 | static void free_reply(uint64_t tag) |
| 319 | { |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 320 | struct fio_net_cmd_reply *reply; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 321 | |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 322 | reply = (struct fio_net_cmd_reply *) (uintptr_t) tag; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 323 | free(reply); |
| 324 | } |
| 325 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 326 | void fio_net_cmd_crc_pdu(struct fio_net_cmd *cmd, const void *pdu) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 327 | { |
| 328 | uint32_t pdu_len; |
| 329 | |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 330 | cmd->cmd_crc16 = __cpu_to_le16(fio_crc16(cmd, FIO_NET_CMD_CRC_SZ)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 331 | |
| 332 | pdu_len = le32_to_cpu(cmd->pdu_len); |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 333 | cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len)); |
| 334 | } |
| 335 | |
| 336 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 337 | { |
| 338 | fio_net_cmd_crc_pdu(cmd, cmd->payload); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 339 | } |
| 340 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 341 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 342 | uint64_t *tagptr, struct flist_head *list) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 343 | { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 344 | struct fio_net_cmd *cmd = NULL; |
| 345 | size_t this_len, cur_len = 0; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 346 | uint64_t tag; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 347 | int ret; |
| 348 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 349 | if (list) { |
| 350 | assert(tagptr); |
| 351 | tag = *tagptr = alloc_reply(*tagptr, opcode); |
| 352 | } else |
| 353 | tag = tagptr ? *tagptr : 0; |
| 354 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 355 | do { |
| 356 | this_len = size; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 357 | if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU) |
| 358 | this_len = FIO_SERVER_MAX_FRAGMENT_PDU; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 359 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 360 | if (!cmd || cur_len < sizeof(*cmd) + this_len) { |
| 361 | if (cmd) |
| 362 | free(cmd); |
| 363 | |
| 364 | cur_len = sizeof(*cmd) + this_len; |
| 365 | cmd = malloc(cur_len); |
| 366 | } |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 367 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 368 | fio_init_net_cmd(cmd, opcode, buf, this_len, tag); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 369 | |
| 370 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 371 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 372 | |
| 373 | fio_net_cmd_crc(cmd); |
| 374 | |
| 375 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 376 | size -= this_len; |
| 377 | buf += this_len; |
| 378 | } while (!ret && size); |
| 379 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 380 | if (list) { |
| 381 | if (ret) |
| 382 | free_reply(tag); |
| 383 | else |
| 384 | add_reply(tag, list); |
| 385 | } |
| 386 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 387 | if (cmd) |
| 388 | free(cmd); |
| 389 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 390 | return ret; |
| 391 | } |
| 392 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 393 | static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 394 | { |
Jens Axboe | 178cde9 | 2011-10-05 22:14:31 +0200 | [diff] [blame] | 395 | struct fio_net_cmd cmd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 396 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 397 | fio_init_net_cmd(&cmd, opcode, NULL, 0, tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 398 | fio_net_cmd_crc(&cmd); |
| 399 | |
| 400 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 401 | } |
| 402 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 403 | /* |
| 404 | * If 'list' is non-NULL, then allocate and store the sent command for |
| 405 | * later verification. |
| 406 | */ |
| 407 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag, |
| 408 | struct flist_head *list) |
| 409 | { |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 410 | int ret; |
| 411 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 412 | if (list) |
| 413 | tag = alloc_reply(tag, opcode); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 414 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 415 | ret = fio_net_send_simple_stack_cmd(sk, opcode, tag); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 416 | if (ret) { |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 417 | if (list) |
| 418 | free_reply(tag); |
| 419 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 420 | return ret; |
| 421 | } |
| 422 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 423 | if (list) |
| 424 | add_reply(tag, list); |
| 425 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 429 | int fio_net_send_quit(int sk) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 430 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 431 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 432 | |
Jens Axboe | 8c95307 | 2012-03-20 14:35:35 +0100 | [diff] [blame] | 433 | return fio_net_send_simple_cmd(sk, FIO_NET_CMD_QUIT, 0, NULL); |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 434 | } |
| 435 | |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 436 | static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error, |
| 437 | int signal) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 438 | { |
| 439 | struct cmd_end_pdu epdu; |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 440 | uint64_t tag = 0; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 441 | |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 442 | if (cmd) |
| 443 | tag = cmd->tag; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 444 | |
| 445 | epdu.error = __cpu_to_le32(error); |
| 446 | epdu.signal = __cpu_to_le32(signal); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 447 | return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL); |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | int fio_net_send_stop(int sk, int error, int signal) |
| 451 | { |
| 452 | dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal); |
| 453 | return fio_net_send_ack(sk, NULL, error, signal); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static void fio_server_add_fork_item(pid_t pid, struct flist_head *list) |
| 457 | { |
| 458 | struct fio_fork_item *ffi; |
| 459 | |
| 460 | ffi = malloc(sizeof(*ffi)); |
| 461 | ffi->exitval = 0; |
| 462 | ffi->signal = 0; |
| 463 | ffi->exited = 0; |
| 464 | ffi->pid = pid; |
| 465 | flist_add_tail(&ffi->list, list); |
| 466 | } |
| 467 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 468 | static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 469 | { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 470 | dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid); |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 471 | fio_server_add_fork_item(pid, conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 474 | static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 475 | { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 476 | dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid); |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 477 | fio_server_add_fork_item(pid, job_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static void fio_server_check_fork_item(struct fio_fork_item *ffi) |
| 481 | { |
| 482 | int ret, status; |
| 483 | |
| 484 | ret = waitpid(ffi->pid, &status, WNOHANG); |
| 485 | if (ret < 0) { |
| 486 | if (errno == ECHILD) { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 487 | log_err("fio: connection pid %u disappeared\n", (int) ffi->pid); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 488 | ffi->exited = 1; |
| 489 | } else |
| 490 | log_err("fio: waitpid: %s\n", strerror(errno)); |
| 491 | } else if (ret == ffi->pid) { |
| 492 | if (WIFSIGNALED(status)) { |
| 493 | ffi->signal = WTERMSIG(status); |
| 494 | ffi->exited = 1; |
| 495 | } |
| 496 | if (WIFEXITED(status)) { |
| 497 | if (WEXITSTATUS(status)) |
| 498 | ffi->exitval = WEXITSTATUS(status); |
| 499 | ffi->exited = 1; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static void fio_server_fork_item_done(struct fio_fork_item *ffi) |
| 505 | { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 506 | dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 507 | |
| 508 | /* |
| 509 | * Fold STOP and QUIT... |
| 510 | */ |
| 511 | fio_net_send_stop(server_fd, ffi->exitval, ffi->signal); |
| 512 | fio_net_send_quit(server_fd); |
| 513 | flist_del(&ffi->list); |
| 514 | free(ffi); |
| 515 | } |
| 516 | |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 517 | static void fio_server_check_fork_items(struct flist_head *list) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 518 | { |
| 519 | struct flist_head *entry, *tmp; |
| 520 | struct fio_fork_item *ffi; |
| 521 | |
| 522 | flist_for_each_safe(entry, tmp, list) { |
| 523 | ffi = flist_entry(entry, struct fio_fork_item, list); |
| 524 | |
| 525 | fio_server_check_fork_item(ffi); |
| 526 | |
| 527 | if (ffi->exited) |
| 528 | fio_server_fork_item_done(ffi); |
| 529 | } |
| 530 | } |
| 531 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 532 | static void fio_server_check_jobs(struct flist_head *job_list) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 533 | { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 534 | fio_server_check_fork_items(job_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 535 | } |
| 536 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 537 | static void fio_server_check_conns(struct flist_head *conn_list) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 538 | { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 539 | fio_server_check_fork_items(conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 542 | static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 543 | { |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 544 | pid_t pid; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 545 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 546 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 547 | set_genesis_time(); |
| 548 | |
| 549 | pid = fork(); |
| 550 | if (pid) { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 551 | fio_server_add_job_pid(job_list, pid); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
Jens Axboe | 2e1df07 | 2012-02-09 11:15:02 +0100 | [diff] [blame] | 555 | ret = fio_backend(); |
Jens Axboe | 2bb3f0a | 2012-03-28 12:22:40 +0200 | [diff] [blame] | 556 | free_threads_shm(); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 557 | _exit(ret); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 560 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
| 561 | { |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 562 | struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload; |
| 563 | void *buf = pdu->buf; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 564 | struct cmd_start_pdu spdu; |
| 565 | |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 566 | pdu->buf_len = le32_to_cpu(pdu->buf_len); |
| 567 | pdu->client_type = le32_to_cpu(pdu->client_type); |
| 568 | |
| 569 | if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) { |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 570 | fio_net_send_quit(server_fd); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 571 | return -1; |
| 572 | } |
| 573 | |
| 574 | spdu.jobs = cpu_to_le32(thread_number); |
Jens Axboe | 108fea7 | 2012-11-14 13:09:45 -0700 | [diff] [blame] | 575 | spdu.stat_outputs = cpu_to_le32(stat_number); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 576 | fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 580 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 581 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 582 | void *pdu = cmd->payload; |
| 583 | struct cmd_single_line_pdu *cslp; |
| 584 | struct cmd_line_pdu *clp; |
| 585 | unsigned long offset; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 586 | struct cmd_start_pdu spdu; |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 587 | char **argv; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 588 | int i; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 589 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 590 | clp = pdu; |
| 591 | clp->lines = le16_to_cpu(clp->lines); |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 592 | clp->client_type = le16_to_cpu(clp->client_type); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 593 | argv = malloc(clp->lines * sizeof(char *)); |
| 594 | offset = sizeof(*clp); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 595 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 596 | dprint(FD_NET, "server: %d command line args\n", clp->lines); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 597 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 598 | for (i = 0; i < clp->lines; i++) { |
| 599 | cslp = pdu + offset; |
| 600 | argv[i] = (char *) cslp->text; |
| 601 | |
| 602 | offset += sizeof(*cslp) + le16_to_cpu(cslp->len); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 603 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 604 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 605 | |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 606 | if (parse_cmd_line(clp->lines, argv, clp->client_type)) { |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 607 | fio_net_send_quit(server_fd); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 608 | free(argv); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 609 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 610 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 611 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 612 | free(argv); |
| 613 | |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 614 | spdu.jobs = cpu_to_le32(thread_number); |
Jens Axboe | 1e5324e | 2012-11-14 14:25:31 -0700 | [diff] [blame] | 615 | spdu.stat_outputs = cpu_to_le32(stat_number); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 616 | fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 617 | return 0; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 618 | } |
| 619 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 620 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 621 | { |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 622 | struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload; |
| 623 | struct cmd_probe_reply_pdu probe; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 624 | uint64_t tag = cmd->tag; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 625 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 626 | dprint(FD_NET, "server: sending probe reply\n"); |
| 627 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 628 | memset(&probe, 0, sizeof(probe)); |
| 629 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 0dcebdf | 2013-01-23 15:42:16 -0700 | [diff] [blame] | 630 | #ifdef CONFIG_BIG_ENDIAN |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 631 | probe.bigendian = 1; |
| 632 | #endif |
Jens Axboe | 750db47 | 2012-04-16 11:44:48 +0200 | [diff] [blame] | 633 | strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version)); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 634 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 635 | probe.os = FIO_OS; |
| 636 | probe.arch = FIO_ARCH; |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 637 | probe.bpp = sizeof(void *); |
Jens Axboe | d31e26d | 2012-03-28 21:38:24 +0200 | [diff] [blame] | 638 | probe.cpus = __cpu_to_le32(cpus_online()); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 639 | |
| 640 | /* |
| 641 | * If the client supports compression and we do too, then enable it |
| 642 | */ |
| 643 | if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) { |
| 644 | probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB); |
| 645 | use_zlib = 1; |
| 646 | } else { |
| 647 | probe.flags = 0; |
| 648 | use_zlib = 0; |
| 649 | } |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 650 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 651 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int handle_send_eta_cmd(struct fio_net_cmd *cmd) |
| 655 | { |
| 656 | struct jobs_eta *je; |
| 657 | size_t size; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 658 | uint64_t tag = cmd->tag; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 659 | int i; |
| 660 | |
Jens Axboe | b814fb2 | 2011-10-12 09:20:34 +0200 | [diff] [blame] | 661 | if (!thread_number) |
| 662 | return 0; |
| 663 | |
| 664 | size = sizeof(*je) + thread_number * sizeof(char) + 1; |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 665 | je = malloc(size); |
| 666 | memset(je, 0, size); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 667 | |
| 668 | if (!calc_thread_status(je, 1)) { |
| 669 | free(je); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | dprint(FD_NET, "server sending status\n"); |
| 674 | |
| 675 | je->nr_running = cpu_to_le32(je->nr_running); |
| 676 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 677 | je->nr_pending = cpu_to_le32(je->nr_pending); |
Jens Axboe | 714e85f | 2013-04-15 10:21:56 +0200 | [diff] [blame] | 678 | je->nr_setting_up = cpu_to_le32(je->nr_setting_up); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 679 | je->files_open = cpu_to_le32(je->files_open); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 680 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 681 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | 3e47bd2 | 2012-02-29 13:45:02 +0100 | [diff] [blame] | 682 | je->m_rate[i] = cpu_to_le32(je->m_rate[i]); |
| 683 | je->t_rate[i] = cpu_to_le32(je->t_rate[i]); |
| 684 | je->m_iops[i] = cpu_to_le32(je->m_iops[i]); |
| 685 | je->t_iops[i] = cpu_to_le32(je->t_iops[i]); |
Jens Axboe | 16725bb | 2013-04-11 12:43:05 +0200 | [diff] [blame] | 686 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 687 | je->iops[i] = cpu_to_le32(je->iops[i]); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Jens Axboe | c1970b9 | 2012-03-06 15:37:40 +0100 | [diff] [blame] | 690 | je->elapsed_sec = cpu_to_le64(je->elapsed_sec); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 691 | je->eta_sec = cpu_to_le64(je->eta_sec); |
Jens Axboe | 8c621fb | 2012-03-08 12:30:48 +0100 | [diff] [blame] | 692 | je->nr_threads = cpu_to_le32(je->nr_threads); |
Jens Axboe | b7f05eb | 2012-05-11 20:33:02 +0200 | [diff] [blame] | 693 | je->is_pow2 = cpu_to_le32(je->is_pow2); |
Jens Axboe | ed2c0a1 | 2013-04-11 12:55:16 +0200 | [diff] [blame] | 694 | je->unit_base = cpu_to_le32(je->unit_base); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 695 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 696 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 697 | free(je); |
| 698 | return 0; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 701 | static int send_update_job_reply(int fd, uint64_t __tag, int error) |
| 702 | { |
| 703 | uint64_t tag = __tag; |
| 704 | uint32_t pdu_error; |
| 705 | |
| 706 | pdu_error = __cpu_to_le32(error); |
| 707 | return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL); |
| 708 | } |
| 709 | |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 710 | static int handle_update_job_cmd(struct fio_net_cmd *cmd) |
| 711 | { |
| 712 | struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload; |
| 713 | struct thread_data *td; |
| 714 | uint32_t tnumber; |
| 715 | |
| 716 | tnumber = le32_to_cpu(pdu->thread_number); |
| 717 | |
| 718 | dprint(FD_NET, "server: updating options for job %u\n", tnumber); |
| 719 | |
Jens Axboe | 30ffacb | 2012-03-28 09:15:05 +0200 | [diff] [blame] | 720 | if (!tnumber || tnumber > thread_number) { |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 721 | send_update_job_reply(server_fd, cmd->tag, ENODEV); |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 722 | return 0; |
| 723 | } |
| 724 | |
Jens Axboe | 30ffacb | 2012-03-28 09:15:05 +0200 | [diff] [blame] | 725 | td = &threads[tnumber - 1]; |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 726 | convert_thread_options_to_cpu(&td->o, &pdu->top); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 727 | send_update_job_reply(server_fd, cmd->tag, 0); |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 728 | return 0; |
| 729 | } |
| 730 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 731 | static int handle_command(struct flist_head *job_list, struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 732 | { |
| 733 | int ret; |
| 734 | |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 735 | dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n", |
| 736 | fio_server_op(cmd->opcode), cmd->pdu_len, |
| 737 | (unsigned long long) cmd->tag); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 738 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 739 | switch (cmd->opcode) { |
| 740 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 741 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 742 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 743 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 744 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 745 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 746 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 747 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 748 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 749 | case FIO_NET_CMD_JOBLINE: |
| 750 | ret = handle_jobline_cmd(cmd); |
| 751 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 752 | case FIO_NET_CMD_PROBE: |
| 753 | ret = handle_probe_cmd(cmd); |
| 754 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 755 | case FIO_NET_CMD_SEND_ETA: |
| 756 | ret = handle_send_eta_cmd(cmd); |
| 757 | break; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 758 | case FIO_NET_CMD_RUN: |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 759 | ret = handle_run_cmd(job_list, cmd); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 760 | break; |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 761 | case FIO_NET_CMD_UPDATE_JOB: |
| 762 | ret = handle_update_job_cmd(cmd); |
| 763 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 764 | default: |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 765 | log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 766 | ret = 1; |
| 767 | } |
| 768 | |
| 769 | return ret; |
| 770 | } |
| 771 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 772 | static int handle_connection(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 773 | { |
| 774 | struct fio_net_cmd *cmd = NULL; |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 775 | FLIST_HEAD(job_list); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 776 | int ret = 0; |
| 777 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 778 | reset_fio_state(); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 779 | server_fd = sk; |
| 780 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 781 | /* read forever */ |
| 782 | while (!exit_backend) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 783 | struct pollfd pfd = { |
| 784 | .fd = sk, |
| 785 | .events = POLLIN, |
| 786 | }; |
| 787 | |
| 788 | ret = 0; |
| 789 | do { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 790 | int timeout = 1000; |
| 791 | |
| 792 | if (!flist_empty(&job_list)) |
| 793 | timeout = 100; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 794 | |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 795 | ret = poll(&pfd, 1, timeout); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 796 | if (ret < 0) { |
| 797 | if (errno == EINTR) |
| 798 | break; |
| 799 | log_err("fio: poll: %s\n", strerror(errno)); |
| 800 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 801 | } else if (!ret) { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 802 | fio_server_check_jobs(&job_list); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 803 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 804 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 805 | |
| 806 | if (pfd.revents & POLLIN) |
| 807 | break; |
| 808 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 809 | ret = 1; |
| 810 | break; |
| 811 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 812 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 813 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 814 | fio_server_check_jobs(&job_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 815 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 816 | if (ret < 0) |
| 817 | break; |
| 818 | |
| 819 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 820 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 821 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 822 | break; |
| 823 | } |
| 824 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 825 | ret = handle_command(&job_list, cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 826 | if (ret) |
| 827 | break; |
| 828 | |
| 829 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 830 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | if (cmd) |
| 834 | free(cmd); |
| 835 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 836 | close(sk); |
| 837 | _exit(ret); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 840 | static int accept_loop(int listen_sk) |
| 841 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 842 | struct sockaddr_in addr; |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 843 | struct sockaddr_in6 addr6; |
| 844 | socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 845 | struct pollfd pfd; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 846 | int ret = 0, sk, flags, exitval = 0; |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 847 | FLIST_HEAD(conn_list); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 848 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 849 | dprint(FD_NET, "server enter accept loop\n"); |
| 850 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 851 | flags = fcntl(listen_sk, F_GETFL); |
| 852 | flags |= O_NONBLOCK; |
| 853 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 854 | |
| 855 | while (!exit_backend) { |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 856 | const char *from; |
| 857 | char buf[64]; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 858 | pid_t pid; |
| 859 | |
| 860 | pfd.fd = listen_sk; |
| 861 | pfd.events = POLLIN; |
| 862 | do { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 863 | int timeout = 1000; |
| 864 | |
| 865 | if (!flist_empty(&conn_list)) |
| 866 | timeout = 100; |
| 867 | |
| 868 | ret = poll(&pfd, 1, timeout); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 869 | if (ret < 0) { |
| 870 | if (errno == EINTR) |
| 871 | break; |
| 872 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 873 | break; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 874 | } else if (!ret) { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 875 | fio_server_check_conns(&conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 876 | continue; |
| 877 | } |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 878 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 879 | if (pfd.revents & POLLIN) |
| 880 | break; |
| 881 | } while (!exit_backend); |
| 882 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 883 | fio_server_check_conns(&conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 884 | |
| 885 | if (exit_backend || ret < 0) |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 886 | break; |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 887 | |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 888 | if (use_ipv6) |
| 889 | sk = accept(listen_sk, (struct sockaddr *) &addr6, &len); |
| 890 | else |
| 891 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
| 892 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 893 | if (sk < 0) { |
| 894 | log_err("fio: accept: %s\n", strerror(errno)); |
| 895 | return -1; |
| 896 | } |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 897 | |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 898 | if (use_ipv6) |
| 899 | from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf)); |
| 900 | else |
| 901 | from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf)); |
| 902 | |
| 903 | dprint(FD_NET, "server: connect from %s\n", from); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 904 | |
| 905 | pid = fork(); |
| 906 | if (pid) { |
| 907 | close(sk); |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 908 | fio_server_add_conn_pid(&conn_list, pid); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 909 | continue; |
| 910 | } |
| 911 | |
| 912 | /* exits */ |
| 913 | handle_connection(sk); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 914 | } |
| 915 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 916 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 917 | } |
| 918 | |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 919 | int fio_server_text_output(int level, const char *buf, size_t len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 920 | { |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 921 | struct cmd_text_pdu *pdu; |
| 922 | unsigned int tlen; |
| 923 | struct timeval tv; |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 924 | |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 925 | if (server_fd == -1) |
| 926 | return log_local_buf(buf, len); |
| 927 | |
| 928 | tlen = sizeof(*pdu) + len; |
| 929 | pdu = malloc(tlen); |
| 930 | |
| 931 | pdu->level = __cpu_to_le32(level); |
| 932 | pdu->buf_len = __cpu_to_le32(len); |
| 933 | |
| 934 | gettimeofday(&tv, NULL); |
| 935 | pdu->log_sec = __cpu_to_le64(tv.tv_sec); |
| 936 | pdu->log_usec = __cpu_to_le64(tv.tv_usec); |
| 937 | |
| 938 | memcpy(pdu->buf, buf, len); |
| 939 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 940 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL); |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 941 | free(pdu); |
| 942 | return len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 943 | } |
| 944 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 945 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 946 | { |
| 947 | dst->max_val = cpu_to_le64(src->max_val); |
| 948 | dst->min_val = cpu_to_le64(src->min_val); |
| 949 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 950 | |
| 951 | /* |
| 952 | * Encode to IEEE 754 for network transfer |
| 953 | */ |
| 954 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 955 | dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f)); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 959 | { |
| 960 | int i; |
| 961 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 962 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 963 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 964 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 965 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 966 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 967 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 968 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 969 | } |
| 970 | |
| 971 | dst->kb_base = cpu_to_le32(src->kb_base); |
Steven Noonan | ad705bc | 2013-04-08 15:05:25 -0700 | [diff] [blame] | 972 | dst->unit_base = cpu_to_le32(src->unit_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 973 | dst->groupid = cpu_to_le32(src->groupid); |
Jens Axboe | 771e58b | 2013-01-30 12:56:23 +0100 | [diff] [blame] | 974 | dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /* |
| 978 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 979 | * into a single payload. |
| 980 | */ |
| 981 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 982 | { |
| 983 | struct cmd_ts_pdu p; |
| 984 | int i, j; |
| 985 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 986 | dprint(FD_NET, "server sending end stats\n"); |
| 987 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 988 | memset(&p, 0, sizeof(p)); |
| 989 | |
Jens Axboe | 4e59d0f | 2014-03-14 08:41:39 -0600 | [diff] [blame] | 990 | strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1); |
| 991 | strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1); |
| 992 | strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 993 | |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 994 | p.ts.error = cpu_to_le32(ts->error); |
| 995 | p.ts.thread_number = cpu_to_le32(ts->thread_number); |
| 996 | p.ts.groupid = cpu_to_le32(ts->groupid); |
| 997 | p.ts.pid = cpu_to_le32(ts->pid); |
| 998 | p.ts.members = cpu_to_le32(ts->members); |
Jens Axboe | 771e58b | 2013-01-30 12:56:23 +0100 | [diff] [blame] | 999 | p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1000 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 1001 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1002 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 1003 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 1004 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 1005 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 1006 | } |
| 1007 | |
| 1008 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 1009 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 1010 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 1011 | p.ts.minf = cpu_to_le64(ts->minf); |
| 1012 | p.ts.majf = cpu_to_le64(ts->majf); |
| 1013 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 1014 | |
| 1015 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 1016 | fio_fp64_t *src = &ts->percentile_list[i]; |
| 1017 | fio_fp64_t *dst = &p.ts.percentile_list[i]; |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 1018 | |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 1019 | dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f)); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 1020 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1021 | |
| 1022 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 1023 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 1024 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 1025 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 1026 | } |
| 1027 | |
| 1028 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 1029 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 1030 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 1031 | } |
| 1032 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 1033 | for (i = 0; i < DDIR_RWDIR_CNT; i++) |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1034 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 1035 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 1036 | |
Jens Axboe | 78799de | 2013-01-29 20:53:52 +0100 | [diff] [blame] | 1037 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1038 | p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]); |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 1039 | p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 1042 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1043 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 1044 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 1045 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1046 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 1047 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 1048 | } |
| 1049 | |
| 1050 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 1051 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 1052 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 1053 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 1054 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Steven Noonan | ad705bc | 2013-04-08 15:05:25 -0700 | [diff] [blame] | 1055 | p.ts.unit_base = cpu_to_le32(ts->unit_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1056 | |
Jens Axboe | 3e260a4 | 2013-12-09 12:38:53 -0700 | [diff] [blame] | 1057 | p.ts.latency_depth = cpu_to_le32(ts->latency_depth); |
| 1058 | p.ts.latency_target = cpu_to_le64(ts->latency_target); |
| 1059 | p.ts.latency_window = cpu_to_le64(ts->latency_window); |
| 1060 | p.ts.latency_percentile.u.i = __cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f)); |
| 1061 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1062 | convert_gs(&p.rs, rs); |
| 1063 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1064 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | void fio_server_send_gs(struct group_run_stats *rs) |
| 1068 | { |
| 1069 | struct group_run_stats gs; |
| 1070 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 1071 | dprint(FD_NET, "server sending group run stats\n"); |
| 1072 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1073 | convert_gs(&gs, rs); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1074 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1077 | static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src) |
| 1078 | { |
| 1079 | int i; |
| 1080 | |
| 1081 | for (i = 0; i < 2; i++) { |
| 1082 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 1083 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 1084 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 1085 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 1086 | } |
| 1087 | |
| 1088 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 1089 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 1090 | dst->slavecount = cpu_to_le32(src->slavecount); |
| 1091 | dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f)); |
| 1092 | } |
| 1093 | |
| 1094 | static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src) |
| 1095 | { |
| 1096 | int i; |
| 1097 | |
| 1098 | strcpy((char *) dst->name, (char *) src->name); |
| 1099 | |
| 1100 | for (i = 0; i < 2; i++) { |
| 1101 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 1102 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 1103 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 1104 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 1105 | } |
| 1106 | |
| 1107 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 1108 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 1109 | dst->msec = cpu_to_le64(src->msec); |
| 1110 | } |
| 1111 | |
| 1112 | void fio_server_send_du(void) |
| 1113 | { |
| 1114 | struct disk_util *du; |
| 1115 | struct flist_head *entry; |
| 1116 | struct cmd_du_pdu pdu; |
| 1117 | |
| 1118 | dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list)); |
| 1119 | |
Jens Axboe | 0766d92 | 2011-10-13 12:02:08 +0200 | [diff] [blame] | 1120 | memset(&pdu, 0, sizeof(pdu)); |
| 1121 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1122 | flist_for_each(entry, &disk_list) { |
| 1123 | du = flist_entry(entry, struct disk_util, list); |
| 1124 | |
| 1125 | convert_dus(&pdu.dus, &du->dus); |
| 1126 | convert_agg(&pdu.agg, &du->agg); |
| 1127 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1128 | fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1132 | /* |
| 1133 | * Send a command with a separate PDU, not inlined in the command |
| 1134 | */ |
| 1135 | static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf, |
| 1136 | off_t size, uint64_t tag, uint32_t flags) |
| 1137 | { |
| 1138 | struct fio_net_cmd cmd; |
| 1139 | struct iovec iov[2]; |
| 1140 | |
Jens Axboe | 9f24726 | 2014-03-03 13:53:39 -0700 | [diff] [blame] | 1141 | iov[0].iov_base = (void *) &cmd; |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1142 | iov[0].iov_len = sizeof(cmd); |
| 1143 | iov[1].iov_base = (void *) buf; |
| 1144 | iov[1].iov_len = size; |
| 1145 | |
| 1146 | __fio_init_net_cmd(&cmd, opcode, size, tag); |
| 1147 | cmd.flags = __cpu_to_le32(flags); |
| 1148 | fio_net_cmd_crc_pdu(&cmd, buf); |
| 1149 | |
Jens Axboe | 8c95307 | 2012-03-20 14:35:35 +0100 | [diff] [blame] | 1150 | return fio_sendv_data(sk, iov, 2); |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1153 | static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log) |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1154 | { |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1155 | int ret = 0; |
| 1156 | #ifdef CONFIG_ZLIB |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1157 | z_stream stream; |
| 1158 | void *out_pdu; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1159 | |
| 1160 | /* |
| 1161 | * Dirty - since the log is potentially huge, compress it into |
| 1162 | * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving |
| 1163 | * side defragment it. |
| 1164 | */ |
| 1165 | out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU); |
| 1166 | |
| 1167 | stream.zalloc = Z_NULL; |
| 1168 | stream.zfree = Z_NULL; |
| 1169 | stream.opaque = Z_NULL; |
| 1170 | |
| 1171 | if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) { |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1172 | ret = 1; |
| 1173 | goto err; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1176 | stream.next_in = (void *) log->log; |
| 1177 | stream.avail_in = log->nr_samples * sizeof(struct io_sample); |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1178 | |
| 1179 | do { |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1180 | unsigned int this_len, flags = 0; |
| 1181 | int ret; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1182 | |
| 1183 | stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU; |
| 1184 | stream.next_out = out_pdu; |
Jens Axboe | 3c547fe | 2012-03-14 21:53:00 +0100 | [diff] [blame] | 1185 | ret = deflate(&stream, Z_FINISH); |
| 1186 | /* may be Z_OK, or Z_STREAM_END */ |
| 1187 | if (ret < 0) |
| 1188 | goto err_zlib; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1189 | |
| 1190 | this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out; |
| 1191 | |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1192 | if (stream.avail_in) |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1193 | flags = FIO_NET_CMD_F_MORE; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1194 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1195 | ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, |
| 1196 | out_pdu, this_len, 0, flags); |
| 1197 | if (ret) |
| 1198 | goto err_zlib; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1199 | } while (stream.avail_in); |
| 1200 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1201 | err_zlib: |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1202 | deflateEnd(&stream); |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1203 | err: |
| 1204 | free(out_pdu); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1205 | #endif |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1206 | return ret; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1209 | int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name) |
| 1210 | { |
| 1211 | struct cmd_iolog_pdu pdu; |
| 1212 | int i, ret = 0; |
| 1213 | |
| 1214 | pdu.thread_number = cpu_to_le32(td->thread_number); |
| 1215 | pdu.nr_samples = __cpu_to_le32(log->nr_samples); |
| 1216 | pdu.log_type = cpu_to_le32(log->log_type); |
| 1217 | pdu.compressed = cpu_to_le32(use_zlib); |
| 1218 | strcpy((char *) pdu.name, name); |
| 1219 | |
| 1220 | for (i = 0; i < log->nr_samples; i++) { |
| 1221 | struct io_sample *s = &log->log[i]; |
| 1222 | |
| 1223 | s->time = cpu_to_le64(s->time); |
| 1224 | s->val = cpu_to_le64(s->val); |
| 1225 | s->ddir = cpu_to_le32(s->ddir); |
| 1226 | s->bs = cpu_to_le32(s->bs); |
| 1227 | } |
| 1228 | |
| 1229 | /* |
| 1230 | * Send header first, it's not compressed. |
| 1231 | */ |
| 1232 | ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu, |
| 1233 | sizeof(pdu), 0, FIO_NET_CMD_F_MORE); |
| 1234 | if (ret) |
| 1235 | return ret; |
| 1236 | |
| 1237 | /* |
| 1238 | * Now send actual log, compress if we can, otherwise just plain |
| 1239 | */ |
| 1240 | if (use_zlib) |
| 1241 | return fio_send_iolog_gz(&pdu, log); |
| 1242 | |
| 1243 | return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log, |
| 1244 | log->nr_samples * sizeof(struct io_sample), 0, 0); |
| 1245 | } |
| 1246 | |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 1247 | void fio_server_send_add_job(struct thread_data *td) |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1248 | { |
| 1249 | struct cmd_add_job_pdu pdu; |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1250 | |
Jens Axboe | 731e30a | 2012-03-14 16:35:37 +0100 | [diff] [blame] | 1251 | memset(&pdu, 0, sizeof(pdu)); |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 1252 | pdu.thread_number = cpu_to_le32(td->thread_number); |
| 1253 | pdu.groupid = cpu_to_le32(td->groupid); |
| 1254 | convert_thread_options_to_net(&pdu.top, &td->o); |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1255 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1256 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL); |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1257 | } |
| 1258 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 1259 | void fio_server_send_start(struct thread_data *td) |
| 1260 | { |
| 1261 | assert(server_fd != -1); |
| 1262 | |
| 1263 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL); |
| 1264 | } |
| 1265 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1266 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1267 | { |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1268 | struct sockaddr *addr; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 1269 | socklen_t socklen; |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1270 | char buf[80]; |
| 1271 | const char *str; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1272 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1273 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1274 | if (use_ipv6) |
| 1275 | sk = socket(AF_INET6, SOCK_STREAM, 0); |
| 1276 | else |
| 1277 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 1278 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1279 | if (sk < 0) { |
| 1280 | log_err("fio: socket: %s\n", strerror(errno)); |
| 1281 | return -1; |
| 1282 | } |
| 1283 | |
| 1284 | opt = 1; |
Jens Axboe | 1f81991 | 2013-01-23 17:21:41 -0700 | [diff] [blame] | 1285 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1286 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1287 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1288 | return -1; |
| 1289 | } |
| 1290 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 1291 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1292 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1293 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1294 | return -1; |
| 1295 | } |
| 1296 | #endif |
| 1297 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1298 | if (use_ipv6) { |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1299 | const void *src = &saddr_in6.sin6_addr; |
| 1300 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1301 | addr = (struct sockaddr *) &saddr_in6; |
| 1302 | socklen = sizeof(saddr_in6); |
| 1303 | saddr_in6.sin6_family = AF_INET6; |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1304 | str = inet_ntop(AF_INET6, src, buf, sizeof(buf)); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1305 | } else { |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1306 | const void *src = &saddr_in.sin_addr; |
| 1307 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1308 | addr = (struct sockaddr *) &saddr_in; |
| 1309 | socklen = sizeof(saddr_in); |
| 1310 | saddr_in.sin_family = AF_INET; |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1311 | str = inet_ntop(AF_INET, src, buf, sizeof(buf)); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1312 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1313 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1314 | if (bind(sk, addr, socklen) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1315 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1316 | log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1317 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1318 | return -1; |
| 1319 | } |
| 1320 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1321 | return sk; |
| 1322 | } |
| 1323 | |
| 1324 | static int fio_init_server_sock(void) |
| 1325 | { |
| 1326 | struct sockaddr_un addr; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 1327 | socklen_t len; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1328 | mode_t mode; |
| 1329 | int sk; |
| 1330 | |
| 1331 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1332 | if (sk < 0) { |
| 1333 | log_err("fio: socket: %s\n", strerror(errno)); |
| 1334 | return -1; |
| 1335 | } |
| 1336 | |
| 1337 | mode = umask(000); |
| 1338 | |
| 1339 | memset(&addr, 0, sizeof(addr)); |
| 1340 | addr.sun_family = AF_UNIX; |
| 1341 | strcpy(addr.sun_path, bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1342 | |
| 1343 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 1344 | |
| 1345 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 1346 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1347 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1348 | return -1; |
| 1349 | } |
| 1350 | |
| 1351 | umask(mode); |
| 1352 | return sk; |
| 1353 | } |
| 1354 | |
| 1355 | static int fio_init_server_connection(void) |
| 1356 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1357 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1358 | int sk; |
| 1359 | |
| 1360 | dprint(FD_NET, "starting server\n"); |
| 1361 | |
| 1362 | if (!bind_sock) |
| 1363 | sk = fio_init_server_ip(); |
| 1364 | else |
| 1365 | sk = fio_init_server_sock(); |
| 1366 | |
| 1367 | if (sk < 0) |
| 1368 | return sk; |
| 1369 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1370 | if (!bind_sock) { |
| 1371 | char *p, port[16]; |
| 1372 | const void *src; |
| 1373 | int af; |
| 1374 | |
| 1375 | if (use_ipv6) { |
| 1376 | af = AF_INET6; |
| 1377 | src = &saddr_in6.sin6_addr; |
| 1378 | } else { |
| 1379 | af = AF_INET; |
| 1380 | src = &saddr_in.sin_addr; |
| 1381 | } |
| 1382 | |
| 1383 | p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str)); |
| 1384 | |
| 1385 | sprintf(port, ",%u", fio_net_port); |
| 1386 | if (p) |
| 1387 | strcat(p, port); |
| 1388 | else |
| 1389 | strcpy(bind_str, port); |
| 1390 | } else |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1391 | strcpy(bind_str, bind_sock); |
| 1392 | |
| 1393 | log_info("fio: server listening on %s\n", bind_str); |
| 1394 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1395 | if (listen(sk, 0) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1396 | log_err("fio: listen: %s\n", strerror(errno)); |
| 1397 | return -1; |
| 1398 | } |
| 1399 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1400 | return sk; |
| 1401 | } |
| 1402 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1403 | int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp, |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1404 | struct in6_addr *inp6) |
| 1405 | |
| 1406 | { |
| 1407 | int ret = 0; |
| 1408 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1409 | if (ipv6) |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1410 | ret = inet_pton(AF_INET6, host, inp6); |
| 1411 | else |
| 1412 | ret = inet_pton(AF_INET, host, inp); |
| 1413 | |
| 1414 | if (ret != 1) { |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1415 | struct addrinfo hints, *res; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1416 | |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1417 | memset(&hints, 0, sizeof(hints)); |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1418 | hints.ai_family = ipv6 ? AF_INET6 : AF_INET; |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1419 | hints.ai_socktype = SOCK_STREAM; |
| 1420 | |
| 1421 | ret = getaddrinfo(host, NULL, &hints, &res); |
| 1422 | if (ret) { |
| 1423 | log_err("fio: failed to resolve <%s> (%s)\n", host, |
| 1424 | gai_strerror(ret)); |
Jens Axboe | 3caf43e | 2014-01-24 18:52:16 -0800 | [diff] [blame] | 1425 | return 1; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1428 | if (ipv6) |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1429 | memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6)); |
| 1430 | else |
| 1431 | memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp)); |
| 1432 | |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1433 | ret = 1; |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1434 | freeaddrinfo(res); |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | return !(ret == 1); |
| 1438 | } |
| 1439 | |
Jens Axboe | 660a2bf | 2011-10-24 09:35:06 +0200 | [diff] [blame] | 1440 | /* |
| 1441 | * Parse a host/ip/port string. Reads from 'str'. |
| 1442 | * |
| 1443 | * Outputs: |
| 1444 | * |
| 1445 | * For IPv4: |
| 1446 | * *ptr is the host, *port is the port, inp is the destination. |
| 1447 | * For IPv6: |
| 1448 | * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1. |
| 1449 | * For local domain sockets: |
| 1450 | * *ptr is the filename, *is_sock is 1. |
| 1451 | */ |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1452 | int fio_server_parse_string(const char *str, char **ptr, int *is_sock, |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1453 | int *port, struct in_addr *inp, |
| 1454 | struct in6_addr *inp6, int *ipv6) |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1455 | { |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1456 | const char *host = str; |
| 1457 | char *portp; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1458 | int lport = 0; |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1459 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1460 | *ptr = NULL; |
| 1461 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1462 | *port = fio_net_port; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1463 | *ipv6 = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1464 | |
| 1465 | if (!strncmp(str, "sock:", 5)) { |
| 1466 | *ptr = strdup(str + 5); |
| 1467 | *is_sock = 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1468 | |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Is it ip:<ip or host>:port |
| 1474 | */ |
| 1475 | if (!strncmp(host, "ip:", 3)) |
| 1476 | host += 3; |
| 1477 | else if (!strncmp(host, "ip4:", 4)) |
| 1478 | host += 4; |
| 1479 | else if (!strncmp(host, "ip6:", 4)) { |
| 1480 | host += 4; |
| 1481 | *ipv6 = 1; |
| 1482 | } else if (host[0] == ':') { |
| 1483 | /* String is :port */ |
| 1484 | host++; |
| 1485 | lport = atoi(host); |
| 1486 | if (!lport || lport > 65535) { |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 1487 | log_err("fio: bad server port %u\n", lport); |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1488 | return 1; |
| 1489 | } |
| 1490 | /* no hostname given, we are done */ |
| 1491 | *port = lport; |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
Jens Axboe | b96b90c | 2014-03-25 10:37:56 -0700 | [diff] [blame] | 1496 | * If no port seen yet, check if there's a last ',' at the end |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1497 | */ |
| 1498 | if (!lport) { |
| 1499 | portp = strchr(host, ','); |
| 1500 | if (portp) { |
| 1501 | *portp = '\0'; |
| 1502 | portp++; |
| 1503 | lport = atoi(portp); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1504 | if (!lport || lport > 65535) { |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 1505 | log_err("fio: bad server port %u\n", lport); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1506 | return 1; |
| 1507 | } |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | if (lport) |
| 1512 | *port = lport; |
| 1513 | |
| 1514 | if (!strlen(host)) |
| 1515 | return 0; |
| 1516 | |
| 1517 | *ptr = strdup(host); |
| 1518 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1519 | if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) { |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1520 | free(*ptr); |
| 1521 | *ptr = NULL; |
| 1522 | return 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | if (*port == 0) |
| 1526 | *port = fio_net_port; |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1531 | /* |
| 1532 | * Server arg should be one of: |
| 1533 | * |
| 1534 | * sock:/path/to/socket |
| 1535 | * ip:1.2.3.4 |
| 1536 | * 1.2.3.4 |
| 1537 | * |
| 1538 | * Where sock uses unix domain sockets, and ip binds the server to |
| 1539 | * a specific interface. If no arguments are given to the server, it |
| 1540 | * uses IP and binds to 0.0.0.0. |
| 1541 | * |
| 1542 | */ |
| 1543 | static int fio_handle_server_arg(void) |
| 1544 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1545 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1546 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1547 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1548 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 1549 | |
| 1550 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1551 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1552 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1553 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1554 | &port, &saddr_in.sin_addr, |
| 1555 | &saddr_in6.sin6_addr, &use_ipv6); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1556 | |
| 1557 | if (!is_sock && bind_sock) { |
| 1558 | free(bind_sock); |
| 1559 | bind_sock = NULL; |
| 1560 | } |
| 1561 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1562 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1563 | fio_net_port = port; |
| 1564 | saddr_in.sin_port = htons(port); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1565 | saddr_in6.sin6_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1566 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Jens Axboe | 43cdea1 | 2014-03-25 15:06:17 -0700 | [diff] [blame] | 1569 | static void sig_int(int sig) |
| 1570 | { |
| 1571 | if (bind_sock) |
| 1572 | unlink(bind_sock); |
| 1573 | } |
| 1574 | |
| 1575 | static void set_sig_handlers(void) |
| 1576 | { |
| 1577 | struct sigaction act; |
| 1578 | |
| 1579 | memset(&act, 0, sizeof(act)); |
| 1580 | act.sa_handler = sig_int; |
| 1581 | act.sa_flags = SA_RESTART; |
| 1582 | sigaction(SIGINT, &act, NULL); |
| 1583 | } |
| 1584 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1585 | static int fio_server(void) |
| 1586 | { |
| 1587 | int sk, ret; |
| 1588 | |
| 1589 | dprint(FD_NET, "starting server\n"); |
| 1590 | |
| 1591 | if (fio_handle_server_arg()) |
| 1592 | return -1; |
| 1593 | |
| 1594 | sk = fio_init_server_connection(); |
| 1595 | if (sk < 0) |
| 1596 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1597 | |
Jens Axboe | 43cdea1 | 2014-03-25 15:06:17 -0700 | [diff] [blame] | 1598 | set_sig_handlers(); |
| 1599 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1600 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1601 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1602 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1603 | |
| 1604 | if (fio_server_arg) { |
| 1605 | free(fio_server_arg); |
| 1606 | fio_server_arg = NULL; |
| 1607 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1608 | if (bind_sock) |
| 1609 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1610 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1611 | return ret; |
| 1612 | } |
| 1613 | |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1614 | void fio_server_got_signal(int signal) |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1615 | { |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1616 | if (signal == SIGPIPE) |
| 1617 | server_fd = -1; |
| 1618 | else { |
| 1619 | log_info("\nfio: terminating on signal %d\n", signal); |
| 1620 | exit_backend = 1; |
| 1621 | } |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1624 | static int check_existing_pidfile(const char *pidfile) |
| 1625 | { |
| 1626 | struct stat sb; |
| 1627 | char buf[16]; |
| 1628 | pid_t pid; |
| 1629 | FILE *f; |
| 1630 | |
| 1631 | if (stat(pidfile, &sb)) |
| 1632 | return 0; |
| 1633 | |
| 1634 | f = fopen(pidfile, "r"); |
| 1635 | if (!f) |
| 1636 | return 0; |
| 1637 | |
Jens Axboe | bfc3b17 | 2011-10-10 21:16:55 +0200 | [diff] [blame] | 1638 | if (fread(buf, sb.st_size, 1, f) <= 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1639 | fclose(f); |
| 1640 | return 1; |
| 1641 | } |
| 1642 | fclose(f); |
| 1643 | |
| 1644 | pid = atoi(buf); |
| 1645 | if (kill(pid, SIGCONT) < 0) |
Jens Axboe | ea5aa1b | 2011-10-11 11:45:35 +0200 | [diff] [blame] | 1646 | return errno != ESRCH; |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1647 | |
| 1648 | return 1; |
| 1649 | } |
| 1650 | |
| 1651 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1652 | { |
| 1653 | FILE *fpid; |
| 1654 | |
| 1655 | fpid = fopen(pidfile, "w"); |
| 1656 | if (!fpid) { |
| 1657 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1658 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1662 | fclose(fpid); |
| 1663 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * If pidfile is specified, background us. |
| 1668 | */ |
| 1669 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1670 | { |
| 1671 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1672 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1673 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1674 | #if defined(WIN32) |
Jens Axboe | 905c78b | 2012-03-06 19:34:22 +0100 | [diff] [blame] | 1675 | WSADATA wsd; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 1676 | WSAStartup(MAKEWORD(2, 2), &wsd); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1677 | #endif |
| 1678 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1679 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1680 | return fio_server(); |
| 1681 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1682 | if (check_existing_pidfile(pidfile)) { |
| 1683 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 1684 | pidfile); |
| 1685 | return -1; |
| 1686 | } |
| 1687 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1688 | pid = fork(); |
| 1689 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1690 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1691 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 1692 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1693 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1694 | int ret = write_pid(pid, pidfile); |
| 1695 | |
| 1696 | exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1697 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1698 | |
| 1699 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1700 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1701 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1702 | close(STDIN_FILENO); |
| 1703 | close(STDOUT_FILENO); |
| 1704 | close(STDERR_FILENO); |
| 1705 | f_out = NULL; |
| 1706 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1707 | |
| 1708 | ret = fio_server(); |
| 1709 | |
| 1710 | closelog(); |
| 1711 | unlink(pidfile); |
| 1712 | free(pidfile); |
| 1713 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1714 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1715 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1716 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1717 | { |
| 1718 | fio_server_arg = strdup(arg); |
| 1719 | } |