From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Fernando Fernandez Mancera <ffmancera@riseup.net>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [PATCH 1/2 nft WIP] src: introduce prio_expr in chain priority
Date: Tue, 16 Jul 2019 20:06:47 +0200 [thread overview]
Message-ID: <20190716180646.ajihkibvox4nkd2c@salvia> (raw)
In-Reply-To: <20190716090812.873-2-ffmancera@riseup.net>
On Tue, Jul 16, 2019 at 11:08:12AM +0200, Fernando Fernandez Mancera wrote:
> Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
> ---
> include/rule.h | 8 ++++----
> src/evaluate.c | 29 +++++++++++++++++++----------
> src/parser_bison.y | 25 +++++++++++++++++--------
> src/rule.c | 4 ++--
> 4 files changed, 42 insertions(+), 24 deletions(-)
>
> diff --git a/include/rule.h b/include/rule.h
> index aefb24d..4d7cec8 100644
> --- a/include/rule.h
> +++ b/include/rule.h
> @@ -173,13 +173,13 @@ enum chain_flags {
> * struct prio_spec - extendend priority specification for mixed
> * textual/numerical parsing.
> *
> - * @str: name of the standard priority value
> - * @num: Numerical value. This MUST contain the parsed value of str after
> + * @prio_expr: expr of the standard priority value
> + * @num: Numerical value. This MUST contain the parsed value of prio_expr after
> * evaluation.
> */
> struct prio_spec {
> - const char *str;
> - int num;
> + struct expr *prio_expr;
Use:
struct expr *expr;
instead.
> + int num;
You could just store this in the expression, no need for this num
field.
> struct location loc;
> };
>
> diff --git a/src/evaluate.c b/src/evaluate.c
> index 8086f75..cee65cd 100644
> --- a/src/evaluate.c
> +++ b/src/evaluate.c
> @@ -3181,15 +3181,24 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set)
> return 0;
> }
>
> -static bool evaluate_priority(struct prio_spec *prio, int family, int hook)
> +static bool evaluate_priority(struct eval_ctx *ctx, struct prio_spec *prio,
> + int family, int hook)
> {
> int priority;
> + char prio_str[NFT_NAME_MAXLEN];
>
> /* A numeric value has been used to specify priority. */
> - if (prio->str == NULL)
> + if (prio->prio_expr == NULL)
prio_expr == NULL never happens.
> return true;
>
> - priority = std_prio_lookup(prio->str, family, hook);
> + if (expr_evaluate(ctx, &prio->prio_expr) < 0)
> + return false;
> + if (prio->prio_expr->etype == EXPR_VALUE)
You should bail out here with expr_error() is this is not an EXPR_VALUE.
> + mpz_export_data(prio_str, prio->prio_expr->value,
> + BYTEORDER_HOST_ENDIAN,
> + NFT_NAME_MAXLEN);
Remove the branch above, expr_evalute() already deals with
transforming the symbol to value expression.
> +
> + priority = std_prio_lookup(prio_str, family, hook);
> if (priority == NF_IP_PRI_LAST)
> return false;
> prio->num += priority;
> @@ -3211,10 +3220,10 @@ static int flowtable_evaluate(struct eval_ctx *ctx, struct flowtable *ft)
> if (ft->hooknum == NF_INET_NUMHOOKS)
> return chain_error(ctx, ft, "invalid hook %s", ft->hookstr);
>
> - if (!evaluate_priority(&ft->priority, NFPROTO_NETDEV, ft->hooknum))
> + if (!evaluate_priority(ctx, &ft->priority, NFPROTO_NETDEV, ft->hooknum))
> return __stmt_binary_error(ctx, &ft->priority.loc, NULL,
Better use expr_error() here?
> - "'%s' is invalid priority.",
> - ft->priority.str);
> + "invalid priority expression %s.",
> + expr_name(ft->priority.prio_expr));
>
> if (!ft->dev_expr)
> return chain_error(ctx, ft, "Unbound flowtable not allowed (must specify devices)");
> @@ -3410,11 +3419,11 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain)
> return chain_error(ctx, chain, "invalid hook %s",
> chain->hookstr);
>
> - if (!evaluate_priority(&chain->priority, chain->handle.family,
> - chain->hooknum))
> + if (!evaluate_priority(ctx, &chain->priority,
> + chain->handle.family, chain->hooknum))
> return __stmt_binary_error(ctx, &chain->priority.loc, NULL,
> - "'%s' is invalid priority in this context.",
> - chain->priority.str);
> + "invalid priority expression %s in this context.",
> + expr_name(chain->priority.prio_expr));
> }
>
> list_for_each_entry(rule, &chain->rules, list) {
> diff --git a/src/parser_bison.y b/src/parser_bison.y
> index a4905f2..c6a43cf 100644
> --- a/src/parser_bison.y
> +++ b/src/parser_bison.y
> @@ -626,8 +626,8 @@ int nft_lex(void *, void *, void *);
> %type <stmt> meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc
> %destructor { stmt_free($$); } meter_stmt meter_stmt_alloc flow_stmt_legacy_alloc
>
> -%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr
> -%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr
> +%type <expr> symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr
> +%destructor { expr_free($$); } symbol_expr verdict_expr integer_expr variable_expr chain_expr prio_expr
> %type <expr> primary_expr shift_expr and_expr
> %destructor { expr_free($$); } primary_expr shift_expr and_expr
> %type <expr> exclusive_or_expr inclusive_or_expr
> @@ -1926,30 +1926,39 @@ extended_prio_name : OUT
> | STRING
> ;
>
> +prio_expr : extended_prio_name
> + {
> + $$ = constant_expr_alloc(&@$, &string_type,
> + BYTEORDER_HOST_ENDIAN,
> + NFT_NAME_MAXLEN *
this should be strlen($1) * BITS_PER_BYTE
> + BITS_PER_BYTE, $1);
> + }
> + ;
next prev parent reply other threads:[~2019-07-16 18:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-16 9:08 [PATCH 0/2 nft WIP] Using variables in chain priority Fernando Fernandez Mancera
2019-07-16 9:08 ` [PATCH 1/2 nft WIP] src: introduce prio_expr " Fernando Fernandez Mancera
2019-07-16 18:06 ` Pablo Neira Ayuso [this message]
2019-07-16 22:42 ` Fernando Fernandez Mancera
2019-07-16 9:08 ` [PATCH 2/2 nft WIP] src: allow variables " Fernando Fernandez Mancera
2019-07-16 18:07 ` Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190716180646.ajihkibvox4nkd2c@salvia \
--to=pablo@netfilter.org \
--cc=ffmancera@riseup.net \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).