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