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