Skip to content

Commit d260e9e

Browse files
committed
Merge branch 'tcp-sw-rx-timestamps'
Mike Maloney says: ==================== net: Add software rx timestamp for TCP. Add software rx timestamps for TCP, and a test to ensure consistency of behavior between IP, UDP, and TCP implementation. Changes since v1: -Initialize tss->ts[1] to 0 if caller requested any timestamps. -Fix test case to validate that tss->ts[1] is zero. -Fix tests to actually use a raw socket. -Fix --tcp flag to work on the test. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents b285477 + 16e7812 commit d260e9e

File tree

8 files changed

+501
-6
lines changed

8 files changed

+501
-6
lines changed

include/net/tcp.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,12 @@ struct tcp_skb_cb {
774774
u16 tcp_gso_segs;
775775
u16 tcp_gso_size;
776776
};
777+
778+
/* Used to stash the receive timestamp while this skb is in the
779+
* out of order queue, as skb->tstamp is overwritten by the
780+
* rbnode.
781+
*/
782+
ktime_t swtstamp;
777783
};
778784
__u8 tcp_flags; /* TCP header flags. (tcp[13]) */
779785

@@ -790,7 +796,8 @@ struct tcp_skb_cb {
790796
__u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */
791797
__u8 txstamp_ack:1, /* Record TX timestamp for ack? */
792798
eor:1, /* Is skb MSG_EOR marked? */
793-
unused:6;
799+
has_rxtstamp:1, /* SKB has a RX timestamp */
800+
unused:5;
794801
__u32 ack_seq; /* Sequence number ACK'd */
795802
union {
796803
struct {

net/ipv4/tcp.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@
269269
#include <linux/err.h>
270270
#include <linux/time.h>
271271
#include <linux/slab.h>
272+
#include <linux/errqueue.h>
272273

273274
#include <net/icmp.h>
274275
#include <net/inet_common.h>
@@ -1695,6 +1696,61 @@ int tcp_peek_len(struct socket *sock)
16951696
}
16961697
EXPORT_SYMBOL(tcp_peek_len);
16971698

1699+
static void tcp_update_recv_tstamps(struct sk_buff *skb,
1700+
struct scm_timestamping *tss)
1701+
{
1702+
if (skb->tstamp)
1703+
tss->ts[0] = ktime_to_timespec(skb->tstamp);
1704+
else
1705+
tss->ts[0] = (struct timespec) {0};
1706+
1707+
if (skb_hwtstamps(skb)->hwtstamp)
1708+
tss->ts[2] = ktime_to_timespec(skb_hwtstamps(skb)->hwtstamp);
1709+
else
1710+
tss->ts[2] = (struct timespec) {0};
1711+
}
1712+
1713+
/* Similar to __sock_recv_timestamp, but does not require an skb */
1714+
void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
1715+
struct scm_timestamping *tss)
1716+
{
1717+
struct timeval tv;
1718+
bool has_timestamping = false;
1719+
1720+
if (tss->ts[0].tv_sec || tss->ts[0].tv_nsec) {
1721+
if (sock_flag(sk, SOCK_RCVTSTAMP)) {
1722+
if (sock_flag(sk, SOCK_RCVTSTAMPNS)) {
1723+
put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
1724+
sizeof(tss->ts[0]), &tss->ts[0]);
1725+
} else {
1726+
tv.tv_sec = tss->ts[0].tv_sec;
1727+
tv.tv_usec = tss->ts[0].tv_nsec / 1000;
1728+
1729+
put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
1730+
sizeof(tv), &tv);
1731+
}
1732+
}
1733+
1734+
if (sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE)
1735+
has_timestamping = true;
1736+
else
1737+
tss->ts[0] = (struct timespec) {0};
1738+
}
1739+
1740+
if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec) {
1741+
if (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)
1742+
has_timestamping = true;
1743+
else
1744+
tss->ts[2] = (struct timespec) {0};
1745+
}
1746+
1747+
if (has_timestamping) {
1748+
tss->ts[1] = (struct timespec) {0};
1749+
put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING,
1750+
sizeof(*tss), tss);
1751+
}
1752+
}
1753+
16981754
/*
16991755
* This routine copies from a sock struct into the user buffer.
17001756
*
@@ -1716,6 +1772,8 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
17161772
long timeo;
17171773
struct sk_buff *skb, *last;
17181774
u32 urg_hole = 0;
1775+
struct scm_timestamping tss;
1776+
bool has_tss = false;
17191777

17201778
if (unlikely(flags & MSG_ERRQUEUE))
17211779
return inet_recv_error(sk, msg, len, addr_len);
@@ -1911,6 +1969,10 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
19111969
if (used + offset < skb->len)
19121970
continue;
19131971

1972+
if (TCP_SKB_CB(skb)->has_rxtstamp) {
1973+
tcp_update_recv_tstamps(skb, &tss);
1974+
has_tss = true;
1975+
}
19141976
if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
19151977
goto found_fin_ok;
19161978
if (!(flags & MSG_PEEK))
@@ -1929,6 +1991,9 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
19291991
* on connected socket. I was just happy when found this 8) --ANK
19301992
*/
19311993

1994+
if (has_tss)
1995+
tcp_recv_timestamp(msg, sk, &tss);
1996+
19321997
/* Clean up data we have read: This will do ACK frames. */
19331998
tcp_cleanup_rbuf(sk, copied);
19341999

net/ipv4/tcp_input.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4246,9 +4246,15 @@ static void tcp_sack_remove(struct tcp_sock *tp)
42464246
tp->rx_opt.num_sacks = num_sacks;
42474247
}
42484248

