default_gateway.go 4.3 KB

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