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