Skip to contentSkip to conversation

Various tcp_probe module improvements

PATCH In review Daniel Borkmann 2 participants 7 messages Aug 21, 2013 – Aug 22, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Various tcp_probe module improvements

Future follow-up work could include a small, generic user space helper utility for iproute2's misc folder that would add a top-like display of both, TCP and SCTP's kprobe-based snoop modules.

Daniel Borkmann (3):
net: tcp_probe: also include rcv_wnd next to snd_wnd
net: tcp_probe: kprobes: adapt jtcp_rcv_established signature
net: tcp_probe: add IPv6 support

 net/ipv4/tcp_probe.c |   67 +++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 55 insertions(+), 12 deletions(-)
#1 lore raw

PATCH 1/3 net: tcp_probe: also include rcv_wnd next to snd_wnd

It is helpful to sometimes know the TCP window sizes of an established socket e.g. to confirm that window scaling is working or to tweak the window size to improve high-latency connections, etc etc. Currently the TCP snooper only exports the send window size, but not the receive window size. Therefore, also add the receive window size to the end of the output line.

---
 net/ipv4/tcp_probe.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
net/ipv4/tcp_probe.c +4 -2
index d4943f6..fae788b 100644
@@ -60,6 +60,7 @@ struct tcp_log {
 	u32	snd_nxt;
 	u32	snd_una;
 	u32	snd_wnd;
+	u32	rcv_wnd;
 	u32	snd_cwnd;
 	u32	ssthresh;
 	u32	srtt;
@@ -116,6 +117,7 @@ static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
 			p->snd_una = tp->snd_una;
 			p->snd_cwnd = tp->snd_cwnd;
 			p->snd_wnd = tp->snd_wnd;
+			p->rcv_wnd = tp->rcv_wnd;
 			p->ssthresh = tcp_current_ssthresh(sk);
 			p->srtt = tp->srtt >> 3;
 
@@ -157,13 +159,13 @@ static int tcpprobe_sprint(char *tbuf, int n)
 		= ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
 
 	return scnprintf(tbuf, n,
-			"%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u\n",
+			"%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u %u\n",
 			(unsigned long) tv.tv_sec,
 			(unsigned long) tv.tv_nsec,
 			&p->saddr, ntohs(p->sport),
 			&p->daddr, ntohs(p->dport),
 			p->length, p->snd_nxt, p->snd_una,
-			p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt);
+			p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt, p->rcv_wnd);
 }
 
 static ssize_t tcpprobe_read(struct file *file, char __user *buf,
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/3 net: tcp_probe: kprobes: adapt jtcp_rcv_established signature

In reply to Daniel Borkmann

This patches fixes a rather unproblematic function signature mismatch as the const specifier was missing for the th variable; and next to that it adds a build-time assertion so that future function signature mismatches for kprobes will not end badly, similarly as commit 22222997 ("net: sctp: add build check for sctp_sf_eat_sack_6_2/jsctp_sf_eat_sack") did it for SCTP.

---
 net/ipv4/tcp_probe.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
net/ipv4/tcp_probe.c +8 -1
index fae788b..a2392f4 100644
@@ -92,7 +92,7 @@ static inline int tcp_probe_avail(void)
  * Note: arguments must match tcp_rcv_established()!
  */
 static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
-			       struct tcphdr *th, unsigned int len)
+				const struct tcphdr *th, unsigned int len)
 {
 	const struct tcp_sock *tp = tcp_sk(sk);
 	const struct inet_sock *inet = inet_sk(sk);
@@ -225,6 +225,13 @@ static __init int tcpprobe_init(void)
 {
 	int ret = -ENOMEM;
 
+	/* Warning: if the function signature of tcp_rcv_established,
+	 * has been changed, you also have to change the signature of
+	 * jtcp_rcv_established, otherwise you end up right here!
+	 */
+	BUILD_BUG_ON(__same_type(tcp_rcv_established,
+				 jtcp_rcv_established) == 0);
+
 	init_waitqueue_head(&tcp_probe.wait);
 	spin_lock_init(&tcp_probe.lock);
 
Signature · 1 line
1.7.1
#3 lore raw

PATCH 3/3 net: tcp_probe: add IPv6 support

In reply to Daniel Borkmann

The tcp_probe currently only supports analysis of IPv4 connections. Therefore, it would be nice to have IPv6 supported as well. Since we have the recently added %pISpc specifier that is IPv4/IPv6 generic, build related sockaddress structures from the flow information and pass this to our format string. Tested with SSH and HTTP sessions on IPv4 and IPv6.

---
 net/ipv4/tcp_probe.c |   54 ++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 44 insertions(+), 10 deletions(-)
net/ipv4/tcp_probe.c +44 -10
index a2392f4..301a3ef 100644
@@ -54,8 +54,11 @@ static const char procname[] = "tcpprobe";
 
 struct tcp_log {
 	ktime_t tstamp;
-	__be32	saddr, daddr;
-	__be16	sport, dport;
+	union {
+		struct sockaddr		raw;
+		struct sockaddr_in	v4;
+		struct sockaddr_in6	v6;
+	}	src, dst;
 	u16	length;
 	u32	snd_nxt;
 	u32	snd_una;
@@ -87,6 +90,30 @@ static inline int tcp_probe_avail(void)
 	return bufsize - tcp_probe_used() - 1;
 }
 
+#define tcp_probe_copy_fl_to_si4(inet, si4, mem)		\
+	do {							\
+		si4.sin_family = AF_INET;			\
+		si4.sin_port = inet->inet_##mem##port;		\
+		si4.sin_addr.s_addr = inet->inet_##mem##addr;	\
+	} while (0)						\
+
+#if IS_ENABLED(CONFIG_IPV6)
+#define tcp_probe_copy_fl_to_si6(inet, si6, mem)		\
+	do {							\
+		struct ipv6_pinfo *pi6 = inet->pinet6;		\
+		si6.sin6_family = AF_INET6;			\
+		si6.sin6_port = inet->inet_##mem##port;		\
+		si6.sin6_addr = pi6->mem##addr;			\
+		si6.sin6_flowinfo = 0; /* No need here. */	\
+		si6.sin6_scope_id = 0;	/* No need here. */	\
+	} while (0)
+#else
+#define tcp_probe_copy_fl_to_si6(fl, si6, mem)			\
+	do {							\
+		memset(&si6, 0, sizeof(si6));			\
+	} while (0)
+#endif
+
 /*
  * Hook inserted to be called before each receive packet.
  * Note: arguments must match tcp_rcv_established()!
@@ -108,10 +135,19 @@ static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
 			struct tcp_log *p = tcp_probe.log + tcp_probe.head;
 
 			p->tstamp = ktime_get();
-			p->saddr = inet->inet_saddr;
-			p->sport = inet->inet_sport;
-			p->daddr = inet->inet_daddr;
-			p->dport = inet->inet_dport;
+			switch (sk->sk_family) {
+			case AF_INET:
+				tcp_probe_copy_fl_to_si4(inet, p->src.v4, s);
+				tcp_probe_copy_fl_to_si4(inet, p->dst.v4, d);
+				break;
+			case AF_INET6:
+				tcp_probe_copy_fl_to_si6(inet, p->src.v6, s);
+				tcp_probe_copy_fl_to_si6(inet, p->dst.v6, d);
+				break;
+			default:
+				BUG();
+			}
+
 			p->length = skb->len;
 			p->snd_nxt = tp->snd_nxt;
 			p->snd_una = tp->snd_una;
@@ -159,12 +195,10 @@ static int tcpprobe_sprint(char *tbuf, int n)
 		= ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start));
 
 	return scnprintf(tbuf, n,
-			"%lu.%09lu %pI4:%u %pI4:%u %d %#x %#x %u %u %u %u %u\n",
+			"%lu.%09lu %pISpc %pISpc %d %#x %#x %u %u %u %u %u\n",
 			(unsigned long) tv.tv_sec,
 			(unsigned long) tv.tv_nsec,
-			&p->saddr, ntohs(p->sport),
-			&p->daddr, ntohs(p->dport),
-			p->length, p->snd_nxt, p->snd_una,
+			&p->src, &p->dst, p->length, p->snd_nxt, p->snd_una,
 			p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt, p->rcv_wnd);
 }
 
Signature · 1 line
1.7.1
maintainer
#4 lore raw

Re: [PATCH net-next 3/3] net: tcp_probe: add IPv6 support

From: Daniel Borkmann <[email protected]>
Date: Wed, 21 Aug 2013 19:48:00 +0200

net/ipv4/tcp_probe.c static const char procname[] = "tcpprobe"; · line 61

Quoted 5 lines
net/ipv4/tcp_probe.c +5 -0
+	union {
+		struct sockaddr		raw;
+		struct sockaddr_in	v4;
+		struct sockaddr_in6	v6;
+	}	src, dst;

This bloats up the tcp_log structure unnecessarily. You have absolutely no use for the port member, for example.

I know you want to use this so that you can be lazy and only use the single printf format specifier. Here, keeping the tcp_log compact is more important.

#5 lore raw

Re: [PATCH net-next 3/3] net: tcp_probe: add IPv6 support

Quoted 12 lines · David Miller

From: Daniel Borkmann <[email protected]>
Date: Wed, 21 Aug 2013 19:48:00 +0200

net/ipv4/tcp_probe.c static const char procname[] = "tcpprobe"; · line 61

Quoted 5 lines
net/ipv4/tcp_probe.c +5 -0
+	union {
+		struct sockaddr		raw;
+		struct sockaddr_in	v4;
+		struct sockaddr_in6	v6;
+	}	src, dst;

This bloats up the tcp_log structure unnecessarily. You have absolutely no use for the port member, for example.

Quoted 3 lines

I know you want to use this so that you can be lazy and only use the single printf format specifier. Here, keeping the tcp_log compact is more important.

Well, the port is being filled out *and* used in both IPv4 and IPv6 cases. That is why %pISpc is being used, hence IP + port that is being printed. The port was of course also printed to the log before in IPv4 only case, so nothing would change there.

maintainer
#6 lore raw

Re: [PATCH net-next 3/3] net: tcp_probe: add IPv6 support

From: Daniel Borkmann <[email protected]>
Date: Thu, 22 Aug 2013 23:20:23 +0200

Quoted 13 lines · David Miller
Quoted 12 lines

From: Daniel Borkmann <[email protected]>
Date: Wed, 21 Aug 2013 19:48:00 +0200

net/ipv4/tcp_probe.c static const char procname[] = "tcpprobe"; · line 61

Quoted 5 lines
net/ipv4/tcp_probe.c +5 -0
+	union {
+		struct sockaddr		raw;
+		struct sockaddr_in	v4;
+		struct sockaddr_in6	v6;
+	}	src, dst;

This bloats up the tcp_log structure unnecessarily. You have absolutely no use for the port member, for example.

Quoted 8 lines
Quoted 3 lines

I know you want to use this so that you can be lazy and only use the single printf format specifier. Here, keeping the tcp_log compact is more important.

Well, the port is being filled out *and* used in both IPv4 and IPv6 cases. That is why %pISpc is being used, hence IP + port that is being printed. The port was of course also printed to the log before in IPv4 only case, so nothing would change there.

Good point, ok I'll reconsider this series.