bridge_store.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //go:build linux
  2. package bridge
  3. import (
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "net"
  8. "github.com/containerd/log"
  9. "github.com/docker/docker/libnetwork/datastore"
  10. "github.com/docker/docker/libnetwork/discoverapi"
  11. "github.com/docker/docker/libnetwork/netlabel"
  12. "github.com/docker/docker/libnetwork/types"
  13. )
  14. const (
  15. // network config prefix was not specific enough.
  16. // To be backward compatible, need custom endpoint
  17. // prefix with different root
  18. bridgePrefix = "bridge"
  19. bridgeEndpointPrefix = "bridge-endpoint"
  20. )
  21. func (d *driver) initStore(option map[string]interface{}) error {
  22. if data, ok := option[netlabel.LocalKVClient]; ok {
  23. var err error
  24. dsc, ok := data.(discoverapi.DatastoreConfigData)
  25. if !ok {
  26. return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
  27. }
  28. d.store, err = datastore.FromConfig(dsc)
  29. if err != nil {
  30. return types.InternalErrorf("bridge driver failed to initialize data store: %v", err)
  31. }
  32. err = d.populateNetworks()
  33. if err != nil {
  34. return err
  35. }
  36. err = d.populateEndpoints()
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. return nil
  42. }
  43. func (d *driver) populateNetworks() error {
  44. kvol, err := d.store.List(datastore.Key(bridgePrefix), &networkConfiguration{})
  45. if err != nil && err != datastore.ErrKeyNotFound {
  46. return fmt.Errorf("failed to get bridge network configurations from store: %v", err)
  47. }
  48. // It's normal for network configuration state to be empty. Just return.
  49. if err == datastore.ErrKeyNotFound {
  50. return nil
  51. }
  52. for _, kvo := range kvol {
  53. ncfg := kvo.(*networkConfiguration)
  54. if err = d.createNetwork(ncfg); err != nil {
  55. log.G(context.TODO()).Warnf("could not create bridge network for id %s bridge name %s while booting up from persistent state: %v", ncfg.ID, ncfg.BridgeName, err)
  56. }
  57. log.G(context.TODO()).Debugf("Network (%.7s) restored", ncfg.ID)
  58. }
  59. return nil
  60. }
  61. func (d *driver) populateEndpoints() error {
  62. kvol, err := d.store.List(datastore.Key(bridgeEndpointPrefix), &bridgeEndpoint{})
  63. if err != nil && err != datastore.ErrKeyNotFound {
  64. return fmt.Errorf("failed to get bridge endpoints from store: %v", err)
  65. }
  66. if err == datastore.ErrKeyNotFound {
  67. return nil
  68. }
  69. for _, kvo := range kvol {
  70. ep := kvo.(*bridgeEndpoint)
  71. n, ok := d.networks[ep.nid]
  72. if !ok {
  73. log.G(context.TODO()).Debugf("Network (%.7s) not found for restored bridge endpoint (%.7s)", ep.nid, ep.id)
  74. log.G(context.TODO()).Debugf("Deleting stale bridge endpoint (%.7s) from store", ep.id)
  75. if err := d.storeDelete(ep); err != nil {
  76. log.G(context.TODO()).Debugf("Failed to delete stale bridge endpoint (%.7s) from store", ep.id)
  77. }
  78. continue
  79. }
  80. n.endpoints[ep.id] = ep
  81. n.restorePortAllocations(ep)
  82. log.G(context.TODO()).Debugf("Endpoint (%.7s) restored to network (%.7s)", ep.id, ep.nid)
  83. }
  84. return nil
  85. }
  86. func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
  87. if d.store == nil {
  88. log.G(context.TODO()).Warnf("bridge store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
  89. return nil
  90. }
  91. if err := d.store.PutObjectAtomic(kvObject); err != nil {
  92. return fmt.Errorf("failed to update bridge store for object type %T: %v", kvObject, err)
  93. }
  94. return nil
  95. }
  96. func (d *driver) storeDelete(kvObject datastore.KVObject) error {
  97. if d.store == nil {
  98. log.G(context.TODO()).Debugf("bridge store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
  99. return nil
  100. }
  101. retry:
  102. if err := d.store.DeleteObjectAtomic(kvObject); err != nil {
  103. if err == datastore.ErrKeyModified {
  104. if err := d.store.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
  105. return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
  106. }
  107. goto retry
  108. }
  109. return err
  110. }
  111. return nil
  112. }
  113. func (ncfg *networkConfiguration) MarshalJSON() ([]byte, error) {
  114. nMap := make(map[string]interface{})
  115. nMap["ID"] = ncfg.ID
  116. nMap["BridgeName"] = ncfg.BridgeName
  117. nMap["EnableIPv6"] = ncfg.EnableIPv6
  118. nMap["EnableIPMasquerade"] = ncfg.EnableIPMasquerade
  119. nMap["EnableICC"] = ncfg.EnableICC
  120. nMap["InhibitIPv4"] = ncfg.InhibitIPv4
  121. nMap["Mtu"] = ncfg.Mtu
  122. nMap["Internal"] = ncfg.Internal
  123. nMap["DefaultBridge"] = ncfg.DefaultBridge
  124. nMap["DefaultBindingIP"] = ncfg.DefaultBindingIP.String()
  125. // This key is "HostIP" instead of "HostIPv4" to preserve compatibility with the on-disk format.
  126. nMap["HostIP"] = ncfg.HostIPv4.String()
  127. nMap["HostIPv6"] = ncfg.HostIPv6.String()
  128. nMap["DefaultGatewayIPv4"] = ncfg.DefaultGatewayIPv4.String()
  129. nMap["DefaultGatewayIPv6"] = ncfg.DefaultGatewayIPv6.String()
  130. nMap["ContainerIfacePrefix"] = ncfg.ContainerIfacePrefix
  131. nMap["BridgeIfaceCreator"] = ncfg.BridgeIfaceCreator
  132. if ncfg.AddressIPv4 != nil {
  133. nMap["AddressIPv4"] = ncfg.AddressIPv4.String()
  134. }
  135. if ncfg.AddressIPv6 != nil {
  136. nMap["AddressIPv6"] = ncfg.AddressIPv6.String()
  137. }
  138. return json.Marshal(nMap)
  139. }
  140. func (ncfg *networkConfiguration) UnmarshalJSON(b []byte) error {
  141. var (
  142. err error
  143. nMap map[string]interface{}
  144. )
  145. if err = json.Unmarshal(b, &nMap); err != nil {
  146. return err
  147. }
  148. if v, ok := nMap["AddressIPv4"]; ok {
  149. if ncfg.AddressIPv4, err = types.ParseCIDR(v.(string)); err != nil {
  150. return types.InternalErrorf("failed to decode bridge network address IPv4 after json unmarshal: %s", v.(string))
  151. }
  152. }
  153. if v, ok := nMap["AddressIPv6"]; ok {
  154. if ncfg.AddressIPv6, err = types.ParseCIDR(v.(string)); err != nil {
  155. return types.InternalErrorf("failed to decode bridge network address IPv6 after json unmarshal: %s", v.(string))
  156. }
  157. }
  158. if v, ok := nMap["ContainerIfacePrefix"]; ok {
  159. ncfg.ContainerIfacePrefix = v.(string)
  160. }
  161. // This key is "HostIP" instead of "HostIPv4" to preserve compatibility with the on-disk format.
  162. if v, ok := nMap["HostIP"]; ok {
  163. ncfg.HostIPv4 = net.ParseIP(v.(string))
  164. }
  165. if v, ok := nMap["HostIPv6"]; ok {
  166. ncfg.HostIPv6 = net.ParseIP(v.(string))
  167. }
  168. ncfg.DefaultBridge = nMap["DefaultBridge"].(bool)
  169. ncfg.DefaultBindingIP = net.ParseIP(nMap["DefaultBindingIP"].(string))
  170. ncfg.DefaultGatewayIPv4 = net.ParseIP(nMap["DefaultGatewayIPv4"].(string))
  171. ncfg.DefaultGatewayIPv6 = net.ParseIP(nMap["DefaultGatewayIPv6"].(string))
  172. ncfg.ID = nMap["ID"].(string)
  173. ncfg.BridgeName = nMap["BridgeName"].(string)
  174. ncfg.EnableIPv6 = nMap["EnableIPv6"].(bool)
  175. ncfg.EnableIPMasquerade = nMap["EnableIPMasquerade"].(bool)
  176. ncfg.EnableICC = nMap["EnableICC"].(bool)
  177. if v, ok := nMap["InhibitIPv4"]; ok {
  178. ncfg.InhibitIPv4 = v.(bool)
  179. }
  180. ncfg.Mtu = int(nMap["Mtu"].(float64))
  181. if v, ok := nMap["Internal"]; ok {
  182. ncfg.Internal = v.(bool)
  183. }
  184. if v, ok := nMap["BridgeIfaceCreator"]; ok {
  185. ncfg.BridgeIfaceCreator = ifaceCreator(v.(float64))
  186. }
  187. return nil
  188. }
  189. func (ncfg *networkConfiguration) Key() []string {
  190. return []string{bridgePrefix, ncfg.ID}
  191. }
  192. func (ncfg *networkConfiguration) KeyPrefix() []string {
  193. return []string{bridgePrefix}
  194. }
  195. func (ncfg *networkConfiguration) Value() []byte {
  196. b, err := json.Marshal(ncfg)
  197. if err != nil {
  198. return nil
  199. }
  200. return b
  201. }
  202. func (ncfg *networkConfiguration) SetValue(value []byte) error {
  203. return json.Unmarshal(value, ncfg)
  204. }
  205. func (ncfg *networkConfiguration) Index() uint64 {
  206. return ncfg.dbIndex
  207. }
  208. func (ncfg *networkConfiguration) SetIndex(index uint64) {
  209. ncfg.dbIndex = index
  210. ncfg.dbExists = true
  211. }
  212. func (ncfg *networkConfiguration) Exists() bool {
  213. return ncfg.dbExists
  214. }
  215. func (ncfg *networkConfiguration) Skip() bool {
  216. return false
  217. }
  218. func (ncfg *networkConfiguration) New() datastore.KVObject {
  219. return &networkConfiguration{}
  220. }
  221. func (ncfg *networkConfiguration) CopyTo(o datastore.KVObject) error {
  222. dstNcfg := o.(*networkConfiguration)
  223. *dstNcfg = *ncfg
  224. return nil
  225. }
  226. func (ep *bridgeEndpoint) MarshalJSON() ([]byte, error) {
  227. epMap := make(map[string]interface{})
  228. epMap["id"] = ep.id
  229. epMap["nid"] = ep.nid
  230. epMap["SrcName"] = ep.srcName
  231. epMap["MacAddress"] = ep.macAddress.String()
  232. epMap["Addr"] = ep.addr.String()
  233. if ep.addrv6 != nil {
  234. epMap["Addrv6"] = ep.addrv6.String()
  235. }
  236. epMap["Config"] = ep.config
  237. epMap["ContainerConfig"] = ep.containerConfig
  238. epMap["ExternalConnConfig"] = ep.extConnConfig
  239. epMap["PortMapping"] = ep.portMapping
  240. return json.Marshal(epMap)
  241. }
  242. func (ep *bridgeEndpoint) UnmarshalJSON(b []byte) error {
  243. var (
  244. err error
  245. epMap map[string]interface{}
  246. )
  247. if err = json.Unmarshal(b, &epMap); err != nil {
  248. return fmt.Errorf("Failed to unmarshal to bridge endpoint: %v", err)
  249. }
  250. if v, ok := epMap["MacAddress"]; ok {
  251. if ep.macAddress, err = net.ParseMAC(v.(string)); err != nil {
  252. return types.InternalErrorf("failed to decode bridge endpoint MAC address (%s) after json unmarshal: %v", v.(string), err)
  253. }
  254. }
  255. if v, ok := epMap["Addr"]; ok {
  256. if ep.addr, err = types.ParseCIDR(v.(string)); err != nil {
  257. return types.InternalErrorf("failed to decode bridge endpoint IPv4 address (%s) after json unmarshal: %v", v.(string), err)
  258. }
  259. }
  260. if v, ok := epMap["Addrv6"]; ok {
  261. if ep.addrv6, err = types.ParseCIDR(v.(string)); err != nil {
  262. return types.InternalErrorf("failed to decode bridge endpoint IPv6 address (%s) after json unmarshal: %v", v.(string), err)
  263. }
  264. }
  265. ep.id = epMap["id"].(string)
  266. ep.nid = epMap["nid"].(string)
  267. ep.srcName = epMap["SrcName"].(string)
  268. d, _ := json.Marshal(epMap["Config"])
  269. if err := json.Unmarshal(d, &ep.config); err != nil {
  270. log.G(context.TODO()).Warnf("Failed to decode endpoint config %v", err)
  271. }
  272. d, _ = json.Marshal(epMap["ContainerConfig"])
  273. if err := json.Unmarshal(d, &ep.containerConfig); err != nil {
  274. log.G(context.TODO()).Warnf("Failed to decode endpoint container config %v", err)
  275. }
  276. d, _ = json.Marshal(epMap["ExternalConnConfig"])
  277. if err := json.Unmarshal(d, &ep.extConnConfig); err != nil {
  278. log.G(context.TODO()).Warnf("Failed to decode endpoint external connectivity configuration %v", err)
  279. }
  280. d, _ = json.Marshal(epMap["PortMapping"])
  281. if err := json.Unmarshal(d, &ep.portMapping); err != nil {
  282. log.G(context.TODO()).Warnf("Failed to decode endpoint port mapping %v", err)
  283. }
  284. return nil
  285. }
  286. func (ep *bridgeEndpoint) Key() []string {
  287. return []string{bridgeEndpointPrefix, ep.id}
  288. }
  289. func (ep *bridgeEndpoint) KeyPrefix() []string {
  290. return []string{bridgeEndpointPrefix}
  291. }
  292. func (ep *bridgeEndpoint) Value() []byte {
  293. b, err := json.Marshal(ep)
  294. if err != nil {
  295. return nil
  296. }
  297. return b
  298. }
  299. func (ep *bridgeEndpoint) SetValue(value []byte) error {
  300. return json.Unmarshal(value, ep)
  301. }
  302. func (ep *bridgeEndpoint) Index() uint64 {
  303. return ep.dbIndex
  304. }
  305. func (ep *bridgeEndpoint) SetIndex(index uint64) {
  306. ep.dbIndex = index
  307. ep.dbExists = true
  308. }
  309. func (ep *bridgeEndpoint) Exists() bool {
  310. return ep.dbExists
  311. }
  312. func (ep *bridgeEndpoint) Skip() bool {
  313. return false
  314. }
  315. func (ep *bridgeEndpoint) New() datastore.KVObject {
  316. return &bridgeEndpoint{}
  317. }
  318. func (ep *bridgeEndpoint) CopyTo(o datastore.KVObject) error {
  319. dstEp := o.(*bridgeEndpoint)
  320. *dstEp = *ep
  321. return nil
  322. }
  323. func (n *bridgeNetwork) restorePortAllocations(ep *bridgeEndpoint) {
  324. if ep.extConnConfig == nil ||
  325. ep.extConnConfig.ExposedPorts == nil ||
  326. ep.extConnConfig.PortBindings == nil {
  327. return
  328. }
  329. tmp := ep.extConnConfig.PortBindings
  330. ep.extConnConfig.PortBindings = ep.portMapping
  331. _, err := n.allocatePorts(ep, n.config.DefaultBindingIP, n.driver.config.EnableUserlandProxy)
  332. if err != nil {
  333. log.G(context.TODO()).Warnf("Failed to reserve existing port mapping for endpoint %.7s:%v", ep.id, err)
  334. }
  335. ep.extConnConfig.PortBindings = tmp
  336. }