tst.ipv4localtcp.ksh revision 286171
1193323Sed#!/usr/bin/ksh
2193323Sed#
3193323Sed# CDDL HEADER START
4193323Sed#
5193323Sed# The contents of this file are subject to the terms of the
6193323Sed# Common Development and Distribution License (the "License").
7193323Sed# You may not use this file except in compliance with the License.
8193323Sed#
9193323Sed# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10193323Sed# or http://www.opensolaris.org/os/licensing.
11193323Sed# See the License for the specific language governing permissions
12193323Sed# and limitations under the License.
13193323Sed#
14280031Sdim# When distributing Covered Code, include this CDDL HEADER in each
15280031Sdim# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16193323Sed# If applicable, add the following below this CDDL HEADER, with the
17276479Sdim# fields enclosed by brackets "[]" replaced with your own identifying
18276479Sdim# information: Portions Copyright [yyyy] [name of copyright owner]
19288943Sdim#
20276479Sdim# CDDL HEADER END
21280031Sdim#
22202878Srdivacky
23288943Sdim#
24280031Sdim# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25276479Sdim#
26206274Srdivacky
27193323Sed#
28280031Sdim# Test {ip,tcp}:::{send,receive} of IPv4 TCP to local host.
29249423Sdim#
30276479Sdim# This may fail due to:
31276479Sdim#
32276479Sdim# 1. A change to the ip stack breaking expected probe behavior,
33280031Sdim#    which is the reason we are testing.
34249423Sdim# 2. The lo0 interface missing or not up.
35206274Srdivacky# 3. The local ssh service is not online.
36296417Sdim# 4. An unlikely race causes the unlocked global send/receive
37276479Sdim#    variables to be corrupted.
38276479Sdim#
39193323Sed# This test performs a TCP connection and checks that at least the
40193323Sed# following packet counts were traced:
41276479Sdim#
42276479Sdim# 3 x ip:::send (2 during the TCP handshake, then a FIN)
43234353Sdim# 3 x tcp:::send (2 during the TCP handshake, then a FIN)
44234353Sdim# 2 x ip:::receive (1 during the TCP handshake, then the FIN ACK)
45288943Sdim# 2 x tcp:::receive (1 during the TCP handshake, then the FIN ACK)
46276479Sdim
47276479Sdim# The actual count tested is 5 each way, since we are tracing both
48276479Sdim# source and destination events.
49276479Sdim#
50193323Sed# For this test to work, we are assuming that the TCP handshake and
51193323Sed# TCP close will enter the IP code path and not use tcp fusion.
52193323Sed#
53288943Sdim
54288943Sdimif (( $# != 1 )); then
55288943Sdim	print -u2 "expected one argument: <dtrace-path>"
56288943Sdim	exit 2
57288943Sdimfi
58288943Sdim
59288943Sdimdtrace=$1
60288943Sdimlocal=127.0.0.1
61288943SdimDIR=/var/tmp/dtest.$$
62288943Sdim
63288943Sdimtcpport=1024
64221345Sdimbound=5000
65288943Sdimwhile [ $tcpport -lt $bound ]; do
66288943Sdim	nc -z $local $tcpport >/dev/null || break
67288943Sdim	tcpport=$(($tcpport + 1))
68288943Sdimdone
69288943Sdimif [ $tcpport -eq $bound ]; then
70288943Sdim	echo "couldn't find an available TCP port"
71288943Sdim	exit 1
72261991Sdimfi
73276479Sdim
74221345Sdimmkdir $DIR
75288943Sdimcd $DIR
76288943Sdim
77288943Sdim# nc will exit when the connection is closed.
78288943Sdimnc -l $local $tcpport &
79288943Sdim
80288943Sdimcat > test.pl <<-EOPERL
81288943Sdim	use IO::Socket;
82288943Sdim	my \$s = IO::Socket::INET->new(
83288943Sdim	    Proto => "tcp",
84288943Sdim	    PeerAddr => "$local",
85288943Sdim	    PeerPort => $tcpport,
86288943Sdim	    Timeout => 3);
87288943Sdim	die "Could not connect to host $local port $tcpport" unless \$s;
88288943Sdim	close \$s;
89288943Sdim	sleep(2);
90288943SdimEOPERL
91288943Sdim
92288943Sdim$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
93280031SdimBEGIN
94221345Sdim{
95288943Sdim	ipsend = tcpsend = ipreceive = tcpreceive = 0;
96288943Sdim}
97288943Sdim
98288943Sdimip:::send
99288943Sdim/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
100276479Sdim    args[4]->ipv4_protocol == IPPROTO_TCP/
101288943Sdim{
102288943Sdim	ipsend++;
103288943Sdim}
104288943Sdim
105288943Sdimtcp:::send
106288943Sdim/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
107288943Sdim{
108288943Sdim	tcpsend++;
109288943Sdim}
110221345Sdim
111288943Sdimip:::receive
112288943Sdim/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
113296417Sdim    args[4]->ipv4_protocol == IPPROTO_TCP/
114276479Sdim{
115276479Sdim	ipreceive++;
116288943Sdim}
117288943Sdim
118288943Sdimtcp:::receive
119276479Sdim/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
120296417Sdim{
121288943Sdim	tcpreceive++;
122288943Sdim}
123288943Sdim
124288943SdimEND
125288943Sdim{
126288943Sdim	printf("Minimum TCP events seen\n\n");
127288943Sdim	printf("ip:::send - %s\n", ipsend >= 5 ? "yes" : "no");
128288943Sdim	printf("ip:::receive - %s\n", ipreceive >= 5 ? "yes" : "no");
129288943Sdim	printf("tcp:::send - %s\n", tcpsend >= 5 ? "yes" : "no");
130288943Sdim	printf("tcp:::receive - %s\n", tcpreceive >= 5 ? "yes" : "no");
131288943Sdim}
132288943SdimEODTRACE
133288943Sdim
134288943Sdimstatus=$?
135288943Sdim
136288943Sdimcd /
137288943Sdim/bin/rm -rf $DIR
138288943Sdim
139288943Sdimexit $status
140249423Sdim