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