change PTO to be per packet number space

This commit is contained in:
Marten Seemann 2019-11-12 11:26:18 +07:00
parent fbbe225719
commit 9c3b553e47
8 changed files with 197 additions and 110 deletions

View file

@ -25,7 +25,9 @@ type packetNumberSpace struct {
history *sentPacketHistory
pns *packetNumberGenerator
lossTime time.Time
lossTime time.Time
lastSentAckElicitingPacketTime time.Time
largestAcked protocol.PacketNumber
largestSent protocol.PacketNumber
}
@ -40,9 +42,6 @@ func newPacketNumberSpace(initialPN protocol.PacketNumber) *packetNumberSpace {
}
type sentPacketHandler struct {
lastSentAckElicitingPacketTime time.Time // only applies to the application-data packet number space
lastSentCryptoPacketTime time.Time
nextSendTime time.Time
initialPackets *packetNumberSpace
@ -62,6 +61,7 @@ type sentPacketHandler struct {
// The number of times a PTO has been sent without receiving an ack.
ptoCount uint32
ptoMode SendMode
// The number of PTO probe packets that should be sent.
// Only applies to the application-data packet number space.
numProbesToSend int
@ -153,10 +153,7 @@ func (h *sentPacketHandler) sentPacketImpl(packet *Packet) bool /* is ack-elicit
isAckEliciting := len(packet.Frames) > 0
if isAckEliciting {
if packet.EncryptionLevel != protocol.Encryption1RTT {
h.lastSentCryptoPacketTime = packet.SendTime
}
h.lastSentAckElicitingPacketTime = packet.SendTime
pnSpace.lastSentAckElicitingPacketTime = packet.SendTime
packet.includedInBytesInFlight = true
h.bytesInFlight += packet.Length
if h.numProbesToSend > 0 {
@ -281,7 +278,7 @@ func (h *sentPacketHandler) determineNewlyAckedPackets(
return ackedPackets, err
}
func (h *sentPacketHandler) getEarliestLossTime() (time.Time, protocol.EncryptionLevel) {
func (h *sentPacketHandler) getEarliestLossTimeAndSpace() (time.Time, protocol.EncryptionLevel) {
var encLevel protocol.EncryptionLevel
var lossTime time.Time
@ -300,6 +297,26 @@ func (h *sentPacketHandler) getEarliestLossTime() (time.Time, protocol.Encryptio
return lossTime, encLevel
}
// same logic as getEarliestLossTimeAndSpace, but for lastSentAckElicitingPacketTime instead of lossTime
func (h *sentPacketHandler) getEarliestSentTimeAndSpace() (time.Time, protocol.EncryptionLevel) {
var encLevel protocol.EncryptionLevel
var sentTime time.Time
if h.initialPackets != nil {
sentTime = h.initialPackets.lastSentAckElicitingPacketTime
encLevel = protocol.EncryptionInitial
}
if h.handshakePackets != nil && (sentTime.IsZero() || (!h.handshakePackets.lastSentAckElicitingPacketTime.IsZero() && h.handshakePackets.lastSentAckElicitingPacketTime.Before(sentTime))) {
sentTime = h.handshakePackets.lastSentAckElicitingPacketTime
encLevel = protocol.EncryptionHandshake
}
if sentTime.IsZero() || (!h.oneRTTPackets.lastSentAckElicitingPacketTime.IsZero() && h.oneRTTPackets.lastSentAckElicitingPacketTime.Before(sentTime)) {
sentTime = h.oneRTTPackets.lastSentAckElicitingPacketTime
encLevel = protocol.Encryption1RTT
}
return sentTime, encLevel
}
func (h *sentPacketHandler) hasOutstandingCryptoPackets() bool {
var hasInitial, hasHandshake bool
if h.initialPackets != nil {
@ -316,7 +333,7 @@ func (h *sentPacketHandler) hasOutstandingPackets() bool {
}
func (h *sentPacketHandler) setLossDetectionTimer() {
if lossTime, _ := h.getEarliestLossTime(); !lossTime.IsZero() {
if lossTime, _ := h.getEarliestLossTimeAndSpace(); !lossTime.IsZero() {
// Early retransmit timer or time loss detection.
h.alarm = lossTime
}
@ -329,7 +346,8 @@ func (h *sentPacketHandler) setLossDetectionTimer() {
}
// PTO alarm
h.alarm = h.lastSentAckElicitingPacketTime.Add(h.rttStats.PTO(true) << h.ptoCount)
sentTime, encLevel := h.getEarliestSentTimeAndSpace()
h.alarm = sentTime.Add(h.rttStats.PTO(encLevel == protocol.Encryption1RTT) << h.ptoCount)
}
func (h *sentPacketHandler) detectLostPackets(
@ -415,10 +433,10 @@ func (h *sentPacketHandler) OnLossDetectionTimeout() error {
}
func (h *sentPacketHandler) onVerifiedLossDetectionTimeout() error {
lossTime, encLevel := h.getEarliestLossTime()
if !lossTime.IsZero() {
earliestLossTime, encLevel := h.getEarliestLossTimeAndSpace()
if !earliestLossTime.IsZero() {
if h.logger.Debug() {
h.logger.Debugf("Loss detection alarm fired in loss timer mode. Loss time: %s", lossTime)
h.logger.Debugf("Loss detection alarm fired in loss timer mode. Loss time: %s", earliestLossTime)
}
// Early retransmit or time loss detection
return h.detectLostPackets(time.Now(), encLevel, h.bytesInFlight)
@ -426,10 +444,21 @@ func (h *sentPacketHandler) onVerifiedLossDetectionTimeout() error {
// PTO
if h.logger.Debug() {
h.logger.Debugf("Loss detection alarm fired in PTO mode. PTO count: %d", h.ptoCount)
h.logger.Debugf("Loss detection alarm for %s fired in PTO mode. PTO count: %d", encLevel, h.ptoCount)
}
_, encLevel = h.getEarliestSentTimeAndSpace()
h.ptoCount++
h.numProbesToSend += 2
switch encLevel {
case protocol.EncryptionInitial:
h.ptoMode = SendPTOInitial
case protocol.EncryptionHandshake:
h.ptoMode = SendPTOHandshake
case protocol.Encryption1RTT:
h.ptoMode = SendPTOAppData
default:
return fmt.Errorf("TPO timer in unexpected encryption level: %s", encLevel)
}
return nil
}
@ -492,7 +521,7 @@ func (h *sentPacketHandler) SendMode() SendMode {
return SendNone
}
if h.numProbesToSend > 0 {
return SendPTO
return h.ptoMode
}
// Only send ACKs if we're congestion limited.
if !h.congestion.CanSend(h.bytesInFlight) {
@ -526,17 +555,9 @@ func (h *sentPacketHandler) ShouldSendNumPackets() int {
return int(math.Ceil(float64(protocol.MinPacingDelay) / float64(delay)))
}
func (h *sentPacketHandler) QueueProbePacket() bool {
var p *Packet
if h.initialPackets != nil {
p = h.initialPackets.history.FirstOutstanding()
}
if p == nil && h.handshakePackets != nil {
p = h.handshakePackets.history.FirstOutstanding()
}
if p == nil {
p = h.oneRTTPackets.history.FirstOutstanding()
}
func (h *sentPacketHandler) QueueProbePacket(encLevel protocol.EncryptionLevel) bool {
pnSpace := h.getPacketNumberSpace(encLevel)
p := pnSpace.history.FirstOutstanding()
if p == nil {
return false
}
@ -546,8 +567,8 @@ func (h *sentPacketHandler) QueueProbePacket() bool {
if p.includedInBytesInFlight {
h.bytesInFlight -= p.Length
}
if err := h.getPacketNumberSpace(p.EncryptionLevel).history.Remove(p.PacketNumber); err != nil {
// should never happen. We just got this packet from the history a lines above.
if err := pnSpace.history.Remove(p.PacketNumber); err != nil {
// should never happen. We just got this packet from the history.
panic(err)
}
return true