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); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 101 | cmd->tag = le64_to_cpu(cmd->tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 102 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 103 | |
| 104 | switch (cmd->version) { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 105 | case FIO_SERVER_VER: |
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 | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 208 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, |
| 209 | uint64_t tag) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 210 | { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 211 | struct fio_net_cmd *cmd = NULL; |
| 212 | size_t this_len, cur_len = 0; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 213 | int ret; |
| 214 | |
| 215 | do { |
| 216 | this_len = size; |
| 217 | if (this_len > FIO_SERVER_MAX_PDU) |
| 218 | this_len = FIO_SERVER_MAX_PDU; |
| 219 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 220 | if (!cmd || cur_len < sizeof(*cmd) + this_len) { |
| 221 | if (cmd) |
| 222 | free(cmd); |
| 223 | |
| 224 | cur_len = sizeof(*cmd) + this_len; |
| 225 | cmd = malloc(cur_len); |
| 226 | } |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 227 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 228 | fio_init_net_cmd(cmd, opcode, buf, this_len, tag); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 229 | |
| 230 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 231 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 232 | |
| 233 | fio_net_cmd_crc(cmd); |
| 234 | |
| 235 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 236 | size -= this_len; |
| 237 | buf += this_len; |
| 238 | } while (!ret && size); |
| 239 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 240 | if (cmd) |
| 241 | free(cmd); |
| 242 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 243 | return ret; |
| 244 | } |
| 245 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 246 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 247 | { |
Jens Axboe | 178cde9 | 2011-10-05 22:14:31 +0200 | [diff] [blame] | 248 | struct fio_net_cmd cmd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 249 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 250 | fio_init_net_cmd(&cmd, opcode, NULL, 0, tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 251 | fio_net_cmd_crc(&cmd); |
| 252 | |
| 253 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 254 | } |
| 255 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 256 | static int fio_server_send_quit_cmd(void) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 257 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 258 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 259 | 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] | 260 | } |
| 261 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 262 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 263 | { |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 264 | char *buf = (char *) cmd->payload; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 265 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 266 | |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 267 | if (parse_jobs_ini(buf, 1, 0)) { |
| 268 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 269 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 270 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 271 | |
| 272 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0); |
| 273 | |
| 274 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 275 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 276 | reset_fio_state(); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 281 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 282 | void *pdu = cmd->payload; |
| 283 | struct cmd_single_line_pdu *cslp; |
| 284 | struct cmd_line_pdu *clp; |
| 285 | unsigned long offset; |
| 286 | char **argv; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 287 | int ret, i; |
| 288 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 289 | clp = pdu; |
| 290 | clp->lines = le16_to_cpu(clp->lines); |
| 291 | argv = malloc(clp->lines * sizeof(char *)); |
| 292 | offset = sizeof(*clp); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 293 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 294 | dprint(FD_NET, "server: %d command line args\n", clp->lines); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 295 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 296 | for (i = 0; i < clp->lines; i++) { |
| 297 | cslp = pdu + offset; |
| 298 | argv[i] = (char *) cslp->text; |
| 299 | |
| 300 | offset += sizeof(*cslp) + le16_to_cpu(cslp->len); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 301 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 302 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 303 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 304 | if (parse_cmd_line(clp->lines, argv)) { |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 305 | fio_server_send_quit_cmd(); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 306 | free(argv); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 307 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 308 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 309 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 310 | free(argv); |
| 311 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 312 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0); |
| 313 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 314 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 315 | fio_server_send_quit_cmd(); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 316 | reset_fio_state(); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 317 | return ret; |
| 318 | } |
| 319 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 320 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 321 | { |
| 322 | struct cmd_probe_pdu probe; |
| 323 | |
| 324 | memset(&probe, 0, sizeof(probe)); |
| 325 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 326 | #ifdef FIO_BIG_ENDIAN |
| 327 | probe.bigendian = 1; |
| 328 | #endif |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 329 | probe.fio_major = FIO_MAJOR; |
| 330 | probe.fio_minor = FIO_MINOR; |
| 331 | probe.fio_patch = FIO_PATCH; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 332 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 333 | probe.os = FIO_OS; |
| 334 | probe.arch = FIO_ARCH; |
| 335 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 336 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), 0); |
| 337 | } |
| 338 | |
| 339 | static int handle_send_eta_cmd(struct fio_net_cmd *cmd) |
| 340 | { |
| 341 | struct jobs_eta *je; |
| 342 | size_t size; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 343 | int i; |
| 344 | |
| 345 | size = sizeof(*je) + thread_number * sizeof(char); |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 346 | je = malloc(size); |
| 347 | memset(je, 0, size); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 348 | |
| 349 | if (!calc_thread_status(je, 1)) { |
| 350 | free(je); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | dprint(FD_NET, "server sending status\n"); |
| 355 | |
| 356 | je->nr_running = cpu_to_le32(je->nr_running); |
| 357 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 358 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 359 | je->files_open = cpu_to_le32(je->files_open); |
| 360 | je->m_rate = cpu_to_le32(je->m_rate); |
| 361 | je->t_rate = cpu_to_le32(je->t_rate); |
| 362 | je->m_iops = cpu_to_le32(je->m_iops); |
| 363 | je->t_iops = cpu_to_le32(je->t_iops); |
| 364 | |
| 365 | for (i = 0; i < 2; i++) { |
| 366 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 367 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 368 | } |
| 369 | |
| 370 | je->elapsed_sec = cpu_to_le32(je->nr_running); |
| 371 | je->eta_sec = cpu_to_le64(je->eta_sec); |
| 372 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 373 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 374 | free(je); |
| 375 | return 0; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 378 | static int handle_command(struct fio_net_cmd *cmd) |
| 379 | { |
| 380 | int ret; |
| 381 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 382 | dprint(FD_NET, "server: got opcode %d, pdu=%u\n", cmd->opcode, cmd->pdu_len); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 383 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 384 | switch (cmd->opcode) { |
| 385 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 386 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 387 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 388 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 389 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 390 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 391 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 392 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 393 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 394 | case FIO_NET_CMD_JOBLINE: |
| 395 | ret = handle_jobline_cmd(cmd); |
| 396 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 397 | case FIO_NET_CMD_PROBE: |
| 398 | ret = handle_probe_cmd(cmd); |
| 399 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 400 | case FIO_NET_CMD_SEND_ETA: |
| 401 | ret = handle_send_eta_cmd(cmd); |
| 402 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 403 | default: |
| 404 | log_err("fio: unknown opcode: %d\n", cmd->opcode); |
| 405 | ret = 1; |
| 406 | } |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 411 | static int handle_connection(int sk, int block) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 412 | { |
| 413 | struct fio_net_cmd *cmd = NULL; |
| 414 | int ret = 0; |
| 415 | |
| 416 | /* read forever */ |
| 417 | while (!exit_backend) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 418 | struct pollfd pfd = { |
| 419 | .fd = sk, |
| 420 | .events = POLLIN, |
| 421 | }; |
| 422 | |
| 423 | ret = 0; |
| 424 | do { |
| 425 | ret = poll(&pfd, 1, 100); |
| 426 | if (ret < 0) { |
| 427 | if (errno == EINTR) |
| 428 | break; |
| 429 | log_err("fio: poll: %s\n", strerror(errno)); |
| 430 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 431 | } else if (!ret) { |
| 432 | if (!block) |
| 433 | return 0; |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 434 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 435 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 436 | |
| 437 | if (pfd.revents & POLLIN) |
| 438 | break; |
| 439 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 440 | ret = 1; |
| 441 | break; |
| 442 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 443 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 444 | |
| 445 | if (ret < 0) |
| 446 | break; |
| 447 | |
| 448 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 449 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 450 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 454 | ret = handle_command(cmd); |
| 455 | if (ret) |
| 456 | break; |
| 457 | |
| 458 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 459 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (cmd) |
| 463 | free(cmd); |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 468 | void fio_server_idle_loop(void) |
| 469 | { |
| 470 | if (server_fd != -1) |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 471 | handle_connection(server_fd, 0); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 474 | static int accept_loop(int listen_sk) |
| 475 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 476 | struct sockaddr_in addr; |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 477 | fio_socklen_t len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 478 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 479 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 480 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 481 | dprint(FD_NET, "server enter accept loop\n"); |
| 482 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 483 | flags = fcntl(listen_sk, F_GETFL); |
| 484 | flags |= O_NONBLOCK; |
| 485 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 486 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 487 | pfd.fd = listen_sk; |
| 488 | pfd.events = POLLIN; |
| 489 | do { |
| 490 | ret = poll(&pfd, 1, 100); |
| 491 | if (ret < 0) { |
| 492 | if (errno == EINTR) |
| 493 | break; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 494 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 495 | goto out; |
| 496 | } else if (!ret) |
| 497 | continue; |
| 498 | |
| 499 | if (pfd.revents & POLLIN) |
| 500 | break; |
| 501 | } while (!exit_backend); |
| 502 | |
| 503 | if (exit_backend) |
| 504 | goto out; |
| 505 | |
Jens Axboe | 6b976bf | 2011-10-04 15:07:43 +0200 | [diff] [blame] | 506 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 507 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 508 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 509 | return -1; |
| 510 | } |
| 511 | |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 512 | 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] | 513 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 514 | server_fd = sk; |
| 515 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 516 | exitval = handle_connection(sk, 1); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 517 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 518 | server_fd = -1; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 519 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 520 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 521 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 522 | goto again; |
| 523 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 524 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 525 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 526 | } |
| 527 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 528 | int fio_server_text_output(const char *buf, size_t len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 529 | { |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 530 | if (server_fd != -1) |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 531 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0); |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 532 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 533 | return log_local_buf(buf, len); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 534 | } |
| 535 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 536 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 537 | { |
| 538 | dst->max_val = cpu_to_le64(src->max_val); |
| 539 | dst->min_val = cpu_to_le64(src->min_val); |
| 540 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * Encode to IEEE 754 for network transfer |
| 544 | */ |
| 545 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 546 | 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] | 547 | } |
| 548 | |
| 549 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 550 | { |
| 551 | int i; |
| 552 | |
| 553 | for (i = 0; i < 2; i++) { |
| 554 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 555 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 556 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 557 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 558 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 559 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 560 | } |
| 561 | |
| 562 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 563 | dst->groupid = cpu_to_le32(src->groupid); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 568 | * into a single payload. |
| 569 | */ |
| 570 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 571 | { |
| 572 | struct cmd_ts_pdu p; |
| 573 | int i, j; |
| 574 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 575 | dprint(FD_NET, "server sending end stats\n"); |
| 576 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 577 | memset(&p, 0, sizeof(p)); |
| 578 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 579 | strcpy(p.ts.name, ts->name); |
| 580 | strcpy(p.ts.verror, ts->verror); |
| 581 | strcpy(p.ts.description, ts->description); |
| 582 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 583 | p.ts.error = cpu_to_le32(ts->error); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 584 | p.ts.groupid = cpu_to_le32(ts->groupid); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 585 | p.ts.pid = cpu_to_le32(ts->pid); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 586 | p.ts.members = cpu_to_le32(ts->members); |
| 587 | |
| 588 | for (i = 0; i < 2; i++) { |
| 589 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 590 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 591 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 592 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 593 | } |
| 594 | |
| 595 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 596 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 597 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 598 | p.ts.minf = cpu_to_le64(ts->minf); |
| 599 | p.ts.majf = cpu_to_le64(ts->majf); |
| 600 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 601 | |
| 602 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
| 603 | fio_fp64_t *fp = &p.ts.percentile_list[i]; |
| 604 | |
| 605 | fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f)); |
| 606 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 607 | |
| 608 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 609 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 610 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 611 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 612 | } |
| 613 | |
| 614 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 615 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 616 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 617 | } |
| 618 | |
| 619 | for (i = 0; i < 2; i++) |
| 620 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 621 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 622 | |
| 623 | for (i = 0; i < 3; i++) { |
| 624 | 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] | 625 | 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] | 626 | } |
| 627 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 628 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 629 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 630 | |
| 631 | for (i = 0; i < 2; i++) { |
| 632 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 633 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 634 | } |
| 635 | |
| 636 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 637 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 638 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 639 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 640 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 641 | |
| 642 | convert_gs(&p.rs, rs); |
| 643 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 644 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void fio_server_send_gs(struct group_run_stats *rs) |
| 648 | { |
| 649 | struct group_run_stats gs; |
| 650 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 651 | dprint(FD_NET, "server sending group run stats\n"); |
| 652 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 653 | convert_gs(&gs, rs); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 654 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 657 | int fio_server_log(const char *format, ...) |
| 658 | { |
| 659 | char buffer[1024]; |
| 660 | va_list args; |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 661 | size_t len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 662 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 663 | dprint(FD_NET, "server log\n"); |
| 664 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 665 | va_start(args, format); |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 666 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 667 | va_end(args); |
| 668 | |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 669 | return fio_server_text_output(buffer, len); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 670 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 671 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 672 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 673 | { |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 674 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 675 | |
| 676 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 677 | if (sk < 0) { |
| 678 | log_err("fio: socket: %s\n", strerror(errno)); |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | opt = 1; |
| 683 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 684 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 685 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 686 | return -1; |
| 687 | } |
| 688 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 689 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 690 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 691 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 692 | return -1; |
| 693 | } |
| 694 | #endif |
| 695 | |
| 696 | saddr_in.sin_family = AF_INET; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 697 | |
| 698 | if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { |
| 699 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 700 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 701 | return -1; |
| 702 | } |
| 703 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 704 | return sk; |
| 705 | } |
| 706 | |
| 707 | static int fio_init_server_sock(void) |
| 708 | { |
| 709 | struct sockaddr_un addr; |
| 710 | fio_socklen_t len; |
| 711 | mode_t mode; |
| 712 | int sk; |
| 713 | |
| 714 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 715 | if (sk < 0) { |
| 716 | log_err("fio: socket: %s\n", strerror(errno)); |
| 717 | return -1; |
| 718 | } |
| 719 | |
| 720 | mode = umask(000); |
| 721 | |
| 722 | memset(&addr, 0, sizeof(addr)); |
| 723 | addr.sun_family = AF_UNIX; |
| 724 | strcpy(addr.sun_path, bind_sock); |
| 725 | unlink(bind_sock); |
| 726 | |
| 727 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 728 | |
| 729 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 730 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 731 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 732 | return -1; |
| 733 | } |
| 734 | |
| 735 | umask(mode); |
| 736 | return sk; |
| 737 | } |
| 738 | |
| 739 | static int fio_init_server_connection(void) |
| 740 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 741 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 742 | int sk; |
| 743 | |
| 744 | dprint(FD_NET, "starting server\n"); |
| 745 | |
| 746 | if (!bind_sock) |
| 747 | sk = fio_init_server_ip(); |
| 748 | else |
| 749 | sk = fio_init_server_sock(); |
| 750 | |
| 751 | if (sk < 0) |
| 752 | return sk; |
| 753 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 754 | if (!bind_sock) |
| 755 | sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port); |
| 756 | else |
| 757 | strcpy(bind_str, bind_sock); |
| 758 | |
| 759 | log_info("fio: server listening on %s\n", bind_str); |
| 760 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 761 | if (listen(sk, 1) < 0) { |
| 762 | log_err("fio: listen: %s\n", strerror(errno)); |
| 763 | return -1; |
| 764 | } |
| 765 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 766 | return sk; |
| 767 | } |
| 768 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 769 | int fio_server_parse_string(const char *str, char **ptr, int *is_sock, |
| 770 | int *port, struct in_addr *inp) |
| 771 | { |
| 772 | *ptr = NULL; |
| 773 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 774 | *port = fio_net_port; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 775 | |
| 776 | if (!strncmp(str, "sock:", 5)) { |
| 777 | *ptr = strdup(str + 5); |
| 778 | *is_sock = 1; |
| 779 | } else { |
| 780 | const char *host = str; |
| 781 | char *portp; |
| 782 | int lport = 0; |
| 783 | |
| 784 | /* |
| 785 | * Is it ip:<ip or host>:port |
| 786 | */ |
| 787 | if (!strncmp(host, "ip:", 3)) |
| 788 | host += 3; |
| 789 | else if (host[0] == ':') { |
| 790 | /* String is :port */ |
| 791 | host++; |
| 792 | lport = atoi(host); |
| 793 | if (!lport || lport > 65535) { |
| 794 | log_err("fio: bad server port %u\n", port); |
| 795 | return 1; |
| 796 | } |
| 797 | /* no hostname given, we are done */ |
| 798 | *port = lport; |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * If no port seen yet, check if there's a last ':' at the end |
| 804 | */ |
| 805 | if (!lport) { |
| 806 | portp = strchr(host, ':'); |
| 807 | if (portp) { |
| 808 | *portp = '\0'; |
| 809 | portp++; |
| 810 | lport = atoi(portp); |
| 811 | if (!lport || lport > 65535) { |
| 812 | log_err("fio: bad server port %u\n", port); |
| 813 | return 1; |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | if (lport) |
| 819 | *port = lport; |
| 820 | |
| 821 | *ptr = strdup(host); |
| 822 | |
| 823 | if (inet_aton(host, inp) != 1) { |
| 824 | struct hostent *hent; |
| 825 | |
| 826 | hent = gethostbyname(host); |
| 827 | if (!hent) { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 828 | free(*ptr); |
| 829 | *ptr = NULL; |
| 830 | return 1; |
| 831 | } |
| 832 | |
| 833 | memcpy(inp, hent->h_addr, 4); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | if (*port == 0) |
| 838 | *port = fio_net_port; |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 843 | /* |
| 844 | * Server arg should be one of: |
| 845 | * |
| 846 | * sock:/path/to/socket |
| 847 | * ip:1.2.3.4 |
| 848 | * 1.2.3.4 |
| 849 | * |
| 850 | * Where sock uses unix domain sockets, and ip binds the server to |
| 851 | * a specific interface. If no arguments are given to the server, it |
| 852 | * uses IP and binds to 0.0.0.0. |
| 853 | * |
| 854 | */ |
| 855 | static int fio_handle_server_arg(void) |
| 856 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 857 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 858 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 859 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 860 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 861 | |
| 862 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 863 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 864 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 865 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 866 | &port, &saddr_in.sin_addr); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 867 | |
| 868 | if (!is_sock && bind_sock) { |
| 869 | free(bind_sock); |
| 870 | bind_sock = NULL; |
| 871 | } |
| 872 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 873 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 874 | fio_net_port = port; |
| 875 | saddr_in.sin_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 876 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | static int fio_server(void) |
| 880 | { |
| 881 | int sk, ret; |
| 882 | |
| 883 | dprint(FD_NET, "starting server\n"); |
| 884 | |
| 885 | if (fio_handle_server_arg()) |
| 886 | return -1; |
| 887 | |
| 888 | sk = fio_init_server_connection(); |
| 889 | if (sk < 0) |
| 890 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 891 | |
| 892 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 893 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 894 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 895 | |
| 896 | if (fio_server_arg) { |
| 897 | free(fio_server_arg); |
| 898 | fio_server_arg = NULL; |
| 899 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 900 | if (bind_sock) |
| 901 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 902 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 903 | return ret; |
| 904 | } |
| 905 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 906 | static void sig_int(int sig) |
| 907 | { |
| 908 | fio_terminate_threads(TERMINATE_ALL); |
| 909 | exit_backend = 1; |
| 910 | } |
| 911 | |
| 912 | static void server_signal_handler(void) |
| 913 | { |
| 914 | struct sigaction act; |
| 915 | |
| 916 | memset(&act, 0, sizeof(act)); |
| 917 | act.sa_handler = sig_int; |
| 918 | act.sa_flags = SA_RESTART; |
| 919 | sigaction(SIGINT, &act, NULL); |
| 920 | |
| 921 | memset(&act, 0, sizeof(act)); |
| 922 | act.sa_handler = sig_int; |
| 923 | act.sa_flags = SA_RESTART; |
| 924 | sigaction(SIGTERM, &act, NULL); |
| 925 | } |
| 926 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 927 | static int check_existing_pidfile(const char *pidfile) |
| 928 | { |
| 929 | struct stat sb; |
| 930 | char buf[16]; |
| 931 | pid_t pid; |
| 932 | FILE *f; |
| 933 | |
| 934 | if (stat(pidfile, &sb)) |
| 935 | return 0; |
| 936 | |
| 937 | f = fopen(pidfile, "r"); |
| 938 | if (!f) |
| 939 | return 0; |
| 940 | |
| 941 | if (fread(buf, sb.st_size, 1, f) < 0) { |
| 942 | fclose(f); |
| 943 | return 1; |
| 944 | } |
| 945 | fclose(f); |
| 946 | |
| 947 | pid = atoi(buf); |
| 948 | if (kill(pid, SIGCONT) < 0) |
| 949 | return 0; |
| 950 | |
| 951 | return 1; |
| 952 | } |
| 953 | |
| 954 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 955 | { |
| 956 | FILE *fpid; |
| 957 | |
| 958 | fpid = fopen(pidfile, "w"); |
| 959 | if (!fpid) { |
| 960 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 961 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 965 | fclose(fpid); |
| 966 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* |
| 970 | * If pidfile is specified, background us. |
| 971 | */ |
| 972 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 973 | { |
| 974 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 975 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 976 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 977 | server_signal_handler(); |
| 978 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 979 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 980 | return fio_server(); |
| 981 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 982 | if (check_existing_pidfile(pidfile)) { |
| 983 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 984 | pidfile); |
| 985 | return -1; |
| 986 | } |
| 987 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 988 | pid = fork(); |
| 989 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 990 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 991 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 992 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 993 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 994 | int ret = write_pid(pid, pidfile); |
| 995 | |
| 996 | exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 997 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 998 | |
| 999 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1000 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1001 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1002 | close(STDIN_FILENO); |
| 1003 | close(STDOUT_FILENO); |
| 1004 | close(STDERR_FILENO); |
| 1005 | f_out = NULL; |
| 1006 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1007 | |
| 1008 | ret = fio_server(); |
| 1009 | |
| 1010 | closelog(); |
| 1011 | unlink(pidfile); |
| 1012 | free(pidfile); |
| 1013 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1014 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1015 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1016 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1017 | { |
| 1018 | fio_server_arg = strdup(arg); |
| 1019 | } |