1#!/usr/bin/env ksh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25#
26
27#
28# Test {ip,tcp}:::{send,receive} of IPv4 TCP to local host.
29#
30# This may fail due to:
31#
32# 1. A change to the ip stack breaking expected probe behavior,
33#    which is the reason we are testing.
34# 2. The lo0 interface missing or not up.
35# 3. An unlikely race causes the unlocked global send/receive
36#    variables to be corrupted.
37#
38# This test performs a TCP connection and checks that at least the
39# following packet counts were traced:
40#
41# 7 x ip:::send (3 during the setup, 4 during the teardown)
42# 7 x tcp:::send (3 during the setup, 4 during the teardown)
43# 7 x ip:::receive (3 during the setup, 4 during the teardown)
44# 7 x tcp:::receive (3 during the setup, 4 during the teardown)
45
46# The actual count tested is 7 each way, since we are tracing both
47# source and destination events.
48#
49
50if (( $# != 1 )); then
51	print -u2 "expected one argument: <dtrace-path>"
52	exit 2
53fi
54
55dtrace=$1
56local=127.0.0.1
57DIR=/var/tmp/dtest.$$
58
59tcpport=1024
60bound=5000
61while [ $tcpport -lt $bound ]; do
62	nc -z $local $tcpport >/dev/null || break
63	tcpport=$(($tcpport + 1))
64done
65if [ $tcpport -eq $bound ]; then
66	echo "couldn't find an available TCP port"
67	exit 1
68fi
69
70mkdir $DIR
71cd $DIR
72
73# nc will exit when the connection is closed.
74nc -l $local $tcpport &
75
76cat > test.pl <<-EOPERL
77	use IO::Socket;
78	my \$s = IO::Socket::INET->new(
79	    Proto => "tcp",
80	    PeerAddr => "$local",
81	    PeerPort => $tcpport,
82	    Timeout => 3);
83	die "Could not connect to host $local port $tcpport" unless \$s;
84	close \$s;
85	sleep(2);
86EOPERL
87
88$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
89BEGIN
90{
91	ipsend = tcpsend = ipreceive = tcpreceive = 0;
92}
93
94ip:::send
95/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
96    args[4]->ipv4_protocol == IPPROTO_TCP/
97{
98	ipsend++;
99}
100
101tcp:::send
102/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
103{
104	tcpsend++;
105}
106
107ip:::receive
108/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
109    args[4]->ipv4_protocol == IPPROTO_TCP/
110{
111	ipreceive++;
112}
113
114tcp:::receive
115/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
116{
117	tcpreceive++;
118}
119
120END
121{
122	printf("Minimum TCP events seen\n\n");
123	printf("ip:::send - %s\n", ipsend >= 7 ? "yes" : "no");
124	printf("ip:::receive - %s\n", ipreceive >= 7 ? "yes" : "no");
125	printf("tcp:::send - %s\n", tcpsend >= 7 ? "yes" : "no");
126	printf("tcp:::receive - %s\n", tcpreceive >= 7 ? "yes" : "no");
127}
128EODTRACE
129
130status=$?
131
132cd /
133/bin/rm -rf $DIR
134
135exit $status
136