Re: [PATCH net-next 2/2] tcp: handle TCP_TIME_WAIT/TCP_NEW_SYN_RECV in tcp_set_state tracepoint

From: Eric Dumazet
Date: Thu Nov 09 2017 - 09:52:54 EST


On Thu, 2017-11-09 at 14:26 +0000, Yafang Shao wrote:
> When TCP connetion in TCP_TIME_WAIT or TCP_NEW_SYN_RECV state, it can't
> get the sport/dport/saddr/daddr from inet_sock.
>
> trace_tcp_set_state may be called when the oldstate in these two states.
>
> Signed-off-by: Yafang Shao <laoar.shao@xxxxxxxxx>
> ---
> include/trace/events/tcp.h | 33 ++++++++++++++++++++++++++-------
> 1 file changed, 26 insertions(+), 7 deletions(-)
>
> diff --git a/include/trace/events/tcp.h b/include/trace/events/tcp.h
> index 07cccca..1982a71 100644
> --- a/include/trace/events/tcp.h
> +++ b/include/trace/events/tcp.h
> @@ -196,7 +196,6 @@
> ),
>
> TP_fast_assign(
> - struct inet_sock *inet = inet_sk(sk);
> struct in6_addr *pin6;
> __be32 *p32;
>
> @@ -204,14 +203,34 @@
> __entry->oldstate = oldstate;
> __entry->newstate = newstate;
>
> - __entry->sport = ntohs(inet->inet_sport);
> - __entry->dport = ntohs(inet->inet_dport);
> + if (oldstate == TCP_TIME_WAIT) {
> + __entry->sport = ntohs(inet_twsk(sk)->tw_sport);
> + __entry->dport = ntohs(inet_twsk(sk)->tw_dport);
>
> - p32 = (__be32 *) __entry->saddr;
> - *p32 = inet->inet_saddr;
> + p32 = (__be32 *) __entry->saddr;
> + *p32 = inet_twsk(sk)->tw_rcv_saddr;
>
> - p32 = (__be32 *) __entry->daddr;
> - *p32 = inet->inet_daddr;
> + p32 = (__be32 *) __entry->daddr;
> + *p32 = inet_twsk(sk)->tw_daddr;
> + } else if (oldstate == TCP_NEW_SYN_RECV) {
> + __entry->sport = inet_rsk(inet_reqsk(sk))->ir_num;
> + __entry->dport = ntohs(inet_rsk(inet_reqsk(sk))->ir_rmt_port);
> +
> + p32 = (__be32 *) __entry->saddr;
> + *p32 = inet_rsk(inet_reqsk(sk))->ir_loc_addr;
> +
> + p32 = (__be32 *) __entry->daddr;
> + *p32 = inet_rsk(inet_reqsk(sk))->ir_rmt_addr;
> + } else {
> + __entry->sport = ntohs(inet_sk(sk)->inet_sport);
> + __entry->dport = ntohs(inet_sk(sk)->inet_dport);
> +
> + p32 = (__be32 *) __entry->saddr;
> + *p32 = inet_sk(sk)->inet_saddr;
> +
> + p32 = (__be32 *) __entry->daddr;
> + *p32 = inet_sk(sk)->inet_daddr;
> + }


Wow.


Since all three variants of sockets (full sockets, request sockets,
timewait sockets) are all hashed into ehash table these days, they all
have the fields at the same offset

For IPv4, that would be :

__sk_common.skc_daddr (or inet_daddr)
__sk_common.skc_rcv_saddr (or inet_rcv_saddr )
__sk_common.skc_dport (or inet_dport)
__sk_common.skc_num (or inet_num)

Look at __inet_lookup_established() and INET_MATCH() : They deal with
the three variants, without having to look at sk_state.

If you were using the fields that are common to all sockets, no need to
add all this unnecessary complexity.