From f12ec3b2956b2f9e6ff1c0b8f1270f01e6cbf8e2 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 13 Jan 2016 11:50:52 +0000 Subject: [PATCH 1/6] refactor(ish): make fastdp hold on to interface rather than just the MTU --- router/fastdp.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/router/fastdp.go b/router/fastdp.go index 6a867f9d0d..d0c5a43f18 100644 --- a/router/fastdp.go +++ b/router/fastdp.go @@ -32,7 +32,7 @@ type FastDatapath struct { lock sync.Mutex // guards state and synchronises use of dpif dpif *odp.Dpif dp odp.DatapathHandle - mtu int // [1] + iface *net.Interface deleteFlowsCount uint64 missCount uint64 missHandlers map[odp.VportID]missHandler @@ -59,10 +59,6 @@ type FastDatapath struct { // forwarders by remote peer forwarders map[mesh.PeerName]*fastDatapathForwarder - - // [1] The mtu from the datapath netdev (which should match the - // mtus on all the veths hooked up to the datapath). We validate - // that we are able to support that mtu. } func NewFastDatapath(dpName string, port int) (*FastDatapath, error) { @@ -92,7 +88,7 @@ func NewFastDatapath(dpName string, port int) (*FastDatapath, error) { dpname: dpName, dpif: dpif, dp: dp, - mtu: iface.MTU, + iface: iface, missHandlers: make(map[odp.VportID]missHandler), sendToPort: nil, sendToMAC: make(map[MAC]bridgeSender), @@ -665,7 +661,7 @@ func (fwd *fastDatapathForwarder) sendHeartbeat() { // the heartbeat payload consists of the 64-bit connection uid // followed by the 16-bit packet size. - buf := make([]byte, EthernetOverhead+fwd.fastdp.mtu) + buf := make([]byte, EthernetOverhead+fwd.fastdp.iface.MTU) binary.BigEndian.PutUint64(buf[EthernetOverhead:], fwd.connUID) binary.BigEndian.PutUint16(buf[EthernetOverhead+8:], uint16(len(buf))) From e5c256daa1c9fa94ab8667b5f4e7c89eea8b0c32 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 13 Jan 2016 11:52:43 +0000 Subject: [PATCH 2/6] add Bridge.Interface() --- router/bridge.go | 9 +++++++++ router/fastdp.go | 4 ++++ router/pcap.go | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/router/bridge.go b/router/bridge.go index 00481a0a35..434669bd99 100644 --- a/router/bridge.go +++ b/router/bridge.go @@ -1,5 +1,9 @@ package router +import ( + "net" +) + // Interface to packet handling on the local virtual bridge type Bridge interface { // Inject a packet to be delivered locally @@ -9,6 +13,7 @@ type Bridge interface { // should not be included. StartConsumingPackets(BridgeConsumer) error + Interface() *net.Interface String() string Stats() map[string]int } @@ -26,6 +31,10 @@ func (NullBridge) StartConsumingPackets(BridgeConsumer) error { return nil } +func (NullBridge) Interface() *net.Interface { + return nil +} + func (NullBridge) String() string { return "" } diff --git a/router/fastdp.go b/router/fastdp.go index d0c5a43f18..ad294bfd9b 100644 --- a/router/fastdp.go +++ b/router/fastdp.go @@ -204,6 +204,10 @@ func (fastdp *FastDatapath) Bridge() Bridge { return fastDatapathBridge{fastdp} } +func (fastdp fastDatapathBridge) Interface() *net.Interface { + return fastdp.iface +} + func (fastdp fastDatapathBridge) String() string { return fmt.Sprint(fastdp.dpname, " (via ODP)") } diff --git a/router/pcap.go b/router/pcap.go index 848870be24..781bb81612 100644 --- a/router/pcap.go +++ b/router/pcap.go @@ -104,6 +104,10 @@ func newPcapHandle(ifName string, promisc bool, snaplen int, bufSz int) (handle return } +func (p *Pcap) Interface() *net.Interface { + return p.iface +} + func (p *Pcap) String() string { return fmt.Sprint(p.iface.Name, " (via pcap)") } From 3a367595d7e253b0e368aad736ca1567e309e4f6 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 13 Jan 2016 13:52:00 +0000 Subject: [PATCH 3/6] derive routerName from networkConfig.Bridge.Interface() --- prog/weaver/main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 50bf68e297..45feea02a7 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -65,7 +65,6 @@ func main() { peers []string noDNS bool dnsConfig dnsConfig - iface *net.Interface datapathName string trustedSubnetStr string @@ -141,11 +140,12 @@ func main() { networkConfig.Bridge = fastdp.Bridge() overlays.Add("fastdp", fastdp.Overlay()) case ifaceName != "": - var err error - iface, err = weavenet.EnsureInterface(ifaceName) + iface, err := weavenet.EnsureInterface(ifaceName) checkFatal(err) networkConfig.Bridge, err = weave.NewPcap(iface, bufSzMB*1024*1024) // bufsz flag is in MB checkFatal(err) + default: + networkConfig.Bridge = weave.NullBridge{} } sleeve := weave.NewSleeveOverlay(config.Port) overlays.Add("sleeve", sleeve) @@ -162,8 +162,9 @@ func main() { } if routerName == "" { + iface := networkConfig.Bridge.Interface() if iface == nil { - Log.Fatal("Either an interface must be specified with --iface or a name with -name") + Log.Fatal("Either an interface must be specified with --datapath or --iface, or a name with --name") } routerName = iface.HardwareAddr.String() } From aa44b66a31a95fb1848d816f7d671735664a40ce Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 13 Jan 2016 12:06:42 +0000 Subject: [PATCH 4/6] refactor: extract overlay creation --- prog/weaver/main.go | 47 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 45feea02a7..32e9a58bbe 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -130,26 +130,8 @@ func main() { } config.ProtocolMinVersion = byte(protocolMinVersion) - overlays := weave.NewOverlaySwitch() - switch { - case datapathName != "" && ifaceName != "": - Log.Fatal("At most one of --datapath and --iface must be specified.") - case datapathName != "": - fastdp, err := weave.NewFastDatapath(datapathName, config.Port) - checkFatal(err) - networkConfig.Bridge = fastdp.Bridge() - overlays.Add("fastdp", fastdp.Overlay()) - case ifaceName != "": - iface, err := weavenet.EnsureInterface(ifaceName) - checkFatal(err) - networkConfig.Bridge, err = weave.NewPcap(iface, bufSzMB*1024*1024) // bufsz flag is in MB - checkFatal(err) - default: - networkConfig.Bridge = weave.NullBridge{} - } - sleeve := weave.NewSleeveOverlay(config.Port) - overlays.Add("sleeve", sleeve) - overlays.SetCompatOverlay(sleeve) + overlays, bridge := createOverlay(datapathName, ifaceName, config.Port, bufSzMB) + networkConfig.Bridge = bridge if password == "" { password = os.Getenv("WEAVE_PASSWORD") @@ -310,6 +292,31 @@ func (nopPacketLogging) LogPacket(string, weave.PacketKey) { func (nopPacketLogging) LogForwardPacket(string, weave.ForwardPacketKey) { } +func createOverlay(datapathName string, ifaceName string, port int, bufSzMB int) (weave.NetworkOverlay, weave.Bridge) { + overlays := weave.NewOverlaySwitch() + var bridge weave.Bridge + switch { + case datapathName != "" && ifaceName != "": + Log.Fatal("At most one of --datapath and --iface must be specified.") + case datapathName != "": + fastdp, err := weave.NewFastDatapath(datapathName, port) + checkFatal(err) + bridge = fastdp.Bridge() + overlays.Add("fastdp", fastdp.Overlay()) + case ifaceName != "": + iface, err := weavenet.EnsureInterface(ifaceName) + checkFatal(err) + bridge, err = weave.NewPcap(iface, bufSzMB*1024*1024) // bufsz flag is in MB + checkFatal(err) + default: + bridge = weave.NullBridge{} + } + sleeve := weave.NewSleeveOverlay(port) + overlays.Add("sleeve", sleeve) + overlays.SetCompatOverlay(sleeve) + return overlays, bridge +} + func parseAndCheckCIDR(cidrStr string) address.CIDR { _, cidr, err := address.ParseCIDR(cidrStr) checkFatal(err) From 9f79e5be54f72dfd3cfa83c151afc8d317c54ced Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 13 Jan 2016 12:31:17 +0000 Subject: [PATCH 5/6] refactor: rename Conceptually, an OverlaySwitch is just a single Overlay. --- prog/weaver/main.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 32e9a58bbe..4a20bec9ab 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -130,7 +130,7 @@ func main() { } config.ProtocolMinVersion = byte(protocolMinVersion) - overlays, bridge := createOverlay(datapathName, ifaceName, config.Port, bufSzMB) + overlay, bridge := createOverlay(datapathName, ifaceName, config.Port, bufSzMB) networkConfig.Bridge = bridge if password == "" { @@ -178,7 +178,7 @@ func main() { Log.Fatal("Unable to parse trusted subnets: ", err) } - router := weave.NewNetworkRouter(config, networkConfig, name, nickName, overlays) + router := weave.NewNetworkRouter(config, networkConfig, name, nickName, overlay) Log.Println("Our name is", router.Ourself) var dockerCli *docker.Client @@ -293,7 +293,7 @@ func (nopPacketLogging) LogForwardPacket(string, weave.ForwardPacketKey) { } func createOverlay(datapathName string, ifaceName string, port int, bufSzMB int) (weave.NetworkOverlay, weave.Bridge) { - overlays := weave.NewOverlaySwitch() + overlay := weave.NewOverlaySwitch() var bridge weave.Bridge switch { case datapathName != "" && ifaceName != "": @@ -302,7 +302,7 @@ func createOverlay(datapathName string, ifaceName string, port int, bufSzMB int) fastdp, err := weave.NewFastDatapath(datapathName, port) checkFatal(err) bridge = fastdp.Bridge() - overlays.Add("fastdp", fastdp.Overlay()) + overlay.Add("fastdp", fastdp.Overlay()) case ifaceName != "": iface, err := weavenet.EnsureInterface(ifaceName) checkFatal(err) @@ -312,9 +312,9 @@ func createOverlay(datapathName string, ifaceName string, port int, bufSzMB int) bridge = weave.NullBridge{} } sleeve := weave.NewSleeveOverlay(port) - overlays.Add("sleeve", sleeve) - overlays.SetCompatOverlay(sleeve) - return overlays, bridge + overlay.Add("sleeve", sleeve) + overlay.SetCompatOverlay(sleeve) + return overlay, bridge } func parseAndCheckCIDR(cidrStr string) address.CIDR { From 9b4a5d1cb40319df7b3a2b5632d4555e3d2e23b9 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 13 Jan 2016 12:49:38 +0000 Subject: [PATCH 6/6] tiny refactor: just move around some code The router name derivation uses the result of createOverlay, so move it closer to that. --- prog/weaver/main.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 4a20bec9ab..53aa8d26da 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -133,24 +133,13 @@ func main() { overlay, bridge := createOverlay(datapathName, ifaceName, config.Port, bufSzMB) networkConfig.Bridge = bridge - if password == "" { - password = os.Getenv("WEAVE_PASSWORD") - } - if password == "" { - Log.Println("Communication between peers is unencrypted.") - } else { - config.Password = []byte(password) - Log.Println("Communication between peers via untrusted networks is encrypted.") - } - if routerName == "" { - iface := networkConfig.Bridge.Interface() + iface := bridge.Interface() if iface == nil { Log.Fatal("Either an interface must be specified with --datapath or --iface, or a name with --name") } routerName = iface.HardwareAddr.String() } - name, err := mesh.PeerNameFromUserInput(routerName) checkFatal(err) @@ -159,6 +148,16 @@ func main() { checkFatal(err) } + if password == "" { + password = os.Getenv("WEAVE_PASSWORD") + } + if password == "" { + Log.Println("Communication between peers is unencrypted.") + } else { + config.Password = []byte(password) + Log.Println("Communication between peers via untrusted networks is encrypted.") + } + if prof != "" { p := *profile.CPUProfile p.ProfilePath = prof