gre_offload.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *	IPV4 GSO/GRO offload support
 *	Linux INET implementation
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 *	GRE GSO support
 */

#include <linux/skbuff.h>
14
#include <linux/init.h>
15 16 17 18 19 20
#include <net/protocol.h>
#include <net/gre.h>

static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
				       netdev_features_t features)
{
21
	int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
22
	struct sk_buff *segs = ERR_PTR(-EINVAL);
23
	u16 mac_offset = skb->mac_header;
24
	__be16 protocol = skb->protocol;
25 26
	u16 mac_len = skb->mac_len;
	int gre_offset, outer_hlen;
27
	bool need_csum, ufo;
28

29 30 31
	if (!skb->encapsulation)
		goto out;

32
	if (unlikely(tnl_hlen < sizeof(struct gre_base_hdr)))
33 34
		goto out;

35
	if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
36 37
		goto out;

38 39
	/* setup inner skb. */
	skb->encapsulation = 0;
40
	SKB_GSO_CB(skb)->encap_level = 0;
41
	__skb_pull(skb, tnl_hlen);
42 43 44
	skb_reset_mac_header(skb);
	skb_set_network_header(skb, skb_inner_network_offset(skb));
	skb->mac_len = skb_inner_network_offset(skb);
45
	skb->protocol = skb->inner_protocol;
46

47 48 49
	need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_GRE_CSUM);
	skb->encap_hdr_csum = need_csum;

50 51
	ufo = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);

52 53
	features &= skb->dev->hw_enc_features;

54 55 56 57 58 59 60 61 62 63
	/* The only checksum offload we care about from here on out is the
	 * outer one so strip the existing checksum feature flags based
	 * on the fact that we will be computing our checksum in software.
	 */
	if (ufo) {
		features &= ~NETIF_F_CSUM_MASK;
		if (!need_csum)
			features |= NETIF_F_HW_CSUM;
	}

64
	/* segment inner packet. */
65
	segs = skb_mac_gso_segment(skb, features);
H
Himangi Saraogi 已提交
66
	if (IS_ERR_OR_NULL(segs)) {
67 68
		skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
				     mac_len);
69
		goto out;
70
	}
71

72 73
	outer_hlen = skb_tnl_header_len(skb);
	gre_offset = outer_hlen - tnl_hlen;
74 75
	skb = segs;
	do {
76
		struct gre_base_hdr *greh;
77
		__sum16 *pcsum;
78

79 80 81 82 83
		/* Set up inner headers if we are offloading inner checksum */
		if (skb->ip_summed == CHECKSUM_PARTIAL) {
			skb_reset_inner_headers(skb);
			skb->encapsulation = 1;
		}
84

85 86
		skb->mac_len = mac_len;
		skb->protocol = protocol;
87 88 89 90 91 92 93 94 95 96

		__skb_push(skb, outer_hlen);
		skb_reset_mac_header(skb);
		skb_set_network_header(skb, mac_len);
		skb_set_transport_header(skb, gre_offset);

		if (!need_csum)
			continue;

		greh = (struct gre_base_hdr *)skb_transport_header(skb);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
		pcsum = (__sum16 *)(greh + 1);

		if (skb_is_gso(skb)) {
			unsigned int partial_adj;

			/* Adjust checksum to account for the fact that
			 * the partial checksum is based on actual size
			 * whereas headers should be based on MSS size.
			 */
			partial_adj = skb->len + skb_headroom(skb) -
				      SKB_GSO_CB(skb)->data_offset -
				      skb_shinfo(skb)->gso_size;
			*pcsum = ~csum_fold((__force __wsum)htonl(partial_adj));
		} else {
			*pcsum = 0;
		}
113

114 115
		*(pcsum + 1) = 0;
		*pcsum = gso_make_checksum(skb, 0);
116 117 118 119 120
	} while ((skb = skb->next));
out:
	return segs;
}

121 122 123 124 125 126 127 128 129 130 131 132
static struct sk_buff **gre_gro_receive(struct sk_buff **head,
					struct sk_buff *skb)
{
	struct sk_buff **pp = NULL;
	struct sk_buff *p;
	const struct gre_base_hdr *greh;
	unsigned int hlen, grehlen;
	unsigned int off;
	int flush = 1;
	struct packet_offload *ptype;
	__be16 type;

133 134 135 136 137
	if (NAPI_GRO_CB(skb)->encap_mark)
		goto out;

	NAPI_GRO_CB(skb)->encap_mark = 1;

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	off = skb_gro_offset(skb);
	hlen = off + sizeof(*greh);
	greh = skb_gro_header_fast(skb, off);
	if (skb_gro_header_hard(skb, hlen)) {
		greh = skb_gro_header_slow(skb, hlen, off);
		if (unlikely(!greh))
			goto out;
	}

