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 | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame^] | 279 | buf[pdu->buf_len] = '\0'; |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 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 | 8c95307 | 2012-03-20 14:35:35 +0100 | [diff] [blame] | 395 | return fio_net_send_simple_cmd(sk, 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); |
Jens Axboe | 8c95307 | 2012-03-20 14:35:35 +0100 | [diff] [blame] | 406 | return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 470 | static void fio_server_check_fork_items(struct flist_head *list) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 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 | { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 487 | fio_server_check_fork_items(&job_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void fio_server_check_conns(void) |
| 491 | { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 492 | fio_server_check_fork_items(&conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 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 | 3c3ed07 | 2012-03-27 09:12:39 +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 { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 691 | int timeout = 1000; |
| 692 | |
| 693 | if (!flist_empty(&job_list)) |
| 694 | timeout = 100; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame^] | 695 | |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 696 | ret = poll(&pfd, 1, timeout); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 697 | if (ret < 0) { |
| 698 | if (errno == EINTR) |
| 699 | break; |
| 700 | log_err("fio: poll: %s\n", strerror(errno)); |
| 701 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 702 | } else if (!ret) { |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 703 | fio_server_check_jobs(); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 704 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 705 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 706 | |
| 707 | if (pfd.revents & POLLIN) |
| 708 | break; |
| 709 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 710 | ret = 1; |
| 711 | break; |
| 712 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 713 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 714 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 715 | fio_server_check_jobs(); |
| 716 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 717 | if (ret < 0) |
| 718 | break; |
| 719 | |
| 720 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 721 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 722 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 726 | ret = handle_command(cmd); |
| 727 | if (ret) |
| 728 | break; |
| 729 | |
| 730 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 731 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | if (cmd) |
| 735 | free(cmd); |
| 736 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 737 | close(sk); |
| 738 | _exit(ret); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 741 | static int accept_loop(int listen_sk) |
| 742 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 743 | struct sockaddr_in addr; |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 744 | fio_socklen_t len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 745 | struct pollfd pfd; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 746 | int ret = 0, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 747 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 748 | dprint(FD_NET, "server enter accept loop\n"); |
| 749 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 750 | flags = fcntl(listen_sk, F_GETFL); |
| 751 | flags |= O_NONBLOCK; |
| 752 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 753 | |
| 754 | while (!exit_backend) { |
| 755 | pid_t pid; |
| 756 | |
| 757 | pfd.fd = listen_sk; |
| 758 | pfd.events = POLLIN; |
| 759 | do { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 760 | int timeout = 1000; |
| 761 | |
| 762 | if (!flist_empty(&conn_list)) |
| 763 | timeout = 100; |
| 764 | |
| 765 | ret = poll(&pfd, 1, timeout); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 766 | if (ret < 0) { |
| 767 | if (errno == EINTR) |
| 768 | break; |
| 769 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 770 | break; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 771 | } else if (!ret) { |
| 772 | fio_server_check_conns(); |
| 773 | continue; |
| 774 | } |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 775 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 776 | if (pfd.revents & POLLIN) |
| 777 | break; |
| 778 | } while (!exit_backend); |
| 779 | |
| 780 | fio_server_check_conns(); |
| 781 | |
| 782 | if (exit_backend || ret < 0) |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 783 | break; |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 784 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 785 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
| 786 | if (sk < 0) { |
| 787 | log_err("fio: accept: %s\n", strerror(errno)); |
| 788 | return -1; |
| 789 | } |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 790 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 791 | dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr)); |
| 792 | |
| 793 | pid = fork(); |
| 794 | if (pid) { |
| 795 | close(sk); |
| 796 | fio_server_add_conn_pid(pid); |
| 797 | continue; |
| 798 | } |
| 799 | |
| 800 | /* exits */ |
| 801 | handle_connection(sk); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 802 | } |
| 803 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 804 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 805 | } |
| 806 | |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 807 | 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] | 808 | { |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 809 | struct cmd_text_pdu *pdu; |
| 810 | unsigned int tlen; |
| 811 | struct timeval tv; |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 812 | |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 813 | if (server_fd == -1) |
| 814 | return log_local_buf(buf, len); |
| 815 | |
| 816 | tlen = sizeof(*pdu) + len; |
| 817 | pdu = malloc(tlen); |
| 818 | |
| 819 | pdu->level = __cpu_to_le32(level); |
| 820 | pdu->buf_len = __cpu_to_le32(len); |
| 821 | |
| 822 | gettimeofday(&tv, NULL); |
| 823 | pdu->log_sec = __cpu_to_le64(tv.tv_sec); |
| 824 | pdu->log_usec = __cpu_to_le64(tv.tv_usec); |
| 825 | |
| 826 | memcpy(pdu->buf, buf, len); |
| 827 | |
| 828 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, 0); |
| 829 | free(pdu); |
| 830 | return len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 831 | } |
| 832 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 833 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 834 | { |
| 835 | dst->max_val = cpu_to_le64(src->max_val); |
| 836 | dst->min_val = cpu_to_le64(src->min_val); |
| 837 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * Encode to IEEE 754 for network transfer |
| 841 | */ |
| 842 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 843 | 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] | 844 | } |
| 845 | |
| 846 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 847 | { |
| 848 | int i; |
| 849 | |
| 850 | for (i = 0; i < 2; i++) { |
| 851 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 852 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 853 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 854 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 855 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 856 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 857 | } |
| 858 | |
| 859 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 860 | dst->groupid = cpu_to_le32(src->groupid); |
| 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 865 | * into a single payload. |
| 866 | */ |
| 867 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 868 | { |
| 869 | struct cmd_ts_pdu p; |
| 870 | int i, j; |
| 871 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 872 | dprint(FD_NET, "server sending end stats\n"); |
| 873 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 874 | memset(&p, 0, sizeof(p)); |
| 875 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 876 | strcpy(p.ts.name, ts->name); |
| 877 | strcpy(p.ts.verror, ts->verror); |
| 878 | strcpy(p.ts.description, ts->description); |
| 879 | |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 880 | p.ts.error = cpu_to_le32(ts->error); |
| 881 | p.ts.thread_number = cpu_to_le32(ts->thread_number); |
| 882 | p.ts.groupid = cpu_to_le32(ts->groupid); |
| 883 | p.ts.pid = cpu_to_le32(ts->pid); |
| 884 | p.ts.members = cpu_to_le32(ts->members); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 885 | |
| 886 | for (i = 0; i < 2; i++) { |
| 887 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 888 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 889 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 890 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 891 | } |
| 892 | |
| 893 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 894 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 895 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 896 | p.ts.minf = cpu_to_le64(ts->minf); |
| 897 | p.ts.majf = cpu_to_le64(ts->majf); |
| 898 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 899 | |
| 900 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 901 | fio_fp64_t *src = &ts->percentile_list[i]; |
| 902 | fio_fp64_t *dst = &p.ts.percentile_list[i]; |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 903 | |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 904 | 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] | 905 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 906 | |
| 907 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 908 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 909 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 910 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 911 | } |
| 912 | |
| 913 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 914 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 915 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 916 | } |
| 917 | |
| 918 | for (i = 0; i < 2; i++) |
| 919 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 920 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 921 | |
| 922 | for (i = 0; i < 3; i++) { |
| 923 | 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] | 924 | 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] | 925 | } |
| 926 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 927 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 928 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 929 | |
| 930 | for (i = 0; i < 2; i++) { |
| 931 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 932 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 933 | } |
| 934 | |
| 935 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 936 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 937 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 938 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 939 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 940 | |
| 941 | convert_gs(&p.rs, rs); |
| 942 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 943 | 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] | 944 | } |
| 945 | |
| 946 | void fio_server_send_gs(struct group_run_stats *rs) |
| 947 | { |
| 948 | struct group_run_stats gs; |
| 949 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 950 | dprint(FD_NET, "server sending group run stats\n"); |
| 951 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 952 | convert_gs(&gs, rs); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 953 | 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] | 954 | } |
| 955 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 956 | static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src) |
| 957 | { |
| 958 | int i; |
| 959 | |
| 960 | for (i = 0; i < 2; i++) { |
| 961 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 962 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 963 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 964 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 965 | } |
| 966 | |
| 967 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 968 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 969 | dst->slavecount = cpu_to_le32(src->slavecount); |
| 970 | dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f)); |
| 971 | } |
| 972 | |
| 973 | static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src) |
| 974 | { |
| 975 | int i; |
| 976 | |
| 977 | strcpy((char *) dst->name, (char *) src->name); |
| 978 | |
| 979 | for (i = 0; i < 2; i++) { |
| 980 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 981 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 982 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 983 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 984 | } |
| 985 | |
| 986 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 987 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 988 | dst->msec = cpu_to_le64(src->msec); |
| 989 | } |
| 990 | |
| 991 | void fio_server_send_du(void) |
| 992 | { |
| 993 | struct disk_util *du; |
| 994 | struct flist_head *entry; |
| 995 | struct cmd_du_pdu pdu; |
| 996 | |
| 997 | dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list)); |
| 998 | |
Jens Axboe | 0766d92 | 2011-10-13 12:02:08 +0200 | [diff] [blame] | 999 | memset(&pdu, 0, sizeof(pdu)); |
| 1000 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1001 | flist_for_each(entry, &disk_list) { |
| 1002 | du = flist_entry(entry, struct disk_util, list); |
| 1003 | |
| 1004 | convert_dus(&pdu.dus, &du->dus); |
| 1005 | convert_agg(&pdu.agg, &du->agg); |
| 1006 | |
| 1007 | fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0); |
| 1008 | } |
| 1009 | } |
| 1010 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1011 | /* |
| 1012 | * Send a command with a separate PDU, not inlined in the command |
| 1013 | */ |
| 1014 | static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf, |
| 1015 | off_t size, uint64_t tag, uint32_t flags) |
| 1016 | { |
| 1017 | struct fio_net_cmd cmd; |
| 1018 | struct iovec iov[2]; |
| 1019 | |
| 1020 | iov[0].iov_base = &cmd; |
| 1021 | iov[0].iov_len = sizeof(cmd); |
| 1022 | iov[1].iov_base = (void *) buf; |
| 1023 | iov[1].iov_len = size; |
| 1024 | |
| 1025 | __fio_init_net_cmd(&cmd, opcode, size, tag); |
| 1026 | cmd.flags = __cpu_to_le32(flags); |
| 1027 | fio_net_cmd_crc_pdu(&cmd, buf); |
| 1028 | |
Jens Axboe | 8c95307 | 2012-03-20 14:35:35 +0100 | [diff] [blame] | 1029 | return fio_sendv_data(sk, iov, 2); |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1030 | } |
| 1031 | |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1032 | int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name) |
| 1033 | { |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1034 | struct cmd_iolog_pdu pdu; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1035 | z_stream stream; |
| 1036 | void *out_pdu; |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1037 | int i, ret = 0; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1038 | |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 1039 | pdu.thread_number = cpu_to_le32(td->thread_number); |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1040 | pdu.nr_samples = __cpu_to_le32(log->nr_samples); |
| 1041 | pdu.log_type = cpu_to_le32(log->log_type); |
| 1042 | strcpy((char *) pdu.name, name); |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1043 | |
| 1044 | for (i = 0; i < log->nr_samples; i++) { |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1045 | struct io_sample *s = &log->log[i]; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1046 | |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1047 | s->time = cpu_to_le64(s->time); |
| 1048 | s->val = cpu_to_le64(s->val); |
| 1049 | s->ddir = cpu_to_le32(s->ddir); |
| 1050 | s->bs = cpu_to_le32(s->bs); |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * Dirty - since the log is potentially huge, compress it into |
| 1055 | * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving |
| 1056 | * side defragment it. |
| 1057 | */ |
| 1058 | out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU); |
| 1059 | |
| 1060 | stream.zalloc = Z_NULL; |
| 1061 | stream.zfree = Z_NULL; |
| 1062 | stream.opaque = Z_NULL; |
| 1063 | |
| 1064 | if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) { |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1065 | ret = 1; |
| 1066 | goto err; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /* |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1070 | * Send header first, it's not compressed. |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1071 | */ |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1072 | ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu, |
| 1073 | sizeof(pdu), 0, FIO_NET_CMD_F_MORE); |
| 1074 | if (ret) |
| 1075 | goto err_zlib; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1076 | |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1077 | stream.next_in = (void *) log->log; |
| 1078 | stream.avail_in = log->nr_samples * sizeof(struct io_sample); |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1079 | |
| 1080 | do { |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1081 | unsigned int this_len, flags = 0; |
| 1082 | int ret; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1083 | |
| 1084 | stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU; |
| 1085 | stream.next_out = out_pdu; |
Jens Axboe | 3c547fe | 2012-03-14 21:53:00 +0100 | [diff] [blame] | 1086 | ret = deflate(&stream, Z_FINISH); |
| 1087 | /* may be Z_OK, or Z_STREAM_END */ |
| 1088 | if (ret < 0) |
| 1089 | goto err_zlib; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1090 | |
| 1091 | this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out; |
| 1092 | |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1093 | if (stream.avail_in) |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1094 | flags = FIO_NET_CMD_F_MORE; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1095 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1096 | ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, |
| 1097 | out_pdu, this_len, 0, flags); |
| 1098 | if (ret) |
| 1099 | goto err_zlib; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1100 | } while (stream.avail_in); |
| 1101 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1102 | err_zlib: |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1103 | deflateEnd(&stream); |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1104 | err: |
| 1105 | free(out_pdu); |
| 1106 | return ret; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 1109 | void fio_server_send_add_job(struct thread_data *td) |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1110 | { |
| 1111 | struct cmd_add_job_pdu pdu; |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1112 | |
Jens Axboe | 731e30a | 2012-03-14 16:35:37 +0100 | [diff] [blame] | 1113 | memset(&pdu, 0, sizeof(pdu)); |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 1114 | pdu.thread_number = cpu_to_le32(td->thread_number); |
| 1115 | pdu.groupid = cpu_to_le32(td->groupid); |
| 1116 | convert_thread_options_to_net(&pdu.top, &td->o); |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1117 | |
| 1118 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), 0); |
| 1119 | } |
| 1120 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 1121 | void fio_server_send_start(struct thread_data *td) |
| 1122 | { |
| 1123 | assert(server_fd != -1); |
| 1124 | |
| 1125 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL); |
| 1126 | } |
| 1127 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1128 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1129 | { |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1130 | struct sockaddr *addr; |
| 1131 | fio_socklen_t socklen; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1132 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1133 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1134 | if (use_ipv6) |
| 1135 | sk = socket(AF_INET6, SOCK_STREAM, 0); |
| 1136 | else |
| 1137 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 1138 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1139 | if (sk < 0) { |
| 1140 | log_err("fio: socket: %s\n", strerror(errno)); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | |
| 1144 | opt = 1; |
| 1145 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 1146 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1147 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1148 | return -1; |
| 1149 | } |
| 1150 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 1151 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1152 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1153 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1154 | return -1; |
| 1155 | } |
| 1156 | #endif |
| 1157 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1158 | if (use_ipv6) { |
| 1159 | addr = (struct sockaddr *) &saddr_in6; |
| 1160 | socklen = sizeof(saddr_in6); |
| 1161 | saddr_in6.sin6_family = AF_INET6; |
| 1162 | } else { |
| 1163 | addr = (struct sockaddr *) &saddr_in; |
| 1164 | socklen = sizeof(saddr_in); |
| 1165 | saddr_in.sin_family = AF_INET; |
| 1166 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1167 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1168 | if (bind(sk, addr, socklen) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1169 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1170 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1171 | return -1; |
| 1172 | } |
| 1173 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1174 | return sk; |
| 1175 | } |
| 1176 | |
| 1177 | static int fio_init_server_sock(void) |
| 1178 | { |
| 1179 | struct sockaddr_un addr; |
| 1180 | fio_socklen_t len; |
| 1181 | mode_t mode; |
| 1182 | int sk; |
| 1183 | |
| 1184 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1185 | if (sk < 0) { |
| 1186 | log_err("fio: socket: %s\n", strerror(errno)); |
| 1187 | return -1; |
| 1188 | } |
| 1189 | |
| 1190 | mode = umask(000); |
| 1191 | |
| 1192 | memset(&addr, 0, sizeof(addr)); |
| 1193 | addr.sun_family = AF_UNIX; |
| 1194 | strcpy(addr.sun_path, bind_sock); |
| 1195 | unlink(bind_sock); |
| 1196 | |
| 1197 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 1198 | |
| 1199 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 1200 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1201 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1202 | return -1; |
| 1203 | } |
| 1204 | |
| 1205 | umask(mode); |
| 1206 | return sk; |
| 1207 | } |
| 1208 | |
| 1209 | static int fio_init_server_connection(void) |
| 1210 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1211 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1212 | int sk; |
| 1213 | |
| 1214 | dprint(FD_NET, "starting server\n"); |
| 1215 | |
| 1216 | if (!bind_sock) |
| 1217 | sk = fio_init_server_ip(); |
| 1218 | else |
| 1219 | sk = fio_init_server_sock(); |
| 1220 | |
| 1221 | if (sk < 0) |
| 1222 | return sk; |
| 1223 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1224 | if (!bind_sock) { |
| 1225 | char *p, port[16]; |
| 1226 | const void *src; |
| 1227 | int af; |
| 1228 | |
| 1229 | if (use_ipv6) { |
| 1230 | af = AF_INET6; |
| 1231 | src = &saddr_in6.sin6_addr; |
| 1232 | } else { |
| 1233 | af = AF_INET; |
| 1234 | src = &saddr_in.sin_addr; |
| 1235 | } |
| 1236 | |
| 1237 | p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str)); |
| 1238 | |
| 1239 | sprintf(port, ",%u", fio_net_port); |
| 1240 | if (p) |
| 1241 | strcat(p, port); |
| 1242 | else |
| 1243 | strcpy(bind_str, port); |
| 1244 | } else |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1245 | strcpy(bind_str, bind_sock); |
| 1246 | |
| 1247 | log_info("fio: server listening on %s\n", bind_str); |
| 1248 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1249 | if (listen(sk, 0) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1250 | log_err("fio: listen: %s\n", strerror(errno)); |
| 1251 | return -1; |
| 1252 | } |
| 1253 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1254 | return sk; |
| 1255 | } |
| 1256 | |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1257 | int fio_server_parse_host(const char *host, int *ipv6, struct in_addr *inp, |
| 1258 | struct in6_addr *inp6) |
| 1259 | |
| 1260 | { |
| 1261 | int ret = 0; |
| 1262 | |
| 1263 | if (*ipv6) |
| 1264 | ret = inet_pton(AF_INET6, host, inp6); |
| 1265 | else |
| 1266 | ret = inet_pton(AF_INET, host, inp); |
| 1267 | |
| 1268 | if (ret != 1) { |
| 1269 | struct hostent *hent; |
| 1270 | |
| 1271 | hent = gethostbyname(host); |
| 1272 | if (!hent) { |
| 1273 | log_err("fio: failed to resolve <%s>\n", host); |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | if (*ipv6) { |
| 1278 | if (hent->h_addrtype != AF_INET6) { |
| 1279 | log_info("fio: falling back to IPv4\n"); |
| 1280 | *ipv6 = 0; |
| 1281 | } else |
| 1282 | memcpy(inp6, hent->h_addr_list[0], 16); |
| 1283 | } |
| 1284 | if (!*ipv6) { |
| 1285 | if (hent->h_addrtype != AF_INET) { |
| 1286 | log_err("fio: lookup type mismatch\n"); |
| 1287 | return 0; |
| 1288 | } |
| 1289 | memcpy(inp, hent->h_addr_list[0], 4); |
| 1290 | } |
| 1291 | ret = 1; |
| 1292 | } |
| 1293 | |
| 1294 | return !(ret == 1); |
| 1295 | } |
| 1296 | |
Jens Axboe | 660a2bf | 2011-10-24 09:35:06 +0200 | [diff] [blame] | 1297 | /* |
| 1298 | * Parse a host/ip/port string. Reads from 'str'. |
| 1299 | * |
| 1300 | * Outputs: |
| 1301 | * |
| 1302 | * For IPv4: |
| 1303 | * *ptr is the host, *port is the port, inp is the destination. |
| 1304 | * For IPv6: |
| 1305 | * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1. |
| 1306 | * For local domain sockets: |
| 1307 | * *ptr is the filename, *is_sock is 1. |
| 1308 | */ |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1309 | 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] | 1310 | int *port, struct in_addr *inp, |
| 1311 | struct in6_addr *inp6, int *ipv6) |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1312 | { |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1313 | const char *host = str; |
| 1314 | char *portp; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1315 | int lport = 0; |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1316 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1317 | *ptr = NULL; |
| 1318 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1319 | *port = fio_net_port; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1320 | *ipv6 = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1321 | |
| 1322 | if (!strncmp(str, "sock:", 5)) { |
| 1323 | *ptr = strdup(str + 5); |
| 1324 | *is_sock = 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1325 | |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | /* |
| 1330 | * Is it ip:<ip or host>:port |
| 1331 | */ |
| 1332 | if (!strncmp(host, "ip:", 3)) |
| 1333 | host += 3; |
| 1334 | else if (!strncmp(host, "ip4:", 4)) |
| 1335 | host += 4; |
| 1336 | else if (!strncmp(host, "ip6:", 4)) { |
| 1337 | host += 4; |
| 1338 | *ipv6 = 1; |
| 1339 | } else if (host[0] == ':') { |
| 1340 | /* String is :port */ |
| 1341 | host++; |
| 1342 | lport = atoi(host); |
| 1343 | if (!lport || lport > 65535) { |
| 1344 | log_err("fio: bad server port %u\n", port); |
| 1345 | return 1; |
| 1346 | } |
| 1347 | /* no hostname given, we are done */ |
| 1348 | *port = lport; |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * If no port seen yet, check if there's a last ':' at the end |
| 1354 | */ |
| 1355 | if (!lport) { |
| 1356 | portp = strchr(host, ','); |
| 1357 | if (portp) { |
| 1358 | *portp = '\0'; |
| 1359 | portp++; |
| 1360 | lport = atoi(portp); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1361 | if (!lport || lport > 65535) { |
| 1362 | log_err("fio: bad server port %u\n", port); |
| 1363 | return 1; |
| 1364 | } |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | if (lport) |
| 1369 | *port = lport; |
| 1370 | |
| 1371 | if (!strlen(host)) |
| 1372 | return 0; |
| 1373 | |
| 1374 | *ptr = strdup(host); |
| 1375 | |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1376 | if (fio_server_parse_host(*ptr, ipv6, inp, inp6)) { |
| 1377 | free(*ptr); |
| 1378 | *ptr = NULL; |
| 1379 | return 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | if (*port == 0) |
| 1383 | *port = fio_net_port; |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1388 | /* |
| 1389 | * Server arg should be one of: |
| 1390 | * |
| 1391 | * sock:/path/to/socket |
| 1392 | * ip:1.2.3.4 |
| 1393 | * 1.2.3.4 |
| 1394 | * |
| 1395 | * Where sock uses unix domain sockets, and ip binds the server to |
| 1396 | * a specific interface. If no arguments are given to the server, it |
| 1397 | * uses IP and binds to 0.0.0.0. |
| 1398 | * |
| 1399 | */ |
| 1400 | static int fio_handle_server_arg(void) |
| 1401 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1402 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1403 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1404 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1405 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 1406 | |
| 1407 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1408 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1409 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1410 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1411 | &port, &saddr_in.sin_addr, |
| 1412 | &saddr_in6.sin6_addr, &use_ipv6); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1413 | |
| 1414 | if (!is_sock && bind_sock) { |
| 1415 | free(bind_sock); |
| 1416 | bind_sock = NULL; |
| 1417 | } |
| 1418 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1419 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1420 | fio_net_port = port; |
| 1421 | saddr_in.sin_port = htons(port); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1422 | saddr_in6.sin6_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1423 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | static int fio_server(void) |
| 1427 | { |
| 1428 | int sk, ret; |
| 1429 | |
| 1430 | dprint(FD_NET, "starting server\n"); |
| 1431 | |
| 1432 | if (fio_handle_server_arg()) |
| 1433 | return -1; |
| 1434 | |
| 1435 | sk = fio_init_server_connection(); |
| 1436 | if (sk < 0) |
| 1437 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1438 | |
| 1439 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1440 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1441 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1442 | |
| 1443 | if (fio_server_arg) { |
| 1444 | free(fio_server_arg); |
| 1445 | fio_server_arg = NULL; |
| 1446 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1447 | if (bind_sock) |
| 1448 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1449 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1450 | return ret; |
| 1451 | } |
| 1452 | |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1453 | void fio_server_got_signal(int signal) |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1454 | { |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1455 | if (signal == SIGPIPE) |
| 1456 | server_fd = -1; |
| 1457 | else { |
| 1458 | log_info("\nfio: terminating on signal %d\n", signal); |
| 1459 | exit_backend = 1; |
| 1460 | } |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1463 | static int check_existing_pidfile(const char *pidfile) |
| 1464 | { |
| 1465 | struct stat sb; |
| 1466 | char buf[16]; |
| 1467 | pid_t pid; |
| 1468 | FILE *f; |
| 1469 | |
| 1470 | if (stat(pidfile, &sb)) |
| 1471 | return 0; |
| 1472 | |
| 1473 | f = fopen(pidfile, "r"); |
| 1474 | if (!f) |
| 1475 | return 0; |
| 1476 | |
Jens Axboe | bfc3b17 | 2011-10-10 21:16:55 +0200 | [diff] [blame] | 1477 | if (fread(buf, sb.st_size, 1, f) <= 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1478 | fclose(f); |
| 1479 | return 1; |
| 1480 | } |
| 1481 | fclose(f); |
| 1482 | |
| 1483 | pid = atoi(buf); |
| 1484 | if (kill(pid, SIGCONT) < 0) |
Jens Axboe | ea5aa1b | 2011-10-11 11:45:35 +0200 | [diff] [blame] | 1485 | return errno != ESRCH; |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1486 | |
| 1487 | return 1; |
| 1488 | } |
| 1489 | |
| 1490 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1491 | { |
| 1492 | FILE *fpid; |
| 1493 | |
| 1494 | fpid = fopen(pidfile, "w"); |
| 1495 | if (!fpid) { |
| 1496 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1497 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1501 | fclose(fpid); |
| 1502 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /* |
| 1506 | * If pidfile is specified, background us. |
| 1507 | */ |
| 1508 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1509 | { |
| 1510 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1511 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1512 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1513 | #if defined(WIN32) |
Jens Axboe | 905c78b | 2012-03-06 19:34:22 +0100 | [diff] [blame] | 1514 | WSADATA wsd; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame^] | 1515 | WSAStartup(MAKEWORD(2, 2), &wsd); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1516 | #endif |
| 1517 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1518 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1519 | return fio_server(); |
| 1520 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1521 | if (check_existing_pidfile(pidfile)) { |
| 1522 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 1523 | pidfile); |
| 1524 | return -1; |
| 1525 | } |
| 1526 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1527 | pid = fork(); |
| 1528 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1529 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1530 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 1531 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1532 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1533 | int ret = write_pid(pid, pidfile); |
| 1534 | |
| 1535 | exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1536 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1537 | |
| 1538 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1539 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1540 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1541 | close(STDIN_FILENO); |
| 1542 | close(STDOUT_FILENO); |
| 1543 | close(STDERR_FILENO); |
| 1544 | f_out = NULL; |
| 1545 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1546 | |
| 1547 | ret = fio_server(); |
| 1548 | |
| 1549 | closelog(); |
| 1550 | unlink(pidfile); |
| 1551 | free(pidfile); |
| 1552 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1553 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1554 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1555 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1556 | { |
| 1557 | fio_server_arg = strdup(arg); |
| 1558 | } |