X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=server.c;h=a54db7bf5d385aee5dbbc9e66d159169681f4de3;hp=29e2387326cfd0cf644ac639192018f876164dc2;hb=fa2ea806de0d6410320abd97599bc52f5a3e72cc;hpb=d05c4a03365f1b677c05840865e67ffaf2c5b05b diff --git a/server.c b/server.c index 29e23873..a54db7bf 100644 --- a/server.c +++ b/server.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include #include @@ -18,12 +20,18 @@ #include "fio.h" #include "server.h" #include "crc/crc16.h" +#include "ieee754.h" + +#include "fio_version.h" int fio_net_port = 8765; int exit_backend = 0; static int server_fd = -1; +static char *fio_server_arg; +static char *bind_sock; +static struct sockaddr_in saddr_in; int fio_send_data(int sk, const void *p, unsigned int len) { @@ -94,7 +102,7 @@ static int verify_convert_cmd(struct fio_net_cmd *cmd) cmd->pdu_len = le32_to_cpu(cmd->pdu_len); switch (cmd->version) { - case FIO_SERVER_VER1: + case FIO_SERVER_VER: break; default: log_err("fio: bad server cmd version %d\n", cmd->version); @@ -112,7 +120,7 @@ static int verify_convert_cmd(struct fio_net_cmd *cmd) /* * Read (and defragment, if necessary) incoming commands */ -struct fio_net_cmd *fio_net_recv_cmd(int sk, int block) +struct fio_net_cmd *fio_net_recv_cmd(int sk) { struct fio_net_cmd cmd, *cmdret = NULL; size_t cmd_size = 0, pdu_offset = 0; @@ -121,37 +129,6 @@ struct fio_net_cmd *fio_net_recv_cmd(int sk, int block) void *pdu = NULL; do { - struct pollfd pfd; - - pfd.fd = sk; - pfd.events = POLLIN; - ret = 0; - do { - int timeo = block ? 100 : 10; - - ret = poll(&pfd, 1, timeo); - if (ret < 0) { - if (errno == EINTR) - break; - log_err("fio: poll: %s\n", strerror(errno)); - break; - } else if (!ret) { - if (!block) - return NULL; - continue; - } - - if (pfd.revents & POLLIN) - break; - if (pfd.revents & (POLLERR|POLLHUP)) { - ret = 1; - break; - } - } while (ret >= 0 && block); - - if (ret < 0) - break; - ret = fio_recv_data(sk, &cmd, sizeof(cmd)); if (ret) break; @@ -259,12 +236,9 @@ int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size) int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t serial) { - struct fio_net_cmd cmd = { - .version = __cpu_to_le16(FIO_SERVER_VER1), - .opcode = cpu_to_le16(opcode), - .serial = cpu_to_le64(serial), - }; + struct fio_net_cmd cmd; + fio_init_net_cmd(&cmd, opcode, NULL, 0); fio_net_cmd_crc(&cmd); return fio_send_data(sk, &cmd, sizeof(cmd)); @@ -281,8 +255,10 @@ static int handle_job_cmd(struct fio_net_cmd *cmd) char *buf = (char *) cmd->payload; int ret; - if (parse_jobs_ini(buf, 1, 0)) + if (parse_jobs_ini(buf, 1, 0)) { + fio_server_send_quit_cmd(); return -1; + } fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0); @@ -294,21 +270,35 @@ static int handle_job_cmd(struct fio_net_cmd *cmd) static int handle_jobline_cmd(struct fio_net_cmd *cmd) { - struct cmd_line_pdu *pdu = (struct cmd_line_pdu *) cmd->payload; - char *argv[FIO_NET_CMD_JOBLINE_ARGV]; + void *pdu = cmd->payload; + struct cmd_single_line_pdu *cslp; + struct cmd_line_pdu *clp; + unsigned long offset; + char **argv; int ret, i; - pdu->argc = le16_to_cpu(pdu->argc); + clp = pdu; + clp->lines = le16_to_cpu(clp->lines); + argv = malloc(clp->lines * sizeof(char *)); + offset = sizeof(*clp); + + dprint(FD_NET, "server: %d command line args\n", clp->lines); - dprint(FD_NET, "server: %d command line args\n", pdu->argc); + for (i = 0; i < clp->lines; i++) { + cslp = pdu + offset; + argv[i] = (char *) cslp->text; - for (i = 0; i < pdu->argc; i++) { - argv[i] = (char *) pdu->argv[i]; + offset += sizeof(*cslp) + le16_to_cpu(cslp->len); dprint(FD_NET, "server: %d: %s\n", i, argv[i]); } - if (parse_cmd_line(pdu->argc, argv)) + if (parse_cmd_line(clp->lines, argv)) { + fio_server_send_quit_cmd(); + free(argv); return -1; + } + + free(argv); fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0); @@ -331,6 +321,9 @@ static int handle_probe_cmd(struct fio_net_cmd *cmd) probe.fio_minor = FIO_MINOR; probe.fio_patch = FIO_PATCH; + probe.os = FIO_OS; + probe.arch = FIO_ARCH; + return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe)); } @@ -371,7 +364,37 @@ static int handle_connection(int sk, int block) /* read forever */ while (!exit_backend) { - cmd = fio_net_recv_cmd(sk, block); + struct pollfd pfd = { + .fd = sk, + .events = POLLIN, + }; + + ret = 0; + do { + ret = poll(&pfd, 1, 100); + if (ret < 0) { + if (errno == EINTR) + break; + log_err("fio: poll: %s\n", strerror(errno)); + break; + } else if (!ret) { + if (!block) + return 0; + continue; + } + + if (pfd.revents & POLLIN) + break; + if (pfd.revents & (POLLERR|POLLHUP)) { + ret = 1; + break; + } + } while (!exit_backend); + + if (ret < 0) + break; + + cmd = fio_net_recv_cmd(sk); if (!cmd) { ret = -1; break; @@ -464,9 +487,12 @@ static void convert_io_stat(struct io_stat *dst, struct io_stat *src) dst->max_val = cpu_to_le64(src->max_val); dst->min_val = cpu_to_le64(src->min_val); dst->samples = cpu_to_le64(src->samples); - /* FIXME */ - dst->mean = __cpu_to_le64(src->mean); - dst->S = __cpu_to_le64(src->S); + + /* + * Encode to IEEE 754 for network transfer + */ + dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); + dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f)); } static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) @@ -521,7 +547,12 @@ void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) p.ts.minf = cpu_to_le64(ts->minf); p.ts.majf = cpu_to_le64(ts->majf); p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); - p.ts.percentile_list = NULL; + + for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { + fio_fp64_t *fp = &p.ts.percentile_list[i]; + + fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f)); + } for (i = 0; i < FIO_IO_U_MAP_NR; i++) { p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); @@ -627,14 +658,9 @@ int fio_server_log(const char *format, ...) return fio_server_text_output(buffer, len); } -static int fio_server(void) +static int fio_init_server_ip(void) { - struct sockaddr_in saddr_in; - struct sockaddr addr; - fio_socklen_t len; - int sk, opt, ret; - - dprint(FD_NET, "starting server\n"); + int sk, opt; sk = socket(AF_INET, SOCK_STREAM, 0); if (sk < 0) { @@ -645,37 +671,224 @@ static int fio_server(void) opt = 1; if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { log_err("fio: setsockopt: %s\n", strerror(errno)); + close(sk); return -1; } #ifdef SO_REUSEPORT if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { log_err("fio: setsockopt: %s\n", strerror(errno)); + close(sk); return -1; } #endif saddr_in.sin_family = AF_INET; - saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); - saddr_in.sin_port = htons(fio_net_port); if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { log_err("fio: bind: %s\n", strerror(errno)); + close(sk); + return -1; + } + + return sk; +} + +static int fio_init_server_sock(void) +{ + struct sockaddr_un addr; + fio_socklen_t len; + mode_t mode; + int sk; + + sk = socket(AF_UNIX, SOCK_STREAM, 0); + if (sk < 0) { + log_err("fio: socket: %s\n", strerror(errno)); + return -1; + } + + mode = umask(000); + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strcpy(addr.sun_path, bind_sock); + unlink(bind_sock); + + len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; + + if (bind(sk, (struct sockaddr *) &addr, len) < 0) { + log_err("fio: bind: %s\n", strerror(errno)); + close(sk); return -1; } + umask(mode); + return sk; +} + +static int fio_init_server_connection(void) +{ + char bind_str[128]; + int sk; + + dprint(FD_NET, "starting server\n"); + + if (!bind_sock) + sk = fio_init_server_ip(); + else + sk = fio_init_server_sock(); + + if (sk < 0) + return sk; + + if (!bind_sock) + sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port); + else + strcpy(bind_str, bind_sock); + + log_info("fio: server listening on %s\n", bind_str); + if (listen(sk, 1) < 0) { log_err("fio: listen: %s\n", strerror(errno)); return -1; } - len = sizeof(addr); - if (getsockname(sk, &addr, &len) < 0) { - log_err("fio: getsockname: %s\n", strerror(errno)); - return -1; + return sk; +} + +int fio_server_parse_string(const char *str, char **ptr, int *is_sock, + int *port, struct in_addr *inp) +{ + *ptr = NULL; + *is_sock = 0; + *port = fio_net_port; + + if (!strncmp(str, "sock:", 5)) { + *ptr = strdup(str + 5); + *is_sock = 1; + } else { + const char *host = str; + char *portp; + int lport = 0; + + /* + * Is it ip::port + */ + if (!strncmp(host, "ip:", 3)) + host += 3; + else if (host[0] == ':') { + /* String is :port */ + host++; + lport = atoi(host); + if (!lport || lport > 65535) { + log_err("fio: bad server port %u\n", port); + return 1; + } + /* no hostname given, we are done */ + *port = lport; + return 0; + } + + /* + * If no port seen yet, check if there's a last ':' at the end + */ + if (!lport) { + portp = strchr(host, ':'); + if (portp) { + *portp = '\0'; + portp++; + lport = atoi(portp); + if (!lport || lport > 65535) { + log_err("fio: bad server port %u\n", port); + return 1; + } + } + } + + if (lport) + *port = lport; + + *ptr = strdup(host); + + if (inet_aton(host, inp) != 1) { + struct hostent *hent; + + hent = gethostbyname(host); + if (!hent) { + free(*ptr); + *ptr = NULL; + return 1; + } + + memcpy(inp, hent->h_addr, 4); + } } + if (*port == 0) + *port = fio_net_port; + + return 0; +} + +/* + * Server arg should be one of: + * + * sock:/path/to/socket + * ip:1.2.3.4 + * 1.2.3.4 + * + * Where sock uses unix domain sockets, and ip binds the server to + * a specific interface. If no arguments are given to the server, it + * uses IP and binds to 0.0.0.0. + * + */ +static int fio_handle_server_arg(void) +{ + int port = fio_net_port; + int is_sock, ret = 0; + + saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); + + if (!fio_server_arg) + goto out; + + ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, + &port, &saddr_in.sin_addr); + + if (!is_sock && bind_sock) { + free(bind_sock); + bind_sock = NULL; + } + +out: + fio_net_port = port; + saddr_in.sin_port = htons(port); + return ret; +} + +static int fio_server(void) +{ + int sk, ret; + + dprint(FD_NET, "starting server\n"); + + if (fio_handle_server_arg()) + return -1; + + sk = fio_init_server_connection(); + if (sk < 0) + return -1; + ret = accept_loop(sk); + close(sk); + + if (fio_server_arg) { + free(fio_server_arg); + fio_server_arg = NULL; + } + if (bind_sock) + free(bind_sock); + return ret; } @@ -726,3 +939,8 @@ int fio_start_server(int daemonize) log_syslog = 1; return fio_server(); } + +void fio_server_set_arg(const char *arg) +{ + fio_server_arg = strdup(arg); +}