Source file src/vendor/golang.org/x/net/quic/conn_loss.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package quic
     6  
     7  import "fmt"
     8  
     9  // handleAckOrLoss deals with the final fate of a packet we sent:
    10  // Either the peer acknowledges it, or we declare it lost.
    11  //
    12  // In order to handle packet loss, we must retain any information sent to the peer
    13  // until the peer has acknowledged it.
    14  //
    15  // When information is acknowledged, we can discard it.
    16  //
    17  // When information is lost, we mark it for retransmission.
    18  // See RFC 9000, Section 13.3 for a complete list of information which is retransmitted on loss.
    19  // https://www.rfc-editor.org/rfc/rfc9000#section-13.3
    20  func (c *Conn) handleAckOrLoss(space numberSpace, sent *sentPacket, fate packetFate) {
    21  	if fate == packetLost && c.logEnabled(QLogLevelPacket) {
    22  		c.logPacketLost(space, sent)
    23  	}
    24  
    25  	// The list of frames in a sent packet is marshaled into a buffer in the sentPacket
    26  	// by the packetWriter. Unmarshal that buffer here. This code must be kept in sync with
    27  	// packetWriter.append*.
    28  	//
    29  	// A sent packet meets its fate (acked or lost) only once, so it's okay to consume
    30  	// the sentPacket's buffer here.
    31  	for !sent.done() {
    32  		switch f := sent.next(); f {
    33  		default:
    34  			panic(fmt.Sprintf("BUG: unhandled acked/lost frame type %x", f))
    35  		case frameTypeAck, frameTypeAckECN:
    36  			// Unlike most information, loss of an ACK frame does not trigger
    37  			// retransmission. ACKs are sent in response to ack-eliciting packets,
    38  			// and always contain the latest information available.
    39  			//
    40  			// Acknowledgement of an ACK frame may allow us to discard information
    41  			// about older packets.
    42  			largest := packetNumber(sent.nextInt())
    43  			if fate == packetAcked {
    44  				c.acks[space].handleAck(largest)
    45  			}
    46  		case frameTypeCrypto:
    47  			start, end := sent.nextRange()
    48  			c.crypto[space].ackOrLoss(start, end, fate)
    49  		case frameTypeMaxData:
    50  			c.ackOrLossMaxData(sent.num, fate)
    51  		case frameTypeResetStream,
    52  			frameTypeStopSending,
    53  			frameTypeMaxStreamData,
    54  			frameTypeStreamDataBlocked:
    55  			id := streamID(sent.nextInt())
    56  			s := c.streamForID(id)
    57  			if s == nil {
    58  				continue
    59  			}
    60  			s.ackOrLoss(sent.num, f, fate)
    61  		case frameTypeStreamBase,
    62  			frameTypeStreamBase | streamFinBit:
    63  			id := streamID(sent.nextInt())
    64  			start, end := sent.nextRange()
    65  			s := c.streamForID(id)
    66  			if s == nil {
    67  				continue
    68  			}
    69  			fin := f&streamFinBit != 0
    70  			s.ackOrLossData(sent.num, start, end, fin, fate)
    71  		case frameTypeMaxStreamsBidi:
    72  			c.streams.remoteLimit[bidiStream].sendMax.ackLatestOrLoss(sent.num, fate)
    73  		case frameTypeMaxStreamsUni:
    74  			c.streams.remoteLimit[uniStream].sendMax.ackLatestOrLoss(sent.num, fate)
    75  		case frameTypeNewConnectionID:
    76  			seq := int64(sent.nextInt())
    77  			c.connIDState.ackOrLossNewConnectionID(sent.num, seq, fate)
    78  		case frameTypeRetireConnectionID:
    79  			seq := int64(sent.nextInt())
    80  			c.connIDState.ackOrLossRetireConnectionID(sent.num, seq, fate)
    81  		case frameTypeHandshakeDone:
    82  			c.handshakeConfirmed.ackOrLoss(sent.num, fate)
    83  		}
    84  	}
    85  }
    86  

View as plain text