xref: /linux/include/trace/events/tcp.h (revision 860a9bed265146b10311bcadbbcef59c3af4454d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #undef TRACE_SYSTEM
3 #define TRACE_SYSTEM tcp
4 
5 #if !defined(_TRACE_TCP_H) || defined(TRACE_HEADER_MULTI_READ)
6 #define _TRACE_TCP_H
7 
8 #include <linux/ipv6.h>
9 #include <linux/tcp.h>
10 #include <linux/tracepoint.h>
11 #include <net/ipv6.h>
12 #include <net/tcp.h>
13 #include <linux/sock_diag.h>
14 
15 /*
16  * tcp event with arguments sk and skb
17  *
18  * Note: this class requires a valid sk pointer; while skb pointer could
19  *       be NULL.
20  */
21 DECLARE_EVENT_CLASS(tcp_event_sk_skb,
22 
23 	TP_PROTO(const struct sock *sk, const struct sk_buff *skb),
24 
25 	TP_ARGS(sk, skb),
26 
27 	TP_STRUCT__entry(
28 		__field(const void *, skbaddr)
29 		__field(const void *, skaddr)
30 		__field(int, state)
31 		__field(__u16, sport)
32 		__field(__u16, dport)
33 		__field(__u16, family)
34 		__array(__u8, saddr, 4)
35 		__array(__u8, daddr, 4)
36 		__array(__u8, saddr_v6, 16)
37 		__array(__u8, daddr_v6, 16)
38 	),
39 
40 	TP_fast_assign(
41 		const struct inet_sock *inet = inet_sk(sk);
42 		__be32 *p32;
43 
44 		__entry->skbaddr = skb;
45 		__entry->skaddr = sk;
46 		__entry->state = sk->sk_state;
47 
48 		__entry->sport = ntohs(inet->inet_sport);
49 		__entry->dport = ntohs(inet->inet_dport);
50 		__entry->family = sk->sk_family;
51 
52 		p32 = (__be32 *) __entry->saddr;
53 		*p32 = inet->inet_saddr;
54 
55 		p32 = (__be32 *) __entry->daddr;
56 		*p32 =  inet->inet_daddr;
57 
58 		TP_STORE_ADDRS(__entry, inet->inet_saddr, inet->inet_daddr,
59 			      sk->sk_v6_rcv_saddr, sk->sk_v6_daddr);
60 	),
61 
62 	TP_printk("skbaddr=%p skaddr=%p family=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c state=%s",
63 		  __entry->skbaddr, __entry->skaddr,
64 		  show_family_name(__entry->family),
65 		  __entry->sport, __entry->dport, __entry->saddr, __entry->daddr,
66 		  __entry->saddr_v6, __entry->daddr_v6,
67 		  show_tcp_state_name(__entry->state))
68 );
69 
70 DEFINE_EVENT(tcp_event_sk_skb, tcp_retransmit_skb,
71 
72 	TP_PROTO(const struct sock *sk, const struct sk_buff *skb),
73 
74 	TP_ARGS(sk, skb)
75 );
76 
77 /*
78  * skb of trace_tcp_send_reset is the skb that caused RST. In case of
79  * active reset, skb should be NULL
80  */
81 TRACE_EVENT(tcp_send_reset,
82 
83 	TP_PROTO(const struct sock *sk, const struct sk_buff *skb),
84 
85 	TP_ARGS(sk, skb),
86 
87 	TP_STRUCT__entry(
88 		__field(const void *, skbaddr)
89 		__field(const void *, skaddr)
90 		__field(int, state)
91 		__array(__u8, saddr, sizeof(struct sockaddr_in6))
92 		__array(__u8, daddr, sizeof(struct sockaddr_in6))
93 	),
94 
95 	TP_fast_assign(
96 		__entry->skbaddr = skb;
97 		__entry->skaddr = sk;
98 		/* Zero means unknown state. */
99 		__entry->state = sk ? sk->sk_state : 0;
100 
101 		memset(__entry->saddr, 0, sizeof(struct sockaddr_in6));
102 		memset(__entry->daddr, 0, sizeof(struct sockaddr_in6));
103 
104 		if (sk && sk_fullsock(sk)) {
105 			const struct inet_sock *inet = inet_sk(sk);
106 
107 			TP_STORE_ADDR_PORTS(__entry, inet, sk);
108 		} else if (skb) {
109 			const struct tcphdr *th = (const struct tcphdr *)skb->data;
110 			/*
111 			 * We should reverse the 4-tuple of skb, so later
112 			 * it can print the right flow direction of rst.
113 			 */
114 			TP_STORE_ADDR_PORTS_SKB(skb, th, entry->daddr, entry->saddr);
115 		}
116 	),
117 
118 	TP_printk("skbaddr=%p skaddr=%p src=%pISpc dest=%pISpc state=%s",
119 		  __entry->skbaddr, __entry->skaddr,
120 		  __entry->saddr, __entry->daddr,
121 		  __entry->state ? show_tcp_state_name(__entry->state) : "UNKNOWN")
122 );
123 
124 /*
125  * tcp event with arguments sk
126  *
127  * Note: this class requires a valid sk pointer.
128  */
129 DECLARE_EVENT_CLASS(tcp_event_sk,
130 
131 	TP_PROTO(struct sock *sk),
132 
133 	TP_ARGS(sk),
134 
135 	TP_STRUCT__entry(
136 		__field(const void *, skaddr)
137 		__field(__u16, sport)
138 		__field(__u16, dport)
139 		__field(__u16, family)
140 		__array(__u8, saddr, 4)
141 		__array(__u8, daddr, 4)
142 		__array(__u8, saddr_v6, 16)
143 		__array(__u8, daddr_v6, 16)
144 		__field(__u64, sock_cookie)
145 	),
146 
147 	TP_fast_assign(
148 		struct inet_sock *inet = inet_sk(sk);
149 		__be32 *p32;
150 
151 		__entry->skaddr = sk;
152 
153 		__entry->sport = ntohs(inet->inet_sport);
154 		__entry->dport = ntohs(inet->inet_dport);
155 		__entry->family = sk->sk_family;
156 
157 		p32 = (__be32 *) __entry->saddr;
158 		*p32 = inet->inet_saddr;
159 
160 		p32 = (__be32 *) __entry->daddr;
161 		*p32 =  inet->inet_daddr;
162 
163 		TP_STORE_ADDRS(__entry, inet->inet_saddr, inet->inet_daddr,
164 			       sk->sk_v6_rcv_saddr, sk->sk_v6_daddr);
165 
166 		__entry->sock_cookie = sock_gen_cookie(sk);
167 	),
168 
169 	TP_printk("family=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c sock_cookie=%llx",
170 		  show_family_name(__entry->family),
171 		  __entry->sport, __entry->dport,
172 		  __entry->saddr, __entry->daddr,
173 		  __entry->saddr_v6, __entry->daddr_v6,
174 		  __entry->sock_cookie)
175 );
176 
177 DEFINE_EVENT(tcp_event_sk, tcp_receive_reset,
178 
179 	TP_PROTO(struct sock *sk),
180 
181 	TP_ARGS(sk)
182 );
183 
184 DEFINE_EVENT(tcp_event_sk, tcp_destroy_sock,
185 
186 	TP_PROTO(struct sock *sk),
187 
188 	TP_ARGS(sk)
189 );
190 
191 DEFINE_EVENT(tcp_event_sk, tcp_rcv_space_adjust,
192 
193 	TP_PROTO(struct sock *sk),
194 
195 	TP_ARGS(sk)
196 );
197 
198 TRACE_EVENT(tcp_retransmit_synack,
199 
200 	TP_PROTO(const struct sock *sk, const struct request_sock *req),
201 
202 	TP_ARGS(sk, req),
203 
204 	TP_STRUCT__entry(
205 		__field(const void *, skaddr)
206 		__field(const void *, req)
207 		__field(__u16, sport)
208 		__field(__u16, dport)
209 		__field(__u16, family)
210 		__array(__u8, saddr, 4)
211 		__array(__u8, daddr, 4)
212 		__array(__u8, saddr_v6, 16)
213 		__array(__u8, daddr_v6, 16)
214 	),
215 
216 	TP_fast_assign(
217 		struct inet_request_sock *ireq = inet_rsk(req);
218 		__be32 *p32;
219 
220 		__entry->skaddr = sk;
221 		__entry->req = req;
222 
223 		__entry->sport = ireq->ir_num;
224 		__entry->dport = ntohs(ireq->ir_rmt_port);
225 		__entry->family = sk->sk_family;
226 
227 		p32 = (__be32 *) __entry->saddr;
228 		*p32 = ireq->ir_loc_addr;
229 
230 		p32 = (__be32 *) __entry->daddr;
231 		*p32 = ireq->ir_rmt_addr;
232 
233 		TP_STORE_ADDRS(__entry, ireq->ir_loc_addr, ireq->ir_rmt_addr,
234 			      ireq->ir_v6_loc_addr, ireq->ir_v6_rmt_addr);
235 	),
236 
237 	TP_printk("family=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c",
238 		  show_family_name(__entry->family),
239 		  __entry->sport, __entry->dport,
240 		  __entry->saddr, __entry->daddr,
241 		  __entry->saddr_v6, __entry->daddr_v6)
242 );
243 
244 #include <trace/events/net_probe_common.h>
245 
246 TRACE_EVENT(tcp_probe,
247 
248 	TP_PROTO(struct sock *sk, struct sk_buff *skb),
249 
250 	TP_ARGS(sk, skb),
251 
252 	TP_STRUCT__entry(
253 		/* sockaddr_in6 is always bigger than sockaddr_in */
254 		__array(__u8, saddr, sizeof(struct sockaddr_in6))
255 		__array(__u8, daddr, sizeof(struct sockaddr_in6))
256 		__field(__u16, sport)
257 		__field(__u16, dport)
258 		__field(__u16, family)
259 		__field(__u32, mark)
260 		__field(__u16, data_len)
261 		__field(__u32, snd_nxt)
262 		__field(__u32, snd_una)
263 		__field(__u32, snd_cwnd)
264 		__field(__u32, ssthresh)
265 		__field(__u32, snd_wnd)
266 		__field(__u32, srtt)
267 		__field(__u32, rcv_wnd)
268 		__field(__u64, sock_cookie)
269 		__field(const void *, skbaddr)
270 		__field(const void *, skaddr)
271 	),
272 
273 	TP_fast_assign(
274 		const struct tcphdr *th = (const struct tcphdr *)skb->data;
275 		const struct inet_sock *inet = inet_sk(sk);
276 		const struct tcp_sock *tp = tcp_sk(sk);
277 
278 		memset(__entry->saddr, 0, sizeof(struct sockaddr_in6));
279 		memset(__entry->daddr, 0, sizeof(struct sockaddr_in6));
280 
281 		TP_STORE_ADDR_PORTS(__entry, inet, sk);
282 
283 		/* For filtering use */
284 		__entry->sport = ntohs(inet->inet_sport);
285 		__entry->dport = ntohs(inet->inet_dport);
286 		__entry->mark = skb->mark;
287 		__entry->family = sk->sk_family;
288 
289 		__entry->data_len = skb->len - __tcp_hdrlen(th);
290 		__entry->snd_nxt = tp->snd_nxt;
291 		__entry->snd_una = tp->snd_una;
292 		__entry->snd_cwnd = tcp_snd_cwnd(tp);
293 		__entry->snd_wnd = tp->snd_wnd;
294 		__entry->rcv_wnd = tp->rcv_wnd;
295 		__entry->ssthresh = tcp_current_ssthresh(sk);
296 		__entry->srtt = tp->srtt_us >> 3;
297 		__entry->sock_cookie = sock_gen_cookie(sk);
298 
299 		__entry->skbaddr = skb;
300 		__entry->skaddr = sk;
301 	),
302 
303 	TP_printk("family=%s src=%pISpc dest=%pISpc mark=%#x data_len=%d snd_nxt=%#x snd_una=%#x snd_cwnd=%u ssthresh=%u snd_wnd=%u srtt=%u rcv_wnd=%u sock_cookie=%llx skbaddr=%p skaddr=%p",
304 		  show_family_name(__entry->family),
305 		  __entry->saddr, __entry->daddr, __entry->mark,
306 		  __entry->data_len, __entry->snd_nxt, __entry->snd_una,
307 		  __entry->snd_cwnd, __entry->ssthresh, __entry->snd_wnd,
308 		  __entry->srtt, __entry->rcv_wnd, __entry->sock_cookie,
309 		  __entry->skbaddr, __entry->skaddr)
310 );
311 
312 /*
313  * tcp event with only skb
314  */
315 DECLARE_EVENT_CLASS(tcp_event_skb,
316 
317 	TP_PROTO(const struct sk_buff *skb),
318 
319 	TP_ARGS(skb),
320 
321 	TP_STRUCT__entry(
322 		__field(const void *, skbaddr)
323 		__array(__u8, saddr, sizeof(struct sockaddr_in6))
324 		__array(__u8, daddr, sizeof(struct sockaddr_in6))
325 	),
326 
327 	TP_fast_assign(
328 		const struct tcphdr *th = (const struct tcphdr *)skb->data;
329 		__entry->skbaddr = skb;
330 
331 		memset(__entry->saddr, 0, sizeof(struct sockaddr_in6));
332 		memset(__entry->daddr, 0, sizeof(struct sockaddr_in6));
333 
334 		TP_STORE_ADDR_PORTS_SKB(skb, th, __entry->saddr, __entry->daddr);
335 	),
336 
337 	TP_printk("skbaddr=%p src=%pISpc dest=%pISpc",
338 		  __entry->skbaddr, __entry->saddr, __entry->daddr)
339 );
340 
341 DEFINE_EVENT(tcp_event_skb, tcp_bad_csum,
342 
343 	TP_PROTO(const struct sk_buff *skb),
344 
345 	TP_ARGS(skb)
346 );
347 
348 TRACE_EVENT(tcp_cong_state_set,
349 
350 	TP_PROTO(struct sock *sk, const u8 ca_state),
351 
352 	TP_ARGS(sk, ca_state),
353 
354 	TP_STRUCT__entry(
355 		__field(const void *, skaddr)
356 		__field(__u16, sport)
357 		__field(__u16, dport)
358 		__field(__u16, family)
359 		__array(__u8, saddr, 4)
360 		__array(__u8, daddr, 4)
361 		__array(__u8, saddr_v6, 16)
362 		__array(__u8, daddr_v6, 16)
363 		__field(__u8, cong_state)
364 	),
365 
366 	TP_fast_assign(
367 		struct inet_sock *inet = inet_sk(sk);
368 		__be32 *p32;
369 
370 		__entry->skaddr = sk;
371 
372 		__entry->sport = ntohs(inet->inet_sport);
373 		__entry->dport = ntohs(inet->inet_dport);
374 		__entry->family = sk->sk_family;
375 
376 		p32 = (__be32 *) __entry->saddr;
377 		*p32 = inet->inet_saddr;
378 
379 		p32 = (__be32 *) __entry->daddr;
380 		*p32 =  inet->inet_daddr;
381 
382 		TP_STORE_ADDRS(__entry, inet->inet_saddr, inet->inet_daddr,
383 			   sk->sk_v6_rcv_saddr, sk->sk_v6_daddr);
384 
385 		__entry->cong_state = ca_state;
386 	),
387 
388 	TP_printk("family=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c cong_state=%u",
389 		  show_family_name(__entry->family),
390 		  __entry->sport, __entry->dport,
391 		  __entry->saddr, __entry->daddr,
392 		  __entry->saddr_v6, __entry->daddr_v6,
393 		  __entry->cong_state)
394 );
395 
396 #endif /* _TRACE_TCP_H */
397 
398 /* This part must be outside protection */
399 #include <trace/define_trace.h>
400