netfilter 00/07: netfilter update
Subsystems (3)
Hi Dave,
following is a final netfilter update for 2.6.31, containing:
- a fix for a warning when reading one of the the nf_log files
- a patch to use the new mod_timer_pending() function in conntrack to avoid taking the global conntrack lock for timer updates
- a set of patches from Pablo to make conntrack event notifications runtime selectable and optionally reliable. This works by making sure at least the final destroy event is successfully transmitted, otherwise the event is re-broadcast until a configurable timeout is reached.
- a patch from Joe Perches to convert a bunch of printks without a log level to pr_err()
Please apply or pull from:
git://git.kernel.org/pub/scm/linux/kernel/git/kaber/nf-next-2.6.git master
Thanks!
include/linux/list_nulls.h | 18 +++ include/net/netfilter/nf_conntrack.h | 2 + include/net/netfilter/nf_conntrack_ecache.h | 175 ++++++++++++++--------- include/net/netfilter/nf_conntrack_extend.h | 2 + include/net/netfilter/nf_conntrack_helper.h | 2 + include/net/netns/conntrack.h | 7 +- net/netfilter/nf_conntrack_core.c | 132 ++++++++++++----- net/netfilter/nf_conntrack_ecache.c | 209 ++++++++++++++++++--------- net/netfilter/nf_conntrack_helper.c | 14 ++ net/netfilter/nf_conntrack_netlink.c | 68 +++++---- net/netfilter/nf_log.c | 6 +- net/netfilter/x_tables.c | 16 +- 12 files changed, 431 insertions(+), 220 deletions(-)
Joe Perches (1):
x_tables: Convert printk to pr_err
Pablo Neira Ayuso (4):
netfilter: conntrack: move event caching to conntrack extension infrastructure
netfilter: conntrack: move helper destruction to nf_ct_helper_destroy()
list_nulls: add hlist_nulls_add_head and hlist_nulls_del
netfilter: conntrack: optional reliable conntrack event delivery
Patrick McHardy (2):
netfilter: nf_log: fix sleeping function called from invalid context
netfilter: nf_conntrack: use mod_timer_pending() for conntrack refresh
netfilter 01/07: nf_log: fix sleeping function called from invalid context
commit 266d07cb1c9a0c345d7d3aea889f92062894059e
Author: Patrick McHardy <[email protected]> Date: Sat Jun 13 12:21:10 2009 +0200
netfilter: nf_log: fix sleeping function called from invalid context
Fix regression introduced by 17625274 "netfilter: sysctl support of
logger choice":
BUG: sleeping function called from invalid context at /mnt/s390test/linux-2.6-tip/arch/s390/include/asm/uaccess.h:234
in_atomic(): 1, irqs_disabled(): 0, pid: 3245, name: sysctl
CPU: 1 Not tainted 2.6.30-rc8-tipjun10-02053-g39ae214 #1
Process sysctl (pid: 3245, task: 000000007f675da0, ksp: 000000007eb17cf0)
0000000000000000 000000007eb17be8 0000000000000002 0000000000000000
000000007eb17c88 000000007eb17c00 000000007eb17c00 0000000000048156
00000000003e2de8 000000007f676118 000000007eb17f10 0000000000000000
0000000000000000 000000007eb17be8 000000000000000d 000000007eb17c58
00000000003e2050 000000000001635c 000000007eb17be8 000000007eb17c30
Call Trace:
(Ý<00000000000162e6>¨ show_trace+0x13a/0x148)
Ý<00000000000349ea>¨ __might_sleep+0x13a/0x164
Ý<0000000000050300>¨ proc_dostring+0x134/0x22c
Ý<0000000000312b70>¨ nf_log_proc_dostring+0xfc/0x188
Ý<0000000000136f5e>¨ proc_sys_call_handler+0xf6/0x118
Ý<0000000000136fda>¨ proc_sys_read+0x26/0x34
Ý<00000000000d6e9c>¨ vfs_read+0xac/0x158
Ý<00000000000d703e>¨ SyS_read+0x56/0x88
Ý<0000000000027f42>¨ sysc_noemu+0x10/0x16
Use the nf_log_mutex instead of RCU to fix this.
Reported-and-tested-by: Maran Pakkirisamy <[email protected]>
Signed-off-by: Patrick McHardy <[email protected]>net/netfilter/nf_log.c +3 -3
@@ -248,14 +248,14 @@ static int nf_log_proc_dostring(ctl_table *table, int write, struct file *filp,rcu_assign_pointer (nf_loggers[tindex], logger);mutex_unlock (&nf_log_mutex); }else { -rcu_read_lock (); - logger =rcu_dereference (nf_loggers[tindex]); +mutex_lock (&nf_log_mutex); + logger = nf_loggers[tindex];if (!logger) table->data ="NONE" ;else table->data = logger->name; r =proc_dostring (table, write, filp, buffer, lenp, ppos); -rcu_read_unlock (); +mutex_unlock (&nf_log_mutex); }return r;
netfilter 02/07: nf_conntrack: use mod_timer_pending() for conntrack refresh
In reply to Patrick McHardycommit 65cb9fda32be613216f601a330b311c3bd7a8436
Author: Patrick McHardy <[email protected]> Date: Sat Jun 13 12:21:49 2009 +0200
netfilter: nf_conntrack: use mod_timer_pending() for conntrack refresh
Use mod_timer_pending() instead of atomic sequence of del_timer()/ add_timer(). mod_timer_pending() does not rearm an inactive timer, so we don't need the conntrack lock anymore to make sure we don't accidentally rearm a timer of a conntrack which is in the process of being destroyed.
With this change, we don't need to take the global lock anymore at all, counter updates can be performed under the per-conntrack lock.
net/netfilter/nf_conntrack_core.c +6 -11
@@ -807,8 +807,6 @@ void __nf_ct_refresh_acct(struct nf_conn *ct,NF_CT_ASSERT (ct->timeout.data == (unsigned long )ct);NF_CT_ASSERT (skb); -spin_lock_bh (&nf_conntrack_lock); -/* Only update if this is not a fixed timeout */ if (test_bit (IPS_FIXED_TIMEOUT_BIT , &ct->status)) goto acct;
@@ -822,11 +820,8 @@ void __nf_ct_refresh_acct(struct nf_conn *ct,/* Only update the timeout if the new timeout is at least HZ jiffies from the old timeout. Need del_timer for race avoidance (may already be dying). */ -if (newtime - ct->timeout.expires >=HZ - &&del_timer (&ct->timeout)) { - ct->timeout.expires = newtime; -add_timer (&ct->timeout); - } +if (newtime - ct->timeout.expires >=HZ ) +mod_timer_pending (&ct->timeout, newtime); } acct:
@@ -835,13 +830,13 @@ acct: acct =nf_conn_acct_find (ct);if (acct) { +spin_lock_bh (&ct->lock); acct[CTINFO2DIR (ctinfo)].packets++; acct[CTINFO2DIR (ctinfo)].bytes += skb->len -skb_network_offset (skb); +spin_unlock_bh (&ct->lock); } } - -spin_unlock_bh (&nf_conntrack_lock); }EXPORT_SYMBOL_GPL (__nf_ct_refresh_acct);
@@ -853,14 +848,14 @@ bool __nf_ct_kill_acct(struct nf_conn *ct,if (do_acct) {struct nf_conn_counter *acct; -spin_lock_bh (&nf_conntrack_lock); acct =nf_conn_acct_find (ct);if (acct) { +spin_lock_bh (&ct->lock); acct[CTINFO2DIR (ctinfo)].packets++; acct[CTINFO2DIR (ctinfo)].bytes += skb->len -skb_network_offset (skb); +spin_unlock_bh (&ct->lock); } -spin_unlock_bh (&nf_conntrack_lock); }if (del_timer (&ct->timeout)) {
netfilter 03/07: conntrack: move event caching to conntrack extension infrastructure
In reply to Patrick McHardycommit a0891aa6a635f658f29bb061a00d6d3486941519
Author: Pablo Neira Ayuso <[email protected]> Date: Sat Jun 13 12:26:29 2009 +0200
netfilter: conntrack: move event caching to conntrack extension infrastructure
This patch reworks the per-cpu event caching to use the conntrack extension infrastructure.
The main drawback is that we consume more memory per conntrack if event delivery is enabled. This patch is required by the reliable event delivery that follows to this patch.
BTW, this patch allows you to enable/disable event delivery via /proc/sys/net/netfilter/nf_conntrack_events in runtime, although you can still disable event caching as compilation option.
include/net/netfilter/nf_conntrack_ecache.h +69 -61
@@ -6,61 +6,52 @@#define _NF_CONNTRACK_ECACHE_H#include <net/netfilter/nf_conntrack.h> -#include <linux/interrupt.h> #include <net/net_namespace.h> #include <net/netfilter/nf_conntrack_expect.h> +#include <linux/netfilter/nf_conntrack_common.h> +#include <linux/netfilter/nf_conntrack_tuple_common.h> +#include <net/netfilter/nf_conntrack_extend.h> -/* Connection tracking event bits */ +/* Connection tracking event types */ enum ip_conntrack_events { -/* New conntrack */ -IPCT_NEW_BIT =0 , -IPCT_NEW = (1 <<IPCT_NEW_BIT ), - -/* Expected connection */ -IPCT_RELATED_BIT =1 , -IPCT_RELATED = (1 <<IPCT_RELATED_BIT ), - -/* Destroyed conntrack */ -IPCT_DESTROY_BIT =2 , -IPCT_DESTROY = (1 <<IPCT_DESTROY_BIT ), - -/* Status has changed */ -IPCT_STATUS_BIT =3 , -IPCT_STATUS = (1 <<IPCT_STATUS_BIT ), +IPCT_NEW =0 ,/* new conntrack */ +IPCT_RELATED =1 ,/* related conntrack */ +IPCT_DESTROY =2 ,/* destroyed conntrack */ +IPCT_STATUS =3 ,/* status has changed */ +IPCT_PROTOINFO =4 ,/* protocol information has changed */ +IPCT_HELPER =5 ,/* new helper has been set */ +IPCT_MARK =6 ,/* new mark has been set */ +IPCT_NATSEQADJ =7 ,/* NAT is doing sequence adjustment */ +IPCT_SECMARK =8 ,/* new security mark has been set */ +}; -/* Update of protocol info */ -IPCT_PROTOINFO_BIT =4 , -IPCT_PROTOINFO = (1 <<IPCT_PROTOINFO_BIT ), +enum ip_conntrack_expect_events { +IPEXP_NEW =0 ,/* new expectation */ +}; -/* New helper for conntrack */ -IPCT_HELPER_BIT =5 , -IPCT_HELPER = (1 <<IPCT_HELPER_BIT ), +struct nf_conntrack_ecache { +unsigned long cache;/* bitops want long */ +}; -/* Mark is set */ -IPCT_MARK_BIT =6 , -IPCT_MARK = (1 <<IPCT_MARK_BIT ), +static inline struct nf_conntrack_ecache * +nf_ct_ecache_find (const struct nf_conn *ct) +{ +return nf_ct_ext_find (ct,NF_CT_EXT_ECACHE ); +} -/* NAT sequence adjustment */ -IPCT_NATSEQADJ_BIT =7 , -IPCT_NATSEQADJ = (1 <<IPCT_NATSEQADJ_BIT ), +static inline struct nf_conntrack_ecache * +nf_ct_ecache_ext_add (struct nf_conn *ct,gfp_t gfp) +{ +struct net *net =nf_ct_net (ct); -/* Secmark is set */ -IPCT_SECMARK_BIT =8 , -IPCT_SECMARK = (1 <<IPCT_SECMARK_BIT ), -}; +if (!net->ct.sysctl_events) +return NULL ; -enum ip_conntrack_expect_events { -IPEXP_NEW_BIT =0 , -IPEXP_NEW = (1 <<IPEXP_NEW_BIT ), +return nf_ct_ext_add (ct,NF_CT_EXT_ECACHE , gfp); }; #ifdef CONFIG_NF_CONNTRACK_EVENTS -struct nf_conntrack_ecache { - struct nf_conn *ct; - unsigned int events; -}; - /* This structure is passed to event handler */ struct nf_ct_event { struct nf_conn *ct;
@@ -76,30 +67,30 @@ extern struct nf_ct_event_notifier *nf_conntrack_event_cb;extern int nf_conntrack_register_notifier (struct nf_ct_event_notifier *nb);extern void nf_conntrack_unregister_notifier (struct nf_ct_event_notifier *nb); -extern void nf_ct_deliver_cached_events (const struct nf_conn *ct); -extern void __nf_ct_event_cache_init (struct nf_conn *ct); -extern void nf_ct_event_cache_flush (struct net *net); +extern void nf_ct_deliver_cached_events (struct nf_conn *ct);static inline void nf_conntrack_event_cache (enum ip_conntrack_events event,struct nf_conn *ct) { -struct net *net =nf_ct_net (ct); -struct nf_conntrack_ecache *ecache; - -local_bh_disable (); - ecache =per_cpu_ptr (net->ct.ecache,raw_smp_processor_id ()); -if (ct != ecache->ct) -__nf_ct_event_cache_init (ct); - ecache->events |= event; -local_bh_enable (); +struct nf_conntrack_ecache *e; + +if (nf_conntrack_event_cb ==NULL ) +return ; + + e =nf_ct_ecache_find (ct); +if (e ==NULL ) +return ; + +set_bit (event, &e->cache); }static inline void -nf_conntrack_event_report (enum ip_conntrack_events event, -struct nf_conn *ct, -u32 pid, -int report) +nf_conntrack_eventmask_report (unsigned int eventmask, +struct nf_conn *ct, +u32 pid, +int report) { +struct net *net =nf_ct_net (ct);struct nf_ct_event_notifier *notify;rcu_read_lock ();
@@ -107,22 +98,32 @@ nf_conntrack_event_report(enum ip_conntrack_events event,if (notify ==NULL ) goto out_unlock; +if (!net->ct.sysctl_events) + goto out_unlock; +if (nf_ct_is_confirmed (ct) && !nf_ct_is_dying (ct)) {struct nf_ct_event item = { .ct = ct, .pid = pid, .report = report }; - notify->fcn (event, &item); + notify->fcn (eventmask, &item); } out_unlock:rcu_read_unlock (); }static inline void +nf_conntrack_event_report (enum ip_conntrack_events event,struct nf_conn *ct, +u32 pid,int report) +{ +nf_conntrack_eventmask_report (1 << event, ct, pid, report); +} + +static inline void nf_conntrack_event (enum ip_conntrack_events event,struct nf_conn *ct) { -nf_conntrack_event_report (event, ct,0 ,0 ); +nf_conntrack_eventmask_report (1 << event, ct,0 ,0 ); }struct nf_exp_event {
@@ -145,6 +146,7 @@ nf_ct_expect_event_report(enum ip_conntrack_expect_events event, u32 pid, int report) { +struct net *net =nf_ct_exp_net (exp);struct nf_exp_event_notifier *notify;rcu_read_lock ();
@@ -152,13 +154,16 @@ nf_ct_expect_event_report(enum ip_conntrack_expect_events event,if (notify ==NULL ) goto out_unlock; +if (!net->ct.sysctl_events) + goto out_unlock; + {struct nf_exp_event item = { .exp = exp, .pid = pid, .report = report }; - notify->fcn (event, &item); + notify->fcn (1 << event, &item); } out_unlock:rcu_read_unlock ();
@@ -178,6 +183,10 @@ extern void nf_conntrack_ecache_fini(struct net *net);static inline void nf_conntrack_event_cache (enum ip_conntrack_events event,struct nf_conn *ct) {} +static inline void nf_conntrack_eventmask_report (unsigned int eventmask, +struct nf_conn *ct, +u32 pid, +int report) {}static inline void nf_conntrack_event (enum ip_conntrack_events event,struct nf_conn *ct) {} static inline void nf_conntrack_event_report(enum ip_conntrack_events event,
@@ -191,7 +200,6 @@ static inline void nf_ct_expect_event_report(enum ip_conntrack_expect_events e, struct nf_conntrack_expect *exp, u32 pid, int report) {} -static inline void nf_ct_event_cache_flush (struct net *net) {}static inline int nf_conntrack_ecache_init (struct net *net) {
include/net/netfilter/nf_conntrack_extend.h +2 -0
@@ -8,12 +8,14 @@ enum nf_ct_ext_idNF_CT_EXT_HELPER ,NF_CT_EXT_NAT ,NF_CT_EXT_ACCT , +NF_CT_EXT_ECACHE , NF_CT_EXT_NUM, };#define NF_CT_EXT_HELPER_TYPE struct nf_conn_help#define NF_CT_EXT_NAT_TYPE struct nf_conn_nat#define NF_CT_EXT_ACCT_TYPE struct nf_conn_counter +#define NF_CT_EXT_ECACHE_TYPE struct nf_conntrack_ecache/* Extensions: optional stuff which isn't permanently in struct. */ struct nf_ct_ext {
include/net/netns/conntrack.h +2 -3
@@ -15,15 +15,14 @@ struct netns_ct {struct hlist_head *expect_hash;struct hlist_nulls_head unconfirmed;struct ip_conntrack_stat *stat; -#ifdef CONFIG_NF_CONNTRACK_EVENTS -struct nf_conntrack_ecache *ecache; -#endif +int sysctl_events;int sysctl_acct;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 *event_sysctl_header;#endif int hash_vmalloc;int expect_vmalloc;
net/netfilter/nf_conntrack_core.c +8 -7
@@ -39,6 +39,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_ecache.h> #include <net/netfilter/nf_nat.h> #include <net/netfilter/nf_nat_core.h>
@@ -577,6 +578,7 @@ init_conntrack(struct net *net, }nf_ct_acct_ext_add (ct,GFP_ATOMIC ); +nf_ct_ecache_ext_add (ct,GFP_ATOMIC );spin_lock_bh (&nf_conntrack_lock); exp =nf_ct_find_expectation (net, tuple);
@@ -1031,8 +1033,6 @@ static void nf_conntrack_cleanup_init_net(void)static void nf_conntrack_cleanup_net (struct net *net) { -nf_ct_event_cache_flush (net); -nf_conntrack_ecache_fini (net); i_see_dead_people:nf_ct_iterate_cleanup (net, kill_all,NULL );if (atomic_read (&net->ct.count) !=0 ) {
@@ -1045,6 +1045,7 @@ static void nf_conntrack_cleanup_net(struct net *net)nf_ct_free_hashtable (net->ct.hash, net->ct.hash_vmalloc, nf_conntrack_htable_size); +nf_conntrack_ecache_fini (net);nf_conntrack_acct_fini (net);nf_conntrack_expect_fini (net);free_percpu (net->ct.stat);
@@ -1220,9 +1221,6 @@ static int nf_conntrack_init_net(struct net *net) ret = -ENOMEM ; goto err_stat; } - ret =nf_conntrack_ecache_init (net); -if (ret <0 ) - goto err_ecache; net->ct.hash =nf_ct_alloc_hashtable (&nf_conntrack_htable_size, &net->ct.hash_vmalloc,1 );if (!net->ct.hash) {
@@ -1236,6 +1234,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_ecache_init (net); +if (ret <0 ) + goto err_ecache;/* Set up fake conntrack: - to never be deleted, not in any hashes */
@@ -1248,14 +1249,14 @@ static int nf_conntrack_init_net(struct net *net)return 0 ; +err_ecache: +nf_conntrack_acct_fini (net); err_acct:nf_conntrack_expect_fini (net); err_expect:nf_ct_free_hashtable (net->ct.hash, net->ct.hash_vmalloc, nf_conntrack_htable_size); err_hash: -nf_conntrack_ecache_fini (net); -err_ecache:free_percpu (net->ct.stat); err_stat:return ret;
net/netfilter/nf_conntrack_ecache.c +117 -68
220 lines not on this page · raw message
net/netfilter/nf_conntrack_netlink.c +25 -24
112 lines not on this page · raw message
netfilter 04/07: conntrack: move helper destruction to nf_ct_helper_destroy()
In reply to Patrick McHardycommit 9858a3ae1d4b390fbaa9c30b83cb66d861b76294
Author: Pablo Neira Ayuso <[email protected]> Date: Sat Jun 13 12:28:22 2009 +0200
netfilter: conntrack: move helper destruction to nf_ct_helper_destroy()
This patch moves the helper destruction to a function that lives in nf_conntrack_helper.c. This new function is used in the patch to add ctnetlink reliable event delivery.
include/net/netfilter/nf_conntrack_helper.h +2 -0
@@ -50,6 +50,8 @@ extern struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp);extern int __nf_ct_try_assign_helper (struct nf_conn *ct,gfp_t flags); +extern void nf_ct_helper_destroy (struct nf_conn *ct); +static inline struct nf_conn_help *nfct_help (const struct nf_conn *ct) {return nf_ct_ext_find (ct,NF_CT_EXT_HELPER );
net/netfilter/nf_conntrack_core.c +1 -10
@@ -224,17 +224,8 @@ static void death_by_timeout(unsigned long ul_conntrack) {struct nf_conn *ct = (void *)ul_conntrack;struct net *net =nf_ct_net (ct); -struct nf_conn_help *help =nfct_help (ct); -struct nf_conntrack_helper *helper; - -if (help) { -rcu_read_lock (); - helper =rcu_dereference (help->helper); -if (helper && helper->destroy) - helper->destroy (ct); -rcu_read_unlock (); - } +nf_ct_helper_destroy (ct);spin_lock_bh (&nf_conntrack_lock);/* Inside lock so preempt is disabled on module removal path. * Otherwise we can get spurious warnings. */
net/netfilter/nf_conntrack_helper.c +14 -0
@@ -136,6 +136,20 @@ static inline int unhelp(struct nf_conntrack_tuple_hash *i,return 0 ; } +void nf_ct_helper_destroy (struct nf_conn *ct) +{ +struct nf_conn_help *help =nfct_help (ct); +struct nf_conntrack_helper *helper; + +if (help) { +rcu_read_lock (); + helper =rcu_dereference (help->helper); +if (helper && helper->destroy) + helper->destroy (ct); +rcu_read_unlock (); + } +} +int nf_conntrack_helper_register (struct nf_conntrack_helper *me) {unsigned int h =helper_hash (&me->tuple);
list_nulls 05/07: add hlist_nulls_add_head and hlist_nulls_del
In reply to Patrick McHardycommit d219dce76c64f2c883dad0537fa09a56d5ff0a10
Author: Pablo Neira Ayuso <[email protected]> Date: Sat Jun 13 12:28:57 2009 +0200
list_nulls: add hlist_nulls_add_head and hlist_nulls_del
This patch adds the hlist_nulls_add_head() function which is based on hlist_nulls_add_head_rcu() but without the use of rcu_assign_pointer(). It also adds hlist_nulls_del which is exactly the same like hlist_nulls_del_rcu().
include/linux/list_nulls.h +18 -0
@@ -56,6 +56,18 @@ static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)return is_a_nulls (h->first); } +static inline void hlist_nulls_add_head (struct hlist_nulls_node *n, +struct hlist_nulls_head *h) +{ +struct hlist_nulls_node *first = h->first; + + n->next = first; + n->pprev = &h->first; + h->first = n; +if (!is_a_nulls (first)) + first->pprev = &n->next; +} +static inline void __hlist_nulls_del (struct hlist_nulls_node *n) {struct hlist_nulls_node *next = n->next;
@@ -65,6 +77,12 @@ static inline void __hlist_nulls_del(struct hlist_nulls_node *n) next->pprev = pprev; } +static inline void hlist_nulls_del (struct hlist_nulls_node *n) +{ +__hlist_nulls_del (n); + n->pprev =LIST_POISON2 ; +} +/** * hlist_nulls_for_each_entry - iterate over list of given type * @tpos: the type * to use as a loop cursor.
netfilter 06/07: conntrack: optional reliable conntrack event delivery
In reply to Patrick McHardycommit dd7669a92c6066b2b31bae7e04cd787092920883
Author: Pablo Neira Ayuso <[email protected]> Date: Sat Jun 13 12:30:52 2009 +0200
netfilter: conntrack: optional reliable conntrack event delivery
This patch improves ctnetlink event reliability if one broadcast listener has set the NETLINK_BROADCAST_ERROR socket option.
The logic is the following: if an event delivery fails, we keep the undelivered events in the missed event cache. Once the next packet arrives, we add the new events (if any) to the missed events in the cache and we try a new delivery, and so on. Thus, if ctnetlink fails to deliver an event, we try to deliver them once we see a new packet. Therefore, we may lose state transitions but the userspace process gets in sync at some point.
At worst case, if no events were delivered to userspace, we make sure that destroy events are successfully delivered. Basically, if ctnetlink fails to deliver the destroy event, we remove the conntrack entry from the hashes and we insert them in the dying list, which contains inactive entries. Then, the conntrack timer is added with an extra grace timeout of random32() % 15 seconds to trigger the event again (this grace timeout is tunable via /proc). The use of a limited random timeout value allows distributing the "destroy" resends, thus, avoiding accumulating lots "destroy" events at the same time. Event delivery may re-order but we can identify them by means of the tuple plus the conntrack ID.
The maximum number of conntrack entries (active or inactive) is still handled by nf_conntrack_max. Thus, we may start dropping packets at some point if we accumulate a lot of inactive conntrack entries that did not successfully report the destroy event to userspace.
During my stress tests consisting of setting a very small buffer of 2048 bytes for conntrackd and the NETLINK_BROADCAST_ERROR socket flag, and generating lots of very small connections, I noticed very few destroy entries on the fly waiting to be resend.
A simple way to test this patch consist of creating a lot of entries, set a very small Netlink buffer in conntrackd (+ a patch which is not in the git tree to set the BROADCAST_ERROR flag) and invoke `conntrack -F'.
For expectations, no changes are introduced in this patch. Currently, event delivery is only done for new expectations (no events from expectation expiration, removal and confirmation). In that case, they need a per-expectation event cache to implement the same idea that is exposed in this patch.
This patch can be useful to provide reliable flow-accouting. We still have to add a new conntrack extension to store the creation and destroy time.
include/net/netfilter/nf_conntrack.h +2 -0
@@ -201,6 +201,8 @@ extern struct nf_conntrack_tuple_hash * __nf_conntrack_find(struct net *net, const struct nf_conntrack_tuple *tuple);extern void nf_conntrack_hash_insert (struct nf_conn *ct); +extern void nf_ct_delete_from_lists (struct nf_conn *ct); +extern void nf_ct_insert_dying_list (struct nf_conn *ct);extern void nf_conntrack_flush_report (struct net *net,u32 pid,int report);
include/net/netfilter/nf_conntrack_ecache.h +44 -17
109 lines not on this page · raw message
include/net/netns/conntrack.h +2 -0
@@ -14,8 +14,10 @@ struct netns_ct {struct hlist_nulls_head *hash;struct hlist_head *expect_hash;struct hlist_nulls_head unconfirmed; +struct hlist_nulls_head dying;struct ip_conntrack_stat *stat;int sysctl_events; +unsigned int sysctl_events_retry_timeout;int sysctl_acct;int sysctl_checksum;unsigned int sysctl_log_invalid;/* Log invalid packets */
net/netfilter/nf_conntrack_core.c +78 -11
132 lines not on this page · raw message
net/netfilter/nf_conntrack_ecache.c +26 -2
@@ -56,8 +56,21 @@ void nf_ct_deliver_cached_events(struct nf_conn *ct) .pid = 0, .report = 0 }; - - notify->fcn (events, &item); +int ret; +/* We make a copy of the missed event cache without taking +* the lock, thus we may send missed events twice. However, +* this does not harm and it happens very rarely. */ +unsigned long missed = e->missed; + + ret = notify->fcn (events | missed, &item); +if (unlikely (ret <0 || missed)) { +spin_lock_bh (&ct->lock); +if (ret <0 ) + e->missed |= events; +else + e->missed &= ~missed; +spin_unlock_bh (&ct->lock); + } } out_unlock:
@@ -133,6 +146,7 @@ EXPORT_SYMBOL_GPL(nf_ct_expect_unregister_notifier);#define NF_CT_EVENTS_DEFAULT 1static int nf_ct_events__read_mostly =NF_CT_EVENTS_DEFAULT ; +static int nf_ct_events_retry_timeout__read_mostly =15 *HZ ;#ifdef CONFIG_SYSCTL static struct ctl_table event_sysctl_table[] = {
@@ -144,6 +158,14 @@ static struct ctl_table event_sysctl_table[] = { .mode = 0644, .proc_handler = proc_dointvec, }, + { + .ctl_name = CTL_UNNUMBERED, + .procname = "nf_conntrack_events_retry_timeout", + .data = &init_net.ct.sysctl_events_retry_timeout, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = proc_dointvec_jiffies, + }, {} };#endif /* CONFIG_SYSCTL */
@@ -165,6 +187,7 @@ static int nf_conntrack_event_init_sysctl(struct net *net) goto out; table[0 ].data = &net->ct.sysctl_events; + table[1 ].data = &net->ct.sysctl_events_retry_timeout; net->ct.event_sysctl_header = register_net_sysctl_table(net,
@@ -205,6 +228,7 @@ int nf_conntrack_ecache_init(struct net *net)int ret; net->ct.sysctl_events = nf_ct_events; + net->ct.sysctl_events_retry_timeout = nf_ct_events_retry_timeout;if (net_eq (net, &init_net)) { ret =nf_ct_extend_register (&event_extend);
net/netfilter/nf_conntrack_netlink.c +14 -5
@@ -463,6 +463,7 @@ ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)struct sk_buff *skb;unsigned int type;unsigned int flags =0 , group; +int err;/* ignore our fake conntrack entry */ if (ct == &nf_conntrack_untracked)
@@ -558,7 +559,10 @@ ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)rcu_read_unlock ();nlmsg_end (skb, nlh); -nfnetlink_send (skb, item->pid, group, item->report,GFP_ATOMIC ); + err =nfnetlink_send (skb, item->pid, group, item->report,GFP_ATOMIC ); +if (err == -ENOBUFS || err == -EAGAIN ) +return -ENOBUFS ; +return 0 ; nla_put_failure:
@@ -798,10 +802,15 @@ ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, } } -nf_conntrack_event_report (IPCT_DESTROY , - ct, -NETLINK_CB (skb).pid, -nlmsg_report (nlh)); +if (nf_conntrack_event_report (IPCT_DESTROY , ct, +NETLINK_CB (skb).pid, +nlmsg_report (nlh)) <0 ) { +nf_ct_delete_from_lists (ct); +/* we failed to report the event, try later */ +nf_ct_insert_dying_list (ct); +nf_ct_put (ct); +return 0 ; + }/* death_by_timeout would report the event again */ set_bit (IPS_DYING_BIT , &ct->status);
x_tables 07/07: Convert printk to pr_err
In reply to Patrick McHardycommit 3dd5d7e3ba5e9b05586ff0d59ae6d700b7b7c607
Author: Joe Perches <[email protected]> Date: Sat Jun 13 12:32:39 2009 +0200
x_tables: Convert printk to pr_err
net/netfilter/x_tables.c +8 -8
@@ -364,14 +364,14 @@ int xt_check_match(struct xt_mtchk_param *par,* ebt_among is exempt from centralized matchsize checking * because it uses a dynamic-size data set. */ -printk ("%s_tables: %s match: invalid size %Zu != %u\n" , +pr_err ("%s_tables: %s match: invalid size %Zu != %u\n" , xt_prefix[par->family], par->match->name,XT_ALIGN (par->match->matchsize), size);return -EINVAL ; }if (par->match->table !=NULL &&strcmp (par->match->table, par->table) !=0 ) { -printk ("%s_tables: %s match: only valid in %s table, not %s\n" , +pr_err ("%s_tables: %s match: only valid in %s table, not %s\n" , xt_prefix[par->family], par->match->name, par->match->table, par->table);return -EINVAL ;
@@ -379,7 +379,7 @@ int xt_check_match(struct xt_mtchk_param *par,if (par->match->hooks && (par->hook_mask & ~par->match->hooks) !=0 ) {char used[64 ], allow[64 ]; - printk("%s_tables: %s match: used from hooks %s, but only " + pr_err("%s_tables: %s match: used from hooks %s, but only " "valid from %s\n", xt_prefix[par->family], par->match->name, textify_hooks(used, sizeof(used), par->hook_mask),
@@ -387,7 +387,7 @@ int xt_check_match(struct xt_mtchk_param *par,return -EINVAL ; }if (par->match->proto && (par->match->proto != proto || inv_proto)) { -printk ("%s_tables: %s match: only valid for protocol %u\n" , +pr_err ("%s_tables: %s match: only valid for protocol %u\n" , xt_prefix[par->family], par->match->name, par->match->proto);return -EINVAL ;
@@ -514,14 +514,14 @@ int xt_check_target(struct xt_tgchk_param *par, unsigned int size, u_int8_t proto, bool inv_proto) { if (XT_ALIGN(par->target->targetsize) != size) { - printk("%s_tables: %s target: invalid size %Zu != %u\n", + pr_err("%s_tables: %s target: invalid size %Zu != %u\n", xt_prefix[par->family], par->target->name, XT_ALIGN(par->target->targetsize), size);return -EINVAL ; }if (par->target->table !=NULL &&strcmp (par->target->table, par->table) !=0 ) { -printk ("%s_tables: %s target: only valid in %s table, not %s\n" , +pr_err ("%s_tables: %s target: only valid in %s table, not %s\n" , xt_prefix[par->family], par->target->name, par->target->table, par->table);return -EINVAL ;
@@ -529,7 +529,7 @@ int xt_check_target(struct xt_tgchk_param *par,if (par->target->hooks && (par->hook_mask & ~par->target->hooks) !=0 ) {char used[64 ], allow[64 ]; - printk("%s_tables: %s target: used from hooks %s, but only " + pr_err("%s_tables: %s target: used from hooks %s, but only " "usable from %s\n", xt_prefix[par->family], par->target->name, textify_hooks(used, sizeof(used), par->hook_mask),
@@ -537,7 +537,7 @@ int xt_check_target(struct xt_tgchk_param *par,return -EINVAL ; }if (par->target->proto && (par->target->proto != proto || inv_proto)) { -printk ("%s_tables: %s target: only valid for protocol %u\n" , +pr_err ("%s_tables: %s target: only valid for protocol %u\n" , xt_prefix[par->family], par->target->name, par->target->proto);return -EINVAL ;
David Miller
maintainerFrom: Patrick McHardy <[email protected]>
Date: Sat, 13 Jun 2009 15:09:34 +0200 (MEST)
Quoted 13 lines
following is a final netfilter update for 2.6.31, containing:
- a fix for a warning when reading one of the the
nf_logfiles- a patch to use the new
mod_timer_pending()function in conntrack to avoid taking the global conntrack lock for timer updates- a set of patches from Pablo to make conntrack event notifications runtime selectable and optionally reliable. This works by making sure at least the final destroy event is successfully transmitted, otherwise the event is re-broadcast until a configurable timeout is reached.
Quoted 7 lines
- a patch from Joe Perches to convert a bunch of printks without a log level to
pr_err()Please apply or pull from:
git://git.kernel.org/pub/scm/linux/kernel/git/kaber/nf-next-2.6.gitmaster
Pulled, thanks a lot Patrick.