ipvlan_store.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //go:build linux
  2. package ipvlan
  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. ipvlanPrefix = "ipvlan"
  15. ipvlanNetworkPrefix = ipvlanPrefix + "/network"
  16. ipvlanEndpointPrefix = ipvlanPrefix + "/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. IpvlanMode string
  27. IpvlanFlag 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 ok bool
  40. d.store, ok = data.(*datastore.Store)
  41. if !ok {
  42. return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
  43. }
  44. err := d.populateNetworks()
  45. if err != nil {
  46. return err
  47. }
  48. err = d.populateEndpoints()
  49. if err != nil {
  50. return err
  51. }
  52. }
  53. return nil
  54. }
  55. // populateNetworks is invoked at driver init to recreate persistently stored networks
  56. func (d *driver) populateNetworks() error {
  57. kvol, err := d.store.List(&configuration{})
  58. if err != nil && err != datastore.ErrKeyNotFound {
  59. return fmt.Errorf("failed to get ipvlan network configurations from store: %v", err)
  60. }
  61. // If empty it simply means no ipvlan networks have been created yet
  62. if err == datastore.ErrKeyNotFound {
  63. return nil
  64. }
  65. for _, kvo := range kvol {
  66. config := kvo.(*configuration)
  67. if _, err = d.createNetwork(config); err != nil {
  68. log.G(context.TODO()).Warnf("could not create ipvlan network for id %s from persistent state", config.ID)
  69. }
  70. }
  71. return nil
  72. }
  73. func (d *driver) populateEndpoints() error {
  74. kvol, err := d.store.List(&endpoint{})
  75. if err != nil && err != datastore.ErrKeyNotFound {
  76. return fmt.Errorf("failed to get ipvlan endpoints from store: %v", err)
  77. }
  78. if err == datastore.ErrKeyNotFound {
  79. return nil
  80. }
  81. for _, kvo := range kvol {
  82. ep := kvo.(*endpoint)
  83. n, ok := d.networks[ep.nid]
  84. if !ok {
  85. log.G(context.TODO()).Debugf("Network (%.7s) not found for restored ipvlan endpoint (%.7s)", ep.nid, ep.id)
  86. log.G(context.TODO()).Debugf("Deleting stale ipvlan endpoint (%.7s) from store", ep.id)
  87. if err := d.storeDelete(ep); err != nil {
  88. log.G(context.TODO()).Debugf("Failed to delete stale ipvlan endpoint (%.7s) from store", ep.id)
  89. }
  90. continue
  91. }
  92. n.endpoints[ep.id] = ep
  93. log.G(context.TODO()).Debugf("Endpoint (%.7s) restored to network (%.7s)", ep.id, ep.nid)
  94. }
  95. return nil
  96. }
  97. // storeUpdate used to update persistent ipvlan network records as they are created
  98. func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
  99. if d.store == nil {
  100. log.G(context.TODO()).Warnf("ipvlan store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
  101. return nil
  102. }
  103. if err := d.store.PutObjectAtomic(kvObject); err != nil {
  104. return fmt.Errorf("failed to update ipvlan store for object type %T: %v", kvObject, err)
  105. }
  106. return nil
  107. }
  108. // storeDelete used to delete ipvlan network records from persistent cache as they are deleted
  109. func (d *driver) storeDelete(kvObject datastore.KVObject) error {
  110. if d.store == nil {
  111. log.G(context.TODO()).Debugf("ipvlan store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
  112. return nil
  113. }
  114. retry:
  115. if err := d.store.DeleteObjectAtomic(kvObject); err != nil {
  116. if err == datastore.ErrKeyModified {
  117. if err := d.store.GetObject(kvObject); err != nil {
  118. return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
  119. }
  120. goto retry
  121. }
  122. return err
  123. }
  124. return nil
  125. }
  126. func (config *configuration) MarshalJSON() ([]byte, error) {
  127. nMap := make(map[string]interface{})
  128. nMap["ID"] = config.ID
  129. nMap["Mtu"] = config.Mtu
  130. nMap["Parent"] = config.Parent
  131. nMap["IpvlanMode"] = config.IpvlanMode
  132. nMap["IpvlanFlag"] = config.IpvlanFlag
  133. nMap["Internal"] = config.Internal
  134. nMap["CreatedSubIface"] = config.CreatedSlaveLink
  135. if len(config.Ipv4Subnets) > 0 {
  136. iis, err := json.Marshal(config.Ipv4Subnets)
  137. if err != nil {
  138. return nil, err
  139. }
  140. nMap["Ipv4Subnets"] = string(iis)
  141. }
  142. if len(config.Ipv6Subnets) > 0 {
  143. iis, err := json.Marshal(config.Ipv6Subnets)
  144. if err != nil {
  145. return nil, err
  146. }
  147. nMap["Ipv6Subnets"] = string(iis)
  148. }
  149. return json.Marshal(nMap)
  150. }
  151. func (config *configuration) UnmarshalJSON(b []byte) error {
  152. var (
  153. err error
  154. nMap map[string]interface{}
  155. )
  156. if err = json.Unmarshal(b, &nMap); err != nil {
  157. return err
  158. }
  159. config.ID = nMap["ID"].(string)
  160. config.Mtu = int(nMap["Mtu"].(float64))
  161. config.Parent = nMap["Parent"].(string)
  162. config.IpvlanMode = nMap["IpvlanMode"].(string)
  163. if v, ok := nMap["IpvlanFlag"]; ok {
  164. config.IpvlanFlag = v.(string)
  165. } else {
  166. // Migrate config from an older daemon which did not have the flag configurable.
  167. config.IpvlanFlag = flagBridge
  168. }
  169. config.Internal = nMap["Internal"].(bool)
  170. config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool)
  171. if v, ok := nMap["Ipv4Subnets"]; ok {
  172. if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil {
  173. return err
  174. }
  175. }
  176. if v, ok := nMap["Ipv6Subnets"]; ok {
  177. if err := json.Unmarshal([]byte(v.(string)), &config.Ipv6Subnets); err != nil {
  178. return err
  179. }
  180. }
  181. return nil
  182. }
  183. func (config *configuration) Key() []string {
  184. return []string{ipvlanNetworkPrefix, config.ID}
  185. }
  186. func (config *configuration) KeyPrefix() []string {
  187. return []string{ipvlanNetworkPrefix}
  188. }
  189. func (config *configuration) Value() []byte {
  190. b, err := json.Marshal(config)
  191. if err != nil {
  192. return nil
  193. }
  194. return b
  195. }
  196. func (config *configuration) SetValue(value []byte) error {
  197. return json.Unmarshal(value, config)
  198. }
  199. func (config *configuration) Index() uint64 {
  200. return config.dbIndex
  201. }
  202. func (config *configuration) SetIndex(index uint64) {
  203. config.dbIndex = index
  204. config.dbExists = true
  205. }
  206. func (config *configuration) Exists() bool {
  207. return config.dbExists
  208. }
  209. func (config *configuration) Skip() bool {
  210. return false
  211. }
  212. func (config *configuration) New() datastore.KVObject {
  213. return &configuration{}
  214. }
  215. func (config *configuration) CopyTo(o datastore.KVObject) error {
  216. dstNcfg := o.(*configuration)
  217. *dstNcfg = *config
  218. return nil
  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 ipvlan 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 ipvlan 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 ipvlan 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 ipvlan 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{ipvlanEndpointPrefix, ep.id}
  266. }
  267. func (ep *endpoint) KeyPrefix() []string {
  268. return []string{ipvlanEndpointPrefix}
  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. }