2015-06-10 17:24:19 -04:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2016-06-06 21:17:10 -04:00
|
|
|
|
2016-11-01 00:26:14 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-06-10 17:24:19 -04:00
|
|
|
)
|
|
|
|
|
2016-04-28 19:54:47 -04:00
|
|
|
const ovPeerTable = "overlay_peer_table"
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
type peerKey struct {
|
|
|
|
peerIP net.IP
|
|
|
|
peerMac net.HardwareAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerEntry struct {
|
2015-09-29 02:06:57 -04:00
|
|
|
eid string
|
|
|
|
vtep net.IP
|
|
|
|
peerIPMask net.IPMask
|
|
|
|
inSandbox bool
|
|
|
|
isLocal bool
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type peerMap struct {
|
|
|
|
mp map[string]peerEntry
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerNetworkMap struct {
|
2015-12-07 17:20:13 -05:00
|
|
|
mp map[string]*peerMap
|
2015-06-10 17:24:19 -04:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pKey peerKey) String() string {
|
|
|
|
return fmt.Sprintf("%s %s", pKey.peerIP, pKey.peerMac)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pKey *peerKey) Scan(state fmt.ScanState, verb rune) error {
|
|
|
|
ipB, err := state.Token(true, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pKey.peerIP = net.ParseIP(string(ipB))
|
|
|
|
|
|
|
|
macB, err := state.Token(true, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pKey.peerMac, err = net.ParseMAC(string(macB))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var peerDbWg sync.WaitGroup
|
|
|
|
|
2015-10-02 15:20:29 -04:00
|
|
|
func (d *driver) peerDbWalk(f func(string, *peerKey, *peerEntry) bool) error {
|
|
|
|
d.peerDb.Lock()
|
|
|
|
nids := []string{}
|
|
|
|
for nid := range d.peerDb.mp {
|
|
|
|
nids = append(nids, nid)
|
|
|
|
}
|
|
|
|
d.peerDb.Unlock()
|
|
|
|
|
|
|
|
for _, nid := range nids {
|
|
|
|
d.peerDbNetworkWalk(nid, func(pKey *peerKey, pEntry *peerEntry) bool {
|
|
|
|
return f(nid, pKey, pEntry)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) peerDbNetworkWalk(nid string, f func(*peerKey, *peerEntry) bool) error {
|
2015-06-10 17:24:19 -04:00
|
|
|
d.peerDb.Lock()
|
|
|
|
pMap, ok := d.peerDb.mp[nid]
|
2017-02-07 13:04:38 -05:00
|
|
|
d.peerDb.Unlock()
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-07 13:04:38 -05:00
|
|
|
|
|
|
|
mp := map[string]peerEntry{}
|
2015-06-10 17:24:19 -04:00
|
|
|
|
|
|
|
pMap.Lock()
|
|
|
|
for pKeyStr, pEntry := range pMap.mp {
|
2017-02-07 13:04:38 -05:00
|
|
|
mp[pKeyStr] = pEntry
|
|
|
|
}
|
|
|
|
pMap.Unlock()
|
|
|
|
|
|
|
|
for pKeyStr, pEntry := range mp {
|
2015-06-10 17:24:19 -04:00
|
|
|
var pKey peerKey
|
|
|
|
if _, err := fmt.Sscan(pKeyStr, &pKey); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Peer key scan on network %s failed: %v", nid, err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
if f(&pKey, &pEntry) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
func (d *driver) peerDbSearch(nid string, peerIP net.IP) (net.HardwareAddr, net.IPMask, net.IP, error) {
|
2015-06-10 17:24:19 -04:00
|
|
|
var (
|
2015-09-29 02:06:57 -04:00
|
|
|
peerMac net.HardwareAddr
|
|
|
|
vtep net.IP
|
|
|
|
peerIPMask net.IPMask
|
|
|
|
found bool
|
2015-06-10 17:24:19 -04:00
|
|
|
)
|
|
|
|
|
2015-10-02 15:20:29 -04:00
|
|
|
err := d.peerDbNetworkWalk(nid, func(pKey *peerKey, pEntry *peerEntry) bool {
|
2015-06-10 17:24:19 -04:00
|
|
|
if pKey.peerIP.Equal(peerIP) {
|
|
|
|
peerMac = pKey.peerMac
|
2015-09-29 02:06:57 -04:00
|
|
|
peerIPMask = pEntry.peerIPMask
|
2015-06-10 17:24:19 -04:00
|
|
|
vtep = pEntry.vtep
|
|
|
|
found = true
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
|
|
|
|
return found
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2015-09-29 02:06:57 -04:00
|
|
|
return nil, nil, nil, fmt.Errorf("peerdb search for peer ip %q failed: %v", peerIP, err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2015-09-29 02:06:57 -04:00
|
|
|
return nil, nil, nil, fmt.Errorf("peer ip %q not found in peerdb", peerIP)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
return peerMac, peerIPMask, vtep, nil
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
func (d *driver) peerDbAdd(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
|
2015-06-10 17:24:19 -04:00
|
|
|
peerMac net.HardwareAddr, vtep net.IP, isLocal bool) {
|
|
|
|
|
|
|
|
peerDbWg.Wait()
|
|
|
|
|
|
|
|
d.peerDb.Lock()
|
|
|
|
pMap, ok := d.peerDb.mp[nid]
|
|
|
|
if !ok {
|
2015-12-07 17:20:13 -05:00
|
|
|
d.peerDb.mp[nid] = &peerMap{
|
2015-06-10 17:24:19 -04:00
|
|
|
mp: make(map[string]peerEntry),
|
|
|
|
}
|
|
|
|
|
|
|
|
pMap = d.peerDb.mp[nid]
|
|
|
|
}
|
|
|
|
d.peerDb.Unlock()
|
|
|
|
|
|
|
|
pKey := peerKey{
|
|
|
|
peerIP: peerIP,
|
|
|
|
peerMac: peerMac,
|
|
|
|
}
|
|
|
|
|
|
|
|
pEntry := peerEntry{
|
2015-09-29 02:06:57 -04:00
|
|
|
eid: eid,
|
|
|
|
vtep: vtep,
|
|
|
|
peerIPMask: peerIPMask,
|
|
|
|
isLocal: isLocal,
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pMap.Lock()
|
|
|
|
pMap.mp[pKey.String()] = pEntry
|
|
|
|
pMap.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
func (d *driver) peerDbDelete(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
|
2016-09-07 13:45:40 -04:00
|
|
|
peerMac net.HardwareAddr, vtep net.IP) peerEntry {
|
2015-06-10 17:24:19 -04:00
|
|
|
peerDbWg.Wait()
|
|
|
|
|
|
|
|
d.peerDb.Lock()
|
|
|
|
pMap, ok := d.peerDb.mp[nid]
|
|
|
|
if !ok {
|
|
|
|
d.peerDb.Unlock()
|
2016-09-07 13:45:40 -04:00
|
|
|
return peerEntry{}
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
d.peerDb.Unlock()
|
|
|
|
|
|
|
|
pKey := peerKey{
|
|
|
|
peerIP: peerIP,
|
|
|
|
peerMac: peerMac,
|
|
|
|
}
|
|
|
|
|
|
|
|
pMap.Lock()
|
2016-08-08 14:55:06 -04:00
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
pEntry, ok := pMap.mp[pKey.String()]
|
|
|
|
if ok {
|
2016-08-08 14:55:06 -04:00
|
|
|
// Mismatched endpoint ID(possibly outdated). Do not
|
|
|
|
// delete peerdb
|
|
|
|
if pEntry.eid != eid {
|
|
|
|
pMap.Unlock()
|
2016-09-07 13:45:40 -04:00
|
|
|
return pEntry
|
2016-08-08 14:55:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
delete(pMap.mp, pKey.String())
|
|
|
|
pMap.Unlock()
|
2016-08-08 14:55:06 -04:00
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
return pEntry
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (d *driver) peerDbUpdateSandbox(nid string) {
|
2015-06-10 17:24:19 -04:00
|
|
|
d.peerDb.Lock()
|
|
|
|
pMap, ok := d.peerDb.mp[nid]
|
|
|
|
if !ok {
|
|
|
|
d.peerDb.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.peerDb.Unlock()
|
|
|
|
|
|
|
|
peerDbWg.Add(1)
|
|
|
|
|
|
|
|
var peerOps []func()
|
|
|
|
pMap.Lock()
|
|
|
|
for pKeyStr, pEntry := range pMap.mp {
|
|
|
|
var pKey peerKey
|
|
|
|
if _, err := fmt.Sscan(pKeyStr, &pKey); err != nil {
|
|
|
|
fmt.Printf("peer key scan failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pEntry.isLocal {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-07-20 21:01:40 -04:00
|
|
|
// Go captures variables by reference. The pEntry could be
|
|
|
|
// pointing to the same memory location for every iteration. Make
|
|
|
|
// a copy of pEntry before capturing it in the following closure.
|
|
|
|
entry := pEntry
|
2015-06-10 17:24:19 -04:00
|
|
|
op := func() {
|
2015-09-29 02:06:57 -04:00
|
|
|
if err := d.peerAdd(nid, entry.eid, pKey.peerIP, entry.peerIPMask,
|
2015-07-20 21:01:40 -04:00
|
|
|
pKey.peerMac, entry.vtep,
|
2015-06-10 17:24:19 -04:00
|
|
|
false); err != nil {
|
|
|
|
fmt.Printf("peerdbupdate in sandbox failed for ip %s and mac %s: %v",
|
|
|
|
pKey.peerIP, pKey.peerMac, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peerOps = append(peerOps, op)
|
|
|
|
}
|
|
|
|
pMap.Unlock()
|
|
|
|
|
|
|
|
for _, op := range peerOps {
|
|
|
|
op()
|
|
|
|
}
|
|
|
|
|
|
|
|
peerDbWg.Done()
|
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
func (d *driver) peerAdd(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
|
2015-06-10 17:24:19 -04:00
|
|
|
peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
|
|
|
|
|
|
|
|
if err := validateID(nid, eid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateDb {
|
2015-09-29 02:06:57 -04:00
|
|
|
d.peerDbAdd(nid, eid, peerIP, peerIPMask, peerMac, vtep, false)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n := d.network(nid)
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sbox := n.sandbox()
|
|
|
|
if sbox == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
IP := &net.IPNet{
|
|
|
|
IP: peerIP,
|
|
|
|
Mask: peerIPMask,
|
|
|
|
}
|
|
|
|
|
|
|
|
s := n.getSubnetforIP(IP)
|
|
|
|
if s == nil {
|
2016-11-14 19:41:54 -05:00
|
|
|
return fmt.Errorf("couldn't find the subnet %q in network %q", IP.String(), n.id)
|
2015-09-29 02:06:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := n.obtainVxlanID(s); err != nil {
|
|
|
|
return fmt.Errorf("couldn't get vxlan id for %q: %v", s.subnetIP.String(), err)
|
|
|
|
}
|
|
|
|
|
2016-06-08 01:54:28 -04:00
|
|
|
if err := n.joinSubnetSandbox(s, false); err != nil {
|
2015-09-29 02:06:57 -04:00
|
|
|
return fmt.Errorf("subnet sandbox join failed for %q: %v", s.subnetIP.String(), err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
if err := d.checkEncryption(nid, vtep, n.vxlanID(s), false, true); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
// Add neighbor entry for the peer IP
|
2015-09-29 02:06:57 -04:00
|
|
|
if err := sbox.AddNeighbor(peerIP, peerMac, sbox.NeighborOptions().LinkName(s.vxlanName)); err != nil {
|
2016-08-04 17:58:39 -04:00
|
|
|
return fmt.Errorf("could not add neighbor entry into the sandbox: %v", err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add fdb entry to the bridge for the peer mac
|
2015-09-29 02:06:57 -04:00
|
|
|
if err := sbox.AddNeighbor(vtep, peerMac, sbox.NeighborOptions().LinkName(s.vxlanName),
|
2015-06-10 17:24:19 -04:00
|
|
|
sbox.NeighborOptions().Family(syscall.AF_BRIDGE)); err != nil {
|
|
|
|
return fmt.Errorf("could not add fdb entry into the sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
func (d *driver) peerDelete(nid, eid string, peerIP net.IP, peerIPMask net.IPMask,
|
2015-06-10 17:24:19 -04:00
|
|
|
peerMac net.HardwareAddr, vtep net.IP, updateDb bool) error {
|
|
|
|
|
|
|
|
if err := validateID(nid, eid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
var pEntry peerEntry
|
2015-06-10 17:24:19 -04:00
|
|
|
if updateDb {
|
2016-09-07 13:45:40 -04:00
|
|
|
pEntry = d.peerDbDelete(nid, eid, peerIP, peerIPMask, peerMac, vtep)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n := d.network(nid)
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sbox := n.sandbox()
|
|
|
|
if sbox == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-07 13:45:40 -04:00
|
|
|
// Delete fdb entry to the bridge for the peer mac only if the
|
|
|
|
// entry existed in local peerdb. If it is a stale delete
|
|
|
|
// request, still call DeleteNeighbor but only to cleanup any
|
|
|
|
// leftover sandbox neighbor cache and not actually delete the
|
|
|
|
// kernel state.
|
|
|
|
if (eid == pEntry.eid && vtep.Equal(pEntry.vtep)) ||
|
|
|
|
(eid != pEntry.eid && !vtep.Equal(pEntry.vtep)) {
|
|
|
|
if err := sbox.DeleteNeighbor(vtep, peerMac,
|
|
|
|
eid == pEntry.eid && vtep.Equal(pEntry.vtep)); err != nil {
|
|
|
|
return fmt.Errorf("could not delete fdb entry into the sandbox: %v", err)
|
|
|
|
}
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete neighbor entry for the peer IP
|
2016-09-07 13:45:40 -04:00
|
|
|
if eid == pEntry.eid {
|
|
|
|
if err := sbox.DeleteNeighbor(peerIP, peerMac, true); err != nil {
|
|
|
|
return fmt.Errorf("could not delete neighbor entry into the sandbox: %v", err)
|
|
|
|
}
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
2016-06-06 21:17:10 -04:00
|
|
|
if err := d.checkEncryption(nid, vtep, 0, false, false); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warn(err)
|
2016-06-06 21:17:10 -04:00
|
|
|
}
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-02 15:20:29 -04:00
|
|
|
|
|
|
|
func (d *driver) pushLocalDb() {
|
|
|
|
d.peerDbWalk(func(nid string, pKey *peerKey, pEntry *peerEntry) bool {
|
|
|
|
if pEntry.isLocal {
|
|
|
|
d.pushLocalEndpointEvent("join", nid, pEntry.eid)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2017-02-10 17:24:24 -05:00
|
|
|
|
|
|
|
func (d *driver) peerDBUpdateSelf() {
|
|
|
|
d.peerDbWalk(func(nid string, pkey *peerKey, pEntry *peerEntry) bool {
|
|
|
|
if pEntry.isLocal {
|
|
|
|
pEntry.vtep = net.ParseIP(d.advertiseAddress)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|