1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #ifndef _NET_IPV6_GRO_H
4 #define _NET_IPV6_GRO_H
6 #include <linux/indirect_call_wrapper.h>
8 #include <linux/ipv6.h>
9 #include <net/ip6_checksum.h>
10 #include <linux/skbuff.h>
16 /* Virtual address of skb_shinfo(skb)->frags[0].page + offset. */
19 /* Length of frag0. */
20 unsigned int frag0_len;
24 /* used in skb_gro_receive() slow path */
27 /* jiffies when first packet was created/queued */
32 /* This indicates where we are processing relative to skb->data. */
35 /* This is non-zero if the packet cannot be merged with the new skb. */
38 /* Save the IP ID here and check when we get to the transport layer */
41 /* Number of segments aggregated. */
44 /* Used in ipv6_gro_receive() and foo-over-udp and esp-in-udp */
47 /* Used in napi_gro_cb::free */
48 #define NAPI_GRO_FREE 1
49 #define NAPI_GRO_FREE_STOLEN_HEAD 2
50 /* portion of the cb set to zero at every gro iteration */
53 /* Start offset for remote checksum offload */
54 u16 gro_remcsum_start;
56 /* This is non-zero if the packet may be of the same flow. */
59 /* Used in tunnel GRO receive */
62 /* GRO checksum is valid */
65 /* Number of checksums via CHECKSUM_UNNECESSARY */
71 /* Used in foo-over-udp, set in udp[46]_gro_receive */
74 /* Used in GRE, set in fou/gue_gro_receive */
77 /* Used to determine if flush_id can be ignored */
80 /* Number of gro_receive callbacks this packet already went through */
81 u8 recursion_counter:4;
83 /* GRO is done by frag_list pointer chaining. */
87 /* used to support CHECKSUM_COMPLETE for tunneling protocols */
91 #define NAPI_GRO_CB(skb) ((struct napi_gro_cb *)(skb)->cb)
93 #define GRO_RECURSION_LIMIT 15
94 static inline int gro_recursion_inc_test(struct sk_buff *skb)
96 return ++NAPI_GRO_CB(skb)->recursion_counter == GRO_RECURSION_LIMIT;
99 typedef struct sk_buff *(*gro_receive_t)(struct list_head *, struct sk_buff *);
100 static inline struct sk_buff *call_gro_receive(gro_receive_t cb,
101 struct list_head *head,
104 if (unlikely(gro_recursion_inc_test(skb))) {
105 NAPI_GRO_CB(skb)->flush |= 1;
109 return cb(head, skb);
112 typedef struct sk_buff *(*gro_receive_sk_t)(struct sock *, struct list_head *,
114 static inline struct sk_buff *call_gro_receive_sk(gro_receive_sk_t cb,
116 struct list_head *head,
119 if (unlikely(gro_recursion_inc_test(skb))) {
120 NAPI_GRO_CB(skb)->flush |= 1;
124 return cb(sk, head, skb);
127 static inline unsigned int skb_gro_offset(const struct sk_buff *skb)
129 return NAPI_GRO_CB(skb)->data_offset;
132 static inline unsigned int skb_gro_len(const struct sk_buff *skb)
134 return skb->len - NAPI_GRO_CB(skb)->data_offset;
137 static inline void skb_gro_pull(struct sk_buff *skb, unsigned int len)
139 NAPI_GRO_CB(skb)->data_offset += len;
142 static inline void *skb_gro_header_fast(struct sk_buff *skb,
145 return NAPI_GRO_CB(skb)->frag0 + offset;
148 static inline int skb_gro_header_hard(struct sk_buff *skb, unsigned int hlen)
150 return NAPI_GRO_CB(skb)->frag0_len < hlen;
153 static inline void skb_gro_frag0_invalidate(struct sk_buff *skb)
155 NAPI_GRO_CB(skb)->frag0 = NULL;
156 NAPI_GRO_CB(skb)->frag0_len = 0;
159 static inline void *skb_gro_header_slow(struct sk_buff *skb, unsigned int hlen,
162 if (!pskb_may_pull(skb, hlen))
165 skb_gro_frag0_invalidate(skb);
166 return skb->data + offset;
169 static inline void *skb_gro_header(struct sk_buff *skb,
170 unsigned int hlen, unsigned int offset)
174 ptr = skb_gro_header_fast(skb, offset);
175 if (skb_gro_header_hard(skb, hlen))
176 ptr = skb_gro_header_slow(skb, hlen, offset);
180 static inline void *skb_gro_network_header(struct sk_buff *skb)
182 return (NAPI_GRO_CB(skb)->frag0 ?: skb->data) +
183 skb_network_offset(skb);
186 static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto)
188 const struct iphdr *iph = skb_gro_network_header(skb);
190 return csum_tcpudp_nofold(iph->saddr, iph->daddr,
191 skb_gro_len(skb), proto, 0);
194 static inline void skb_gro_postpull_rcsum(struct sk_buff *skb,
195 const void *start, unsigned int len)
197 if (NAPI_GRO_CB(skb)->csum_valid)
198 NAPI_GRO_CB(skb)->csum = wsum_negate(csum_partial(start, len,
199 wsum_negate(NAPI_GRO_CB(skb)->csum)));
202 /* GRO checksum functions. These are logical equivalents of the normal
203 * checksum functions (in skbuff.h) except that they operate on the GRO
204 * offsets and fields in sk_buff.
207 __sum16 __skb_gro_checksum_complete(struct sk_buff *skb);
209 static inline bool skb_at_gro_remcsum_start(struct sk_buff *skb)
211 return (NAPI_GRO_CB(skb)->gro_remcsum_start == skb_gro_offset(skb));
214 static inline bool __skb_gro_checksum_validate_needed(struct sk_buff *skb,
218 return ((skb->ip_summed != CHECKSUM_PARTIAL ||
219 skb_checksum_start_offset(skb) <
220 skb_gro_offset(skb)) &&
221 !skb_at_gro_remcsum_start(skb) &&
222 NAPI_GRO_CB(skb)->csum_cnt == 0 &&
223 (!zero_okay || check));
226 static inline __sum16 __skb_gro_checksum_validate_complete(struct sk_buff *skb,
229 if (NAPI_GRO_CB(skb)->csum_valid &&
230 !csum_fold(csum_add(psum, NAPI_GRO_CB(skb)->csum)))
233 NAPI_GRO_CB(skb)->csum = psum;
235 return __skb_gro_checksum_complete(skb);
238 static inline void skb_gro_incr_csum_unnecessary(struct sk_buff *skb)
240 if (NAPI_GRO_CB(skb)->csum_cnt > 0) {
241 /* Consume a checksum from CHECKSUM_UNNECESSARY */
242 NAPI_GRO_CB(skb)->csum_cnt--;
244 /* Update skb for CHECKSUM_UNNECESSARY and csum_level when we
245 * verified a new top level checksum or an encapsulated one
246 * during GRO. This saves work if we fallback to normal path.
248 __skb_incr_checksum_unnecessary(skb);
252 #define __skb_gro_checksum_validate(skb, proto, zero_okay, check, \
256 if (__skb_gro_checksum_validate_needed(skb, zero_okay, check)) \
257 __ret = __skb_gro_checksum_validate_complete(skb, \
258 compute_pseudo(skb, proto)); \
260 skb_gro_incr_csum_unnecessary(skb); \
264 #define skb_gro_checksum_validate(skb, proto, compute_pseudo) \
265 __skb_gro_checksum_validate(skb, proto, false, 0, compute_pseudo)
267 #define skb_gro_checksum_validate_zero_check(skb, proto, check, \
269 __skb_gro_checksum_validate(skb, proto, true, check, compute_pseudo)
271 #define skb_gro_checksum_simple_validate(skb) \
272 __skb_gro_checksum_validate(skb, 0, false, 0, null_compute_pseudo)
274 static inline bool __skb_gro_checksum_convert_check(struct sk_buff *skb)
276 return (NAPI_GRO_CB(skb)->csum_cnt == 0 &&
277 !NAPI_GRO_CB(skb)->csum_valid);
280 static inline void __skb_gro_checksum_convert(struct sk_buff *skb,
283 NAPI_GRO_CB(skb)->csum = ~pseudo;
284 NAPI_GRO_CB(skb)->csum_valid = 1;
287 #define skb_gro_checksum_try_convert(skb, proto, compute_pseudo) \
289 if (__skb_gro_checksum_convert_check(skb)) \
290 __skb_gro_checksum_convert(skb, \
291 compute_pseudo(skb, proto)); \
299 static inline void skb_gro_remcsum_init(struct gro_remcsum *grc)
305 static inline void *skb_gro_remcsum_process(struct sk_buff *skb, void *ptr,
306 unsigned int off, size_t hdrlen,
307 int start, int offset,
308 struct gro_remcsum *grc,
312 size_t plen = hdrlen + max_t(size_t, offset + sizeof(u16), start);
314 BUG_ON(!NAPI_GRO_CB(skb)->csum_valid);
317 NAPI_GRO_CB(skb)->gro_remcsum_start = off + hdrlen + start;
321 ptr = skb_gro_header(skb, off + plen, off);
325 delta = remcsum_adjust(ptr + hdrlen, NAPI_GRO_CB(skb)->csum,
328 /* Adjust skb->csum since we changed the packet */
329 NAPI_GRO_CB(skb)->csum = csum_add(NAPI_GRO_CB(skb)->csum, delta);
331 grc->offset = off + hdrlen + offset;
337 static inline void skb_gro_remcsum_cleanup(struct sk_buff *skb,
338 struct gro_remcsum *grc)
341 size_t plen = grc->offset + sizeof(u16);
346 ptr = skb_gro_header(skb, plen, grc->offset);
350 remcsum_unadjust((__sum16 *)ptr, grc->delta);
353 #ifdef CONFIG_XFRM_OFFLOAD
354 static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush)
356 if (PTR_ERR(pp) != -EINPROGRESS)
357 NAPI_GRO_CB(skb)->flush |= flush;
359 static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb,
362 struct gro_remcsum *grc)
364 if (PTR_ERR(pp) != -EINPROGRESS) {
365 NAPI_GRO_CB(skb)->flush |= flush;
366 skb_gro_remcsum_cleanup(skb, grc);
367 skb->remcsum_offload = 0;
371 static inline void skb_gro_flush_final(struct sk_buff *skb, struct sk_buff *pp, int flush)
373 NAPI_GRO_CB(skb)->flush |= flush;
375 static inline void skb_gro_flush_final_remcsum(struct sk_buff *skb,
378 struct gro_remcsum *grc)
380 NAPI_GRO_CB(skb)->flush |= flush;
381 skb_gro_remcsum_cleanup(skb, grc);
382 skb->remcsum_offload = 0;
386 INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
388 INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
389 INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
391 INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
393 INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp4_gro_receive(struct list_head *,
395 INDIRECT_CALLABLE_DECLARE(int udp4_gro_complete(struct sk_buff *, int));
397 INDIRECT_CALLABLE_DECLARE(struct sk_buff *udp6_gro_receive(struct list_head *,
399 INDIRECT_CALLABLE_DECLARE(int udp6_gro_complete(struct sk_buff *, int));
401 #define indirect_call_gro_receive_inet(cb, f2, f1, head, skb) \
403 unlikely(gro_recursion_inc_test(skb)) ? \
404 NAPI_GRO_CB(skb)->flush |= 1, NULL : \
405 INDIRECT_CALL_INET(cb, f2, f1, head, skb); \
408 struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb,
409 struct udphdr *uh, struct sock *sk);
410 int udp_gro_complete(struct sk_buff *skb, int nhoff, udp_lookup_t lookup);
412 static inline struct udphdr *udp_gro_udphdr(struct sk_buff *skb)
415 unsigned int hlen, off;
417 off = skb_gro_offset(skb);
418 hlen = off + sizeof(*uh);
419 uh = skb_gro_header(skb, hlen, off);
424 static inline __wsum ip6_gro_compute_pseudo(struct sk_buff *skb, int proto)
426 const struct ipv6hdr *iph = skb_gro_network_header(skb);
428 return ~csum_unfold(csum_ipv6_magic(&iph->saddr, &iph->daddr,
429 skb_gro_len(skb), proto, 0));
432 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb);
434 /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
435 static inline void gro_normal_list(struct napi_struct *napi)
439 netif_receive_skb_list_internal(&napi->rx_list);
440 INIT_LIST_HEAD(&napi->rx_list);
444 /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
445 * pass the whole batch up to the stack.
447 static inline void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb, int segs)
449 list_add_tail(&skb->list, &napi->rx_list);
450 napi->rx_count += segs;
451 if (napi->rx_count >= READ_ONCE(gro_normal_batch))
452 gro_normal_list(napi);
455 /* This function is the alternative of 'inet_iif' and 'inet_sdif'
456 * functions in case we can not rely on fields of IPCB.
458 * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized.
459 * The caller must hold the RCU read lock.
461 static inline void inet_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif)
463 *iif = inet_iif(skb) ?: skb->dev->ifindex;
466 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
467 if (netif_is_l3_slave(skb->dev)) {
468 struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev);
471 *iif = master ? master->ifindex : 0;
476 /* This function is the alternative of 'inet6_iif' and 'inet6_sdif'
477 * functions in case we can not rely on fields of IP6CB.
479 * The caller must verify skb_valid_dst(skb) is false and skb->dev is initialized.
480 * The caller must hold the RCU read lock.
482 static inline void inet6_get_iif_sdif(const struct sk_buff *skb, int *iif, int *sdif)
484 /* using skb->dev->ifindex because skb_dst(skb) is not initialized */
485 *iif = skb->dev->ifindex;
488 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
489 if (netif_is_l3_slave(skb->dev)) {
490 struct net_device *master = netdev_master_upper_dev_get_rcu(skb->dev);
493 *iif = master ? master->ifindex : 0;
498 extern struct list_head offload_base;
500 #endif /* _NET_IPV6_GRO_H */