xref: /linux/net/core/gro.c (revision f2bb3434544454099a5b6dec213567267b05d79d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 #include <net/psp.h>
3 #include <net/gro.h>
4 #include <net/dst_metadata.h>
5 #include <net/busy_poll.h>
6 #include <trace/events/net.h>
7 #include <linux/skbuff_ref.h>
8 
9 #define MAX_GRO_SKBS 8
10 
11 static DEFINE_SPINLOCK(offload_lock);
12 
13 /**
14  *	dev_add_offload - register offload handlers
15  *	@po: protocol offload declaration
16  *
17  *	Add protocol offload handlers to the networking stack. The passed
18  *	&proto_offload is linked into kernel lists and may not be freed until
19  *	it has been removed from the kernel lists.
20  *
21  *	This call does not sleep therefore it can not
22  *	guarantee all CPU's that are in middle of receiving packets
23  *	will see the new offload handlers (until the next received packet).
24  */
25 void dev_add_offload(struct packet_offload *po)
26 {
27 	struct packet_offload *elem;
28 
29 	spin_lock(&offload_lock);
30 	list_for_each_entry(elem, &net_hotdata.offload_base, list) {
31 		if (po->priority < elem->priority)
32 			break;
33 	}
34 	list_add_rcu(&po->list, elem->list.prev);
35 	spin_unlock(&offload_lock);
36 }
37 EXPORT_SYMBOL(dev_add_offload);
38 
39 /**
40  *	__dev_remove_offload	 - remove offload handler
41  *	@po: packet offload declaration
42  *
43  *	Remove a protocol offload handler that was previously added to the
44  *	kernel offload handlers by dev_add_offload(). The passed &offload_type
45  *	is removed from the kernel lists and can be freed or reused once this
46  *	function returns.
47  *
48  *      The packet type might still be in use by receivers
49  *	and must not be freed until after all the CPU's have gone
50  *	through a quiescent state.
51  */
52 static void __dev_remove_offload(struct packet_offload *po)
53 {
54 	struct list_head *head = &net_hotdata.offload_base;
55 	struct packet_offload *po1;
56 
57 	spin_lock(&offload_lock);
58 
59 	list_for_each_entry(po1, head, list) {
60 		if (po == po1) {
61 			list_del_rcu(&po->list);
62 			goto out;
63 		}
64 	}
65 
66 	pr_warn("dev_remove_offload: %p not found\n", po);
67 out:
68 	spin_unlock(&offload_lock);
69 }
70 
71 /**
72  *	dev_remove_offload	 - remove packet offload handler
73  *	@po: packet offload declaration
74  *
75  *	Remove a packet offload handler that was previously added to the kernel
76  *	offload handlers by dev_add_offload(). The passed &offload_type is
77  *	removed from the kernel lists and can be freed or reused once this
78  *	function returns.
79  *
80  *	This call sleeps to guarantee that no CPU is looking at the packet
81  *	type after return.
82  */
83 void dev_remove_offload(struct packet_offload *po)
84 {
85 	__dev_remove_offload(po);
86 
87 	synchronize_net();
88 }
89 EXPORT_SYMBOL(dev_remove_offload);
90 
91 
92 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
93 {
94 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
95 	unsigned int offset = skb_gro_offset(skb);
96 	unsigned int headlen = skb_headlen(skb);
97 	unsigned int len = skb_gro_len(skb);
98 	unsigned int delta_truesize;
99 	unsigned int new_truesize;
100 	struct sk_buff *lp;
101 	int segs;
102 
103 	/* Do not splice page pool based packets w/ non-page pool
104 	 * packets. This can result in reference count issues as page
105 	 * pool pages will not decrement the reference count and will
106 	 * instead be immediately returned to the pool or have frag
107 	 * count decremented.
108 	 */
109 	if (p->pp_recycle != skb->pp_recycle)
110 		return -ETOOMANYREFS;
111 
112 	if (skb_zcopy(p) || skb_zcopy(skb))
113 		return -ETOOMANYREFS;
114 
115 	if (unlikely(p->len + len >= netif_get_gro_max_size(p->dev, p) ||
116 		     NAPI_GRO_CB(skb)->flush))
117 		return -E2BIG;
118 
119 	if (unlikely(p->len + len >= GRO_LEGACY_MAX_SIZE)) {
120 		if (NAPI_GRO_CB(skb)->proto != IPPROTO_TCP ||
121 		    p->encapsulation)
122 			return -E2BIG;
123 	}
124 
125 	segs = NAPI_GRO_CB(skb)->count;
126 	lp = NAPI_GRO_CB(p)->last;
127 	pinfo = skb_shinfo(lp);
128 
129 	if (headlen <= offset) {
130 		skb_frag_t *frag;
131 		skb_frag_t *frag2;
132 		int i = skbinfo->nr_frags;
133 		int nr_frags = pinfo->nr_frags + i;
134 
135 		if (nr_frags > MAX_SKB_FRAGS)
136 			goto merge;
137 
138 		offset -= headlen;
139 		pinfo->nr_frags = nr_frags;
140 		skbinfo->nr_frags = 0;
141 
142 		frag = pinfo->frags + nr_frags;
143 		frag2 = skbinfo->frags + i;
144 		do {
145 			*--frag = *--frag2;
146 		} while (--i);
147 
148 		skb_frag_off_add(frag, offset);
149 		skb_frag_size_sub(frag, offset);
150 
151 		/* all fragments truesize : remove (head size + sk_buff) */
152 		new_truesize = SKB_TRUESIZE(skb_end_offset(skb));
153 		delta_truesize = skb->truesize - new_truesize;
154 
155 		skb->truesize = new_truesize;
156 		skb->len -= skb->data_len;
157 		skb->data_len = 0;
158 
159 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
160 		goto done;
161 	} else if (skb->head_frag) {
162 		int nr_frags = pinfo->nr_frags;
163 		skb_frag_t *frag = pinfo->frags + nr_frags;
164 		struct page *page = virt_to_head_page(skb->head);
165 		unsigned int first_size = headlen - offset;
166 		unsigned int first_offset;
167 
168 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
169 			goto merge;
170 
171 		first_offset = skb->data -
172 			       (unsigned char *)page_address(page) +
173 			       offset;
174 
175 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
176 
177 		skb_frag_fill_page_desc(frag, page, first_offset, first_size);
178 
179 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
180 		/* We dont need to clear skbinfo->nr_frags here */
181 
182 		new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
183 		delta_truesize = skb->truesize - new_truesize;
184 		skb->truesize = new_truesize;
185 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
186 		goto done;
187 	}
188 
189 merge:
190 	/* sk ownership - if any - completely transferred to the aggregated packet */
191 	skb->destructor = NULL;
192 	skb->sk = NULL;
193 	delta_truesize = skb->truesize;
194 	if (offset > headlen) {
195 		unsigned int eat = offset - headlen;
196 
197 		skb_frag_off_add(&skbinfo->frags[0], eat);
198 		skb_frag_size_sub(&skbinfo->frags[0], eat);
199 		skb->data_len -= eat;
200 		skb->len -= eat;
201 		offset = headlen;
202 	}
203 
204 	__skb_pull(skb, offset);
205 
206 	if (NAPI_GRO_CB(p)->last == p)
207 		skb_shinfo(p)->frag_list = skb;
208 	else
209 		NAPI_GRO_CB(p)->last->next = skb;
210 	NAPI_GRO_CB(p)->last = skb;
211 	__skb_header_release(skb);
212 	lp = p;
213 
214 done:
215 	NAPI_GRO_CB(p)->count += segs;
216 	p->data_len += len;
217 	p->truesize += delta_truesize;
218 	p->len += len;
219 	skb_shinfo(p)->flags |= skbinfo->flags & SKBFL_SHARED_FRAG;
220 	if (lp != p) {
221 		lp->data_len += len;
222 		lp->truesize += delta_truesize;
223 		lp->len += len;
224 		skb_shinfo(lp)->flags |= skbinfo->flags & SKBFL_SHARED_FRAG;
225 	}
226 	NAPI_GRO_CB(skb)->same_flow = 1;
227 	return 0;
228 }
229 
230 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
231 {
232 	if (unlikely(p->len + skb->len >= 65536))
233 		return -E2BIG;
234 
235 	if (!pskb_may_pull(skb, skb_gro_offset(skb))) {
236 		NAPI_GRO_CB(skb)->flush = 1;
237 		return -ENOMEM;
238 	}
239 
240 	if (NAPI_GRO_CB(p)->last == p)
241 		skb_shinfo(p)->frag_list = skb;
242 	else
243 		NAPI_GRO_CB(p)->last->next = skb;
244 
245 	skb_pull(skb, skb_gro_offset(skb));
246 
247 	NAPI_GRO_CB(p)->last = skb;
248 	NAPI_GRO_CB(p)->count++;
249 	p->data_len += skb->len;
250 
251 	/* sk ownership - if any - completely transferred to the aggregated packet */
252 	skb->destructor = NULL;
253 	skb->sk = NULL;
254 	p->truesize += skb->truesize;
255 	p->len += skb->len;
256 
257 	skb_shinfo(p)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
258 
259 	NAPI_GRO_CB(skb)->same_flow = 1;
260 
261 	return 0;
262 }
263 
264 static void gro_complete(struct gro_node *gro, struct sk_buff *skb)
265 {
266 	struct list_head *head = &net_hotdata.offload_base;
267 	struct packet_offload *ptype;
268 	__be16 type = skb->protocol;
269 	int err = -ENOENT;
270 
271 	BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
272 
273 	if (NAPI_GRO_CB(skb)->count == 1) {
274 		skb_shinfo(skb)->gso_size = 0;
275 		goto out;
276 	}
277 
278 	/* NICs can feed encapsulated packets into GRO */
279 	skb->encapsulation = 0;
280 	rcu_read_lock();
281 	list_for_each_entry_rcu(ptype, head, list) {
282 		if (ptype->type != type || !ptype->callbacks.gro_complete)
283 			continue;
284 
285 		err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
286 					 ipv6_gro_complete, inet_gro_complete,
287 					 skb, 0);
288 		break;
289 	}
290 	rcu_read_unlock();
291 
292 	if (err) {
293 		WARN_ON(&ptype->list == head);
294 		kfree_skb(skb);
295 		return;
296 	}
297 
298 out:
299 	gro_normal_one(gro, skb, NAPI_GRO_CB(skb)->count);
300 }
301 
302 static void __gro_flush_chain(struct gro_node *gro, u32 index, bool flush_old)
303 {
304 	struct list_head *head = &gro->hash[index].list;
305 	struct sk_buff *skb, *p;
306 
307 	list_for_each_entry_safe_reverse(skb, p, head, list) {
308 		if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
309 			return;
310 		skb_list_del_init(skb);
311 		gro_complete(gro, skb);
312 		gro->hash[index].count--;
313 	}
314 
315 	if (!gro->hash[index].count)
316 		__clear_bit(index, &gro->bitmask);
317 }
318 
319 /*
320  * gro->hash[].list contains packets ordered by age.
321  * youngest packets at the head of it.
322  * Complete skbs in reverse order to reduce latencies.
323  */
324 void __gro_flush(struct gro_node *gro, bool flush_old)
325 {
326 	unsigned long bitmask = gro->bitmask;
327 	unsigned int i, base = ~0U;
328 
329 	while ((i = ffs(bitmask)) != 0) {
330 		bitmask >>= i;
331 		base += i;
332 		__gro_flush_chain(gro, base, flush_old);
333 	}
334 }
335 EXPORT_SYMBOL(__gro_flush);
336 
337 static unsigned long gro_list_prepare_tc_ext(const struct sk_buff *skb,
338 					     const struct sk_buff *p,
339 					     unsigned long diffs)
340 {
341 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
342 	struct tc_skb_ext *skb_ext;
343 	struct tc_skb_ext *p_ext;
344 
345 	skb_ext = skb_ext_find(skb, TC_SKB_EXT);
346 	p_ext = skb_ext_find(p, TC_SKB_EXT);
347 
348 	diffs |= (!!p_ext) ^ (!!skb_ext);
349 	if (!diffs && unlikely(skb_ext))
350 		diffs |= p_ext->chain ^ skb_ext->chain;
351 #endif
352 	return diffs;
353 }
354 
355 static void gro_list_prepare(const struct list_head *head,
356 			     const struct sk_buff *skb)
357 {
358 	unsigned int maclen = skb->dev->hard_header_len;
359 	u32 hash = skb_get_hash_raw(skb);
360 	struct sk_buff *p;
361 
362 	list_for_each_entry(p, head, list) {
363 		unsigned long diffs;
364 
365 		if (hash != skb_get_hash_raw(p)) {
366 			NAPI_GRO_CB(p)->same_flow = 0;
367 			continue;
368 		}
369 
370 		diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
371 		diffs |= p->vlan_all ^ skb->vlan_all;
372 		diffs |= skb_metadata_differs(p, skb);
373 		if (maclen == ETH_HLEN)
374 			diffs |= compare_ether_header(skb_mac_header(p),
375 						      skb_mac_header(skb));
376 		else if (!diffs)
377 			diffs = memcmp(skb_mac_header(p),
378 				       skb_mac_header(skb),
379 				       maclen);
380 
381 		/* in most common scenarios 'slow_gro' is 0
382 		 * otherwise we are already on some slower paths
383 		 * either skip all the infrequent tests altogether or
384 		 * avoid trying too hard to skip each of them individually
385 		 */
386 		if (!diffs && unlikely(skb->slow_gro | p->slow_gro)) {
387 			diffs |= p->sk != skb->sk;
388 			diffs |= skb_metadata_dst_cmp(p, skb);
389 			diffs |= skb_get_nfct(p) ^ skb_get_nfct(skb);
390 
391 			diffs |= gro_list_prepare_tc_ext(skb, p, diffs);
392 			diffs |= __psp_skb_coalesce_diff(skb, p, diffs);
393 		}
394 
395 		NAPI_GRO_CB(p)->same_flow = !diffs;
396 	}
397 }
398 
399 static inline void skb_gro_reset_offset(struct sk_buff *skb, u32 nhoff)
400 {
401 	const struct skb_shared_info *pinfo;
402 	const skb_frag_t *frag0;
403 	unsigned int headlen;
404 
405 	NAPI_GRO_CB(skb)->network_offset = 0;
406 	NAPI_GRO_CB(skb)->data_offset = 0;
407 	headlen = skb_headlen(skb);
408 	NAPI_GRO_CB(skb)->frag0 = skb->data;
409 	NAPI_GRO_CB(skb)->frag0_len = headlen;
410 	if (headlen)
411 		return;
412 
413 	pinfo = skb_shinfo(skb);
414 	frag0 = &pinfo->frags[0];
415 
416 	if (pinfo->nr_frags && skb_frag_page(frag0) &&
417 	    !PageHighMem(skb_frag_page(frag0)) &&
418 	    (!NET_IP_ALIGN || !((skb_frag_off(frag0) + nhoff) & 3))) {
419 		NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
420 		NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
421 						    skb_frag_size(frag0),
422 						    skb->end - skb->tail);
423 	}
424 }
425 
426 static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
427 {
428 	struct skb_shared_info *pinfo = skb_shinfo(skb);
429 
430 	DEBUG_NET_WARN_ON_ONCE(skb->end - skb->tail < grow);
431 
432 	memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
433 
434 	skb->data_len -= grow;
435 	skb->tail += grow;
436 
437 	skb_frag_off_add(&pinfo->frags[0], grow);
438 	skb_frag_size_sub(&pinfo->frags[0], grow);
439 
440 	if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
441 		skb_frag_unref(skb, 0);
442 		memmove(pinfo->frags, pinfo->frags + 1,
443 			--pinfo->nr_frags * sizeof(pinfo->frags[0]));
444 	}
445 }
446 
447 static void gro_try_pull_from_frag0(struct sk_buff *skb)
448 {
449 	int grow = skb_gro_offset(skb) - skb_headlen(skb);
450 
451 	if (grow > 0)
452 		gro_pull_from_frag0(skb, grow);
453 }
454 
455 static void gro_flush_oldest(struct gro_node *gro, struct list_head *head)
456 {
457 	struct sk_buff *oldest;
458 
459 	oldest = list_last_entry(head, struct sk_buff, list);
460 
461 	/* We are called with head length >= MAX_GRO_SKBS, so this is
462 	 * impossible.
463 	 */
464 	if (WARN_ON_ONCE(!oldest))
465 		return;
466 
467 	/* Do not adjust napi->gro_hash[].count, caller is adding a new
468 	 * SKB to the chain.
469 	 */
470 	skb_list_del_init(oldest);
471 	gro_complete(gro, oldest);
472 }
473 
474 static enum gro_result dev_gro_receive(struct gro_node *gro,
475 				       struct sk_buff *skb)
476 {
477 	u32 bucket = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
478 	struct list_head *head = &net_hotdata.offload_base;
479 	struct gro_list *gro_list = &gro->hash[bucket];
480 	struct packet_offload *ptype;
481 	__be16 type = skb->protocol;
482 	struct sk_buff *pp = NULL;
483 	enum gro_result ret;
484 	int same_flow;
485 
486 	if (netif_elide_gro(skb->dev))
487 		goto normal;
488 
489 	gro_list_prepare(&gro_list->list, skb);
490 
491 	rcu_read_lock();
492 	list_for_each_entry_rcu(ptype, head, list) {
493 		if (ptype->type == type && ptype->callbacks.gro_receive)
494 			goto found_ptype;
495 	}
496 	rcu_read_unlock();
497 	goto normal;
498 
499 found_ptype:
500 	skb_set_network_header(skb, skb_gro_offset(skb));
501 	skb_reset_mac_len(skb);
502 	BUILD_BUG_ON(sizeof_field(struct napi_gro_cb, zeroed) != sizeof(u32));
503 	BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct napi_gro_cb, zeroed),
504 					sizeof(u32))); /* Avoid slow unaligned acc */
505 	*(u32 *)&NAPI_GRO_CB(skb)->zeroed = 0;
506 	NAPI_GRO_CB(skb)->flush = skb_has_frag_list(skb);
507 	NAPI_GRO_CB(skb)->count = 1;
508 	if (unlikely(skb_is_gso(skb))) {
509 		NAPI_GRO_CB(skb)->count = skb_shinfo(skb)->gso_segs;
510 		/* Only support TCP and non DODGY users. */
511 		if (!skb_is_gso_tcp(skb) ||
512 		    (skb_shinfo(skb)->gso_type & SKB_GSO_DODGY))
513 			NAPI_GRO_CB(skb)->flush = 1;
514 	}
515 
516 	/* Setup for GRO checksum validation */
517 	switch (skb->ip_summed) {
518 	case CHECKSUM_COMPLETE:
519 		NAPI_GRO_CB(skb)->csum = skb->csum;
520 		NAPI_GRO_CB(skb)->csum_valid = 1;
521 		break;
522 	case CHECKSUM_UNNECESSARY:
523 		NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
524 		break;
525 	}
526 
527 	pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
528 				ipv6_gro_receive, inet_gro_receive,
529 				&gro_list->list, skb);
530 
531 	rcu_read_unlock();
532 
533 	if (PTR_ERR(pp) == -EINPROGRESS) {
534 		ret = GRO_CONSUMED;
535 		goto ok;
536 	}
537 
538 	same_flow = NAPI_GRO_CB(skb)->same_flow;
539 	ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
540 
541 	if (pp) {
542 		skb_list_del_init(pp);
543 		gro_complete(gro, pp);
544 		gro_list->count--;
545 	}
546 
547 	if (same_flow)
548 		goto ok;
549 
550 	if (NAPI_GRO_CB(skb)->flush)
551 		goto normal;
552 
553 	if (unlikely(gro_list->count >= MAX_GRO_SKBS))
554 		gro_flush_oldest(gro, &gro_list->list);
555 	else
556 		gro_list->count++;
557 
558 	/* Must be called before setting NAPI_GRO_CB(skb)->{age|last} */
559 	gro_try_pull_from_frag0(skb);
560 	NAPI_GRO_CB(skb)->age = jiffies;
561 	NAPI_GRO_CB(skb)->last = skb;
562 	if (!skb_is_gso(skb))
563 		skb_shinfo(skb)->gso_size = skb_gro_len(skb);
564 	list_add(&skb->list, &gro_list->list);
565 	ret = GRO_HELD;
566 ok:
567 	if (gro_list->count) {
568 		if (!test_bit(bucket, &gro->bitmask))
569 			__set_bit(bucket, &gro->bitmask);
570 	} else if (test_bit(bucket, &gro->bitmask)) {
571 		__clear_bit(bucket, &gro->bitmask);
572 	}
573 
574 	return ret;
575 
576 normal:
577 	ret = GRO_NORMAL;
578 	gro_try_pull_from_frag0(skb);
579 	goto ok;
580 }
581 
582 struct packet_offload *gro_find_receive_by_type(__be16 type)
583 {
584 	struct list_head *offload_head = &net_hotdata.offload_base;
585 	struct packet_offload *ptype;
586 
587 	list_for_each_entry_rcu(ptype, offload_head, list) {
588 		if (ptype->type != type || !ptype->callbacks.gro_receive)
589 			continue;
590 		return ptype;
591 	}
592 	return NULL;
593 }
594 EXPORT_SYMBOL(gro_find_receive_by_type);
595 
596 struct packet_offload *gro_find_complete_by_type(__be16 type)
597 {
598 	struct list_head *offload_head = &net_hotdata.offload_base;
599 	struct packet_offload *ptype;
600 
601 	list_for_each_entry_rcu(ptype, offload_head, list) {
602 		if (ptype->type != type || !ptype->callbacks.gro_complete)
603 			continue;
604 		return ptype;
605 	}
606 	return NULL;
607 }
608 EXPORT_SYMBOL(gro_find_complete_by_type);
609 
610 static gro_result_t gro_skb_finish(struct gro_node *gro, struct sk_buff *skb,
611 				   gro_result_t ret)
612 {
613 	switch (ret) {
614 	case GRO_NORMAL:
615 		gro_normal_one(gro, skb, 1);
616 		break;
617 
618 	case GRO_MERGED_FREE:
619 		if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
620 			napi_skb_free_stolen_head(skb);
621 		else if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
622 			__kfree_skb(skb);
623 		else
624 			__napi_kfree_skb(skb, SKB_CONSUMED);
625 		break;
626 
627 	case GRO_HELD:
628 	case GRO_MERGED:
629 	case GRO_CONSUMED:
630 		break;
631 	}
632 
633 	return ret;
634 }
635 
636 gro_result_t gro_receive_skb(struct gro_node *gro, struct sk_buff *skb)
637 {
638 	gro_result_t ret;
639 
640 	__skb_mark_napi_id(skb, gro);
641 	trace_napi_gro_receive_entry(skb);
642 
643 	skb_gro_reset_offset(skb, 0);
644 
645 	ret = gro_skb_finish(gro, skb, dev_gro_receive(gro, skb));
646 	trace_napi_gro_receive_exit(ret);
647 
648 	return ret;
649 }
650 EXPORT_SYMBOL(gro_receive_skb);
651 
652 static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
653 {
654 	struct skb_shared_info *shinfo;
655 
656 	if (unlikely(skb->pfmemalloc)) {
657 		consume_skb(skb);
658 		return;
659 	}
660 	__skb_pull(skb, skb_headlen(skb));
661 	/* restore the reserve we had after netdev_alloc_skb_ip_align() */
662 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
663 	__vlan_hwaccel_clear_tag(skb);
664 	skb->dev = napi->dev;
665 	skb->skb_iif = 0;
666 
667 	/* eth_type_trans() assumes pkt_type is PACKET_HOST */
668 	skb->pkt_type = PACKET_HOST;
669 
670 	skb->encapsulation = 0;
671 	skb->ip_summed = CHECKSUM_NONE;
672 
673 	shinfo = skb_shinfo(skb);
674 	shinfo->gso_type = 0;
675 	shinfo->gso_size = 0;
676 	shinfo->hwtstamps.hwtstamp = 0;
677 
678 	if (unlikely(skb->slow_gro)) {
679 		skb_orphan(skb);
680 		skb_ext_reset(skb);
681 		nf_reset_ct(skb);
682 		skb->slow_gro = 0;
683 	}
684 
685 	napi->skb = skb;
686 }
687 
688 struct sk_buff *napi_get_frags(struct napi_struct *napi)
689 {
690 	struct sk_buff *skb = napi->skb;
691 
692 	if (!skb) {
693 		skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
694 		if (skb) {
695 			napi->skb = skb;
696 			skb_mark_napi_id(skb, napi);
697 		}
698 	}
699 	return skb;
700 }
701 EXPORT_SYMBOL(napi_get_frags);
702 
703 static gro_result_t napi_frags_finish(struct napi_struct *napi,
704 				      struct sk_buff *skb,
705 				      gro_result_t ret)
706 {
707 	switch (ret) {
708 	case GRO_NORMAL:
709 	case GRO_HELD:
710 		__skb_push(skb, ETH_HLEN);
711 		skb->protocol = eth_type_trans(skb, skb->dev);
712 		if (ret == GRO_NORMAL)
713 			gro_normal_one(&napi->gro, skb, 1);
714 		break;
715 
716 	case GRO_MERGED_FREE:
717 		if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
718 			napi_skb_free_stolen_head(skb);
719 		else
720 			napi_reuse_skb(napi, skb);
721 		break;
722 
723 	case GRO_MERGED:
724 	case GRO_CONSUMED:
725 		break;
726 	}
727 
728 	return ret;
729 }
730 
731 /* Upper GRO stack assumes network header starts at gro_offset=0
732  * Drivers could call both napi_gro_frags() and napi_gro_receive()
733  * We copy ethernet header into skb->data to have a common layout.
734  */
735 static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
736 {
737 	struct sk_buff *skb = napi->skb;
738 	const struct ethhdr *eth;
739 	unsigned int hlen = sizeof(*eth);
740 
741 	napi->skb = NULL;
742 
743 	skb_reset_mac_header(skb);
744 	skb_gro_reset_offset(skb, hlen);
745 
746 	if (unlikely(!skb_gro_may_pull(skb, hlen))) {
747 		eth = skb_gro_header_slow(skb, hlen, 0);
748 		if (unlikely(!eth)) {
749 			net_warn_ratelimited("%s: dropping impossible skb from %s\n",
750 					     __func__, napi->dev->name);
751 			napi_reuse_skb(napi, skb);
752 			return NULL;
753 		}
754 	} else {
755 		eth = (const struct ethhdr *)skb->data;
756 
757 		if (NAPI_GRO_CB(skb)->frag0 != skb->data)
758 			gro_pull_from_frag0(skb, hlen);
759 
760 		NAPI_GRO_CB(skb)->frag0 += hlen;
761 		NAPI_GRO_CB(skb)->frag0_len -= hlen;
762 	}
763 	__skb_pull(skb, hlen);
764 
765 	/*
766 	 * This works because the only protocols we care about don't require
767 	 * special handling.
768 	 * We'll fix it up properly in napi_frags_finish()
769 	 */
770 	skb->protocol = eth->h_proto;
771 
772 	return skb;
773 }
774 
775 gro_result_t napi_gro_frags(struct napi_struct *napi)
776 {
777 	gro_result_t ret;
778 	struct sk_buff *skb = napi_frags_skb(napi);
779 
780 	trace_napi_gro_frags_entry(skb);
781 
782 	ret = napi_frags_finish(napi, skb, dev_gro_receive(&napi->gro, skb));
783 	trace_napi_gro_frags_exit(ret);
784 
785 	return ret;
786 }
787 EXPORT_SYMBOL(napi_gro_frags);
788 
789 /* Compute the checksum from gro_offset and return the folded value
790  * after adding in any pseudo checksum.
791  */
792 __sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
793 {
794 	__wsum wsum;
795 	__sum16 sum;
796 
797 	wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
798 
799 	/* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
800 	sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
801 	/* See comments in __skb_checksum_complete(). */
802 	if (likely(!sum)) {
803 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
804 		    !skb->csum_complete_sw)
805 			netdev_rx_csum_fault(skb->dev, skb);
806 	}
807 
808 	NAPI_GRO_CB(skb)->csum = wsum;
809 	NAPI_GRO_CB(skb)->csum_valid = 1;
810 
811 	return sum;
812 }
813 EXPORT_SYMBOL(__skb_gro_checksum_complete);
814 
815 void gro_init(struct gro_node *gro)
816 {
817 	for (u32 i = 0; i < GRO_HASH_BUCKETS; i++) {
818 		INIT_LIST_HEAD(&gro->hash[i].list);
819 		gro->hash[i].count = 0;
820 	}
821 
822 	gro->bitmask = 0;
823 	gro->cached_napi_id = 0;
824 
825 	INIT_LIST_HEAD(&gro->rx_list);
826 	gro->rx_count = 0;
827 }
828 
829 void gro_cleanup(struct gro_node *gro)
830 {
831 	struct sk_buff *skb, *n;
832 
833 	for (u32 i = 0; i < GRO_HASH_BUCKETS; i++) {
834 		list_for_each_entry_safe(skb, n, &gro->hash[i].list, list)
835 			kfree_skb(skb);
836 
837 		gro->hash[i].count = 0;
838 	}
839 
840 	gro->bitmask = 0;
841 	gro->cached_napi_id = 0;
842 
843 	list_for_each_entry_safe(skb, n, &gro->rx_list, list)
844 		kfree_skb(skb);
845 
846 	gro->rx_count = 0;
847 }
848