nft: Use verbose flag to toggle debug output
Phil Sutter
reviewerPATCH 1/2 nft: Use verbose flag to toggle debug output
Copy legacy iptables' behaviour, printing debug output if verbose flag is given more than once.
Since nft debug output applies to netlink messages which are not created until nft_action() phase, carrying verbose value is non-trivial - introduce a field in struct nft_handle for that.
--- iptables/nft-shared.h | 1 - iptables/nft.c | 38 ++++++++++++++++++++------------------ iptables/nft.h | 1 + iptables/xtables.c | 1 + 4 files changed, 22 insertions(+), 19 deletions(-)
iptables/nft-shared.h +0 -1
@@ -13,7 +13,6 @@#include "xshared.h" #ifdef DEBUG -#define NLDEBUG #define DEBUG_DEL #endif
iptables/nft.c +20 -18
@@ -926,15 +926,16 @@ void nft_fini(struct nft_handle *h)mnl_socket_close (h->nl); } -static void nft_chain_print_debug (struct nftnl_chain *c,struct nlmsghdr *nlh) +static void nft_chain_print_debug (struct nft_handle *h, +struct nftnl_chain *c,struct nlmsghdr *nlh) { -#ifdef NLDEBUG -char tmp[1024 ]; - -nftnl_chain_snprintf (tmp,sizeof (tmp), c,0 ,0 ); -printf ("DEBUG: chain: %s\n" , tmp); -mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len,sizeof (struct nfgenmsg )); -#endif +if (h->verbose >1 ) { +nftnl_chain_fprintf (stdout, c,0 ,0 ); +fprintf (stdout,"\n" ); + } +if (h->verbose >2 ) +mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len, +sizeof (struct nfgenmsg )); } static struct nftnl_chain *nft_chain_new(struct nft_handle *h,
@@ -1386,15 +1387,16 @@ int add_log(struct nftnl_rule *r, struct iptables_command_state *cs)return 0 ; } -static void nft_rule_print_debug (struct nftnl_rule *r,struct nlmsghdr *nlh) +static void nft_rule_print_debug (struct nft_handle *h, +struct nftnl_rule *r,struct nlmsghdr *nlh) { -#ifdef NLDEBUG -char tmp[1024 ]; - -nftnl_rule_snprintf (tmp,sizeof (tmp), r,0 ,0 ); -printf ("DEBUG: rule: %s\n" , tmp); -mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len,sizeof (struct nfgenmsg )); -#endif +if (h->verbose >1 ) { +nftnl_rule_fprintf (stdout, r,0 ,0 ); +fprintf (stdout,"\n" ); + } +if (h->verbose >2 ) +mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len, +sizeof (struct nfgenmsg )); }int add_counters (struct nftnl_rule *r,uint64_t packets,uint64_t bytes)
@@ -2698,7 +2700,7 @@ static void nft_compat_chain_batch_add(struct nft_handle *h, uint16_t type, nlh =nftnl_chain_nlmsg_build_hdr (nftnl_batch_buffer (h->batch), type, h->family, flags, seq);nftnl_chain_nlmsg_build_payload (nlh, chain); -nft_chain_print_debug (chain, nlh); +nft_chain_print_debug (h, chain, nlh); } static void nft_compat_rule_batch_add(struct nft_handle *h, uint16_t type,
@@ -2710,7 +2712,7 @@ static void nft_compat_rule_batch_add(struct nft_handle *h, uint16_t type, nlh =nftnl_rule_nlmsg_build_hdr (nftnl_batch_buffer (h->batch), type, h->family, flags, seq);nftnl_rule_nlmsg_build_payload (nlh, rule); -nft_rule_print_debug (rule, nlh); +nft_rule_print_debug (h, rule, nlh); }static void batch_obj_del (struct nft_handle *h,struct obj_update *o)
iptables/nft.h +1 -0
@@ -109,6 +109,7 @@ struct nft_handle {int8_t config_done;struct list_head cmd_list;bool cache_init; +int verbose;/* meta data, for error reporting */ struct {
iptables/xtables.c +1 -0
@@ -163,6 +163,7 @@ int do_commandx(struct nft_handle *h, int argc, char *argv[], char **table, h->ops->init_cs (&cs);do_parse (argc, argv, &p, &cs, &args); + h->verbose = p.verbose;if (!nft_table_builtin_find (h, p.table)) xtables_error(VERSION_PROBLEM,
Signature · 1 line
2.34.1
Phil Sutter
reviewerPATCH 2/2 iptables-restore: Support for extra debug output
Treat --verbose just like iptables itself, increasing debug level with number of invocations.
To propagate the level into do_command() callback, insert virtual '-v' flags into rule lines.
The only downside of this is that simple verbose output is changed and now also prints the rules as they are added - which would be useful if the lines contained the chain they apply to.
--- iptables/iptables-restore.8.in | 1 + iptables/iptables-restore.c | 6 +++++- .../shell/testcases/ipt-restore/0014-verbose-restore_0 | 9 ++++++--- iptables/xtables-restore.c | 6 +++++- 4 files changed, 17 insertions(+), 5 deletions(-)
iptables/iptables-restore.8.in +1 -0
@@ -54,6 +54,7 @@ Only parse and construct the ruleset, but do not commit it. .TP \fB\-v\fP, \fB\-\-verbose\fP Print additional debug info during ruleset processing. +Specify multiple times to increase debug level. .TP \fB\-V\fP, \fB\-\-version\fP Print the program version number.
iptables/iptables-restore.c +5 -1
@@ -114,7 +114,7 @@ ip46tables_restore_main(const struct iptables_restore_cb *cb, counters =1 ;break ;case 'v' : - verbose =1 ; + verbose++;break ;case 'V' : printf("%s v%s\n",
@@ -317,11 +317,15 @@ ip46tables_restore_main(const struct iptables_restore_cb *cb,char *pcnt =NULL ;char *bcnt =NULL ;char *parsestart = buffer; +int i;add_argv (&av_store, argv[0 ],0 );add_argv (&av_store,"-t" ,0 );add_argv (&av_store, curtable,0 ); +for (i =0 ; !noflush && i < verbose; i++) +add_argv (&av_store,"-v" ,0 ); +tokenize_rule_counters (&parsestart, &pcnt, &bcnt, line);if (counters && pcnt && bcnt) {add_argv (&av_store,"--set-counters" ,0 );
iptables/tests/shell/testcases/ipt-restore/0014-verbose-restore_0 +6 -3
@@ -33,6 +33,7 @@ Flushing chain \`bar' Flushing chain \`foo' Deleting chain \`bar' Deleting chain \`foo' +ACCEPT all opt -- in * out * 0.0.0.0/0 -> 0.0.0.0/0 Flushing chain \`PREROUTING' Flushing chain \`INPUT' Flushing chain \`OUTPUT'
@@ -41,6 +42,7 @@ Flushing chain \`natbar' Flushing chain \`natfoo' Deleting chain \`natbar' Deleting chain \`natfoo' +ACCEPT all opt -- in * out * 0.0.0.0/0 -> 0.0.0.0/0 Flushing chain \`PREROUTING' Flushing chain \`OUTPUT' Flushing chain \`rawfoo'
@@ -58,9 +60,10 @@ Flushing chain \`OUTPUT' Flushing chain \`secfoo' Deleting chain \`secfoo'" -for ipt in iptables-restore ip6tables-restore; do - diff -u -Z <(echo "$EXPECT") <($XT_MULTI $ipt -v <<< "$DUMP") -done +EXPECT6=$(sed -e 's/0\.0\.0\.0/::/g' -e 's/opt --/opt /' <<< "$EXPECT") + +diff -u -Z <(echo "$EXPECT") <($XT_MULTI iptables-restore -v <<< "$DUMP") +diff -u -Z <(echo "$EXPECT6") <($XT_MULTI ip6tables-restore -v <<< "$DUMP") DUMP="*filter :baz - [0:0]
iptables/xtables-restore.c +5 -1
@@ -206,11 +206,15 @@ static void xtables_restore_parse_line(struct nft_handle *h,char *pcnt =NULL ;char *bcnt =NULL ;char *parsestart = buffer; +int i;add_argv (&state->av_store, xt_params->program_name,0 );add_argv (&state->av_store,"-t" ,0 );add_argv (&state->av_store, state->curtable->name,0 ); +for (i =0 ; !h->noflush && i < verbose; i++) +add_argv (&state->av_store,"-v" ,0 ); +tokenize_rule_counters (&parsestart, &pcnt, &bcnt, line);if (counters && pcnt && bcnt) {add_argv (&state->av_store,"--set-counters" ,0 );
@@ -309,7 +313,7 @@ xtables_restore_main(int family, const char *progname, int argc, char *argv[]) counters =1 ;break ;case 'v' : - verbose =1 ; + verbose++;break ;case 'V' :printf ("%s v%s\n" , prog_name, prog_vers);
Signature · 1 line
2.34.1
Pablo Neira Ayuso
maintainerQuoted 43 lines · Phil Sutter
Copy legacy iptables' behaviour, printing debug output if verbose flag is given more than once.
Since nft debug output applies to netlink messages which are not created until
nft_action()phase, carrying verbose value is non-trivial - introduce a field in structnft_handlefor that.Signed-off-by Phil Sutter <[email protected]>--- iptables/nft-shared.h | 1 - iptables/nft.c | 38 ++++++++++++++++++++------------------ iptables/nft.h | 1 + iptables/xtables.c | 1 + 4 files changed, 22 insertions(+), 19 deletions(-)iptables/nft-shared.h +0
-1@@ -13,7 +13,6 @@#include "xshared.h" #ifdef DEBUG -#define NLDEBUG #define DEBUG_DEL #endif iptables/nft.c +2
-5@@ -926,15 +926,16 @@ void nft_fini(struct nft_handle *h)mnl_socket_close (h->nl); } -static void nft_chain_print_debug (struct nftnl_chain *c,struct nlmsghdr *nlh) +static void nft_chain_print_debug (struct nft_handle *h, +struct nftnl_chain *c,struct nlmsghdr *nlh) { -#ifdef NLDEBUG -char tmp[1024 ]; - -nftnl_chain_snprintf (tmp,sizeof (tmp), c,0 ,0 );
iptables/nft.c void nft_fini(struct nft_handle *h) · line 938
Quoted 10 lines
iptables/nft.c +7
-3-printf ("DEBUG: chain: %s\n" , tmp); -mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len,sizeof (struct nfgenmsg )); -#endif +if (h->verbose >1 ) { +nftnl_chain_fprintf (stdout, c,0 ,0 ); +fprintf (stdout,"\n" ); + } +if (h->verbose >2 ) +mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len, +sizeof (struct nfgenmsg ));
OK, so -v is netlink byte printing and -vv means print netlink message too. LGTM.
I'd mention this in the commit description before applying.
Quoted 73 lines
iptables/nft.c +11
-10} static struct nftnl_chain *nft_chain_new(struct nft_handle *h,@@ -1386,15 +1387,16 @@ int add_log(struct nftnl_rule *r, struct iptables_command_state *cs)return 0 ; } -static void nft_rule_print_debug (struct nftnl_rule *r,struct nlmsghdr *nlh) +static void nft_rule_print_debug (struct nft_handle *h, +struct nftnl_rule *r,struct nlmsghdr *nlh) { -#ifdef NLDEBUG -char tmp[1024 ]; - -nftnl_rule_snprintf (tmp,sizeof (tmp), r,0 ,0 ); -printf ("DEBUG: rule: %s\n" , tmp); -mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len,sizeof (struct nfgenmsg )); -#endif +if (h->verbose >1 ) { +nftnl_rule_fprintf (stdout, r,0 ,0 ); +fprintf (stdout,"\n" ); + } +if (h->verbose >2 ) +mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len, +sizeof (struct nfgenmsg )); }int add_counters (struct nftnl_rule *r,uint64_t packets,uint64_t bytes)@@ -2698,7 +2700,7 @@ static void nft_compat_chain_batch_add(struct nft_handle *h, uint16_t type, nlh =nftnl_chain_nlmsg_build_hdr (nftnl_batch_buffer (h->batch), type, h->family, flags, seq);nftnl_chain_nlmsg_build_payload (nlh, chain); -nft_chain_print_debug (chain, nlh); +nft_chain_print_debug (h, chain, nlh); } static void nft_compat_rule_batch_add(struct nft_handle *h, uint16_t type,@@ -2710,7 +2712,7 @@ static void nft_compat_rule_batch_add(struct nft_handle *h, uint16_t type, nlh =nftnl_rule_nlmsg_build_hdr (nftnl_batch_buffer (h->batch), type, h->family, flags, seq);nftnl_rule_nlmsg_build_payload (nlh, rule); -nft_rule_print_debug (rule, nlh); +nft_rule_print_debug (h, rule, nlh); }static void batch_obj_del (struct nft_handle *h,struct obj_update *o)iptables/nft.h +1
-0@@ -109,6 +109,7 @@ struct nft_handle {int8_t config_done;struct list_head cmd_list;bool cache_init; +int verbose;/* meta data, for error reporting */ struct {iptables/xtables.c +1
-0@@ -163,6 +163,7 @@ int do_commandx(struct nft_handle *h, int argc, char *argv[], char **table, h->ops->init_cs (&cs);do_parse (argc, argv, &p, &cs, &args); + h->verbose = p.verbose;if (!nft_table_builtin_find (h, p.table)) xtables_error(VERSION_PROBLEM,Signature · 1 line
2.34.1
Phil Sutter
reviewerQuoted 1 line · Pablo Neira Ayuso
On Fri, Jan 28, 2022 at 09:36:59PM +0100, Phil Sutter wrote:
[...]
Quoted 13 lines
Quoted 13 lines
iptables/nft.c +4
-8-static void nft_chain_print_debug (struct nftnl_chain *c,struct nlmsghdr *nlh) +static void nft_chain_print_debug (struct nft_handle *h, +struct nftnl_chain *c,struct nlmsghdr *nlh) { -#ifdef NLDEBUG -char tmp[1024 ]; - -nftnl_chain_snprintf (tmp,sizeof (tmp), c,0 ,0 ); -printf ("DEBUG: chain: %s\n" , tmp); -mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len,sizeof (struct nfgenmsg )); -#endif +if (h->verbose >1 ) { +nftnl_chain_fprintf (stdout, c,0 ,0 );
Quoted 8 lines
iptables/nft.c void nft_fini(struct nft_handle *h) · line 938
Quoted 5 lines
iptables/nft.c +5
-0+fprintf (stdout,"\n" ); + } +if (h->verbose >2 ) +mnl_nlmsg_fprintf (stdout, nlh, nlh->nlmsg_len, +sizeof (struct nfgenmsg ));OK, so -v is netlink byte printing and -vv means print netlink message too. LGTM.
-v is "normal verbose output", -vv is also nftnl debug and -vvv is also netlink message dump.
Quoted 1 line
I'd mention this in the commit description before applying.
Your comment is proof this needs better documentation! :D Guess I'll describe the behaviour in iptables man page as well.
Thanks, Phil