closewait.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/agnhost/net/common"
  29. )
  30. // leakedConnection is a global variable that should leak the active
  31. // connection assigned here.
  32. //lint:ignore U1000 intentional unused variable
  33. var leakedConnection *net.TCPConn
  34. // CloseWaitServerOptions holds server JSON options.
  35. type CloseWaitServerOptions struct {
  36. // Address to bind for the test
  37. LocalAddr string
  38. // Timeout to wait after sending the FIN.
  39. PostFinTimeoutSeconds int
  40. }
  41. type closeWaitServer struct {
  42. options *CloseWaitServerOptions
  43. }
  44. // NewCloseWaitServer returns a new Runner.
  45. func NewCloseWaitServer() common.Runner {
  46. return &closeWaitServer{}
  47. }
  48. // NewOptions allocates new options structure.
  49. func (server *closeWaitServer) NewOptions() interface{} {
  50. return &CloseWaitServerOptions{}
  51. }
  52. // Run the server-side of the test.
  53. func (server *closeWaitServer) Run(logger *log.Logger, rawOptions interface{}) error {
  54. if options, ok := rawOptions.(*CloseWaitServerOptions); ok {
  55. server.options = options
  56. } else {
  57. return errors.New("invalid type")
  58. }
  59. logger.Printf("Run %v", server.options)
  60. addr, err := net.ResolveTCPAddr("tcp", server.options.LocalAddr)
  61. if err != nil {
  62. return err
  63. }
  64. listener, err := net.ListenTCP("tcp", addr)
  65. if err != nil {
  66. return err
  67. }
  68. defer listener.Close()
  69. logger.Printf("Server listening on %v", addr)
  70. conn, err := listener.AcceptTCP()
  71. if err != nil {
  72. return err
  73. }
  74. defer conn.Close()
  75. logger.Printf("Client connected")
  76. // Send client half-close FIN so client is now in CLOSE_WAIT. We keep
  77. // the client -> server pipe open to verify whether or not the NAT
  78. // dropped our connection.
  79. if err := conn.CloseWrite(); err != nil {
  80. return err
  81. }
  82. logger.Printf("Server sent FIN, waiting %v seconds",
  83. server.options.PostFinTimeoutSeconds)
  84. <-time.After(time.Duration(server.options.PostFinTimeoutSeconds) * time.Second)
  85. logger.Printf("Done")
  86. return nil
  87. }
  88. // CloseWaitClientOptions holds client JSON options.
  89. type CloseWaitClientOptions struct {
  90. // RemoteAddr of the server to connect to.
  91. RemoteAddr string
  92. // TimeoutSeconds on I/O with the server.
  93. TimeoutSeconds int
  94. // Half-close timeout (to give the test time to check the status of the
  95. // conntrack table entry.
  96. PostFinTimeoutSeconds int
  97. // Leak connection (assign to global variable so connection persists
  98. // as long as the process remains.
  99. LeakConnection bool
  100. }
  101. type closeWaitClient struct {
  102. options *CloseWaitClientOptions
  103. }
  104. // NewCloseWaitClient creates a new runner
  105. func NewCloseWaitClient() common.Runner {
  106. return &closeWaitClient{}
  107. }
  108. // NewOptions allocates new options structure.
  109. func (client *closeWaitClient) NewOptions() interface{} {
  110. return &CloseWaitClientOptions{}
  111. }
  112. // Run the client.m
  113. func (client *closeWaitClient) Run(logger *log.Logger, rawOptions interface{}) error {
  114. if options, ok := rawOptions.(*CloseWaitClientOptions); ok {
  115. client.options = options
  116. } else {
  117. return errors.New("invalid type")
  118. }
  119. logger.Printf("Run %v", client.options)
  120. addr, err := net.ResolveTCPAddr("tcp", client.options.RemoteAddr)
  121. if err != nil {
  122. return err
  123. }
  124. conn, err := net.DialTCP("tcp", nil, addr)
  125. if err != nil {
  126. return err
  127. }
  128. if !client.options.LeakConnection {
  129. defer conn.Close()
  130. }
  131. logger.Printf("Connected to server")
  132. if client.options.TimeoutSeconds > 0 {
  133. delay := time.Duration(client.options.TimeoutSeconds) * time.Second
  134. conn.SetReadDeadline(time.Now().Add(delay))
  135. }
  136. buf := make([]byte, 1, 1)
  137. size, err := conn.Read(buf)
  138. if err != nil && err != io.EOF {
  139. return err
  140. }
  141. if size != 0 {
  142. return errors.New("Got data but expected EOF")
  143. }
  144. logger.Printf("Server has half-closed the connection, waiting %v seconds",
  145. client.options.PostFinTimeoutSeconds)
  146. if client.options.LeakConnection {
  147. logger.Printf("Leaking client connection (assigning to global variable)")
  148. leakedConnection = conn
  149. }
  150. <-time.After(
  151. time.Duration(client.options.PostFinTimeoutSeconds) * time.Second)
  152. logger.Printf("Done")
  153. return nil
  154. }