	/* Only support version 0 and K (key), C (csum) flags. Note that
	 * although the support for the S (seq#) flag can be added easily
	 * for GRO, this is problematic for GSO hence can not be enabled
	 * here because a GRO pkt may end up in the forwarding path, thus
	 * requiring GSO support to break it up correctly.
	 */
	if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0)
		goto out;

156 157 158 159 160 161 162 163
	/* We can only support GRE_CSUM if we can track the location of
	 * the GRE header.  In the case of FOU/GUE we cannot because the
	 * outer UDP header displaces the GRE header leaving us in a state
	 * of limbo.
	 */
	if ((greh->flags & GRE_CSUM) && NAPI_GRO_CB(skb)->is_fou)
		goto out;

164 165 166 167
	type = greh->protocol;

	rcu_read_lock();
	ptype = gro_find_receive_by_type(type);
168
	if (!ptype)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		goto out_unlock;

	grehlen = GRE_HEADER_SECTION;

	if (greh->flags & GRE_KEY)
		grehlen += GRE_HEADER_SECTION;

	if (greh->flags & GRE_CSUM)
		grehlen += GRE_HEADER_SECTION;

	hlen = off + grehlen;
	if (skb_gro_header_hard(skb, hlen)) {
		greh = skb_gro_header_slow(skb, hlen, off);
		if (unlikely(!greh))
			goto out_unlock;
	}
185 186

	/* Don't bother verifying checksum if we're going to flush anyway. */
187 188
	if ((greh->flags & GRE_CSUM) && !NAPI_GRO_CB(skb)->flush) {
		if (skb_gro_checksum_simple_validate(skb))
189
			goto out_unlock;
190

191 192 193 194
		skb_gro_checksum_try_convert(skb, IPPROTO_GRE, 0,
					     null_compute_pseudo);
	}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	for (p = *head; p; p = p->next) {
		const struct gre_base_hdr *greh2;

		if (!NAPI_GRO_CB(p)->same_flow)
			continue;

		/* The following checks are needed to ensure only pkts
		 * from the same tunnel are considered for aggregation.
		 * The criteria for "the same tunnel" includes:
		 * 1) same version (we only support version 0 here)
		 * 2) same protocol (we only support ETH_P_IP for now)
		 * 3) same set of flags
		 * 4) same key if the key field is present.
		 */
		greh2 = (struct gre_base_hdr *)(p->data + off);

		if (greh2->flags != greh->flags ||
		    greh2->protocol != greh->protocol) {
			NAPI_GRO_CB(p)->same_flow = 0;
			continue;
		}
		if (greh->flags & GRE_KEY) {
			/* compare keys */
			if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
				NAPI_GRO_CB(p)->same_flow = 0;
				continue;
			}
		}
	}

	skb_gro_pull(skb, grehlen);

	/* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
	skb_gro_postpull_rcsum(skb, greh, grehlen);

	pp = ptype->callbacks.gro_receive(head, skb);
231
	flush = 0;
232 233 234 235 236 237 238 239 240

out_unlock:
	rcu_read_unlock();
out:
	NAPI_GRO_CB(skb)->flush |= flush;

	return pp;
}

241
static int gre_gro_complete(struct sk_buff *skb, int nhoff)
242 243 244 245 246 247 248
{
	struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
	struct packet_offload *ptype;
	unsigned int grehlen = sizeof(*greh);
	int err = -ENOENT;
	__be16 type;

249 250 251
	skb->encapsulation = 1;
	skb_shinfo(skb)->gso_type = SKB_GSO_GRE;

252 253 254 255 256 257 258 259 260
	type = greh->protocol;
	if (greh->flags & GRE_KEY)
		grehlen += GRE_HEADER_SECTION;

	if (greh->flags & GRE_CSUM)
		grehlen += GRE_HEADER_SECTION;

	rcu_read_lock();
	ptype = gro_find_complete_by_type(type);
261
	if (ptype)
262 263 264
		err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);

	rcu_read_unlock();
265 266 267

	skb_set_inner_mac_header(skb, nhoff + grehlen);

268 269 270
	return err;
}

271 272 273
static const struct net_offload gre_offload = {
	.callbacks = {
		.gso_segment = gre_gso_segment,
274 275
		.gro_receive = gre_gro_receive,
		.gro_complete = gre_gro_complete,
276 277 278
	},
};

279
static int __init gre_offload_init(void)
280
{
281 282 283 284 285 286 287 288 289 290 291 292 293
	int err;

	err = inet_add_offload(&gre_offload, IPPROTO_GRE);
#if IS_ENABLED(CONFIG_IPV6)
	if (err)
		return err;

	err = inet6_add_offload(&gre_offload, IPPROTO_GRE);
	if (err)
		inet_del_offload(&gre_offload, IPPROTO_GRE);
#endif

	return err;
294
}
295
device_initcall(gre_offload_init);