xref: /xnu-11215/bsd/dev/dtrace/scripts/tcp.d (revision 186b8fce)
1 /*
2  * Copyright (c) 2006-2008 Apple Computer, Inc.  All Rights Reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 #pragma D depends_on library darwin.d
25 #pragma D depends_on module mach_kernel
26 #pragma D depends_on provider tcp
27 
28 /*
29  * TCP flags
30  */
31 inline int TH_FIN = 0x01;
32 #pragma D binding "1.0" TH_FIN
33 inline int TH_SYN = 0x02;
34 #pragma D binding "1.0" TH_SYN
35 inline int TH_RST = 0x04;
36 #pragma D binding "1.0" TH_RST
37 inline int TH_PUSH = 0x08;
38 #pragma D binding "1.0" TH_PUSH
39 inline int TH_ACK = 0x10;
40 #pragma D binding "1.0" TH_ACK
41 inline int TH_URG = 0x20;
42 #pragma D binding "1.0" TH_URG
43 inline int TH_ECE = 0x40;
44 #pragma D binding "1.0" TH_ECE
45 inline int TH_CWR = 0x80;
46 #pragma D binding "1.0" TH_CWR
47 
48 /*
49  * TCP states
50  */
51 inline int TCPS_CLOSED = 0;
52 #pragma D binding "1.0" TCPS_CLOSED
53 inline int TCPS_LISTEN = 1;
54 #pragma D binding "1.0" TCPS_LISTEN
55 inline int TCPS_SYN_SENT = 2;
56 #pragma D binding "1.0" TCPS_SYN_SENT
57 inline int TCPS_SYN_RECEIVED = 3;
58 #pragma D binding "1.0" TCPS_SYN_RECEIVED
59 inline int TCPS_ESTABLISHED = 4;
60 #pragma D binding "1.0" TCPS_ESTABLISHED
61 inline int TCPS_CLOSE_WAIT = 5;
62 #pragma D binding "1.0" TCPS_CLOSE_WAIT
63 inline int TCPS_FIN_WAIT_1 = 6;
64 #pragma D binding "1.0" TCPS_FIN_WAIT_1
65 inline int TCPS_CLOSING = 7;
66 #pragma D binding "1.0" TCPS_CLOSING
67 inline int TCPS_LAST_ACK = 8;
68 #pragma D binding "1.0" TCPS_LAST_ACK
69 inline int TCPS_FIN_WAIT_2 = 9;
70 #pragma D binding "1.0" TCPS_FIN_WAIT_2
71 inline int TCPS_TIME_WAIT = 10;
72 #pragma D binding "1.0" TCPS_TIME_WAIT
73 
74 /*
75  * TCP congestion control events
76  */
77 inline int TCP_CC_CWND_INIT = 0;
78 #pragma D binding "1.0" TCP_CC_CWND_INIT
79 inline int TCP_CC_INSEQ_ACK_RCVD = 1;
80 #pragma D binding "1.0" TCP_CC_INSEQ_ACK_RCVD
81 inline int TCP_CC_ACK_RCVD = 2;
82 #pragma D binding "1.0" TCP_CC_ACK_RCVD
83 inline int TCP_CC_ENTER_FASTRECOVERY = 3;
84 #pragma D binding "1.0" TCP_CC_ENTER_FASTRECOVERY
85 inline int TCP_CC_IN_FASTRECOVERY = 4;
86 #pragma D binding "1.0" TCP_CC_IN_FASTRECOVERY
87 inline int TCP_CC_EXIT_FASTRECOVERY = 5;
88 #pragma D binding "1.0" TCP_CC_EXIT_FASTRECOVERY
89 inline int TCP_CC_PARTIAL_ACK = 6;
90 #pragma D binding "1.0" TCP_CC_PARTIAL_ACK
91 inline int TCP_CC_IDLE_TIMEOUT = 7;
92 #pragma D binding "1.0" TCP_CC_IDLE_TIMEOUT
93 inline int TCP_CC_REXMT_TIMEOUT = 8;
94 #pragma D binding "1.0" TCP_CC_REXMT_TIMEOUT
95 inline int TCP_CC_ECN_RCVD = 9;
96 #pragma D binding "1.0" TCP_CC_ECN_RCVD
97 inline int TCP_CC_BAD_REXMT_RECOVERY = 10;
98 #pragma D binding "1.0" TCP_CC_BAD_REXMT_RECOVERY
99 inline int TCP_CC_OUTPUT_ERROR = 11;
100 #pragma D binding "1.0" TCP_CC_OUTPUT_ERROR
101 inline int TCP_CC_CHANGE_ALGO = 12;
102 #pragma D binding "1.0" TCP_CC_CHANGE_ALGO
103 inline int TCP_CC_FLOW_CONTROL = 13;
104 #pragma D binding "1.0" TCP_CC_FLOW_CONTROL
105 inline int TCP_CC_SUSPEND = 14;
106 #pragma D binding "1.0" TCP_CC_SUSPEND
107 inline int TCP_CC_LIMITED_TRANSMIT = 15;
108 #pragma D binding "1.0" TCP_CC_LIMITED_TRANSMIT
109 inline int TCP_CC_EARLY_RETRANSMIT = 16;
110 #pragma D binding "1.0" TCP_CC_EARLY_RETRANSMIT
111 
112 
113 /*
114  * tcpinfo is the TCP header field
115  */
116 typedef struct tcpinfo {
117 	uint16_t tcp_sport;	/* source port */
118 	uint16_t tcp_dport;	/* destination port */
119 	uint32_t tcp_seq;	/* sequence number */
120 	uint32_t tcp_ack;	/* acknowledgement number */
121 	uint8_t tcp_offset;	/* data offset, in bytes */
122 	uint8_t tcp_flags;	/* flags */
123 	uint16_t tcp_window;	/* window size */
124 	uint16_t tcp_checksum;	/* checksum */
125 	uint16_t tcp_urgent;	/* urgent data pointer */
126 	struct tcphdr *tcp_hdr;	/* raw TCP header */
127 } tcpinfo_t;
128 
129 #pragma D binding "1.0" translator
130 translator tcpinfo_t < struct tcphdr *T > {
131 	tcp_sport = ntohs(T->th_sport);
132 	tcp_dport = ntohs(T->th_dport);
133 	tcp_seq = ntohl(T->th_seq);
134 	tcp_ack = ntohl(T->th_ack);
135 	tcp_offset = T->th_off << 2;
136 	tcp_flags = T->th_flags;
137 	tcp_window = ntohs(T->th_win);
138 	tcp_checksum = ntohs(T->th_sum);
139 	tcp_urgent = ntohs(T->th_urp);
140 	tcp_hdr = T;
141 };
142 
143 /*
144  * tcpsinfo contains stable TCP details from TCP control block
145  */
146 typedef struct tcpsinfo {
147 	int tcps_local;		/* is delivered locally, boolean */
148 	int tcps_active;	/* active open, boolean */
149 	string tcps_state;	/* TCP state, as a string */
150 	u_int t_flags;		/* flags */
151 	uint32_t rcv_nxt;	/* receive next */
152 	uint32_t rcv_adv;	/* advertised window */
153 	uint32_t rcv_wnd;	/* receive window */
154 	uint32_t snd_wnd;	/* send window */
155 	uint32_t snd_cwnd;	/* congestion controlled window */
156 	uint32_t snd_ssthresh;	/* slow-start threshold */
157 	uint32_t snd_una;	/* send unacknowledged */
158 	uint32_t snd_nxt;	/* send next */
159 	uint32_t snd_max;	/* send max */
160 	uint32_t snd_recover;	/* send recover for NewReno */
161 	int	t_rxtcur;	/* retransmit timeout in ms */
162 	u_int	t_maxseg;	/* maximum segment size */
163 	u_int	t_rttbest;	/* best rtt we have seen */
164 	int	rcv_numsacks;	/* number of sack blocks present */
165 	int	snd_numholes;	/* number of holes seen by sender */
166 	struct tcpcb* tcpcb;	/* Pointer to tcp control block */
167 } tcpsinfo_t;
168 
169 #pragma D binding "1.0" translator
170 translator tcpsinfo_t < struct tcpcb *T> {
171 	tcps_local = 0;		/* Not used */
172 	tcps_active = 0;
173 	tcps_state = T ?
174 		T->t_state == TCPS_CLOSED ? "state-closed" :
175 		T->t_state == TCPS_LISTEN ? "state-listen" :
176 		T->t_state == TCPS_SYN_SENT ? "state-syn-sent" :
177 		T->t_state == TCPS_SYN_RECEIVED ? "state-syn-received" :
178 		T->t_state == TCPS_ESTABLISHED ? "state-established" :
179 		T->t_state == TCPS_CLOSE_WAIT ? "state-close-wait" :
180 		T->t_state == TCPS_FIN_WAIT_1 ? "state-fin-wait1" :
181 		T->t_state == TCPS_CLOSING ? "state-closing" :
182 		T->t_state == TCPS_LAST_ACK ? "state-last-ack" :
183 		T->t_state == TCPS_FIN_WAIT_2 ? "state-fin-wait2" :
184 		T->t_state == TCPS_TIME_WAIT ? "state-time-wait" :
185 		"<unknown>" : "<null>";
186 	t_flags = T->t_flags;
187 	rcv_nxt = T->rcv_nxt;
188 	rcv_adv = T->rcv_adv;
189 	rcv_wnd = T->rcv_wnd;
190 	snd_wnd = T->snd_wnd;
191 	snd_cwnd = T->snd_cwnd;
192 	snd_ssthresh = T->snd_ssthresh;
193 	snd_una = T->snd_una;
194 	snd_nxt = T->snd_nxt;
195 	snd_max = T->snd_max;
196 	snd_recover = T->snd_recover;
197 	t_rxtcur = T->t_rxtcur;
198 	t_maxseg = T->t_maxseg;
199 	t_rttbest = T->t_rttbest;
200 	rcv_numsacks = T->rcv_numsacks;
201 	snd_numholes = T->snd_numholes;
202 	tcpcb = T;
203 };
204 
205 /*
206  * tcpnsinfo provides the new tcp state for state changes.
207  */
208 typedef struct tcpnsinfo {
209 	string tcps_state;	/* TCP state, as a string */
210 } tcpnsinfo_t;
211 
212 #pragma D binding "1.0" translator
213 translator tcpnsinfo_t < int32_t I > {
214 	tcps_state = I ?
215 		I == TCPS_LISTEN ? "state-listen" :
216 		I == TCPS_SYN_SENT ? "state-syn-sent" :
217 		I == TCPS_SYN_RECEIVED ? "state-syn-received" :
218 		I == TCPS_ESTABLISHED ? "state-established" :
219 		I == TCPS_CLOSE_WAIT ? "state-close-wait" :
220 		I == TCPS_FIN_WAIT_1 ? "state-fin-wait1" :
221 		I == TCPS_CLOSING ? "state-closing" :
222 		I == TCPS_LAST_ACK ? "state-last-ack" :
223 		I == TCPS_FIN_WAIT_2 ? "state-fin-wait2" :
224 		I == TCPS_TIME_WAIT ? "state-time-wait" :
225 		"<unknown>" : "state-closed";
226 };
227 
228 /*
229  * tcpccevent provides the congestion control event for TCP cc probes
230  */
231 typedef struct tcpccevent {
232 	string tcp_cc;		/* TCP congestion control event, as a string */
233 } tcpccevent_t;
234 
235 #pragma D binding "1.0" translator
236 translator tcpccevent_t < int32_t I > {
237 	tcp_cc = I ?
238 		I == TCP_CC_INSEQ_ACK_RCVD ? "inseq-ack-rcvd" :
239 		I == TCP_CC_ACK_RCVD ? "ack-rcvd" :
240 		I == TCP_CC_ENTER_FASTRECOVERY ? "enter-fastrecovery" :
241 		I == TCP_CC_EXIT_FASTRECOVERY ? "exit-fastrecovery" :
242 		I == TCP_CC_PARTIAL_ACK ? "partial-ack" :
243 		I == TCP_CC_IDLE_TIMEOUT ? "idle-timeout" :
244 		I == TCP_CC_REXMT_TIMEOUT ? "rexmt-timeout" :
245 		I == TCP_CC_ECN_RCVD ? "ecn-rcvd" :
246 		I == TCP_CC_BAD_REXMT_RECOVERY ? "bad-rexmt" :
247 		I == TCP_CC_OUTPUT_ERROR ? "output-error" :
248 		I == TCP_CC_CHANGE_ALGO ? "change-algo" :
249 		I == TCP_CC_FLOW_CONTROL ? "flow-control" :
250 		I == TCP_CC_SUSPEND ? "suspend" :
251 		I == TCP_CC_LIMITED_TRANSMIT ? "limited-transmit" :
252 		I == TCP_CC_EARLY_RETRANSMIT ? "early-rexmt" :
253 		"<unknown>" : "cwnd-init";
254 };
255