Skip to content

Commit 4370240

Browse files
committed
Merge branch 'tcpflags'
Eric Dumazet says: ==================== tcp: no longer keep around headers in input path Looking at tcp_try_coalesce() I was wondering why I did : if (tcp_hdr(from)->fin) return false; The answer would be to allow the aggregation, if we simply OR the FIN and PSH flags eventually present in @from to @to packet. (Note a change is also needed in skb_try_coalesce() to avoid calling skb_put() with 0 len) Then, looking at tcp_recvmsg(), I realized we access tcp_hdr(skb)->syn (and maybe tcp_hdr(skb)->fin) for every packet we process from socket receive queue. We have to understand TCP flags are cold in cpu caches most of the time (assuming TCP timestamps, and that application calls recvmsg() a long time after incoming packet was processed), and bringing a whole cache line only to access one bit is not very nice. It would make sense to use in TCP input path TCP_SKB_CB(skb)->tcp_flags as we do in output path. This saves one cache line miss, and TCP tcp_collapse() can avoid dealing with the headers. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 13bb518 + b3d6cb9 commit 4370240

5 files changed

Lines changed: 22 additions & 32 deletions

File tree

‎net/core/skbuff.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3936,7 +3936,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
39363936
return false;
39373937

39383938
if (len <= skb_tailroom(to)) {
3939-
BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3939+
if (len)
3940+
BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
39403941
*delta_truesize = 0;
39413942
return true;
39423943
}

‎net/ipv4/tcp.c‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,9 +1510,9 @@ static struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off)
15101510

