gro.c 20 KB

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