Adds a WriteRawPacket method to the InjectableLinkEndpoint interface.

Also exposes ipv4.MaxTotalSize since it is a generally useful constant.

PiperOrigin-RevId: 235799755
Change-Id: I1fa8d5294bf355acf5527cfdf274b3687d3c8b13
This commit is contained in:
Googler
2019-02-26 14:58:37 -08:00
committed by Shentubot
parent a2b794b30d
commit 12d9cf6fab
5 changed files with 41 additions and 5 deletions
+5
View File
@@ -288,6 +288,11 @@ func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload b
return rawfile.NonBlockingWrite2(e.fd, hdr.View(), payload.ToView())
}
// WriteRawPacket writes a raw packet directly to the file descriptor.
func (e *endpoint) WriteRawPacket(dest tcpip.Address, packet []byte) *tcpip.Error {
return rawfile.NonBlockingWrite(e.fd, packet)
}
func (e *endpoint) capViews(k, n int, buffers []int) int {
c := 0
for i, s := range buffers {
+11 -1
View File
@@ -94,7 +94,17 @@ func (m *InjectableEndpoint) WritePacket(r *stack.Route, hdr buffer.Prependable,
return tcpip.ErrNoRoute
}
// NewInjectableEndpoint creates a new multi-fd-based injectable endpoint.
// WriteRawPacket writes outbound packets to the appropriate
// LinkInjectableEndpoint based on the dest address.
func (m *InjectableEndpoint) WriteRawPacket(dest tcpip.Address, packet []byte) *tcpip.Error {
endpoint, ok := m.routes[dest]
if !ok {
return tcpip.ErrNoRoute
}
return endpoint.WriteRawPacket(dest, packet)
}
// NewInjectableEndpoint creates a new multi-endpoint injectable endpoint.
func NewInjectableEndpoint(routes map[tcpip.Address]stack.InjectableLinkEndpoint, mtu uint32) (tcpip.LinkEndpointID, *InjectableEndpoint) {
e := &InjectableEndpoint{
routes: routes,
+16
View File
@@ -28,8 +28,24 @@ import (
"gvisor.googlesource.com/gvisor/pkg/tcpip/stack"
)
func TestInjectableEndpointRawDispatch(t *testing.T) {
endpoint, sock, dstIP := makeTestInjectableEndpoint(t)
endpoint.WriteRawPacket(dstIP, []byte{0xFA})
buf := make([]byte, ipv4.MaxTotalSize)
bytesRead, err := sock.Read(buf)
if err != nil {
t.Fatalf("Unable to read from socketpair: %v", err)
}
if got, want := buf[:bytesRead], []byte{0xFA}; !bytes.Equal(got, want) {
t.Fatalf("Read %v from the socketpair, wanted %v", got, want)
}
}
func TestInjectableEndpointDispatch(t *testing.T) {
endpoint, sock, dstIP := makeTestInjectableEndpoint(t)
hdr := buffer.NewPrependable(1)
hdr.Prepend(1)[0] = 0xFA
packetRoute := stack.Route{RemoteAddress: dstIP}
+4 -4
View File
@@ -38,9 +38,9 @@ const (
// ProtocolNumber is the ipv4 protocol number.
ProtocolNumber = header.IPv4ProtocolNumber
// maxTotalSize is maximum size that can be encoded in the 16-bit
// MaxTotalSize is maximum size that can be encoded in the 16-bit
// TotalLength field of the ipv4 header.
maxTotalSize = 0xffff
MaxTotalSize = 0xffff
// buckets is the number of identifier buckets.
buckets = 2048
@@ -204,8 +204,8 @@ func (p *protocol) Option(option interface{}) *tcpip.Error {
// calculateMTU calculates the network-layer payload MTU based on the link-layer
// payload mtu.
func calculateMTU(mtu uint32) uint32 {
if mtu > maxTotalSize {
mtu = maxTotalSize
if mtu > MaxTotalSize {
mtu = MaxTotalSize
}
return mtu - header.IPv4MinimumSize
}
+5
View File
@@ -260,6 +260,11 @@ type InjectableLinkEndpoint interface {
// Inject injects an inbound packet.
Inject(protocol tcpip.NetworkProtocolNumber, vv buffer.VectorisedView)
// WriteRawPacket writes a fully formed outbound packet directly to the link.
//
// dest is used by endpoints with multiple raw destinations.
WriteRawPacket(dest tcpip.Address, packet []byte) *tcpip.Error
}
// A LinkAddressResolver is an extension to a NetworkProtocol that