nfacct infrastructure (version 2)
Subsystems (2)
[email protected]
maintainercover nfacct infrastructure (version 2)
From: Pablo Neira Ayuso <[email protected]>
Hi!
These patchset contains the second version of the nfacct infrastructure after digesting comments from Patrick and Eric.
Now, we've got one new xt_nfacct match instead of the target as suggested by Jan and Jozsef.
I'd like to apply this to nf-next, if nobody complains in the following days, I'll do so.
Thanks!
Pablo Neira Ayuso (2):
netfilter: add extended accounting infrastructure over nfnetlink
netfilter: xtables: add nfacct match to support extended accounting
include/linux/netfilter/Kbuild | 2 + include/linux/netfilter/nfnetlink.h | 3 +- include/linux/netfilter/nfnetlink_acct.h | 35 +++ include/linux/netfilter/xt_nfacct.h | 17 ++ net/netfilter/Kconfig | 19 ++ net/netfilter/Makefile | 2 + net/netfilter/nfnetlink_acct.c | 345 ++++++++++++++++++++++++++++++ net/netfilter/xt_nfacct.c | 76 +++++++ 8 files changed, 498 insertions(+), 1 deletions(-) create mode 100644 include/linux/netfilter/nfnetlink_acct.h create mode 100644 include/linux/netfilter/xt_nfacct.h create mode 100644 net/netfilter/nfnetlink_acct.c create mode 100644 net/netfilter/xt_nfacct.c
Signature · 1 line
1.7.2.5
[email protected]
maintainerPATCH 1/2 netfilter: add extended accounting infrastructure over nfnetlink
From: Pablo Neira Ayuso <[email protected]>
We currently have two ways to account traffic in netfilter:
- iptables chain and rule counters:
# iptables -L -n -v
Chain INPUT (policy DROP 3 packets, 867 bytes)
pkts bytes target prot opt in out source destination
8 1104 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0- use flow-based accounting provided by ctnetlink:
# conntrack -L tcp 6 431999 ESTABLISHED src=192.168.1.130 dst=212.106.219.168 sport=58152 dport=80 packets=47 bytes=7654 src=212.106.219.168 dst=192.168.1.130 sport=80 dport=58152 packets=49 bytes=66340 [ASSURED] mark=0 use=1
While trying to display real-time accounting statistics, we require to pool the kernel periodically to obtain this information. This is OK if the number of flows is relatively low. However, in case that the number of flows is huge, we can spend a considerable amount of cycles to iterate over the list of flows that have been obtained.
Moreover, if we want to obtain the sum of the flow accounting results that match some criteria, we have to iterate over the whole list of existing flows, look for matchings and update the counters.
This patch adds the extended accounting infrastructure for nfnetlink which aims to allow displaying real-time traffic accounting without the need of complicated and resource-consuming implementation in user-space. Basically, this new infrastructure allows you to create accounting objects. One accounting object is composed of packet and byte counters.
In order to manipulate create accounting objects, you require the new libnetfilter_acct library. It contains several examples of use:
libnetfilter_acct/examples# ./nfacct-add http-traffic libnetfilter_acct/examples# ./nfacct-get
http-traffic = { pkts = 000000000000, bytes = 000000000000 };
Then, you can use one of this accounting objects in several iptables rules using the new nfacct match (which comes in a follow-up patch):
# iptables -I INPUT -p tcp --sport 80 -m nfacct --nfacct-name http-traffic # iptables -I OUTPUT -p tcp --dport 80 -m nfacct --nfacct-name http-traffic
The idea is simple: if one packet matches the rule, the nfacct match updates the counters.
Thanks to Patrick McHardy and Eric Dumazet for reviewing and providing feedback for this contribution.
--- include/linux/netfilter/Kbuild | 1 + include/linux/netfilter/nfnetlink.h | 3 +- include/linux/netfilter/nfnetlink_acct.h | 36 +++ net/netfilter/Kconfig | 8 + net/netfilter/Makefile | 1 + net/netfilter/nfnetlink_acct.c | 360 ++++++++++++++++++++++++++++++ 6 files changed, 408 insertions(+), 1 deletions(-) create mode 100644 include/linux/netfilter/nfnetlink_acct.h create mode 100644 net/netfilter/nfnetlink_acct.c
include/linux/netfilter/Kbuild +1 -0
@@ -7,6 +7,7 @@ header-y += nf_conntrack_tcp.h header-y += nf_conntrack_tuple_common.h header-y += nf_nat.h header-y += nfnetlink.h +header-y += nfnetlink_acct.h header-y += nfnetlink_compat.h header-y += nfnetlink_conntrack.h header-y += nfnetlink_log.h
include/linux/netfilter/nfnetlink.h +2 -1
@@ -48,7 +48,8 @@ struct nfgenmsg {#define NFNL_SUBSYS_ULOG 4#define NFNL_SUBSYS_OSF 5#define NFNL_SUBSYS_IPSET 6 -#define NFNL_SUBSYS_COUNT 7 +#define NFNL_SUBSYS_ACCT 7 +#define NFNL_SUBSYS_COUNT 8#ifdef __KERNEL__
include/linux/netfilter/nfnetlink_acct.h new file +36 -0
@@ -0,0 +1,36 @@ +#ifndef _NFNL_ACCT_H_ +#define _NFNL_ACCT_H_ + +#ifndef NFACCT_NAME_MAX +#define NFACCT_NAME_MAX 32 +#endif + +enum nfnl_acct_msg_types { +NFNL_MSG_ACCT_NEW , +NFNL_MSG_ACCT_GET , +NFNL_MSG_ACCT_GET_CTRZERO , +NFNL_MSG_ACCT_DEL , +NFNL_MSG_ACCT_MAX +}; + +enum nfnl_acct_type { +NFACCT_UNSPEC , +NFACCT_NAME , +NFACCT_PKTS , +NFACCT_BYTES , +NFACCT_USE , + __NFACCT_MAX +}; +#define NFACCT_MAX (__NFACCT_MAX - 1) + +#ifdef __KERNEL__ + +struct nf_acct ; + +extern struct nf_acct *nfnl_acct_find_get (const char *filter_name); +extern void nfnl_acct_put (struct nf_acct *acct); +extern void nfnl_acct_update (const struct sk_buff *skb,struct nf_acct *nfacct); + +#endif /* __KERNEL__ */ + +#endif /* _NFNL_ACCT_H */
net/netfilter/Kconfig +8 -0
@@ -4,6 +4,14 @@ menu "Core Netfilter Configuration" config NETFILTER_NETLINK tristate +config NETFILTER_NETLINK_ACCT +tristate "Netfilter NFACCT over NFNETLINK interface" + depends on NETFILTER_ADVANCED + select NETFILTER_NETLINK + help + If this option is enabled, the kernel will include support + for extended accounting via NFNETLINK. + config NETFILTER_NETLINK_QUEUE tristate "Netfilter NFQUEUE over NFNETLINK interface" depends on NETFILTER_ADVANCED
net/netfilter/Makefile +1 -0
@@ -7,6 +7,7 @@ nf_conntrack-$(CONFIG_NF_CONNTRACK_EVENTS) += nf_conntrack_ecache.o obj-$(CONFIG_NETFILTER) = netfilter.o obj-$(CONFIG_NETFILTER_NETLINK) += nfnetlink.o +obj-$(CONFIG_NETFILTER_NETLINK_ACCT) += nfnetlink_acct.o obj-$(CONFIG_NETFILTER_NETLINK_QUEUE) += nfnetlink_queue.o obj-$(CONFIG_NETFILTER_NETLINK_LOG) += nfnetlink_log.o
net/netfilter/nfnetlink_acct.c new file +360 -0
360 lines not on this page · raw message
Signature · 1 line
1.7.2.5
Re: [PATCH 1/2] netfilter: add extended accounting infrastructure over nfnetlink
Le vendredi 23 décembre 2011 à 14:42 +0100, [email protected] a écrit :
Quoted 7 lines
From: Pablo Neira Ayuso <[email protected]>
net/netfilter/nfnetlink_acct.c new file +3
-0+struct nf_acct *nfnl_acct_find_get (const char *acct_name) +{ +struct nf_acct *cur, *acct =NULL ;
net/netfilter/nfnetlink_acct.c line 304
Quoted 10 lines
net/netfilter/nfnetlink_acct.c new file +10
-0+ +rcu_read_lock (); +list_for_each_entry (cur, &nfnl_acct_list, head) { +if (strncmp (cur->name, acct_name,NFACCT_NAME_MAX )!=0 ) +continue ; + +if (!try_module_get (THIS_MODULE )) +return NULL ; + +if (!atomic_inc_not_zero (&cur->refcnt))
You leak the module reference here.
net/netfilter/nfnetlink_acct.c line 313
Quoted 9 lines
net/netfilter/nfnetlink_acct.c new file +9
-0+return NULL ; + + acct = cur; +break ; + } +rcu_read_unlock (); +return acct; +} +EXPORT_SYMBOL_GPL (nfnl_acct_find_get);
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] netfilter: add extended accounting infrastructure over nfnetlink
Quoted 23 lines · Eric Dumazet
Le vendredi 23 décembre 2011 à 14:42 +0100, [email protected] a écrit :
Quoted 7 lines
From: Pablo Neira Ayuso <[email protected]>
net/netfilter/nfnetlink_acct.c new file +3
-0+struct nf_acct *nfnl_acct_find_get (const char *acct_name) +{ +struct nf_acct *cur, *acct =NULL ;net/netfilter/nfnetlink_acct.c line 304
Quoted 10 lines
net/netfilter/nfnetlink_acct.c new file +10
-0+ +rcu_read_lock (); +list_for_each_entry (cur, &nfnl_acct_list, head) { +if (strncmp (cur->name, acct_name,NFACCT_NAME_MAX )!=0 ) +continue ; + +if (!try_module_get (THIS_MODULE )) +return NULL ; + +if (!atomic_inc_not_zero (&cur->refcnt))You leak the module reference here.
Quoted 10 lines
net/netfilter/nfnetlink_acct.c line 313
Quoted 9 lines
net/netfilter/nfnetlink_acct.c new file +9
-0+return NULL ; + + acct = cur; +break ; + } +rcu_read_unlock (); +return acct; +} +EXPORT_SYMBOL_GPL (nfnl_acct_find_get);
Also make sure you add missing "rcu_read_unlock();" before return NULL ;)
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [PATCH 1/2] netfilter: add extended accounting infrastructure over nfnetlink
Quoted 36 lines · Eric Dumazet
Quoted 23 lines · Eric Dumazet
Le vendredi 23 décembre 2011 à 14:42 +0100, [email protected] a écrit :
Quoted 7 lines
From: Pablo Neira Ayuso <[email protected]>
net/netfilter/nfnetlink_acct.c new file +3
-0+struct nf_acct *nfnl_acct_find_get (const char *acct_name) +{ +struct nf_acct *cur, *acct =NULL ;net/netfilter/nfnetlink_acct.c line 304
Quoted 10 lines
net/netfilter/nfnetlink_acct.c new file +10
-0+ +rcu_read_lock (); +list_for_each_entry (cur, &nfnl_acct_list, head) { +if (strncmp (cur->name, acct_name,NFACCT_NAME_MAX )!=0 ) +continue ; + +if (!try_module_get (THIS_MODULE )) +return NULL ; + +if (!atomic_inc_not_zero (&cur->refcnt))You leak the module reference here.
Quoted 10 lines
net/netfilter/nfnetlink_acct.c line 313
Quoted 9 lines
net/netfilter/nfnetlink_acct.c new file +9
-0+return NULL ; + + acct = cur; +break ; + } +rcu_read_unlock (); +return acct; +} +EXPORT_SYMBOL_GPL (nfnl_acct_find_get);
Quoted 2 lines
Also make sure you add missing "
rcu_read_unlock();" before return NULL ;)
Sure, fixed as well. Thanks!
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [PATCH 1/2] netfilter: add extended accounting infrastructure over nfnetlink
In reply to Eric DumazetQuoted 6 lines · Eric Dumazet
Le vendredi 23 décembre 2011 à 14:42 +0100, [email protected] a écrit :
Quoted 2 lines
From: Pablo Neira Ayuso <[email protected]>
Quoted 15 lines
net/netfilter/nfnetlink_acct.c line 304
Quoted 13 lines
net/netfilter/nfnetlink_acct.c new file +13
-0+struct nf_acct *nfnl_acct_find_get (const char *acct_name) +{ +struct nf_acct *cur, *acct =NULL ; + +rcu_read_lock (); +list_for_each_entry (cur, &nfnl_acct_list, head) { +if (strncmp (cur->name, acct_name,NFACCT_NAME_MAX )!=0 ) +continue ; + +if (!try_module_get (THIS_MODULE )) +return NULL ; + +if (!atomic_inc_not_zero (&cur->refcnt))You leak the module reference here.
Indeed, thanks. I've fixed this.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] netfilter: add extended accounting infrastructure over nfnetlink
In reply to [email protected]Review
Quoted 2 lines · On Fri, Dec 23, 2011 at 9:42 PM, <[email protected]> wrote:
(quoted diff) +2
-0+ +static LIST_HEAD(nfnl_acct_list);
You suppose that there won't be many accounting instants. It isn't scalable, as we have to iterate the list to find a special item. How about a hash table?
Quoted 5 lines
net/netfilter/nfnetlink_acct.c new file +5
-0+ +static int +nfnl_acct_dump (struct sk_buff *skb,struct netlink_callback *cb) +{ + struct nf_acct *cur, *last;
net/netfilter/nfnetlink_acct.c line 149
Quoted 10 lines
net/netfilter/nfnetlink_acct.c new file +10
-0+ + if (cb->args[2]) + return 0; + + last = (struct nf_acct *)cb->args[1]; + if (cb->args[1]) + cb->args[1] = 0; + + rcu_read_lock(); + list_for_each_entry(cur, &nfnl_acct_list, head) {
You should use the RCU variant here.
Quoted 4 lines
+ if (last && cur != last)
+ continue;net/netfilter/nfnetlink_acct.c new file +2
-0+ + if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).pid,
net/netfilter/nfnetlink_acct.c line 163
Quoted 10 lines
net/netfilter/nfnetlink_acct.c new file +10
-0+ cb->nlh->nlmsg_seq, + NFNL_MSG_ACCT_NEW, cur) < 0) { + cb->args[1] = (unsigned long)cur; + break; + } + + if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == + NFNL_MSG_ACCT_GET_CTRZERO) { + atomic64_set(&cur->pkts, 0); + atomic64_set(&cur->bytes, 0);
The get and zero operations should be done in an atomic context, otherwise counters added between them will be lost.
Signature · 6 lines
Regards, Changli Gao([email protected]) -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [PATCH 1/2] netfilter: add extended accounting infrastructure over nfnetlink
Quoted 7 lines · Changli Gao
Review
Quoted 2 lines · On Fri, Dec 23, 2011 at 9:42 PM, <[email protected]> wrote:
(quoted diff) +2
-0+ +static LIST_HEAD(nfnl_acct_list);You suppose that there won't be many accounting instants. It isn't scalable, as we have to iterate the list to find a special item. How about a hash table?
The lookup only happens if the rule is loaded.
There's no real gain from using hashing here.
Quoted 8 lines
Quoted 8 lines
net/netfilter/nfnetlink_acct.c new file +8
-0+static int +nfnl_acct_dump (struct sk_buff *skb,struct netlink_callback *cb) +{ + struct nf_acct *cur, *last; + + if (cb->args[2]) + return 0; +
Quoted 8 lines
Quoted 6 lines
+ last = (struct
nf_acct*)cb->args[1];
+ if (cb->args[1])
+ cb->args[1] = 0;
+
+rcu_read_lock();
+list_for_each_entry(cur, &nfnl_acct_list, head){You should use the RCU variant here.
I'll fix this, thanks.
Quoted 9 lines
Quoted 9 lines
+ if (last && cur != last)
+ continue;
+
+ if (nfnl_acct_fill_info(skb, NETLINK_CB(cb->skb).pid,
+ cb->nlh->nlmsg_seq,
+ NFNL_MSG_ACCT_NEW, cur) < 0) {
+ cb->args[1] = (unsigned long)cur;
+ break;
+ }
Quoted 8 lines
Quoted 5 lines
+
+ if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type)==
+NFNL_MSG_ACCT_GET_CTRZERO) {
+atomic64_set(&cur->pkts, 0);
+atomic64_set(&cur->bytes, 0);The get and zero operations should be done in an atomic context, otherwise counters added between them will be lost.
Good catch.
I think we can fix this with xchg.
Similar patch should go to nf_conntrack_netlink.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
[email protected]
maintainerPATCH 2/2 netfilter: xtables: add nfacct match to support extended accounting
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
This patch adds the match that allows to perform extended accounting. It requires the new nfnetlink_acct infrastructure.
# iptables -I INPUT -p tcp --sport 80 -m nfacct --nfacct-name http-traffic # iptables -I OUTPUT -p tcp --dport 80 -m nfacct --nfacct-name http-traffic
--- include/linux/netfilter/Kbuild | 1 + include/linux/netfilter/xt_nfacct.h | 13 ++++++ net/netfilter/Kconfig | 10 +++++ net/netfilter/Makefile | 1 + net/netfilter/xt_nfacct.c | 76 +++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 0 deletions(-) create mode 100644 include/linux/netfilter/xt_nfacct.h create mode 100644 net/netfilter/xt_nfacct.c
include/linux/netfilter/Kbuild +1 -0
@@ -23,6 +23,7 @@ header-y += xt_DSCP.h header-y += xt_IDLETIMER.h header-y += xt_LED.h header-y += xt_MARK.h +header-y += xt_nfacct.h header-y += xt_NFLOG.h header-y += xt_NFQUEUE.h header-y += xt_RATEEST.h
include/linux/netfilter/xt_nfacct.h new file +13 -0
@@ -0,0 +1,13 @@ +#ifndef _XT_NFACCT_MATCH_H +#define _XT_NFACCT_MATCH_H + +#include <linux/netfilter/nfnetlink_acct.h> + +struct nf_acct ; + +struct xt_nfacct_match_info { +char name[NFACCT_NAME_MAX ]; +struct nf_acct *nfacct; +}; + +#endif /* _XT_NFACCT_MATCH_H */
net/netfilter/Kconfig +10 -0
@@ -887,6 +887,16 @@ config NETFILTER_XT_MATCH_MULTIPORT To compile it as a module, choose M here. If unsure, say N. +config NETFILTER_XT_MATCH_NFACCT + tristate '"nfacct" match support' + default m if NETFILTER_ADVANCED=n + select NETFILTER_NETLINK_ACCT + help + This option allows you to use the extended accounting through + nfnetlink_acct. + + To compile it as a module, choose M here. If unsure, say N. + config NETFILTER_XT_MATCH_OSF tristate '"osf" Passive OS fingerprint match' depends on NETFILTER_ADVANCED && NETFILTER_NETLINK
net/netfilter/Makefile +1 -0
@@ -91,6 +91,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_LENGTH) += xt_length.o obj-$(CONFIG_NETFILTER_XT_MATCH_LIMIT) += xt_limit.o obj-$(CONFIG_NETFILTER_XT_MATCH_MAC) += xt_mac.o obj-$(CONFIG_NETFILTER_XT_MATCH_MULTIPORT) += xt_multiport.o +obj-$(CONFIG_NETFILTER_XT_MATCH_NFACCT) += xt_nfacct.o obj-$(CONFIG_NETFILTER_XT_MATCH_OSF) += xt_osf.o obj-$(CONFIG_NETFILTER_XT_MATCH_OWNER) += xt_owner.o obj-$(CONFIG_NETFILTER_XT_MATCH_PHYSDEV) += xt_physdev.o
net/netfilter/xt_nfacct.c new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
1.7.2.5