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