4249+
enum tcp_queue {
4250+
OOO_QUEUE,
4251+
RCV_QUEUE,
4252+
};
4253+
42494254
/**
42504255
* tcp_try_coalesce - try to merge skb to prior one
42514256
* @sk: socket
4257+
* @dest: destination queue
42524258
* @to: prior buffer
42534259
* @from: buffer to add in queue
42544260
* @fragstolen: pointer to boolean
@@ -4260,6 +4266,7 @@ static void tcp_sack_remove(struct tcp_sock *tp)
42604266
* Returns true if caller should free @from instead of queueing it
42614267
*/
42624268
static bool tcp_try_coalesce(struct sock *sk,
4269+
enum tcp_queue dest,
42634270
struct sk_buff *to,
42644271
struct sk_buff *from,
42654272
bool *fragstolen)
@@ -4281,6 +4288,15 @@ static bool tcp_try_coalesce(struct sock *sk,
42814288
TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
42824289
TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
42834290
TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
4291+
4292+
if (TCP_SKB_CB(from)->has_rxtstamp) {
4293+
TCP_SKB_CB(to)->has_rxtstamp = true;
4294+
if (dest == OOO_QUEUE)
4295+
TCP_SKB_CB(to)->swtstamp = TCP_SKB_CB(from)->swtstamp;
4296+
else
4297+
to->tstamp = from->tstamp;
4298+
}
4299+
42844300
return true;
42854301
}
42864302

@@ -4315,6 +4331,9 @@ static void tcp_ofo_queue(struct sock *sk)
43154331
}
43164332
p = rb_next(p);
43174333
rb_erase(&skb->rbnode, &tp->out_of_order_queue);
4334+
/* Replace tstamp which was stomped by rbnode */
4335+
if (TCP_SKB_CB(skb)->has_rxtstamp)
4336+
skb->tstamp = TCP_SKB_CB(skb)->swtstamp;
43184337

43194338
if (unlikely(!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
43204339
SOCK_DEBUG(sk, "ofo packet was already received\n");
@@ -4326,7 +4345,8 @@ static void tcp_ofo_queue(struct sock *sk)
43264345
TCP_SKB_CB(skb)->end_seq);
43274346

43284347
tail = skb_peek_tail(&sk->sk_receive_queue);
4329-
eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
4348+
eaten = tail && tcp_try_coalesce(sk, RCV_QUEUE,
4349+
tail, skb, &fragstolen);
43304350
tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
43314351
fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
43324352
if (!eaten)
@@ -4380,6 +4400,10 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
43804400
return;
43814401
}
43824402

4403+
/* Stash tstamp to avoid being stomped on by rbnode */
4404+
if (TCP_SKB_CB(skb)->has_rxtstamp)
4405+
TCP_SKB_CB(skb)->swtstamp = skb->tstamp;
4406+
43834407
inet_csk_schedule_ack(sk);
43844408

43854409
NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
@@ -4405,7 +4429,8 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
44054429
/* In the typical case, we are adding an skb to the end of the list.
44064430
* Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
44074431
*/
4408-
if (tcp_try_coalesce(sk, tp->ooo_last_skb, skb, &fragstolen)) {
4432+
if (tcp_try_coalesce(sk, OOO_QUEUE, tp->ooo_last_skb,
4433+
skb, &fragstolen)) {
44094434
coalesce_done:
44104435
tcp_grow_window(sk, skb);
44114436
kfree_skb_partial(skb, fragstolen);
@@ -4455,7 +4480,8 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
44554480
__kfree_skb(skb1);
44564481
goto merge_right;
44574482
}
4458-
} else if (tcp_try_coalesce(sk, skb1, skb, &fragstolen)) {
4483+
} else if (tcp_try_coalesce(sk, OOO_QUEUE, skb1,
4484+
skb, &fragstolen)) {
44594485
goto coalesce_done;
44604486
}
44614487
p = &parent->rb_right;
@@ -4506,7 +4532,8 @@ static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int
45064532

45074533
__skb_pull(skb, hdrlen);
45084534
eaten = (tail &&
4509-
tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0;
4535+
tcp_try_coalesce(sk, RCV_QUEUE, tail,
4536+
skb, fragstolen)) ? 1 : 0;
45104537
tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
45114538
if (!eaten) {
45124539
__skb_queue_tail(&sk->sk_receive_queue, skb);

net/ipv4/tcp_ipv4.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,8 @@ int tcp_v4_rcv(struct sk_buff *skb)
16371637
TCP_SKB_CB(skb)->tcp_tw_isn = 0;
16381638
TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
16391639
TCP_SKB_CB(skb)->sacked = 0;
1640+
TCP_SKB_CB(skb)->has_rxtstamp =
1641+
skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
16401642

16411643
lookup:
16421644
sk = __inet_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th), th->source,

net/ipv6/tcp_ipv6.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,8 @@ static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,
13941394
TCP_SKB_CB(skb)->tcp_tw_isn = 0;
13951395
TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
13961396
TCP_SKB_CB(skb)->sacked = 0;
1397+
TCP_SKB_CB(skb)->has_rxtstamp =
1398+
skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
13971399
}
13981400

13991401
static int tcp_v6_rcv(struct sk_buff *skb)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
timestamping
2+
rxtimestamp
23
txtimestamp
34
hwtstamp_config

tools/testing/selftests/networking/timestamping/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
TEST_PROGS := hwtstamp_config timestamping txtimestamp
1+
CFLAGS += -I../../../../../usr/include
2+
3+
TEST_PROGS := hwtstamp_config rxtimestamp timestamping txtimestamp
24

35
all: $(TEST_PROGS)
46

0 commit comments

Comments
 (0)