Skip to contentSkip to conversation

netfilter: nf_queue: avoid expensive gso/checksumming

PATCH Superseded Florian Westphal 2 participants 8 messages Apr 16, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: nf_queue: avoid expensive gso/checksumming

Hi Pablo,

please consider pulling from

git://git.breakpoint.cc/fw/nf-next.git nfqueue_gso_avoidance_04

to retrieve the following changes since commit aaa795ad25e18488b026572c7ba2ca8f99ced0b7:

netfilter: nat: propagate errors from xfrm_me_harder() (2013-04-08 12:34:01 +0200)

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

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 AND check the new NFQA_SKB_INFO attribute when processing a packet.

This new attribute currently contains two bits:

- 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.

I've done a few tests with old userspace and did not notice any issues.

Feedback welcome.

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

diffstat:

 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           |  169 ++++++++++++++++++++---
 4 files changed, 198 insertions(+), 130 deletions(-)
author maintainer
#1 lore raw

PATCH 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 d812c12..40a5001 100644
@@ -61,6 +61,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().
@@ -75,10 +102,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;
 
@@ -111,26 +134,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
#2 lore raw

PATCH 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
#3 lore raw

PATCH 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.

---
 include/net/netfilter/nf_queue.h     |    6 ++
 net/netfilter/nf_queue.c             |   96 ++--------------------
 net/netfilter/nfnetlink_queue_core.c |  143 ++++++++++++++++++++++++++++++----
 3 files changed, 141 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 +126 -17

160 lines not on this page · raw message

Signature · 1 line
1.7.8.6
author maintainer
#4 lore raw

PATCH 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.

---
 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 8ccaece..839c65b 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 (skb_is_gso(packet))
+		flags = NFQA_SKB_GSO;
+	if (packet->ip_summed == CHECKSUM_PARTIAL)
+		flags |= NFQA_SKB_CSUMNOTREADY;
+
+	return nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags));
+}
+
 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
#5 lore raw

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

Quoted 15 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.

---

net/netfilter/nfnetlink_queue_core.c +1 -0
+static int nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet)

net/netfilter/nfnetlink_queue_core.c nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen) · line 287

Quoted 10 lines
net/netfilter/nfnetlink_queue_core.c +9 -0
+{
+	__u32 flags = 0;
+
+	if (skb_is_gso(packet))
+		flags = NFQA_SKB_GSO;
 
+	if (packet->ip_summed == CHECKSUM_PARTIAL)
+		flags |= NFQA_SKB_CSUMNOTREADY;
+
+	return nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags));

Maybe you could avoid sending NFQA_SKB_INFO if flags == 0 ?

author maintainer
#6 lore raw

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

Quoted 10 lines · Eric Dumazet
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.

[..]

Quoted 7 lines
Quoted 6 lines
net/netfilter/nfnetlink_queue_core.c +6 -0
+static int nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet)
+{
+	__u32 flags = 0;
+
+	if (skb_is_gso(packet))
+		flags = NFQA_SKB_GSO;
Quoted 6 lines

net/netfilter/nfnetlink_queue_core.c nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen) · line 287

Quoted 4 lines
net/netfilter/nfnetlink_queue_core.c +4 -0
+	if (packet->ip_summed == CHECKSUM_PARTIAL)
+		flags |= NFQA_SKB_CSUMNOTREADY;
+
+	return nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags));

Maybe you could avoid sending NFQA_SKB_INFO if flags == 0 ?

Sure, will change it in the next round.

author maintainer
#7 lore raw

PATCH 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 839c65b..07ed239 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;
 
@@ -623,7 +624,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