19cd928feSAlan Maguire#!/usr/bin/ksh
29cd928feSAlan Maguire#
39cd928feSAlan Maguire# CDDL HEADER START
49cd928feSAlan Maguire#
59cd928feSAlan Maguire# The contents of this file are subject to the terms of the
69cd928feSAlan Maguire# Common Development and Distribution License (the "License").
79cd928feSAlan Maguire# You may not use this file except in compliance with the License.
89cd928feSAlan Maguire#
99cd928feSAlan Maguire# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
109cd928feSAlan Maguire# or http://www.opensolaris.org/os/licensing.
119cd928feSAlan Maguire# See the License for the specific language governing permissions
129cd928feSAlan Maguire# and limitations under the License.
139cd928feSAlan Maguire#
149cd928feSAlan Maguire# When distributing Covered Code, include this CDDL HEADER in each
159cd928feSAlan Maguire# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
169cd928feSAlan Maguire# If applicable, add the following below this CDDL HEADER, with the
179cd928feSAlan Maguire# fields enclosed by brackets "[]" replaced with your own identifying
189cd928feSAlan Maguire# information: Portions Copyright [yyyy] [name of copyright owner]
199cd928feSAlan Maguire#
209cd928feSAlan Maguire# CDDL HEADER END
219cd928feSAlan Maguire#
229cd928feSAlan Maguire
239cd928feSAlan Maguire#
249cd928feSAlan Maguire# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
259cd928feSAlan Maguire#
269cd928feSAlan Maguire
279cd928feSAlan Maguire#
289cd928feSAlan Maguire# Test tcp:::state-change and tcp:::{send,receive} by connecting to
299cd928feSAlan Maguire# the remote ssh service and sending a test message. This should result
309cd928feSAlan Maguire# in a "Protocol mismatch" response and a close of the connection.
319cd928feSAlan Maguire# A number of state transition events along with tcp send and receive
329cd928feSAlan Maguire# events for the message should result.
339cd928feSAlan Maguire#
349cd928feSAlan Maguire# This may fail due to:
359cd928feSAlan Maguire#
369cd928feSAlan Maguire# 1. A change to the ip stack breaking expected probe behavior,
379cd928feSAlan Maguire#    which is the reason we are testing.
389cd928feSAlan Maguire# 2. The lo0 interface missing or not up.
399cd928feSAlan Maguire# 3. The remote ssh service is not online.
409cd928feSAlan Maguire# 4. An unlikely race causes the unlocked global send/receive
419cd928feSAlan Maguire#    variables to be corrupted.
429cd928feSAlan Maguire#
439cd928feSAlan Maguire# This test performs a TCP connection to the ssh service (port 22) and
449cd928feSAlan Maguire# checks that at least the following packet counts were traced:
459cd928feSAlan Maguire#
469cd928feSAlan Maguire# 4 x ip:::send (2 during the TCP handshake, the message, then a FIN)
479cd928feSAlan Maguire# 4 x tcp:::send (2 during the TCP handshake, the messages, then a FIN)
489cd928feSAlan Maguire# 3 x ip:::receive (1 during the TCP handshake, the response, then the FIN ACK)
499cd928feSAlan Maguire# 3 x tcp:::receive (1 during the TCP handshake, the response, then the FIN ACK)
509cd928feSAlan Maguire#
519cd928feSAlan Maguire# For this test to work, we are assuming that the TCP handshake and
529cd928feSAlan Maguire# TCP close will enter the IP code path and not use tcp fusion.
539cd928feSAlan Maguire#
549cd928feSAlan Maguire
559cd928feSAlan Maguireif (( $# != 1 )); then
569cd928feSAlan Maguire	print -u2 "expected one argument: <dtrace-path>"
579cd928feSAlan Maguire	exit 2
589cd928feSAlan Maguirefi
599cd928feSAlan Maguire
609cd928feSAlan Maguiredtrace=$1
619cd928feSAlan Maguiregetaddr=./get.ipv4remote.pl
629cd928feSAlan Maguiretcpport=22
639cd928feSAlan MaguireDIR=/var/tmp/dtest.$$
649cd928feSAlan Maguire
659cd928feSAlan Maguireif [[ ! -x $getaddr ]]; then
669cd928feSAlan Maguire	print -u2 "could not find or execute sub program: $getaddr"
679cd928feSAlan Maguire	exit 3
689cd928feSAlan Maguirefi
699cd928feSAlan Maguire$getaddr $tcpport | read source dest
709cd928feSAlan Maguireif (( $? != 0 )); then
719cd928feSAlan Maguire	exit 4
729cd928feSAlan Maguirefi
739cd928feSAlan Maguire
749cd928feSAlan Maguiremkdir $DIR
759cd928feSAlan Maguirecd $DIR
769cd928feSAlan Maguire
779cd928feSAlan Maguirecat > test.pl <<-EOPERL
789cd928feSAlan Maguire	use IO::Socket;
799cd928feSAlan Maguire	my \$s = IO::Socket::INET->new(
809cd928feSAlan Maguire	    Proto => "tcp",
819cd928feSAlan Maguire	    PeerAddr => "$dest",
829cd928feSAlan Maguire	    PeerPort => $tcpport,
839cd928feSAlan Maguire	    Timeout => 3);
849cd928feSAlan Maguire	die "Could not connect to host $dest port $tcpport" unless \$s;
859cd928feSAlan Maguire	print \$s "testing state machine transitions";
869cd928feSAlan Maguire	close \$s;
879cd928feSAlan MaguireEOPERL
889cd928feSAlan Maguire
89*c090e5dfSBryan Cantrill$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
909cd928feSAlan MaguireBEGIN
919cd928feSAlan Maguire{
929cd928feSAlan Maguire	ipsend = tcpsend = ipreceive = tcpreceive = 0;
939cd928feSAlan Maguire	connreq = connest = 0;
949cd928feSAlan Maguire}
959cd928feSAlan Maguire
969cd928feSAlan Maguireip:::send
979cd928feSAlan Maguire/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
989cd928feSAlan Maguire    args[4]->ipv4_protocol == IPPROTO_TCP/
999cd928feSAlan Maguire{
1009cd928feSAlan Maguire	ipsend++;
1019cd928feSAlan Maguire}
1029cd928feSAlan Maguire
1039cd928feSAlan Maguiretcp:::send
1049cd928feSAlan Maguire/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
1059cd928feSAlan Maguire    args[4]->tcp_dport == $tcpport/
1069cd928feSAlan Maguire{
1079cd928feSAlan Maguire	tcpsend++;
1089cd928feSAlan Maguire}
1099cd928feSAlan Maguire
1109cd928feSAlan Maguireip:::receive
1119cd928feSAlan Maguire/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
1129cd928feSAlan Maguire    args[4]->ipv4_protocol == IPPROTO_TCP/
1139cd928feSAlan Maguire{
1149cd928feSAlan Maguire	ipreceive++;
1159cd928feSAlan Maguire}
1169cd928feSAlan Maguire
1179cd928feSAlan Maguiretcp:::receive
1189cd928feSAlan Maguire/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
1199cd928feSAlan Maguire    args[4]->tcp_sport == $tcpport/
1209cd928feSAlan Maguire{
1219cd928feSAlan Maguire	tcpreceive++;
1229cd928feSAlan Maguire}
1239cd928feSAlan Maguire
1249cd928feSAlan Maguiretcp:::state-change
1259cd928feSAlan Maguire{
1269cd928feSAlan Maguire	state_event[args[3]->tcps_state]++;
1279cd928feSAlan Maguire}
1289cd928feSAlan Maguire
1299cd928feSAlan Maguiretcp:::connect-request
1309cd928feSAlan Maguire/args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
1319cd928feSAlan Maguire args[4]->tcp_dport == $tcpport/
1329cd928feSAlan Maguire{
1339cd928feSAlan Maguire	connreq++;
1349cd928feSAlan Maguire}
1359cd928feSAlan Maguire
1369cd928feSAlan Maguiretcp:::connect-established
1379cd928feSAlan Maguire/args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
1389cd928feSAlan Maguire args[4]->tcp_sport == $tcpport/
1399cd928feSAlan Maguire{
1409cd928feSAlan Maguire	connest++;
1419cd928feSAlan Maguire}
1429cd928feSAlan Maguire
1439cd928feSAlan MaguireEND
1449cd928feSAlan Maguire{
1459cd928feSAlan Maguire	printf("Minimum TCP events seen\n\n");
1469cd928feSAlan Maguire	printf("ip:::send - %s\n", ipsend >= 4 ? "yes" : "no");
1479cd928feSAlan Maguire	printf("ip:::receive - %s\n", ipreceive >= 3 ? "yes" : "no");
1489cd928feSAlan Maguire	printf("tcp:::send - %s\n", tcpsend >= 4 ? "yes" : "no");
1499cd928feSAlan Maguire	printf("tcp:::receive - %s\n", tcpreceive >= 3 ? "yes" : "no");
1509cd928feSAlan Maguire	printf("tcp:::state-change to syn-sent - %s\n",
1519cd928feSAlan Maguire	    state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
1529cd928feSAlan Maguire	printf("tcp:::state-change to established - %s\n",
1539cd928feSAlan Maguire	    state_event[TCP_STATE_ESTABLISHED] >= 1 ? "yes" : "no");
1549cd928feSAlan Maguire	printf("tcp:::state-change to fin-wait-1 - %s\n",
1559cd928feSAlan Maguire	    state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
1569cd928feSAlan Maguire	printf("tcp:::state-change to fin-wait-2 - %s\n",
1579cd928feSAlan Maguire	    state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
1589cd928feSAlan Maguire	printf("tcp:::state-change to time-wait - %s\n",
1599cd928feSAlan Maguire	    state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
1609cd928feSAlan Maguire	printf("tcp:::connect-request - %s\n",
1619cd928feSAlan Maguire	    connreq >=1 ? "yes" : "no");
1629cd928feSAlan Maguire	printf("tcp:::connect-established - %s\n",
1639cd928feSAlan Maguire	    connest >=1 ? "yes" : "no");
1649cd928feSAlan Maguire}
1659cd928feSAlan MaguireEODTRACE
1669cd928feSAlan Maguire
1679cd928feSAlan Maguirestatus=$?
1689cd928feSAlan Maguire
1699cd928feSAlan Maguirecd /
1709cd928feSAlan Maguire/usr/bin/rm -rf $DIR
1719cd928feSAlan Maguire
1729cd928feSAlan Maguireexit $status