Netfilter-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH nf-next] netfilter: add and use nf_hook_slow_list()
@ 2019-10-09 14:30 Florian Westphal
  2019-10-10 17:21 ` Edward Cree
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Westphal @ 2019-10-09 14:30 UTC (permalink / raw)
  To: netfilter-devel; +Cc: Florian Westphal, Edward Cree

At this time, NF_HOOK_LIST() macro will iterate the list and then call
nf_hook() for each skb.

This makes it so the entire list is passed into the netfilter core.
The advantage is that we only need to fetch the rule blob once per list
instead of per-skb.  If no rules are present, the list operations
can be elided entirely.

NF_HOOK_LIST only supports ipv4 and ipv6, but those are the only
callers.

Cc: Edward Cree <ecree@solarflare.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
---
 include/linux/netfilter.h | 41 +++++++++++++++++++++++++++++----------
 net/netfilter/core.c      | 20 +++++++++++++++++++
 2 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/include/linux/netfilter.h b/include/linux/netfilter.h
index 77ebb61faf48..eb312e7ca36e 100644
--- a/include/linux/netfilter.h
+++ b/include/linux/netfilter.h
@@ -199,6 +199,8 @@ extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
 int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
 		 const struct nf_hook_entries *e, unsigned int i);
 
+void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
+		       const struct nf_hook_entries *e);
 /**
  *	nf_hook - call a netfilter hook
  *
@@ -311,17 +313,36 @@ NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
 	     struct list_head *head, struct net_device *in, struct net_device *out,
 	     int (*okfn)(struct net *, struct sock *, struct sk_buff *))
 {
-	struct sk_buff *skb, *next;
-	struct list_head sublist;
-
-	INIT_LIST_HEAD(&sublist);
-	list_for_each_entry_safe(skb, next, head, list) {
-		list_del(&skb->list);
-		if (nf_hook(pf, hook, net, sk, skb, in, out, okfn) == 1)
-			list_add_tail(&skb->list, &sublist);
+	struct nf_hook_entries *hook_head = NULL;
+
+#ifdef CONFIG_JUMP_LABEL
+	if (__builtin_constant_p(pf) &&
+	    __builtin_constant_p(hook) &&
+	    !static_key_false(&nf_hooks_needed[pf][hook]))
+		return;
+#endif
+
+	rcu_read_lock();
+	switch (pf) {
+	case NFPROTO_IPV4:
+		hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
+		break;
+	case NFPROTO_IPV6:
+		hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
+		break;
+	default:
+		WARN_ON_ONCE(1);
+		break;
 	}
-	/* Put passed packets back on main list */
-	list_splice(&sublist, head);
+
+	if (hook_head) {
+		struct nf_hook_state state;
+
+		nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
+
+		nf_hook_slow_list(head, &state, hook_head);
+	}
+	rcu_read_unlock();
 }
 
 /* Call setsockopt() */
diff --git a/net/netfilter/core.c b/net/netfilter/core.c
index 5d5bdf450091..306587b07a4f 100644
--- a/net/netfilter/core.c
+++ b/net/netfilter/core.c
@@ -536,6 +536,26 @@ int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
 }
 EXPORT_SYMBOL(nf_hook_slow);
 
+void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
+		       const struct nf_hook_entries *e)
+{
+	struct sk_buff *skb, *next;
+	struct list_head sublist;
+	int ret;
+
+	INIT_LIST_HEAD(&sublist);
+
+	list_for_each_entry_safe(skb, next, head, list) {
+		list_del(&skb->list);
+		ret = nf_hook_slow(skb, state, e, 0);
+		if (ret == 1)
+			list_add_tail(&skb->list, &sublist);
+	}
+	/* Put passed packets back on main list */
+	list_splice(&sublist, head);
+}
+EXPORT_SYMBOL(nf_hook_slow_list);
+
 /* This needs to be compiled in any case to avoid dependencies between the
  * nfnetlink_queue code and nf_conntrack.
  */
-- 
2.21.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH nf-next] netfilter: add and use nf_hook_slow_list()
  2019-10-09 14:30 [PATCH nf-next] netfilter: add and use nf_hook_slow_list() Florian Westphal
@ 2019-10-10 17:21 ` Edward Cree
  2019-10-10 19:41   ` Florian Westphal
  0 siblings, 1 reply; 3+ messages in thread
From: Edward Cree @ 2019-10-10 17:21 UTC (permalink / raw)
  To: Florian Westphal, netfilter-devel

On 09/10/2019 15:30, Florian Westphal wrote:
> At this time, NF_HOOK_LIST() macro will iterate the list and then call
> nf_hook() for each skb.
>
> This makes it so the entire list is passed into the netfilter core.
> The advantage is that we only need to fetch the rule blob once per list
> instead of per-skb.  If no rules are present, the list operations
> can be elided entirely.
>
> NF_HOOK_LIST only supports ipv4 and ipv6, but those are the only
> callers.
>
> Cc: Edward Cree <ecree@solarflare.com>
> Signed-off-by: Florian Westphal <fw@strlen.de>
> ---
LGTM (but see below).
Acked-by: Edward Cree <ecree@solarflare.com>
>  include/linux/netfilter.h | 41 +++++++++++++++++++++++++++++----------
>  net/netfilter/core.c      | 20 +++++++++++++++++++
>  2 files changed, 51 insertions(+), 10 deletions(-)
>
> diff --git a/include/linux/netfilter.h b/include/linux/netfilter.h
> index 77ebb61faf48..eb312e7ca36e 100644
> --- a/include/linux/netfilter.h
> +++ b/include/linux/netfilter.h
> @@ -199,6 +199,8 @@ extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
>  int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
>                const struct nf_hook_entries *e, unsigned int i);
>
> +void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
> +                    const struct nf_hook_entries *e);
>  /**
>   *   nf_hook - call a netfilter hook
>   *
> @@ -311,17 +313,36 @@ NF_HOOK_LIST(uint8_t pf, unsigned int hook, struct net *net, struct sock *sk,
>            struct list_head *head, struct net_device *in, struct net_device *out,
>            int (*okfn)(struct net *, struct sock *, struct sk_buff *))
>  {
> -     struct sk_buff *skb, *next;
> -     struct list_head sublist;
> -
> -     INIT_LIST_HEAD(&sublist);
> -     list_for_each_entry_safe(skb, next, head, list) {
> -             list_del(&skb->list);
> -             if (nf_hook(pf, hook, net, sk, skb, in, out, okfn) == 1)
> -                     list_add_tail(&skb->list, &sublist);
> +     struct nf_hook_entries *hook_head = NULL;
> +
> +#ifdef CONFIG_JUMP_LABEL
> +     if (__builtin_constant_p(pf) &&
> +         __builtin_constant_p(hook) &&
> +         !static_key_false(&nf_hooks_needed[pf][hook]))
> +             return;
> +#endif
> +
> +     rcu_read_lock();
> +     switch (pf) {
> +     case NFPROTO_IPV4:
> +             hook_head = rcu_dereference(net->nf.hooks_ipv4[hook]);
> +             break;
> +     case NFPROTO_IPV6:
> +             hook_head = rcu_dereference(net->nf.hooks_ipv6[hook]);
> +             break;
> +     default:
> +             WARN_ON_ONCE(1);
> +             break;
>       }
> -     /* Put passed packets back on main list */
> -     list_splice(&sublist, head);
> +
> +     if (hook_head) {
> +             struct nf_hook_state state;
> +
> +             nf_hook_state_init(&state, hook, pf, in, out, sk, net, okfn);
> +
> +             nf_hook_slow_list(head, &state, hook_head);
> +     }
> +     rcu_read_unlock();
>  }
>
>  /* Call setsockopt() */
> diff --git a/net/netfilter/core.c b/net/netfilter/core.c
> index 5d5bdf450091..306587b07a4f 100644
> --- a/net/netfilter/core.c
> +++ b/net/netfilter/core.c
> @@ -536,6 +536,26 @@ int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state,
>  }
>  EXPORT_SYMBOL(nf_hook_slow);
>
> +void nf_hook_slow_list(struct list_head *head, struct nf_hook_state *state,
> +                    const struct nf_hook_entries *e)
> +{
> +     struct sk_buff *skb, *next;
> +     struct list_head sublist;
> +     int ret;
> +
> +     INIT_LIST_HEAD(&sublist);
> +
> +     list_for_each_entry_safe(skb, next, head, list) {
> +             list_del(&skb->list);
I know this was just copied from the existing code, but I've been getting
 a lot more paranoid lately about skbs escaping with non-NULL ->next
 pointers, since several bugs of that kind have turned up elsewhere.
So should this maybe be skb_list_del_init()?

-Ed
> +             ret = nf_hook_slow(skb, state, e, 0);
> +             if (ret == 1)
> +                     list_add_tail(&skb->list, &sublist);
> +     }
> +     /* Put passed packets back on main list */
> +     list_splice(&sublist, head);
> +}
> +EXPORT_SYMBOL(nf_hook_slow_list);
> +
>  /* This needs to be compiled in any case to avoid dependencies between the
>   * nfnetlink_queue code and nf_conntrack.
>   */

The information contained in this message is confidential and is intended for the addressee(s) only. If you have received this message in error, please notify the sender immediately and delete the message. Unless you are an addressee (or authorized to receive for an addressee), you may not use, copy or disclose to anyone this message or any information contained in this message. The unauthorized use, disclosure, copying or alteration of this message is strictly prohibited.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH nf-next] netfilter: add and use nf_hook_slow_list()
  2019-10-10 17:21 ` Edward Cree
