sandbox.go 5.8 KB

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