1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
package quic
import (
"fmt"
"math/rand/v2"
"testing"
"time"
"github.com/quic-go/quic-go/internal/protocol"
"github.com/quic-go/quic-go/internal/utils"
"github.com/quic-go/quic-go/logging"
"github.com/stretchr/testify/require"
)
func TestMTUDiscovererTiming(t *testing.T) {
const rtt = 100 * time.Millisecond
var rttStats utils.RTTStats
rttStats.UpdateRTT(rtt, 0)
d := newMTUDiscoverer(&rttStats, 1000, 2000, nil)
now := time.Now()
require.False(t, d.ShouldSendProbe(now))
d.Start(now)
require.False(t, d.ShouldSendProbe(now))
require.False(t, d.ShouldSendProbe(now.Add(rtt*9/2)))
now = now.Add(5 * rtt)
require.True(t, d.ShouldSendProbe(now))
// only a single outstanding probe packet is permitted
ping, _ := d.GetPing(now)
require.False(t, d.ShouldSendProbe(now))
now = now.Add(5 * rtt)
require.False(t, d.ShouldSendProbe(now))
ping.Handler.OnLost(ping.Frame)
require.True(t, d.ShouldSendProbe(now))
}
func TestMTUDiscovererAckAndLoss(t *testing.T) {
d := newMTUDiscoverer(&utils.RTTStats{}, 1000, 2000, nil)
// we use an RTT of 0 here, so we don't have to advance the timer on every step
now := time.Now()
ping, size := d.GetPing(now)
require.Equal(t, protocol.ByteCount(1500), size)
// the MTU is reduced if the frame is lost
ping.Handler.OnLost(ping.Frame)
require.Equal(t, protocol.ByteCount(1000), d.CurrentSize()) // no change to the MTU yet
require.True(t, d.ShouldSendProbe(now))
ping, size = d.GetPing(now)
require.Equal(t, protocol.ByteCount(1250), size)
ping.Handler.OnAcked(ping.Frame)
require.Equal(t, protocol.ByteCount(1250), d.CurrentSize()) // the MTU is increased
// Even though the 1500 byte MTU probe packet was lost, we try again with a higher MTU.
// This protects against regular (non-MTU-related) packet loss.
require.True(t, d.ShouldSendProbe(now))
ping, size = d.GetPing(now)
require.Greater(t, size, protocol.ByteCount(1500))
ping.Handler.OnAcked(ping.Frame)
require.Equal(t, size, d.CurrentSize())
// We continue probing until the MTU is close to the maximum.
var steps int
oldSize := size
for d.ShouldSendProbe(now) {
ping, size = d.GetPing(now)
require.Greater(t, size, oldSize)
oldSize = size
ping.Handler.OnAcked(ping.Frame)
steps++
require.Less(t, steps, 10)
}
require.Less(t, 2000-maxMTUDiff, size)
}
func TestMTUDiscovererMTUDiscovery(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
testMTUDiscovererMTUDiscovery(t)
})
}
}
func testMTUDiscovererMTUDiscovery(t *testing.T) {
const rtt = 100 * time.Millisecond
const startMTU protocol.ByteCount = 1000
var rttStats utils.RTTStats
rttStats.UpdateRTT(rtt, 0)
maxMTU := protocol.ByteCount(rand.IntN(int(3000-startMTU))) + startMTU + 1
var tracedMTU protocol.ByteCount
var tracerDone bool
d := newMTUDiscoverer(&rttStats, startMTU, maxMTU,
&logging.ConnectionTracer{
UpdatedMTU: func(mtu logging.ByteCount, done bool) {
tracedMTU = mtu
tracerDone = done
},
},
)
now := time.Now()
d.Start(now)
realMTU := protocol.ByteCount(rand.IntN(int(maxMTU-startMTU))) + startMTU
t.Logf("MTU: %d, max: %d", realMTU, maxMTU)
now = now.Add(mtuProbeDelay * rtt)
var probes []protocol.ByteCount
for d.ShouldSendProbe(now) {
require.Less(t, len(probes), 25, fmt.Sprintf("too many iterations: %v", probes))
ping, size := d.GetPing(now)
probes = append(probes, size)
if size <= realMTU {
ping.Handler.OnAcked(ping.Frame)
} else {
ping.Handler.OnLost(ping.Frame)
}
now = now.Add(mtuProbeDelay * rtt)
}
currentMTU := d.CurrentSize()
diff := realMTU - currentMTU
require.GreaterOrEqual(t, diff, protocol.ByteCount(0))
if maxMTU > currentMTU+maxMTU {
require.Equal(t, currentMTU, tracedMTU)
require.True(t, tracerDone)
}
t.Logf("MTU discovered: %d (diff: %d)", currentMTU, diff)
t.Logf("probes sent (%d): %v", len(probes), probes)
require.LessOrEqual(t, diff, maxMTUDiff)
}
func TestMTUDiscovererWithRandomLoss(t *testing.T) {
for i := range 5 {
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) {
testMTUDiscovererWithRandomLoss(t)
})
}
}
func testMTUDiscovererWithRandomLoss(t *testing.T) {
const rtt = 100 * time.Millisecond
const startMTU protocol.ByteCount = 1000
const maxRandomLoss = maxLostMTUProbes - 1
rttStats := &utils.RTTStats{}
rttStats.SetInitialRTT(rtt)
require.Equal(t, rtt, rttStats.SmoothedRTT())
maxMTU := protocol.ByteCount(rand.IntN(int(3000-startMTU))) + startMTU + 1
var tracedMTU protocol.ByteCount
var tracerDone bool
d := newMTUDiscoverer(rttStats, startMTU, maxMTU,
&logging.ConnectionTracer{
UpdatedMTU: func(mtu logging.ByteCount, done bool) {
tracedMTU = mtu
tracerDone = done
},
},
)
d.Start(time.Now())
now := time.Now()
realMTU := protocol.ByteCount(rand.IntN(int(maxMTU-startMTU))) + startMTU
t.Logf("MTU: %d, max: %d", realMTU, maxMTU)
now = now.Add(mtuProbeDelay * rtt)
var probes, randomLosses []protocol.ByteCount
for d.ShouldSendProbe(now) {
require.Less(t, len(probes), 32, fmt.Sprintf("too many iterations: %v", probes))
ping, size := d.GetPing(now)
probes = append(probes, size)
packetFits := size <= realMTU
var acked bool
if packetFits {
randomLoss := rand.IntN(maxLostMTUProbes) == 0 && len(randomLosses) < maxRandomLoss
if randomLoss {
randomLosses = append(randomLosses, size)
} else {
ping.Handler.OnAcked(ping.Frame)
acked = true
}
}
if !acked {
ping.Handler.OnLost(ping.Frame)
}
now = now.Add(mtuProbeDelay * rtt)
}
currentMTU := d.CurrentSize()
diff := realMTU - currentMTU
require.GreaterOrEqual(t, diff, protocol.ByteCount(0))
if maxMTU > currentMTU+maxMTU {
require.Equal(t, currentMTU, tracedMTU)
require.True(t, tracerDone)
}
t.Logf("MTU discovered with random losses %v: %d (diff: %d)", randomLosses, currentMTU, diff)
t.Logf("probes sent (%d): %v", len(probes), probes)
require.LessOrEqual(t, diff, maxMTUDiff)
}
func TestMTUDiscovererReset(t *testing.T) {
t.Run("probe on old path acknowledged", func(t *testing.T) {
testMTUDiscovererReset(t, true)
})
t.Run("probe on old path lost", func(t *testing.T) {
testMTUDiscovererReset(t, false)
})
}
func testMTUDiscovererReset(t *testing.T, ackLastProbe bool) {
const startMTU protocol.ByteCount = 1000
const maxMTU = 1400
const rtt = 100 * time.Millisecond
rttStats := &utils.RTTStats{}
rttStats.SetInitialRTT(rtt)
now := time.Now()
d := newMTUDiscoverer(rttStats, startMTU, maxMTU, nil)
d.Start(now)
ping, _ := d.GetPing(now.Add(5 * rtt))
ping.Handler.OnAcked(ping.Frame)
require.Greater(t, d.CurrentSize(), startMTU)
now = now.Add(5 * rtt)
// send another probe packet, but neither acknowledge nor lose it before resetting
ping, _ = d.GetPing(now.Add(5 * rtt))
now = now.Add(2 * rtt) // advance the timer by an arbitrary amount
const newStartMTU protocol.ByteCount = 900
const newMaxMTU = 1500
d.Reset(now, newStartMTU, newMaxMTU)
require.Equal(t, d.CurrentSize(), newStartMTU)
// Now acknowledge / lose the probe packet.
// This should be ignored, since it's on the old path.
if ackLastProbe {
ping.Handler.OnAcked(ping.Frame)
} else {
ping.Handler.OnLost(ping.Frame)
}
// the MTU should not have changed
require.Equal(t, d.CurrentSize(), newStartMTU)
// the next probe should be sent after 5 RTTs
require.False(t, d.ShouldSendProbe(now.Add(5*rtt).Add(-time.Microsecond)))
require.True(t, d.ShouldSendProbe(now.Add(5*rtt)))
}
|