summaryrefslogtreecommitdiffstats
path: root/tapset/tcpmib.stp
blob: 6a9b8145401762693efa6c32e3c91780c0f4a317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 *      Copyright (C) 2009 IBM Corp.
 *      This file is part of systemtap, and is free software.  You can
 *      redistribute it and/or modify it under the terms of the GNU General
 *      Public License (GPL); either version 2, or (at your option) any
 *      later version.
 *      
 *      Version 1.0     wilder@us.ibm.com     2009-07-06
 */

/* Global counter definitions for mib TCP_MIB. */

%{
#include <net/sock.h>
#include <linux/tcp.h>
#include <net/tcp_states.h>
#include <linux/skbuff.h>
#include <net/route.h>
%}

global ActiveOpens
%( kernel_v > "2.6.21" %?
global AttemptFails
%)
%( kernel_v > "2.6.24" %?
global CurrEstab
%)
%( kernel_v > "2.6.24" %?
global EstabResets
%)
// global InErrs, this mib is not yet supported
global InSegs
global OutRsts
global OutSegs
global PassiveOpens
global RetransSegs

/**
 * sfunction tcpmib_get_state - Get a socket's state.
 *
 * Returns the sk_state from a struct sock.
 * @sk: Pointer to a struct sock.
 *
 */
function tcpmib_get_state:long (sk:long)
%{ /* pure */
	struct sock *sk = (struct sock *)(long)THIS->sk;
	THIS->__retvalue = kread(&(sk->sk_state));
	CATCH_DEREF_FAULT();
%}

/**
 * sfunction tcpmib_local_addr - Get the source address.
 *
 * Returns the saddr from a struct inet_sock.
 * @sk: Pointer to a struct inet_sock.
 *
 */
function tcpmib_local_addr:long(sk:long)
%{ /* pure */
        struct inet_sock *inet = (struct inet_sock *) (long) THIS->sk;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
        THIS->__retvalue = ntohl(kread(&(inet->inet_saddr)));
#else
        THIS->__retvalue = ntohl(kread(&(inet->saddr)));
#endif
        CATCH_DEREF_FAULT();
%}

/**
 * sfunction tcpmib_remote_addr - Get the remote address.
 *
 * Returns the daddr from a struct inet_sock.
 * @sk: Pointer to a struct inet_sock.
 *
 */
function tcpmib_remote_addr:long(sk:long)
%{ /* pure */
        struct inet_sock *inet = (struct inet_sock *) (long) THIS->sk;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
        THIS->__retvalue = ntohl(kread(&(inet->inet_daddr)));
#else
        THIS->__retvalue = ntohl(kread(&(inet->daddr)));
#endif
        CATCH_DEREF_FAULT();
%}

/**
 * sfunction tcpmib_local_port - Get the local port.
 *
 * Returns the sport from a struct inet_sock.
 * @sk: Pointer to a struct inet_sock.
 *
 */
function tcpmib_local_port:long(sk:long)
%{ /* pure */
        struct inet_sock *inet = (struct inet_sock *) (long) THIS->sk;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
        THIS->__retvalue = ntohs(kread(&(inet->inet_sport)));
#else
        THIS->__retvalue = ntohs(kread(&(inet->sport)));
#endif
        CATCH_DEREF_FAULT();
%}

/**
 * sfunction tcpmib_remote_port - Get the remote port.
 *
 * Returns the dport from a struct inet_sock.
 * @sk: Pointer to a struct inet_sock.
 *
 */
function tcpmib_remote_port:long(sk:long)
%{ /* pure */
        struct inet_sock *inet = (struct inet_sock *) (long) THIS->sk;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33)
        THIS->__retvalue = ntohs(kread(&(inet->inet_dport)));
#else
        THIS->__retvalue = ntohs(kread(&(inet->dport)));
#endif
        CATCH_DEREF_FAULT();
%}

