Implement 'reset {set,map,element}' commands
Phil Sutter
reviewercover Implement 'reset {set,map,element}' commands
This series makes use of the new NFT_MSG_GETSETELEM_RESET message type to reset state in set elements.
Patches one and two are fallout from working on the actual implementation in patch three.
Phil Sutter (3):
evaluate: Merge some cases in cmd_evaluate_list()
evaluate: Cache looked up set for list commands Implement 'reset {set,map,element}' commands
doc/libnftables-json.adoc | 2 +- doc/nft.txt | 13 ++-- include/linux/netfilter/nf_tables.h | 2 + include/mnl.h | 6 +- include/netlink.h | 5 +- src/cache.c | 9 ++- src/evaluate.c | 42 +++-------- src/json.c | 9 ++- src/mnl.c | 22 ++++-- src/netlink.c | 8 +-- src/parser_bison.y | 12 ++++ src/parser_json.c | 4 ++ src/rule.c | 27 +++++-- tests/shell/testcases/sets/reset_command_0 | 82 ++++++++++++++++++++++ 14 files changed, 181 insertions(+), 62 deletions(-) create mode 100755 tests/shell/testcases/sets/reset_command_0
Signature · 1 line
2.40.0
Phil Sutter
reviewerPATCH 1/3 evaluate: Merge some cases in cmd_evaluate_list()
The code for set, map and meter were almost identical apart from the specific last check. Fold them together and make the distinction in that spot only.
--- src/evaluate.c | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-)
src/evaluate.c +4 -32
@@ -5337,38 +5337,8 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)return 0 ;case CMD_OBJ_SET : - table =table_cache_find (&ctx->nft->cache.table_cache, - cmd->handle.table.name, - cmd->handle.family); -if (!table) -return table_not_found (ctx); - - set =set_cache_find (table, cmd->handle.set.name); -if (set ==NULL ) -return set_not_found (ctx, &ctx->cmd->handle.set.location, - ctx->cmd->handle.set.name); -else if (!set_is_literal (set->flags)) -return cmd_error (ctx, &ctx->cmd->handle.set.location, -"%s" ,strerror (ENOENT )); - -return 0 ; -case CMD_OBJ_METER : - table =table_cache_find (&ctx->nft->cache.table_cache, - cmd->handle.table.name, - cmd->handle.family); -if (!table) -return table_not_found (ctx); - - set =set_cache_find (table, cmd->handle.set.name); -if (set ==NULL ) -return set_not_found (ctx, &ctx->cmd->handle.set.location, - ctx->cmd->handle.set.name); -else if (!set_is_meter (set->flags)) -return cmd_error (ctx, &ctx->cmd->handle.set.location, -"%s" ,strerror (ENOENT )); - -return 0 ;case CMD_OBJ_MAP : +case CMD_OBJ_METER : table =table_cache_find (&ctx->nft->cache.table_cache, cmd->handle.table.name, cmd->handle.family);
@@ -5379,7 +5349,9 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)if (set ==NULL )return set_not_found (ctx, &ctx->cmd->handle.set.location, ctx->cmd->handle.set.name); -else if (!map_is_literal (set->flags)) +if ((cmd->obj ==CMD_OBJ_SET && !set_is_literal (set->flags)) || + (cmd->obj ==CMD_OBJ_MAP && !map_is_literal (set->flags)) || + (cmd->obj ==CMD_OBJ_METER && !set_is_meter (set->flags)))return cmd_error (ctx, &ctx->cmd->handle.set.location,"%s" ,strerror (ENOENT ));
Signature · 1 line
2.40.0
Phil Sutter
reviewerPATCH 2/3 evaluate: Cache looked up set for list commands
In reply to Phil SutterEvaluation phase checks the given table and set exist in cache. Relieve execution phase from having to perform the lookup again by storing the set reference in cmd->set. Just have to increase the ref counter so cmd_free() does the right thing (which lacked handling of MAP and METER objects for some reason).
--- src/evaluate.c | 1 + src/json.c | 9 ++++++--- src/rule.c | 12 ++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-)
src/evaluate.c +1 -0
@@ -5355,6 +5355,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)return cmd_error (ctx, &ctx->cmd->handle.set.location,"%s" ,strerror (ENOENT )); + cmd->set =set_get (set);return 0 ;case CMD_OBJ_CHAIN : table = table_cache_find(&ctx->nft->cache.table_cache,
src/json.c +6 -3
@@ -1783,10 +1783,13 @@ static json_t *do_list_chains_json(struct netlink_ctx *ctx, struct cmd *cmd)static json_t *do_list_set_json (struct netlink_ctx *ctx,struct cmd *cmd,struct table *table) { -struct set *set =set_cache_find (table, cmd->handle.set.name); +struct set *set = cmd->set; -if (set ==NULL ) -return json_null (); +if (!set) { + set =set_cache_find (table, cmd->handle.set.name); +if (set ==NULL ) +return json_null (); + }return json_pack ("[o]" ,set_print_json (&ctx->nft->output, set)); }
src/rule.c +8 -4
@@ -1356,6 +1356,8 @@ void cmd_free(struct cmd *cmd)set_free (cmd->elem.set);break ;case CMD_OBJ_SET : +case CMD_OBJ_MAP : +case CMD_OBJ_METER :case CMD_OBJ_SETELEMS :set_free (cmd->set);break ;
@@ -2289,11 +2291,13 @@ static void __do_list_set(struct netlink_ctx *ctx, struct cmd *cmd,static int do_list_set (struct netlink_ctx *ctx,struct cmd *cmd,struct table *table) { -struct set *set; +struct set *set = cmd->set; - set =set_cache_find (table, cmd->handle.set.name); -if (set ==NULL ) -return -1 ; +if (!set) { + set =set_cache_find (table, cmd->handle.set.name); +if (set ==NULL ) +return -1 ; + }__do_list_set (ctx, cmd, set);
Signature · 1 line
2.40.0
Phil Sutter
reviewerPATCH 3/3 Implement 'reset {set,map,element}' commands
In reply to Phil SutterAll these are used to reset state in set/map elements, i.e. reset the timeout or zero quota and counter values.
While 'reset element' expects a (list of) elements to be specified which should be reset, 'reset set/map' will reset all elements in the given set/map.
--- doc/libnftables-json.adoc | 2 +- doc/nft.txt | 13 ++-- include/linux/netfilter/nf_tables.h | 2 + include/mnl.h | 6 +- include/netlink.h | 5 +- src/cache.c | 9 ++- src/evaluate.c | 5 ++ src/mnl.c | 22 ++++-- src/netlink.c | 8 +-- src/parser_bison.y | 12 ++++ src/parser_json.c | 4 ++ src/rule.c | 15 +++- tests/shell/testcases/sets/reset_command_0 | 82 ++++++++++++++++++++++ 13 files changed, 162 insertions(+), 23 deletions(-) create mode 100755 tests/shell/testcases/sets/reset_command_0
doc/libnftables-json.adoc +1 -1
@@ -175,7 +175,7 @@ kind, optionally filtered by *family* and for some, also *table*. ____ *{ "reset":* 'RESET_OBJECT' *}* -'RESET_OBJECT' := 'COUNTER' | 'COUNTERS' | 'QUOTA' | 'QUOTAS' | 'RULE' | 'RULES' +'RESET_OBJECT' := 'COUNTER' | 'COUNTERS' | 'QUOTA' | 'QUOTAS' | 'RULE' | 'RULES' | 'SET' | 'MAP' | 'ELEMENT' ____ Reset state in suitable objects, i.e. zero their internal counter.
doc/nft.txt +8 -5
@@ -570,7 +570,7 @@ section describes nft set syntax in more detail. [verse] *add set* ['family'] 'table' 'set' *{ type* 'type' | *typeof* 'expression' *;* [*flags* 'flags' *;*] [*timeout* 'timeout' *;*] [*gc-interval* 'gc-interval' *;*] [*elements = {* 'element'[*,* ...] *} ;*] [*size* 'size' *;*] [*comment* 'comment' *;*'] [*policy* 'policy' *;*] [*auto-merge ;*] *}* -{*delete* | *destroy* | *list* | *flush*} *set* ['family'] 'table' 'set' +{*delete* | *destroy* | *list* | *flush* | *reset* } *set* ['family'] 'table' 'set' *list sets* ['family'] *delete set* ['family'] 'table' *handle* 'handle' {*add* | *delete* | *destroy* } *element* ['family'] 'table' 'set' *{* 'element'[*,* ...] *}*
@@ -585,6 +585,7 @@ be tuned with the flags that can be specified at set creation time. *destroy*:: Delete the specified set, it does not fail if it does not exist. *list*:: Display the elements in the specified set. *flush*:: Remove all elements from the specified set. +*reset*:: Reset timeout and other state in all contained elements. .Set specifications [options="header"]
@@ -623,7 +624,7 @@ MAPS ----- [verse] *add map* ['family'] 'table' 'map' *{ type* 'type' | *typeof* 'expression' [*flags* 'flags' *;*] [*elements = {* 'element'[*,* ...] *} ;*] [*size* 'size' *;*] [*comment* 'comment' *;*'] [*policy* 'policy' *;*] *}* -{*delete* | *destroy* | *list* | *flush*} *map* ['family'] 'table' 'map' +{*delete* | *destroy* | *list* | *flush* | *reset* } *map* ['family'] 'table' 'map' *list maps* ['family'] Maps store data based on some specific key used as input. They are uniquely identified by a user-defined name and attached to tables.
@@ -634,8 +635,7 @@ Maps store data based on some specific key used as input. They are uniquely iden *destroy*:: Delete the specified map, it does not fail if it does not exist. *list*:: Display the elements in the specified map. *flush*:: Remove all elements from the specified map. -*add element*:: Comma-separated list of elements to add into the specified map. -*delete element*:: Comma-separated list of element keys to delete from the specified map. +*reset*:: Reset timeout and other state in all contained elements. .Map specifications [options="header"]
@@ -682,7 +682,7 @@ ELEMENTS -------- [verse] ____ -{*add* | *create* | *delete* | *destroy* | *get* } *element* ['family'] 'table' 'set' *{* 'ELEMENT'[*,* ...] *}* +{*add* | *create* | *delete* | *destroy* | *get* | *reset* } *element* ['family'] 'table' 'set' *{* 'ELEMENT'[*,* ...] *}* 'ELEMENT' := 'key_expression' 'OPTIONS' [*:* 'value_expression'] 'OPTIONS' := [*timeout* 'TIMESPEC'] [*expires* 'TIMESPEC'] [*comment* 'string']
@@ -702,6 +702,9 @@ listed elements may already exist. be non-trivial in very large and/or interval sets. In the latter case, the containing interval is returned instead of just the element itself. +*reset* command resets timeout or other state attached to the given +element(s). + .Element options [options="header"] |=================
include/linux/netfilter/nf_tables.h +2 -0
@@ -105,6 +105,7 @@ enum nft_verdicts {* @NFT_MSG_DESTROYSETELEM: destroy a set element (enum nft_set_elem_attributes) * @NFT_MSG_DESTROYOBJ: destroy a stateful object (enum nft_object_attributes) * @NFT_MSG_DESTROYFLOWTABLE: destroy flow table (enum nft_flowtable_attributes) +* @NFT_MSG_GETSETELEM_RESET: get set elements and reset attached stateful expressio ns (enum nft_set_elem_attributes) */ enum nf_tables_msg_types {NFT_MSG_NEWTABLE ,
@@ -140,6 +141,7 @@ enum nf_tables_msg_types {NFT_MSG_DESTROYSETELEM ,NFT_MSG_DESTROYOBJ ,NFT_MSG_DESTROYFLOWTABLE , +NFT_MSG_GETSETELEM_RESET , NFT_MSG_MAX, };
include/mnl.h +4 -2
@@ -68,9 +68,11 @@ int mnl_nft_setelem_add(struct netlink_ctx *ctx, struct cmd *cmd,int mnl_nft_setelem_del (struct netlink_ctx *ctx,struct cmd *cmd,const struct handle *h,const struct expr *init);int mnl_nft_setelem_flush (struct netlink_ctx *ctx,const struct cmd *cmd); -int mnl_nft_setelem_get (struct netlink_ctx *ctx,struct nftnl_set *nls); +int mnl_nft_setelem_get (struct netlink_ctx *ctx,struct nftnl_set *nls, +bool reset);struct nftnl_set *mnl_nft_setelem_get_one (struct netlink_ctx *ctx, -struct nftnl_set *nls); +struct nftnl_set *nls, +bool reset); struct nftnl_obj_list *mnl_nft_obj_dump(struct netlink_ctx *ctx, int family, const char *table,
include/netlink.h +3 -2
@@ -165,10 +165,11 @@ extern struct stmt *netlink_parse_set_expr(const struct set *set, const struct nftnl_expr *nle);extern int netlink_list_setelems (struct netlink_ctx *ctx, -const struct handle *h,struct set *set); +const struct handle *h,struct set *set, +bool reset);extern int netlink_get_setelem (struct netlink_ctx *ctx,const struct handle *h,const struct location *loc,struct set *cache_set, -struct set *set,struct expr *init); +struct set *set,struct expr *init,bool reset);extern int netlink_delinearize_setelem (struct nftnl_set_elem *nlse,struct set *set,struct nft_cache *cache);
src/cache.c +7 -2
@@ -280,6 +280,11 @@ static unsigned int evaluate_cache_reset(struct cmd *cmd, unsigned int flags, flags |=NFT_CACHE_SET |NFT_CACHE_FLOWTABLE |NFT_CACHE_OBJECT |NFT_CACHE_CHAIN ;break ; +case CMD_OBJ_ELEMENTS : +case CMD_OBJ_SET : +case CMD_OBJ_MAP : + flags |=NFT_CACHE_SET ; +break ;default : flags |=NFT_CACHE_TABLE ;break ;
@@ -1066,7 +1071,7 @@ static int cache_init_objects(struct netlink_ctx *ctx, unsigned int flags,continue ; ret =netlink_list_setelems (ctx, &set->handle, - set); + set, false);if (ret <0 ) goto cache_fails; }
@@ -1079,7 +1084,7 @@ static int cache_init_objects(struct netlink_ctx *ctx, unsigned int flags,continue ; ret =netlink_list_setelems (ctx, &set->handle, - set); + set, false);if (ret <0 ) goto cache_fails; }
src/evaluate.c +5 -0
@@ -5450,6 +5450,11 @@ static int cmd_evaluate_reset(struct eval_ctx *ctx, struct cmd *cmd)return table_not_found (ctx);return 0 ; +case CMD_OBJ_ELEMENTS : +return setelem_evaluate (ctx, cmd); +case CMD_OBJ_SET : +case CMD_OBJ_MAP : +return cmd_evaluate_list (ctx, cmd);default :BUG ("invalid command object type %u\n" , cmd->obj); }
src/mnl.c +18 -4
@@ -1870,14 +1870,21 @@ int mnl_nft_setelem_del(struct netlink_ctx *ctx, struct cmd *cmd, }struct nftnl_set *mnl_nft_setelem_get_one (struct netlink_ctx *ctx, -struct nftnl_set *nls_in) +struct nftnl_set *nls_in, +bool reset) {char buf[MNL_SOCKET_BUFFER_SIZE ];struct nftnl_set *nls_out;struct nlmsghdr *nlh; +int msg_type;int err; - nlh =nftnl_nlmsg_build_hdr (buf,NFT_MSG_GETSETELEM , +if (reset) + msg_type =NFT_MSG_GETSETELEM_RESET ; +else + msg_type =NFT_MSG_GETSETELEM ; + + nlh =nftnl_nlmsg_build_hdr (buf, msg_type,nftnl_set_get_u32 (nls_in,NFTNL_SET_FAMILY ),NLM_F_ACK , ctx->seqnum);nftnl_set_elems_nlmsg_build_payload (nlh, nls_in);
@@ -1900,12 +1907,19 @@ struct nftnl_set *mnl_nft_setelem_get_one(struct netlink_ctx *ctx,return nls_out; } -int mnl_nft_setelem_get (struct netlink_ctx *ctx,struct nftnl_set *nls) +int mnl_nft_setelem_get (struct netlink_ctx *ctx,struct nftnl_set *nls, +bool reset) {char buf[MNL_SOCKET_BUFFER_SIZE ];struct nlmsghdr *nlh; +int msg_type; + +if (reset) + msg_type =NFT_MSG_GETSETELEM_RESET ; +else + msg_type =NFT_MSG_GETSETELEM ; - nlh =nftnl_nlmsg_build_hdr (buf,NFT_MSG_GETSETELEM , + nlh =nftnl_nlmsg_build_hdr (buf, msg_type,nftnl_set_get_u32 (nls,NFTNL_SET_FAMILY ),NLM_F_DUMP , ctx->seqnum);nftnl_set_elems_nlmsg_build_payload (nlh, nls);
src/netlink.c +4 -4
@@ -1515,7 +1515,7 @@ static int list_setelements(struct nftnl_set *s, struct netlink_ctx *ctx) }int netlink_list_setelems (struct netlink_ctx *ctx,const struct handle *h, -struct set *set) +struct set *set,bool reset) {struct nftnl_set *nls;int err;
@@ -1530,7 +1530,7 @@ int netlink_list_setelems(struct netlink_ctx *ctx, const struct handle *h,if (h->handle.id)nftnl_set_set_u64 (nls,NFTNL_SET_HANDLE , h->handle.id); - err =mnl_nft_setelem_get (ctx, nls); + err =mnl_nft_setelem_get (ctx, nls, reset);if (err <0 ) {nftnl_set_free (nls);if (errno ==EINTR )
@@ -1558,7 +1558,7 @@ int netlink_list_setelems(struct netlink_ctx *ctx, const struct handle *h,int netlink_get_setelem (struct netlink_ctx *ctx,const struct handle *h,const struct location *loc,struct set *cache_set, -struct set *set,struct expr *init) +struct set *set,struct expr *init,bool reset) {struct nftnl_set *nls, *nls_out =NULL ;int err =0 ;
@@ -1577,7 +1577,7 @@ int netlink_get_setelem(struct netlink_ctx *ctx, const struct handle *h,netlink_dump_set (nls, ctx); - nls_out =mnl_nft_setelem_get_one (ctx, nls); + nls_out =mnl_nft_setelem_get_one (ctx, nls, reset);if (!nls_out) {nftnl_set_free (nls);return -1 ;
src/parser_bison.y +12 -0
@@ -1716,6 +1716,18 @@ reset_cmd : COUNTERS ruleset_spec { $$ = cmd_alloc(CMD_RESET, CMD_OBJ_RULE, &$2, &@$, NULL); } + | ELEMENT set_spec set_block_expr + { + $$ = cmd_alloc(CMD_RESET, CMD_OBJ_ELEMENTS, &$2, &@$, $3); + } + | SET set_or_id_spec + { + $$ = cmd_alloc(CMD_RESET, CMD_OBJ_SET, &$2, &@$, NULL); + } + | MAP set_or_id_spec + { + $$ = cmd_alloc(CMD_RESET, CMD_OBJ_MAP, &$2, &@$, NULL); + } ; flush_cmd : TABLE table_spec
src/parser_json.c +4 -0
@@ -3099,6 +3099,7 @@ static struct cmd *json_parse_cmd_add_set(struct json_ctx *ctx, json_t *root,case CMD_DESTROY :case CMD_LIST :case CMD_FLUSH : +case CMD_RESET :return cmd_alloc (op, obj, &h, int_loc,NULL );default :break ;
@@ -3848,6 +3849,9 @@ static struct cmd *json_parse_cmd_reset(struct json_ctx *ctx, { "quotas", CMD_OBJ_QUOTAS, json_parse_cmd_list_multiple }, { "rule", CMD_OBJ_RULE, json_parse_cmd_reset_rule }, { "rules", CMD_OBJ_RULES, json_parse_cmd_reset_rule }, + { "element", CMD_OBJ_ELEMENTS, json_parse_cmd_add_element }, + { "set", CMD_OBJ_SET, json_parse_cmd_add_set }, + { "map", CMD_OBJ_MAP, json_parse_cmd_add_set }, };unsigned int i;json_t *tmp;
src/rule.c +12 -3
@@ -2386,7 +2386,7 @@ static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd)return 0 ; } -static int do_get_setelems (struct netlink_ctx *ctx,struct cmd *cmd) +static int do_get_setelems (struct netlink_ctx *ctx,struct cmd *cmd,bool reset) {struct set *set, *new_set;struct expr *init;
@@ -2404,7 +2404,7 @@ static int do_get_setelems(struct netlink_ctx *ctx, struct cmd *cmd)/* Fetch from kernel the elements that have been requested .*/ err =netlink_get_setelem (ctx, &cmd->handle, &cmd->location, - cmd->elem.set, new_set, init); + cmd->elem.set, new_set, init, reset);if (err >=0 )__do_list_set (ctx, cmd, new_set);
@@ -2420,7 +2420,7 @@ static int do_command_get(struct netlink_ctx *ctx, struct cmd *cmd) {switch (cmd->obj) {case CMD_OBJ_ELEMENTS : -return do_get_setelems (ctx, cmd); +return do_get_setelems (ctx, cmd, false);default :BUG ("invalid command object type %u\n" , cmd->obj); }
@@ -2457,6 +2457,15 @@ static int do_command_reset(struct netlink_ctx *ctx, struct cmd *cmd)return do_command_list (ctx, cmd);case CMD_OBJ_RULE :return netlink_reset_rules (ctx, cmd, false); +case CMD_OBJ_ELEMENTS : +return do_get_setelems (ctx, cmd, true); +case CMD_OBJ_SET : +case CMD_OBJ_MAP : + ret =netlink_list_setelems (ctx, &cmd->handle, cmd->set, true); +if (ret <0 ) +return ret; + +return do_command_list (ctx, cmd);default :BUG ("invalid command object type %u\n" , cmd->obj); }
tests/shell/testcases/sets/reset_command_0 new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.40.0
Phil Sutter
reviewerQuoted 5 lines · Phil Sutter
This series makes use of the new
NFT_MSG_GETSETELEM_RESETmessage type to reset state in set elements.Patches one and two are fallout from working on the actual implementation in patch three.
Series applied.