network.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. Package libnetwork provides the basic functionality and extension points to
  3. create network namespaces and allocate interfaces for containers to use.
  4. // Create a new controller instance
  5. controller := libnetwork.New()
  6. // This option is only needed for in-tree drivers. Plugins(in future) will get
  7. // their options through plugin infrastructure.
  8. option := options.Generic{}
  9. driver, err := controller.NewNetworkDriver("simplebridge", option)
  10. if err != nil {
  11. return
  12. }
  13. netOptions := options.Generic{}
  14. // Create a network for containers to join.
  15. network, err := controller.NewNetwork(driver, "network1", netOptions)
  16. if err != nil {
  17. return
  18. }
  19. // For a new container: create a sandbox instance (providing a unique key).
  20. // For linux it is a filesystem path
  21. networkPath := "/var/lib/docker/.../4d23e"
  22. networkNamespace, err := sandbox.NewSandbox(networkPath)
  23. if err != nil {
  24. return
  25. }
  26. // For each new container: allocate IP and interfaces. The returned network
  27. // settings will be used for container infos (inspect and such), as well as
  28. // iptables rules for port publishing.
  29. ep, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), "")
  30. if err != nil {
  31. return
  32. }
  33. */
  34. package libnetwork
  35. import (
  36. "sync"
  37. "github.com/docker/docker/pkg/stringid"
  38. "github.com/docker/libnetwork/driverapi"
  39. "github.com/docker/libnetwork/sandbox"
  40. "github.com/docker/libnetwork/types"
  41. )
  42. // NetworkController provides the interface for controller instance which manages
  43. // networks.
  44. type NetworkController interface {
  45. // NOTE: This method will go away when moving to plugin infrastructure
  46. NewNetworkDriver(networkType string, options interface{}) (*NetworkDriver, error)
  47. // Create a new network. The options parameter carries network specific options.
  48. // Labels support will be added in the near future.
  49. NewNetwork(d *NetworkDriver, name string, options interface{}) (Network, error)
  50. }
  51. // A Network represents a logical connectivity zone that containers may
  52. // join using the Link method. A Network is managed by a specific driver.
  53. type Network interface {
  54. // A user chosen name for this network.
  55. Name() string
  56. // A system generated id for this network.
  57. ID() string
  58. // The type of network, which corresponds to its managing driver.
  59. Type() string
  60. // Create a new endpoint to this network symbolically identified by the
  61. // specified unique name. The options parameter carry driver specific options.
  62. // Labels support will be added in the near future.
  63. CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error)
  64. // Endpoints returns the list of Endpoint in this network.
  65. Endpoints() []Endpoint
  66. // Delete the network.
  67. Delete() error
  68. }
  69. // Endpoint represents a logical connection between a network and a sandbox.
  70. type Endpoint interface {
  71. // A system generated id for this endpoint.
  72. ID() string
  73. // Name returns the name of this endpoint.
  74. Name() string
  75. // Network returns the name of the network to which this endpoint is attached.
  76. Network() string
  77. // SandboxInfo returns the sandbox information for this endpoint.
  78. SandboxInfo() *sandbox.Info
  79. // Delete and detaches this endpoint from the network.
  80. Delete() error
  81. }
  82. // NetworkDriver provides a reference to driver and way to push driver specific config
  83. type NetworkDriver struct {
  84. networkType string
  85. internalDriver driverapi.Driver
  86. }
  87. type endpoint struct {
  88. name string
  89. id types.UUID
  90. network *network
  91. sandboxInfo *sandbox.Info
  92. }
  93. type network struct {
  94. ctrlr *controller
  95. name string
  96. networkType string
  97. id types.UUID
  98. driver *NetworkDriver
  99. endpoints endpointTable
  100. sync.Mutex
  101. }
  102. type networkTable map[types.UUID]*network
  103. type endpointTable map[types.UUID]*endpoint
  104. type controller struct {
  105. networks networkTable
  106. drivers driverTable
  107. sync.Mutex
  108. }
  109. // New creates a new instance of network controller.
  110. func New() NetworkController {
  111. return &controller{networkTable{}, enumerateDrivers(), sync.Mutex{}}
  112. }
  113. func (c *controller) NewNetworkDriver(networkType string, options interface{}) (*NetworkDriver, error) {
  114. d, ok := c.drivers[networkType]
  115. if !ok {
  116. return nil, NetworkTypeError(networkType)
  117. }
  118. if err := d.Config(options); err != nil {
  119. return nil, err
  120. }
  121. return &NetworkDriver{networkType: networkType, internalDriver: d}, nil
  122. }
  123. // NewNetwork creates a new network of the specified NetworkDriver. The options
  124. // are driver specific and modeled in a generic way.
  125. func (c *controller) NewNetwork(nd *NetworkDriver, name string, options interface{}) (Network, error) {
  126. if nd == nil {
  127. return nil, ErrNilNetworkDriver
  128. }
  129. c.Lock()
  130. for _, n := range c.networks {
  131. if n.name == name {
  132. c.Unlock()
  133. return nil, NetworkNameError(name)
  134. }
  135. }
  136. c.Unlock()
  137. network := &network{
  138. name: name,
  139. id: types.UUID(stringid.GenerateRandomID()),
  140. ctrlr: c,
  141. driver: nd}
  142. network.endpoints = make(endpointTable)
  143. d := network.driver.internalDriver
  144. if d == nil {
  145. return nil, ErrInvalidNetworkDriver
  146. }
  147. if err := d.CreateNetwork(network.id, options); err != nil {
  148. return nil, err
  149. }
  150. c.Lock()
  151. c.networks[network.id] = network
  152. c.Unlock()
  153. return network, nil
  154. }
  155. func (n *network) Name() string {
  156. return n.name
  157. }
  158. func (n *network) ID() string {
  159. return string(n.id)
  160. }
  161. func (n *network) Type() string {
  162. if n.driver == nil {
  163. return ""
  164. }
  165. return n.driver.networkType
  166. }
  167. func (n *network) Delete() error {
  168. var err error
  169. n.ctrlr.Lock()
  170. _, ok := n.ctrlr.networks[n.id]
  171. if !ok {
  172. n.ctrlr.Unlock()
  173. return &UnknownNetworkError{name: n.name, id: string(n.id)}
  174. }
  175. n.Lock()
  176. numEps := len(n.endpoints)
  177. n.Unlock()
  178. if numEps != 0 {
  179. n.ctrlr.Unlock()
  180. return &ActiveEndpointsError{name: n.name, id: string(n.id)}
  181. }
  182. delete(n.ctrlr.networks, n.id)
  183. n.ctrlr.Unlock()
  184. defer func() {
  185. if err != nil {
  186. n.ctrlr.Lock()
  187. n.ctrlr.networks[n.id] = n
  188. n.ctrlr.Unlock()
  189. }
  190. }()
  191. d := n.driver.internalDriver
  192. err = d.DeleteNetwork(n.id)
  193. return err
  194. }
  195. func (n *network) CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error) {
  196. ep := &endpoint{name: name}
  197. ep.id = types.UUID(stringid.GenerateRandomID())
  198. ep.network = n
  199. d := n.driver.internalDriver
  200. sinfo, err := d.CreateEndpoint(n.id, ep.id, sboxKey, options)
  201. if err != nil {
  202. return nil, err
  203. }
  204. ep.sandboxInfo = sinfo
  205. n.Lock()
  206. n.endpoints[ep.id] = ep
  207. n.Unlock()
  208. return ep, nil
  209. }
  210. func (n *network) Endpoints() []Endpoint {
  211. n.Lock()
  212. defer n.Unlock()
  213. list := make([]Endpoint, len(n.endpoints))
  214. idx := 0
  215. for _, e := range n.endpoints {
  216. list[idx] = e
  217. idx++
  218. }
  219. return list
  220. }
  221. func (ep *endpoint) ID() string {
  222. return string(ep.id)
  223. }
  224. func (ep *endpoint) Name() string {
  225. return ep.name
  226. }
  227. func (ep *endpoint) Network() string {
  228. return ep.network.name
  229. }
  230. func (ep *endpoint) SandboxInfo() *sandbox.Info {
  231. if ep.sandboxInfo == nil {
  232. return nil
  233. }
  234. return ep.sandboxInfo.GetCopy()
  235. }
  236. func (ep *endpoint) Delete() error {
  237. var err error
  238. n := ep.network
  239. n.Lock()
  240. _, ok := n.endpoints[ep.id]
  241. if !ok {
  242. n.Unlock()
  243. return &UnknownEndpointError{name: ep.name, id: string(ep.id)}
  244. }
  245. delete(n.endpoints, ep.id)
  246. n.Unlock()
  247. defer func() {
  248. if err != nil {
  249. n.Lock()
  250. n.endpoints[ep.id] = ep
  251. n.Unlock()
  252. }
  253. }()
  254. d := n.driver.internalDriver
  255. err = d.DeleteEndpoint(n.id, ep.id)
  256. return err
  257. }