Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions coalesce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package tls

import (
"bytes"
"io"
"net"
"sync"
"testing"
)

// writeSizeConn records the size of every write to the connection.
type writeSizeConn struct {
net.Conn
mu sync.Mutex
writes []int
}

func (c *writeSizeConn) Write(b []byte) (int, error) {
c.mu.Lock()
c.writes = append(c.writes, len(b))
c.mu.Unlock()
return c.Conn.Write(b)
}

func (c *writeSizeConn) reset() []int {
c.mu.Lock()
defer c.mu.Unlock()
w := c.writes
c.writes = nil
return w
}

// TestWriteCoalescesRecords checks that the records of one large write go to
// the connection in writes of several records, up to coalesceLimit, and that
// the peer reads the same data, with every cipher kind.
func TestWriteCoalescesRecords(t *testing.T) {
configs := map[string]func(*Config){
"TLSv13": func(*Config) {},
"AEAD": func(c *Config) {
c.MaxVersion = VersionTLS12
c.CipherSuites = []uint16{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}
},
"CBC": func(c *Config) {
c.MaxVersion = VersionTLS12
c.CipherSuites = []uint16{TLS_RSA_WITH_AES_256_CBC_SHA}
},
"Stream": func(c *Config) {
c.MaxVersion = VersionTLS12
c.CipherSuites = []uint16{TLS_RSA_WITH_RC4_128_SHA}
},
}
for name, configure := range configs {
for _, dynamic := range []bool{false, true} {
config := testConfig.Clone()
configure(config)
serverConfig := config.Clone()
serverConfig.DynamicRecordSizingDisabled = !dynamic
testWriteCoalescesRecords(t, name, dynamic, config, serverConfig)
}
}
}

func testWriteCoalescesRecords(t *testing.T, name string, dynamic bool, clientConfig, serverConfig *Config) {
clientConn, serverConn := localPipe(t)
counted := &writeSizeConn{Conn: serverConn}
server := Server(counted, serverConfig)
defer server.Close()

plaintext := make([]byte, 1<<20+123)
for i := range plaintext {
plaintext[i] = byte(i * 7)
}
got := make(chan []byte, 1)
go func() {
client := Client(clientConn, clientConfig)
defer client.Close()
data, err := io.ReadAll(io.LimitReader(client, int64(len(plaintext))))
if err != nil {
t.Errorf("%s: client read: %v", name, err)
}
got <- data
}()
if err := server.Handshake(); err != nil {
t.Fatalf("%s: handshake: %v", name, err)
}
counted.reset()
if n, err := server.Write(plaintext); err != nil || n != len(plaintext) {
t.Fatalf("%s: write = %d, %v", name, n, err)
}
writes := counted.reset()
if data := <-got; !bytes.Equal(data, plaintext) {
t.Fatalf("%s: the client read other data (%d bytes)", name, len(data))
}

total := 0
for i, size := range writes {
total += size
if size > coalesceLimit {
t.Errorf("%s: write #%d of %d bytes, over the limit of %d", name, i, size, coalesceLimit)
}
}
fullRecords := len(plaintext)/maxPlaintext + 1
maxWrites := fullRecords/7 + 1 // about eight records per write
if dynamic {
maxWrites += recordSizeBoostThreshold / (8 * 1000) // the small records first
}
if len(writes) > maxWrites {
t.Errorf("%s (dynamic %v): %d writes for %d bytes of records, want at most %d", name, dynamic, len(writes), total, maxWrites)
}
}

// TestWriteSmallRecordsUnchanged checks that a write of one record is still
// one write of that record.
func TestWriteSmallRecordsUnchanged(t *testing.T) {
clientConn, serverConn := localPipe(t)
counted := &writeSizeConn{Conn: serverConn}
server := Server(counted, testConfig.Clone())
defer server.Close()
clientDone := make(chan struct{})
go func() {
defer close(clientDone)
client := Client(clientConn, testConfig.Clone())
defer client.Close()
buf := make([]byte, 100)
for i := 0; i < 3; i++ {
if _, err := io.ReadFull(client, buf[:10]); err != nil {
t.Errorf("client read: %v", err)
return
}
}
}()
if err := server.Handshake(); err != nil {
t.Fatal(err)
}
counted.reset()
for i := 0; i < 3; i++ {
if _, err := server.Write(bytes.Repeat([]byte{'a'}, 10)); err != nil {
t.Fatal(err)
}
if writes := counted.reset(); len(writes) != 1 || writes[0] != recordHeaderLen+10+1+16 {
t.Fatalf("write %d: writes %v, want one record of %d bytes", i, writes, recordHeaderLen+10+1+16)
}
}
<-clientDone // before the connections are closed
}
67 changes: 57 additions & 10 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,17 @@ var outBufPool = sync.Pool{
},
}

