gro.c 19 KB

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