macvlan_store.go 8.4 KB

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