gro.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _NET_IPV6_GRO_H
  3. #define _NET_IPV6_GRO_H
  4. #include <linux/indirect_call_wrapper.h>
  5. #include <linux/ip.h>
  6. #include <linux/ipv6.h>
  7. #include <net/ip6_checksum.h>
  8. #include <linux/skbuff.h>
  9. #include <net/udp.h>
  10. struct napi_gro_cb {
  11. /* Virtual address of skb_shinfo(skb)->frags[0].page + offset. */
  12. void *frag0;
  13. /* Length of frag0. */
  14. unsigned int frag0_len;
  15. /* This indicates where we are processing relative to skb->data. */
  16. int data_offset;
  17. /* This is non-zero if the packet cannot be merged with the new skb. */
  18. u16 flush;
  19. /* Save the IP ID here and check when we get to the transport layer */
  20. u16 flush_id;
  21. /* Number of segments aggregated. */
  22. u16 count;
  23. /* Used in ipv6_gro_receive() and foo-over-udp */
  24. u16 proto;
  25. /* jiffies when first packet was created/queued */
  26. unsigned long age;
  27. /* Used in napi_gro_cb::free */
  28. #define NAPI_GRO_FREE 1
  29. #define NAPI_GRO_FREE_STOLEN_HEAD 2
  30. /* portion of the cb set to zero at every gro iteration */
  31. struct_group(zeroed,
  32. /* Start offset for remote checksum offload */
  33. u16 gro_remcsum_start;
  34. /* This is non-zero if the packet may be of the same flow. */
  35. u8 same_flow:1;
  36. /* Used in tunnel GRO receive */
  37. u8 encap_mark:1;
  38. /* GRO checksum is valid */
  39. u8 csum_valid:1;
  40. /* Number of checksums via CHECKSUM_UNNECESSARY */
  41. u8 csum_cnt:3;
  42. /* Free the skb? */
  43. u8 free:2;
  44. /* Used in foo-over-udp, set in udp[46]_gro_receive */
  45. u8 is_ipv6:1;
  46. /* Used in GRE, set in fou/gue_gro_receive */
  47. u8 is_fou:1;
  48. /* Used to determine if flush_id can be ignored */
  49. u8 is_atomic:1;
  50. /* Number of gro_receive callbacks this packet already went through */
  51. u8 recursion_counter:4;
  52. /* GRO is done by frag_list pointer chaining. */
  53. u8 is_flist:1;
  54. );
  55. /* used to support CHECKSUM_COMPLETE for tunneling protocols */
  56. __wsum csum;
  57. /* used in skb_gro_receive() slow path */
  58. struct sk_buff *last;
  59. };
  60. #define NAPI_GRO_CB(skb) ((struct napi_gro_cb *)(skb)->cb)
  61. #define GRO_RECURSION_LIMIT 15
  62. static inline int gro_recursion_inc_test(struct sk_buff *skb)
  63. {
  64. return ++NAPI_GRO_CB(skb)->recursion_counter == GRO_RECURSION_LIMIT;
  65. }
  66. typedef struct sk_buff *(*gro_receive_t)(struct list_head *, struct sk_buff *);
  67. static inline struct sk_buff *call_gro_receive(gro_receive_t cb,
  68. struct list_head *head,
  69. struct sk_buff *skb)
  70. {
  71. if (unlikely(gro_recursion_inc_test(skb))) {
  72. NAPI_GRO_CB(skb)->flush |= 1;
  73. return NULL;
  74. }
  75. return cb(head, skb);
  76. }
  77. typedef struct sk_buff *(*gro_receive_sk_t)(struct sock *, struct list_head *,
  78. struct sk_buff *);
  79. static inline struct sk_buff *call_gro_receive_sk(gro_receive_sk_t cb,
  80. struct sock *sk,
  81. struct list_head *head,
  82. struct sk_buff *skb)
  83. {
  84. if (unlikely(gro_recursion_inc_test(skb))) {
  85. NAPI_GRO_CB(skb)->flush |= 1;
  86. return NULL;
  87. }
  88. return cb(sk, head, skb);
  89. }
  90. static inline unsigned int skb_gro_offset(const struct sk_buff *skb)
  91. {
  92. return NAPI_GRO_CB(skb)->data_offset;
  93. }
  94. static inline unsigned int skb_gro_len(const struct sk_buff *skb)
  95. {
  96. return skb->len - NAPI_GRO_CB(skb)->data_offset;
  97. }
  98. static inline void skb_gro_pull(struct sk_buff *skb, unsigned int len)
  99. {
  100. NAPI_GRO_CB(skb)->data_offset += len;
  101. }
  102. static inline void *skb_gro_header_fast(struct sk_buff *skb,
  103. unsigned int offset)
  104. {
  105. return NAPI_GRO_CB(skb)->frag0 + offset;
  106. }
  107. static inline int skb_gro_header_hard(struct sk_buff *skb, unsigned int hlen)
  108. {
  109. return NAPI_GRO_CB(skb)->frag0_len < hlen;
  110. }
  111. static inline void skb_gro_frag0_invalidate(struct sk_buff *skb)
  112. {
  113. NAPI_GRO_CB(skb)->frag0 = NULL;
  114. NAPI_GRO_CB(skb)->frag0_len = 0;
  115. }
  116. static inline void *skb_gro_header_slow(struct sk_buff *skb, unsigned int hlen,
  117. unsigned int offset)
  118. {
  119. if (!pskb_may_pull(skb, hlen))
  120. return NULL;
  121. skb_gro_frag0_invalidate(skb);
  122. return skb->data + offset;
  123. }
  124. static inline void *skb_gro_header(struct sk_buff *skb,
  125. unsigned int hlen, unsigned int offset)
  126. {
  127. void *ptr;
  128. ptr = skb_gro_header_fast(skb, offset);
  129. if (skb_gro_header_hard(skb, hlen))
  130. ptr = skb_gro_header_slow(skb, hlen, offset);
  131. return ptr;
  132. }
  133. static inline void *skb_gro_network_header(struct sk_buff *skb)
  134. {
  135. return (NAPI_GRO_CB(skb)->frag0 ?: skb->data) +
  136. skb_network_offset(skb);
  137. }
  138. static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto)
  139. {
  140. const struct iphdr *iph = skb_gro_network_header(skb);
  141. return csum_tcpudp_nofold(iph->saddr, iph->daddr,
  142. skb_gro_len(skb), proto, 0);
  143. }
  144. static inline void skb_gro_postpull_rcsum(struct sk_buff *skb,
  145. const void *start, unsigned int len)
  146. {
  147. if (NAPI_GRO_CB(skb)->csum_valid)
  148. NAPI_GRO_CB(skb)->csum = wsum_negate(csum_partial(start, len,
  149. wsum_negate(NAPI_GRO_CB(skb)->csum)));
  150. }
  151. /* GRO checksum functions. These are logical equivalents of the normal
  152. * checksum functions (in skbuff.h) except that they operate on the GRO
  153. * offsets and fields in sk_buff.
  154. */
  155. __sum16 __skb_gro_checksum_complete(struct sk_buff *skb);
  156. static inline bool skb_at_gro_remcsum_start(struct sk_buff *skb)
  157. {
  158. return (NAPI_GRO_CB(skb)->gro_remcsum_start == skb_gro_offset(skb));
  159. }
  160. static inline bool __skb_gro_checksum_validate_needed(struct sk_buff *skb,
  161. bool zero_okay,
  162. __sum16 check)
  163. {
  164. return ((skb->ip_summed != CHECKSUM_PARTIAL ||
  165. skb_checksum_start_offset(skb) <
  166. skb_gro_offset(skb)) &&
  167. !skb_at_gro_remcsum_start(skb) &&
  168. NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  169. (!zero_okay || check));
  170. }
  171. static inline __sum16 __skb_gro_checksum_validate_complete(struct sk_buff *skb,
  172. __wsum psum)
  173. {
  174. if (NAPI_GRO_CB(skb)->csum_valid &&
  175. !csum_fold(csum_add(psum, NAPI_GRO_CB(skb)->csum)))
  176. return 0;
  177. NAPI_GRO_CB(skb)->csum = psum;
  178. return __skb_gro_checksum_complete(skb);
  179. }
  180. static inline void skb_gro_incr_csum_unnecessary(struct sk_buff *skb)
  181. {
  182. if (NAPI_GRO_CB(skb)->csum_cnt > 0) {
  183. /* Consume a checksum from CHECKSUM_UNNECESSARY */
  184. NAPI_GRO_CB(skb)->csum_cnt--;
  185. } else {
  186. /* Update skb for CHECKSUM_UNNECESSARY and csum_level when we
  187. * verified a new top level checksum or an encapsulated one
  188. * during GRO. This saves work if we fallback to normal path.
  189. */
  190. __skb_incr_checksum_unnecessary(skb);
  191. }
  192. }
  193. #define __skb_gro_checksum_validate(skb, proto, zero_okay, check, \
  194. compute_pseudo) \
  195. ({ \
  196. __sum16 __ret = 0; \
  197. if (__skb_gro_checksum_validate_needed(skb, zero_okay, check)) \
  198. __ret = __skb_gro_checksum_validate_complete(skb, \
  199. compute_pseudo(skb, proto)); \
  200. if (!__ret) \
  201. skb_gro_incr_csum_unnecessary(skb); \
  202. __ret; \
  203. })
  204. #define skb_gro_checksum_validate(skb, proto, compute_pseudo) \
  205. __skb_gro_checksum_validate(skb, proto, false, 0, compute_pseudo)
  206. #define skb_gro_checksum_validate_zero_check(skb, proto, check, \
  207. compute_pseudo) \
  208. __skb_gro_checksum_validate(skb, proto, true, check, compute_pseudo)
  209. #define skb_gro_checksum_simple_validate(skb) \
  210. __skb_gro_checksum_validate(skb, 0, false, 0, null_compute_pseudo)
  211. static inline bool __skb_gro_checksum_convert_check(struct sk_buff *skb)
  212. {
  213. return (NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  214. !NAPI_GRO_CB(skb)->csum_valid);
  215. }
  216. static inline void __skb_gro_checksum_convert(struct sk_buff *skb,
  217. __wsum pseudo)
  218. {
  219. NAPI_GRO_CB(skb)->csum = ~pseudo;
  220. NAPI_GRO_CB(skb)->csum_valid = 1;
  221. }
  222. #define skb_gro_checksum_try_convert(skb, proto, compute_pseudo) \
  223. do { \
  224. if (__skb_gro_checksum_convert_check(skb)) \
  225. __skb_gro_checksum_convert(skb, \
  226. compute_pseudo(skb, proto)); \
  227. } while (0)
  228. struct gro_remcsum {
  229. int offset;
  230. __wsum delta;
  231. };
  232. static inline void skb_gro_remcsum_init(struct gro_remcsum *grc)
  233. {
  234. grc->offset = 0;
  235. grc->delta = 0;
  236. }
  237. static inline void *skb_gro_remcsum_process(struct sk_buff *skb, void *ptr,
  238. unsigned int off, size_t hdrlen,
  239. int start, int offset,
  240. struct gro_remcsum *grc,
  241. bool nopartial)
  242. {
  243. __wsum delta;
  244. size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start);
  245. BUG_ON(!NAPI_GRO_CB(skb)->csum_valid);
  246. if (!nopartial) {
  247. NAPI_GRO_CB(skb)->gro_remcsum_start = off + hdrlen + start;
  248. return ptr;
  249. }
  250. ptr = skb_gro_header(skb, off + plen, off);
  251. if (!ptr)
  252. return NULL;
  253. delta = remcsum_adjust(ptr + hdrlen, NAPI_GRO_CB(skb)->csum,
  254. start, offset);
  255. /* Adjust skb->csum since we changed the packet */
  256. NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
  257. grc->offset = off + hdrlen + offset;
  258. grc->delta = delta;
  259. return ptr;
  260. }
  261. static inline void skb_gro_remcsum_cleanup(struct sk_buff *skb,
  262. struct gro_remcsum *grc)
  263. {
  264. void *ptr;
  265. size_t plen = grc->offset + sizeof(u16);
  266. if (!grc->delta)
  267. return;
  268. ptr = skb_gro_header(skb, plen, grc->offset);
  269. if (!ptr)
  270. return;
  271. remcsum_unadjust((__sum16 *)ptr, grc->delta);
  272. }
  273. #ifdef CONFIG_XFRM_OFFLOAD
  274. static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush)
  275. {
  276. if (PTR_ERR(pp) != -EINPROGRESS)
  277. NAPI_GRO_CB(skb)->flush |= flush;
  278. }
  279. static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb,
  280. struct sk_buff *pp,
  281. int flush,
  282. struct gro_remcsum *grc)
  283. {
  284. if (PTR_ERR(pp) != -EINPROGRESS) {
  285. NAPI_GRO_CB(skb)->flush |= flush;
  286. skb_gro_remcsum_cleanup(skb, grc);
  287. skb->remcsum_offload = 0;
  288. }
  289. }
  290. #else
  291. static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush)
  292. {
  293. NAPI_GRO_CB(skb)->flush |= flush;
  294. }
  295. static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb,
  296. struct sk_buff *pp,
  297. int flush,
  298. struct gro_remcsum *grc)
  299. {
  300. NAPI_GRO_CB(skb)->flush |= flush;
  301. skb_gro_remcsum_cleanup(skb, grc);
  302. skb->remcsum_offload = 0;
  303. }
  304. #endif
  305. INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
  306. struct sk_buff *));
  307. INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
  308. INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
  309. struct sk_buff *));
  310. INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
  311. INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp4_gro_receive(struct list_head *,
  312. struct sk_buff *));
  313. INDIRECT_CALLABLE_DECLARE(int udp4_gro_complete(struct sk_buff *, int));
  314. INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp6_gro_receive(struct list_head *,
  315. struct sk_buff *));
  316. INDIRECT_CALLABLE_DECLARE(int udp6_gro_complete(struct sk_buff *, int));
  317. #define indirect_call_gro_receive_inet(cb, f2, f1, head, skb) \
  318. ({ \
  319. unlikely(gro_recursion_inc_test(skb)) ? \
  320. NAPI_GRO_CB(skb)->flush |= 1, NULL : \
  321. INDIRECT_CALL_INET(cb, f2, f1, head, skb); \
  322. })
  323. struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
  324. struct udphdr *uh, struct sock *sk);
  325. int udp_gro_complete(struct sk_buff *skb, int nhoff, udp_lookup_t lookup);
  326. static inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb)
  327. {
  328. struct udphdr *uh;
  329. unsigned int hlen, off;
  330. off = skb_gro_offset(skb);
  331. hlen = off + sizeof(*uh);
  332. uh = skb_gro_header(skb, hlen, off);
  333. return uh;
  334. }
  335. static inline __wsum ip6_gro_compute_pseudo(struct sk_buff *skb, int proto)
  336. {
  337. const struct ipv6hdr *iph = skb_gro_network_header(skb);
  338. return ~csum_unfold(csum_ipv6_magic(&iph->saddr, &iph->daddr,
  339. skb_gro_len(skb), proto, 0));
  340. }
  341. int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb);
  342. /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
  343. static inline void gro_normal_list(struct napi_struct *napi)
  344. {
  345. if (!napi->rx_count)
  346. return;
  347. netif_receive_skb_list_internal(&napi->rx_list);
  348. INIT_LIST_HEAD(&napi->rx_list);
  349. napi->rx_count = 0;
  350. }
  351. /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
  352. * pass the whole batch up to the stack.
  353. */
  354. static inline void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb, int segs)
  355. {
  356. list_add_tail(&skb->list, &napi->rx_list);
  357. napi->rx_count += segs;
  358. if (napi->rx_count >= READ_ONCE(gro_normal_batch))
  359. gro_normal_list(napi);
  360. }
  361. #endif /* _NET_IPV6_GRO_H */