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,sctp}:::{send,receive} of IPv4 SCTP 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 SCTP association and checks that at least the
39# following packet counts were traced:
40#
41# 7 x ip:::send (4 during the setup, 3 during the teardown)
42# 7 x sctp:::send (4 during the setup, 3 during the teardown)
43# 7 x ip:::receive (4 during the setup, 3 during the teardown)
44# 7 x sctp:::receive (4 during the setup, 3 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
59sctpport=1024
60bound=5000
61while [ $sctpport -lt $bound ]; do
62	ncat --sctp -z $local $sctpport > /dev/null || break
63	sctpport=$(($sctpport + 1))
64done
65if [ $sctpport -eq $bound ]; then
66	echo "couldn't find an available SCTP port"
67	exit 1
68fi
69
70mkdir $DIR
71cd $DIR
72
73# ncat will exit when the association is closed.
74ncat --sctp --listen $local $sctpport &
75
76cat > test.pl <<-EOPERL
77	use IO::Socket;
78	my \$s = IO::Socket::INET->new(
79	    Type => SOCK_STREAM,
80	    Proto => "sctp",
81	    LocalAddr => "$local",
82	    PeerAddr => "$local",
83	    PeerPort => $sctpport,
84	    Timeout => 3);
85	die "Could not connect to host $local port $sctpport \$@" unless \$s;
86	close \$s;
87	sleep(2);
88EOPERL
89
90$dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
91BEGIN
92{
93	ipsend = sctpsend = ipreceive = sctpreceive = 0;
94}
95
96ip:::send
97/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
98    args[4]->ipv4_protocol == IPPROTO_SCTP/
99{
100	ipsend++;
101}
102
103sctp:::send
104/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
105{
106	sctpsend++;
107}
108
109ip:::receive
110/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
111    args[4]->ipv4_protocol == IPPROTO_SCTP/
112{
113	ipreceive++;
114}
115
116sctp:::receive
117/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local"/
118{
119	sctpreceive++;
120}
121
122END
123{
124	printf("Minimum SCTP events seen\n\n");
125	printf("ip:::send (%d) - %s\n", ipsend, ipsend >= 7 ? "yes" : "no");
126	printf("ip:::receive (%d) - %s\n", ipreceive, ipreceive >= 7 ? "yes" : "no");
127	printf("sctp:::send (%d) - %s\n", sctpsend, sctpsend >= 7 ? "yes" : "no");
128	printf("sctp:::receive (%d) - %s\n", sctpreceive, sctpreceive >= 7 ? "yes" : "no");
129}
130EODTRACE
131
132status=$?
133
134cd /
135/bin/rm -rf $DIR
136
137exit $status
138