2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-10-08 17:59:47 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/discoverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-10-08 17:59:47 -04:00
|
|
|
)
|
|
|
|
|
2016-06-10 11:30:56 -04:00
|
|
|
const (
|
|
|
|
// network config prefix was not specific enough.
|
|
|
|
// To be backward compatible, need custom endpoint
|
|
|
|
// prefix with different root
|
|
|
|
bridgePrefix = "bridge"
|
|
|
|
bridgeEndpointPrefix = "bridge-endpoint"
|
|
|
|
)
|
2015-10-08 17:59:47 -04:00
|
|
|
|
|
|
|
func (d *driver) initStore(option map[string]interface{}) error {
|
2016-01-27 19:37:47 -05:00
|
|
|
if data, ok := option[netlabel.LocalKVClient]; ok {
|
|
|
|
var err error
|
|
|
|
dsc, ok := data.(discoverapi.DatastoreConfigData)
|
|
|
|
if !ok {
|
|
|
|
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
|
2015-10-08 17:59:47 -04:00
|
|
|
}
|
2016-01-27 19:37:47 -05:00
|
|
|
d.store, err = datastore.NewDataStoreFromConfig(dsc)
|
2015-10-08 17:59:47 -04:00
|
|
|
if err != nil {
|
2016-01-27 19:37:47 -05:00
|
|
|
return types.InternalErrorf("bridge driver failed to initialize data store: %v", err)
|
2015-10-08 17:59:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-10 11:30:56 -04:00
|
|
|
err = d.populateNetworks()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.populateEndpoints()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-08 17:59:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) populateNetworks() error {
|
|
|
|
kvol, err := d.store.List(datastore.Key(bridgePrefix), &networkConfiguration{})
|
2016-02-26 13:05:47 -05:00
|
|
|
if err != nil && err != datastore.ErrKeyNotFound {
|
2015-10-08 17:59:47 -04:00
|
|
|
return fmt.Errorf("failed to get bridge network configurations from store: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's normal for network configuration state to be empty. Just return.
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kvo := range kvol {
|
|
|
|
ncfg := kvo.(*networkConfiguration)
|
|
|
|
if err = d.createNetwork(ncfg); err != nil {
|
2016-05-16 14:51:40 -04:00
|
|
|
logrus.Warnf("could not create bridge network for id %s bridge name %s while booting up from persistent state: %v", ncfg.ID, ncfg.BridgeName, err)
|
2015-10-08 17:59:47 -04:00
|
|
|
}
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Debugf("Network (%.7s) restored", ncfg.ID)
|
2016-06-10 11:30:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) populateEndpoints() error {
|
|
|
|
kvol, err := d.store.List(datastore.Key(bridgeEndpointPrefix), &bridgeEndpoint{})
|
|
|
|
if err != nil && err != datastore.ErrKeyNotFound {
|
|
|
|
return fmt.Errorf("failed to get bridge endpoints from store: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == datastore.ErrKeyNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kvo := range kvol {
|
|
|
|
ep := kvo.(*bridgeEndpoint)
|
|
|
|
n, ok := d.networks[ep.nid]
|
|
|
|
if !ok {
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Debugf("Network (%.7s) not found for restored bridge endpoint (%.7s)", ep.nid, ep.id)
|
|
|
|
logrus.Debugf("Deleting stale bridge endpoint (%.7s) from store", ep.id)
|
2016-06-10 11:30:56 -04:00
|
|
|
if err := d.storeDelete(ep); err != nil {
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Debugf("Failed to delete stale bridge endpoint (%.7s) from store", ep.id)
|
2016-06-10 11:30:56 -04:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n.endpoints[ep.id] = ep
|
|
|
|
n.restorePortAllocations(ep)
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Debugf("Endpoint (%.7s) restored to network (%.7s)", ep.id, ep.nid)
|
2015-10-08 17:59:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
|
|
|
|
if d.store == nil {
|
|
|
|
logrus.Warnf("bridge store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.store.PutObjectAtomic(kvObject); err != nil {
|
|
|
|
return fmt.Errorf("failed to update bridge store for object type %T: %v", kvObject, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) storeDelete(kvObject datastore.KVObject) error {
|
|
|
|
if d.store == nil {
|
|
|
|
logrus.Debugf("bridge store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retry:
|
|
|
|
if err := d.store.DeleteObjectAtomic(kvObject); err != nil {
|
|
|
|
if err == datastore.ErrKeyModified {
|
|
|
|
if err := d.store.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
|
|
|
|
return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
|
|
|
|
}
|
|
|
|
goto retry
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) MarshalJSON() ([]byte, error) {
|
|
|
|
nMap := make(map[string]interface{})
|
|
|
|
nMap["ID"] = ncfg.ID
|
|
|
|
nMap["BridgeName"] = ncfg.BridgeName
|
|
|
|
nMap["EnableIPv6"] = ncfg.EnableIPv6
|
|
|
|
nMap["EnableIPMasquerade"] = ncfg.EnableIPMasquerade
|
|
|
|
nMap["EnableICC"] = ncfg.EnableICC
|
2018-12-23 19:05:20 -05:00
|
|
|
nMap["InhibitIPv4"] = ncfg.InhibitIPv4
|
2015-10-08 17:59:47 -04:00
|
|
|
nMap["Mtu"] = ncfg.Mtu
|
2016-04-05 20:02:05 -04:00
|
|
|
nMap["Internal"] = ncfg.Internal
|
2015-10-08 17:59:47 -04:00
|
|
|
nMap["DefaultBridge"] = ncfg.DefaultBridge
|
|
|
|
nMap["DefaultBindingIP"] = ncfg.DefaultBindingIP.String()
|
2019-09-25 01:08:25 -04:00
|
|
|
nMap["HostIP"] = ncfg.HostIP.String()
|
2015-10-08 17:59:47 -04:00
|
|
|
nMap["DefaultGatewayIPv4"] = ncfg.DefaultGatewayIPv4.String()
|
|
|
|
nMap["DefaultGatewayIPv6"] = ncfg.DefaultGatewayIPv6.String()
|
2017-02-27 17:23:12 -05:00
|
|
|
nMap["ContainerIfacePrefix"] = ncfg.ContainerIfacePrefix
|
2016-06-27 23:42:50 -04:00
|
|
|
nMap["BridgeIfaceCreator"] = ncfg.BridgeIfaceCreator
|
2015-10-08 17:59:47 -04:00
|
|
|
|
|
|
|
if ncfg.AddressIPv4 != nil {
|
|
|
|
nMap["AddressIPv4"] = ncfg.AddressIPv4.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if ncfg.AddressIPv6 != nil {
|
|
|
|
nMap["AddressIPv6"] = ncfg.AddressIPv6.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(nMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) UnmarshalJSON(b []byte) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
nMap map[string]interface{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = json.Unmarshal(b, &nMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := nMap["AddressIPv4"]; ok {
|
|
|
|
if ncfg.AddressIPv4, err = types.ParseCIDR(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode bridge network address IPv4 after json unmarshal: %s", v.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := nMap["AddressIPv6"]; ok {
|
|
|
|
if ncfg.AddressIPv6, err = types.ParseCIDR(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode bridge network address IPv6 after json unmarshal: %s", v.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-27 17:23:12 -05:00
|
|
|
if v, ok := nMap["ContainerIfacePrefix"]; ok {
|
|
|
|
ncfg.ContainerIfacePrefix = v.(string)
|
|
|
|
}
|
|
|
|
|
2019-09-25 01:08:25 -04:00
|
|
|
if v, ok := nMap["HostIP"]; ok {
|
|
|
|
ncfg.HostIP = net.ParseIP(v.(string))
|
|
|
|
}
|
|
|
|
|
2015-10-09 06:53:22 -04:00
|
|
|
ncfg.DefaultBridge = nMap["DefaultBridge"].(bool)
|
2015-10-08 17:59:47 -04:00
|
|
|
ncfg.DefaultBindingIP = net.ParseIP(nMap["DefaultBindingIP"].(string))
|
|
|
|
ncfg.DefaultGatewayIPv4 = net.ParseIP(nMap["DefaultGatewayIPv4"].(string))
|
|
|
|
ncfg.DefaultGatewayIPv6 = net.ParseIP(nMap["DefaultGatewayIPv6"].(string))
|
|
|
|
ncfg.ID = nMap["ID"].(string)
|
|
|
|
ncfg.BridgeName = nMap["BridgeName"].(string)
|
|
|
|
ncfg.EnableIPv6 = nMap["EnableIPv6"].(bool)
|
|
|
|
ncfg.EnableIPMasquerade = nMap["EnableIPMasquerade"].(bool)
|
|
|
|
ncfg.EnableICC = nMap["EnableICC"].(bool)
|
2020-03-02 10:38:13 -05:00
|
|
|
if v, ok := nMap["InhibitIPv4"]; ok {
|
|
|
|
ncfg.InhibitIPv4 = v.(bool)
|
|
|
|
}
|
|
|
|
|
2015-10-08 17:59:47 -04:00
|
|
|
ncfg.Mtu = int(nMap["Mtu"].(float64))
|
2016-04-05 20:02:05 -04:00
|
|
|
if v, ok := nMap["Internal"]; ok {
|
|
|
|
ncfg.Internal = v.(bool)
|
|
|
|
}
|
2015-10-08 17:59:47 -04:00
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
if v, ok := nMap["BridgeIfaceCreator"]; ok {
|
|
|
|
ncfg.BridgeIfaceCreator = ifaceCreator(v.(float64))
|
|
|
|
}
|
|
|
|
|
2015-10-08 17:59:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) Key() []string {
|
|
|
|
return []string{bridgePrefix, ncfg.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) KeyPrefix() []string {
|
|
|
|
return []string{bridgePrefix}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) Value() []byte {
|
|
|
|
b, err := json.Marshal(ncfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) SetValue(value []byte) error {
|
|
|
|
return json.Unmarshal(value, ncfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) Index() uint64 {
|
|
|
|
return ncfg.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) SetIndex(index uint64) {
|
|
|
|
ncfg.dbIndex = index
|
|
|
|
ncfg.dbExists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) Exists() bool {
|
|
|
|
return ncfg.dbExists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) Skip() bool {
|
2016-06-10 20:32:19 -04:00
|
|
|
return false
|
2015-10-08 17:59:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) New() datastore.KVObject {
|
|
|
|
return &networkConfiguration{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) CopyTo(o datastore.KVObject) error {
|
|
|
|
dstNcfg := o.(*networkConfiguration)
|
|
|
|
*dstNcfg = *ncfg
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ncfg *networkConfiguration) DataScope() string {
|
|
|
|
return datastore.LocalScope
|
|
|
|
}
|
2016-06-10 11:30:56 -04:00
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) MarshalJSON() ([]byte, error) {
|
|
|
|
epMap := make(map[string]interface{})
|
|
|
|
epMap["id"] = ep.id
|
|
|
|
epMap["nid"] = ep.nid
|
|
|
|
epMap["SrcName"] = ep.srcName
|
|
|
|
epMap["MacAddress"] = ep.macAddress.String()
|
|
|
|
epMap["Addr"] = ep.addr.String()
|
|
|
|
if ep.addrv6 != nil {
|
|
|
|
epMap["Addrv6"] = ep.addrv6.String()
|
|
|
|
}
|
|
|
|
epMap["Config"] = ep.config
|
|
|
|
epMap["ContainerConfig"] = ep.containerConfig
|
|
|
|
epMap["ExternalConnConfig"] = ep.extConnConfig
|
|
|
|
epMap["PortMapping"] = ep.portMapping
|
|
|
|
|
|
|
|
return json.Marshal(epMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) UnmarshalJSON(b []byte) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
epMap map[string]interface{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = json.Unmarshal(b, &epMap); err != nil {
|
|
|
|
return fmt.Errorf("Failed to unmarshal to bridge endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := epMap["MacAddress"]; ok {
|
|
|
|
if ep.macAddress, err = net.ParseMAC(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode bridge endpoint MAC address (%s) after json unmarshal: %v", v.(string), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := epMap["Addr"]; ok {
|
|
|
|
if ep.addr, err = types.ParseCIDR(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode bridge endpoint IPv4 address (%s) after json unmarshal: %v", v.(string), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := epMap["Addrv6"]; ok {
|
|
|
|
if ep.addrv6, err = types.ParseCIDR(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode bridge endpoint IPv6 address (%s) after json unmarshal: %v", v.(string), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ep.id = epMap["id"].(string)
|
|
|
|
ep.nid = epMap["nid"].(string)
|
|
|
|
ep.srcName = epMap["SrcName"].(string)
|
|
|
|
d, _ := json.Marshal(epMap["Config"])
|
|
|
|
if err := json.Unmarshal(d, &ep.config); err != nil {
|
|
|
|
logrus.Warnf("Failed to decode endpoint config %v", err)
|
|
|
|
}
|
|
|
|
d, _ = json.Marshal(epMap["ContainerConfig"])
|
|
|
|
if err := json.Unmarshal(d, &ep.containerConfig); err != nil {
|
|
|
|
logrus.Warnf("Failed to decode endpoint container config %v", err)
|
|
|
|
}
|
|
|
|
d, _ = json.Marshal(epMap["ExternalConnConfig"])
|
|
|
|
if err := json.Unmarshal(d, &ep.extConnConfig); err != nil {
|
|
|
|
logrus.Warnf("Failed to decode endpoint external connectivity configuration %v", err)
|
|
|
|
}
|
|
|
|
d, _ = json.Marshal(epMap["PortMapping"])
|
|
|
|
if err := json.Unmarshal(d, &ep.portMapping); err != nil {
|
|
|
|
logrus.Warnf("Failed to decode endpoint port mapping %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) Key() []string {
|
|
|
|
return []string{bridgeEndpointPrefix, ep.id}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) KeyPrefix() []string {
|
|
|
|
return []string{bridgeEndpointPrefix}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) Value() []byte {
|
|
|
|
b, err := json.Marshal(ep)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) SetValue(value []byte) error {
|
|
|
|
return json.Unmarshal(value, ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) Index() uint64 {
|
|
|
|
return ep.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) SetIndex(index uint64) {
|
|
|
|
ep.dbIndex = index
|
|
|
|
ep.dbExists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) Exists() bool {
|
|
|
|
return ep.dbExists
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) Skip() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) New() datastore.KVObject {
|
|
|
|
return &bridgeEndpoint{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) CopyTo(o datastore.KVObject) error {
|
|
|
|
dstEp := o.(*bridgeEndpoint)
|
|
|
|
*dstEp = *ep
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *bridgeEndpoint) DataScope() string {
|
|
|
|
return datastore.LocalScope
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *bridgeNetwork) restorePortAllocations(ep *bridgeEndpoint) {
|
|
|
|
if ep.extConnConfig == nil ||
|
|
|
|
ep.extConnConfig.ExposedPorts == nil ||
|
|
|
|
ep.extConnConfig.PortBindings == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tmp := ep.extConnConfig.PortBindings
|
|
|
|
ep.extConnConfig.PortBindings = ep.portMapping
|
|
|
|
_, err := n.allocatePorts(ep, n.config.DefaultBindingIP, n.driver.config.EnableUserlandProxy)
|
|
|
|
if err != nil {
|
2018-07-05 16:33:01 -04:00
|
|
|
logrus.Warnf("Failed to reserve existing port mapping for endpoint %.7s:%v", ep.id, err)
|
2016-06-10 11:30:56 -04:00
|
|
|
}
|
|
|
|
ep.extConnConfig.PortBindings = tmp
|
|
|
|
}
|