|
| 1 | +// These tests and benchmarks isolate provider-side TCP option negotiation from |
| 2 | +// carrier performance, including packet ownership and asymmetric path limits. |
| 3 | +package connect |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/binary" |
| 7 | + "net" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/urnetwork/connect/protocol" |
| 11 | +) |
| 12 | + |
| 13 | +// Builds a return packetizer with the same IPv4 and timestamp overhead as an |
| 14 | +// established provider flow. |
| 15 | +func newTcpOptionPacketizationState(peerMss uint32) *ConnectionState { |
| 16 | + return &ConnectionState{ |
| 17 | + ipVersion: 4, |
| 18 | + sourceIp: net.IPv4(10, 0, 0, 1).To4(), |
| 19 | + sourcePort: 40000, |
| 20 | + destinationIp: net.IPv4(203, 0, 113, 7).To4(), |
| 21 | + destinationPort: 443, |
| 22 | + receiveSeq: 1000, |
| 23 | + sendSeq: 2000, |
| 24 | + windowSize: 1024 * 1024, |
| 25 | + windowScale: 5, |
| 26 | + enableTimestamp: true, |
| 27 | + timestampRecent: 3000, |
| 28 | + peerMss: peerMss, |
| 29 | + timestampValueForTest: func() uint32 { |
| 30 | + return 4000 |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// A smaller source MTU produces a smaller advertised MSS. The provider must |
| 36 | +// subtract its timestamp option and keep every return packet inside that MTU. |
| 37 | +func TestTcpSequenceHonorsPeerMssInReturnPacketization(t *testing.T) { |
| 38 | + settings := DefaultTcpBufferSettingsWithBufferSize(8) |
| 39 | + sequence := newTcpSequenceWithTransferKey( |
| 40 | + t.Context(), |
| 41 | + func( |
| 42 | + source TransferPath, |
| 43 | + transferKey TransferKey, |
| 44 | + provideMode protocol.ProvideMode, |
| 45 | + recoveryMode receiveRecoveryMode, |
| 46 | + ipPath *IpPath, |
| 47 | + packet []byte, |
| 48 | + ) { |
| 49 | + }, |
| 50 | + TransferPath{}, |
| 51 | + TransferKey{}, |
| 52 | + protocol.ProvideMode_Public, |
| 53 | + 4, |
| 54 | + net.IPv4(10, 0, 0, 1).To4(), |
| 55 | + 40000, |
| 56 | + net.IPv4(203, 0, 113, 7).To4(), |
| 57 | + 443, |
| 58 | + 0, |
| 59 | + settings, |
| 60 | + ) |
| 61 | + defer sequence.Close() |
| 62 | + sequence.timestampValueForTest = func() uint32 { return 4000 } |
| 63 | + |
| 64 | + const receiverMtu = 640 |
| 65 | + const peerMss = receiverMtu - Ipv4HeaderSizeWithoutExtensions - TcpHeaderSizeWithoutExtensions |
| 66 | + synOptions := []byte{ |
| 67 | + 2, 4, 0, 0, |
| 68 | + 1, 1, 8, 10, |
| 69 | + 0, 0, 0, 1, |
| 70 | + 0, 0, 0, 0, |
| 71 | + } |
| 72 | + binary.BigEndian.PutUint16(synOptions[2:4], peerMss) |
| 73 | + sequence.mutex.Lock() |
| 74 | + sequence.initializeSynWithLock(&parsedTcp{ |
| 75 | + seq: 1000, |
| 76 | + windowSize: 65535, |
| 77 | + options: synOptions, |
| 78 | + }) |
| 79 | + sequence.mutex.Unlock() |
| 80 | + if sequence.peerMss != peerMss { |
| 81 | + t.Fatalf("peer MSS=%d, want %d", sequence.peerMss, peerMss) |
| 82 | + } |
| 83 | + |
| 84 | + payload := make([]byte, 8*1024) |
| 85 | + packets, err := sequence.DataPackets(payload, len(payload), settings.Mtu) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("packetize provider return: %v", err) |
| 88 | + } |
| 89 | + payloadByteCount := 0 |
| 90 | + for packetIndex, packet := range packets { |
| 91 | + if receiverMtu < len(packet) { |
| 92 | + t.Errorf( |
| 93 | + "return packet %d is %d bytes, exceeds receiver MTU %d", |
| 94 | + packetIndex, |
| 95 | + len(packet), |
| 96 | + receiverMtu, |
| 97 | + ) |
| 98 | + } |
| 99 | + _, _, _, transport, ok := parseIpv4(packet) |
| 100 | + if !ok { |
| 101 | + t.Fatalf("return packet %d is not IPv4", packetIndex) |
| 102 | + } |
| 103 | + tcp := &parsedTcp{} |
| 104 | + if !parseTcpPacket(sequence.destinationIp, sequence.sourceIp, transport, tcp) { |
| 105 | + t.Fatalf("return packet %d is not TCP", packetIndex) |
| 106 | + } |
| 107 | + payloadByteCount += len(tcp.payload) |
| 108 | + MessagePoolReturn(packet) |
| 109 | + } |
| 110 | + if payloadByteCount != len(payload) { |
| 111 | + t.Fatalf("packetized payload=%d bytes, want %d", payloadByteCount, len(payload)) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// A peer MSS equal to the local fixed-header limit must not reduce the normal |
| 116 | +// packet size beyond the timestamp bytes the sender already has to include. |
| 117 | +func TestTcpSequencePeerMssPreservesEqualMtuPacketization(t *testing.T) { |
| 118 | + const localMtu = 1440 |
| 119 | + const peerMss = localMtu - Ipv4HeaderSizeWithoutExtensions - TcpHeaderSizeWithoutExtensions |
| 120 | + state := newTcpOptionPacketizationState(peerMss) |
| 121 | + packetPayloadByteCount := peerMss - tcpTimestampOptionByteCount |
| 122 | + payload := make([]byte, packetPayloadByteCount) |
| 123 | + packets, err := state.DataPackets(payload, len(payload), localMtu) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("packetize equal-MTU return: %v", err) |
| 126 | + } |
| 127 | + if len(packets) != 1 || len(packets[0]) != localMtu { |
| 128 | + t.Fatalf( |
| 129 | + "equal-MTU packets=(%d, %d bytes), want (1, %d bytes)", |
| 130 | + len(packets), |
| 131 | + len(packets[0]), |
| 132 | + localMtu, |
| 133 | + ) |
| 134 | + } |
| 135 | + MessagePoolReturn(packets[0]) |
| 136 | +} |
| 137 | + |
| 138 | +// Measures provider packetization and counts only packets a receiver with the |
| 139 | +// advertised MTU can accept. |
| 140 | +func benchmarkTcpReturnPacketization(b *testing.B, peerMss uint32, receiverMtu int) { |
| 141 | + const providerMtu = 1440 |
| 142 | + const payloadByteCount = 64 * 1024 |
| 143 | + state := newTcpOptionPacketizationState(peerMss) |
| 144 | + payload := make([]byte, payloadByteCount) |
| 145 | + acceptedPayloadByteCount := 0 |
| 146 | + packetCount := 0 |
| 147 | + droppedPacketCount := 0 |
| 148 | + |
| 149 | + b.ResetTimer() |
| 150 | + for range b.N { |
| 151 | + packets, err := state.DataPackets(payload, len(payload), providerMtu) |
| 152 | + if err != nil { |
| 153 | + b.Fatal(err) |
| 154 | + } |
| 155 | + for _, packet := range packets { |
| 156 | + packetCount += 1 |
| 157 | + if len(packet) <= receiverMtu { |
| 158 | + _, _, _, transport, ok := parseIpv4(packet) |
| 159 | + if !ok { |
| 160 | + b.Fatal("generated packet is not IPv4") |
| 161 | + } |
| 162 | + tcp := &parsedTcp{} |
| 163 | + if !parseTcpPacket(state.destinationIp, state.sourceIp, transport, tcp) { |
| 164 | + b.Fatal("generated packet is not TCP") |
| 165 | + } |
| 166 | + acceptedPayloadByteCount += len(tcp.payload) |
| 167 | + } else { |
| 168 | + droppedPacketCount += 1 |
| 169 | + } |
| 170 | + MessagePoolReturn(packet) |
| 171 | + } |
| 172 | + } |
| 173 | + b.StopTimer() |
| 174 | + if b.N == 0 { |
| 175 | + return |
| 176 | + } |
| 177 | + b.SetBytes(int64(acceptedPayloadByteCount / b.N)) |
| 178 | + b.ReportMetric(float64(packetCount)/float64(b.N), "packets/op") |
| 179 | + b.ReportMetric(float64(droppedPacketCount)/float64(b.N), "oversized-drops/op") |
| 180 | +} |
| 181 | + |
| 182 | +// Historical behavior ignores a smaller peer MSS. |
| 183 | +func BenchmarkTcpReturnPacketizationAsymmetricLocalMtu(b *testing.B) { |
| 184 | + benchmarkTcpReturnPacketization(b, 0, 640) |
| 185 | +} |
| 186 | + |
| 187 | +// The candidate behavior honors a smaller peer MSS. |
| 188 | +func BenchmarkTcpReturnPacketizationAsymmetricPeerMss(b *testing.B) { |
| 189 | + benchmarkTcpReturnPacketization(b, 600, 640) |
| 190 | +} |
| 191 | + |
| 192 | +// Historical behavior on the normal equal-MTU path. |
| 193 | +func BenchmarkTcpReturnPacketizationEqualLocalMtu(b *testing.B) { |
| 194 | + benchmarkTcpReturnPacketization(b, 0, 1440) |
| 195 | +} |
| 196 | + |
| 197 | +// The candidate behavior on the normal equal-MTU path. |
| 198 | +func BenchmarkTcpReturnPacketizationEqualPeerMss(b *testing.B) { |
| 199 | + benchmarkTcpReturnPacketization(b, 1400, 1440) |
| 200 | +} |
0 commit comments