xref: /openbmc/linux/include/trace/events/tcp.h (revision b830f94f)
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 		__array(__u8, saddr, 4)
63 		__array(__u8, daddr, 4)
64 		__array(__u8, saddr_v6, 16)
65 		__array(__u8, daddr_v6, 16)
66 	),
67 
68 	TP_fast_assign(
69 		struct inet_sock *inet = inet_sk(sk);
70 		__be32 *p32;
71 
72 		__entry->skbaddr = skb;
73 		__entry->skaddr = sk;
74 		__entry->state = sk->sk_state;
75 
76 		__entry->sport = ntohs(inet->inet_sport);
77 		__entry->dport = ntohs(inet->inet_dport);
78 
79 		p32 = (__be32 *) __entry->saddr;
80 		*p32 = inet->inet_saddr;
81 
82 		p32 = (__be32 *) __entry->daddr;
83 		*p32 =  inet->inet_daddr;
84 
85 		TP_STORE_ADDRS(__entry, inet->inet_saddr, inet->inet_daddr,
86 			      sk->sk_v6_rcv_saddr, sk->sk_v6_daddr);
87 	),
88 
89 	TP_printk("sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c state=%s\n",
90 		  __entry->sport, __entry->dport, __entry->saddr, __entry->daddr,
91 		  __entry->saddr_v6, __entry->daddr_v6,
92 		  show_tcp_state_name(__entry->state))
93 );
94 
95 DEFINE_EVENT(tcp_event_sk_skb, tcp_retransmit_skb,
96 
97 	TP_PROTO(const struct sock *sk, const struct sk_buff *skb),
98 
99 	TP_ARGS(sk, skb)
100 );
101 
102 /*
103  * skb of trace_tcp_send_reset is the skb that caused RST. In case of
104  * active reset, skb should be NULL
105  */
106 DEFINE_EVENT(tcp_event_sk_skb, tcp_send_reset,
107 
108 	TP_PROTO(const struct sock *sk, const struct sk_buff *skb),
109 
110 	TP_ARGS(sk, skb)
111 );
112 
113 /*
114  * tcp event with arguments sk
115  *
116  * Note: this class requires a valid sk pointer.
117  */
118 DECLARE_EVENT_CLASS(tcp_event_sk,
119 
120 	TP_PROTO(struct sock *sk),
121 
122 	TP_ARGS(sk),
123 
124 	TP_STRUCT__entry(
125 		__field(const void *, skaddr)
126 		__field(__u16, sport)
127 		__field(__u16, dport)
128 		__array(__u8, saddr, 4)
129 		__array(__u8, daddr, 4)
130 		__array(__u8, saddr_v6, 16)
131 		__array(__u8, daddr_v6, 16)
132 		__field(__u64, sock_cookie)
133 	),
134 
135 	TP_fast_assign(
136 		struct inet_sock *inet = inet_sk(sk);
137 		__be32 *p32;
138 
139 		__entry->skaddr = sk;
140 
141 		__entry->sport = ntohs(inet->inet_sport);
142 		__entry->dport = ntohs(inet->inet_dport);
143 
144 		p32 = (__be32 *) __entry->saddr;
145 		*p32 = inet->inet_saddr;
146 
147 		p32 = (__be32 *) __entry->daddr;
148 		*p32 =  inet->inet_daddr;
149 
150 		TP_STORE_ADDRS(__entry, inet->inet_saddr, inet->inet_daddr,
151 			       sk->sk_v6_rcv_saddr, sk->sk_v6_daddr);
152 
153 		__entry->sock_cookie = sock_gen_cookie(sk);
154 	),
155 
156 	TP_printk("sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c sock_cookie=%llx",
157 		  __entry->sport, __entry->dport,
158 		  __entry->saddr, __entry->daddr,
159 		  __entry->saddr_v6, __entry->daddr_v6,
160 		  __entry->sock_cookie)
161 );
162 
163 DEFINE_EVENT(tcp_event_sk, tcp_receive_reset,
164 
165 	TP_PROTO(struct sock *sk),
166 
167 	TP_ARGS(sk)
168 );
169 
170 DEFINE_EVENT(tcp_event_sk, tcp_destroy_sock,
171 
172 	TP_PROTO(struct sock *sk),
173 
174 	TP_ARGS(sk)
175 );
176 
177 DEFINE_EVENT(tcp_event_sk, tcp_rcv_space_adjust,
178 
179 	TP_PROTO(struct sock *sk),
180 
181 	TP_ARGS(sk)
182 );
183 
184 TRACE_EVENT(tcp_retransmit_synack,
185 
186 	TP_PROTO(const struct sock *sk, const struct request_sock *req),
187 
188 	TP_ARGS(sk, req),
189 
190 	TP_STRUCT__entry(
191 		__field(const void *, skaddr)
192 		__field(const void *, req)
193 		__field(__u16, sport)
194 		__field(__u16, dport)
195 		__array(__u8, saddr, 4)
196 		__array(__u8, daddr, 4)
197 		__array(__u8, saddr_v6, 16)
198 		__array(__u8, daddr_v6, 16)
199 	),
200 
201 	TP_fast_assign(
202 		struct inet_request_sock *ireq = inet_rsk(req);
203 		__be32 *p32;
204 
205 		__entry->skaddr = sk;
206 		__entry->req = req;
207 
208 		__entry->sport = ireq->ir_num;
209 		__entry->dport = ntohs(ireq->ir_rmt_port);
210 
211 		p32 = (__be32 *) __entry->saddr;
212 		*p32 = ireq->ir_loc_addr;
213 
214 		p32 = (__be32 *) __entry->daddr;
215 		*p32 = ireq->ir_rmt_addr;
216 
217 		TP_STORE_ADDRS(__entry, ireq->ir_loc_addr, ireq->ir_rmt_addr,
218 			      ireq->ir_v6_loc_addr, ireq->ir_v6_rmt_addr);
219 	),
220 
221 	TP_printk("sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c",
222 		  __entry->sport, __entry->dport,
223 		  __entry->saddr, __entry->daddr,
224 		  __entry->saddr_v6, __entry->daddr_v6)
225 );
226 
227 #include <trace/events/net_probe_common.h>
228 
229 TRACE_EVENT(tcp_probe,
230 
231 	TP_PROTO(struct sock *sk, struct sk_buff *skb),
232 
233 	TP_ARGS(sk, skb),
234 
235 	TP_STRUCT__entry(
236 		/* sockaddr_in6 is always bigger than sockaddr_in */
237 		__array(__u8, saddr, sizeof(struct sockaddr_in6))
238 		__array(__u8, daddr, sizeof(struct sockaddr_in6))
239 		__field(__u16, sport)
240 		__field(__u16, dport)
241 		__field(__u32, mark)
242 		__field(__u16, data_len)
243 		__field(__u32, snd_nxt)
244 		__field(__u32, snd_una)
245 		__field(__u32, snd_cwnd)
246 		__field(__u32, ssthresh)
247 		__field(__u32, snd_wnd)
248 		__field(__u32, srtt)
249 		__field(__u32, rcv_wnd)
250 		__field(__u64, sock_cookie)
251 	),
252 
253 	TP_fast_assign(
254 		const struct tcphdr *th = (const struct tcphdr *)skb->data;
255 		const struct inet_sock *inet = inet_sk(sk);
256 		const struct tcp_sock *tp = tcp_sk(sk);
257 
258 		memset(__entry->saddr, 0, sizeof(struct sockaddr_in6));
259 		memset(__entry->daddr, 0, sizeof(struct sockaddr_in6));
260 
261 		TP_STORE_ADDR_PORTS(__entry, inet, sk);
262 
263 		/* For filtering use */
264 		__entry->sport = ntohs(inet->inet_sport);
265 		__entry->dport = ntohs(inet->inet_dport);
266 		__entry->mark = skb->mark;
267 
268 		__entry->data_len = skb->len - __tcp_hdrlen(th);
269 		__entry->snd_nxt = tp->snd_nxt;
270 		__entry->snd_una = tp->snd_una;
271 		__entry->snd_cwnd = tp->snd_cwnd;
272 		__entry->snd_wnd = tp->snd_wnd;
273 		__entry->rcv_wnd = tp->rcv_wnd;
274 		__entry->ssthresh = tcp_current_ssthresh(sk);
275 		__entry->srtt = tp->srtt_us >> 3;
276 		__entry->sock_cookie = sock_gen_cookie(sk);
277 	),
278 
279 	TP_printk("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",
280 		  __entry->saddr, __entry->daddr, __entry->mark,
281 		  __entry->data_len, __entry->snd_nxt, __entry->snd_una,
282 		  __entry->snd_cwnd, __entry->ssthresh, __entry->snd_wnd,
283 		  __entry->srtt, __entry->rcv_wnd, __entry->sock_cookie)
284 );
285 
286 #endif /* _TRACE_TCP_H */
287 
288 /* This part must be outside protection */
289 #include <trace/define_trace.h>
290