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 remote 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 send and receive
32210753Srpaulo# 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 remote 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# 4 x ip:::send (2 during the TCP handshake, the message, then a FIN)
47210753Srpaulo# 4 x tcp:::send (2 during the TCP handshake, the messages, then a FIN)
48210753Srpaulo# 3 x ip:::receive (1 during the TCP handshake, the response, then the FIN ACK)
49210753Srpaulo# 3 x tcp:::receive (1 during the TCP handshake, the response, then the FIN ACK)
50210753Srpaulo#
51210753Srpaulo# For this test to work, we are assuming that the TCP handshake and
52210753Srpaulo# TCP close will enter the IP code path and not use tcp fusion.
53210753Srpaulo#
54210753Srpaulo
55210753Srpauloif (( $# != 1 )); then
56210753Srpaulo	print -u2 "expected one argument: <dtrace-path>"
57210753Srpaulo	exit 2
58210753Srpaulofi
59210753Srpaulo
60210753Srpaulodtrace=$1
61210753Srpaulogetaddr=./get.ipv4remote.pl
62210753Srpaulotcpport=22
63210753SrpauloDIR=/var/tmp/dtest.$$
64210753Srpaulo
65210753Srpauloif [[ ! -x $getaddr ]]; then
66210753Srpaulo	print -u2 "could not find or execute sub program: $getaddr"
67210753Srpaulo	exit 3
68210753Srpaulofi
69210753Srpaulo$getaddr $tcpport | read source dest
70210753Srpauloif (( $? != 0 )); then
71210753Srpaulo	exit 4
72210753Srpaulofi
73210753Srpaulo
74210753Srpaulomkdir $DIR
75210753Srpaulocd $DIR
76210753Srpaulo
77210753Srpaulocat > test.pl <<-EOPERL
78210753Srpaulo	use IO::Socket;
79210753Srpaulo	my \$s = IO::Socket::INET->new(
80210753Srpaulo	    Proto => "tcp",
81210753Srpaulo	    PeerAddr => "$dest",
82210753Srpaulo	    PeerPort => $tcpport,
83210753Srpaulo	    Timeout => 3);
84210753Srpaulo	die "Could not connect to host $dest port $tcpport" unless \$s;
85210753Srpaulo	print \$s "testing state machine transitions";
86210753Srpaulo	close \$s;
87210753SrpauloEOPERL
88210753Srpaulo
89210753Srpaulo$dtrace -c '/usr/bin/perl test.pl' -qs /dev/stdin <<EODTRACE
90210753SrpauloBEGIN
91210753Srpaulo{
92210753Srpaulo	ipsend = tcpsend = ipreceive = tcpreceive = 0;
93210753Srpaulo	connreq = connest = 0;
94210753Srpaulo}
95210753Srpaulo
96210753Srpauloip:::send
97210753Srpaulo/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
98210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
99210753Srpaulo{
100210753Srpaulo	ipsend++;
101210753Srpaulo}
102210753Srpaulo
103210753Srpaulotcp:::send
104210753Srpaulo/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
105210753Srpaulo    args[4]->tcp_dport == $tcpport/
106210753Srpaulo{
107210753Srpaulo	tcpsend++;
108210753Srpaulo}
109210753Srpaulo
110210753Srpauloip:::receive
111210753Srpaulo/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
112210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
113210753Srpaulo{
114210753Srpaulo	ipreceive++;
115210753Srpaulo}
116210753Srpaulo
117210753Srpaulotcp:::receive
118210753Srpaulo/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
119210753Srpaulo    args[4]->tcp_sport == $tcpport/
120210753Srpaulo{
121210753Srpaulo	tcpreceive++;
122210753Srpaulo}
123210753Srpaulo
124210753Srpaulotcp:::state-change
125210753Srpaulo{
126210753Srpaulo	state_event[args[3]->tcps_state]++;
127210753Srpaulo}
128210753Srpaulo
129210753Srpaulotcp:::connect-request
130210753Srpaulo/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
131210753Srpaulo args[4]->tcp_dport == $tcpport/
132210753Srpaulo{
133210753Srpaulo	connreq++;
134210753Srpaulo}
135210753Srpaulo
136210753Srpaulotcp:::connect-established
137210753Srpaulo/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
138210753Srpaulo args[4]->tcp_sport == $tcpport/
139210753Srpaulo{
140210753Srpaulo	connest++;
141210753Srpaulo}
142210753Srpaulo
143210753SrpauloEND
144210753Srpaulo{
145210753Srpaulo	printf("Minimum TCP events seen\n\n");
146210753Srpaulo	printf("ip:::send - %s\n", ipsend >= 4 ? "yes" : "no");
147210753Srpaulo	printf("ip:::receive - %s\n", ipreceive >= 3 ? "yes" : "no");
148210753Srpaulo	printf("tcp:::send - %s\n", tcpsend >= 4 ? "yes" : "no");
149210753Srpaulo	printf("tcp:::receive - %s\n", tcpreceive >= 3 ? "yes" : "no");
150210753Srpaulo	printf("tcp:::state-change to syn-sent - %s\n",
151210753Srpaulo	    state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
152210753Srpaulo	printf("tcp:::state-change to established - %s\n",
153210753Srpaulo	    state_event[TCP_STATE_ESTABLISHED] >= 1 ? "yes" : "no");
154210753Srpaulo	printf("tcp:::state-change to fin-wait-1 - %s\n",
155210753Srpaulo	    state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
156210753Srpaulo	printf("tcp:::state-change to fin-wait-2 - %s\n",
157210753Srpaulo	    state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
158210753Srpaulo	printf("tcp:::state-change to time-wait - %s\n",
159210753Srpaulo	    state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
160210753Srpaulo	printf("tcp:::connect-request - %s\n",
161210753Srpaulo	    connreq >=1 ? "yes" : "no");
162210753Srpaulo	printf("tcp:::connect-established - %s\n",
163210753Srpaulo	    connest >=1 ? "yes" : "no");
164210753Srpaulo}
165210753SrpauloEODTRACE
166210753Srpaulo
167210753Srpaulostatus=$?
168210753Srpaulo
169210753Srpaulocd /
170211545Srpaulo/bin/rm -rf $DIR
171210753Srpaulo
172210753Srpauloexit $status
173