1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <net/dst_metadata.h>
4 #include <net/busy_poll.h>
5 #include <trace/events/net.h>
6 #include <linux/skbuff_ref.h>
10 /* This should be increased if a protocol with a bigger head is added. */
11 #define GRO_MAX_HEAD (MAX_HEADER + 128)
13 static DEFINE_SPINLOCK(offload_lock);
16 * dev_add_offload - register offload handlers
17 * @po: protocol offload declaration
19 * Add protocol offload handlers to the networking stack. The passed
20 * &proto_offload is linked into kernel lists and may not be freed until
21 * it has been removed from the kernel lists.
23 * This call does not sleep therefore it can not
24 * guarantee all CPU's that are in middle of receiving packets
25 * will see the new offload handlers (until the next received packet).
27 void dev_add_offload(struct packet_offload *po)
29 struct packet_offload *elem;
31 spin_lock(&offload_lock);
32 list_for_each_entry(elem, &net_hotdata.offload_base, list) {
33 if (po->priority < elem->priority)
36 list_add_rcu(&po->list, elem->list.prev);
37 spin_unlock(&offload_lock);
39 EXPORT_SYMBOL(dev_add_offload);
42 * __dev_remove_offload - remove offload handler
43 * @po: packet offload declaration
45 * Remove a protocol offload handler that was previously added to the
46 * kernel offload handlers by dev_add_offload(). The passed &offload_type
47 * is removed from the kernel lists and can be freed or reused once this
50 * The packet type might still be in use by receivers
51 * and must not be freed until after all the CPU's have gone
52 * through a quiescent state.
54 static void __dev_remove_offload(struct packet_offload *po)
56 struct list_head *head = &net_hotdata.offload_base;
57 struct packet_offload *po1;
59 spin_lock(&offload_lock);
61 list_for_each_entry(po1, head, list) {
63 list_del_rcu(&po->list);
68 pr_warn("dev_remove_offload: %p not found\n", po);
70 spin_unlock(&offload_lock);
74 * dev_remove_offload - remove packet offload handler
75 * @po: packet offload declaration
77 * Remove a packet offload handler that was previously added to the kernel
78 * offload handlers by dev_add_offload(). The passed &offload_type is
79 * removed from the kernel lists and can be freed or reused once this
82 * This call sleeps to guarantee that no CPU is looking at the packet
85 void dev_remove_offload(struct packet_offload *po)
87 __dev_remove_offload(po);
91 EXPORT_SYMBOL(dev_remove_offload);
94 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
96 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
97 unsigned int offset = skb_gro_offset(skb);
98 unsigned int headlen = skb_headlen(skb);
99 unsigned int len = skb_gro_len(skb);
100 unsigned int delta_truesize;
101 unsigned int gro_max_size;
102 unsigned int new_truesize;
106 /* Do not splice page pool based packets w/ non-page pool
107 * packets. This can result in reference count issues as page
108 * pool pages will not decrement the reference count and will
109 * instead be immediately returned to the pool or have frag
112 if (p->pp_recycle != skb->pp_recycle)
113 return -ETOOMANYREFS;
115 /* pairs with WRITE_ONCE() in netif_set_gro(_ipv4)_max_size() */
116 gro_max_size = p->protocol == htons(ETH_P_IPV6) ?
117 READ_ONCE(p->dev->gro_max_size) :
118 READ_ONCE(p->dev->gro_ipv4_max_size);
120 if (unlikely(p->len + len >= gro_max_size || NAPI_GRO_CB(skb)->flush))
123 if (unlikely(p->len + len >= GRO_LEGACY_MAX_SIZE)) {
124 if (NAPI_GRO_CB(skb)->proto != IPPROTO_TCP ||
125 (p->protocol == htons(ETH_P_IPV6) &&
126 skb_headroom(p) < sizeof(struct hop_jumbo_hdr)) ||
131 segs = NAPI_GRO_CB(skb)->count;
132 lp = NAPI_GRO_CB(p)->last;
133 pinfo = skb_shinfo(lp);
135 if (headlen <= offset) {
138 int i = skbinfo->nr_frags;
139 int nr_frags = pinfo->nr_frags + i;
141 if (nr_frags > MAX_SKB_FRAGS)
145 pinfo->nr_frags = nr_frags;
146 skbinfo->nr_frags = 0;
148 frag = pinfo->frags + nr_frags;
149 frag2 = skbinfo->frags + i;
154 skb_frag_off_add(frag, offset);
155 skb_frag_size_sub(frag, offset);
157 /* all fragments truesize : remove (head size + sk_buff) */
158 new_truesize = SKB_TRUESIZE(skb_end_offset(skb));
159 delta_truesize = skb->truesize - new_truesize;
161 skb->truesize = new_truesize;
162 skb->len -= skb->data_len;
165 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
167 } else if (skb->head_frag) {
168 int nr_frags = pinfo->nr_frags;
169 skb_frag_t *frag = pinfo->frags + nr_frags;
170 struct page *page = virt_to_head_page(skb->head);
171 unsigned int first_size = headlen - offset;
172 unsigned int first_offset;
174 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
177 first_offset = skb->data -
178 (unsigned char *)page_address(page) +
181 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
183 skb_frag_fill_page_desc(frag, page, first_offset, first_size);
185 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
186 /* We dont need to clear skbinfo->nr_frags here */
188 new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
189 delta_truesize = skb->truesize - new_truesize;
190 skb->truesize = new_truesize;
191 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
196 /* sk ownership - if any - completely transferred to the aggregated packet */
197 skb->destructor = NULL;
199 delta_truesize = skb->truesize;
200 if (offset > headlen) {
201 unsigned int eat = offset - headlen;
203 skb_frag_off_add(&skbinfo->frags[0], eat);
204 skb_frag_size_sub(&skbinfo->frags[0], eat);
205 skb->data_len -= eat;
210 __skb_pull(skb, offset);
212 if (NAPI_GRO_CB(p)->last == p)
213 skb_shinfo(p)->frag_list = skb;
215 NAPI_GRO_CB(p)->last->next = skb;
216 NAPI_GRO_CB(p)->last = skb;
217 __skb_header_release(skb);
221 NAPI_GRO_CB(p)->count += segs;
223 p->truesize += delta_truesize;
227 lp->truesize += delta_truesize;
230 NAPI_GRO_CB(skb)->same_flow = 1;
234 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
236 if (unlikely(p->len + skb->len >= 65536))
239 if (NAPI_GRO_CB(p)->last == p)
240 skb_shinfo(p)->frag_list = skb;
242 NAPI_GRO_CB(p)->last->next = skb;
244 skb_pull(skb, skb_gro_offset(skb));
246 NAPI_GRO_CB(p)->last = skb;
247 NAPI_GRO_CB(p)->count++;
248 p->data_len += skb->len;
250 /* sk ownership - if any - completely transferred to the aggregated packet */
251 skb->destructor = NULL;
253 p->truesize += skb->truesize;
256 NAPI_GRO_CB(skb)->same_flow = 1;
262 static void napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
264 struct list_head *head = &net_hotdata.offload_base;
265 struct packet_offload *ptype;
266 __be16 type = skb->protocol;
269 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
271 if (NAPI_GRO_CB(skb)->count == 1) {
272 skb_shinfo(skb)->gso_size = 0;
277 list_for_each_entry_rcu(ptype, head, list) {
278 if (ptype->type != type || !ptype->callbacks.gro_complete)
281 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
282 ipv6_gro_complete, inet_gro_complete,
289 WARN_ON(&ptype->list == head);
295 gro_normal_one(napi, skb, NAPI_GRO_CB(skb)->count);
298 static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
301 struct list_head *head = &napi->gro_hash[index].list;
302 struct sk_buff *skb, *p;
304 list_for_each_entry_safe_reverse(skb, p, head, list) {
305 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
307 skb_list_del_init(skb);
308 napi_gro_complete(napi, skb);
309 napi->gro_hash[index].count--;
312 if (!napi->gro_hash[index].count)
313 __clear_bit(index, &napi->gro_bitmask);
316 /* napi->gro_hash[].list contains packets ordered by age.
317 * youngest packets at the head of it.
318 * Complete skbs in reverse order to reduce latencies.
320 void napi_gro_flush(struct napi_struct *napi, bool flush_old)
322 unsigned long bitmask = napi->gro_bitmask;
323 unsigned int i, base = ~0U;
325 while ((i = ffs(bitmask)) != 0) {
328 __napi_gro_flush_chain(napi, base, flush_old);
331 EXPORT_SYMBOL(napi_gro_flush);
333 static unsigned long gro_list_prepare_tc_ext(const struct sk_buff *skb,
334 const struct sk_buff *p,
337 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
338 struct tc_skb_ext *skb_ext;
339 struct tc_skb_ext *p_ext;
341 skb_ext = skb_ext_find(skb, TC_SKB_EXT);
342 p_ext = skb_ext_find(p, TC_SKB_EXT);
344 diffs |= (!!p_ext) ^ (!!skb_ext);
345 if (!diffs && unlikely(skb_ext))
346 diffs |= p_ext->chain ^ skb_ext->chain;
351 static void gro_list_prepare(const struct list_head *head,
352 const struct sk_buff *skb)
354 unsigned int maclen = skb->dev->hard_header_len;
355 u32 hash = skb_get_hash_raw(skb);
358 list_for_each_entry(p, head, list) {
361 if (hash != skb_get_hash_raw(p)) {
362 NAPI_GRO_CB(p)->same_flow = 0;
366 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
367 diffs |= p->vlan_all ^ skb->vlan_all;
368 diffs |= skb_metadata_differs(p, skb);
369 if (maclen == ETH_HLEN)
370 diffs |= compare_ether_header(skb_mac_header(p),
371 skb_mac_header(skb));
373 diffs = memcmp(skb_mac_header(p),
377 /* in most common scenarions 'slow_gro' is 0
378 * otherwise we are already on some slower paths
379 * either skip all the infrequent tests altogether or
380 * avoid trying too hard to skip each of them individually
382 if (!diffs && unlikely(skb->slow_gro | p->slow_gro)) {
383 diffs |= p->sk != skb->sk;
384 diffs |= skb_metadata_dst_cmp(p, skb);
385 diffs |= skb_get_nfct(p) ^ skb_get_nfct(skb);
387 diffs |= gro_list_prepare_tc_ext(skb, p, diffs);
390 NAPI_GRO_CB(p)->same_flow = !diffs;
394 static inline void skb_gro_reset_offset(struct sk_buff *skb, u32 nhoff)
396 const struct skb_shared_info *pinfo;
397 const skb_frag_t *frag0;
398 unsigned int headlen;
400 NAPI_GRO_CB(skb)->network_offset = 0;
401 NAPI_GRO_CB(skb)->data_offset = 0;
402 headlen = skb_headlen(skb);
403 NAPI_GRO_CB(skb)->frag0 = skb->data;
404 NAPI_GRO_CB(skb)->frag0_len = headlen;
408 pinfo = skb_shinfo(skb);
409 frag0 = &pinfo->frags[0];
411 if (pinfo->nr_frags && !PageHighMem(skb_frag_page(frag0)) &&
412 (!NET_IP_ALIGN || !((skb_frag_off(frag0) + nhoff) & 3))) {
413 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
414 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
415 skb_frag_size(frag0),
416 skb->end - skb->tail);
420 static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
422 struct skb_shared_info *pinfo = skb_shinfo(skb);
424 BUG_ON(skb->end - skb->tail < grow);
426 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
428 skb->data_len -= grow;
431 skb_frag_off_add(&pinfo->frags[0], grow);
432 skb_frag_size_sub(&pinfo->frags[0], grow);
434 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
435 skb_frag_unref(skb, 0);
436 memmove(pinfo->frags, pinfo->frags + 1,
437 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
441 static void gro_try_pull_from_frag0(struct sk_buff *skb)
443 int grow = skb_gro_offset(skb) - skb_headlen(skb);
446 gro_pull_from_frag0(skb, grow);
449 static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
451 struct sk_buff *oldest;
453 oldest = list_last_entry(head, struct sk_buff, list);
455 /* We are called with head length >= MAX_GRO_SKBS, so this is
458 if (WARN_ON_ONCE(!oldest))
461 /* Do not adjust napi->gro_hash[].count, caller is adding a new
464 skb_list_del_init(oldest);
465 napi_gro_complete(napi, oldest);
468 static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
470 u32 bucket = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
471 struct gro_list *gro_list = &napi->gro_hash[bucket];
472 struct list_head *head = &net_hotdata.offload_base;
473 struct packet_offload *ptype;
474 __be16 type = skb->protocol;
475 struct sk_buff *pp = NULL;
479 if (netif_elide_gro(skb->dev))
482 gro_list_prepare(&gro_list->list, skb);
485 list_for_each_entry_rcu(ptype, head, list) {
486 if (ptype->type == type && ptype->callbacks.gro_receive)
493 skb_set_network_header(skb, skb_gro_offset(skb));
494 skb_reset_mac_len(skb);
495 BUILD_BUG_ON(sizeof_field(struct napi_gro_cb, zeroed) != sizeof(u32));
496 BUILD_BUG_ON(!IS_ALIGNED(offsetof(struct napi_gro_cb, zeroed),
497 sizeof(u32))); /* Avoid slow unaligned acc */
498 *(u32 *)&NAPI_GRO_CB(skb)->zeroed = 0;
499 NAPI_GRO_CB(skb)->flush = skb_has_frag_list(skb);
500 NAPI_GRO_CB(skb)->count = 1;
501 if (unlikely(skb_is_gso(skb))) {
502 NAPI_GRO_CB(skb)->count = skb_shinfo(skb)->gso_segs;
503 /* Only support TCP and non DODGY users. */
504 if (!skb_is_gso_tcp(skb) ||
505 (skb_shinfo(skb)->gso_type & SKB_GSO_DODGY))
506 NAPI_GRO_CB(skb)->flush = 1;
509 /* Setup for GRO checksum validation */
510 switch (skb->ip_summed) {
511 case CHECKSUM_COMPLETE:
512 NAPI_GRO_CB(skb)->csum = skb->csum;
513 NAPI_GRO_CB(skb)->csum_valid = 1;
515 case CHECKSUM_UNNECESSARY:
516 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
520 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
521 ipv6_gro_receive, inet_gro_receive,
522 &gro_list->list, skb);
526 if (PTR_ERR(pp) == -EINPROGRESS) {
531 same_flow = NAPI_GRO_CB(skb)->same_flow;
532 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
535 skb_list_del_init(pp);
536 napi_gro_complete(napi, pp);
543 if (NAPI_GRO_CB(skb)->flush)
546 if (unlikely(gro_list->count >= MAX_GRO_SKBS))
547 gro_flush_oldest(napi, &gro_list->list);
551 /* Must be called before setting NAPI_GRO_CB(skb)->{age|last} */
552 gro_try_pull_from_frag0(skb);
553 NAPI_GRO_CB(skb)->age = jiffies;
554 NAPI_GRO_CB(skb)->last = skb;
555 if (!skb_is_gso(skb))
556 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
557 list_add(&skb->list, &gro_list->list);
560 if (gro_list->count) {
561 if (!test_bit(bucket, &napi->gro_bitmask))
562 __set_bit(bucket, &napi->gro_bitmask);
563 } else if (test_bit(bucket, &napi->gro_bitmask)) {
564 __clear_bit(bucket, &napi->gro_bitmask);
571 gro_try_pull_from_frag0(skb);
575 struct packet_offload *gro_find_receive_by_type(__be16 type)
577 struct list_head *offload_head = &net_hotdata.offload_base;
578 struct packet_offload *ptype;
580 list_for_each_entry_rcu(ptype, offload_head, list) {
581 if (ptype->type != type || !ptype->callbacks.gro_receive)
587 EXPORT_SYMBOL(gro_find_receive_by_type);
589 struct packet_offload *gro_find_complete_by_type(__be16 type)
591 struct list_head *offload_head = &net_hotdata.offload_base;
592 struct packet_offload *ptype;
594 list_for_each_entry_rcu(ptype, offload_head, list) {
595 if (ptype->type != type || !ptype->callbacks.gro_complete)
601 EXPORT_SYMBOL(gro_find_complete_by_type);
603 static gro_result_t napi_skb_finish(struct napi_struct *napi,
609 gro_normal_one(napi, skb, 1);
612 case GRO_MERGED_FREE:
613 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
614 napi_skb_free_stolen_head(skb);
615 else if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
618 __napi_kfree_skb(skb, SKB_CONSUMED);
630 gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
634 skb_mark_napi_id(skb, napi);
635 trace_napi_gro_receive_entry(skb);
637 skb_gro_reset_offset(skb, 0);
639 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
640 trace_napi_gro_receive_exit(ret);
644 EXPORT_SYMBOL(napi_gro_receive);
646 static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
648 if (unlikely(skb->pfmemalloc)) {
652 __skb_pull(skb, skb_headlen(skb));
653 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
654 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
655 __vlan_hwaccel_clear_tag(skb);
656 skb->dev = napi->dev;
659 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
660 skb->pkt_type = PACKET_HOST;
662 skb->encapsulation = 0;
663 skb_shinfo(skb)->gso_type = 0;
664 skb_shinfo(skb)->gso_size = 0;
665 if (unlikely(skb->slow_gro)) {
675 struct sk_buff *napi_get_frags(struct napi_struct *napi)
677 struct sk_buff *skb = napi->skb;
680 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
683 skb_mark_napi_id(skb, napi);
688 EXPORT_SYMBOL(napi_get_frags);
690 static gro_result_t napi_frags_finish(struct napi_struct *napi,
697 __skb_push(skb, ETH_HLEN);
698 skb->protocol = eth_type_trans(skb, skb->dev);
699 if (ret == GRO_NORMAL)
700 gro_normal_one(napi, skb, 1);
703 case GRO_MERGED_FREE:
704 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
705 napi_skb_free_stolen_head(skb);
707 napi_reuse_skb(napi, skb);
718 /* Upper GRO stack assumes network header starts at gro_offset=0
719 * Drivers could call both napi_gro_frags() and napi_gro_receive()
720 * We copy ethernet header into skb->data to have a common layout.
722 static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
724 struct sk_buff *skb = napi->skb;
725 const struct ethhdr *eth;
726 unsigned int hlen = sizeof(*eth);
730 skb_reset_mac_header(skb);
731 skb_gro_reset_offset(skb, hlen);
733 if (unlikely(!skb_gro_may_pull(skb, hlen))) {
734 eth = skb_gro_header_slow(skb, hlen, 0);
735 if (unlikely(!eth)) {
736 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
737 __func__, napi->dev->name);
738 napi_reuse_skb(napi, skb);
742 eth = (const struct ethhdr *)skb->data;
744 if (NAPI_GRO_CB(skb)->frag0 != skb->data)
745 gro_pull_from_frag0(skb, hlen);
747 NAPI_GRO_CB(skb)->frag0 += hlen;
748 NAPI_GRO_CB(skb)->frag0_len -= hlen;
750 __skb_pull(skb, hlen);
753 * This works because the only protocols we care about don't require
755 * We'll fix it up properly in napi_frags_finish()
757 skb->protocol = eth->h_proto;
762 gro_result_t napi_gro_frags(struct napi_struct *napi)
765 struct sk_buff *skb = napi_frags_skb(napi);
767 trace_napi_gro_frags_entry(skb);
769 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
770 trace_napi_gro_frags_exit(ret);
774 EXPORT_SYMBOL(napi_gro_frags);
776 /* Compute the checksum from gro_offset and return the folded value
777 * after adding in any pseudo checksum.
779 __sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
784 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
786 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
787 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
788 /* See comments in __skb_checksum_complete(). */
790 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
791 !skb->csum_complete_sw)
792 netdev_rx_csum_fault(skb->dev, skb);
795 NAPI_GRO_CB(skb)->csum = wsum;
796 NAPI_GRO_CB(skb)->csum_valid = 1;
800 EXPORT_SYMBOL(__skb_gro_checksum_complete);