sandbox.go 3.8 KB

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