// coalesceLimit bounds the records of one write that are sent to the
// connection together: eight full-sized records. A large write goes out in
// writes of up to about 128 KiB instead of one write per record, and the
// network stack is paid per write. Nothing is held back: all of it is written
// before the call returns.
const coalesceLimit = 8 * (recordHeaderLen + maxPlaintext + maxRecordExpansion)

// maxRecordExpansion bounds what encryption adds to a record payload: an
// explicit nonce, a MAC or an AEAD tag, CBC padding, the TLS 1.3 content type.
const maxRecordExpansion = 16 + 48 + 256 + 1

// writeRecordLocked writes a TLS record with the given type and payload to the
// connection and updates the record layer state.
func (c *Conn) writeRecordLocked(typ recordType, data []byte) (int, error) {
Expand Down Expand Up @@ -1048,15 +1059,43 @@ func (c *Conn) writeRecordLocked(typ recordType, data []byte) (int, error) {
outBufPool.Put(outBufPtr)
}()

var n int
// outBuf holds the encrypted records not written yet, which carry queued
// bytes of data.
outBuf = outBuf[:0]
var n, queued int
for len(data) > 0 {
m := len(data)
if maxPayload := c.maxPayloadSizeForWrite(typ); m > maxPayload {
m = maxPayload
}

_, outBuf = sliceForAppend(outBuf[:0], recordHeaderLen)
outBuf[0] = byte(typ)
start := len(outBuf)
need := start + recordHeaderLen + m + maxRecordExpansion
if start > 0 && need > coalesceLimit {
if _, err := c.write(outBuf); err != nil {
return n, err
}
n += queued
outBuf, queued = outBuf[:0], 0
start, need = 0, recordHeaderLen+m+maxRecordExpansion
}
if cap(outBuf) < need {
// Room for this record and, up to the limit, the ones after it,
// so that every record is encrypted in place behind the last.
size := start + len(data) + (len(data)/maxPlaintext+1)*(recordHeaderLen+maxRecordExpansion)
if size > coalesceLimit {
size = coalesceLimit
}
if size < need {
size = need
}
grown := make([]byte, start, size)
copy(grown, outBuf)
outBuf = grown
}

record := outBuf[start : start+recordHeaderLen]
record[0] = byte(typ)
vers := c.vers
if vers == 0 {
// Some TLS servers fail if the record version is
Expand All @@ -1067,21 +1106,29 @@ func (c *Conn) writeRecordLocked(typ recordType, data []byte) (int, error) {
// See RFC 8446, Section 5.1.
vers = VersionTLS12
}
outBuf[1] = byte(vers >> 8)
outBuf[2] = byte(vers)
outBuf[3] = byte(m >> 8)
outBuf[4] = byte(m)
record[1] = byte(vers >> 8)
record[2] = byte(vers)
record[3] = byte(m >> 8)
record[4] = byte(m)

var err error
outBuf, err = c.out.encrypt(outBuf, data[:m], c.config.rand())
record, err = c.out.encrypt(record, data[:m], c.config.rand())
if err != nil {
return n, err
}
if &record[0] == &outBuf[:start+1][start] {
outBuf = outBuf[:start+len(record)] // encrypted in place
} else {
outBuf = append(outBuf[:start], record...)
}
queued += m
data = data[m:]
}
if len(outBuf) > 0 {
if _, err := c.write(outBuf); err != nil {
return n, err
}
n += m
data = data[m:]
n += queued
}

if typ == recordTypeChangeCipherSpec && c.vers != VersionTLS13 {
Expand Down
Loading