X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=server.c;h=38698bc3a126f8df9979a3c8af562f844c18c3d0;hp=a70dc14f414aefad94812840b20b2c326e6b76cf;hb=a37f69b72a74cbde6151458b890aab8d093f0c9f;hpb=a781480e959e4cad32f27915bdb5dab948aa1c6e diff --git a/server.c b/server.c index a70dc14f..38698bc3 100644 --- a/server.c +++ b/server.c @@ -15,7 +15,7 @@ #include "fio.h" #include "server.h" -#include "crc/crc32.h" +#include "crc/crc16.h" int fio_net_port = 8765; @@ -29,6 +29,8 @@ static int server_fd; int fio_send_data(int sk, const void *p, unsigned int len) { + assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU); + do { int ret = send(sk, p, len, 0); @@ -75,15 +77,15 @@ int fio_recv_data(int sk, void *p, unsigned int len) static int verify_convert_cmd(struct fio_net_cmd *cmd) { - uint32_t crc; + uint16_t crc; - cmd->cmd_crc32 = le32_to_cpu(cmd->cmd_crc32); - cmd->pdu_crc32 = le32_to_cpu(cmd->pdu_crc32); + cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); + cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); - crc = crc32(cmd, sizeof(*cmd) - 2 * sizeof(uint32_t)); - if (crc != cmd->cmd_crc32) { + crc = crc16(cmd, FIO_NET_CMD_CRC_SZ); + if (crc != cmd->cmd_crc16) { log_err("fio: server bad crc on command (got %x, wanted %x)\n", - cmd->cmd_crc32, crc); + cmd->cmd_crc16, crc); return 1; } @@ -112,7 +114,7 @@ static int verify_convert_cmd(struct fio_net_cmd *cmd) struct fio_net_cmd *fio_net_cmd_read(int sk) { struct fio_net_cmd cmd, *ret = NULL; - uint32_t crc; + uint16_t crc; if (fio_recv_data(sk, &cmd, sizeof(cmd))) return NULL; @@ -135,10 +137,10 @@ struct fio_net_cmd *fio_net_cmd_read(int sk) } /* Verify payload crc */ - crc = crc32(ret->payload, ret->pdu_len); - if (crc != ret->pdu_crc32) { + crc = crc16(ret->payload, ret->pdu_len); + if (crc != ret->pdu_crc16) { log_err("fio: server bad crc on payload (got %x, wanted %x)\n", - ret->pdu_crc32, crc); + ret->pdu_crc16, crc); free(ret); return NULL; } @@ -150,12 +152,40 @@ void fio_net_cmd_crc(struct fio_net_cmd *cmd) { uint32_t pdu_len; - cmd->cmd_crc32 = cpu_to_le32(crc32(cmd, - sizeof(*cmd) - 2 * sizeof(uint32_t))); + cmd->cmd_crc16 = cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ)); pdu_len = le32_to_cpu(cmd->pdu_len); if (pdu_len) - cmd->pdu_crc32 = cpu_to_le32(crc32(cmd->payload, pdu_len)); + cmd->pdu_crc16 = cpu_to_le16(crc16(cmd->payload, pdu_len)); +} + +int fio_net_send_cmd(int fd, uint16_t opcode, const char *buf, off_t size) +{ + struct fio_net_cmd *cmd; + size_t this_len; + int ret; + + do { + this_len = size; + if (this_len > FIO_SERVER_MAX_PDU) + this_len = FIO_SERVER_MAX_PDU; + + cmd = malloc(sizeof(*cmd) + this_len); + + fio_init_net_cmd(cmd, opcode, buf, this_len); + + if (this_len < size) + cmd->flags |= FIO_NET_CMD_F_MORE; + + fio_net_cmd_crc(cmd); + + ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); + free(cmd); + size -= this_len; + buf += this_len; + } while (!ret && size); + + return ret; } static int send_simple_command(int sk, uint16_t opcode, uint64_t serial) @@ -163,7 +193,7 @@ static int send_simple_command(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_le16(serial), + .serial = cpu_to_le64(serial), }; fio_net_cmd_crc(&cmd); @@ -176,7 +206,11 @@ static int send_simple_command(int sk, uint16_t opcode, uint64_t serial) */ static int ack_command(int sk, struct fio_net_cmd *cmd) { +#if 0 return send_simple_command(sk, FIO_NET_CMD_ACK, cmd->serial); +#else + return 0; +#endif } #if 0 @@ -186,7 +220,12 @@ static int nak_command(int sk, struct fio_net_cmd *cmd) } #endif -static int handle_cur_job(struct fio_net_cmd *cmd, int done) +static int send_quit_command(void) +{ + return send_simple_command(server_fd, FIO_NET_CMD_QUIT, 0); +} + +static int handle_cur_job(struct fio_net_cmd *cmd) { unsigned int left = job_max_len - job_cur_len; int ret = 0; @@ -199,15 +238,19 @@ static int handle_cur_job(struct fio_net_cmd *cmd, int done) memcpy(job_buf + job_cur_len, cmd->payload, cmd->pdu_len); job_cur_len += cmd->pdu_len; - if (done) { - parse_jobs_ini(job_buf, 1, 0); - ret = exec_run(); - reset_fio_state(); - free(job_buf); - job_buf = NULL; - job_cur_len = job_max_len = 0; - } + /* + * More data coming for this job + */ + if (cmd->flags & FIO_NET_CMD_F_MORE) + return 0; + parse_jobs_ini(job_buf, 1, 0); + ret = exec_run(); + send_quit_command(); + reset_fio_state(); + free(job_buf); + job_buf = NULL; + job_cur_len = job_max_len = 0; return ret; } @@ -224,10 +267,7 @@ static int handle_command(struct fio_net_cmd *cmd) case FIO_NET_CMD_NAK: return 1; case FIO_NET_CMD_JOB: - ret = handle_cur_job(cmd, 0); - break; - case FIO_NET_CMD_JOB_END: - ret = handle_cur_job(cmd, 1); + ret = handle_cur_job(cmd); break; default: log_err("fio: unknown opcode: %d\n", cmd->opcode); @@ -285,7 +325,7 @@ again: if (ret < 0) { if (errno == EINTR) break; - perror("poll"); + log_err("fio: poll: %s\n", strerror(errno)); goto out; } else if (!ret) continue; @@ -369,30 +409,18 @@ int fio_server(void) int fio_server_text_output(const char *buf, unsigned int len) { - struct fio_net_cmd *cmd; - int size = sizeof(*cmd) + len; - - cmd = malloc(size); - fio_init_net_cmd(cmd); - cmd->opcode = cpu_to_le16(FIO_NET_CMD_TEXT); - cmd->pdu_len = cpu_to_le32(len); - memcpy(&cmd->payload, buf, len); - - fio_net_cmd_crc(cmd); - - fio_send_data(server_fd, cmd, size); - free(cmd); - return size; + return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len); } int fio_server_log(const char *format, ...) { char buffer[1024]; va_list args; + size_t len; va_start(args, format); - snprintf(buffer, sizeof(buffer), format, args); + len = vsnprintf(buffer, sizeof(buffer), format, args); va_end(args); - return fio_server_text_output(buffer, strlen(buffer)); + return fio_server_text_output(buffer, len); }