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