Skip to contentSkip to conversation

IPsec GRO

RFC PATCH Superseded Steffen Klassert 2 participants 7 messages Jan 26, 2017 – Jan 27, 2017

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Steffen Klassert

author maintainer
#0 lore raw

RFC PATCH 1/1 IPsec GRO

This introduces a device independent napi instance that handles GRO for IPsec. I was not able to use gro cells directly because I don't have a netdevice where I can hang off the napi instance. We now may have a secpath at a GRO merged skb, so we need to drop it. This is the only cange to the generic networking code.

The packet still travels two times through the stack, but might be aggregated in the second round. We can avoid the second round with implementing GRO callbacks for the IPsec protocols. This will be a separate patchset as this needs some more generic networking changes because of the asynchronous nature of IPsec.

author maintainer
#1 lore raw

RFC PATCH 1/2 net: Drop secpath on free after gro merge.

With a followup patch, a gro merged skb can have a secpath. So drop it before freeing or reusing the skb.

---
 net/core/dev.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/dev.c +2 -0
index 56818f7..c9e541e 100644
@@ -4623,6 +4623,7 @@ static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
 	case GRO_MERGED_FREE:
 		if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) {
 			skb_dst_drop(skb);
+			secpath_put(skb->sp);
 			kmem_cache_free(skbuff_head_cache, skb);
 		} else {
 			__kfree_skb(skb);
@@ -4663,6 +4664,7 @@ static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
 	skb->encapsulation = 0;
 	skb_shinfo(skb)->gso_type = 0;
 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
+	secpath_put(skb->sp);
 
 	napi->skb = skb;
 }
Signature · 1 line
1.9.1
author maintainer
#2 lore raw

Re: [PATCH RFC ipsec-next 1/2] net: Drop secpath on free after gro merge.

Quoted 21 lines · Steffen Klassert

With a followup patch, a gro merged skb can have a secpath. So drop it before freeing or reusing the skb.

---
 net/core/dev.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/dev.c +1 -0
index 56818f7..c9e541e 100644
@@ -4623,6 +4623,7 @@ static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)
 	case GRO_MERGED_FREE:
 		if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) {
 			skb_dst_drop(skb);
+			secpath_put(skb->sp);
 			kmem_cache_free(skbuff_head_cache, skb);
 		} else {
 			__kfree_skb(skb);

net/core/dev.c static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) · line 4667

Quoted 4 lines
net/core/dev.c +1 -0
@@ -4663,6 +4664,7 @@ static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
 	skb->encapsulation = 0;
 	skb_shinfo(skb)->gso_type = 0;
 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
+	secpath_put(skb->sp);

I have to use secpath_reset(skb) here of course. skb->sp is not even defined if CONFIG_XFRM is not set.

Thanks to "kbuild test robot <[email protected]>" for
pointing to that.

author maintainer
#3 lore raw

RFC PATCH 2/2 xfrm: Add a device independent napi instance

In reply to Steffen Klassert

This patch adds a napi instance for IPsec GRO.
With this, we handle IPsec GRO with no impact
on the generic networking code.

---
 net/xfrm/xfrm_input.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 86 insertions(+), 1 deletion(-)
net/xfrm/xfrm_input.c +86 -1
index 6e3f025..c5fc12d 100644
@@ -21,6 +21,90 @@
 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
 static struct xfrm_input_afinfo __rcu *xfrm_input_afinfo[NPROTO];
 
