github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/libnetwork/drivers/ipvlan/ipvlan_store.go (about)

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