2016-10-12 16:55:20 -07:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
2016-10-31 22:26:14 -06:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-02-07 19:12:24 -08:00
|
|
|
"github.com/docker/libnetwork/types"
|
2016-10-12 16:55:20 -07:00
|
|
|
|
|
|
|
"github.com/Microsoft/hcsshim"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ovPeerTable = "overlay_peer_table"
|
|
|
|
|
|
|
|
func (d *driver) peerAdd(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
|
|
|
|
peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
|
|
|
|
|
2016-10-31 22:26:14 -06:00
|
|
|
logrus.Debugf("WINOVERLAY: Enter peerAdd for ca ip %s with ca mac %s", peerIP.String(), peerMac.String())
|
2016-10-12 16:55:20 -07:00
|
|
|
|
|
|
|
if err := validateID(nid, eid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := d.network(nid)
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateDb {
|
2016-10-31 22:26:14 -06:00
|
|
|
logrus.Info("WINOVERLAY: peerAdd: notifying HNS of the REMOTE endpoint")
|
2016-10-12 16:55:20 -07:00
|
|
|
|
|
|
|
hnsEndpoint := &hcsshim.HNSEndpoint{
|
2017-02-07 19:12:24 -08:00
|
|
|
Name: eid,
|
2016-10-12 16:55:20 -07:00
|
|
|
VirtualNetwork: n.hnsId,
|
|
|
|
MacAddress: peerMac.String(),
|
|
|
|
IPAddress: peerIP,
|
|
|
|
IsRemoteEndpoint: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
paPolicy, err := json.Marshal(hcsshim.PaPolicy{
|
|
|
|
Type: "PA",
|
|
|
|
PA: vtep.String(),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hnsEndpoint.Policies = append(hnsEndpoint.Policies, paPolicy)
|
|
|
|
|
|
|
|
configurationb, err := json.Marshal(hnsEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-27 19:42:32 +08:00
|
|
|
// Temp: We have to create an endpoint object to keep track of the HNS ID for
|
2016-10-12 16:55:20 -07:00
|
|
|
// this endpoint so that we can retrieve it later when the endpoint is deleted.
|
|
|
|
// This seems unnecessary when we already have dockers EID. See if we can pass
|
|
|
|
// the global EID to HNS to use as it's ID, rather than having each HNS assign
|
|
|
|
// it's own local ID for the endpoint
|
|
|
|
|
|
|
|
addr, err := types.ParseCIDR(peerIP.String() + "/32")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.removeEndpointWithAddress(addr)
|
|
|
|
|
|
|
|
hnsresponse, err := hcsshim.HNSEndpointRequest("POST", "", string(configurationb))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ep := &endpoint{
|
|
|
|
id: eid,
|
|
|
|
nid: nid,
|
|
|
|
addr: addr,
|
|
|
|
mac: peerMac,
|
|
|
|
profileId: hnsresponse.Id,
|
|
|
|
remote: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
n.addEndpoint(ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) peerDelete(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
|
|
|
|
peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
|
|
|
|
|
2016-10-31 22:26:14 -06:00
|
|
|
logrus.Infof("WINOVERLAY: Enter peerDelete for endpoint %s and peer ip %s", eid, peerIP.String())
|
2016-10-12 16:55:20 -07:00
|
|
|
|
|
|
|
if err := validateID(nid, eid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n := d.network(nid)
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ep := n.endpoint(eid)
|
|
|
|
if ep == nil {
|
|
|
|
return fmt.Errorf("could not find endpoint with id %s", eid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateDb {
|
|
|
|
_, err := hcsshim.HNSEndpointRequest("DELETE", ep.profileId, "")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.deleteEndpoint(eid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|