move congestion controller related constants to the congestion package

This commit is contained in:
Marten Seemann 2020-01-22 14:40:14 +07:00
parent 62d3a4166a
commit 31e4691ffd
9 changed files with 144 additions and 146 deletions

View file

@ -10,7 +10,7 @@ import (
)
const initialCongestionWindowPackets = 10
const defaultWindowTCP = protocol.ByteCount(initialCongestionWindowPackets) * protocol.DefaultTCPMSS
const defaultWindowTCP = protocol.ByteCount(initialCongestionWindowPackets) * defaultTCPMSS
type mockClock time.Time
@ -22,7 +22,7 @@ func (c *mockClock) Advance(d time.Duration) {
*c = mockClock(time.Time(*c).Add(d))
}
const MaxCongestionWindow protocol.ByteCount = 200 * protocol.DefaultTCPMSS
const MaxCongestionWindow protocol.ByteCount = 200 * defaultTCPMSS
var _ = Describe("Cubic Sender", func() {
var (
@ -40,7 +40,7 @@ var _ = Describe("Cubic Sender", func() {
ackedPacketNumber = 0
clock = mockClock{}
rttStats = NewRTTStats()
sender = NewCubicSender(&clock, rttStats, true /*reno*/, initialCongestionWindowPackets*protocol.DefaultTCPMSS, MaxCongestionWindow)
sender = newCubicSender(&clock, rttStats, true /*reno*/, initialCongestionWindowPackets*defaultTCPMSS, MaxCongestionWindow)
})
SendAvailableSendWindowLen := func(packetLength protocol.ByteCount) int {
@ -60,9 +60,9 @@ var _ = Describe("Cubic Sender", func() {
sender.MaybeExitSlowStart()
for i := 0; i < n; i++ {
ackedPacketNumber++
sender.OnPacketAcked(ackedPacketNumber, protocol.DefaultTCPMSS, bytesInFlight, clock.Now())
sender.OnPacketAcked(ackedPacketNumber, defaultTCPMSS, bytesInFlight, clock.Now())
}
bytesInFlight -= protocol.ByteCount(n) * protocol.DefaultTCPMSS
bytesInFlight -= protocol.ByteCount(n) * defaultTCPMSS
clock.Advance(time.Millisecond)
}
@ -76,12 +76,12 @@ var _ = Describe("Cubic Sender", func() {
// Does not increment acked_packet_number_.
LosePacket := func(number protocol.PacketNumber) {
sender.OnPacketLost(number, protocol.DefaultTCPMSS, bytesInFlight)
bytesInFlight -= protocol.DefaultTCPMSS
sender.OnPacketLost(number, defaultTCPMSS, bytesInFlight)
bytesInFlight -= defaultTCPMSS
}
SendAvailableSendWindow := func() int { return SendAvailableSendWindowLen(protocol.DefaultTCPMSS) }
LoseNPackets := func(n int) { LoseNPacketsLen(n, protocol.DefaultTCPMSS) }
SendAvailableSendWindow := func() int { return SendAvailableSendWindowLen(defaultTCPMSS) }
LoseNPackets := func(n int) { LoseNPacketsLen(n, defaultTCPMSS) }
It("has the right values at startup", func() {
// At startup make sure we are at the default.
@ -121,7 +121,7 @@ var _ = Describe("Cubic Sender", func() {
bytesToSend := sender.GetCongestionWindow()
// It's expected 2 acks will arrive when the bytes_in_flight are greater than
// half the CWND.
Expect(bytesToSend).To(Equal(defaultWindowTCP + protocol.DefaultTCPMSS*2*2))
Expect(bytesToSend).To(Equal(defaultWindowTCP + defaultTCPMSS*2*2))
})
It("exponential slow start", func() {
@ -139,7 +139,7 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
cwnd := sender.GetCongestionWindow()
Expect(cwnd).To(Equal(defaultWindowTCP + protocol.DefaultTCPMSS*2*numberOfAcks))
Expect(cwnd).To(Equal(defaultWindowTCP + defaultTCPMSS*2*numberOfAcks))
Expect(sender.BandwidthEstimate()).To(Equal(BandwidthFromDelta(cwnd, rttStats.SmoothedRTT())))
})
@ -152,12 +152,12 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
SendAvailableSendWindow()
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Lose a packet to exit slow start.
LoseNPackets(1)
packetsInRecoveryWindow := expectedSendWindow / protocol.DefaultTCPMSS
packetsInRecoveryWindow := expectedSendWindow / defaultTCPMSS
// We should now have fallen out of slow start with a reduced window.
expectedSendWindow = protocol.ByteCount(float32(expectedSendWindow) * renoBeta)
@ -165,7 +165,7 @@ var _ = Describe("Cubic Sender", func() {
// Recovery phase. We need to ack every packet in the recovery window before
// we exit recovery.
numberOfPacketsInWindow := expectedSendWindow / protocol.DefaultTCPMSS
numberOfPacketsInWindow := expectedSendWindow / defaultTCPMSS
AckNPackets(int(packetsInRecoveryWindow))
SendAvailableSendWindow()
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
@ -177,7 +177,7 @@ var _ = Describe("Cubic Sender", func() {
// Next ack should increase cwnd by 1.
AckNPackets(1)
expectedSendWindow += protocol.DefaultTCPMSS
expectedSendWindow += defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Now RTO and ensure slow start gets reset.
@ -197,26 +197,26 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
SendAvailableSendWindow()
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Lose a packet to exit slow start. We should now have fallen out of
// slow start with a window reduced by 1.
LoseNPackets(1)
expectedSendWindow -= protocol.DefaultTCPMSS
expectedSendWindow -= defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Lose 5 packets in recovery and verify that congestion window is reduced
// further.
LoseNPackets(5)
expectedSendWindow -= 5 * protocol.DefaultTCPMSS
expectedSendWindow -= 5 * defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
packetsInRecoveryWindow := expectedSendWindow / protocol.DefaultTCPMSS
packetsInRecoveryWindow := expectedSendWindow / defaultTCPMSS
// Recovery phase. We need to ack every packet in the recovery window before
// we exit recovery.
numberOfPacketsInWindow := expectedSendWindow / protocol.DefaultTCPMSS
numberOfPacketsInWindow := expectedSendWindow / defaultTCPMSS
AckNPackets(int(packetsInRecoveryWindow))
SendAvailableSendWindow()
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
@ -228,7 +228,7 @@ var _ = Describe("Cubic Sender", func() {
// Next ack should increase cwnd by 1.
AckNPackets(1)
expectedSendWindow += protocol.DefaultTCPMSS
expectedSendWindow += defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Now RTO and ensure slow start gets reset.
@ -244,23 +244,23 @@ var _ = Describe("Cubic Sender", func() {
const numberOfAcks = 10
for i := 0; i < numberOfAcks; i++ {
// Send our full send window in half sized packets.
SendAvailableSendWindowLen(protocol.DefaultTCPMSS / 2)
SendAvailableSendWindowLen(defaultTCPMSS / 2)
AckNPackets(2)
}
SendAvailableSendWindowLen(protocol.DefaultTCPMSS / 2)
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
SendAvailableSendWindowLen(defaultTCPMSS / 2)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Lose a packet to exit slow start. We should now have fallen out of
// slow start with a window reduced by 1.
LoseNPackets(1)
expectedSendWindow -= protocol.DefaultTCPMSS
expectedSendWindow -= defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Lose 10 packets in recovery and verify that congestion window is reduced
// by 5 packets.
LoseNPacketsLen(10, protocol.DefaultTCPMSS/2)
expectedSendWindow -= 5 * protocol.DefaultTCPMSS
LoseNPacketsLen(10, defaultTCPMSS/2)
expectedSendWindow -= 5 * defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
})
@ -287,7 +287,7 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
SendAvailableSendWindow()
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
LoseNPackets(1)
@ -302,7 +302,7 @@ var _ = Describe("Cubic Sender", func() {
// outstanding packets. The number of packets before we exit recovery is the
// original CWND minus the packet that has been lost and the one which
// triggered the loss.
remainingPacketsInRecovery := sendWindowBeforeLoss/protocol.DefaultTCPMSS - 2
remainingPacketsInRecovery := sendWindowBeforeLoss/defaultTCPMSS - 2
for i := protocol.ByteCount(0); i < remainingPacketsInRecovery; i++ {
AckNPackets(1)
@ -311,7 +311,7 @@ var _ = Describe("Cubic Sender", func() {
}
// We need to ack another window before we increase CWND by 1.
numberOfPacketsInWindow := expectedSendWindow / protocol.DefaultTCPMSS
numberOfPacketsInWindow := expectedSendWindow / defaultTCPMSS
for i := protocol.ByteCount(0); i < numberOfPacketsInWindow; i++ {
AckNPackets(1)
Expect(SendAvailableSendWindow()).To(Equal(1))
@ -319,7 +319,7 @@ var _ = Describe("Cubic Sender", func() {
}
AckNPackets(1)
expectedSendWindow += protocol.DefaultTCPMSS
expectedSendWindow += defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
})
@ -336,13 +336,13 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
SendAvailableSendWindow()
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Lose one more than the congestion window reduction, so that after loss,
// bytes_in_flight is lesser than the congestion window.
sendWindowAfterLoss := protocol.ByteCount(renoBeta * float32(expectedSendWindow))
numPacketsToLose := (expectedSendWindow-sendWindowAfterLoss)/protocol.DefaultTCPMSS + 1
numPacketsToLose := (expectedSendWindow-sendWindowAfterLoss)/defaultTCPMSS + 1
LoseNPackets(int(numPacketsToLose))
// Immediately after the loss, ensure at least one packet can be sent.
// Losses without subsequent acks can occur with timer based loss detection.
@ -384,8 +384,8 @@ var _ = Describe("Cubic Sender", func() {
// Expect the window to decrease to the minimum once the RTO fires
// and slow start threshold to be set to 1/2 of the CWND.
sender.OnRetransmissionTimeout(true)
Expect(sender.GetCongestionWindow()).To(Equal(2 * protocol.DefaultTCPMSS))
Expect(sender.SlowstartThreshold()).To(Equal(5 * protocol.DefaultTCPMSS))
Expect(sender.GetCongestionWindow()).To(Equal(2 * defaultTCPMSS))
Expect(sender.SlowstartThreshold()).To(Equal(5 * defaultTCPMSS))
})
It("RTO congestion window no retransmission", func() {
@ -399,8 +399,8 @@ var _ = Describe("Cubic Sender", func() {
It("tcp cubic reset epoch on quiescence", func() {
const maxCongestionWindow = 50
const maxCongestionWindowBytes = maxCongestionWindow * protocol.DefaultTCPMSS
sender = NewCubicSender(&clock, rttStats, false, initialCongestionWindowPackets*protocol.DefaultTCPMSS, maxCongestionWindowBytes)
const maxCongestionWindowBytes = maxCongestionWindow * defaultTCPMSS
sender = newCubicSender(&clock, rttStats, false, initialCongestionWindowPackets*defaultTCPMSS, maxCongestionWindowBytes)
numSent := SendAvailableSendWindow()
@ -433,7 +433,7 @@ var _ = Describe("Cubic Sender", func() {
savedCwnd = sender.GetCongestionWindow()
SendAvailableSendWindow()
AckNPackets(1)
Expect(savedCwnd).To(BeNumerically("~", sender.GetCongestionWindow(), protocol.DefaultTCPMSS))
Expect(savedCwnd).To(BeNumerically("~", sender.GetCongestionWindow(), defaultTCPMSS))
Expect(maxCongestionWindowBytes).To(BeNumerically(">", sender.GetCongestionWindow()))
})
@ -463,7 +463,7 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
SendAvailableSendWindow()
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
LoseNPackets(1)
@ -484,7 +484,7 @@ var _ = Describe("Cubic Sender", func() {
Expect(sender.InRecovery()).To(BeFalse())
// Out of recovery now. Congestion window should not grow for half an RTT.
packetsInSendWindow := expectedSendWindow / protocol.DefaultTCPMSS
packetsInSendWindow := expectedSendWindow / defaultTCPMSS
SendAvailableSendWindow()
AckNPackets(int(packetsInSendWindow/2 - 2))
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
@ -492,7 +492,7 @@ var _ = Describe("Cubic Sender", func() {
// Next ack should increase congestion window by 1MSS.
SendAvailableSendWindow()
AckNPackets(2)
expectedSendWindow += protocol.DefaultTCPMSS
expectedSendWindow += defaultTCPMSS
packetsInSendWindow++
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
@ -504,7 +504,7 @@ var _ = Describe("Cubic Sender", func() {
// Next ack should cause congestion window to grow by 1MSS.
SendAvailableSendWindow()
AckNPackets(2)
expectedSendWindow += protocol.DefaultTCPMSS
expectedSendWindow += defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
})
@ -518,7 +518,7 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
SendAvailableSendWindow()
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
LoseNPackets(1)
@ -539,7 +539,7 @@ var _ = Describe("Cubic Sender", func() {
Expect(sender.InRecovery()).To(BeFalse())
// Out of recovery now. Congestion window should not grow during RTT.
for i := protocol.ByteCount(0); i < expectedSendWindow/protocol.DefaultTCPMSS-2; i += 2 {
for i := protocol.ByteCount(0); i < expectedSendWindow/defaultTCPMSS-2; i += 2 {
// Send our full send window.
SendAvailableSendWindow()
AckNPackets(2)
@ -549,7 +549,7 @@ var _ = Describe("Cubic Sender", func() {
// Next ack should cause congestion window to grow by 1MSS.
SendAvailableSendWindow()
AckNPackets(2)
expectedSendWindow += protocol.DefaultTCPMSS
expectedSendWindow += defaultTCPMSS
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
})
@ -562,7 +562,7 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(1)
Expect(sender.GetCongestionWindow()).To(Equal(protocol.ByteCount(renoBeta * float32(defaultWindowTCP))))
windowInPackets := renoBeta * float32(defaultWindowTCP) / float32(protocol.DefaultTCPMSS)
windowInPackets := renoBeta * float32(defaultWindowTCP) / float32(defaultTCPMSS)
numSent := SendAvailableSendWindow()
Expect(numSent).To(BeEquivalentTo(windowInPackets))
})
@ -580,7 +580,7 @@ var _ = Describe("Cubic Sender", func() {
AckNPackets(2)
}
SendAvailableSendWindow()
expectedSendWindow := defaultWindowTCP + (protocol.DefaultTCPMSS * 2 * numberOfAcks)
expectedSendWindow := defaultWindowTCP + (defaultTCPMSS * 2 * numberOfAcks)
Expect(sender.GetCongestionWindow()).To(Equal(expectedSendWindow))
// Loses a packet to exit slow start.
@ -600,19 +600,19 @@ var _ = Describe("Cubic Sender", func() {
})
It("default max cwnd", func() {
sender = NewCubicSender(&clock, rttStats, true /*reno*/, initialCongestionWindowPackets*protocol.DefaultTCPMSS, protocol.DefaultMaxCongestionWindow)
sender = newCubicSender(&clock, rttStats, true /*reno*/, initialCongestionWindowPackets*defaultTCPMSS, maxCongestionWindow)
defaultMaxCongestionWindowPackets := protocol.DefaultMaxCongestionWindow / protocol.DefaultTCPMSS
defaultMaxCongestionWindowPackets := maxCongestionWindow / defaultTCPMSS
for i := 1; i < int(defaultMaxCongestionWindowPackets); i++ {
sender.MaybeExitSlowStart()
sender.OnPacketAcked(protocol.PacketNumber(i), 1350, sender.GetCongestionWindow(), clock.Now())
}
Expect(sender.GetCongestionWindow()).To(Equal(protocol.DefaultMaxCongestionWindow))
Expect(sender.GetCongestionWindow()).To(Equal(maxCongestionWindow))
})
It("limit cwnd increase in congestion avoidance", func() {
// Enable Cubic.
sender = NewCubicSender(&clock, rttStats, false, initialCongestionWindowPackets*protocol.DefaultTCPMSS, MaxCongestionWindow)
sender = newCubicSender(&clock, rttStats, false, initialCongestionWindowPackets*defaultTCPMSS, MaxCongestionWindow)
numSent := SendAvailableSendWindow()
// Make sure we fall out of slow start.
@ -644,6 +644,6 @@ var _ = Describe("Cubic Sender", func() {
// Ack two packets. The CWND should increase by only one packet.
AckNPackets(2)
Expect(sender.GetCongestionWindow()).To(Equal(savedCwnd + protocol.DefaultTCPMSS))
Expect(sender.GetCongestionWindow()).To(Equal(savedCwnd + defaultTCPMSS))
})
})