refactor: move all dataplane code out of LocalPeer
This commit is contained in:
1 parent
e756f07cad
commit
431d04bd40
2 files changed
+46
-54
No files matched your search
@@ -27,56 +27,6 @@ func NewLocalPeer(name PeerName, nickName string, router *Router) *LocalPeer {
|
|||||||
return peer
|
return peer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (peer *LocalPeer) Forward(dstPeer *Peer, key PacketKey) FlowOp {
|
|
||||||
return peer.Relay(ForwardPacketKey{
|
|
||||||
PacketKey: key,
|
|
||||||
SrcPeer: peer.Peer,
|
|
||||||
DstPeer: dstPeer,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (peer *LocalPeer) Broadcast(key PacketKey) FlowOp {
|
|
||||||
return peer.RelayBroadcast(peer.Peer, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (peer *LocalPeer) Relay(key ForwardPacketKey) FlowOp {
|
|
||||||
relayPeerName, found := peer.router.Routes.Unicast(key.DstPeer.Name)
|
|
||||||
if !found {
|
|
||||||
// Not necessarily an error as there could be a race with the
|
|
||||||
// dst disappearing whilst the frame is in flight
|
|
||||||
log.Println("Received packet for unknown destination:", key.DstPeer)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, found := peer.ConnectionTo(relayPeerName)
|
|
||||||
if !found {
|
|
||||||
// Again, could just be a race, not necessarily an error
|
|
||||||
log.Println("Unable to find connection to relay peer", relayPeerName)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return conn.(*LocalConnection).Forward(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, key PacketKey) FlowOp {
|
|
||||||
nextHops := peer.router.Routes.Broadcast(srcPeer.Name)
|
|
||||||
if len(nextHops) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
op := NewMultiFlowOp(true)
|
|
||||||
|
|
||||||
for _, conn := range peer.ConnectionsTo(nextHops) {
|
|
||||||
op.Add(conn.(*LocalConnection).Forward(ForwardPacketKey{
|
|
||||||
PacketKey: key,
|
|
||||||
SrcPeer: srcPeer,
|
|
||||||
DstPeer: conn.Remote(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
return op
|
|
||||||
}
|
|
||||||
|
|
||||||
func (peer *LocalPeer) Connections() ConnectionSet {
|
func (peer *LocalPeer) Connections() ConnectionSet {
|
||||||
connections := make(ConnectionSet)
|
connections := make(ConnectionSet)
|
||||||
peer.RLock()
|
peer.RLock()
|
||||||
|
|||||||
+46
-4
@@ -135,10 +135,13 @@ func (router *Router) handleCapturedPacket(key PacketKey) FlowOp {
|
|||||||
// If we don't know which peer corresponds to the dest
|
// If we don't know which peer corresponds to the dest
|
||||||
// MAC, broadcast it.
|
// MAC, broadcast it.
|
||||||
router.PacketLogging.LogPacket("Broadcasting", key)
|
router.PacketLogging.LogPacket("Broadcasting", key)
|
||||||
return router.Ourself.Broadcast(key)
|
return router.relayBroadcast(router.Ourself.Peer, key)
|
||||||
default:
|
default:
|
||||||
router.PacketLogging.LogPacket("Forwarding", key)
|
router.PacketLogging.LogPacket("Forwarding", key)
|
||||||
return router.Ourself.Forward(dstPeer, key)
|
return router.relay(ForwardPacketKey{
|
||||||
|
PacketKey: key,
|
||||||
|
SrcPeer: router.Ourself.Peer,
|
||||||
|
DstPeer: dstPeer})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +178,7 @@ func (router *Router) handleForwardedPacket(key ForwardPacketKey) FlowOp {
|
|||||||
if key.DstPeer != router.Ourself.Peer {
|
if key.DstPeer != router.Ourself.Peer {
|
||||||
// it's not for us, we're just relaying it
|
// it's not for us, we're just relaying it
|
||||||
router.PacketLogging.LogForwardPacket("Relaying", key)
|
router.PacketLogging.LogForwardPacket("Relaying", key)
|
||||||
return router.Ourself.Relay(key)
|
return router.relay(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point, it's either unicast to us, or a broadcast
|
// At this point, it's either unicast to us, or a broadcast
|
||||||
@@ -196,7 +199,7 @@ func (router *Router) handleForwardedPacket(key ForwardPacketKey) FlowOp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router.PacketLogging.LogForwardPacket("Relaying broadcast", key)
|
router.PacketLogging.LogForwardPacket("Relaying broadcast", key)
|
||||||
relayFop := router.Ourself.RelayBroadcast(key.SrcPeer, key.PacketKey)
|
relayFop := router.relayBroadcast(key.SrcPeer, key.PacketKey)
|
||||||
switch {
|
switch {
|
||||||
case injectFop == nil:
|
case injectFop == nil:
|
||||||
return relayFop
|
return relayFop
|
||||||
@@ -212,6 +215,45 @@ func (router *Router) handleForwardedPacket(key ForwardPacketKey) FlowOp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Routing
|
||||||
|
|
||||||
|
func (router *Router) relay(key ForwardPacketKey) FlowOp {
|
||||||
|
relayPeerName, found := router.Routes.Unicast(key.DstPeer.Name)
|
||||||
|
if !found {
|
||||||
|
// Not necessarily an error as there could be a race with the
|
||||||
|
// dst disappearing whilst the frame is in flight
|
||||||
|
log.Println("Received packet for unknown destination:", key.DstPeer)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, found := router.Ourself.ConnectionTo(relayPeerName)
|
||||||
|
if !found {
|
||||||
|
// Again, could just be a race, not necessarily an error
|
||||||
|
log.Println("Unable to find connection to relay peer", relayPeerName)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn.(*LocalConnection).Forward(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (router *Router) relayBroadcast(srcPeer *Peer, key PacketKey) FlowOp {
|
||||||
|
nextHops := router.Routes.Broadcast(srcPeer.Name)
|
||||||
|
if len(nextHops) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
op := NewMultiFlowOp(true)
|
||||||
|
|
||||||
|
for _, conn := range router.Ourself.ConnectionsTo(nextHops) {
|
||||||
|
op.Add(conn.(*LocalConnection).Forward(ForwardPacketKey{
|
||||||
|
PacketKey: key,
|
||||||
|
SrcPeer: srcPeer,
|
||||||
|
DstPeer: conn.Remote()}))
|
||||||
|
}
|
||||||
|
|
||||||
|
return op
|
||||||
|
}
|
||||||
|
|
||||||
// Gossiper methods - the Router is the topology Gossiper
|
// Gossiper methods - the Router is the topology Gossiper
|
||||||
|
|
||||||
type TopologyGossipData struct {
|
type TopologyGossipData struct {
|
||||||
|
|||||||
Reference in new issue
Block a user