default_gateway.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 needs to be attached to the default
  66. // gateway network.
  67. func (sb *sandbox) needDefaultGW() bool {
  68. var needGW bool
  69. if sb.inDelete {
  70. return false
  71. }
  72. for _, ep := range sb.getConnectedEndpoints() {
  73. if ep.endpointInGWNetwork() {
  74. continue
  75. }
  76. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  77. continue
  78. }
  79. if ep.getNetwork().Internal() {
  80. continue
  81. }
  82. // During stale sandbox cleanup, joinInfo may be nil
  83. if ep.joinInfo != nil && ep.joinInfo.disableGatewayService {
  84. return false
  85. }
  86. // TODO v6 needs to be handled.
  87. if len(ep.Gateway()) > 0 {
  88. return false
  89. }
  90. for _, r := range ep.StaticRoutes() {
  91. if r.Destination.String() == "0.0.0.0/0" {
  92. return false
  93. }
  94. }
  95. needGW = true
  96. }
  97. return needGW
  98. }
  99. func (sb *sandbox) getEndpointInGWNetwork() *endpoint {
  100. for _, ep := range sb.getConnectedEndpoints() {
  101. if ep.getNetwork().name == libnGWNetwork {
  102. return ep
  103. }
  104. }
  105. return nil
  106. }
  107. func (ep *endpoint) endpointInGWNetwork() bool {
  108. if ep.getNetwork().name == libnGWNetwork {
  109. return true
  110. }
  111. return false
  112. }
  113. func (sb *sandbox) getEPwithoutGateway() *endpoint {
  114. for _, ep := range sb.getConnectedEndpoints() {
  115. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  116. continue
  117. }
  118. if len(ep.Gateway()) == 0 {
  119. return ep
  120. }
  121. }
  122. return nil
  123. }
  124. // Looks for the default gw network and creates it if not there.
  125. // Parallel executions are serialized.
  126. func (c *controller) defaultGwNetwork() (Network, error) {
  127. procGwNetwork <- true
  128. defer func() { <-procGwNetwork }()
  129. n, err := c.NetworkByName(libnGWNetwork)
  130. if err != nil {
  131. if _, ok := err.(types.NotFoundError); ok {
  132. n, err = c.createGWNetwork()
  133. }
  134. }
  135. return n, err
  136. }
  137. // Returns the endpoint which is providing external connectivity to the sandbox
  138. func (sb *sandbox) getGatewayEndpoint() *endpoint {
  139. for _, ep := range sb.getConnectedEndpoints() {
  140. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  141. continue
  142. }
  143. if len(ep.Gateway()) != 0 {
  144. return ep
  145. }
  146. }
  147. return nil
  148. }