Skip to content

Commit

Permalink
Merge branch 'pr1254'
Browse files Browse the repository at this point in the history
  • Loading branch information
squaremo committed Aug 3, 2015
2 parents ca89cfe + 778038f commit 281bf02
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 54 deletions.
7 changes: 0 additions & 7 deletions common/utils.go
Expand Up @@ -6,10 +6,3 @@ func Assert(test bool) {
panic("Assertion failure")
}
}

func OnOff(b bool) string {
if b {
return "on"
}
return "off"
}
12 changes: 5 additions & 7 deletions router/connection.go
Expand Up @@ -7,8 +7,6 @@ import (
"strconv"
"sync"
"time"

. "github.com/weaveworks/weave/common"
)

type Connection interface {
Expand Down Expand Up @@ -84,11 +82,11 @@ func (conn *RemoteConnection) BreakTie(Connection) ConnectionTieBreak { return T
func (conn *RemoteConnection) Shutdown(error) {}

func (conn *RemoteConnection) Log(args ...interface{}) {
Log.Println(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...)
log.Println(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...)
}

func (conn *RemoteConnection) ErrorLog(args ...interface{}) {
Log.Errorln(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...)
log.Errorln(append(append([]interface{}{}, fmt.Sprintf("->[%s|%s]:", conn.remoteTCPAddr, conn.remote)), args...)...)
}

func (conn *RemoteConnection) String() string {
Expand All @@ -107,7 +105,7 @@ func (conn *RemoteConnection) String() string {
// end up in the local peer's connections map.
func StartLocalConnection(connRemote *RemoteConnection, tcpConn *net.TCPConn, udpAddr *net.UDPAddr, router *Router, acceptNewPeer bool) {
if connRemote.local != router.Ourself.Peer {
Log.Fatal("Attempt to create local connection from a peer which is not ourself")
log.Fatal("Attempt to create local connection from a peer which is not ourself")
}
// NB, we're taking a copy of connRemote here.
actionChan := make(chan ConnectionAction, ChannelSize)
Expand Down Expand Up @@ -233,7 +231,7 @@ func (conn *LocalConnection) ReceivedHeartbeat(remoteUDPAddr *net.UDPAddr, connU
if oldRemoteUDPAddr == nil {
return conn.sendFastHeartbeats()
} else if oldRemoteUDPAddr.String() != remoteUDPAddr.String() {
Log.Println("Peer", conn.remote, "moved from", oldRemoteUDPAddr, "to", remoteUDPAddr)
log.Println("Peer", conn.remote, "moved from", oldRemoteUDPAddr, "to", remoteUDPAddr)
}
return nil
})
Expand Down Expand Up @@ -488,7 +486,7 @@ func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err

func (conn *LocalConnection) shutdown(err error) {
if conn.remote == nil {
Log.Errorf("->[%s] connection shutting down due to error during handshake: %v", conn.remoteTCPAddr, err)
log.Errorf("->[%s] connection shutting down due to error during handshake: %v", conn.remoteTCPAddr, err)
} else {
conn.ErrorLog("connection shutting down due to error:", err)
}
Expand Down
6 changes: 2 additions & 4 deletions router/connection_maker.go
Expand Up @@ -6,8 +6,6 @@ import (
"math/rand"
"net"
"time"

. "github.com/weaveworks/weave/common"
)

const (
Expand Down Expand Up @@ -299,9 +297,9 @@ func (cm *ConnectionMaker) connectToTargets(validTarget map[string]struct{}, dir
}

func (cm *ConnectionMaker) attemptConnection(address string, acceptNewPeer bool) {
Log.Printf("->[%s] attempting connection", address)
log.Printf("->[%s] attempting connection", address)
if err := cm.ourself.CreateConnection(address, acceptNewPeer); err != nil {
Log.Errorf("->[%s] error during connection attempt: %v", address, err)
log.Errorf("->[%s] error during connection attempt: %v", address, err)
cm.ConnectionTerminated(address, err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions router/ethernet_decoder.go
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
. "github.com/weaveworks/weave/common"
)

type EthernetDecoder struct {
Expand Down Expand Up @@ -61,7 +60,7 @@ func (dec *EthernetDecoder) sendICMPFragNeeded(mtu int, sendFrame func([]byte) e
return err
}

Log.Printf("Sending ICMP 3,4 (%v -> %v): PMTU= %v", dec.IP.DstIP, dec.IP.SrcIP, mtu)
log.Printf("Sending ICMP 3,4 (%v -> %v): PMTU= %v", dec.IP.DstIP, dec.IP.SrcIP, mtu)
return sendFrame(buf.Bytes())
}

Expand Down
4 changes: 1 addition & 3 deletions router/gossip_channel.go
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/gob"
"fmt"
"sync"

. "github.com/weaveworks/weave/common"
)

type GossipChannel struct {
Expand Down Expand Up @@ -226,5 +224,5 @@ func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) {
}

func (c *GossipChannel) log(args ...interface{}) {
Log.Println(append(append([]interface{}{}, "[gossip "+c.name+"]:"), args...)...)
log.Println(append(append([]interface{}{}, "[gossip "+c.name+"]:"), args...)...)
}
20 changes: 9 additions & 11 deletions router/local_peer.go
Expand Up @@ -5,8 +5,6 @@ import (
"net"
"sync"
"time"

. "github.com/weaveworks/weave/common"
)

type LocalPeer struct {
Expand Down Expand Up @@ -42,13 +40,13 @@ func (peer *LocalPeer) Relay(srcPeer, dstPeer *Peer, frame []byte, dec *Ethernet
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:", dstPeer)
log.Println("Received packet for unknown destination:", 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)
log.Println("Unable to find connection to relay peer", relayPeerName)
return nil
}
return conn.(*LocalConnection).Forward(&ForwardedFrame{
Expand All @@ -71,9 +69,9 @@ func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, frame []byte, dec *Ethernet
dec)
if err != nil {
if ftbe, ok := err.(FrameTooBigError); ok {
Log.Warningf("dropping too big DF broadcast frame (%v -> %v): PMTU= %v", dec.IP.DstIP, dec.IP.SrcIP, ftbe.EPMTU)
log.Warningf("dropping too big DF broadcast frame (%v -> %v): PMTU= %v", dec.IP.DstIP, dec.IP.SrcIP, ftbe.EPMTU)
} else {
Log.Errorln(err)
log.Errorln(err)
}
}
}
Expand Down Expand Up @@ -182,10 +180,10 @@ func (peer *LocalPeer) actorLoop(actionChan <-chan LocalPeerAction) {

func (peer *LocalPeer) handleAddConnection(conn Connection) error {
if peer.Peer != conn.Local() {
Log.Fatal("Attempt made to add connection to peer where peer is not the source of connection")
log.Fatal("Attempt made to add connection to peer where peer is not the source of connection")
}
if conn.Remote() == nil {
Log.Fatal("Attempt made to add connection to peer with unknown remote peer")
log.Fatal("Attempt made to add connection to peer with unknown remote peer")
}
toName := conn.Remote().Name
dupErr := fmt.Errorf("Multiple connections to %s added to %s", conn.Remote(), peer.String())
Expand Down Expand Up @@ -224,7 +222,7 @@ func (peer *LocalPeer) handleAddConnection(conn Connection) error {

func (peer *LocalPeer) handleConnectionEstablished(conn Connection) {
if peer.Peer != conn.Local() {
Log.Fatal("Peer informed of active connection where peer is not the source of connection")
log.Fatal("Peer informed of active connection where peer is not the source of connection")
}
if dupConn, found := peer.connections[conn.Remote().Name]; !found || conn != dupConn {
conn.Shutdown(fmt.Errorf("Cannot set unknown connection active"))
Expand All @@ -237,10 +235,10 @@ func (peer *LocalPeer) handleConnectionEstablished(conn Connection) {

func (peer *LocalPeer) handleDeleteConnection(conn Connection) {
if peer.Peer != conn.Local() {
Log.Fatal("Attempt made to delete connection from peer where peer is not the source of connection")
log.Fatal("Attempt made to delete connection from peer where peer is not the source of connection")
}
if conn.Remote() == nil {
Log.Fatal("Attempt made to delete connection to peer with unknown remote peer")
log.Fatal("Attempt made to delete connection to peer with unknown remote peer")
}
toName := conn.Remote().Name
if connFound, found := peer.connections[toName]; !found || connFound != conn {
Expand Down
35 changes: 20 additions & 15 deletions router/router.go
Expand Up @@ -9,8 +9,6 @@ import (
"sync"
"syscall"
"time"

. "github.com/weaveworks/weave/common"
)

const (
Expand Down Expand Up @@ -69,11 +67,11 @@ type PacketSourceSink interface {
func NewRouter(config Config, name PeerName, nickName string) *Router {
router := &Router{Config: config, gossipChannels: make(GossipChannels)}
onMacExpiry := func(mac net.HardwareAddr, peer *Peer) {
Log.Println("Expired MAC", mac, "at", peer)
log.Println("Expired MAC", mac, "at", peer)
}
onPeerGC := func(peer *Peer) {
router.Macs.Delete(peer)
Log.Println("Removed unreachable peer", peer)
log.Println("Removed unreachable peer", peer)
}
router.Ourself = NewLocalPeer(name, nickName, router)
router.Macs = NewMacCache(macMaxAge, onMacExpiry)
Expand Down Expand Up @@ -120,8 +118,8 @@ func (router *Router) UsingPassword() bool {
func (router *Router) Status() string {
var buf bytes.Buffer
fmt.Fprintln(&buf, "Our name is", router.Ourself)
fmt.Fprintln(&buf, "Encryption", OnOff(router.UsingPassword()))
fmt.Fprintln(&buf, "Peer discovery", OnOff(router.PeerDiscovery))
fmt.Fprintln(&buf, "Encryption", onOff(router.UsingPassword()))
fmt.Fprintln(&buf, "Peer discovery", onOff(router.PeerDiscovery))
fmt.Fprintln(&buf, "Sniffing traffic on", router.Iface)
fmt.Fprintf(&buf, "MACs:\n%s", router.Macs)
fmt.Fprintf(&buf, "Peers:\n%s", router.Peers)
Expand All @@ -130,13 +128,20 @@ func (router *Router) Status() string {
return buf.String()
}

func onOff(b bool) string {
if b {
return "on"
}
return "off"
}

func (router *Router) sniff(pio PacketSourceSink) {
Log.Println("Sniffing traffic on", router.Iface)
log.Println("Sniffing traffic on", router.Iface)

dec := NewEthernetDecoder()
mac := router.Iface.HardwareAddr
if router.Macs.Enter(mac, router.Ourself.Peer) {
Log.Println("Discovered our MAC", mac)
log.Println("Discovered our MAC", mac)
}
go func() {
for {
Expand All @@ -163,7 +168,7 @@ func (router *Router) handleCapturedPacket(frameData []byte, dec *EthernetDecode
return
}
if router.Macs.Enter(srcMac, router.Ourself.Peer) {
Log.Println("Discovered local MAC", srcMac)
log.Println("Discovered local MAC", srcMac)
}
if dec.DropFrame() {
return
Expand Down Expand Up @@ -206,7 +211,7 @@ func (router *Router) listenTCP(localPort int) {
for {
tcpConn, err := ln.AcceptTCP()
if err != nil {
Log.Errorln(err)
log.Errorln(err)
continue
}
router.acceptTCP(tcpConn)
Expand All @@ -220,7 +225,7 @@ func (router *Router) acceptTCP(tcpConn *net.TCPConn) {
// on router.Port and we wait for them to send us something on UDP to
// start.
remoteAddrStr := tcpConn.RemoteAddr().String()
Log.Printf("->[%s] connection accepted", remoteAddrStr)
log.Printf("->[%s] connection accepted", remoteAddrStr)
connRemote := NewRemoteConnection(router.Ourself.Peer, nil, remoteAddrStr, false, false)
StartLocalConnection(connRemote, tcpConn, nil, router, true)
}
Expand Down Expand Up @@ -250,10 +255,10 @@ func (router *Router) udpReader(conn *net.UDPConn, po PacketSink) {
if err == io.EOF {
return
} else if err != nil {
Log.Warnln("ignoring UDP read error", err)
log.Warnln("ignoring UDP read error", err)
continue
} else if n < NameSize {
Log.Warnln("ignoring too short UDP packet from", sender)
log.Warnln("ignoring too short UDP packet from", sender)
continue
}
name := PeerNameFromBin(buf[:NameSize])
Expand Down Expand Up @@ -330,7 +335,7 @@ func (router *Router) handleUDPPacketFunc(relayConn *LocalConnection, dec *Ether
dstMac := dec.Eth.DstMAC

if router.Macs.Enter(srcMac, srcPeer) {
Log.Println("Discovered remote MAC", srcMac, "at", srcPeer)
log.Println("Discovered remote MAC", srcMac, "at", srcPeer)
}
if po != nil {
router.LogFrame("Injecting", frame, dec)
Expand Down Expand Up @@ -416,7 +421,7 @@ func (router *Router) applyTopologyUpdate(update []byte) (PeerNameSet, PeerNameS
// itself included in the update, and we didn't know about
// already. We ignore this; eventually we should receive an
// update containing a complete topology.
Log.Println("Topology gossip:", err)
log.Println("Topology gossip:", err)
return nil, nil, nil
}
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions router/udp_sender.go
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
. "github.com/weaveworks/weave/common"
)

type UDPSender interface {
Expand Down Expand Up @@ -87,7 +86,7 @@ func (sender *RawUDPSender) Send(msg []byte) error {
}
defer f.Close()
fd := int(f.Fd())
Log.Println("EMSGSIZE on send, expecting PMTU update (IP packet was",
log.Println("EMSGSIZE on send, expecting PMTU update (IP packet was",
len(packet), "bytes, payload was", len(msg), "bytes)")
pmtu, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_MTU)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions router/utils.go
Expand Up @@ -7,20 +7,22 @@ import (
"fmt"
"net"

. "github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/common"
)

var log = common.Log

var void = struct{}{}

func checkFatal(e error) {
if e != nil {
Log.Fatal(e)
log.Fatal(e)
}
}

func checkWarn(e error) {
if e != nil {
Log.Warnln(e)
log.Warnln(e)
}
}

Expand Down

0 comments on commit 281bf02

Please sign in to comment.