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