From: Florian Westphal <fw@strlen.de>
To: <netfilter-devel@vger.kernel.org>
Cc: Florian Westphal <fw@strlen.de>
Subject: [PATCH xtables-nft 2/6] xtables: add skip flag to objects
Date: Tue, 23 Apr 2019 15:16:21 +0200 [thread overview]
Message-ID: <20190423131625.23377-3-fw@strlen.de> (raw)
In-Reply-To: <20190423131625.23377-1-fw@strlen.de>
This will be used to skip transaction objects when committing to
kernel. This is needed for example when we restore a table that
doesn't exist yet. In such a case we would already build a flush
operation so we can just enable it when we hit problem with the
generation id and we find that the table/chain was already created
in the mean time.
Signed-off-by: Florian Westphal <fw@strlen.de>
---
iptables/nft.c | 49 +++++++++++++++++++++++++++++++------------------
1 file changed, 31 insertions(+), 18 deletions(-)
diff --git a/iptables/nft.c b/iptables/nft.c
index 16d7b7951969..1cef7a13c90d 100644
--- a/iptables/nft.c
+++ b/iptables/nft.c
@@ -264,7 +264,8 @@ enum obj_action {
struct obj_update {
struct list_head head;
- enum obj_update_type type;
+ enum obj_update_type type:8;
+ uint8_t skip:1;
unsigned int seq;
union {
struct nftnl_table *table;
@@ -342,13 +343,13 @@ static int mnl_append_error(const struct nft_handle *h,
return snprintf(buf, len, "%s: %s", errmsg, tcr);
}
-static int batch_add(struct nft_handle *h, enum obj_update_type type, void *ptr)
+static struct obj_update *batch_add(struct nft_handle *h, enum obj_update_type type, void *ptr)
{
struct obj_update *obj;
obj = calloc(1, sizeof(struct obj_update));
if (obj == NULL)
- return -1;
+ return NULL;
obj->ptr = ptr;
obj->error.lineno = h->error.lineno;
@@ -356,11 +357,12 @@ static int batch_add(struct nft_handle *h, enum obj_update_type type, void *ptr)
list_add_tail(&obj->head, &h->obj_list);
h->obj_list_num++;
- return 0;
+ return obj;
}
-static int batch_table_add(struct nft_handle *h, enum obj_update_type type,
- struct nftnl_table *t)
+static struct obj_update *
+batch_table_add(struct nft_handle *h, enum obj_update_type type,
+ struct nftnl_table *t)
{
return batch_add(h, type, t);
}
@@ -368,13 +370,13 @@ static int batch_table_add(struct nft_handle *h, enum obj_update_type type,
static int batch_chain_add(struct nft_handle *h, enum obj_update_type type,
struct nftnl_chain *c)
{
- return batch_add(h, type, c);
+ return batch_add(h, type, c) ? 0 : -1;
}
static int batch_rule_add(struct nft_handle *h, enum obj_update_type type,
struct nftnl_rule *r)
{
- return batch_add(h, type, r);
+ return batch_add(h, type, r) ? 0 : -1;
}
const struct builtin_table xtables_ipv4[NFT_TABLE_MAX] = {
@@ -609,7 +611,7 @@ static int nft_table_builtin_add(struct nft_handle *h,
nftnl_table_set(t, NFTNL_TABLE_NAME, (char *)_t->name);
- ret = batch_table_add(h, NFT_COMPAT_TABLE_ADD, t);
+ ret = batch_table_add(h, NFT_COMPAT_TABLE_ADD, t) ? 0 : - 1;
return ret;
}
@@ -2000,9 +2002,10 @@ int nft_for_each_table(struct nft_handle *h,
return 0;
}
-static int __nft_table_flush(struct nft_handle *h, const char *table)
+static int __nft_table_flush(struct nft_handle *h, const char *table, bool exists)
{
const struct builtin_table *_t;
+ struct obj_update *obj;
struct nftnl_table *t;
t = nftnl_table_alloc();
@@ -2011,7 +2014,14 @@ static int __nft_table_flush(struct nft_handle *h, const char *table)
nftnl_table_set_str(t, NFTNL_TABLE_NAME, table);
- batch_table_add(h, NFT_COMPAT_TABLE_FLUSH, t);
+ obj = batch_table_add(h, NFT_COMPAT_TABLE_FLUSH, t);
+ if (!obj) {
+ nftnl_table_free(t);
+ return -1;
+ }
+
+ if (!exists)
+ obj->skip = 1;
_t = nft_table_builtin_find(h, table);
assert(_t);
@@ -2027,6 +2037,7 @@ int nft_table_flush(struct nft_handle *h, const char *table)
struct nftnl_table_list_iter *iter;
struct nftnl_table_list *list;
struct nftnl_table *t;
+ bool exists = false;
int ret = 0;
nft_fn = nft_table_flush;
@@ -2048,17 +2059,15 @@ int nft_table_flush(struct nft_handle *h, const char *table)
const char *table_name =
nftnl_table_get_str(t, NFTNL_TABLE_NAME);
- if (strcmp(table_name, table) != 0)
- goto next;
+ if (strcmp(table_name, table) == 0) {
+ exists = true;
+ break;
+ }
- ret = __nft_table_flush(h, table);
- if (ret < 0)
- goto err_table_iter;
-next:
t = nftnl_table_list_iter_next(iter);
}
-err_table_iter:
+ ret = __nft_table_flush(h, table, exists);
nftnl_table_list_iter_destroy(iter);
err_table_list:
nftnl_table_list_free(list);
@@ -2658,6 +2667,10 @@ static int nft_action(struct nft_handle *h, int action)
mnl_batch_begin(h->batch, seq++);
list_for_each_entry(n, &h->obj_list, head) {
+
+ if (n->skip)
+ continue;
+
n->seq = seq++;
switch (n->type) {
case NFT_COMPAT_TABLE_ADD:
--
2.21.0
next prev parent reply other threads:[~2019-04-23 13:22 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-23 13:16 [PATCH xtables-nft 0/6] handle parallel restore case Florian Westphal
2019-04-23 13:16 ` [PATCH xtables-nft 1/6] xtables: unify user chain add/flush for " Florian Westphal
2019-04-23 13:16 ` Florian Westphal [this message]
2019-04-23 13:16 ` [PATCH xtables-nft 3/6] xtables: add and use nft_build_cache Florian Westphal
2019-04-23 13:16 ` [PATCH xtables-nft 4/6] xtables: add and set "implict" flag on transaction objects Florian Westphal
2019-04-23 13:16 ` [PATCH xtables-nft 5/6] xtables: handle concurrent ruleset modifications Florian Westphal
2019-04-23 13:16 ` [PATCH xtables-nft 6/6] tests: add test script for race-free restore Florian Westphal
2019-04-26 17:05 ` [PATCH xtables-nft 0/6] handle parallel restore case Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190423131625.23377-3-fw@strlen.de \
--to=fw@strlen.de \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).