network.go 29 KB

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