1210753Srpaulo#!/usr/bin/ksh
2210753Srpaulo#
3210753Srpaulo# CDDL HEADER START
4210753Srpaulo#
5210753Srpaulo# The contents of this file are subject to the terms of the
6210753Srpaulo# Common Development and Distribution License (the "License").
7210753Srpaulo# You may not use this file except in compliance with the License.
8210753Srpaulo#
9210753Srpaulo# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10210753Srpaulo# or http://www.opensolaris.org/os/licensing.
11210753Srpaulo# See the License for the specific language governing permissions
12210753Srpaulo# and limitations under the License.
13210753Srpaulo#
14210753Srpaulo# When distributing Covered Code, include this CDDL HEADER in each
15210753Srpaulo# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16210753Srpaulo# If applicable, add the following below this CDDL HEADER, with the
17210753Srpaulo# fields enclosed by brackets "[]" replaced with your own identifying
18210753Srpaulo# information: Portions Copyright [yyyy] [name of copyright owner]
19210753Srpaulo#
20210753Srpaulo# CDDL HEADER END
21210753Srpaulo#
22210753Srpaulo
23210753Srpaulo#
24210753Srpaulo# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25210753Srpaulo#
26210753Srpaulo
27210753Srpaulo#
28210753Srpaulo# Test tcp:::state-change and tcp:::{send,receive} by connecting to
29210753Srpaulo# the local ssh service and sending a test message. This should result
30210753Srpaulo# in a "Protocol mismatch" response and a close of the connection.
31210753Srpaulo# A number of state transition events along with tcp fusion send and
32210753Srpaulo# receive events for the message should result.
33210753Srpaulo#
34210753Srpaulo# This may fail due to:
35210753Srpaulo#
36210753Srpaulo# 1. A change to the ip stack breaking expected probe behavior,
37210753Srpaulo#    which is the reason we are testing.
38210753Srpaulo# 2. The lo0 interface missing or not up.
39210753Srpaulo# 3. The local ssh service is not online.
40210753Srpaulo# 4. An unlikely race causes the unlocked global send/receive
41210753Srpaulo#    variables to be corrupted.
42210753Srpaulo#
43210753Srpaulo# This test performs a TCP connection to the ssh service (port 22) and
44210753Srpaulo# checks that at least the following packet counts were traced:
45210753Srpaulo#
46210753Srpaulo# 3 x ip:::send (2 during the TCP handshake, then a FIN)
47210753Srpaulo# 4 x tcp:::send (2 during the TCP handshake, 1 message then a FIN)
48210753Srpaulo# 2 x ip:::receive (1 during the TCP handshake, then the FIN ACK)
49210753Srpaulo# 3 x tcp:::receive (1 during the TCP handshake, 1 message then the FIN ACK)
50210753Srpaulo#
51210753Srpaulo# The actual ip count tested is 5 each way, since we are tracing both
52210753Srpaulo# source and destination events.  The actual tcp count tested is 7
53210753Srpaulo# each way, since the TCP fusion send/receive events will not reach IP.
54210753Srpaulo#
55210753Srpaulo# For this test to work, we are assuming that the TCP handshake and
56210753Srpaulo# TCP close will enter the IP code path and not use tcp fusion.
57210753Srpaulo#
58210753Srpaulo
59210753Srpauloif (( $# != 1 )); then
60210753Srpaulo	print -u2 "expected one argument: <dtrace-path>"
61210753Srpaulo	exit 2
62210753Srpaulofi
63210753Srpaulo
64210753Srpaulodtrace=$1
65210753Srpaulolocal=127.0.0.1
66210753Srpaulotcpport=22
67210753SrpauloDIR=/var/tmp/dtest.$$
68210753Srpaulo
69210753Srpaulomkdir $DIR
70210753Srpaulocd $DIR
71210753Srpaulo
72210753Srpaulocat > test.pl <<-EOPERL
73210753Srpaulo	use IO::Socket;
74210753Srpaulo	my \$s = IO::Socket::INET->new(
75210753Srpaulo	    Proto => "tcp",
76210753Srpaulo	    PeerAddr => "$local",
77210753Srpaulo	    PeerPort => $tcpport,
78210753Srpaulo	    Timeout => 3);
79210753Srpaulo	die "Could not connect to host $local port $tcpport" unless \$s;
80210753Srpaulo	print \$s "testing state machine transitions";
81210753Srpaulo	close \$s;
82280773Smarkj	sleep(2);
83210753SrpauloEOPERL
84210753Srpaulo
85286171Smarkj$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
86210753SrpauloBEGIN
87210753Srpaulo{
88210753Srpaulo	ipsend = tcpsend = ipreceive = tcpreceive = 0;
89210753Srpaulo	connreq = connest = connaccept = 0;
90210753Srpaulo}
91210753Srpaulo
92210753Srpauloip:::send
93210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
94210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
95210753Srpaulo{
96210753Srpaulo	ipsend++;
97210753Srpaulo}
98210753Srpaulo
99210753Srpaulotcp:::send
100210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
101210753Srpaulo (args[4]->tcp_sport == $tcpport || args[4]->tcp_dport == $tcpport)/
102210753Srpaulo{
103210753Srpaulo	tcpsend++;
104210753Srpaulo}
105210753Srpaulo
106210753Srpauloip:::receive
107210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
108210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
109210753Srpaulo{
110210753Srpaulo	ipreceive++;
111210753Srpaulo}
112210753Srpaulo
113210753Srpaulotcp:::receive
114210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
115210753Srpaulo (args[4]->tcp_sport == $tcpport || args[4]->tcp_dport == $tcpport)/
116210753Srpaulo{
117210753Srpaulo	tcpreceive++;
118210753Srpaulo}
119210753Srpaulo
120210753Srpaulotcp:::state-change
121210753Srpaulo{
122210753Srpaulo	state_event[args[3]->tcps_state]++;
123210753Srpaulo}
124210753Srpaulo
125210753Srpaulotcp:::connect-request
126210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
127210753Srpaulo args[4]->tcp_dport == $tcpport/
128210753Srpaulo{
129210753Srpaulo	connreq++;
130210753Srpaulo}
131210753Srpaulo
132210753Srpaulotcp:::connect-established
133210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
134210753Srpaulo args[4]->tcp_sport == $tcpport/
135210753Srpaulo{
136210753Srpaulo	connest++;
137210753Srpaulo}
138210753Srpaulo
139210753Srpaulotcp:::accept-established
140210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
141210753Srpaulo args[4]->tcp_dport == $tcpport/
142210753Srpaulo{
143210753Srpaulo	connaccept++;
144210753Srpaulo}
145210753Srpaulo
146210753SrpauloEND
147210753Srpaulo{
148210753Srpaulo	printf("Minimum TCP events seen\n\n");
149210753Srpaulo	printf("ip:::send - %s\n", ipsend >= 5 ? "yes" : "no");
150210753Srpaulo	printf("ip:::receive - %s\n", ipreceive >= 5 ? "yes" : "no");
151210753Srpaulo	printf("tcp:::send - %s\n", tcpsend >= 7 ? "yes" : "no");
152210753Srpaulo	printf("tcp:::receive - %s\n", tcpreceive >= 7 ? "yes" : "no");
153210753Srpaulo	printf("tcp:::state-change to syn-sent - %s\n",
154210753Srpaulo	    state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
155210753Srpaulo	printf("tcp:::state-change to syn-received - %s\n",
156210753Srpaulo	    state_event[TCP_STATE_SYN_RECEIVED] >=1 ? "yes" : "no");
157210753Srpaulo	printf("tcp:::state-change to established - %s\n",
158210753Srpaulo	    state_event[TCP_STATE_ESTABLISHED] >= 2 ? "yes" : "no");
159210753Srpaulo	printf("tcp:::state-change to fin-wait-1 - %s\n",
160210753Srpaulo	    state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
161210753Srpaulo	printf("tcp:::state-change to close-wait - %s\n",
162210753Srpaulo	    state_event[TCP_STATE_CLOSE_WAIT] >= 1 ? "yes" : "no");
163210753Srpaulo	printf("tcp:::state-change to fin-wait-2 - %s\n",
164210753Srpaulo	    state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
165210753Srpaulo	printf("tcp:::state-change to last-ack - %s\n",
166210753Srpaulo	    state_event[TCP_STATE_LAST_ACK] >= 1 ? "yes" : "no");
167210753Srpaulo	printf("tcp:::state-change to time-wait - %s\n",
168210753Srpaulo	    state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
169210753Srpaulo	printf("tcp:::connect-request - %s\n",
170210753Srpaulo	    connreq >=1 ? "yes" : "no");
171210753Srpaulo	printf("tcp:::connect-established - %s\n",
172210753Srpaulo	    connest >=1 ? "yes" : "no");
173210753Srpaulo	printf("tcp:::accept-established - %s\n",
174210753Srpaulo	    connaccept >=1 ? "yes" : "no");
175210753Srpaulo}
176210753SrpauloEODTRACE
177210753Srpaulo
178210753Srpaulostatus=$?
179210753Srpaulo
180210753Srpaulocd /
181211545Srpaulo/bin/rm -rf $DIR
182210753Srpaulo
183210753Srpauloexit $status
184