refactor: separate forwarder from overlay connection
...since the main connections only need to know about the former. We make LocalConnection.Overlay (formerly '.forwarder') public since the router needs access to it, and it shouldn't be doing that via a private field.
This commit is contained in:
1 parent
97622b961c
commit
d78f6614cc
7 files changed
+56
-49
No files matched your search
+18
-19
@@ -49,7 +49,7 @@ type LocalConnection struct {
|
|||||||
uid uint64
|
uid uint64
|
||||||
actionChan chan<- ConnectionAction
|
actionChan chan<- ConnectionAction
|
||||||
finished <-chan struct{} // closed to signal that actorLoop has finished
|
finished <-chan struct{} // closed to signal that actorLoop has finished
|
||||||
forwarder OverlayForwarder
|
OverlayConn OverlayConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConnectionAction func() error
|
type ConnectionAction func() error
|
||||||
@@ -190,7 +190,7 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished ch
|
|||||||
|
|
||||||
conn.Log("connection ready; using protocol version", conn.version)
|
conn.Log("connection ready; using protocol version", conn.version)
|
||||||
|
|
||||||
params := ForwarderParams{
|
params := OverlayConnectionParams{
|
||||||
RemotePeer: conn.remote,
|
RemotePeer: conn.remote,
|
||||||
LocalAddr: conn.TCPConn.LocalAddr().(*net.TCPAddr),
|
LocalAddr: conn.TCPConn.LocalAddr().(*net.TCPAddr),
|
||||||
RemoteAddr: conn.TCPConn.RemoteAddr().(*net.TCPAddr),
|
RemoteAddr: conn.TCPConn.RemoteAddr().(*net.TCPAddr),
|
||||||
@@ -200,28 +200,27 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished ch
|
|||||||
SendControlMessage: conn.sendOverlayControlMessage,
|
SendControlMessage: conn.sendOverlayControlMessage,
|
||||||
Features: intro.Features,
|
Features: intro.Features,
|
||||||
}
|
}
|
||||||
if conn.forwarder, err = conn.Router.Overlay.MakeForwarder(params); err != nil {
|
if conn.OverlayConn, err = conn.Router.Overlay.PrepareConnection(params); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// As soon as we do AddConnection, the new connection becomes
|
// As soon as we do AddConnection, the new connection becomes
|
||||||
// visible to the packet routing logic. So AddConnection must
|
// visible to the packet routing logic. So AddConnection must
|
||||||
// come after MakeForwarder
|
// come after PrepareConnection
|
||||||
if err = conn.Router.Ourself.AddConnection(conn); err != nil {
|
if err = conn.Router.Ourself.AddConnection(conn); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.Router.ConnectionMaker.ConnectionCreated(conn)
|
conn.Router.ConnectionMaker.ConnectionCreated(conn)
|
||||||
|
|
||||||
// Forwarder confirmation comes after AddConnection, because
|
// OverlayConnection confirmation comes after AddConnection,
|
||||||
// only after that completes do we know the connection is
|
// because only after that completes do we know the connection is
|
||||||
// valid: in particular that it is not a duplicate connection
|
// valid: in particular that it is not a duplicate connection to
|
||||||
// to the same peer. Sending heartbeats on a duplicate
|
// the same peer. Overlay communication on a duplicate connection
|
||||||
// connection can trip up crypto at the other end, since the
|
// can cause problems such as tripping up overlay crypto at the
|
||||||
// associated UDP packets may get decoded by the other
|
// other end due to data being decoded by the other connection. It
|
||||||
// connection. It is also generally wasteful to engage in any
|
// is also generally wasteful to engage in any interaction with
|
||||||
// interaction with the remote on a connection that turns out
|
// the remote on a connection that turns out to be invalid.
|
||||||
// to be invalid.
|
conn.OverlayConn.Confirm()
|
||||||
conn.forwarder.Confirm()
|
|
||||||
|
|
||||||
// receiveTCP must follow also AddConnection. In the absence
|
// receiveTCP must follow also AddConnection. In the absence
|
||||||
// of any indirect connectivity to the remote peer, the first
|
// of any indirect connectivity to the remote peer, the first
|
||||||
@@ -341,8 +340,8 @@ func (conn *LocalConnection) registerRemote(remote *Peer, acceptNewPeer bool) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err error) {
|
func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err error) {
|
||||||
fwdErrorChan := conn.forwarder.ErrorChannel()
|
fwdErrorChan := conn.OverlayConn.ErrorChannel()
|
||||||
fwdEstablishedChan := conn.forwarder.EstablishedChannel()
|
fwdEstablishedChan := conn.OverlayConn.EstablishedChannel()
|
||||||
|
|
||||||
for err == nil {
|
for err == nil {
|
||||||
select {
|
select {
|
||||||
@@ -383,8 +382,8 @@ func (conn *LocalConnection) shutdown(err error) {
|
|||||||
conn.heartbeatTCP.Stop()
|
conn.heartbeatTCP.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn.forwarder != nil {
|
if conn.OverlayConn != nil {
|
||||||
conn.forwarder.Stop()
|
conn.OverlayConn.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.Router.ConnectionMaker.ConnectionTerminated(conn, err)
|
conn.Router.ConnectionMaker.ConnectionTerminated(conn, err)
|
||||||
@@ -428,7 +427,7 @@ func (conn *LocalConnection) handleProtocolMsg(tag ProtocolTag, payload []byte)
|
|||||||
switch tag {
|
switch tag {
|
||||||
case ProtocolHeartbeat:
|
case ProtocolHeartbeat:
|
||||||
case ProtocolConnectionEstablished, ProtocolFragmentationReceived, ProtocolPMTUVerified, ProtocolOverlayControlMsg:
|
case ProtocolConnectionEstablished, ProtocolFragmentationReceived, ProtocolPMTUVerified, ProtocolOverlayControlMsg:
|
||||||
conn.forwarder.ControlMessage(byte(tag), payload)
|
conn.OverlayConn.ControlMessage(byte(tag), payload)
|
||||||
case ProtocolGossipUnicast, ProtocolGossipBroadcast, ProtocolGossip:
|
case ProtocolGossipUnicast, ProtocolGossipBroadcast, ProtocolGossip:
|
||||||
return conn.Router.handleGossip(tag, payload)
|
return conn.Router.handleGossip(tag, payload)
|
||||||
default:
|
default:
|
||||||
|
|||||||
+1
-1
@@ -506,7 +506,7 @@ type fastDatapathForwarder struct {
|
|||||||
errorChan chan error
|
errorChan chan error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fastdp fastDatapathOverlay) MakeForwarder(params ForwarderParams) (OverlayForwarder, error) {
|
func (fastdp fastDatapathOverlay) PrepareConnection(params OverlayConnectionParams) (OverlayConnection, error) {
|
||||||
if params.SessionKey != nil {
|
if params.SessionKey != nil {
|
||||||
// No encryption suport in fastdp. The weaver main.go
|
// No encryption suport in fastdp. The weaver main.go
|
||||||
// is responsible for ensuring this doesn't happen.
|
// is responsible for ensuring this doesn't happen.
|
||||||
|
|||||||
+28
-21
@@ -9,8 +9,9 @@ type Overlay interface {
|
|||||||
// Start consuming forwarded packets.
|
// Start consuming forwarded packets.
|
||||||
StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error
|
StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error
|
||||||
|
|
||||||
// Form a packet-forwarding connection.
|
// Prepare on overlay connection. The connection should remain
|
||||||
MakeForwarder(ForwarderParams) (OverlayForwarder, error)
|
// passive until it has been Confirm()ed.
|
||||||
|
PrepareConnection(OverlayConnectionParams) (OverlayConnection, error)
|
||||||
|
|
||||||
// The routes have changed, so any cached information should
|
// The routes have changed, so any cached information should
|
||||||
// be discarded.
|
// be discarded.
|
||||||
@@ -26,12 +27,12 @@ type Overlay interface {
|
|||||||
Diagnostics() interface{}
|
Diagnostics() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ForwarderParams struct {
|
type OverlayConnectionParams struct {
|
||||||
RemotePeer *Peer
|
RemotePeer *Peer
|
||||||
|
|
||||||
// The local address of the corresponding TCP connection. Used to
|
// The local address of the corresponding TCP connection. Used to
|
||||||
// derive the local IP address for sending. May differ for
|
// derive the local IP address for sending. May differ for
|
||||||
// different forwarders.
|
// different overlay connections.
|
||||||
LocalAddr *net.TCPAddr
|
LocalAddr *net.TCPAddr
|
||||||
|
|
||||||
// The remote address of the corresponding TCP connection. Used to
|
// The remote address of the corresponding TCP connection. Used to
|
||||||
@@ -50,7 +51,7 @@ type ForwarderParams struct {
|
|||||||
SessionKey *[32]byte
|
SessionKey *[32]byte
|
||||||
|
|
||||||
// Function to send a control message to the counterpart
|
// Function to send a control message to the counterpart
|
||||||
// forwarder.
|
// overlay connection.
|
||||||
SendControlMessage func(tag byte, msg []byte) error
|
SendControlMessage func(tag byte, msg []byte) error
|
||||||
|
|
||||||
// Features passed at connection initiation
|
// Features passed at connection initiation
|
||||||
@@ -61,26 +62,22 @@ type ForwarderParams struct {
|
|||||||
// to decode the frame.
|
// to decode the frame.
|
||||||
type OverlayConsumer func(ForwardPacketKey) FlowOp
|
type OverlayConsumer func(ForwardPacketKey) FlowOp
|
||||||
|
|
||||||
// All of the machinery to forward packets to a particular peer
|
// All of the machinery to manage overlay connectivity to a particular
|
||||||
type OverlayForwarder interface {
|
// peer
|
||||||
// Forward a packet across the connection. May be called as
|
type OverlayConnection interface {
|
||||||
// soon as the forwarder is created, in particular before
|
|
||||||
// Confirm(). The return value nil means the key could not be
|
|
||||||
// handled by this forwarder.
|
|
||||||
Forward(ForwardPacketKey) FlowOp
|
|
||||||
|
|
||||||
// Confirm that the connection is really wanted, and so the
|
// Confirm that the connection is really wanted, and so the
|
||||||
// Overlay should begin heartbeats etc. to verify the
|
// Overlay should begin heartbeats etc. to verify the operation of
|
||||||
// operation of the forwarder.
|
// the overlay connection.
|
||||||
Confirm()
|
Confirm()
|
||||||
|
|
||||||
// A channel indicating that the forwarder is established,
|
// A channel indicating that the overlay connection is
|
||||||
// i.e. its operation has been confirmed.
|
// established, i.e. its operation has been confirmed.
|
||||||
EstablishedChannel() <-chan struct{}
|
EstablishedChannel() <-chan struct{}
|
||||||
|
|
||||||
// A channel indicating an error from the forwarder. The
|
// A channel indicating an error from the overlay connection. The
|
||||||
// forwarder is not expected to be operational after the first
|
// overlay connection is not expected to be operational after the
|
||||||
// error, so the channel only needs to buffer a single error.
|
// first error, so the channel only needs to buffer a single
|
||||||
|
// error.
|
||||||
ErrorChannel() <-chan error
|
ErrorChannel() <-chan error
|
||||||
|
|
||||||
Stop()
|
Stop()
|
||||||
@@ -94,13 +91,23 @@ type OverlayForwarder interface {
|
|||||||
DisplayName() string
|
DisplayName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// All of the machinery to forward packets to a particular peer
|
||||||
|
type OverlayForwarder interface {
|
||||||
|
OverlayConnection
|
||||||
|
// Forward a packet across the connection. May be called as soon
|
||||||
|
// as the overlay connection is created, in particular before
|
||||||
|
// Confirm(). The return value nil means the key could not be
|
||||||
|
// handled by this forwarder.
|
||||||
|
Forward(ForwardPacketKey) FlowOp
|
||||||
|
}
|
||||||
|
|
||||||
type NullOverlay struct{}
|
type NullOverlay struct{}
|
||||||
|
|
||||||
func (NullOverlay) StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error {
|
func (NullOverlay) StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (NullOverlay) MakeForwarder(ForwarderParams) (OverlayForwarder, error) {
|
func (NullOverlay) PrepareConnection(OverlayConnectionParams) (OverlayConnection, error) {
|
||||||
return NullOverlay{}, nil
|
return NullOverlay{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ type namedOverlay struct {
|
|||||||
|
|
||||||
// Find the common set of overlays supported by both sides, with the
|
// Find the common set of overlays supported by both sides, with the
|
||||||
// ordering being the same on both sides too.
|
// ordering being the same on both sides too.
|
||||||
func (osw *OverlaySwitch) commonOverlays(params ForwarderParams) ([]namedOverlay, error) {
|
func (osw *OverlaySwitch) commonOverlays(params OverlayConnectionParams) ([]namedOverlay, error) {
|
||||||
var peerOverlays []string
|
var peerOverlays []string
|
||||||
if overlaysFeature, present := params.Features["Overlays"]; present {
|
if overlaysFeature, present := params.Features["Overlays"]; present {
|
||||||
peerOverlays = strings.Split(overlaysFeature, " ")
|
peerOverlays = strings.Split(overlaysFeature, " ")
|
||||||
@@ -160,9 +160,9 @@ type subForwarderEvent struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (osw *OverlaySwitch) MakeForwarder(params ForwarderParams) (OverlayForwarder, error) {
|
func (osw *OverlaySwitch) PrepareConnection(params OverlayConnectionParams) (OverlayConnection, error) {
|
||||||
if _, present := params.Features["Overlays"]; !present && osw.compatOverlay != nil {
|
if _, present := params.Features["Overlays"]; !present && osw.compatOverlay != nil {
|
||||||
return osw.compatOverlay.MakeForwarder(params)
|
return osw.compatOverlay.PrepareConnection(params)
|
||||||
}
|
}
|
||||||
|
|
||||||
overlays, err := osw.commonOverlays(params)
|
overlays, err := osw.commonOverlays(params)
|
||||||
@@ -200,11 +200,12 @@ func (osw *OverlaySwitch) MakeForwarder(params ForwarderParams) (OverlayForwarde
|
|||||||
return origSendControlMessage(ProtocolOverlayControlMsg, xmsg)
|
return origSendControlMessage(ProtocolOverlayControlMsg, xmsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
subFwd, err := overlay.MakeForwarder(params)
|
subConn, err := overlay.PrepareConnection(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fwd.stopFrom(0)
|
fwd.stopFrom(0)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
subFwd := subConn.(OverlayForwarder)
|
||||||
|
|
||||||
subStopChan := make(chan struct{})
|
subStopChan := make(chan struct{})
|
||||||
go monitorForwarder(i, eventsChan, subStopChan, subFwd)
|
go monitorForwarder(i, eventsChan, subStopChan, subFwd)
|
||||||
|
|||||||
+2
-2
@@ -263,7 +263,7 @@ func (router *Router) relay(key ForwardPacketKey) FlowOp {
|
|||||||
return DiscardingFlowOp{}
|
return DiscardingFlowOp{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn.(*LocalConnection).forwarder.Forward(key)
|
return conn.(*LocalConnection).OverlayConn.(OverlayForwarder).Forward(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (router *Router) relayBroadcast(srcPeer *Peer, key PacketKey) FlowOp {
|
func (router *Router) relayBroadcast(srcPeer *Peer, key PacketKey) FlowOp {
|
||||||
@@ -275,7 +275,7 @@ func (router *Router) relayBroadcast(srcPeer *Peer, key PacketKey) FlowOp {
|
|||||||
op := NewMultiFlowOp(true)
|
op := NewMultiFlowOp(true)
|
||||||
|
|
||||||
for _, conn := range router.Ourself.ConnectionsTo(nextHops) {
|
for _, conn := range router.Ourself.ConnectionsTo(nextHops) {
|
||||||
op.Add(conn.(*LocalConnection).forwarder.Forward(ForwardPacketKey{
|
op.Add(conn.(*LocalConnection).OverlayConn.(OverlayForwarder).Forward(ForwardPacketKey{
|
||||||
PacketKey: key,
|
PacketKey: key,
|
||||||
SrcPeer: srcPeer,
|
SrcPeer: srcPeer,
|
||||||
DstPeer: conn.Remote()}))
|
DstPeer: conn.Remote()}))
|
||||||
|
|||||||
+1
-1
@@ -349,7 +349,7 @@ type controlMessage struct {
|
|||||||
msg []byte
|
msg []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sleeve *SleeveOverlay) MakeForwarder(params ForwarderParams) (OverlayForwarder, error) {
|
func (sleeve *SleeveOverlay) PrepareConnection(params OverlayConnectionParams) (OverlayConnection, error) {
|
||||||
name := sleeve.localPeer.NameByte
|
name := sleeve.localPeer.NameByte
|
||||||
var crypto sleeveCrypto
|
var crypto sleeveCrypto
|
||||||
if params.SessionKey != nil {
|
if params.SessionKey != nil {
|
||||||
|
|||||||
+1
-1
@@ -176,7 +176,7 @@ func NewLocalConnectionStatusSlice(cm *ConnectionMaker) []LocalConnectionStatus
|
|||||||
state = "established"
|
state = "established"
|
||||||
}
|
}
|
||||||
lc, _ := conn.(*LocalConnection)
|
lc, _ := conn.(*LocalConnection)
|
||||||
info := fmt.Sprintf("%-6v %v", lc.forwarder.DisplayName(), conn.Remote())
|
info := fmt.Sprintf("%-6v %v", lc.OverlayConn.DisplayName(), conn.Remote())
|
||||||
slice = append(slice, LocalConnectionStatus{conn.RemoteTCPAddr(), conn.Outbound(), state, info})
|
slice = append(slice, LocalConnectionStatus{conn.RemoteTCPAddr(), conn.Outbound(), state, info})
|
||||||
}
|
}
|
||||||
for address, target := range cm.targets {
|
for address, target := range cm.targets {
|
||||||
|
|||||||
Reference in new issue
Block a user