Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <limits.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 5 | #include <errno.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <sys/poll.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/wait.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 10 | #include <sys/mman.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 11 | #include <netinet/in.h> |
| 12 | #include <arpa/inet.h> |
| 13 | #include <netdb.h> |
| 14 | |
| 15 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame^] | 16 | #include "server.h" |
| 17 | #include "crc/crc32.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 18 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame^] | 19 | int fio_net_port = 8765; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 20 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 21 | int exit_backend = 0; |
| 22 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame^] | 23 | static char *job_buf; |
| 24 | static unsigned int job_cur_len; |
| 25 | static unsigned int job_max_len; |
| 26 | |
| 27 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 28 | { |
| 29 | do { |
| 30 | int ret = send(sk, p, len, 0); |
| 31 | |
| 32 | if (ret > 0) { |
| 33 | len -= ret; |
| 34 | if (!len) |
| 35 | break; |
| 36 | p += ret; |
| 37 | continue; |
| 38 | } else if (!ret) |
| 39 | break; |
| 40 | else if (errno == EAGAIN || errno == EINTR) |
| 41 | continue; |
| 42 | } while (!exit_backend); |
| 43 | |
| 44 | if (!len) |
| 45 | return 0; |
| 46 | |
| 47 | return 1; |
| 48 | } |
| 49 | |
| 50 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 51 | { |
| 52 | do { |
| 53 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 54 | |
| 55 | if (ret > 0) { |
| 56 | len -= ret; |
| 57 | if (!len) |
| 58 | break; |
| 59 | p += ret; |
| 60 | continue; |
| 61 | } else if (!ret) |
| 62 | break; |
| 63 | else if (errno == EAGAIN || errno == EINTR) |
| 64 | continue; |
| 65 | } while (!exit_backend); |
| 66 | |
| 67 | if (!len) |
| 68 | return 0; |
| 69 | |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 74 | { |
| 75 | uint32_t crc; |
| 76 | |
| 77 | cmd->cmd_crc32 = le32_to_cpu(cmd->cmd_crc32); |
| 78 | cmd->pdu_crc32 = le32_to_cpu(cmd->pdu_crc32); |
| 79 | |
| 80 | crc = crc32(cmd, sizeof(*cmd) - 2 * sizeof(uint32_t)); |
| 81 | if (crc != cmd->cmd_crc32) { |
| 82 | log_err("fio: server bad crc on command (got %x, wanted %x)\n", |
| 83 | cmd->cmd_crc32, crc); |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | cmd->version = le16_to_cpu(cmd->version); |
| 88 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 89 | cmd->flags = le32_to_cpu(cmd->flags); |
| 90 | cmd->serial = le64_to_cpu(cmd->serial); |
| 91 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 92 | |
| 93 | switch (cmd->version) { |
| 94 | case FIO_SERVER_VER1: |
| 95 | break; |
| 96 | default: |
| 97 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | if (cmd->pdu_len > FIO_SERVER_MAX_PDU) { |
| 102 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static struct fio_net_cmd *read_command(int sk) |
| 110 | { |
| 111 | struct fio_net_cmd cmd, *ret = NULL; |
| 112 | uint32_t crc; |
| 113 | |
| 114 | if (fio_recv_data(sk, &cmd, sizeof(cmd))) |
| 115 | return NULL; |
| 116 | |
| 117 | /* We have a command, verify it and swap if need be */ |
| 118 | if (verify_convert_cmd(&cmd)) |
| 119 | return NULL; |
| 120 | |
| 121 | /* Command checks out, alloc real command and fill in */ |
| 122 | ret = malloc(sizeof(cmd) + cmd.pdu_len); |
| 123 | memcpy(ret, &cmd, sizeof(cmd)); |
| 124 | |
| 125 | if (!ret->pdu_len) |
| 126 | return ret; |
| 127 | |
| 128 | /* There's payload, get it */ |
| 129 | if (fio_recv_data(sk, (void *) ret + sizeof(*ret), ret->pdu_len)) { |
| 130 | free(ret); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
| 134 | /* Verify payload crc */ |
| 135 | crc = crc32(ret->payload, ret->pdu_len); |
| 136 | if (crc != ret->pdu_crc32) { |
| 137 | log_err("fio: server bad crc on payload (got %x, wanted %x)\n", |
| 138 | ret->pdu_crc32, crc); |
| 139 | free(ret); |
| 140 | return NULL; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 148 | { |
| 149 | uint32_t pdu_len; |
| 150 | |
| 151 | cmd->cmd_crc32 = cpu_to_le32(crc32(cmd, |
| 152 | sizeof(*cmd) - 2 * sizeof(uint32_t))); |
| 153 | |
| 154 | pdu_len = le32_to_cpu(cmd->pdu_len); |
| 155 | if (pdu_len) |
| 156 | cmd->pdu_crc32 = cpu_to_le32(crc32(cmd->payload, pdu_len)); |
| 157 | } |
| 158 | |
| 159 | static int send_simple_command(int sk, uint16_t opcode, uint64_t serial) |
| 160 | { |
| 161 | struct fio_net_cmd cmd = { |
| 162 | .version = cpu_to_le16(FIO_SERVER_VER1), |
| 163 | .opcode = cpu_to_le16(opcode), |
| 164 | .serial = cpu_to_le16(serial), |
| 165 | }; |
| 166 | |
| 167 | fio_net_cmd_crc(&cmd); |
| 168 | |
| 169 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Send an ack for this command |
| 174 | */ |
| 175 | static int ack_command(int sk, struct fio_net_cmd *cmd) |
| 176 | { |
| 177 | return send_simple_command(sk, FIO_NET_CMD_ACK, cmd->serial); |
| 178 | } |
| 179 | |
| 180 | #if 0 |
| 181 | static int nak_command(int sk, struct fio_net_cmd *cmd) |
| 182 | { |
| 183 | return send_simple_command(sk, FIO_NET_CMD_NAK, cmd->serial); |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | static int handle_cur_job(struct fio_net_cmd *cmd, int done) |
| 188 | { |
| 189 | unsigned int left = job_max_len - job_cur_len; |
| 190 | int ret = 0; |
| 191 | |
| 192 | if (left < cmd->pdu_len) { |
| 193 | job_buf = realloc(job_buf, job_max_len + 2 * cmd->pdu_len); |
| 194 | job_max_len += 2 * cmd->pdu_len; |
| 195 | } |
| 196 | |
| 197 | memcpy(job_buf + job_cur_len, cmd->payload, cmd->pdu_len); |
| 198 | job_cur_len += cmd->pdu_len; |
| 199 | |
| 200 | if (done) { |
| 201 | parse_jobs_ini(job_buf, 1, 0); |
| 202 | ret = exec_run(); |
| 203 | reset_fio_state(); |
| 204 | free(job_buf); |
| 205 | job_buf = NULL; |
| 206 | job_cur_len = job_max_len = 0; |
| 207 | } |
| 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static int handle_command(struct fio_net_cmd *cmd) |
| 213 | { |
| 214 | int ret; |
| 215 | |
| 216 | switch (cmd->opcode) { |
| 217 | case FIO_NET_CMD_QUIT: |
| 218 | exit_backend = 1; |
| 219 | return 1; |
| 220 | case FIO_NET_CMD_ACK: |
| 221 | return 0; |
| 222 | case FIO_NET_CMD_NAK: |
| 223 | return 1; |
| 224 | case FIO_NET_CMD_JOB: |
| 225 | ret = handle_cur_job(cmd, 0); |
| 226 | break; |
| 227 | case FIO_NET_CMD_JOB_END: |
| 228 | ret = handle_cur_job(cmd, 1); |
| 229 | break; |
| 230 | default: |
| 231 | log_err("fio: unknown opcode: %d\n", cmd->opcode); |
| 232 | ret = 1; |
| 233 | } |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static int handle_connection(int sk) |
| 239 | { |
| 240 | struct fio_net_cmd *cmd = NULL; |
| 241 | int ret = 0; |
| 242 | |
| 243 | /* read forever */ |
| 244 | while (!exit_backend) { |
| 245 | cmd = read_command(sk); |
| 246 | if (!cmd) { |
| 247 | ret = 1; |
| 248 | break; |
| 249 | } |
| 250 | |
| 251 | ret = ack_command(sk, cmd); |
| 252 | if (ret) |
| 253 | break; |
| 254 | |
| 255 | ret = handle_command(cmd); |
| 256 | if (ret) |
| 257 | break; |
| 258 | |
| 259 | free(cmd); |
| 260 | } |
| 261 | |
| 262 | if (cmd) |
| 263 | free(cmd); |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 268 | static int accept_loop(int listen_sk) |
| 269 | { |
| 270 | struct sockaddr addr; |
| 271 | unsigned int len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 272 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame^] | 273 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 274 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 275 | flags = fcntl(listen_sk, F_GETFL); |
| 276 | flags |= O_NONBLOCK; |
| 277 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 278 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 279 | pfd.fd = listen_sk; |
| 280 | pfd.events = POLLIN; |
| 281 | do { |
| 282 | ret = poll(&pfd, 1, 100); |
| 283 | if (ret < 0) { |
| 284 | if (errno == EINTR) |
| 285 | break; |
| 286 | perror("poll"); |
| 287 | goto out; |
| 288 | } else if (!ret) |
| 289 | continue; |
| 290 | |
| 291 | if (pfd.revents & POLLIN) |
| 292 | break; |
| 293 | } while (!exit_backend); |
| 294 | |
| 295 | if (exit_backend) |
| 296 | goto out; |
| 297 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 298 | sk = accept(listen_sk, &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 299 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 300 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 301 | return -1; |
| 302 | } |
| 303 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame^] | 304 | exitval = handle_connection(sk); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 305 | |
| 306 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 307 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 308 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 309 | goto again; |
| 310 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 311 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame^] | 312 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | int fio_server(void) |
| 316 | { |
| 317 | struct sockaddr_in saddr_in; |
| 318 | struct sockaddr addr; |
| 319 | unsigned int len; |
Jens Axboe | afcf775 | 2011-09-29 18:28:46 -0600 | [diff] [blame] | 320 | int sk, opt, ret; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 321 | |
| 322 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 323 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 324 | log_err("fio: socket: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | opt = 1; |
| 329 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 330 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | #ifdef SO_REUSEPORT |
| 334 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 335 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 336 | return 1; |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | saddr_in.sin_family = AF_INET; |
| 341 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame^] | 342 | saddr_in.sin_port = htons(fio_net_port); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 343 | |
| 344 | if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 345 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | if (listen(sk, 1) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 350 | log_err("fio: listen: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | len = sizeof(addr); |
| 355 | if (getsockname(sk, &addr, &len) < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 356 | log_err("fio: getsockname: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 357 | return -1; |
| 358 | } |
| 359 | |
Jens Axboe | afcf775 | 2011-09-29 18:28:46 -0600 | [diff] [blame] | 360 | ret = accept_loop(sk); |
| 361 | close(sk); |
| 362 | return ret; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 363 | } |