Skip to contentSkip to conversation

netfilter: nf_queue: avoid expensive gso/checksums

PATCH Applied Florian Westphal 2 participants 11 messages Apr 19, 2013 – Apr 27, 2013

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

v2 cover netfilter: nf_queue: avoid expensive gso/checksums

Hello Pablo,

here is V2 of the gso avoidance patchset for nfnetlink_queue.

With these patches, userspace can now instruct the kernel that it is gso/gro aware and can handle "invalid" checksums that appear in packet headers.

For old userspace, nothing is changed: the kernel segments gso skbs and adjusts checksums.

To avoid gso/checksum fixup overhead, userspace applications must set the new NFQA_CFG_F_GSO config flag via NFQA_CFG_FLAGS attribute.

Then, for every packet received, userspace needs to check for the presence of the new NFQA_SKB_INFO attribute. If it exists, userspace needs to test NFQA_SKB_CSUMNOTREADY bit. If set, this means that userspace must NOT very packet checksums, since they will be fixed later on by the kernel.

The other bit is
NFQA_SKB_GSO, which could be used for statistics, or to determine when packet size exceeds mtu.

Feedback welcome.

Update for libnetfilter_queue (including example program/documentation) will follow later.

The following changes since commit d37d696804a83479f240b397670a07ccb53a7417:

netfilter: xt_rpfilter: depend on raw or mangle table (2013-04-19 00:22:55 +0200)

are available in the git repository at:
git://git.breakpoint.cc/fw/nf-next.git nfqueue_gso_avoidance_06

Florian Westphal (5):
netfilter: nf_queue: move device refcount bump to extra function
netfilter: nfnetlink_queue: avoid peer_portid test
netfilter: move skb_gso_segment into nfnetlink_queue module
netfilter: nfnetlink_queue: add skb info attribute
netfilter: nfqueue: avoid expensive gso segmentation and checksum fixup

Changes since V1:
- fix OOPS if CONFIG_BRIDGE_NETFILTER=y and old non-gso userspace listener is killed
- only add NFQA_SKB_INFO when skb is gso or CHECKSUM_PARTIAL

 include/net/netfilter/nf_queue.h               |    6 +
 include/uapi/linux/netfilter/nfnetlink_queue.h |   10 ++-
 net/netfilter/nf_queue.c                       |  143 +++++--------------
 net/netfilter/nfnetlink_queue_core.c           |  180 +++++++++++++++++++++---
 4 files changed, 209 insertions(+), 130 deletions(-)
author maintainer
#1 lore raw

PATCH v2 1/5 netfilter: nf_queue: move device refcount bump to extra function

required by future patch that will need to duplicate the nf_queue_entry, bumping refcounts of the copy.

---
 net/netfilter/nf_queue.c |   49 ++++++++++++++++++++++++++-------------------
 1 files changed, 28 insertions(+), 21 deletions(-)
net/netfilter/nf_queue.c +28 -21
index 5ccf01e..1d91e77 100644
@@ -66,6 +66,33 @@ static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
 	module_put(entry->elem->owner);
 }
 
