bridge.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. package bridge
  2. import (
  3. "errors"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "github.com/docker/libnetwork/driverapi"
  9. "github.com/docker/libnetwork/ipallocator"
  10. "github.com/docker/libnetwork/netlabel"
  11. "github.com/docker/libnetwork/netutils"
  12. "github.com/docker/libnetwork/options"
  13. "github.com/docker/libnetwork/portmapper"
  14. "github.com/docker/libnetwork/sandbox"
  15. "github.com/docker/libnetwork/types"
  16. "github.com/vishvananda/netlink"
  17. )
  18. const (
  19. networkType = "bridge"
  20. vethPrefix = "veth"
  21. vethLen = 7
  22. containerVethPrefix = "eth"
  23. maxAllocatePortAttempts = 10
  24. ifaceID = 1
  25. )
  26. var (
  27. ipAllocator *ipallocator.IPAllocator
  28. portMapper *portmapper.PortMapper
  29. )
  30. // configuration info for the "bridge" driver.
  31. type configuration struct {
  32. EnableIPForwarding bool
  33. }
  34. // networkConfiguration for network specific configuration
  35. type networkConfiguration struct {
  36. BridgeName string
  37. AddressIPv4 *net.IPNet
  38. FixedCIDR *net.IPNet
  39. FixedCIDRv6 *net.IPNet
  40. EnableIPv6 bool
  41. EnableIPTables bool
  42. EnableIPMasquerade bool
  43. EnableICC bool
  44. Mtu int
  45. DefaultGatewayIPv4 net.IP
  46. DefaultGatewayIPv6 net.IP
  47. DefaultBindingIP net.IP
  48. AllowNonDefaultBridge bool
  49. EnableUserlandProxy bool
  50. }
  51. // endpointConfiguration represents the user specified configuration for the sandbox endpoint
  52. type endpointConfiguration struct {
  53. MacAddress net.HardwareAddr
  54. PortBindings []types.PortBinding
  55. ExposedPorts []types.TransportPort
  56. }
  57. // containerConfiguration represents the user specified configuration for a container
  58. type containerConfiguration struct {
  59. ParentEndpoints []string
  60. ChildEndpoints []string
  61. }
  62. type bridgeEndpoint struct {
  63. id types.UUID
  64. intf *sandbox.Interface
  65. macAddress net.HardwareAddr
  66. config *endpointConfiguration // User specified parameters
  67. containerConfig *containerConfiguration
  68. portMapping []types.PortBinding // Operation port bindings
  69. }
  70. type bridgeNetwork struct {
  71. id types.UUID
  72. bridge *bridgeInterface // The bridge's L3 interface
  73. config *networkConfiguration
  74. endpoints map[types.UUID]*bridgeEndpoint // key: endpoint id
  75. sync.Mutex
  76. }
  77. type driver struct {
  78. config *configuration
  79. network *bridgeNetwork
  80. networks map[types.UUID]*bridgeNetwork
  81. sync.Mutex
  82. }
  83. func init() {
  84. ipAllocator = ipallocator.New()
  85. portMapper = portmapper.New()
  86. }
  87. // New constructs a new bridge driver
  88. func newDriver() driverapi.Driver {
  89. return &driver{networks: map[types.UUID]*bridgeNetwork{}}
  90. }
  91. // Init registers a new instance of bridge driver
  92. func Init(dc driverapi.DriverCallback) error {
  93. return dc.RegisterDriver(networkType, newDriver())
  94. }
  95. // Validate performs a static validation on the network configuration parameters.
  96. // Whatever can be assessed a priori before attempting any programming.
  97. func (c *networkConfiguration) Validate() error {
  98. if c.Mtu < 0 {
  99. return ErrInvalidMtu(c.Mtu)
  100. }
  101. // If bridge v4 subnet is specified
  102. if c.AddressIPv4 != nil {
  103. // If Container restricted subnet is specified, it must be a subset of bridge subnet
  104. if c.FixedCIDR != nil {
  105. // Check Network address
  106. if !c.AddressIPv4.Contains(c.FixedCIDR.IP) {
  107. return &ErrInvalidContainerSubnet{}
  108. }
  109. // Check it is effectively a subset
  110. brNetLen, _ := c.AddressIPv4.Mask.Size()
  111. cnNetLen, _ := c.FixedCIDR.Mask.Size()
  112. if brNetLen > cnNetLen {
  113. return &ErrInvalidContainerSubnet{}
  114. }
  115. }
  116. // If default gw is specified, it must be part of bridge subnet
  117. if c.DefaultGatewayIPv4 != nil {
  118. if !c.AddressIPv4.Contains(c.DefaultGatewayIPv4) {
  119. return &ErrInvalidGateway{}
  120. }
  121. }
  122. }
  123. // If default v6 gw is specified, FixedCIDRv6 must be specified and gw must belong to FixedCIDRv6 subnet
  124. if c.EnableIPv6 && c.DefaultGatewayIPv6 != nil {
  125. if c.FixedCIDRv6 == nil || !c.FixedCIDRv6.Contains(c.DefaultGatewayIPv6) {
  126. return &ErrInvalidGateway{}
  127. }
  128. }
  129. return nil
  130. }
  131. // Conflict check if two NetworkConfiguration objects overlap in the multinetwork
  132. func (c *networkConfiguration) Conflict(o *networkConfiguration) bool {
  133. if o == nil {
  134. return false
  135. }
  136. // Also empty, becasue only one network with empty name is allowed
  137. if c.BridgeName == o.BridgeName {
  138. return true
  139. }
  140. // They must be in different subnets
  141. if (c.AddressIPv4 != nil && o.AddressIPv4.IP != nil) &&
  142. (c.AddressIPv4.Contains(o.AddressIPv4.IP) || o.AddressIPv4.Contains(c.AddressIPv4.IP)) {
  143. return true
  144. }
  145. return false
  146. }
  147. // FromMap retrieve the configuration data from the map form.
  148. func (c *networkConfiguration) FromMap(data map[string]interface{}) error {
  149. var err error
  150. if i, ok := data["BridgeName"]; ok && i != nil {
  151. if c.BridgeName, ok = i.(string); !ok {
  152. return types.BadRequestErrorf("invalid type for BridgeName value")
  153. }
  154. }
  155. if i, ok := data["Mtu"]; ok && i != nil {
  156. if s, ok := i.(string); ok {
  157. if c.Mtu, err = strconv.Atoi(s); err != nil {
  158. return types.BadRequestErrorf("failed to parse Mtu value: %s", err.Error())
  159. }
  160. } else {
  161. return types.BadRequestErrorf("invalid type for Mtu value")
  162. }
  163. }
  164. if i, ok := data["EnableIPv6"]; ok && i != nil {
  165. if s, ok := i.(string); ok {
  166. if c.EnableIPv6, err = strconv.ParseBool(s); err != nil {
  167. return types.BadRequestErrorf("failed to parse EnableIPv6 value: %s", err.Error())
  168. }
  169. } else {
  170. return types.BadRequestErrorf("invalid type for EnableIPv6 value")
  171. }
  172. }
  173. if i, ok := data["EnableIPTables"]; ok && i != nil {
  174. if s, ok := i.(string); ok {
  175. if c.EnableIPTables, err = strconv.ParseBool(s); err != nil {
  176. return types.BadRequestErrorf("failed to parse EnableIPTables value: %s", err.Error())
  177. }
  178. } else {
  179. return types.BadRequestErrorf("invalid type for EnableIPTables value")
  180. }
  181. }
  182. if i, ok := data["EnableIPMasquerade"]; ok && i != nil {
  183. if s, ok := i.(string); ok {
  184. if c.EnableIPMasquerade, err = strconv.ParseBool(s); err != nil {
  185. return types.BadRequestErrorf("failed to parse EnableIPMasquerade value: %s", err.Error())
  186. }
  187. } else {
  188. return types.BadRequestErrorf("invalid type for EnableIPMasquerade value")
  189. }
  190. }
  191. if i, ok := data["EnableICC"]; ok && i != nil {
  192. if s, ok := i.(string); ok {
  193. if c.EnableICC, err = strconv.ParseBool(s); err != nil {
  194. return types.BadRequestErrorf("failed to parse EnableICC value: %s", err.Error())
  195. }
  196. } else {
  197. return types.BadRequestErrorf("invalid type for EnableICC value")
  198. }
  199. }
  200. if i, ok := data["AllowNonDefaultBridge"]; ok && i != nil {
  201. if s, ok := i.(string); ok {
  202. if c.AllowNonDefaultBridge, err = strconv.ParseBool(s); err != nil {
  203. return types.BadRequestErrorf("failed to parse AllowNonDefaultBridge value: %s", err.Error())
  204. }
  205. } else {
  206. return types.BadRequestErrorf("invalid type for AllowNonDefaultBridge value")
  207. }
  208. }
  209. if i, ok := data["AddressIPv4"]; ok && i != nil {
  210. if s, ok := i.(string); ok {
  211. if ip, nw, e := net.ParseCIDR(s); e == nil {
  212. nw.IP = ip
  213. c.AddressIPv4 = nw
  214. } else {
  215. return types.BadRequestErrorf("failed to parse AddressIPv4 value")
  216. }
  217. } else {
  218. return types.BadRequestErrorf("invalid type for AddressIPv4 value")
  219. }
  220. }
  221. if i, ok := data["FixedCIDR"]; ok && i != nil {
  222. if s, ok := i.(string); ok {
  223. if ip, nw, e := net.ParseCIDR(s); e == nil {
  224. nw.IP = ip
  225. c.FixedCIDR = nw
  226. } else {
  227. return types.BadRequestErrorf("failed to parse FixedCIDR value")
  228. }
  229. } else {
  230. return types.BadRequestErrorf("invalid type for FixedCIDR value")
  231. }
  232. }
  233. if i, ok := data["FixedCIDRv6"]; ok && i != nil {
  234. if s, ok := i.(string); ok {
  235. if ip, nw, e := net.ParseCIDR(s); e == nil {
  236. nw.IP = ip
  237. c.FixedCIDRv6 = nw
  238. } else {
  239. return types.BadRequestErrorf("failed to parse FixedCIDRv6 value")
  240. }
  241. } else {
  242. return types.BadRequestErrorf("invalid type for FixedCIDRv6 value")
  243. }
  244. }
  245. if i, ok := data["DefaultGatewayIPv4"]; ok && i != nil {
  246. if s, ok := i.(string); ok {
  247. if c.DefaultGatewayIPv4 = net.ParseIP(s); c.DefaultGatewayIPv4 == nil {
  248. return types.BadRequestErrorf("failed to parse DefaultGatewayIPv4 value")
  249. }
  250. } else {
  251. return types.BadRequestErrorf("invalid type for DefaultGatewayIPv4 value")
  252. }
  253. }
  254. if i, ok := data["DefaultGatewayIPv6"]; ok && i != nil {
  255. if s, ok := i.(string); ok {
  256. if c.DefaultGatewayIPv6 = net.ParseIP(s); c.DefaultGatewayIPv6 == nil {
  257. return types.BadRequestErrorf("failed to parse DefaultGatewayIPv6 value")
  258. }
  259. } else {
  260. return types.BadRequestErrorf("invalid type for DefaultGatewayIPv6 value")
  261. }
  262. }
  263. if i, ok := data["DefaultBindingIP"]; ok && i != nil {
  264. if s, ok := i.(string); ok {
  265. if c.DefaultBindingIP = net.ParseIP(s); c.DefaultBindingIP == nil {
  266. return types.BadRequestErrorf("failed to parse DefaultBindingIP value")
  267. }
  268. } else {
  269. return types.BadRequestErrorf("invalid type for DefaultBindingIP value")
  270. }
  271. }
  272. return nil
  273. }
  274. func (n *bridgeNetwork) getEndpoint(eid types.UUID) (*bridgeEndpoint, error) {
  275. n.Lock()
  276. defer n.Unlock()
  277. if eid == "" {
  278. return nil, InvalidEndpointIDError(eid)
  279. }
  280. if ep, ok := n.endpoints[eid]; ok {
  281. return ep, nil
  282. }
  283. return nil, nil
  284. }
  285. func (d *driver) Config(option map[string]interface{}) error {
  286. var config *configuration
  287. d.Lock()
  288. defer d.Unlock()
  289. if d.config != nil {
  290. return &ErrConfigExists{}
  291. }
  292. genericData, ok := option[netlabel.GenericData]
  293. if ok && genericData != nil {
  294. switch opt := genericData.(type) {
  295. case options.Generic:
  296. opaqueConfig, err := options.GenerateFromModel(opt, &configuration{})
  297. if err != nil {
  298. return err
  299. }
  300. config = opaqueConfig.(*configuration)
  301. case *configuration:
  302. config = opt
  303. default:
  304. return &ErrInvalidDriverConfig{}
  305. }
  306. d.config = config
  307. } else {
  308. config = &configuration{}
  309. }
  310. if config.EnableIPForwarding {
  311. return setupIPForwarding(config)
  312. }
  313. return nil
  314. }
  315. func (d *driver) getNetwork(id types.UUID) (*bridgeNetwork, error) {
  316. d.Lock()
  317. defer d.Unlock()
  318. if id == "" {
  319. return nil, types.BadRequestErrorf("invalid network id: %s", id)
  320. }
  321. if nw, ok := d.networks[id]; ok {
  322. return nw, nil
  323. }
  324. return nil, nil
  325. }
  326. func parseNetworkGenericOptions(data interface{}) (*networkConfiguration, error) {
  327. var (
  328. err error
  329. config *networkConfiguration
  330. )
  331. switch opt := data.(type) {
  332. case *networkConfiguration:
  333. config = opt
  334. case map[string]interface{}:
  335. config = &networkConfiguration{}
  336. err = config.FromMap(opt)
  337. case options.Generic:
  338. var opaqueConfig interface{}
  339. if opaqueConfig, err = options.GenerateFromModel(opt, config); err == nil {
  340. config = opaqueConfig.(*networkConfiguration)
  341. }
  342. default:
  343. err = types.BadRequestErrorf("do not recognize network configuration format: %T", opt)
  344. }
  345. return config, err
  346. }
  347. func parseNetworkOptions(option options.Generic) (*networkConfiguration, error) {
  348. var err error
  349. config := &networkConfiguration{}
  350. // Parse generic label first, config will be re-assigned
  351. if genData, ok := option[netlabel.GenericData]; ok && genData != nil {
  352. if config, err = parseNetworkGenericOptions(genData); err != nil {
  353. return nil, err
  354. }
  355. }
  356. // Process well-known labels next
  357. if _, ok := option[netlabel.EnableIPv6]; ok {
  358. config.EnableIPv6 = option[netlabel.EnableIPv6].(bool)
  359. }
  360. // Finally validate the configuration
  361. if err = config.Validate(); err != nil {
  362. return nil, err
  363. }
  364. return config, nil
  365. }
  366. // Create a new network using bridge plugin
  367. func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) error {
  368. var err error
  369. // Driver must be configured
  370. d.Lock()
  371. // Sanity checks
  372. if _, ok := d.networks[id]; ok {
  373. d.Unlock()
  374. return types.ForbiddenErrorf("network %s exists", id)
  375. }
  376. // Parse and validate the config. It should not conflict with existing networks' config
  377. config, err := parseNetworkOptions(option)
  378. if err != nil {
  379. d.Unlock()
  380. return err
  381. }
  382. for _, nw := range d.networks {
  383. if nw.config.Conflict(config) {
  384. d.Unlock()
  385. return types.ForbiddenErrorf("conflicts with network %s (%s)", nw.id, nw.config.BridgeName)
  386. }
  387. }
  388. // Create and set network handler in driver
  389. network := &bridgeNetwork{id: id, endpoints: make(map[types.UUID]*bridgeEndpoint), config: config}
  390. d.networks[id] = network
  391. d.Unlock()
  392. // On failure make sure to reset driver network handler to nil
  393. defer func() {
  394. if err != nil {
  395. d.Lock()
  396. delete(d.networks, id)
  397. d.Unlock()
  398. }
  399. }()
  400. // Create or retrieve the bridge L3 interface
  401. bridgeIface := newInterface(config)
  402. network.bridge = bridgeIface
  403. // Verify network does not conflict with previously configured networks
  404. // on parameters that were chosen by the driver.
  405. d.Lock()
  406. for _, nw := range d.networks {
  407. if nw.id == id {
  408. continue
  409. }
  410. // Verify the name (which may have been set by newInterface()) does not conflict with
  411. // existing bridge interfaces. Ironically the system chosen name gets stored in the config...
  412. // Basically we are checking if the two original configs were both empty.
  413. if nw.config.BridgeName == config.BridgeName {
  414. d.Unlock()
  415. return types.ForbiddenErrorf("conflicts with network %s (%s)", nw.id, nw.config.BridgeName)
  416. }
  417. // If this network config specifies the AddressIPv4, we need
  418. // to make sure it does not conflict with any previously allocated
  419. // bridges. This could not be completely caught by the config conflict
  420. // check, because networks which config does not specify the AddressIPv4
  421. // get their address and subnet selected by the driver (see electBridgeIPv4())
  422. if config.AddressIPv4 != nil {
  423. if nw.bridge.bridgeIPv4.Contains(config.AddressIPv4.IP) ||
  424. config.AddressIPv4.Contains(nw.bridge.bridgeIPv4.IP) {
  425. d.Unlock()
  426. return types.ForbiddenErrorf("conflicts with network %s (%s)", nw.id, nw.config.BridgeName)
  427. }
  428. }
  429. }
  430. d.Unlock()
  431. // Prepare the bridge setup configuration
  432. bridgeSetup := newBridgeSetup(config, bridgeIface)
  433. // If the bridge interface doesn't exist, we need to start the setup steps
  434. // by creating a new device and assigning it an IPv4 address.
  435. bridgeAlreadyExists := bridgeIface.exists()
  436. if !bridgeAlreadyExists {
  437. bridgeSetup.queueStep(setupDevice)
  438. }
  439. // Even if a bridge exists try to setup IPv4.
  440. bridgeSetup.queueStep(setupBridgeIPv4)
  441. // Conditionally queue setup steps depending on configuration values.
  442. for _, step := range []struct {
  443. Condition bool
  444. Fn setupStep
  445. }{
  446. // Enable IPv6 on the bridge if required. We do this even for a
  447. // previously existing bridge, as it may be here from a previous
  448. // installation where IPv6 wasn't supported yet and needs to be
  449. // assigned an IPv6 link-local address.
  450. {config.EnableIPv6, setupBridgeIPv6},
  451. // We ensure that the bridge has the expectedIPv4 and IPv6 addresses in
  452. // the case of a previously existing device.
  453. {bridgeAlreadyExists, setupVerifyAndReconcile},
  454. // Setup the bridge to allocate containers IPv4 addresses in the
  455. // specified subnet.
  456. {config.FixedCIDR != nil, setupFixedCIDRv4},
  457. // Setup the bridge to allocate containers global IPv6 addresses in the
  458. // specified subnet.
  459. {config.FixedCIDRv6 != nil, setupFixedCIDRv6},
  460. // Setup Loopback Adresses Routing
  461. {!config.EnableUserlandProxy, setupLoopbackAdressesRouting},
  462. // Setup IPTables.
  463. {config.EnableIPTables, setupIPTables},
  464. // Setup DefaultGatewayIPv4
  465. {config.DefaultGatewayIPv4 != nil, setupGatewayIPv4},
  466. // Setup DefaultGatewayIPv6
  467. {config.DefaultGatewayIPv6 != nil, setupGatewayIPv6},
  468. } {
  469. if step.Condition {
  470. bridgeSetup.queueStep(step.Fn)
  471. }
  472. }
  473. // Block bridge IP from being allocated.
  474. bridgeSetup.queueStep(allocateBridgeIP)
  475. // Apply the prepared list of steps, and abort at the first error.
  476. bridgeSetup.queueStep(setupDeviceUp)
  477. if err = bridgeSetup.apply(); err != nil {
  478. return err
  479. }
  480. return nil
  481. }
  482. func (d *driver) DeleteNetwork(nid types.UUID) error {
  483. var err error
  484. // Get network handler and remove it from driver
  485. d.Lock()
  486. n, ok := d.networks[nid]
  487. if !ok {
  488. d.Unlock()
  489. return types.InternalMaskableErrorf("network %s does not exist", nid)
  490. }
  491. delete(d.networks, nid)
  492. d.Unlock()
  493. // On failure set network handler back in driver, but
  494. // only if is not already taken over by some other thread
  495. defer func() {
  496. if err != nil {
  497. d.Lock()
  498. if _, ok := d.networks[nid]; !ok {
  499. d.networks[nid] = n
  500. }
  501. d.Unlock()
  502. }
  503. }()
  504. // Sanity check
  505. if n == nil {
  506. err = driverapi.ErrNoNetwork(nid)
  507. return err
  508. }
  509. // Cannot remove network if endpoints are still present
  510. if len(n.endpoints) != 0 {
  511. err = ActiveEndpointsError(n.id)
  512. return err
  513. }
  514. // Programming
  515. err = netlink.LinkDel(n.bridge.Link)
  516. return err
  517. }
  518. func (d *driver) CreateEndpoint(nid, eid types.UUID, epInfo driverapi.EndpointInfo, epOptions map[string]interface{}) error {
  519. var (
  520. ipv6Addr *net.IPNet
  521. err error
  522. )
  523. if epInfo == nil {
  524. return errors.New("invalid endpoint info passed")
  525. }
  526. if len(epInfo.Interfaces()) != 0 {
  527. return errors.New("non empty interface list passed to bridge(local) driver")
  528. }
  529. // Get the network handler and make sure it exists
  530. d.Lock()
  531. n, ok := d.networks[nid]
  532. if !ok {
  533. d.Unlock()
  534. return types.NotFoundErrorf("network %s does not exist", nid)
  535. }
  536. config := n.config
  537. d.Unlock()
  538. if n == nil {
  539. return driverapi.ErrNoNetwork(nid)
  540. }
  541. // Sanity check
  542. n.Lock()
  543. if n.id != nid {
  544. n.Unlock()
  545. return InvalidNetworkIDError(nid)
  546. }
  547. n.Unlock()
  548. // Check if endpoint id is good and retrieve correspondent endpoint
  549. ep, err := n.getEndpoint(eid)
  550. if err != nil {
  551. return err
  552. }
  553. // Endpoint with that id exists either on desired or other sandbox
  554. if ep != nil {
  555. return driverapi.ErrEndpointExists(eid)
  556. }
  557. // Try to convert the options to endpoint configuration
  558. epConfig, err := parseEndpointOptions(epOptions)
  559. if err != nil {
  560. return err
  561. }
  562. // Create and add the endpoint
  563. n.Lock()
  564. endpoint := &bridgeEndpoint{id: eid, config: epConfig}
  565. n.endpoints[eid] = endpoint
  566. n.Unlock()
  567. // On failure make sure to remove the endpoint
  568. defer func() {
  569. if err != nil {
  570. n.Lock()
  571. delete(n.endpoints, eid)
  572. n.Unlock()
  573. }
  574. }()
  575. // Generate a name for what will be the host side pipe interface
  576. name1, err := generateIfaceName()
  577. if err != nil {
  578. return err
  579. }
  580. // Generate a name for what will be the sandbox side pipe interface
  581. name2, err := generateIfaceName()
  582. if err != nil {
  583. return err
  584. }
  585. // Generate and add the interface pipe host <-> sandbox
  586. veth := &netlink.Veth{
  587. LinkAttrs: netlink.LinkAttrs{Name: name1, TxQLen: 0},
  588. PeerName: name2}
  589. if err = netlink.LinkAdd(veth); err != nil {
  590. return err
  591. }
  592. // Get the host side pipe interface handler
  593. host, err := netlink.LinkByName(name1)
  594. if err != nil {
  595. return err
  596. }
  597. defer func() {
  598. if err != nil {
  599. netlink.LinkDel(host)
  600. }
  601. }()
  602. // Get the sandbox side pipe interface handler
  603. sbox, err := netlink.LinkByName(name2)
  604. if err != nil {
  605. return err
  606. }
  607. defer func() {
  608. if err != nil {
  609. netlink.LinkDel(sbox)
  610. }
  611. }()
  612. // Set the sbox's MAC. If specified, use the one configured by user, otherwise use a random one
  613. mac := electMacAddress(epConfig)
  614. err = netlink.LinkSetHardwareAddr(sbox, mac)
  615. if err != nil {
  616. return err
  617. }
  618. endpoint.macAddress = mac
  619. // Add bridge inherited attributes to pipe interfaces
  620. if config.Mtu != 0 {
  621. err = netlink.LinkSetMTU(host, config.Mtu)
  622. if err != nil {
  623. return err
  624. }
  625. err = netlink.LinkSetMTU(sbox, config.Mtu)
  626. if err != nil {
  627. return err
  628. }
  629. }
  630. // Attach host side pipe interface into the bridge
  631. if err = netlink.LinkSetMaster(host,
  632. &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: config.BridgeName}}); err != nil {
  633. return err
  634. }
  635. // v4 address for the sandbox side pipe interface
  636. ip4, err := ipAllocator.RequestIP(n.bridge.bridgeIPv4, nil)
  637. if err != nil {
  638. return err
  639. }
  640. ipv4Addr := &net.IPNet{IP: ip4, Mask: n.bridge.bridgeIPv4.Mask}
  641. // v6 address for the sandbox side pipe interface
  642. ipv6Addr = &net.IPNet{}
  643. if config.EnableIPv6 {
  644. var ip6 net.IP
  645. network := n.bridge.bridgeIPv6
  646. if config.FixedCIDRv6 != nil {
  647. network = config.FixedCIDRv6
  648. }
  649. ones, _ := network.Mask.Size()
  650. if ones <= 80 {
  651. ip6 = make(net.IP, len(network.IP))
  652. copy(ip6, network.IP)
  653. for i, h := range mac {
  654. ip6[i+10] = h
  655. }
  656. }
  657. ip6, err := ipAllocator.RequestIP(network, ip6)
  658. if err != nil {
  659. return err
  660. }
  661. ipv6Addr = &net.IPNet{IP: ip6, Mask: network.Mask}
  662. }
  663. // Create the sandbox side pipe interface
  664. intf := &sandbox.Interface{}
  665. intf.SrcName = name2
  666. intf.DstName = containerVethPrefix
  667. intf.Address = ipv4Addr
  668. if config.EnableIPv6 {
  669. intf.AddressIPv6 = ipv6Addr
  670. }
  671. // Store the interface in endpoint, this is needed for cleanup on DeleteEndpoint()
  672. endpoint.intf = intf
  673. err = epInfo.AddInterface(ifaceID, endpoint.macAddress, *ipv4Addr, *ipv6Addr)
  674. if err != nil {
  675. return err
  676. }
  677. // Program any required port mapping and store them in the endpoint
  678. endpoint.portMapping, err = allocatePorts(epConfig, intf, config.DefaultBindingIP, config.EnableUserlandProxy)
  679. if err != nil {
  680. return err
  681. }
  682. return nil
  683. }
  684. func (d *driver) DeleteEndpoint(nid, eid types.UUID) error {
  685. var err error
  686. // Get the network handler and make sure it exists
  687. d.Lock()
  688. n, ok := d.networks[nid]
  689. if !ok {
  690. d.Unlock()
  691. return types.NotFoundErrorf("network %s does not exist", nid)
  692. }
  693. config := n.config
  694. d.Unlock()
  695. if n == nil {
  696. return driverapi.ErrNoNetwork(nid)
  697. }
  698. // Sanity Check
  699. n.Lock()
  700. if n.id != nid {
  701. n.Unlock()
  702. return InvalidNetworkIDError(nid)
  703. }
  704. n.Unlock()
  705. // Check endpoint id and if an endpoint is actually there
  706. ep, err := n.getEndpoint(eid)
  707. if err != nil {
  708. return err
  709. }
  710. if ep == nil {
  711. return EndpointNotFoundError(eid)
  712. }
  713. // Remove it
  714. n.Lock()
  715. delete(n.endpoints, eid)
  716. n.Unlock()
  717. // On failure make sure to set back ep in n.endpoints, but only
  718. // if it hasn't been taken over already by some other thread.
  719. defer func() {
  720. if err != nil {
  721. n.Lock()
  722. if _, ok := n.endpoints[eid]; !ok {
  723. n.endpoints[eid] = ep
  724. }
  725. n.Unlock()
  726. }
  727. }()
  728. // Remove port mappings. Do not stop endpoint delete on unmap failure
  729. releasePorts(ep)
  730. // Release the v4 address allocated to this endpoint's sandbox interface
  731. err = ipAllocator.ReleaseIP(n.bridge.bridgeIPv4, ep.intf.Address.IP)
  732. if err != nil {
  733. return err
  734. }
  735. // Release the v6 address allocated to this endpoint's sandbox interface
  736. if config.EnableIPv6 {
  737. err := ipAllocator.ReleaseIP(n.bridge.bridgeIPv6, ep.intf.AddressIPv6.IP)
  738. if err != nil {
  739. return err
  740. }
  741. }
  742. // Try removal of link. Discard error: link pair might have
  743. // already been deleted by sandbox delete.
  744. link, err := netlink.LinkByName(ep.intf.SrcName)
  745. if err == nil {
  746. netlink.LinkDel(link)
  747. }
  748. return nil
  749. }
  750. func (d *driver) EndpointOperInfo(nid, eid types.UUID) (map[string]interface{}, error) {
  751. // Get the network handler and make sure it exists
  752. d.Lock()
  753. n, ok := d.networks[nid]
  754. if !ok {
  755. d.Unlock()
  756. return nil, types.NotFoundErrorf("network %s does not exist", nid)
  757. }
  758. d.Unlock()
  759. if n == nil {
  760. return nil, driverapi.ErrNoNetwork(nid)
  761. }
  762. // Sanity check
  763. n.Lock()
  764. if n.id != nid {
  765. n.Unlock()
  766. return nil, InvalidNetworkIDError(nid)
  767. }
  768. n.Unlock()
  769. // Check if endpoint id is good and retrieve correspondent endpoint
  770. ep, err := n.getEndpoint(eid)
  771. if err != nil {
  772. return nil, err
  773. }
  774. if ep == nil {
  775. return nil, driverapi.ErrNoEndpoint(eid)
  776. }
  777. m := make(map[string]interface{})
  778. if ep.portMapping != nil {
  779. // Return a copy of the operational data
  780. pmc := make([]types.PortBinding, 0, len(ep.portMapping))
  781. for _, pm := range ep.portMapping {
  782. pmc = append(pmc, pm.GetCopy())
  783. }
  784. m[netlabel.PortMap] = pmc
  785. }
  786. if len(ep.macAddress) != 0 {
  787. m[netlabel.MacAddress] = ep.macAddress
  788. }
  789. return m, nil
  790. }
  791. // Join method is invoked when a Sandbox is attached to an endpoint.
  792. func (d *driver) Join(nid, eid types.UUID, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
  793. network, err := d.getNetwork(nid)
  794. if err != nil {
  795. return err
  796. }
  797. endpoint, err := network.getEndpoint(eid)
  798. if err != nil {
  799. return err
  800. }
  801. if endpoint == nil {
  802. return EndpointNotFoundError(eid)
  803. }
  804. for _, iNames := range jinfo.InterfaceNames() {
  805. // Make sure to set names on the correct interface ID.
  806. if iNames.ID() == ifaceID {
  807. err = iNames.SetNames(endpoint.intf.SrcName, endpoint.intf.DstName)
  808. if err != nil {
  809. return err
  810. }
  811. }
  812. }
  813. err = jinfo.SetGateway(network.bridge.gatewayIPv4)
  814. if err != nil {
  815. return err
  816. }
  817. err = jinfo.SetGatewayIPv6(network.bridge.gatewayIPv6)
  818. if err != nil {
  819. return err
  820. }
  821. if !network.config.EnableICC {
  822. return d.link(network, endpoint, options, true)
  823. }
  824. return nil
  825. }
  826. // Leave method is invoked when a Sandbox detaches from an endpoint.
  827. func (d *driver) Leave(nid, eid types.UUID) error {
  828. network, err := d.getNetwork(nid)
  829. if err != nil {
  830. return err
  831. }
  832. endpoint, err := network.getEndpoint(eid)
  833. if err != nil {
  834. return err
  835. }
  836. if endpoint == nil {
  837. return EndpointNotFoundError(eid)
  838. }
  839. if !network.config.EnableICC {
  840. return d.link(network, endpoint, nil, false)
  841. }
  842. return nil
  843. }
  844. func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, options map[string]interface{}, enable bool) error {
  845. var (
  846. cc *containerConfiguration
  847. err error
  848. )
  849. if enable {
  850. cc, err = parseContainerOptions(options)
  851. if err != nil {
  852. return err
  853. }
  854. } else {
  855. cc = endpoint.containerConfig
  856. }
  857. if cc == nil {
  858. return nil
  859. }
  860. if endpoint.config != nil && endpoint.config.ExposedPorts != nil {
  861. for _, p := range cc.ParentEndpoints {
  862. var parentEndpoint *bridgeEndpoint
  863. parentEndpoint, err = network.getEndpoint(types.UUID(p))
  864. if err != nil {
  865. return err
  866. }
  867. if parentEndpoint == nil {
  868. err = InvalidEndpointIDError(p)
  869. return err
  870. }
  871. l := newLink(parentEndpoint.intf.Address.IP.String(),
  872. endpoint.intf.Address.IP.String(),
  873. endpoint.config.ExposedPorts, network.config.BridgeName)
  874. if enable {
  875. err = l.Enable()
  876. if err != nil {
  877. return err
  878. }
  879. defer func() {
  880. if err != nil {
  881. l.Disable()
  882. }
  883. }()
  884. } else {
  885. l.Disable()
  886. }
  887. }
  888. }
  889. for _, c := range cc.ChildEndpoints {
  890. var childEndpoint *bridgeEndpoint
  891. childEndpoint, err = network.getEndpoint(types.UUID(c))
  892. if err != nil {
  893. return err
  894. }
  895. if childEndpoint == nil {
  896. err = InvalidEndpointIDError(c)
  897. return err
  898. }
  899. if childEndpoint.config == nil || childEndpoint.config.ExposedPorts == nil {
  900. continue
  901. }
  902. l := newLink(endpoint.intf.Address.IP.String(),
  903. childEndpoint.intf.Address.IP.String(),
  904. childEndpoint.config.ExposedPorts, network.config.BridgeName)
  905. if enable {
  906. err = l.Enable()
  907. if err != nil {
  908. return err
  909. }
  910. defer func() {
  911. if err != nil {
  912. l.Disable()
  913. }
  914. }()
  915. } else {
  916. l.Disable()
  917. }
  918. }
  919. if enable {
  920. endpoint.containerConfig = cc
  921. }
  922. return nil
  923. }
  924. func (d *driver) Type() string {
  925. return networkType
  926. }
  927. func parseEndpointOptions(epOptions map[string]interface{}) (*endpointConfiguration, error) {
  928. if epOptions == nil {
  929. return nil, nil
  930. }
  931. ec := &endpointConfiguration{}
  932. if opt, ok := epOptions[netlabel.MacAddress]; ok {
  933. if mac, ok := opt.(net.HardwareAddr); ok {
  934. ec.MacAddress = mac
  935. } else {
  936. return nil, &ErrInvalidEndpointConfig{}
  937. }
  938. }
  939. if opt, ok := epOptions[netlabel.PortMap]; ok {
  940. if bs, ok := opt.([]types.PortBinding); ok {
  941. ec.PortBindings = bs
  942. } else {
  943. return nil, &ErrInvalidEndpointConfig{}
  944. }
  945. }
  946. if opt, ok := epOptions[netlabel.ExposedPorts]; ok {
  947. if ports, ok := opt.([]types.TransportPort); ok {
  948. ec.ExposedPorts = ports
  949. } else {
  950. return nil, &ErrInvalidEndpointConfig{}
  951. }
  952. }
  953. return ec, nil
  954. }
  955. func parseContainerOptions(cOptions map[string]interface{}) (*containerConfiguration, error) {
  956. if cOptions == nil {
  957. return nil, nil
  958. }
  959. genericData := cOptions[netlabel.GenericData]
  960. if genericData == nil {
  961. return nil, nil
  962. }
  963. switch opt := genericData.(type) {
  964. case options.Generic:
  965. opaqueConfig, err := options.GenerateFromModel(opt, &containerConfiguration{})
  966. if err != nil {
  967. return nil, err
  968. }
  969. return opaqueConfig.(*containerConfiguration), nil
  970. case *containerConfiguration:
  971. return opt, nil
  972. default:
  973. return nil, nil
  974. }
  975. }
  976. func electMacAddress(epConfig *endpointConfiguration) net.HardwareAddr {
  977. if epConfig != nil && epConfig.MacAddress != nil {
  978. return epConfig.MacAddress
  979. }
  980. return netutils.GenerateRandomMAC()
  981. }
  982. // Generates a name to be used for a virtual ethernet
  983. // interface. The name is constructed by 'veth' appended
  984. // by a randomly generated hex value. (example: veth0f60e2c)
  985. func generateIfaceName() (string, error) {
  986. for i := 0; i < 3; i++ {
  987. name, err := netutils.GenerateRandomName(vethPrefix, vethLen)
  988. if err != nil {
  989. continue
  990. }
  991. if _, err := net.InterfaceByName(name); err != nil {
  992. if strings.Contains(err.Error(), "no such") {
  993. return name, nil
  994. }
  995. return "", err
  996. }
  997. }
  998. return "", &ErrIfaceName{}
  999. }