network.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. package libnetwork
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "strings"
  7. "sync"
  8. log "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/pkg/stringid"
  10. "github.com/docker/libnetwork/config"
  11. "github.com/docker/libnetwork/datastore"
  12. "github.com/docker/libnetwork/driverapi"
  13. "github.com/docker/libnetwork/etchosts"
  14. "github.com/docker/libnetwork/ipamapi"
  15. "github.com/docker/libnetwork/netlabel"
  16. "github.com/docker/libnetwork/netutils"
  17. "github.com/docker/libnetwork/options"
  18. "github.com/docker/libnetwork/types"
  19. )
  20. // A Network represents a logical connectivity zone that containers may
  21. // join using the Link method. A Network is managed by a specific driver.
  22. type Network interface {
  23. // A user chosen name for this network.
  24. Name() string
  25. // A system generated id for this network.
  26. ID() string
  27. // The type of network, which corresponds to its managing driver.
  28. Type() string
  29. // Create a new endpoint to this network symbolically identified by the
  30. // specified unique name. The options parameter carry driver specific options.
  31. CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error)
  32. // Delete the network.
  33. Delete() error
  34. // Endpoints returns the list of Endpoint(s) in this network.
  35. Endpoints() []Endpoint
  36. // WalkEndpoints uses the provided function to walk the Endpoints
  37. WalkEndpoints(walker EndpointWalker)
  38. // EndpointByName returns the Endpoint which has the passed name. If not found, the error ErrNoSuchEndpoint is returned.
  39. EndpointByName(name string) (Endpoint, error)
  40. // EndpointByID returns the Endpoint which has the passed id. If not found, the error ErrNoSuchEndpoint is returned.
  41. EndpointByID(id string) (Endpoint, error)
  42. // Return certain operational data belonging to this network
  43. Info() NetworkInfo
  44. }
  45. // NetworkInfo returns some configuration and operational information about the network
  46. type NetworkInfo interface {
  47. IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf)
  48. IpamInfo() ([]*IpamInfo, []*IpamInfo)
  49. DriverOptions() map[string]string
  50. Scope() string
  51. IPv6Enabled() bool
  52. Internal() bool
  53. Labels() map[string]string
  54. }
  55. // EndpointWalker is a client provided function which will be used to walk the Endpoints.
  56. // When the function returns true, the walk will stop.
  57. type EndpointWalker func(ep Endpoint) bool
  58. type svcInfo struct {
  59. svcMap map[string][]net.IP
  60. ipMap map[string]string
  61. }
  62. // IpamConf contains all the ipam related configurations for a network
  63. type IpamConf struct {
  64. // The master address pool for containers and network interfaces
  65. PreferredPool string
  66. // A subset of the master pool. If specified,
  67. // this becomes the container pool
  68. SubPool string
  69. // Preferred Network Gateway address (optional)
  70. Gateway string
  71. // Auxiliary addresses for network driver. Must be within the master pool.
  72. // libnetwork will reserve them if they fall into the container pool
  73. AuxAddresses map[string]string
  74. }
  75. // Validate checks whether the configuration is valid
  76. func (c *IpamConf) Validate() error {
  77. if c.Gateway != "" && nil == net.ParseIP(c.Gateway) {
  78. return types.BadRequestErrorf("invalid gateway address %s in Ipam configuration", c.Gateway)
  79. }
  80. return nil
  81. }
  82. // IpamInfo contains all the ipam related operational info for a network
  83. type IpamInfo struct {
  84. PoolID string
  85. Meta map[string]string
  86. driverapi.IPAMData
  87. }
  88. // MarshalJSON encodes IpamInfo into json message
  89. func (i *IpamInfo) MarshalJSON() ([]byte, error) {
  90. m := map[string]interface{}{
  91. "PoolID": i.PoolID,
  92. }
  93. v, err := json.Marshal(&i.IPAMData)
  94. if err != nil {
  95. return nil, err
  96. }
  97. m["IPAMData"] = string(v)
  98. if i.Meta != nil {
  99. m["Meta"] = i.Meta
  100. }
  101. return json.Marshal(m)
  102. }
  103. // UnmarshalJSON decodes json message into PoolData
  104. func (i *IpamInfo) UnmarshalJSON(data []byte) error {
  105. var (
  106. m map[string]interface{}
  107. err error
  108. )
  109. if err = json.Unmarshal(data, &m); err != nil {
  110. return err
  111. }
  112. i.PoolID = m["PoolID"].(string)
  113. if v, ok := m["Meta"]; ok {
  114. b, _ := json.Marshal(v)
  115. if err = json.Unmarshal(b, &i.Meta); err != nil {
  116. return err
  117. }
  118. }
  119. if v, ok := m["IPAMData"]; ok {
  120. if err = json.Unmarshal([]byte(v.(string)), &i.IPAMData); err != nil {
  121. return err
  122. }
  123. }
  124. return nil
  125. }
  126. type network struct {
  127. ctrlr *controller
  128. name string
  129. networkType string
  130. id string
  131. scope string
  132. labels map[string]string
  133. ipamType string
  134. ipamOptions map[string]string
  135. addrSpace string
  136. ipamV4Config []*IpamConf
  137. ipamV6Config []*IpamConf
  138. ipamV4Info []*IpamInfo
  139. ipamV6Info []*IpamInfo
  140. enableIPv6 bool
  141. postIPv6 bool
  142. epCnt *endpointCnt
  143. generic options.Generic
  144. dbIndex uint64
  145. dbExists bool
  146. persist bool
  147. stopWatchCh chan struct{}
  148. drvOnce *sync.Once
  149. internal bool
  150. inDelete bool
  151. sync.Mutex
  152. }
  153. func (n *network) Name() string {
  154. n.Lock()
  155. defer n.Unlock()
  156. return n.name
  157. }
  158. func (n *network) ID() string {
  159. n.Lock()
  160. defer n.Unlock()
  161. return n.id
  162. }
  163. func (n *network) Type() string {
  164. n.Lock()
  165. defer n.Unlock()
  166. return n.networkType
  167. }
  168. func (n *network) Key() []string {
  169. n.Lock()
  170. defer n.Unlock()
  171. return []string{datastore.NetworkKeyPrefix, n.id}
  172. }
  173. func (n *network) KeyPrefix() []string {
  174. return []string{datastore.NetworkKeyPrefix}
  175. }
  176. func (n *network) Value() []byte {
  177. n.Lock()
  178. defer n.Unlock()
  179. b, err := json.Marshal(n)
  180. if err != nil {
  181. return nil
  182. }
  183. return b
  184. }
  185. func (n *network) SetValue(value []byte) error {
  186. return json.Unmarshal(value, n)
  187. }
  188. func (n *network) Index() uint64 {
  189. n.Lock()
  190. defer n.Unlock()
  191. return n.dbIndex
  192. }
  193. func (n *network) SetIndex(index uint64) {
  194. n.Lock()
  195. n.dbIndex = index
  196. n.dbExists = true
  197. n.Unlock()
  198. }
  199. func (n *network) Exists() bool {
  200. n.Lock()
  201. defer n.Unlock()
  202. return n.dbExists
  203. }
  204. func (n *network) Skip() bool {
  205. n.Lock()
  206. defer n.Unlock()
  207. return !n.persist
  208. }
  209. func (n *network) New() datastore.KVObject {
  210. n.Lock()
  211. defer n.Unlock()
  212. return &network{
  213. ctrlr: n.ctrlr,
  214. drvOnce: &sync.Once{},
  215. scope: n.scope,
  216. }
  217. }
  218. // CopyTo deep copies to the destination IpamConfig
  219. func (c *IpamConf) CopyTo(dstC *IpamConf) error {
  220. dstC.PreferredPool = c.PreferredPool
  221. dstC.SubPool = c.SubPool
  222. dstC.Gateway = c.Gateway
  223. if c.AuxAddresses != nil {
  224. dstC.AuxAddresses = make(map[string]string, len(c.AuxAddresses))
  225. for k, v := range c.AuxAddresses {
  226. dstC.AuxAddresses[k] = v
  227. }
  228. }
  229. return nil
  230. }
  231. // CopyTo deep copies to the destination IpamInfo
  232. func (i *IpamInfo) CopyTo(dstI *IpamInfo) error {
  233. dstI.PoolID = i.PoolID
  234. if i.Meta != nil {
  235. dstI.Meta = make(map[string]string)
  236. for k, v := range i.Meta {
  237. dstI.Meta[k] = v
  238. }
  239. }
  240. dstI.AddressSpace = i.AddressSpace
  241. dstI.Pool = types.GetIPNetCopy(i.Pool)
  242. dstI.Gateway = types.GetIPNetCopy(i.Gateway)
  243. if i.AuxAddresses != nil {
  244. dstI.AuxAddresses = make(map[string]*net.IPNet)
  245. for k, v := range i.AuxAddresses {
  246. dstI.AuxAddresses[k] = types.GetIPNetCopy(v)
  247. }
  248. }
  249. return nil
  250. }
  251. func (n *network) CopyTo(o datastore.KVObject) error {
  252. n.Lock()
  253. defer n.Unlock()
  254. dstN := o.(*network)
  255. dstN.name = n.name
  256. dstN.id = n.id
  257. dstN.networkType = n.networkType
  258. dstN.scope = n.scope
  259. dstN.ipamType = n.ipamType
  260. dstN.enableIPv6 = n.enableIPv6
  261. dstN.persist = n.persist
  262. dstN.postIPv6 = n.postIPv6
  263. dstN.dbIndex = n.dbIndex
  264. dstN.dbExists = n.dbExists
  265. dstN.drvOnce = n.drvOnce
  266. dstN.internal = n.internal
  267. dstN.inDelete = n.inDelete
  268. // copy labels
  269. if dstN.labels == nil {
  270. dstN.labels = make(map[string]string, len(n.labels))
  271. }
  272. for k, v := range n.labels {
  273. dstN.labels[k] = v
  274. }
  275. for _, v4conf := range n.ipamV4Config {
  276. dstV4Conf := &IpamConf{}
  277. v4conf.CopyTo(dstV4Conf)
  278. dstN.ipamV4Config = append(dstN.ipamV4Config, dstV4Conf)
  279. }
  280. for _, v4info := range n.ipamV4Info {
  281. dstV4Info := &IpamInfo{}
  282. v4info.CopyTo(dstV4Info)
  283. dstN.ipamV4Info = append(dstN.ipamV4Info, dstV4Info)
  284. }
  285. for _, v6conf := range n.ipamV6Config {
  286. dstV6Conf := &IpamConf{}
  287. v6conf.CopyTo(dstV6Conf)
  288. dstN.ipamV6Config = append(dstN.ipamV6Config, dstV6Conf)
  289. }
  290. for _, v6info := range n.ipamV6Info {
  291. dstV6Info := &IpamInfo{}
  292. v6info.CopyTo(dstV6Info)
  293. dstN.ipamV6Info = append(dstN.ipamV6Info, dstV6Info)
  294. }
  295. dstN.generic = options.Generic{}
  296. for k, v := range n.generic {
  297. dstN.generic[k] = v
  298. }
  299. return nil
  300. }
  301. func (n *network) DataScope() string {
  302. return n.Scope()
  303. }
  304. func (n *network) getEpCnt() *endpointCnt {
  305. n.Lock()
  306. defer n.Unlock()
  307. return n.epCnt
  308. }
  309. // TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
  310. func (n *network) MarshalJSON() ([]byte, error) {
  311. netMap := make(map[string]interface{})
  312. netMap["name"] = n.name
  313. netMap["id"] = n.id
  314. netMap["networkType"] = n.networkType
  315. netMap["scope"] = n.scope
  316. netMap["labels"] = n.labels
  317. netMap["ipamType"] = n.ipamType
  318. netMap["addrSpace"] = n.addrSpace
  319. netMap["enableIPv6"] = n.enableIPv6
  320. if n.generic != nil {
  321. netMap["generic"] = n.generic
  322. }
  323. netMap["persist"] = n.persist
  324. netMap["postIPv6"] = n.postIPv6
  325. if len(n.ipamV4Config) > 0 {
  326. ics, err := json.Marshal(n.ipamV4Config)
  327. if err != nil {
  328. return nil, err
  329. }
  330. netMap["ipamV4Config"] = string(ics)
  331. }
  332. if len(n.ipamV4Info) > 0 {
  333. iis, err := json.Marshal(n.ipamV4Info)
  334. if err != nil {
  335. return nil, err
  336. }
  337. netMap["ipamV4Info"] = string(iis)
  338. }
  339. if len(n.ipamV6Config) > 0 {
  340. ics, err := json.Marshal(n.ipamV6Config)
  341. if err != nil {
  342. return nil, err
  343. }
  344. netMap["ipamV6Config"] = string(ics)
  345. }
  346. if len(n.ipamV6Info) > 0 {
  347. iis, err := json.Marshal(n.ipamV6Info)
  348. if err != nil {
  349. return nil, err
  350. }
  351. netMap["ipamV6Info"] = string(iis)
  352. }
  353. netMap["internal"] = n.internal
  354. netMap["inDelete"] = n.inDelete
  355. return json.Marshal(netMap)
  356. }
  357. // TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
  358. func (n *network) UnmarshalJSON(b []byte) (err error) {
  359. var netMap map[string]interface{}
  360. if err := json.Unmarshal(b, &netMap); err != nil {
  361. return err
  362. }
  363. n.name = netMap["name"].(string)
  364. n.id = netMap["id"].(string)
  365. n.networkType = netMap["networkType"].(string)
  366. n.enableIPv6 = netMap["enableIPv6"].(bool)
  367. // if we weren't unmarshaling to netMap we could simply set n.labels
  368. // unfortunately, we can't because map[string]interface{} != map[string]string
  369. if labels, ok := netMap["labels"].(map[string]interface{}); ok {
  370. n.labels = make(map[string]string, len(labels))
  371. for label, value := range labels {
  372. n.labels[label] = value.(string)
  373. }
  374. }
  375. if v, ok := netMap["generic"]; ok {
  376. n.generic = v.(map[string]interface{})
  377. // Restore opts in their map[string]string form
  378. if v, ok := n.generic[netlabel.GenericData]; ok {
  379. var lmap map[string]string
  380. ba, err := json.Marshal(v)
  381. if err != nil {
  382. return err
  383. }
  384. if err := json.Unmarshal(ba, &lmap); err != nil {
  385. return err
  386. }
  387. n.generic[netlabel.GenericData] = lmap
  388. }
  389. }
  390. if v, ok := netMap["persist"]; ok {
  391. n.persist = v.(bool)
  392. }
  393. if v, ok := netMap["postIPv6"]; ok {
  394. n.postIPv6 = v.(bool)
  395. }
  396. if v, ok := netMap["ipamType"]; ok {
  397. n.ipamType = v.(string)
  398. } else {
  399. n.ipamType = ipamapi.DefaultIPAM
  400. }
  401. if v, ok := netMap["addrSpace"]; ok {
  402. n.addrSpace = v.(string)
  403. }
  404. if v, ok := netMap["ipamV4Config"]; ok {
  405. if err := json.Unmarshal([]byte(v.(string)), &n.ipamV4Config); err != nil {
  406. return err
  407. }
  408. }
  409. if v, ok := netMap["ipamV4Info"]; ok {
  410. if err := json.Unmarshal([]byte(v.(string)), &n.ipamV4Info); err != nil {
  411. return err
  412. }
  413. }
  414. if v, ok := netMap["ipamV6Config"]; ok {
  415. if err := json.Unmarshal([]byte(v.(string)), &n.ipamV6Config); err != nil {
  416. return err
  417. }
  418. }
  419. if v, ok := netMap["ipamV6Info"]; ok {
  420. if err := json.Unmarshal([]byte(v.(string)), &n.ipamV6Info); err != nil {
  421. return err
  422. }
  423. }
  424. if v, ok := netMap["internal"]; ok {
  425. n.internal = v.(bool)
  426. }
  427. if s, ok := netMap["scope"]; ok {
  428. n.scope = s.(string)
  429. }
  430. if v, ok := netMap["inDelete"]; ok {
  431. n.inDelete = v.(bool)
  432. }
  433. // Reconcile old networks with the recently added `--ipv6` flag
  434. if !n.enableIPv6 {
  435. n.enableIPv6 = len(n.ipamV6Info) > 0
  436. }
  437. return nil
  438. }
  439. // NetworkOption is an option setter function type used to pass various options to
  440. // NewNetwork method. The various setter functions of type NetworkOption are
  441. // provided by libnetwork, they look like NetworkOptionXXXX(...)
  442. type NetworkOption func(n *network)
  443. // NetworkOptionGeneric function returns an option setter for a Generic option defined
  444. // in a Dictionary of Key-Value pair
  445. func NetworkOptionGeneric(generic map[string]interface{}) NetworkOption {
  446. return func(n *network) {
  447. if n.generic == nil {
  448. n.generic = make(map[string]interface{})
  449. }
  450. if val, ok := generic[netlabel.EnableIPv6]; ok {
  451. n.enableIPv6 = val.(bool)
  452. }
  453. if val, ok := generic[netlabel.Internal]; ok {
  454. n.internal = val.(bool)
  455. }
  456. for k, v := range generic {
  457. n.generic[k] = v
  458. }
  459. }
  460. }
  461. // NetworkOptionPersist returns an option setter to set persistence policy for a network
  462. func NetworkOptionPersist(persist bool) NetworkOption {
  463. return func(n *network) {
  464. n.persist = persist
  465. }
  466. }
  467. // NetworkOptionEnableIPv6 returns an option setter to explicitly configure IPv6
  468. func NetworkOptionEnableIPv6(enableIPv6 bool) NetworkOption {
  469. return func(n *network) {
  470. if n.generic == nil {
  471. n.generic = make(map[string]interface{})
  472. }
  473. n.enableIPv6 = enableIPv6
  474. n.generic[netlabel.EnableIPv6] = enableIPv6
  475. }
  476. }
  477. // NetworkOptionInternalNetwork returns an option setter to config the network
  478. // to be internal which disables default gateway service
  479. func NetworkOptionInternalNetwork() NetworkOption {
  480. return func(n *network) {
  481. if n.generic == nil {
  482. n.generic = make(map[string]interface{})
  483. }
  484. n.internal = true
  485. n.generic[netlabel.Internal] = true
  486. }
  487. }
  488. // NetworkOptionIpam function returns an option setter for the ipam configuration for this network
  489. func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption {
  490. return func(n *network) {
  491. if ipamDriver != "" {
  492. n.ipamType = ipamDriver
  493. }
  494. n.ipamOptions = opts
  495. n.addrSpace = addrSpace
  496. n.ipamV4Config = ipV4
  497. n.ipamV6Config = ipV6
  498. }
  499. }
  500. // NetworkOptionDriverOpts function returns an option setter for any driver parameter described by a map
  501. func NetworkOptionDriverOpts(opts map[string]string) NetworkOption {
  502. return func(n *network) {
  503. if n.generic == nil {
  504. n.generic = make(map[string]interface{})
  505. }
  506. if opts == nil {
  507. opts = make(map[string]string)
  508. }
  509. // Store the options
  510. n.generic[netlabel.GenericData] = opts
  511. }
  512. }
  513. // NetworkOptionLabels function returns an option setter for labels specific to a network
  514. func NetworkOptionLabels(labels map[string]string) NetworkOption {
  515. return func(n *network) {
  516. n.labels = labels
  517. }
  518. }
  519. // NetworkOptionDeferIPv6Alloc instructs the network to defer the IPV6 address allocation until after the endpoint has been created
  520. // It is being provided to support the specific docker daemon flags where user can deterministically assign an IPv6 address
  521. // to a container as combination of fixed-cidr-v6 + mac-address
  522. // TODO: Remove this option setter once we support endpoint ipam options
  523. func NetworkOptionDeferIPv6Alloc(enable bool) NetworkOption {
  524. return func(n *network) {
  525. n.postIPv6 = enable
  526. }
  527. }
  528. func (n *network) processOptions(options ...NetworkOption) {
  529. for _, opt := range options {
  530. if opt != nil {
  531. opt(n)
  532. }
  533. }
  534. }
  535. func (n *network) driverScope() string {
  536. c := n.getController()
  537. c.Lock()
  538. // Check if a driver for the specified network type is available
  539. dd, ok := c.drivers[n.networkType]
  540. c.Unlock()
  541. if !ok {
  542. var err error
  543. dd, err = c.loadDriver(n.networkType)
  544. if err != nil {
  545. // If driver could not be resolved simply return an empty string
  546. return ""
  547. }
  548. }
  549. return dd.capability.DataScope
  550. }
  551. func (n *network) driver(load bool) (driverapi.Driver, error) {
  552. c := n.getController()
  553. c.Lock()
  554. // Check if a driver for the specified network type is available
  555. dd, ok := c.drivers[n.networkType]
  556. c.Unlock()
  557. if !ok && load {
  558. var err error
  559. dd, err = c.loadDriver(n.networkType)
  560. if err != nil {
  561. return nil, err
  562. }
  563. } else if !ok {
  564. // don't fail if driver loading is not required
  565. return nil, nil
  566. }
  567. n.Lock()
  568. n.scope = dd.capability.DataScope
  569. n.Unlock()
  570. return dd.driver, nil
  571. }
  572. func (n *network) Delete() error {
  573. return n.delete(false)
  574. }
  575. func (n *network) delete(force bool) error {
  576. n.Lock()
  577. c := n.ctrlr
  578. name := n.name
  579. id := n.id
  580. n.Unlock()
  581. n, err := c.getNetworkFromStore(id)
  582. if err != nil {
  583. return &UnknownNetworkError{name: name, id: id}
  584. }
  585. if !force && n.getEpCnt().EndpointCnt() != 0 {
  586. return &ActiveEndpointsError{name: n.name, id: n.id}
  587. }
  588. // Mark the network for deletion
  589. n.inDelete = true
  590. if err = c.updateToStore(n); err != nil {
  591. return fmt.Errorf("error marking network %s (%s) for deletion: %v", n.Name(), n.ID(), err)
  592. }
  593. if err = n.deleteNetwork(); err != nil {
  594. if !force {
  595. return err
  596. }
  597. log.Debugf("driver failed to delete stale network %s (%s): %v", n.Name(), n.ID(), err)
  598. }
  599. n.ipamRelease()
  600. if err = c.updateToStore(n); err != nil {
  601. log.Warnf("Failed to update store after ipam release for network %s (%s): %v", n.Name(), n.ID(), err)
  602. }
  603. // deleteFromStore performs an atomic delete operation and the
  604. // network.epCnt will help prevent any possible
  605. // race between endpoint join and network delete
  606. if err = c.deleteFromStore(n.getEpCnt()); err != nil {
  607. if !force {
  608. return fmt.Errorf("error deleting network endpoint count from store: %v", err)
  609. }
  610. log.Debugf("Error deleting endpoint count from store for stale network %s (%s) for deletion: %v", n.Name(), n.ID(), err)
  611. }
  612. if err = c.deleteFromStore(n); err != nil {
  613. return fmt.Errorf("error deleting network from store: %v", err)
  614. }
  615. return nil
  616. }
  617. func (n *network) deleteNetwork() error {
  618. d, err := n.driver(true)
  619. if err != nil {
  620. return fmt.Errorf("failed deleting network: %v", err)
  621. }
  622. if err := d.DeleteNetwork(n.ID()); err != nil {
  623. // Forbidden Errors should be honored
  624. if _, ok := err.(types.ForbiddenError); ok {
  625. return err
  626. }
  627. if _, ok := err.(types.MaskableError); !ok {
  628. log.Warnf("driver error deleting network %s : %v", n.name, err)
  629. }
  630. }
  631. return nil
  632. }
  633. func (n *network) addEndpoint(ep *endpoint) error {
  634. d, err := n.driver(true)
  635. if err != nil {
  636. return fmt.Errorf("failed to add endpoint: %v", err)
  637. }
  638. err = d.CreateEndpoint(n.id, ep.id, ep.Interface(), ep.generic)
  639. if err != nil {
  640. return types.InternalErrorf("failed to create endpoint %s on network %s: %v",
  641. ep.Name(), n.Name(), err)
  642. }
  643. return nil
  644. }
  645. func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
  646. var err error
  647. if !config.IsValidName(name) {
  648. return nil, ErrInvalidName(name)
  649. }
  650. if _, err = n.EndpointByName(name); err == nil {
  651. return nil, types.ForbiddenErrorf("service endpoint with name %s already exists", name)
  652. }
  653. ep := &endpoint{name: name, generic: make(map[string]interface{}), iface: &endpointInterface{}}
  654. ep.id = stringid.GenerateRandomID()
  655. // Initialize ep.network with a possibly stale copy of n. We need this to get network from
  656. // store. But once we get it from store we will have the most uptodate copy possibly.
  657. ep.network = n
  658. ep.locator = n.getController().clusterHostID()
  659. ep.network, err = ep.getNetworkFromStore()
  660. if err != nil {
  661. return nil, fmt.Errorf("failed to get network during CreateEndpoint: %v", err)
  662. }
  663. n = ep.network
  664. ep.processOptions(options...)
  665. if opt, ok := ep.generic[netlabel.MacAddress]; ok {
  666. if mac, ok := opt.(net.HardwareAddr); ok {
  667. ep.iface.mac = mac
  668. }
  669. }
  670. ipam, err := n.getController().getIPAM(n.ipamType)
  671. if err != nil {
  672. return nil, err
  673. }
  674. if ipam.capability.RequiresMACAddress {
  675. if ep.iface.mac == nil {
  676. ep.iface.mac = netutils.GenerateRandomMAC()
  677. }
  678. if ep.ipamOptions == nil {
  679. ep.ipamOptions = make(map[string]string)
  680. }
  681. ep.ipamOptions[netlabel.MacAddress] = ep.iface.mac.String()
  682. }
  683. if err = ep.assignAddress(ipam.driver, true, n.enableIPv6 && !n.postIPv6); err != nil {
  684. return nil, err
  685. }
  686. defer func() {
  687. if err != nil {
  688. ep.releaseAddress()
  689. }
  690. }()
  691. if err = n.addEndpoint(ep); err != nil {
  692. return nil, err
  693. }
  694. defer func() {
  695. if err != nil {
  696. if e := ep.deleteEndpoint(false); e != nil {
  697. log.Warnf("cleaning up endpoint failed %s : %v", name, e)
  698. }
  699. }
  700. }()
  701. if err = ep.assignAddress(ipam.driver, false, n.enableIPv6 && n.postIPv6); err != nil {
  702. return nil, err
  703. }
  704. if err = n.getController().updateToStore(ep); err != nil {
  705. return nil, err
  706. }
  707. defer func() {
  708. if err != nil {
  709. if e := n.getController().deleteFromStore(ep); e != nil {
  710. log.Warnf("error rolling back endpoint %s from store: %v", name, e)
  711. }
  712. }
  713. }()
  714. // Watch for service records
  715. n.getController().watchSvcRecord(ep)
  716. defer func() {
  717. if err != nil {
  718. n.getController().unWatchSvcRecord(ep)
  719. }
  720. }()
  721. // Increment endpoint count to indicate completion of endpoint addition
  722. if err = n.getEpCnt().IncEndpointCnt(); err != nil {
  723. return nil, err
  724. }
  725. return ep, nil
  726. }
  727. func (n *network) Endpoints() []Endpoint {
  728. var list []Endpoint
  729. endpoints, err := n.getEndpointsFromStore()
  730. if err != nil {
  731. log.Error(err)
  732. }
  733. for _, ep := range endpoints {
  734. list = append(list, ep)
  735. }
  736. return list
  737. }
  738. func (n *network) WalkEndpoints(walker EndpointWalker) {
  739. for _, e := range n.Endpoints() {
  740. if walker(e) {
  741. return
  742. }
  743. }
  744. }
  745. func (n *network) EndpointByName(name string) (Endpoint, error) {
  746. if name == "" {
  747. return nil, ErrInvalidName(name)
  748. }
  749. var e Endpoint
  750. s := func(current Endpoint) bool {
  751. if current.Name() == name {
  752. e = current
  753. return true
  754. }
  755. return false
  756. }
  757. n.WalkEndpoints(s)
  758. if e == nil {
  759. return nil, ErrNoSuchEndpoint(name)
  760. }
  761. return e, nil
  762. }
  763. func (n *network) EndpointByID(id string) (Endpoint, error) {
  764. if id == "" {
  765. return nil, ErrInvalidID(id)
  766. }
  767. ep, err := n.getEndpointFromStore(id)
  768. if err != nil {
  769. return nil, ErrNoSuchEndpoint(id)
  770. }
  771. return ep, nil
  772. }
  773. func (n *network) updateSvcRecord(ep *endpoint, localEps []*endpoint, isAdd bool) {
  774. epName := ep.Name()
  775. if iface := ep.Iface(); iface.Address() != nil {
  776. myAliases := ep.MyAliases()
  777. if isAdd {
  778. // If anonymous endpoint has an alias use the first alias
  779. // for ip->name mapping. Not having the reverse mapping
  780. // breaks some apps
  781. if ep.isAnonymous() {
  782. if len(myAliases) > 0 {
  783. n.addSvcRecords(myAliases[0], iface.Address().IP, true)
  784. }
  785. } else {
  786. n.addSvcRecords(epName, iface.Address().IP, true)
  787. }
  788. for _, alias := range myAliases {
  789. n.addSvcRecords(alias, iface.Address().IP, false)
  790. }
  791. } else {
  792. if ep.isAnonymous() {
  793. if len(myAliases) > 0 {
  794. n.deleteSvcRecords(myAliases[0], iface.Address().IP, true)
  795. }
  796. } else {
  797. n.deleteSvcRecords(epName, iface.Address().IP, true)
  798. }
  799. for _, alias := range myAliases {
  800. n.deleteSvcRecords(alias, iface.Address().IP, false)
  801. }
  802. }
  803. }
  804. }
  805. func (n *network) addSvcRecords(name string, epIP net.IP, ipMapUpdate bool) {
  806. c := n.getController()
  807. c.Lock()
  808. defer c.Unlock()
  809. sr, ok := c.svcDb[n.ID()]
  810. if !ok {
  811. sr = svcInfo{
  812. svcMap: make(map[string][]net.IP),
  813. ipMap: make(map[string]string),
  814. }
  815. c.svcDb[n.ID()] = sr
  816. }
  817. if ipMapUpdate {
  818. reverseIP := netutils.ReverseIP(epIP.String())
  819. if _, ok := sr.ipMap[reverseIP]; !ok {
  820. sr.ipMap[reverseIP] = name
  821. }
  822. }
  823. ipList := sr.svcMap[name]
  824. for _, ip := range ipList {
  825. if ip.Equal(epIP) {
  826. return
  827. }
  828. }
  829. sr.svcMap[name] = append(sr.svcMap[name], epIP)
  830. }
  831. func (n *network) deleteSvcRecords(name string, epIP net.IP, ipMapUpdate bool) {
  832. c := n.getController()
  833. c.Lock()
  834. defer c.Unlock()
  835. sr, ok := c.svcDb[n.ID()]
  836. if !ok {
  837. return
  838. }
  839. if ipMapUpdate {
  840. delete(sr.ipMap, netutils.ReverseIP(epIP.String()))
  841. }
  842. ipList := sr.svcMap[name]
  843. for i, ip := range ipList {
  844. if ip.Equal(epIP) {
  845. ipList = append(ipList[:i], ipList[i+1:]...)
  846. break
  847. }
  848. }
  849. sr.svcMap[name] = ipList
  850. if len(ipList) == 0 {
  851. delete(sr.svcMap, name)
  852. }
  853. }
  854. func (n *network) getSvcRecords(ep *endpoint) []etchosts.Record {
  855. n.Lock()
  856. defer n.Unlock()
  857. var recs []etchosts.Record
  858. sr, _ := n.ctrlr.svcDb[n.id]
  859. for h, ip := range sr.svcMap {
  860. if ep != nil && strings.Split(h, ".")[0] == ep.Name() {
  861. continue
  862. }
  863. recs = append(recs, etchosts.Record{
  864. Hosts: h,
  865. IP: ip[0].String(),
  866. })
  867. }
  868. return recs
  869. }
  870. func (n *network) getController() *controller {
  871. n.Lock()
  872. defer n.Unlock()
  873. return n.ctrlr
  874. }
  875. func (n *network) ipamAllocate() error {
  876. // For now also exclude bridge from using new ipam
  877. if n.Type() == "host" || n.Type() == "null" {
  878. return nil
  879. }
  880. ipam, err := n.getController().getIpamDriver(n.ipamType)
  881. if err != nil {
  882. return err
  883. }
  884. if n.addrSpace == "" {
  885. if n.addrSpace, err = n.deriveAddressSpace(); err != nil {
  886. return err
  887. }
  888. }
  889. err = n.ipamAllocateVersion(4, ipam)
  890. if err != nil {
  891. return err
  892. }
  893. defer func() {
  894. if err != nil {
  895. n.ipamReleaseVersion(4, ipam)
  896. }
  897. }()
  898. if !n.enableIPv6 {
  899. return nil
  900. }
  901. return n.ipamAllocateVersion(6, ipam)
  902. }
  903. func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
  904. var (
  905. cfgList *[]*IpamConf
  906. infoList *[]*IpamInfo
  907. err error
  908. )
  909. switch ipVer {
  910. case 4:
  911. cfgList = &n.ipamV4Config
  912. infoList = &n.ipamV4Info
  913. case 6:
  914. cfgList = &n.ipamV6Config
  915. infoList = &n.ipamV6Info
  916. default:
  917. return types.InternalErrorf("incorrect ip version passed to ipam allocate: %d", ipVer)
  918. }
  919. if len(*cfgList) == 0 {
  920. if ipVer == 6 {
  921. return nil
  922. }
  923. *cfgList = []*IpamConf{{}}
  924. }
  925. *infoList = make([]*IpamInfo, len(*cfgList))
  926. log.Debugf("Allocating IPv%d pools for network %s (%s)", ipVer, n.Name(), n.ID())
  927. for i, cfg := range *cfgList {
  928. if err = cfg.Validate(); err != nil {
  929. return err
  930. }
  931. d := &IpamInfo{}
  932. (*infoList)[i] = d
  933. d.PoolID, d.Pool, d.Meta, err = ipam.RequestPool(n.addrSpace, cfg.PreferredPool, cfg.SubPool, n.ipamOptions, ipVer == 6)
  934. if err != nil {
  935. return err
  936. }
  937. defer func() {
  938. if err != nil {
  939. if err := ipam.ReleasePool(d.PoolID); err != nil {
  940. log.Warnf("Failed to release address pool %s after failure to create network %s (%s)", d.PoolID, n.Name(), n.ID())
  941. }
  942. }
  943. }()
  944. if gws, ok := d.Meta[netlabel.Gateway]; ok {
  945. if d.Gateway, err = types.ParseCIDR(gws); err != nil {
  946. return types.BadRequestErrorf("failed to parse gateway address (%v) returned by ipam driver: %v", gws, err)
  947. }
  948. }
  949. // If user requested a specific gateway, libnetwork will allocate it
  950. // irrespective of whether ipam driver returned a gateway already.
  951. // If none of the above is true, libnetwork will allocate one.
  952. if cfg.Gateway != "" || d.Gateway == nil {
  953. var gatewayOpts = map[string]string{
  954. ipamapi.RequestAddressType: netlabel.Gateway,
  955. }
  956. if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil {
  957. return types.InternalErrorf("failed to allocate gateway (%v): %v", cfg.Gateway, err)
  958. }
  959. }
  960. // Auxiliary addresses must be part of the master address pool
  961. // If they fall into the container addressable pool, libnetwork will reserve them
  962. if cfg.AuxAddresses != nil {
  963. var ip net.IP
  964. d.IPAMData.AuxAddresses = make(map[string]*net.IPNet, len(cfg.AuxAddresses))
  965. for k, v := range cfg.AuxAddresses {
  966. if ip = net.ParseIP(v); ip == nil {
  967. return types.BadRequestErrorf("non parsable secondary ip address (%s:%s) passed for network %s", k, v, n.Name())
  968. }
  969. if !d.Pool.Contains(ip) {
  970. return types.ForbiddenErrorf("auxilairy address: (%s:%s) must belong to the master pool: %s", k, v, d.Pool)
  971. }
  972. // Attempt reservation in the container addressable pool, silent the error if address does not belong to that pool
  973. if d.IPAMData.AuxAddresses[k], _, err = ipam.RequestAddress(d.PoolID, ip, nil); err != nil && err != ipamapi.ErrIPOutOfRange {
  974. return types.InternalErrorf("failed to allocate secondary ip address (%s:%s): %v", k, v, err)
  975. }
  976. }
  977. }
  978. }
  979. return nil
  980. }
  981. func (n *network) ipamRelease() {
  982. // For now exclude host and null
  983. if n.Type() == "host" || n.Type() == "null" {
  984. return
  985. }
  986. ipam, err := n.getController().getIpamDriver(n.ipamType)
  987. if err != nil {
  988. log.Warnf("Failed to retrieve ipam driver to release address pool(s) on delete of network %s (%s): %v", n.Name(), n.ID(), err)
  989. return
  990. }
  991. n.ipamReleaseVersion(4, ipam)
  992. n.ipamReleaseVersion(6, ipam)
  993. }
  994. func (n *network) ipamReleaseVersion(ipVer int, ipam ipamapi.Ipam) {
  995. var infoList *[]*IpamInfo
  996. switch ipVer {
  997. case 4:
  998. infoList = &n.ipamV4Info
  999. case 6:
  1000. infoList = &n.ipamV6Info
  1001. default:
  1002. log.Warnf("incorrect ip version passed to ipam release: %d", ipVer)
  1003. return
  1004. }
  1005. if len(*infoList) == 0 {
  1006. return
  1007. }
  1008. log.Debugf("releasing IPv%d pools from network %s (%s)", ipVer, n.Name(), n.ID())
  1009. for _, d := range *infoList {
  1010. if d.Gateway != nil {
  1011. if err := ipam.ReleaseAddress(d.PoolID, d.Gateway.IP); err != nil {
  1012. log.Warnf("Failed to release gateway ip address %s on delete of network %s (%s): %v", d.Gateway.IP, n.Name(), n.ID(), err)
  1013. }
  1014. }
  1015. if d.IPAMData.AuxAddresses != nil {
  1016. for k, nw := range d.IPAMData.AuxAddresses {
  1017. if d.Pool.Contains(nw.IP) {
  1018. if err := ipam.ReleaseAddress(d.PoolID, nw.IP); err != nil && err != ipamapi.ErrIPOutOfRange {
  1019. log.Warnf("Failed to release secondary ip address %s (%v) on delete of network %s (%s): %v", k, nw.IP, n.Name(), n.ID(), err)
  1020. }
  1021. }
  1022. }
  1023. }
  1024. if err := ipam.ReleasePool(d.PoolID); err != nil {
  1025. log.Warnf("Failed to release address pool %s on delete of network %s (%s): %v", d.PoolID, n.Name(), n.ID(), err)
  1026. }
  1027. }
  1028. *infoList = nil
  1029. }
  1030. func (n *network) getIPInfo(ipVer int) []*IpamInfo {
  1031. var info []*IpamInfo
  1032. switch ipVer {
  1033. case 4:
  1034. info = n.ipamV4Info
  1035. case 6:
  1036. info = n.ipamV6Info
  1037. default:
  1038. return nil
  1039. }
  1040. l := make([]*IpamInfo, 0, len(info))
  1041. n.Lock()
  1042. for _, d := range info {
  1043. l = append(l, d)
  1044. }
  1045. n.Unlock()
  1046. return l
  1047. }
  1048. func (n *network) getIPData(ipVer int) []driverapi.IPAMData {
  1049. var info []*IpamInfo
  1050. switch ipVer {
  1051. case 4:
  1052. info = n.ipamV4Info
  1053. case 6:
  1054. info = n.ipamV6Info
  1055. default:
  1056. return nil
  1057. }
  1058. l := make([]driverapi.IPAMData, 0, len(info))
  1059. n.Lock()
  1060. for _, d := range info {
  1061. l = append(l, d.IPAMData)
  1062. }
  1063. n.Unlock()
  1064. return l
  1065. }
  1066. func (n *network) deriveAddressSpace() (string, error) {
  1067. c := n.getController()
  1068. c.Lock()
  1069. ipd, ok := c.ipamDrivers[n.ipamType]
  1070. c.Unlock()
  1071. if !ok {
  1072. return "", types.NotFoundErrorf("could not find ipam driver %s to get default address space", n.ipamType)
  1073. }
  1074. if n.DataScope() == datastore.GlobalScope {
  1075. return ipd.defaultGlobalAddressSpace, nil
  1076. }
  1077. return ipd.defaultLocalAddressSpace, nil
  1078. }
  1079. func (n *network) Info() NetworkInfo {
  1080. return n
  1081. }
  1082. func (n *network) DriverOptions() map[string]string {
  1083. n.Lock()
  1084. defer n.Unlock()
  1085. if n.generic != nil {
  1086. if m, ok := n.generic[netlabel.GenericData]; ok {
  1087. return m.(map[string]string)
  1088. }
  1089. }
  1090. return map[string]string{}
  1091. }
  1092. func (n *network) Scope() string {
  1093. n.Lock()
  1094. defer n.Unlock()
  1095. return n.scope
  1096. }
  1097. func (n *network) IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf) {
  1098. n.Lock()
  1099. defer n.Unlock()
  1100. v4L := make([]*IpamConf, len(n.ipamV4Config))
  1101. v6L := make([]*IpamConf, len(n.ipamV6Config))
  1102. for i, c := range n.ipamV4Config {
  1103. cc := &IpamConf{}
  1104. c.CopyTo(cc)
  1105. v4L[i] = cc
  1106. }
  1107. for i, c := range n.ipamV6Config {
  1108. cc := &IpamConf{}
  1109. c.CopyTo(cc)
  1110. v6L[i] = cc
  1111. }
  1112. return n.ipamType, n.ipamOptions, v4L, v6L
  1113. }
  1114. func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) {
  1115. n.Lock()
  1116. defer n.Unlock()
  1117. v4Info := make([]*IpamInfo, len(n.ipamV4Info))
  1118. v6Info := make([]*IpamInfo, len(n.ipamV6Info))
  1119. for i, info := range n.ipamV4Info {
  1120. ic := &IpamInfo{}
  1121. info.CopyTo(ic)
  1122. v4Info[i] = ic
  1123. }
  1124. for i, info := range n.ipamV6Info {
  1125. ic := &IpamInfo{}
  1126. info.CopyTo(ic)
  1127. v6Info[i] = ic
  1128. }
  1129. return v4Info, v6Info
  1130. }
  1131. func (n *network) Internal() bool {
  1132. n.Lock()
  1133. defer n.Unlock()
  1134. return n.internal
  1135. }
  1136. func (n *network) IPv6Enabled() bool {
  1137. n.Lock()
  1138. defer n.Unlock()
  1139. return n.enableIPv6
  1140. }
  1141. func (n *network) Labels() map[string]string {
  1142. n.Lock()
  1143. defer n.Unlock()
  1144. var lbls = make(map[string]string, len(n.labels))
  1145. for k, v := range n.labels {
  1146. lbls[k] = v
  1147. }
  1148. return lbls
  1149. }