@ 2019-10-10 19:41   ` Florian Westphal
  0 siblings, 0 replies; 3+ messages in thread
From: Florian Westphal @ 2019-10-10 19:41 UTC (permalink / raw)
  To: Edward Cree; +Cc: Florian Westphal, netfilter-devel

Edward Cree <ecree@solarflare.com> wrote:
> On 09/10/2019 15:30, Florian Westphal wrote:
> > At this time, NF_HOOK_LIST() macro will iterate the list and then call
> > nf_hook() for each skb.
> >
> > This makes it so the entire list is passed into the netfilter core.
> > The advantage is that we only need to fetch the rule blob once per list
> > instead of per-skb.  If no rules are present, the list operations
> > can be elided entirely.
> >
> > NF_HOOK_LIST only supports ipv4 and ipv6, but those are the only
> > callers.
> >
> > Cc: Edward Cree <ecree@solarflare.com>
> > Signed-off-by: Florian Westphal <fw@strlen.de>
> > ---
> LGTM (but see below).
> Acked-by: Edward Cree <ecree@solarflare.com>

[..]

> > +     list_for_each_entry_safe(skb, next, head, list) {
> > +             list_del(&skb->list);
> I know this was just copied from the existing code, but I've been getting
> a lot more paranoid lately about skbs escaping with non-NULL ->next
> pointers, since several bugs of that kind have turned up elsewhere.
> So should this maybe be skb_list_del_init()?

Ok, I can make that change and send a v2.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-09 14:30 [PATCH nf-next] netfilter: add and use nf_hook_slow_list() Florian Westphal
2019-10-10 17:21 ` Edward Cree
2019-10-10 19:41   ` Florian Westphal

Netfilter-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/netfilter-devel/0 netfilter-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 netfilter-devel netfilter-devel/ https://lore.kernel.org/netfilter-devel \
		netfilter-devel@vger.kernel.org
	public-inbox-index netfilter-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.netfilter-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git