/usr/src/cmd/dtrace/test/tst/common/ip/tst.remotetcpstate.ksh

https://github.com/buffygb/illumos-gate · Korn Shell · 172 lines · 102 code · 19 blank · 51 comment · 28 complexity · eaaf7f6d008b5ebc3c49600595909be3 MD5 · raw file

  1. #!/usr/bin/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. # Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
  24. #
  25. #
  26. # Test tcp:::state-change and tcp:::{send,receive} by connecting to
  27. # the remote ssh service and sending a test message. This should result
  28. # in a "Protocol mismatch" response and a close of the connection.
  29. # A number of state transition events along with tcp send and receive
  30. # events for the message should result.
  31. #
  32. # This may fail due to:
  33. #
  34. # 1. A change to the ip stack breaking expected probe behavior,
  35. # which is the reason we are testing.
  36. # 2. The lo0 interface missing or not up.
  37. # 3. The remote ssh service is not online.
  38. # 4. An unlikely race causes the unlocked global send/receive
  39. # variables to be corrupted.
  40. #
  41. # This test performs a TCP connection to the ssh service (port 22) and
  42. # checks that at least the following packet counts were traced:
  43. #
  44. # 4 x ip:::send (2 during the TCP handshake, the message, then a FIN)
  45. # 4 x tcp:::send (2 during the TCP handshake, the messages, then a FIN)
  46. # 3 x ip:::receive (1 during the TCP handshake, the response, then the FIN ACK)
  47. # 3 x tcp:::receive (1 during the TCP handshake, the response, then the FIN ACK)
  48. #
  49. # For this test to work, we are assuming that the TCP handshake and
  50. # TCP close will enter the IP code path and not use tcp fusion.
  51. #
  52. if (( $# != 1 )); then
  53. print -u2 "expected one argument: <dtrace-path>"
  54. exit 2
  55. fi
  56. dtrace=$1
  57. getaddr=./get.ipv4remote.pl
  58. tcpport=22
  59. DIR=/var/tmp/dtest.$$
  60. if [[ ! -x $getaddr ]]; then
  61. print -u2 "could not find or execute sub program: $getaddr"
  62. exit 3
  63. fi
  64. $getaddr $tcpport | read source dest
  65. if (( $? != 0 )); then
  66. exit 4
  67. fi
  68. mkdir $DIR
  69. cd $DIR
  70. cat > test.pl <<-EOPERL
  71. use IO::Socket;
  72. my \$s = IO::Socket::INET->new(
  73. Proto => "tcp",
  74. PeerAddr => "$dest",
  75. PeerPort => $tcpport,
  76. Timeout => 3);
  77. die "Could not connect to host $dest port $tcpport" unless \$s;
  78. print \$s "testing state machine transitions";
  79. close \$s;
  80. EOPERL
  81. $dtrace -c 'perl test.pl' -qs /dev/stdin <<EODTRACE
  82. BEGIN
  83. {
  84. ipsend = tcpsend = ipreceive = tcpreceive = 0;
  85. connreq = connest = 0;
  86. }
  87. ip:::send
  88. /args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
  89. args[4]->ipv4_protocol == IPPROTO_TCP/
  90. {
  91. ipsend++;
  92. }
  93. tcp:::send
  94. /args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
  95. args[4]->tcp_dport == $tcpport/
  96. {
  97. tcpsend++;
  98. }
  99. ip:::receive
  100. /args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
  101. args[4]->ipv4_protocol == IPPROTO_TCP/
  102. {
  103. ipreceive++;
  104. }
  105. tcp:::receive
  106. /args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
  107. args[4]->tcp_sport == $tcpport/
  108. {
  109. tcpreceive++;
  110. }
  111. tcp:::state-change
  112. {
  113. state_event[args[3]->tcps_state]++;
  114. }
  115. tcp:::connect-request
  116. /args[2]->ip_saddr == "$source" && args[2]->ip_daddr == "$dest" &&
  117. args[4]->tcp_dport == $tcpport/
  118. {
  119. connreq++;
  120. }
  121. tcp:::connect-established
  122. /args[2]->ip_saddr == "$dest" && args[2]->ip_daddr == "$source" &&
  123. args[4]->tcp_sport == $tcpport/
  124. {
  125. connest++;
  126. }
  127. END
  128. {
  129. printf("Minimum TCP events seen\n\n");
  130. printf("ip:::send - %s\n", ipsend >= 4 ? "yes" : "no");
  131. printf("ip:::receive - %s\n", ipreceive >= 3 ? "yes" : "no");
  132. printf("tcp:::send - %s\n", tcpsend >= 4 ? "yes" : "no");
  133. printf("tcp:::receive - %s\n", tcpreceive >= 3 ? "yes" : "no");
  134. printf("tcp:::state-change to syn-sent - %s\n",
  135. state_event[TCP_STATE_SYN_SENT] >=1 ? "yes" : "no");
  136. printf("tcp:::state-change to established - %s\n",
  137. state_event[TCP_STATE_ESTABLISHED] >= 1 ? "yes" : "no");
  138. printf("tcp:::state-change to fin-wait-1 - %s\n",
  139. state_event[TCP_STATE_FIN_WAIT_1] >= 1 ? "yes" : "no");
  140. printf("tcp:::state-change to fin-wait-2 - %s\n",
  141. state_event[TCP_STATE_FIN_WAIT_2] >= 1 ? "yes" : "no");
  142. printf("tcp:::state-change to time-wait - %s\n",
  143. state_event[TCP_STATE_TIME_WAIT] >= 1 ? "yes" : "no");
  144. printf("tcp:::connect-request - %s\n",
  145. connreq >=1 ? "yes" : "no");
  146. printf("tcp:::connect-established - %s\n",
  147. connest >=1 ? "yes" : "no");
  148. }
  149. EODTRACE
  150. status=$?
  151. cd /
  152. /usr/bin/rm -rf $DIR
  153. exit $status