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