Skip to content

Commit 4b0ebbc

Browse files
Richard Gobertkuba-moo
authored andcommitted
net: gro: move L3 flush checks to tcp_gro_receive and udp_gro_receive_segment
{inet,ipv6}_gro_receive functions perform flush checks (ttl, flags, iph->id, ...) against all packets in a loop. These flush checks are used in all merging UDP and TCP flows. These checks need to be done only once and only against the found p skb, since they only affect flush and not same_flow. This patch leverages correct network header offsets from the cb for both outer and inner network headers - allowing these checks to be done only once, in tcp_gro_receive and udp_gro_receive_segment. As a result, NAPI_GRO_CB(p)->flush is not used at all. In addition, flush_id checks are more declarative and contained in inet_gro_flush, thus removing the need for flush_id in napi_gro_cb. This results in less parsing code for non-loop flush tests for TCP and UDP flows. To make sure results are not within noise range - I've made netfilter drop all TCP packets, and measured CPU performance in GRO (in this case GRO is responsible for about 50% of the CPU utilization). perf top while replaying 64 parallel IP/TCP streams merging in GRO: (gro_receive_network_flush is compiled inline to tcp_gro_receive) net-next: 6.94% [kernel] [k] inet_gro_receive 3.02% [kernel] [k] tcp_gro_receive patch applied: 4.27% [kernel] [k] tcp_gro_receive 4.22% [kernel] [k] inet_gro_receive perf top while replaying 64 parallel IP/IP/TCP streams merging in GRO (same results for any encapsulation, in this case inet_gro_receive is top offender in net-next) net-next: 10.09% [kernel] [k] inet_gro_receive 2.08% [kernel] [k] tcp_gro_receive patch applied: 6.97% [kernel] [k] inet_gro_receive 3.68% [kernel] [k] tcp_gro_receive Signed-off-by: Richard Gobert <[email protected]> Reviewed-by: Willem de Bruijn <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 186b1ea commit 4b0ebbc

File tree

6 files changed

+73
-84
lines changed

6 files changed

+73
-84
lines changed

include/net/gro.h

Lines changed: 68 additions & 8 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 {
@@ -442,6 +439,69 @@ static inline __wsum ip6_gro_compute_pseudo(const struct sk_buff *skb,
442439
skb_gro_len(skb), proto, 0));
443440
}
444441

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+
445505
int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb);
446506
int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb);
447507

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 & 40 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,43 +1527,8 @@ 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;
15721533
NAPI_GRO_CB(skb)->inner_network_offset = off;
15731534

net/ipv4/tcp_offload.c

Lines changed: 3 additions & 14 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

@@ -480,7 +469,7 @@ INDIRECT_CALLABLE_SCOPE int tcp4_gro_complete(struct sk_buff *skb, int thoff)
480469
iph->daddr, 0);
481470

482471
skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4 |
483-
(NAPI_GRO_CB(skb)->is_atomic * SKB_GSO_TCP_FIXEDID);
472+
(NAPI_GRO_CB(skb)->ip_fixedid * SKB_GSO_TCP_FIXEDID);
484473

485474
tcp_gro_complete(skb);
486475
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: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,8 @@ INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
290290
nlen - sizeof(struct ipv6hdr)))
291291
goto not_same_flow;
292292
}
293-
/* flush if Traffic Class fields are different */
294-
NAPI_GRO_CB(p)->flush |= !!((first_word & htonl(0x0FF00000)) |
295-
(__force __be32)(iph->hop_limit ^ iph2->hop_limit));
296-
NAPI_GRO_CB(p)->flush |= flush;
297-
298-
/* If the previous IP ID value was based on an atomic
299-
* datagram we can overwrite the value and ignore it.
300-
*/
301-
if (NAPI_GRO_CB(skb)->is_atomic)
302-
NAPI_GRO_CB(p)->flush_id = 0;
303293
}
304294

305-
NAPI_GRO_CB(skb)->is_atomic = true;
306295
NAPI_GRO_CB(skb)->flush |= flush;
307296

308297
skb_gro_postpull_rcsum(skb, iph, nlen);

0 commit comments

Comments
 (0)