Debian Codegraph @

tapset/linux/tcpmib.stp

raw
1 /*
2 * Copyright (C) 2009 IBM Corp.
3 * Copyright (C) 2010-2018 Red Hat Inc.
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 *
10 * Version 1.0 [email protected] 2009-07-06
11 */
12
13 /* Global counter definitions for mib TCP_MIB. */
14
15 // See the BZ1546179 block comment in tapset/linux/networking.stp for
16 // an explanation of the try/catch statements around sk_buff structure
17 // accesses.
18
19 %{
20 #include <net/sock.h>
21 #include <linux/tcp.h>
22 #include <linux/skbuff.h>
23 #include <net/route.h>
24 %}
25
26 global ActiveOpens
27 global AttemptFails
28 global CurrEstab
29 global EstabResets
30 // global InErrs, this mib is not yet supported
31 global InSegs
32 global OutRsts
33 global OutSegs
34 global PassiveOpens
35 global RetransSegs
36
37 /**
38 * sfunction tcpmib_get_state - Get a socket's state
39 * @sk: pointer to a struct sock
40 *
41 * Returns the sk_state from a struct sock.
42 */
43 function tcpmib_get_state:long (sk:long)
44 {
45 return @cast(sk, "sock", "kernel")->__sk_common->skc_state;
46 }
47
48 /**
49 * sfunction tcpmib_local_addr - Get the source address
50 * @sk: pointer to a struct inet_sock
51 *
52 * Returns the saddr from a struct inet_sock in host order.
53 */
54 function tcpmib_local_addr:long(sk:long)
55 {
56 return ntohl(__ip_sock_saddr(sk))
57 }
58
59 /**
60 * sfunction tcpmib_remote_addr - Get the remote address
61 * @sk: pointer to a struct inet_sock
62 *
63 * Returns the daddr from a struct inet_sock in host order.
64 */
65 function tcpmib_remote_addr:long(sk:long)
66 {
67 return ntohl(__ip_sock_daddr(sk))
68 }
69
70 /**
71 * sfunction tcpmib_local_port - Get the local port
72 * @sk: pointer to a struct inet_sock
73 *
74 * Returns the sport from a struct inet_sock in host order.
75 */
76 function tcpmib_local_port:long(sk:long)
77 {
78 return __tcp_sock_sport(sk)
79 }
80
81 /**
82 * sfunction tcpmib_remote_port - Get the remote port
83 * @sk: pointer to a struct inet_sock
84 *
85 * Returns the dport from a struct inet_sock in host order.
86 */
87 function tcpmib_remote_port:long(sk:long)
88 {
89 return __tcp_sock_dport(sk)
90 }
91
92 /**
93 * probe tcpmib.ActiveOpens - Count an active opening of a socket
94 * @sk: pointer to the struct sock being acted on
95 * @op: value to be added to the counter (default value of 1)
96 *
97 * The packet pointed to by @skb is filtered by the function
98 * tcpmib_filter_key(). If the packet passes the filter is is
99 * counted in the global @ActiveOpens (equivalent to SNMP's MIB
100 * TCP_MIB_ACTIVEOPENS)
101 */
102 probe
103 tcpmib.ActiveOpens = kernel.function("tcp_connect").return
104 {
105 sk = @entry($sk);
106 op = 1;
107 if ( $return ) next;
108 // definition in tcpipstat.stp
109 key = tcpmib_filter_key(sk, op);
110 if ( key ) ActiveOpens[key] += op;
111 }
112
113 /**
114 * probe tcpmib.AttemptFails - Count a failed attempt to open a socket
115 * @sk: pointer to the struct sock being acted on
116 * @op: value to be added to the counter (default value of 1)
117 *
118 * The packet pointed to by @skb is filtered by the function
119 * tcpmib_filter_key(). If the packet passes the filter is is
120 * counted in the global @AttemptFails (equivalent to SNMP's MIB
121 * TCP_MIB_ATTEMPTFAILS)
122 */
123 probe
124 tcpmib.AttemptFails = kernel.function("tcp_done").call ?
125 {
126 sk = $sk;
127 state = tcpmib_get_state($sk);
128 op = 1;
129
130 TCP_SYN_SENT = 2;
131 TCP_SYN_RECV = 3;
132 if( state == TCP_SYN_SENT || state == TCP_SYN_RECV){
133 key = tcpmib_filter_key($sk, op);
134 if ( key ) AttemptFails[key] += op;
135 }
136 }
137
138 /**
139 * probe tcpmib.CurrEstab - Update the count of open sockets
140 * @sk: pointer to the struct sock being acted on
141 * @op: value to be added to the counter (default value of 1)
142 *
143 * The packet pointed to by @skb is filtered by the function
144 * tcpmib_filter_key(). If the packet passes the filter is is
145 * counted in the global @CurrEstab (equivalent to SNMP's MIB
146 * TCP_MIB_CURRESTAB)
147 */
148 probe
149 tcpmib.CurrEstab = kernel.function("tcp_set_state").call ?
150 {
151 sk = $sk;
152 state = $state;
153 oldstate = tcpmib_get_state($sk);
154
155 TCP_ESTABLISHED = 1;
156 if ( oldstate == TCP_ESTABLISHED ) {
157 op = -1;
158 key = tcpmib_filter_key($sk, op);
159 if ( key ) CurrEstab[key] += op;
160 } else {
161 if ( state == TCP_ESTABLISHED ) {
162 op = 1;
163 key = tcpmib_filter_key($sk, op);
164 if ( key ) CurrEstab[key] += op;
165 }
166 }
167 }
168
169 /**
170 * probe tcpmib.EstabResets - Count the reset of a socket
171 * @sk: pointer to the struct sock being acted on
172 * @op: value to be added to the counter (default value of 1)
173 *
174 * The packet pointed to by @skb is filtered by the function
175 * tcpmib_filter_key(). If the packet passes the filter is is
176 * counted in the global @EstabResets (equivalent to SNMP's MIB
177 * TCP_MIB_ESTABRESETS)
178 */
179 probe
180 tcpmib.EstabResets = kernel.function("tcp_set_state").call ?
181 {
182 sk = $sk;
183 state = $state;
184 oldstate = tcpmib_get_state($sk);
185 op = 1;
186
187 TCP_CLOSE = 7;
188 TCP_CLOSE_WAIT = 8;
189 TCP_ESTABLISHED = 1;
190 if ( ( state == TCP_CLOSE ) &&
191 (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED) ){
192 key = tcpmib_filter_key($sk, op);
193 if ( key ) EstabResets[key] += op;
194 }
195 }
196
197 /**
198 * probe tcpmib.InSegs - Count an incoming tcp segment
199 * @sk: pointer to the struct sock being acted on
200 * @op: value to be added to the counter (default value of 1)
201 *
202 * The packet pointed to by @skb is filtered by the function
203 * tcpmib_filter_key() (or ipmib_filter_key() for tcp v4).
204 * If the packet passes the filter is is
205 * counted in the global @InSegs (equivalent to SNMP's MIB
206 * TCP_MIB_INSEGS)
207 */
208 probe
209 tcpmib.InSegs =
210 kernel.function("__inet_lookup_established").return !,
211 kernel.function("tcp_v4_rcv")
212 {
213 if (@defined($return)) {
214 sk=$return
215 key = tcpmib_filter_key($return, op)
216 }
217 else {
218 # We don't know the sk in tcp_v4_rcv()
219 sk=0
220 key = ipmib_filter_key($skb, op, 0);
221 }
222 op=1
223 if ( key ) InSegs[key] += op;
224 }
225
226 /**
227 * probe tcpmib.OutRsts - Count the sending of a reset packet
228 * @sk: pointer to the struct sock being acted on
229 * @op: value to be added to the counter (default value of 1)
230 *
231 * The packet pointed to by @skb is filtered by the function
232 * tcpmib_filter_key(). If the packet passes the filter is is
233 * counted in the global @OutRsts (equivalent to SNMP's MIB
234 * TCP_MIB_OUTRSTS)
235 */
236 probe
237 tcpmib.OutRsts = __tcpmib.OutRsts.*
238 {
239 }
240
241 @__private30 function _rtn_local:long ()
242 %{
243 STAP_RETVALUE = RTN_LOCAL;
244 %}
245
246 @__private30 function _is_reset:long (skb:long)
247 %{ /* pure */
248 struct tcphdr *th;
249 struct sk_buff *skb = (struct sk_buff *)(uintptr_t)STAP_ARG_skb;
250 struct tcphdr th_copy;
251
252
253 #ifdef NET_SKBUFF_DATA_USES_OFFSET
254 th = (struct tcphdr *)(kread(&(skb->network_header)) + kread(&(skb->head)));
255 #else
256 th = (struct tcphdr *)kread(&(skb->network_header));
257 #endif
258
259 // We'd like to kread the 'rst' field here. But, it is a
260 // bitfield (and you can't take the address of a
261 // bitfield). So, let's kread the entire tcphdr, then grab the
262 // 'rst' field out of the copy. Luckily, the tcphdr is only 20
263 // bytes long.
264 memset (((void*)&th_copy), 0, sizeof(th_copy));
265 kderef_buffer(((void *)&th_copy), th, sizeof(struct tcphdr));
266 STAP_RETVALUE = th_copy.rst;
267 CATCH_DEREF_FAULT();
268 %}
269
270 @__private30 function _tcpmib_input_route_type:long (skb:long)
271 %{ /* pure */
272 struct rtable *rt;
273 struct sk_buff *skb = (struct sk_buff *)(uintptr_t)STAP_ARG_skb;
274
275 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
276 rt = (struct rtable *)kread(&(skb->_skb_refdst));
277 #else
278
279 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
280 rt = (struct rtable *)kread(&(skb->_skb_dst));
281 #else
282 rt = (struct rtable *)kread(&(skb->rtable));
283 #endif
284 #endif
285
286 if ( rt )
287 STAP_RETVALUE = kread(&(rt->rt_type));
288 else
289 STAP_RETVALUE = RTN_UNSPEC;
290 CATCH_DEREF_FAULT();
291 %}
292
293 /*
294 * note:
295 * tcp_v4_send_reset may be called with a NULL sk.
296 * This happens when sending a reset in response to a syn
297 * when no socket exists (for example the service is not running).
298 * Without a socket we can't count the reset.
299 */
300 probe
301 __tcpmib.OutRsts.tcp_v4_send_reset = kernel.function("tcp_v4_send_reset")
302 {
303 sk = @choose_defined($sk, 0)
304 op = 1;
305 if ( _is_reset($skb) ) next
306 if (_tcpmib_input_route_type($skb) != _rtn_local() )
307 next;
308 key = (@defined($sk) ? tcpmib_filter_key(sk, op)
309 : ipmib_filter_key($skb, op, 1))
310 if ( key ) OutRsts[key] += op;
311 }
312
313 probe
314 __tcpmib.OutRsts.tcp_send_active_reset =
315 kernel.function("tcp_send_active_reset")
316 {
317 /* Almost correct,
318 * If alloc_skb() fails it incorrectly bumps TCP_MIB_OUTRSTS,
319 */
320 sk = $sk;
321 op = 1;
322 key = tcpmib_filter_key($sk, op);
323 if ( key ) OutRsts[key] += op;
324 }
325
326 /**
327 * probe tcpmib.OutSegs - Count the sending of a TCP segment
328 * @sk: pointer to the struct sock being acted on
329 * @op: value to be added to the counter (default value of 1)
330 *
331 * The packet pointed to by @skb is filtered by the function
332 * tcpmib_filter_key(). If the packet passes the filter is is
333 * counted in the global @OutSegs (equivalent to SNMP's MIB
334 * TCP_MIB_OUTSEGS)
335 */
336 probe
337 tcpmib.OutSegs=kernel.function("ip_queue_xmit").return
338 {
339 if ( $return < 0 ) next;
340 op = 1;
341
342 // Only count the events with protocol IPPROTO_TCP,6.
343 try {
344 iphdr = __get_skb_iphdr(@entry($skb))
345 if ( !(__ip_skb_proto(iphdr) == 6) ) next ;
346 } catch { }
347 try {
348 sk = @entry($skb->sk);
349 key = tcpmib_filter_key(sk, op);
350 } catch { }
351 if ( key ) OutSegs[key] += op;
352 }
353
354 /**
355 * probe tcpmib.PassiveOpens - Count the passive creation of a socket
356 * @sk: pointer to the struct sock being acted on
357 * @op: value to be added to the counter (default value of 1)
358 *
359 * The packet pointed to by @skb is filtered by the function
360 * tcpmib_filter_key(). If the packet passes the filter is is
361 * counted in the global @PassiveOpens (equivalent to SNMP's MIB
362 * TCP_MIB_PASSIVEOPENS)
363 */
364 probe
365 tcpmib.PassiveOpens=kernel.function("tcp_v4_syn_recv_sock").return
366 {
367 sk = $return;
368 op = 1;
369 if ( !sk ) next;
370 key = tcpmib_filter_key(sk, op);
371 if ( key ) PassiveOpens[key] += op;
372 }
373
374 /**
375 * probe tcpmib.RetransSegs - Count the retransmission of a TCP segment
376 * @sk: pointer to the struct sock being acted on
377 * @op: value to be added to the counter (default value of 1)
378 *
379 * The packet pointed to by @skb is filtered by the function
380 * tcpmib_filter_key(). If the packet passes the filter is is
381 * counted in the global @RetransSegs (equivalent to SNMP's MIB
382 * TCP_MIB_RETRANSSEGS)
383 */
384 probe
385 tcpmib.RetransSegs=kernel.function("tcp_retransmit_skb").return
386 {
387 sk = @entry($sk);
388 op = 1;
389 if ( $return ) next;
390 key = tcpmib_filter_key(sk, op);
391 if ( key ) RetransSegs[key] += op;
392 }