closewait.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package nat
  14. /*
  15. client/server for testing CLOSE_WAIT timeout condition in iptables NAT.
  16. client server
  17. | |
  18. |<--tcp handshake-->|
  19. |<-------fin--------| half-close from server
  20. | | client is in CLOSE_WAIT
  21. */
  22. import (
  23. "errors"
  24. "io"
  25. "log"
  26. "net"
  27. "time"
  28. "k8s.io/kubernetes/test/images/net/common"
  29. )
  30. // leakedConnection is a global variable that should leak the active
  31. // connection assigned here.
  32. var leakedConnection *net.TCPConn
  33. // CloseWaitServerOptions holds server JSON options.
  34. type CloseWaitServerOptions struct {
  35. // Address to bind for the test
  36. LocalAddr string
  37. // Timeout to wait after sending the FIN.
  38. PostFinTimeoutSeconds int
  39. }
  40. type closeWaitServer struct {
  41. options *CloseWaitServerOptions
  42. }
  43. // NewCloseWaitServer returns a new Runner.
  44. func NewCloseWaitServer() common.Runner {
  45. return &closeWaitServer{}
  46. }
  47. // NewOptions allocates new options structure.
  48. func (server *closeWaitServer) NewOptions() interface{} {
  49. return &CloseWaitServerOptions{}
  50. }
  51. // Run the server-side of the test.
  52. func (server *closeWaitServer) Run(logger *log.Logger, rawOptions interface{}) error {
  53. if options, ok := rawOptions.(*CloseWaitServerOptions); ok {
  54. server.options = options
  55. } else {
  56. return errors.New("invalid type")
  57. }
  58. logger.Printf("Run %v", server.options)
  59. addr, err := net.ResolveTCPAddr("tcp", server.options.LocalAddr)
  60. if err != nil {
  61. return err
  62. }
  63. listener, err := net.ListenTCP("tcp", addr)
  64. if err != nil {
  65. return err
  66. }
  67. defer listener.Close()
  68. logger.Printf("Server listening on %v", addr)
  69. conn, err := listener.AcceptTCP()
  70. if err != nil {
  71. return err
  72. }
  73. defer conn.Close()
  74. logger.Printf("Client connected")
  75. // Send client half-close FIN so client is now in CLOSE_WAIT. We keep
  76. // the client -> server pipe open to verify whether or not the NAT
  77. // dropped our connection.
  78. if err := conn.CloseWrite(); err != nil {
  79. return err
  80. }
  81. logger.Printf("Server sent FIN, waiting %v seconds",
  82. server.options.PostFinTimeoutSeconds)
  83. <-time.After(time.Duration(server.options.PostFinTimeoutSeconds) * time.Second)
  84. logger.Printf("Done")
  85. return nil
  86. }
  87. // CloseWaitClientOptions holds client JSON options.
  88. type CloseWaitClientOptions struct {
  89. // RemoteAddr of the server to connect to.
  90. RemoteAddr string
  91. // TimeoutSeconds on I/O with the server.
  92. TimeoutSeconds int
  93. // Half-close timeout (to give the test time to check the status of the
  94. // conntrack table entry.
  95. PostFinTimeoutSeconds int
  96. // Leak connection (assign to global variable so connection persists
  97. // as long as the process remains.
  98. LeakConnection bool
  99. }
  100. type closeWaitClient struct {
  101. options *CloseWaitClientOptions
  102. }
  103. // NewCloseWaitClient creates a new runner
  104. func NewCloseWaitClient() common.Runner {
  105. return &closeWaitClient{}
  106. }
  107. // NewOptions allocates new options structure.
  108. func (client *closeWaitClient) NewOptions() interface{} {
  109. return &CloseWaitClientOptions{}
  110. }
  111. // Run the client.m
  112. func (client *closeWaitClient) Run(logger *log.Logger, rawOptions interface{}) error {
  113. if options, ok := rawOptions.(*CloseWaitClientOptions); ok {
  114. client.options = options
  115. } else {
  116. return errors.New("invalid type")
  117. }
  118. logger.Printf("Run %v", client.options)
  119. addr, err := net.ResolveTCPAddr("tcp", client.options.RemoteAddr)
  120. if err != nil {
  121. return err
  122. }
  123. conn, err := net.DialTCP("tcp", nil, addr)
  124. if err != nil {
  125. return err
  126. }
  127. if !client.options.LeakConnection {
  128. defer conn.Close()
  129. }
  130. logger.Printf("Connected to server")
  131. if client.options.TimeoutSeconds > 0 {
  132. delay := time.Duration(client.options.TimeoutSeconds) * time.Second
  133. conn.SetReadDeadline(time.Now().Add(delay))
  134. }
  135. buf := make([]byte, 1, 1)
  136. size, err := conn.Read(buf)
  137. if err != nil && err != io.EOF {
  138. return err
  139. }
  140. if size != 0 {
  141. return errors.New("Got data but expected EOF")
  142. }
  143. logger.Printf("Server has half-closed the connection, waiting %v seconds",
  144. client.options.PostFinTimeoutSeconds)
  145. if client.options.LeakConnection {
  146. logger.Printf("Leaking client connection (assigning to global variable)")
  147. leakedConnection = conn
  148. }
  149. <-time.After(
  150. time.Duration(client.options.PostFinTimeoutSeconds) * time.Second)
  151. logger.Printf("Done")
  152. return nil
  153. }