ipvlan_store.go 9.0 KB

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