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> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 7 | #include <sys/poll.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/wait.h> |
Jens Axboe | d05c4a0 | 2011-10-05 00:16:11 +0200 | [diff] [blame] | 10 | #include <sys/socket.h> |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 11 | #include <sys/stat.h> |
| 12 | #include <sys/un.h> |
Bruce Cran | 1f51bba | 2013-04-12 15:40:21 +0200 | [diff] [blame] | 13 | #include <sys/uio.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 | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_ZLIB |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 20 | #include <zlib.h> |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 21 | #endif |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 22 | |
| 23 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 24 | #include "server.h" |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 25 | #include "crc/crc16.h" |
Jens Axboe | c7c6cb4 | 2011-10-13 14:12:40 +0200 | [diff] [blame] | 26 | #include "lib/ieee754.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 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 | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 38 | #ifdef CONFIG_ZLIB |
| 39 | static unsigned int has_zlib = 1; |
| 40 | #else |
| 41 | static unsigned int has_zlib = 0; |
| 42 | #endif |
| 43 | static unsigned int use_zlib; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 44 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 45 | struct fio_fork_item { |
| 46 | struct flist_head list; |
| 47 | int exitval; |
| 48 | int signal; |
| 49 | int exited; |
| 50 | pid_t pid; |
| 51 | }; |
| 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 | 8d7b618 | 2014-04-14 09:07:30 -0600 | [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 | dfbf1f6 | 2014-04-14 11:43:47 -0600 | [diff] [blame] | 211 | struct fio_net_cmd cmd, *tmp, *cmdret = NULL; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 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 | dfbf1f6 | 2014-04-14 11:43:47 -0600 | [diff] [blame] | 234 | if (cmd_size / 1024 > FIO_SERVER_MAX_CMD_MB * 1024) { |
| 235 | log_err("fio: cmd+pdu too large (%llu)\n", (unsigned long long) cmd_size); |
| 236 | ret = 1; |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | tmp = realloc(cmdret, cmd_size); |
| 241 | if (!tmp) { |
| 242 | log_err("fio: server failed allocating cmd\n"); |
| 243 | ret = 1; |
| 244 | break; |
| 245 | } |
| 246 | cmdret = tmp; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 247 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 248 | if (first) |
| 249 | memcpy(cmdret, &cmd, sizeof(cmd)); |
Jens Axboe | 67f15dc | 2011-10-15 16:07:40 +0200 | [diff] [blame] | 250 | else if (cmdret->opcode != cmd.opcode) { |
| 251 | log_err("fio: fragment opcode mismatch (%d != %d)\n", |
| 252 | cmdret->opcode, cmd.opcode); |
| 253 | ret = 1; |
| 254 | break; |
| 255 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 256 | |
| 257 | if (!cmd.pdu_len) |
| 258 | break; |
| 259 | |
| 260 | /* There's payload, get it */ |
| 261 | pdu = (void *) cmdret->payload + pdu_offset; |
| 262 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 263 | if (ret) |
| 264 | break; |
| 265 | |
| 266 | /* Verify payload crc */ |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 267 | crc = fio_crc16(pdu, cmd.pdu_len); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 268 | if (crc != cmd.pdu_crc16) { |
| 269 | log_err("fio: server bad crc on payload "); |
| 270 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 271 | ret = 1; |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 276 | if (!first) |
| 277 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 278 | first = 0; |
| 279 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 280 | |
| 281 | if (ret) { |
| 282 | free(cmdret); |
| 283 | cmdret = NULL; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 284 | } else if (cmdret) { |
| 285 | /* zero-terminate text input */ |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 286 | if (cmdret->pdu_len) { |
| 287 | if (cmdret->opcode == FIO_NET_CMD_TEXT) { |
| 288 | struct cmd_text_pdu *pdu = (struct cmd_text_pdu *) cmdret->payload; |
| 289 | char *buf = (char *) pdu->buf; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 290 | |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 291 | buf[pdu->buf_len] = '\0'; |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 292 | } else if (cmdret->opcode == FIO_NET_CMD_JOB) { |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 293 | struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmdret->payload; |
| 294 | char *buf = (char *) pdu->buf; |
| 295 | int len = le32_to_cpu(pdu->buf_len); |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 296 | |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 297 | buf[len] = '\0'; |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 298 | } |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 299 | } |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 300 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 301 | /* frag flag is internal */ |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 302 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 303 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 304 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 305 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 306 | } |
| 307 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 308 | static void add_reply(uint64_t tag, struct flist_head *list) |
| 309 | { |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 310 | struct fio_net_cmd_reply *reply; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 311 | |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 312 | reply = (struct fio_net_cmd_reply *) (uintptr_t) tag; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 313 | flist_add_tail(&reply->list, list); |
| 314 | } |
| 315 | |
| 316 | static uint64_t alloc_reply(uint64_t tag, uint16_t opcode) |
| 317 | { |
| 318 | struct fio_net_cmd_reply *reply; |
| 319 | |
| 320 | reply = calloc(1, sizeof(*reply)); |
| 321 | INIT_FLIST_HEAD(&reply->list); |
| 322 | gettimeofday(&reply->tv, NULL); |
| 323 | reply->saved_tag = tag; |
| 324 | reply->opcode = opcode; |
| 325 | |
| 326 | return (uintptr_t) reply; |
| 327 | } |
| 328 | |
| 329 | static void free_reply(uint64_t tag) |
| 330 | { |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 331 | struct fio_net_cmd_reply *reply; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 332 | |
Aaron Carroll | a572bbf | 2013-04-12 07:55:02 +0200 | [diff] [blame] | 333 | reply = (struct fio_net_cmd_reply *) (uintptr_t) tag; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 334 | free(reply); |
| 335 | } |
| 336 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 337 | 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] | 338 | { |
| 339 | uint32_t pdu_len; |
| 340 | |
Jens Axboe | 25dfa84 | 2012-02-29 10:01:34 +0100 | [diff] [blame] | 341 | 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] | 342 | |
| 343 | pdu_len = le32_to_cpu(cmd->pdu_len); |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 344 | cmd->pdu_crc16 = __cpu_to_le16(fio_crc16(pdu, pdu_len)); |
| 345 | } |
| 346 | |
| 347 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 348 | { |
| 349 | fio_net_cmd_crc_pdu(cmd, cmd->payload); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 350 | } |
| 351 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 352 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 353 | uint64_t *tagptr, struct flist_head *list) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 354 | { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 355 | struct fio_net_cmd *cmd = NULL; |
| 356 | size_t this_len, cur_len = 0; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 357 | uint64_t tag; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 358 | int ret; |
| 359 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 360 | if (list) { |
| 361 | assert(tagptr); |
| 362 | tag = *tagptr = alloc_reply(*tagptr, opcode); |
| 363 | } else |
| 364 | tag = tagptr ? *tagptr : 0; |
| 365 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 366 | do { |
| 367 | this_len = size; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 368 | if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU) |
| 369 | this_len = FIO_SERVER_MAX_FRAGMENT_PDU; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 370 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 371 | if (!cmd || cur_len < sizeof(*cmd) + this_len) { |
| 372 | if (cmd) |
| 373 | free(cmd); |
| 374 | |
| 375 | cur_len = sizeof(*cmd) + this_len; |
| 376 | cmd = malloc(cur_len); |
| 377 | } |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 378 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 379 | fio_init_net_cmd(cmd, opcode, buf, this_len, tag); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 380 | |
| 381 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 382 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 383 | |
| 384 | fio_net_cmd_crc(cmd); |
| 385 | |
| 386 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 387 | size -= this_len; |
| 388 | buf += this_len; |
| 389 | } while (!ret && size); |
| 390 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 391 | if (list) { |
| 392 | if (ret) |
| 393 | free_reply(tag); |
| 394 | else |
| 395 | add_reply(tag, list); |
| 396 | } |
| 397 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 398 | if (cmd) |
| 399 | free(cmd); |
| 400 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 401 | return ret; |
| 402 | } |
| 403 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 404 | 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] | 405 | { |
Jens Axboe | 178cde9 | 2011-10-05 22:14:31 +0200 | [diff] [blame] | 406 | struct fio_net_cmd cmd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 407 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 408 | fio_init_net_cmd(&cmd, opcode, NULL, 0, tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 409 | fio_net_cmd_crc(&cmd); |
| 410 | |
| 411 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 412 | } |
| 413 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 414 | /* |
| 415 | * If 'list' is non-NULL, then allocate and store the sent command for |
| 416 | * later verification. |
| 417 | */ |
| 418 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag, |
| 419 | struct flist_head *list) |
| 420 | { |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 421 | int ret; |
| 422 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 423 | if (list) |
| 424 | tag = alloc_reply(tag, opcode); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 425 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 426 | ret = fio_net_send_simple_stack_cmd(sk, opcode, tag); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 427 | if (ret) { |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 428 | if (list) |
| 429 | free_reply(tag); |
| 430 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 431 | return ret; |
| 432 | } |
| 433 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 434 | if (list) |
| 435 | add_reply(tag, list); |
| 436 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 440 | int fio_net_send_quit(int sk) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 441 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 442 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 443 | |
Jens Axboe | 8c95307 | 2012-03-20 14:35:35 +0100 | [diff] [blame] | 444 | 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] | 445 | } |
| 446 | |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 447 | static int fio_net_send_ack(int sk, struct fio_net_cmd *cmd, int error, |
| 448 | int signal) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 449 | { |
| 450 | struct cmd_end_pdu epdu; |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 451 | uint64_t tag = 0; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 452 | |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 453 | if (cmd) |
| 454 | tag = cmd->tag; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 455 | |
| 456 | epdu.error = __cpu_to_le32(error); |
| 457 | epdu.signal = __cpu_to_le32(signal); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 458 | return fio_net_send_cmd(sk, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), &tag, NULL); |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | int fio_net_send_stop(int sk, int error, int signal) |
| 462 | { |
| 463 | dprint(FD_NET, "server: sending stop (%d, %d)\n", error, signal); |
| 464 | return fio_net_send_ack(sk, NULL, error, signal); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | static void fio_server_add_fork_item(pid_t pid, struct flist_head *list) |
| 468 | { |
| 469 | struct fio_fork_item *ffi; |
| 470 | |
| 471 | ffi = malloc(sizeof(*ffi)); |
| 472 | ffi->exitval = 0; |
| 473 | ffi->signal = 0; |
| 474 | ffi->exited = 0; |
| 475 | ffi->pid = pid; |
| 476 | flist_add_tail(&ffi->list, list); |
| 477 | } |
| 478 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 479 | static void fio_server_add_conn_pid(struct flist_head *conn_list, pid_t pid) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 480 | { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 481 | dprint(FD_NET, "server: forked off connection job (pid=%u)\n", (int) pid); |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 482 | fio_server_add_fork_item(pid, conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 485 | static void fio_server_add_job_pid(struct flist_head *job_list, pid_t pid) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 486 | { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 487 | dprint(FD_NET, "server: forked off job job (pid=%u)\n", (int) pid); |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 488 | fio_server_add_fork_item(pid, job_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static void fio_server_check_fork_item(struct fio_fork_item *ffi) |
| 492 | { |
| 493 | int ret, status; |
| 494 | |
| 495 | ret = waitpid(ffi->pid, &status, WNOHANG); |
| 496 | if (ret < 0) { |
| 497 | if (errno == ECHILD) { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 498 | log_err("fio: connection pid %u disappeared\n", (int) ffi->pid); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 499 | ffi->exited = 1; |
| 500 | } else |
| 501 | log_err("fio: waitpid: %s\n", strerror(errno)); |
| 502 | } else if (ret == ffi->pid) { |
| 503 | if (WIFSIGNALED(status)) { |
| 504 | ffi->signal = WTERMSIG(status); |
| 505 | ffi->exited = 1; |
| 506 | } |
| 507 | if (WIFEXITED(status)) { |
| 508 | if (WEXITSTATUS(status)) |
| 509 | ffi->exitval = WEXITSTATUS(status); |
| 510 | ffi->exited = 1; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | static void fio_server_fork_item_done(struct fio_fork_item *ffi) |
| 516 | { |
Jens Axboe | a7533db | 2013-11-06 11:19:42 -0700 | [diff] [blame] | 517 | dprint(FD_NET, "pid %u exited, sig=%u, exitval=%d\n", (int) ffi->pid, ffi->signal, ffi->exitval); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 518 | |
| 519 | /* |
| 520 | * Fold STOP and QUIT... |
| 521 | */ |
| 522 | fio_net_send_stop(server_fd, ffi->exitval, ffi->signal); |
| 523 | fio_net_send_quit(server_fd); |
| 524 | flist_del(&ffi->list); |
| 525 | free(ffi); |
| 526 | } |
| 527 | |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 528 | static void fio_server_check_fork_items(struct flist_head *list) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 529 | { |
| 530 | struct flist_head *entry, *tmp; |
| 531 | struct fio_fork_item *ffi; |
| 532 | |
| 533 | flist_for_each_safe(entry, tmp, list) { |
| 534 | ffi = flist_entry(entry, struct fio_fork_item, list); |
| 535 | |
| 536 | fio_server_check_fork_item(ffi); |
| 537 | |
| 538 | if (ffi->exited) |
| 539 | fio_server_fork_item_done(ffi); |
| 540 | } |
| 541 | } |
| 542 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 543 | static void fio_server_check_jobs(struct flist_head *job_list) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 544 | { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 545 | fio_server_check_fork_items(job_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 548 | static void fio_server_check_conns(struct flist_head *conn_list) |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 549 | { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 550 | fio_server_check_fork_items(conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 551 | } |
| 552 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 553 | static int handle_run_cmd(struct flist_head *job_list, struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 554 | { |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 555 | pid_t pid; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 556 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 557 | |
Jens Axboe | 386626f | 2014-06-09 13:25:13 -0600 | [diff] [blame] | 558 | fio_time_init(); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 559 | set_genesis_time(); |
| 560 | |
| 561 | pid = fork(); |
| 562 | if (pid) { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 563 | fio_server_add_job_pid(job_list, pid); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
Jens Axboe | 2e1df07 | 2012-02-09 11:15:02 +0100 | [diff] [blame] | 567 | ret = fio_backend(); |
Jens Axboe | 2bb3f0a | 2012-03-28 12:22:40 +0200 | [diff] [blame] | 568 | free_threads_shm(); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 569 | _exit(ret); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 572 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
| 573 | { |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 574 | struct cmd_job_pdu *pdu = (struct cmd_job_pdu *) cmd->payload; |
| 575 | void *buf = pdu->buf; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 576 | struct cmd_start_pdu spdu; |
| 577 | |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 578 | pdu->buf_len = le32_to_cpu(pdu->buf_len); |
| 579 | pdu->client_type = le32_to_cpu(pdu->client_type); |
| 580 | |
| 581 | if (parse_jobs_ini(buf, 1, 0, pdu->client_type)) { |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 582 | fio_net_send_quit(server_fd); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 583 | return -1; |
| 584 | } |
| 585 | |
| 586 | spdu.jobs = cpu_to_le32(thread_number); |
Jens Axboe | 108fea7 | 2012-11-14 13:09:45 -0700 | [diff] [blame] | 587 | spdu.stat_outputs = cpu_to_le32(stat_number); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 588 | fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 592 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 593 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 594 | void *pdu = cmd->payload; |
| 595 | struct cmd_single_line_pdu *cslp; |
| 596 | struct cmd_line_pdu *clp; |
| 597 | unsigned long offset; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 598 | struct cmd_start_pdu spdu; |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 599 | char **argv; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 600 | int i; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 601 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 602 | clp = pdu; |
| 603 | clp->lines = le16_to_cpu(clp->lines); |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 604 | clp->client_type = le16_to_cpu(clp->client_type); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 605 | argv = malloc(clp->lines * sizeof(char *)); |
| 606 | offset = sizeof(*clp); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 607 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 608 | dprint(FD_NET, "server: %d command line args\n", clp->lines); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 609 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 610 | for (i = 0; i < clp->lines; i++) { |
| 611 | cslp = pdu + offset; |
| 612 | argv[i] = (char *) cslp->text; |
| 613 | |
| 614 | offset += sizeof(*cslp) + le16_to_cpu(cslp->len); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 615 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 616 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 617 | |
Jens Axboe | 46bcd49 | 2012-03-14 11:31:21 +0100 | [diff] [blame] | 618 | if (parse_cmd_line(clp->lines, argv, clp->client_type)) { |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 619 | fio_net_send_quit(server_fd); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 620 | free(argv); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 621 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 622 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 623 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 624 | free(argv); |
| 625 | |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 626 | spdu.jobs = cpu_to_le32(thread_number); |
Jens Axboe | 1e5324e | 2012-11-14 14:25:31 -0700 | [diff] [blame] | 627 | spdu.stat_outputs = cpu_to_le32(stat_number); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 628 | fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), NULL, NULL); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 629 | return 0; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 630 | } |
| 631 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 632 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 633 | { |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 634 | struct cmd_client_probe_pdu *pdu = (struct cmd_client_probe_pdu *) cmd->payload; |
| 635 | struct cmd_probe_reply_pdu probe; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 636 | uint64_t tag = cmd->tag; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 637 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 638 | dprint(FD_NET, "server: sending probe reply\n"); |
| 639 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 640 | memset(&probe, 0, sizeof(probe)); |
| 641 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 0dcebdf | 2013-01-23 15:42:16 -0700 | [diff] [blame] | 642 | #ifdef CONFIG_BIG_ENDIAN |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 643 | probe.bigendian = 1; |
| 644 | #endif |
Jens Axboe | 750db47 | 2012-04-16 11:44:48 +0200 | [diff] [blame] | 645 | strncpy((char *) probe.fio_version, fio_version_string, sizeof(probe.fio_version)); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 646 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 647 | probe.os = FIO_OS; |
| 648 | probe.arch = FIO_ARCH; |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 649 | probe.bpp = sizeof(void *); |
Jens Axboe | d31e26d | 2012-03-28 21:38:24 +0200 | [diff] [blame] | 650 | probe.cpus = __cpu_to_le32(cpus_online()); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 651 | |
| 652 | /* |
| 653 | * If the client supports compression and we do too, then enable it |
| 654 | */ |
| 655 | if (has_zlib && le64_to_cpu(pdu->flags) & FIO_PROBE_FLAG_ZLIB) { |
| 656 | probe.flags = __cpu_to_le64(FIO_PROBE_FLAG_ZLIB); |
| 657 | use_zlib = 1; |
| 658 | } else { |
| 659 | probe.flags = 0; |
| 660 | use_zlib = 0; |
| 661 | } |
Jens Axboe | 38fdef2 | 2011-10-11 14:30:06 +0200 | [diff] [blame] | 662 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 663 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), &tag, NULL); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | static int handle_send_eta_cmd(struct fio_net_cmd *cmd) |
| 667 | { |
| 668 | struct jobs_eta *je; |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 669 | uint64_t tag = cmd->tag; |
Jens Axboe | 61f6cce | 2014-06-24 08:40:21 -0600 | [diff] [blame] | 670 | size_t size; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 671 | int i; |
| 672 | |
Jens Axboe | 61f6cce | 2014-06-24 08:40:21 -0600 | [diff] [blame] | 673 | je = get_jobs_eta(1, &size); |
| 674 | if (!je) |
Jens Axboe | b814fb2 | 2011-10-12 09:20:34 +0200 | [diff] [blame] | 675 | return 0; |
| 676 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 677 | dprint(FD_NET, "server sending status\n"); |
| 678 | |
| 679 | je->nr_running = cpu_to_le32(je->nr_running); |
| 680 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 681 | je->nr_pending = cpu_to_le32(je->nr_pending); |
Jens Axboe | 714e85f | 2013-04-15 10:21:56 +0200 | [diff] [blame] | 682 | je->nr_setting_up = cpu_to_le32(je->nr_setting_up); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 683 | je->files_open = cpu_to_le32(je->files_open); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 684 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 685 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | 3e47bd2 | 2012-02-29 13:45:02 +0100 | [diff] [blame] | 686 | je->m_rate[i] = cpu_to_le32(je->m_rate[i]); |
| 687 | je->t_rate[i] = cpu_to_le32(je->t_rate[i]); |
| 688 | je->m_iops[i] = cpu_to_le32(je->m_iops[i]); |
| 689 | je->t_iops[i] = cpu_to_le32(je->t_iops[i]); |
Jens Axboe | 16725bb | 2013-04-11 12:43:05 +0200 | [diff] [blame] | 690 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 691 | je->iops[i] = cpu_to_le32(je->iops[i]); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 692 | } |
| 693 | |
Jens Axboe | c1970b9 | 2012-03-06 15:37:40 +0100 | [diff] [blame] | 694 | je->elapsed_sec = cpu_to_le64(je->elapsed_sec); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 695 | je->eta_sec = cpu_to_le64(je->eta_sec); |
Jens Axboe | 8c621fb | 2012-03-08 12:30:48 +0100 | [diff] [blame] | 696 | je->nr_threads = cpu_to_le32(je->nr_threads); |
Jens Axboe | b7f05eb | 2012-05-11 20:33:02 +0200 | [diff] [blame] | 697 | je->is_pow2 = cpu_to_le32(je->is_pow2); |
Jens Axboe | ed2c0a1 | 2013-04-11 12:55:16 +0200 | [diff] [blame] | 698 | je->unit_base = cpu_to_le32(je->unit_base); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 699 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 700 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, &tag, NULL); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 701 | free(je); |
| 702 | return 0; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 703 | } |
| 704 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 705 | static int send_update_job_reply(int fd, uint64_t __tag, int error) |
| 706 | { |
| 707 | uint64_t tag = __tag; |
| 708 | uint32_t pdu_error; |
| 709 | |
| 710 | pdu_error = __cpu_to_le32(error); |
| 711 | return fio_net_send_cmd(fd, FIO_NET_CMD_UPDATE_JOB, &pdu_error, sizeof(pdu_error), &tag, NULL); |
| 712 | } |
| 713 | |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 714 | static int handle_update_job_cmd(struct fio_net_cmd *cmd) |
| 715 | { |
| 716 | struct cmd_add_job_pdu *pdu = (struct cmd_add_job_pdu *) cmd->payload; |
| 717 | struct thread_data *td; |
| 718 | uint32_t tnumber; |
| 719 | |
| 720 | tnumber = le32_to_cpu(pdu->thread_number); |
| 721 | |
| 722 | dprint(FD_NET, "server: updating options for job %u\n", tnumber); |
| 723 | |
Jens Axboe | 30ffacb | 2012-03-28 09:15:05 +0200 | [diff] [blame] | 724 | if (!tnumber || tnumber > thread_number) { |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 725 | send_update_job_reply(server_fd, cmd->tag, ENODEV); |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
Jens Axboe | 30ffacb | 2012-03-28 09:15:05 +0200 | [diff] [blame] | 729 | td = &threads[tnumber - 1]; |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 730 | convert_thread_options_to_cpu(&td->o, &pdu->top); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 731 | send_update_job_reply(server_fd, cmd->tag, 0); |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 732 | return 0; |
| 733 | } |
| 734 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 735 | static int handle_command(struct flist_head *job_list, struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 736 | { |
| 737 | int ret; |
| 738 | |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 739 | dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%llx\n", |
| 740 | fio_server_op(cmd->opcode), cmd->pdu_len, |
| 741 | (unsigned long long) cmd->tag); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 742 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 743 | switch (cmd->opcode) { |
| 744 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 745 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 746 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 747 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 748 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 749 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 750 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 751 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 752 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 753 | case FIO_NET_CMD_JOBLINE: |
| 754 | ret = handle_jobline_cmd(cmd); |
| 755 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 756 | case FIO_NET_CMD_PROBE: |
| 757 | ret = handle_probe_cmd(cmd); |
| 758 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 759 | case FIO_NET_CMD_SEND_ETA: |
| 760 | ret = handle_send_eta_cmd(cmd); |
| 761 | break; |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 762 | case FIO_NET_CMD_RUN: |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 763 | ret = handle_run_cmd(job_list, cmd); |
Jens Axboe | b9d2f30 | 2012-03-08 20:36:28 +0100 | [diff] [blame] | 764 | break; |
Jens Axboe | f58bd2a | 2012-03-27 10:06:42 +0200 | [diff] [blame] | 765 | case FIO_NET_CMD_UPDATE_JOB: |
| 766 | ret = handle_update_job_cmd(cmd); |
| 767 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 768 | default: |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 769 | log_err("fio: unknown opcode: %s\n", fio_server_op(cmd->opcode)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 770 | ret = 1; |
| 771 | } |
| 772 | |
| 773 | return ret; |
| 774 | } |
| 775 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 776 | static int handle_connection(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 777 | { |
| 778 | struct fio_net_cmd *cmd = NULL; |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 779 | FLIST_HEAD(job_list); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 780 | int ret = 0; |
| 781 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 782 | reset_fio_state(); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 783 | server_fd = sk; |
| 784 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 785 | /* read forever */ |
| 786 | while (!exit_backend) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 787 | struct pollfd pfd = { |
| 788 | .fd = sk, |
| 789 | .events = POLLIN, |
| 790 | }; |
| 791 | |
| 792 | ret = 0; |
| 793 | do { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 794 | int timeout = 1000; |
| 795 | |
| 796 | if (!flist_empty(&job_list)) |
| 797 | timeout = 100; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 798 | |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 799 | ret = poll(&pfd, 1, timeout); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 800 | if (ret < 0) { |
| 801 | if (errno == EINTR) |
| 802 | break; |
| 803 | log_err("fio: poll: %s\n", strerror(errno)); |
| 804 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 805 | } else if (!ret) { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 806 | fio_server_check_jobs(&job_list); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 807 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 808 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 809 | |
| 810 | if (pfd.revents & POLLIN) |
| 811 | break; |
| 812 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 813 | ret = 1; |
| 814 | break; |
| 815 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 816 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 817 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 818 | fio_server_check_jobs(&job_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 819 | |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 820 | if (ret < 0) |
| 821 | break; |
| 822 | |
| 823 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 824 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 825 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 826 | break; |
| 827 | } |
| 828 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 829 | ret = handle_command(&job_list, cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 830 | if (ret) |
| 831 | break; |
| 832 | |
| 833 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 834 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | if (cmd) |
| 838 | free(cmd); |
| 839 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 840 | close(sk); |
| 841 | _exit(ret); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 844 | static int accept_loop(int listen_sk) |
| 845 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 846 | struct sockaddr_in addr; |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 847 | struct sockaddr_in6 addr6; |
| 848 | socklen_t len = use_ipv6 ? sizeof(addr6) : sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 849 | struct pollfd pfd; |
Jens Axboe | 4a85161 | 2014-04-14 10:04:21 -0600 | [diff] [blame] | 850 | int ret = 0, sk, exitval = 0; |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 851 | FLIST_HEAD(conn_list); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 852 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 853 | dprint(FD_NET, "server enter accept loop\n"); |
| 854 | |
Jens Axboe | 4a85161 | 2014-04-14 10:04:21 -0600 | [diff] [blame] | 855 | fio_set_fd_nonblocking(listen_sk, "server"); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 856 | |
| 857 | while (!exit_backend) { |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 858 | const char *from; |
| 859 | char buf[64]; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 860 | pid_t pid; |
| 861 | |
| 862 | pfd.fd = listen_sk; |
| 863 | pfd.events = POLLIN; |
| 864 | do { |
Jens Axboe | 5416325 | 2012-03-23 12:25:18 +0100 | [diff] [blame] | 865 | int timeout = 1000; |
| 866 | |
| 867 | if (!flist_empty(&conn_list)) |
| 868 | timeout = 100; |
| 869 | |
| 870 | ret = poll(&pfd, 1, timeout); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 871 | if (ret < 0) { |
| 872 | if (errno == EINTR) |
| 873 | break; |
| 874 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 875 | break; |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 876 | } else if (!ret) { |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 877 | fio_server_check_conns(&conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 878 | continue; |
| 879 | } |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 880 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 881 | if (pfd.revents & POLLIN) |
| 882 | break; |
| 883 | } while (!exit_backend); |
| 884 | |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 885 | fio_server_check_conns(&conn_list); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 886 | |
| 887 | if (exit_backend || ret < 0) |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 888 | break; |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 889 | |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 890 | if (use_ipv6) |
| 891 | sk = accept(listen_sk, (struct sockaddr *) &addr6, &len); |
| 892 | else |
| 893 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
| 894 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 895 | if (sk < 0) { |
| 896 | log_err("fio: accept: %s\n", strerror(errno)); |
| 897 | return -1; |
| 898 | } |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 899 | |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 900 | if (use_ipv6) |
| 901 | from = inet_ntop(AF_INET6, (struct sockaddr *) &addr6.sin6_addr, buf, sizeof(buf)); |
| 902 | else |
| 903 | from = inet_ntop(AF_INET, (struct sockaddr *) &addr.sin_addr, buf, sizeof(buf)); |
| 904 | |
| 905 | dprint(FD_NET, "server: connect from %s\n", from); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 906 | |
| 907 | pid = fork(); |
| 908 | if (pid) { |
| 909 | close(sk); |
Jens Axboe | 6348b5d | 2013-11-07 13:37:09 -0700 | [diff] [blame] | 910 | fio_server_add_conn_pid(&conn_list, pid); |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 911 | continue; |
| 912 | } |
| 913 | |
| 914 | /* exits */ |
| 915 | handle_connection(sk); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 916 | } |
| 917 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 918 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 919 | } |
| 920 | |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 921 | 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] | 922 | { |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 923 | struct cmd_text_pdu *pdu; |
| 924 | unsigned int tlen; |
| 925 | struct timeval tv; |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 926 | |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 927 | if (server_fd == -1) |
| 928 | return log_local_buf(buf, len); |
| 929 | |
| 930 | tlen = sizeof(*pdu) + len; |
| 931 | pdu = malloc(tlen); |
| 932 | |
| 933 | pdu->level = __cpu_to_le32(level); |
| 934 | pdu->buf_len = __cpu_to_le32(len); |
| 935 | |
| 936 | gettimeofday(&tv, NULL); |
| 937 | pdu->log_sec = __cpu_to_le64(tv.tv_sec); |
| 938 | pdu->log_usec = __cpu_to_le64(tv.tv_usec); |
| 939 | |
| 940 | memcpy(pdu->buf, buf, len); |
| 941 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 942 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, pdu, tlen, NULL, NULL); |
Jens Axboe | 084d1c6 | 2012-03-03 20:28:07 +0100 | [diff] [blame] | 943 | free(pdu); |
| 944 | return len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 945 | } |
| 946 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 947 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 948 | { |
| 949 | dst->max_val = cpu_to_le64(src->max_val); |
| 950 | dst->min_val = cpu_to_le64(src->min_val); |
| 951 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 952 | |
| 953 | /* |
| 954 | * Encode to IEEE 754 for network transfer |
| 955 | */ |
| 956 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 957 | 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] | 958 | } |
| 959 | |
| 960 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 961 | { |
| 962 | int i; |
| 963 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 964 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 965 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 966 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 967 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 968 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 969 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 970 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 971 | } |
| 972 | |
| 973 | dst->kb_base = cpu_to_le32(src->kb_base); |
Steven Noonan | ad705bc | 2013-04-08 15:05:25 -0700 | [diff] [blame] | 974 | dst->unit_base = cpu_to_le32(src->unit_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 975 | dst->groupid = cpu_to_le32(src->groupid); |
Jens Axboe | 771e58b | 2013-01-30 12:56:23 +0100 | [diff] [blame] | 976 | dst->unified_rw_rep = cpu_to_le32(src->unified_rw_rep); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /* |
| 980 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 981 | * into a single payload. |
| 982 | */ |
| 983 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 984 | { |
| 985 | struct cmd_ts_pdu p; |
| 986 | int i, j; |
| 987 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 988 | dprint(FD_NET, "server sending end stats\n"); |
| 989 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 990 | memset(&p, 0, sizeof(p)); |
| 991 | |
Jens Axboe | 4e59d0f | 2014-03-14 08:41:39 -0600 | [diff] [blame] | 992 | strncpy(p.ts.name, ts->name, FIO_JOBNAME_SIZE - 1); |
| 993 | strncpy(p.ts.verror, ts->verror, FIO_VERROR_SIZE - 1); |
| 994 | strncpy(p.ts.description, ts->description, FIO_JOBDESC_SIZE - 1); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 995 | |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 996 | p.ts.error = cpu_to_le32(ts->error); |
| 997 | p.ts.thread_number = cpu_to_le32(ts->thread_number); |
| 998 | p.ts.groupid = cpu_to_le32(ts->groupid); |
| 999 | p.ts.pid = cpu_to_le32(ts->pid); |
| 1000 | p.ts.members = cpu_to_le32(ts->members); |
Jens Axboe | 771e58b | 2013-01-30 12:56:23 +0100 | [diff] [blame] | 1001 | p.ts.unified_rw_rep = cpu_to_le32(ts->unified_rw_rep); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1002 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 1003 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1004 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 1005 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 1006 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 1007 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 1008 | } |
| 1009 | |
| 1010 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 1011 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 1012 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 1013 | p.ts.minf = cpu_to_le64(ts->minf); |
| 1014 | p.ts.majf = cpu_to_le64(ts->majf); |
| 1015 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 1016 | |
| 1017 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 1018 | fio_fp64_t *src = &ts->percentile_list[i]; |
| 1019 | fio_fp64_t *dst = &p.ts.percentile_list[i]; |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 1020 | |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 1021 | 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] | 1022 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1023 | |
| 1024 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 1025 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 1026 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 1027 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 1028 | } |
| 1029 | |
| 1030 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 1031 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 1032 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 1033 | } |
| 1034 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 1035 | for (i = 0; i < DDIR_RWDIR_CNT; i++) |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1036 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 1037 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 1038 | |
Jens Axboe | 78799de | 2013-01-29 20:53:52 +0100 | [diff] [blame] | 1039 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1040 | 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] | 1041 | 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] | 1042 | } |
| 1043 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 1044 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1045 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 1046 | |
Jens Axboe | 298921d | 2012-09-24 18:47:01 +0200 | [diff] [blame] | 1047 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1048 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 1049 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 1050 | } |
| 1051 | |
| 1052 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 1053 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 1054 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 1055 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 1056 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Steven Noonan | ad705bc | 2013-04-08 15:05:25 -0700 | [diff] [blame] | 1057 | p.ts.unit_base = cpu_to_le32(ts->unit_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1058 | |
Jens Axboe | 3e260a4 | 2013-12-09 12:38:53 -0700 | [diff] [blame] | 1059 | p.ts.latency_depth = cpu_to_le32(ts->latency_depth); |
| 1060 | p.ts.latency_target = cpu_to_le64(ts->latency_target); |
| 1061 | p.ts.latency_window = cpu_to_le64(ts->latency_window); |
| 1062 | p.ts.latency_percentile.u.i = __cpu_to_le64(fio_double_to_uint64(ts->latency_percentile.u.f)); |
| 1063 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1064 | convert_gs(&p.rs, rs); |
| 1065 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1066 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), NULL, NULL); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | void fio_server_send_gs(struct group_run_stats *rs) |
| 1070 | { |
| 1071 | struct group_run_stats gs; |
| 1072 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 1073 | dprint(FD_NET, "server sending group run stats\n"); |
| 1074 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 1075 | convert_gs(&gs, rs); |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1076 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), NULL, NULL); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1079 | static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src) |
| 1080 | { |
| 1081 | int i; |
| 1082 | |
| 1083 | for (i = 0; i < 2; i++) { |
| 1084 | dst->ios[i] = cpu_to_le32(src->ios[i]); |
| 1085 | dst->merges[i] = cpu_to_le32(src->merges[i]); |
| 1086 | dst->sectors[i] = cpu_to_le64(src->sectors[i]); |
| 1087 | dst->ticks[i] = cpu_to_le32(src->ticks[i]); |
| 1088 | } |
| 1089 | |
| 1090 | dst->io_ticks = cpu_to_le32(src->io_ticks); |
| 1091 | dst->time_in_queue = cpu_to_le32(src->time_in_queue); |
| 1092 | dst->slavecount = cpu_to_le32(src->slavecount); |
| 1093 | dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f)); |
| 1094 | } |
| 1095 | |
| 1096 | static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src) |
| 1097 | { |
| 1098 | int i; |
| 1099 | |
Jens Axboe | 5914042 | 2014-04-14 08:28:08 -0600 | [diff] [blame] | 1100 | dst->name[FIO_DU_NAME_SZ - 1] = '\0'; |
| 1101 | strncpy((char *) dst->name, (char *) src->name, FIO_DU_NAME_SZ - 1); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1102 | |
| 1103 | for (i = 0; i < 2; i++) { |
Jens Axboe | a3b4cf7 | 2014-04-11 12:17:53 -0600 | [diff] [blame] | 1104 | dst->s.ios[i] = cpu_to_le32(src->s.ios[i]); |
| 1105 | dst->s.merges[i] = cpu_to_le32(src->s.merges[i]); |
| 1106 | dst->s.sectors[i] = cpu_to_le64(src->s.sectors[i]); |
| 1107 | dst->s.ticks[i] = cpu_to_le32(src->s.ticks[i]); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1108 | } |
| 1109 | |
Jens Axboe | a3b4cf7 | 2014-04-11 12:17:53 -0600 | [diff] [blame] | 1110 | dst->s.io_ticks = cpu_to_le32(src->s.io_ticks); |
| 1111 | dst->s.time_in_queue = cpu_to_le32(src->s.time_in_queue); |
| 1112 | dst->s.msec = cpu_to_le64(src->s.msec); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | void fio_server_send_du(void) |
| 1116 | { |
| 1117 | struct disk_util *du; |
| 1118 | struct flist_head *entry; |
| 1119 | struct cmd_du_pdu pdu; |
| 1120 | |
| 1121 | dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list)); |
| 1122 | |
Jens Axboe | 0766d92 | 2011-10-13 12:02:08 +0200 | [diff] [blame] | 1123 | memset(&pdu, 0, sizeof(pdu)); |
| 1124 | |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1125 | flist_for_each(entry, &disk_list) { |
| 1126 | du = flist_entry(entry, struct disk_util, list); |
| 1127 | |
| 1128 | convert_dus(&pdu.dus, &du->dus); |
| 1129 | convert_agg(&pdu.agg, &du->agg); |
| 1130 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1131 | fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), NULL, NULL); |
Jens Axboe | d09a64a | 2011-10-13 11:38:56 +0200 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1135 | /* |
| 1136 | * Send a command with a separate PDU, not inlined in the command |
| 1137 | */ |
| 1138 | static int fio_send_cmd_ext_pdu(int sk, uint16_t opcode, const void *buf, |
| 1139 | off_t size, uint64_t tag, uint32_t flags) |
| 1140 | { |
| 1141 | struct fio_net_cmd cmd; |
| 1142 | struct iovec iov[2]; |
| 1143 | |
Jens Axboe | 9f24726 | 2014-03-03 13:53:39 -0700 | [diff] [blame] | 1144 | iov[0].iov_base = (void *) &cmd; |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1145 | iov[0].iov_len = sizeof(cmd); |
| 1146 | iov[1].iov_base = (void *) buf; |
| 1147 | iov[1].iov_len = size; |
| 1148 | |
| 1149 | __fio_init_net_cmd(&cmd, opcode, size, tag); |
| 1150 | cmd.flags = __cpu_to_le32(flags); |
| 1151 | fio_net_cmd_crc_pdu(&cmd, buf); |
| 1152 | |
Jens Axboe | 8c95307 | 2012-03-20 14:35:35 +0100 | [diff] [blame] | 1153 | return fio_sendv_data(sk, iov, 2); |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1156 | static int fio_send_iolog_gz(struct cmd_iolog_pdu *pdu, struct io_log *log) |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1157 | { |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1158 | int ret = 0; |
| 1159 | #ifdef CONFIG_ZLIB |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1160 | z_stream stream; |
| 1161 | void *out_pdu; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1162 | |
| 1163 | /* |
| 1164 | * Dirty - since the log is potentially huge, compress it into |
| 1165 | * FIO_SERVER_MAX_FRAGMENT_PDU chunks and let the receiving |
| 1166 | * side defragment it. |
| 1167 | */ |
| 1168 | out_pdu = malloc(FIO_SERVER_MAX_FRAGMENT_PDU); |
| 1169 | |
| 1170 | stream.zalloc = Z_NULL; |
| 1171 | stream.zfree = Z_NULL; |
| 1172 | stream.opaque = Z_NULL; |
| 1173 | |
| 1174 | if (deflateInit(&stream, Z_DEFAULT_COMPRESSION) != Z_OK) { |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1175 | ret = 1; |
| 1176 | goto err; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
Jens Axboe | f5ed765 | 2012-03-14 16:28:07 +0100 | [diff] [blame] | 1179 | stream.next_in = (void *) log->log; |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 1180 | stream.avail_in = log->nr_samples * log_entry_sz(log); |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1181 | |
| 1182 | do { |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1183 | unsigned int this_len, flags = 0; |
| 1184 | int ret; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1185 | |
| 1186 | stream.avail_out = FIO_SERVER_MAX_FRAGMENT_PDU; |
| 1187 | stream.next_out = out_pdu; |
Jens Axboe | 3c547fe | 2012-03-14 21:53:00 +0100 | [diff] [blame] | 1188 | ret = deflate(&stream, Z_FINISH); |
| 1189 | /* may be Z_OK, or Z_STREAM_END */ |
| 1190 | if (ret < 0) |
| 1191 | goto err_zlib; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1192 | |
| 1193 | this_len = FIO_SERVER_MAX_FRAGMENT_PDU - stream.avail_out; |
| 1194 | |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1195 | if (stream.avail_in) |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1196 | flags = FIO_NET_CMD_F_MORE; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1197 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1198 | ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, |
| 1199 | out_pdu, this_len, 0, flags); |
| 1200 | if (ret) |
| 1201 | goto err_zlib; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1202 | } while (stream.avail_in); |
| 1203 | |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1204 | err_zlib: |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1205 | deflateEnd(&stream); |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1206 | err: |
| 1207 | free(out_pdu); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1208 | #endif |
Jens Axboe | 53bd8db | 2012-03-14 21:51:38 +0100 | [diff] [blame] | 1209 | return ret; |
Jens Axboe | 1b42725 | 2012-03-14 15:03:03 +0100 | [diff] [blame] | 1210 | } |
| 1211 | |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1212 | int fio_send_iolog(struct thread_data *td, struct io_log *log, const char *name) |
| 1213 | { |
| 1214 | struct cmd_iolog_pdu pdu; |
| 1215 | int i, ret = 0; |
| 1216 | |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 1217 | pdu.nr_samples = cpu_to_le64(log->nr_samples); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1218 | pdu.thread_number = cpu_to_le32(td->thread_number); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1219 | pdu.log_type = cpu_to_le32(log->log_type); |
| 1220 | pdu.compressed = cpu_to_le32(use_zlib); |
Jens Axboe | 9bdb926 | 2014-04-14 11:45:34 -0600 | [diff] [blame] | 1221 | |
| 1222 | strncpy((char *) pdu.name, name, FIO_NET_NAME_MAX); |
| 1223 | pdu.name[FIO_NET_NAME_MAX - 1] = '\0'; |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1224 | |
| 1225 | for (i = 0; i < log->nr_samples; i++) { |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 1226 | struct io_sample *s = get_sample(log, i); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1227 | |
Jens Axboe | bac4af1 | 2014-07-03 13:42:28 -0600 | [diff] [blame] | 1228 | s->time = cpu_to_le64(s->time); |
| 1229 | s->val = cpu_to_le64(s->val); |
| 1230 | s->__ddir = cpu_to_le32(s->__ddir); |
| 1231 | s->bs = cpu_to_le32(s->bs); |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 1232 | |
| 1233 | if (log->log_offset) { |
| 1234 | struct io_sample_offset *so = (void *) s; |
| 1235 | |
| 1236 | so->offset = cpu_to_le64(so->offset); |
| 1237 | } |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | /* |
| 1241 | * Send header first, it's not compressed. |
| 1242 | */ |
| 1243 | ret = fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, &pdu, |
| 1244 | sizeof(pdu), 0, FIO_NET_CMD_F_MORE); |
| 1245 | if (ret) |
| 1246 | return ret; |
| 1247 | |
| 1248 | /* |
| 1249 | * Now send actual log, compress if we can, otherwise just plain |
| 1250 | */ |
| 1251 | if (use_zlib) |
| 1252 | return fio_send_iolog_gz(&pdu, log); |
| 1253 | |
| 1254 | return fio_send_cmd_ext_pdu(server_fd, FIO_NET_CMD_IOLOG, log->log, |
Jens Axboe | ccefd5f | 2014-06-30 20:59:03 -0600 | [diff] [blame] | 1255 | log->nr_samples * log_entry_sz(log), 0, 0); |
Jens Axboe | 3989b14 | 2013-04-12 13:13:24 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 1258 | void fio_server_send_add_job(struct thread_data *td) |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1259 | { |
| 1260 | struct cmd_add_job_pdu pdu; |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1261 | |
Jens Axboe | 731e30a | 2012-03-14 16:35:37 +0100 | [diff] [blame] | 1262 | memset(&pdu, 0, sizeof(pdu)); |
Jens Axboe | 2f122b1 | 2012-03-15 13:10:19 +0100 | [diff] [blame] | 1263 | pdu.thread_number = cpu_to_le32(td->thread_number); |
| 1264 | pdu.groupid = cpu_to_le32(td->groupid); |
| 1265 | convert_thread_options_to_net(&pdu.top, &td->o); |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1266 | |
Jens Axboe | 40c6051 | 2012-03-27 16:03:04 +0200 | [diff] [blame] | 1267 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ADD_JOB, &pdu, sizeof(pdu), NULL, NULL); |
Jens Axboe | 807f997 | 2012-03-02 10:25:24 +0100 | [diff] [blame] | 1268 | } |
| 1269 | |
Jens Axboe | 122c772 | 2012-03-19 09:13:15 +0100 | [diff] [blame] | 1270 | void fio_server_send_start(struct thread_data *td) |
| 1271 | { |
| 1272 | assert(server_fd != -1); |
| 1273 | |
| 1274 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_SERVER_START, 0, NULL); |
| 1275 | } |
| 1276 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1277 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1278 | { |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1279 | struct sockaddr *addr; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 1280 | socklen_t socklen; |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1281 | char buf[80]; |
| 1282 | const char *str; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1283 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1284 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1285 | if (use_ipv6) |
| 1286 | sk = socket(AF_INET6, SOCK_STREAM, 0); |
| 1287 | else |
| 1288 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 1289 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1290 | if (sk < 0) { |
| 1291 | log_err("fio: socket: %s\n", strerror(errno)); |
| 1292 | return -1; |
| 1293 | } |
| 1294 | |
| 1295 | opt = 1; |
Jens Axboe | 1f81991 | 2013-01-23 17:21:41 -0700 | [diff] [blame] | 1296 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, (void *)&opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1297 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1298 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1299 | return -1; |
| 1300 | } |
| 1301 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 1302 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1303 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1304 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1305 | return -1; |
| 1306 | } |
| 1307 | #endif |
| 1308 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1309 | if (use_ipv6) { |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1310 | const void *src = &saddr_in6.sin6_addr; |
| 1311 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1312 | addr = (struct sockaddr *) &saddr_in6; |
| 1313 | socklen = sizeof(saddr_in6); |
| 1314 | saddr_in6.sin6_family = AF_INET6; |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1315 | str = inet_ntop(AF_INET6, src, buf, sizeof(buf)); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1316 | } else { |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1317 | const void *src = &saddr_in.sin_addr; |
| 1318 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1319 | addr = (struct sockaddr *) &saddr_in; |
| 1320 | socklen = sizeof(saddr_in); |
| 1321 | saddr_in.sin_family = AF_INET; |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1322 | str = inet_ntop(AF_INET, src, buf, sizeof(buf)); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1323 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1324 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1325 | if (bind(sk, addr, socklen) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1326 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | 17e3531 | 2014-03-25 10:50:55 -0700 | [diff] [blame] | 1327 | log_info("fio: failed with IPv%c %s\n", use_ipv6 ? '6' : '4', str); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1328 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1329 | return -1; |
| 1330 | } |
| 1331 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1332 | return sk; |
| 1333 | } |
| 1334 | |
| 1335 | static int fio_init_server_sock(void) |
| 1336 | { |
| 1337 | struct sockaddr_un addr; |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame] | 1338 | socklen_t len; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1339 | mode_t mode; |
| 1340 | int sk; |
| 1341 | |
| 1342 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 1343 | if (sk < 0) { |
| 1344 | log_err("fio: socket: %s\n", strerror(errno)); |
| 1345 | return -1; |
| 1346 | } |
| 1347 | |
| 1348 | mode = umask(000); |
| 1349 | |
| 1350 | memset(&addr, 0, sizeof(addr)); |
| 1351 | addr.sun_family = AF_UNIX; |
Jens Axboe | a48fddb | 2014-04-14 08:55:13 -0600 | [diff] [blame] | 1352 | strncpy(addr.sun_path, bind_sock, sizeof(addr.sun_path) - 1); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1353 | |
| 1354 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 1355 | |
| 1356 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 1357 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 1358 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1359 | return -1; |
| 1360 | } |
| 1361 | |
| 1362 | umask(mode); |
| 1363 | return sk; |
| 1364 | } |
| 1365 | |
| 1366 | static int fio_init_server_connection(void) |
| 1367 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1368 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1369 | int sk; |
| 1370 | |
| 1371 | dprint(FD_NET, "starting server\n"); |
| 1372 | |
| 1373 | if (!bind_sock) |
| 1374 | sk = fio_init_server_ip(); |
| 1375 | else |
| 1376 | sk = fio_init_server_sock(); |
| 1377 | |
| 1378 | if (sk < 0) |
| 1379 | return sk; |
| 1380 | |
Jens Axboe | afdcad2 | 2014-04-14 08:54:09 -0600 | [diff] [blame] | 1381 | memset(bind_str, 0, sizeof(bind_str)); |
| 1382 | |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1383 | if (!bind_sock) { |
| 1384 | char *p, port[16]; |
| 1385 | const void *src; |
| 1386 | int af; |
| 1387 | |
| 1388 | if (use_ipv6) { |
| 1389 | af = AF_INET6; |
| 1390 | src = &saddr_in6.sin6_addr; |
| 1391 | } else { |
| 1392 | af = AF_INET; |
| 1393 | src = &saddr_in.sin_addr; |
| 1394 | } |
| 1395 | |
| 1396 | p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str)); |
| 1397 | |
| 1398 | sprintf(port, ",%u", fio_net_port); |
| 1399 | if (p) |
| 1400 | strcat(p, port); |
| 1401 | else |
Jens Axboe | afdcad2 | 2014-04-14 08:54:09 -0600 | [diff] [blame] | 1402 | strncpy(bind_str, port, sizeof(bind_str) - 1); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1403 | } else |
Jens Axboe | afdcad2 | 2014-04-14 08:54:09 -0600 | [diff] [blame] | 1404 | strncpy(bind_str, bind_sock, sizeof(bind_str) - 1); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1405 | |
| 1406 | log_info("fio: server listening on %s\n", bind_str); |
| 1407 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 1408 | if (listen(sk, 0) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1409 | log_err("fio: listen: %s\n", strerror(errno)); |
Jens Axboe | 2fd973c | 2014-04-08 21:13:42 -0600 | [diff] [blame] | 1410 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1411 | return -1; |
| 1412 | } |
| 1413 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1414 | return sk; |
| 1415 | } |
| 1416 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1417 | int fio_server_parse_host(const char *host, int ipv6, struct in_addr *inp, |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1418 | struct in6_addr *inp6) |
| 1419 | |
| 1420 | { |
| 1421 | int ret = 0; |
| 1422 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1423 | if (ipv6) |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1424 | ret = inet_pton(AF_INET6, host, inp6); |
| 1425 | else |
| 1426 | ret = inet_pton(AF_INET, host, inp); |
| 1427 | |
| 1428 | if (ret != 1) { |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1429 | struct addrinfo hints, *res; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1430 | |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1431 | memset(&hints, 0, sizeof(hints)); |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1432 | hints.ai_family = ipv6 ? AF_INET6 : AF_INET; |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1433 | hints.ai_socktype = SOCK_STREAM; |
| 1434 | |
| 1435 | ret = getaddrinfo(host, NULL, &hints, &res); |
| 1436 | if (ret) { |
| 1437 | log_err("fio: failed to resolve <%s> (%s)\n", host, |
| 1438 | gai_strerror(ret)); |
Jens Axboe | 3caf43e | 2014-01-24 18:52:16 -0800 | [diff] [blame] | 1439 | return 1; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1442 | if (ipv6) |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1443 | memcpy(inp6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr, sizeof(*inp6)); |
| 1444 | else |
| 1445 | memcpy(inp, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(*inp)); |
| 1446 | |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1447 | ret = 1; |
Jens Axboe | 479471c | 2014-01-23 20:42:06 -0800 | [diff] [blame] | 1448 | freeaddrinfo(res); |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | return !(ret == 1); |
| 1452 | } |
| 1453 | |
Jens Axboe | 660a2bf | 2011-10-24 09:35:06 +0200 | [diff] [blame] | 1454 | /* |
| 1455 | * Parse a host/ip/port string. Reads from 'str'. |
| 1456 | * |
| 1457 | * Outputs: |
| 1458 | * |
| 1459 | * For IPv4: |
| 1460 | * *ptr is the host, *port is the port, inp is the destination. |
| 1461 | * For IPv6: |
| 1462 | * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1. |
| 1463 | * For local domain sockets: |
| 1464 | * *ptr is the filename, *is_sock is 1. |
| 1465 | */ |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1466 | 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] | 1467 | int *port, struct in_addr *inp, |
| 1468 | struct in6_addr *inp6, int *ipv6) |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1469 | { |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1470 | const char *host = str; |
| 1471 | char *portp; |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1472 | int lport = 0; |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1473 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1474 | *ptr = NULL; |
| 1475 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1476 | *port = fio_net_port; |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1477 | *ipv6 = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1478 | |
| 1479 | if (!strncmp(str, "sock:", 5)) { |
| 1480 | *ptr = strdup(str + 5); |
| 1481 | *is_sock = 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1482 | |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Is it ip:<ip or host>:port |
| 1488 | */ |
| 1489 | if (!strncmp(host, "ip:", 3)) |
| 1490 | host += 3; |
| 1491 | else if (!strncmp(host, "ip4:", 4)) |
| 1492 | host += 4; |
| 1493 | else if (!strncmp(host, "ip6:", 4)) { |
| 1494 | host += 4; |
| 1495 | *ipv6 = 1; |
| 1496 | } else if (host[0] == ':') { |
| 1497 | /* String is :port */ |
| 1498 | host++; |
| 1499 | lport = atoi(host); |
| 1500 | if (!lport || lport > 65535) { |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 1501 | log_err("fio: bad server port %u\n", lport); |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1502 | return 1; |
| 1503 | } |
| 1504 | /* no hostname given, we are done */ |
| 1505 | *port = lport; |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
Jens Axboe | b96b90c | 2014-03-25 10:37:56 -0700 | [diff] [blame] | 1510 | * If no port seen yet, check if there's a last ',' at the end |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1511 | */ |
| 1512 | if (!lport) { |
| 1513 | portp = strchr(host, ','); |
| 1514 | if (portp) { |
| 1515 | *portp = '\0'; |
| 1516 | portp++; |
| 1517 | lport = atoi(portp); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1518 | if (!lport || lport > 65535) { |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 1519 | log_err("fio: bad server port %u\n", lport); |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1520 | return 1; |
| 1521 | } |
Jens Axboe | 7686762 | 2011-10-25 09:52:51 +0200 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | if (lport) |
| 1526 | *port = lport; |
| 1527 | |
| 1528 | if (!strlen(host)) |
| 1529 | return 0; |
| 1530 | |
| 1531 | *ptr = strdup(host); |
| 1532 | |
Jens Axboe | 3aa3cee | 2014-01-24 18:56:56 -0800 | [diff] [blame] | 1533 | if (fio_server_parse_host(*ptr, *ipv6, inp, inp6)) { |
Jens Axboe | 3ec62ec | 2012-03-01 12:01:29 +0100 | [diff] [blame] | 1534 | free(*ptr); |
| 1535 | *ptr = NULL; |
| 1536 | return 1; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | if (*port == 0) |
| 1540 | *port = fio_net_port; |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1545 | /* |
| 1546 | * Server arg should be one of: |
| 1547 | * |
| 1548 | * sock:/path/to/socket |
| 1549 | * ip:1.2.3.4 |
| 1550 | * 1.2.3.4 |
| 1551 | * |
| 1552 | * Where sock uses unix domain sockets, and ip binds the server to |
| 1553 | * a specific interface. If no arguments are given to the server, it |
| 1554 | * uses IP and binds to 0.0.0.0. |
| 1555 | * |
| 1556 | */ |
| 1557 | static int fio_handle_server_arg(void) |
| 1558 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1559 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1560 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1561 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1562 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 1563 | |
| 1564 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1565 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1566 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1567 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1568 | &port, &saddr_in.sin_addr, |
| 1569 | &saddr_in6.sin6_addr, &use_ipv6); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1570 | |
| 1571 | if (!is_sock && bind_sock) { |
| 1572 | free(bind_sock); |
| 1573 | bind_sock = NULL; |
| 1574 | } |
| 1575 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 1576 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 1577 | fio_net_port = port; |
| 1578 | saddr_in.sin_port = htons(port); |
Jens Axboe | 811826b | 2011-10-24 09:11:50 +0200 | [diff] [blame] | 1579 | saddr_in6.sin6_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 1580 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1581 | } |
| 1582 | |
Jens Axboe | 43cdea1 | 2014-03-25 15:06:17 -0700 | [diff] [blame] | 1583 | static void sig_int(int sig) |
| 1584 | { |
| 1585 | if (bind_sock) |
| 1586 | unlink(bind_sock); |
| 1587 | } |
| 1588 | |
| 1589 | static void set_sig_handlers(void) |
| 1590 | { |
| 1591 | struct sigaction act; |
| 1592 | |
| 1593 | memset(&act, 0, sizeof(act)); |
| 1594 | act.sa_handler = sig_int; |
| 1595 | act.sa_flags = SA_RESTART; |
| 1596 | sigaction(SIGINT, &act, NULL); |
| 1597 | } |
| 1598 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1599 | static int fio_server(void) |
| 1600 | { |
| 1601 | int sk, ret; |
| 1602 | |
| 1603 | dprint(FD_NET, "starting server\n"); |
| 1604 | |
| 1605 | if (fio_handle_server_arg()) |
| 1606 | return -1; |
| 1607 | |
| 1608 | sk = fio_init_server_connection(); |
| 1609 | if (sk < 0) |
| 1610 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1611 | |
Jens Axboe | 43cdea1 | 2014-03-25 15:06:17 -0700 | [diff] [blame] | 1612 | set_sig_handlers(); |
| 1613 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1614 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1615 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1616 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1617 | |
| 1618 | if (fio_server_arg) { |
| 1619 | free(fio_server_arg); |
| 1620 | fio_server_arg = NULL; |
| 1621 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1622 | if (bind_sock) |
| 1623 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1624 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 1625 | return ret; |
| 1626 | } |
| 1627 | |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1628 | void fio_server_got_signal(int signal) |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1629 | { |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 1630 | if (signal == SIGPIPE) |
| 1631 | server_fd = -1; |
| 1632 | else { |
| 1633 | log_info("\nfio: terminating on signal %d\n", signal); |
| 1634 | exit_backend = 1; |
| 1635 | } |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1636 | } |
| 1637 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1638 | static int check_existing_pidfile(const char *pidfile) |
| 1639 | { |
| 1640 | struct stat sb; |
| 1641 | char buf[16]; |
| 1642 | pid_t pid; |
| 1643 | FILE *f; |
| 1644 | |
| 1645 | if (stat(pidfile, &sb)) |
| 1646 | return 0; |
| 1647 | |
| 1648 | f = fopen(pidfile, "r"); |
| 1649 | if (!f) |
| 1650 | return 0; |
| 1651 | |
Jens Axboe | bfc3b17 | 2011-10-10 21:16:55 +0200 | [diff] [blame] | 1652 | if (fread(buf, sb.st_size, 1, f) <= 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1653 | fclose(f); |
| 1654 | return 1; |
| 1655 | } |
| 1656 | fclose(f); |
| 1657 | |
| 1658 | pid = atoi(buf); |
| 1659 | if (kill(pid, SIGCONT) < 0) |
Jens Axboe | ea5aa1b | 2011-10-11 11:45:35 +0200 | [diff] [blame] | 1660 | return errno != ESRCH; |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1661 | |
| 1662 | return 1; |
| 1663 | } |
| 1664 | |
| 1665 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1666 | { |
| 1667 | FILE *fpid; |
| 1668 | |
| 1669 | fpid = fopen(pidfile, "w"); |
| 1670 | if (!fpid) { |
| 1671 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1672 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1676 | fclose(fpid); |
| 1677 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | /* |
| 1681 | * If pidfile is specified, background us. |
| 1682 | */ |
| 1683 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1684 | { |
| 1685 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1686 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1687 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1688 | #if defined(WIN32) |
Jens Axboe | 905c78b | 2012-03-06 19:34:22 +0100 | [diff] [blame] | 1689 | WSADATA wsd; |
Jens Axboe | 3c3ed07 | 2012-03-27 09:12:39 +0200 | [diff] [blame] | 1690 | WSAStartup(MAKEWORD(2, 2), &wsd); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 1691 | #endif |
| 1692 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1693 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1694 | return fio_server(); |
| 1695 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1696 | if (check_existing_pidfile(pidfile)) { |
| 1697 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 1698 | pidfile); |
Jens Axboe | b8ba87a | 2014-04-11 11:37:34 -0600 | [diff] [blame] | 1699 | free(pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1700 | return -1; |
| 1701 | } |
| 1702 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1703 | pid = fork(); |
| 1704 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1705 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1706 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 1707 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1708 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1709 | int ret = write_pid(pid, pidfile); |
| 1710 | |
Jens Axboe | b8ba87a | 2014-04-11 11:37:34 -0600 | [diff] [blame] | 1711 | free(pidfile); |
Jens Axboe | 5ea7355 | 2014-09-11 17:05:58 -0600 | [diff] [blame] | 1712 | _exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1713 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1714 | |
| 1715 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1716 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1717 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1718 | close(STDIN_FILENO); |
| 1719 | close(STDOUT_FILENO); |
| 1720 | close(STDERR_FILENO); |
| 1721 | f_out = NULL; |
| 1722 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1723 | |
| 1724 | ret = fio_server(); |
| 1725 | |
| 1726 | closelog(); |
| 1727 | unlink(pidfile); |
| 1728 | free(pidfile); |
| 1729 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1730 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1731 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1732 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1733 | { |
| 1734 | fio_server_arg = strdup(arg); |
| 1735 | } |