default_gateway.go 4.9 KB

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