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
61210753Srpaulotcpport=22
62210753SrpauloDIR=/var/tmp/dtest.$$
63210753Srpaulo
64210753Srpaulomkdir $DIR
65210753Srpaulocd $DIR
66210753Srpaulo
67210753Srpaulocat > test.pl <<-EOPERL
68210753Srpaulo	use IO::Socket;
69210753Srpaulo	my \$s = IO::Socket::INET->new(
70210753Srpaulo	    Proto => "tcp",
71210753Srpaulo	    PeerAddr => "$local",
72210753Srpaulo	    PeerPort => $tcpport,
73210753Srpaulo	    Timeout => 3);
74210753Srpaulo	die "Could not connect to host $local port $tcpport" unless \$s;
75210753Srpaulo	close \$s;
76210753SrpauloEOPERL
77210753Srpaulo
78210753Srpaulo$dtrace -c '/usr/bin/perl test.pl' -qs /dev/stdin <<EODTRACE
79210753SrpauloBEGIN
80210753Srpaulo{
81210753Srpaulo	ipsend = tcpsend = ipreceive = tcpreceive = 0;
82210753Srpaulo}
83210753Srpaulo
84210753Srpauloip:::send
85210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
86210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
87210753Srpaulo{
88210753Srpaulo	ipsend++;
89210753Srpaulo}
90210753Srpaulo
91210753Srpaulotcp:::send
92210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
93210753Srpaulo{
94210753Srpaulo	tcpsend++;
95210753Srpaulo}
96210753Srpaulo
97210753Srpauloip:::receive
98210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
99210753Srpaulo    args[4]->ipv4_protocol == IPPROTO_TCP/
100210753Srpaulo{
101210753Srpaulo	ipreceive++;
102210753Srpaulo}
103210753Srpaulo
104210753Srpaulotcp:::receive
105210753Srpaulo/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
106210753Srpaulo{
107210753Srpaulo	tcpreceive++;
108210753Srpaulo}
109210753Srpaulo
110210753SrpauloEND
111210753Srpaulo{
112210753Srpaulo	printf("Minimum TCP events seen\n\n");
113210753Srpaulo	printf("ip:::send - %s\n", ipsend >= 5 ? "yes" : "no");
114210753Srpaulo	printf("ip:::receive - %s\n", ipreceive >= 5 ? "yes" : "no");
115210753Srpaulo	printf("tcp:::send - %s\n", tcpsend >= 5 ? "yes" : "no");
116210753Srpaulo	printf("tcp:::receive - %s\n", tcpreceive >= 5 ? "yes" : "no");
117210753Srpaulo}
118210753SrpauloEODTRACE
119210753Srpaulo
120210753Srpaulostatus=$?
121210753Srpaulo
122210753Srpaulocd /
123211545Srpaulo/bin/rm -rf $DIR
124210753Srpaulo
125210753Srpauloexit $status
126