netfilter: nf_conntrack_tstamp: add flow-based timestamp extension
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/1 netfilter: nf_conntrack_tstamp: add flow-based timestamp extension
This patch adds flow-based timestamping for conntracks. This conntrack extension is disabled by default. Basically, we use two 64-bits variables to store the creation timestamp once the conntrack has been confirmed and the other to store the deletion time. This extension is disabled by default, to enable it, you have to:
echo 1 > /proc/sys/net/netfilter/nf_conntrack_timestamp
This patch allows to save memory for user-space flow-based loogers such as ulogd2. In short, ulogd2 does not need to keep a hashtable with the conntrack in user-space to know when they were created and destroyed, instead we use the kernel timestamp. If we want to have a sane IPFIX implementation in user-space, this nanosecs resolution timestamps are also useful. Other custom user-space applications can benefit from this via libnetfilter_conntrack.
This patch does not modifies the /proc output to display the start timestamping in nanosecs (which is not very useful). We would need some generic functions similar to those in xt_time to convert that output to local time in the kernel. I think that ctnetlink is better for this, we pass the timestamps in nanosecs and we call localtime() in the user-space application. For that reason, I decided to only modify the ctnetlink part (including dumping and event notifications).
--- include/linux/netfilter/nfnetlink_conntrack.h | 9 ++ include/net/netfilter/nf_conntrack_extend.h | 2 include/net/netfilter/nf_conntrack_timestamp.h | 45 +++++++++ include/net/netns/conntrack.h | 2 net/netfilter/Makefile | 2 net/netfilter/nf_conntrack_core.c | 26 +++++ net/netfilter/nf_conntrack_netlink.c | 42 ++++++++ net/netfilter/nf_conntrack_timestamp.c | 120 ++++++++++++++++++++++++ 8 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 include/net/netfilter/nf_conntrack_timestamp.h create mode 100644 net/netfilter/nf_conntrack_timestamp.c
include/linux/netfilter/nfnetlink_conntrack.h +9 -0
@@ -42,6 +42,7 @@ enum ctattr_type {CTA_SECMARK ,/* obsolete */ CTA_ZONE ,CTA_SECCTX , +CTA_TIMESTAMP , __CTA_MAX };#define CTA_MAX (__CTA_MAX - 1)
@@ -127,6 +128,14 @@ enum ctattr_counters { };#define CTA_COUNTERS_MAX (__CTA_COUNTERS_MAX - 1) +enum ctattr_tstamp { +CTA_TIMESTAMP_UNSPEC , +CTA_TIMESTAMP_START , +CTA_TIMESTAMP_STOP , + __CTA_TIMESTAMP_MAX +}; +#define CTA_TIMESTAMP_MAX (__CTA_TIMESTAMP_MAX - 1) +enum ctattr_nat {CTA_NAT_UNSPEC ,CTA_NAT_MINIP ,
include/net/netfilter/nf_conntrack_extend.h +2 -0
@@ -11,6 +11,7 @@ enum nf_ct_ext_id {NF_CT_EXT_ACCT ,NF_CT_EXT_ECACHE ,NF_CT_EXT_ZONE , +NF_CT_EXT_TSTAMP , NF_CT_EXT_NUM, };
@@ -19,6 +20,7 @@ enum nf_ct_ext_id {#define NF_CT_EXT_ACCT_TYPE struct nf_conn_counter#define NF_CT_EXT_ECACHE_TYPE struct nf_conntrack_ecache#define NF_CT_EXT_ZONE_TYPE struct nf_conntrack_zone +#define NF_CT_EXT_TSTAMP_TYPE struct nf_conn_tstamp/* Extensions: optional stuff which isn't permanently in struct. */ struct nf_ct_ext {
include/net/netfilter/nf_conntrack_timestamp.h new file +45 -0
45 lines not on this page · raw message
include/net/netns/conntrack.h +2 -0
@@ -21,11 +21,13 @@ struct netns_ct {int sysctl_events;unsigned int sysctl_events_retry_timeout;int sysctl_acct; +int sysctl_tstamp;int sysctl_checksum;unsigned int sysctl_log_invalid;/* Log invalid packets */ #ifdef CONFIG_SYSCTL struct ctl_table_header *sysctl_header;struct ctl_table_header *acct_sysctl_header; +struct ctl_table_header *tstamp_sysctl_header;struct ctl_table_header *event_sysctl_header;#endif int hash_vmalloc;
net/netfilter/Makefile +1 -1
@@ -1,6 +1,6 @@ netfilter-objs := core.o nf_log.o nf_queue.o nf_sockopt.o -nf_conntrack-y := nf_conntrack_core.o nf_conntrack_standalone.o nf_conntrack_expect.o nf_conntrack_helper.o nf_conntrack_proto.o nf_conntrack_l3proto_generic.o nf_conntrack_proto_generic.o nf_conntrack_proto_tcp.o nf_conntrack_proto_udp.o nf_conntrack_extend.o nf_conntrack_acct.o +nf_conntrack-y := nf_conntrack_core.o nf_conntrack_standalone.o nf_conntrack_expect.o nf_conntrack_helper.o nf_conntrack_proto.o nf_conntrack_l3proto_generic.o nf_conntrack_proto_generic.o nf_conntrack_proto_tcp.o nf_conntrack_proto_udp.o nf_conntrack_extend.o nf_conntrack_acct.o nf_conntrack_timestamp.o nf_conntrack-$(CONFIG_NF_CONNTRACK_EVENTS) += nf_conntrack_ecache.o obj-$(CONFIG_NETFILTER) = netfilter.o
net/netfilter/nf_conntrack_core.c +26 -0
@@ -41,6 +41,7 @@#include <net/netfilter/nf_conntrack_core.h> #include <net/netfilter/nf_conntrack_extend.h> #include <net/netfilter/nf_conntrack_acct.h> +#include <net/netfilter/nf_conntrack_timestamp.h> #include <net/netfilter/nf_conntrack_ecache.h> #include <net/netfilter/nf_conntrack_zones.h> #include <net/netfilter/nf_nat.h>
@@ -282,6 +283,11 @@ EXPORT_SYMBOL_GPL(nf_ct_insert_dying_list);static void death_by_timeout (unsigned long ul_conntrack) {struct nf_conn *ct = (void *)ul_conntrack; +struct nf_conn_tstamp *tstamp; + + tstamp =nf_conn_tstamp_find (ct); +if (tstamp && tstamp->stop ==0 ) + tstamp->stop =ktime_to_ns (ktime_get_real ());if (!test_bit (IPS_DYING_BIT , &ct->status) &&unlikely (nf_conntrack_event (IPCT_DESTROY , ct) <0 )) {
@@ -419,6 +425,7 @@ __nf_conntrack_confirm(struct sk_buff *skb)struct nf_conntrack_tuple_hash *h;struct nf_conn *ct;struct nf_conn_help *help; +struct nf_conn_tstamp *tstamp;struct hlist_nulls_node *n;enum ip_conntrack_info ctinfo;struct net *net;
@@ -488,6 +495,14 @@ __nf_conntrack_confirm(struct sk_buff *skb)atomic_inc (&ct->ct_general.use);set_bit (IPS_CONFIRMED_BIT , &ct->status); +/* set conntrack timestamp, if enabled. */ + tstamp =nf_conn_tstamp_find (ct); +if (tstamp) { +if (skb->tstamp.tv64 ==0 ) +__net_timestamp ((struct sk_buff *)skb); + + tstamp->start =ktime_to_ns (skb->tstamp); + }/* Since the lookup is lockless, hash insertion must be done after * starting the timer and setting the CONFIRMED bit. The RCU barriers * guarantee that no other CPU can find the conntrack before the above
@@ -741,6 +756,7 @@ init_conntrack(struct net *net, struct nf_conn *tmpl, }nf_ct_acct_ext_add (ct,GFP_ATOMIC ); +nf_ct_tstamp_ext_add (ct,GFP_ATOMIC ); ecache = tmpl ?nf_ct_ecache_find (tmpl) :NULL ; nf_ct_ecache_ext_add(ct, ecache ? ecache->ctmask : 0,
@@ -1181,6 +1197,11 @@ struct __nf_ct_flush_report {static int kill_report (struct nf_conn *i,void *data) {struct __nf_ct_flush_report *fr = (struct __nf_ct_flush_report *)data; +struct nf_conn_tstamp *tstamp; + + tstamp =nf_conn_tstamp_find (i); +if (tstamp && tstamp->stop ==0 ) + tstamp->stop =ktime_to_ns (ktime_get_real ());/* If we fail to deliver the event, death_by_timeout() will retry */ if (nf_conntrack_event_report(IPCT_DESTROY, i,
@@ -1499,6 +1520,9 @@ static int nf_conntrack_init_net(struct net *net) ret =nf_conntrack_acct_init (net);if (ret <0 ) goto err_acct; + ret =nf_conntrack_tstamp_init (net); +if (ret <0 ) + goto err_tstamp; ret =nf_conntrack_ecache_init (net);if (ret <0 ) goto err_ecache;
@@ -1506,6 +1530,8 @@ static int nf_conntrack_init_net(struct net *net)return 0 ; err_ecache: +nf_conntrack_tstamp_fini (net); +err_tstamp:nf_conntrack_acct_fini (net); err_acct:nf_conntrack_expect_fini (net);
net/netfilter/nf_conntrack_netlink.c +41 -1
84 lines not on this page · raw message
net/netfilter/nf_conntrack_timestamp.c new file +120 -0
120 lines not on this page · raw message
Pablo Neira Ayuso
maintainerQuoted 5 lines · Pablo Neira Ayuso
This patch adds flow-based timestamping for conntracks. This conntrack extension is disabled by default. Basically, we use two 64-bits variables to store the creation timestamp once the conntrack has been confirmed and the other to store the deletion time.
Sorry, this patch does not apply, I'll fix it and will resend.
Am 13.01.2011 16:40, schrieb Pablo Neira Ayuso:
Quoted 9 lines · Pablo Neira Ayuso
Quoted 5 lines
This patch adds flow-based timestamping for conntracks. This conntrack extension is disabled by default. Basically, we use two 64-bits variables to store the creation timestamp once the conntrack has been confirmed and the other to store the deletion time.
Sorry, this patch does not apply, I'll fix it and will resend.
Perhaps its best to wait until I've pulled in your tree since this will likely cause more conflicts. I should get to this tonight or tommorrow morning.
Am 13.01.2011 13:30, schrieb Pablo Neira Ayuso:
Quoted 9 lines
This patch adds flow-based timestamping for conntracks. This conntrack extension is disabled by default. Basically, we use two 64-bits variables to store the creation timestamp once the conntrack has been confirmed and the other to store the deletion time. This extension is disabled by default, to enable it, you have to:
echo 1 >
/proc/sys/net/netfilter/nf_conntrack_timestamp
Quoted 8 lines
This patch allows to save memory for user-space flow-based loogers such as ulogd2. In short, ulogd2 does not need to keep a hashtable with the conntrack in user-space to know when they were created and destroyed, instead we use the kernel timestamp. If we want to have a sane IPFIX implementation in user-space, this nanosecs resolution timestamps are also useful. Other custom user-space applications can benefit from this via
libnetfilter_conntrack.
No general objections from me.
Quoted 9 lines
This patch does not modifies the /proc output to display the start timestamping in nanosecs (which is not very useful). We would need some generic functions similar to those in
xt_timeto convert that output to local time in the kernel. I think that ctnetlink is better for this, we pass the timestamps in nanosecs and we calllocaltime()in the user-space application. For that reason, I decided to only modify the ctnetlink part (including dumping and event notifications).
Just as an idea, showing the time-delta (aka lifetime) of the connection could be interesting and doesn't require any timezone conversions. But this could certainly be done in a follow up patch.
Quoted 22 lines
include/net/netfilter/nf_conntrack_timestamp.h new file +19
-0@@ -0,0 +1,45 @@ +#ifndef _NF_CONNTRACK_TSTAMP_H +#define _NF_CONNTRACK_TSTAMP_H + +#include <net/net_namespace.h> +#include <linux/netfilter/nf_conntrack_common.h> +#include <linux/netfilter/nf_conntrack_tuple_common.h> +#include <net/netfilter/nf_conntrack.h> +#include <net/netfilter/nf_conntrack_extend.h> + +struct nf_conn_tstamp { +u_int64_t start; +u_int64_t stop; +}; + +static inline +struct nf_conn_tstamp *nf_conn_tstamp_find (const struct nf_conn *ct) +{ +return nf_ct_ext_find (ct,NF_CT_EXT_TSTAMP ); +}
include/net/netfilter/nf_conntrack_timestamp.h line 29
Quoted 10 lines
include/net/netfilter/nf_conntrack_timestamp.h new file +10
-0+ +static inline +struct nf_conn_tstamp *nf_ct_tstamp_ext_add (struct nf_conn *ct,gfp_t gfp) +{ +struct net *net =nf_ct_net (ct); + +if (!net->ct.sysctl_tstamp) +return NULL ; + +return nf_ct_ext_add (ct,NF_CT_EXT_TSTAMP , gfp);
How about making this configurable at compile time to avoid any overhead (memory in ct_extend and runtime) for anyone not needing it like most of the other ct_extend options?
Pablo Neira Ayuso
maintainerQuoted 31 lines · Patrick McHardy
Am 13.01.2011 13:30, schrieb Pablo Neira Ayuso:
Quoted 9 lines
This patch adds flow-based timestamping for conntracks. This conntrack extension is disabled by default. Basically, we use two 64-bits variables to store the creation timestamp once the conntrack has been confirmed and the other to store the deletion time. This extension is disabled by default, to enable it, you have to:
echo 1 >
/proc/sys/net/netfilter/nf_conntrack_timestampQuoted 8 lines
This patch allows to save memory for user-space flow-based loogers such as ulogd2. In short, ulogd2 does not need to keep a hashtable with the conntrack in user-space to know when they were created and destroyed, instead we use the kernel timestamp. If we want to have a sane IPFIX implementation in user-space, this nanosecs resolution timestamps are also useful. Other custom user-space applications can benefit from this via
libnetfilter_conntrack.No general objections from me.
Quoted 9 lines
This patch does not modifies the /proc output to display the start timestamping in nanosecs (which is not very useful). We would need some generic functions similar to those in
xt_timeto convert that output to local time in the kernel. I think that ctnetlink is better for this, we pass the timestamps in nanosecs and we calllocaltime()in the user-space application. For that reason, I decided to only modify the ctnetlink part (including dumping and event notifications).
Quoted 4 lines
Just as an idea, showing the time-delta (aka lifetime) of the connection could be interesting and doesn't require any timezone conversions. But this could certainly be done in a follow up patch.
That's interesting indeed. We can obtain the current time in ct_seq_start and store it in ct_iter_state, then calculate the time-delta for each flow entry to display this in the /proc output. The conntrack tool can do similar but in user-space.
Quoted 28 lines
Quoted 28 lines
include/net/netfilter/nf_conntrack_timestamp.h new file +25
-0@@ -0,0 +1,45 @@ +#ifndef _NF_CONNTRACK_TSTAMP_H +#define _NF_CONNTRACK_TSTAMP_H + +#include <net/net_namespace.h> +#include <linux/netfilter/nf_conntrack_common.h> +#include <linux/netfilter/nf_conntrack_tuple_common.h> +#include <net/netfilter/nf_conntrack.h> +#include <net/netfilter/nf_conntrack_extend.h> + +struct nf_conn_tstamp { +u_int64_t start; +u_int64_t stop; +}; + +static inline +struct nf_conn_tstamp *nf_conn_tstamp_find (const struct nf_conn *ct) +{ +return nf_ct_ext_find (ct,NF_CT_EXT_TSTAMP ); +} + +static inline +struct nf_conn_tstamp *nf_ct_tstamp_ext_add (struct nf_conn *ct,gfp_t gfp) +{ +struct net *net =nf_ct_net (ct); +
Quoted 8 lines
include/net/netfilter/nf_conntrack_timestamp.h line 29
Quoted 4 lines
include/net/netfilter/nf_conntrack_timestamp.h new file +4
-0+if (!net->ct.sysctl_tstamp) +return NULL ; + +return nf_ct_ext_add (ct,NF_CT_EXT_TSTAMP , gfp);How about making this configurable at compile time to avoid any overhead (memory in
ct_extendand runtime) for anyone not needing it like most of the otherct_extendoptions?
I'm fine with this, I'll add it.
Looking at the source, should we do the same with the accounting? I remember that we decided to remove this compile-time option time ago.
Quoted 14 lines · Pablo Neira Ayuso
Quoted 5 lines
Quoted 5 lines
include/net/netfilter/nf_conntrack_timestamp.h new file +5
-0+static inline +struct nf_conn_tstamp *nf_ct_tstamp_ext_add (struct nf_conn *ct,gfp_t gfp) +{ +struct net *net =nf_ct_net (ct); +Quoted 8 lines
include/net/netfilter/nf_conntrack_timestamp.h line 29
Quoted 4 lines
include/net/netfilter/nf_conntrack_timestamp.h new file +4
-0+if (!net->ct.sysctl_tstamp) +return NULL ; + +return nf_ct_ext_add (ct,NF_CT_EXT_TSTAMP , gfp);How about making this configurable at compile time to avoid any overhead (memory in
ct_extendand runtime) for anyone not needing it like most of the otherct_extendoptions?
Quoted 4 lines
I'm fine with this, I'll add it.
Looking at the source, should we do the same with the accounting? I remember that we decided to remove this compile-time option time ago.
I see no reason why not.