json_cmd_assoc and cmd
Pablo Neira Ayuso
maintainerHi Phil,
Why json_cmd_assoc is not placed in struct cmd instead? I mean, just store the json_t *json in cmd?
This code could be simplified if you store this information in the command object.
Thanks.
Phil Sutter
reviewerHi Pablo,
Quoted 2 lines · Pablo Neira Ayuso
Why
json_cmd_associs not placed in struct cmd instead? I mean, just store thejson_t*json in cmd?
The global list (json_cmd_list) is used in json_events_cb(). Unless I miss something, the cmd list is not available from struct netlink_mon_handler.
Maybe I could move struct cmds list head into struct nft_ctx?
Thanks, Phil
Pablo Neira Ayuso
maintainerQuoted 9 lines · Phil Sutter
Hi Pablo,
Quoted 2 lines · Pablo Neira Ayuso
Why
json_cmd_associs not placed in struct cmd instead? I mean, just store thejson_t*json in cmd?The global list (
json_cmd_list) is used injson_events_cb(). Unless I miss something, the cmd list is not available from structnetlink_mon_handler.
I see, thanks for explaining.
Quoted 1 line
Maybe I could move struct cmds list head into struct
nft_ctx?
Probably place this in netlink_ctx?
We could also store num_cmds there too.
BTW, not directly related to this, but isn't this strange?
list_for_each_entry(cmd, cmds, list) {
memset(&ctx, 0, sizeof(ctx));
ctx.msgs = msgs;
ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum);
ctx.batch = batch;
ctx.nft = nft;
init_list_head(&ctx.list);
ret = do_command(&ctx, cmd);
...ctx is reset over and over again. Then, recycled here:
ret = mnl_batch_talk(&ctx, &err_list, num_cmds);
I wonder if we can get this better.
Quoted 12 lines · Pablo Neira Ayuso
BTW, not directly related to this, but isn't this strange?
list_for_each_entry(cmd, cmds, list) { memset(&ctx, 0, sizeof(ctx)); ctx.msgs = msgs; ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum); ctx.batch = batch; ctx.nft = nft; init_list_head(&ctx.list); ret = do_command(&ctx, cmd); ...
Quoted 5 lines
ctx is reset over and over again. Then, recycled here:
ret = mnl_batch_talk(&ctx, &err_list, num_cmds);I wonder if we can get this better.
Something like this?
...
struct netlink_ctx ctx = { .msgs = msgs, .nft = nft };
...
ctx.batch = batch = mnl_batch_init();
batch_seqnum = mnl_batch_begin(batch, mnl_seqnum_alloc(&seqnum));
list_for_each_entry(cmd, cmds, list) {
ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum);
init_list_head(&ctx.list);
ret = do_command(&ctx, cmd);
...
}J.
Phil Sutter
reviewerHi,
Quoted 21 lines · Jeremy Sowden
Quoted 12 lines · Pablo Neira Ayuso
BTW, not directly related to this, but isn't this strange?
list_for_each_entry(cmd, cmds, list) { memset(&ctx, 0, sizeof(ctx)); ctx.msgs = msgs; ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum); ctx.batch = batch; ctx.nft = nft; init_list_head(&ctx.list); ret = do_command(&ctx, cmd); ...Quoted 5 lines
ctx is reset over and over again. Then, recycled here:
ret = mnl_batch_talk(&ctx, &err_list, num_cmds);I wonder if we can get this better.
Something like this?
Quoted 12 lines
...
struct netlink_ctx ctx = { .msgs = msgs, .nft = nft };
...
ctx.batch = batch = mnl_batch_init();
batch_seqnum = mnl_batch_begin(batch, mnl_seqnum_alloc(&seqnum));
list_for_each_entry(cmd, cmds, list) {
ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum);
init_list_head(&ctx.list);
ret = do_command(&ctx, cmd);
...
}Yes, that at least simplifies the foreach loop a bit. I wonder though if we could eliminate struct netlink_ctx altogether by moving pointers into struct nft_ctx.
Pablo, do you think that's feasible?
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 13 lines · Jeremy Sowden
Quoted 12 lines · Pablo Neira Ayuso
BTW, not directly related to this, but isn't this strange?
list_for_each_entry(cmd, cmds, list) { memset(&ctx, 0, sizeof(ctx)); ctx.msgs = msgs; ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum); ctx.batch = batch; ctx.nft = nft; init_list_head(&ctx.list); ret = do_command(&ctx, cmd); ...
Quoted 7 lines
Quoted 5 lines
ctx is reset over and over again. Then, recycled here:
ret = mnl_batch_talk(&ctx, &err_list, num_cmds);I wonder if we can get this better.
Something like this?
Yes, something like that would get things in better shape I think, more comments below.
Quoted 8 lines
struct netlink_ctx ctx = { .msgs = msgs, .nft = nft };
...
ctx.batch = batch = mnl_batch_init();
batch_seqnum = mnl_batch_begin(batch, mnl_seqnum_alloc(&seqnum));
list_for_each_entry(cmd, cmds, list) {
ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum);
init_list_head(&ctx.list);I think we don't need to re-initialize this list over and over again (from what I see when doing: git grep "ctx->list").
This always does list_splice_tail_init() to attach the object list where they belong.
You can probably add something like:
if (!list_empty(&ctx->list))
BUG("command list is not empty\n");I would make a patch and run tests/shell and tests/py to check if what I'm suggesting this fine :-)
Quoted 2 lines
ret = do_command(&ctx, cmd); ...
Thanks!
Quoted 34 lines · Pablo Neira Ayuso
Quoted 13 lines · Jeremy Sowden
Quoted 12 lines · Pablo Neira Ayuso
BTW, not directly related to this, but isn't this strange?
list_for_each_entry(cmd, cmds, list) { memset(&ctx, 0, sizeof(ctx)); ctx.msgs = msgs; ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum); ctx.batch = batch; ctx.nft = nft; init_list_head(&ctx.list); ret = do_command(&ctx, cmd); ...Quoted 7 lines
Quoted 5 lines
ctx is reset over and over again. Then, recycled here:
ret = mnl_batch_talk(&ctx, &err_list, num_cmds);I wonder if we can get this better.
Something like this?
Yes, something like that would get things in better shape I think, more comments below.
Quoted 8 lines
struct netlink_ctx ctx = { .msgs = msgs, .nft = nft }; ... ctx.batch = batch = mnl_batch_init(); batch_seqnum = mnl_batch_begin(batch, mnl_seqnum_alloc(&seqnum)); list_for_each_entry(cmd, cmds, list) { ctx.seqnum = cmd->seqnum = mnl_seqnum_alloc(&seqnum); init_list_head(&ctx.list);
Quoted 5 lines
I think we don't need to re-initialize this list over and over again (from what I see when doing: git grep "
ctx->list").This always does
list_splice_tail_init()to attach the object list where they belong.
Right. Got it.
Quoted 7 lines
You can probably add something like:
if (!list_empty(&ctx->list)) BUG("command list is not empty\n");I would make a patch and run tests/shell and tests/py to check if what I'm suggesting this fine :-)
I've compiled the changes I outlined above and run make check. Will add the list changes and submit a patch once I've tested them.
Quoted 2 lines
Quoted 2 lines
ret = do_command(&ctx, cmd); ...
J.
PATCH 1/1 libnftables: got rid of repeated initialization of netlink_ctx variable in loop.
In reply to Pablo Neira AyusoMost members in the context doesn't change, so there is no need to memset it and reassign most of its members on every iteration. Moved that code out of the loop.
--- src/libnftables.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
src/libnftables.c +4 -7
@@ -23,7 +23,7 @@ static int nft_netlink(struct nft_ctx *nft, {uint32_t batch_seqnum, seqnum =0 , num_cmds =0 ;struct nftnl_batch *batch; -struct netlink_ctx ctx; +struct netlink_ctx ctx = { .msgs = msgs, .nft = nft };struct cmd *cmd;struct mnl_err *err, *tmp;LIST_HEAD (err_list);
@@ -32,16 +32,13 @@ static int nft_netlink(struct nft_ctx *nft,if (list_empty (cmds))return 0 ; - batch =mnl_batch_init (); +init_list_head (&ctx.list); + + ctx.batch = batch =mnl_batch_init (); batch_seqnum =mnl_batch_begin (batch,mnl_seqnum_alloc (&seqnum));list_for_each_entry (cmd, cmds, list) { -memset (&ctx,0 ,sizeof (ctx)); - ctx.msgs = msgs; ctx.seqnum = cmd->seqnum =mnl_seqnum_alloc (&seqnum); - ctx.batch = batch; - ctx.nft = nft; -init_list_head (&ctx.list); ret =do_command (&ctx, cmd);if (ret <0 ) { netlink_io_error(&ctx, &cmd->location,
Signature · 1 line
2.20.1
Phil Sutter
reviewerRe: [PATCH nft] libnftables: got rid of repeated initialization of netlink_ctx variable in loop.
Hi,
Quoted 5 lines · Jeremy Sowden
Most members in the context doesn't change, so there is no need to memset it and reassign most of its members on every iteration. Moved that code out of the loop.
Fixes 49900d448ac9 ("libnftables: Move library stuff out of main.c")
This commit merely moved the code from src/main.c into the current location. I would rather point at a72315d2bad47 ("src: add rule batching support"), which introduced the loop (and already contains some of the pointless assignments). Note that commit is from 2013. :)
Quoted 11 lines
Reported-by Pablo Neira Ayuso <[email protected]>Signed-off-by Jeremy Sowden <[email protected]>--- src/libnftables.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)src/libnftables.c +0
-0
src/libnftables.c static int nft_netlink(struct nft_ctx *nft, · line 26
Quoted 5 lines
src/libnftables.c +1
-1@@ -23,7 +23,7 @@ static int nft_netlink(struct nft_ctx *nft, {uint32_t batch_seqnum, seqnum =0 , num_cmds =0 ;struct nftnl_batch *batch; -struct netlink_ctx ctx; +struct netlink_ctx ctx = { .msgs = msgs, .nft = nft };
Use '.list = LIST_HEAD_INIT(ctx.list),' here. Refer to cache_update() in src/rule.c for inspiration. :)
src/libnftables.c static int nft_netlink(struct nft_ctx *nft, · line 35
Quoted 9 lines
src/libnftables.c +1
-1struct cmd *cmd;struct mnl_err *err, *tmp;LIST_HEAD (err_list);@@ -32,16 +32,13 @@ static int nft_netlink(struct nft_ctx *nft,if (list_empty (cmds))return 0 ; - batch =mnl_batch_init (); +init_list_head (&ctx.list);
Drop this.
src/libnftables.c static int nft_netlink(struct nft_ctx *nft, · line 37
Quoted 2 lines
src/libnftables.c +2
-0+ + ctx.batch = batch =mnl_batch_init ();
Move ctx.batch init into declaration as well. Drop dedicated 'batch' variable and replace by 'ctx.batch'.
Thanks, Phil
v2 cover netlink_ctx initialization fixes.
A couple of patches to tidy up initialization of a pair of netlink_ctx variables.
Since v1 (based on Phil's feedback -- thanks, Phil):
* Eliminated batch local variable from first patch.
* Do initialization of .list and .batch into struct initializer from first patch.
* Updated commit in "Fixes:" tag in first patch.
* Added second patch.
Jeremy Sowden (2):
libnftables: got rid of repeated initialization of netlink_ctx
variable in loop.
rule: removed duplicate member initializer.
src/libnftables.c | 23 ++++++++++------------- src/rule.c | 1 - 2 files changed, 10 insertions(+), 14 deletions(-)
Signature · 1 line
2.20.1
PATCH v2 1/2 libnftables: got rid of repeated initialization of netlink_ctx variable in loop.
Most members in the context doesn't change, so there is no need to memset it and reassign them on every iteration. Moved that code out of the loop.
--- src/libnftables.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
src/libnftables.c +10 -13
@@ -22,8 +22,12 @@ static int nft_netlink(struct nft_ctx *nft, struct mnl_socket *nf_sock) {uint32_t batch_seqnum, seqnum =0 , num_cmds =0 ; -struct nftnl_batch *batch; -struct netlink_ctx ctx; +struct netlink_ctx ctx = { + .nft = nft, + .msgs = msgs, + .list =LIST_HEAD_INIT (ctx.list), + .batch =mnl_batch_init (), + };struct cmd *cmd;struct mnl_err *err, *tmp;LIST_HEAD (err_list);
@@ -32,16 +36,9 @@ static int nft_netlink(struct nft_ctx *nft,if (list_empty (cmds))return 0 ; - batch =mnl_batch_init (); - - batch_seqnum =mnl_batch_begin (batch,mnl_seqnum_alloc (&seqnum)); + batch_seqnum =mnl_batch_begin (ctx.batch,mnl_seqnum_alloc (&seqnum));list_for_each_entry (cmd, cmds, list) { -memset (&ctx,0 ,sizeof (ctx)); - ctx.msgs = msgs; ctx.seqnum = cmd->seqnum =mnl_seqnum_alloc (&seqnum); - ctx.batch = batch; - ctx.nft = nft; -init_list_head (&ctx.list); ret =do_command (&ctx, cmd);if (ret <0 ) { netlink_io_error(&ctx, &cmd->location,
@@ -52,9 +49,9 @@ static int nft_netlink(struct nft_ctx *nft, num_cmds++; }if (!nft->check) -mnl_batch_end (batch,mnl_seqnum_alloc (&seqnum)); +mnl_batch_end (ctx.batch,mnl_seqnum_alloc (&seqnum)); -if (!mnl_batch_ready (batch)) +if (!mnl_batch_ready (ctx.batch)) goto out; ret =mnl_batch_talk (&ctx, &err_list, num_cmds);
@@ -83,7 +80,7 @@ static int nft_netlink(struct nft_ctx *nft, } } out: -mnl_batch_reset (batch); +mnl_batch_reset (ctx.batch);return ret; }
Signature · 1 line
2.20.1
Re: [PATCH nft v2 1/2] libnftables: got rid of repeated initialization of netlink_ctx variable in loop.
Quoted 7 lines · Jeremy Sowden
Most members in the context doesn't change, so there is no need to memset it and reassign them on every iteration. Moved that code out of the loop.
Fixes a72315d2bad4 ("src: add rule batching support")Reported-by Pablo Neira Ayuso <[email protected]>Signed-off-by Jeremy Sowden <[email protected]>
PATCH v2 2/2 rule: removed duplicate member initializer.
In reply to Jeremy SowdenInitialization of a netlink_ctx included two initializers for .nft. Removed one of them.
--- src/rule.c | 1 - 1 file changed, 1 deletion(-)
src/rule.c +0 -1
@@ -240,7 +240,6 @@ int cache_update(struct nft_ctx *nft, unsigned int flags, struct list_head *msgs .list = LIST_HEAD_INIT(ctx.list), .nft = nft, .msgs = msgs, - .nft = nft, };struct nft_cache *cache = &nft->cache;uint32_t genid, genid_stop;
Signature · 1 line
2.20.1
Re: [PATCH nft v2 2/2] rule: removed duplicate member initializer.
Quoted 2 lines · Jeremy Sowden
Initialization of a
netlink_ctxincluded two initializers for .nft. Removed one of them.
Oh, good catch! The fact that I stared ad this code ten minutes ago and didn't see it speaks for itself. :)
Quoted 2 lines
Fixes 2dc07bcd7eaa ("src: pass struct nft_ctx through struct netlink_ctx")Signed-off-by Jeremy Sowden <[email protected]>
Re: [PATCH nft v2 0/2] netlink_ctx initialization fixes.
In reply to Jeremy SowdenQuoted 2 lines · Jeremy Sowden
A couple of patches to tidy up initialization of a pair of
netlink_ctxvariables.
Applied, thanks.