Implement 'reset rule' and 'reset rules' commands
Phil Sutter
reviewerPATCH v2 1/1 Implement 'reset rule' and 'reset rules' commands
Reset rule counters and quotas in kernel, i.e. without having to reload them. Requires respective kernel patch to support NFT_MSG_GETRULE_RESET message type.
---
Changes since v1:
- Rename struct nft_cache_filter::rule_id to rule_handle as requested
- Drop accidental whitespace change from parser_json.c
- Add test case
--- doc/libnftables-json.adoc | 2 +- doc/nft.txt | 6 +- include/cache.h | 7 + include/linux/netfilter/nf_tables.h | 1 + include/mnl.h | 4 +- include/netlink.h | 3 + include/rule.h | 1 + src/cache.c | 40 +++++- src/evaluate.c | 2 + src/json.c | 1 + src/mnl.c | 18 ++- src/netlink.c | 49 +++++++ src/parser_bison.y | 16 +++ src/parser_json.c | 35 +++++ src/rule.c | 10 ++ src/scanner.l | 1 + .../testcases/rule_management/0011reset_0 | 127 ++++++++++++++++++ .../rule_management/dumps/0011reset_0.nft | 23 ++++ 18 files changed, 333 insertions(+), 13 deletions(-) create mode 100755 tests/shell/testcases/rule_management/0011reset_0 create mode 100644 tests/shell/testcases/rule_management/dumps/0011reset_0.nft
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' +'RESET_OBJECT' := 'COUNTER' | 'COUNTERS' | 'QUOTA' | 'QUOTAS' | 'RULE' | 'RULES' ____ Reset state in suitable objects, i.e. zero their internal counter.
doc/nft.txt +5 -1
@@ -481,7 +481,10 @@ RULES [verse] {*add* | *insert*} *rule* ['family'] 'table' 'chain' [*handle* 'handle' | *index* 'index'] 'statement' ... [*comment* 'comment'] *replace rule* ['family'] 'table' 'chain' *handle* 'handle' 'statement' ... [*comment* 'comment'] -*delete rule* ['family'] 'table' 'chain' *handle* 'handle' +{*delete* | *reset*} *rule* ['family'] 'table' 'chain' *handle* 'handle' +*reset rules* ['family'] +*reset rules* *table* ['family'] 'table' +*reset rules* *chain* ['family'] 'table' ['chain'] Rules are added to chains in the given table. If the family is not specified, the ip family is used. Rules are constructed from two kinds of components according
@@ -509,6 +512,7 @@ case the rule is inserted after the specified rule. beginning of the chain or before the specified rule. *replace*:: Similar to *add*, but the rule replaces the specified rule. *delete*:: Delete the specified rule. +*reset*:: Reset rule-contained state, i.e. counter and quota statement values. .*add a rule to ip table output chain* -------------
include/cache.h +7 -0
@@ -3,6 +3,8 @@#include <string.h> +struct handle ; +enum cache_level_bits {NFT_CACHE_TABLE_BIT = (1 <<0 ),NFT_CACHE_CHAIN_BIT = (1 <<1 ),
@@ -55,6 +57,7 @@ struct nft_cache_filter {const char *chain;const char *set;const char *ft; +uint64_t rule_handle; } list;struct {
@@ -138,4 +141,8 @@ struct nft_cache {void nft_chain_cache_update (struct netlink_ctx *ctx,struct table *table,const char *chain); +int rule_cache_dump (struct netlink_ctx *ctx,const struct handle *h, +const struct nft_cache_filter *filter, +bool dump,bool reset); +#endif /* _NFT_CACHE_H_ */
include/linux/netfilter/nf_tables.h +1 -0
@@ -124,6 +124,7 @@ enum nf_tables_msg_types {NFT_MSG_NEWFLOWTABLE ,NFT_MSG_GETFLOWTABLE ,NFT_MSG_DELFLOWTABLE , +NFT_MSG_GETRULE_RESET , NFT_MSG_MAX, };
include/mnl.h +3 -1
@@ -34,7 +34,9 @@ int mnl_nft_rule_del(struct netlink_ctx *ctx, struct cmd *cmd);int mnl_nft_rule_replace (struct netlink_ctx *ctx,struct cmd *cmd);struct nftnl_rule_list *mnl_nft_rule_dump (struct netlink_ctx *ctx,int family, -const char *table,const char *chain); +const char *table,const char *chain, +uint64_t rule_handle, +bool dump,bool reset);int mnl_nft_chain_add (struct netlink_ctx *ctx,struct cmd *cmd,unsigned int flags);
include/netlink.h +3 -0
@@ -183,6 +183,9 @@ extern int netlink_list_flowtables(struct netlink_ctx *ctx,extern struct flowtable *netlink_delinearize_flowtable (struct netlink_ctx *ctx,struct nftnl_flowtable *nlo); +extern int netlink_reset_rules (struct netlink_ctx *ctx,const struct cmd *cmd, +bool dump); +extern void netlink_dump_chain (const struct nftnl_chain *nlc,struct netlink_ctx *ctx); extern void netlink_dump_rule(const struct nftnl_rule *nlr,
include/rule.h +1 -0
@@ -620,6 +620,7 @@ enum cmd_obj {CMD_OBJ_SETELEMS ,CMD_OBJ_SETS ,CMD_OBJ_RULE , +CMD_OBJ_RULES ,CMD_OBJ_CHAIN ,CMD_OBJ_CHAINS , CMD_OBJ_TABLE,
src/cache.c +34 -6
@@ -263,6 +263,29 @@ static unsigned int evaluate_cache_list(struct nft_ctx *nft, struct cmd *cmd,return flags; } +static unsigned int evaluate_cache_reset (struct cmd *cmd,unsigned int flags, +struct nft_cache_filter *filter) +{ +switch (cmd->obj) { +case CMD_OBJ_RULES : +if (filter) { +if (cmd->handle.table.name) { + filter->list.family = cmd->handle.family; + filter->list.table = cmd->handle.table.name; + } +if (cmd->handle.chain.name) + filter->list.chain = cmd->handle.chain.name; + } + flags |=NFT_CACHE_RULE ; +break ; +default : + flags |=NFT_CACHE_TABLE ; +break ; + } + +return flags; +} +static int nft_handle_validate (const struct cmd *cmd,struct list_head *msgs) {const struct handle *h = &cmd->handle;
@@ -277,6 +300,7 @@ static int nft_handle_validate(const struct cmd *cmd, struct list_head *msgs) }break ;case CMD_OBJ_RULE : +case CMD_OBJ_RULES :case CMD_OBJ_CHAIN :case CMD_OBJ_CHAINS : if (h->table.name &&
@@ -403,7 +427,7 @@ int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds, flags =evaluate_cache_get (cmd, flags);break ;case CMD_RESET : - flags |=NFT_CACHE_TABLE ; + flags |=evaluate_cache_reset (cmd, flags, filter);break ;case CMD_LIST : flags |=evaluate_cache_list (nft, cmd, flags, filter);
@@ -592,7 +616,7 @@ static int list_rule_cb(struct nftnl_rule *nlr, void *data) chain =nftnl_rule_get_str (nlr,NFTNL_RULE_CHAIN );if (h->family != family || -strcmp (table, h->table.name) !=0 || + (h->table.name &&strcmp (table, h->table.name) !=0 ) || (h->chain.name &&strcmp (chain, h->chain.name) !=0 ))return 0 ;
@@ -604,19 +628,23 @@ static int list_rule_cb(struct nftnl_rule *nlr, void *data)return 0 ; } -static int rule_cache_dump (struct netlink_ctx *ctx,const struct handle *h, -const struct nft_cache_filter *filter) +int rule_cache_dump (struct netlink_ctx *ctx,const struct handle *h, +const struct nft_cache_filter *filter, +bool dump,bool reset) {struct nftnl_rule_list *rule_cache;const char *table =NULL ;const char *chain =NULL ; +uint64_t rule_handle =0 ;if (filter) { table = filter->list.table; chain = filter->list.chain; + rule_handle = filter->list.rule_handle; } - rule_cache =mnl_nft_rule_dump (ctx, h->family, table, chain); + rule_cache =mnl_nft_rule_dump (ctx, h->family, + table, chain, rule_handle, dump, reset);if (rule_cache ==NULL ) {if (errno ==EINTR )return -1 ;
@@ -948,7 +976,7 @@ static int rule_init_cache(struct netlink_ctx *ctx, struct table *table,struct chain *chain;int ret; - ret =rule_cache_dump (ctx, &table->handle, filter); + ret =rule_cache_dump (ctx, &table->handle, filter, true, false);list_for_each_entry_safe (rule, nrule, &ctx->list, list) { chain =chain_cache_find (table, rule->handle.chain.name);
src/evaluate.c +2 -0
@@ -5286,6 +5286,8 @@ static int cmd_evaluate_reset(struct eval_ctx *ctx, struct cmd *cmd)case CMD_OBJ_QUOTA :case CMD_OBJ_COUNTERS :case CMD_OBJ_QUOTAS : +case CMD_OBJ_RULES : +case CMD_OBJ_RULE :if (cmd->handle.table.name ==NULL )return 0 ; if (!table_cache_find(&ctx->nft->cache.table_cache,
src/json.c +1 -0
@@ -1911,6 +1911,7 @@ int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd)case CMD_OBJ_SET : root =do_list_set_json (ctx, cmd, table);break ; +case CMD_OBJ_RULES :case CMD_OBJ_RULESET : root =do_list_ruleset_json (ctx, cmd);break ;
src/mnl.c +14 -4
@@ -654,13 +654,21 @@ static int rule_cb(const struct nlmsghdr *nlh, void *data) }struct nftnl_rule_list *mnl_nft_rule_dump (struct netlink_ctx *ctx,int family, -const char *table,const char *chain) +const char *table,const char *chain, +uint64_t rule_handle, +bool dump,bool reset) { +uint16_t nl_flags = dump ?NLM_F_DUMP :NLM_F_ACK ;char buf[MNL_SOCKET_BUFFER_SIZE ];struct nftnl_rule_list *nlr_list;struct nftnl_rule *nlr =NULL ;struct nlmsghdr *nlh; -int ret; +int msg_type, ret; + +if (reset) + msg_type =NFT_MSG_GETRULE_RESET ; +else + msg_type =NFT_MSG_GETRULE ;if (table) { nlr =nftnl_rule_alloc ();
@@ -670,14 +678,16 @@ struct nftnl_rule_list *mnl_nft_rule_dump(struct netlink_ctx *ctx, int family,nftnl_rule_set_str (nlr,NFTNL_RULE_TABLE , table);if (chain)nftnl_rule_set_str (nlr,NFTNL_RULE_CHAIN , chain); +if (rule_handle) +nftnl_rule_set_u64 (nlr,NFTNL_RULE_HANDLE , rule_handle); } nlr_list =nftnl_rule_list_alloc ();if (nlr_list ==NULL )memory_allocation_error (); - nlh =nftnl_nlmsg_build_hdr (buf,NFT_MSG_GETRULE , family, -NLM_F_DUMP , ctx->seqnum); + nlh =nftnl_nlmsg_build_hdr (buf, msg_type, family, + nl_flags, ctx->seqnum);if (nlr) {nftnl_rule_nlmsg_build_payload (nlh, nlr);nftnl_rule_free (nlr);
src/netlink.c +49 -0
55 lines not on this page · raw message
src/parser_bison.y +16 -0
@@ -1597,6 +1597,22 @@ reset_cmd : COUNTERS ruleset_spec { $$ = cmd_alloc(CMD_RESET, CMD_OBJ_QUOTA, &$2, &@$, NULL); } + | RULES ruleset_spec + { + $$ = cmd_alloc(CMD_RESET, CMD_OBJ_RULES, &$2, &@$, NULL); + } + | RULES TABLE table_spec + { + $$ = cmd_alloc(CMD_RESET, CMD_OBJ_RULES, &$3, &@$, NULL); + } + | RULES CHAIN chain_spec + { + $$ = cmd_alloc(CMD_RESET, CMD_OBJ_RULES, &$3, &@$, NULL); + } + | RULE ruleid_spec + { + $$ = cmd_alloc(CMD_RESET, CMD_OBJ_RULE, &$2, &@$, NULL); + } ; flush_cmd : TABLE table_spec
src/parser_json.c +35 -0
@@ -3763,6 +3763,39 @@ static struct cmd *json_parse_cmd_list(struct json_ctx *ctx,return NULL ; } +static struct cmd *json_parse_cmd_reset_rule (struct json_ctx *ctx, +json_t *root,enum cmd_ops op, +enum cmd_obj obj) +{ +struct handle h = { + .family =NFPROTO_UNSPEC , + }; +const char *family =NULL , *table =NULL , *chain =NULL ; + + +if (obj ==CMD_OBJ_RULE && +json_unpack_err (ctx, root,"{s:s, s:s, s:s, s:I}" , +"family" , &family,"table" , &table, +"chain" , &chain,"handle" , &h.handle.id)) +return NULL ; +else if (obj ==CMD_OBJ_RULES ) { +json_unpack (root,"{s:s}" ,"family" , &family); +json_unpack (root,"{s:s}" ,"table" , &table); +json_unpack (root,"{s:s}" ,"chain" , &chain); + } + +if (family &&parse_family (family, &h.family)) { +json_error (ctx,"Unknown family '%s'." , family); +return NULL ; + } +if (table) { + h.table.name =xstrdup (table); +if (chain) + h.chain.name =xstrdup (chain); + } +return cmd_alloc (op, obj, &h, int_loc,NULL ); +} +static struct cmd *json_parse_cmd_reset (struct json_ctx *ctx,json_t *root,enum cmd_ops op) {
@@ -3776,6 +3809,8 @@ static struct cmd *json_parse_cmd_reset(struct json_ctx *ctx, { "counters", CMD_OBJ_COUNTERS, json_parse_cmd_list_multiple }, { "quota", CMD_OBJ_QUOTA, json_parse_cmd_add_object }, { "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 }, };unsigned int i;json_t *tmp;
src/rule.c +10 -0
@@ -2504,6 +2504,8 @@ static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd)case CMD_OBJ_SET :return do_list_set (ctx, cmd, table);case CMD_OBJ_RULESET : +case CMD_OBJ_RULES : +case CMD_OBJ_RULE :return do_list_ruleset (ctx, cmd);case CMD_OBJ_METERS :return do_list_sets (ctx, cmd);
@@ -2611,6 +2613,14 @@ static int do_command_reset(struct netlink_ctx *ctx, struct cmd *cmd)case CMD_OBJ_QUOTA : type =NFT_OBJECT_QUOTA ;break ; +case CMD_OBJ_RULES : + ret =netlink_reset_rules (ctx, cmd, true); +if (ret <0 ) +return ret; + +return do_command_list (ctx, cmd); +case CMD_OBJ_RULE : +return netlink_reset_rules (ctx, cmd, false);default :BUG ("invalid command object type %u\n" , cmd->obj); }
src/scanner.l +1 -0
@@ -402,6 +402,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) <SCANSTATE_CMD_LIST,SCANSTATE_CMD_RESET>{ "counters" { return COUNTERS; } "quotas" { return QUOTAS; } + "rules" { return RULES; } } "log" { scanner_push_start_cond(yyscanner, SCANSTATE_STMT_LOG); return LOG; }
tests/shell/testcases/rule_management/0011reset_0 new file +127 -0
127 lines not on this page · raw message
tests/shell/testcases/rule_management/dumps/0011reset_0.nft new file +23 -0
@@ -0,0 +1,23 @@ +table ip t { + chain c { + counter packets 0 bytes 0 accept + counter packets 0 bytes 0 drop + } + + chain c2 { + counter packets 0 bytes 0 accept + counter packets 0 bytes 0 drop + } +} +table inet t { + chain c { + counter packets 0 bytes 0 accept + counter packets 0 bytes 0 drop + } +} +table ip t2 { + chain c2 { + counter packets 0 bytes 0 accept + counter packets 0 bytes 0 drop + } +}
Signature · 1 line
2.38.0
Pablo Neira Ayuso
maintainerQuoted 3 lines · Phil Sutter
Reset rule counters and quotas in kernel, i.e. without having to reload them. Requires respective kernel patch to support
NFT_MSG_GETRULE_RESETmessage type.
Only thing to mention: This adds a new rule_cache_dump() call, this was consolidated on top of the cache infrastructure, to have a single spot in the code to fetch kernel objects via netlink. This triggers to netlink dumps, one to populate the cache and another for the reset, right?
Apart from that, patch LGTM.
Phil Sutter
reviewerQuoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
Reset rule counters and quotas in kernel, i.e. without having to reload them. Requires respective kernel patch to support
NFT_MSG_GETRULE_RESETmessage type.Only thing to mention: This adds a new
rule_cache_dump()call, this was consolidated on top of the cache infrastructure, to have a single spot in the code to fetch kernel objects via netlink. This triggers to netlink dumps, one to populate the cache and another for the reset, right?
Well, rule_cache_dump() becomes dual-use, because the rule reset call is so similar to the regular rule dump one.
I also changed it to allow turning the actual dump off (by sending NLM_F_ACK instead of NLM_F_DUMP to kernel). This is used for resetting a single rule (identified by rule-handle). Same functionality as with resetting (a single) quota instead of "quotas".
If dump is active, the kernel returns the rules that have been reset to user space, so users see the last state data. To correctly print this, nft needs a table and chain cache. My v2 has this wrong, it needlessly requests a rule cache also. Thanks for making me validate this aspect of the implementation! :)
The only caveat I just noticed with reduced cache is that "whole ruleset reset" does not print the rules because list_rule_cb() expects h->family to be set. Making it accept NFPROTO_UNSPEC is trivial, though.
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 12 lines · Phil Sutter
Quoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
Reset rule counters and quotas in kernel, i.e. without having to reload them. Requires respective kernel patch to support
NFT_MSG_GETRULE_RESETmessage type.Only thing to mention: This adds a new
rule_cache_dump()call, this was consolidated on top of the cache infrastructure, to have a single spot in the code to fetch kernel objects via netlink. This triggers to netlink dumps, one to populate the cache and another for the reset, right?
Quoted 7 lines
Well,
rule_cache_dump()becomes dual-use, because the rule reset call is so similar to the regular rule dump one.I also changed it to allow turning the actual dump off (by sending
NLM_F_ACKinstead ofNLM_F_DUMPto kernel). This is used for resetting a single rule (identified by rule-handle). Same functionality as with resetting (a single) quota instead of "quotas".
Makes sense.
Quoted 5 lines
If dump is active, the kernel returns the rules that have been reset to user space, so users see the last state data. To correctly print this, nft needs a table and chain cache. My v2 has this wrong, it needlessly requests a rule cache also. Thanks for making me validate this aspect of the implementation! :)
Ok.
Quoted 3 lines
The only caveat I just noticed with reduced cache is that "whole ruleset reset" does not print the rules because
list_rule_cb()expectsh->familyto be set. Making it acceptNFPROTO_UNSPECis trivial, though.
I guess you refer to this chunk.
@@ -591,8 +615,8 @@ static int list_rule_cb(struct nftnl_rule *nlr, void *data)
table = nftnl_rule_get_str(nlr, NFTNL_RULE_TABLE);
chain = nftnl_rule_get_str(nlr, NFTNL_RULE_CHAIN);
- if (h->family != family ||
- strcmp(table, h->table.name) != 0 ||
+ if ((h->family != NFPROTO_UNSPEC && h->family != family) || + (h->table.name && strcmp(table, h->table.name) != 0) ||
(h->chain.name && strcmp(chain, h->chain.name) != 0))
return 0;
There is one more thing that will need a re-visit sooner or later.
NLM_F_DUMP might hit EINTR, meaning that while dumping-and-reset, there was a ruleset update from the transaction path.
In this case, the listing would not be consistent. This would require to trigger a bit more complicated path, that is: 1) keep previous list of rules, 2) re-start dump with empty list, 3) look up via handle to check if the rule in the previous list, to fetch the counter into the new rule.
I think this problem already exists for the counter/quota/... reset.
I still have to extend the cache infrastructure to support for rules. It is on my TODO list for monitor infrastructure. Then probably this reset command can be reworked on top of the cache.
Anyway, your patch is a good starter. Better cache integration and this corner case can possibly wait.
Phil Sutter
reviewerQuoted 53 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter
Quoted 10 lines · Pablo Neira Ayuso
Quoted 3 lines · Phil Sutter
Reset rule counters and quotas in kernel, i.e. without having to reload them. Requires respective kernel patch to support
NFT_MSG_GETRULE_RESETmessage type.Only thing to mention: This adds a new
rule_cache_dump()call, this was consolidated on top of the cache infrastructure, to have a single spot in the code to fetch kernel objects via netlink. This triggers to netlink dumps, one to populate the cache and another for the reset, right?Quoted 7 lines
Well,
rule_cache_dump()becomes dual-use, because the rule reset call is so similar to the regular rule dump one.I also changed it to allow turning the actual dump off (by sending
NLM_F_ACKinstead ofNLM_F_DUMPto kernel). This is used for resetting a single rule (identified by rule-handle). Same functionality as with resetting (a single) quota instead of "quotas".Makes sense.
Quoted 5 lines
If dump is active, the kernel returns the rules that have been reset to user space, so users see the last state data. To correctly print this, nft needs a table and chain cache. My v2 has this wrong, it needlessly requests a rule cache also. Thanks for making me validate this aspect of the implementation! :)
Ok.
Quoted 3 lines
The only caveat I just noticed with reduced cache is that "whole ruleset reset" does not print the rules because
list_rule_cb()expectsh->familyto be set. Making it acceptNFPROTO_UNSPECis trivial, though.I guess you refer to this chunk.
src/cache.c +2
-2@@ -591,8 +615,8 @@ static int list_rule_cb(struct nftnl_rule *nlr, void *data) table =nftnl_rule_get_str (nlr,NFTNL_RULE_TABLE ); chain =nftnl_rule_get_str (nlr,NFTNL_RULE_CHAIN ); -if (h->family != family || -strcmp (table, h->table.name) !=0 || +if ((h->family !=NFPROTO_UNSPEC && h->family != family) || + (h->table.name &&strcmp (table, h->table.name) !=0 ) || (h->chain.name &&strcmp (chain, h->chain.name) !=0 ))return 0 ;There is one more thing that will need a re-visit sooner or later.
NLM_F_DUMPmight hit EINTR, meaning that while dumping-and-reset, there was a ruleset update from the transaction path.
Quoted 7 lines
In this case, the listing would not be consistent. This would require to trigger a bit more complicated path, that is: 1) keep previous list of rules, 2) re-start dump with empty list, 3) look up via handle to check if the rule in the previous list, to fetch the counter into the new rule.
I think this problem already exists for the counter/quota/... reset.
Yes, I more or less copied from 'reset <obj>' command so they likely share their bugs. :)
Quoted 6 lines
I still have to extend the cache infrastructure to support for rules. It is on my TODO list for monitor infrastructure. Then probably this reset command can be reworked on top of the cache.
Anyway, your patch is a good starter. Better cache integration and this corner case can possibly wait.
OK. There's yet another bug: Listing is broken if reset rules contain a set reference. So it needs a cache that contains about everything *but* rules. I'll also skip on set elements, they're strictly not needed. But this in turn leads to a suggestion from Eric about a 'reset set' command - follow-up material, obviously.
Cheers, Phil