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