From fa2ea806de0d6410320abd97599bc52f5a3e72cc Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Sat, 8 Oct 2011 21:07:29 +0200 Subject: [PATCH] Add protocol support for an arbitrary number of command line arguments Make it more efficient as well, don't pass a lot of potentially padded space, pass only the exact amount required. Signed-off-by: Jens Axboe --- client.c | 53 +++++++++++++++++++++++++++++++++++------------------ init.c | 10 +++------- server.c | 28 ++++++++++++++++++++-------- server.h | 18 ++++++++++-------- 4 files changed, 68 insertions(+), 41 deletions(-) diff --git a/client.c b/client.c index 8f8dc90a..874f0aec 100644 --- a/client.c +++ b/client.c @@ -111,32 +111,25 @@ static void remove_client(struct fio_client *client) nr_clients--; } -static int __fio_client_add_cmd_option(struct fio_client *client, - const char *opt) +static void __fio_client_add_cmd_option(struct fio_client *client, + const char *opt) { int index; - if (client->argc == FIO_NET_CMD_JOBLINE_ARGV) { - log_err("fio: max cmd line number reached.\n"); - log_err("fio: cmd line <%s> has been ignored.\n", opt); - return 1; - } - index = client->argc++; client->argv = realloc(client->argv, sizeof(char *) * client->argc); client->argv[index] = strdup(opt); dprint(FD_NET, "client: add cmd %d: %s\n", index, opt); - return 0; } -int fio_client_add_cmd_option(void *cookie, const char *opt) +void fio_client_add_cmd_option(void *cookie, const char *opt) { struct fio_client *client = cookie; if (!client || !opt) - return 0; + return; - return __fio_client_add_cmd_option(client, opt); + __fio_client_add_cmd_option(client, opt); } int fio_client_add(const char *hostname, void **cookie) @@ -280,17 +273,41 @@ static void probe_client(struct fio_client *client) static int send_client_cmd_line(struct fio_client *client) { - struct cmd_line_pdu *pdu; + struct cmd_single_line_pdu *cslp; + struct cmd_line_pdu *clp; + unsigned long offset; + void *pdu; + size_t mem; int i, ret; dprint(FD_NET, "client: send cmdline %d\n", client->argc); - pdu = malloc(sizeof(*pdu)); - for (i = 0; i < client->argc; i++) - strcpy((char *) pdu->argv[i], client->argv[i]); + /* + * Find out how much mem we need + */ + for (i = 0, mem = 0; i < client->argc; i++) + mem += strlen(client->argv[i]) + 1; + + /* + * We need one cmd_line_pdu, and argc number of cmd_single_line_pdu + */ + mem += sizeof(*clp) + (client->argc * sizeof(*cslp)); + + pdu = malloc(mem); + clp = pdu; + offset = sizeof(*clp); + + for (i = 0; i < client->argc; i++) { + uint16_t arg_len = strlen(client->argv[i]) + 1; + + cslp = pdu + offset; + strcpy((char *) cslp->text, client->argv[i]); + cslp->len = cpu_to_le16(arg_len); + offset += sizeof(*cslp) + arg_len; + } - pdu->argc = cpu_to_le16(client->argc); - ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, sizeof(*pdu)); + clp->lines = cpu_to_le16(client->argc); + ret = fio_net_send_cmd(client->fd, FIO_NET_CMD_JOBLINE, pdu, mem); free(pdu); return ret; } diff --git a/init.c b/init.c index e3ff39c0..e2f7baba 100644 --- a/init.c +++ b/init.c @@ -1243,9 +1243,9 @@ static int client_flag_set(char c) return 0; } -int parse_cmd_client(void *client, char *opt) +void parse_cmd_client(void *client, char *opt) { - return fio_client_add_cmd_option(client, opt); + fio_client_add_cmd_option(client, opt); } int parse_cmd_line(int argc, char *argv[]) @@ -1267,11 +1267,7 @@ int parse_cmd_line(int argc, char *argv[]) did_arg = 1; if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) { - if (parse_cmd_client(cur_client, argv[optind - 1])) { - exit_val = 1; - do_exit++; - break; - } + parse_cmd_client(cur_client, argv[optind - 1]); c &= ~FIO_CLIENT_FLAG; } diff --git a/server.c b/server.c index f9c3c579..a54db7bf 100644 --- a/server.c +++ b/server.c @@ -102,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_VER3: + case FIO_SERVER_VER: break; default: log_err("fio: bad server cmd version %d\n", cmd->version); @@ -270,24 +270,36 @@ 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", pdu->argc); + dprint(FD_NET, "server: %d command line args\n", clp->lines); - for (i = 0; i < pdu->argc; i++) { - argv[i] = (char *) pdu->argv[i]; + for (i = 0; i < clp->lines; i++) { + cslp = pdu + offset; + argv[i] = (char *) cslp->text; + + 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); ret = exec_run(); diff --git a/server.h b/server.h index c6e9ce21..d68cbf52 100644 --- a/server.h +++ b/server.h @@ -26,8 +26,7 @@ struct fio_net_cmd { }; enum { - FIO_SERVER_VER = 3, - FIO_SERVER_VER3 = 3, + FIO_SERVER_VER = 4, FIO_SERVER_MAX_PDU = 1024, @@ -48,8 +47,6 @@ enum { /* crc does not include the crc fields */ FIO_NET_CMD_CRC_SZ = sizeof(struct fio_net_cmd) - 2 * sizeof(uint16_t), - - FIO_NET_CMD_JOBLINE_ARGV = 128, }; struct cmd_ts_pdu { @@ -67,9 +64,14 @@ struct cmd_probe_pdu { uint8_t arch; }; +struct cmd_single_line_pdu { + uint16_t len; + uint8_t text[0]; +}; + struct cmd_line_pdu { - uint16_t argc; - uint8_t argv[FIO_NET_CMD_JOBLINE_ARGV][64]; + uint16_t lines; + struct cmd_single_line_pdu options[0]; }; extern int fio_start_server(int); @@ -91,7 +93,7 @@ extern int fio_clients_connect(void); extern int fio_clients_send_ini(const char *); extern int fio_handle_clients(void); extern int fio_client_add(const char *, void **); -extern int fio_client_add_cmd_option(void *, const char *); +extern void fio_client_add_cmd_option(void *, const char *); extern int fio_recv_data(int sk, void *p, unsigned int len); extern int fio_send_data(int sk, const void *p, unsigned int len); @@ -106,7 +108,7 @@ static inline void fio_init_net_cmd(struct fio_net_cmd *cmd, uint16_t opcode, { memset(cmd, 0, sizeof(*cmd)); - cmd->version = __cpu_to_le16(FIO_SERVER_VER3); + cmd->version = __cpu_to_le16(FIO_SERVER_VER); cmd->opcode = cpu_to_le16(opcode); if (pdu) { -- 2.25.1