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