2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
package overlay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/serf/serf"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-06-10 17:24:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type ovNotify struct {
|
|
|
|
action string
|
2016-03-29 14:19:23 -04:00
|
|
|
ep *endpoint
|
|
|
|
nw *network
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type logWriter struct{}
|
|
|
|
|
|
|
|
func (l *logWriter) Write(p []byte) (int, error) {
|
|
|
|
str := string(p)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.Contains(str, "[WARN]"):
|
|
|
|
logrus.Warn(str)
|
|
|
|
case strings.Contains(str, "[DEBUG]"):
|
|
|
|
logrus.Debug(str)
|
|
|
|
case strings.Contains(str, "[INFO]"):
|
|
|
|
logrus.Info(str)
|
|
|
|
case strings.Contains(str, "[ERR]"):
|
|
|
|
logrus.Error(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) serfInit() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
config := serf.DefaultConfig()
|
|
|
|
config.Init()
|
2016-07-19 21:17:30 -04:00
|
|
|
config.MemberlistConfig.BindAddr = d.advertiseAddress
|
2015-06-10 17:24:19 -04:00
|
|
|
|
|
|
|
d.eventCh = make(chan serf.Event, 4)
|
|
|
|
config.EventCh = d.eventCh
|
|
|
|
config.UserCoalescePeriod = 1 * time.Second
|
|
|
|
config.UserQuiescentPeriod = 50 * time.Millisecond
|
|
|
|
|
2015-10-06 18:29:56 -04:00
|
|
|
config.LogOutput = &logWriter{}
|
2015-10-28 17:15:57 -04:00
|
|
|
config.MemberlistConfig.LogOutput = config.LogOutput
|
2015-06-10 17:24:19 -04:00
|
|
|
|
|
|
|
s, err := serf.Create(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create cluster node: %v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
s.Shutdown()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
d.serfInstance = s
|
|
|
|
|
|
|
|
d.notifyCh = make(chan ovNotify)
|
|
|
|
d.exitCh = make(chan chan struct{})
|
|
|
|
|
|
|
|
go d.startSerfLoop(d.eventCh, d.notifyCh, d.exitCh)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-02 00:50:54 -04:00
|
|
|
func (d *driver) serfJoin(neighIP string) error {
|
|
|
|
if neighIP == "" {
|
2015-09-18 15:54:08 -04:00
|
|
|
return fmt.Errorf("no neighbor to join")
|
|
|
|
}
|
2017-02-07 14:12:38 -05:00
|
|
|
if _, err := d.serfInstance.Join([]string{neighIP}, true); err != nil {
|
2015-09-18 15:54:08 -04:00
|
|
|
return fmt.Errorf("Failed to join the cluster at neigh IP %s: %v",
|
2015-10-02 00:50:54 -04:00
|
|
|
neighIP, err)
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:24:19 -04:00
|
|
|
func (d *driver) notifyEvent(event ovNotify) {
|
2016-03-29 14:19:23 -04:00
|
|
|
ep := event.ep
|
2015-06-10 17:24:19 -04:00
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
ePayload := fmt.Sprintf("%s %s %s %s", event.action, ep.addr.IP.String(),
|
|
|
|
net.IP(ep.addr.Mask).String(), ep.mac.String())
|
2015-06-10 17:24:19 -04:00
|
|
|
eName := fmt.Sprintf("jl %s %s %s", d.serfInstance.LocalMember().Addr.String(),
|
2016-03-29 14:19:23 -04:00
|
|
|
event.nw.id, ep.id)
|
2015-06-10 17:24:19 -04:00
|
|
|
|
|
|
|
if err := d.serfInstance.UserEvent(eName, []byte(ePayload), true); err != nil {
|
2015-10-06 18:29:56 -04:00
|
|
|
logrus.Errorf("Sending user event failed: %v\n", err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) processEvent(u serf.UserEvent) {
|
2017-02-07 14:12:38 -05:00
|
|
|
logrus.Debugf("Received user event name:%s, payload:%s LTime:%d \n", u.Name,
|
|
|
|
string(u.Payload), uint64(u.LTime))
|
2015-06-10 17:24:19 -04:00
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
var dummy, action, vtepStr, nid, eid, ipStr, maskStr, macStr string
|
2015-06-10 17:24:19 -04:00
|
|
|
if _, err := fmt.Sscan(u.Name, &dummy, &vtepStr, &nid, &eid); err != nil {
|
|
|
|
fmt.Printf("Failed to scan name string: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fmt.Sscan(string(u.Payload), &action,
|
2015-09-29 02:06:57 -04:00
|
|
|
&ipStr, &maskStr, &macStr); err != nil {
|
2015-06-10 17:24:19 -04:00
|
|
|
fmt.Printf("Failed to scan value string: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2015-10-06 18:29:56 -04:00
|
|
|
logrus.Debugf("Parsed data = %s/%s/%s/%s/%s/%s\n", nid, eid, vtepStr, ipStr, maskStr, macStr)
|
2015-06-10 17:24:19 -04:00
|
|
|
|
|
|
|
mac, err := net.ParseMAC(macStr)
|
|
|
|
if err != nil {
|
2015-10-06 18:29:56 -04:00
|
|
|
logrus.Errorf("Failed to parse mac: %v\n", err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.serfInstance.LocalMember().Addr.String() == vtepStr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case "join":
|
2017-08-14 12:20:55 -04:00
|
|
|
d.peerAdd(nid, eid, net.ParseIP(ipStr), net.IPMask(net.ParseIP(maskStr).To4()), mac, net.ParseIP(vtepStr), false, false, false)
|
2015-06-10 17:24:19 -04:00
|
|
|
case "leave":
|
2017-09-05 13:43:20 -04:00
|
|
|
d.peerDelete(nid, eid, net.ParseIP(ipStr), net.IPMask(net.ParseIP(maskStr).To4()), mac, net.ParseIP(vtepStr), false)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) processQuery(q *serf.Query) {
|
2015-10-06 18:29:56 -04:00
|
|
|
logrus.Debugf("Received query name:%s, payload:%s\n", q.Name,
|
2015-06-10 17:24:19 -04:00
|
|
|
string(q.Payload))
|
|
|
|
|
|
|
|
var nid, ipStr string
|
|
|
|
if _, err := fmt.Sscan(string(q.Payload), &nid, &ipStr); err != nil {
|
|
|
|
fmt.Printf("Failed to scan query payload string: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2017-09-05 13:43:20 -04:00
|
|
|
pKey, pEntry, err := d.peerDbSearch(nid, net.ParseIP(ipStr))
|
2015-06-10 17:24:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-05 13:43:20 -04:00
|
|
|
logrus.Debugf("Sending peer query resp mac %v, mask %s, vtep %s", pKey.peerMac, net.IP(pEntry.peerIPMask).String(), pEntry.vtep)
|
|
|
|
q.Respond([]byte(fmt.Sprintf("%s %s %s", pKey.peerMac.String(), net.IP(pEntry.peerIPMask).String(), pEntry.vtep.String())))
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 02:06:57 -04:00
|
|
|
func (d *driver) resolvePeer(nid string, peerIP net.IP) (net.HardwareAddr, net.IPMask, net.IP, error) {
|
2015-12-03 13:21:43 -05:00
|
|
|
if d.serfInstance == nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("could not resolve peer: serf instance not initialized")
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:15:56 -04:00
|
|
|
qPayload := fmt.Sprintf("%s %s", nid, peerIP.String())
|
2015-06-10 17:24:19 -04:00
|
|
|
resp, err := d.serfInstance.Query("peerlookup", []byte(qPayload), nil)
|
|
|
|
if err != nil {
|
2015-09-29 02:06:57 -04:00
|
|
|
return nil, nil, nil, fmt.Errorf("resolving peer by querying the cluster failed: %v", err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
respCh := resp.ResponseCh()
|
|
|
|
select {
|
|
|
|
case r := <-respCh:
|
2015-09-29 02:06:57 -04:00
|
|
|
var macStr, maskStr, vtepStr string
|
|
|
|
if _, err := fmt.Sscan(string(r.Payload), &macStr, &maskStr, &vtepStr); err != nil {
|
|
|
|
return nil, nil, nil, fmt.Errorf("bad response %q for the resolve query: %v", string(r.Payload), err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mac, err := net.ParseMAC(macStr)
|
|
|
|
if err != nil {
|
2015-09-29 02:06:57 -04:00
|
|
|
return nil, nil, nil, fmt.Errorf("failed to parse mac: %v", err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
2017-02-07 14:12:38 -05:00
|
|
|
logrus.Debugf("Received peer query response, mac %s, vtep %s, mask %s", macStr, vtepStr, maskStr)
|
2015-09-29 02:06:57 -04:00
|
|
|
return mac, net.IPMask(net.ParseIP(maskStr).To4()), net.ParseIP(vtepStr), nil
|
2015-06-10 17:24:19 -04:00
|
|
|
|
|
|
|
case <-time.After(time.Second):
|
2015-09-29 02:06:57 -04:00
|
|
|
return nil, nil, nil, fmt.Errorf("timed out resolving peer by querying the cluster")
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) startSerfLoop(eventCh chan serf.Event, notifyCh chan ovNotify,
|
|
|
|
exitCh chan chan struct{}) {
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case notify, ok := <-notifyCh:
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
d.notifyEvent(notify)
|
|
|
|
case ch, ok := <-exitCh:
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.serfInstance.Leave(); err != nil {
|
2015-10-06 18:29:56 -04:00
|
|
|
logrus.Errorf("failed leaving the cluster: %v\n", err)
|
2015-06-10 17:24:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
d.serfInstance.Shutdown()
|
|
|
|
close(ch)
|
|
|
|
return
|
|
|
|
case e, ok := <-eventCh:
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.EventType() == serf.EventQuery {
|
|
|
|
d.processQuery(e.(*serf.Query))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
u, ok := e.(serf.UserEvent)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
d.processEvent(u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-18 15:54:08 -04:00
|
|
|
|
|
|
|
func (d *driver) isSerfAlive() bool {
|
|
|
|
d.Lock()
|
|
|
|
serfInstance := d.serfInstance
|
|
|
|
d.Unlock()
|
|
|
|
if serfInstance == nil || serfInstance.State() != serf.SerfAlive {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|