+/* Bump dev refs so they don't vanish while packet is out */
+static bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
+{
+	if (!try_module_get(entry->elem->owner))
+		return false;
+
+	if (entry->indev)
+		dev_hold(entry->indev);
+	if (entry->outdev)
+		dev_hold(entry->outdev);
+#ifdef CONFIG_BRIDGE_NETFILTER
+	if (entry->skb->nf_bridge) {
+		struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
+		struct net_device *physdev;
+
+		physdev = nf_bridge->physindev;
+		if (physdev)
+			dev_hold(physdev);
+		physdev = nf_bridge->physoutdev;
+		if (physdev)
+			dev_hold(physdev);
+	}
+#endif
+
+	return true;
+}
+
 /*
  * Any packet that leaves via this function must come back
  * through nf_reinject().
@@ -80,10 +107,6 @@ static int __nf_queue(struct sk_buff *skb,
 {
 	int status = -ENOENT;
 	struct nf_queue_entry *entry = NULL;
-#ifdef CONFIG_BRIDGE_NETFILTER
-	struct net_device *physindev;
-	struct net_device *physoutdev;
-#endif
 	const struct nf_afinfo *afinfo;
 	const struct nf_queue_handler *qh;
 
@@ -116,26 +139,10 @@ static int __nf_queue(struct sk_buff *skb,
 		.okfn	= okfn,
 	};
 
-	/* If it's going away, ignore hook. */
-	if (!try_module_get(entry->elem->owner)) {
+	if (!nf_queue_entry_get_refs(entry)) {
 		status = -ECANCELED;
 		goto err_unlock;
 	}
-	/* Bump dev refs so they don't vanish while packet is out */
-	if (indev)
-		dev_hold(indev);
-	if (outdev)
-		dev_hold(outdev);
-#ifdef CONFIG_BRIDGE_NETFILTER
-	if (skb->nf_bridge) {
-		physindev = skb->nf_bridge->physindev;
-		if (physindev)
-			dev_hold(physindev);
-		physoutdev = skb->nf_bridge->physoutdev;
-		if (physoutdev)
-			dev_hold(physoutdev);
-	}
-#endif
 	skb_dst_force(skb);
 	afinfo->saveroute(skb, entry);
 	status = qh->outfn(entry, queuenum);
Signature · 1 line
1.7.8.6
author maintainer
#3 lore raw

PATCH v2 2/5 netfilter: nfnetlink_queue: avoid peer_portid test

In reply to Florian Westphal

The portid is the netlink port id of the skb that created the queue.

Add test to ensure the portid cannot be 0 at create time, and the check at enqueue time will always be false.

---
 net/netfilter/nfnetlink_queue_core.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
net/netfilter/nfnetlink_queue_core.c +3 -4
index 5e280b3..94e2e4f 100644
@@ -107,6 +107,9 @@ instance_create(struct nfnl_queue_net *q, u_int16_t queue_num,
 	unsigned int h;
 	int err;
 
+	if (portid == 0)
+		return ERR_PTR(-EINVAL);
+
 	spin_lock(&q->instances_lock);
 	if (instance_lookup(q, queue_num)) {
 		err = -EEXIST;
@@ -506,10 +509,6 @@ nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
 	}
 	spin_lock_bh(&queue->lock);
 
-	if (!queue->peer_portid) {
-		err = -EINVAL;
-		goto err_out_free_nskb;
-	}
 	if (queue->queue_total >= queue->queue_maxlen) {
 		if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
 			failopen = 1;
Signature · 1 line
1.7.8.6
author maintainer
#5 lore raw

PATCH v2 3/5 netfilter: move skb_gso_segment into nfnetlink_queue module

In reply to Florian Westphal

There is nothing wrong with the current code.

However, skb_gso_segment is expensive, so it would be nice if we could avoid it in the future.

Since userspace must be prepared to receive larger-than-mtu-packets (which will also have incorrect l3/l4 checksums), we cannot simply remove it.

The plan is to add a per-queue feature flag that userspace can set when binding the queue.

The problem is that in nf_queue, we only have a queue number, not the queue context/configuration settings.

This patch should have no impact other than the skb_gso_segment call now being in a function that has access to the queue config data.

The size new size attribute in nf_queue_entry is needed so nfnetlink_queue can duplicate the entry of the gso skb when segmenting the skb while also copying the route key.

Next step: avoid skb_gso_segment when queue config says so.

---
Change since V2:
- don't mess up entry->skb assignment when duplicating nf_queue_entry to avoid OOPS with CONFIG_BRIDGE_NETFILTER=y

 include/net/netfilter/nf_queue.h     |    6 ++
 net/netfilter/nf_queue.c             |   96 ++-------------------
 net/netfilter/nfnetlink_queue_core.c |  154 ++++++++++++++++++++++++++++++----
 3 files changed, 152 insertions(+), 104 deletions(-)
include/net/netfilter/nf_queue.h +6 -0
index fb1c0be..aaba4bb 100644
@@ -9,10 +9,13 @@ struct nf_queue_entry {
 
 	struct nf_hook_ops	*elem;
 	u_int8_t		pf;
+	u16			size; /* sizeof(entry) + saved route keys */
 	unsigned int		hook;
 	struct net_device	*indev;
 	struct net_device	*outdev;
 	int			(*okfn)(struct sk_buff *);
+
+	/* extra space to store route keys */
 };
 
 #define nf_queue_entry_reroute(x) ((void *)x + sizeof(struct nf_queue_entry))
@@ -27,4 +30,7 @@ void nf_register_queue_handler(const struct nf_queue_handler *qh);
 void nf_unregister_queue_handler(void);
 extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
 
+bool nf_queue_entry_get_refs(struct nf_queue_entry *entry);
+void nf_queue_entry_release_refs(struct nf_queue_entry *entry);
+
 #endif /* _NF_QUEUE_H */
net/netfilter/nf_queue.c +9 -87

139 lines not on this page · raw message

net/netfilter/nfnetlink_queue_core.c +137 -17

171 lines not on this page · raw message

Signature · 1 line
1.7.8.6
author maintainer
#7 lore raw

PATCH v2 4/5 netfilter: nfnetlink_queue: add skb info attribute

In reply to Florian Westphal

Once we allow userspace to receive gso/gro packets, userspace needs to be able to determine when checksums appear to be broken, but are not.

NFQA_SKB_CSUMNOTREADY means 'checksums will be fixed in kernel later, pretend they are ok'.

NFQA_SKB_GSO could be used for statistics, or to determine when packet size exceeds mtu.

---
Change since v2:
add NFQA_SKB_INFO only if value is nonzero.

 include/uapi/linux/netfilter/nfnetlink_queue.h |    7 +++++++
 net/netfilter/nfnetlink_queue_core.c           |   16 ++++++++++++++++
 2 files changed, 23 insertions(+), 0 deletions(-)
include/uapi/linux/netfilter/nfnetlink_queue.h +7 -0
index 70ec8c2..0069da3 100644
@@ -45,6 +45,7 @@ enum nfqnl_attr_type {
 	NFQA_CT,			/* nf_conntrack_netlink.h */
 	NFQA_CT_INFO,			/* enum ip_conntrack_info */
 	NFQA_CAP_LEN,			/* __u32 length of captured packet */
+	NFQA_SKB_INFO,			/* __u32 skb meta information */
 
 	__NFQA_MAX
 };
@@ -98,4 +99,10 @@ enum nfqnl_attr_config {
 #define NFQA_CFG_F_CONNTRACK			(1 << 1)
 #define NFQA_CFG_F_MAX				(1 << 2)
 
+/* flags for NFQA_SKB_INFO */
+/* packet appears to have wrong checksums, but they are ok */
+#define NFQA_SKB_CSUMNOTREADY (1 << 0)
+/* packet is GSO (i.e., exceeds device mtu) */
+#define NFQA_SKB_GSO (1 << 1)
+
 #endif /* _NFNETLINK_QUEUE_H */
net/netfilter/nfnetlink_queue_core.c +16 -0
index 38c8a7a..c927a7b 100644
@@ -275,6 +275,18 @@ nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen)
 	skb_shinfo(to)->nr_frags = j;
 }
 
+static int nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet)
+{
+	__u32 flags = 0;
+
+	if (packet->ip_summed == CHECKSUM_PARTIAL)
+		flags = NFQA_SKB_CSUMNOTREADY;
+	if (skb_is_gso(packet))
+		flags |= NFQA_SKB_GSO;
+
+	return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
+}
+
 static struct sk_buff *
 nfqnl_build_packet_message(struct nfqnl_instance *queue,
 			   struct nf_queue_entry *entry,
@@ -304,6 +316,7 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,
 #endif
 		+ nla_total_size(sizeof(u_int32_t))	/* mark */
 		+ nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
+		+ nla_total_size(sizeof(u_int32_t))	/* skbinfo */
 		+ nla_total_size(sizeof(u_int32_t));	/* cap_len */
 
 	if (entskb->tstamp.tv64)
@@ -456,6 +469,9 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,
 	if (cap_len > 0 && nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
 		goto nla_put_failure;
 
+	if (nfqnl_put_packet_info(skb, entskb))
+		goto nla_put_failure;
+
 	if (data_len) {
 		struct nlattr *nla;
 
Signature · 1 line
1.7.8.6
author maintainer
#9 lore raw

PATCH v2 5/5 netfilter: nfqueue: avoid expensive gso segmentation and checksum fixup

In reply to Florian Westphal

Userspace can now indicate that it can cope with larger-than-mtu sized packets and packets that have invalid ipv4/tcp checksums.

---
 include/uapi/linux/netfilter/nfnetlink_queue.h |    3 ++-
 net/netfilter/nfnetlink_queue_core.c           |    5 +++--
 2 files changed, 5 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nfnetlink_queue.h +2 -1
index 0069da3..a2308ae 100644
@@ -97,7 +97,8 @@ enum nfqnl_attr_config {
 /* Flags for NFQA_CFG_FLAGS */
 #define NFQA_CFG_F_FAIL_OPEN			(1 << 0)
 #define NFQA_CFG_F_CONNTRACK			(1 << 1)
-#define NFQA_CFG_F_MAX				(1 << 2)
+#define NFQA_CFG_F_GSO				(1 << 2)
+#define NFQA_CFG_F_MAX				(1 << 3)
 
 /* flags for NFQA_SKB_INFO */
 /* packet appears to have wrong checksums, but they are ok */
net/netfilter/nfnetlink_queue_core.c +3 -2
index c927a7b..f1e3c90c 100644
@@ -330,7 +330,8 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,
 		break;
 
 	case NFQNL_COPY_PACKET:
-		if (entskb->ip_summed == CHECKSUM_PARTIAL &&
+		if (!(queue->flags & NFQA_CFG_F_GSO) &&
+		    entskb->ip_summed == CHECKSUM_PARTIAL &&
 		    skb_checksum_help(entskb))
 			return NULL;
 
@@ -634,7 +635,7 @@ nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
 	if (queue->copy_mode == NFQNL_COPY_NONE)
 		return -EINVAL;
 
-	if (!skb_is_gso(entry->skb))
+	if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(entry->skb))
 		return __nfqnl_enqueue_packet(net, queue, entry);
 
 	skb = entry->skb;
Signature · 1 line
1.7.8.6
#4 lore raw

Re: [PATCH 2/5] netfilter: nfnetlink_queue: avoid peer_portid test

In reply to Florian Westphal

Hi Florian,

Quoted 15 lines · Florian Westphal

The portid is the netlink port id of the skb that created the queue.

Add test to ensure the portid cannot be 0 at create time, and the check at enqueue time will always be false.

---
 net/netfilter/nfnetlink_queue_core.c |    7 +++----
 1 files changed, 3 insertions(+), 4 deletions(-)
net/netfilter/nfnetlink_queue_core.c +0 -0
index 5e280b3..94e2e4f 100644

net/netfilter/nfnetlink_queue_core.c instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, · line 111

Quoted 5 lines
net/netfilter/nfnetlink_queue_core.c +2 -0
@@ -107,6 +107,9 @@ instance_create(struct nfnl_queue_net *q, u_int16_t queue_num,
 	unsigned int h;
 	int err;
 
+	if (portid == 0)
+		return ERR_PTR(-EINVAL);

The instance_create function takes NETLINK_CB(skb).portid. IIRC, netlink always sets that for us to non zero, so I think we would never hit that error.

net/netfilter/nfnetlink_queue_core.c nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum) · line 512

Quoted 12 lines
net/netfilter/nfnetlink_queue_core.c +1 -4
+
 	spin_lock(&q->instances_lock);
 	if (instance_lookup(q, queue_num)) {
 		err = -EEXIST;
@@ -506,10 +509,6 @@ nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
 	}
 	spin_lock_bh(&queue->lock);
 
-	if (!queue->peer_portid) {
-		err = -EINVAL;
-		goto err_out_free_nskb;
-	}

I'm trying to remember under what circunstances the queue portid can be left unset, but I don't find any. Will check again this tomorrow with fresh mind.

Quoted 10 lines
net/netfilter/nfnetlink_queue_core.c +0 -0
 	if (queue->queue_total >= queue->queue_maxlen) {
 		if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
 			failopen = 1;
Signature · 6 lines
1.7.8.6

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer Applied
#2 lore raw

Re: [PATCH 1/5] netfilter: nf_queue: move device refcount bump to extra function

In reply to Florian Westphal
Quoted 2 lines · Florian Westphal

required by future patch that will need to duplicate the nf_queue_entry, bumping refcounts of the copy.

Applied, thanks Florian.

maintainer Applied
#6 lore raw

Re: [PATCH 3/5] netfilter: move skb_gso_segment into nfnetlink_queue module

In reply to Florian Westphal
Quoted 15 lines · Florian Westphal

There is nothing wrong with the current code.

However, skb_gso_segment is expensive, so it would be nice if we could avoid it in the future.

Since userspace must be prepared to receive larger-than-mtu-packets (which will also have incorrect l3/l4 checksums), we cannot simply remove it.

The plan is to add a per-queue feature flag that userspace can set when binding the queue.

The problem is that in nf_queue, we only have a queue number, not the queue context/configuration settings.

Quoted 8 lines

This patch should have no impact other than the skb_gso_segment call now being in a function that has access to the queue config data.

The size new size attribute in nf_queue_entry is needed so nfnetlink_queue can duplicate the entry of the gso skb when segmenting the skb while also copying the route key.

Next step: avoid skb_gso_segment when queue config says so.

Applied, thanks.

maintainer Applied
#8 lore raw

Re: [PATCH 4/5] netfilter: nfnetlink_queue: add skb info attribute

In reply to Florian Westphal
Quoted 9 lines · Florian Westphal

Once we allow userspace to receive gso/gro packets, userspace needs to be able to determine when checksums appear to be broken, but are not.

NFQA_SKB_CSUMNOTREADY means 'checksums will be fixed in kernel later, pretend they are ok'.

NFQA_SKB_GSO could be used for statistics, or to determine when packet size exceeds mtu.

Applied, thanks.

#10 lore raw

Re: [PATCH 5/5] netfilter: nfqueue: avoid expensive gso segmentation and checksum fixup

In reply to Florian Westphal
Quoted 2 lines · Florian Westphal

Userspace can now indicate that it can cope with larger-than-mtu sized packets and packets that have invalid ipv4/tcp checksums.

And also applied, thanks Florian.