From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f47.google.com (mail-wm0-f47.google.com [74.125.82.47]) by dpdk.org (Postfix) with ESMTP id 9716BF91D for ; Fri, 16 Dec 2016 17:25:53 +0100 (CET) Received: by mail-wm0-f47.google.com with SMTP id a197so38673159wmd.0 for ; Fri, 16 Dec 2016 08:25:53 -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=4VZ1/VfsOeSlBV9fl2lmcf/Cy6F+rBU3EXC0UW3OSho=; b=k8i/v/HhAXOCqnhGa39mn+3JLFN/w+rZDoJt60koSEeNOAaSHPKlzUYvKzk2VIh7lZ I2DXzhGBnwRjHnuDGWge9mzDWRsMP02B/GE++HtNQ3h27QTthE1cc1jhUp4SarpfwTBz pLRfFeDOmAyekYbB3X0eOKEy4qV12qpjExXRt20iDPb65vDtLwKG08j42Khg3v0hcWxR f0uRvYr8/47iEVLXkfHe286zv/IUiEYjQAcTCt56KfloLi9CGEK7VBA9rl3LRvn3v7mx pe9VqUzNNU/94qU6y5g9sc+Ve+mT1tQFvoNBcXmABLLlxHe6leoOygK7rgLVP/kDV9/C OAWg== 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=4VZ1/VfsOeSlBV9fl2lmcf/Cy6F+rBU3EXC0UW3OSho=; b=bfVVb/HUsplGOtP65oZaYdbBhrWm+qUiRdSLt51JqtYo/bacT38g8HvAQLLt/jmqMU 7zVqyaRiWiPmpZb3Jp25bS5f6i/f6GGWD/uty9373qqDmE0+Ev3PqpE/f6q3f6yMGT5J QwJ+1q8Nez4tZxW7P7+bQstYgEOsRmDgobeIJWd6s7syNqn6N6EIaXWQSApfXq+KIsEm pYXb8PCkxiL13y1IjuA7dbMG6J82esCK+1ItW6USRTU0TpcNtjR5eFapiisDs0+yhPbm LHF/HVYmCHvPA9iRFZq/pT+cDrRPPTSDhvudlhSXLF3ffXKwfU8ygKHJ4+HSUcjlozxT 1mCg== X-Gm-Message-State: AIkVDXLzuUBL5EVRuRYLBRyCPoBuaPOW/MpXalH7hdxlDtTKVRKwkcV39l9ymyktEKZTMK+P X-Received: by 10.28.136.198 with SMTP id k189mr3379187wmd.24.1481905553015; Fri, 16 Dec 2016 08:25:53 -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 135sm4031512wmh.14.2016.12.16.08.25.51 for (version=TLS1_2 cipher=AES128-SHA bits=128/128); Fri, 16 Dec 2016 08:25:52 -0800 (PST) From: Adrien Mazarguil To: dev@dpdk.org Date: Fri, 16 Dec 2016 17:25:05 +0100 Message-Id: <9f99a4dd42f8ff38116b7e38042b1205333c6009.1481903839.git.adrien.mazarguil@6wind.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: References: Subject: [dpdk-dev] [PATCH v2 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: Fri, 16 Dec 2016 16:25:53 -0000 Parse all integer types and handle conversion to network byte order in a single function. Signed-off-by: Adrien Mazarguil --- 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 a36da06..81281f9 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 = 0; ctx->prev = 0; 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