src: add support to command "destroy"
PATCH v6 1/1 src: add support to command "destroy"
"destroy" command performs a deletion as "delete" command but does not fail when the object does not exist. As there is no NLM_F_* flag for ignoring such error, it needs to be ignored directly on error handling.
Example of use:
# nft list ruleset
table ip filter {
chain output {
}
}
# nft destroy table ip missingtable
# echo $?
0
# nft list ruleset
table ip filter {
chain output {
}
}---
v1: initial patch
v2: improve error handling
v3: update manpage
v4: use NFT_MSG_DESTROY* from kernel patch
v5: add variable to handle msg_type
v6: rebase to master branch
--- doc/nft.txt | 33 +++++--- include/linux/netfilter/nf_tables.h | 16 ++++ include/rule.h | 2 + src/cache.c | 1 + src/evaluate.c | 3 + src/libnftables.c | 1 + src/mnl.c | 46 +++++++++--- src/parser_bison.y | 75 ++++++++++++++++++- src/parser_json.c | 16 ++-- src/rule.c | 1 + src/scanner.l | 1 + .../testcases/rule_management/0011destroy_0 | 8 ++ .../testcases/rule_management/0012destroy_0 | 7 ++ .../rule_management/dumps/0011destroy_0.nft | 4 + .../rule_management/dumps/0012destroy_0.nft | 4 + 15 files changed, 189 insertions(+), 29 deletions(-) create mode 100755 tests/shell/testcases/rule_management/0011destroy_0 create mode 100755 tests/shell/testcases/rule_management/0012destroy_0 create mode 100644 tests/shell/testcases/rule_management/dumps/0011destroy_0.nft create mode 100644 tests/shell/testcases/rule_management/dumps/0012destroy_0.nft
doc/nft.txt +23 -10
@@ -322,9 +322,10 @@ TABLES ------ [verse] {*add* | *create*} *table* ['family'] 'table' [ {*comment* 'comment' *;*'} *{ flags* 'flags' *; }*] -{*delete* | *list* | *flush*} *table* ['family'] 'table' +{*delete* | *destroy* | *list* | *flush*} *table* ['family'] 'table' *list tables* ['family'] *delete table* ['family'] *handle* 'handle' +*destroy table* ['family'] *handle* 'handle' Tables are containers for chains, sets and stateful objects. They are identified by their address family and their name. The address family must be one of *ip*,
@@ -368,6 +369,7 @@ add table inet mytable [horizontal] *add*:: Add a new table for the given family with the given name. *delete*:: Delete the specified table. +*destroy*:: Delete the specified table, won't fail if it doesn't exist. *list*:: List all chains and rules of the specified table. *flush*:: Flush all chains and rules of the specified table.
@@ -375,9 +377,10 @@ CHAINS ------ [verse] {*add* | *create*} *chain* ['family'] 'table' 'chain' [*{ type* 'type' *hook* 'hook' [*device* 'device'] *priority* 'priority' *;* [*policy* 'policy' *;*] [*comment* 'comment' *;*'] *}*] -{*delete* | *list* | *flush*} *chain* ['family'] 'table' 'chain' +{*delete* | *destroy* | *list* | *flush*} *chain* ['family'] 'table' 'chain' *list chains* ['family'] *delete chain* ['family'] 'table' *handle* 'handle' +*destroy chain* ['family'] 'table' *handle* 'handle' *rename chain* ['family'] 'table' 'chain' 'newname' Chains are containers for rules. They exist in two kinds, base chains and
@@ -390,6 +393,7 @@ organization. are specified, the chain is created as a base chain and hooked up to the networking stack. *create*:: Similar to the *add* command, but returns an error if the chain already exists. *delete*:: Delete the specified chain. The chain must not contain any rules or be used as jump target. +*destroy*:: Delete the specified chain, won't fail if it doesn't exist. The chain must not contain any rules or be used as jump target. *rename*:: Rename the specified chain. *list*:: List all rules of the specified chain. *flush*:: Flush all rules of the specified chain.
@@ -482,6 +486,7 @@ RULES {*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' +*destroy rule* ['family'] 'table' 'chain' *handle* 'handle' 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 +514,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. +*destroy*:: Delete the specified rule, won't fail if it doesn't exist. .*add a rule to ip table output chain* -------------
@@ -559,10 +565,10 @@ 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* | *list* | *flush*} *set* ['family'] 'table' 'set' +{*delete* | *destroy* | *list* | *flush*} *set* ['family'] 'table' 'set' *list sets* ['family'] *delete set* ['family'] 'table' *handle* 'handle' -{*add* | *delete*} *element* ['family'] 'table' 'set' *{* 'element'[*,* ...] *}* +{*add* | *delete* | *destroy* } *element* ['family'] 'table' 'set' *{* 'element'[*,* ...] *}* Sets are element containers of a user-defined data type, they are uniquely identified by a user-defined name and attached to tables. Their behaviour can
@@ -571,6 +577,7 @@ be tuned with the flags that can be specified at set creation time. [horizontal] *add*:: Add a new set in the specified table. See the Set specification table below for more information about how to specify properties of a set. *delete*:: Delete the specified set. +*destroy*:: Delete the specified set, won't fail if it doesn't exist. *list*:: Display the elements in the specified set. *flush*:: Remove all elements from the specified set.
@@ -612,7 +619,7 @@ MAPS ----- [verse] *add map* ['family'] 'table' 'map' *{ type* 'type' | *typeof* 'expression' [*flags* 'flags' *;*] [*elements = {* 'element'[*,* ...] *} ;*] [*size* 'size' *;*] [*comment* 'comment' *;*'] [*policy* 'policy' *;*] *}* -{*delete* | *list* | *flush*} *map* ['family'] 'table' 'map' +{*delete* | *destroy* | *list* | *flush*} *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.
@@ -620,6 +627,7 @@ Maps store data based on some specific key used as input. They are uniquely iden [horizontal] *add*:: Add a new map in the specified table. *delete*:: Delete the specified map. +*destroy*:: Delete the specified map, won't fail if it doesn't 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.
@@ -654,7 +662,7 @@ ELEMENTS -------- [verse] ____ -{*add* | *create* | *delete* | *get* } *element* ['family'] 'table' 'set' *{* 'ELEMENT'[*,* ...] *}* +{*add* | *create* | *delete* | *destroy* | *get* } *element* ['family'] 'table' 'set' *{* 'ELEMENT'[*,* ...] *}* 'ELEMENT' := 'key_expression' 'OPTIONS' [*:* 'value_expression'] 'OPTIONS' := [*timeout* 'TIMESPEC'] [*expires* 'TIMESPEC'] [*comment* 'string']
@@ -692,7 +700,7 @@ FLOWTABLES [verse] {*add* | *create*} *flowtable* ['family'] 'table' 'flowtable' *{ hook* 'hook' *priority* 'priority' *; devices = {* 'device'[*,* ...] *} ; }* *list flowtables* ['family'] -{*delete* | *list*} *flowtable* ['family'] 'table' 'flowtable' +{*delete* | *destroy* | *list*} *flowtable* ['family'] 'table' 'flowtable' *delete* *flowtable* ['family'] 'table' *handle* 'handle' Flowtables allow you to accelerate packet forwarding in software. Flowtables
@@ -716,6 +724,7 @@ and subtraction can be used to set relative priority, e.g. filter + 5 equals to [horizontal] *add*:: Add a new flowtable for the given family with the given name. *delete*:: Delete the specified flowtable. +*destroy*:: Delete the specified flowtable, won't fail if it doesn't exist. *list*:: List all flowtables. LISTING
@@ -732,12 +741,15 @@ kernel modules, such as nf_conntrack. STATEFUL OBJECTS ---------------- [verse] -{*add* | *delete* | *list* | *reset*} *counter* ['family'] 'table' 'object' -{*add* | *delete* | *list* | *reset*} *quota* ['family'] 'table' 'object' -{*add* | *delete* | *list*} *limit* ['family'] 'table' 'object' +{*add* | *delete* | *destroy* | *list* | *reset*} *counter* ['family'] 'table' 'object' +{*add* | *delete* | *destroy* | *list* | *reset*} *quota* ['family'] 'table' 'object' +{*add* | *delete* | *destroy* | *list*} *limit* ['family'] 'table' 'object' *delete* 'counter' ['family'] 'table' *handle* 'handle' *delete* 'quota' ['family'] 'table' *handle* 'handle' *delete* 'limit' ['family'] 'table' *handle* 'handle' +*destroy* 'counter' ['family'] 'table' *handle* 'handle' +*destroy* 'quota' ['family'] 'table' *handle* 'handle' +*destroy* 'limit' ['family'] 'table' *handle* 'handle' *list counters* ['family'] *list quotas* ['family'] *list limits* ['family']
@@ -753,6 +765,7 @@ keywords "type name" are used e.g. "counter name". [horizontal] *add*:: Add a new stateful object in the specified table. *delete*:: Delete the specified object. +*destroy*:: Delete the specified object, won't fail if it doesn't exist. *list*:: Display stateful information the object holds. *reset*:: List-and-reset stateful object.
include/linux/netfilter/nf_tables.h +16 -0
@@ -97,6 +97,14 @@ enum nft_verdicts {* @NFT_MSG_NEWFLOWTABLE: add new flow table (enum nft_flowtable_attributes) * @NFT_MSG_GETFLOWTABLE: get flow table (enum nft_flowtable_attributes) * @NFT_MSG_DELFLOWTABLE: delete flow table (enum nft_flowtable_attributes) +* @NFT_MSG_GETRULE_RESET: get rules and reset stateful expressions (enum nft_obj_attributes) +* @NFT_MSG_DESTROYTABLE: destroy a table (enum nft_table_attributes) +* @NFT_MSG_DESTROYCHAIN: destroy a chain (enum nft_chain_attributes) +* @NFT_MSG_DESTROYRULE: destroy a rule (enum nft_rule_attributes) +* @NFT_MSG_DESTROYSET: destroy a set (enum nft_set_attributes) +* @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) */ enum nf_tables_msg_types {NFT_MSG_NEWTABLE ,
@@ -124,6 +132,14 @@ enum nf_tables_msg_types {NFT_MSG_NEWFLOWTABLE ,NFT_MSG_GETFLOWTABLE ,NFT_MSG_DELFLOWTABLE , +NFT_MSG_GETRULE_RESET , +NFT_MSG_DESTROYTABLE , +NFT_MSG_DESTROYCHAIN , +NFT_MSG_DESTROYRULE , +NFT_MSG_DESTROYSET , +NFT_MSG_DESTROYSETELEM , +NFT_MSG_DESTROYOBJ , +NFT_MSG_DESTROYFLOWTABLE , NFT_MSG_MAX, };
include/rule.h +2 -0
@@ -563,6 +563,7 @@ void flowtable_print(const struct flowtable *n, struct output_ctx *octx);* @CMD_EXPORT: export the ruleset in a given format * @CMD_MONITOR: event listener * @CMD_DESCRIBE: describe an expression +* @CMD_DESTROY: destroy object */ enum cmd_ops {CMD_INVALID ,
@@ -580,6 +581,7 @@ enum cmd_ops {CMD_EXPORT ,CMD_MONITOR ,CMD_DESCRIBE , + CMD_DESTROY, };/**
src/cache.c +1 -0
@@ -391,6 +391,7 @@ int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds, flags =NFT_CACHE_FULL ;break ;case CMD_DELETE : +case CMD_DESTROY : flags |=NFT_CACHE_TABLE |NFT_CACHE_CHAIN | NFT_CACHE_SET |
src/evaluate.c +3 -0
@@ -1580,6 +1580,7 @@ static int interval_set_eval(struct eval_ctx *ctx, struct set *set, }break ;case CMD_DELETE : +case CMD_DESTROY : ret =set_delete (ctx->msgs, ctx->cmd, set, init, ctx->nft->debug_mask);break ;
@@ -5404,6 +5405,7 @@ static const char * const cmd_op_name[] = { [CMD_EXPORT] = "export", [CMD_MONITOR] = "monitor", [CMD_DESCRIBE] = "describe", + [CMD_DESTROY] = "destroy", };static const char *cmd_op_to_name (enum cmd_ops op)
@@ -5436,6 +5438,7 @@ int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)case CMD_INSERT :return cmd_evaluate_add (ctx, cmd);case CMD_DELETE : +case CMD_DESTROY :return cmd_evaluate_delete (ctx, cmd);case CMD_GET :return cmd_evaluate_get (ctx, cmd);
src/libnftables.c +1 -0
@@ -89,6 +89,7 @@ static int nft_netlink(struct nft_ctx *nft, last_seqnum =UINT32_MAX ; } } +/* nfnetlink uses the first netlink message header in the batch whose * sequence number is zero to report for EOPNOTSUPP and EPERM errors in * some scenarios. Now it is safe to release pending errors here.
src/mnl.c +36 -10
135 lines not on this page · raw message
src/parser_bison.y +73 -2
99 lines not on this page · raw message
src/parser_json.c +9 -7
@@ -2928,7 +2928,7 @@ static struct cmd *json_parse_cmd_add_rule(struct json_ctx *ctx, json_t *root,if (op !=CMD_DELETE &&json_unpack_err (ctx, root,"{s:o}" ,"expr" , &tmp))return NULL ; -else if (op ==CMD_DELETE && +else if ((op ==CMD_DELETE || op ==CMD_DESTROY ) &&json_unpack_err (ctx, root,"{s:I}" ,"handle" , &h.handle.id))return NULL ;
@@ -2939,7 +2939,7 @@ static struct cmd *json_parse_cmd_add_rule(struct json_ctx *ctx, json_t *root, h.table.name =xstrdup (h.table.name); h.chain.name =xstrdup (h.chain.name); -if (op ==CMD_DELETE ) +if (op ==CMD_DELETE || op ==CMD_DESTROY )return cmd_alloc (op, obj, &h, int_loc,NULL );if (!json_is_array (tmp)) {
@@ -3035,7 +3035,7 @@ static struct cmd *json_parse_cmd_add_set(struct json_ctx *ctx, json_t *root,if (op !=CMD_DELETE &&json_unpack_err (ctx, root,"{s:s}" ,"name" , &h.set.name)) {return NULL ; - }else if (op ==CMD_DELETE && + }else if ((op ==CMD_DELETE || op ==CMD_DESTROY ) &&json_unpack (root,"{s:s}" ,"name" , &h.set.name) &&json_unpack (root,"{s:I}" ,"handle" , &h.handle.id)) {json_error (ctx,"Either name or handle required to delete a set." );
@@ -3052,6 +3052,7 @@ static struct cmd *json_parse_cmd_add_set(struct json_ctx *ctx, json_t *root,switch (op) {case CMD_DELETE : +case CMD_DESTROY :case CMD_LIST :case CMD_FLUSH :return cmd_alloc (op, obj, &h, int_loc,NULL );
@@ -3233,7 +3234,7 @@ static struct cmd *json_parse_cmd_add_flowtable(struct json_ctx *ctx,if (op !=CMD_DELETE &&json_unpack_err (ctx, root,"{s:s}" ,"name" , &h.flowtable.name)) {return NULL ; - }else if (op ==CMD_DELETE && + }else if ((op ==CMD_DELETE || op ==CMD_DESTROY ) &&json_unpack (root,"{s:s}" ,"name" , &h.flowtable.name) &&json_unpack (root,"{s:I}" ,"handle" , &h.handle.id)) {json_error (ctx,"Either name or handle required to delete a flowtable." );
@@ -3248,7 +3249,7 @@ static struct cmd *json_parse_cmd_add_flowtable(struct json_ctx *ctx,if (h.flowtable.name) h.flowtable.name =xstrdup (h.flowtable.name); -if (op ==CMD_DELETE || op ==CMD_LIST ) +if (op ==CMD_DELETE || op ==CMD_LIST || op ==CMD_DESTROY )return cmd_alloc (op, cmd_obj, &h, int_loc,NULL ); if (json_unpack_err(ctx, root, "{s:s, s:I}",
@@ -3337,7 +3338,7 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx, cmd_obj == NFT_OBJECT_CT_HELPER) && json_unpack_err(ctx, root, "{s:s}", "name", &h.obj.name)) {return NULL ; - } else if (op == CMD_DELETE && + } else if ((op == CMD_DELETE || op == CMD_DESTROY) && cmd_obj != NFT_OBJECT_CT_HELPER && json_unpack(root, "{s:s}", "name", &h.obj.name) && json_unpack(root, "{s:I}", "handle", &h.handle.id)) {
@@ -3353,7 +3354,7 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx,if (h.obj.name) h.obj.name =xstrdup (h.obj.name); -if (op ==CMD_DELETE || op ==CMD_LIST ) { +if (op ==CMD_DELETE || op ==CMD_LIST || op ==CMD_DESTROY ) {if (cmd_obj ==NFT_OBJECT_CT_HELPER )return cmd_alloc_obj_ct (op,NFT_OBJECT_CT_HELPER , &h, int_loc,obj_alloc (int_loc));
@@ -3866,6 +3867,7 @@ static struct cmd *json_parse_cmd(struct json_ctx *ctx, json_t *root) { "reset", CMD_RESET, json_parse_cmd_reset }, { "flush", CMD_FLUSH, json_parse_cmd_flush }, { "rename", CMD_RENAME, json_parse_cmd_rename }, + { "destroy", CMD_DESTROY, json_parse_cmd_add },//{ "export", CMD_EXPORT, json_parse_cmd_export }, //{ "monitor", CMD_MONITOR, json_parse_cmd_monitor }, //{ "describe", CMD_DESCRIBE, json_parse_cmd_describe }
src/rule.c +1 -0
@@ -2728,6 +2728,7 @@ int do_command(struct netlink_ctx *ctx, struct cmd *cmd)case CMD_REPLACE :return do_command_replace (ctx, cmd);case CMD_DELETE : +case CMD_DESTROY :return do_command_delete (ctx, cmd);case CMD_GET :return do_command_get (ctx, cmd);
src/scanner.l +1 -0
@@ -358,6 +358,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "import" { scanner_push_start_cond(yyscanner, SCANSTATE_CMD_IMPORT); return IMPORT; } "export" { scanner_push_start_cond(yyscanner, SCANSTATE_CMD_EXPORT); return EXPORT; } "monitor" { scanner_push_start_cond(yyscanner, SCANSTATE_CMD_MONITOR); return MONITOR; } +"destroy" { return DESTROY; } "position" { return POSITION; } "index" { return INDEX; }
tests/shell/testcases/rule_management/0011destroy_0 new file +8 -0
@@ -0,0 +1,8 @@ +#!/bin/bash + +set -e +$NFT add table t +$NFT add chain t c +$NFT insert rule t c accept # should have handle 2 + +$NFT destroy rule t c handle 2
tests/shell/testcases/rule_management/0012destroy_0 new file +7 -0
@@ -0,0 +1,7 @@ +#!/bin/bash + +set -e +$NFT add table t +$NFT add chain t c + +$NFT destroy rule t c handle 3333
tests/shell/testcases/rule_management/dumps/0011destroy_0.nft new file +4 -0
@@ -0,0 +1,4 @@ +table ip t { + chain c { + } +}
tests/shell/testcases/rule_management/dumps/0012destroy_0.nft new file +4 -0
@@ -0,0 +1,4 @@ +table ip t { + chain c { + } +}
Signature · 1 line
2.30.2
Quoted 6 lines · Fernando Fernandez Mancera
"destroy" command performs a deletion as "delete" command but does not fail when the object does not exist. As there is no NLM_F_* flag for ignoring such error, it needs to be ignored directly on error handling.
Example of use:
Quoted 13 lines
# nft list ruleset
table ip filter {
chain output {
}
}
# nft destroy table ip missingtable
# echo $?
0
# nft list ruleset
table ip filter {
chain output {
}
}Applied, thanks.
[...]
Quoted 4 lines
create mode 100755 tests/shell/testcases/rule_management/0011destroy_0 create mode 100755 tests/shell/testcases/rule_management/0012destroy_0 create mode 100644 tests/shell/testcases/rule_management/dumps/0011destroy_0.nft create mode 100644 tests/shell/testcases/rule_management/dumps/0012destroy_0.nft
More tests for other objects would be good to have.