Skip to content

Commit

Permalink
Merge branch 'tcp-sw-rx-timestamps'
Browse files Browse the repository at this point in the history
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 <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Aug 24, 2017
2 parents b285477 + 16e7812 commit d260e9e
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 6 deletions.
9 changes: 8 additions & 1 deletion include/net/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,12 @@ struct tcp_skb_cb {
u16 tcp_gso_segs;
u16 tcp_gso_size;
};

/* Used to stash the receive timestamp while this skb is in the
* out of order queue, as skb->tstamp is overwritten by the
* rbnode.
*/
ktime_t swtstamp;
};
__u8 tcp_flags; /* TCP header flags. (tcp[13]) */

Expand All @@ -790,7 +796,8 @@ struct tcp_skb_cb {
__u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */
__u8 txstamp_ack:1, /* Record TX timestamp for ack? */
eor:1, /* Is skb MSG_EOR marked? */
unused:6;
has_rxtstamp:1, /* SKB has a RX timestamp */
unused:5;
__u32 ack_seq; /* Sequence number ACK'd */
union {
struct {
Expand Down
65 changes: 65 additions & 0 deletions net/ipv4/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
#include <linux/err.h>
#include <linux/time.h>
#include <linux/slab.h>
#include <linux/errqueue.h>

#include <net/icmp.h>
#include <net/inet_common.h>
Expand Down Expand Up @@ -1695,6 +1696,61 @@ int tcp_peek_len(struct socket *sock)
}
EXPORT_SYMBOL(tcp_peek_len);

static void tcp_update_recv_tstamps(struct sk_buff *skb,
struct scm_timestamping *tss)
{
if (skb->tstamp)
tss->ts[0] = ktime_to_timespec(skb->tstamp);
else
tss->ts[0] = (struct timespec) {0};

if (skb_hwtstamps(skb)->hwtstamp)
tss->ts[2] = ktime_to_timespec(skb_hwtstamps(skb)->hwtstamp);
else
tss->ts[2] = (struct timespec) {0};
}

/* Similar to __sock_recv_timestamp, but does not require an skb */
void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
struct scm_timestamping *tss)
{
struct timeval tv;
bool has_timestamping = false;

if (tss->ts[0].tv_sec || tss->ts[0].tv_nsec) {
if (sock_flag(sk, SOCK_RCVTSTAMP)) {
if (sock_flag(sk, SOCK_RCVTSTAMPNS)) {
put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
sizeof(tss->ts[0]), &tss->ts[0]);
} else {
tv.tv_sec = tss->ts[0].tv_sec;
tv.tv_usec = tss->ts[0].tv_nsec / 1000;

put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
sizeof(tv), &tv);
}
}

if (sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE)
has_timestamping = true;
else
tss->ts[0] = (struct timespec) {0};
}

if (tss->ts[2].tv_sec || tss->ts[2].tv_nsec) {
if (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE)
has_timestamping = true;
else
tss->ts[2] = (struct timespec) {0};
}

if (has_timestamping) {
tss->ts[1] = (struct timespec) {0};
put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING,
sizeof(*tss), tss);
}
}

/*
* This routine copies from a sock struct into the user buffer.
*
Expand All @@ -1716,6 +1772,8 @@ int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
long timeo;
struct sk_buff *skb, *last;
u32 urg_hole = 0;
struct scm_timestamping tss;
bool has_tss = false;

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

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

if (has_tss)
tcp_recv_timestamp(msg, sk, &tss);

/* Clean up data we have read: This will do ACK frames. */
tcp_cleanup_rbuf(sk, copied);

Expand Down
35 changes: 31 additions & 4 deletions net/ipv4/tcp_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -4246,9 +4246,15 @@ static void tcp_sack_remove(struct tcp_sock *tp)
tp->rx_opt.num_sacks = num_sacks;
}

enum tcp_queue {
OOO_QUEUE,
RCV_QUEUE,
};

