src: add import operation
RFC PATCH 1/1 src: add import operation
The import operation reads XML/JSON data from stdin.
A basic way to test is:
% nft export json | nft import json
This operation flush the kernel ruleset before adding the one imported.
Adding data from a file:
% cat file.json | nft import json
% nft import json < file.json
--- include/mnl.h | 23 ++++ include/netlink.h | 12 ++ include/rule.h | 2 src/evaluate.c | 1 src/mnl.c | 278 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/netlink.c | 195 +++++++++++++++++++++++++++++++++++++ src/parser.y | 21 +++- src/rule.c | 50 ++++++++++ src/scanner.l | 1 9 files changed, 579 insertions(+), 4 deletions(-)
include/mnl.h +23 -0
@@ -23,6 +23,7 @@ void mnl_batch_end(void);int mnl_batch_talk (struct mnl_socket *nl,struct list_head *err_list);int mnl_nft_rule_batch_add (struct nft_rule *nlr,unsigned int flags,uint32_t seqnum); +int mnl_nft_rule_list_add (struct nft_rule_list *nlrl,unsigned int flags);int mnl_nft_rule_batch_del (struct nft_rule *nlr,unsigned int flags,uint32_t seqnum);
@@ -35,8 +36,13 @@ struct nft_rule_list *mnl_nft_rule_dump(struct mnl_socket *nf_sock,int mnl_nft_chain_add (struct mnl_socket *nf_sock,struct nft_chain *nlc,unsigned int flags); +int mnl_nft_chain_list_add (struct mnl_socket *nf_sock, +struct nft_chain_list *nlcl, +unsigned int flags);int mnl_nft_chain_delete (struct mnl_socket *nf_sock,struct nft_chain *nlc,unsigned int flags); +int mnl_nft_chain_list_delete (struct mnl_socket *nfsock, +struct nft_chain_list *nlcl,unsigned int flags);struct nft_chain_list *mnl_nft_chain_dump (struct mnl_socket *nf_sock,int family); int mnl_nft_chain_get(struct mnl_socket *nf_sock, struct nft_chain *nlc,
@@ -44,8 +50,13 @@ int mnl_nft_chain_get(struct mnl_socket *nf_sock, struct nft_chain *nlc,int mnl_nft_table_add (struct mnl_socket *nf_sock,struct nft_table *nlt,unsigned int flags); +int mnl_nft_table_list_add (struct mnl_socket *nf_sock, +struct nft_table_list *nltl, +unsigned int flags);int mnl_nft_table_delete (struct mnl_socket *nf_sock,struct nft_table *nlt,unsigned int flags); +int mnl_nft_table_list_delete (struct mnl_socket *nfsock, +struct nft_table_list *nltl,unsigned int flags);struct nft_table_list *mnl_nft_table_dump (struct mnl_socket *nf_sock,int family); int mnl_nft_table_get(struct mnl_socket *nf_sock, struct nft_table *nlt,
@@ -53,18 +64,30 @@ int mnl_nft_table_get(struct mnl_socket *nf_sock, struct nft_table *nlt,int mnl_nft_set_add (struct mnl_socket *nf_sock,struct nft_set *nls,unsigned int flags); +int mnl_nft_set_list_add (struct mnl_socket *nf_sock, +struct nft_set_list *nlsl, +unsigned int flags);int mnl_nft_set_delete (struct mnl_socket *nf_sock,struct nft_set *nls,unsigned int flags); +int mnl_nft_set_list_delete (struct mnl_socket *nfsock, +struct nft_set_list *nlsl,unsigned int flags);struct nft_set_list *mnl_nft_set_dump (struct mnl_socket *nf_sock,int family,const char *table);int mnl_nft_set_get (struct mnl_socket *nf_sock,struct nft_set *nls);int mnl_nft_setelem_add (struct mnl_socket *nf_sock,struct nft_set *nls,unsigned int flags); +int mnl_nft_set_elem_list_add (struct mnl_socket *nf_sock, +struct nft_set_list *nlsl, +unsigned int flags);int mnl_nft_setelem_delete (struct mnl_socket *nf_sock,struct nft_set *nls,unsigned int flags);int mnl_nft_setelem_get (struct mnl_socket *nf_sock,struct nft_set *nls);struct nft_ruleset *mnl_nft_ruleset_dump (struct mnl_socket *nf_sock,uint32_t family); +int mnl_nft_ruleset_add (struct mnl_socket *nf_sock,struct nft_ruleset *rs, +unsigned int tflags,unsigned int cflags, +unsigned int rflags,unsigned int sflags, +unsigned int seflags);#endif /* _NFTABLES_MNL_H_ */
include/netlink.h +12 -0
@@ -139,4 +139,16 @@ extern int netlink_io_error(struct netlink_ctx *ctx,extern struct nft_ruleset *netlink_dump_ruleset (struct netlink_ctx *ctx,const struct handle *h,const struct location *loc); +extern int netlink_flush_ruleset (struct netlink_ctx *ctx, +const struct handle *h, +const struct location *loc); +extern int netlink_delete_ruleset (struct netlink_ctx *ctx, +const struct handle *h, +const struct location *loc); +extern int netlink_wipe_ruleset (struct netlink_ctx *ctx, +const struct handle *h, +const struct location *loc); +extern int netlink_add_ruleset (struct netlink_ctx *ctx, +const struct location *loc, +struct nft_ruleset *rs);#endif /* NFTABLES_NETLINK_H */
include/rule.h +2 -0
@@ -205,6 +205,7 @@ extern void set_print(const struct set *set);* @CMD_FLUSH: flush container * @CMD_RENAME: rename object * @CMD_EXPORT: export the ruleset in a given format +* @CMD_IMPORT: import a ruleset in a given format */ enum cmd_ops {CMD_INVALID ,
@@ -215,6 +216,7 @@ enum cmd_ops {CMD_FLUSH ,CMD_RENAME ,CMD_EXPORT , + CMD_IMPORT, };/**
src/evaluate.c +1 -0
@@ -1392,6 +1392,7 @@ static int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)case CMD_FLUSH :case CMD_RENAME :case CMD_EXPORT : +case CMD_IMPORT :return 0 ;default :BUG ("invalid command operation %u\n" , cmd->op);
src/mnl.c +278 -0
329 lines not on this page · raw message
src/netlink.c +195 -0
204 lines not on this page · raw message
src/parser.y +17 -4
@@ -175,6 +175,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token RENAME "rename" %token DESCRIBE "describe" %token EXPORT "export" +%token IMPORT "import" %token ACCEPT "accept" %token DROP "drop"
@@ -346,8 +347,8 @@ static void location_update(struct location *loc, struct location *rhs, int n) %type <cmd> line %destructor { cmd_free($$); } line -%type <cmd> base_cmd add_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd -%destructor { cmd_free($$); } base_cmd add_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd +%type <cmd> base_cmd add_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd import_cmd +%destructor { cmd_free($$); } base_cmd add_cmd insert_cmd delete_cmd list_cmd flush_cmd rename_cmd export_cmd import_cmd %type <handle> table_spec tables_spec chain_spec chain_identifier ruleid_spec %destructor { handle_free(&$$); } table_spec tables_spec chain_spec chain_identifier ruleid_spec
@@ -467,8 +468,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) %destructor { expr_free($$); } ct_expr %type <val> ct_key -%type <val> export_format - +%type <val> export_format import_format %% input : /* empty */
@@ -537,6 +537,7 @@ base_cmd : /* empty */ add_cmd { $$ = $1; } | FLUSH flush_cmd { $$ = $2; } | RENAME rename_cmd { $$ = $2; } | EXPORT export_cmd { $$ = $2; } + | IMPORT import_cmd { $$ = $2; } | DESCRIBE primary_expr { expr_describe($2);
@@ -679,6 +680,15 @@ export_cmd : export_format } ; + +import_cmd : import_format + { + struct handle h = { .family = NFPROTO_UNSPEC }; + $$ = cmd_alloc(CMD_IMPORT, CMD_OBJ_RULESET, &h, &@$, NULL); + $$->format = $1; + } + ; + table_block_alloc : /* empty */ { $$ = table_alloc();
@@ -1843,6 +1853,9 @@ mh_hdr_field : NEXTHDR { $$ = MHHDR_NEXTHDR; } | CHECKSUM { $$ = MHHDR_CHECKSUM; } ; +import_format : export_format + ; + export_format : XML { $$ = NFT_OUTPUT_XML; } | JSON { $$ = NFT_OUTPUT_JSON; } ;
src/rule.c +50 -0
68 lines not on this page · raw message
src/scanner.l +1 -0
@@ -254,6 +254,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "flush" { return FLUSH; } "rename" { return RENAME; } "export" { return EXPORT; } +"import" { return IMPORT; } "position" { return POSITION; }
Quoted 10 lines · Arturo Borrero Gonzalez
The import operation reads XML/JSON data from stdin.
A basic way to test is:
% nft export json | nft import jsonThis operation flush the kernel ruleset before adding the one imported.
Adding data from a file:
% cat file.json | nft import json
% nft import json < file.json
OK I think I understand your -EBUSY problem. We're destroying the rules' expressions from within the RCU callback, so its happening asynchronous. You most likely don't get EBUSY when deleting the table, but when deleting the set because the bindings from the lookup rules are not released yet. You then will *also* get -EBUSY from table deletion.
Basically I see these possibilities to fix this:
- Use synchronize_rcu() instead of call_rcu() for rule deletion. We added the call_rcu() because deletion performance was suffering badly from lots of synchronize_rcu() calls. This is obviously not a problem anymore with batched deletion.
- Use synchronize_rcu() before deleting sets. Same effect, but in case someone really does unbatched deletes, it will very likely result in less synchronize_rcu() calls.
Regarding this patch, I don't think we should add this before we support atomic replacement of tables, chains and sets as well. IOW, this needs quite a bit of kernel work.
Quoted 20 lines · Patrick McHardy
Quoted 10 lines · Arturo Borrero Gonzalez
The import operation reads XML/JSON data from stdin.
A basic way to test is:
% nft export json | nft import jsonThis operation flush the kernel ruleset before adding the one imported.
Adding data from a file:
% cat file.json | nft import json
% nft import json < file.jsonOK I think I understand your
-EBUSYproblem. We're destroying the rules' expressions from within the RCU callback, so its happening asynchronous. You most likely don't get EBUSY when deleting the table, but when deleting the set because the bindings from the lookup rules are not released yet. You then will *also* get-EBUSYfrom table deletion.Basically I see these possibilities to fix this:
Quoted 8 lines
- Use
synchronize_rcu()instead ofcall_rcu()for rule deletion. We added thecall_rcu()because deletion performance was suffering badly from lots ofsynchronize_rcu()calls. This is obviously not a problem anymore with batched deletion.- Use
synchronize_rcu()before deleting sets. Same effect, but in case someone really does unbatched deletes, it will very likely result in lesssynchronize_rcu()calls.
Actually this one is nonsense. synchronize_rcu() doesn't guarantee that a scheduled RCU callback has already returned. I'd suggest to switch back rule deletion to use synchronize_rcu().
Basically our API is currently broken since it contains a race condition. Userspace must be able to assume that the referenced objects can be removed after the DELRULE command has completed.
Quoted 3 lines
Regarding this patch, I don't think we should add this before we support atomic replacement of tables, chains and sets as well. IOW, this needs quite a bit of kernel work.