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 | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 11 | #include <sys/mman.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 12 | #include <netinet/in.h> |
| 13 | #include <arpa/inet.h> |
| 14 | #include <netdb.h> |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 15 | #include <syslog.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 16 | |
| 17 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 18 | #include "server.h" |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 19 | #include "crc/crc16.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 20 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 21 | int fio_net_port = 8765; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 22 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 23 | int exit_backend = 0; |
| 24 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 25 | static int server_fd = -1; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 26 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 27 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 28 | { |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 29 | assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU); |
| 30 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 31 | do { |
| 32 | int ret = send(sk, p, len, 0); |
| 33 | |
| 34 | if (ret > 0) { |
| 35 | len -= ret; |
| 36 | if (!len) |
| 37 | break; |
| 38 | p += ret; |
| 39 | continue; |
| 40 | } else if (!ret) |
| 41 | break; |
| 42 | else if (errno == EAGAIN || errno == EINTR) |
| 43 | continue; |
| 44 | } while (!exit_backend); |
| 45 | |
| 46 | if (!len) |
| 47 | return 0; |
| 48 | |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 53 | { |
| 54 | do { |
| 55 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 56 | |
| 57 | if (ret > 0) { |
| 58 | len -= ret; |
| 59 | if (!len) |
| 60 | break; |
| 61 | p += ret; |
| 62 | continue; |
| 63 | } else if (!ret) |
| 64 | break; |
| 65 | else if (errno == EAGAIN || errno == EINTR) |
| 66 | continue; |
| 67 | } while (!exit_backend); |
| 68 | |
| 69 | if (!len) |
| 70 | return 0; |
| 71 | |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 76 | { |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 77 | uint16_t crc; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 78 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 79 | cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); |
| 80 | cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 81 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 82 | crc = crc16(cmd, FIO_NET_CMD_CRC_SZ); |
| 83 | if (crc != cmd->cmd_crc16) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 84 | 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] | 85 | cmd->cmd_crc16, crc); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | cmd->version = le16_to_cpu(cmd->version); |
| 90 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 91 | cmd->flags = le32_to_cpu(cmd->flags); |
| 92 | cmd->serial = le64_to_cpu(cmd->serial); |
| 93 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 94 | |
| 95 | switch (cmd->version) { |
| 96 | case FIO_SERVER_VER1: |
| 97 | break; |
| 98 | default: |
| 99 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | if (cmd->pdu_len > FIO_SERVER_MAX_PDU) { |
| 104 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Read (and defragment, if necessary) incoming commands |
| 113 | */ |
| 114 | struct fio_net_cmd *fio_net_recv_cmd(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 115 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 116 | struct fio_net_cmd cmd, *cmdret = NULL; |
| 117 | size_t cmd_size = 0, pdu_offset = 0; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 118 | uint16_t crc; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 119 | int ret, first = 1; |
| 120 | void *pdu = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 121 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 122 | do { |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 123 | struct pollfd pfd; |
| 124 | |
| 125 | pfd.fd = sk; |
| 126 | pfd.events = POLLIN; |
| 127 | ret = 0; |
| 128 | do { |
| 129 | ret = poll(&pfd, 1, 100); |
| 130 | if (ret < 0) { |
| 131 | log_err("fio: poll: %s\n", strerror(errno)); |
| 132 | break; |
| 133 | } else if (!ret) |
| 134 | continue; |
| 135 | |
| 136 | if (pfd.revents & POLLIN) |
| 137 | break; |
| 138 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 139 | ret = 1; |
| 140 | break; |
| 141 | } |
| 142 | } while (ret >= 0); |
| 143 | |
| 144 | if (ret < 0) |
| 145 | break; |
| 146 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 147 | ret = fio_recv_data(sk, &cmd, sizeof(cmd)); |
| 148 | if (ret) |
| 149 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 150 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 151 | /* We have a command, verify it and swap if need be */ |
| 152 | ret = verify_convert_cmd(&cmd); |
| 153 | if (ret) |
| 154 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 155 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 156 | if (first) |
| 157 | cmd_size = sizeof(cmd) + cmd.pdu_len; |
| 158 | else |
| 159 | cmd_size += cmd.pdu_len; |
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 | cmdret = realloc(cmdret, cmd_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 162 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 163 | if (first) |
| 164 | memcpy(cmdret, &cmd, sizeof(cmd)); |
| 165 | else |
| 166 | assert(cmdret->opcode == cmd.opcode); |
| 167 | |
| 168 | if (!cmd.pdu_len) |
| 169 | break; |
| 170 | |
| 171 | /* There's payload, get it */ |
| 172 | pdu = (void *) cmdret->payload + pdu_offset; |
| 173 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 174 | if (ret) |
| 175 | break; |
| 176 | |
| 177 | /* Verify payload crc */ |
| 178 | crc = crc16(pdu, cmd.pdu_len); |
| 179 | if (crc != cmd.pdu_crc16) { |
| 180 | log_err("fio: server bad crc on payload "); |
| 181 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 182 | ret = 1; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 187 | if (!first) |
| 188 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 189 | first = 0; |
| 190 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 191 | |
| 192 | if (ret) { |
| 193 | free(cmdret); |
| 194 | cmdret = NULL; |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 195 | } else if (cmdret) |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 196 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 197 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 198 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 202 | { |
| 203 | uint32_t pdu_len; |
| 204 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 205 | 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] | 206 | |
| 207 | pdu_len = le32_to_cpu(cmd->pdu_len); |
| 208 | if (pdu_len) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 209 | cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 210 | } |
| 211 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 212 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 213 | { |
| 214 | struct fio_net_cmd *cmd; |
| 215 | size_t this_len; |
| 216 | int ret; |
| 217 | |
| 218 | do { |
| 219 | this_len = size; |
| 220 | if (this_len > FIO_SERVER_MAX_PDU) |
| 221 | this_len = FIO_SERVER_MAX_PDU; |
| 222 | |
| 223 | cmd = malloc(sizeof(*cmd) + this_len); |
| 224 | |
| 225 | fio_init_net_cmd(cmd, opcode, buf, this_len); |
| 226 | |
| 227 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 228 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 229 | |
| 230 | fio_net_cmd_crc(cmd); |
| 231 | |
| 232 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
| 233 | free(cmd); |
| 234 | size -= this_len; |
| 235 | buf += this_len; |
| 236 | } while (!ret && size); |
| 237 | |
| 238 | return ret; |
| 239 | } |
| 240 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 241 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t serial) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 242 | { |
| 243 | struct fio_net_cmd cmd = { |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 244 | .version = __cpu_to_le16(FIO_SERVER_VER1), |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 245 | .opcode = cpu_to_le16(opcode), |
Jens Axboe | bdab444 | 2011-09-30 22:58:13 -0600 | [diff] [blame] | 246 | .serial = cpu_to_le64(serial), |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | fio_net_cmd_crc(&cmd); |
| 250 | |
| 251 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 252 | } |
| 253 | |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 254 | static int send_quit_command(void) |
| 255 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 256 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 257 | return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0); |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 258 | } |
| 259 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 260 | static int handle_cur_job(struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 261 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 262 | void *buf = cmd->payload; |
| 263 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 264 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 265 | parse_jobs_ini(buf, 1, 0); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 266 | ret = exec_run(); |
| 267 | send_quit_command(); |
| 268 | reset_fio_state(); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static int handle_command(struct fio_net_cmd *cmd) |
| 273 | { |
| 274 | int ret; |
| 275 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 276 | dprint(FD_NET, "server: got opcode %d\n", cmd->opcode); |
| 277 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 278 | switch (cmd->opcode) { |
| 279 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 280 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 281 | return 1; |
| 282 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 283 | exit_backend = 1; |
| 284 | return 1; |
| 285 | case FIO_NET_CMD_ACK: |
| 286 | return 0; |
| 287 | case FIO_NET_CMD_NAK: |
| 288 | return 1; |
| 289 | case FIO_NET_CMD_JOB: |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 290 | ret = handle_cur_job(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 291 | break; |
| 292 | default: |
| 293 | log_err("fio: unknown opcode: %d\n", cmd->opcode); |
| 294 | ret = 1; |
| 295 | } |
| 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
| 300 | static int handle_connection(int sk) |
| 301 | { |
| 302 | struct fio_net_cmd *cmd = NULL; |
| 303 | int ret = 0; |
| 304 | |
| 305 | /* read forever */ |
| 306 | while (!exit_backend) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 307 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 308 | if (!cmd) { |
| 309 | ret = 1; |
| 310 | break; |
| 311 | } |
| 312 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 313 | ret = handle_command(cmd); |
| 314 | if (ret) |
| 315 | break; |
| 316 | |
| 317 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 318 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | if (cmd) |
| 322 | free(cmd); |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 327 | void fio_server_idle_loop(void) |
| 328 | { |
| 329 | if (server_fd != -1) |
| 330 | handle_connection(server_fd); |
| 331 | } |
| 332 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 333 | static int accept_loop(int listen_sk) |
| 334 | { |
| 335 | struct sockaddr addr; |
| 336 | unsigned int len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 337 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 338 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 339 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 340 | flags = fcntl(listen_sk, F_GETFL); |
| 341 | flags |= O_NONBLOCK; |
| 342 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 343 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 344 | pfd.fd = listen_sk; |
| 345 | pfd.events = POLLIN; |
| 346 | do { |
| 347 | ret = poll(&pfd, 1, 100); |
| 348 | if (ret < 0) { |
| 349 | if (errno == EINTR) |
| 350 | break; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 351 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 352 | goto out; |
| 353 | } else if (!ret) |
| 354 | continue; |
| 355 | |
| 356 | if (pfd.revents & POLLIN) |
| 357 | break; |
| 358 | } while (!exit_backend); |
| 359 | |
| 360 | if (exit_backend) |
| 361 | goto out; |
| 362 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 363 | sk = accept(listen_sk, &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 364 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 365 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 366 | return -1; |
| 367 | } |
| 368 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 369 | dprint(FD_NET, "server got a connection\n"); |
| 370 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 371 | server_fd = sk; |
| 372 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 373 | printf("handle\n"); |
| 374 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 375 | exitval = handle_connection(sk); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 376 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame^] | 377 | printf("out, exit %d\n", exitval); |
| 378 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 379 | server_fd = -1; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 380 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 381 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 382 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 383 | goto again; |
| 384 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 385 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 386 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 387 | } |
| 388 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 389 | static int fio_server(void) |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 390 | { |
| 391 | struct sockaddr_in saddr_in; |
| 392 | struct sockaddr addr; |
| 393 | unsigned int len; |
Jens Axboe | afcf775 | 2011-09-29 18:28:46 -0600 | [diff] [blame] | 394 | int sk, opt, ret; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 395 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 396 | dprint(FD_NET, "starting server\n"); |
| 397 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 398 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 399 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 400 | log_err("fio: socket: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 401 | return -1; |
| 402 | } |
| 403 | |
| 404 | opt = 1; |
| 405 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 406 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 407 | return -1; |
| 408 | } |
| 409 | #ifdef SO_REUSEPORT |
| 410 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 411 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 412 | return 1; |
| 413 | } |
| 414 | #endif |
| 415 | |
| 416 | saddr_in.sin_family = AF_INET; |
| 417 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 418 | saddr_in.sin_port = htons(fio_net_port); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 419 | |
| 420 | if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 421 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | if (listen(sk, 1) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 426 | log_err("fio: listen: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | len = sizeof(addr); |
| 431 | if (getsockname(sk, &addr, &len) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 432 | log_err("fio: getsockname: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 433 | return -1; |
| 434 | } |
| 435 | |
Jens Axboe | afcf775 | 2011-09-29 18:28:46 -0600 | [diff] [blame] | 436 | ret = accept_loop(sk); |
| 437 | close(sk); |
| 438 | return ret; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 439 | } |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 440 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 441 | int fio_server_text_output(const char *buf, unsigned int len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 442 | { |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 443 | if (server_fd != -1) |
| 444 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len); |
| 445 | |
| 446 | return 0; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 447 | } |
| 448 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 449 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 450 | { |
| 451 | dst->max_val = cpu_to_le64(src->max_val); |
| 452 | dst->min_val = cpu_to_le64(src->min_val); |
| 453 | dst->samples = cpu_to_le64(src->samples); |
| 454 | /* FIXME */ |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 455 | dst->mean = __cpu_to_le64(src->mean); |
| 456 | dst->S = __cpu_to_le64(src->S); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 460 | { |
| 461 | int i; |
| 462 | |
| 463 | for (i = 0; i < 2; i++) { |
| 464 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 465 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 466 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 467 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 468 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 469 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 470 | } |
| 471 | |
| 472 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 473 | dst->groupid = cpu_to_le32(src->groupid); |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 478 | * into a single payload. |
| 479 | */ |
| 480 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 481 | { |
| 482 | struct cmd_ts_pdu p; |
| 483 | int i, j; |
| 484 | |
| 485 | strcpy(p.ts.name, ts->name); |
| 486 | strcpy(p.ts.verror, ts->verror); |
| 487 | strcpy(p.ts.description, ts->description); |
| 488 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 489 | p.ts.error = cpu_to_le32(ts->error); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 490 | p.ts.groupid = cpu_to_le32(ts->groupid); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 491 | p.ts.pid = cpu_to_le32(ts->pid); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 492 | p.ts.members = cpu_to_le32(ts->members); |
| 493 | |
| 494 | for (i = 0; i < 2; i++) { |
| 495 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 496 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 497 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 498 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 499 | } |
| 500 | |
| 501 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 502 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 503 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 504 | p.ts.minf = cpu_to_le64(ts->minf); |
| 505 | p.ts.majf = cpu_to_le64(ts->majf); |
| 506 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
| 507 | p.ts.percentile_list = NULL; |
| 508 | |
| 509 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 510 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 511 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 512 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 513 | } |
| 514 | |
| 515 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 516 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 517 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 518 | } |
| 519 | |
| 520 | for (i = 0; i < 2; i++) |
| 521 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 522 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 523 | |
| 524 | for (i = 0; i < 3; i++) { |
| 525 | p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]); |
| 526 | p.ts.short_io_u[i] = cpu_to_le64(ts->total_io_u[i]); |
| 527 | } |
| 528 | |
| 529 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
| 530 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 531 | |
| 532 | for (i = 0; i < 2; i++) { |
| 533 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 534 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 535 | } |
| 536 | |
| 537 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 538 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 539 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 540 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 541 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 542 | |
| 543 | convert_gs(&p.rs, rs); |
| 544 | |
| 545 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p)); |
| 546 | } |
| 547 | |
| 548 | void fio_server_send_gs(struct group_run_stats *rs) |
| 549 | { |
| 550 | struct group_run_stats gs; |
| 551 | |
| 552 | convert_gs(&gs, rs); |
| 553 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs)); |
| 554 | } |
| 555 | |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 556 | void fio_server_send_status(void) |
| 557 | { |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 558 | struct jobs_eta *je; |
| 559 | size_t size; |
| 560 | void *buf; |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 561 | int i; |
| 562 | |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 563 | size = sizeof(*je) + thread_number * sizeof(char); |
| 564 | buf = malloc(size); |
| 565 | memset(buf, 0, size); |
| 566 | je = buf; |
| 567 | |
| 568 | if (!calc_thread_status(je)) { |
| 569 | free(je); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 570 | return; |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 573 | je->nr_running = cpu_to_le32(je->nr_running); |
| 574 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 575 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 576 | je->files_open = cpu_to_le32(je->files_open); |
| 577 | je->m_rate = cpu_to_le32(je->m_rate); |
| 578 | je->t_rate = cpu_to_le32(je->t_rate); |
| 579 | je->m_iops = cpu_to_le32(je->m_iops); |
| 580 | je->t_iops = cpu_to_le32(je->t_iops); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 581 | |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 582 | for (i = 0; i < 2; i++) { |
| 583 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 584 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 585 | } |
| 586 | |
| 587 | je->elapsed_sec = cpu_to_le32(je->nr_running); |
| 588 | je->eta_sec = cpu_to_le64(je->eta_sec); |
| 589 | |
| 590 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, buf, size); |
| 591 | free(je); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 594 | int fio_server_log(const char *format, ...) |
| 595 | { |
| 596 | char buffer[1024]; |
| 597 | va_list args; |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 598 | size_t len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 599 | |
| 600 | va_start(args, format); |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 601 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 602 | va_end(args); |
| 603 | |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 604 | return fio_server_text_output(buffer, len); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 605 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 606 | |
| 607 | int fio_start_server(int daemonize) |
| 608 | { |
| 609 | pid_t pid; |
| 610 | |
| 611 | if (!daemonize) |
| 612 | return fio_server(); |
| 613 | |
| 614 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 615 | pid = fork(); |
| 616 | if (pid < 0) { |
| 617 | syslog(LOG_ERR, "failed server fork"); |
| 618 | return 1; |
| 619 | } else if (pid) |
| 620 | exit(0); |
| 621 | |
| 622 | setsid(); |
| 623 | close(STDIN_FILENO); |
| 624 | close(STDOUT_FILENO); |
| 625 | close(STDERR_FILENO); |
| 626 | f_out = NULL; |
| 627 | f_err = NULL; |
| 628 | log_syslog = 1; |
| 629 | return fio_server(); |
| 630 | } |