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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25210753Srpaulo#
26210753Srpaulo
27210753Srpaulo#
28210753Srpaulo# Test {ip,tcp}:::{send,receive} of IPv4 TCP to local host.
29210753Srpaulo#
30210753Srpaulo# This may fail due to:
31210753Srpaulo#
32210753Srpaulo# 1. A change to the ip stack breaking expected probe behavior,
33210753Srpaulo#    which is the reason we are testing.
34210753Srpaulo# 2. The lo0 interface missing or not up.
35210753Srpaulo# 3. The local ssh service is not online.
36210753Srpaulo# 4. An unlikely race causes the unlocked global send/receive
37210753Srpaulo#    variables to be corrupted.
38210753Srpaulo#
39210753Srpaulo# This test performs a TCP connection and checks that at least the
40210753Srpaulo# following packet counts were traced:
41210753Srpaulo#
42210753Srpaulo# 3 x ip:::send (2 during the TCP handshake, then a FIN)
43210753Srpaulo# 3 x tcp:::send (2 during the TCP handshake, then a FIN)
44210753Srpaulo# 2 x ip:::receive (1 during the TCP handshake, then the FIN ACK)
45210753Srpaulo# 2 x tcp:::receive (1 during the TCP handshake, then the FIN ACK)
46210753Srpaulo
47210753Srpaulo# The actual count tested is 5 each way, since we are tracing both
48210753Srpaulo# source and destination events.
49210753Srpaulo#
50210753Srpaulo# For this test to work, we are assuming that the TCP handshake and
51210753Srpaulo# TCP close will enter the IP code path and not use tcp fusion.
52210753Srpaulo#
53210753Srpaulo
54210753Srpauloif (( $# != 1 )); then
55210753Srpaulo	print -u2 "expected one argument: <dtrace-path>"
56210753Srpaulo	exit 2
57210753Srpaulofi
58210753Srpaulo
59210753Srpaulodtrace=$1
60210753Srpaulolocal=127.0.0.1
61210753SrpauloDIR=/var/tmp/dtest.$$
62210753Srpaulo
63286171Smarkjtcpport=1024
64286171Smarkjbound=5000
65286171Smarkjwhile [ $tcpport -lt $bound ]; do
66286171Smarkj	nc -z $local $tcpport >/dev/null || break
67286171Smarkj	tcpport=$(($tcpport + 1))
68286171Smarkjdone
69286171Smarkjif [ $tcpport -eq $bound ]; then
70286171Smarkj	echo "couldn't find an available TCP port"
71286171Smarkj	exit 1
72286171Smarkjfi
73286171Smarkj
74210753Srpaulomkdir $DIR
75210753Srpaulocd $DIR
76210753Srpaulo
77286171Smarkj# nc will exit when the connection is closed.
78286171Smarkjnc -l $local $tcpport &
79286171Smarkj
80210753Srpaulocat > test.pl <<-EOPERL
81210753Srpaulo	use IO::Socket;
82210753Srpaulo	my \$s = IO::Socket::INET->new(
83210753Srpaulo	    Proto => "tcp",
84210753Srpaulo	    PeerAddr => "$local",
85210753Srpaulo	    PeerPort => $tcpport,
86210753Srpaulo	    Timeout => 3);
87210753Srpaulo	die "Could not connect to host $local port $tcpport" unless \$s;
88210753Srpaulo	close \$s;
89280773Smarkj	sleep(2);
90210753SrpauloEOPERL
91210753Srpaulo
92286171Smarkj$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
93210753SrpauloBEGIN
94210753Srpaulo{
95210753Srpaulo	ipsend = tcpsend = ipreceive = tcpreceive = 0;
96210753Srpaulo}
97210753Srpaulo
98210753Srpauloip:::send
99210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
100210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
101210753Srpaulo{
102210753Srpaulo	ipsend++;
103210753Srpaulo}
104210753Srpaulo
105210753Srpaulotcp:::send
106210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
107210753Srpaulo{
108210753Srpaulo	tcpsend++;
109210753Srpaulo}
110210753Srpaulo
111210753Srpauloip:::receive
112210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
113210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
114210753Srpaulo{
115210753Srpaulo	ipreceive++;
116210753Srpaulo}
117210753Srpaulo
118210753Srpaulotcp:::receive
119210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
120210753Srpaulo{
121210753Srpaulo	tcpreceive++;
122210753Srpaulo}
123210753Srpaulo
124210753SrpauloEND
125210753Srpaulo{
126210753Srpaulo	printf("Minimum TCP events seen\n\n");
127210753Srpaulo	printf("ip:::send - %s\n", ipsend >= 5 ? "yes" : "no");
128210753Srpaulo	printf("ip:::receive - %s\n", ipreceive >= 5 ? "yes" : "no");
129210753Srpaulo	printf("tcp:::send - %s\n", tcpsend >= 5 ? "yes" : "no");
130210753Srpaulo	printf("tcp:::receive - %s\n", tcpreceive >= 5 ? "yes" : "no");
131210753Srpaulo}
132210753SrpauloEODTRACE
133210753Srpaulo
134210753Srpaulostatus=$?
135210753Srpaulo
136210753Srpaulocd /
137211545Srpaulo/bin/rm -rf $DIR
138210753Srpaulo
139210753Srpauloexit $status
140