15111511
while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
15121512
offset = seq - TCP_SKB_CB(skb)->seq;
1513-
if (tcp_hdr(skb)->syn)
1513+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)
15141514
offset--;
1515-
if (offset < skb->len || tcp_hdr(skb)->fin) {
1515+
if (offset < skb->len || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) {
15161516
*off = offset;
15171517
return skb;
15181518
}
@@ -1585,7 +1585,7 @@ int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
15851585
if (offset + 1 != skb->len)
15861586
continue;
15871587
}
1588-
if (tcp_hdr(skb)->fin) {
1588+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) {
15891589
sk_eat_skb(sk, skb, false);
15901590
++seq;
15911591
break;
@@ -1722,11 +1722,11 @@ int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
17221722
break;
17231723

17241724
offset = *seq - TCP_SKB_CB(skb)->seq;
1725-
if (tcp_hdr(skb)->syn)
1725+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)
17261726
offset--;
17271727
if (offset < skb->len)
17281728
goto found_ok_skb;
1729-
if (tcp_hdr(skb)->fin)
1729+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
17301730
goto found_fin_ok;
17311731
WARN(!(flags & MSG_PEEK),
17321732
"recvmsg bug 2: copied %X seq %X rcvnxt %X fl %X\n",
@@ -1959,7 +1959,7 @@ int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
19591959
if (used + offset < skb->len)
19601960
continue;
19611961

1962-
if (tcp_hdr(skb)->fin)
1962+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
19631963
goto found_fin_ok;
19641964
if (!(flags & MSG_PEEK)) {
19651965
sk_eat_skb(sk, skb, copied_early);
@@ -2160,8 +2160,10 @@ void tcp_close(struct sock *sk, long timeout)
21602160
* reader process may not have drained the data yet!
21612161
*/
21622162
while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
2163-
u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq -
2164-
tcp_hdr(skb)->fin;
2163+
u32 len = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq;
2164+
2165+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
2166+
len--;
21652167
data_was_unread += len;
21662168
__kfree_skb(skb);
21672169
}

‎net/ipv4/tcp_input.c‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,7 +4093,7 @@ static void tcp_ofo_queue(struct sock *sk)
40934093
__skb_unlink(skb, &tp->out_of_order_queue);
40944094
__skb_queue_tail(&sk->sk_receive_queue, skb);
40954095
tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
4096-
if (tcp_hdr(skb)->fin)
4096+
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
40974097
tcp_fin(sk);
40984098
}
40994099
}
@@ -4143,9 +4143,6 @@ static bool tcp_try_coalesce(struct sock *sk,
41434143

41444144
*fragstolen = false;
41454145

4146-
if (tcp_hdr(from)->fin)
4147-
return false;
4148-
41494146
/* Its possible this segment overlaps with prior segment in queue */
41504147
if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
41514148
return false;
@@ -4158,6 +4155,7 @@ static bool tcp_try_coalesce(struct sock *sk,
41584155
NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
41594156
TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
41604157
TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
4158+
TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
41614159
return true;
41624160
}
41634161

@@ -4513,7 +4511,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list,
45134511
* - bloated or contains data before "start" or
45144512
* overlaps to the next one.
45154513
*/
4516-
if (!tcp_hdr(skb)->syn && !tcp_hdr(skb)->fin &&
4514+
if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
45174515
(tcp_win_from_space(skb->truesize) > skb->len ||
45184516
before(TCP_SKB_CB(skb)->seq, start))) {
45194517
end_of_skbs = false;
@@ -4532,30 +4530,18 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list,
45324530
/* Decided to skip this, advance start seq. */
45334531
start = TCP_SKB_CB(skb)->end_seq;
45344532
}
4535-
if (end_of_skbs || tcp_hdr(skb)->syn || tcp_hdr(skb)->fin)
4533+
if (end_of_skbs ||
4534+
(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
45364535
return;
45374536

45384537
while (before(start, end)) {
4538+
int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
45394539
struct sk_buff *nskb;
4540-
unsigned int header = skb_headroom(skb);
4541-
int copy = SKB_MAX_ORDER(header, 0);
45424540

4543-
/* Too big header? This can happen with IPv6. */
4544-
if (copy < 0)
4545-
return;
4546-
if (end - start < copy)
4547-
copy = end - start;
4548-
nskb = alloc_skb(copy + header, GFP_ATOMIC);
4541+
nskb = alloc_skb(copy, GFP_ATOMIC);
45494542
if (!nskb)
45504543
return;
45514544

4552-
skb_set_mac_header(nskb, skb_mac_header(skb) - skb->head);
4553-
skb_set_network_header(nskb, (skb_network_header(skb) -
4554-
skb->head));
4555-
skb_set_transport_header(nskb, (skb_transport_header(skb) -
4556-
skb->head));
4557-
skb_reserve(nskb, header);
4558-
memcpy(nskb->head, skb->head, header);
45594545
memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
45604546
TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
45614547
__skb_queue_before(list, skb, nskb);
@@ -4579,8 +4565,7 @@ tcp_collapse(struct sock *sk, struct sk_buff_head *list,
45794565
skb = tcp_collapse_one(sk, skb, list);
45804566
if (!skb ||
45814567
skb == tail ||
4582-
tcp_hdr(skb)->syn ||
4583-
tcp_hdr(skb)->fin)
4568+
(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
45844569
return;
45854570
}
45864571
}

‎net/ipv4/tcp_ipv4.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,7 @@ int tcp_v4_rcv(struct sk_buff *skb)
16381638
TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
16391639
skb->len - th->doff * 4);
16401640
TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1641+
TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th);
16411642
TCP_SKB_CB(skb)->tcp_tw_isn = 0;
16421643
TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
16431644
TCP_SKB_CB(skb)->sacked = 0;

‎net/ipv6/tcp_ipv6.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ static int tcp_v6_rcv(struct sk_buff *skb)
14151415
TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
14161416
skb->len - th->doff*4);
14171417
TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1418+
TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th);
14181419
TCP_SKB_CB(skb)->tcp_tw_isn = 0;
14191420
TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
14201421
TCP_SKB_CB(skb)->sacked = 0;

0 commit comments

Comments
 (0)