sandbox.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 renamin 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. // Destroy the sandbox
  30. Destroy() error
  31. }
  32. // Info represents all possible information that
  33. // the driver wants to place in the sandbox which includes
  34. // interfaces, routes and gateway
  35. type Info struct {
  36. Interfaces []*Interface
  37. // IPv4 gateway for the sandbox.
  38. Gateway net.IP
  39. // IPv6 gateway for the sandbox.
  40. GatewayIPv6 net.IP
  41. // TODO: Add routes and ip tables etc.
  42. }
  43. // Interface represents the settings and identity of a network device. It is
  44. // used as a return type for Network.Link, and it is common practice for the
  45. // caller to use this information when moving interface SrcName from host
  46. // namespace to DstName in a different net namespace with the appropriate
  47. // network settings.
  48. type Interface struct {
  49. // The name of the interface in the origin network namespace.
  50. SrcName string
  51. // The name that will be assigned to the interface once moves inside a
  52. // network namespace. When the caller passes in a DstName, it is only
  53. // expected to pass a prefix. The name will modified with an appropriately
  54. // auto-generated suffix.
  55. DstName string
  56. // IPv4 address for the interface.
  57. Address *net.IPNet
  58. // IPv6 address for the interface.
  59. AddressIPv6 *net.IPNet
  60. }
  61. // GetCopy returns a copy of this Interface structure
  62. func (i *Interface) GetCopy() *Interface {
  63. return &Interface{
  64. SrcName: i.SrcName,
  65. DstName: i.DstName,
  66. Address: types.GetIPNetCopy(i.Address),
  67. AddressIPv6: types.GetIPNetCopy(i.AddressIPv6),
  68. }
  69. }
  70. // Equal checks if this instance of Interface is equal to the passed one
  71. func (i *Interface) Equal(o *Interface) bool {
  72. if i == o {
  73. return true
  74. }
  75. if o == nil {
  76. return false
  77. }
  78. if i.SrcName != o.SrcName || i.DstName != o.DstName {
  79. return false
  80. }
  81. if !types.CompareIPNet(i.Address, o.Address) {
  82. return false
  83. }
  84. if !types.CompareIPNet(i.AddressIPv6, o.AddressIPv6) {
  85. return false
  86. }
  87. return true
  88. }
  89. // GetCopy returns a copy of this SandboxInfo structure
  90. func (s *Info) GetCopy() *Info {
  91. list := make([]*Interface, len(s.Interfaces))
  92. for i, iface := range s.Interfaces {
  93. list[i] = iface.GetCopy()
  94. }
  95. gw := types.GetIPCopy(s.Gateway)
  96. gw6 := types.GetIPCopy(s.GatewayIPv6)
  97. return &Info{Interfaces: list, Gateway: gw, GatewayIPv6: gw6}
  98. }
  99. // Equal checks if this instance of SandboxInfo is equal to the passed one
  100. func (s *Info) Equal(o *Info) bool {
  101. if s == o {
  102. return true
  103. }
  104. if o == nil {
  105. return false
  106. }
  107. if !s.Gateway.Equal(o.Gateway) {
  108. return false
  109. }
  110. if !s.GatewayIPv6.Equal(o.GatewayIPv6) {
  111. return false
  112. }
  113. if (s.Interfaces == nil && o.Interfaces != nil) ||
  114. (s.Interfaces != nil && o.Interfaces == nil) ||
  115. (len(s.Interfaces) != len(o.Interfaces)) {
  116. return false
  117. }
  118. // Note: At the moment, the two lists must be in the same order
  119. for i := 0; i < len(s.Interfaces); i++ {
  120. if !s.Interfaces[i].Equal(o.Interfaces[i]) {
  121. return false
  122. }
  123. }
  124. return true
  125. }