sandbox.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Package osl describes structures and interfaces which abstract os entities
  2. package osl
  3. import (
  4. "net"
  5. "github.com/docker/docker/libnetwork/types"
  6. )
  7. // SandboxType specify the time of the sandbox, this can be used to apply special configs
  8. type SandboxType int
  9. const (
  10. // SandboxTypeIngress indicates that the sandbox is for the ingress
  11. SandboxTypeIngress = iota
  12. // SandboxTypeLoadBalancer indicates that the sandbox is a load balancer
  13. SandboxTypeLoadBalancer = iota
  14. )
  15. type Iface struct {
  16. SrcName, DstPrefix string
  17. }
  18. // IfaceOption is a function option type to set interface options.
  19. type IfaceOption func(i *nwIface)
  20. // NeighOption is a function option type to set neighbor options.
  21. type NeighOption func(nh *neigh)
  22. // Sandbox represents a network sandbox, identified by a specific key. It
  23. // holds a list of Interfaces, routes etc, and more can be added dynamically.
  24. type Sandbox interface {
  25. // Key returns the path where the network namespace is mounted.
  26. Key() string
  27. // AddInterface adds an existing Interface to this sandbox. The operation will rename
  28. // from the Interface SrcName to DstName as it moves, and reconfigure the
  29. // interface according to the specified settings. The caller is expected
  30. // to only provide a prefix for DstName. The AddInterface api will auto-generate
  31. // an appropriate suffix for the DstName to disambiguate.
  32. AddInterface(SrcName string, DstPrefix string, options ...IfaceOption) error
  33. // SetGateway sets the default IPv4 gateway for the sandbox.
  34. SetGateway(gw net.IP) error
  35. // SetGatewayIPv6 sets the default IPv6 gateway for the sandbox.
  36. SetGatewayIPv6(gw net.IP) error
  37. // UnsetGateway the previously set default IPv4 gateway in the sandbox.
  38. UnsetGateway() error
  39. // UnsetGatewayIPv6 unsets the previously set default IPv6 gateway in the sandbox.
  40. UnsetGatewayIPv6() error
  41. // GetLoopbackIfaceName returns the name of the loopback interface
  42. GetLoopbackIfaceName() string
  43. // AddAliasIP adds the passed IP address to the named interface
  44. AddAliasIP(ifName string, ip *net.IPNet) error
  45. // RemoveAliasIP removes the passed IP address from the named interface
  46. RemoveAliasIP(ifName string, ip *net.IPNet) error
  47. // DisableARPForVIP disables ARP replies and requests for VIP addresses
  48. // on a particular interface.
  49. DisableARPForVIP(ifName string) error
  50. // AddStaticRoute adds a static route to the sandbox.
  51. AddStaticRoute(*types.StaticRoute) error
  52. // RemoveStaticRoute removes a static route from the sandbox.
  53. RemoveStaticRoute(*types.StaticRoute) error
  54. // AddNeighbor adds a neighbor entry into the sandbox.
  55. AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, force bool, option ...NeighOption) error
  56. // DeleteNeighbor deletes neighbor entry from the sandbox.
  57. DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error
  58. // NeighborOptions returns an interface with methods to set neighbor options.
  59. NeighborOptions() NeighborOptionSetter
  60. // InterfaceOptions an interface with methods to set interface options.
  61. InterfaceOptions() IfaceOptionSetter
  62. // InvokeFunc invoke a function in the network namespace.
  63. InvokeFunc(func()) error
  64. // Info returns an interface with methods to get sandbox state.
  65. Info() Info
  66. // Destroy destroys the sandbox.
  67. Destroy() error
  68. // Restore restores the sandbox.
  69. Restore(ifsopt map[Iface][]IfaceOption, routes []*types.StaticRoute, gw net.IP, gw6 net.IP) error
  70. // ApplyOSTweaks applies operating system specific knobs on the sandbox.
  71. ApplyOSTweaks([]SandboxType)
  72. }
  73. // NeighborOptionSetter interface defines the option setter methods for interface options
  74. type NeighborOptionSetter interface {
  75. // LinkName returns an option setter to set the srcName of the link that should
  76. // be used in the neighbor entry
  77. LinkName(string) NeighOption
  78. // Family returns an option setter to set the address family for the neighbor
  79. // entry. eg. AF_BRIDGE
  80. Family(int) NeighOption
  81. }
  82. // IfaceOptionSetter interface defines the option setter methods for interface options.
  83. type IfaceOptionSetter interface {
  84. // Bridge returns an option setter to set if the interface is a bridge.
  85. Bridge(bool) IfaceOption
  86. // MacAddress returns an option setter to set the MAC address.
  87. MacAddress(net.HardwareAddr) IfaceOption
  88. // Address returns an option setter to set IPv4 address.
  89. Address(*net.IPNet) IfaceOption
  90. // AddressIPv6 returns an option setter to set IPv6 address.
  91. AddressIPv6(*net.IPNet) IfaceOption
  92. // LinkLocalAddresses returns an option setter to set the link-local IP addresses.
  93. LinkLocalAddresses([]*net.IPNet) IfaceOption
  94. // Master returns an option setter to set the master interface if any for this
  95. // interface. The master interface name should refer to the srcname of a
  96. // previously added interface of type bridge.
  97. Master(string) IfaceOption
  98. // Routes returns an option setter to set interface routes.
  99. Routes([]*net.IPNet) IfaceOption
  100. }
  101. // Info represents all possible information that
  102. // the driver wants to place in the sandbox which includes
  103. // interfaces, routes and gateway
  104. type Info interface {
  105. // Interfaces returns the collection of Interface previously added with the AddInterface
  106. // method. Note that this doesn't include network interfaces added in any
  107. // other way (such as the default loopback interface which is automatically
  108. // created on creation of a sandbox).
  109. Interfaces() []Interface
  110. // Gateway returns the IPv4 gateway for the sandbox.
  111. Gateway() net.IP
  112. // GatewayIPv6 returns the IPv6 gateway for the sandbox.
  113. GatewayIPv6() net.IP
  114. // StaticRoutes returns additional static routes for the sandbox. Note that
  115. // directly connected routes are stored on the particular interface they
  116. // refer to.
  117. StaticRoutes() []*types.StaticRoute
  118. // TODO: Add ip tables etc.
  119. }
  120. // Interface represents the settings and identity of a network device. It is
  121. // used as a return type for Network.Link, and it is common practice for the
  122. // caller to use this information when moving interface SrcName from host
  123. // namespace to DstName in a different net namespace with the appropriate
  124. // network settings.
  125. type Interface interface {
  126. // SrcName returns the name of the interface in the origin network namespace.
  127. SrcName() string
  128. // DstName returns the name that will be assigned to the interface once
  129. // moved inside a network namespace. When the caller passes in a DstName,
  130. // it is only expected to pass a prefix. The name will be modified with an
  131. // auto-generated suffix.
  132. DstName() string
  133. // Address returns the IPv4 address for the interface.
  134. Address() *net.IPNet
  135. // AddressIPv6 returns the IPv6 address for the interface.
  136. AddressIPv6() *net.IPNet
  137. // LinkLocalAddresses returns the link-local IP addresses assigned to the
  138. // interface.
  139. LinkLocalAddresses() []*net.IPNet
  140. // Routes returns IP routes for the interface.
  141. Routes() []*net.IPNet
  142. // Bridge returns true if the interface is a bridge.
  143. Bridge() bool
  144. // Master returns the srcname of the master interface for this interface.
  145. Master() string
  146. // Remove an interface from the sandbox by renaming to original name
  147. // and moving it out of the sandbox.
  148. Remove() error
  149. // Statistics returns the statistics for this interface
  150. Statistics() (*types.InterfaceStatistics, error)
  151. }