From patchwork Wed Nov 16 16:23:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrien Mazarguil X-Patchwork-Id: 17030 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 135666893; Wed, 16 Nov 2016 17:25:14 +0100 (CET) Received: from mail-wm0-f54.google.com (mail-wm0-f54.google.com [74.125.82.54]) by dpdk.org (Postfix) with ESMTP id B04CC559C for ; Wed, 16 Nov 2016 17:24:18 +0100 (CET) Received: by mail-wm0-f54.google.com with SMTP id g23so250203874wme.1 for ; Wed, 16 Nov 2016 08:24:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=dqVwkd07/r6AH35n7NznZUfr1PVdVoaaKqQC4RSJy4s=; b=YxtrdpOKHRkJFhQDdNupQrmC0YaeD1IRDd9UKbtcSlB4gQySu4gYGHra0MkeNDBDhT oK6RP0KEb4iy/6NT/HQjM/qmd/I2AHr09wai3iACfRA0Hqyzwm6/Rj56S0elesxwGOPL azOP+ovnSi15o4hAeT5YDVy0i7zio63mFLXMiNgCyF03JX+4GP2Lz/CSZH2IALLYK9dP et1r2qoA1QPZDEPz1+YxejAMSGdZeUsO2Mw5KbHIEl181yoDHBlv6jcgdxBBTfxNiMx4 +q7xjQhG0ZOaptZrm9pOSpfFQHUSVDhAgZMNvN8pPghbeNrPAdD2AcH6M0w0PwZroQt9 +BDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dqVwkd07/r6AH35n7NznZUfr1PVdVoaaKqQC4RSJy4s=; b=Eqju8igFWQVcE4LWhPmDFXl2HiD5huhRWBDJEjuDcTbAYP+FXj7HISI3MnZqD5DSab oBMt5Yjw2/jomO39TlALGaRMEWCAHkKwibvZa3NPHU/4kgTj9r8QoUmvT4b5QChbXoOt KP+D9LUGUvyEuljrxBNoon6u6H86WKlOQIZTyrutnSdCgIW/ysZTsffNPrTqQrUcnexG p/2xRIATY6csT1BZ4gW8JpeoKLXX9zIaEkwftXls/il9+IU/GEWV1rnnKpwhWX6lQjoF pxwN4HQJyIbF7cZQFUvaadDY9ctRJhXTLNSk6OVFKXm/ZAcP6m4usmg95at2VxTCzD3r 0zHA== X-Gm-Message-State: ABUngvcpnPq3u4HBVeOcgLEaSjGtGFiYkDgsQqUkBGHTHLFcPztoK0c3JoVjxcSfv9Dcn4WF X-Received: by 10.194.115.226 with SMTP id jr2mr2491594wjb.75.1479313458086; Wed, 16 Nov 2016 08:24:18 -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 gk6sm41331064wjc.21.2016.11.16.08.24.16 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Wed, 16 Nov 2016 08:24:17 -0800 (PST) From: Adrien Mazarguil To: dev@dpdk.org Cc: Thomas Monjalon , Pablo de Lara , Olivier Matz Date: Wed, 16 Nov 2016 17:23:32 +0100 Message-Id: X-Mailer: git-send-email 2.1.4 In-Reply-To: References: Subject: [dpdk-dev] [PATCH 06/22] app/testpmd: add rte_flow integer support X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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 7dbda84..7078f80 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__, 0, } +/** 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; }