sandbox.go 3.7 KB

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