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