/**
 * probe tcpmib.ActiveOpens - Count an active opening of a socket.
 * @sk:	Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: ActiveOpens
 * MIB: TCP_MIB_ACTIVEOPENS
 *
 */
probe
tcpmib.ActiveOpens=kernel.function("tcp_connect").return
{
	sk = $sk;
	op = 1;
	if ( $return ) next;
        // definition in tcpipstat.stp
	key = tcpmib_filter_key(sk,op);
	if ( key ) ActiveOpens[key] += op;
}

/**
 * probe tcpmib.AttemptFails - Count a failed attempt to open a socket.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: AttemptFails
 * MIB: TCP_MIB_ATTEMPTFAILS
 *
 */
%( kernel_v > "2.6.21" %?
probe
tcpmib.AttemptFails=kernel.function("tcp_done")
{
	sk = $sk;
	state = tcpmib_get_state(sk);
	op = 1;

	TCP_SYN_SENT = 2;
	TCP_SYN_RECV = 3;
	if( state == TCP_SYN_SENT || state == TCP_SYN_RECV){
		key = tcpmib_filter_key(sk,op);
		if ( key ) AttemptFails[key] += op;
	}
}
%)

/**
 * probe tcpmib.CurrEstab - Update the count of open sockets.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: CurrEstab
 * MIB: TCP_MIB_CURRESTAB
 *
 */
%( kernel_v > "2.6.24" %?
probe
tcpmib.CurrEstab=kernel.function("tcp_set_state")
{
	sk = $sk;
	state = $state;
	oldstate = tcpmib_get_state(sk);

	TCP_ESTABLISHED = 1;
	if ( oldstate == TCP_ESTABLISHED ) {
		op = -1;
		key = tcpmib_filter_key(sk,op);
		if ( key ) CurrEstab[key] += op;
	} else {
		if ( state == TCP_ESTABLISHED ) {
			op = 1;
			key = tcpmib_filter_key(sk,op);
			if ( key ) CurrEstab[key] += op;
		}
	}
}
%)

/**
 * probe tcpmib.EstabResets - Count the reset of a socket.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: EstabResets
 * MIB: TCP_MIB_ESTABRESETS
 *
 */
%( kernel_v > "2.6.24" %?
probe
tcpmib.EstabResets=kernel.function("tcp_set_state")
{
	sk = $sk;
        state = $state;
        oldstate = tcpmib_get_state(sk);
	op = 1;

	TCP_CLOSE = 7;
	TCP_CLOSE_WAIT = 8;
	TCP_ESTABLISHED = 1;
	if ( ( state == TCP_CLOSE ) && 
	   (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED) ){
        	key = tcpmib_filter_key(sk,op);
		if ( key ) EstabResets[key] += op;
	}
}
%)

/**
 * probe tcpmib.InSegs - Count an incomming tcp segment.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: InSegs
 * MIB: TCP_MIB_INSEGS
 *
 */
%( kernel_v > "2.6.24" %?
probe
tcpmib.InSegs=kernel.function("__inet_lookup_established").return ?
{
        sk=$return
	op=1
	key = tcpmib_filter_key(sk,op);
	if ( key ) InSegs[key] += op;
}
%:
probe
tcpmib.InSegs=kernel.function("tcp_v4_rcv")
{
	skb=$skb
	op=1;
	key = ipmib_filter_key(skb,op,0);
	if ( key ) InSegs[key] += op;
}
%)

/**
 * probe tcpmib.OutRsts - Count the sending of a reset packet.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: OutRsts
 * MIB: TCP_MIB_OUTRSTS
 *
 */
probe
tcpmib.OutRsts = 	tcpmib.OutRsts.A,
			tcpmib.OutRsts.B { }

function _rtn_local:long ()
%{
        THIS->__retvalue = RTN_LOCAL;
%}

