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