default_gateway.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package libnetwork
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/Sirupsen/logrus"
  6. "github.com/docker/libnetwork/netlabel"
  7. "github.com/docker/libnetwork/types"
  8. )
  9. const (
  10. gwEPlen = 12
  11. )
  12. var procGwNetwork = make(chan (bool), 1)
  13. /*
  14. libnetwork creates a bridge network "docker_gw_bridge" for providing
  15. default gateway for the containers if none of the container's endpoints
  16. have GW set by the driver. ICC is set to false for the GW_bridge network.
  17. If a driver can't provide external connectivity it can choose to not set
  18. the GW IP for the endpoint.
  19. endpoint on the GW_bridge network is managed dynamically by libnetwork.
  20. ie:
  21. - its created when an endpoint without GW joins the container
  22. - its deleted when an endpoint with GW joins the container
  23. */
  24. func (sb *sandbox) setupDefaultGW() error {
  25. // check if the container already has a GW endpoint
  26. if ep := sb.getEndpointInGWNetwork(); ep != nil {
  27. return nil
  28. }
  29. c := sb.controller
  30. // Look for default gw network. In case of error (includes not found),
  31. // retry and create it if needed in a serialized execution.
  32. n, err := c.NetworkByName(libnGWNetwork)
  33. if err != nil {
  34. if n, err = c.defaultGwNetwork(); err != nil {
  35. return err
  36. }
  37. }
  38. createOptions := []EndpointOption{CreateOptionAnonymous()}
  39. eplen := gwEPlen
  40. if len(sb.containerID) < gwEPlen {
  41. eplen = len(sb.containerID)
  42. }
  43. sbLabels := sb.Labels()
  44. if sbLabels[netlabel.PortMap] != nil {
  45. createOptions = append(createOptions, CreateOptionPortMapping(sbLabels[netlabel.PortMap].([]types.PortBinding)))
  46. }
  47. if sbLabels[netlabel.ExposedPorts] != nil {
  48. createOptions = append(createOptions, CreateOptionExposedPorts(sbLabels[netlabel.ExposedPorts].([]types.TransportPort)))
  49. }
  50. epOption := getPlatformOption()
  51. if epOption != nil {
  52. createOptions = append(createOptions, epOption)
  53. }
  54. newEp, err := n.CreateEndpoint("gateway_"+sb.containerID[0:eplen], createOptions...)
  55. if err != nil {
  56. return fmt.Errorf("container %s: endpoint create on GW Network failed: %v", sb.containerID, err)
  57. }
  58. defer func() {
  59. if err != nil {
  60. if err2 := newEp.Delete(true); err2 != nil {
  61. logrus.Warnf("Failed to remove gw endpoint for container %s after failing to join the gateway network: %v",
  62. sb.containerID, err2)
  63. }
  64. }
  65. }()
  66. epLocal := newEp.(*endpoint)
  67. if err = epLocal.sbJoin(sb); err != nil {
  68. return fmt.Errorf("container %s: endpoint join on GW Network failed: %v", sb.containerID, err)
  69. }
  70. return nil
  71. }
  72. // If present, detach and remove the endpoint connecting the sandbox to the default gw network.
  73. func (sb *sandbox) clearDefaultGW() error {
  74. var ep *endpoint
  75. if ep = sb.getEndpointInGWNetwork(); ep == nil {
  76. return nil
  77. }
  78. if err := ep.sbLeave(sb, false); err != nil {
  79. return fmt.Errorf("container %s: endpoint leaving GW Network failed: %v", sb.containerID, err)
  80. }
  81. if err := ep.Delete(false); err != nil {
  82. return fmt.Errorf("container %s: deleting endpoint on GW Network failed: %v", sb.containerID, err)
  83. }
  84. return nil
  85. }
  86. // Evaluate whether the sandbox requires a default gateway based
  87. // on the endpoints to which it is connected. It does not account
  88. // for the default gateway network endpoint.
  89. func (sb *sandbox) needDefaultGW() bool {
  90. var needGW bool
  91. for _, ep := range sb.getConnectedEndpoints() {
  92. if ep.endpointInGWNetwork() {
  93. continue
  94. }
  95. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  96. continue
  97. }
  98. if ep.getNetwork().Internal() {
  99. continue
  100. }
  101. // During stale sandbox cleanup, joinInfo may be nil
  102. if ep.joinInfo != nil && ep.joinInfo.disableGatewayService {
  103. continue
  104. }
  105. // TODO v6 needs to be handled.
  106. if len(ep.Gateway()) > 0 {
  107. return false
  108. }
  109. for _, r := range ep.StaticRoutes() {
  110. if r.Destination != nil && r.Destination.String() == "0.0.0.0/0" {
  111. return false
  112. }
  113. }
  114. needGW = true
  115. }
  116. return needGW
  117. }
  118. func (sb *sandbox) getEndpointInGWNetwork() *endpoint {
  119. for _, ep := range sb.getConnectedEndpoints() {
  120. if ep.getNetwork().name == libnGWNetwork && strings.HasPrefix(ep.Name(), "gateway_") {
  121. return ep
  122. }
  123. }
  124. return nil
  125. }
  126. func (ep *endpoint) endpointInGWNetwork() bool {
  127. if ep.getNetwork().name == libnGWNetwork && strings.HasPrefix(ep.Name(), "gateway_") {
  128. return true
  129. }
  130. return false
  131. }
  132. func (sb *sandbox) getEPwithoutGateway() *endpoint {
  133. for _, ep := range sb.getConnectedEndpoints() {
  134. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  135. continue
  136. }
  137. if len(ep.Gateway()) == 0 {
  138. return ep
  139. }
  140. }
  141. return nil
  142. }
  143. // Looks for the default gw network and creates it if not there.
  144. // Parallel executions are serialized.
  145. func (c *controller) defaultGwNetwork() (Network, error) {
  146. procGwNetwork <- true
  147. defer func() { <-procGwNetwork }()
  148. n, err := c.NetworkByName(libnGWNetwork)
  149. if err != nil {
  150. if _, ok := err.(types.NotFoundError); ok {
  151. n, err = c.createGWNetwork()
  152. }
  153. }
  154. return n, err
  155. }
  156. // Returns the endpoint which is providing external connectivity to the sandbox
  157. func (sb *sandbox) getGatewayEndpoint() *endpoint {
  158. for _, ep := range sb.getConnectedEndpoints() {
  159. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  160. continue
  161. }
  162. if len(ep.Gateway()) != 0 {
  163. return ep
  164. }
  165. }
  166. return nil
  167. }