macvlan_store.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //go:build linux
  2. // +build linux
  3. package macvlan
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "net"
  8. "github.com/docker/docker/libnetwork/datastore"
  9. "github.com/docker/docker/libnetwork/discoverapi"
  10. "github.com/docker/docker/libnetwork/netlabel"
  11. "github.com/docker/docker/libnetwork/types"
  12. "github.com/sirupsen/logrus"
  13. )
  14. const (
  15. macvlanPrefix = "macvlan"
  16. macvlanNetworkPrefix = macvlanPrefix + "/network"
  17. macvlanEndpointPrefix = macvlanPrefix + "/endpoint"
  18. )
  19. // networkConfiguration for this driver's network specific configuration
  20. type configuration struct {
  21. ID string
  22. Mtu int
  23. dbIndex uint64
  24. dbExists bool
  25. Internal bool
  26. Parent string
  27. MacvlanMode string
  28. CreatedSlaveLink bool
  29. Ipv4Subnets []*ipSubnet
  30. Ipv6Subnets []*ipSubnet
  31. }
  32. type ipSubnet struct {
  33. SubnetIP string
  34. GwIP string
  35. }
  36. // initStore drivers are responsible for caching their own persistent state
  37. func (d *driver) initStore(option map[string]interface{}) error {
  38. if data, ok := option[netlabel.LocalKVClient]; ok {
  39. var err error
  40. dsc, ok := data.(discoverapi.DatastoreConfigData)
  41. if !ok {
  42. return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
  43. }
  44. d.store, err = datastore.NewDataStoreFromConfig(dsc)
  45. if err != nil {
  46. return types.InternalErrorf("macvlan driver failed to initialize data store: %v", err)
  47. }
  48. err = d.populateNetworks()
  49. if err != nil {
  50. return err
  51. }
  52. err = d.populateEndpoints()
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. return nil
  58. }
  59. // populateNetworks is invoked at driver init to recreate persistently stored networks
  60. func (d *driver) populateNetworks() error {
  61. kvol, err := d.store.List(datastore.Key(macvlanPrefix), &configuration{})
  62. if err != nil && err != datastore.ErrKeyNotFound {
  63. return fmt.Errorf("failed to get macvlan network configurations from store: %v", err)
  64. }
  65. // If empty it simply means no macvlan networks have been created yet
  66. if err == datastore.ErrKeyNotFound {
  67. return nil
  68. }
  69. for _, kvo := range kvol {
  70. config := kvo.(*configuration)
  71. if _, err = d.createNetwork(config); err != nil {
  72. logrus.Warnf("Could not create macvlan network for id %s from persistent state", config.ID)
  73. }
  74. }
  75. return nil
  76. }
  77. func (d *driver) populateEndpoints() error {
  78. kvol, err := d.store.List(datastore.Key(macvlanEndpointPrefix), &endpoint{})
  79. if err != nil && err != datastore.ErrKeyNotFound {
  80. return fmt.Errorf("failed to get macvlan endpoints from store: %v", err)
  81. }
  82. if err == datastore.ErrKeyNotFound {
  83. return nil
  84. }
  85. for _, kvo := range kvol {
  86. ep := kvo.(*endpoint)
  87. n, ok := d.networks[ep.nid]
  88. if !ok {
  89. logrus.Debugf("Network (%.7s) not found for restored macvlan endpoint (%.7s)", ep.nid, ep.id)
  90. logrus.Debugf("Deleting stale macvlan endpoint (%.7s) from store", ep.id)
  91. if err := d.storeDelete(ep); err != nil {
  92. logrus.Debugf("Failed to delete stale macvlan endpoint (%.7s) from store", ep.id)
  93. }
  94. continue
  95. }
  96. n.endpoints[ep.id] = ep
  97. logrus.Debugf("Endpoint (%.7s) restored to network (%.7s)", ep.id, ep.nid)
  98. }
  99. return nil
  100. }
  101. // storeUpdate used to update persistent macvlan network records as they are created
  102. func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
  103. if d.store == nil {
  104. logrus.Warnf("macvlan store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
  105. return nil
  106. }
  107. if err := d.store.PutObjectAtomic(kvObject); err != nil {
  108. return fmt.Errorf("failed to update macvlan store for object type %T: %v", kvObject, err)
  109. }
  110. return nil
  111. }
  112. // storeDelete used to delete macvlan records from persistent cache as they are deleted
  113. func (d *driver) storeDelete(kvObject datastore.KVObject) error {
  114. if d.store == nil {
  115. logrus.Debugf("macvlan store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
  116. return nil
  117. }
  118. retry:
  119. if err := d.store.DeleteObjectAtomic(kvObject); err != nil {
  120. if err == datastore.ErrKeyModified {
  121. if err := d.store.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
  122. return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
  123. }
  124. goto retry
  125. }
  126. return err
  127. }
  128. return nil
  129. }
  130. func (config *configuration) MarshalJSON() ([]byte, error) {
  131. nMap := make(map[string]interface{})
  132. nMap["ID"] = config.ID
  133. nMap["Mtu"] = config.Mtu
  134. nMap["Parent"] = config.Parent
  135. nMap["MacvlanMode"] = config.MacvlanMode
  136. nMap["Internal"] = config.Internal
  137. nMap["CreatedSubIface"] = config.CreatedSlaveLink
  138. if len(config.Ipv4Subnets) > 0 {
  139. iis, err := json.Marshal(config.Ipv4Subnets)
  140. if err != nil {
  141. return nil, err
  142. }
  143. nMap["Ipv4Subnets"] = string(iis)
  144. }
  145. if len(config.Ipv6Subnets) > 0 {
  146. iis, err := json.Marshal(config.Ipv6Subnets)
  147. if err != nil {
  148. return nil, err
  149. }
  150. nMap["Ipv6Subnets"] = string(iis)
  151. }
  152. return json.Marshal(nMap)
  153. }
  154. func (config *configuration) UnmarshalJSON(b []byte) error {
  155. var (
  156. err error
  157. nMap map[string]interface{}
  158. )
  159. if err = json.Unmarshal(b, &nMap); err != nil {
  160. return err
  161. }
  162. config.ID = nMap["ID"].(string)
  163. config.Mtu = int(nMap["Mtu"].(float64))
  164. config.Parent = nMap["Parent"].(string)
  165. config.MacvlanMode = nMap["MacvlanMode"].(string)
  166. config.Internal = nMap["Internal"].(bool)
  167. config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool)
  168. if v, ok := nMap["Ipv4Subnets"]; ok {
  169. if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil {
  170. return err
  171. }
  172. }
  173. if v, ok := nMap["Ipv6Subnets"]; ok {
  174. if err := json.Unmarshal([]byte(v.(string)), &config.Ipv6Subnets); err != nil {
  175. return err
  176. }
  177. }
  178. return nil
  179. }
  180. func (config *configuration) Key() []string {
  181. return []string{macvlanNetworkPrefix, config.ID}
  182. }
  183. func (config *configuration) KeyPrefix() []string {
  184. return []string{macvlanNetworkPrefix}
  185. }
  186. func (config *configuration) Value() []byte {
  187. b, err := json.Marshal(config)
  188. if err != nil {
  189. return nil
  190. }
  191. return b
  192. }
  193. func (config *configuration) SetValue(value []byte) error {
  194. return json.Unmarshal(value, config)
  195. }
  196. func (config *configuration) Index() uint64 {
  197. return config.dbIndex
  198. }
  199. func (config *configuration) SetIndex(index uint64) {
  200. config.dbIndex = index
  201. config.dbExists = true
  202. }
  203. func (config *configuration) Exists() bool {
  204. return config.dbExists
  205. }
  206. func (config *configuration) Skip() bool {
  207. return false
  208. }
  209. func (config *configuration) New() datastore.KVObject {
  210. return &configuration{}
  211. }
  212. func (config *configuration) CopyTo(o datastore.KVObject) error {
  213. dstNcfg := o.(*configuration)
  214. *dstNcfg = *config
  215. return nil
  216. }
  217. func (config *configuration) DataScope() string {
  218. return datastore.LocalScope
  219. }
  220. func (ep *endpoint) MarshalJSON() ([]byte, error) {
  221. epMap := make(map[string]interface{})
  222. epMap["id"] = ep.id
  223. epMap["nid"] = ep.nid
  224. epMap["SrcName"] = ep.srcName
  225. if len(ep.mac) != 0 {
  226. epMap["MacAddress"] = ep.mac.String()
  227. }
  228. if ep.addr != nil {
  229. epMap["Addr"] = ep.addr.String()
  230. }
  231. if ep.addrv6 != nil {
  232. epMap["Addrv6"] = ep.addrv6.String()
  233. }
  234. return json.Marshal(epMap)
  235. }
  236. func (ep *endpoint) UnmarshalJSON(b []byte) error {
  237. var (
  238. err error
  239. epMap map[string]interface{}
  240. )
  241. if err = json.Unmarshal(b, &epMap); err != nil {
  242. return fmt.Errorf("Failed to unmarshal to macvlan endpoint: %v", err)
  243. }
  244. if v, ok := epMap["MacAddress"]; ok {
  245. if ep.mac, err = net.ParseMAC(v.(string)); err != nil {
  246. return types.InternalErrorf("failed to decode macvlan endpoint MAC address (%s) after json unmarshal: %v", v.(string), err)
  247. }
  248. }
  249. if v, ok := epMap["Addr"]; ok {
  250. if ep.addr, err = types.ParseCIDR(v.(string)); err != nil {
  251. return types.InternalErrorf("failed to decode macvlan endpoint IPv4 address (%s) after json unmarshal: %v", v.(string), err)
  252. }
  253. }
  254. if v, ok := epMap["Addrv6"]; ok {
  255. if ep.addrv6, err = types.ParseCIDR(v.(string)); err != nil {
  256. return types.InternalErrorf("failed to decode macvlan endpoint IPv6 address (%s) after json unmarshal: %v", v.(string), err)
  257. }
  258. }
  259. ep.id = epMap["id"].(string)
  260. ep.nid = epMap["nid"].(string)
  261. ep.srcName = epMap["SrcName"].(string)
  262. return nil
  263. }
  264. func (ep *endpoint) Key() []string {
  265. return []string{macvlanEndpointPrefix, ep.id}
  266. }
  267. func (ep *endpoint) KeyPrefix() []string {
  268. return []string{macvlanEndpointPrefix}
  269. }
  270. func (ep *endpoint) Value() []byte {
  271. b, err := json.Marshal(ep)
  272. if err != nil {
  273. return nil
  274. }
  275. return b
  276. }
  277. func (ep *endpoint) SetValue(value []byte) error {
  278. return json.Unmarshal(value, ep)
  279. }
  280. func (ep *endpoint) Index() uint64 {
  281. return ep.dbIndex
  282. }
  283. func (ep *endpoint) SetIndex(index uint64) {
  284. ep.dbIndex = index
  285. ep.dbExists = true
  286. }
  287. func (ep *endpoint) Exists() bool {
  288. return ep.dbExists
  289. }
  290. func (ep *endpoint) Skip() bool {
  291. return false
  292. }
  293. func (ep *endpoint) New() datastore.KVObject {
  294. return &endpoint{}
  295. }
  296. func (ep *endpoint) CopyTo(o datastore.KVObject) error {
  297. dstEp := o.(*endpoint)
  298. *dstEp = *ep
  299. return nil
  300. }
  301. func (ep *endpoint) DataScope() string {
  302. return datastore.LocalScope
  303. }