Commit ff3cd7b3 authored by Pablo Neira Ayuso's avatar Pablo Neira Ayuso

netfilter: nf_tables: refactor chain statistic routines

Add new routines to encapsulate chain statistics allocation and
replacement.
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
parent 958bee14
...@@ -805,8 +805,7 @@ static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { ...@@ -805,8 +805,7 @@ static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
[NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
}; };
static int static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
{ {
struct nlattr *tb[NFTA_COUNTER_MAX+1]; struct nlattr *tb[NFTA_COUNTER_MAX+1];
struct nft_stats __percpu *newstats; struct nft_stats __percpu *newstats;
...@@ -815,14 +814,14 @@ nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr) ...@@ -815,14 +814,14 @@ nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy); err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
if (err < 0) if (err < 0)
return err; return ERR_PTR(err);
if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
return -EINVAL; return ERR_PTR(-EINVAL);
newstats = alloc_percpu(struct nft_stats); newstats = alloc_percpu(struct nft_stats);
if (newstats == NULL) if (newstats == NULL)
return -ENOMEM; return ERR_PTR(-ENOMEM);
/* Restore old counters on this cpu, no problem. Per-cpu statistics /* Restore old counters on this cpu, no problem. Per-cpu statistics
* are not exposed to userspace. * are not exposed to userspace.
...@@ -831,6 +830,12 @@ nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr) ...@@ -831,6 +830,12 @@ nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
return newstats;
}
static void nft_chain_stats_replace(struct nft_base_chain *chain,
struct nft_stats __percpu *newstats)
{
if (chain->stats) { if (chain->stats) {
struct nft_stats __percpu *oldstats = struct nft_stats __percpu *oldstats =
nft_dereference(chain->stats); nft_dereference(chain->stats);
...@@ -840,8 +845,6 @@ nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr) ...@@ -840,8 +845,6 @@ nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
free_percpu(oldstats); free_percpu(oldstats);
} else } else
rcu_assign_pointer(chain->stats, newstats); rcu_assign_pointer(chain->stats, newstats);
return 0;
} }
static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
...@@ -860,6 +863,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, ...@@ -860,6 +863,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
u8 policy = NF_ACCEPT; u8 policy = NF_ACCEPT;
u64 handle = 0; u64 handle = 0;
unsigned int i; unsigned int i;
struct nft_stats __percpu *stats;
int err; int err;
bool create; bool create;
...@@ -920,10 +924,11 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, ...@@ -920,10 +924,11 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
if (!(chain->flags & NFT_BASE_CHAIN)) if (!(chain->flags & NFT_BASE_CHAIN))
return -EOPNOTSUPP; return -EOPNOTSUPP;
err = nf_tables_counters(nft_base_chain(chain), stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
nla[NFTA_CHAIN_COUNTERS]); if (IS_ERR(stats))
if (err < 0) return PTR_ERR(stats);
return err;
nft_chain_stats_replace(nft_base_chain(chain), stats);
} }
if (nla[NFTA_CHAIN_POLICY]) if (nla[NFTA_CHAIN_POLICY])
...@@ -977,23 +982,21 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, ...@@ -977,23 +982,21 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
return -ENOMEM; return -ENOMEM;
if (nla[NFTA_CHAIN_COUNTERS]) { if (nla[NFTA_CHAIN_COUNTERS]) {
err = nf_tables_counters(basechain, stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]);
nla[NFTA_CHAIN_COUNTERS]); if (IS_ERR(stats)) {
if (err < 0) {
module_put(type->owner); module_put(type->owner);
kfree(basechain); kfree(basechain);
return err; return PTR_ERR(stats);
} }
basechain->stats = stats;
} else { } else {
struct nft_stats __percpu *newstats; stats = alloc_percpu(struct nft_stats);
if (IS_ERR(stats)) {
newstats = alloc_percpu(struct nft_stats);
if (newstats == NULL) {
module_put(type->owner); module_put(type->owner);
kfree(basechain); kfree(basechain);
return -ENOMEM; return PTR_ERR(stats);
} }
rcu_assign_pointer(basechain->stats, newstats); rcu_assign_pointer(basechain->stats, stats);
} }
basechain->type = type; basechain->type = type;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment