overlay.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package overlay
  2. //go:generate protoc -I.:../../Godeps/_workspace/src/github.com/gogo/protobuf --gogo_out=import_path=github.com/docker/libnetwork/drivers/overlay,Mgogoproto/gogo.proto=github.com/gogo/protobuf/gogoproto:. overlay.proto
  3. import (
  4. "fmt"
  5. "net"
  6. "sync"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/libnetwork/datastore"
  9. "github.com/docker/libnetwork/discoverapi"
  10. "github.com/docker/libnetwork/driverapi"
  11. "github.com/docker/libnetwork/idm"
  12. "github.com/docker/libnetwork/netlabel"
  13. "github.com/docker/libnetwork/osl"
  14. "github.com/docker/libnetwork/types"
  15. "github.com/hashicorp/serf/serf"
  16. )
  17. const (
  18. networkType = "overlay"
  19. vethPrefix = "veth"
  20. vethLen = 7
  21. vxlanIDStart = 256
  22. vxlanIDEnd = (1 << 24) - 1
  23. vxlanPort = 4789
  24. vxlanEncap = 50
  25. secureOption = "encrypted"
  26. )
  27. var initVxlanIdm = make(chan (bool), 1)
  28. type driver struct {
  29. eventCh chan serf.Event
  30. notifyCh chan ovNotify
  31. exitCh chan chan struct{}
  32. bindAddress string
  33. advertiseAddress string
  34. neighIP string
  35. config map[string]interface{}
  36. peerDb peerNetworkMap
  37. secMap *encrMap
  38. serfInstance *serf.Serf
  39. networks networkTable
  40. store datastore.DataStore
  41. localStore datastore.DataStore
  42. vxlanIdm *idm.Idm
  43. once sync.Once
  44. joinOnce sync.Once
  45. keys []*key
  46. sync.Mutex
  47. }
  48. // Init registers a new instance of overlay driver
  49. func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
  50. c := driverapi.Capability{
  51. DataScope: datastore.GlobalScope,
  52. }
  53. d := &driver{
  54. networks: networkTable{},
  55. peerDb: peerNetworkMap{
  56. mp: map[string]*peerMap{},
  57. },
  58. secMap: &encrMap{nodes: map[string][]*spi{}},
  59. config: config,
  60. }
  61. if data, ok := config[netlabel.GlobalKVClient]; ok {
  62. var err error
  63. dsc, ok := data.(discoverapi.DatastoreConfigData)
  64. if !ok {
  65. return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
  66. }
  67. d.store, err = datastore.NewDataStoreFromConfig(dsc)
  68. if err != nil {
  69. return types.InternalErrorf("failed to initialize data store: %v", err)
  70. }
  71. }
  72. if data, ok := config[netlabel.LocalKVClient]; ok {
  73. var err error
  74. dsc, ok := data.(discoverapi.DatastoreConfigData)
  75. if !ok {
  76. return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
  77. }
  78. d.localStore, err = datastore.NewDataStoreFromConfig(dsc)
  79. if err != nil {
  80. return types.InternalErrorf("failed to initialize local data store: %v", err)
  81. }
  82. }
  83. if err := d.restoreEndpoints(); err != nil {
  84. logrus.Warnf("Failure during overlay endpoints restore: %v", err)
  85. }
  86. // If an error happened when the network join the sandbox during the endpoints restore
  87. // we should reset it now along with the once variable, so that subsequent endpoint joins
  88. // outside of the restore path can potentially fix the network join and succeed.
  89. for nid, n := range d.networks {
  90. if n.initErr != nil {
  91. logrus.Infof("resetting init error and once variable for network %s after unsuccessful endpoint restore: %v", nid, n.initErr)
  92. n.initErr = nil
  93. n.once = &sync.Once{}
  94. }
  95. }
  96. return dc.RegisterDriver(networkType, d, c)
  97. }
  98. // Endpoints are stored in the local store. Restore them and reconstruct the overlay sandbox
  99. func (d *driver) restoreEndpoints() error {
  100. if d.localStore == nil {
  101. logrus.Warn("Cannot restore overlay endpoints because local datastore is missing")
  102. return nil
  103. }
  104. kvol, err := d.localStore.List(datastore.Key(overlayEndpointPrefix), &endpoint{})
  105. if err != nil && err != datastore.ErrKeyNotFound {
  106. return fmt.Errorf("failed to read overlay endpoint from store: %v", err)
  107. }
  108. if err == datastore.ErrKeyNotFound {
  109. return nil
  110. }
  111. for _, kvo := range kvol {
  112. ep := kvo.(*endpoint)
  113. n := d.network(ep.nid)
  114. if n == nil {
  115. logrus.Debugf("Network (%s) not found for restored endpoint (%s)", ep.nid[0:7], ep.id[0:7])
  116. logrus.Debugf("Deleting stale overlay endpoint (%s) from store", ep.id[0:7])
  117. if err := d.deleteEndpointFromStore(ep); err != nil {
  118. logrus.Debugf("Failed to delete stale overlay endpoint (%s) from store", ep.id[0:7])
  119. }
  120. continue
  121. }
  122. n.addEndpoint(ep)
  123. s := n.getSubnetforIP(ep.addr)
  124. if s == nil {
  125. return fmt.Errorf("could not find subnet for endpoint %s", ep.id)
  126. }
  127. if err := n.joinSandbox(true); err != nil {
  128. return fmt.Errorf("restore network sandbox failed: %v", err)
  129. }
  130. if err := n.joinSubnetSandbox(s, true); err != nil {
  131. return fmt.Errorf("restore subnet sandbox failed for %q: %v", s.subnetIP.String(), err)
  132. }
  133. Ifaces := make(map[string][]osl.IfaceOption)
  134. vethIfaceOption := make([]osl.IfaceOption, 1)
  135. vethIfaceOption = append(vethIfaceOption, n.sbox.InterfaceOptions().Master(s.brName))
  136. Ifaces[fmt.Sprintf("%s+%s", "veth", "veth")] = vethIfaceOption
  137. err := n.sbox.Restore(Ifaces, nil, nil, nil)
  138. if err != nil {
  139. return fmt.Errorf("failed to restore overlay sandbox: %v", err)
  140. }
  141. n.incEndpointCount()
  142. d.peerDbAdd(ep.nid, ep.id, ep.addr.IP, ep.addr.Mask, ep.mac, net.ParseIP(d.advertiseAddress), true)
  143. }
  144. return nil
  145. }
  146. // Fini cleans up the driver resources
  147. func Fini(drv driverapi.Driver) {
  148. d := drv.(*driver)
  149. if d.exitCh != nil {
  150. waitCh := make(chan struct{})
  151. d.exitCh <- waitCh
  152. <-waitCh
  153. }
  154. }
  155. func (d *driver) configure() error {
  156. if d.store == nil {
  157. return nil
  158. }
  159. if d.vxlanIdm == nil {
  160. return d.initializeVxlanIdm()
  161. }
  162. return nil
  163. }
  164. func (d *driver) initializeVxlanIdm() error {
  165. var err error
  166. initVxlanIdm <- true
  167. defer func() { <-initVxlanIdm }()
  168. if d.vxlanIdm != nil {
  169. return nil
  170. }
  171. d.vxlanIdm, err = idm.New(d.store, "vxlan-id", vxlanIDStart, vxlanIDEnd)
  172. if err != nil {
  173. return fmt.Errorf("failed to initialize vxlan id manager: %v", err)
  174. }
  175. return nil
  176. }
  177. func (d *driver) Type() string {
  178. return networkType
  179. }
  180. func (d *driver) IsBuiltIn() bool {
  181. return true
  182. }
  183. func validateSelf(node string) error {
  184. advIP := net.ParseIP(node)
  185. if advIP == nil {
  186. return fmt.Errorf("invalid self address (%s)", node)
  187. }
  188. addrs, err := net.InterfaceAddrs()
  189. if err != nil {
  190. return fmt.Errorf("Unable to get interface addresses %v", err)
  191. }
  192. for _, addr := range addrs {
  193. ip, _, err := net.ParseCIDR(addr.String())
  194. if err == nil && ip.Equal(advIP) {
  195. return nil
  196. }
  197. }
  198. return fmt.Errorf("Multi-Host overlay networking requires cluster-advertise(%s) to be configured with a local ip-address that is reachable within the cluster", advIP.String())
  199. }
  200. func (d *driver) nodeJoin(advertiseAddress, bindAddress string, self bool) {
  201. if self && !d.isSerfAlive() {
  202. d.Lock()
  203. d.advertiseAddress = advertiseAddress
  204. d.bindAddress = bindAddress
  205. d.Unlock()
  206. // If there is no cluster store there is no need to start serf.
  207. if d.store != nil {
  208. if err := validateSelf(advertiseAddress); err != nil {
  209. logrus.Warnf("%s", err.Error())
  210. }
  211. err := d.serfInit()
  212. if err != nil {
  213. logrus.Errorf("initializing serf instance failed: %v", err)
  214. d.Lock()
  215. d.advertiseAddress = ""
  216. d.bindAddress = ""
  217. d.Unlock()
  218. return
  219. }
  220. }
  221. }
  222. d.Lock()
  223. if !self {
  224. d.neighIP = advertiseAddress
  225. }
  226. neighIP := d.neighIP
  227. d.Unlock()
  228. if d.serfInstance != nil && neighIP != "" {
  229. var err error
  230. d.joinOnce.Do(func() {
  231. err = d.serfJoin(neighIP)
  232. if err == nil {
  233. d.pushLocalDb()
  234. }
  235. })
  236. if err != nil {
  237. logrus.Errorf("joining serf neighbor %s failed: %v", advertiseAddress, err)
  238. d.Lock()
  239. d.joinOnce = sync.Once{}
  240. d.Unlock()
  241. return
  242. }
  243. }
  244. }
  245. func (d *driver) pushLocalEndpointEvent(action, nid, eid string) {
  246. n := d.network(nid)
  247. if n == nil {
  248. logrus.Debugf("Error pushing local endpoint event for network %s", nid)
  249. return
  250. }
  251. ep := n.endpoint(eid)
  252. if ep == nil {
  253. logrus.Debugf("Error pushing local endpoint event for ep %s / %s", nid, eid)
  254. return
  255. }
  256. if !d.isSerfAlive() {
  257. return
  258. }
  259. d.notifyCh <- ovNotify{
  260. action: "join",
  261. nw: n,
  262. ep: ep,
  263. }
  264. }
  265. // DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
  266. func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
  267. var err error
  268. switch dType {
  269. case discoverapi.NodeDiscovery:
  270. nodeData, ok := data.(discoverapi.NodeDiscoveryData)
  271. if !ok || nodeData.Address == "" {
  272. return fmt.Errorf("invalid discovery data")
  273. }
  274. d.nodeJoin(nodeData.Address, nodeData.BindAddress, nodeData.Self)
  275. case discoverapi.DatastoreConfig:
  276. if d.store != nil {
  277. return types.ForbiddenErrorf("cannot accept datastore configuration: Overlay driver has a datastore configured already")
  278. }
  279. dsc, ok := data.(discoverapi.DatastoreConfigData)
  280. if !ok {
  281. return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
  282. }
  283. d.store, err = datastore.NewDataStoreFromConfig(dsc)
  284. if err != nil {
  285. return types.InternalErrorf("failed to initialize data store: %v", err)
  286. }
  287. case discoverapi.EncryptionKeysConfig:
  288. encrData, ok := data.(discoverapi.DriverEncryptionConfig)
  289. if !ok {
  290. return fmt.Errorf("invalid encryption key notification data")
  291. }
  292. keys := make([]*key, 0, len(encrData.Keys))
  293. for i := 0; i < len(encrData.Keys); i++ {
  294. k := &key{
  295. value: encrData.Keys[i],
  296. tag: uint32(encrData.Tags[i]),
  297. }
  298. keys = append(keys, k)
  299. }
  300. if err := d.setKeys(keys); err != nil {
  301. logrus.Warn(err)
  302. }
  303. case discoverapi.EncryptionKeysUpdate:
  304. var newKey, delKey, priKey *key
  305. encrData, ok := data.(discoverapi.DriverEncryptionUpdate)
  306. if !ok {
  307. return fmt.Errorf("invalid encryption key notification data")
  308. }
  309. if encrData.Key != nil {
  310. newKey = &key{
  311. value: encrData.Key,
  312. tag: uint32(encrData.Tag),
  313. }
  314. }
  315. if encrData.Primary != nil {
  316. priKey = &key{
  317. value: encrData.Primary,
  318. tag: uint32(encrData.PrimaryTag),
  319. }
  320. }
  321. if encrData.Prune != nil {
  322. delKey = &key{
  323. value: encrData.Prune,
  324. tag: uint32(encrData.PruneTag),
  325. }
  326. }
  327. if err := d.updateKeys(newKey, priKey, delKey); err != nil {
  328. logrus.Warn(err)
  329. }
  330. default:
  331. }
  332. return nil
  333. }
  334. // DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
  335. func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
  336. return nil
  337. }