refactor: make DF flag passing logic more obvious

...and provide a better abstraction for sending special frames.
This commit is contained in:
Matthias Radestock committed 2015-06-26 11:11:17 +01:00
1 parent 33d85aaee5
commit 039a2030db
3 files changed
+28 -33

No files matched your search

+9 -22
View File
@@ -50,7 +50,7 @@ type LocalConnection struct {
SessionKey *[32]byte SessionKey *[32]byte
heartbeatTCP *time.Ticker heartbeatTCP *time.Ticker
heartbeatTimeout *time.Timer heartbeatTimeout *time.Timer
heartbeatFrame *ForwardedFrame heartbeatFrame []byte
heartbeat *time.Ticker heartbeat *time.Ticker
fragTest *time.Ticker fragTest *time.Ticker
forwarder *Forwarder forwarder *Forwarder
@@ -156,8 +156,7 @@ func (conn *LocalConnection) setEffectivePMTU(pmtu int) {
} }
// Called by the connection's actor process, and by the connection's // Called by the connection's actor process, and by the connection's
// TCP received process. StackFrag is read in conn.Forward (called by // TCP receiver process. StackFrag is read in conn.forward
// router udp listener and sniffer processes)
func (conn *LocalConnection) setStackFrag(frag bool) { func (conn *LocalConnection) setStackFrag(frag bool) {
conn.Lock() conn.Lock()
defer conn.Unlock() defer conn.Unlock()
@@ -251,15 +250,11 @@ func (conn *LocalConnection) SetEstablished() {
} }
// Send a large frame down the DF channel in order to prompt // Send a large frame down the DF channel in order to prompt
// PMTU discovery to start. // PMTU discovery to start.
conn.Forward(true, &ForwardedFrame{ conn.Send(true, PMTUDiscovery)
srcPeer: conn.local,
dstPeer: conn.remote,
frame: PMTUDiscovery},
nil)
conn.heartbeat = time.NewTicker(SlowHeartbeat) conn.heartbeat = time.NewTicker(SlowHeartbeat)
conn.fragTest = time.NewTicker(FragTestInterval) conn.fragTest = time.NewTicker(FragTestInterval)
// avoid initial waits for timers to fire // avoid initial waits for timers to fire
conn.Forward(true, conn.heartbeatFrame, nil) conn.Send(true, conn.heartbeatFrame)
conn.performFragTest() conn.performFragTest()
return nil return nil
}) })
@@ -355,12 +350,8 @@ func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished ch
func (conn *LocalConnection) initHeartbeats() error { func (conn *LocalConnection) initHeartbeats() error {
conn.heartbeatTCP = time.NewTicker(TCPHeartbeat) conn.heartbeatTCP = time.NewTicker(TCPHeartbeat)
conn.heartbeatTimeout = time.NewTimer(HeartbeatTimeout) conn.heartbeatTimeout = time.NewTimer(HeartbeatTimeout)
heartbeatFrameBytes := make([]byte, EthernetOverhead+8) conn.heartbeatFrame = make([]byte, EthernetOverhead+8)
binary.BigEndian.PutUint64(heartbeatFrameBytes[EthernetOverhead:], conn.uid) binary.BigEndian.PutUint64(conn.heartbeatFrame[EthernetOverhead:], conn.uid)
conn.heartbeatFrame = &ForwardedFrame{
srcPeer: conn.local,
dstPeer: conn.remote,
frame: heartbeatFrameBytes}
if conn.remoteUDPAddr == nil { if conn.remoteUDPAddr == nil {
return nil return nil
} }
@@ -377,7 +368,7 @@ func (conn *LocalConnection) actorLoop(actionChan <-chan ConnectionAction) (err
case <-conn.heartbeatTimeout.C: case <-conn.heartbeatTimeout.C:
err = fmt.Errorf("timed out waiting for UDP heartbeat") err = fmt.Errorf("timed out waiting for UDP heartbeat")
case <-tickerChan(conn.heartbeat): case <-tickerChan(conn.heartbeat):
conn.Forward(true, conn.heartbeatFrame, nil) conn.Send(true, conn.heartbeatFrame)
case <-tickerChan(conn.fragTest): case <-tickerChan(conn.fragTest):
conn.performFragTest() conn.performFragTest()
} }
@@ -485,18 +476,14 @@ func (conn *LocalConnection) sendFastHeartbeats() error {
err := conn.ensureForwarders() err := conn.ensureForwarders()
if err == nil { if err == nil {
conn.heartbeat = time.NewTicker(FastHeartbeat) conn.heartbeat = time.NewTicker(FastHeartbeat)
conn.Forward(true, conn.heartbeatFrame, nil) // avoid initial wait conn.Send(true, conn.heartbeatFrame) // avoid initial wait
} }
return err return err
} }
func (conn *LocalConnection) performFragTest() { func (conn *LocalConnection) performFragTest() {
conn.setStackFrag(false) conn.setStackFrag(false)
conn.Forward(false, &ForwardedFrame{ conn.Send(false, FragTest)
srcPeer: conn.local,
dstPeer: conn.remote,
frame: FragTest},
nil)
} }
func tickerChan(ticker *time.Ticker) <-chan time.Time { func tickerChan(ticker *time.Ticker) <-chan time.Time {
+17 -9
View File
@@ -59,11 +59,23 @@ func (conn *LocalConnection) stopForwarders() {
conn.forwarderDF.Shutdown() conn.forwarderDF.Shutdown()
} }
// Called from peer.Relay[Broadcast] which is itself invoked from // Called from connection's actor process, and from the connection's
// router (both UDP listener process and sniffer process). Also called // TCP receiver process.
// from connection's heartbeat process, and from the connection's TCP func (conn *LocalConnection) Send(df bool, frameBytes []byte) error {
// receiver process. frame := &ForwardedFrame{
func (conn *LocalConnection) Forward(df bool, frame *ForwardedFrame, dec *EthernetDecoder) error { srcPeer: conn.local,
dstPeer: conn.remote,
frame: frameBytes}
return conn.forward(frame, nil, df)
}
// Called from LocalPeer.Relay[Broadcast] which is itself invoked from
// router (both UDP listener process and sniffer process).
func (conn *LocalConnection) Forward(frame *ForwardedFrame, dec *EthernetDecoder) error {
return conn.forward(frame, dec, dec != nil && dec.DF())
}
func (conn *LocalConnection) forward(frame *ForwardedFrame, dec *EthernetDecoder, df bool) error {
conn.RLock() conn.RLock()
var ( var (
forwarder = conn.forwarder forwarder = conn.forwarder
@@ -78,10 +90,6 @@ func (conn *LocalConnection) Forward(df bool, frame *ForwardedFrame, dec *Ethern
return nil return nil
} }
if dec != nil {
df = dec.DF()
}
// We could use non-blocking channel sends here, i.e. drop frames // We could use non-blocking channel sends here, i.e. drop frames
// on the floor when the forwarder is busy. This would allow our // on the floor when the forwarder is busy. This would allow our
// caller - the capturing loop in the router - to read frames more // caller - the capturing loop in the router - to read frames more
+2 -2
View File
@@ -50,7 +50,7 @@ func (peer *LocalPeer) Relay(srcPeer, dstPeer *Peer, frame []byte, dec *Ethernet
log.Println("Unable to find connection to relay peer", relayPeerName) log.Println("Unable to find connection to relay peer", relayPeerName)
return nil return nil
} }
return conn.(*LocalConnection).Forward(false, &ForwardedFrame{ return conn.(*LocalConnection).Forward(&ForwardedFrame{
srcPeer: srcPeer, srcPeer: srcPeer,
dstPeer: dstPeer, dstPeer: dstPeer,
frame: frame}, frame: frame},
@@ -63,7 +63,7 @@ func (peer *LocalPeer) RelayBroadcast(srcPeer *Peer, frame []byte, dec *Ethernet
return return
} }
for _, conn := range peer.ConnectionsTo(nextHops) { for _, conn := range peer.ConnectionsTo(nextHops) {
err := conn.(*LocalConnection).Forward(false, &ForwardedFrame{ err := conn.(*LocalConnection).Forward(&ForwardedFrame{
srcPeer: srcPeer, srcPeer: srcPeer,
dstPeer: conn.Remote(), dstPeer: conn.Remote(),
frame: frame}, frame: frame},