From 18f8e29af8e886f49cb73aedf9a5aa46dfbdd598 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Sun, 8 Nov 2015 12:34:17 +0000 Subject: [PATCH] refactor: separate NetworkOverlay from Overlay ...since connections only need to know about the latter. --- prog/weaver/main.go | 2 +- router/fastdp.go | 2 +- router/overlay.go | 20 ++++++++++++-------- router/overlay_switch.go | 14 +++++++------- router/router.go | 6 +++--- router/sleeve.go | 2 +- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 48bb26df..bb13c7e2 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -150,7 +150,7 @@ func main() { } config.ProtocolMinVersion = byte(protocolMinVersion) - var fastDPOverlay weave.Overlay + var fastDPOverlay weave.NetworkOverlay if datapathName != "" { // A datapath name implies that "Bridge" and "Overlay" // packet handling use fast datapath, although other diff --git a/router/fastdp.go b/router/fastdp.go index 0c92bf9c..b1765a94 100644 --- a/router/fastdp.go +++ b/router/fastdp.go @@ -297,7 +297,7 @@ type fastDatapathOverlay struct { *FastDatapath } -func (fastdp *FastDatapath) Overlay() Overlay { +func (fastdp *FastDatapath) Overlay() NetworkOverlay { return fastDatapathOverlay{fastdp} } diff --git a/router/overlay.go b/router/overlay.go index a219e631..ae5fb4a1 100644 --- a/router/overlay.go +++ b/router/overlay.go @@ -4,15 +4,22 @@ import ( "net" ) -// Interface to overlay network packet handling type Overlay interface { - // Start consuming forwarded packets. - StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error + // Enhance a features map with overlay-related features + AddFeaturesTo(map[string]string) // Prepare on overlay connection. The connection should remain // passive until it has been Confirm()ed. PrepareConnection(OverlayConnectionParams) (OverlayConnection, error) + // Obtain diagnostic information specific to the overlay + Diagnostics() interface{} +} + +// Interface to overlay network packet handling +type NetworkOverlay interface { + Overlay + // The routes have changed, so any cached information should // be discarded. InvalidateRoutes() @@ -20,11 +27,8 @@ type Overlay interface { // A mapping of a short id to a peer has changed InvalidateShortIDs() - // Enhance a features map with overlay-related features - AddFeaturesTo(map[string]string) - - // Obtain diagnostic information specific to the overlay - Diagnostics() interface{} + // Start consuming forwarded packets. + StartConsumingPackets(*Peer, *Peers, OverlayConsumer) error } type OverlayConnectionParams struct { diff --git a/router/overlay_switch.go b/router/overlay_switch.go index 8cf67922..9ead7987 100644 --- a/router/overlay_switch.go +++ b/router/overlay_switch.go @@ -13,16 +13,16 @@ import ( // uses the best one that seems to be working. type OverlaySwitch struct { - overlays map[string]Overlay + overlays map[string]NetworkOverlay overlayNames []string - compatOverlay Overlay + compatOverlay NetworkOverlay } func NewOverlaySwitch() *OverlaySwitch { - return &OverlaySwitch{overlays: make(map[string]Overlay)} + return &OverlaySwitch{overlays: make(map[string]NetworkOverlay)} } -func (osw *OverlaySwitch) Add(name string, overlay Overlay) { +func (osw *OverlaySwitch) Add(name string, overlay NetworkOverlay) { // check for repeated names if _, present := osw.overlays[name]; present { log.Fatal("OverlaySwitch: repeated overlay name") @@ -32,7 +32,7 @@ func (osw *OverlaySwitch) Add(name string, overlay Overlay) { osw.overlayNames = append(osw.overlayNames, name) } -func (osw *OverlaySwitch) SetCompatOverlay(overlay Overlay) { +func (osw *OverlaySwitch) SetCompatOverlay(overlay NetworkOverlay) { osw.compatOverlay = overlay } @@ -70,7 +70,7 @@ func (osw *OverlaySwitch) StartConsumingPackets(localPeer *Peer, peers *Peers, c } type namedOverlay struct { - Overlay + NetworkOverlay name string } @@ -82,7 +82,7 @@ func (osw *OverlaySwitch) commonOverlays(params OverlayConnectionParams) ([]name peerOverlays = strings.Split(overlaysFeature, " ") } - common := make(map[string]Overlay) + common := make(map[string]NetworkOverlay) for _, name := range peerOverlays { if overlay := osw.overlays[name]; overlay != nil { common[name] = overlay diff --git a/router/router.go b/router/router.go index 014a19b4..6b29d558 100644 --- a/router/router.go +++ b/router/router.go @@ -70,7 +70,7 @@ type Router struct { acceptLimiter *TokenBucket } -func NewRouter(config Config, name PeerName, nickName string, overlay Overlay) *Router { +func NewRouter(config Config, name PeerName, nickName string, overlay NetworkOverlay) *Router { router := &Router{Config: config, gossipChannels: make(GossipChannels)} if router.Bridge == nil { @@ -108,7 +108,7 @@ func NewRouter(config Config, name PeerName, nickName string, overlay Overlay) * func (router *Router) Start() { log.Println("Sniffing traffic on", router.Bridge) checkFatal(router.Bridge.StartConsumingPackets(router.handleCapturedPacket)) - checkFatal(router.Overlay.StartConsumingPackets(router.Ourself.Peer, router.Peers, router.handleForwardedPacket)) + checkFatal(router.Overlay.(NetworkOverlay).StartConsumingPackets(router.Ourself.Peer, router.Peers, router.handleForwardedPacket)) router.listenTCP(router.Port) } @@ -218,7 +218,7 @@ func (router *Router) handleForwardedPacket(key ForwardPacketKey) FlowOp { // We need to clear out any flows destined to the MAC // that forward to the old peer. - router.Overlay.InvalidateRoutes() + router.Overlay.(NetworkOverlay).InvalidateRoutes() } router.PacketLogging.LogForwardPacket("Injecting", key) diff --git a/router/sleeve.go b/router/sleeve.go index bf4102f9..3b329a1a 100644 --- a/router/sleeve.go +++ b/router/sleeve.go @@ -71,7 +71,7 @@ type SleeveOverlay struct { forwarders map[PeerName]*sleeveForwarder } -func NewSleeveOverlay(localPort int) Overlay { +func NewSleeveOverlay(localPort int) NetworkOverlay { return &SleeveOverlay{localPort: localPort} }