Skip to content

Commit 563e0bb

Browse files
laoardavem330
authored andcommitted
net: tracepoint: replace tcp_set_state tracepoint with inet_sock_set_state tracepoint
As sk_state is a common field for struct sock, so the state transition tracepoint should not be a TCP specific feature. Currently it traces all AF_INET state transition, so I rename this tracepoint to inet_sock_set_state tracepoint with some minor changes and move it into trace/events/sock.h. We dont need to create a file named trace/events/inet_sock.h for this one single tracepoint. Two helpers are introduced to trace sk_state transition - void inet_sk_state_store(struct sock *sk, int newstate); - void inet_sk_set_state(struct sock *sk, int state); As trace header should not be included in other header files, so they are defined in sock.c. The protocol such as SCTP maybe compiled as a ko, hence export inet_sk_set_state(). Signed-off-by: Yafang Shao <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d7b850a commit 563e0bb

File tree

7 files changed

+128
-40
lines changed

7 files changed

+128
-40
lines changed

include/net/inet_sock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ static inline void inet_sk_copy_descendant(struct sock *sk_to,
290290
#endif
291291

292292
int inet_sk_rebuild_header(struct sock *sk);
293+
void inet_sk_set_state(struct sock *sk, int state);
294+
void inet_sk_state_store(struct sock *sk, int newstate);
293295

294296
static inline unsigned int __inet_ehashfn(const __be32 laddr,
295297
const __u16 lport,

include/trace/events/sock.h

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,50 @@
66
#define _TRACE_SOCK_H
77

88
#include <net/sock.h>
9+
#include <net/ipv6.h>
910
#include <linux/tracepoint.h>
11+
#include <linux/ipv6.h>
12+
#include <linux/tcp.h>
13+
14+
/* The protocol traced by sock_set_state */
15+
#define inet_protocol_names \
16+
EM(IPPROTO_TCP) \
17+
EM(IPPROTO_DCCP) \
18+
EMe(IPPROTO_SCTP)
19+
20+
#define tcp_state_names \
21+
EM(TCP_ESTABLISHED) \
22+
EM(TCP_SYN_SENT) \
23+
EM(TCP_SYN_RECV) \
24+
EM(TCP_FIN_WAIT1) \
25+
EM(TCP_FIN_WAIT2) \
26+
EM(TCP_TIME_WAIT) \
27+
EM(TCP_CLOSE) \
28+
EM(TCP_CLOSE_WAIT) \
29+
EM(TCP_LAST_ACK) \
30+
EM(TCP_LISTEN) \
31+
EM(TCP_CLOSING) \
32+
EMe(TCP_NEW_SYN_RECV)
33+
34+
/* enums need to be exported to user space */
35+
#undef EM
36+
#undef EMe
37+
#define EM(a) TRACE_DEFINE_ENUM(a);
38+
#define EMe(a) TRACE_DEFINE_ENUM(a);
39+
40+
inet_protocol_names
41+
tcp_state_names
42+
43+
#undef EM
44+
#undef EMe
45+
#define EM(a) { a, #a },
46+
#define EMe(a) { a, #a }
47+
48+
#define show_inet_protocol_name(val) \
49+
__print_symbolic(val, inet_protocol_names)
50+
51+
#define show_tcp_state_name(val) \
52+
__print_symbolic(val, tcp_state_names)
1053

1154
TRACE_EVENT(sock_rcvqueue_full,
1255

@@ -63,6 +106,70 @@ TRACE_EVENT(sock_exceed_buf_limit,
63106
__entry->rmem_alloc)
64107
);
65108

109+
TRACE_EVENT(inet_sock_set_state,
110+
111+
TP_PROTO(const struct sock *sk, const int oldstate, const int newstate),
112+
113+
TP_ARGS(sk, oldstate, newstate),
114+
115+
TP_STRUCT__entry(
116+
__field(const void *, skaddr)
117+
__field(int, oldstate)
118+
__field(int, newstate)
119+
__field(__u16, sport)
120+
__field(__u16, dport)
121+
__field(__u8, protocol)
122+
__array(__u8, saddr, 4)
123+
__array(__u8, daddr, 4)
124+
__array(__u8, saddr_v6, 16)
125+
__array(__u8, daddr_v6, 16)
126+
),
127+
128+
TP_fast_assign(
129+
struct inet_sock *inet = inet_sk(sk);
130+
struct in6_addr *pin6;
131+
__be32 *p32;
132+
133+
__entry->skaddr = sk;
134+
__entry->oldstate = oldstate;
135+
__entry->newstate = newstate;
136+
137+
__entry->protocol = sk->sk_protocol;
138+
__entry->sport = ntohs(inet->inet_sport);
139+
__entry->dport = ntohs(inet->inet_dport);
140+
141+
p32 = (__be32 *) __entry->saddr;
142+
*p32 = inet->inet_saddr;
143+
144+
p32 = (__be32 *) __entry->daddr;
145+
*p32 = inet->inet_daddr;
146+
147+
#if IS_ENABLED(CONFIG_IPV6)
148+
if (sk->sk_family == AF_INET6) {
149+
pin6 = (struct in6_addr *)__entry->saddr_v6;
150+
*pin6 = sk->sk_v6_rcv_saddr;
151+
pin6 = (struct in6_addr *)__entry->daddr_v6;
152+
*pin6 = sk->sk_v6_daddr;
153+
} else
154+
#endif
155+
{
156+
pin6 = (struct in6_addr *)__entry->saddr_v6;
157+
ipv6_addr_set_v4mapped(inet->inet_saddr, pin6);
158+
pin6 = (struct in6_addr *)__entry->daddr_v6;
159+
ipv6_addr_set_v4mapped(inet->inet_daddr, pin6);
160+
}
161+
),
162+
163+
TP_printk("protocol=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4"
164+
"saddrv6=%pI6c daddrv6=%pI6c oldstate=%s newstate=%s",
165+
show_inet_protocol_name(__entry->protocol),
166+
__entry->sport, __entry->dport,
167+
__entry->saddr, __entry->daddr,
168+
__entry->saddr_v6, __entry->daddr_v6,
169+
show_tcp_state_name(__entry->oldstate),
170+
show_tcp_state_name(__entry->newstate))
171+
);
172+
66173
#endif /* _TRACE_SOCK_H */
67174

68175
/* This part must be outside protection */

include/trace/events/tcp.h

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

12-
#define tcp_state_names \
13-
EM(TCP_ESTABLISHED) \
14-
EM(TCP_SYN_SENT) \
15-
EM(TCP_SYN_RECV) \
16-
EM(TCP_FIN_WAIT1) \
17-
EM(TCP_FIN_WAIT2) \
18-
EM(TCP_TIME_WAIT) \
19-
EM(TCP_CLOSE) \
20-
EM(TCP_CLOSE_WAIT) \
21-
EM(TCP_LAST_ACK) \
22-
EM(TCP_LISTEN) \
23-
EM(TCP_CLOSING) \
24-
EMe(TCP_NEW_SYN_RECV) \
25-
26-
/* enums need to be exported to user space */
27-
#undef EM
28-
#undef EMe
29-
#define EM(a) TRACE_DEFINE_ENUM(a);
30-
#define EMe(a) TRACE_DEFINE_ENUM(a);
31-
32-
tcp_state_names
33-
34-
#undef EM
35-
#undef EMe
36-
#define EM(a) tcp_state_name(a),
37-
#define EMe(a) tcp_state_name(a)
38-
39-
#define tcp_state_name(state) { state, #state }
40-
#define show_tcp_state_name(val) \
41-
__print_symbolic(val, tcp_state_names)
42-
4312
/*
4413
* tcp event with arguments sk and skb
4514
*

net/ipv4/af_inet.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
#endif
122122
#include <net/l3mdev.h>
123123

124+
#include <trace/events/sock.h>
124125

125126
/* The inetsw table contains everything that inet_create needs to
126127
* build a new socket.
@@ -1220,6 +1221,19 @@ int inet_sk_rebuild_header(struct sock *sk)
12201221
}
12211222
EXPORT_SYMBOL(inet_sk_rebuild_header);
12221223

1224+
void inet_sk_set_state(struct sock *sk, int state)
1225+
{
1226+
trace_inet_sock_set_state(sk, sk->sk_state, state);
1227+
sk->sk_state = state;
1228+
}
1229+
EXPORT_SYMBOL(inet_sk_set_state);
1230+
1231+
void inet_sk_state_store(struct sock *sk, int newstate)
1232+
{
1233+
trace_inet_sock_set_state(sk, sk->sk_state, newstate);
1234+
smp_store_release(&sk->sk_state, newstate);
1235+
}
1236+
12231237
struct sk_buff *inet_gso_segment(struct sk_buff *skb,
12241238
netdev_features_t features)
12251239
{

net/ipv4/inet_connection_sock.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
783783
if (newsk) {
784784
struct inet_connection_sock *newicsk = inet_csk(newsk);
785785

786-
newsk->sk_state = TCP_SYN_RECV;
786+
inet_sk_set_state(newsk, TCP_SYN_RECV);
787787
newicsk->icsk_bind_hash = NULL;
788788

789789
inet_sk(newsk)->inet_dport = inet_rsk(req)->ir_rmt_port;
@@ -877,7 +877,7 @@ int inet_csk_listen_start(struct sock *sk, int backlog)
877877
* It is OK, because this socket enters to hash table only
878878
* after validation is complete.
879879
*/
880-
sk_state_store(sk, TCP_LISTEN);
880+
inet_sk_state_store(sk, TCP_LISTEN);
881881
if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
882882
inet->inet_sport = htons(inet->inet_num);
883883

@@ -888,7 +888,7 @@ int inet_csk_listen_start(struct sock *sk, int backlog)
888888
return 0;
889889
}
890890

891-
sk->sk_state = TCP_CLOSE;
891+
inet_sk_set_state(sk, TCP_CLOSE);
892892
return err;
893893
}
894894
EXPORT_SYMBOL_GPL(inet_csk_listen_start);

