tcp: add tracepoint trace_tcp_set_state()
authorSong Liu <songliubraving@fb.com>
Mon, 23 Oct 2017 16:20:27 +0000 (09:20 -0700)
committerDavid S. Miller <davem@davemloft.net>
Tue, 24 Oct 2017 00:21:25 +0000 (01:21 +0100)
This patch adds tracepoint trace_tcp_set_state. Besides usual fields
(s/d ports, IP addresses), old and new state of the socket is also
printed with TP_printk, with __print_symbolic().

Signed-off-by: Song Liu <songliubraving@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/trace/events/tcp.h
net/ipv4/tcp.c

index 1724c12c25cf026b7c69fb2455ca1463598185d0..03699ba716230882db91bf8d2bbc11d12099accd 100644 (file)
@@ -9,6 +9,22 @@
 #include <linux/tracepoint.h>
 #include <net/ipv6.h>
 
+#define tcp_state_name(state)  { state, #state }
+#define show_tcp_state_name(val)                       \
+       __print_symbolic(val,                           \
+               tcp_state_name(TCP_ESTABLISHED),        \
+               tcp_state_name(TCP_SYN_SENT),           \
+               tcp_state_name(TCP_SYN_RECV),           \
+               tcp_state_name(TCP_FIN_WAIT1),          \
+               tcp_state_name(TCP_FIN_WAIT2),          \
+               tcp_state_name(TCP_TIME_WAIT),          \
+               tcp_state_name(TCP_CLOSE),              \
+               tcp_state_name(TCP_CLOSE_WAIT),         \
+               tcp_state_name(TCP_LAST_ACK),           \
+               tcp_state_name(TCP_LISTEN),             \
+               tcp_state_name(TCP_CLOSING),            \
+               tcp_state_name(TCP_NEW_SYN_RECV))
+
 /*
  * tcp event with arguments sk and skb
  *
@@ -161,6 +177,66 @@ DEFINE_EVENT(tcp_event_sk, tcp_destroy_sock,
        TP_ARGS(sk)
 );
 
+TRACE_EVENT(tcp_set_state,
+
+       TP_PROTO(const struct sock *sk, const int oldstate, const int newstate),
+
+       TP_ARGS(sk, oldstate, newstate),
+
+       TP_STRUCT__entry(
+               __field(const void *, skaddr)
+               __field(int, oldstate)
+               __field(int, newstate)
+               __field(__u16, sport)
+               __field(__u16, dport)
+               __array(__u8, saddr, 4)
+               __array(__u8, daddr, 4)
+               __array(__u8, saddr_v6, 16)
+               __array(__u8, daddr_v6, 16)
+       ),
+
+       TP_fast_assign(
+               struct inet_sock *inet = inet_sk(sk);
+               struct in6_addr *pin6;
+               __be32 *p32;
+
+               __entry->skaddr = sk;
+               __entry->oldstate = oldstate;
+               __entry->newstate = newstate;
+
+               __entry->sport = ntohs(inet->inet_sport);
+               __entry->dport = ntohs(inet->inet_dport);
+
+               p32 = (__be32 *) __entry->saddr;
+               *p32 = inet->inet_saddr;
+
+               p32 = (__be32 *) __entry->daddr;
+               *p32 =  inet->inet_daddr;
+
+#if IS_ENABLED(CONFIG_IPV6)
+               if (sk->sk_family == AF_INET6) {
+                       pin6 = (struct in6_addr *)__entry->saddr_v6;
+                       *pin6 = sk->sk_v6_rcv_saddr;
+                       pin6 = (struct in6_addr *)__entry->daddr_v6;
+                       *pin6 = sk->sk_v6_daddr;
+               } else
+#endif
+               {
+                       pin6 = (struct in6_addr *)__entry->saddr_v6;
+                       ipv6_addr_set_v4mapped(inet->inet_saddr, pin6);
+                       pin6 = (struct in6_addr *)__entry->daddr_v6;
+                       ipv6_addr_set_v4mapped(inet->inet_daddr, pin6);
+               }
+       ),
+
+       TP_printk("sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c oldstate=%s newstate=%s",
+                 __entry->sport, __entry->dport,
+                 __entry->saddr, __entry->daddr,
+                 __entry->saddr_v6, __entry->daddr_v6,
+                 show_tcp_state_name(__entry->oldstate),
+                 show_tcp_state_name(__entry->newstate))
+);
+
 #endif /* _TRACE_TCP_H */
 
 /* This part must be outside protection */
index 8b1fa4dd4538439b80e89b669edf1da8121bcb85..be07e9b6dbdde405ea1f4806df0c062a3b3c9e1f 100644 (file)
 #include <asm/ioctls.h>
 #include <net/busy_poll.h>
 
+#include <trace/events/tcp.h>
+
 int sysctl_tcp_min_tso_segs __read_mostly = 2;
 
 int sysctl_tcp_autocorking __read_mostly = 1;
@@ -2040,6 +2042,8 @@ void tcp_set_state(struct sock *sk, int state)
 {
        int oldstate = sk->sk_state;
 
+       trace_tcp_set_state(sk, oldstate, state);
+
        switch (state) {
        case TCP_ESTABLISHED:
                if (oldstate != TCP_ESTABLISHED)