Skip to content

Commit e8fce23

Browse files
liu-song-6davem330
authored andcommitted
tcp: add tracepoint trace_tcp_set_state()
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>
1 parent e1a4aa5 commit e8fce23

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

include/trace/events/tcp.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99
#include <linux/tracepoint.h>
1010
#include <net/ipv6.h>
1111

12+
#define tcp_state_name(state) { state, #state }
13+
#define show_tcp_state_name(val) \
14+
__print_symbolic(val, \
15+
tcp_state_name(TCP_ESTABLISHED), \
16+
tcp_state_name(TCP_SYN_SENT), \
17+
tcp_state_name(TCP_SYN_RECV), \
18+
tcp_state_name(TCP_FIN_WAIT1), \
19+
tcp_state_name(TCP_FIN_WAIT2), \
20+
tcp_state_name(TCP_TIME_WAIT), \
21+
tcp_state_name(TCP_CLOSE), \
22+
tcp_state_name(TCP_CLOSE_WAIT), \
23+
tcp_state_name(TCP_LAST_ACK), \
24+
tcp_state_name(TCP_LISTEN), \
25+
tcp_state_name(TCP_CLOSING), \
26+
tcp_state_name(TCP_NEW_SYN_RECV))
27+
1228
/*
1329
* tcp event with arguments sk and skb
1430
*
@@ -161,6 +177,66 @@ DEFINE_EVENT(tcp_event_sk, tcp_destroy_sock,
161177
TP_ARGS(sk)
162178
);
163179

180+
TRACE_EVENT(tcp_set_state,
181+
182+
TP_PROTO(const struct sock *sk, const int oldstate, const int newstate),
183+
184+
TP_ARGS(sk, oldstate, newstate),
185+
186+
TP_STRUCT__entry(
187+
__field(const void *, skaddr)
188+
__field(int, oldstate)
189+
__field(int, newstate)
190+
__field(__u16, sport)
191+
__field(__u16, dport)
192+
__array(__u8, saddr, 4)
193+
__array(__u8, daddr, 4)
194+
__array(__u8, saddr_v6, 16)
195+
__array(__u8, daddr_v6, 16)
196+
),
197+
198+
TP_fast_assign(
199+
struct inet_sock *inet = inet_sk(sk);
200+
struct in6_addr *pin6;
201+
__be32 *p32;
202+
203+
__entry->skaddr = sk;
204+
__entry->oldstate = oldstate;
205+
__entry->newstate = newstate;
206+
207+
__entry->sport = ntohs(inet->inet_sport);
208+
__entry->dport = ntohs(inet->inet_dport);
209+
210+
p32 = (__be32 *) __entry->saddr;
211+
*p32 = inet->inet_saddr;
212+
213+
p32 = (__be32 *) __entry->daddr;
214+
*p32 = inet->inet_daddr;
215+
216+
#if IS_ENABLED(CONFIG_IPV6)
217+
if (sk->sk_family == AF_INET6) {
218+
pin6 = (struct in6_addr *)__entry->saddr_v6;
219+
*pin6 = sk->sk_v6_rcv_saddr;
220+
pin6 = (struct in6_addr *)__entry->daddr_v6;
221+
*pin6 = sk->sk_v6_daddr;
222+
} else
223+
#endif
224+
{
225+
pin6 = (struct in6_addr *)__entry->saddr_v6;
226+
ipv6_addr_set_v4mapped(inet->inet_saddr, pin6);
227+
pin6 = (struct in6_addr *)__entry->daddr_v6;
228+
ipv6_addr_set_v4mapped(inet->inet_daddr, pin6);
229+
}
230+
),
231+
232+
TP_printk("sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c oldstate=%s newstate=%s",
233+
__entry->sport, __entry->dport,
234+
__entry->saddr, __entry->daddr,
235+
__entry->saddr_v6, __entry->daddr_v6,
236+
show_tcp_state_name(__entry->oldstate),
237+
show_tcp_state_name(__entry->newstate))
238+
);
239+
164240
#endif /* _TRACE_TCP_H */
165241

166242
/* This part must be outside protection */

net/ipv4/tcp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@
282282
#include <asm/ioctls.h>
283283
#include <net/busy_poll.h>
284284

285+
#include <trace/events/tcp.h>
286+
285287
int sysctl_tcp_min_tso_segs __read_mostly = 2;
286288

287289
int sysctl_tcp_autocorking __read_mostly = 1;
@@ -2040,6 +2042,8 @@ void tcp_set_state(struct sock *sk, int state)
20402042
{
20412043
int oldstate = sk->sk_state;
20422044

2045+
trace_tcp_set_state(sk, oldstate, state);
2046+
20432047
switch (state) {
20442048
case TCP_ESTABLISHED:
20452049
if (oldstate != TCP_ESTABLISHED)

0 commit comments

Comments
 (0)