sandbox.go 3.6 KB

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