default_gateway.go 4.1 KB

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