function _is_reset:long (skb:long)
%{ /* pure */
        struct tcphdr *th;
        struct sk_buff *skb = (struct sk_buff *)(long)THIS->skb;

	#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,21)
		th = (struct tcphdr *)kread(&(skb->h.th));
	#else

	#ifdef NET_SKBUFF_DATA_USES_OFFSET
		th = (struct tcphdr *)(kread(&(skb->network_header)) + kread(&(skb->head)));
	#else
		th = (struct tcphdr *)kread(&(skb->network_header));
	#endif
	#endif
        THIS->__retvalue = th->rst;
        CATCH_DEREF_FAULT();
%}

function _tcpmib_input_route_type:long (skb:long)
%{ /* pure */
	struct rtable *rt;
	struct sk_buff *skb = (struct sk_buff *)(long)THIS->skb;

	#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
		rt = (struct rtable *)kread(&(skb->_skb_dst));
	#else

	#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
		rt = (struct rtable *)kread(&(skb->dst));
	#else
		rt = (struct rtable *)kread(&(skb->rtable));
	#endif
	#endif

	if ( rt )
		THIS->__retvalue = kread(&(rt->rt_type));
	else
		THIS->__retvalue = RTN_UNSPEC;
	CATCH_DEREF_FAULT();
%}

/*
 * note:
 * tcp_v4_send_reset may be called with a NULL sk.
 * This happens when sending a reset in response to a syn 
 * when no socket exists (for example the service is not running).
 * Without a socket we can't count the reset.
 */
probe
tcpmib.OutRsts.A=kernel.function("tcp_v4_send_reset")
{
%( kernel_v >= "2.6.20" %?
        sk = $sk;
%:
        sk = 0;
%)
        skb = $skb
        op = 1;
        if ( _is_reset(skb) ) next
        if (_tcpmib_input_route_type($skb) != _rtn_local() )
                next;
%( kernel_v > "2.6.20" %?
        key = tcpmib_filter_key(sk,op);
%:
	key = ipmib_filter_key(skb,op,1);
%)
        if ( key ) OutRsts[key] += op;
}

probe
tcpmib.OutRsts.B= kernel.function("tcp_send_active_reset")
{
	/* Almost correct,
	 * If alloc_skb() fails it incorrectly bumps TCP_MIB_OUTRSTS,
	 */
	sk = $sk;
	op = 1;
	key = tcpmib_filter_key(sk,op);
        if ( key ) OutRsts[key] += op;
}

/**
 * probe tcpmib.OutSegs - Count the sending of a TCP segment.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: OutSegs
 * MIB: TCP_MIB_OUTSEGS
 *
 */
probe
tcpmib.OutSegs=kernel.function("ip_queue_xmit").return
{
	if ( $return < 0 ) next;
	sk = $skb->sk;
	op = 1;
	
        //  Only count the events with protocol IPPROTO_TCP,6.
	iphdr = __get_skb_iphdr($skb);
        if( !(__ip_skb_proto(iphdr) == 6) ) next ;
	key = tcpmib_filter_key(sk,op);
	if ( key ) OutSegs[key] += op;
}

/**
 * probe tcpmib.PassiveOpens - Count the passive creation of a socket.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: PassiveOpens
 * MIB: TCP_MIB_PASSIVEOPENS
 *
 */
probe
tcpmib.PassiveOpens=kernel.function("tcp_v4_syn_recv_sock").return
{
        sk = $return;
        op = 1;
        if ( !sk ) next;
        key = tcpmib_filter_key(sk,op);
        if ( key ) PassiveOpens[key] += op;
}

/**
 * probe tcpmib.RetransSegs - Count the retransmission of a TCP segment.
 * @sk: Pointer to the struct sock being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: RetransSegs
 * MIB: TCP_MIB_RETRANSSEGS
 *
 */
probe
tcpmib.RetransSegs=kernel.function("tcp_retransmit_skb").return
{
	sk = $sk;
	op = 1;
	if ( $return ) next;
	key = tcpmib_filter_key(sk,op);
	if ( key ) RetransSegs[key] += op;
}