IPsec performance optimizations
Subsystems (4)
Steffen Klassert
maintainerRFC PATCH 1/1 IPsec performance optimizations
This patchset adds several performance optimizations for the ESP IPsec protocol. This RFC version is intended to be a discussion base for the IPsec workshop at the netdev 1.2 conference.
The patchset has two parts, patches 1 - 4 are software optimizations. These patches are complete and could go upstream after some review.
Patch 5 - 11 are needed to create an API for ESP offload to network devices. Mellanox prepares the mlx5 driver for the use of the created API, see
https://git.kernel.org/cgit/linux/kernel/git/klassert/linux-stk.git/?h=net-next-ipsec-offload-api3
This part is still under development, changes are very likely before it can go upstream.
Patch 1 and 2 try to avoid the linearization of ESP packets whenever possible.
Patch 3 prepares the generic networking codepath for IPsec GRO.
Patch 4 implements software GRO a codepath for ESP on ipv4 and ipv6.
Patch 5 extends the skbuff gso_type to unsigned int. We need a GSO flag for ESP, but all available gso_type flags are currently in use.
Patch 6 adds the needed netdev features to configure IPsec offloads.
Patch 7 adds gso handlers for esp4 and esp6, currently only used in combination with ESP hardware offload.
Patch 8 - 9 prepares for IPsec hardware offloading.
Patch 10 implements an IPsec hardware offloading API.
Patch 11 allows for TSO and checksum offloading of the inner IPsec packet.
Steffen Klassert
maintainerRFC PATCH 1/11 esp4: Avoid skb_cow_data whenever possible
This patch tries to avoid skb_cow_data on esp4.
On the encrypt side we add the IPsec tailbits to the lineaer part of the buffer if there is space on on it. If there is no space on the linaer part, we add a page fragment with the tailbits to the buffer and use separate src and dst scatterlists.
On the decrypt side, we leave the buffer as it is if it is not cloned.
With this, we can avoid a linearization of the buffer in most of the cases.
Joint work with:
Sowmini Varadhan <[email protected]>
Ilan Tayari <[email protected]>
--- include/net/xfrm.h | 2 + net/ipv4/esp4.c | 338 +++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 266 insertions(+), 74 deletions(-)
include/net/xfrm.h +2 -0
@@ -213,6 +213,8 @@ struct xfrm_state {/* Last used time */ unsigned long lastused; +struct page_frag xfrag; +/* Reference to data common to all the instances of this * transformer. */ const struct xfrm_type *type;
net/ipv4/esp4.c +264 -74
451 lines not on this page · raw message
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 2/11 esp6: Avoid skb_cow_data whenever possible
In reply to Steffen KlassertThis patch tries to avoid skb_cow_data on esp6.
On the encrypt side we add the IPsec tailbits to the lineaer part of the buffer if there is space on on it. If there is no space on the linear part, we add a page fragment with the tailbits to the buffer and use separate src and dst scatterlists.
On the decrypt side, we leave the buffer as it is if it is not cloned.
With this, we can avoid a linearization of the buffer in most of the cases.
Joint work with:
Sowmini Varadhan <[email protected]>
Ilan Tayari <[email protected]>
--- net/ipv6/esp6.c | 302 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 246 insertions(+), 56 deletions(-)
net/ipv6/esp6.c +246 -56
@@ -44,6 +44,8 @@#include <net/protocol.h> #include <linux/icmpv6.h> +#include <linux/highmem.h> +struct esp_skb_cb {struct xfrm_skb_cb xfrm;void *tmp;
@@ -114,11 +116,40 @@ static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead, __alignof__(struct scatterlist)); } +static void esp_ssg_unref (struct xfrm_state *x,void *tmp) +{ +__be32 *seqhi; +struct crypto_aead *aead = x->data; +int seqhilen =0 ; +u8 *iv; +struct aead_request *req; +struct scatterlist *sg; + +if (x->props.flags &XFRM_STATE_ESN ) + seqhilen +=sizeof (__be32); + + seqhi =esp_tmp_seqhi (tmp); + iv =esp_tmp_iv (aead, tmp, seqhilen); + req =esp_tmp_req (aead, iv); + +/* Unref skb_frag_pages in the src scatterlist if necessary. +* Skip the first sg which comes from skb->data. +*/ +if (req->src != req->dst) +for (sg =sg_next (req->src); sg; sg =sg_next (sg)) +put_page (sg_page (sg)); +} +static void esp_output_done (struct crypto_async_request *base,int err) {struct sk_buff *skb = base->data; +void *tmp; +struct dst_entry *dst =skb_dst (skb); +struct xfrm_state *x = dst->xfrm; -kfree (ESP_SKB_CB (skb)->tmp); + tmp =ESP_SKB_CB (skb)->tmp; +esp_ssg_unref (x, tmp); +kfree (tmp);xfrm_output_resume (skb, err); }
@@ -138,6 +169,27 @@ static void esp_output_restore_header(struct sk_buff *skb)esp_restore_header (skb,skb_transport_offset (skb) -sizeof (__be32)); } +static struct ip_esp_hdr *esp_output_set_esn (struct sk_buff *skb, +struct ip_esp_hdr *esph, +__be32 *seqhi) +{ +struct xfrm_state *x =skb_dst (skb)->xfrm; + +/* For ESN we move the header forward by 4 bytes to +* accomodate the high bits. We will move it back after +* encryption. +*/ +if ((x->props.flags &XFRM_STATE_ESN )) { + esph = (void *)(skb_transport_header (skb) -sizeof (__be32)); + *seqhi = esph->spi; + esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.hi); + } + + esph->spi = x->id.spi; + +return esph; +} +static void esp_output_done_esn (struct crypto_async_request *base,int err) {struct sk_buff *skb = base->data;
@@ -152,8 +204,9 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)struct ip_esp_hdr *esph;struct crypto_aead *aead;struct aead_request *req; -struct scatterlist *sg; +struct scatterlist *sg, *dsg;struct sk_buff *trailer; +struct page *page;void *tmp;int blksize;int clen;
@@ -164,10 +217,13 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)int nfrags;int assoclen;int seqhilen; +int tailen;u8 *iv;u8 *tail; +u8 *vaddr;__be32 *seqhi;__be64 seqno; +__u8 proto = *skb_mac_header (skb);/* skb is pure payload to encrypt */ aead = x->data;
@@ -186,11 +242,7 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb) blksize =ALIGN (crypto_aead_blocksize (aead),4 ); clen =ALIGN (skb->len +2 + tfclen, blksize); plen = clen - skb->len - tfclen; - - err =skb_cow_data (skb, tfclen + plen + alen, &trailer); -if (err <0 ) - goto error; - nfrags = err; + tailen = tfclen + plen + alen; assoclen =sizeof (*esph); seqhilen =0 ;
@@ -200,19 +252,130 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb) assoclen += seqhilen; } - tmp =esp_alloc_tmp (aead, nfrags, seqhilen); -if (!tmp) { - err = -ENOMEM ; - goto error; + *skb_mac_header (skb) =IPPROTO_ESP ; + esph =ip_esp_hdr (skb); + +if (!skb_cloned (skb)) { +if (tailen <=skb_availroom (skb)) { + nfrags =1 ; + trailer = skb; + tail =skb_tail_pointer (trailer); + + goto skip_cow; + }else if ((skb_shinfo (skb)->nr_frags <MAX_SKB_FRAGS ) + && !skb_has_frag_list (skb)) { +int allocsize; +struct sock *sk = skb->sk; +struct page_frag *pfrag = &x->xfrag; + + allocsize =ALIGN (tailen,L1_CACHE_BYTES ); + +spin_lock_bh (&x->lock); + +if (unlikely (!skb_page_frag_refill (allocsize, pfrag,GFP_ATOMIC ))) { +spin_unlock_bh (&x->lock); + goto cow; + } + + page = pfrag->page; +get_page (page); + + vaddr =kmap_atomic (page); + + tail = vaddr + pfrag->offset; + +/* Fill padding... */ +if (tfclen) { +memset (tail,0 , tfclen); + tail += tfclen; + } +do { +int i; +for (i =0 ; i < plen -2 ; i++) + tail[i] = i +1 ; + }while (0 ); + tail[plen -2 ] = plen -2 ; + tail[plen -1 ] = proto; + +kunmap_atomic (vaddr); + + nfrags =skb_shinfo (skb)->nr_frags; + +__skb_fill_page_desc (skb, nfrags, page, pfrag->offset, + tailen); +skb_shinfo (skb)->nr_frags = ++nfrags; + + pfrag->offset = pfrag->offset + allocsize; + nfrags++; + + skb->len += tailen; + skb->data_len += tailen; + skb->truesize += tailen; +if (sk) +atomic_add (tailen, &sk->sk_wmem_alloc); + +skb_push (skb, -skb_network_offset (skb)); + + esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); + esph->spi = x->id.spi; + + tmp =esp_alloc_tmp (aead, nfrags +2 , seqhilen); +if (!tmp) { +spin_unlock_bh (&x->lock); + err = -ENOMEM ; + goto error; + } + seqhi =esp_tmp_seqhi (tmp); + iv =esp_tmp_iv (aead, tmp, seqhilen); + req =esp_tmp_req (aead, iv); + sg =esp_req_sg (aead, req); + dsg = &sg[nfrags]; + + esph =esp_output_set_esn (skb, esph, seqhi); + +sg_init_table (sg, nfrags); +skb_to_sgvec (skb, sg, + (unsigned char *)esph - skb->data, + assoclen + ivlen + clen + alen); + + allocsize =ALIGN (skb->data_len,L1_CACHE_BYTES ); + +if (unlikely (!skb_page_frag_refill (allocsize, pfrag,GFP_ATOMIC ))) { +spin_unlock_bh (&x->lock); + err = -ENOMEM ; + goto error; + } + +skb_shinfo (skb)->nr_frags =1 ; + + page = pfrag->page; +get_page (page); +/* replace page frags in skb with new page */ +__skb_fill_page_desc (skb,0 , page, pfrag->offset, skb->data_len); + pfrag->offset = pfrag->offset + allocsize; + +sg_init_table (dsg,skb_shinfo (skb)->nr_frags +1 ); +skb_to_sgvec (skb, dsg, + (unsigned char *)esph - skb->data, + assoclen + ivlen + clen + alen); + +spin_unlock_bh (&x->lock); + + goto skip_cow2; + } } - seqhi =esp_tmp_seqhi (tmp); - iv =esp_tmp_iv (aead, tmp, seqhilen); - req =esp_tmp_req (aead, iv); - sg =esp_req_sg (aead, req); +cow: + err =skb_cow_data (skb, tailen, &trailer); +if (err <0 ) + goto error; + nfrags = err; -/* Fill padding... */ tail =skb_tail_pointer (trailer); + esph =ip_esp_hdr (skb); + +skip_cow: +/* Fill padding... */ if (tfclen) {memset (tail,0 , tfclen); tail += tfclen;
@@ -223,36 +386,40 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb) tail[i] = i +1 ; }while (0 ); tail[plen -2 ] = plen -2 ; - tail[plen -1 ] = *skb_mac_header (skb); + tail[plen -1 ] = proto;pskb_put (skb, trailer, clen - skb->len + alen);skb_push (skb, -skb_network_offset (skb)); - esph =ip_esp_hdr (skb); - *skb_mac_header (skb) =IPPROTO_ESP ; esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); + esph->spi = x->id.spi; -aead_request_set_callback (req,0 , esp_output_done, skb); - -/* For ESN we move the header forward by 4 bytes to -* accomodate the high bits. We will move it back after -* encryption. -*/ -if ((x->props.flags &XFRM_STATE_ESN )) { - esph = (void *)(skb_transport_header (skb) -sizeof (__be32)); - *seqhi = esph->spi; - esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.hi); -aead_request_set_callback (req,0 , esp_output_done_esn, skb); + tmp =esp_alloc_tmp (aead, nfrags, seqhilen); +if (!tmp) { + err = -ENOMEM ; + goto error; } - esph->spi = x->id.spi; + seqhi =esp_tmp_seqhi (tmp); + iv =esp_tmp_iv (aead, tmp, seqhilen); + req =esp_tmp_req (aead, iv); + sg =esp_req_sg (aead, req); + dsg = sg; + + esph =esp_output_set_esn (skb, esph, seqhi);sg_init_table (sg, nfrags);skb_to_sgvec (skb, sg, (unsigned char *)esph - skb->data, assoclen + ivlen + clen + alen); -aead_request_set_crypt (req, sg, sg, ivlen + clen, iv); +skip_cow2: +if ((x->props.flags &XFRM_STATE_ESN )) +aead_request_set_callback (req,0 , esp_output_done_esn, skb); +else +aead_request_set_callback (req,0 , esp_output_done, skb); + +aead_request_set_crypt (req, sg, dsg, ivlen + clen, iv);aead_request_set_ad (req, assoclen); seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
@@ -278,6 +445,8 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)esp_output_restore_header (skb); } +if (sg != dsg) +esp_ssg_unref (x, tmp);kfree (tmp); error:
@@ -343,6 +512,23 @@ static void esp_input_restore_header(struct sk_buff *skb)__skb_pull (skb,4 ); } +static void esp_input_set_header (struct sk_buff *skb,__be32 *seqhi) +{ +struct xfrm_state *x =xfrm_input_state (skb); +struct ip_esp_hdr *esph = (struct ip_esp_hdr *)skb->data; + +/* For ESN we move the header forward by 4 bytes to +* accomodate the high bits. We will move it back after +* decryption. +*/ +if ((x->props.flags &XFRM_STATE_ESN )) { + esph = (void *)skb_push (skb,4 ); + *seqhi = esph->spi; + esph->spi = esph->seq_no; + esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.input.hi); + } +} +static void esp_input_done_esn (struct crypto_async_request *base,int err) {struct sk_buff *skb = base->data;
@@ -378,14 +564,6 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb) goto out; } - nfrags =skb_cow_data (skb,0 , &trailer); -if (nfrags <0 ) { - ret = -EINVAL ; - goto out; - } - - ret = -ENOMEM ; - assoclen =sizeof (*esph); seqhilen =0 ;
@@ -394,6 +572,27 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb) assoclen += seqhilen; } +if (!skb_cloned (skb)) { +if (!skb_is_nonlinear (skb)) { + nfrags =1 ; + + goto skip_cow; + }else if (!skb_has_frag_list (skb)) { + nfrags =skb_shinfo (skb)->nr_frags; + nfrags++; + + goto skip_cow; + } + } + + nfrags =skb_cow_data (skb,0 , &trailer); +if (nfrags <0 ) { + ret = -EINVAL ; + goto out; + } + +skip_cow: + ret = -ENOMEM ; tmp =esp_alloc_tmp (aead, nfrags, seqhilen);if (!tmp) goto out;
@@ -404,26 +603,17 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb) req =esp_tmp_req (aead, iv); sg =esp_req_sg (aead, req); - skb->ip_summed =CHECKSUM_NONE ; +esp_input_set_header (skb, seqhi); - esph = (struct ip_esp_hdr *)skb->data; +sg_init_table (sg, nfrags); +skb_to_sgvec (skb, sg,0 , skb->len); -aead_request_set_callback (req,0 , esp_input_done, skb); + skb->ip_summed =CHECKSUM_NONE ; -/* For ESN we move the header forward by 4 bytes to -* accomodate the high bits. We will move it back after -* decryption. -*/ -if ((x->props.flags &XFRM_STATE_ESN )) { - esph = (void *)skb_push (skb,4 ); - *seqhi = esph->spi; - esph->spi = esph->seq_no; - esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.input.hi); +if ((x->props.flags &XFRM_STATE_ESN ))aead_request_set_callback (req,0 , esp_input_done_esn, skb); - } - -sg_init_table (sg, nfrags); -skb_to_sgvec (skb, sg,0 , skb->len); +else +aead_request_set_callback (req,0 , esp_input_done, skb);aead_request_set_crypt (req, sg, sg, elen + ivlen, iv);aead_request_set_ad (req, assoclen);
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 3/11 net: Prepare for IPsec GRO
In reply to Steffen KlassertThis patch prepares the generic codepath for IPsec GRO. We introduce a new GRO_CONSUMED notifier to reflect that IPsec can return asynchronous. On IPsec GRO we grab the packet and reinject it back to layer 2 after IPsec processing. We also use one xfrm_gro bit on the sk_buff that will be set from IPsec to notify about GRO. If this bit is set, we call napi_gro_receive for the backlog device instead of __netif_receive_skb.
--- include/linux/netdevice.h | 1 + include/linux/skbuff.h | 14 +++++++++++++- net/core/dev.c | 17 ++++++++++++++++- net/xfrm/Kconfig | 4 ++++ 4 files changed, 34 insertions(+), 2 deletions(-)
include/linux/netdevice.h +1 -0
@@ -342,6 +342,7 @@ enum gro_result {GRO_HELD ,GRO_NORMAL ,GRO_DROP , + GRO_CONSUMED, };typedef enum gro_result gro_result_t ;
include/linux/skbuff.h +13 -1
@@ -732,7 +732,10 @@ struct sk_buff {#ifdef CONFIG_NET_SWITCHDEV __u8 offload_fwd_mark:1;#endif -/* 2, 4 or 5 bit hole */ +#ifdef CONFIG_XFRM_OFFLOAD + __u8 xfrm_gro:1; +#endif +/* 1 to 5 bits hole */ #ifdef CONFIG_NET_SCHED __u16 tc_index;/* traffic control index */
@@ -3679,6 +3682,15 @@ static inline struct sec_path *skb_sec_path(struct sk_buff *skb)#endif } +static inline bool skb_xfrm_gro (struct sk_buff *skb) +{ +#ifdef CONFIG_XFRM_OFFLOAD +return skb->xfrm_gro; +#else +return false; +#endif +} +/* Keeps track of mac header offset relative to skb->head. * It is useful for TSO of Tunneling protocol. e.g. GRE. * For non-tunnel skb it points to skb_mac_header() and for
net/core/dev.c +16 -1
@@ -4531,6 +4531,11 @@ static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff }rcu_read_unlock (); +if (PTR_ERR (pp) == -EINPROGRESS ) { + ret =GRO_CONSUMED ; + goto ok; + } +if (&ptype->list == head) goto normal;
@@ -4629,6 +4634,9 @@ 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); +#ifdef CONFIG_XFRM_OFFLOAD +secpath_put (skb->sp); +#endif kmem_cache_free (skbuff_head_cache, skb); }else {__kfree_skb (skb);
@@ -4637,6 +4645,7 @@ static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb)case GRO_HELD :case GRO_MERGED : +case GRO_CONSUMED :break ; }
@@ -4707,6 +4716,7 @@ static gro_result_t napi_frags_finish(struct napi_struct *napi,break ;case GRO_MERGED : +case GRO_CONSUMED :break ; }
@@ -4849,7 +4859,12 @@ static int process_backlog(struct napi_struct *napi, int quota)while ((skb =__skb_dequeue (&sd->process_queue))) {rcu_read_lock (); -__netif_receive_skb (skb); + +if (skb_xfrm_gro (skb)) +napi_gro_receive (napi, skb); +else +__netif_receive_skb (skb); +rcu_read_unlock ();input_queue_head_incr (sd);if (++work >= quota)
net/xfrm/Kconfig +4 -0
@@ -5,6 +5,10 @@ config XFRM bool depends on NET +config XFRM_OFFLOAD + bool + depends on XFRM + config XFRM_ALGO tristate select XFRM
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 4/11 esp: Add a software GRO codepath
In reply to Steffen KlassertThis patch adds GRO callbacks for ESP on ipv4 and ipv6.
In case the GRO layer detects an ESP packet, the esp{4,6}_gro_receive() function calls the xfrm input layer which decapsulates the packet and reinject it into layer 2 by calling netif_rx().
--- net/ipv4/Kconfig | 6 +++ net/ipv4/Makefile | 1 + net/ipv4/esp4.c | 5 +++ net/ipv4/esp4_offload.c | 94 +++++++++++++++++++++++++++++++++++++++ net/ipv4/ip_vti.c | 4 ++ net/ipv4/xfrm4_input.c | 3 ++ net/ipv4/xfrm4_mode_transport.c | 3 +- net/ipv6/Kconfig | 6 +++ net/ipv6/Makefile | 1 + net/ipv6/esp6.c | 5 +++ net/ipv6/esp6_offload.c | 98 +++++++++++++++++++++++++++++++++++++++++ net/ipv6/xfrm6_input.c | 3 ++ net/xfrm/xfrm_input.c | 26 ++++++++--- 13 files changed, 247 insertions(+), 8 deletions(-) create mode 100644 net/ipv4/esp4_offload.c create mode 100644 net/ipv6/esp6_offload.c
net/ipv4/Kconfig +6 -0
@@ -360,6 +360,12 @@ config INET_ESP If unsure, say Y. +config INET_ESP_OFFLOAD + tristate "IP: ESP transformation offload" + depends on INET_ESP + select XFRM_OFFLOAD + default n + config INET_IPCOMP tristate "IP: IPComp transformation" select INET_XFRM_TUNNEL
net/ipv4/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-$(CONFIG_NET_IPVTI) += ip_vti.o obj-$(CONFIG_SYN_COOKIES) += syncookies.o obj-$(CONFIG_INET_AH) += ah4.o obj-$(CONFIG_INET_ESP) += esp4.o +obj-$(CONFIG_INET_ESP_OFFLOAD) += esp4_offload.o obj-$(CONFIG_INET_IPCOMP) += ipcomp.o obj-$(CONFIG_INET_XFRM_TUNNEL) += xfrm4_tunnel.o obj-$(CONFIG_INET_XFRM_MODE_BEET) += xfrm4_mode_beet.o
net/ipv4/esp4.c +5 -0
@@ -635,6 +635,11 @@ static int esp_input(struct xfrm_state *x, struct sk_buff *skb) nfrags =1 ; goto skip_cow; + }else if (skb_xfrm_gro (skb)) { + nfrags =skb_shinfo (skb)->nr_frags; + nfrags++; + + goto skip_cow; }else if (!skb_has_frag_list (skb)) { nfrags =skb_shinfo (skb)->nr_frags; nfrags++;
net/ipv4/esp4_offload.c new file +94 -0
94 lines not on this page · raw message
net/ipv4/ip_vti.c +4 -0
@@ -60,6 +60,10 @@ static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi, tunnel =ip_tunnel_lookup (itn, skb->dev->ifindex,TUNNEL_NO_KEY , iph->saddr, iph->daddr,0 );if (tunnel) { +/* encap_type < -1 indicates a GRO call, we don't support this. */ +if (encap_type <-1 ) +return -EOPNOTSUPP ; +if (!xfrm4_policy_check (NULL ,XFRM_POLICY_IN , skb)) goto drop;
net/ipv4/xfrm4_input.c +3 -0
@@ -53,6 +53,9 @@ int xfrm4_transport_finish(struct sk_buff *skb, int async) iph->tot_len =htons (skb->len);ip_send_check (iph); +if (skb_xfrm_gro (skb)) +return 0 ; +NF_HOOK (NFPROTO_IPV4 ,NF_INET_PRE_ROUTING ,dev_net (skb->dev),NULL , skb, skb->dev,NULL , xfrm4_rcv_encap_finish);
net/ipv4/xfrm4_mode_transport.c +2 -1
@@ -50,7 +50,8 @@ static int xfrm4_transport_input(struct xfrm_state *x, struct sk_buff *skb) skb->network_header = skb->transport_header; }ip_hdr (skb)->tot_len =htons (skb->len + ihl); -skb_reset_transport_header (skb); +if (!skb_xfrm_gro (skb)) +skb_reset_transport_header (skb);return 0 ; }
net/ipv6/Kconfig +6 -0
@@ -75,6 +75,12 @@ config INET6_ESP If unsure, say Y. +config INET6_ESP_OFFLOAD + tristate "IPv6: ESP transformation offload" + depends on INET6_ESP + select XFRM_OFFLOAD + default n + config INET6_IPCOMP tristate "IPv6: IPComp transformation" select INET6_XFRM_TUNNEL
net/ipv6/Makefile +1 -0
@@ -28,6 +28,7 @@ ipv6-objs += $(ipv6-y) obj-$(CONFIG_INET6_AH) += ah6.o obj-$(CONFIG_INET6_ESP) += esp6.o +obj-$(CONFIG_INET6_ESP_OFFLOAD) += esp6_offload.o obj-$(CONFIG_INET6_IPCOMP) += ipcomp6.o obj-$(CONFIG_INET6_XFRM_TUNNEL) += xfrm6_tunnel.o obj-$(CONFIG_INET6_TUNNEL) += tunnel6.o
net/ipv6/esp6.c +5 -0
@@ -577,6 +577,11 @@ static int esp6_input(struct xfrm_state *x, struct sk_buff *skb) nfrags =1 ; goto skip_cow; + }else if (skb_xfrm_gro (skb)) { + nfrags =skb_shinfo (skb)->nr_frags; + nfrags++; + + goto skip_cow; }else if (!skb_has_frag_list (skb)) { nfrags =skb_shinfo (skb)->nr_frags; nfrags++;
net/ipv6/esp6_offload.c new file +98 -0
98 lines not on this page · raw message
net/ipv6/xfrm6_input.c +3 -0
@@ -43,6 +43,9 @@ int xfrm6_transport_finish(struct sk_buff *skb, int async)ipv6_hdr (skb)->payload_len =htons (skb->len);__skb_push (skb, skb->data -skb_network_header (skb)); +if (skb_xfrm_gro (skb)) +return -1 ; +NF_HOOK (NFPROTO_IPV6 ,NF_INET_PRE_ROUTING ,dev_net (skb->dev),NULL , skb, skb->dev,NULL , ip6_rcv_finish);
net/xfrm/xfrm_input.c +19 -7
@@ -193,13 +193,17 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)int decaps =0 ;int async =0 ; -/* A negative encap_type indicates async resumption. */ if (encap_type <0 ) { - async =1 ; - x =xfrm_input_state (skb); - seq =XFRM_SKB_CB (skb)->seq.input.low; - family = x->outer_mode->afinfo->family; - goto resume; +/* An encap_type of -1 indicates async resumption. */ +if (encap_type ==-1 ) { + async =1 ; + x =xfrm_input_state (skb); + seq =XFRM_SKB_CB (skb)->seq.input.low; + family = x->outer_mode->afinfo->family; + goto resume; + } +/* encap_type < -1 indicates a GRO call. */ + encap_type =0 ; } daddr = (xfrm_address_t *)(skb_network_header(skb) +
@@ -374,7 +378,15 @@ resume:netif_rx (skb);return 0 ; }else { -return x->inner_mode->afinfo->transport_finish (skb, async); +int xfrm_gro =skb_xfrm_gro (skb); + + err = x->inner_mode->afinfo->transport_finish (skb, async); +if (xfrm_gro) { +netif_rx (skb); +return 0 ; + } + +return err; } drop_unlock:
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 5/11 skbuff: Extend gso_type to unsigned int.
In reply to Steffen KlassertAll available gso_type flags are currently in use, so extend gso_type to be able to add further flags.
--- include/linux/skbuff.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/skbuff.h +1 -1
@@ -417,7 +417,7 @@ struct skb_shared_info {unsigned short gso_size;/* Warning: this field is not always filled in (UFO)! */ unsigned short gso_segs; -unsigned short gso_type; +unsigned int gso_type;struct sk_buff *frag_list;struct skb_shared_hwtstamps hwtstamps;u32 tskey;
Signature · 1 line
1.9.1
RE: [PATCH RFC 05/11] skbuff: Extend gso_type to unsigned int.
From: Steffen Klassert
Quoted 14 lines
Sent: 23 September 2016 08:54
All availablegso_typeflags are currently in use, so extendgso_typeto be able to add further flags.Signed-off-by Steffen Klassert <[email protected]>--- include/linux/skbuff.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/skbuff.h +0
-0
include/linux/skbuff.h struct skb_shared_info { · line 423
Quoted 8 lines
include/linux/skbuff.h +1
-1@@ -417,7 +417,7 @@ struct skb_shared_info {unsigned short gso_size;/* Warning: this field is not always filled in (UFO)! */ unsigned short gso_segs; -unsigned short gso_type; +unsigned int gso_type;struct sk_buff *frag_list;struct skb_shared_hwtstamps hwtstamps;u32 tskey;
That add a lot of padding.
I'm not even sure DM will like this structure being extended. If ktime_t is 64 bit I think there is already some padding later on.
David
Re: [PATCH RFC 05/11] skbuff: Extend gso_type to unsigned int.
Quoted 19 lines · David Laight
From: Steffen Klassert
Quoted 18 lines
Sent: 23 September 2016 08:54
All availablegso_typeflags are currently in use, so extendgso_typeto be able to add further flags.Signed-off-by Steffen Klassert <[email protected]>--- include/linux/skbuff.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/skbuff.h +0
-1@@ -417,7 +417,7 @@ struct skb_shared_info {unsigned short gso_size;/* Warning: this field is not always filled in (UFO)! */ unsigned short gso_segs; -unsigned short gso_type;
Quoted 8 lines
include/linux/skbuff.h struct skb_shared_info { · line 423
Quoted 4 lines
include/linux/skbuff.h +1
-0+unsigned int gso_type;struct sk_buff *frag_list;struct skb_shared_hwtstamps hwtstamps;u32 tskey;That add a lot of padding.
I'm not even sure DM will like this structure being extended. Ifktime_tis 64 bit I think there is already some padding later on.
+1. Just increasing the size is a bad idea as it will add a 6 byte hole and push frag 0 outside the first cache line.
You may want to take a look at rearranging the structure a bit. That way you could eat into the 4 byte hole that is available on x86_64 so that instead of shifting everything up by 8 bytes they can mostly stay put with only a bit of rearranging. This is one of those times a tool like pahole comes in handy.
Also it occurs to me that one other thing you could look at is if you were to move gso_type into the slot after dataref we might be able to make use of the hole while also saving us some initialization time as the change in size would push us from 32 to 36 which would likely impact the memset operation by at least a 1 cycle increase. If we could make it so that we don't have to initialize the memory for gso_type unless we are planning to set gso_size we can avoid the extra overhead for that. It would just be a matter of validating that nothing reads gso_type unless gso_size is set and that gso_type is always written to prior to setting gso_size to a non-zero value.
- Alex
Steffen Klassert
maintainerRFC PATCH 6/11 net: Add ESP offload features
In reply to Steffen KlassertThis patch adds netdev features to configure IPsec offloads.
--- include/linux/netdev_features.h | 8 +++++++- include/linux/netdevice.h | 1 + include/linux/skbuff.h | 2 ++ net/core/ethtool.c | 3 +++ 4 files changed, 13 insertions(+), 1 deletion(-)
include/linux/netdev_features.h +7 -1
@@ -54,8 +54,9 @@ enum {*/ NETIF_F_GSO_TUNNEL_REMCSUM_BIT ,/* ... TUNNEL with TSO & REMCSUM */ NETIF_F_GSO_SCTP_BIT ,/* ... SCTP fragmentation */ +NETIF_F_GSO_ESP_BIT ,/* ... ESP with TSO */ /**/ NETIF_F_GSO_LAST =/* last bit, see GSO_MASK */ -NETIF_F_GSO_SCTP_BIT , +NETIF_F_GSO_ESP_BIT ,NETIF_F_FCOE_CRC_BIT ,/* FCoE CRC32 */ NETIF_F_SCTP_CRC_BIT, /* SCTP checksum offload */
@@ -72,6 +73,8 @@ enum {NETIF_F_HW_VLAN_STAG_FILTER_BIT ,/* Receive filtering on VLAN STAGs */ NETIF_F_HW_L2FW_DOFFLOAD_BIT ,/* Allow L2 Forwarding in Hardware */ NETIF_F_BUSY_POLL_BIT ,/* Busy poll */ +NETIF_F_HW_ESP_BIT ,/* Hardware ESP transformation offload */ +NETIF_F_HW_ESP_TX_CSUM_BIT ,/* ESP with TX checksum offload */ NETIF_F_HW_TC_BIT, /* Offload TC infrastructure */
@@ -130,12 +133,15 @@ enum {#define NETIF_F_GSO_PARTIAL __NETIF_F(GSO_PARTIAL)#define NETIF_F_GSO_TUNNEL_REMCSUM __NETIF_F(GSO_TUNNEL_REMCSUM)#define NETIF_F_GSO_SCTP __NETIF_F(GSO_SCTP) +#define NETIF_F_GSO_ESP __NETIF_F(GSO_ESP)#define NETIF_F_HW_VLAN_STAG_FILTER __NETIF_F(HW_VLAN_STAG_FILTER)#define NETIF_F_HW_VLAN_STAG_RX __NETIF_F(HW_VLAN_STAG_RX)#define NETIF_F_HW_VLAN_STAG_TX __NETIF_F(HW_VLAN_STAG_TX)#define NETIF_F_HW_L2FW_DOFFLOAD __NETIF_F(HW_L2FW_DOFFLOAD)#define NETIF_F_BUSY_POLL __NETIF_F(BUSY_POLL)#define NETIF_F_HW_TC __NETIF_F(HW_TC) +#define NETIF_F_HW_ESP __NETIF_F(HW_ESP) +#define NETIF_F_HW_ESP_TX_CSUM __NETIF_F(HW_ESP_TX_CSUM)#define for_each_netdev_feature (mask_addr, bit) \ for_each_set_bit(bit, (unsigned long *)mask_addr, NETDEV_FEATURE_COUNT)
include/linux/netdevice.h +1 -0
@@ -4090,6 +4090,7 @@ static inline bool net_gso_ok(netdev_features_t features, int gso_type)BUILD_BUG_ON (SKB_GSO_PARTIAL != (NETIF_F_GSO_PARTIAL >>NETIF_F_GSO_SHIFT ));BUILD_BUG_ON (SKB_GSO_TUNNEL_REMCSUM != (NETIF_F_GSO_TUNNEL_REMCSUM >>NETIF_F_GSO_SHIFT ));BUILD_BUG_ON (SKB_GSO_SCTP != (NETIF_F_GSO_SCTP >>NETIF_F_GSO_SHIFT )); +BUILD_BUG_ON (SKB_GSO_ESP != (NETIF_F_GSO_ESP >>NETIF_F_GSO_SHIFT ));return (features & feature) == feature; }
include/linux/skbuff.h +2 -0
@@ -490,6 +490,8 @@ enum {SKB_GSO_TUNNEL_REMCSUM =1 <<14 ,SKB_GSO_SCTP =1 <<15 , + + SKB_GSO_ESP = 1 << 16, };#if BITS_PER_LONG >32
net/core/ethtool.c +3 -0
@@ -90,6 +90,7 @@ static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation", [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial", [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation", + [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation", [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc", [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
@@ -104,6 +105,8 @@ static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload", [NETIF_F_BUSY_POLL_BIT] = "busy-poll", [NETIF_F_HW_TC_BIT] = "hw-tc-offload", + [NETIF_F_HW_ESP_BIT] = "esp-hw-offload", + [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload", }; static const char
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 7/11 esp: Add gso handlers for esp4 and esp6
In reply to Steffen KlassertThis patch extends the xfrm_type by an encap function pointer and implements esp4_gso_encap and esp6_gso_encap. These functions doing the basic esp encapsulation for a GSO packet. In case the GSP packet needs to be segmented in software, we add gso_segment functions. The secpath is extended to pass the needed informations down the layers. This codepath is going to be used on esp hardware offloads.
--- include/net/xfrm.h | 14 ++++++- net/ipv4/esp4.c | 29 +++++++++++-- net/ipv4/esp4_offload.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++ net/ipv6/esp6.c | 27 +++++++++++- net/ipv6/esp6_offload.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_replay.c | 3 +- 6 files changed, 280 insertions(+), 7 deletions(-)
include/net/xfrm.h +13 -1
@@ -372,6 +372,7 @@ struct xfrm_type {int (*init_state)(struct xfrm_state *x);void (*destructor)(struct xfrm_state *);int (*input)(struct xfrm_state *,struct sk_buff *skb); +void (*encap)(struct xfrm_state *,struct sk_buff *pskb);int (*output)(struct xfrm_state *,struct sk_buff *pskb);int (*reject)(struct xfrm_state *,struct sk_buff *,const struct flowi *);
@@ -979,7 +980,18 @@ void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);struct sec_path {atomic_t refcnt;int len; -struct xfrm_state *xvec[XFRM_MAX_DEPTH ]; + +/* Output sequence number for replay protection on offloading. */ +struct { +__u32 low; +__u32 hi; + } seq; + +struct xfrm_state *xvec[XFRM_MAX_DEPTH ]; + +__u8 proto; +__u8 flags; +#define SKB_GSO_SEGMENT 1 };static inline int secpath_exists (struct sk_buff *skb)
net/ipv4/esp4.c +26 -3
@@ -182,6 +182,22 @@ static void esp_output_done_esn(struct crypto_async_request *base, int err)esp_output_done (base, err); } +static void esp4_gso_encap (struct xfrm_state *x,struct sk_buff *skb) +{ +struct ip_esp_hdr *esph; +struct iphdr *iph =ip_hdr (skb); +int proto = iph->protocol; + +skb_push (skb, -skb_network_offset (skb)); + esph =ip_esp_hdr (skb); + *skb_mac_header (skb) =IPPROTO_ESP ; + + esph->spi = x->id.spi; + esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); + + skb->sp->proto = proto; +} +static int esp_output (struct xfrm_state *x,struct sk_buff *skb) {struct esp_output_extra *extra;
@@ -207,7 +223,7 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)int extralen;int tailen;__be64 seqno; -__u8 proto = *skb_mac_header (skb); +__u8 proto;/* skb is pure payload to encrypt */
@@ -231,12 +247,18 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb) assoclen =sizeof (*esph); extralen =0 ; +if (skb->sp && (skb->sp->flags &SKB_GSO_SEGMENT )) { + proto = skb->sp->proto; + }else { + proto = *skb_mac_header (skb); + *skb_mac_header (skb) =IPPROTO_ESP ; + } +if (x->props.flags &XFRM_STATE_ESN ) { extralen +=sizeof (*extra); assoclen +=sizeof (__be32); } - *skb_mac_header (skb) =IPPROTO_ESP ; esph =ip_esp_hdr (skb);/* this is non-NULL only with UDP Encapsulation */
@@ -942,7 +964,8 @@ static const struct xfrm_type esp_type = .destructor = esp_destroy, .get_mtu = esp4_get_mtu, .input = esp_input, - .output = esp_output + .output = esp_output, + .encap = esp4_gso_encap, };static struct xfrm4_protocol esp4_protocol = {
net/ipv4/esp4_offload.c +108 -0
118 lines not on this page · raw message
net/ipv6/esp6.c +25 -2
@@ -198,6 +198,22 @@ static void esp_output_done_esn(struct crypto_async_request *base, int err)esp_output_done (base, err); } +static void esp6_gso_encap (struct xfrm_state *x,struct sk_buff *skb) +{ +struct ip_esp_hdr *esph; +struct ipv6hdr *iph =ipv6_hdr (skb); +int proto = iph->nexthdr; + +skb_push (skb, -skb_network_offset (skb)); + esph =ip_esp_hdr (skb); + *skb_mac_header (skb) =IPPROTO_ESP ; + + esph->spi = x->id.spi; + esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); + + skb->sp->proto = proto; +} +static int esp6_output (struct xfrm_state *x,struct sk_buff *skb) {int err;
@@ -223,7 +239,7 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)u8 *vaddr;__be32 *seqhi;__be64 seqno; -__u8 proto = *skb_mac_header (skb); +__u8 proto;/* skb is pure payload to encrypt */ aead = x->data;
@@ -247,12 +263,18 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb) assoclen =sizeof (*esph); seqhilen =0 ; +if (skb->sp && (skb->sp->flags &SKB_GSO_SEGMENT )) { + proto = skb->sp->proto; + }else { + proto = *skb_mac_header (skb); + *skb_mac_header (skb) =IPPROTO_ESP ; + } +if (x->props.flags &XFRM_STATE_ESN ) { seqhilen +=sizeof (__be32); assoclen += seqhilen; } - *skb_mac_header (skb) =IPPROTO_ESP ; esph =ip_esp_hdr (skb);if (!skb_cloned (skb)) {
@@ -871,6 +893,7 @@ static const struct xfrm_type esp6_type = { .get_mtu = esp6_get_mtu, .input = esp6_input, .output = esp6_output, + .encap = esp6_gso_encap, .hdr_offset = xfrm6_find_1stfragopt, };
net/ipv6/esp6_offload.c +106 -0
116 lines not on this page · raw message
net/xfrm/xfrm_replay.c +2 -1
@@ -45,7 +45,8 @@ u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq)return seq_hi; } - +EXPORT_SYMBOL (xfrm_replay_seqhi); +;static void xfrm_replay_notify (struct xfrm_state *x,int event) {struct km_event c;
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 8/11 xfrm: Move device notifications to a sepatate file
In reply to Steffen KlassertThis is needed for the upcomming IPsec device offloading.
--- include/net/xfrm.h | 1 + net/xfrm/Makefile | 2 +- net/xfrm/xfrm_device.c | 43 +++++++++++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_policy.c | 17 +---------------- 4 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 net/xfrm/xfrm_device.c
include/net/xfrm.h +1 -0
@@ -1408,6 +1408,7 @@ struct xfrm6_tunnel {void xfrm_init (void );void xfrm4_init (void );int xfrm_state_init (struct net *net); +void xfrm_dev_init (void );void xfrm_state_fini (struct net *net);void xfrm4_state_init (void );void xfrm4_protocol_init (void );
net/xfrm/Makefile +1 -1
@@ -4,7 +4,7 @@ obj-$(CONFIG_XFRM) := xfrm_policy.o xfrm_state.o xfrm_hash.o \ xfrm_input.o xfrm_output.o \ - xfrm_sysctl.o xfrm_replay.o + xfrm_sysctl.o xfrm_replay.o xfrm_device.o obj-$(CONFIG_XFRM_STATISTICS) += xfrm_proc.o obj-$(CONFIG_XFRM_ALGO) += xfrm_algo.o obj-$(CONFIG_XFRM_USER) += xfrm_user.o
net/xfrm/xfrm_device.c new file +43 -0
43 lines not on this page · raw message
net/xfrm/xfrm_policy.c +1 -16
@@ -2921,21 +2921,6 @@ int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo) }EXPORT_SYMBOL (xfrm_policy_unregister_afinfo); -static int xfrm_dev_event (struct notifier_block *this,unsigned long event,void *ptr) -{ -struct net_device *dev =netdev_notifier_info_to_dev (ptr); - -switch (event) { -case NETDEV_DOWN : -xfrm_garbage_collect (dev_net (dev)); - } -return NOTIFY_DONE ; -} - -static struct notifier_block xfrm_dev_notifier = { - .notifier_call = xfrm_dev_event, -}; -#ifdef CONFIG_XFRM_STATISTICS static int __net_init xfrm_statistics_init (struct net *net) {
@@ -3012,7 +2997,7 @@ static int __net_init xfrm_policy_init(struct net *net)INIT_WORK (&net->xfrm.policy_hash_work, xfrm_hash_resize);INIT_WORK (&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);if (net_eq (net, &init_net)) -register_netdevice_notifier (&xfrm_dev_notifier); +xfrm_dev_init ();return 0 ; out_bydst:
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 9/11 xfrm: Add an IPsec hardware offloading API
In reply to Steffen KlassertThis patch adds all the bits that are needed to do IPsec hardware offload for IPsec states and ESP packets. We add xfrmdev_ops to the net_device. xfrmdev_ops has function pointers that are needed to manage the xfrm states in the hardware and to do a per packet offloading decision.
Joint work with:
Ilan Tayari <[email protected]>
Guy Shapiro <[email protected]>
--- include/linux/netdevice.h | 14 ++++++++ include/net/xfrm.h | 40 +++++++++++++++++++++ include/uapi/linux/xfrm.h | 8 +++++ net/ipv4/esp4.c | 31 ++++++++++++++++- net/ipv4/xfrm4_output.c | 3 +- net/ipv6/esp6.c | 32 ++++++++++++++++- net/xfrm/xfrm_device.c | 51 ++++++++++++++++++++++++++- net/xfrm/xfrm_input.c | 41 +++++++++++++++++++++- net/xfrm/xfrm_output.c | 61 ++++++++++++++++++++++++++++++-- net/xfrm/xfrm_policy.c | 11 +++--- net/xfrm/xfrm_state.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_user.c | 81 ++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 449 insertions(+), 13 deletions(-)
include/linux/netdevice.h +14 -0
@@ -832,6 +832,16 @@ struct netdev_xdp { }; }; +#ifdef CONFIG_XFRM +struct xfrmdev_ops { +int (*xdo_dev_state_add) (struct xfrm_state *x); +void (*xdo_dev_state_delete) (struct xfrm_state *x); +void (*xdo_dev_state_free) (struct xfrm_state *x); +bool (*xdo_dev_offload_ok) (struct sk_buff *skb, +struct xfrm_state *x); +}; +#endif +/* * This structure defines the management hooks for network devices. * The following hooks can be defined; unless noted otherwise, they are
@@ -1708,6 +1718,10 @@ struct net_device {const struct ndisc_ops *ndisc_ops;#endif +#ifdef CONFIG_XFRM +const struct xfrmdev_ops *xfrmdev_ops; +#endif +const struct header_ops *header_ops;unsigned int flags;
include/net/xfrm.h +40 -0
@@ -120,6 +120,13 @@ struct xfrm_state_walk {struct xfrm_address_filter *filter; }; +struct xfrm_state_offload { +struct net_device *dev; +unsigned long offload_handle; +unsigned int num_exthdrs; +u8 flags; +}; +/* Full description of state of transformer. */ struct xfrm_state {possible_net_t xs_net;
@@ -207,6 +214,8 @@ struct xfrm_state {struct xfrm_lifetime_cur curlft;struct tasklet_hrtimer mtimer; +struct xfrm_state_offload xso; +/* used to fix curlft->add_time when changing date */ long saved_tmo;
@@ -373,6 +382,7 @@ struct xfrm_type {void (*destructor)(struct xfrm_state *);int (*input)(struct xfrm_state *,struct sk_buff *skb);void (*encap)(struct xfrm_state *,struct sk_buff *pskb); +int (*input_tail)(struct xfrm_state *x,struct sk_buff *skb);int (*output)(struct xfrm_state *,struct sk_buff *pskb);int (*reject)(struct xfrm_state *,struct sk_buff *,const struct flowi *);
@@ -977,6 +987,22 @@ static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)void xfrm_dst_ifdown (struct dst_entry *dst,struct net_device *dev); +struct xfrm_offload_state { +u32 flags; +#define SA_DELETE_REQ 1 +#define CRYPTO_DONE 2 +#define CRYPTO_NEXT_DONE 4 +u32 status; +#define CRYPTO_SUCCESS 1 +#define CRYPTO_GENERIC_ERROR 2 +#define CRYPTO_TRANSPORT_AH_AUTH_FAILED 4 +#define CRYPTO_TRANSPORT_ESP_AUTH_FAILED 8 +#define CRYPTO_TUNNEL_AH_AUTH_FAILED 16 +#define CRYPTO_TUNNEL_ESP_AUTH_FAILED 32 +#define CRYPTO_INVALID_PACKET_SYNTAX 64 +#define CRYPTO_INVALID_PROTOCOL 128 +}; +struct sec_path {atomic_t refcnt;int len;
@@ -988,6 +1014,7 @@ struct sec_path { } seq;struct xfrm_state *xvec[XFRM_MAX_DEPTH ]; +struct xfrm_offload_state ovec[XFRM_MAX_DEPTH ];__u8 proto;__u8 flags;
@@ -1514,6 +1541,7 @@ struct xfrmk_spdinfo {struct xfrm_state *xfrm_find_acq_byseq (struct net *net,u32 mark,u32 seq);int xfrm_state_delete (struct xfrm_state *x);int xfrm_state_flush (struct net *net,u8 proto,bool task_valid); +int xfrm_dev_state_flush (struct net *net,struct net_device *dev,bool task_valid);void xfrm_sad_getinfo (struct net *net,struct xfrmk_sadinfo *si);void xfrm_spd_getinfo (struct net *net,struct xfrmk_spdinfo *si);u32 xfrm_replay_seqhi (struct xfrm_state *x,__be32 net_seq);
@@ -1593,6 +1621,11 @@ static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb) }#endif +struct dst_entry *__xfrm_dst_lookup (struct net *net,int tos,int oif, +const xfrm_address_t *saddr, +const xfrm_address_t *daddr, +int family); +struct xfrm_policy *xfrm_policy_alloc (struct net *net,gfp_t gfp);void xfrm_policy_walk_init (struct xfrm_policy_walk *walk,u8 type);
@@ -1787,6 +1820,13 @@ static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb) {return skb->sp->xvec[skb->sp->len -1 ]; } +static inline struct xfrm_offload_state *xfrm_offload_input (struct sk_buff *skb) +{ +if (!skb->sp || !skb->sp->len) +return NULL ; + +return &skb->sp->ovec[skb->sp->len -1 ]; +}#endif static inline int xfrm_mark_get (struct nlattr **attrs,struct xfrm_mark *m)
include/uapi/linux/xfrm.h +8 -0
@@ -303,6 +303,7 @@ enum xfrm_attr_type_t {XFRMA_PROTO ,/* __u8 */ XFRMA_ADDRESS_FILTER ,/* struct xfrm_address_filter */ XFRMA_PAD , +XFRMA_OFFLOAD_DEV ,/* struct xfrm_state_offload */ __XFRMA_MAX#define XFRMA_MAX (__XFRMA_MAX - 1)
@@ -494,6 +495,13 @@ struct xfrm_address_filter {__u8 dplen; }; +struct xfrm_user_offload { +int ifindex; +__u8 flags; +}; +#define XFRM_OFFLOAD_IPV6 1 +#define XFRM_OFFLOAD_INBOUND 2 +#ifndef __KERNEL__/* backwards compatibility for userspace */ #define XFRMGRP_ACQUIRE 1
net/ipv4/esp4.c +30 -1
@@ -361,6 +361,14 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb) esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); esph->spi = x->id.spi; +if (x->xso.offload_handle && skb->sp) { +if (skb->sp->flags &SKB_GSO_SEGMENT ) + esph->seq_no =htonl (skb->sp->seq.low); + +spin_unlock_bh (&x->lock); +return 0 ; + } + tmp =esp_alloc_tmp (aead, nfrags +2 , extralen);if (!tmp) {spin_unlock_bh (&x->lock);
@@ -435,6 +443,13 @@ skip_cow: esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); esph->spi = x->id.spi; +if (x->xso.offload_handle && skb->sp) { +if (skb->sp->flags &SKB_GSO_SEGMENT ) + esph->seq_no =htonl (skb->sp->seq.low); + +return 0 ; + } + tmp =esp_alloc_tmp (aead, nfrags, extralen);if (!tmp) { err = -ENOMEM ;
@@ -498,6 +513,7 @@ static int esp_input_done2(struct sk_buff *skb, int err) {const struct iphdr *iph;struct xfrm_state *x =xfrm_input_state (skb); +struct xfrm_offload_state *xo =xfrm_offload_input (skb);struct crypto_aead *aead = x->data;int alen =crypto_aead_authsize (aead);int hlen =sizeof (struct ip_esp_hdr ) +crypto_aead_ivsize (aead);
@@ -506,7 +522,8 @@ static int esp_input_done2(struct sk_buff *skb, int err)u8 nexthdr[2 ];int padlen; -kfree (ESP_SKB_CB (skb)->tmp); +if (!(xo->flags &CRYPTO_DONE )) +kfree (ESP_SKB_CB (skb)->tmp);if (unlikely (err)) goto out;
@@ -616,6 +633,17 @@ static void esp_input_done_esn(struct crypto_async_request *base, int err)esp_input_done (base, err); } +static int esp_input_tail (struct xfrm_state *x,struct sk_buff *skb) +{ +struct crypto_aead *aead = x->data; + +if (!pskb_may_pull (skb,sizeof (struct ip_esp_hdr ) +crypto_aead_ivsize (aead))) +return -EINVAL ; + + skb->ip_summed =CHECKSUM_NONE ; + +return esp_input_done2 (skb,0 ); +}/* * Note: detecting truncated vs. non-truncated authentication data is very * expensive, so we only support truncated data, which is the recommended
@@ -964,6 +992,7 @@ static const struct xfrm_type esp_type = .destructor = esp_destroy, .get_mtu = esp4_get_mtu, .input = esp_input, + .input_tail = esp_input_tail, .output = esp_output, .encap = esp4_gso_encap, };
net/ipv4/xfrm4_output.c +2 -1
@@ -29,7 +29,8 @@ static int xfrm4_tunnel_check_size(struct sk_buff *skb) goto out; mtu =dst_mtu (skb_dst (skb)); -if (skb->len > mtu) { +if ((!skb_is_gso (skb) && skb->len > mtu) || + (skb_is_gso (skb) &&skb_gso_network_seglen (skb) >ip_skb_dst_mtu (skb->sk, skb))) { skb->protocol =htons (ETH_P_IP );if (skb->sk)
net/ipv6/esp6.c +31 -1
@@ -341,6 +341,14 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb) esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); esph->spi = x->id.spi; +if (x->xso.offload_handle && skb->sp) { +if (skb->sp->flags &SKB_GSO_SEGMENT ) + esph->seq_no =htonl (skb->sp->seq.low); + +spin_unlock_bh (&x->lock); +return 0 ; + } + tmp =esp_alloc_tmp (aead, nfrags +2 , seqhilen);if (!tmp) {spin_unlock_bh (&x->lock);
@@ -416,6 +424,13 @@ skip_cow: esph->seq_no =htonl (XFRM_SKB_CB (skb)->seq.output.low); esph->spi = x->id.spi; +if (x->xso.offload_handle && skb->sp) { +if (skb->sp->flags &SKB_GSO_SEGMENT ) + esph->seq_no =htonl (skb->sp->seq.low); + +return 0 ; + } + tmp =esp_alloc_tmp (aead, nfrags, seqhilen);if (!tmp) { err = -ENOMEM ;
@@ -478,6 +493,7 @@ error:static int esp_input_done2 (struct sk_buff *skb,int err) {struct xfrm_state *x =xfrm_input_state (skb); +struct xfrm_offload_state *xo =xfrm_offload_input (skb);struct crypto_aead *aead = x->data;int alen =crypto_aead_authsize (aead);int hlen =sizeof (struct ip_esp_hdr ) +crypto_aead_ivsize (aead);
@@ -486,7 +502,8 @@ static int esp_input_done2(struct sk_buff *skb, int err)int padlen;u8 nexthdr[2 ]; -kfree (ESP_SKB_CB (skb)->tmp); +if (!(xo->flags &CRYPTO_DONE )) +kfree (ESP_SKB_CB (skb)->tmp);if (unlikely (err)) goto out;
@@ -559,6 +576,18 @@ static void esp_input_done_esn(struct crypto_async_request *base, int err)esp_input_done (base, err); } +static int esp6_input_tail (struct xfrm_state *x,struct sk_buff *skb) +{ +struct crypto_aead *aead = x->data; + +if (!pskb_may_pull (skb,sizeof (struct ip_esp_hdr ) +crypto_aead_ivsize (aead))) +return -EINVAL ; + + skb->ip_summed =CHECKSUM_NONE ; + +return esp_input_done2 (skb,0 ); +} +static int esp6_input (struct xfrm_state *x,struct sk_buff *skb) {struct ip_esp_hdr *esph;
@@ -892,6 +921,7 @@ static const struct xfrm_type esp6_type = { .destructor = esp6_destroy, .get_mtu = esp6_get_mtu, .input = esp6_input, + .input_tail = esp6_input_tail, .output = esp6_output, .encap = esp6_gso_encap, .hdr_offset = xfrm6_find_1stfragopt,
net/xfrm/xfrm_device.c +50 -1
63 lines not on this page · raw message
net/xfrm/xfrm_input.c +40 -1
71 lines not on this page · raw message
net/xfrm/xfrm_output.c +58 -3
84 lines not on this page · raw message
net/xfrm/xfrm_policy.c +6 -5
@@ -121,11 +121,11 @@ static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)rcu_read_unlock (); } -static inline struct dst_entry *__xfrm_dst_lookup (struct net *net, -int tos,int oif, -const xfrm_address_t *saddr, -const xfrm_address_t *daddr, -int family) +struct dst_entry *__xfrm_dst_lookup (struct net *net, +int tos,int oif, +const xfrm_address_t *saddr, +const xfrm_address_t *daddr, +int family) {struct xfrm_policy_afinfo *afinfo;struct dst_entry *dst;
@@ -140,6 +140,7 @@ static inline struct dst_entry *__xfrm_dst_lookup(struct net *net,return dst; } +EXPORT_SYMBOL (__xfrm_dst_lookup); static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos, int oif,
net/xfrm/xfrm_state.c +89 -0
131 lines not on this page · raw message
net/xfrm/xfrm_user.c +81 -0
117 lines not on this page · raw message
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 10/11 xfrm: Add xfrm_replay_overflow functions for offloading
In reply to Steffen KlassertThis patch adds functions that handles IPsec sequence numbers for GSO segments and TSO offloading. We need to calculate and update the sequence numbers based on the segments that GSO/TSO will generate. We need this to keep software and hardware sequence number counter in sync.
--- net/xfrm/xfrm_replay.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-)
net/xfrm/xfrm_replay.c +101 -2
@@ -559,6 +559,83 @@ static void xfrm_replay_advance_esn(struct xfrm_state *x, __be32 net_seq) x->repl->notify (x,XFRM_REPLAY_UPDATE ); } +static int xfrm_replay_overflow_offload (struct xfrm_state *x,struct sk_buff *skb) +{ +int err =0 ; +struct net *net =xs_net (x); +struct sec_path *sp = skb->sp; +__u32 oseq = x->replay.oseq; + +if (!sp) +return xfrm_replay_overflow (x, skb); + +if (x->type->flags &XFRM_TYPE_REPLAY_PROT ) { +if (!skb_is_gso (skb)) { +XFRM_SKB_CB (skb)->seq.output.low = ++oseq; + sp->seq.low = oseq; + }else { +XFRM_SKB_CB (skb)->seq.output.low = oseq +1 ; + sp->seq.low = oseq +1 ; + oseq +=skb_shinfo (skb)->gso_segs; + } + +XFRM_SKB_CB (skb)->seq.output.hi =0 ; + sp->seq.hi =0 ; +if (unlikely (oseq < x->replay.oseq)) { +xfrm_audit_state_replay_overflow (x, skb); + err = -EOVERFLOW ; + +return err; + } + + x->replay.oseq = oseq; + +if (xfrm_aevent_is_on (net)) + x->repl->notify (x,XFRM_REPLAY_UPDATE ); + } + +return err; +} + +static int xfrm_replay_overflow_offload_bmp (struct xfrm_state *x,struct sk_buff *skb) +{ +int err =0 ; +struct sec_path *sp = skb->sp; +struct xfrm_replay_state_esn *replay_esn = x->replay_esn; +struct net *net =xs_net (x); +__u32 oseq = replay_esn->oseq; + +if (!sp) +return xfrm_replay_overflow_bmp (x, skb); + +if (x->type->flags &XFRM_TYPE_REPLAY_PROT ) { +if (!skb_is_gso (skb)) { +XFRM_SKB_CB (skb)->seq.output.low = ++oseq; + sp->seq.low = oseq; + }else { +XFRM_SKB_CB (skb)->seq.output.low = oseq +1 ; + sp->seq.low = oseq +1 ; + oseq +=skb_shinfo (skb)->gso_segs; + } + +XFRM_SKB_CB (skb)->seq.output.hi =0 ; + sp->seq.hi =0 ; +if (unlikely (oseq < replay_esn->oseq)) { +xfrm_audit_state_replay_overflow (x, skb); + err = -EOVERFLOW ; + +return err; + }else { + replay_esn->oseq = oseq; + } + +if (xfrm_aevent_is_on (net)) + x->repl->notify (x,XFRM_REPLAY_UPDATE ); + } + +return err; +} +static const struct xfrm_replay xfrm_replay_legacy = { .advance = xfrm_replay_advance, .check = xfrm_replay_check,
@@ -567,6 +644,14 @@ static const struct xfrm_replay xfrm_replay_legacy = { .overflow = xfrm_replay_overflow, }; +static const struct xfrm_replay xfrm_replay_offload_legacy = { + .advance = xfrm_replay_advance, + .check = xfrm_replay_check, + .recheck = xfrm_replay_check, + .notify = xfrm_replay_notify, + .overflow = xfrm_replay_overflow_offload, +}; +static const struct xfrm_replay xfrm_replay_bmp = { .advance = xfrm_replay_advance_bmp, .check = xfrm_replay_check_bmp,
@@ -575,6 +660,14 @@ static const struct xfrm_replay xfrm_replay_bmp = { .overflow = xfrm_replay_overflow_bmp, }; +static const struct xfrm_replay xfrm_replay_offload = { + .advance = xfrm_replay_advance_bmp, + .check = xfrm_replay_check_bmp, + .recheck = xfrm_replay_check_bmp, + .notify = xfrm_replay_notify_bmp, + .overflow = xfrm_replay_overflow_offload_bmp, +}; +static const struct xfrm_replay xfrm_replay_esn = { .advance = xfrm_replay_advance_esn, .check = xfrm_replay_check_esn,
@@ -596,10 +689,16 @@ int xfrm_init_replay(struct xfrm_state *x)if (replay_esn->replay_window ==0 )return -EINVAL ; x->repl = &xfrm_replay_esn; - }else + }else if (x->xso.offload_handle) { + x->repl = &xfrm_replay_offload; + }else { x->repl = &xfrm_replay_bmp; - }else + } + }else if (x->xso.offload_handle) { + x->repl = &xfrm_replay_offload_legacy; + }else { x->repl = &xfrm_replay_legacy; + }return 0 ; }
Signature · 1 line
1.9.1
Steffen Klassert
maintainerRFC PATCH 11/11 xfrm: Add encapsulation header offsets while SKB is not encrypted
In reply to Steffen KlassertFrom: Ilan Tayari <[email protected]>
Both esp4 and esp6 used to assume that the SKB payload is encrypted and therefore the inner_network and inner_transport offsets are not relevant.
When doing crypto offload in the NIC, this is no longer the case and the NIC driver needs these offsets so it can do TX TCP checksum offloading.
This patch sets the inner_network and inner_transport members of the SKB, as well as encapsulation, to reflect the actual positions of these headers, and removes them only once encryption is done on the payload.
--- net/ipv4/xfrm4_mode_transport.c | 2 ++ net/ipv4/xfrm4_mode_tunnel.c | 3 +++ net/ipv6/xfrm6_mode_transport.c | 1 + net/ipv6/xfrm6_mode_tunnel.c | 3 +++ net/xfrm/xfrm_output.c | 6 +++--- 5 files changed, 12 insertions(+), 3 deletions(-)
net/ipv4/xfrm4_mode_transport.c +2 -0
@@ -23,6 +23,8 @@ static int xfrm4_transport_output(struct xfrm_state *x, struct sk_buff *skb)struct iphdr *iph =ip_hdr (skb);int ihl = iph->ihl *4 ; +skb_set_inner_transport_header (skb,skb_transport_offset (skb)); +skb_set_network_header (skb, -x->props.header_len); skb->mac_header = skb->network_header + offsetof(struct iphdr , protocol);
net/ipv4/xfrm4_mode_tunnel.c +3 -0
@@ -33,6 +33,9 @@ static int xfrm4_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)struct iphdr *top_iph;int flags; +skb_set_inner_network_header (skb,skb_network_offset (skb)); +skb_set_inner_transport_header (skb,skb_transport_offset (skb)); +skb_set_network_header (skb, -x->props.header_len); skb->mac_header = skb->network_header + offsetof(struct iphdr , protocol);
net/ipv6/xfrm6_mode_transport.c +1 -0
@@ -26,6 +26,7 @@ static int xfrm6_transport_output(struct xfrm_state *x, struct sk_buff *skb)int hdr_len; iph =ipv6_hdr (skb); +skb_set_inner_transport_header (skb,skb_transport_offset (skb)); hdr_len = x->type->hdr_offset (x, skb, &prevhdr);skb_set_mac_header (skb, (prevhdr - x->props.header_len) - skb->data);
net/ipv6/xfrm6_mode_tunnel.c +3 -0
@@ -36,6 +36,9 @@ static int xfrm6_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)struct ipv6hdr *top_iph;int dsfield; +skb_set_inner_network_header (skb,skb_network_offset (skb)); +skb_set_inner_transport_header (skb,skb_transport_offset (skb)); +skb_set_network_header (skb, -x->props.header_len); skb->mac_header = skb->network_header + offsetof(struct ipv6hdr , nexthdr);
net/xfrm/xfrm_output.c +3 -3
@@ -99,9 +99,6 @@ static int xfrm_output_one(struct sk_buff *skb, int err)skb_dst_force (skb); -/* Inner headers are invalid now. */ - skb->encapsulation =0 ; -if (skb_shinfo (skb)->gso_type &SKB_GSO_ESP ) { x->type->encap (x, skb); }else {
@@ -243,6 +240,7 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb)if (skb->sp)secpath_put (skb->sp); skb->sp = sp; + skb->encapsulation =1 ; skb->sp->xvec[skb->sp->len++] = x;xfrm_state_hold (x);
@@ -254,6 +252,8 @@ int xfrm_output(struct sock *sk, struct sk_buff *skb) }if (x->xso.dev->features &NETIF_F_HW_ESP_TX_CSUM ) goto out; + }else { + skb->encapsulation =0 ; }if (skb_is_gso (skb))
Signature · 1 line
1.9.1