/**
* tcp_try_coalesce - try to merge skb to prior one
* @sk: socket
* @dest: destination queue
* @to: prior buffer
* @from: buffer to add in queue
* @fragstolen: pointer to boolean
Expand All @@ -4260,6 +4266,7 @@ static void tcp_sack_remove(struct tcp_sock *tp)
* Returns true if caller should free @from instead of queueing it
*/
static bool tcp_try_coalesce(struct sock *sk,
enum tcp_queue dest,
struct sk_buff *to,
struct sk_buff *from,
bool *fragstolen)
Expand All @@ -4281,6 +4288,15 @@ static bool tcp_try_coalesce(struct sock *sk,
TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;

if (TCP_SKB_CB(from)->has_rxtstamp) {
TCP_SKB_CB(to)->has_rxtstamp = true;
if (dest == OOO_QUEUE)
TCP_SKB_CB(to)->swtstamp = TCP_SKB_CB(from)->swtstamp;
else
to->tstamp = from->tstamp;
}

return true;
}

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

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

tail = skb_peek_tail(&sk->sk_receive_queue);
eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
eaten = tail && tcp_try_coalesce(sk, RCV_QUEUE,
tail, skb, &fragstolen);
tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
if (!eaten)
Expand Down Expand Up @@ -4380,6 +4400,10 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
return;
}

/* Stash tstamp to avoid being stomped on by rbnode */
if (TCP_SKB_CB(skb)->has_rxtstamp)
TCP_SKB_CB(skb)->swtstamp = skb->tstamp;

inet_csk_schedule_ack(sk);

NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
Expand All @@ -4405,7 +4429,8 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
/* In the typical case, we are adding an skb to the end of the list.
* Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
*/
if (tcp_try_coalesce(sk, tp->ooo_last_skb, skb, &fragstolen)) {
if (tcp_try_coalesce(sk, OOO_QUEUE, tp->ooo_last_skb,
skb, &fragstolen)) {
coalesce_done:
tcp_grow_window(sk, skb);
kfree_skb_partial(skb, fragstolen);
Expand Down Expand Up @@ -4455,7 +4480,8 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
__kfree_skb(skb1);
goto merge_right;
}
} else if (tcp_try_coalesce(sk, skb1, skb, &fragstolen)) {
} else if (tcp_try_coalesce(sk, OOO_QUEUE, skb1,
skb, &fragstolen)) {
goto coalesce_done;
}
p = &parent->rb_right;
Expand Down Expand Up @@ -4506,7 +4532,8 @@ static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int

__skb_pull(skb, hdrlen);
eaten = (tail &&
tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0;
tcp_try_coalesce(sk, RCV_QUEUE, tail,
skb, fragstolen)) ? 1 : 0;
tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
if (!eaten) {
__skb_queue_tail(&sk->sk_receive_queue, skb);
Expand Down
2 changes: 2 additions & 0 deletions net/ipv4/tcp_ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,8 @@ int tcp_v4_rcv(struct sk_buff *skb)
TCP_SKB_CB(skb)->tcp_tw_isn = 0;
TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
TCP_SKB_CB(skb)->sacked = 0;
TCP_SKB_CB(skb)->has_rxtstamp =
skb->tstamp || skb_hwtstamps(skb)->hwtstamp;

lookup:
sk = __inet_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th), th->source,
Expand Down
2 changes: 2 additions & 0 deletions net/ipv6/tcp_ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,8 @@ static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,
TCP_SKB_CB(skb)->tcp_tw_isn = 0;
TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
TCP_SKB_CB(skb)->sacked = 0;
TCP_SKB_CB(skb)->has_rxtstamp =
skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
}

static int tcp_v6_rcv(struct sk_buff *skb)
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/networking/timestamping/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
timestamping
rxtimestamp
txtimestamp
hwtstamp_config
4 changes: 3 additions & 1 deletion tools/testing/selftests/networking/timestamping/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
TEST_PROGS := hwtstamp_config timestamping txtimestamp
CFLAGS += -I../../../../../usr/include

TEST_PROGS := hwtstamp_config rxtimestamp timestamping txtimestamp

all: $(TEST_PROGS)

Expand Down
Loading

0 comments on commit d260e9e

Please sign in to comment.