driverapi.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package driverapi
  2. import (
  3. "errors"
  4. "net"
  5. "github.com/docker/libnetwork/netutils"
  6. )
  7. var (
  8. // ErrEndpointExists is returned if more than one endpoint is added to the network
  9. ErrEndpointExists = errors.New("Endpoint already exists (Only one endpoint allowed)")
  10. // ErrNoNetwork is returned if no network with the specified id exists
  11. ErrNoNetwork = errors.New("No network exists")
  12. // ErrNoEndpoint is returned if no endpoint with the specified id exists
  13. ErrNoEndpoint = errors.New("No endpoint exists")
  14. )
  15. // UUID represents a globally unique ID of various resources like network and endpoint
  16. type UUID string
  17. // Driver is an interface that every plugin driver needs to implement.
  18. type Driver interface {
  19. // Push driver specific config to the driver
  20. Config(config interface{}) error
  21. // CreateNetwork invokes the driver method to create a network passing
  22. // the network id and network specific config. The config mechanism will
  23. // eventually be replaced with labels which are yet to be introduced.
  24. CreateNetwork(nid UUID, config interface{}) error
  25. // DeleteNetwork invokes the driver method to delete network passing
  26. // the network id.
  27. DeleteNetwork(nid UUID) error
  28. // CreateEndpoint invokes the driver method to create an endpoint
  29. // passing the network id, endpoint id, sandbox key and driver
  30. // specific config. The config mechanism will eventually be replaced
  31. // with labels which are yet to be introduced.
  32. CreateEndpoint(nid, eid UUID, key string, config interface{}) (*SandboxInfo, error)
  33. // DeleteEndpoint invokes the driver method to delete an endpoint
  34. // passing the network id and endpoint id.
  35. DeleteEndpoint(nid, eid UUID) error
  36. }
  37. // Interface represents the settings and identity of a network device. It is
  38. // used as a return type for Network.Link, and it is common practice for the
  39. // caller to use this information when moving interface SrcName from host
  40. // namespace to DstName in a different net namespace with the appropriate
  41. // network settings.
  42. type Interface struct {
  43. // The name of the interface in the origin network namespace.
  44. SrcName string
  45. // The name that will be assigned to the interface once moves inside a
  46. // network namespace.
  47. DstName string
  48. // IPv4 address for the interface.
  49. Address *net.IPNet
  50. // IPv6 address for the interface.
  51. AddressIPv6 *net.IPNet
  52. }
  53. // SandboxInfo represents all possible information that
  54. // the driver wants to place in the sandbox which includes
  55. // interfaces, routes and gateway
  56. type SandboxInfo struct {
  57. Interfaces []*Interface
  58. // IPv4 gateway for the sandbox.
  59. Gateway net.IP
  60. // IPv6 gateway for the sandbox.
  61. GatewayIPv6 net.IP
  62. // TODO: Add routes and ip tables etc.
  63. }
  64. // GetCopy returns a copy of this Interface structure
  65. func (i *Interface) GetCopy() *Interface {
  66. return &Interface{
  67. SrcName: i.SrcName,
  68. DstName: i.DstName,
  69. Address: netutils.GetIPNetCopy(i.Address),
  70. AddressIPv6: netutils.GetIPNetCopy(i.AddressIPv6),
  71. }
  72. }
  73. // Equal checks if this instance of Interface is equal to the passed one
  74. func (i *Interface) Equal(o *Interface) bool {
  75. if i == o {
  76. return true
  77. }
  78. if o == nil {
  79. return false
  80. }
  81. if i.SrcName != o.SrcName || i.DstName != o.DstName {
  82. return false
  83. }
  84. if !netutils.CompareIPNet(i.Address, o.Address) {
  85. return false
  86. }
  87. if !netutils.CompareIPNet(i.AddressIPv6, o.AddressIPv6) {
  88. return false
  89. }
  90. return true
  91. }
  92. // GetCopy returns a copy of this SandboxInfo structure
  93. func (s *SandboxInfo) GetCopy() *SandboxInfo {
  94. list := make([]*Interface, len(s.Interfaces))
  95. for i, iface := range s.Interfaces {
  96. list[i] = iface.GetCopy()
  97. }
  98. gw := netutils.GetIPCopy(s.Gateway)
  99. gw6 := netutils.GetIPCopy(s.GatewayIPv6)
  100. return &SandboxInfo{Interfaces: list, Gateway: gw, GatewayIPv6: gw6}
  101. }
  102. // Equal checks if this instance of SandboxInfo is equal to the passed one
  103. func (s *SandboxInfo) Equal(o *SandboxInfo) bool {
  104. if s == o {
  105. return true
  106. }
  107. if o == nil {
  108. return false
  109. }
  110. if !s.Gateway.Equal(o.Gateway) {
  111. return false
  112. }
  113. if !s.GatewayIPv6.Equal(o.GatewayIPv6) {
  114. return false
  115. }
  116. if (s.Interfaces == nil && o.Interfaces != nil) ||
  117. (s.Interfaces != nil && o.Interfaces == nil) ||
  118. (len(s.Interfaces) != len(o.Interfaces)) {
  119. return false
  120. }
  121. // Note: At the moment, the two lists must be in the same order
  122. for i := 0; i < len(s.Interfaces); i++ {
  123. if !s.Interfaces[i].Equal(o.Interfaces[i]) {
  124. return false
  125. }
  126. }
  127. return true
  128. }