Implement UDP Preflight

Add a preflight method that validates a set of provided write options against
the current stack state, possibly binding the endpoint if a `To` address is
provided as a write option. In Fuchsia, this method will be used along with a
cache to amortize the cost of validating send-path arguments across multiple
sends.

Fuchsia Bug: https://fxbug.dev/99501

PiperOrigin-RevId: 456777866
This commit is contained in:
Nick Brown
2022-06-23 08:39:06 -07:00
committed by gVisor bot
parent 0ae98218be
commit a6d53a75ca
4 changed files with 486 additions and 290 deletions
+10
View File
@@ -687,6 +687,16 @@ type Endpoint interface {
SocketOptions() *SocketOptions
}
// EndpointWithPreflight is the interface implemented by endpoints that need
// to expose the `Preflight` method for preparing the endpoint prior to
// calling `Write`.
type EndpointWithPreflight interface {
// Prepares the endpoint for writes using the provided WriteOptions,
// returning an error if the options were incompatible with the endpoint's
// current state.
Preflight(WriteOptions) Error
}
// LinkPacketInfo holds Link layer information for a received packet.
//
// +stateify savable
+1
View File
@@ -59,6 +59,7 @@ go_test(
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
"//pkg/tcpip/testutil",
"//pkg/tcpip/transport",
"//pkg/tcpip/transport/icmp",
"//pkg/tcpip/transport/testing/context",
"//pkg/waiter",
+12
View File
@@ -15,6 +15,7 @@
package udp
import (
"bytes"
"fmt"
"io"
"math"
@@ -344,6 +345,17 @@ func (e *endpoint) prepareForWriteInner(to *tcpip.FullAddress) (retry bool, err
return true, nil
}
var _ tcpip.EndpointWithPreflight = (*endpoint)(nil)
// Validates the passed WriteOptions and prepares the endpoint for writes
// using those options. If the endpoint is unbound and the `To` address
// is specified, binds the endpoint to that address.
func (e *endpoint) Preflight(opts tcpip.WriteOptions) tcpip.Error {
var r bytes.Reader
_, err := e.prepareForWrite(&r, opts)
return err
}
// Write writes data to the endpoint's peer. This method does not block
// if the data cannot be written.
func (e *endpoint) Write(p tcpip.Payloader, opts tcpip.WriteOptions) (int64, tcpip.Error) {
File diff suppressed because it is too large Load Diff