From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH nft v2 04/11] src: parser: add syntax to provide size of variable-sized data types
Date: Fri, 13 Dec 2019 17:03:38 +0100 [thread overview]
Message-ID: <20191213160345.30057-5-fw@strlen.de> (raw)
In-Reply-To: <20191213160345.30057-1-fw@strlen.de>
This allows creation of sets with string and integer types by
providing the datatype width using a comma, e.g.
"type string, 64" or "integer, 32".
This is mainly intended as a fallback for the upcoming "typeof"
keyword -- if we can't make sense of the kernel provided type
(or its missing entirely), we can then fallback to this format.
Signed-off-by: Florian Westphal <fw@strlen.de>
---
src/parser_bison.y | 18 ++++++++++++++++++
src/rule.c | 24 +++++++++++++++++++-----
2 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/src/parser_bison.y b/src/parser_bison.y
index 6d17539fa57e..c6cad19f52fb 100644
--- a/src/parser_bison.y
+++ b/src/parser_bison.y
@@ -1891,6 +1891,24 @@ data_type_atom_expr : type_identifier
$$ = constant_expr_alloc(&@1, &time_type, time_type.byteorder,
time_type.size, NULL);
}
+ | type_identifier COMMA NUM
+ {
+ const struct datatype *dtype = datatype_lookup_byname($1);
+ if (dtype == NULL) {
+ erec_queue(error(&@1, "unknown datatype %s", $1),
+ state->msgs);
+ YYERROR;
+ }
+
+ if (dtype->size) {
+ erec_queue(error(&@1, "Datatype %s has a fixed type", $1),
+ state->msgs);
+ YYERROR;
+ }
+ $$ = constant_expr_alloc(&@1, dtype, dtype->byteorder,
+ $3, NULL);
+ xfree($1);
+ }
;
data_type_expr : data_type_atom_expr
diff --git a/src/rule.c b/src/rule.c
index f8cd4a73054b..a94860865f31 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -438,6 +438,16 @@ const char *set_policy2str(uint32_t policy)
}
}
+static void set_print_key(const struct expr *expr, struct output_ctx *octx)
+{
+ const struct datatype *dtype = expr->dtype;
+
+ if (dtype->size || dtype->type == TYPE_VERDICT)
+ nft_print(octx, "%s", dtype->name);
+ else
+ nft_print(octx, "%s,%d", dtype->name, expr->len);
+}
+
static void set_print_declaration(const struct set *set,
struct print_fmt_options *opts,
struct output_ctx *octx)
@@ -466,12 +476,16 @@ static void set_print_declaration(const struct set *set,
if (nft_output_handle(octx))
nft_print(octx, " # handle %" PRIu64, set->handle.handle.id);
nft_print(octx, "%s", opts->nl);
- nft_print(octx, "%s%stype %s",
- opts->tab, opts->tab, set->key->dtype->name);
- if (set_is_datamap(set->flags))
- nft_print(octx, " : %s", set->data->dtype->name);
- else if (set_is_objmap(set->flags))
+ nft_print(octx, "%s%stype ",
+ opts->tab, opts->tab);
+ set_print_key(set->key, octx);
+
+ if (set_is_datamap(set->flags)) {
+ nft_print(octx, " : ");
+ set_print_key(set->data, octx);
+ } else if (set_is_objmap(set->flags)) {
nft_print(octx, " : %s", obj_type_name(set->objtype));
+ }
nft_print(octx, "%s", opts->stmt_separator);
--
2.23.0
next prev parent reply other threads:[~2019-12-13 20:38 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-13 16:03 [PATCH nft v2 00/10] add typeof keyword Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 01/11] parser: add a helper for concat expression handling Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 02/11] libnftnl: split nft_ctx_new/free Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 03/11] src: store expr, not dtype to track data in sets Florian Westphal
2019-12-13 16:03 ` Florian Westphal [this message]
2019-12-13 16:03 ` [PATCH nft v2 05/11] parser: add typeof keyword for declarations Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 06/11] src: add "typeof" print support Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 07/11] mnl: round up the map data size too Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 08/11] src: netlink: remove assertion Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 09/11] evaluate: print a hint about 'type,width' syntax on 0 keylen Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 10/11] doc: mention 'typeof' as alternative to 'type' keyword Florian Westphal
2019-12-13 16:03 ` [PATCH nft v2 11/11] tests: add typeof test cases Florian Westphal
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=20191213160345.30057-5-fw@strlen.de \
--to=fw@strlen.de \
--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