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 | 89cf148 | 2011-10-07 10:10:18 +0200 | [diff] [blame] | 25 | #include "fio_version.h" |
| 26 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 27 | int fio_net_port = 8765; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 28 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 29 | int exit_backend = 0; |
| 30 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 31 | static int server_fd = -1; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 32 | static char *fio_server_arg; |
| 33 | static char *bind_sock; |
| 34 | static struct sockaddr_in saddr_in; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 35 | static struct sockaddr_in6 saddr_in6; |
Jens Axboe | 01be038 | 2011-10-15 14:43:41 +0200 | [diff] [blame] | 36 | static int first_cmd_check; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 37 | static int use_ipv6; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 38 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 39 | static const char *fio_server_ops[FIO_NET_CMD_NR] = { |
| 40 | "", |
| 41 | "QUIT", |
| 42 | "EXIT", |
| 43 | "JOB", |
| 44 | "JOBLINE", |
| 45 | "TEXT", |
| 46 | "TS", |
| 47 | "GS", |
| 48 | "SEND_ETA", |
| 49 | "ETA", |
| 50 | "PROBE", |
| 51 | "START", |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 52 | "STOP", |
| 53 | "DISK_UTIL", |
Jens Axboe | 01be038 | 2011-10-15 14:43:41 +0200 | [diff] [blame] | 54 | "RUN", |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | const char *fio_server_op(unsigned int op) |
| 58 | { |
| 59 | static char buf[32]; |
| 60 | |
| 61 | if (op < FIO_NET_CMD_NR) |
| 62 | return fio_server_ops[op]; |
| 63 | |
| 64 | sprintf(buf, "UNKNOWN/%d", op); |
| 65 | return buf; |
| 66 | } |
| 67 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 68 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 69 | { |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 70 | assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU); |
| 71 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 72 | do { |
| 73 | int ret = send(sk, p, len, 0); |
| 74 | |
| 75 | if (ret > 0) { |
| 76 | len -= ret; |
| 77 | if (!len) |
| 78 | break; |
| 79 | p += ret; |
| 80 | continue; |
| 81 | } else if (!ret) |
| 82 | break; |
| 83 | else if (errno == EAGAIN || errno == EINTR) |
| 84 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 85 | else |
| 86 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 87 | } while (!exit_backend); |
| 88 | |
| 89 | if (!len) |
| 90 | return 0; |
| 91 | |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 96 | { |
| 97 | do { |
| 98 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 99 | |
| 100 | if (ret > 0) { |
| 101 | len -= ret; |
| 102 | if (!len) |
| 103 | break; |
| 104 | p += ret; |
| 105 | continue; |
| 106 | } else if (!ret) |
| 107 | break; |
| 108 | else if (errno == EAGAIN || errno == EINTR) |
| 109 | continue; |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 110 | else |
| 111 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 112 | } while (!exit_backend); |
| 113 | |
| 114 | if (!len) |
| 115 | return 0; |
| 116 | |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 121 | { |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 122 | uint16_t crc; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 123 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 124 | cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); |
| 125 | cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 126 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 127 | crc = crc16(cmd, FIO_NET_CMD_CRC_SZ); |
| 128 | if (crc != cmd->cmd_crc16) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 129 | 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] | 130 | cmd->cmd_crc16, crc); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | cmd->version = le16_to_cpu(cmd->version); |
| 135 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 136 | cmd->flags = le32_to_cpu(cmd->flags); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 137 | cmd->tag = le64_to_cpu(cmd->tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 138 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 139 | |
| 140 | switch (cmd->version) { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 141 | case FIO_SERVER_VER: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 142 | break; |
| 143 | default: |
| 144 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | if (cmd->pdu_len > FIO_SERVER_MAX_PDU) { |
| 149 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 156 | /* |
| 157 | * Read (and defragment, if necessary) incoming commands |
| 158 | */ |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 159 | struct fio_net_cmd *fio_net_recv_cmd(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 160 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 161 | struct fio_net_cmd cmd, *cmdret = NULL; |
| 162 | size_t cmd_size = 0, pdu_offset = 0; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 163 | uint16_t crc; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 164 | int ret, first = 1; |
| 165 | void *pdu = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 166 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 167 | do { |
| 168 | ret = fio_recv_data(sk, &cmd, sizeof(cmd)); |
| 169 | if (ret) |
| 170 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 171 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 172 | /* We have a command, verify it and swap if need be */ |
| 173 | ret = verify_convert_cmd(&cmd); |
| 174 | if (ret) |
| 175 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 176 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 177 | if (first) { |
| 178 | /* if this is text, add room for \0 at the end */ |
| 179 | cmd_size = sizeof(cmd) + cmd.pdu_len + 1; |
| 180 | assert(!cmdret); |
| 181 | } else |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 182 | cmd_size += cmd.pdu_len; |
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 | cmdret = realloc(cmdret, cmd_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 185 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 186 | if (first) |
| 187 | memcpy(cmdret, &cmd, sizeof(cmd)); |
Jens Axboe | 67f15dc | 2011-10-15 16:07:40 +0200 | [diff] [blame] | 188 | else if (cmdret->opcode != cmd.opcode) { |
| 189 | log_err("fio: fragment opcode mismatch (%d != %d)\n", |
| 190 | cmdret->opcode, cmd.opcode); |
| 191 | ret = 1; |
| 192 | break; |
| 193 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 194 | |
| 195 | if (!cmd.pdu_len) |
| 196 | break; |
| 197 | |
| 198 | /* There's payload, get it */ |
| 199 | pdu = (void *) cmdret->payload + pdu_offset; |
| 200 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 201 | if (ret) |
| 202 | break; |
| 203 | |
| 204 | /* Verify payload crc */ |
| 205 | crc = crc16(pdu, cmd.pdu_len); |
| 206 | if (crc != cmd.pdu_crc16) { |
| 207 | log_err("fio: server bad crc on payload "); |
| 208 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 209 | ret = 1; |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 214 | if (!first) |
| 215 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 216 | first = 0; |
| 217 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 218 | |
| 219 | if (ret) { |
| 220 | free(cmdret); |
| 221 | cmdret = NULL; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 222 | } else if (cmdret) { |
| 223 | /* zero-terminate text input */ |
| 224 | if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT || |
| 225 | cmdret->opcode == FIO_NET_CMD_JOB)) { |
| 226 | char *buf = (char *) cmdret->payload; |
| 227 | |
| 228 | buf[cmdret->pdu_len ] = '\0'; |
| 229 | } |
| 230 | /* frag flag is internal */ |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 231 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 232 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 233 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 234 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 238 | { |
| 239 | uint32_t pdu_len; |
| 240 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 241 | cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 242 | |
| 243 | pdu_len = le32_to_cpu(cmd->pdu_len); |
| 244 | if (pdu_len) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 245 | cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 246 | } |
| 247 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 248 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, |
| 249 | uint64_t tag) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 250 | { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 251 | struct fio_net_cmd *cmd = NULL; |
| 252 | size_t this_len, cur_len = 0; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 253 | int ret; |
| 254 | |
| 255 | do { |
| 256 | this_len = size; |
| 257 | if (this_len > FIO_SERVER_MAX_PDU) |
| 258 | this_len = FIO_SERVER_MAX_PDU; |
| 259 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 260 | if (!cmd || cur_len < sizeof(*cmd) + this_len) { |
| 261 | if (cmd) |
| 262 | free(cmd); |
| 263 | |
| 264 | cur_len = sizeof(*cmd) + this_len; |
| 265 | cmd = malloc(cur_len); |
| 266 | } |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 267 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 268 | fio_init_net_cmd(cmd, opcode, buf, this_len, tag); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 269 | |
| 270 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 271 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 272 | |
| 273 | fio_net_cmd_crc(cmd); |
| 274 | |
| 275 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 276 | size -= this_len; |
| 277 | buf += this_len; |
| 278 | } while (!ret && size); |
| 279 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 280 | if (cmd) |
| 281 | free(cmd); |
| 282 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 283 | return ret; |
| 284 | } |
| 285 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 286 | 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] | 287 | { |
Jens Axboe | 178cde9 | 2011-10-05 22:14:31 +0200 | [diff] [blame] | 288 | struct fio_net_cmd cmd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 289 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 290 | fio_init_net_cmd(&cmd, opcode, NULL, 0, tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 291 | fio_net_cmd_crc(&cmd); |
| 292 | |
| 293 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 294 | } |
| 295 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 296 | /* |
| 297 | * If 'list' is non-NULL, then allocate and store the sent command for |
| 298 | * later verification. |
| 299 | */ |
| 300 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag, |
| 301 | struct flist_head *list) |
| 302 | { |
| 303 | struct fio_net_int_cmd *cmd; |
| 304 | int ret; |
| 305 | |
| 306 | if (!list) |
| 307 | return fio_net_send_simple_stack_cmd(sk, opcode, tag); |
| 308 | |
| 309 | cmd = malloc(sizeof(*cmd)); |
| 310 | |
Jens Axboe | df38093 | 2011-10-11 14:25:08 +0200 | [diff] [blame] | 311 | fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 312 | fio_net_cmd_crc(&cmd->cmd); |
| 313 | |
| 314 | INIT_FLIST_HEAD(&cmd->list); |
| 315 | gettimeofday(&cmd->tv, NULL); |
| 316 | cmd->saved_tag = tag; |
| 317 | |
| 318 | ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd)); |
| 319 | if (ret) { |
| 320 | free(cmd); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | flist_add_tail(&cmd->list, list); |
| 325 | return 0; |
| 326 | } |
| 327 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 328 | static int fio_server_send_quit_cmd(void) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 329 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 330 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 331 | 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] | 332 | } |
| 333 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 334 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 335 | { |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 336 | char *buf = (char *) cmd->payload; |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 337 | struct cmd_start_pdu spdu; |
| 338 | struct cmd_end_pdu epdu; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 339 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 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); |
| 347 | 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] | 348 | |
Jens Axboe | 2e1df07 | 2012-02-09 11:15:02 +0100 | [diff] [blame] | 349 | ret = fio_backend(); |
Jens Axboe | 11e950b | 2011-10-16 21:34:14 +0200 | [diff] [blame] | 350 | |
| 351 | epdu.error = ret; |
| 352 | fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0); |
| 353 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 354 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 355 | reset_fio_state(); |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 360 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 361 | void *pdu = cmd->payload; |
| 362 | struct cmd_single_line_pdu *cslp; |
| 363 | struct cmd_line_pdu *clp; |
| 364 | unsigned long offset; |
| 365 | char **argv; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 366 | int ret, i; |
| 367 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 368 | clp = pdu; |
| 369 | clp->lines = le16_to_cpu(clp->lines); |
| 370 | argv = malloc(clp->lines * sizeof(char *)); |
| 371 | offset = sizeof(*clp); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 372 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 373 | dprint(FD_NET, "server: %d command line args\n", clp->lines); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 374 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 375 | for (i = 0; i < clp->lines; i++) { |
| 376 | cslp = pdu + offset; |
| 377 | argv[i] = (char *) cslp->text; |
| 378 | |
| 379 | offset += sizeof(*cslp) + le16_to_cpu(cslp->len); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 380 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 381 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 382 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 383 | if (parse_cmd_line(clp->lines, argv)) { |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 384 | fio_server_send_quit_cmd(); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 385 | free(argv); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 386 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 387 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 388 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 389 | free(argv); |
| 390 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 391 | 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] | 392 | |
Jens Axboe | 2e1df07 | 2012-02-09 11:15:02 +0100 | [diff] [blame] | 393 | ret = fio_backend(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 394 | fio_server_send_quit_cmd(); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 395 | reset_fio_state(); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 396 | return ret; |
| 397 | } |
| 398 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 399 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 400 | { |
| 401 | struct cmd_probe_pdu probe; |
| 402 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 403 | dprint(FD_NET, "server: sending probe reply\n"); |
| 404 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 405 | memset(&probe, 0, sizeof(probe)); |
| 406 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 407 | #ifdef FIO_BIG_ENDIAN |
| 408 | probe.bigendian = 1; |
| 409 | #endif |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 410 | probe.fio_major = FIO_MAJOR; |
| 411 | probe.fio_minor = FIO_MINOR; |
| 412 | probe.fio_patch = FIO_PATCH; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 413 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 414 | probe.os = FIO_OS; |
| 415 | probe.arch = FIO_ARCH; |
| 416 | |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 417 | probe.bpp = sizeof(void *); |
| 418 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 419 | 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] | 420 | } |
| 421 | |
| 422 | static int handle_send_eta_cmd(struct fio_net_cmd *cmd) |
| 423 | { |
| 424 | struct jobs_eta *je; |
| 425 | size_t size; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 426 | int i; |
| 427 | |
Jens Axboe | b814fb2 | 2011-10-12 09:20:34 +0200 | [diff] [blame] | 428 | if (!thread_number) |
| 429 | return 0; |
| 430 | |
| 431 | size = sizeof(*je) + thread_number * sizeof(char) + 1; |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 432 | je = malloc(size); |
| 433 | memset(je, 0, size); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 434 | |
| 435 | if (!calc_thread_status(je, 1)) { |
| 436 | free(je); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | dprint(FD_NET, "server sending status\n"); |
| 441 | |
| 442 | je->nr_running = cpu_to_le32(je->nr_running); |
| 443 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 444 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 445 | je->files_open = cpu_to_le32(je->files_open); |
| 446 | je->m_rate = cpu_to_le32(je->m_rate); |
| 447 | je->t_rate = cpu_to_le32(je->t_rate); |
| 448 | je->m_iops = cpu_to_le32(je->m_iops); |
| 449 | je->t_iops = cpu_to_le32(je->t_iops); |
| 450 | |
| 451 | for (i = 0; i < 2; i++) { |
| 452 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 453 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 454 | } |
| 455 | |
| 456 | je->elapsed_sec = cpu_to_le32(je->nr_running); |
| 457 | je->eta_sec = cpu_to_le64(je->eta_sec); |
| 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) |
| 1191 | WSADATA wsd; |
| 1192 | WSAStartup(MAKEWORD(2,2), &wsd); |
| 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 | } |