2015-05-14 02:23:45 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-06-05 16:31:12 -04:00
|
|
|
"encoding/json"
|
2015-10-05 07:21:15 -04:00
|
|
|
"fmt"
|
2015-05-14 02:23:45 -04:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-05-20 16:28:46 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-14 02:23:45 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// EndpointInfo provides an interface to retrieve network resources bound to the endpoint.
|
|
|
|
type EndpointInfo interface {
|
2015-09-09 19:06:35 -04:00
|
|
|
// Iface returns InterfaceInfo, go interface that can be used
|
|
|
|
// to get more information on the interface which was assigned to
|
|
|
|
// the endpoint by the driver. This can be used after the
|
|
|
|
// endpoint has been created.
|
|
|
|
Iface() InterfaceInfo
|
2015-05-14 02:23:45 -04:00
|
|
|
|
|
|
|
// Gateway returns the IPv4 gateway assigned by the driver.
|
|
|
|
// This will only return a valid value if a container has joined the endpoint.
|
|
|
|
Gateway() net.IP
|
|
|
|
|
|
|
|
// GatewayIPv6 returns the IPv6 gateway assigned by the driver.
|
|
|
|
// This will only return a valid value if a container has joined the endpoint.
|
|
|
|
GatewayIPv6() net.IP
|
|
|
|
|
2015-12-02 18:21:50 -05:00
|
|
|
// StaticRoutes returns the list of static routes configured by the network
|
|
|
|
// driver when the container joins a network
|
|
|
|
StaticRoutes() []*types.StaticRoute
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// Sandbox returns the attached sandbox if there, nil otherwise.
|
|
|
|
Sandbox() Sandbox
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceInfo provides an interface to retrieve interface addresses bound to the endpoint.
|
|
|
|
type InterfaceInfo interface {
|
|
|
|
// MacAddress returns the MAC address assigned to the endpoint.
|
|
|
|
MacAddress() net.HardwareAddr
|
|
|
|
|
|
|
|
// Address returns the IPv4 address assigned to the endpoint.
|
2015-10-03 19:11:50 -04:00
|
|
|
Address() *net.IPNet
|
2015-05-14 02:23:45 -04:00
|
|
|
|
|
|
|
// AddressIPv6 returns the IPv6 address assigned to the endpoint.
|
2015-10-03 19:11:50 -04:00
|
|
|
AddressIPv6() *net.IPNet
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type endpointInterface struct {
|
2015-05-21 14:04:49 -04:00
|
|
|
mac net.HardwareAddr
|
2015-10-03 19:11:50 -04:00
|
|
|
addr *net.IPNet
|
|
|
|
addrv6 *net.IPNet
|
2015-05-21 14:04:49 -04:00
|
|
|
srcName string
|
|
|
|
dstPrefix string
|
2015-05-19 20:08:56 -04:00
|
|
|
routes []*net.IPNet
|
2015-10-04 00:25:57 -04:00
|
|
|
v4PoolID string
|
|
|
|
v6PoolID string
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
func (epi *endpointInterface) MarshalJSON() ([]byte, error) {
|
|
|
|
epMap := make(map[string]interface{})
|
2015-10-03 19:11:50 -04:00
|
|
|
if epi.mac != nil {
|
|
|
|
epMap["mac"] = epi.mac.String()
|
|
|
|
}
|
|
|
|
if epi.addr != nil {
|
|
|
|
epMap["addr"] = epi.addr.String()
|
|
|
|
}
|
|
|
|
if epi.addrv6 != nil {
|
|
|
|
epMap["addrv6"] = epi.addrv6.String()
|
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
epMap["srcName"] = epi.srcName
|
|
|
|
epMap["dstPrefix"] = epi.dstPrefix
|
|
|
|
var routes []string
|
|
|
|
for _, route := range epi.routes {
|
|
|
|
routes = append(routes, route.String())
|
|
|
|
}
|
|
|
|
epMap["routes"] = routes
|
2015-10-04 00:25:57 -04:00
|
|
|
epMap["v4PoolID"] = epi.v4PoolID
|
|
|
|
epMap["v6PoolID"] = epi.v6PoolID
|
2015-06-05 16:31:12 -04:00
|
|
|
return json.Marshal(epMap)
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (epi *endpointInterface) UnmarshalJSON(b []byte) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
epMap map[string]interface{}
|
|
|
|
)
|
|
|
|
if err = json.Unmarshal(b, &epMap); err != nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
if v, ok := epMap["mac"]; ok {
|
|
|
|
if epi.mac, err = net.ParseMAC(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode endpoint interface mac address after json unmarshal: %s", v.(string))
|
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
if v, ok := epMap["addr"]; ok {
|
|
|
|
if epi.addr, err = types.ParseCIDR(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode endpoint interface ipv4 address after json unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := epMap["addrv6"]; ok {
|
|
|
|
if epi.addrv6, err = types.ParseCIDR(v.(string)); err != nil {
|
|
|
|
return types.InternalErrorf("failed to decode endpoint interface ipv6 address after json unmarshal: %v", err)
|
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
epi.srcName = epMap["srcName"].(string)
|
|
|
|
epi.dstPrefix = epMap["dstPrefix"].(string)
|
|
|
|
|
|
|
|
rb, _ := json.Marshal(epMap["routes"])
|
|
|
|
var routes []string
|
|
|
|
json.Unmarshal(rb, &routes)
|
|
|
|
epi.routes = make([]*net.IPNet, 0)
|
|
|
|
for _, route := range routes {
|
2015-06-18 21:49:34 -04:00
|
|
|
ip, ipr, err := net.ParseCIDR(route)
|
2015-06-05 16:31:12 -04:00
|
|
|
if err == nil {
|
2015-06-18 21:49:34 -04:00
|
|
|
ipr.IP = ip
|
2015-06-05 16:31:12 -04:00
|
|
|
epi.routes = append(epi.routes, ipr)
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 00:25:57 -04:00
|
|
|
epi.v4PoolID = epMap["v4PoolID"].(string)
|
|
|
|
epi.v6PoolID = epMap["v6PoolID"].(string)
|
2015-06-05 16:31:12 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
func (epi *endpointInterface) CopyTo(dstEpi *endpointInterface) error {
|
|
|
|
dstEpi.mac = types.GetMacCopy(epi.mac)
|
|
|
|
dstEpi.addr = types.GetIPNetCopy(epi.addr)
|
|
|
|
dstEpi.addrv6 = types.GetIPNetCopy(epi.addrv6)
|
|
|
|
dstEpi.srcName = epi.srcName
|
|
|
|
dstEpi.dstPrefix = epi.dstPrefix
|
2015-10-04 00:25:57 -04:00
|
|
|
dstEpi.v4PoolID = epi.v4PoolID
|
|
|
|
dstEpi.v6PoolID = epi.v6PoolID
|
2015-10-05 07:21:15 -04:00
|
|
|
|
|
|
|
for _, route := range epi.routes {
|
|
|
|
dstEpi.routes = append(dstEpi.routes, types.GetIPNetCopy(route))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
type endpointJoinInfo struct {
|
2015-12-02 21:07:44 -05:00
|
|
|
gw net.IP
|
|
|
|
gw6 net.IP
|
|
|
|
StaticRoutes []*types.StaticRoute
|
|
|
|
disableGatewayService bool
|
2015-06-10 21:50:19 -04:00
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
func (ep *endpoint) Info() EndpointInfo {
|
2015-10-05 07:21:15 -04:00
|
|
|
n, err := ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err = n.getEndpointFromStore(ep.ID())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sb, ok := ep.getSandbox()
|
|
|
|
if !ok {
|
|
|
|
// endpoint hasn't joined any sandbox.
|
|
|
|
// Just return the endpoint
|
|
|
|
return ep
|
|
|
|
}
|
|
|
|
|
2015-11-23 00:44:06 -05:00
|
|
|
if epi := sb.getEndpoint(ep.ID()); epi != nil {
|
|
|
|
return epi
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) DriverInfo() (map[string]interface{}, error) {
|
2015-10-28 19:22:18 -04:00
|
|
|
ep, err := ep.retrieveFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sb, ok := ep.getSandbox(); ok {
|
|
|
|
if gwep := sb.getEndpointInGWNetwork(); gwep != nil && gwep.ID() != ep.ID() {
|
|
|
|
return gwep.DriverInfo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
n, err := ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find network in store for driver info: %v", err)
|
|
|
|
}
|
2015-05-14 02:23:45 -04:00
|
|
|
|
2016-01-16 17:24:44 -05:00
|
|
|
driver, err := n.driver(true)
|
2015-10-05 07:21:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get driver info: %v", err)
|
|
|
|
}
|
2015-05-14 02:23:45 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
return driver.EndpointOperInfo(n.ID(), ep.ID())
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (ep *endpoint) Iface() InterfaceInfo {
|
2015-05-14 02:23:45 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
if ep.iface != nil {
|
|
|
|
return ep.iface
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
return nil
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (ep *endpoint) Interface() driverapi.InterfaceInfo {
|
2015-05-14 02:23:45 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
if ep.iface != nil {
|
|
|
|
return ep.iface
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
return nil
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (epi *endpointInterface) SetMacAddress(mac net.HardwareAddr) error {
|
|
|
|
if epi.mac != nil {
|
|
|
|
return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", epi.mac, mac)
|
|
|
|
}
|
|
|
|
if mac == nil {
|
|
|
|
return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
|
|
|
|
}
|
|
|
|
epi.mac = types.GetMacCopy(mac)
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-14 02:23:45 -04:00
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (epi *endpointInterface) SetIPAddress(address *net.IPNet) error {
|
|
|
|
if address.IP == nil {
|
|
|
|
return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
|
|
|
|
}
|
|
|
|
if address.IP.To4() == nil {
|
|
|
|
return setAddress(&epi.addrv6, address)
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
return setAddress(&epi.addr, address)
|
|
|
|
}
|
2015-05-14 02:23:45 -04:00
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func setAddress(ifaceAddr **net.IPNet, address *net.IPNet) error {
|
|
|
|
if *ifaceAddr != nil {
|
|
|
|
return types.ForbiddenErrorf("endpoint interface IP present (%s). Cannot be modified with (%s).", *ifaceAddr, address)
|
|
|
|
}
|
|
|
|
*ifaceAddr = types.GetIPNetCopy(address)
|
2015-05-14 02:23:45 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
func (epi *endpointInterface) MacAddress() net.HardwareAddr {
|
|
|
|
return types.GetMacCopy(epi.mac)
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (epi *endpointInterface) Address() *net.IPNet {
|
|
|
|
return types.GetIPNetCopy(epi.addr)
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
func (epi *endpointInterface) AddressIPv6() *net.IPNet {
|
|
|
|
return types.GetIPNetCopy(epi.addrv6)
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
func (epi *endpointInterface) SetNames(srcName string, dstPrefix string) error {
|
|
|
|
epi.srcName = srcName
|
|
|
|
epi.dstPrefix = dstPrefix
|
2015-05-14 02:23:45 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (ep *endpoint) InterfaceName() driverapi.InterfaceNameInfo {
|
2015-05-14 02:23:45 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
if ep.iface != nil {
|
|
|
|
return ep.iface
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
return nil
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
func (ep *endpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
|
2015-05-19 20:08:56 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
r := types.StaticRoute{Destination: destination, RouteType: routeType, NextHop: nextHop}
|
2015-05-19 20:08:56 -04:00
|
|
|
|
|
|
|
if routeType == types.NEXTHOP {
|
|
|
|
// If the route specifies a next-hop, then it's loosely routed (i.e. not bound to a particular interface).
|
|
|
|
ep.joinInfo.StaticRoutes = append(ep.joinInfo.StaticRoutes, &r)
|
|
|
|
} else {
|
|
|
|
// If the route doesn't specify a next-hop, it must be a connected route, bound to an interface.
|
2015-09-17 20:55:24 -04:00
|
|
|
ep.iface.routes = append(ep.iface.routes, r.Destination)
|
2015-05-19 20:08:56 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (ep *endpoint) Sandbox() Sandbox {
|
|
|
|
cnt, ok := ep.getSandbox()
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
return cnt
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
2015-12-02 18:21:50 -05:00
|
|
|
func (ep *endpoint) StaticRoutes() []*types.StaticRoute {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
if ep.joinInfo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep.joinInfo.StaticRoutes
|
|
|
|
}
|
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
func (ep *endpoint) Gateway() net.IP {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
if ep.joinInfo == nil {
|
|
|
|
return net.IP{}
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
return types.GetIPCopy(ep.joinInfo.gw)
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) GatewayIPv6() net.IP {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
if ep.joinInfo == nil {
|
|
|
|
return net.IP{}
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
return types.GetIPCopy(ep.joinInfo.gw6)
|
2015-05-14 02:23:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetGateway(gw net.IP) error {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
ep.joinInfo.gw = types.GetIPCopy(gw)
|
2015-05-14 02:23:45 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetGatewayIPv6(gw6 net.IP) error {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
ep.joinInfo.gw6 = types.GetIPCopy(gw6)
|
2015-05-14 02:23:45 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-28 19:22:18 -04:00
|
|
|
|
|
|
|
func (ep *endpoint) retrieveFromStore() (*endpoint, error) {
|
|
|
|
n, err := ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not find network in store to get latest endpoint %s: %v", ep.Name(), err)
|
|
|
|
}
|
|
|
|
return n.getEndpointFromStore(ep.ID())
|
|
|
|
}
|
2015-12-02 21:07:44 -05:00
|
|
|
|
|
|
|
func (ep *endpoint) DisableGatewayService() {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
ep.joinInfo.disableGatewayService = true
|
|
|
|
}
|