default_gateway.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package libnetwork
  2. import (
  3. "fmt"
  4. "github.com/docker/libnetwork/netlabel"
  5. "github.com/docker/libnetwork/options"
  6. "github.com/docker/libnetwork/types"
  7. )
  8. const (
  9. libnGWNetwork = "docker_gwbridge"
  10. gwEPlen = 12
  11. )
  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. n, err := c.NetworkByName(libnGWNetwork)
  31. if err != nil {
  32. if _, ok := err.(types.NotFoundError); !ok {
  33. return err
  34. }
  35. n, err = c.createGWNetwork()
  36. if err != nil {
  37. return err
  38. }
  39. }
  40. if opt, ok := srcEp.generic[netlabel.PortMap]; ok {
  41. if pb, ok := opt.([]types.PortBinding); ok {
  42. createOptions = append(createOptions, CreateOptionPortMapping(pb))
  43. }
  44. }
  45. if opt, ok := srcEp.generic[netlabel.ExposedPorts]; ok {
  46. if exp, ok := opt.([]types.TransportPort); ok {
  47. createOptions = append(createOptions, CreateOptionExposedPorts(exp))
  48. }
  49. }
  50. eplen := gwEPlen
  51. if len(sb.containerID) < gwEPlen {
  52. eplen = len(sb.containerID)
  53. }
  54. newEp, err := n.CreateEndpoint("gateway_"+sb.containerID[0:eplen], createOptions...)
  55. if err != nil {
  56. return fmt.Errorf("container %s: endpoint create on GW Network failed: %v", sb.containerID, err)
  57. }
  58. epLocal := newEp.(*endpoint)
  59. if err := epLocal.sbJoin(sb); err != nil {
  60. return fmt.Errorf("container %s: endpoint join on GW Network failed: %v", sb.containerID, err)
  61. }
  62. return nil
  63. }
  64. func (sb *sandbox) clearDefaultGW() error {
  65. var ep *endpoint
  66. if ep = sb.getEndpointInGWNetwork(); ep == nil {
  67. return nil
  68. }
  69. if err := ep.sbLeave(sb); err != nil {
  70. return fmt.Errorf("container %s: endpoint leaving GW Network failed: %v", sb.containerID, err)
  71. }
  72. if err := ep.Delete(); err != nil {
  73. return fmt.Errorf("container %s: deleting endpoint on GW Network failed: %v", sb.containerID, err)
  74. }
  75. return nil
  76. }
  77. func (c *controller) createGWNetwork() (Network, error) {
  78. netOption := options.Generic{
  79. "BridgeName": libnGWNetwork,
  80. "EnableICC": false,
  81. "EnableIPMasquerade": true,
  82. }
  83. n, err := c.NewNetwork("bridge", libnGWNetwork,
  84. NetworkOptionGeneric(options.Generic{
  85. netlabel.GenericData: netOption,
  86. netlabel.EnableIPv6: false,
  87. }))
  88. if err != nil {
  89. return nil, fmt.Errorf("error creating external connectivity network: %v", err)
  90. }
  91. return n, err
  92. }
  93. func (sb *sandbox) needDefaultGW() bool {
  94. var needGW bool
  95. for _, ep := range sb.getConnectedEndpoints() {
  96. if ep.endpointInGWNetwork() {
  97. continue
  98. }
  99. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  100. continue
  101. }
  102. // TODO v6 needs to be handled.
  103. if len(ep.Gateway()) > 0 {
  104. return false
  105. }
  106. needGW = true
  107. }
  108. return needGW
  109. }
  110. func (sb *sandbox) getEndpointInGWNetwork() *endpoint {
  111. for _, ep := range sb.getConnectedEndpoints() {
  112. if ep.getNetwork().name == libnGWNetwork {
  113. return ep
  114. }
  115. }
  116. return nil
  117. }
  118. func (ep *endpoint) endpointInGWNetwork() bool {
  119. if ep.getNetwork().name == libnGWNetwork {
  120. return true
  121. }
  122. return false
  123. }
  124. func (sb *sandbox) getEPwithoutGateway() *endpoint {
  125. for _, ep := range sb.getConnectedEndpoints() {
  126. if ep.getNetwork().Type() == "null" || ep.getNetwork().Type() == "host" {
  127. continue
  128. }
  129. if len(ep.Gateway()) == 0 {
  130. return ep
  131. }
  132. }
  133. return nil
  134. }