From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wj0-f176.google.com (mail-wj0-f176.google.com [209.85.210.176]) by dpdk.org (Postfix) with ESMTP id 8C945FBDF for ; Tue, 20 Dec 2016 19:43:13 +0100 (CET) Received: by mail-wj0-f176.google.com with SMTP id xy5so183358159wjc.0 for ; Tue, 20 Dec 2016 10:43:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references; bh=g2zXB5jbb8UiXSYcePIrBYi46x+wxKDDwet+V5BJVII=; b=uXGAZSXsbA7rjOkk90bjWZx3HMj+85kfFrS65k6Li1oX8nzddl433GOLOJRt7nd05y oO/NqKZSx73YSdMdu08sg7k7DubzRt92/kkNma26+0GfE6rKVyWOCELgrHxeug2HNGPa dbUIgRFvX0kIA4SZNScVZrQds2sMQ7MWED0GCQcSySqfFqn/PGlRkDYIkIlgALaTor7z f4dedRpocO3U8jET0yLtXKEHOokrPn2IY0N/QfS8XWPbStEJL+LBTNPmtujCJ35P5JnO NAW6IiHnjjrB9zYT01aV1Oc0xPI3q81dmhwq1+CI+OzslCGrX26cxsWsmY1AuP/s8BeV q6eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=g2zXB5jbb8UiXSYcePIrBYi46x+wxKDDwet+V5BJVII=; b=NZJjan2Elel9wHocqIT7pmLQW7TnbFKHJQ5JDjQ0lpiExPtW1o7XJ56EeU5yUYT/eJ A99yYcueW9q+waQyn8QSwVS+Fw0/O+cOUqBfUqRSKC0ccOor95LbNWA50TIvwA2Bz4tJ FGSlPsRIm+DJnTmNZ4kB0o1zmXxGZaoPzwTzbipo6UKOElJG7ZtM45FJ+87RykAeYB3b u1AXklV1CJOl00l3VJal/jNDjDUvOHH1V6wiC3iv/dG+PGd4sMfjHOIGENkPKuZ0jFGZ vYuUI3NSqKSEseJYil0u+ZndAGkhLyZuXBpTL8pleokRLD2ot+FsSRn6hQ8LgXgWpnVU qYZQ== X-Gm-Message-State: AIkVDXKJpnxqlH0sc7DuOtGLG4/etk04gIDbx9MPQK2uBgb3c791WZ5cKJ7qmwXLOk8a9hYG X-Received: by 10.194.43.73 with SMTP id u9mr611912wjl.109.1482259392862; Tue, 20 Dec 2016 10:43:12 -0800 (PST) Received: from 6wind.com (guy78-3-82-239-227-177.fbx.proxad.net. [82.239.227.177]) by smtp.gmail.com with ESMTPSA id jm6sm26757900wjb.27.2016.12.20.10.43.11 for (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 20 Dec 2016 10:43:12 -0800 (PST) From: Adrien Mazarguil To: dev@dpdk.org Date: Tue, 20 Dec 2016 19:42:25 +0100 Message-Id: <1dfc0bde5a415da15c53bc1b6871c5023decf44e.1482257521.git.adrien.mazarguil@6wind.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: Subject: [dpdk-dev] [PATCH v4 08/25] app/testpmd: add rte_flow integer support X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Dec 2016 18:43:13 -0000 Parse all integer types and handle conversion to network byte order in a single function. Signed-off-by: Adrien Mazarguil Acked-by: Olga Shern --- app/test-pmd/cmdline_flow.c | 148 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/app/test-pmd/cmdline_flow.c b/app/test-pmd/cmdline_flow.c index f5aef0f..c5a4209 100644 --- a/app/test-pmd/cmdline_flow.c +++ b/app/test-pmd/cmdline_flow.c @@ -34,11 +34,14 @@ #include #include #include +#include +#include #include #include #include #include +#include #include #include @@ -50,6 +53,10 @@ enum index { ZERO = 0, END, + /* Common tokens. */ + INTEGER, + UNSIGNED, + /* Top-level command. */ FLOW, }; @@ -61,12 +68,24 @@ enum index { struct context { /** Stack of subsequent token lists to process. */ const enum index *next[CTX_STACK_SIZE]; + /** Arguments for stacked tokens. */ + const void *args[CTX_STACK_SIZE]; enum index curr; /**< Current token index. */ enum index prev; /**< Index of the last token seen. */ int next_num; /**< Number of entries in next[]. */ + int args_num; /**< Number of entries in args[]. */ uint32_t reparse:1; /**< Start over from the beginning. */ uint32_t eol:1; /**< EOL has been detected. */ uint32_t last:1; /**< No more arguments. */ + void *object; /**< Address of current object for relative offsets. */ +}; + +/** Token argument. */ +struct arg { + uint32_t hton:1; /**< Use network byte ordering. */ + uint32_t sign:1; /**< Value is signed. */ + uint32_t offset; /**< Relative offset from ctx->object. */ + uint32_t size; /**< Field size. */ }; /** Parser token definition. */ @@ -80,6 +99,8 @@ struct token { * parser consumes the last entry of that stack. */ const enum index *const *next; + /** Arguments stack for subsequent tokens that need them. */ + const struct arg *const *args; /** * Token-processing callback, returns -1 in case of error, the * length of the matched string otherwise. If NULL, attempts to @@ -112,6 +133,22 @@ struct token { /** Static initializer for a NEXT() entry. */ #define NEXT_ENTRY(...) (const enum index []){ __VA_ARGS__, ZERO, } +/** Static initializer for the args field. */ +#define ARGS(...) (const struct arg *const []){ __VA_ARGS__, NULL, } + +/** Static initializer for ARGS() to target a field. */ +#define ARGS_ENTRY(s, f) \ + (&(const struct arg){ \ + .offset = offsetof(s, f), \ + .size = sizeof(((s *)0)->f), \ + }) + +/** Static initializer for ARGS() to target a pointer. */ +#define ARGS_ENTRY_PTR(s, f) \ + (&(const struct arg){ \ + .size = sizeof(*((s *)0)->f), \ + }) + /** Parser output buffer layout expected by cmd_flow_parsed(). */ struct buffer { enum index command; /**< Flow command. */ @@ -121,6 +158,11 @@ struct buffer { static int parse_init(struct context *, const struct token *, const char *, unsigned int, void *, unsigned int); +static int parse_int(struct context *, const struct token *, + const char *, unsigned int, + void *, unsigned int); +static int comp_none(struct context *, const struct token *, + unsigned int, char *, unsigned int); /** Token definitions. */ static const struct token token_list[] = { @@ -135,6 +177,21 @@ static const struct token token_list[] = { .type = "RETURN", .help = "command may end here", }, + /* Common tokens. */ + [INTEGER] = { + .name = "{int}", + .type = "INTEGER", + .help = "integer value", + .call = parse_int, + .comp = comp_none, + }, + [UNSIGNED] = { + .name = "{unsigned}", + .type = "UNSIGNED", + .help = "unsigned integer value", + .call = parse_int, + .comp = comp_none, + }, /* Top-level command. */ [FLOW] = { .name = "flow", @@ -144,6 +201,23 @@ static const struct token token_list[] = { }, }; +/** Remove and return last entry from argument stack. */ +static const struct arg * +pop_args(struct context *ctx) +{ + return ctx->args_num ? ctx->args[--ctx->args_num] : NULL; +} + +/** Add entry on top of the argument stack. */ +static int +push_args(struct context *ctx, const struct arg *arg) +{ + if (ctx->args_num == CTX_STACK_SIZE) + return -1; + ctx->args[ctx->args_num++] = arg; + return 0; +} + /** Default parsing function for token name matching. */ static int parse_default(struct context *ctx, const struct token *token, @@ -178,9 +252,74 @@ parse_init(struct context *ctx, const struct token *token, /* Initialize buffer. */ memset(out, 0x00, sizeof(*out)); memset((uint8_t *)out + sizeof(*out), 0x22, size - sizeof(*out)); + ctx->object = out; return len; } +/** + * Parse signed/unsigned integers 8 to 64-bit long. + * + * Last argument (ctx->args) is retrieved to determine integer type and + * storage location. + */ +static int +parse_int(struct context *ctx, const struct token *token, + const char *str, unsigned int len, + void *buf, unsigned int size) +{ + const struct arg *arg = pop_args(ctx); + uintmax_t u; + char *end; + + (void)token; + /* Argument is expected. */ + if (!arg) + return -1; + errno = 0; + u = arg->sign ? + (uintmax_t)strtoimax(str, &end, 0) : + strtoumax(str, &end, 0); + if (errno || (size_t)(end - str) != len) + goto error; + if (!ctx->object) + return len; + buf = (uint8_t *)ctx->object + arg->offset; + size = arg->size; + switch (size) { + case sizeof(uint8_t): + *(uint8_t *)buf = u; + break; + case sizeof(uint16_t): + *(uint16_t *)buf = arg->hton ? rte_cpu_to_be_16(u) : u; + break; + case sizeof(uint32_t): + *(uint32_t *)buf = arg->hton ? rte_cpu_to_be_32(u) : u; + break; + case sizeof(uint64_t): + *(uint64_t *)buf = arg->hton ? rte_cpu_to_be_64(u) : u; + break; + default: + goto error; + } + return len; +error: + push_args(ctx, arg); + return -1; +} + +/** No completion. */ +static int +comp_none(struct context *ctx, const struct token *token, + unsigned int ent, char *buf, unsigned int size) +{ + (void)ctx; + (void)token; + (void)ent; + (void)buf; + (void)size; + return 0; +} + /** Internal context. */ static struct context cmd_flow_context; @@ -195,9 +334,11 @@ cmd_flow_context_init(struct context *ctx) ctx->curr = ZERO; ctx->prev = ZERO; ctx->next_num = 0; + ctx->args_num = 0; ctx->reparse = 0; ctx->eol = 0; ctx->last = 0; + ctx->object = NULL; } /** Parse a token (cmdline API). */ @@ -270,6 +411,13 @@ cmd_flow_parse(cmdline_parse_token_hdr_t *hdr, const char *src, void *result, return -1; ctx->next[ctx->next_num++] = token->next[i]; } + /* Push arguments if any. */ + if (token->args) + for (i = 0; token->args[i]; ++i) { + if (ctx->args_num == RTE_DIM(ctx->args)) + return -1; + ctx->args[ctx->args_num++] = token->args[i]; + } return len; } -- 2.1.4