src: add support for multi-statement in dynamic sets and maps
Pablo Neira Ayuso
maintainerPATCH v2 1/2 src: add support for multi-statement in dynamic sets and maps
This patch allows for two statements for dynamic set updates, e.g.
nft rule x y add @y { ip daddr limit rate 1/second counter }
Signed-off-by Pablo Neira Ayuso <[email protected]>
---
v2: fix memleaks.
fix crashes reported by tests/shell.
include/statement.h | 4 +-- src/evaluate.c | 24 +++++++------ src/netlink.c | 1 + src/netlink_delinearize.c | 74 +++++++++++++++++++++++++++++++-------- src/netlink_linearize.c | 41 +++++++++++++++++----- src/parser_bison.y | 27 ++++++++++---- src/statement.c | 34 +++++++++++++----- 7 files changed, 157 insertions(+), 48 deletions(-)
include/statement.h +2 -2
@@ -201,7 +201,7 @@ uint32_t fwd_stmt_type(const char *type);struct set_stmt {struct expr *set;struct expr *key; -struct stmt *stmt; +struct list_head stmt_list;enum nft_dynset_ops op; };
@@ -213,7 +213,7 @@ struct map_stmt {struct expr *set;struct expr *key;struct expr *data; -struct stmt *stmt; +struct list_head stmt_list;enum nft_dynset_ops op; };
src/evaluate.c +14 -10
@@ -3370,6 +3370,8 @@ static int stmt_evaluate_log(struct eval_ctx *ctx, struct stmt *stmt)static int stmt_evaluate_set (struct eval_ctx *ctx,struct stmt *stmt) { +struct stmt *this; +expr_set_context (&ctx->ectx,NULL ,0 );if (expr_evaluate (ctx, &stmt->set.set) <0 )return -1 ;
@@ -3389,12 +3391,12 @@ static int stmt_evaluate_set(struct eval_ctx *ctx, struct stmt *stmt)if (stmt->set.key->comment !=NULL )return expr_error (ctx->msgs, stmt->set.key,"Key expression comments are not supported" ); -if (stmt->set.stmt) { -if (stmt_evaluate (ctx, stmt->set.stmt) <0 ) +list_for_each_entry (this, &stmt->set.stmt_list, list) { +if (stmt_evaluate (ctx, this) <0 )return -1 ; -if (!(stmt->set.stmt->flags &STMT_F_STATEFUL )) -return stmt_binary_error (ctx, stmt->set.stmt, stmt, -"meter statement must be stateful" ); +if (!(this->flags &STMT_F_STATEFUL )) +return stmt_error (ctx, this, +"statement must be stateful" ); }return 0 ;
@@ -3402,6 +3404,8 @@ static int stmt_evaluate_set(struct eval_ctx *ctx, struct stmt *stmt)static int stmt_evaluate_map (struct eval_ctx *ctx,struct stmt *stmt) { +struct stmt *this; +expr_set_context (&ctx->ectx,NULL ,0 );if (expr_evaluate (ctx, &stmt->map.set) <0 )return -1 ;
@@ -3435,12 +3439,12 @@ static int stmt_evaluate_map(struct eval_ctx *ctx, struct stmt *stmt)return expr_error (ctx->msgs, stmt->map.data,"Data expression comments are not supported" ); -if (stmt->map.stmt) { -if (stmt_evaluate (ctx, stmt->map.stmt) <0 ) +list_for_each_entry (this, &stmt->map.stmt_list, list) { +if (stmt_evaluate (ctx, this) <0 )return -1 ; -if (!(stmt->map.stmt->flags &STMT_F_STATEFUL )) -return stmt_binary_error (ctx, stmt->map.stmt, stmt, -"meter statement must be stateful" ); +if (!(this->flags &STMT_F_STATEFUL )) +return stmt_error (ctx, this, +"statement must be stateful" ); }return 0 ;
src/netlink.c +1 -0
@@ -1132,6 +1132,7 @@ key_end: key =bitmask_expr_to_binops (key); expr =set_elem_expr_alloc (&netlink_location, key); +if (nftnl_set_elem_is_set (nlse,NFTNL_SET_ELEM_TIMEOUT )) expr->timeout =nftnl_set_elem_get_u64 (nlse,NFTNL_SET_ELEM_TIMEOUT );if (nftnl_set_elem_is_set (nlse,NFTNL_SET_ELEM_EXPIRATION ))
src/netlink_delinearize.c +60 -14
120 lines not on this page · raw message
src/netlink_linearize.c +33 -8
68 lines not on this page · raw message
src/parser_bison.y +21 -6
@@ -624,8 +624,8 @@ int nft_lex(void *, void *, void *); %type <obj> obj_block_alloc counter_block quota_block ct_helper_block ct_timeout_block ct_expect_block limit_block secmark_block synproxy_block %destructor { obj_free($$); } obj_block_alloc -%type <list> stmt_list -%destructor { stmt_list_free($$); xfree($$); } stmt_list +%type <list> stmt_list stateful_stmt_list +%destructor { stmt_list_free($$); xfree($$); } stmt_list stateful_stmt_list %type <stmt> stmt match_stmt verdict_stmt %destructor { stmt_free($$); } stmt match_stmt verdict_stmt %type <stmt> counter_stmt counter_stmt_alloc stateful_stmt
@@ -2656,6 +2656,19 @@ stmt_list : stmt } ; +stateful_stmt_list : stateful_stmt + { + $$ = xmalloc(sizeof(*$$)); + init_list_head($$); + list_add_tail(&$1->list, $$); + } + | stateful_stmt_list stateful_stmt + { + $$ = $1; + list_add_tail(&$2->list, $1); + } + ; + stateful_stmt : counter_stmt | limit_stmt | quota_stmt
@@ -3675,13 +3688,14 @@ set_stmt : SET set_stmt_op set_elem_expr_stmt set_ref_expr $$->set.key = $4; $$->set.set = $2; } - | set_stmt_op set_ref_expr '{' set_elem_expr_stmt stateful_stmt '}' + | set_stmt_op set_ref_expr '{' set_elem_expr_stmt stateful_stmt_list '}' { $$ = set_stmt_alloc(&@$); $$->set.op = $1; $$->set.key = $4; $$->set.set = $2; - $$->set.stmt = $5; + list_splice_tail($5, &$$->set.stmt_list); + free($5); } ;
@@ -3698,14 +3712,15 @@ map_stmt : set_stmt_op set_ref_expr '{' set_elem_expr_stmt COLON set_elem_expr_ $$->map.data = $6; $$->map.set = $2; } - | set_stmt_op set_ref_expr '{' set_elem_expr_stmt stateful_stmt COLON set_elem_expr_stmt '}' + | set_stmt_op set_ref_expr '{' set_elem_expr_stmt stateful_stmt_list COLON set_elem_expr_stmt '}' { $$ = map_stmt_alloc(&@$); $$->map.op = $1; $$->map.key = $4; $$->map.data = $7; - $$->map.stmt = $5; $$->map.set = $2; + list_splice_tail($5, &$$->map.stmt_list); + free($5); } ;
src/statement.c +26 -8
@@ -732,15 +732,16 @@ const char * const set_stmt_op_names[] = {static void set_stmt_print (const struct stmt *stmt,struct output_ctx *octx) {unsigned int flags = octx->flags; +struct stmt *this;nft_print (octx,"%s " , set_stmt_op_names[stmt->set.op]);expr_print (stmt->set.set, octx);nft_print (octx," { " );expr_print (stmt->set.key, octx); -if (stmt->set.stmt) { +list_for_each_entry (this, &stmt->set.stmt_list, list) {nft_print (octx," " ); octx->flags |=NFT_CTX_OUTPUT_STATELESS ; -stmt_print (stmt->set.stmt, octx); +stmt_print (this, octx); octx->flags = flags; }nft_print (octx," }" );
@@ -748,9 +749,12 @@ static void set_stmt_print(const struct stmt *stmt, struct output_ctx *octx)static void set_stmt_destroy (struct stmt *stmt) { +struct stmt *this, *next; +expr_free (stmt->set.key);expr_free (stmt->set.set); -stmt_free (stmt->set.stmt); +list_for_each_entry_safe (this, next, &stmt->set.stmt_list, list) +stmt_free (this); }static const struct stmt_ops set_stmt_ops = {
@@ -763,21 +767,27 @@ static const struct stmt_ops set_stmt_ops = {struct stmt *set_stmt_alloc (const struct location *loc) { -return stmt_alloc (loc, &set_stmt_ops); +struct stmt *stmt; + + stmt =stmt_alloc (loc, &set_stmt_ops); +init_list_head (&stmt->set.stmt_list); + +return stmt; }static void map_stmt_print (const struct stmt *stmt,struct output_ctx *octx) {unsigned int flags = octx->flags; +struct stmt *this;nft_print (octx,"%s " , set_stmt_op_names[stmt->map.op]);expr_print (stmt->map.set, octx);nft_print (octx," { " );expr_print (stmt->map.key, octx); -if (stmt->map.stmt) { +list_for_each_entry (this, &stmt->map.stmt_list, list) {nft_print (octx," " ); octx->flags |=NFT_CTX_OUTPUT_STATELESS ; -stmt_print (stmt->map.stmt, octx); +stmt_print (this, octx); octx->flags = flags; }nft_print (octx," : " );
@@ -787,10 +797,13 @@ static void map_stmt_print(const struct stmt *stmt, struct output_ctx *octx)static void map_stmt_destroy (struct stmt *stmt) { +struct stmt *this, *next; +expr_free (stmt->map.key);expr_free (stmt->map.data);expr_free (stmt->map.set); -stmt_free (stmt->map.stmt); +list_for_each_entry_safe (this, next, &stmt->map.stmt_list, list) +stmt_free (this); }static const struct stmt_ops map_stmt_ops = {
@@ -802,7 +815,12 @@ static const struct stmt_ops map_stmt_ops = {struct stmt *map_stmt_alloc (const struct location *loc) { -return stmt_alloc (loc, &map_stmt_ops); +struct stmt *stmt; + + stmt =stmt_alloc (loc, &map_stmt_ops); +init_list_head (&stmt->map.stmt_list); + +return stmt; }static void dup_stmt_print (const struct stmt *stmt,struct output_ctx *octx)
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 2/2 src: add set element multi-statement support
Extend the set element infrastructure to support for several statements.
Signed-off-by Pablo Neira Ayuso <[email protected]>
---
v2: fix memleaks.
fix crashes reported by tests/shell.
include/expression.h | 2 +- include/list.h | 7 ++++ include/rule.h | 2 +- src/evaluate.c | 58 ++++++++++++++++++++++-------- src/expression.c | 18 +++++++--- src/json.c | 10 ++++-- src/mnl.c | 17 +++++++-- src/netlink.c | 66 +++++++++++++++++++++++++++++++--- src/parser_bison.y | 84 ++++++++++++++++++++++++++------------------ src/rule.c | 24 ++++++++++--- src/segtree.c | 6 ++-- 11 files changed, 219 insertions(+), 75 deletions(-)
include/expression.h +1 -1
@@ -280,7 +280,7 @@ struct expr {uint64_t timeout;uint64_t expiration;const char *comment; -struct stmt *stmt; +struct list_head stmt_list;uint32_t elem_flags; };struct {
include/list.h +7 -0
@@ -348,6 +348,13 @@ static inline void list_splice_tail_init(struct list_head *list,#define list_first_entry (ptr, type, member) \ list_entry((ptr)->next, type, member) +/** +* list_next_entry - get the next element in list +* @pos: the type * to cursor +* @member: the name of the list_head within the struct. +*/ +#define list_next_entry (pos, member) \ + list_entry((pos)->member.next, typeof(*(pos)), member)/** * list_for_each_entry - iterate over list of given type
include/rule.h +1 -1
@@ -333,7 +333,7 @@ struct set {struct expr *init;struct expr *rg_cache;uint32_t policy; -struct stmt *stmt; +struct list_head stmt_list;bool root;bool automerge;bool key_typeof_valid;
src/evaluate.c +44 -14
71 lines not on this page · raw message
src/expression.c +13 -5
@@ -1248,7 +1248,13 @@ struct expr *set_ref_expr_alloc(const struct location *loc, struct set *set)static void set_elem_expr_print (const struct expr *expr,struct output_ctx *octx) { +struct stmt *stmt; +expr_print (expr->key, octx); +list_for_each_entry (stmt, &expr->stmt_list, list) { +nft_print (octx," " ); +stmt_print (stmt, octx); + }if (expr->timeout) {nft_print (octx," timeout " );time_print (expr->timeout, octx);
@@ -1257,19 +1263,18 @@ static void set_elem_expr_print(const struct expr *expr,nft_print (octx," expires " );time_print (expr->expiration, octx); } -if (expr->stmt) { -nft_print (octx," " ); -stmt_print (expr->stmt, octx); - }if (expr->comment)nft_print (octx," comment \"%s\"" , expr->comment); }static void set_elem_expr_destroy (struct expr *expr) { +struct stmt *stmt, *next; +xfree (expr->comment);expr_free (expr->key); -stmt_free (expr->stmt); +list_for_each_entry_safe (stmt, next, &expr->stmt_list, list) +stmt_free (stmt); }static void set_elem_expr_clone (struct expr *new,const struct expr *expr)
@@ -1279,6 +1284,7 @@ static void set_elem_expr_clone(struct expr *new, const struct expr *expr) new->timeout = expr->timeout;if (expr->comment) new->comment =xstrdup (expr->comment); +init_list_head (&new->stmt_list); }static const struct expr_ops set_elem_expr_ops = {
@@ -1297,6 +1303,8 @@ struct expr *set_elem_expr_alloc(const struct location *loc, struct expr *key) expr =expr_alloc (loc,EXPR_SET_ELEM , key->dtype, key->byteorder, key->len); expr->key = key; +init_list_head (&expr->stmt_list); +return expr; }
src/json.c +7 -3
@@ -583,13 +583,15 @@ json_t *set_ref_expr_json(const struct expr *expr, struct output_ctx *octx)json_t *set_elem_expr_json (const struct expr *expr,struct output_ctx *octx) {json_t *root =expr_print_json (expr->key, octx); +struct stmt *stmt;json_t *tmp;if (!root)return NULL ;/* these element attributes require formal set elem syntax */ -if (expr->timeout || expr->expiration || expr->comment || expr->stmt) { +if (expr->timeout || expr->expiration || expr->comment || + !list_empty (&expr->stmt_list)) { root =json_pack ("{s:o}" ,"val" , root);if (expr->timeout) {
@@ -604,11 +606,13 @@ json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx) tmp =json_string (expr->comment);json_object_set_new (root,"comment" , tmp); } -if (expr->stmt) { - tmp =stmt_print_json (expr->stmt, octx); +list_for_each_entry (stmt, &expr->stmt_list, list) { + tmp =stmt_print_json (stmt, octx);/* XXX: detect and complain about clashes? */ json_object_update_missing (root, tmp);json_decref (tmp); +/* TODO: only one statement per element. */ +break ; }return json_pack ("{s:o}" ,"elem" , root); }
src/mnl.c +14 -3
@@ -1046,6 +1046,8 @@ int mnl_nft_set_add(struct netlink_ctx *ctx, struct cmd *cmd,struct set *set = cmd->set;struct nftnl_set *nls;struct nlmsghdr *nlh; +struct stmt *stmt; +int num_stmts =0 ; nls =nftnl_set_alloc ();if (!nls)
@@ -1128,9 +1130,18 @@ int mnl_nft_set_add(struct netlink_ctx *ctx, struct cmd *cmd, nftnl_udata_buf_len(udbuf));nftnl_udata_buf_free (udbuf); -if (set->stmt) { -nftnl_set_set_data (nls,NFTNL_SET_EXPR , -netlink_gen_stmt_stateful (set->stmt),0 ); +list_for_each_entry (stmt, &set->stmt_list, list) + num_stmts++; + +if (num_stmts ==1 ) { +list_for_each_entry (stmt, &set->stmt_list, list) { +nftnl_set_set_data (nls,NFTNL_SET_EXPR , +netlink_gen_stmt_stateful (stmt),0 ); +break ; + } + }else if (num_stmts >1 ) { +list_for_each_entry (stmt, &set->stmt_list, list) +nftnl_set_add_expr (nls,netlink_gen_stmt_stateful (stmt)); }netlink_dump_set (nls, ctx);
src/netlink.c +61 -5
120 lines not on this page · raw message
src/parser_bison.y +49 -35
133 lines not on this page · raw message
src/rule.c +20 -4
@@ -338,6 +338,9 @@ struct set *set_alloc(const struct location *loc) set->handle.set_id = ++set_id;if (loc !=NULL ) set->location = *loc; + +init_list_head (&set->stmt_list); +return set; }
@@ -357,6 +360,7 @@ struct set *set_clone(const struct set *set) new_set->policy = set->policy; new_set->automerge = set->automerge; new_set->desc = set->desc; +init_list_head (&new_set->stmt_list);return new_set; }
@@ -369,6 +373,8 @@ struct set *set_get(struct set *set)void set_free (struct set *set) { +struct stmt *stmt, *next; +if (--set->refcnt >0 )return ;if (set->init !=NULL )
@@ -376,7 +382,8 @@ void set_free(struct set *set)if (set->comment)xfree (set->comment);handle_free (&set->handle); -stmt_free (set->stmt); +list_for_each_entry_safe (stmt, next, &set->stmt_list, list) +stmt_free (stmt);expr_free (set->key);expr_free (set->data);xfree (set);
@@ -500,6 +507,7 @@ static void set_print_declaration(const struct set *set, struct output_ctx *octx) {const char *delim ="" ; +struct stmt *stmt;const char *type;uint32_t flags;
@@ -570,14 +578,22 @@ static void set_print_declaration(const struct set *set,nft_print (octx,"%s" , opts->stmt_separator); } -if (set->stmt) { +if (!list_empty (&set->stmt_list))nft_print (octx,"%s%s" , opts->tab, opts->tab); + +if (!list_empty (&set->stmt_list)) { octx->flags |=NFT_CTX_OUTPUT_STATELESS ; -stmt_print (set->stmt, octx); +list_for_each_entry (stmt, &set->stmt_list, list) { +stmt_print (stmt, octx); +if (!list_is_last (&stmt->list, &set->stmt_list)) +nft_print (octx," " ); + } octx->flags &= ~NFT_CTX_OUTPUT_STATELESS ; -nft_print (octx,"%s" , opts->stmt_separator); } +if (!list_empty (&set->stmt_list)) +nft_print (octx,"%s" , opts->stmt_separator); +if (set->automerge)nft_print (octx,"%s%sauto-merge%s" , opts->tab, opts->tab, opts->stmt_separator);
src/segtree.c +2 -4
@@ -935,10 +935,8 @@ static void interval_expr_copy(struct expr *dst, struct expr *src) dst->timeout = src->timeout;if (src->expiration) dst->expiration = src->expiration; -if (src->stmt) { - dst->stmt = src->stmt; - src->stmt =NULL ; - } + +list_splice_init (&src->stmt_list, &dst->stmt_list); }void interval_map_decompose (struct expr *set)
Signature · 1 line
2.20.1