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;
82	sleep(2);
83EOPERL
84
85$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
86BEGIN
87{
88	ipsend = tcpsend = ipreceive = tcpreceive = 0;
89	connreq = connest = connaccept = 0;
90}
91
92ip:::send
93/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
94    args[4]->ipv4_protocol == IPPROTO_TCP/
95{
96	ipsend++;
97}
98
99tcp:::send
100/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
101 (args[4]->tcp_sport == $tcpport || args[4]->tcp_dport == $tcpport)/
102{
103	tcpsend++;
104}
105
106ip:::receive
107/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
108    args[4]->ipv4_protocol == IPPROTO_TCP/
109{
110	ipreceive++;
111}
112
113tcp:::receive
114/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
115 (args[4]->tcp_sport == $tcpport || args[4]->tcp_dport == $tcpport)/
116{
117	tcpreceive++;
118}
119
120tcp:::state-change
121{
122	state_event[args[3]->tcps_state]++;
123}
124
125tcp:::connect-request
126/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
127 args[4]->tcp_dport == $tcpport/
128{
129	connreq++;
130}
131
132tcp:::connect-established
133/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
134 args[4]->tcp_sport == $tcpport/
135{
136	connest++;
137}
138
139tcp:::accept-established
140/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
141 args[4]->tcp_dport == $tcpport/
142{
143	connaccept++;
144}
145
146END
147{
148	printf("Minimum TCP events seen\n\n");
149	printf("ip:::send - %s\n", ipsend >= 5 ? "yes" : "no");
150	printf("ip:::receive - %s\n", ipreceive >= 5 ? "yes" : "no");
151	printf("tcp:::send - %s\n", tcpsend >= 7 ? "yes" : "no");
152	printf("tcp:::receive - %s\n", tcpreceive >= 7 ? "yes" : "no");
153	printf("tcp:::state-change to syn-sent - %s\n",
154	    state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
155	printf("tcp:::state-change to syn-received - %s\n",
156	    state_event[TCP_STATE_SYN_RECEIVED] >=1 ? "yes" : "no");
157	printf("tcp:::state-change to established - %s\n",
158	    state_event[TCP_STATE_ESTABLISHED] >= 2 ? "yes" : "no");
159	printf("tcp:::state-change to fin-wait-1 - %s\n",
160	    state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
161	printf("tcp:::state-change to close-wait - %s\n",
162	    state_event[TCP_STATE_CLOSE_WAIT] >= 1 ? "yes" : "no");
163	printf("tcp:::state-change to fin-wait-2 - %s\n",
164	    state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
165	printf("tcp:::state-change to last-ack - %s\n",
166	    state_event[TCP_STATE_LAST_ACK] >= 1 ? "yes" : "no");
167	printf("tcp:::state-change to time-wait - %s\n",
168	    state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
169	printf("tcp:::connect-request - %s\n",
170	    connreq >=1 ? "yes" : "no");
171	printf("tcp:::connect-established - %s\n",
172	    connest >=1 ? "yes" : "no");
173	printf("tcp:::accept-established - %s\n",
174	    connaccept >=1 ? "yes" : "no");
175}
176EODTRACE
177
178status=$?
179
180cd /
181/bin/rm -rf $DIR
182
183exit $status
184