discover pmtu with binary search

issue #15
This commit is contained in:
Matthias Radestock committed 2014-08-31 13:39:44 +01:00
1 parent 848d852e0d
commit 2649796369
3 files changed
+70 -58

No files matched your search

+3 -17
View File
@@ -99,26 +99,12 @@ func (conn *LocalConnection) CheckFatal(err error) error {
func (conn *LocalConnection) setEffectivePMTU(pmtu int) { func (conn *LocalConnection) setEffectivePMTU(pmtu int) {
conn.Lock() conn.Lock()
defer conn.Unlock() defer conn.Unlock()
conn.effectivePMTU = pmtu if (conn.effectivePMTU != pmtu) {
conn.pmtuVerified = false conn.effectivePMTU = pmtu
conn.log("Effective PMTU set to", pmtu) conn.log("Effective PMTU set to", pmtu)
}
func (conn *LocalConnection) effectivePMTUVerification(pmtu int) {
conn.Lock()
defer conn.Unlock()
if pmtu == conn.effectivePMTU && !conn.pmtuVerified {
conn.pmtuVerified = true
conn.log("Effective PMTU verified at", pmtu)
} }
} }
func (conn *LocalConnection) isEffectivePMTUVerfied() bool {
conn.RLock()
defer conn.RUnlock()
return conn.pmtuVerified
}
// Called by the connection's actor process, by the connection's // Called by the connection's actor process, by the connection's
// heartbeat process and by the connection's TCP received // heartbeat process and by the connection's TCP received
// process. StackFrag is read in conn.Forward (called by router udp // process. StackFrag is read in conn.Forward (called by router udp
+61 -37
View File
@@ -49,7 +49,7 @@ func (conn *LocalConnection) ensureForwarders() error {
conn.stopForward = stopForward conn.stopForward = stopForward
conn.stopForwardDF = stopForwardDF conn.stopForwardDF = stopForwardDF
conn.verifyPMTU = verifyPMTU conn.verifyPMTU = verifyPMTU
conn.effectivePMTU = forwarder.effectivePMTU conn.effectivePMTU = forwarder.unverifiedPMTU
conn.Unlock() conn.Unlock()
go forwarder.Run() go forwarder.Run()
@@ -183,16 +183,17 @@ func fragment(eth layers.Ethernet, ip layers.IPv4, pmtu int, frame *ForwardedFra
// Forwarder // Forwarder
func NewForwarder(conn *LocalConnection, ch <-chan *ForwardedFrame, stop <-chan interface{}, pmtuVerified <-chan int, enc Encryptor, udpSender UDPSender, pmtu int) *Forwarder { func NewForwarder(conn *LocalConnection, ch <-chan *ForwardedFrame, stop <-chan interface{}, verifyPMTU <-chan int, enc Encryptor, udpSender UDPSender, pmtu int) *Forwarder {
forwarder := &Forwarder{ fwd := &Forwarder{
conn: conn, conn: conn,
ch: ch, ch: ch,
stop: stop, stop: stop,
pmtuVerified: pmtuVerified, verifyPMTU: verifyPMTU,
enc: enc, enc: enc,
udpSender: udpSender} udpSender: udpSender}
forwarder.setMaxPayloadAndEffectivePMTU(pmtu) fwd.unverifiedPMTU = pmtu - fwd.effectiveOverhead()
return forwarder fwd.maxPayload = pmtu - UDPOverhead
return fwd
} }
func (fwd *Forwarder) Run() { func (fwd *Forwarder) Run() {
@@ -205,24 +206,36 @@ func (fwd *Forwarder) Run() {
case <-fwd.stop: case <-fwd.stop:
fwd.drain() fwd.drain()
return return
case <-fwd.pmtuVerifyTick: case <-fwd.verifyPMTUTick:
// We only do this case here when we know the buffers are // We only do this case here when we know the buffers are
// all empty so that we don't risk appending verify-frames // all empty so that we don't risk appending verify-frames
// to other data. // to other data.
fwd.pmtuVerifyTick = nil fwd.verifyPMTUTick = nil
if !fwd.conn.isEffectivePMTUVerfied() { if fwd.pmtuVerified {
if fwd.pmtuVerifyCount == 0 { continue
fwd.pmtuVerifyCount = PMTUVerifyAttempts }
fwd.effectivePMTU -= 8 if fwd.pmtuVerifyCount > 0 {
fwd.maxPayload -= 8 fwd.pmtuVerifyCount--
fwd.conn.setEffectivePMTU(fwd.effectivePMTU) fwd.attemptVerifyEffectivePMTU()
} else { } else {
fwd.pmtuVerifyCount-- // we've exceeded the verification attempts of the
} // unverifiedPMTU
fwd.verifyEffectivePMTU() fwd.lowestBadPMTU = fwd.unverifiedPMTU
fwd.verifyEffectivePMTU((fwd.highestGoodPMTU + fwd.lowestBadPMTU) / 2)
}
case epmtu := <-fwd.verifyPMTU:
if fwd.pmtuVerified || epmtu != fwd.unverifiedPMTU {
continue
}
if epmtu+1 < fwd.lowestBadPMTU {
fwd.highestGoodPMTU = fwd.unverifiedPMTU // = epmtu
fwd.verifyEffectivePMTU((fwd.highestGoodPMTU + fwd.lowestBadPMTU) / 2)
} else {
fwd.pmtuVerified = true
fwd.maxPayload = epmtu + fwd.effectiveOverhead() - UDPOverhead
fwd.conn.setEffectivePMTU(epmtu)
fwd.conn.log("Effective PMTU verified at", epmtu)
} }
case pmtu := <-fwd.pmtuVerified:
fwd.conn.effectivePMTUVerification(pmtu)
case frame = <-fwd.ch: case frame = <-fwd.ch:
if !fwd.appendFrame(frame) { if !fwd.appendFrame(frame) {
fwd.logDrop(frame) fwd.logDrop(frame)
@@ -250,20 +263,25 @@ func (fwd *Forwarder) Run() {
} }
} }
func (fwd *Forwarder) setMaxPayloadAndEffectivePMTU(pmtu int) { func (fwd *Forwarder) effectiveOverhead() int {
fwd.maxPayload = pmtu - UDPOverhead return UDPOverhead + fwd.enc.PacketOverhead() + fwd.enc.FrameOverhead() + EthernetOverhead
fwd.effectivePMTU = fwd.maxPayload - fwd.enc.PacketOverhead() - fwd.enc.FrameOverhead() - EthernetOverhead
} }
func (fwd *Forwarder) verifyEffectivePMTU() { func (fwd *Forwarder) verifyEffectivePMTU(newUnverifiedPMTU int) {
fwd.unverifiedPMTU = newUnverifiedPMTU
fwd.pmtuVerifyCount = PMTUVerifyAttempts
fwd.attemptVerifyEffectivePMTU()
}
func (fwd *Forwarder) attemptVerifyEffectivePMTU() {
pmtuVerifyFrame := &ForwardedFrame{ pmtuVerifyFrame := &ForwardedFrame{
srcPeer: fwd.conn.local, srcPeer: fwd.conn.local,
dstPeer: fwd.conn.remote, dstPeer: fwd.conn.remote,
frame: make([]byte, fwd.effectivePMTU+EthernetOverhead)} frame: make([]byte, fwd.unverifiedPMTU+EthernetOverhead)}
fwd.enc.AppendFrame(pmtuVerifyFrame) fwd.enc.AppendFrame(pmtuVerifyFrame)
fwd.flush() fwd.flush()
if fwd.pmtuVerifyTick == nil { if fwd.verifyPMTUTick == nil {
fwd.pmtuVerifyTick = time.After(PMTUVerifyTimeout << (PMTUVerifyAttempts - fwd.pmtuVerifyCount)) fwd.verifyPMTUTick = time.After(PMTUVerifyTimeout << (PMTUVerifyAttempts - fwd.pmtuVerifyCount))
} }
} }
@@ -280,10 +298,16 @@ func (fwd *Forwarder) flush() {
err := fwd.udpSender.Send(fwd.enc.Bytes()) err := fwd.udpSender.Send(fwd.enc.Bytes())
if err != nil { if err != nil {
if mtbe, ok := err.(MsgTooBigError); ok { if mtbe, ok := err.(MsgTooBigError); ok {
fwd.setMaxPayloadAndEffectivePMTU(mtbe.PMTU) newUnverifiedPMTU := mtbe.PMTU - fwd.effectiveOverhead()
fwd.conn.setEffectivePMTU(fwd.effectivePMTU) if newUnverifiedPMTU >= fwd.unverifiedPMTU {
fwd.pmtuVerifyCount = PMTUVerifyAttempts return
fwd.verifyEffectivePMTU() }
fwd.pmtuVerified = false
fwd.maxPayload = mtbe.PMTU - UDPOverhead
fwd.highestGoodPMTU = 8
fwd.lowestBadPMTU = newUnverifiedPMTU + 1
fwd.conn.setEffectivePMTU(newUnverifiedPMTU)
fwd.verifyEffectivePMTU(newUnverifiedPMTU)
} else if PosixError(err) == syscall.ENOBUFS { } else if PosixError(err) == syscall.ENOBUFS {
// TODO handle this better // TODO handle this better
} else { } else {
@@ -306,7 +330,7 @@ func (fwd *Forwarder) drain() {
} }
func (fwd *Forwarder) logDrop(frame *ForwardedFrame) { func (fwd *Forwarder) logDrop(frame *ForwardedFrame) {
fwd.conn.log("dropping too big frame during forwarding: frame len:", len(frame.frame), "; effective PMTU:", fwd.effectivePMTU) fwd.conn.log("Dropping too big frame during forwarding: frame len:", len(frame.frame), "; effective PMTU:", fwd.maxPayload+UDPOverhead-fwd.effectiveOverhead())
} }
// UDP Senders // UDP Senders
+6 -4
View File
@@ -66,7 +66,6 @@ type LocalConnection struct {
established bool established bool
stackFrag bool stackFrag bool
effectivePMTU int effectivePMTU int
pmtuVerified bool
SessionKey *[32]byte SessionKey *[32]byte
heartbeatStop chan<- interface{} heartbeatStop chan<- interface{}
forwardChan chan<- *ForwardedFrame forwardChan chan<- *ForwardedFrame
@@ -90,13 +89,16 @@ type Forwarder struct {
conn *LocalConnection conn *LocalConnection
ch <-chan *ForwardedFrame ch <-chan *ForwardedFrame
stop <-chan interface{} stop <-chan interface{}
pmtuVerifyTick <-chan time.Time verifyPMTUTick <-chan time.Time
pmtuVerified <-chan int verifyPMTU <-chan int
pmtuVerifyCount uint pmtuVerifyCount uint
enc Encryptor enc Encryptor
udpSender UDPSender udpSender UDPSender
maxPayload int maxPayload int
effectivePMTU int pmtuVerified bool
highestGoodPMTU int
unverifiedPMTU int
lowestBadPMTU int
} }
type UDPPacket struct { type UDPPacket struct {