Skip to content

Commit

Permalink
Merge pull request #1888 from weaveworks/refactor-overlay-init
Browse files Browse the repository at this point in the history
refactor overlay creation
  • Loading branch information
awh committed Jan 13, 2016
2 parents 9f0399f + 9b4a5d1 commit b0e98f9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
73 changes: 40 additions & 33 deletions prog/weaver/main.go
Expand Up @@ -65,7 +65,6 @@ func main() {
peers []string
noDNS bool
dnsConfig dnsConfig
iface *net.Interface
datapathName string
trustedSubnetStr string

Expand Down Expand Up @@ -131,43 +130,16 @@ 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 != "":
var err error
iface, err = weavenet.EnsureInterface(ifaceName)
checkFatal(err)
networkConfig.Bridge, err = weave.NewPcap(iface, bufSzMB*1024*1024) // bufsz flag is in MB
checkFatal(err)
}
sleeve := weave.NewSleeveOverlay(config.Port)
overlays.Add("sleeve", sleeve)
overlays.SetCompatOverlay(sleeve)

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.")
}
overlay, bridge := createOverlay(datapathName, ifaceName, config.Port, bufSzMB)
networkConfig.Bridge = bridge

if routerName == "" {
iface := 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()
}

name, err := mesh.PeerNameFromUserInput(routerName)
checkFatal(err)

Expand All @@ -176,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
Expand All @@ -195,7 +177,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
Expand Down Expand Up @@ -309,6 +291,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) {
overlay := 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()
overlay.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)
overlay.Add("sleeve", sleeve)
overlay.SetCompatOverlay(sleeve)
return overlay, bridge
}

func parseAndCheckCIDR(cidrStr string) address.CIDR {
_, cidr, err := address.ParseCIDR(cidrStr)
checkFatal(err)
Expand Down
9 changes: 9 additions & 0 deletions 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
Expand All @@ -9,6 +13,7 @@ type Bridge interface {
// should not be included.
StartConsumingPackets(BridgeConsumer) error

Interface() *net.Interface
String() string
Stats() map[string]int
}
Expand All @@ -26,6 +31,10 @@ func (NullBridge) StartConsumingPackets(BridgeConsumer) error {
return nil
}

func (NullBridge) Interface() *net.Interface {
return nil
}

func (NullBridge) String() string {
return "<no bridge networking>"
}
Expand Down
14 changes: 7 additions & 7 deletions router/fastdp.go
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -208,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)")
}
Expand Down Expand Up @@ -665,7 +665,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)))

Expand Down
4 changes: 4 additions & 0 deletions router/pcap.go
Expand Up @@ -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)")
}
Expand Down

0 comments on commit b0e98f9

Please sign in to comment.