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> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 14 | #include <netinet/in.h> |
| 15 | #include <arpa/inet.h> |
| 16 | #include <netdb.h> |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 17 | #include <syslog.h> |
Jens Axboe | 9e22ecb | 2011-10-04 14:50:21 +0200 | [diff] [blame] | 18 | #include <signal.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 19 | |
| 20 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 21 | #include "server.h" |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 22 | #include "crc/crc16.h" |
Jens Axboe | c7c6cb4 | 2011-10-13 14:12:40 +0200 | [diff] [blame] | 23 | #include "lib/ieee754.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 24 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 25 | int fio_net_port = 8765; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 26 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 27 | int exit_backend = 0; |
| 28 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 29 | static int server_fd = -1; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 30 | static char *fio_server_arg; |
| 31 | static char *bind_sock; |
| 32 | static struct sockaddr_in saddr_in; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 33 | static struct sockaddr_in6 saddr_in6; |
Jens Axboe | 01be038 | 2011-10-15 14:43:41 +0200 | [diff] [blame] | 34 | static int first_cmd_check; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 35 | static int use_ipv6; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 36 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 37 | static const char *fio_server_ops[FIO_NET_CMD_NR] = { |
| 38 | "", |
| 39 | "QUIT", |
| 40 | "EXIT", |
| 41 | "JOB", |
| 42 | "JOBLINE", |
| 43 | "TEXT", |
| 44 | "TS", |
| 45 | "GS", |
| 46 | "SEND_ETA", |
| 47 | "ETA", |
| 48 | "PROBE", |
| 49 | "START", |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 50 | "STOP", |
| 51 | "DISK_UTIL", |
Jens Axboe | 01be038 | 2011-10-15 14:43:41 +0200 | [diff] [blame] | 52 | "RUN", |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | const char *fio_server_op(unsigned int op) |
| 56 | { |
| 57 | static char buf[32]; |
| 58 | |
| 59 | if (op < FIO_NET_CMD_NR) |
| 60 | return fio_server_ops[op]; |
| 61 | |
| 62 | sprintf(buf, "UNKNOWN/%d", op); |
| 63 | return buf; |
| 64 | } |
| 65 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 66 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 67 | { |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 68 | assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU); |
| 69 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 70 | do { |
| 71 | int ret = send(sk, p, len, 0); |
| 72 | |
| 73 | if (ret > 0) { |
| 74 | len -= ret; |
| 75 | if (!len) |
| 76 | break; |
| 77 | p += ret; |
| 78 | continue; |
| 79 | } else if (!ret) |
| 80 | break; |
| 81 | else if (errno == EAGAIN || errno == EINTR) |
| 82 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 83 | else |
| 84 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 85 | } while (!exit_backend); |
| 86 | |
| 87 | if (!len) |
| 88 | return 0; |
| 89 | |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 94 | { |
| 95 | do { |
| 96 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 97 | |
| 98 | if (ret > 0) { |
| 99 | len -= ret; |
| 100 | if (!len) |
| 101 | break; |
| 102 | p += ret; |
| 103 | continue; |
| 104 | } else if (!ret) |
| 105 | break; |
| 106 | else if (errno == EAGAIN || errno == EINTR) |
| 107 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 108 | else |
| 109 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 110 | } while (!exit_backend); |
| 111 | |
| 112 | if (!len) |
| 113 | return 0; |
| 114 | |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 119 | { |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 120 | uint16_t crc; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 121 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 122 | cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); |
| 123 | cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 124 | |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 125 | crc = fio_crc16(cmd, FIO_NET_CMD_CRC_SZ); |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 126 | if (crc != cmd->cmd_crc16) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 127 | 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] | 128 | cmd->cmd_crc16, crc); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | cmd->version = le16_to_cpu(cmd->version); |
| 133 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 134 | cmd->flags = le32_to_cpu(cmd->flags); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 135 | cmd->tag = le64_to_cpu(cmd->tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 136 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 137 | |
| 138 | switch (cmd->version) { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 139 | case FIO_SERVER_VER: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 140 | break; |
| 141 | default: |
| 142 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 143 | return 1; |
| 144 | } |
| 145 | |
| 146 | if (cmd->pdu_len > FIO_SERVER_MAX_PDU) { |
| 147 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Read (and defragment, if necessary) incoming commands |
| 156 | */ |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 157 | struct fio_net_cmd *fio_net_recv_cmd(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 158 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 159 | struct fio_net_cmd cmd, *cmdret = NULL; |
| 160 | size_t cmd_size = 0, pdu_offset = 0; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 161 | uint16_t crc; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 162 | int ret, first = 1; |
| 163 | void *pdu = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 164 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 165 | do { |
| 166 | ret = fio_recv_data(sk, &cmd, sizeof(cmd)); |
| 167 | if (ret) |
| 168 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 169 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 170 | /* We have a command, verify it and swap if need be */ |
| 171 | ret = verify_convert_cmd(&cmd); |
| 172 | if (ret) |
| 173 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 174 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 175 | if (first) { |
| 176 | /* if this is text, add room for \0 at the end */ |
| 177 | cmd_size = sizeof(cmd) + cmd.pdu_len + 1; |
| 178 | assert(!cmdret); |
| 179 | } else |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 180 | cmd_size += cmd.pdu_len; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 181 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 182 | cmdret = realloc(cmdret, cmd_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 183 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 184 | if (first) |
| 185 | memcpy(cmdret, &cmd, sizeof(cmd)); |
Jens Axboe | 67f15dc | 2011-10-15 16:07:40 +0200 | [diff] [blame] | 186 | else if (cmdret->opcode != cmd.opcode) { |
| 187 | log_err("fio: fragment opcode mismatch (%d != %d)\n", |
| 188 | cmdret->opcode, cmd.opcode); |
| 189 | ret = 1; |
| 190 | break; |
| 191 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 192 | |
| 193 | if (!cmd.pdu_len) |
| 194 | break; |
| 195 | |
| 196 | /* There's payload, get it */ |
| 197 | pdu = (void *) cmdret->payload + pdu_offset; |
| 198 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 199 | if (ret) |
| 200 | break; |
| 201 | |
| 202 | /* Verify payload crc */ |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 203 | crc = fio_crc16(pdu, cmd.pdu_len); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 204 | if (crc != cmd.pdu_crc16) { |
| 205 | log_err("fio: server bad crc on payload "); |
| 206 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 207 | ret = 1; |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 212 | if (!first) |
| 213 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 214 | first = 0; |
| 215 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 216 | |
| 217 | if (ret) { |
| 218 | free(cmdret); |
| 219 | cmdret = NULL; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 220 | } else if (cmdret) { |
| 221 | /* zero-terminate text input */ |
| 222 | if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT || |
| 223 | cmdret->opcode == FIO_NET_CMD_JOB)) { |
| 224 | char *buf = (char *) cmdret->payload; |
| 225 | |
| 226 | buf[cmdret->pdu_len ] = '\0'; |
| 227 | } |
| 228 | /* frag flag is internal */ |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 229 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 230 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 231 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 232 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 236 | { |
| 237 | uint32_t pdu_len; |
| 238 | |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 239 | 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] | 240 | |
| 241 | pdu_len = le32_to_cpu(cmd->pdu_len); |
| 242 | if (pdu_len) |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 243 | cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(cmd->payload, pdu_len)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 244 | } |
| 245 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 246 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, |
| 247 | uint64_t tag) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 248 | { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 249 | struct fio_net_cmd *cmd = NULL; |
| 250 | size_t this_len, cur_len = 0; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 251 | int ret; |
| 252 | |
| 253 | do { |
| 254 | this_len = size; |
| 255 | if (this_len > FIO_SERVER_MAX_PDU) |
| 256 | this_len = FIO_SERVER_MAX_PDU; |
| 257 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 258 | if (!cmd || cur_len < sizeof(*cmd) + this_len) { |
| 259 | if (cmd) |
| 260 | free(cmd); |
| 261 | |
| 262 | cur_len = sizeof(*cmd) + this_len; |
| 263 | cmd = malloc(cur_len); |
| 264 | } |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 265 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 266 | fio_init_net_cmd(cmd, opcode, buf, this_len, tag); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 267 | |
| 268 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 269 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 270 | |
| 271 | fio_net_cmd_crc(cmd); |
| 272 | |
| 273 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 274 | size -= this_len; |
| 275 | buf += this_len; |
| 276 | } while (!ret && size); |
| 277 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 278 | if (cmd) |
| 279 | free(cmd); |
| 280 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 281 | return ret; |
| 282 | } |
| 283 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 284 | 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] | 285 | { |
Jens Axboe | 178cde9 | 2011-10-05 22:14:31 +0200 | [diff] [blame] | 286 | struct fio_net_cmd cmd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 287 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 288 | fio_init_net_cmd(&cmd, opcode, NULL, 0, tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 289 | fio_net_cmd_crc(&cmd); |
| 290 | |
| 291 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 292 | } |
| 293 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 294 | /* |
| 295 | * If 'list' is non-NULL, then allocate and store the sent command for |
| 296 | * later verification. |
| 297 | */ |
| 298 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag, |
| 299 | struct flist_head *list) |
| 300 | { |
| 301 | struct fio_net_int_cmd *cmd; |
| 302 | int ret; |
| 303 | |
| 304 | if (!list) |
| 305 | return fio_net_send_simple_stack_cmd(sk, opcode, tag); |
| 306 | |
| 307 | cmd = malloc(sizeof(*cmd)); |
| 308 | |
Jens Axboe | df38093 | 2011-10-11 14:25:08 +0200 | [diff] [blame] | 309 | fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 310 | fio_net_cmd_crc(&cmd->cmd); |
| 311 | |
| 312 | INIT_FLIST_HEAD(&cmd->list); |
| 313 | gettimeofday(&cmd->tv, NULL); |
| 314 | cmd->saved_tag = tag; |
| 315 | |
| 316 | ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd)); |
| 317 | if (ret) { |
| 318 | free(cmd); |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | flist_add_tail(&cmd->list, list); |
| 323 | return 0; |
| 324 | } |
| 325 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 326 | static int fio_server_send_quit_cmd(void) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 327 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 328 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 329 | return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL); |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 330 | } |
| 331 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 332 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 333 | { |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 334 | char *buf = (char *) cmd->payload; |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 335 | struct cmd_start_pdu spdu; |
| 336 | struct cmd_end_pdu epdu; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 337 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 338 | |
Jens Axboe | 108fea7 | 2012-11-14 13:09:45 -0700 | [diff] [blame^] | 339 | stat_number = 0; |
| 340 | |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 341 | if (parse_jobs_ini(buf, 1, 0)) { |
| 342 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 343 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 344 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 345 | |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 346 | spdu.jobs = cpu_to_le32(thread_number); |
Jens Axboe | 108fea7 | 2012-11-14 13:09:45 -0700 | [diff] [blame^] | 347 | spdu.stat_outputs = cpu_to_le32(stat_number); |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 348 | fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 349 | |
Jens Axboe | 2e1df07 | 2012-02-09 11:15:02 +0100 | [diff] [blame] | 350 | ret = fio_backend(); |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 351 | |
| 352 | epdu.error = ret; |
| 353 | fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0); |
| 354 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 355 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 356 | reset_fio_state(); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 361 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 362 | void *pdu = cmd->payload; |
| 363 | struct cmd_single_line_pdu *cslp; |
| 364 | struct cmd_line_pdu *clp; |
| 365 | unsigned long offset; |
| 366 | char **argv; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 367 | int ret, i; |
| 368 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 369 | clp = pdu; |
| 370 | clp->lines = le16_to_cpu(clp->lines); |
| 371 | argv = malloc(clp->lines * sizeof(char *)); |
| 372 | offset = sizeof(*clp); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 373 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 374 | dprint(FD_NET, "server: %d command line args\n", clp->lines); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 375 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 376 | for (i = 0; i < clp->lines; i++) { |
| 377 | cslp = pdu + offset; |
| 378 | argv[i] = (char *) cslp->text; |
| 379 | |
| 380 | offset += sizeof(*cslp) + le16_to_cpu(cslp->len); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 381 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 382 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 383 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 384 | if (parse_cmd_line(clp->lines, argv)) { |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 385 | fio_server_send_quit_cmd(); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 386 | free(argv); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 387 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 388 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 389 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 390 | free(argv); |
| 391 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 392 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 393 | |
Jens Axboe | 2e1df07 | 2012-02-09 11:15:02 +0100 | [diff] [blame] | 394 | ret = fio_backend(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 395 | fio_server_send_quit_cmd(); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 396 | reset_fio_state(); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 397 | return ret; |
| 398 | } |
| 399 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 400 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 401 | { |
| 402 | struct cmd_probe_pdu probe; |
| 403 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 404 | dprint(FD_NET, "server: sending probe reply\n"); |
| 405 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 406 | memset(&probe, 0, sizeof(probe)); |
| 407 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 408 | #ifdef FIO_BIG_ENDIAN |
| 409 | probe.bigendian = 1; |
| 410 | #endif |
Jens Axboe | 5c3f7ce | 2012-04-16 11:44:48 +0200 | [diff] [blame] | 411 | strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version)); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 412 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 413 | probe.os = FIO_OS; |
| 414 | probe.arch = FIO_ARCH; |
| 415 | |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 416 | probe.bpp = sizeof(void *); |
| 417 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 418 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static int handle_send_eta_cmd(struct fio_net_cmd *cmd) |
| 422 | { |
| 423 | struct jobs_eta *je; |
| 424 | size_t size; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 425 | int i; |
| 426 | |
Jens Axboe | b814fb2 | 2011-10-12 09:20:34 +0200 | [diff] [blame] | 427 | if (!thread_number) |
| 428 | return 0; |
| 429 | |
| 430 | size = sizeof(*je) + thread_number * sizeof(char) + 1; |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 431 | je = malloc(size); |
| 432 | memset(je, 0, size); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 433 | |
| 434 | if (!calc_thread_status(je, 1)) { |
| 435 | free(je); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | dprint(FD_NET, "server sending status\n"); |
| 440 | |
| 441 | je->nr_running = cpu_to_le32(je->nr_running); |
| 442 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 443 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 444 | je->files_open = cpu_to_le32(je->files_open); |
| 445 | je->m_rate = cpu_to_le32(je->m_rate); |
| 446 | je->t_rate = cpu_to_le32(je->t_rate); |
| 447 | je->m_iops = cpu_to_le32(je->m_iops); |
| 448 | je->t_iops = cpu_to_le32(je->t_iops); |
| 449 | |
| 450 | for (i = 0; i < 2; i++) { |
| 451 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 452 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 453 | } |
| 454 | |
Jens Axboe | abcf4b7 | 2012-03-06 15:37:40 +0100 | [diff] [blame] | 455 | je->elapsed_sec = cpu_to_le64(je->elapsed_sec); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 456 | je->eta_sec = cpu_to_le64(je->eta_sec); |
Jens Axboe | b7f05eb | 2012-05-11 20:33:02 +0200 | [diff] [blame] | 457 | je->is_pow2 = cpu_to_le32(je->is_pow2); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 458 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 459 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 460 | free(je); |
| 461 | return 0; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 464 | static int handle_command(struct fio_net_cmd *cmd) |
| 465 | { |
| 466 | int ret; |
| 467 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 468 | dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n", |
| 469 | fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 470 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 471 | switch (cmd->opcode) { |
| 472 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 473 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 474 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 475 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 476 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 477 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 478 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 479 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 480 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 481 | case FIO_NET_CMD_JOBLINE: |
| 482 | ret = handle_jobline_cmd(cmd); |
| 483 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 484 | case FIO_NET_CMD_PROBE: |
| 485 | ret = handle_probe_cmd(cmd); |
| 486 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 487 | case FIO_NET_CMD_SEND_ETA: |
| 488 | ret = handle_send_eta_cmd(cmd); |
| 489 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 490 | default: |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 491 | log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 492 | ret = 1; |
| 493 | } |
| 494 | |
| 495 | return ret; |
| 496 | } |
| 497 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 498 | static int handle_connection(int sk, int block) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 499 | { |
| 500 | struct fio_net_cmd *cmd = NULL; |
| 501 | int ret = 0; |
| 502 | |
| 503 | /* read forever */ |
| 504 | while (!exit_backend) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 505 | struct pollfd pfd = { |
| 506 | .fd = sk, |
| 507 | .events = POLLIN, |
| 508 | }; |
| 509 | |
| 510 | ret = 0; |
| 511 | do { |
| 512 | ret = poll(&pfd, 1, 100); |
| 513 | if (ret < 0) { |
| 514 | if (errno == EINTR) |
| 515 | break; |
| 516 | log_err("fio: poll: %s\n", strerror(errno)); |
| 517 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 518 | } else if (!ret) { |
| 519 | if (!block) |
| 520 | return 0; |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 521 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 522 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 523 | |
| 524 | if (pfd.revents & POLLIN) |
| 525 | break; |
| 526 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 527 | ret = 1; |
| 528 | break; |
| 529 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 530 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 531 | |
| 532 | if (ret < 0) |
| 533 | break; |
| 534 | |
| 535 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 536 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 537 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 541 | ret = handle_command(cmd); |
| 542 | if (ret) |
| 543 | break; |
| 544 | |
| 545 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 546 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | if (cmd) |
| 550 | free(cmd); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 555 | void fio_server_idle_loop(void) |
| 556 | { |
Jens Axboe | 01be038 | 2011-10-15 14:43:41 +0200 | [diff] [blame] | 557 | if (!first_cmd_check) |
| 558 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_RUN, 0, NULL); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 559 | if (server_fd != -1) |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 560 | handle_connection(server_fd, 0); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 563 | static int accept_loop(int listen_sk) |
| 564 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 565 | struct sockaddr_in addr; |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 566 | fio_socklen_t len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 567 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 568 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 569 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 570 | dprint(FD_NET, "server enter accept loop\n"); |
| 571 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 572 | flags = fcntl(listen_sk, F_GETFL); |
| 573 | flags |= O_NONBLOCK; |
| 574 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 575 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 576 | pfd.fd = listen_sk; |
| 577 | pfd.events = POLLIN; |
| 578 | do { |
| 579 | ret = poll(&pfd, 1, 100); |
| 580 | if (ret < 0) { |
| 581 | if (errno == EINTR) |
| 582 | break; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 583 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 584 | goto out; |
| 585 | } else if (!ret) |
| 586 | continue; |
| 587 | |
| 588 | if (pfd.revents & POLLIN) |
| 589 | break; |
| 590 | } while (!exit_backend); |
| 591 | |
| 592 | if (exit_backend) |
| 593 | goto out; |
| 594 | |
Jens Axboe | 6b976bf | 2011-10-04 15:07:43 +0200 | [diff] [blame] | 595 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 596 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 597 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 598 | return -1; |
| 599 | } |
| 600 | |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 601 | dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr)); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 602 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 603 | server_fd = sk; |
| 604 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 605 | exitval = handle_connection(sk, 1); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 606 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 607 | server_fd = -1; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 608 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 609 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 610 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 611 | goto again; |
| 612 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 613 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 614 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 615 | } |
| 616 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 617 | int fio_server_text_output(const char *buf, size_t len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 618 | { |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 619 | if (server_fd != -1) |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 620 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0); |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 621 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 622 | return log_local_buf(buf, len); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 623 | } |
| 624 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 625 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 626 | { |
| 627 | dst->max_val = cpu_to_le64(src->max_val); |
| 628 | dst->min_val = cpu_to_le64(src->min_val); |
| 629 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 630 | |
| 631 | /* |
| 632 | * Encode to IEEE 754 for network transfer |
| 633 | */ |
| 634 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 635 | 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] | 636 | } |
| 637 | |
| 638 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 639 | { |
| 640 | int i; |
| 641 | |
| 642 | for (i = 0; i < 2; i++) { |
| 643 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 644 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 645 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 646 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 647 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 648 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 649 | } |
| 650 | |
| 651 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 652 | dst->groupid = cpu_to_le32(src->groupid); |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 657 | * into a single payload. |
| 658 | */ |
| 659 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 660 | { |
| 661 | struct cmd_ts_pdu p; |
| 662 | int i, j; |
| 663 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 664 | dprint(FD_NET, "server sending end stats\n"); |
| 665 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 666 | memset(&p, 0, sizeof(p)); |
| 667 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 668 | strcpy(p.ts.name, ts->name); |
| 669 | strcpy(p.ts.verror, ts->verror); |
| 670 | strcpy(p.ts.description, ts->description); |
| 671 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 672 | p.ts.error = cpu_to_le32(ts->error); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 673 | p.ts.groupid = cpu_to_le32(ts->groupid); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 674 | p.ts.pid = cpu_to_le32(ts->pid); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 675 | p.ts.members = cpu_to_le32(ts->members); |
| 676 | |
| 677 | for (i = 0; i < 2; i++) { |
| 678 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 679 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 680 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 681 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 682 | } |
| 683 | |
| 684 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 685 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 686 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 687 | p.ts.minf = cpu_to_le64(ts->minf); |
| 688 | p.ts.majf = cpu_to_le64(ts->majf); |
| 689 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 690 | |
| 691 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 692 | fio_fp64_t *src = &ts->percentile_list[i]; |
| 693 | fio_fp64_t *dst = &p.ts.percentile_list[i]; |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 694 | |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 695 | 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] | 696 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 697 | |
| 698 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 699 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 700 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 701 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 702 | } |
| 703 | |
| 704 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 705 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 706 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 707 | } |
| 708 | |
| 709 | for (i = 0; i < 2; i++) |
| 710 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 711 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 712 | |
| 713 | for (i = 0; i < 3; i++) { |
| 714 | 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] | 715 | 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] | 716 | } |
| 717 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 718 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 719 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 720 | |
| 721 | for (i = 0; i < 2; i++) { |
| 722 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 723 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 724 | } |
| 725 | |
| 726 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 727 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 728 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 729 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 730 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 731 | |
| 732 | convert_gs(&p.rs, rs); |
| 733 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 734 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | void fio_server_send_gs(struct group_run_stats *rs) |
| 738 | { |
| 739 | struct group_run_stats gs; |
| 740 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 741 | dprint(FD_NET, "server sending group run stats\n"); |
| 742 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 743 | convert_gs(&gs, rs); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 744 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 747 | static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src) |
| 748 | { |
| 749 | int i; |
| 750 | |
| 751 | for (i = 0; i < 2; i++) { |
| 752 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 753 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 754 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 755 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 756 | } |
| 757 | |
| 758 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 759 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 760 | dst->slavecount = cpu_to_le32(src->slavecount); |
| 761 | dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f)); |
| 762 | } |
| 763 | |
| 764 | static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src) |
| 765 | { |
| 766 | int i; |
| 767 | |
| 768 | strcpy((char *) dst->name, (char *) src->name); |
| 769 | |
| 770 | for (i = 0; i < 2; i++) { |
| 771 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 772 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 773 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 774 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 775 | } |
| 776 | |
| 777 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 778 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 779 | dst->msec = cpu_to_le64(src->msec); |
| 780 | } |
| 781 | |
| 782 | void fio_server_send_du(void) |
| 783 | { |
| 784 | struct disk_util *du; |
| 785 | struct flist_head *entry; |
| 786 | struct cmd_du_pdu pdu; |
| 787 | |
| 788 | dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list)); |
| 789 | |
Jens Axboe | 0766d92 | 2011-10-13 12:02:08 +0200 | [diff] [blame] | 790 | memset(&pdu, 0, sizeof(pdu)); |
| 791 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 792 | flist_for_each(entry, &disk_list) { |
| 793 | du = flist_entry(entry, struct disk_util, list); |
| 794 | |
| 795 | convert_dus(&pdu.dus, &du->dus); |
| 796 | convert_agg(&pdu.agg, &du->agg); |
| 797 | |
| 798 | fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0); |
| 799 | } |
| 800 | } |
| 801 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 802 | int fio_server_log(const char *format, ...) |
| 803 | { |
| 804 | char buffer[1024]; |
| 805 | va_list args; |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 806 | size_t len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 807 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 808 | dprint(FD_NET, "server log\n"); |
| 809 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 810 | va_start(args, format); |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 811 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 812 | va_end(args); |
| 813 | |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 814 | return fio_server_text_output(buffer, len); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 815 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 816 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 817 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 818 | { |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 819 | struct sockaddr *addr; |
| 820 | fio_socklen_t socklen; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 821 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 822 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 823 | if (use_ipv6) |
| 824 | sk = socket(AF_INET6, SOCK_STREAM, 0); |
| 825 | else |
| 826 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 827 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 828 | if (sk < 0) { |
| 829 | log_err("fio: socket: %s\n", strerror(errno)); |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | opt = 1; |
| 834 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 835 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 836 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 837 | return -1; |
| 838 | } |
| 839 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 840 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 841 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 842 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 843 | return -1; |
| 844 | } |
| 845 | #endif |
| 846 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 847 | if (use_ipv6) { |
| 848 | addr = (struct sockaddr *) &saddr_in6; |
| 849 | socklen = sizeof(saddr_in6); |
| 850 | saddr_in6.sin6_family = AF_INET6; |
| 851 | } else { |
| 852 | addr = (struct sockaddr *) &saddr_in; |
| 853 | socklen = sizeof(saddr_in); |
| 854 | saddr_in.sin_family = AF_INET; |
| 855 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 856 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 857 | if (bind(sk, addr, socklen) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 858 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 859 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 860 | return -1; |
| 861 | } |
| 862 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 863 | return sk; |
| 864 | } |
| 865 | |
| 866 | static int fio_init_server_sock(void) |
| 867 | { |
| 868 | struct sockaddr_un addr; |
| 869 | fio_socklen_t len; |
| 870 | mode_t mode; |
| 871 | int sk; |
| 872 | |
| 873 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 874 | if (sk < 0) { |
| 875 | log_err("fio: socket: %s\n", strerror(errno)); |
| 876 | return -1; |
| 877 | } |
| 878 | |
| 879 | mode = umask(000); |
| 880 | |
| 881 | memset(&addr, 0, sizeof(addr)); |
| 882 | addr.sun_family = AF_UNIX; |
| 883 | strcpy(addr.sun_path, bind_sock); |
| 884 | unlink(bind_sock); |
| 885 | |
| 886 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 887 | |
| 888 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 889 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 890 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 891 | return -1; |
| 892 | } |
| 893 | |
| 894 | umask(mode); |
| 895 | return sk; |
| 896 | } |
| 897 | |
| 898 | static int fio_init_server_connection(void) |
| 899 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 900 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 901 | int sk; |
| 902 | |
| 903 | dprint(FD_NET, "starting server\n"); |
| 904 | |
| 905 | if (!bind_sock) |
| 906 | sk = fio_init_server_ip(); |
| 907 | else |
| 908 | sk = fio_init_server_sock(); |
| 909 | |
| 910 | if (sk < 0) |
| 911 | return sk; |
| 912 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 913 | if (!bind_sock) { |
| 914 | char *p, port[16]; |
| 915 | const void *src; |
| 916 | int af; |
| 917 | |
| 918 | if (use_ipv6) { |
| 919 | af = AF_INET6; |
| 920 | src = &saddr_in6.sin6_addr; |
| 921 | } else { |
| 922 | af = AF_INET; |
| 923 | src = &saddr_in.sin_addr; |
| 924 | } |
| 925 | |
| 926 | p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str)); |
| 927 | |
| 928 | sprintf(port, ",%u", fio_net_port); |
| 929 | if (p) |
| 930 | strcat(p, port); |
| 931 | else |
| 932 | strcpy(bind_str, port); |
| 933 | } else |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 934 | strcpy(bind_str, bind_sock); |
| 935 | |
| 936 | log_info("fio: server listening on %s\n", bind_str); |
| 937 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 938 | if (listen(sk, 0) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 939 | log_err("fio: listen: %s\n", strerror(errno)); |
| 940 | return -1; |
| 941 | } |
| 942 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 943 | return sk; |
| 944 | } |
| 945 | |
Jens Axboe | 660a2bf | 2011-10-24 09:35:06 +0200 | [diff] [blame] | 946 | /* |
| 947 | * Parse a host/ip/port string. Reads from 'str'. |
| 948 | * |
| 949 | * Outputs: |
| 950 | * |
| 951 | * For IPv4: |
| 952 | * *ptr is the host, *port is the port, inp is the destination. |
| 953 | * For IPv6: |
| 954 | * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1. |
| 955 | * For local domain sockets: |
| 956 | * *ptr is the filename, *is_sock is 1. |
| 957 | */ |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 958 | 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] | 959 | int *port, struct in_addr *inp, |
| 960 | struct in6_addr *inp6, int *ipv6) |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 961 | { |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 962 | const char *host = str; |
| 963 | char *portp; |
| 964 | int ret, lport = 0; |
| 965 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 966 | *ptr = NULL; |
| 967 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 968 | *port = fio_net_port; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 969 | *ipv6 = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 970 | |
| 971 | if (!strncmp(str, "sock:", 5)) { |
| 972 | *ptr = strdup(str + 5); |
| 973 | *is_sock = 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 974 | |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Is it ip:<ip or host>:port |
| 980 | */ |
| 981 | if (!strncmp(host, "ip:", 3)) |
| 982 | host += 3; |
| 983 | else if (!strncmp(host, "ip4:", 4)) |
| 984 | host += 4; |
| 985 | else if (!strncmp(host, "ip6:", 4)) { |
| 986 | host += 4; |
| 987 | *ipv6 = 1; |
| 988 | } else if (host[0] == ':') { |
| 989 | /* String is :port */ |
| 990 | host++; |
| 991 | lport = atoi(host); |
| 992 | if (!lport || lport > 65535) { |
| 993 | log_err("fio: bad server port %u\n", port); |
| 994 | return 1; |
| 995 | } |
| 996 | /* no hostname given, we are done */ |
| 997 | *port = lport; |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * If no port seen yet, check if there's a last ':' at the end |
| 1003 | */ |
| 1004 | if (!lport) { |
| 1005 | portp = strchr(host, ','); |
| 1006 | if (portp) { |
| 1007 | *portp = '\0'; |
| 1008 | portp++; |
| 1009 | lport = atoi(portp); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1010 | if (!lport || lport > 65535) { |
| 1011 | log_err("fio: bad server port %u\n", port); |
| 1012 | return 1; |
| 1013 | } |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | if (lport) |
| 1018 | *port = lport; |
| 1019 | |
| 1020 | if (!strlen(host)) |
| 1021 | return 0; |
| 1022 | |
| 1023 | *ptr = strdup(host); |
| 1024 | |
| 1025 | if (*ipv6) |
| 1026 | ret = inet_pton(AF_INET6, host, inp6); |
| 1027 | else |
| 1028 | ret = inet_pton(AF_INET, host, inp); |
| 1029 | |
| 1030 | if (ret != 1) { |
| 1031 | struct hostent *hent; |
| 1032 | |
| 1033 | hent = gethostbyname(host); |
| 1034 | if (!hent) { |
| 1035 | log_err("fio: failed to resolve <%s>\n", host); |
| 1036 | free(*ptr); |
| 1037 | *ptr = NULL; |
| 1038 | return 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1041 | if (*ipv6) { |
| 1042 | if (hent->h_addrtype != AF_INET6) { |
| 1043 | log_info("fio: falling back to IPv4\n"); |
| 1044 | *ipv6 = 0; |
| 1045 | } else |
| 1046 | memcpy(inp6, hent->h_addr_list[0], 16); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1047 | } |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1048 | if (!*ipv6) { |
| 1049 | if (hent->h_addrtype != AF_INET) { |
| 1050 | log_err("fio: lookup type mismatch\n"); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1051 | free(*ptr); |
| 1052 | *ptr = NULL; |
| 1053 | return 1; |
| 1054 | } |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1055 | memcpy(inp, hent->h_addr_list[0], 4); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | if (*port == 0) |
| 1060 | *port = fio_net_port; |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1065 | /* |
| 1066 | * Server arg should be one of: |
| 1067 | * |
| 1068 | * sock:/path/to/socket |
| 1069 | * ip:1.2.3.4 |
| 1070 | * 1.2.3.4 |
| 1071 | * |
| 1072 | * Where sock uses unix domain sockets, and ip binds the server to |
| 1073 | * a specific interface. If no arguments are given to the server, it |
| 1074 | * uses IP and binds to 0.0.0.0. |
| 1075 | * |
| 1076 | */ |
| 1077 | static int fio_handle_server_arg(void) |
| 1078 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1079 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1080 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1081 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1082 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 1083 | |
| 1084 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1085 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1086 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1087 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1088 | &port, &saddr_in.sin_addr, |
| 1089 | &saddr_in6.sin6_addr, &use_ipv6); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1090 | |
| 1091 | if (!is_sock && bind_sock) { |
| 1092 | free(bind_sock); |
| 1093 | bind_sock = NULL; |
| 1094 | } |
| 1095 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1096 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1097 | fio_net_port = port; |
| 1098 | saddr_in.sin_port = htons(port); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1099 | saddr_in6.sin6_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1100 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static int fio_server(void) |
| 1104 | { |
| 1105 | int sk, ret; |
| 1106 | |
| 1107 | dprint(FD_NET, "starting server\n"); |
| 1108 | |
| 1109 | if (fio_handle_server_arg()) |
| 1110 | return -1; |
| 1111 | |
| 1112 | sk = fio_init_server_connection(); |
| 1113 | if (sk < 0) |
| 1114 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1115 | |
| 1116 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1117 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1118 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1119 | |
| 1120 | if (fio_server_arg) { |
| 1121 | free(fio_server_arg); |
| 1122 | fio_server_arg = NULL; |
| 1123 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1124 | if (bind_sock) |
| 1125 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1126 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1127 | return ret; |
| 1128 | } |
| 1129 | |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1130 | void fio_server_got_signal(int signal) |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1131 | { |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1132 | if (signal == SIGPIPE) |
| 1133 | server_fd = -1; |
| 1134 | else { |
| 1135 | log_info("\nfio: terminating on signal %d\n", signal); |
| 1136 | exit_backend = 1; |
| 1137 | } |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1140 | static int check_existing_pidfile(const char *pidfile) |
| 1141 | { |
| 1142 | struct stat sb; |
| 1143 | char buf[16]; |
| 1144 | pid_t pid; |
| 1145 | FILE *f; |
| 1146 | |
| 1147 | if (stat(pidfile, &sb)) |
| 1148 | return 0; |
| 1149 | |
| 1150 | f = fopen(pidfile, "r"); |
| 1151 | if (!f) |
| 1152 | return 0; |
| 1153 | |
Jens Axboe | bfc3b17 | 2011-10-10 21:16:55 +0200 | [diff] [blame] | 1154 | if (fread(buf, sb.st_size, 1, f) <= 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1155 | fclose(f); |
| 1156 | return 1; |
| 1157 | } |
| 1158 | fclose(f); |
| 1159 | |
| 1160 | pid = atoi(buf); |
| 1161 | if (kill(pid, SIGCONT) < 0) |
Jens Axboe | ea5aa1b | 2011-10-11 11:45:35 +0200 | [diff] [blame] | 1162 | return errno != ESRCH; |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1163 | |
| 1164 | return 1; |
| 1165 | } |
| 1166 | |
| 1167 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1168 | { |
| 1169 | FILE *fpid; |
| 1170 | |
| 1171 | fpid = fopen(pidfile, "w"); |
| 1172 | if (!fpid) { |
| 1173 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1174 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1178 | fclose(fpid); |
| 1179 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * If pidfile is specified, background us. |
| 1184 | */ |
| 1185 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1186 | { |
| 1187 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1188 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1189 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1190 | #if defined(WIN32) |
Jens Axboe | 905c78b | 2012-03-06 19:34:22 +0100 | [diff] [blame] | 1191 | WSADATA wsd; |
| 1192 | WSAStartup(MAKEWORD(2,2), &wsd); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1193 | #endif |
| 1194 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1195 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1196 | return fio_server(); |
| 1197 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1198 | if (check_existing_pidfile(pidfile)) { |
| 1199 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 1200 | pidfile); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1204 | pid = fork(); |
| 1205 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1206 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1207 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 1208 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1209 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1210 | int ret = write_pid(pid, pidfile); |
| 1211 | |
| 1212 | exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1213 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1214 | |
| 1215 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1216 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1217 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1218 | close(STDIN_FILENO); |
| 1219 | close(STDOUT_FILENO); |
| 1220 | close(STDERR_FILENO); |
| 1221 | f_out = NULL; |
| 1222 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1223 | |
| 1224 | ret = fio_server(); |
| 1225 | |
| 1226 | closelog(); |
| 1227 | unlink(pidfile); |
| 1228 | free(pidfile); |
| 1229 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1230 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1231 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1232 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1233 | { |
| 1234 | fio_server_arg = strdup(arg); |
| 1235 | } |