github.com/danielpfeifer02/quic-go-prio-packs@v0.41.0-28/internal/wire/ack_frame.go (about) 1 package wire 2 3 import ( 4 "bytes" 5 "errors" 6 "sort" 7 "time" 8 9 "github.com/danielpfeifer02/quic-go-prio-packs/internal/protocol" 10 "github.com/danielpfeifer02/quic-go-prio-packs/internal/utils" 11 "github.com/danielpfeifer02/quic-go-prio-packs/packet_setting" 12 "github.com/danielpfeifer02/quic-go-prio-packs/quicvarint" 13 ) 14 15 var errInvalidAckRanges = errors.New("AckFrame: ACK frame contains invalid ACK ranges") 16 17 // An AckFrame is an ACK frame 18 type AckFrame struct { 19 AckRanges []AckRange // has to be ordered. The highest ACK range goes first, the lowest ACK range goes last 20 DelayTime time.Duration 21 22 ECT0, ECT1, ECNCE uint64 23 } 24 25 // parseAckFrame reads an ACK frame 26 func parseAckFrame(frame *AckFrame, r *bytes.Reader, typ uint64, ackDelayExponent uint8, _ protocol.Version) error { 27 ecn := typ == ackECNFrameType 28 29 la, err := quicvarint.Read(r) 30 if err != nil { 31 return err 32 } 33 largestAcked := protocol.PacketNumber(la) 34 delay, err := quicvarint.Read(r) 35 if err != nil { 36 return err 37 } 38 39 delayTime := time.Duration(delay*1<<ackDelayExponent) * time.Microsecond 40 if delayTime < 0 { 41 // If the delay time overflows, set it to the maximum encode-able value. 42 delayTime = utils.InfDuration 43 } 44 frame.DelayTime = delayTime 45 46 numBlocks, err := quicvarint.Read(r) 47 if err != nil { 48 return err 49 } 50 51 // read the first ACK range 52 ab, err := quicvarint.Read(r) 53 if err != nil { 54 return err 55 } 56 ackBlock := protocol.PacketNumber(ab) 57 if ackBlock > largestAcked { 58 return errors.New("invalid first ACK range") 59 } 60 smallest := largestAcked - ackBlock 61 frame.AckRanges = append(frame.AckRanges, AckRange{Smallest: smallest, Largest: largestAcked}) 62 63 // read all the other ACK ranges 64 for i := uint64(0); i < numBlocks; i++ { 65 g, err := quicvarint.Read(r) 66 if err != nil { 67 return err 68 } 69 gap := protocol.PacketNumber(g) 70 if smallest < gap+2 { 71 return errInvalidAckRanges 72 } 73 largest := smallest - gap - 2 74 75 ab, err := quicvarint.Read(r) 76 if err != nil { 77 return err 78 } 79 ackBlock := protocol.PacketNumber(ab) 80 81 if ackBlock > largest { 82 return errInvalidAckRanges 83 } 84 smallest = largest - ackBlock 85 frame.AckRanges = append(frame.AckRanges, AckRange{Smallest: smallest, Largest: largest}) 86 } 87 88 if !frame.validateAckRanges() { 89 return errInvalidAckRanges 90 } 91 92 if ecn { 93 ect0, err := quicvarint.Read(r) 94 if err != nil { 95 return err 96 } 97 frame.ECT0 = ect0 98 ect1, err := quicvarint.Read(r) 99 if err != nil { 100 return err 101 } 102 frame.ECT1 = ect1 103 ecnce, err := quicvarint.Read(r) 104 if err != nil { 105 return err 106 } 107 frame.ECNCE = ecnce 108 } 109 110 return nil 111 } 112 113 // Append appends an ACK frame. 114 func (f *AckFrame) Append(b []byte, _ protocol.Version) ([]byte, error) { 115 hasECN := f.ECT0 > 0 || f.ECT1 > 0 || f.ECNCE > 0 116 if hasECN { 117 b = append(b, ackECNFrameType) 118 } else { 119 b = append(b, ackFrameType) 120 } 121 b = quicvarint.Append(b, uint64(f.LargestAcked())) 122 b = quicvarint.Append(b, encodeAckDelay(f.DelayTime)) 123 124 numRanges := f.numEncodableAckRanges() 125 b = quicvarint.Append(b, uint64(numRanges-1)) 126 127 // write the first range 128 _, firstRange := f.encodeAckRange(0) 129 b = quicvarint.Append(b, firstRange) 130 131 // write all the other range 132 for i := 1; i < numRanges; i++ { 133 gap, len := f.encodeAckRange(i) 134 b = quicvarint.Append(b, gap) 135 b = quicvarint.Append(b, len) 136 } 137 138 if hasECN { 139 b = quicvarint.Append(b, f.ECT0) 140 b = quicvarint.Append(b, f.ECT1) 141 b = quicvarint.Append(b, f.ECNCE) 142 } 143 return b, nil 144 } 145 146 // Length of a written frame 147 func (f *AckFrame) Length(_ protocol.Version) protocol.ByteCount { 148 largestAcked := f.AckRanges[0].Largest 149 numRanges := f.numEncodableAckRanges() 150 151 length := 1 + quicvarint.Len(uint64(largestAcked)) + quicvarint.Len(encodeAckDelay(f.DelayTime)) 152 153 length += quicvarint.Len(uint64(numRanges - 1)) 154 lowestInFirstRange := f.AckRanges[0].Smallest 155 length += quicvarint.Len(uint64(largestAcked - lowestInFirstRange)) 156 157 for i := 1; i < numRanges; i++ { 158 gap, len := f.encodeAckRange(i) 159 length += quicvarint.Len(gap) 160 length += quicvarint.Len(len) 161 } 162 if f.ECT0 > 0 || f.ECT1 > 0 || f.ECNCE > 0 { 163 length += quicvarint.Len(f.ECT0) 164 length += quicvarint.Len(f.ECT1) 165 length += quicvarint.Len(f.ECNCE) 166 } 167 return length 168 } 169 170 // gets the number of ACK ranges that can be encoded 171 // such that the resulting frame is smaller than the maximum ACK frame size 172 func (f *AckFrame) numEncodableAckRanges() int { 173 length := 1 + quicvarint.Len(uint64(f.LargestAcked())) + quicvarint.Len(encodeAckDelay(f.DelayTime)) 174 length += 2 // assume that the number of ranges will consume 2 bytes 175 for i := 1; i < len(f.AckRanges); i++ { 176 gap, len := f.encodeAckRange(i) 177 rangeLen := quicvarint.Len(gap) + quicvarint.Len(len) 178 if length+rangeLen > protocol.MaxAckFrameSize { 179 // Writing range i would exceed the MaxAckFrameSize. 180 // So encode one range less than that. 181 return i - 1 182 } 183 length += rangeLen 184 } 185 return len(f.AckRanges) 186 } 187 188 func (f *AckFrame) encodeAckRange(i int) (uint64 /* gap */, uint64 /* length */) { 189 if i == 0 { 190 return 0, uint64(f.AckRanges[0].Largest - f.AckRanges[0].Smallest) 191 } 192 return uint64(f.AckRanges[i-1].Smallest - f.AckRanges[i].Largest - 2), 193 uint64(f.AckRanges[i].Largest - f.AckRanges[i].Smallest) 194 } 195 196 // HasMissingRanges returns if this frame reports any missing packets 197 func (f *AckFrame) HasMissingRanges() bool { 198 return len(f.AckRanges) > 1 199 } 200 201 func (f *AckFrame) validateAckRanges() bool { 202 if len(f.AckRanges) == 0 { 203 return false 204 } 205 206 // check the validity of every single ACK range 207 for _, ackRange := range f.AckRanges { 208 if ackRange.Smallest > ackRange.Largest { 209 return false 210 } 211 } 212 213 // check the consistency for ACK with multiple NACK ranges 214 for i, ackRange := range f.AckRanges { 215 if i == 0 { 216 continue 217 } 218 lastAckRange := f.AckRanges[i-1] 219 if lastAckRange.Smallest <= ackRange.Smallest { 220 return false 221 } 222 if lastAckRange.Smallest <= ackRange.Largest+1 { 223 return false 224 } 225 } 226 227 return true 228 } 229 230 // LargestAcked is the largest acked packet number 231 func (f *AckFrame) LargestAcked() protocol.PacketNumber { 232 return f.AckRanges[0].Largest 233 } 234 235 // LowestAcked is the lowest acked packet number 236 func (f *AckFrame) LowestAcked() protocol.PacketNumber { 237 return f.AckRanges[len(f.AckRanges)-1].Smallest 238 } 239 240 // AcksPacket determines if this ACK frame acks a certain packet number 241 func (f *AckFrame) AcksPacket(p protocol.PacketNumber) bool { 242 243 if p < f.LowestAcked() || p > f.LargestAcked() { 244 return false 245 } 246 247 i := sort.Search(len(f.AckRanges), func(i int) bool { 248 return p >= f.AckRanges[i].Smallest 249 }) 250 // i will always be < len(f.AckRanges), since we checked above that p is not bigger than the largest acked 251 return p <= f.AckRanges[i].Largest 252 } 253 254 func (f *AckFrame) Reset() { 255 f.DelayTime = 0 256 f.ECT0 = 0 257 f.ECT1 = 0 258 f.ECNCE = 0 259 for _, r := range f.AckRanges { 260 r.Largest = 0 261 r.Smallest = 0 262 } 263 f.AckRanges = f.AckRanges[:0] 264 } 265 266 func encodeAckDelay(delay time.Duration) uint64 { 267 return uint64(delay.Nanoseconds() / (1000 * (1 << protocol.AckDelayExponent))) 268 } 269 270 // BPF_MAP_TAG 271 func (f *AckFrame) UpdateAckRanges(conn packet_setting.QuicConnection) { // TODONOW: fix this 272 273 // This rules out that the Acks are changed at server or client side 274 // in the example 275 if packet_setting.AckTranslationBPFHandler == nil { 276 return 277 } 278 if conn.RemoteAddr().String() == packet_setting.SERVER_ADDR { 279 return 280 } 281 282 removable_indices := make([]int, 0) 283 284 // fmt.Println() 285 286 for i := 0; i < len(f.AckRanges); i++ { 287 288 // fmt.Println("Range ", i, " Smallest: ", f.AckRanges[i].Smallest, " Largest: ", f.AckRanges[i].Largest) 289 290 smallest := f.AckRanges[i].Smallest 291 largest := f.AckRanges[i].Largest 292 293 var new_smallest, new_largest int64 294 var err error 295 for j := 0; j < (int(largest) - int(smallest) + 1); j++ { 296 // fmt.Println("Trying to translate: ", smallest) // TODO: output seems wrong 297 new_smallest, err = packet_setting.AckTranslationBPFHandler(int64(smallest), conn) 298 if err == nil { 299 break 300 } 301 smallest++ 302 } 303 if err != nil { 304 // fmt.Println("Whole range empty") 305 removable_indices = append(removable_indices, i) 306 continue 307 } 308 f.AckRanges[i].Smallest = protocol.PacketNumber(new_smallest) 309 310 for j := 0; j < (int(largest) - int(smallest) + 1); j++ { 311 new_largest, err = packet_setting.AckTranslationBPFHandler(int64(largest), conn) 312 if err == nil { 313 break 314 } 315 largest-- 316 } 317 if err == nil { 318 f.AckRanges[i].Largest = protocol.PacketNumber(new_largest) 319 } 320 321 } 322 323 for i := len(removable_indices) - 1; i >= 0; i-- { 324 f.AckRanges = append(f.AckRanges[:removable_indices[i]], f.AckRanges[removable_indices[i]+1:]...) 325 } 326 327 }