+struct xfrm_napi {
+	struct sk_buff_head	napi_skbs;
+	struct napi_struct	napi;
+};
+
+static struct xfrm_napi __percpu *napis;
+
+static void xfrm_gro_receive(struct sk_buff *skb)
+{
+	struct xfrm_napi *xnapi;
+	struct net_device *dev = skb->dev;
+
+	if (!napis || skb_cloned(skb) || !(dev->features & NETIF_F_GRO)) {
+		netif_rx(skb);
+		return;
+	}
+
+	xnapi = this_cpu_ptr(napis);
+
+	if (skb_queue_len(&xnapi->napi_skbs) > netdev_max_backlog) {
+		atomic_long_inc(&dev->rx_dropped);
+		kfree_skb(skb);
+		return;
+	}
+
+	__skb_queue_tail(&xnapi->napi_skbs, skb);
+	if (skb_queue_len(&xnapi->napi_skbs) == 1)
+		napi_schedule(&xnapi->napi);
+}
+
+static int xfrm_gro_poll(struct napi_struct *napi, int budget)
+{
+	unsigned long flags;
+	struct sk_buff *skb;
+	int work_done = 0;
+	struct xfrm_napi *xnapi = container_of(napi, struct xfrm_napi, napi);
+
+	while (work_done < budget) {
+		skb = __skb_dequeue(&xnapi->napi_skbs);
+		if (!skb)
+			break;
+		napi_gro_receive(napi, skb);
+		work_done++;
+	}
+
+	if (work_done < budget) {
+		napi_gro_flush(napi, false);
+
+		if (likely(list_empty(&napi->poll_list))) {
+			WARN_ON_ONCE(!test_and_clear_bit(NAPI_STATE_SCHED, &napi->state));
+		} else {
+			/* If n->poll_list is not empty, we need to mask irqs */
+			local_irq_save(flags);
+			__napi_complete(napi);
+			local_irq_restore(flags);
+		}
+	}
+
+	return work_done;
+}
+
+static void xfrm_gro_init(void)
+{
+	int i;
+
+	/* Napi remains disabled if we can't alloc memory. */
+	napis = alloc_percpu(struct xfrm_napi);
+
+	for_each_possible_cpu(i) {
+		struct xfrm_napi *xnapi = per_cpu_ptr(napis, i);
+
+		__skb_queue_head_init(&xnapi->napi_skbs);
+
+		set_bit(NAPI_STATE_NO_BUSY_POLL, &xnapi->napi.state);
+
+		INIT_LIST_HEAD(&xnapi->napi.poll_list);
+		xnapi->napi.poll = xfrm_gro_poll;
+		xnapi->napi.weight = 64;
+		xnapi->napi.gro_list = NULL;
+		xnapi->napi.gro_count = 0;
+		xnapi->napi.skb = NULL;
+	}
+}
+
 int xfrm_input_register_afinfo(struct xfrm_input_afinfo *afinfo)
 {
 	int err = 0;
@@ -371,7 +455,7 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
 
 	if (decaps) {
 		skb_dst_drop(skb);
-		netif_rx(skb);
+		xfrm_gro_receive(skb);
 		return 0;
 	} else {
 		return x->inner_mode->afinfo->transport_finish(skb, async);
@@ -394,6 +478,7 @@ int xfrm_input_resume(struct sk_buff *skb, int nexthdr)
 
 void __init xfrm_input_init(void)
 {
+	xfrm_gro_init();
 	secpath_cachep = kmem_cache_create("secpath_cache",
 					   sizeof(struct sec_path),
 					   0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH RFC ipsec-next 2/2] xfrm: Add a device independent napi instance

Quoted 64 lines · Steffen Klassert

This patch adds a napi instance for IPsec GRO.
With this, we handle IPsec GRO with no impact
on the generic networking code.

---
 net/xfrm/xfrm_input.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 86 insertions(+), 1 deletion(-)
net/xfrm/xfrm_input.c +47 -0
index 6e3f025..c5fc12d 100644
@@ -21,6 +21,90 @@
 static DEFINE_SPINLOCK(xfrm_input_afinfo_lock);
 static struct xfrm_input_afinfo __rcu *xfrm_input_afinfo[NPROTO];
 
+struct xfrm_napi {
+	struct sk_buff_head	napi_skbs;
+	struct napi_struct	napi;
+};
+
+static struct xfrm_napi __percpu *napis;
+
+static void xfrm_gro_receive(struct sk_buff *skb)
+{
+	struct xfrm_napi *xnapi;
+	struct net_device *dev = skb->dev;
+
+	if (!napis || skb_cloned(skb) || !(dev->features & NETIF_F_GRO)) {
+		netif_rx(skb);
+		return;
+	}
+
+	xnapi = this_cpu_ptr(napis);
+
+	if (skb_queue_len(&xnapi->napi_skbs) > netdev_max_backlog) {
+		atomic_long_inc(&dev->rx_dropped);
+		kfree_skb(skb);
+		return;
+	}
+
+	__skb_queue_tail(&xnapi->napi_skbs, skb);
+	if (skb_queue_len(&xnapi->napi_skbs) == 1)
+		napi_schedule(&xnapi->napi);
+}
+
+static int xfrm_gro_poll(struct napi_struct *napi, int budget)
+{
+	unsigned long flags;
+	struct sk_buff *skb;
+	int work_done = 0;
+	struct xfrm_napi *xnapi = container_of(napi, struct xfrm_napi, napi);
+
+	while (work_done < budget) {
+		skb = __skb_dequeue(&xnapi->napi_skbs);
+		if (!skb)
+			break;
+		napi_gro_receive(napi, skb);
+		work_done++;
+	}
+
+	if (work_done < budget) {
+		napi_gro_flush(napi, false);

net/xfrm/xfrm_input.c line 80

Quoted 10 lines
net/xfrm/xfrm_input.c +10 -0
+
+		if (likely(list_empty(&napi->poll_list))) {
+			WARN_ON_ONCE(!test_and_clear_bit(NAPI_STATE_SCHED, &napi->state));
+		} else {
+			/* If n->poll_list is not empty, we need to mask irqs */
+			local_irq_save(flags);
+			__napi_complete(napi);
+			local_irq_restore(flags);
+		}
+	}

The following should work and contain less copy/pasted code.

if (work_done < budget)
napi_complete(napi);

net/xfrm/xfrm_input.c line 90

Quoted 10 lines
net/xfrm/xfrm_input.c +10 -0
+
+	return work_done;
+}
+
+static void xfrm_gro_init(void)
+{
+	int i;
+
+	/* Napi remains disabled if we can't alloc memory. */
+	napis = alloc_percpu(struct xfrm_napi);

napis can be NULL here.

Quoted 6 lines
net/xfrm/xfrm_input.c +6 -0
+
+	for_each_possible_cpu(i) {
+		struct xfrm_napi *xnapi = per_cpu_ptr(napis, i);
+
+		__skb_queue_head_init(&xnapi->napi_skbs);
+

net/xfrm/xfrm_input.c line 106

Quoted 10 lines
net/xfrm/xfrm_input.c +10 -0
+		set_bit(NAPI_STATE_NO_BUSY_POLL, &xnapi->napi.state);
+
+		INIT_LIST_HEAD(&xnapi->napi.poll_list);
+		xnapi->napi.poll = xfrm_gro_poll;
+		xnapi->napi.weight = 64;
+		xnapi->napi.gro_list = NULL;
+		xnapi->napi.gro_count = 0;
+		xnapi->napi.skb = NULL;
+	}
+}

Alternative would be to use a

static struct net_device xfrm_napi_anchor_device;

and use gro_cell

I will present in next netdev conference a work based on NAPI, and having less hand coded napi logic will help a lot.

Thanks.

#5 lore raw

Re: [PATCH RFC ipsec-next 2/2] xfrm: Add a device independent napi instance

Quoted 6 lines · Eric Dumazet

Alternative would be to use a

static struct net_device xfrm_napi_anchor_device;

and use gro_cell

Also take a look at init_dummy_netdev()

Thanks !

author maintainer
#6 lore raw

Re: [PATCH RFC ipsec-next 2/2] xfrm: Add a device independent napi instance

Quoted 10 lines · Eric Dumazet
Quoted 6 lines · Eric Dumazet

Alternative would be to use a

static struct net_device xfrm_napi_anchor_device;

and use gro_cell

Also take a look at init_dummy_netdev()

I already thought about using some dummy net_device for this, but was not sure what I need to initialize. So it seemed to be safer to use a private napi instance.

init_dummy_netdev() is exactly what I need for that, thanks a lot!