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 modifies the /proc output to display the delta time in seconds since the flow start. You can also obtain the flow-start date by means of the conntrack-tools.
--- include/linux/netfilter/nfnetlink_conntrack.h | 9 ++ include/net/netfilter/nf_conntrack_extend.h | 4 + include/net/netfilter/nf_conntrack_timestamp.h | 53 +++++++++++ include/net/netns/conntrack.h | 2 net/netfilter/Kconfig | 11 ++ net/netfilter/Makefile | 1 net/netfilter/nf_conntrack_core.c | 26 +++++ net/netfilter/nf_conntrack_netlink.c | 46 +++++++++ net/netfilter/nf_conntrack_standalone.c | 41 ++++++++ net/netfilter/nf_conntrack_timestamp.c | 120 ++++++++++++++++++++++++ 10 files changed, 312 insertions(+), 1 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 +4 -0
@@ -17,6 +17,9 @@ enum nf_ct_ext_id {#ifdef CONFIG_NF_CONNTRACK_ZONES NF_CT_EXT_ZONE,#endif +#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP +NF_CT_EXT_TSTAMP , +#endif NF_CT_EXT_NUM, };
@@ -25,6 +28,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 +53 -0
53 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/Kconfig +11 -0
@@ -85,6 +85,17 @@ config NF_CONNTRACK_EVENTS If unsure, say `N'. +config NF_CONNTRACK_TIMESTAMP + bool 'Connection tracking timestamping' + depends on NETFILTER_ADVANCED + help + This option enables support for connection tracking timestamping. + This allows you to store the flow start-time and to obtain + the flow-stop time (once it has been destroyed) via Connection + tracking events. + + If unsure, say `N'. + config NF_CT_PROTO_DCCP tristate 'DCCP protocol connection tracking support (EXPERIMENTAL)' depends on EXPERIMENTAL
net/netfilter/Makefile +1 -0
@@ -1,6 +1,7 @@ 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-$(CONFIG_NF_CONNTRACK_TIMESTAMP) += 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
@@ -43,6 +43,7 @@#include <net/netfilter/nf_conntrack_acct.h> #include <net/netfilter/nf_conntrack_ecache.h> #include <net/netfilter/nf_conntrack_zones.h> +#include <net/netfilter/nf_conntrack_timestamp.h> #include <net/netfilter/nf_nat.h> #include <net/netfilter/nf_nat_core.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
@@ -746,6 +761,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,
@@ -1186,6 +1202,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,
@@ -1504,6 +1525,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;
@@ -1511,6 +1535,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 +45 -1
88 lines not on this page · raw message
net/netfilter/nf_conntrack_standalone.c +41 -0
71 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
Patrick McHardy
AppliedQuoted 18 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. This extension is disabled by default, to enable it, you have to:
echo 1 >
/proc/sys/net/netfilter/nf_conntrack_timestampThis 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.
Quoted 3 lines
This patch modifies the /proc output to display the delta time in seconds since the flow start. You can also obtain the flow-start date by means of the conntrack-tools.
Applied, thanks Pablo.