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