xref: /illumos-gate/usr/src/cmd/dtrace/test/tst/common/ip/tst.localtcpstate.ksh (revision b1d7ec75953cd517f5b7c3d9cb427ff8ec5d7d07)
1#!/usr/bin/ksh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25#
26
27#
28# Test tcp:::state-change and tcp:::{send,receive} by connecting to
29# the local ssh service and sending a test message. This should result
30# in a "Protocol mismatch" response and a close of the connection.
31# A number of state transition events along with tcp fusion send and
32# receive events for the message should result.
33#
34# This may fail due to:
35#
36# 1. A change to the ip stack breaking expected probe behavior,
37#    which is the reason we are testing.
38# 2. The lo0 interface missing or not up.
39# 3. The local ssh service is not online.
40# 4. An unlikely race causes the unlocked global send/receive
41#    variables to be corrupted.
42#
43# This test performs a TCP connection to the ssh service (port 22) and
44# checks that at least the following packet counts were traced:
45#
46# 3 x ip:::send (2 during the TCP handshake, then a FIN)
47# 4 x tcp:::send (2 during the TCP handshake, 1 message then a FIN)
48# 2 x ip:::receive (1 during the TCP handshake, then the FIN ACK)
49# 3 x tcp:::receive (1 during the TCP handshake, 1 message then the FIN ACK)
50#
51# The actual ip count tested is 5 each way, since we are tracing both
52# source and destination events.  The actual tcp count tested is 7
53# each way, since the TCP fusion send/receive events will not reach IP.
54#
55# For this test to work, we are assuming that the TCP handshake and
56# TCP close will enter the IP code path and not use tcp fusion.
57#
58
59if (( $# != 1 )); then
60	print -u2 "expected one argument: <dtrace-path>"
61	exit 2
62fi
63
64dtrace=$1
65local=127.0.0.1
66tcpport=22
67DIR=/var/tmp/dtest.$$
68
69mkdir $DIR
70cd $DIR
71
72cat > test.pl <<-EOPERL
73	use IO::Socket;
74	my \$s = IO::Socket::INET->new(
75	    Proto => "tcp",
76	    PeerAddr => "$local",
77	    PeerPort => $tcpport,
78	    Timeout => 3);
79	die "Could not connect to host $local port $tcpport" unless \$s;
80	print \$s "testing state machine transitions";
81	close \$s;
82EOPERL
83
84$dtrace -c '/usr/bin/perl test.pl' -qs /dev/stdin <<EODTRACE
85BEGIN
86{
87	ipsend = tcpsend = ipreceive = tcpreceive = 0;
88	connreq = connest = connaccept = 0;
89}
90
91ip:::send
92/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
93    args[4]->ipv4_protocol == IPPROTO_TCP/
94{
95	ipsend++;
96}
97
98tcp:::send
99/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
100 (args[4]->tcp_sport == $tcpport || args[4]->tcp_dport == $tcpport)/
101{
102	tcpsend++;
103}
104
105ip:::receive
106/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
107    args[4]->ipv4_protocol == IPPROTO_TCP/
108{
109	ipreceive++;
110}
111
112tcp:::receive
113/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
114 (args[4]->tcp_sport == $tcpport || args[4]->tcp_dport == $tcpport)/
115{
116	tcpreceive++;
117}
118
119tcp:::state-change
120{
121	state_event[args[3]->tcps_state]++;
122}
123
124tcp:::connect-request
125/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
126 args[4]->tcp_dport == $tcpport/
127{
128	connreq++;
129}
130
131tcp:::connect-established
132/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
133 args[4]->tcp_sport == $tcpport/
134{
135	connest++;
136}
137
138tcp:::accept-established
139/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
140 args[4]->tcp_dport == $tcpport/
141{
142	connaccept++;
143}
144
145END
146{
147	printf("Minimum TCP events seen\n\n");
148	printf("ip:::send - %s\n", ipsend >= 5 ? "yes" : "no");
149	printf("ip:::receive - %s\n", ipreceive >= 5 ? "yes" : "no");
150	printf("tcp:::send - %s\n", tcpsend >= 7 ? "yes" : "no");
151	printf("tcp:::receive - %s\n", tcpreceive >= 7 ? "yes" : "no");
152	printf("tcp:::state-change to syn-sent - %s\n",
153	    state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
154	printf("tcp:::state-change to syn-received - %s\n",
155	    state_event[TCP_STATE_SYN_RECEIVED] >=1 ? "yes" : "no");
156	printf("tcp:::state-change to established - %s\n",
157	    state_event[TCP_STATE_ESTABLISHED] >= 2 ? "yes" : "no");
158	printf("tcp:::state-change to fin-wait-1 - %s\n",
159	    state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
160	printf("tcp:::state-change to close-wait - %s\n",
161	    state_event[TCP_STATE_CLOSE_WAIT] >= 1 ? "yes" : "no");
162	printf("tcp:::state-change to fin-wait-2 - %s\n",
163	    state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
164	printf("tcp:::state-change to last-ack - %s\n",
165	    state_event[TCP_STATE_LAST_ACK] >= 1 ? "yes" : "no");
166	printf("tcp:::state-change to time-wait - %s\n",
167	    state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
168	printf("tcp:::connect-request - %s\n",
169	    connreq >=1 ? "yes" : "no");
170	printf("tcp:::connect-established - %s\n",
171	    connest >=1 ? "yes" : "no");
172	printf("tcp:::accept-established - %s\n",
173	    connaccept >=1 ? "yes" : "no");
174}
175EODTRACE
176
177status=$?
178
179cd /
180/usr/bin/rm -rf $DIR
181
182exit $status
183