net/ipv4/inet_hashtables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ bool inet_ehash_nolisten(struct sock *sk, struct sock *osk)
544544
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
545545
} else {
546546
percpu_counter_inc(sk->sk_prot->orphan_count);
547-
sk->sk_state = TCP_CLOSE;
547+
inet_sk_set_state(sk, TCP_CLOSE);
548548
sock_set_flag(sk, SOCK_DEAD);
549549
inet_csk_destroy_sock(sk);
550550
}

net/ipv4/tcp.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,6 @@
283283
#include <asm/ioctls.h>
284284
#include <net/busy_poll.h>
285285

286-
#include <trace/events/tcp.h>
287-
288286
struct percpu_counter tcp_orphan_count;
289287
EXPORT_SYMBOL_GPL(tcp_orphan_count);
290288

@@ -2040,8 +2038,6 @@ void tcp_set_state(struct sock *sk, int state)
20402038
{
20412039
int oldstate = sk->sk_state;
20422040

2043-
trace_tcp_set_state(sk, oldstate, state);
2044-
20452041
switch (state) {
20462042
case TCP_ESTABLISHED:
20472043
if (oldstate != TCP_ESTABLISHED)
@@ -2065,7 +2061,7 @@ void tcp_set_state(struct sock *sk, int state)
20652061
/* Change state AFTER socket is unhashed to avoid closed
20662062
* socket sitting in hash tables.
20672063
*/
2068-
sk_state_store(sk, state);
2064+
inet_sk_state_store(sk, state);
20692065

20702066
#ifdef STATE_TRACE
20712067
SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n", sk, statename[oldstate], statename[state]);

0 commit comments

Comments
 (0)