netfilter: conntrack: avoid eache extension allocation
Subsystems (3)
Florian Westphal
maintainercover netfilter: conntrack: avoid eache extension allocation
This patch series changes ecache infra to no longer allocate the extension by default.
After this series, the default behaviour is to allocate the extension if either a conntrack template has been set to configure event masks via nft/iptables ruleset, the syctl 'nf_conntrack_events' is set to 1, or if a userspace program has subscribed to one of the ctnetlink event groups.
This has advantages in case the events are not used:
1. Conntrack allocation/free avoids extra kmalloc/kfree call.
2. nf_confirm hook doesn't perform an indirect call into ctnetlink only to discover that there is nothing to do.
Florian Westphal (4):
netfilter: nfnetlink: allow to detect if ctnetlink listeners exist
netfilter: conntrack: un-inline nf_ct_ecache_ext_add
netfilter: conntrack: add nf_conntrack_events autodetect mode
netfilter: prefer extension check to pointer check
.../networking/nf_conntrack-sysctl.rst | 5 +- include/net/netfilter/nf_conntrack_core.h | 2 +- include/net/netfilter/nf_conntrack_ecache.h | 49 ++++++------------- include/net/netns/conntrack.h | 1 + net/netfilter/nf_conntrack_core.c | 15 ++++-- net/netfilter/nf_conntrack_ecache.c | 39 ++++++++++++++- net/netfilter/nf_conntrack_standalone.c | 2 +- net/netfilter/nfnetlink.c | 40 +++++++++++++-- 8 files changed, 108 insertions(+), 45 deletions(-)
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 1/4 netfilter: nfnetlink: allow to detect if ctnetlink listeners exist
At this time, every new conntrack gets the 'event cache extension' enabled for it.
This is because the 'net.netfilter.nf_conntrack_events' sysctl defaults to 1.
Changing the default to 0 means that commands that rely on the event notification extension, e.g. 'conntrack -E' or conntrackd, stop working.
We COULD detect if there is a listener by means of 'nfnetlink_has_listeners()' and only add the extension if this is true.
The downside is a dependency from conntrack module to nfnetlink module.
This adds a different way: inc/dec a counter whenever a ctnetlink group is being (un)subscribed and toggle a flag in struct net.
Next patches will take advantage of this and will only add the event extension if the flag is set.
--- include/net/netns/conntrack.h | 1 + net/netfilter/nfnetlink.c | 40 ++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-)
include/net/netns/conntrack.h +1 -0
@@ -95,6 +95,7 @@ struct nf_ip_net {struct netns_ct {#ifdef CONFIG_NF_CONNTRACK_EVENTS +bool ctnetlink_has_listener;bool ecache_dwork_pending;#endif u8 sysctl_log_invalid;/* Log invalid packets */
net/netfilter/nfnetlink.c +37 -3
@@ -45,6 +45,7 @@ MODULE_DESCRIPTION("Netfilter messages via netlink socket");static unsigned int nfnetlink_pernet_id__read_mostly ;struct nfnl_net { +unsigned int ctnetlink_listeners;struct sock *nfnl; };
@@ -654,7 +655,6 @@ static void nfnetlink_rcv(struct sk_buff *skb)netlink_rcv_skb (skb, nfnetlink_rcv_msg); } -#ifdef CONFIG_MODULES static int nfnetlink_bind (struct net *net,int group) {const struct nfnetlink_subsystem *ss;
@@ -670,9 +670,44 @@ static int nfnetlink_bind(struct net *net, int group)rcu_read_unlock ();if (!ss)request_module_nowait ("nfnetlink-subsys-%d" , type); + +#ifdef CONFIG_NF_CONNTRACK_EVENTS +if (type ==NFNL_SUBSYS_CTNETLINK ) { +struct nfnl_net *nfnlnet =nfnl_pernet (net); + +nfnl_lock (NFNL_SUBSYS_CTNETLINK ); + +if (WARN_ON_ONCE (nfnlnet->ctnetlink_listeners ==UINT_MAX )) { +nfnl_unlock (NFNL_SUBSYS_CTNETLINK ); +return -EOVERFLOW ; + } + + nfnlnet->ctnetlink_listeners++; +if (nfnlnet->ctnetlink_listeners ==1 ) +WRITE_ONCE (net->ct.ctnetlink_has_listener, true); +nfnl_unlock (NFNL_SUBSYS_CTNETLINK ); + } +#endif return 0 ; } + +static void nfnetlink_unbind (struct net *net,int group) +{ +#ifdef CONFIG_NF_CONNTRACK_EVENTS +int type = nfnl_group2type[group]; + +if (type ==NFNL_SUBSYS_CTNETLINK ) { +struct nfnl_net *nfnlnet =nfnl_pernet (net); + +nfnl_lock (NFNL_SUBSYS_CTNETLINK ); +WARN_ON_ONCE (nfnlnet->ctnetlink_listeners ==0 ); + nfnlnet->ctnetlink_listeners--; +if (nfnlnet->ctnetlink_listeners ==0 ) +WRITE_ONCE (net->ct.ctnetlink_has_listener, false); +nfnl_unlock (NFNL_SUBSYS_CTNETLINK ); + }#endif +}static int __net_init nfnetlink_net_init (struct net *net) {
@@ -680,9 +715,8 @@ static int __net_init nfnetlink_net_init(struct net *net)struct netlink_kernel_cfg cfg = { .groups =NFNLGRP_MAX , .input = nfnetlink_rcv, -#ifdef CONFIG_MODULES .bind = nfnetlink_bind, -#endif + .unbind = nfnetlink_unbind, }; nfnlnet->nfnl =netlink_kernel_create (net,NETLINK_NETFILTER , &cfg);
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 2/4 netfilter: conntrack: un-inline nf_ct_ecache_ext_add
In reply to Florian WestphalOnly called when new ct is allocated or the extension isn't present. This function will be extended, place this in the conntrack module instead of inlining.
The callers already depend on nf_conntrack module. Return value is changed to bool, noone used the returned pointer.
Make sure that the core drops the newly allocated conntrack if the extension is requested but can't be added. This makes it necessary to ifdef the section, as the stub always returns false we'd drop every new conntrack if the the ecache extension is disabled in kconfig.
Add from data path (xt_CT, nft_ct) is unchanged.
--- include/net/netfilter/nf_conntrack_ecache.h | 30 ++++----------------- net/netfilter/nf_conntrack_core.c | 14 +++++++--- net/netfilter/nf_conntrack_ecache.c | 22 +++++++++++++++ 3 files changed, 38 insertions(+), 28 deletions(-)
include/net/netfilter/nf_conntrack_ecache.h +5 -25
@@ -36,31 +36,6 @@ nf_ct_ecache_find(const struct nf_conn *ct)#endif } -static inline struct nf_conntrack_ecache * -nf_ct_ecache_ext_add (struct nf_conn *ct,u16 ctmask,u16 expmask,gfp_t gfp) -{ -#ifdef CONFIG_NF_CONNTRACK_EVENTS -struct net *net =nf_ct_net (ct); -struct nf_conntrack_ecache *e; - -if (!ctmask && !expmask && net->ct.sysctl_events) { - ctmask = ~0 ; - expmask = ~0 ; - } -if (!ctmask && !expmask) -return NULL ; - - e =nf_ct_ext_add (ct,NF_CT_EXT_ECACHE , gfp); -if (e) { - e->ctmask = ctmask; - e->expmask = expmask; - } -return e; -#else -return NULL ; -#endif -} -#ifdef CONFIG_NF_CONNTRACK_EVENTS /* This structure is passed to event handler */
@@ -89,6 +64,7 @@ void nf_ct_deliver_cached_events(struct nf_conn *ct);int nf_conntrack_eventmask_report (unsigned int eventmask,struct nf_conn *ct,u32 portid,int report); +bool nf_ct_ecache_ext_add (struct nf_conn *ct,u16 ctmask,u16 expmask,gfp_t gfp);#else static inline void nf_ct_deliver_cached_events (const struct nf_conn *ct)
@@ -103,6 +79,10 @@ static inline int nf_conntrack_eventmask_report(unsigned int eventmask,return 0 ; } +static inline bool nf_ct_ecache_ext_add (struct nf_conn *ct,u16 ctmask,u16 expmask,gfp_t gfp) +{ +return false; +}#endif static inline void
net/netfilter/nf_conntrack_core.c +11 -3
@@ -1701,7 +1701,9 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,struct nf_conn *ct;struct nf_conn_help *help;struct nf_conntrack_tuple repl_tuple; +#ifdef CONFIG_NF_CONNTRACK_EVENTS struct nf_conntrack_ecache *ecache; +#endif struct nf_conntrack_expect *exp =NULL ;const struct nf_conntrack_zone *zone;struct nf_conn_timeout *timeout_ext;
@@ -1734,10 +1736,16 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,nf_ct_tstamp_ext_add (ct,GFP_ATOMIC );nf_ct_labels_ext_add (ct); +#ifdef CONFIG_NF_CONNTRACK_EVENTS ecache = tmpl ?nf_ct_ecache_find (tmpl) :NULL ; -nf_ct_ecache_ext_add (ct, ecache ? ecache->ctmask :0 , - ecache ? ecache->expmask :0 , -GFP_ATOMIC ); + +if (!nf_ct_ecache_ext_add (ct, ecache ? ecache->ctmask :0 , + ecache ? ecache->expmask :0 , +GFP_ATOMIC )) { +nf_conntrack_free (ct); +return ERR_PTR (-ENOMEM ); + } +#endif cnet =nf_ct_pernet (net);if (cnet->expect_count) {
net/netfilter/nf_conntrack_ecache.c +22 -0
@@ -298,6 +298,28 @@ void nf_conntrack_ecache_work(struct net *net, enum nf_ct_ecache_state state) } } +bool nf_ct_ecache_ext_add (struct nf_conn *ct,u16 ctmask,u16 expmask,gfp_t gfp) +{ +struct net *net =nf_ct_net (ct); +struct nf_conntrack_ecache *e; + +if (!ctmask && !expmask && net->ct.sysctl_events) { + ctmask = ~0 ; + expmask = ~0 ; + } +if (!ctmask && !expmask) +return false; + + e =nf_ct_ext_add (ct,NF_CT_EXT_ECACHE , gfp); +if (e) { + e->ctmask = ctmask; + e->expmask = expmask; + } + +return e !=NULL ; +} +EXPORT_SYMBOL_GPL (nf_ct_ecache_ext_add); +#define NF_CT_EVENTS_DEFAULT 1static int nf_ct_events__read_mostly =NF_CT_EVENTS_DEFAULT ;
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 3/4 netfilter: conntrack: add nf_conntrack_events autodetect mode
In reply to Florian WestphalThis adds the new nf_conntrack_events=2 mode and makes it the default.
This leverages the earlier flag in struct net to allow to avoid the event extension as long as no event listener is active in the namespace.
This avoids, for most cases, allocation of ct->ext area. A followup patch will take further advantage of this by avoiding calls down into the event framework if the extension isn't present.
--- .../networking/nf_conntrack-sysctl.rst | 5 +++- net/netfilter/nf_conntrack_core.c | 3 ++- net/netfilter/nf_conntrack_ecache.c | 27 ++++++++++++++----- net/netfilter/nf_conntrack_standalone.c | 2 +- 4 files changed, 28 insertions(+), 9 deletions(-)
Documentation/networking/nf_conntrack-sysctl.rst +4 -1
@@ -34,10 +34,13 @@ nf_conntrack_count - INTEGER (read-only) nf_conntrack_events - BOOLEAN - 0 - disabled - - not 0 - enabled (default) + - 1 - enabled + - 2 - auto (default) If this option is enabled, the connection tracking code will provide userspace with connection tracking events via ctnetlink. + The default allocates the extension if a userspace program is + listening to ctnetlink events. nf_conntrack_expect_max - INTEGER Maximum size of expectation table. Default value is
net/netfilter/nf_conntrack_core.c +2 -1
@@ -1739,7 +1739,8 @@ init_conntrack(struct net *net, struct nf_conn *tmpl,#ifdef CONFIG_NF_CONNTRACK_EVENTS ecache = tmpl ?nf_ct_ecache_find (tmpl) :NULL ; -if (!nf_ct_ecache_ext_add (ct, ecache ? ecache->ctmask :0 , +if ((ecache || net->ct.sysctl_events) && + !nf_ct_ecache_ext_add (ct, ecache ? ecache->ctmask :0 , ecache ? ecache->expmask :0 ,GFP_ATOMIC )) {nf_conntrack_free (ct);
net/netfilter/nf_conntrack_ecache.c +21 -6
@@ -303,12 +303,27 @@ bool nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfpstruct net *net =nf_ct_net (ct);struct nf_conntrack_ecache *e; -if (!ctmask && !expmask && net->ct.sysctl_events) { - ctmask = ~0 ; - expmask = ~0 ; +switch (net->ct.sysctl_events) { +case 0 : +/* assignment via template / ruleset? ignore sysctl. */ +if (ctmask || expmask) +break ; +return true; +case 2 :/* autodetect: no event listener, don't allocate extension. */ +if (!READ_ONCE (net->ct.ctnetlink_has_listener)) +return true; + fallthrough; +case 1 : +/* always allocate an extension. */ +if (!ctmask && !expmask) { + ctmask = ~0 ; + expmask = ~0 ; + } +break ; +default : +WARN_ON_ONCE (1 ); +return true; } -if (!ctmask && !expmask) -return false; e =nf_ct_ext_add (ct,NF_CT_EXT_ECACHE , gfp);if (e) {
@@ -320,7 +335,7 @@ bool nf_ct_ecache_ext_add(struct nf_conn *ct, u16 ctmask, u16 expmask, gfp_t gfp }EXPORT_SYMBOL_GPL (nf_ct_ecache_ext_add); -#define NF_CT_EVENTS_DEFAULT 1 +#define NF_CT_EVENTS_DEFAULT 2static int nf_ct_events__read_mostly =NF_CT_EVENTS_DEFAULT ;void nf_conntrack_ecache_pernet_init (struct net *net)
net/netfilter/nf_conntrack_standalone.c +1 -1
@@ -693,7 +693,7 @@ static struct ctl_table nf_ct_sysctl_table[] = { .mode = 0644, .proc_handler = proc_dou8vec_minmax, .extra1 = SYSCTL_ZERO, - .extra2 = SYSCTL_ONE, + .extra2 = SYSCTL_TWO, },#endif #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
Signature · 1 line
2.35.1
Florian Westphal
maintainerPATCH 4/4 netfilter: prefer extension check to pointer check
In reply to Florian WestphalThe pointer check usually results in a 'false positive': its likely that the ctnetlink module is loaded but no event monitoring is enabled.
After recent change to autodetect ctnetlink usage and only allocate the ecache extension if a listener is active, check if the extension is present on a given conntrack.
If its not there, there is nothing to report and calls to the notification framework can be elided.
--- include/net/netfilter/nf_conntrack_core.h | 2 +- include/net/netfilter/nf_conntrack_ecache.h | 31 ++++++++++----------- 2 files changed, 16 insertions(+), 17 deletions(-)
include/net/netfilter/nf_conntrack_core.h +1 -1
@@ -60,7 +60,7 @@ static inline int nf_conntrack_confirm(struct sk_buff *skb)if (ct) {if (!nf_ct_is_confirmed (ct)) ret =__nf_conntrack_confirm (skb); -if (likely (ret ==NF_ACCEPT )) +if (ret ==NF_ACCEPT &&nf_ct_ecache_exist (ct))nf_ct_deliver_cached_events (ct); }return ret;
include/net/netfilter/nf_conntrack_ecache.h +15 -16
@@ -36,6 +36,15 @@ nf_ct_ecache_find(const struct nf_conn *ct)#endif } +static inline bool nf_ct_ecache_exist (const struct nf_conn *ct) +{ +#ifdef CONFIG_NF_CONNTRACK_EVENTS +return nf_ct_ext_exist (ct,NF_CT_EXT_ECACHE ); +#else +return false; +#endif +} +#ifdef CONFIG_NF_CONNTRACK_EVENTS /* This structure is passed to event handler */
@@ -108,30 +117,20 @@ nf_conntrack_event_report(enum ip_conntrack_events event, struct nf_conn *ct, u32 portid, int report) {#ifdef CONFIG_NF_CONNTRACK_EVENTS -const struct net *net =nf_ct_net (ct); - -if (!rcu_access_pointer (net->ct.nf_conntrack_event_cb)) -return 0 ; - -return nf_conntrack_eventmask_report (1 << event, ct, portid, report); -#else -return 0 ; +if (nf_ct_ecache_exist (ct)) +return nf_conntrack_eventmask_report (1 << event, ct, portid, report);#endif +return 0 ; }static inline int nf_conntrack_event (enum ip_conntrack_events event,struct nf_conn *ct) {#ifdef CONFIG_NF_CONNTRACK_EVENTS -const struct net *net =nf_ct_net (ct); - -if (!rcu_access_pointer (net->ct.nf_conntrack_event_cb)) -return 0 ; - -return nf_conntrack_eventmask_report (1 << event, ct,0 ,0 ); -#else -return 0 ; +if (nf_ct_ecache_exist (ct)) +return nf_conntrack_eventmask_report (1 << event, ct,0 ,0 );#endif +return 0 ; }#ifdef CONFIG_NF_CONNTRACK_EVENTS
Signature · 1 line
2.35.1
Pablo Neira Ayuso
maintainerQuoted 2 lines · Florian Westphal
This patch series changes ecache infra to no longer allocate the extension by default.
Series applied, thanks