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 
14 #define family_names			\
15 		EM(AF_INET)				\
16 		EMe(AF_INET6)
17 
18 /* The protocol traced by inet_sock_set_state */
19 #define inet_protocol_names		\
20 		EM(IPPROTO_TCP)			\
21 		EM(IPPROTO_DCCP)		\
22 		EM(IPPROTO_SCTP)		\
23 		EMe(IPPROTO_MPTCP)
24 
25 #define tcp_state_names			\
26 		EM(TCP_ESTABLISHED)		\
27 		EM(TCP_SYN_SENT)		\
28 		EM(TCP_SYN_RECV)		\
29 		EM(TCP_FIN_WAIT1)		\
30 		EM(TCP_FIN_WAIT2)		\
31 		EM(TCP_TIME_WAIT)		\
32 		EM(TCP_CLOSE)			\
33 		EM(TCP_CLOSE_WAIT)		\
34 		EM(TCP_LAST_ACK)		\
35 		EM(TCP_LISTEN)			\
36 		EM(TCP_CLOSING)			\
37 		EMe(TCP_NEW_SYN_RECV)
38 
39 #define skmem_kind_names			\
40 		EM(SK_MEM_SEND)			\
41 		EMe(SK_MEM_RECV)
42 
43 /* enums need to be exported to user space */
44 #undef EM
45 #undef EMe
46 #define EM(a)       TRACE_DEFINE_ENUM(a);
47 #define EMe(a)      TRACE_DEFINE_ENUM(a);
48 
49 family_names
50 inet_protocol_names
51 tcp_state_names
52 skmem_kind_names
53 
54 #undef EM
55 #undef EMe
56 #define EM(a)       { a, #a },
57 #define EMe(a)      { a, #a }
58 
59 #define show_family_name(val)			\
60 	__print_symbolic(val, family_names)
61 
62 #define show_inet_protocol_name(val)    \
63 	__print_symbolic(val, inet_protocol_names)
64 
65 #define show_tcp_state_name(val)        \
66 	__print_symbolic(val, tcp_state_names)
67 
68 #define show_skmem_kind_names(val)	\
69 	__print_symbolic(val, skmem_kind_names)
70 
71 TRACE_EVENT(sock_rcvqueue_full,
72 
73 	TP_PROTO(struct sock *sk, struct sk_buff *skb),
74 
75 	TP_ARGS(sk, skb),
76 
77 	TP_STRUCT__entry(
78 		__field(int, rmem_alloc)
79 		__field(unsigned int, truesize)
80 		__field(int, sk_rcvbuf)
81 	),
82 
83 	TP_fast_assign(
84 		__entry->rmem_alloc = atomic_read(&sk->sk_rmem_alloc);
85 		__entry->truesize   = skb->truesize;
86 		__entry->sk_rcvbuf  = READ_ONCE(sk->sk_rcvbuf);
87 	),
88 
89 	TP_printk("rmem_alloc=%d truesize=%u sk_rcvbuf=%d",
90 		__entry->rmem_alloc, __entry->truesize, __entry->sk_rcvbuf)
91 );
92 
93 TRACE_EVENT(sock_exceed_buf_limit,
94 
95 	TP_PROTO(struct sock *sk, struct proto *prot, long allocated, int kind),
96 
97 	TP_ARGS(sk, prot, allocated, kind),
98 
99 	TP_STRUCT__entry(
100 		__array(char, name, 32)
101 		__array(long, sysctl_mem, 3)
102 		__field(long, allocated)
103 		__field(int, sysctl_rmem)
104 		__field(int, rmem_alloc)
105 		__field(int, sysctl_wmem)
106 		__field(int, wmem_alloc)
107 		__field(int, wmem_queued)
108 		__field(int, kind)
109 	),
110 
111 	TP_fast_assign(
112 		strncpy(__entry->name, prot->name, 32);
113 		__entry->sysctl_mem[0] = READ_ONCE(prot->sysctl_mem[0]);
114 		__entry->sysctl_mem[1] = READ_ONCE(prot->sysctl_mem[1]);
115 		__entry->sysctl_mem[2] = READ_ONCE(prot->sysctl_mem[2]);
116 		__entry->allocated = allocated;
117 		__entry->sysctl_rmem = sk_get_rmem0(sk, prot);
118 		__entry->rmem_alloc = atomic_read(&sk->sk_rmem_alloc);
119 		__entry->sysctl_wmem = sk_get_wmem0(sk, prot);
120 		__entry->wmem_alloc = refcount_read(&sk->sk_wmem_alloc);
121 		__entry->wmem_queued = READ_ONCE(sk->sk_wmem_queued);
122 		__entry->kind = kind;
123 	),
124 
125 	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",
126 		__entry->name,
127 		__entry->sysctl_mem[0],
128 		__entry->sysctl_mem[1],
129 		__entry->sysctl_mem[2],
130 		__entry->allocated,
131 		__entry->sysctl_rmem,
132 		__entry->rmem_alloc,
133 		__entry->sysctl_wmem,
134 		__entry->wmem_alloc,
135 		__entry->wmem_queued,
136 		show_skmem_kind_names(__entry->kind)
137 	)
138 );
139 
140 TRACE_EVENT(inet_sock_set_state,
141 
142 	TP_PROTO(const struct sock *sk, const int oldstate, const int newstate),
143 
144 	TP_ARGS(sk, oldstate, newstate),
145 
146 	TP_STRUCT__entry(
147 		__field(const void *, skaddr)
148 		__field(int, oldstate)
149 		__field(int, newstate)
150 		__field(__u16, sport)
151 		__field(__u16, dport)
152 		__field(__u16, family)
153 		__field(__u16, protocol)
154 		__array(__u8, saddr, 4)
155 		__array(__u8, daddr, 4)
156 		__array(__u8, saddr_v6, 16)
157 		__array(__u8, daddr_v6, 16)
158 	),
159 
160 	TP_fast_assign(
161 		struct inet_sock *inet = inet_sk(sk);
162 		struct in6_addr *pin6;
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 #if IS_ENABLED(CONFIG_IPV6)
181 		if (sk->sk_family == AF_INET6) {
182 			pin6 = (struct in6_addr *)__entry->saddr_v6;
183 			*pin6 = sk->sk_v6_rcv_saddr;
184 			pin6 = (struct in6_addr *)__entry->daddr_v6;
185 			*pin6 = sk->sk_v6_daddr;
186 		} else
187 #endif
188 		{
189 			pin6 = (struct in6_addr *)__entry->saddr_v6;
190 			ipv6_addr_set_v4mapped(inet->inet_saddr, pin6);
191 			pin6 = (struct in6_addr *)__entry->daddr_v6;
192 			ipv6_addr_set_v4mapped(inet->inet_daddr, pin6);
193 		}
194 	),
195 
196 	TP_printk("family=%s protocol=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c oldstate=%s newstate=%s",
197 			show_family_name(__entry->family),
198 			show_inet_protocol_name(__entry->protocol),
199 			__entry->sport, __entry->dport,
200 			__entry->saddr, __entry->daddr,
201 			__entry->saddr_v6, __entry->daddr_v6,
202 			show_tcp_state_name(__entry->oldstate),
203 			show_tcp_state_name(__entry->newstate))
204 );
205 
206 TRACE_EVENT(inet_sk_error_report,
207 
208 	TP_PROTO(const struct sock *sk),
209 
210 	TP_ARGS(sk),
211 
212 	TP_STRUCT__entry(
213 		__field(int, error)
214 		__field(__u16, sport)
215 		__field(__u16, dport)
216 		__field(__u16, family)
217 		__field(__u16, protocol)
218 		__array(__u8, saddr, 4)
219 		__array(__u8, daddr, 4)
220 		__array(__u8, saddr_v6, 16)
221 		__array(__u8, daddr_v6, 16)
222 	),
223 
224 	TP_fast_assign(
225 		struct inet_sock *inet = inet_sk(sk);
226 		struct in6_addr *pin6;
227 		__be32 *p32;
228 
229 		__entry->error = sk->sk_err;
230 		__entry->family = sk->sk_family;
231 		__entry->protocol = sk->sk_protocol;
232 		__entry->sport = ntohs(inet->inet_sport);
233 		__entry->dport = ntohs(inet->inet_dport);
234 
235 		p32 = (__be32 *) __entry->saddr;
236 		*p32 = inet->inet_saddr;
237 
238 		p32 = (__be32 *) __entry->daddr;
239 		*p32 =  inet->inet_daddr;
240 
241 #if IS_ENABLED(CONFIG_IPV6)
242 		if (sk->sk_family == AF_INET6) {
243 			pin6 = (struct in6_addr *)__entry->saddr_v6;
244 			*pin6 = sk->sk_v6_rcv_saddr;
245 			pin6 = (struct in6_addr *)__entry->daddr_v6;
246 			*pin6 = sk->sk_v6_daddr;
247 		} else
248 #endif
249 		{
250 			pin6 = (struct in6_addr *)__entry->saddr_v6;
251 			ipv6_addr_set_v4mapped(inet->inet_saddr, pin6);
252 			pin6 = (struct in6_addr *)__entry->daddr_v6;
253 			ipv6_addr_set_v4mapped(inet->inet_daddr, pin6);
254 		}
255 	),
256 
257 	TP_printk("family=%s protocol=%s sport=%hu dport=%hu saddr=%pI4 daddr=%pI4 saddrv6=%pI6c daddrv6=%pI6c error=%d",
258 		  show_family_name(__entry->family),
259 		  show_inet_protocol_name(__entry->protocol),
260 		  __entry->sport, __entry->dport,
261 		  __entry->saddr, __entry->daddr,
262 		  __entry->saddr_v6, __entry->daddr_v6,
263 		  __entry->error)
264 );
265 
266 #endif /* _TRACE_SOCK_H */
267 
268 /* This part must be outside protection */
269 #include <trace/define_trace.h>
270