default_gateway.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, removes the endpoint connecting the sandbox to the default gw network.
  52. // Unless it is the endpoint designated to provide the external connectivity.
  53. // If the sandbox is being deleted, removes the endpoint unconditionally.
  54. func (sb *sandbox) clearDefaultGW() error {
  55. var ep *endpoint
  56. if ep = sb.getEndpointInGWNetwork(); ep == nil {
  57. return nil
  58. }
  59. if ep == sb.getGatewayEndpoint() && !sb.inDelete {
  60. return nil
  61. }
  62. if err := ep.sbLeave(sb, false); err != nil {
  63. return fmt.Errorf("container %s: endpoint leaving GW Network failed: %v", sb.containerID, err)
  64. }
  65. if err := ep.Delete(false); err != nil {
  66. return fmt.Errorf("container %s: deleting endpoint on GW Network failed: %v", sb.containerID, err)
  67. }
  68. return nil
  69. }
  70. func (sb *sandbox) needDefaultGW() bool {
  71. var needGW bool
  72. for _, ep := range sb.getConnectedEndpoints() {
  73. if ep.endpointInGWNetwork() {
  74. return false
  75. }
  76. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  77. continue
  78. }
  79. if ep.getNetwork().Internal() {
  80. return false
  81. }
  82. if ep.joinInfo.disableGatewayService {
  83. return false
  84. }
  85. // TODO v6 needs to be handled.
  86. if len(ep.Gateway()) > 0 {
  87. return false
  88. }
  89. for _, r := range ep.StaticRoutes() {
  90. if r.Destination.String() == "0.0.0.0/0" {
  91. return false
  92. }
  93. }
  94. needGW = true
  95. }
  96. return needGW
  97. }
  98. func (sb *sandbox) getEndpointInGWNetwork() *endpoint {
  99. for _, ep := range sb.getConnectedEndpoints() {
  100. if ep.getNetwork().name == libnGWNetwork {
  101. return ep
  102. }
  103. }
  104. return nil
  105. }
  106. func (ep *endpoint) endpointInGWNetwork() bool {
  107. if ep.getNetwork().name == libnGWNetwork {
  108. return true
  109. }
  110. return false
  111. }
  112. func (sb *sandbox) getEPwithoutGateway() *endpoint {
  113. for _, ep := range sb.getConnectedEndpoints() {
  114. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  115. continue
  116. }
  117. if len(ep.Gateway()) == 0 {
  118. return ep
  119. }
  120. }
  121. return nil
  122. }
  123. // Looks for the default gw network and creates it if not there.
  124. // Parallel executions are serialized.
  125. func (c *controller) defaultGwNetwork() (Network, error) {
  126. procGwNetwork <- true
  127. defer func() { <-procGwNetwork }()
  128. n, err := c.NetworkByName(libnGWNetwork)
  129. if err != nil {
  130. if _, ok := err.(types.NotFoundError); ok {
  131. n, err = c.createGWNetwork()
  132. }
  133. }
  134. return n, err
  135. }
  136. // Returns the endpoint which is providing external connectivity to the sandbox
  137. func (sb *sandbox) getGatewayEndpoint() *endpoint {
  138. for _, ep := range sb.getConnectedEndpoints() {
  139. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  140. continue
  141. }
  142. if len(ep.Gateway()) != 0 {
  143. return ep
  144. }
  145. }
  146. return nil
  147. }