sandbox.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package sandbox
  2. import (
  3. "net"
  4. "github.com/docker/libnetwork/types"
  5. )
  6. // Sandbox represents a network sandbox, identified by a specific key. It
  7. // holds a list of Interfaces, routes etc, and more can be added dynamically.
  8. type Sandbox interface {
  9. // The path where the network namespace is mounted.
  10. Key() string
  11. // The collection of Interface previously added with the AddInterface
  12. // method. Note that this doesn't incude network interfaces added in any
  13. // other way (such as the default loopback interface which are automatically
  14. // created on creation of a sandbox).
  15. Interfaces() []*Interface
  16. // Add an existing Interface to this sandbox. The operation will rename
  17. // from the Interface SrcName to DstName as it moves, and reconfigure the
  18. // interface according to the specified settings. The caller is expected
  19. // to only provide a prefix for DstName. The AddInterface api will auto-generate
  20. // an appropriate suffix for the DstName to disambiguate.
  21. AddInterface(*Interface) error
  22. // Remove an interface from the sandbox by renaming to original name
  23. // and moving it out of the sandbox.
  24. RemoveInterface(*Interface) error
  25. // Set default IPv4 gateway for the sandbox
  26. SetGateway(gw net.IP) error
  27. // Set default IPv6 gateway for the sandbox
  28. SetGatewayIPv6(gw net.IP) error
  29. // Add a static route to the sandbox.
  30. AddStaticRoute(*types.StaticRoute) error
  31. // Remove a static route from the sandbox.
  32. RemoveStaticRoute(*types.StaticRoute) error
  33. // Destroy the sandbox
  34. Destroy() error
  35. }
  36. // Info represents all possible information that
  37. // the driver wants to place in the sandbox which includes
  38. // interfaces, routes and gateway
  39. type Info struct {
  40. Interfaces []*Interface
  41. // IPv4 gateway for the sandbox.
  42. Gateway net.IP
  43. // IPv6 gateway for the sandbox.
  44. GatewayIPv6 net.IP
  45. // Additional static routes for the sandbox. (Note that directly
  46. // connected routes are stored on the particular interface they refer to.)
  47. StaticRoutes []*types.StaticRoute
  48. // TODO: Add ip tables etc.
  49. }
  50. // Interface represents the settings and identity of a network device. It is
  51. // used as a return type for Network.Link, and it is common practice for the
  52. // caller to use this information when moving interface SrcName from host
  53. // namespace to DstName in a different net namespace with the appropriate
  54. // network settings.
  55. type Interface struct {
  56. // The name of the interface in the origin network namespace.
  57. SrcName string
  58. // The name that will be assigned to the interface once moves inside a
  59. // network namespace. When the caller passes in a DstName, it is only
  60. // expected to pass a prefix. The name will modified with an appropriately
  61. // auto-generated suffix.
  62. DstName string
  63. // IPv4 address for the interface.
  64. Address *net.IPNet
  65. // IPv6 address for the interface.
  66. AddressIPv6 *net.IPNet
  67. // IP routes for the interface.
  68. Routes []*net.IPNet
  69. }
  70. // GetCopy returns a copy of this Interface structure
  71. func (i *Interface) GetCopy() *Interface {
  72. copiedRoutes := make([]*net.IPNet, len(i.Routes))
  73. for index := range i.Routes {
  74. copiedRoutes[index] = types.GetIPNetCopy(i.Routes[index])
  75. }
  76. return &Interface{
  77. SrcName: i.SrcName,
  78. DstName: i.DstName,
  79. Address: types.GetIPNetCopy(i.Address),
  80. AddressIPv6: types.GetIPNetCopy(i.AddressIPv6),
  81. Routes: copiedRoutes,
  82. }
  83. }
  84. // Equal checks if this instance of Interface is equal to the passed one
  85. func (i *Interface) Equal(o *Interface) bool {
  86. if i == o {
  87. return true
  88. }
  89. if o == nil {
  90. return false
  91. }
  92. if i.SrcName != o.SrcName || i.DstName != o.DstName {
  93. return false
  94. }
  95. if !types.CompareIPNet(i.Address, o.Address) {
  96. return false
  97. }
  98. if !types.CompareIPNet(i.AddressIPv6, o.AddressIPv6) {
  99. return false
  100. }
  101. if len(i.Routes) != len(o.Routes) {
  102. return false
  103. }
  104. for index := range i.Routes {
  105. if !types.CompareIPNet(i.Routes[index], o.Routes[index]) {
  106. return false
  107. }
  108. }
  109. return true
  110. }
  111. // GetCopy returns a copy of this SandboxInfo structure
  112. func (s *Info) GetCopy() *Info {
  113. list := make([]*Interface, len(s.Interfaces))
  114. for i, iface := range s.Interfaces {
  115. list[i] = iface.GetCopy()
  116. }
  117. gw := types.GetIPCopy(s.Gateway)
  118. gw6 := types.GetIPCopy(s.GatewayIPv6)
  119. routes := make([]*types.StaticRoute, len(s.StaticRoutes))
  120. for i, r := range s.StaticRoutes {
  121. routes[i] = r.GetCopy()
  122. }
  123. return &Info{Interfaces: list,
  124. Gateway: gw,
  125. GatewayIPv6: gw6,
  126. StaticRoutes: routes}
  127. }
  128. // Equal checks if this instance of SandboxInfo is equal to the passed one
  129. func (s *Info) Equal(o *Info) bool {
  130. if s == o {
  131. return true
  132. }
  133. if o == nil {
  134. return false
  135. }
  136. if !s.Gateway.Equal(o.Gateway) {
  137. return false
  138. }
  139. if !s.GatewayIPv6.Equal(o.GatewayIPv6) {
  140. return false
  141. }
  142. if (s.Interfaces == nil && o.Interfaces != nil) ||
  143. (s.Interfaces != nil && o.Interfaces == nil) ||
  144. (len(s.Interfaces) != len(o.Interfaces)) {
  145. return false
  146. }
  147. // Note: At the moment, the two lists must be in the same order
  148. for i := 0; i < len(s.Interfaces); i++ {
  149. if !s.Interfaces[i].Equal(o.Interfaces[i]) {
  150. return false
  151. }
  152. }
  153. for index := range s.StaticRoutes {
  154. ss := s.StaticRoutes[index]
  155. oo := o.StaticRoutes[index]
  156. if !types.CompareIPNet(ss.Destination, oo.Destination) {
  157. return false
  158. }
  159. if !ss.NextHop.Equal(oo.NextHop) {
  160. return false
  161. }
  162. }
  163. return true
  164. }