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) 2010, Oracle and/or its affiliates. All rights reserved.
25#
26
27#
28# Test sctp:::state-change and sctp:::{send,receive} by connecting to
29# the local discard service.
30# A number of state transition events along with SCTP send and
31# receive events for the message should result.
32#
33# This may fail due to:
34#
35# 1. A change to the ip stack breaking expected probe behavior,
36#    which is the reason we are testing.
37# 2. The lo0 interface missing or not up.
38# 3. An unlikely race causes the unlocked global send/receive
39#    variables to be corrupted.
40#
41# This test performs a SCTP connection and checks that at least the
42# following packet counts were traced:
43#
44# 7 x ip:::send (4 during the setup, 3 during the teardown)
45# 7 x sctp:::send (4 during the setup, 3 during the teardown)
46# 7 x ip:::receive (4 during the setup, 3 during the teardown)
47# 7 x sctp:::receive (4 during the setup, 3 during the teardown)
48#
49# The actual count tested is 7 each way, since we are tracing both
50# source and destination events.
51#
52
53if (( $# != 1 )); then
54	print -u2 "expected one argument: <dtrace-path>"
55	exit 2
56fi
57
58dtrace=$1
59local=127.0.0.1
60DIR=/var/tmp/dtest.$$
61
62sctpport=1024
63bound=5000
64
65mkdir $DIR
66cd $DIR
67
68cat > client.pl <<-EOPERL
69	use IO::Socket;
70	my \$s = IO::Socket::INET->new(
71	    Type => SOCK_STREAM,
72	    Proto => "sctp",
73	    LocalAddr => "$local",
74	    PeerAddr => "$local",
75	    PeerPort => \$ARGV[0],
76	    Timeout => 3);
77	die "Could not connect to host $local port \$ARGV[0] \$@" unless \$s;
78	close \$s;
79	sleep(\$ARGV[1]);
80EOPERL
81
82while [ $sctpport -lt $bound ]; do
83	perl client.pl $sctpport 0 2>&- || break
84	sctpport=$(($sctpport + 1))
85done
86if [ $sctpport -eq $bound ]; then
87	echo "couldn't find an available SCTP port"
88	exit 1
89fi
90
91cat > server.pl <<-EOPERL
92	use IO::Socket;
93	my \$l = IO::Socket::INET->new(
94	    Type => SOCK_STREAM,
95	    Proto => "sctp",
96	    LocalAddr => "$local",
97	    LocalPort => $sctpport,
98	    Listen => 1,
99	    Reuse => 1);
100	die "Could not listen on $local port $sctpport \$@" unless \$l;
101	my \$c = \$l->accept();
102	close \$l;
103	while (<\$c>) {};
104	close \$c;
105EOPERL
106
107perl server.pl &
108
109$dtrace -c "perl client.pl $sctpport 2" -qs /dev/stdin <<EODTRACE
110BEGIN
111{
112	ipsend = sctpsend = ipreceive = sctpreceive = 0;
113}
114
115ip:::send
116/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
117    args[4]->ipv4_protocol == IPPROTO_SCTP/
118{
119	ipsend++;
120}
121
122sctp:::send
123/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
124 (args[4]->sctp_sport == $sctpport || args[4]->sctp_dport == $sctpport)/
125{
126	sctpsend++;
127}
128
129ip:::receive
130/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
131    args[4]->ipv4_protocol == IPPROTO_SCTP/
132{
133	ipreceive++;
134}
135
136sctp:::receive
137/args[2]->ip_saddr == "$local" && args[2]->ip_daddr == "$local" &&
138 (args[4]->sctp_sport == $sctpport || args[4]->sctp_dport == $sctpport)/
139{
140	sctpreceive++;
141}
142
143sctp:::state-change
144{
145	state_event[args[3]->sctps_state]++;
146}
147
148END
149{
150	printf("Minimum SCTP events seen\n\n");
151	printf("ip:::send - %s\n", ipsend >= 7 ? "yes" : "no");
152	printf("ip:::receive - %s\n", ipreceive >= 7 ? "yes" : "no");
153	printf("sctp:::send - %s\n", sctpsend >= 7 ? "yes" : "no");
154	printf("sctp:::receive - %s\n", sctpreceive >= 7 ? "yes" : "no");
155	printf("sctp:::state-change to cookie-wait - %s\n",
156	    state_event[SCTP_STATE_COOKIE_WAIT] >=1 ? "yes" : "no");
157	printf("sctp:::state-change to cookie-echoed - %s\n",
158	    state_event[SCTP_STATE_COOKIE_ECHOED] >=1 ? "yes" : "no");
159	printf("sctp:::state-change to established - %s\n",
160	    state_event[SCTP_STATE_ESTABLISHED] >= 2 ? "yes" : "no");
161	printf("sctp:::state-change to shutdown-sent - %s\n",
162	    state_event[SCTP_STATE_SHUTDOWN_SENT] >= 1 ? "yes" : "no");
163	printf("sctp:::state-change to shutdown-received - %s\n",
164	    state_event[SCTP_STATE_SHUTDOWN_RECEIVED] >= 1 ? "yes" : "no");
165	printf("sctp:::state-change to shutdown-ack-sent - %s\n",
166	    state_event[SCTP_STATE_SHUTDOWN_ACK_SENT] >= 1 ? "yes" : "no");
167}
168EODTRACE
169
170status=$?
171
172cd /
173/bin/rm -rf $DIR
174
175exit $status
176