overlay.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //go:build linux
  2. package overlay
  3. //go:generate protoc -I=. -I=../../../vendor/ --gogofaster_out=import_path=github.com/docker/docker/libnetwork/drivers/overlay:. overlay.proto
  4. import (
  5. "context"
  6. "fmt"
  7. "net"
  8. "sync"
  9. "github.com/containerd/log"
  10. "github.com/docker/docker/libnetwork/discoverapi"
  11. "github.com/docker/docker/libnetwork/driverapi"
  12. "github.com/docker/docker/libnetwork/scope"
  13. )
  14. const (
  15. NetworkType = "overlay"
  16. vethPrefix = "veth"
  17. vethLen = len(vethPrefix) + 7
  18. vxlanEncap = 50
  19. secureOption = "encrypted"
  20. )
  21. // overlay driver must implement the discover-API.
  22. var _ discoverapi.Discover = (*driver)(nil)
  23. type driver struct {
  24. bindAddress, advertiseAddress net.IP
  25. config map[string]interface{}
  26. peerDb peerNetworkMap
  27. secMap *encrMap
  28. networks networkTable
  29. initOS sync.Once
  30. localJoinOnce sync.Once
  31. keys []*key
  32. peerOpMu sync.Mutex
  33. sync.Mutex
  34. }
  35. // Register registers a new instance of the overlay driver.
  36. func Register(r driverapi.Registerer, config map[string]interface{}) error {
  37. d := &driver{
  38. networks: networkTable{},
  39. peerDb: peerNetworkMap{
  40. mp: map[string]*peerMap{},
  41. },
  42. secMap: &encrMap{nodes: map[string][]*spi{}},
  43. config: config,
  44. }
  45. return r.RegisterDriver(NetworkType, d, driverapi.Capability{
  46. DataScope: scope.Global,
  47. ConnectivityScope: scope.Global,
  48. })
  49. }
  50. func (d *driver) configure() error {
  51. // Apply OS specific kernel configs if needed
  52. d.initOS.Do(applyOStweaks)
  53. return nil
  54. }
  55. func (d *driver) Type() string {
  56. return NetworkType
  57. }
  58. func (d *driver) IsBuiltIn() bool {
  59. return true
  60. }
  61. // isIPv6Transport reports whether the outer Layer-3 transport for VXLAN datagrams is IPv6.
  62. func (d *driver) isIPv6Transport() (bool, error) {
  63. // Infer whether remote peers' virtual tunnel endpoints will be IPv4 or IPv6
  64. // from the address family of our own advertise address. This is a
  65. // reasonable inference to make as Linux VXLAN links do not support
  66. // mixed-address-family remote peers.
  67. if d.advertiseAddress == nil {
  68. return false, fmt.Errorf("overlay: cannot determine address family of transport: the local data-plane address is not currently known")
  69. }
  70. return d.advertiseAddress.To4() == nil, nil
  71. }
  72. func (d *driver) nodeJoin(data discoverapi.NodeDiscoveryData) error {
  73. if data.Self {
  74. advAddr, bindAddr := net.ParseIP(data.Address), net.ParseIP(data.BindAddress)
  75. if advAddr == nil {
  76. return fmt.Errorf("invalid discovery data")
  77. }
  78. d.Lock()
  79. d.advertiseAddress = advAddr
  80. d.bindAddress = bindAddr
  81. d.Unlock()
  82. // If containers are already running on this network update the
  83. // advertise address in the peerDB
  84. d.localJoinOnce.Do(func() {
  85. d.peerDBUpdateSelf()
  86. })
  87. }
  88. return nil
  89. }
  90. // DiscoverNew is a notification for a new discovery event, such as a new node joining a cluster
  91. func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
  92. switch dType {
  93. case discoverapi.NodeDiscovery:
  94. nodeData, ok := data.(discoverapi.NodeDiscoveryData)
  95. if !ok {
  96. return fmt.Errorf("invalid discovery data type: %T", data)
  97. }
  98. return d.nodeJoin(nodeData)
  99. case discoverapi.EncryptionKeysConfig:
  100. encrData, ok := data.(discoverapi.DriverEncryptionConfig)
  101. if !ok {
  102. return fmt.Errorf("invalid encryption key notification data")
  103. }
  104. keys := make([]*key, 0, len(encrData.Keys))
  105. for i := 0; i < len(encrData.Keys); i++ {
  106. k := &key{
  107. value: encrData.Keys[i],
  108. tag: uint32(encrData.Tags[i]),
  109. }
  110. keys = append(keys, k)
  111. }
  112. if err := d.setKeys(keys); err != nil {
  113. log.G(context.TODO()).Warn(err)
  114. }
  115. case discoverapi.EncryptionKeysUpdate:
  116. var newKey, delKey, priKey *key
  117. encrData, ok := data.(discoverapi.DriverEncryptionUpdate)
  118. if !ok {
  119. return fmt.Errorf("invalid encryption key notification data")
  120. }
  121. if encrData.Key != nil {
  122. newKey = &key{
  123. value: encrData.Key,
  124. tag: uint32(encrData.Tag),
  125. }
  126. }
  127. if encrData.Primary != nil {
  128. priKey = &key{
  129. value: encrData.Primary,
  130. tag: uint32(encrData.PrimaryTag),
  131. }
  132. }
  133. if encrData.Prune != nil {
  134. delKey = &key{
  135. value: encrData.Prune,
  136. tag: uint32(encrData.PruneTag),
  137. }
  138. }
  139. if err := d.updateKeys(newKey, priKey, delKey); err != nil {
  140. return err
  141. }
  142. default:
  143. }
  144. return nil
  145. }
  146. // DiscoverDelete is a notification for a discovery delete event, such as a node leaving a cluster
  147. func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
  148. return nil
  149. }