xref: /linux/include/trace/events/sock.h (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #undef TRACE_SYSTEM
3 #define TRACE_SYSTEM sock
4 
5 #if !defined(_TRACE_SOCK_H) || defined(TRACE_HEADER_MULTI_READ)
6 #define _TRACE_SOCK_H
7 
8 #include <net/sock.h>
9 #include <net/ipv6.h>
10 #include <linux/tracepoint.h>
11 #include <linux/ipv6.h>
12 #include <linux/tcp.h>
13 #include <trace/events/net_probe_common.h>
14 
15 #define family_names			\
16 		EM(AF_INET)				\
17 		EMe(AF_INET6)
18 
19 /* The protocol traced by inet_sock_set_state */
20 #define inet_protocol_names		\
21 		EM(IPPROTO_TCP)			\
22 		EM(IPPROTO_DCCP)		\
23 		EM(IPPROTO_SCTP)		\
24 		EMe(IPPROTO_MPTCP)
25 
26 #define tcp_state_names			\
27 		EM(TCP_ESTABLISHED)		\
28 		EM(TCP_SYN_SENT)		\
29 		EM(TCP_SYN_RECV)		\
30 		EM(TCP_FIN_WAIT1)		\
31 		EM(TCP_FIN_WAIT2)		\
32 		EM(TCP_TIME_WAIT)		\
33 		EM(TCP_CLOSE)			\
34 		EM(TCP_CLOSE_WAIT)		\
35 		EM(TCP_LAST_ACK)		\
36 		EM(TCP_LISTEN)			\
37 		EM(TCP_CLOSING)			\
38 		EMe(TCP_NEW_SYN_RECV)
39 
40 #define skmem_kind_names			\
41 		EM(SK_MEM_SEND)			\
42 		EMe(SK_MEM_RECV)
43 
44 /* enums need to be exported to user space */
45 #undef EM
46 #undef EMe
47 #define EM(a)       TRACE_DEFINE_ENUM(a);
48 #define EMe(a)      TRACE_DEFINE_ENUM(a);
49 
50 family_names
51 inet_protocol_names
52 tcp_state_names
53 skmem_kind_names
54 
55 #undef EM
56 #undef EMe
57 #define EM(a)       { a, #a },
58 #define EMe(a)      { a, #a }
59 
60 #define show_family_name(val)			\
61 	__print_symbolic(val, family_names)
62 
63 #define show_inet_protocol_name(val)    \
64 	__print_symbolic(val, inet_protocol_names)
65 
66 #define show_tcp_state_name(val)        \
67 	__print_symbolic(val, tcp_state_names)
68 
69 #define show_skmem_kind_names(val)	\
70 	__print_symbolic(val, skmem_kind_names)
71 
72 TRACE_EVENT(sock_rcvqueue_full,
73 
74 	TP_PROTO(struct sock *sk, struct sk_buff *skb),
75 
76 	TP_ARGS(sk, skb),
77 
78 	TP_STRUCT__entry(
79 		__field(int, rmem_alloc)
80 		__field(unsigned int, truesize)
81 		__field(int, sk_rcvbuf)
82 	),
83 
84 	TP_fast_assign(
85 		__entry->rmem_alloc = atomic_read(&sk->sk_rmem_alloc);
86 		__entry->truesize   = skb->truesize;
87 		__entry->sk_rcvbuf  = READ_ONCE(sk->sk_rcvbuf);
88 	),
89 
90 	TP_printk("rmem_alloc=%d truesize=%u sk_rcvbuf=%d",
91 		__entry->rmem_alloc, __entry->truesize, __entry->sk_rcvbuf)
92 );
93 
94 TRACE_EVENT(sock_exceed_buf_limit,
95 
96 	TP_PROTO(struct sock *sk, struct proto *prot, long allocated, int kind),
97 
98 	TP_ARGS(sk, prot, allocated, kind),
99 
100 	TP_STRUCT__entry(
101 		__array(char, name, 32)
102 		__array(long, sysctl_mem, 3)
103 		__field(long, allocated)
104 		__field(int, sysctl_rmem)
105 		__field(int, rmem_alloc)
106 		__field(int, sysctl_wmem)
107 		__field(int, wmem_alloc)
108 		__field(int, wmem_queued)
109 		__field(int, kind)
110 	),
111 
112 	TP_fast_assign(
113 		strscpy(__entry->name, prot->name, 32);
114 		__entry->sysctl_mem[0] = READ_ONCE(prot->sysctl_mem[0]);
115 		__entry->sysctl_mem[1] = READ_ONCE(prot->sysctl_mem[1]);
116 		__entry->sysctl_mem[2] = READ_ONCE(prot->sysctl_mem[2]);
117 		__entry->allocated = allocated;
118 		__entry->sysctl_rmem = sk_get_rmem0(sk, prot);
119 		__entry->rmem_alloc = atomic_read(&sk->sk_rmem_alloc);
120 		__entry->sysctl_wmem = sk_get_wmem0(sk, prot);
121 		__entry->wmem_alloc = refcount_read(&sk->sk_wmem_alloc);
122 		__entry->wmem_queued = READ_ONCE(sk->sk_wmem_queued);
123 		__entry->kind = kind;
124 	),
125 
126 	TP_printk("proto:%s sysctl_mem=%ld,%ld,%ld allocated=%ld sysctl_rmem=%d rmem_alloc=%d sysctl_wmem=%d wmem_alloc=%d wmem_queued=%d kind=%s",
127 		__entry->name,
128 		__entry->sysctl_mem[0],
129 		__entry->sysctl_mem[1],
130 		__entry->sysctl_mem[2],
131 		__entry->allocated,
132 		__entry->sysctl_rmem,
133 		__entry->rmem_alloc,
134 		__entry->sysctl_wmem,
135 		__entry->wmem_alloc,
136 		__entry->wmem_queued,
137 		show_skmem_kind_names(__entry->kind)
138 	)
139 );
140 
141 TRACE_EVENT(inet_sock_set_state,
142 
143 	TP_PROTO(const struct sock *sk, const int oldstate, const int newstate),
144 
145 	TP_ARGS(sk, oldstate, newstate),
146 
147 	TP_STRUCT__entry(
148 		__field(const void *, skaddr)
149 		__field(int, oldstate)
150 		__field(int, newstate)
151 		__field(__u16, sport)
152 		__field(__u16, dport)
153 		__field(__u16, family)
154 		__field(__u16, protocol)
155 		__array(__u8, saddr, 4)
156 		__array(__u8, daddr, 4)
157 		__array(__u8, saddr_v6, 16)
158 		__array(__u8, daddr_v6, 16)
159 	),
160 
161 	TP_fast_assign(
162 		const struct inet_sock *inet = inet_sk(sk);
163 		__be32 *p32;
164 
165 		__entry->skaddr = sk;
166 		__entry->oldstate = oldstate;
167 		__entry->newstate = newstate;
168 
169 		__entry->family = sk->sk_family;
170 		__entry->protocol = sk->sk_protocol;
171 		__entry->sport = ntohs(inet->inet_sport);
172 		__entry->dport = ntohs(inet->inet_dport);
173 
174 		p32 = (__be32 *) __entry->saddr;
175 		*p32 = inet->inet_saddr;
176 
177 		p32 = (__be32 *) __entry->daddr;
178 		*p32 =  inet->inet_daddr;
179 
180 		TP_STORE_ADDRS(__entry, inet->inet_saddr, inet->inet_daddr,
181 			       sk->sk_v6_rcv_saddr, sk->sk_v6_daddr);
182 	),
183 
184 	TP_printk("family=%s protocol=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c oldstate=%s newstate=%s",
185 			show_family_name(__entry->family),
186 			show_inet_protocol_name(__entry->protocol),
187 			__entry->sport, __entry->dport,
188 			__entry->saddr, __entry->daddr,
189 			__entry->saddr_v6, __entry->daddr_v6,
190 			show_tcp_state_name(__entry->oldstate),
191 			show_tcp_state_name(__entry->newstate))
192 );
193 
194 TRACE_EVENT(inet_sk_error_report,
195 
196 	TP_PROTO(const struct sock *sk),
197 
198 	TP_ARGS(sk),
199 
200 	TP_STRUCT__entry(
201 		__field(int, error)
202 		__field(__u16, sport)
203 		__field(__u16, dport)
204 		__field(__u16, family)
205 		__field(__u16, protocol)
206 		__array(__u8, saddr, 4)
207 		__array(__u8, daddr, 4)
208 		__array(__u8, saddr_v6, 16)
209 		__array(__u8, daddr_v6, 16)
210 	),
211 
212 	TP_fast_assign(
213 		const struct inet_sock *inet = inet_sk(sk);
214 		__be32 *p32;
215 
216 		__entry->error = sk->sk_err;
217 		__entry->family = sk->sk_family;
218 		__entry->protocol = sk->sk_protocol;
219 		__entry->sport = ntohs(inet->inet_sport);
220 		__entry->dport = ntohs(inet->inet_dport);
221 
222 		p32 = (__be32 *) __entry->saddr;
223 		*p32 = inet->inet_saddr;
224 
225 		p32 = (__be32 *) __entry->daddr;
226 		*p32 =  inet->inet_daddr;
227 
228 		TP_STORE_ADDRS(__entry, inet->inet_saddr, inet->inet_daddr,
229 			       sk->sk_v6_rcv_saddr, sk->sk_v6_daddr);
230 	),
231 
232 	TP_printk("family=%s protocol=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c error=%d",
233 		  show_family_name(__entry->family),
234 		  show_inet_protocol_name(__entry->protocol),
235 		  __entry->sport, __entry->dport,
236 		  __entry->saddr, __entry->daddr,
237 		  __entry->saddr_v6, __entry->daddr_v6,
238 		  __entry->error)
239 );
240 
241 TRACE_EVENT(sk_data_ready,
242 
243 	TP_PROTO(const struct sock *sk),
244 
245 	TP_ARGS(sk),
246 
247 	TP_STRUCT__entry(
248 		__field(const void *, skaddr)
249 		__field(__u16, family)
250 		__field(__u16, protocol)
251 		__field(unsigned long, ip)
252 	),
253 
254 	TP_fast_assign(
255 		__entry->skaddr = sk;
256 		__entry->family = sk->sk_family;
257 		__entry->protocol = sk->sk_protocol;
258 		__entry->ip = _RET_IP_;
259 	),
260 
261 	TP_printk("family=%u protocol=%u func=%ps",
262 		  __entry->family, __entry->protocol, (void *)__entry->ip)
263 );
264 
265 /*
266  * sock send/recv msg length
267  */
268 DECLARE_EVENT_CLASS(sock_msg_length,
269 
270 	TP_PROTO(struct sock *sk, int ret, int flags),
271 
272 	TP_ARGS(sk, ret, flags),
273 
274 	TP_STRUCT__entry(
275 		__field(void *, sk)
276 		__field(__u16, family)
277 		__field(__u16, protocol)
278 		__field(int, ret)
279 		__field(int, flags)
280 	),
281 
282 	TP_fast_assign(
283 		__entry->sk = sk;
284 		__entry->family = sk->sk_family;
285 		__entry->protocol = sk->sk_protocol;
286 		__entry->ret = ret;
287 		__entry->flags = flags;
288 	),
289 
290 	TP_printk("sk address = %p, family = %s protocol = %s, length = %d, error = %d, flags = 0x%x",
291 		  __entry->sk, show_family_name(__entry->family),
292 		  show_inet_protocol_name(__entry->protocol),
293 		  !(__entry->flags & MSG_PEEK) ?
294 		  (__entry->ret > 0 ? __entry->ret : 0) : 0,
295 		  __entry->ret < 0 ? __entry->ret : 0,
296 		  __entry->flags)
297 );
298 
299 DEFINE_EVENT(sock_msg_length, sock_send_length,
300 	TP_PROTO(struct sock *sk, int ret, int flags),
301 
302 	TP_ARGS(sk, ret, flags)
303 );
304 
305 DEFINE_EVENT(sock_msg_length, sock_recv_length,
306 	TP_PROTO(struct sock *sk, int ret, int flags),
307 
308 	TP_ARGS(sk, ret, flags)
309 );
310 #endif /* _TRACE_SOCK_H */
311 
312 /* This part must be outside protection */
313 #include <trace/define_trace.h>
314