gro.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _NET_GRO_H
  3. #define _NET_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. #include <net/hotdata.h>
  11. /* This should be increased if a protocol with a bigger head is added. */
  12. #define GRO_MAX_HEAD (MAX_HEADER + 128)
  13. struct napi_gro_cb {
  14. union {
  15. struct {
  16. /* Virtual address of skb_shinfo(skb)->frags[0].page + offset. */
  17. void *frag0;
  18. /* Length of frag0. */
  19. unsigned int frag0_len;
  20. };
  21. struct {
  22. /* used in skb_gro_receive() slow path */
  23. struct sk_buff *last;
  24. /* jiffies when first packet was created/queued */
  25. unsigned long age;
  26. };
  27. };
  28. /* This indicates where we are processing relative to skb->data. */
  29. int data_offset;
  30. /* This is non-zero if the packet cannot be merged with the new skb. */
  31. u16 flush;
  32. /* Number of segments aggregated. */
  33. u16 count;
  34. /* Used in ipv6_gro_receive() and foo-over-udp and esp-in-udp */
  35. u16 proto;
  36. u16 pad;
  37. /* Used in napi_gro_cb::free */
  38. #define NAPI_GRO_FREE 1
  39. #define NAPI_GRO_FREE_STOLEN_HEAD 2
  40. /* portion of the cb set to zero at every gro iteration */
  41. struct_group(zeroed,
  42. /* Start offset for remote checksum offload */
  43. u16 gro_remcsum_start;
  44. /* This is non-zero if the packet may be of the same flow. */
  45. u8 same_flow:1;
  46. /* Used in tunnel GRO receive */
  47. u8 encap_mark:1;
  48. /* GRO checksum is valid */
  49. u8 csum_valid:1;
  50. /* Number of checksums via CHECKSUM_UNNECESSARY */
  51. u8 csum_cnt:3;
  52. /* Free the skb? */
  53. u8 free:2;
  54. /* Used in foo-over-udp, set in udp[46]_gro_receive */
  55. u8 is_ipv6:1;
  56. /* Used in GRE, set in fou/gue_gro_receive */
  57. u8 is_fou:1;
  58. /* Used to determine if ipid_offset can be ignored */
  59. u8 ip_fixedid:1;
  60. /* Number of gro_receive callbacks this packet already went through */
  61. u8 recursion_counter:4;
  62. /* GRO is done by frag_list pointer chaining. */
  63. u8 is_flist:1;
  64. );
  65. /* used to support CHECKSUM_COMPLETE for tunneling protocols */
  66. __wsum csum;
  67. /* L3 offsets */
  68. union {
  69. struct {
  70. u16 network_offset;
  71. u16 inner_network_offset;
  72. };
  73. u16 network_offsets[2];
  74. };
  75. };
  76. #define NAPI_GRO_CB(skb) ((struct napi_gro_cb *)(skb)->cb)
  77. #define GRO_RECURSION_LIMIT 15
  78. static inline int gro_recursion_inc_test(struct sk_buff *skb)
  79. {
  80. return ++NAPI_GRO_CB(skb)->recursion_counter == GRO_RECURSION_LIMIT;
  81. }
  82. typedef struct sk_buff *(*gro_receive_t)(struct list_head *, struct sk_buff *);
  83. static inline struct sk_buff *call_gro_receive(gro_receive_t cb,
  84. struct list_head *head,
  85. struct sk_buff *skb)
  86. {
  87. if (unlikely(gro_recursion_inc_test(skb))) {
  88. NAPI_GRO_CB(skb)->flush |= 1;
  89. return NULL;
  90. }
  91. return cb(head, skb);
  92. }
  93. typedef struct sk_buff *(*gro_receive_sk_t)(struct sock *, struct list_head *,
  94. struct sk_buff *);
  95. static inline struct sk_buff *call_gro_receive_sk(gro_receive_sk_t cb,
  96. struct sock *sk,
  97. struct list_head *head,
  98. struct sk_buff *skb)
  99. {
  100. if (unlikely(gro_recursion_inc_test(skb))) {
  101. NAPI_GRO_CB(skb)->flush |= 1;
  102. return NULL;
  103. }
  104. return cb(sk, head, skb);
  105. }
  106. static inline unsigned int skb_gro_offset(const struct sk_buff *skb)
  107. {
  108. return NAPI_GRO_CB(skb)->data_offset;
  109. }
  110. static inline unsigned int skb_gro_len(const struct sk_buff *skb)
  111. {
  112. return skb->len - NAPI_GRO_CB(skb)->data_offset;
  113. }
  114. static inline void skb_gro_pull(struct sk_buff *skb, unsigned int len)
  115. {
  116. NAPI_GRO_CB(skb)->data_offset += len;
  117. }
  118. static inline void *skb_gro_header_fast(const struct sk_buff *skb,
  119. unsigned int offset)
  120. {
  121. return NAPI_GRO_CB(skb)->frag0 + offset;
  122. }
  123. static inline bool skb_gro_may_pull(const struct sk_buff *skb,
  124. unsigned int hlen)
  125. {
  126. return likely(hlen <= NAPI_GRO_CB(skb)->frag0_len);
  127. }
  128. static inline void *skb_gro_header_slow(struct sk_buff *skb, unsigned int hlen,
  129. unsigned int offset)
  130. {
  131. if (!pskb_may_pull(skb, hlen))
  132. return NULL;
  133. return skb->data + offset;
  134. }
  135. static inline void *skb_gro_header(struct sk_buff *skb, unsigned int hlen,
  136. unsigned int offset)
  137. {
  138. void *ptr;
  139. ptr = skb_gro_header_fast(skb, offset);
  140. if (!skb_gro_may_pull(skb, hlen))
  141. ptr = skb_gro_header_slow(skb, hlen, offset);
  142. return ptr;
  143. }
  144. static inline int skb_gro_receive_network_offset(const struct sk_buff *skb)
  145. {
  146. return NAPI_GRO_CB(skb)->network_offsets[NAPI_GRO_CB(skb)->encap_mark];
  147. }
  148. static inline void *skb_gro_network_header(const struct sk_buff *skb)
  149. {
  150. if (skb_gro_may_pull(skb, skb_gro_offset(skb)))
  151. return skb_gro_header_fast(skb, skb_gro_receive_network_offset(skb));
  152. return skb->data + skb_gro_receive_network_offset(skb);
  153. }
  154. static inline __wsum inet_gro_compute_pseudo(const struct sk_buff *skb,
  155. int proto)
  156. {
  157. const struct iphdr *iph = skb_gro_network_header(skb);
  158. return csum_tcpudp_nofold(iph->saddr, iph->daddr,
  159. skb_gro_len(skb), proto, 0);
  160. }
  161. static inline void skb_gro_postpull_rcsum(struct sk_buff *skb,
  162. const void *start, unsigned int len)
  163. {
  164. if (NAPI_GRO_CB(skb)->csum_valid)
  165. NAPI_GRO_CB(skb)->csum = wsum_negate(csum_partial(start, len,
  166. wsum_negate(NAPI_GRO_CB(skb)->csum)));
  167. }
  168. /* GRO checksum functions. These are logical equivalents of the normal
  169. * checksum functions (in skbuff.h) except that they operate on the GRO
  170. * offsets and fields in sk_buff.
  171. */
  172. __sum16 __skb_gro_checksum_complete(struct sk_buff *skb);
  173. static inline bool skb_at_gro_remcsum_start(struct sk_buff *skb)
  174. {
  175. return (NAPI_GRO_CB(skb)->gro_remcsum_start == skb_gro_offset(skb));
  176. }
  177. static inline bool __skb_gro_checksum_validate_needed(struct sk_buff *skb,
  178. bool zero_okay,
  179. __sum16 check)
  180. {
  181. return ((skb->ip_summed != CHECKSUM_PARTIAL ||
  182. skb_checksum_start_offset(skb) <
  183. skb_gro_offset(skb)) &&
  184. !skb_at_gro_remcsum_start(skb) &&
  185. NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  186. (!zero_okay || check));
  187. }
  188. static inline __sum16 __skb_gro_checksum_validate_complete(struct sk_buff *skb,
  189. __wsum psum)
  190. {
  191. if (NAPI_GRO_CB(skb)->csum_valid &&
  192. !csum_fold(csum_add(psum, NAPI_GRO_CB(skb)->csum)))
  193. return 0;
  194. NAPI_GRO_CB(skb)->csum = psum;
  195. return __skb_gro_checksum_complete(skb);
  196. }
  197. static inline void skb_gro_incr_csum_unnecessary(struct sk_buff *skb)
  198. {
  199. if (NAPI_GRO_CB(skb)->csum_cnt > 0) {
  200. /* Consume a checksum from CHECKSUM_UNNECESSARY */
  201. NAPI_GRO_CB(skb)->csum_cnt--;
  202. } else {
  203. /* Update skb for CHECKSUM_UNNECESSARY and csum_level when we
  204. * verified a new top level checksum or an encapsulated one
  205. * during GRO. This saves work if we fallback to normal path.
  206. */
  207. __skb_incr_checksum_unnecessary(skb);
  208. }
  209. }
  210. #define __skb_gro_checksum_validate(skb, proto, zero_okay, check, \
  211. compute_pseudo) \
  212. ({ \
  213. __sum16 __ret = 0; \
  214. if (__skb_gro_checksum_validate_needed(skb, zero_okay, check)) \
  215. __ret = __skb_gro_checksum_validate_complete(skb, \
  216. compute_pseudo(skb, proto)); \
  217. if (!__ret) \
  218. skb_gro_incr_csum_unnecessary(skb); \
  219. __ret; \
  220. })
  221. #define skb_gro_checksum_validate(skb, proto, compute_pseudo) \
  222. __skb_gro_checksum_validate(skb, proto, false, 0, compute_pseudo)
  223. #define skb_gro_checksum_validate_zero_check(skb, proto, check, \
  224. compute_pseudo) \
  225. __skb_gro_checksum_validate(skb, proto, true, check, compute_pseudo)
  226. #define skb_gro_checksum_simple_validate(skb) \
  227. __skb_gro_checksum_validate(skb, 0, false, 0, null_compute_pseudo)
  228. static inline bool __skb_gro_checksum_convert_check(struct sk_buff *skb)
  229. {
  230. return (NAPI_GRO_CB(skb)->csum_cnt == 0 &&
  231. !NAPI_GRO_CB(skb)->csum_valid);
  232. }
  233. static inline void __skb_gro_checksum_convert(struct sk_buff *skb,
  234. __wsum pseudo)
  235. {
  236. NAPI_GRO_CB(skb)->csum = ~pseudo;
  237. NAPI_GRO_CB(skb)->csum_valid = 1;
  238. }
  239. #define skb_gro_checksum_try_convert(skb, proto, compute_pseudo) \
  240. do { \
  241. if (__skb_gro_checksum_convert_check(skb)) \
  242. __skb_gro_checksum_convert(skb, \
  243. compute_pseudo(skb, proto)); \
  244. } while (0)
  245. struct gro_remcsum {
  246. int offset;
  247. __wsum delta;
  248. };
  249. static inline void skb_gro_remcsum_init(struct gro_remcsum *grc)
  250. {
  251. grc->offset = 0;
  252. grc->delta = 0;
  253. }
  254. static inline void *skb_gro_remcsum_process(struct sk_buff *skb, void *ptr,
  255. unsigned int off, size_t hdrlen,
  256. int start, int offset,
  257. struct gro_remcsum *grc,
  258. bool nopartial)
  259. {
  260. __wsum delta;
  261. size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start);
  262. BUG_ON(!NAPI_GRO_CB(skb)->csum_valid);
  263. if (!nopartial) {
  264. NAPI_GRO_CB(skb)->gro_remcsum_start = off + hdrlen + start;
  265. return ptr;
  266. }
  267. ptr = skb_gro_header(skb, off + plen, off);
  268. if (!ptr)
  269. return NULL;
  270. delta = remcsum_adjust(ptr + hdrlen, NAPI_GRO_CB(skb)->csum,
  271. start, offset);
  272. /* Adjust skb->csum since we changed the packet */
  273. NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
  274. grc->offset = off + hdrlen + offset;
  275. grc->delta = delta;
  276. return ptr;
  277. }
  278. static inline void skb_gro_remcsum_cleanup(struct sk_buff *skb,
  279. struct gro_remcsum *grc)
  280. {
  281. void *ptr;
  282. size_t plen = grc->offset + sizeof(u16);
  283. if (!grc->delta)
  284. return;
  285. ptr = skb_gro_header(skb, plen, grc->offset);
  286. if (!ptr)
  287. return;
  288. remcsum_unadjust((__sum16 *)ptr, grc->delta);
  289. }
  290. #ifdef CONFIG_XFRM_OFFLOAD
  291. static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush)
  292. {
  293. if (PTR_ERR(pp) != -EINPROGRESS)
  294. NAPI_GRO_CB(skb)->flush |= flush;
  295. }
  296. static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb,
  297. struct sk_buff *pp,
  298. int flush,
  299. struct gro_remcsum *grc)
  300. {
  301. if (PTR_ERR(pp) != -EINPROGRESS) {
  302. NAPI_GRO_CB(skb)->flush |= flush;
  303. skb_gro_remcsum_cleanup(skb, grc);
  304. skb->remcsum_offload = 0;
  305. }
  306. }
  307. #else
  308. static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush)
  309. {
  310. NAPI_GRO_CB(skb)->flush |= flush;
  311. }
  312. static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb,
  313. struct sk_buff *pp,
  314. int flush,
  315. struct gro_remcsum *grc)
  316. {
  317. NAPI_GRO_CB(skb)->flush |= flush;
  318. skb_gro_remcsum_cleanup(skb, grc);
  319. skb->remcsum_offload = 0;
  320. }
  321. #endif
  322. INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
  323. struct sk_buff *));
  324. INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
  325. INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
  326. struct sk_buff *));
  327. INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
  328. INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp4_gro_receive(struct list_head *,
  329. struct sk_buff *));
  330. INDIRECT_CALLABLE_DECLARE(int udp4_gro_complete(struct sk_buff *, int));
  331. INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp6_gro_receive(struct list_head *,
  332. struct sk_buff *));
  333. INDIRECT_CALLABLE_DECLARE(int udp6_gro_complete(struct sk_buff *, int));
  334. #define indirect_call_gro_receive_inet(cb, f2, f1, head, skb) \
  335. ({ \
  336. unlikely(gro_recursion_inc_test(skb)) ? \
  337. NAPI_GRO_CB(skb)->flush |= 1, NULL : \
  338. INDIRECT_CALL_INET(cb, f2, f1, head, skb); \
  339. })
  340. struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
  341. struct udphdr *uh, struct sock *sk);
  342. int udp_gro_complete(struct sk_buff *skb, int nhoff, udp_lookup_t lookup);
  343. static inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb)
  344. {
  345. struct udphdr *uh;
  346. unsigned int hlen, off;
  347. off = skb_gro_offset(skb);
  348. hlen = off + sizeof(*uh);
  349. uh = skb_gro_header(skb, hlen, off);
  350. return uh;
  351. }
  352. static inline __wsum ip6_gro_compute_pseudo(const struct sk_buff *skb,
  353. int proto)
  354. {
  355. const struct ipv6hdr *iph = skb_gro_network_header(skb);
  356. return ~csum_unfold(csum_ipv6_magic(&iph->saddr, &iph->daddr,
  357. skb_gro_len(skb), proto, 0));
  358. }
  359. static inline int inet_gro_flush(const struct iphdr *iph, const struct iphdr *iph2,
  360. struct sk_buff *p, bool outer)
  361. {
  362. const u32 id = ntohl(*(__be32 *)&iph->id);
  363. const u32 id2 = ntohl(*(__be32 *)&iph2->id);
  364. const u16 ipid_offset = (id >> 16) - (id2 >> 16);
  365. const u16 count = NAPI_GRO_CB(p)->count;
  366. const u32 df = id & IP_DF;
  367. int flush;
  368. /* All fields must match except length and checksum. */
  369. flush = (iph->ttl ^ iph2->ttl) | (iph->tos ^ iph2->tos) | (df ^ (id2 & IP_DF));
  370. if (flush | (outer && df))
  371. return flush;
  372. /* When we receive our second frame we can make a decision on if we
  373. * continue this flow as an atomic flow with a fixed ID or if we use
  374. * an incrementing ID.
  375. */
  376. if (count == 1 && df && !ipid_offset)
  377. NAPI_GRO_CB(p)->ip_fixedid = true;
  378. return ipid_offset ^ (count * !NAPI_GRO_CB(p)->ip_fixedid);
  379. }
  380. static inline int ipv6_gro_flush(const struct ipv6hdr *iph, const struct ipv6hdr *iph2)
  381. {
  382. /* <Version:4><Traffic_Class:8><Flow_Label:20> */
  383. __be32 first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
  384. /* Flush if Traffic Class fields are different. */
  385. return !!((first_word & htonl(0x0FF00000)) |
  386. (__force __be32)(iph->hop_limit ^ iph2->hop_limit));
  387. }
  388. static inline int __gro_receive_network_flush(const void *th, const void *th2,
  389. struct sk_buff *p, const u16 diff,
  390. bool outer)
  391. {
  392. const void *nh = th - diff;
  393. const void *nh2 = th2 - diff;
  394. if (((struct iphdr *)nh)->version == 6)
  395. return ipv6_gro_flush(nh, nh2);
  396. else
  397. return inet_gro_flush(nh, nh2, p, outer);
  398. }
  399. static inline int gro_receive_network_flush(const void *th, const void *th2,
  400. struct sk_buff *p)
  401. {
  402. const bool encap_mark = NAPI_GRO_CB(p)->encap_mark;
  403. int off = skb_transport_offset(p);
  404. int flush;
  405. flush = __gro_receive_network_flush(th, th2, p, off - NAPI_GRO_CB(p)->network_offset, encap_mark);
  406. if (encap_mark)
  407. flush |= __gro_receive_network_flush(th, th2, p, off - NAPI_GRO_CB(p)->inner_network_offset, false);
  408. return flush;
  409. }
  410. int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb);
  411. int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb);
  412. /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
  413. static inline void gro_normal_list(struct napi_struct *napi)
  414. {
  415. if (!napi->rx_count)
  416. return;
  417. netif_receive_skb_list_internal(&napi->rx_list);
  418. INIT_LIST_HEAD(&napi->rx_list);
  419. napi->rx_count = 0;
  420. }
  421. /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
  422. * pass the whole batch up to the stack.
  423. */
  424. static inline void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb, int segs)
  425. {
  426. list_add_tail(&skb->list, &napi->rx_list);
  427. napi->rx_count += segs;
  428. if (napi->rx_count >= READ_ONCE(net_hotdata.gro_normal_batch))
  429. gro_normal_list(napi);
  430. }
  431. /* This function is the alternative of 'inet_iif' and 'inet_sdif'
  432. * functions in case we can not rely on fields of IPCB.
  433. *
  434. * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized.
  435. * The caller must hold the RCU read lock.
  436. */
  437. static inline void inet_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif)
  438. {
  439. *iif = inet_iif(skb) ?: skb->dev->ifindex;
  440. *sdif = 0;
  441. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  442. if (netif_is_l3_slave(skb->dev)) {
  443. struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev);
  444. *sdif = *iif;
  445. *iif = master ? master->ifindex : 0;
  446. }
  447. #endif
  448. }
  449. /* This function is the alternative of 'inet6_iif' and 'inet6_sdif'
  450. * functions in case we can not rely on fields of IP6CB.
  451. *
  452. * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized.
  453. * The caller must hold the RCU read lock.
  454. */
  455. static inline void inet6_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif)
  456. {
  457. /* using skb->dev->ifindex because skb_dst(skb) is not initialized */
  458. *iif = skb->dev->ifindex;
  459. *sdif = 0;
  460. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  461. if (netif_is_l3_slave(skb->dev)) {
  462. struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev);
  463. *sdif = *iif;
  464. *iif = master ? master->ifindex : 0;
  465. }
  466. #endif
  467. }
  468. struct packet_offload *gro_find_receive_by_type(__be16 type);
  469. struct packet_offload *gro_find_complete_by_type(__be16 type);
  470. #endif /* _NET_GRO_H */