|
|
029dc7 |
From 25eff54c9a82ad816f7a4c274d39daf2e1aaf3a7 Mon Sep 17 00:00:00 2001
|
|
|
029dc7 |
From: Phil Sutter <phil@nwl.cc>
|
|
|
029dc7 |
Date: Thu, 20 Dec 2018 16:09:15 +0100
|
|
|
029dc7 |
Subject: [PATCH] xtables: Optimize nft_chain_zero_counters()
|
|
|
029dc7 |
|
|
|
029dc7 |
If a chain name was given, make use of nftnl_chain_list_lookup_byname().
|
|
|
029dc7 |
Streamline nft_chain_zero_rule_counters() to be suitable for calling
|
|
|
029dc7 |
from nftnl_chain_list_foreach().
|
|
|
029dc7 |
|
|
|
029dc7 |
There is an unrelated optimization in here, too: Add batch job
|
|
|
029dc7 |
NFT_COMPAT_CHAIN_ZERO only if it is a base chain. Since user-defined
|
|
|
029dc7 |
chains don't have counters, there is no need to do anything for them.
|
|
|
029dc7 |
|
|
|
029dc7 |
Signed-off-by: Phil Sutter <phil@nwl.cc>
|
|
|
029dc7 |
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
|
|
|
029dc7 |
(cherry picked from commit a6ce0c65d3a390bfff16e834c18650beedecf40c)
|
|
|
029dc7 |
Signed-off-by: Phil Sutter <psutter@redhat.com>
|
|
|
029dc7 |
---
|
|
|
029dc7 |
iptables/nft.c | 72 +++++++++++++++++++++++++-------------------------
|
|
|
029dc7 |
1 file changed, 36 insertions(+), 36 deletions(-)
|
|
|
029dc7 |
|
|
|
029dc7 |
diff --git a/iptables/nft.c b/iptables/nft.c
|
|
|
029dc7 |
index a23acbcc9b100..9951bf3212197 100644
|
|
|
029dc7 |
--- a/iptables/nft.c
|
|
|
029dc7 |
+++ b/iptables/nft.c
|
|
|
029dc7 |
@@ -2939,15 +2939,36 @@ int nft_xtables_config_load(struct nft_handle *h, const char *filename,
|
|
|
029dc7 |
return h->config_done;
|
|
|
029dc7 |
}
|
|
|
029dc7 |
|
|
|
029dc7 |
-static void nft_chain_zero_rule_counters(struct nft_handle *h,
|
|
|
029dc7 |
- struct nftnl_chain *c)
|
|
|
029dc7 |
+struct chain_zero_data {
|
|
|
029dc7 |
+ struct nft_handle *handle;
|
|
|
029dc7 |
+ bool verbose;
|
|
|
029dc7 |
+};
|
|
|
029dc7 |
+
|
|
|
029dc7 |
+static int __nft_chain_zero_counters(struct nftnl_chain *c, void *data)
|
|
|
029dc7 |
{
|
|
|
029dc7 |
+ struct chain_zero_data *d = data;
|
|
|
029dc7 |
+ struct nft_handle *h = d->handle;
|
|
|
029dc7 |
struct nftnl_rule_iter *iter;
|
|
|
029dc7 |
struct nftnl_rule *r;
|
|
|
029dc7 |
+ int ret = 0;
|
|
|
029dc7 |
+
|
|
|
029dc7 |
+ if (d->verbose)
|
|
|
029dc7 |
+ fprintf(stdout, "Zeroing chain `%s'\n",
|
|
|
029dc7 |
+ nftnl_chain_get_str(c, NFTNL_CHAIN_NAME));
|
|
|
029dc7 |
+
|
|
|
029dc7 |
+ if (nftnl_chain_is_set(c, NFTNL_CHAIN_HOOKNUM)) {
|
|
|
029dc7 |
+ /* zero base chain counters. */
|
|
|
029dc7 |
+ nftnl_chain_set_u64(c, NFTNL_CHAIN_PACKETS, 0);
|
|
|
029dc7 |
+ nftnl_chain_set_u64(c, NFTNL_CHAIN_BYTES, 0);
|
|
|
029dc7 |
+ nftnl_chain_unset(c, NFTNL_CHAIN_HANDLE);
|
|
|
029dc7 |
+ ret = batch_chain_add(h, NFT_COMPAT_CHAIN_ZERO, c);
|
|
|
029dc7 |
+ if (ret)
|
|
|
029dc7 |
+ return -1;
|
|
|
029dc7 |
+ }
|
|
|
029dc7 |
|
|
|
029dc7 |
iter = nftnl_rule_iter_create(c);
|
|
|
029dc7 |
if (iter == NULL)
|
|
|
029dc7 |
- return;
|
|
|
029dc7 |
+ return -1;
|
|
|
029dc7 |
|
|
|
029dc7 |
r = nftnl_rule_iter_next(iter);
|
|
|
029dc7 |
while (r != NULL) {
|
|
|
029dc7 |
@@ -2989,13 +3010,17 @@ static void nft_chain_zero_rule_counters(struct nft_handle *h,
|
|
|
029dc7 |
}
|
|
|
029dc7 |
|
|
|
029dc7 |
nftnl_rule_iter_destroy(iter);
|
|
|
029dc7 |
+ return 0;
|
|
|
029dc7 |
}
|
|
|
029dc7 |
|
|
|
029dc7 |
int nft_chain_zero_counters(struct nft_handle *h, const char *chain,
|
|
|
029dc7 |
const char *table, bool verbose)
|
|
|
029dc7 |
{
|
|
|
029dc7 |
struct nftnl_chain_list *list;
|
|
|
029dc7 |
- struct nftnl_chain_list_iter *iter;
|
|
|
029dc7 |
+ struct chain_zero_data d = {
|
|
|
029dc7 |
+ .handle = h,
|
|
|
029dc7 |
+ .verbose = verbose,
|
|
|
029dc7 |
+ };
|
|
|
029dc7 |
struct nftnl_chain *c;
|
|
|
029dc7 |
int ret = 0;
|
|
|
029dc7 |
|
|
|
029dc7 |
@@ -3003,41 +3028,16 @@ int nft_chain_zero_counters(struct nft_handle *h, const char *chain,
|
|
|
029dc7 |
if (list == NULL)
|
|
|
029dc7 |
goto err;
|
|
|
029dc7 |
|
|
|
029dc7 |
- iter = nftnl_chain_list_iter_create(list);
|
|
|
029dc7 |
- if (iter == NULL)
|
|
|
029dc7 |
- goto err;
|
|
|
029dc7 |
-
|
|
|
029dc7 |
- c = nftnl_chain_list_iter_next(iter);
|
|
|
029dc7 |
- while (c != NULL) {
|
|
|
029dc7 |
- const char *chain_name =
|
|
|
029dc7 |
- nftnl_chain_get(c, NFTNL_CHAIN_NAME);
|
|
|
029dc7 |
-
|
|
|
029dc7 |
- if (chain != NULL && strcmp(chain, chain_name) != 0)
|
|
|
029dc7 |
- goto next;
|
|
|
029dc7 |
-
|
|
|
029dc7 |
- if (verbose)
|
|
|
029dc7 |
- fprintf(stdout, "Zeroing chain `%s'\n", chain_name);
|
|
|
029dc7 |
-
|
|
|
029dc7 |
- if (nftnl_chain_is_set(c, NFTNL_CHAIN_HOOKNUM)) {
|
|
|
029dc7 |
- /* zero base chain counters. */
|
|
|
029dc7 |
- nftnl_chain_set_u64(c, NFTNL_CHAIN_PACKETS, 0);
|
|
|
029dc7 |
- nftnl_chain_set_u64(c, NFTNL_CHAIN_BYTES, 0);
|
|
|
029dc7 |
- }
|
|
|
029dc7 |
-
|
|
|
029dc7 |
- nft_chain_zero_rule_counters(h, c);
|
|
|
029dc7 |
-
|
|
|
029dc7 |
- nftnl_chain_unset(c, NFTNL_CHAIN_HANDLE);
|
|
|
029dc7 |
-
|
|
|
029dc7 |
- ret = batch_chain_add(h, NFT_COMPAT_CHAIN_ZERO, c);
|
|
|
029dc7 |
+ if (chain) {
|
|
|
029dc7 |
+ c = nftnl_chain_list_lookup_byname(list, chain);
|
|
|
029dc7 |
+ if (!c)
|
|
|
029dc7 |
+ return 0;
|
|
|
029dc7 |
|
|
|
029dc7 |
- if (chain != NULL)
|
|
|
029dc7 |
- break;
|
|
|
029dc7 |
-next:
|
|
|
029dc7 |
- c = nftnl_chain_list_iter_next(iter);
|
|
|
029dc7 |
+ ret = __nft_chain_zero_counters(c, &d);
|
|
|
029dc7 |
+ goto err;
|
|
|
029dc7 |
}
|
|
|
029dc7 |
|
|
|
029dc7 |
- nftnl_chain_list_iter_destroy(iter);
|
|
|
029dc7 |
-
|
|
|
029dc7 |
+ ret = nftnl_chain_list_foreach(list, __nft_chain_zero_counters, &d);
|
|
|
029dc7 |
err:
|
|
|
029dc7 |
/* the core expects 1 for success and 0 for error */
|
|
|
029dc7 |
return ret == 0 ? 1 : 0;
|
|
|
029dc7 |
--
|
|
|
029dc7 |
2.21.0
|
|
|
029dc7 |
|