ipvlan_store.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //go:build linux
  2. // +build linux
  3. package ipvlan
  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. 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.NewDataStoreFromConfig(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. logrus.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. logrus.Debugf("Network (%.7s) not found for restored ipvlan endpoint (%.7s)", ep.nid, ep.id)
  91. logrus.Debugf("Deleting stale ipvlan endpoint (%.7s) from store", ep.id)
  92. if err := d.storeDelete(ep); err != nil {
  93. logrus.Debugf("Failed to delete stale ipvlan endpoint (%.7s) from store", ep.id)
  94. }
  95. continue
  96. }
  97. n.endpoints[ep.id] = ep
  98. logrus.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. logrus.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. logrus.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["Internal"] = config.Internal
  138. nMap["CreatedSubIface"] = config.CreatedSlaveLink
  139. if len(config.Ipv4Subnets) > 0 {
  140. iis, err := json.Marshal(config.Ipv4Subnets)
  141. if err != nil {
  142. return nil, err
  143. }
  144. nMap["Ipv4Subnets"] = string(iis)
  145. }
  146. if len(config.Ipv6Subnets) > 0 {
  147. iis, err := json.Marshal(config.Ipv6Subnets)
  148. if err != nil {
  149. return nil, err
  150. }
  151. nMap["Ipv6Subnets"] = string(iis)
  152. }
  153. return json.Marshal(nMap)
  154. }
  155. func (config *configuration) UnmarshalJSON(b []byte) error {
  156. var (
  157. err error
  158. nMap map[string]interface{}
  159. )
  160. if err = json.Unmarshal(b, &nMap); err != nil {
  161. return err
  162. }
  163. config.ID = nMap["ID"].(string)
  164. config.Mtu = int(nMap["Mtu"].(float64))
  165. config.Parent = nMap["Parent"].(string)
  166. config.IpvlanMode = nMap["IpvlanMode"].(string)
  167. config.Internal = nMap["Internal"].(bool)
  168. config.CreatedSlaveLink = nMap["CreatedSubIface"].(bool)
  169. if v, ok := nMap["Ipv4Subnets"]; ok {
  170. if err := json.Unmarshal([]byte(v.(string)), &config.Ipv4Subnets); err != nil {
  171. return err
  172. }
  173. }
  174. if v, ok := nMap["Ipv6Subnets"]; ok {
  175. if err := json.Unmarshal([]byte(v.(string)), &config.Ipv6Subnets); err != nil {
  176. return err
  177. }
  178. }
  179. return nil
  180. }
  181. func (config *configuration) Key() []string {
  182. return []string{ipvlanNetworkPrefix, config.ID}
  183. }
  184. func (config *configuration) KeyPrefix() []string {
  185. return []string{ipvlanNetworkPrefix}
  186. }
  187. func (config *configuration) Value() []byte {
  188. b, err := json.Marshal(config)
  189. if err != nil {
  190. return nil
  191. }
  192. return b
  193. }
  194. func (config *configuration) SetValue(value []byte) error {
  195. return json.Unmarshal(value, config)
  196. }
  197. func (config *configuration) Index() uint64 {
  198. return config.dbIndex
  199. }
  200. func (config *configuration) SetIndex(index uint64) {
  201. config.dbIndex = index
  202. config.dbExists = true
  203. }
  204. func (config *configuration) Exists() bool {
  205. return config.dbExists
  206. }
  207. func (config *configuration) Skip() bool {
  208. return false
  209. }
  210. func (config *configuration) New() datastore.KVObject {
  211. return &configuration{}
  212. }
  213. func (config *configuration) CopyTo(o datastore.KVObject) error {
  214. dstNcfg := o.(*configuration)
  215. *dstNcfg = *config
  216. return nil
  217. }
  218. func (config *configuration) DataScope() string {
  219. return datastore.LocalScope
  220. }
  221. func (ep *endpoint) MarshalJSON() ([]byte, error) {
  222. epMap := make(map[string]interface{})
  223. epMap["id"] = ep.id
  224. epMap["nid"] = ep.nid
  225. epMap["SrcName"] = ep.srcName
  226. if len(ep.mac) != 0 {
  227. epMap["MacAddress"] = ep.mac.String()
  228. }
  229. if ep.addr != nil {
  230. epMap["Addr"] = ep.addr.String()
  231. }
  232. if ep.addrv6 != nil {
  233. epMap["Addrv6"] = ep.addrv6.String()
  234. }
  235. return json.Marshal(epMap)
  236. }
  237. func (ep *endpoint) UnmarshalJSON(b []byte) error {
  238. var (
  239. err error
  240. epMap map[string]interface{}
  241. )
  242. if err = json.Unmarshal(b, &epMap); err != nil {
  243. return fmt.Errorf("Failed to unmarshal to ipvlan endpoint: %v", err)
  244. }
  245. if v, ok := epMap["MacAddress"]; ok {
  246. if ep.mac, err = net.ParseMAC(v.(string)); err != nil {
  247. return types.InternalErrorf("failed to decode ipvlan endpoint MAC address (%s) after json unmarshal: %v", v.(string), err)
  248. }
  249. }
  250. if v, ok := epMap["Addr"]; ok {
  251. if ep.addr, err = types.ParseCIDR(v.(string)); err != nil {
  252. return types.InternalErrorf("failed to decode ipvlan endpoint IPv4 address (%s) after json unmarshal: %v", v.(string), err)
  253. }
  254. }
  255. if v, ok := epMap["Addrv6"]; ok {
  256. if ep.addrv6, err = types.ParseCIDR(v.(string)); err != nil {
  257. return types.InternalErrorf("failed to decode ipvlan endpoint IPv6 address (%s) after json unmarshal: %v", v.(string), err)
  258. }
  259. }
  260. ep.id = epMap["id"].(string)
  261. ep.nid = epMap["nid"].(string)
  262. ep.srcName = epMap["SrcName"].(string)
  263. return nil
  264. }
  265. func (ep *endpoint) Key() []string {
  266. return []string{ipvlanEndpointPrefix, ep.id}
  267. }
  268. func (ep *endpoint) KeyPrefix() []string {
  269. return []string{ipvlanEndpointPrefix}
  270. }
  271. func (ep *endpoint) Value() []byte {
  272. b, err := json.Marshal(ep)
  273. if err != nil {
  274. return nil
  275. }
  276. return b
  277. }
  278. func (ep *endpoint) SetValue(value []byte) error {
  279. return json.Unmarshal(value, ep)
  280. }
  281. func (ep *endpoint) Index() uint64 {
  282. return ep.dbIndex
  283. }
  284. func (ep *endpoint) SetIndex(index uint64) {
  285. ep.dbIndex = index
  286. ep.dbExists = true
  287. }
  288. func (ep *endpoint) Exists() bool {
  289. return ep.dbExists
  290. }
  291. func (ep *endpoint) Skip() bool {
  292. return false
  293. }
  294. func (ep *endpoint) New() datastore.KVObject {
  295. return &endpoint{}
  296. }
  297. func (ep *endpoint) CopyTo(o datastore.KVObject) error {
  298. dstEp := o.(*endpoint)
  299. *dstEp = *ep
  300. return nil
  301. }
  302. func (ep *endpoint) DataScope() string {
  303. return datastore.LocalScope
  304. }