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