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