]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
echo: Fix for added delays in rule updates
authorPhil Sutter <phil@nwl.cc>
Tue, 15 Aug 2017 11:59:12 +0000 (13:59 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 15 Aug 2017 12:03:36 +0000 (14:03 +0200)
The added cache update upon every command dealing with rules was a
bummer. Instead, perform the needed cache update only if echo option was
set.

Initially, I tried to perform the cache update from within
netlink_echo_callback(), but that turned into a mess since the shared
socket between cache_init() and mnl_batch_talk() would receive
unexpected new input. So instead update the cache from do_command_add(),
netlink_replace_rule_batch() and do_comand_insert() so it completes
before mnl_batch_talk() starts listening.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/evaluate.c
src/netlink.c
src/rule.c

index 64e14b8ba8d987b945aacf8493294a7ab9ae5cf6..0fad0913488d0e46d32c30c679cd440d21944454 100644 (file)
@@ -2965,10 +2965,6 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)
                handle_merge(&cmd->set->handle, &cmd->handle);
                return set_evaluate(ctx, cmd->set);
        case CMD_OBJ_RULE:
-               ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
-                                  ctx->msgs);
-               if (ret < 0)
-                       return ret;
                handle_merge(&cmd->rule->handle, &cmd->handle);
                return rule_evaluate(ctx, cmd->rule);
        case CMD_OBJ_CHAIN:
@@ -2983,11 +2979,6 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)
        case CMD_OBJ_COUNTER:
        case CMD_OBJ_QUOTA:
        case CMD_OBJ_CT_HELPER:
-               ret = cache_update(ctx->nf_sock, ctx->cache, cmd->op,
-                                  ctx->msgs);
-               if (ret < 0)
-                       return ret;
-
                return 0;
        default:
                BUG("invalid command object type %u\n", cmd->obj);
index f631c26b2b9ca53add0be0972421f89c1c866502..68f336255fc50012136d1e14d28413150c1708ad 100644 (file)
@@ -464,7 +464,16 @@ int netlink_replace_rule_batch(struct netlink_ctx *ctx, const struct handle *h,
                               const struct location *loc)
 {
        struct nftnl_rule *nlr;
-       int err, flags = ctx->octx->echo ? NLM_F_ECHO : 0;
+       int err, flags = 0;
+
+       if (ctx->octx->echo) {
+               err = cache_update(ctx->nf_sock, ctx->cache,
+                                  CMD_INVALID, ctx->msgs);
+               if (err < 0)
+                       return err;
+
+               flags |= NLM_F_ECHO;
+       }
 
        nlr = alloc_nftnl_rule(&rule->handle);
        netlink_linearize_rule(ctx, nlr, rule);
index 1bd5c80158b7b4e29f0a33ddc93e2a1d7c7933d2..38cd648eb24a898e8fb18a15c4efd041065a4994 100644 (file)
@@ -1017,8 +1017,16 @@ static int do_command_add(struct netlink_ctx *ctx, struct cmd *cmd, bool excl)
 {
        uint32_t flags = excl ? NLM_F_EXCL : 0;
 
-       if (ctx->octx->echo)
+       if (ctx->octx->echo) {
+               int ret;
+
+               ret = cache_update(ctx->nf_sock, ctx->cache,
+                                 cmd->obj, ctx->msgs);
+               if (ret < 0)
+                       return ret;
+
                flags |= NLM_F_ECHO;
+       }
 
        switch (cmd->obj) {
        case CMD_OBJ_TABLE:
@@ -1058,7 +1066,18 @@ static int do_command_replace(struct netlink_ctx *ctx, struct cmd *cmd)
 
 static int do_command_insert(struct netlink_ctx *ctx, struct cmd *cmd)
 {
-       uint32_t flags = ctx->octx->echo ? NLM_F_ECHO : 0;
+       uint32_t flags = 0;
+
+       if (ctx->octx->echo) {
+               int ret;
+
+               ret = cache_update(ctx->nf_sock, ctx->cache,
+                                 cmd->obj, ctx->msgs);
+               if (ret < 0)
+                       return ret;
+
+               flags |= NLM_F_ECHO;
+       }
 
        switch (cmd->obj) {
        case CMD_OBJ_RULE: