]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
xtables-restore: Minimize caching when flushing
authorPhil Sutter <phil@nwl.cc>
Wed, 25 Sep 2019 09:29:59 +0000 (11:29 +0200)
committerPhil Sutter <phil@nwl.cc>
Mon, 30 Sep 2019 06:14:55 +0000 (08:14 +0200)
Unless --noflush was given, xtables-restore merely needs the list of
tables to decide whether to delete it or not. Introduce nft_fake_cache()
function which populates table list, initializes chain lists (so
nft_chain_list_get() returns an empty list instead of NULL) and sets
'have_cache' to turn any later calls to nft_build_cache() into nops.

If --noflush was given, call nft_build_cache() just once instead of for
each table line in input.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Acked-by: Florian Westphal <fw@strlen.de>
iptables/nft.c
iptables/nft.h
iptables/xtables-restore.c

index fde1b2a3e057e3443fd9d850ac88c05e456dc20d..a2f36b7ee90d2931f38a10d44f5a6717a6e3973e 100644 (file)
@@ -1611,6 +1611,23 @@ retry:
        h->nft_genid = genid_start;
 }
 
+void nft_fake_cache(struct nft_handle *h)
+{
+       int i;
+
+       fetch_table_cache(h);
+       for (i = 0; i < NFT_TABLE_MAX; i++) {
+               enum nft_table_type type = h->tables[i].type;
+
+               if (!h->tables[i].name)
+                       continue;
+
+               h->cache->table[type].chains = nftnl_chain_list_alloc();
+       }
+       h->have_cache = true;
+       mnl_genid_get(h, &h->nft_genid);
+}
+
 void nft_build_cache(struct nft_handle *h)
 {
        if (!h->have_cache)
index 43463d7f262e843f70e4092b6485ca4c37c906d5..bcac8e228c7877bc6f769565084318e0dd372d80 100644 (file)
@@ -73,6 +73,7 @@ int mnl_talk(struct nft_handle *h, struct nlmsghdr *nlh,
             void *data);
 int nft_init(struct nft_handle *h, const struct builtin_table *t);
 void nft_fini(struct nft_handle *h);
+void nft_fake_cache(struct nft_handle *h);
 void nft_build_cache(struct nft_handle *h);
 
 /*
index 27e65b971727e775306e727ccc67824c3f680240..7f28a05c6861934b9ee09eac2bad6cc6699cc90d 100644 (file)
@@ -96,6 +96,11 @@ void xtables_restore_parse(struct nft_handle *h,
 
        line = 0;
 
+       if (!h->noflush)
+               nft_fake_cache(h);
+       else
+               nft_build_cache(h);
+
        /* Grab standard input. */
        while (fgets(buffer, sizeof(buffer), p->in)) {
                int ret = 0;
@@ -145,8 +150,6 @@ void xtables_restore_parse(struct nft_handle *h,
                        if (p->tablename && (strcmp(p->tablename, table) != 0))
                                continue;
 
-                       nft_build_cache(h);
-
                        if (h->noflush == 0) {
                                DEBUGP("Cleaning all chains of table '%s'\n",
                                        table);