Skip to content

Commit e6e4357

Browse files
committed
Merge branch 'net-gro-remove-network_header-use-move-p-flush-flush_id-calculations-to-l4'
Richard Gobert says: ==================== net: gro: remove network_header use, move p->{flush/flush_id} calculations to L4 The cb fields network_offset and inner_network_offset are used instead of skb->network_header throughout GRO. These fields are then leveraged in the next commit to remove flush_id state from napi_gro_cb, and stateful code in {ipv6,inet}_gro_receive which may be unnecessarily complicated due to encapsulation support in GRO. These fields are checked in L4 instead. 3rd patch adds tests for different flush_id flows in GRO. ==================== Link: https://lore.kernel.org/r/20240509190819.2985-1-richardbgobert@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 9af9b89 + bc21fae commit e6e4357

File tree

8 files changed

+224
-95
lines changed

8 files changed

+224
-95
lines changed

include/net/gro.h

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ struct napi_gro_cb {
3636
/* This is non-zero if the packet cannot be merged with the new skb. */
3737
u16 flush;
3838

39-
/* Save the IP ID here and check when we get to the transport layer */
40-
u16 flush_id;
41-
4239
/* Number of segments aggregated. */
4340
u16 count;
4441

4542
/* Used in ipv6_gro_receive() and foo-over-udp and esp-in-udp */
4643
u16 proto;
4744

45+
/* used to support CHECKSUM_COMPLETE for tunneling protocols */
46+
__wsum csum;
47+
4848
/* Used in napi_gro_cb::free */
4949
#define NAPI_GRO_FREE 1
5050
#define NAPI_GRO_FREE_STOLEN_HEAD 2
@@ -75,8 +75,8 @@ struct napi_gro_cb {
7575
/* Used in GRE, set in fou/gue_gro_receive */
7676
u8 is_fou:1;
7777

78-
/* Used to determine if flush_id can be ignored */
79-
u8 is_atomic:1;
78+
/* Used to determine if ipid_offset can be ignored */
79+
u8 ip_fixedid:1;
8080

8181
/* Number of gro_receive callbacks this packet already went through */
8282
u8 recursion_counter:4;
@@ -85,9 +85,6 @@ struct napi_gro_cb {
8585
u8 is_flist:1;
8686
);
8787

88-
/* used to support CHECKSUM_COMPLETE for tunneling protocols */
89-
__wsum csum;
90-
9188
/* L3 offsets */
9289
union {
9390
struct {
@@ -181,12 +178,17 @@ static inline void *skb_gro_header(struct sk_buff *skb, unsigned int hlen,
181178
return ptr;
182179
}
183180

181+
static inline int skb_gro_receive_network_offset(const struct sk_buff *skb)
182+
{
183+
return NAPI_GRO_CB(skb)->network_offsets[NAPI_GRO_CB(skb)->encap_mark];
184+
}
185+
184186
static inline void *skb_gro_network_header(const struct sk_buff *skb)
185187
{
186188
if (skb_gro_may_pull(skb, skb_gro_offset(skb)))
187-
return skb_gro_header_fast(skb, skb_network_offset(skb));
189+
return skb_gro_header_fast(skb, skb_gro_receive_network_offset(skb));
188190

189-
return skb_network_header(skb);
191+
return skb->data + skb_gro_receive_network_offset(skb);
190192
}
191193

192194
static inline __wsum inet_gro_compute_pseudo(const struct sk_buff *skb,
@@ -437,6 +439,69 @@ static inline __wsum ip6_gro_compute_pseudo(const struct sk_buff *skb,
437439
skb_gro_len(skb), proto, 0));
438440
}
439441

442+
static inline int inet_gro_flush(const struct iphdr *iph, const struct iphdr *iph2,
443+
struct sk_buff *p, bool outer)
444+
{
445+
const u32 id = ntohl(*(__be32 *)&iph->id);
446+
const u32 id2 = ntohl(*(__be32 *)&iph2->id);
447+
const u16 ipid_offset = (id >> 16) - (id2 >> 16);
448+
const u16 count = NAPI_GRO_CB(p)->count;
449+
const u32 df = id & IP_DF;
450+
int flush;
451+
452+
/* All fields must match except length and checksum. */
453+
flush = (iph->ttl ^ iph2->ttl) | (iph->tos ^ iph2->tos) | (df ^ (id2 & IP_DF));
454+
455+
if (flush | (outer && df))
456+
return flush;
457+
458+
/* When we receive our second frame we can make a decision on if we
459+
* continue this flow as an atomic flow with a fixed ID or if we use
460+
* an incrementing ID.
461+
*/
462+
if (count == 1 && df && !ipid_offset)
463+
NAPI_GRO_CB(p)->ip_fixedid = true;
464+
465+
return ipid_offset ^ (count * !NAPI_GRO_CB(p)->ip_fixedid);
466+
}
467+
468+
static inline int ipv6_gro_flush(const struct ipv6hdr *iph, const struct ipv6hdr *iph2)
469+
{
470+
/* <Version:4><Traffic_Class:8><Flow_Label:20> */
471+
__be32 first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
472+
473+
/* Flush if Traffic Class fields are different. */
474+
return !!((first_word & htonl(0x0FF00000)) |
475+
(__force __be32)(iph->hop_limit ^ iph2->hop_limit));
476+
}
477+
478+
static inline int __gro_receive_network_flush(const void *th, const void *th2,
479+
struct sk_buff *p, const u16 diff,
480+
bool outer)
481+
{
482+
const void *nh = th - diff;
483+
const void *nh2 = th2 - diff;
484+
485+
if (((struct iphdr *)nh)->version == 6)
486+
return ipv6_gro_flush(nh, nh2);
487+
else
488+
return inet_gro_flush(nh, nh2, p, outer);
489+
}
490+
491+
static inline int gro_receive_network_flush(const void *th, const void *th2,
492+
struct sk_buff *p)
493+
{
494+
const bool encap_mark = NAPI_GRO_CB(p)->encap_mark;
495+
int off = skb_transport_offset(p);
496+
int flush;
497+
498+
flush = __gro_receive_network_flush(th, th2, p, off - NAPI_GRO_CB(p)->network_offset, encap_mark);
499+
if (encap_mark)
500+
flush |= __gro_receive_network_flush(th, th2, p, off - NAPI_GRO_CB(p)->inner_network_offset, false);
501+
502+
return flush;
503+
}
504+
440505
int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb);
441506
int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb);
442507

net/core/gro.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,6 @@ static void gro_list_prepare(const struct list_head *head,
358358
list_for_each_entry(p, head, list) {
359359
unsigned long diffs;
360360

361-
NAPI_GRO_CB(p)->flush = 0;
362-
363361
if (hash != skb_get_hash_raw(p)) {
364362
NAPI_GRO_CB(p)->same_flow = 0;
365363
continue;
@@ -499,7 +497,6 @@ static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff
499497
sizeof(u32))); /* Avoid slow unaligned acc */
500498
*(u32 *)&NAPI_GRO_CB(skb)->zeroed = 0;
501499
NAPI_GRO_CB(skb)->flush = skb_has_frag_list(skb);
502-
NAPI_GRO_CB(skb)->is_atomic = 1;
503500
NAPI_GRO_CB(skb)->count = 1;
504501
if (unlikely(skb_is_gso(skb))) {
505502
NAPI_GRO_CB(skb)->count = skb_shinfo(skb)->gso_segs;

net/ipv4/af_inet.c

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,6 @@ struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
14821482
struct sk_buff *p;
14831483
unsigned int hlen;
14841484
unsigned int off;
1485-
unsigned int id;
14861485
int flush = 1;
14871486
int proto;
14881487

@@ -1508,13 +1507,10 @@ struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
15081507
goto out;
15091508

15101509
NAPI_GRO_CB(skb)->proto = proto;
1511-
id = ntohl(*(__be32 *)&iph->id);
1512-
flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
1513-
id >>= 16;
1510+
flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (ntohl(*(__be32 *)&iph->id) & ~IP_DF));
15141511

15151512
list_for_each_entry(p, head, list) {
15161513
struct iphdr *iph2;
1517-
u16 flush_id;
15181514

15191515
if (!NAPI_GRO_CB(p)->same_flow)
15201516
continue;
@@ -1531,48 +1527,9 @@ struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
15311527
NAPI_GRO_CB(p)->same_flow = 0;
15321528
continue;
15331529
}
1534-
1535-
/* All fields must match except length and checksum. */
1536-
NAPI_GRO_CB(p)->flush |=
1537-
(iph->ttl ^ iph2->ttl) |
1538-
(iph->tos ^ iph2->tos) |
1539-
((iph->frag_off ^ iph2->frag_off) & htons(IP_DF));
1540-
1541-
NAPI_GRO_CB(p)->flush |= flush;
1542-
1543-
/* We need to store of the IP ID check to be included later
1544-
* when we can verify that this packet does in fact belong
1545-
* to a given flow.
1546-
*/
1547-
flush_id = (u16)(id - ntohs(iph2->id));
1548-
1549-
/* This bit of code makes it much easier for us to identify
1550-
* the cases where we are doing atomic vs non-atomic IP ID
1551-
* checks. Specifically an atomic check can return IP ID
1552-
* values 0 - 0xFFFF, while a non-atomic check can only
1553-
* return 0 or 0xFFFF.
1554-
*/
1555-
if (!NAPI_GRO_CB(p)->is_atomic ||
1556-
!(iph->frag_off & htons(IP_DF))) {
1557-
flush_id ^= NAPI_GRO_CB(p)->count;
1558-
flush_id = flush_id ? 0xFFFF : 0;
1559-
}
1560-
1561-
/* If the previous IP ID value was based on an atomic
1562-
* datagram we can overwrite the value and ignore it.
1563-
*/
1564-
if (NAPI_GRO_CB(skb)->is_atomic)
1565-
NAPI_GRO_CB(p)->flush_id = flush_id;
1566-
else
1567-
NAPI_GRO_CB(p)->flush_id |= flush_id;
15681530
}
15691531

1570-
NAPI_GRO_CB(skb)->is_atomic = !!(iph->frag_off & htons(IP_DF));
15711532
NAPI_GRO_CB(skb)->flush |= flush;
1572-
skb_set_network_header(skb, off);
1573-
/* The above will be needed by the transport layer if there is one
1574-
* immediately following this IP hdr.
1575-
*/
15761533
NAPI_GRO_CB(skb)->inner_network_offset = off;
15771534

15781535
/* Note : No need to call skb_gro_postpull_rcsum() here,

net/ipv4/tcp_offload.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -313,27 +313,16 @@ struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb,
313313
if (!p)
314314
goto out_check_final;
315315

316-
/* Include the IP ID check below from the inner most IP hdr */
317316
th2 = tcp_hdr(p);
318-
flush = NAPI_GRO_CB(p)->flush;
319-
flush |= (__force int)(flags & TCP_FLAG_CWR);
317+
flush = (__force int)(flags & TCP_FLAG_CWR);
320318
flush |= (__force int)((flags ^ tcp_flag_word(th2)) &
321319
~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
322320
flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
323321
for (i = sizeof(*th); i < thlen; i += 4)
324322
flush |= *(u32 *)((u8 *)th + i) ^
325323
*(u32 *)((u8 *)th2 + i);
326324

327-
/* When we receive our second frame we can made a decision on if we
328-
* continue this flow as an atomic flow with a fixed ID or if we use
329-
* an incrementing ID.
330-
*/
331-
if (NAPI_GRO_CB(p)->flush_id != 1 ||
332-
NAPI_GRO_CB(p)->count != 1 ||
333-
!NAPI_GRO_CB(p)->is_atomic)
334-
flush |= NAPI_GRO_CB(p)->flush_id;
335-
else
336-
NAPI_GRO_CB(p)->is_atomic = false;
325+
flush |= gro_receive_network_flush(th, th2, p);
337326

338327
mss = skb_shinfo(p)->gso_size;
339328

@@ -463,7 +452,8 @@ struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)
463452

464453
INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
465454
{
466-
const struct iphdr *iph = ip_hdr(skb);
455+
const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
456+
const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
467457
struct tcphdr *th = tcp_hdr(skb);
468458

469459
if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {
@@ -479,7 +469,7 @@ INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
479469
iph->daddr, 0);
480470

481471
skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4 |
482-
(NAPI_GRO_CB(skb)->is_atomic * SKB_GSO_TCP_FIXEDID);
472+
(NAPI_GRO_CB(skb)->ip_fixedid * SKB_GSO_TCP_FIXEDID);
483473

484474
tcp_gro_complete(skb);
485475
return 0;

net/ipv4/udp_offload.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,7 @@ static struct sk_buff *udp_gro_receive_segment(struct list_head *head,
478478
return p;
479479
}
480480

481-
flush = NAPI_GRO_CB(p)->flush;
482-
483-
if (NAPI_GRO_CB(p)->flush_id != 1 ||
484-
NAPI_GRO_CB(p)->count != 1 ||
485-
!NAPI_GRO_CB(p)->is_atomic)
486-
flush |= NAPI_GRO_CB(p)->flush_id;
487-
else
488-
NAPI_GRO_CB(p)->is_atomic = false;
481+
flush = gro_receive_network_flush(uh, uh2, p);
489482

490483
/* Terminate the flow on len mismatch or if it grow "too much".
491484
* Under small packet flood GRO count could elsewhere grow a lot

net/ipv6/ip6_offload.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static int ipv6_gro_pull_exthdrs(struct sk_buff *skb, int off, int proto)
6767
off += len;
6868
}
6969

70-
skb_gro_pull(skb, off - skb_network_offset(skb));
70+
skb_gro_pull(skb, off - skb_gro_receive_network_offset(skb));
7171
return proto;
7272
}
7373

@@ -236,7 +236,6 @@ INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
236236
if (unlikely(!iph))
237237
goto out;
238238

239-
skb_set_network_header(skb, off);
240239
NAPI_GRO_CB(skb)->inner_network_offset = off;
241240

242241
flush += ntohs(iph->payload_len) != skb->len - hlen;
@@ -260,7 +259,7 @@ INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
260259
NAPI_GRO_CB(skb)->proto = proto;
261260

262261
flush--;
263-
nlen = skb_network_header_len(skb);
262+
nlen = skb_gro_offset(skb) - off;
264263

265264
list_for_each_entry(p, head, list) {
266265
const struct ipv6hdr *iph2;
@@ -291,19 +290,8 @@ INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
291290
nlen - sizeof(struct ipv6hdr)))
292291
goto not_same_flow;
293292
}
294-
/* flush if Traffic Class fields are different */
295-
NAPI_GRO_CB(p)->flush |= !!((first_word & htonl(0x0FF00000)) |
296-
(__force __be32)(iph->hop_limit ^ iph2->hop_limit));
297-
NAPI_GRO_CB(p)->flush |= flush;
298-
299-
/* If the previous IP ID value was based on an atomic
300-
* datagram we can overwrite the value and ignore it.
301-
*/
302-
if (NAPI_GRO_CB(skb)->is_atomic)
303-
NAPI_GRO_CB(p)->flush_id = 0;
304293
}
305294

306-
NAPI_GRO_CB(skb)->is_atomic = true;
307295
NAPI_GRO_CB(skb)->flush |= flush;
308296

309297
skb_gro_postpull_rcsum(skb, iph, nlen);

net/ipv6/tcpv6_offload.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb)
7272

7373
INDIRECT_CALLABLE_SCOPE int tcp6_gro_complete(struct sk_buff *skb, int thoff)
7474
{
75-
const struct ipv6hdr *iph = ipv6_hdr(skb);
75+
const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
76+
const struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + offset);
7677
struct tcphdr *th = tcp_hdr(skb);
7778

7879
if (unlikely(NAPI_GRO_CB(skb)->is_flist)) {

0 commit comments

Comments
 (0)