1/*
2Package libnetwork provides the basic functionality and extension points to
3create network namespaces and allocate interfaces for containers to use.
4
5	networkType := "bridge"
6
7	// Create a new controller instance
8	driverOptions := options.Generic{}
9	genericOption := make(map[string]interface{})
10	genericOption[netlabel.GenericData] = driverOptions
11	controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
12	if err != nil {
13		return
14	}
15
16	// Create a network for containers to join.
17	// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make use of
18	network, err := controller.NewNetwork(networkType, "network1", "")
19	if err != nil {
20		return
21	}
22
23	// For each new container: allocate IP and interfaces. The returned network
24	// settings will be used for container infos (inspect and such), as well as
25	// iptables rules for port publishing. This info is contained or accessible
26	// from the returned endpoint.
27	ep, err := network.CreateEndpoint("Endpoint1")
28	if err != nil {
29		return
30	}
31
32	// Create the sandbox for the container.
33	// NewSandbox accepts Variadic optional arguments which libnetwork can use.
34	sbx, err := controller.NewSandbox("container1",
35		libnetwork.OptionHostname("test"),
36		libnetwork.OptionDomainname("docker.io"))
37
38	// A sandbox can join the endpoint via the join api.
39	err = ep.Join(sbx)
40	if err != nil {
41		return
42	}
43*/
44package libnetwork
45
46import (
47	"container/heap"
48	"fmt"
49	"net"
50	"strings"
51	"sync"
52	"time"
53
54	"github.com/sirupsen/logrus"
55	"github.com/docker/docker/pkg/discovery"
56	"github.com/docker/docker/pkg/locker"
57	"github.com/docker/docker/pkg/plugingetter"
58	"github.com/docker/docker/pkg/plugins"
59	"github.com/docker/docker/pkg/stringid"
60	"github.com/docker/libnetwork/cluster"
61	"github.com/docker/libnetwork/config"
62	"github.com/docker/libnetwork/datastore"
63	"github.com/docker/libnetwork/discoverapi"
64	"github.com/docker/libnetwork/driverapi"
65	"github.com/docker/libnetwork/drvregistry"
66	"github.com/docker/libnetwork/hostdiscovery"
67	"github.com/docker/libnetwork/ipamapi"
68	"github.com/docker/libnetwork/netlabel"
69	"github.com/docker/libnetwork/osl"
70	"github.com/docker/libnetwork/types"
71)
72
73// NetworkController provides the interface for controller instance which manages
74// networks.
75type NetworkController interface {
76	// ID provides a unique identity for the controller
77	ID() string
78
79	// BuiltinDrivers returns list of builtin drivers
80	BuiltinDrivers() []string
81
82	// Config method returns the bootup configuration for the controller
83	Config() config.Config
84
85	// Create a new network. The options parameter carries network specific options.
86	NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error)
87
88	// Networks returns the list of Network(s) managed by this controller.
89	Networks() []Network
90
91	// WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
92	WalkNetworks(walker NetworkWalker)
93
94	// NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned.
95	NetworkByName(name string) (Network, error)
96
97	// NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned.
98	NetworkByID(id string) (Network, error)
99
100	// NewSandbox creates a new network sandbox for the passed container id
101	NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error)
102
103	// Sandboxes returns the list of Sandbox(s) managed by this controller.
104	Sandboxes() []Sandbox
105
106	// WalkSandboxes uses the provided function to walk the Sandbox(s) managed by this controller.
107	WalkSandboxes(walker SandboxWalker)
108
109	// SandboxByID returns the Sandbox which has the passed id. If not found, a types.NotFoundError is returned.
110	SandboxByID(id string) (Sandbox, error)
111
112	// SandboxDestroy destroys a sandbox given a container ID
113	SandboxDestroy(id string) error
114
115	// Stop network controller
116	Stop()
117
118	// ReloadCondfiguration updates the controller configuration
119	ReloadConfiguration(cfgOptions ...config.Option) error
120
121	// SetClusterProvider sets cluster provider
122	SetClusterProvider(provider cluster.Provider)
123
124	// Wait for agent initialization complete in libnetwork controller
125	AgentInitWait()
126
127	// SetKeys configures the encryption key for gossip and overlay data path
128	SetKeys(keys []*types.EncryptionKey) error
129}
130
131// NetworkWalker is a client provided function which will be used to walk the Networks.
132// When the function returns true, the walk will stop.
133type NetworkWalker func(nw Network) bool
134
135// SandboxWalker is a client provided function which will be used to walk the Sandboxes.
136// When the function returns true, the walk will stop.
137type SandboxWalker func(sb Sandbox) bool
138
139type sandboxTable map[string]*sandbox
140
141type controller struct {
142	id                     string
143	drvRegistry            *drvregistry.DrvRegistry
144	sandboxes              sandboxTable
145	cfg                    *config.Config
146	stores                 []datastore.DataStore
147	discovery              hostdiscovery.HostDiscovery
148	extKeyListener         net.Listener
149	watchCh                chan *endpoint
150	unWatchCh              chan *endpoint
151	svcRecords             map[string]svcInfo
152	nmap                   map[string]*netWatch
153	serviceBindings        map[serviceKey]*service
154	defOsSbox              osl.Sandbox
155	ingressSandbox         *sandbox
156	sboxOnce               sync.Once
157	agent                  *agent
158	networkLocker          *locker.Locker
159	agentInitDone          chan struct{}
160	keys                   []*types.EncryptionKey
161	clusterConfigAvailable bool
162	sync.Mutex
163}
164
165type initializer struct {
166	fn    drvregistry.InitFunc
167	ntype string
168}
169
170// New creates a new instance of network controller.
171func New(cfgOptions ...config.Option) (NetworkController, error) {
172	c := &controller{
173		id:              stringid.GenerateRandomID(),
174		cfg:             config.ParseConfigOptions(cfgOptions...),
175		sandboxes:       sandboxTable{},
176		svcRecords:      make(map[string]svcInfo),
177		serviceBindings: make(map[serviceKey]*service),
178		agentInitDone:   make(chan struct{}),
179		networkLocker:   locker.New(),
180	}
181
182	if err := c.initStores(); err != nil {
183		return nil, err
184	}
185
186	drvRegistry, err := drvregistry.New(c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope), c.RegisterDriver, nil, c.cfg.PluginGetter)
187	if err != nil {
188		return nil, err
189	}
190
191	for _, i := range getInitializers() {
192		var dcfg map[string]interface{}
193
194		// External plugins don't need config passed through daemon. They can
195		// bootstrap themselves
196		if i.ntype != "remote" {
197			dcfg = c.makeDriverConfig(i.ntype)
198		}
199
200		if err := drvRegistry.AddDriver(i.ntype, i.fn, dcfg); err != nil {
201			return nil, err
202		}
203	}
204
205	if err = initIPAMDrivers(drvRegistry, nil, c.getStore(datastore.GlobalScope)); err != nil {
206		return nil, err
207	}
208
209	c.drvRegistry = drvRegistry
210
211	if c.cfg != nil && c.cfg.Cluster.Watcher != nil {
212		if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil {
213			// Failing to initialize discovery is a bad situation to be in.
214			// But it cannot fail creating the Controller
215			logrus.Errorf("Failed to Initialize Discovery : %v", err)
216		}
217	}
218
219	c.WalkNetworks(populateSpecial)
220
221	// Reserve pools first before doing cleanup. Otherwise the
222	// cleanups of endpoint/network and sandbox below will
223	// generate many unnecessary warnings
224	c.reservePools()
225
226	// Cleanup resources
227	c.sandboxCleanup(c.cfg.ActiveSandboxes)
228	c.cleanupLocalEndpoints()
229	c.networkCleanup()
230
231	if err := c.startExternalKeyListener(); err != nil {
232		return nil, err
233	}
234
235	return c, nil
236}
237
238func (c *controller) SetClusterProvider(provider cluster.Provider) {
239	c.Lock()
240	c.cfg.Daemon.ClusterProvider = provider
241	disableProviderCh := c.cfg.Daemon.DisableProvider
242	c.Unlock()
243	if provider != nil {
244		go c.clusterAgentInit()
245	} else {
246		disableProviderCh <- struct{}{}
247	}
248}
249
250func isValidClusteringIP(addr string) bool {
251	return addr != "" && !net.ParseIP(addr).IsLoopback() && !net.ParseIP(addr).IsUnspecified()
252}
253
254// libnetwork side of agent depends on the keys. On the first receipt of
255// keys setup the agent. For subsequent key set handle the key change
256func (c *controller) SetKeys(keys []*types.EncryptionKey) error {
257	c.Lock()
258	existingKeys := c.keys
259	clusterConfigAvailable := c.clusterConfigAvailable
260	agent := c.agent
261	c.Unlock()
262
263	subsysKeys := make(map[string]int)
264	for _, key := range keys {
265		if key.Subsystem != subsysGossip &&
266			key.Subsystem != subsysIPSec {
267			return fmt.Errorf("key received for unrecognized subsystem")
268		}
269		subsysKeys[key.Subsystem]++
270	}
271	for s, count := range subsysKeys {
272		if count != keyringSize {
273			return fmt.Errorf("incorrect number of keys for susbsystem %v", s)
274		}
275	}
276
277	if len(existingKeys) == 0 {
278		c.Lock()
279		c.keys = keys
280		c.Unlock()
281		if agent != nil {
282			return (fmt.Errorf("libnetwork agent setup without keys"))
283		}
284		if clusterConfigAvailable {
285			return c.agentSetup()
286		}
287		logrus.Debug("received encryption keys before cluster config")
288		return nil
289	}
290	if agent == nil {
291		c.Lock()
292		c.keys = keys
293		c.Unlock()
294		return nil
295	}
296	return c.handleKeyChange(keys)
297}
298
299func (c *controller) getAgent() *agent {
300	c.Lock()
301	defer c.Unlock()
302	return c.agent
303}
304
305func (c *controller) clusterAgentInit() {
306	clusterProvider := c.cfg.Daemon.ClusterProvider
307	for {
308		select {
309		case <-clusterProvider.ListenClusterEvents():
310			if !c.isDistributedControl() {
311				c.Lock()
312				c.clusterConfigAvailable = true
313				keys := c.keys
314				c.Unlock()
315				// agent initialization needs encyrption keys and bind/remote IP which
316				// comes from the daemon cluster events
317				if len(keys) > 0 {
318					c.agentSetup()
319				}
320			}
321		case <-c.cfg.Daemon.DisableProvider:
322			c.Lock()
323			c.clusterConfigAvailable = false
324			c.agentInitDone = make(chan struct{})
325			c.keys = nil
326			c.Unlock()
327
328			// We are leaving the cluster. Make sure we
329			// close the gossip so that we stop all
330			// incoming gossip updates before cleaning up
331			// any remaining service bindings. But before
332			// deleting the networks since the networks
333			// should still be present when cleaning up
334			// service bindings
335			c.agentClose()
336			c.cleanupServiceBindings("")
337
338			c.clearIngress(true)
339
340			return
341		}
342	}
343}
344
345// AgentInitWait waits for agent initialization to be completed in the
346// controller.
347func (c *controller) AgentInitWait() {
348	c.Lock()
349	agentInitDone := c.agentInitDone
350	c.Unlock()
351
352	if agentInitDone != nil {
353		<-agentInitDone
354	}
355}
356
357func (c *controller) makeDriverConfig(ntype string) map[string]interface{} {
358	if c.cfg == nil {
359		return nil
360	}
361
362	config := make(map[string]interface{})
363
364	for _, label := range c.cfg.Daemon.Labels {
365		if !strings.HasPrefix(netlabel.Key(label), netlabel.DriverPrefix+"."+ntype) {
366			continue
367		}
368
369		config[netlabel.Key(label)] = netlabel.Value(label)
370	}
371
372	drvCfg, ok := c.cfg.Daemon.DriverCfg[ntype]
373	if ok {
374		for k, v := range drvCfg.(map[string]interface{}) {
375			config[k] = v
376		}
377	}
378
379	for k, v := range c.cfg.Scopes {
380		if !v.IsValid() {
381			continue
382		}
383		config[netlabel.MakeKVClient(k)] = discoverapi.DatastoreConfigData{
384			Scope:    k,
385			Provider: v.Client.Provider,
386			Address:  v.Client.Address,
387			Config:   v.Client.Config,
388		}
389	}
390
391	return config
392}
393
394var procReloadConfig = make(chan (bool), 1)
395
396func (c *controller) ReloadConfiguration(cfgOptions ...config.Option) error {
397	procReloadConfig <- true
398	defer func() { <-procReloadConfig }()
399
400	// For now we accept the configuration reload only as a mean to provide a global store config after boot.
401	// Refuse the configuration if it alters an existing datastore client configuration.
402	update := false
403	cfg := config.ParseConfigOptions(cfgOptions...)
404
405	for s := range c.cfg.Scopes {
406		if _, ok := cfg.Scopes[s]; !ok {
407			return types.ForbiddenErrorf("cannot accept new configuration because it removes an existing datastore client")
408		}
409	}
410	for s, nSCfg := range cfg.Scopes {
411		if eSCfg, ok := c.cfg.Scopes[s]; ok {
412			if eSCfg.Client.Provider != nSCfg.Client.Provider ||
413				eSCfg.Client.Address != nSCfg.Client.Address {
414				return types.ForbiddenErrorf("cannot accept new configuration because it modifies an existing datastore client")
415			}
416		} else {
417			if err := c.initScopedStore(s, nSCfg); err != nil {
418				return err
419			}
420			update = true
421		}
422	}
423	if !update {
424		return nil
425	}
426
427	c.Lock()
428	c.cfg = cfg
429	c.Unlock()
430
431	var dsConfig *discoverapi.DatastoreConfigData
432	for scope, sCfg := range cfg.Scopes {
433		if scope == datastore.LocalScope || !sCfg.IsValid() {
434			continue
435		}
436		dsConfig = &discoverapi.DatastoreConfigData{
437			Scope:    scope,
438			Provider: sCfg.Client.Provider,
439			Address:  sCfg.Client.Address,
440			Config:   sCfg.Client.Config,
441		}
442		break
443	}
444	if dsConfig == nil {
445		return nil
446	}
447
448	c.drvRegistry.WalkIPAMs(func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool {
449		err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig)
450		if err != nil {
451			logrus.Errorf("Failed to set datastore in driver %s: %v", name, err)
452		}
453		return false
454	})
455
456	c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
457		err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig)
458		if err != nil {
459			logrus.Errorf("Failed to set datastore in driver %s: %v", name, err)
460		}
461		return false
462	})
463
464	if c.discovery == nil && c.cfg.Cluster.Watcher != nil {
465		if err := c.initDiscovery(c.cfg.Cluster.Watcher); err != nil {
466			logrus.Errorf("Failed to Initialize Discovery after configuration update: %v", err)
467		}
468	}
469
470	return nil
471}
472
473func (c *controller) ID() string {
474	return c.id
475}
476
477func (c *controller) BuiltinDrivers() []string {
478	drivers := []string{}
479	for _, i := range getInitializers() {
480		if i.ntype == "remote" {
481			continue
482		}
483		drivers = append(drivers, i.ntype)
484	}
485	return drivers
486}
487
488func (c *controller) validateHostDiscoveryConfig() bool {
489	if c.cfg == nil || c.cfg.Cluster.Discovery == "" || c.cfg.Cluster.Address == "" {
490		return false
491	}
492	return true
493}
494
495func (c *controller) clusterHostID() string {
496	c.Lock()
497	defer c.Unlock()
498	if c.cfg == nil || c.cfg.Cluster.Address == "" {
499		return ""
500	}
501	addr := strings.Split(c.cfg.Cluster.Address, ":")
502	return addr[0]
503}
504
505func (c *controller) isNodeAlive(node string) bool {
506	if c.discovery == nil {
507		return false
508	}
509
510	nodes := c.discovery.Fetch()
511	for _, n := range nodes {
512		if n.String() == node {
513			return true
514		}
515	}
516
517	return false
518}
519
520func (c *controller) initDiscovery(watcher discovery.Watcher) error {
521	if c.cfg == nil {
522		return fmt.Errorf("discovery initialization requires a valid configuration")
523	}
524
525	c.discovery = hostdiscovery.NewHostDiscovery(watcher)
526	return c.discovery.Watch(c.activeCallback, c.hostJoinCallback, c.hostLeaveCallback)
527}
528
529func (c *controller) activeCallback() {
530	ds := c.getStore(datastore.GlobalScope)
531	if ds != nil && !ds.Active() {
532		ds.RestartWatch()
533	}
534}
535
536func (c *controller) hostJoinCallback(nodes []net.IP) {
537	c.processNodeDiscovery(nodes, true)
538}
539
540func (c *controller) hostLeaveCallback(nodes []net.IP) {
541	c.processNodeDiscovery(nodes, false)
542}
543
544func (c *controller) processNodeDiscovery(nodes []net.IP, add bool) {
545	c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
546		c.pushNodeDiscovery(driver, capability, nodes, add)
547		return false
548	})
549}
550
551func (c *controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capability, nodes []net.IP, add bool) {
552	var self net.IP
553	if c.cfg != nil {
554		addr := strings.Split(c.cfg.Cluster.Address, ":")
555		self = net.ParseIP(addr[0])
556	}
557
558	if d == nil || cap.DataScope != datastore.GlobalScope || nodes == nil {
559		return
560	}
561
562	for _, node := range nodes {
563		nodeData := discoverapi.NodeDiscoveryData{Address: node.String(), Self: node.Equal(self)}
564		var err error
565		if add {
566			err = d.DiscoverNew(discoverapi.NodeDiscovery, nodeData)
567		} else {
568			err = d.DiscoverDelete(discoverapi.NodeDiscovery, nodeData)
569		}
570		if err != nil {
571			logrus.Debugf("discovery notification error : %v", err)
572		}
573	}
574}
575
576func (c *controller) Config() config.Config {
577	c.Lock()
578	defer c.Unlock()
579	if c.cfg == nil {
580		return config.Config{}
581	}
582	return *c.cfg
583}
584
585func (c *controller) isManager() bool {
586	c.Lock()
587	defer c.Unlock()
588	if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
589		return false
590	}
591	return c.cfg.Daemon.ClusterProvider.IsManager()
592}
593
594func (c *controller) isAgent() bool {
595	c.Lock()
596	defer c.Unlock()
597	if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
598		return false
599	}
600	return c.cfg.Daemon.ClusterProvider.IsAgent()
601}
602
603func (c *controller) isDistributedControl() bool {
604	return !c.isManager() && !c.isAgent()
605}
606
607func (c *controller) GetPluginGetter() plugingetter.PluginGetter {
608	return c.drvRegistry.GetPluginGetter()
609}
610
611func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
612	c.Lock()
613	hd := c.discovery
614	c.Unlock()
615
616	if hd != nil {
617		c.pushNodeDiscovery(driver, capability, hd.Fetch(), true)
618	}
619
620	c.agentDriverNotify(driver)
621	return nil
622}
623
624// NewNetwork creates a new network of the specified network type. The options
625// are network specific and modeled in a generic way.
626func (c *controller) NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error) {
627	if id != "" {
628		c.networkLocker.Lock(id)
629		defer c.networkLocker.Unlock(id)
630
631		if _, err := c.NetworkByID(id); err == nil {
632			return nil, NetworkNameError(id)
633		}
634	}
635
636	if err := config.ValidateName(name); err != nil {
637		return nil, ErrInvalidName(err.Error())
638	}
639
640	if id == "" {
641		id = stringid.GenerateRandomID()
642	}
643
644	defaultIpam := defaultIpamForNetworkType(networkType)
645	// Construct the network object
646	network := &network{
647		name:        name,
648		networkType: networkType,
649		generic:     map[string]interface{}{netlabel.GenericData: make(map[string]string)},
650		ipamType:    defaultIpam,
651		id:          id,
652		created:     time.Now(),
653		ctrlr:       c,
654		persist:     true,
655		drvOnce:     &sync.Once{},
656	}
657
658	network.processOptions(options...)
659
660	_, cap, err := network.resolveDriver(networkType, true)
661	if err != nil {
662		return nil, err
663	}
664
665	if cap.DataScope == datastore.GlobalScope && !c.isDistributedControl() && !network.dynamic {
666		if c.isManager() {
667			// For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager
668			return nil, ManagerRedirectError(name)
669		}
670
671		return nil, types.ForbiddenErrorf("Cannot create a multi-host network from a worker node. Please create the network from a manager node.")
672	}
673
674	// Make sure we have a driver available for this network type
675	// before we allocate anything.
676	if _, err := network.driver(true); err != nil {
677		return nil, err
678	}
679
680	err = network.ipamAllocate()
681	if err != nil {
682		return nil, err
683	}
684	defer func() {
685		if err != nil {
686			network.ipamRelease()
687		}
688	}()
689
690	err = c.addNetwork(network)
691	if err != nil {
692		return nil, err
693	}
694	defer func() {
695		if err != nil {
696			if e := network.deleteNetwork(); e != nil {
697				logrus.Warnf("couldn't roll back driver network on network %s creation failure: %v", network.name, err)
698			}
699		}
700	}()
701
702	// First store the endpoint count, then the network. To avoid to
703	// end up with a datastore containing a network and not an epCnt,
704	// in case of an ungraceful shutdown during this function call.
705	epCnt := &endpointCnt{n: network}
706	if err = c.updateToStore(epCnt); err != nil {
707		return nil, err
708	}
709	defer func() {
710		if err != nil {
711			if e := c.deleteFromStore(epCnt); e != nil {
712				logrus.Warnf("could not rollback from store, epCnt %v on failure (%v): %v", epCnt, err, e)
713			}
714		}
715	}()
716
717	network.epCnt = epCnt
718	if err = c.updateToStore(network); err != nil {
719		return nil, err
720	}
721
722	joinCluster(network)
723	if !c.isDistributedControl() {
724		arrangeIngressFilterRule()
725	}
726
727	return network, nil
728}
729
730var joinCluster NetworkWalker = func(nw Network) bool {
731	n := nw.(*network)
732	if err := n.joinCluster(); err != nil {
733		logrus.Errorf("Failed to join network %s (%s) into agent cluster: %v", n.Name(), n.ID(), err)
734	}
735	n.addDriverWatches()
736	return false
737}
738
739func (c *controller) reservePools() {
740	networks, err := c.getNetworksForScope(datastore.LocalScope)
741	if err != nil {
742		logrus.Warnf("Could not retrieve networks from local store during ipam allocation for existing networks: %v", err)
743		return
744	}
745
746	for _, n := range networks {
747		if !doReplayPoolReserve(n) {
748			continue
749		}
750		// Construct pseudo configs for the auto IP case
751		autoIPv4 := (len(n.ipamV4Config) == 0 || (len(n.ipamV4Config) == 1 && n.ipamV4Config[0].PreferredPool == "")) && len(n.ipamV4Info) > 0
752		autoIPv6 := (len(n.ipamV6Config) == 0 || (len(n.ipamV6Config) == 1 && n.ipamV6Config[0].PreferredPool == "")) && len(n.ipamV6Info) > 0
753		if autoIPv4 {
754			n.ipamV4Config = []*IpamConf{{PreferredPool: n.ipamV4Info[0].Pool.String()}}
755		}
756		if n.enableIPv6 && autoIPv6 {
757			n.ipamV6Config = []*IpamConf{{PreferredPool: n.ipamV6Info[0].Pool.String()}}
758		}
759		// Account current network gateways
760		for i, c := range n.ipamV4Config {
761			if c.Gateway == "" && n.ipamV4Info[i].Gateway != nil {
762				c.Gateway = n.ipamV4Info[i].Gateway.IP.String()
763			}
764		}
765		if n.enableIPv6 {
766			for i, c := range n.ipamV6Config {
767				if c.Gateway == "" && n.ipamV6Info[i].Gateway != nil {
768					c.Gateway = n.ipamV6Info[i].Gateway.IP.String()
769				}
770			}
771		}
772		// Reserve pools
773		if err := n.ipamAllocate(); err != nil {
774			logrus.Warnf("Failed to allocate ipam pool(s) for network %q (%s): %v", n.Name(), n.ID(), err)
775		}
776		// Reserve existing endpoints' addresses
777		ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
778		if err != nil {
779			logrus.Warnf("Failed to retrieve ipam driver for network %q (%s) during address reservation", n.Name(), n.ID())
780			continue
781		}
782		epl, err := n.getEndpointsFromStore()
783		if err != nil {
784			logrus.Warnf("Failed to retrieve list of current endpoints on network %q (%s)", n.Name(), n.ID())
785			continue
786		}
787		for _, ep := range epl {
788			if err := ep.assignAddress(ipam, true, ep.Iface().AddressIPv6() != nil); err != nil {
789				logrus.Warnf("Failed to reserve current adress for endpoint %q (%s) on network %q (%s)",
790					ep.Name(), ep.ID(), n.Name(), n.ID())
791			}
792		}
793	}
794}
795
796func doReplayPoolReserve(n *network) bool {
797	_, caps, err := n.getController().getIPAMDriver(n.ipamType)
798	if err != nil {
799		logrus.Warnf("Failed to retrieve ipam driver for network %q (%s): %v", n.Name(), n.ID(), err)
800		return false
801	}
802	return caps.RequiresRequestReplay
803}
804
805func (c *controller) addNetwork(n *network) error {
806	d, err := n.driver(true)
807	if err != nil {
808		return err
809	}
810
811	// Create the network
812	if err := d.CreateNetwork(n.id, n.generic, n, n.getIPData(4), n.getIPData(6)); err != nil {
813		return err
814	}
815
816	n.startResolver()
817
818	return nil
819}
820
821func (c *controller) Networks() []Network {
822	var list []Network
823
824	networks, err := c.getNetworksFromStore()
825	if err != nil {
826		logrus.Error(err)
827	}
828
829	for _, n := range networks {
830		if n.inDelete {
831			continue
832		}
833		list = append(list, n)
834	}
835
836	return list
837}
838
839func (c *controller) WalkNetworks(walker NetworkWalker) {
840	for _, n := range c.Networks() {
841		if walker(n) {
842			return
843		}
844	}
845}
846
847func (c *controller) NetworkByName(name string) (Network, error) {
848	if name == "" {
849		return nil, ErrInvalidName(name)
850	}
851	var n Network
852
853	s := func(current Network) bool {
854		if current.Name() == name {
855			n = current
856			return true
857		}
858		return false
859	}
860
861	c.WalkNetworks(s)
862
863	if n == nil {
864		return nil, ErrNoSuchNetwork(name)
865	}
866
867	return n, nil
868}
869
870func (c *controller) NetworkByID(id string) (Network, error) {
871	if id == "" {
872		return nil, ErrInvalidID(id)
873	}
874
875	n, err := c.getNetworkFromStore(id)
876	if err != nil {
877		return nil, ErrNoSuchNetwork(id)
878	}
879
880	return n, nil
881}
882
883// NewSandbox creates a new sandbox for the passed container id
884func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (sBox Sandbox, err error) {
885	if containerID == "" {
886		return nil, types.BadRequestErrorf("invalid container ID")
887	}
888
889	var sb *sandbox
890	c.Lock()
891	for _, s := range c.sandboxes {
892		if s.containerID == containerID {
893			// If not a stub, then we already have a complete sandbox.
894			if !s.isStub {
895				sbID := s.ID()
896				c.Unlock()
897				return nil, types.ForbiddenErrorf("container %s is already present in sandbox %s", containerID, sbID)
898			}
899
900			// We already have a stub sandbox from the
901			// store. Make use of it so that we don't lose
902			// the endpoints from store but reset the
903			// isStub flag.
904			sb = s
905			sb.isStub = false
906			break
907		}
908	}
909	c.Unlock()
910
911	// Create sandbox and process options first. Key generation depends on an option
912	if sb == nil {
913		sb = &sandbox{
914			id:                 stringid.GenerateRandomID(),
915			containerID:        containerID,
916			endpoints:          epHeap{},
917			epPriority:         map[string]int{},
918			populatedEndpoints: map[string]struct{}{},
919			config:             containerConfig{},
920			controller:         c,
921		}
922	}
923	sBox = sb
924
925	heap.Init(&sb.endpoints)
926
927	sb.processOptions(options...)
928
929	c.Lock()
930	if sb.ingress && c.ingressSandbox != nil {
931		c.Unlock()
932		return nil, types.ForbiddenErrorf("ingress sandbox already present")
933	}
934
935	if sb.ingress {
936		c.ingressSandbox = sb
937		sb.id = "ingress_sbox"
938	}
939	c.Unlock()
940	defer func() {
941		if err != nil {
942			c.Lock()
943			if sb.ingress {
944				c.ingressSandbox = nil
945			}
946			c.Unlock()
947		}
948	}()
949
950	if err = sb.setupResolutionFiles(); err != nil {
951		return nil, err
952	}
953
954	if sb.config.useDefaultSandBox {
955		c.sboxOnce.Do(func() {
956			c.defOsSbox, err = osl.NewSandbox(sb.Key(), false, false)
957		})
958
959		if err != nil {
960			c.sboxOnce = sync.Once{}
961			return nil, fmt.Errorf("failed to create default sandbox: %v", err)
962		}
963
964		sb.osSbox = c.defOsSbox
965	}
966
967	if sb.osSbox == nil && !sb.config.useExternalKey {
968		if sb.osSbox, err = osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox, false); err != nil {
969			return nil, fmt.Errorf("failed to create new osl sandbox: %v", err)
970		}
971	}
972
973	c.Lock()
974	c.sandboxes[sb.id] = sb
975	c.Unlock()
976	defer func() {
977		if err != nil {
978			c.Lock()
979			delete(c.sandboxes, sb.id)
980			c.Unlock()
981		}
982	}()
983
984	err = sb.storeUpdate()
985	if err != nil {
986		return nil, fmt.Errorf("updating the store state of sandbox failed: %v", err)
987	}
988
989	return sb, nil
990}
991
992func (c *controller) Sandboxes() []Sandbox {
993	c.Lock()
994	defer c.Unlock()
995
996	list := make([]Sandbox, 0, len(c.sandboxes))
997	for _, s := range c.sandboxes {
998		// Hide stub sandboxes from libnetwork users
999		if s.isStub {
1000			continue
1001		}
1002
1003		list = append(list, s)
1004	}
1005
1006	return list
1007}
1008
1009func (c *controller) WalkSandboxes(walker SandboxWalker) {
1010	for _, sb := range c.Sandboxes() {
1011		if walker(sb) {
1012			return
1013		}
1014	}
1015}
1016
1017func (c *controller) SandboxByID(id string) (Sandbox, error) {
1018	if id == "" {
1019		return nil, ErrInvalidID(id)
1020	}
1021	c.Lock()
1022	s, ok := c.sandboxes[id]
1023	c.Unlock()
1024	if !ok {
1025		return nil, types.NotFoundErrorf("sandbox %s not found", id)
1026	}
1027	return s, nil
1028}
1029
1030// SandboxDestroy destroys a sandbox given a container ID
1031func (c *controller) SandboxDestroy(id string) error {
1032	var sb *sandbox
1033	c.Lock()
1034	for _, s := range c.sandboxes {
1035		if s.containerID == id {
1036			sb = s
1037			break
1038		}
1039	}
1040	c.Unlock()
1041
1042	// It is not an error if sandbox is not available
1043	if sb == nil {
1044		return nil
1045	}
1046
1047	return sb.Delete()
1048}
1049
1050// SandboxContainerWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed containerID
1051func SandboxContainerWalker(out *Sandbox, containerID string) SandboxWalker {
1052	return func(sb Sandbox) bool {
1053		if sb.ContainerID() == containerID {
1054			*out = sb
1055			return true
1056		}
1057		return false
1058	}
1059}
1060
1061// SandboxKeyWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed key
1062func SandboxKeyWalker(out *Sandbox, key string) SandboxWalker {
1063	return func(sb Sandbox) bool {
1064		if sb.Key() == key {
1065			*out = sb
1066			return true
1067		}
1068		return false
1069	}
1070}
1071
1072func (c *controller) loadDriver(networkType string) error {
1073	var err error
1074
1075	if pg := c.GetPluginGetter(); pg != nil {
1076		_, err = pg.Get(networkType, driverapi.NetworkPluginEndpointType, plugingetter.LOOKUP)
1077	} else {
1078		_, err = plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
1079	}
1080
1081	if err != nil {
1082		if err == plugins.ErrNotFound {
1083			return types.NotFoundErrorf(err.Error())
1084		}
1085		return err
1086	}
1087
1088	return nil
1089}
1090
1091func (c *controller) loadIPAMDriver(name string) error {
1092	var err error
1093
1094	if pg := c.GetPluginGetter(); pg != nil {
1095		_, err = pg.Get(name, ipamapi.PluginEndpointType, plugingetter.LOOKUP)
1096	} else {
1097		_, err = plugins.Get(name, ipamapi.PluginEndpointType)
1098	}
1099
1100	if err != nil {
1101		if err == plugins.ErrNotFound {
1102			return types.NotFoundErrorf(err.Error())
1103		}
1104		return err
1105	}
1106
1107	return nil
1108}
1109
1110func (c *controller) getIPAMDriver(name string) (ipamapi.Ipam, *ipamapi.Capability, error) {
1111	id, cap := c.drvRegistry.IPAM(name)
1112	if id == nil {
1113		// Might be a plugin name. Try loading it
1114		if err := c.loadIPAMDriver(name); err != nil {
1115			return nil, nil, err
1116		}
1117
1118		// Now that we resolved the plugin, try again looking up the registry
1119		id, cap = c.drvRegistry.IPAM(name)
1120		if id == nil {
1121			return nil, nil, types.BadRequestErrorf("invalid ipam driver: %q", name)
1122		}
1123	}
1124
1125	return id, cap, nil
1126}
1127
1128func (c *controller) Stop() {
1129	c.clearIngress(false)
1130	c.closeStores()
1131	c.stopExternalKeyListener()
1132	osl.GC()
1133}
1134
1135func (c *controller) clearIngress(clusterLeave bool) {
1136	c.Lock()
1137	ingressSandbox := c.ingressSandbox
1138	c.ingressSandbox = nil
1139	c.Unlock()
1140
1141	if ingressSandbox != nil {
1142		if err := ingressSandbox.Delete(); err != nil {
1143			logrus.Warnf("Could not delete ingress sandbox while leaving: %v", err)
1144		}
1145	}
1146
1147	n, err := c.NetworkByName("ingress")
1148	if err != nil && clusterLeave {
1149		logrus.Warnf("Could not find ingress network while leaving: %v", err)
1150	}
1151
1152	if n != nil {
1153		if err := n.Delete(); err != nil {
1154			logrus.Warnf("Could not delete ingress network while leaving: %v", err)
1155		}
1156	}
1157}
1158