mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
Remove centralized registration of protocols.
Also removes the need for protocol names. PiperOrigin-RevId: 271186030
This commit is contained in:
committed by
gVisor bot
parent
99c86b8dbd
commit
59ccbb1044
@@ -60,7 +60,10 @@ func TestTimeouts(t *testing.T) {
|
||||
|
||||
func newLoopbackStack() (*stack.Stack, *tcpip.Error) {
|
||||
// Create the stack and add a NIC.
|
||||
s := stack.New([]string{ipv4.ProtocolName, ipv6.ProtocolName}, []string{tcp.ProtocolName, udp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol(), ipv6.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol(), udp.NewProtocol()},
|
||||
})
|
||||
|
||||
if err := s.CreateNIC(NICID, loopback.New()); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
// IPv4 addresses into link-local MAC addresses, and advertises IPv4
|
||||
// addresses of its stack with the local network.
|
||||
//
|
||||
// To use it in the networking stack, pass arp.ProtocolName as one of the
|
||||
// network protocols when calling stack.New. Then add an "arp" address to
|
||||
// every NIC on the stack that should respond to ARP requests. That is:
|
||||
// To use it in the networking stack, pass arp.NewProtocol() as one of the
|
||||
// network protocols when calling stack.New. Then add an "arp" address to every
|
||||
// NIC on the stack that should respond to ARP requests. That is:
|
||||
//
|
||||
// if err := s.AddAddress(1, arp.ProtocolNumber, "arp"); err != nil {
|
||||
// // handle err
|
||||
@@ -33,9 +33,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// ProtocolName is the string representation of the ARP protocol name.
|
||||
ProtocolName = "arp"
|
||||
|
||||
// ProtocolNumber is the ARP protocol number.
|
||||
ProtocolNumber = header.ARPProtocolNumber
|
||||
|
||||
@@ -200,8 +197,7 @@ func (p *protocol) Option(option interface{}) *tcpip.Error {
|
||||
|
||||
var broadcastMAC = tcpip.LinkAddress([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
|
||||
|
||||
func init() {
|
||||
stack.RegisterNetworkProtocolFactory(ProtocolName, func() stack.NetworkProtocol {
|
||||
return &protocol{}
|
||||
})
|
||||
// NewProtocol returns an ARP network protocol.
|
||||
func NewProtocol() stack.NetworkProtocol {
|
||||
return &protocol{}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,10 @@ type testContext struct {
|
||||
}
|
||||
|
||||
func newTestContext(t *testing.T) *testContext {
|
||||
s := stack.New([]string{ipv4.ProtocolName, arp.ProtocolName}, []string{icmp.ProtocolName4}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol(), arp.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{icmp.NewProtocol4()},
|
||||
})
|
||||
|
||||
const defaultMTU = 65536
|
||||
ep := channel.New(256, defaultMTU, stackLinkAddr)
|
||||
|
||||
@@ -172,7 +172,10 @@ func (t *testObject) WritePacket(_ *stack.Route, _ *stack.GSO, hdr buffer.Prepen
|
||||
}
|
||||
|
||||
func buildIPv4Route(local, remote tcpip.Address) (stack.Route, *tcpip.Error) {
|
||||
s := stack.New([]string{ipv4.ProtocolName}, []string{udp.ProtocolName, tcp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{udp.NewProtocol(), tcp.NewProtocol()},
|
||||
})
|
||||
s.CreateNIC(1, loopback.New())
|
||||
s.AddAddress(1, ipv4.ProtocolNumber, local)
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
@@ -185,7 +188,10 @@ func buildIPv4Route(local, remote tcpip.Address) (stack.Route, *tcpip.Error) {
|
||||
}
|
||||
|
||||
func buildIPv6Route(local, remote tcpip.Address) (stack.Route, *tcpip.Error) {
|
||||
s := stack.New([]string{ipv6.ProtocolName}, []string{udp.ProtocolName, tcp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv6.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{udp.NewProtocol(), tcp.NewProtocol()},
|
||||
})
|
||||
s.CreateNIC(1, loopback.New())
|
||||
s.AddAddress(1, ipv6.ProtocolNumber, local)
|
||||
s.SetRouteTable([]tcpip.Route{{
|
||||
|
||||
@@ -14,9 +14,9 @@
|
||||
|
||||
// Package ipv4 contains the implementation of the ipv4 network protocol. To use
|
||||
// it in the networking stack, this package must be added to the project, and
|
||||
// activated on the stack by passing ipv4.ProtocolName (or "ipv4") as one of the
|
||||
// network protocols when calling stack.New(). Then endpoints can be created
|
||||
// by passing ipv4.ProtocolNumber as the network protocol number when calling
|
||||
// activated on the stack by passing ipv4.NewProtocol() as one of the network
|
||||
// protocols when calling stack.New(). Then endpoints can be created by passing
|
||||
// ipv4.ProtocolNumber as the network protocol number when calling
|
||||
// Stack.NewEndpoint().
|
||||
package ipv4
|
||||
|
||||
@@ -32,9 +32,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// ProtocolName is the string representation of the ipv4 protocol name.
|
||||
ProtocolName = "ipv4"
|
||||
|
||||
// ProtocolNumber is the ipv4 protocol number.
|
||||
ProtocolNumber = header.IPv4ProtocolNumber
|
||||
|
||||
@@ -53,6 +50,7 @@ type endpoint struct {
|
||||
linkEP stack.LinkEndpoint
|
||||
dispatcher stack.TransportDispatcher
|
||||
fragmentation *fragmentation.Fragmentation
|
||||
protocol *protocol
|
||||
}
|
||||
|
||||
// NewEndpoint creates a new ipv4 endpoint.
|
||||
@@ -64,6 +62,7 @@ func (p *protocol) NewEndpoint(nicid tcpip.NICID, addrWithPrefix tcpip.AddressWi
|
||||
linkEP: linkEP,
|
||||
dispatcher: dispatcher,
|
||||
fragmentation: fragmentation.NewFragmentation(fragmentation.HighFragThreshold, fragmentation.LowFragThreshold, fragmentation.DefaultReassembleTimeout),
|
||||
protocol: p,
|
||||
}
|
||||
|
||||
return e, nil
|
||||
@@ -204,7 +203,7 @@ func (e *endpoint) WritePacket(r *stack.Route, gso *stack.GSO, hdr buffer.Prepen
|
||||
if length > header.IPv4MaximumHeaderSize+8 {
|
||||
// Packets of 68 bytes or less are required by RFC 791 to not be
|
||||
// fragmented, so we only assign ids to larger packets.
|
||||
id = atomic.AddUint32(&ids[hashRoute(r, protocol)%buckets], 1)
|
||||
id = atomic.AddUint32(&e.protocol.ids[hashRoute(r, protocol, e.protocol.hashIV)%buckets], 1)
|
||||
}
|
||||
ip.Encode(&header.IPv4Fields{
|
||||
IHL: header.IPv4MinimumSize,
|
||||
@@ -267,7 +266,7 @@ func (e *endpoint) WriteHeaderIncludedPacket(r *stack.Route, payload buffer.Vect
|
||||
if payload.Size() > header.IPv4MaximumHeaderSize+8 {
|
||||
// Packets of 68 bytes or less are required by RFC 791 to not be
|
||||
// fragmented, so we only assign ids to larger packets.
|
||||
id = atomic.AddUint32(&ids[hashRoute(r, 0 /* protocol */)%buckets], 1)
|
||||
id = atomic.AddUint32(&e.protocol.ids[hashRoute(r, 0 /* protocol */, e.protocol.hashIV)%buckets], 1)
|
||||
}
|
||||
ip.SetID(uint16(id))
|
||||
}
|
||||
@@ -325,14 +324,9 @@ func (e *endpoint) HandlePacket(r *stack.Route, vv buffer.VectorisedView) {
|
||||
// Close cleans up resources associated with the endpoint.
|
||||
func (e *endpoint) Close() {}
|
||||
|
||||
type protocol struct{}
|
||||
|
||||
// NewProtocol creates a new protocol ipv4 protocol descriptor. This is exported
|
||||
// only for tests that short-circuit the stack. Regular use of the protocol is
|
||||
// done via the stack, which gets a protocol descriptor from the init() function
|
||||
// below.
|
||||
func NewProtocol() stack.NetworkProtocol {
|
||||
return &protocol{}
|
||||
type protocol struct {
|
||||
ids []uint32
|
||||
hashIV uint32
|
||||
}
|
||||
|
||||
// Number returns the ipv4 protocol number.
|
||||
@@ -378,7 +372,7 @@ func calculateMTU(mtu uint32) uint32 {
|
||||
// hashRoute calculates a hash value for the given route. It uses the source &
|
||||
// destination address, the transport protocol number, and a random initial
|
||||
// value (generated once on initialization) to generate the hash.
|
||||
func hashRoute(r *stack.Route, protocol tcpip.TransportProtocolNumber) uint32 {
|
||||
func hashRoute(r *stack.Route, protocol tcpip.TransportProtocolNumber, hashIV uint32) uint32 {
|
||||
t := r.LocalAddress
|
||||
a := uint32(t[0]) | uint32(t[1])<<8 | uint32(t[2])<<16 | uint32(t[3])<<24
|
||||
t = r.RemoteAddress
|
||||
@@ -386,22 +380,16 @@ func hashRoute(r *stack.Route, protocol tcpip.TransportProtocolNumber) uint32 {
|
||||
return hash.Hash3Words(a, b, uint32(protocol), hashIV)
|
||||
}
|
||||
|
||||
var (
|
||||
ids []uint32
|
||||
hashIV uint32
|
||||
)
|
||||
|
||||
func init() {
|
||||
ids = make([]uint32, buckets)
|
||||
// NewProtocol returns an IPv4 network protocol.
|
||||
func NewProtocol() stack.NetworkProtocol {
|
||||
ids := make([]uint32, buckets)
|
||||
|
||||
// Randomly initialize hashIV and the ids.
|
||||
r := hash.RandN32(1 + buckets)
|
||||
for i := range ids {
|
||||
ids[i] = r[i]
|
||||
}
|
||||
hashIV = r[buckets]
|
||||
hashIV := r[buckets]
|
||||
|
||||
stack.RegisterNetworkProtocolFactory(ProtocolName, func() stack.NetworkProtocol {
|
||||
return &protocol{}
|
||||
})
|
||||
return &protocol{ids: ids, hashIV: hashIV}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,10 @@ import (
|
||||
)
|
||||
|
||||
func TestExcludeBroadcast(t *testing.T) {
|
||||
s := stack.New([]string{ipv4.ProtocolName}, []string{udp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{udp.NewProtocol()},
|
||||
})
|
||||
|
||||
const defaultMTU = 65536
|
||||
ep := stack.LinkEndpoint(channel.New(256, defaultMTU, ""))
|
||||
@@ -238,7 +241,9 @@ type context struct {
|
||||
|
||||
func buildContext(t *testing.T, packetCollectorErrors []*tcpip.Error, mtu uint32) context {
|
||||
// Make the packet and write it.
|
||||
s := stack.New([]string{ipv4.ProtocolName}, []string{}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()},
|
||||
})
|
||||
ep := newErrorChannel(100 /* Enough for all tests. */, mtu, "", packetCollectorErrors)
|
||||
s.CreateNIC(1, ep)
|
||||
const (
|
||||
|
||||
@@ -81,7 +81,10 @@ func (*stubLinkAddressCache) AddLinkAddress(tcpip.NICID, tcpip.Address, tcpip.Li
|
||||
}
|
||||
|
||||
func TestICMPCounts(t *testing.T) {
|
||||
s := stack.New([]string{ProtocolName}, []string{icmp.ProtocolName6}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{icmp.NewProtocol6()},
|
||||
})
|
||||
{
|
||||
if err := s.CreateNIC(1, &stubLinkEndpoint{}); err != nil {
|
||||
t.Fatalf("CreateNIC(_) = %s", err)
|
||||
@@ -205,8 +208,14 @@ func (e endpointWithResolutionCapability) Capabilities() stack.LinkEndpointCapab
|
||||
|
||||
func newTestContext(t *testing.T) *testContext {
|
||||
c := &testContext{
|
||||
s0: stack.New([]string{ProtocolName}, []string{icmp.ProtocolName6}, stack.Options{}),
|
||||
s1: stack.New([]string{ProtocolName}, []string{icmp.ProtocolName6}, stack.Options{}),
|
||||
s0: stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{icmp.NewProtocol6()},
|
||||
}),
|
||||
s1: stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{icmp.NewProtocol6()},
|
||||
}),
|
||||
}
|
||||
|
||||
const defaultMTU = 65536
|
||||
|
||||
@@ -14,9 +14,9 @@
|
||||
|
||||
// Package ipv6 contains the implementation of the ipv6 network protocol. To use
|
||||
// it in the networking stack, this package must be added to the project, and
|
||||
// activated on the stack by passing ipv6.ProtocolName (or "ipv6") as one of the
|
||||
// network protocols when calling stack.New(). Then endpoints can be created
|
||||
// by passing ipv6.ProtocolNumber as the network protocol number when calling
|
||||
// activated on the stack by passing ipv6.NewProtocol() as one of the network
|
||||
// protocols when calling stack.New(). Then endpoints can be created by passing
|
||||
// ipv6.ProtocolNumber as the network protocol number when calling
|
||||
// Stack.NewEndpoint().
|
||||
package ipv6
|
||||
|
||||
@@ -28,9 +28,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// ProtocolName is the string representation of the ipv6 protocol name.
|
||||
ProtocolName = "ipv6"
|
||||
|
||||
// ProtocolNumber is the ipv6 protocol number.
|
||||
ProtocolNumber = header.IPv6ProtocolNumber
|
||||
|
||||
@@ -160,14 +157,6 @@ func (*endpoint) Close() {}
|
||||
|
||||
type protocol struct{}
|
||||
|
||||
// NewProtocol creates a new protocol ipv6 protocol descriptor. This is exported
|
||||
// only for tests that short-circuit the stack. Regular use of the protocol is
|
||||
// done via the stack, which gets a protocol descriptor from the init() function
|
||||
// below.
|
||||
func NewProtocol() stack.NetworkProtocol {
|
||||
return &protocol{}
|
||||
}
|
||||
|
||||
// Number returns the ipv6 protocol number.
|
||||
func (p *protocol) Number() tcpip.NetworkProtocolNumber {
|
||||
return ProtocolNumber
|
||||
@@ -221,8 +210,7 @@ func calculateMTU(mtu uint32) uint32 {
|
||||
return maxPayloadSize
|
||||
}
|
||||
|
||||
func init() {
|
||||
stack.RegisterNetworkProtocolFactory(ProtocolName, func() stack.NetworkProtocol {
|
||||
return &protocol{}
|
||||
})
|
||||
// NewProtocol returns an IPv6 network protocol.
|
||||
func NewProtocol() stack.NetworkProtocol {
|
||||
return &protocol{}
|
||||
}
|
||||
|
||||
@@ -124,17 +124,20 @@ func testReceiveUDP(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst
|
||||
// UDP packets destined to the IPv6 link-local all-nodes multicast address.
|
||||
func TestReceiveOnAllNodesMulticastAddr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
protocolName string
|
||||
rxf func(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst tcpip.Address, want uint64)
|
||||
name string
|
||||
protocolFactory stack.TransportProtocol
|
||||
rxf func(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst tcpip.Address, want uint64)
|
||||
}{
|
||||
{"ICMP", icmp.ProtocolName6, testReceiveICMP},
|
||||
{"UDP", udp.ProtocolName, testReceiveUDP},
|
||||
{"ICMP", icmp.NewProtocol6(), testReceiveICMP},
|
||||
{"UDP", udp.NewProtocol(), testReceiveUDP},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
s := stack.New([]string{ProtocolName}, []string{test.protocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{test.protocolFactory},
|
||||
})
|
||||
e := channel.New(10, 1280, linkAddr1)
|
||||
if err := s.CreateNIC(1, e); err != nil {
|
||||
t.Fatalf("CreateNIC(_) = %s", err)
|
||||
@@ -152,19 +155,22 @@ func TestReceiveOnAllNodesMulticastAddr(t *testing.T) {
|
||||
// address.
|
||||
func TestReceiveOnSolicitedNodeAddr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
protocolName string
|
||||
rxf func(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst tcpip.Address, want uint64)
|
||||
name string
|
||||
protocolFactory stack.TransportProtocol
|
||||
rxf func(t *testing.T, s *stack.Stack, e *channel.Endpoint, src, dst tcpip.Address, want uint64)
|
||||
}{
|
||||
{"ICMP", icmp.ProtocolName6, testReceiveICMP},
|
||||
{"UDP", udp.ProtocolName, testReceiveUDP},
|
||||
{"ICMP", icmp.NewProtocol6(), testReceiveICMP},
|
||||
{"UDP", udp.NewProtocol(), testReceiveUDP},
|
||||
}
|
||||
|
||||
snmc := header.SolicitedNodeAddr(addr2)
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
s := stack.New([]string{ProtocolName}, []string{test.protocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{test.protocolFactory},
|
||||
})
|
||||
e := channel.New(10, 1280, linkAddr1)
|
||||
if err := s.CreateNIC(1, e); err != nil {
|
||||
t.Fatalf("CreateNIC(_) = %s", err)
|
||||
@@ -237,7 +243,9 @@ func TestAddIpv6Address(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
s := stack.New([]string{ProtocolName}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{NewProtocol()},
|
||||
})
|
||||
if err := s.CreateNIC(1, &stubLinkEndpoint{}); err != nil {
|
||||
t.Fatalf("CreateNIC(_) = %s", err)
|
||||
}
|
||||
|
||||
@@ -31,7 +31,10 @@ import (
|
||||
func setupStackAndEndpoint(t *testing.T, llladdr, rlladdr tcpip.Address) (*stack.Stack, stack.NetworkEndpoint) {
|
||||
t.Helper()
|
||||
|
||||
s := stack.New([]string{ProtocolName}, []string{icmp.ProtocolName6}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{icmp.NewProtocol6()},
|
||||
})
|
||||
|
||||
if err := s.CreateNIC(1, &stubLinkEndpoint{}); err != nil {
|
||||
t.Fatalf("CreateNIC(_) = %s", err)
|
||||
|
||||
@@ -126,7 +126,10 @@ func main() {
|
||||
|
||||
// Create the stack with ipv4 and tcp protocols, then add a tun-based
|
||||
// NIC and ipv4 address.
|
||||
s := stack.New([]string{ipv4.ProtocolName}, []string{tcp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
|
||||
})
|
||||
|
||||
mtu, err := rawfile.GetMTU(tunName)
|
||||
if err != nil {
|
||||
|
||||
@@ -111,7 +111,10 @@ func main() {
|
||||
|
||||
// Create the stack with ip and tcp protocols, then add a tun-based
|
||||
// NIC and address.
|
||||
s := stack.New([]string{ipv4.ProtocolName, ipv6.ProtocolName, arp.ProtocolName}, []string{tcp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol(), ipv6.NewProtocol(), arp.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
|
||||
})
|
||||
|
||||
mtu, err := rawfile.GetMTU(tunName)
|
||||
if err != nil {
|
||||
|
||||
@@ -366,14 +366,6 @@ type LinkAddressCache interface {
|
||||
RemoveWaker(nicid tcpip.NICID, addr tcpip.Address, waker *sleep.Waker)
|
||||
}
|
||||
|
||||
// TransportProtocolFactory functions are used by the stack to instantiate
|
||||
// transport protocols.
|
||||
type TransportProtocolFactory func() TransportProtocol
|
||||
|
||||
// NetworkProtocolFactory provides methods to be used by the stack to
|
||||
// instantiate network protocols.
|
||||
type NetworkProtocolFactory func() NetworkProtocol
|
||||
|
||||
// UnassociatedEndpointFactory produces endpoints for writing packets not
|
||||
// associated with a particular transport protocol. Such endpoints can be used
|
||||
// to write arbitrary packets that include the IP header.
|
||||
@@ -381,34 +373,6 @@ type UnassociatedEndpointFactory interface {
|
||||
NewUnassociatedRawEndpoint(stack *Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error)
|
||||
}
|
||||
|
||||
var (
|
||||
transportProtocols = make(map[string]TransportProtocolFactory)
|
||||
networkProtocols = make(map[string]NetworkProtocolFactory)
|
||||
|
||||
unassociatedFactory UnassociatedEndpointFactory
|
||||
)
|
||||
|
||||
// RegisterTransportProtocolFactory registers a new transport protocol factory
|
||||
// with the stack so that it becomes available to users of the stack. This
|
||||
// function is intended to be called by init() functions of the protocols.
|
||||
func RegisterTransportProtocolFactory(name string, p TransportProtocolFactory) {
|
||||
transportProtocols[name] = p
|
||||
}
|
||||
|
||||
// RegisterNetworkProtocolFactory registers a new network protocol factory with
|
||||
// the stack so that it becomes available to users of the stack. This function
|
||||
// is intended to be called by init() functions of the protocols.
|
||||
func RegisterNetworkProtocolFactory(name string, p NetworkProtocolFactory) {
|
||||
networkProtocols[name] = p
|
||||
}
|
||||
|
||||
// RegisterUnassociatedFactory registers a factory to produce endpoints not
|
||||
// associated with any particular transport protocol. This function is intended
|
||||
// to be called by init() functions of the protocols.
|
||||
func RegisterUnassociatedFactory(f UnassociatedEndpointFactory) {
|
||||
unassociatedFactory = f
|
||||
}
|
||||
|
||||
// GSOType is the type of GSO segments.
|
||||
//
|
||||
// +stateify savable
|
||||
|
||||
+18
-27
@@ -17,11 +17,6 @@
|
||||
//
|
||||
// For consumers, the only function of interest is New(), everything else is
|
||||
// provided by the tcpip/public package.
|
||||
//
|
||||
// For protocol implementers, RegisterTransportProtocolFactory() and
|
||||
// RegisterNetworkProtocolFactory() are used to register protocol factories with
|
||||
// the stack, which will then be used to instantiate protocol objects when
|
||||
// consumers interact with the stack.
|
||||
package stack
|
||||
|
||||
import (
|
||||
@@ -351,6 +346,9 @@ type Stack struct {
|
||||
networkProtocols map[tcpip.NetworkProtocolNumber]NetworkProtocol
|
||||
linkAddrResolvers map[tcpip.NetworkProtocolNumber]LinkAddressResolver
|
||||
|
||||
// unassociatedFactory creates unassociated endpoints. If nil, raw
|
||||
// endpoints are disabled. It is set during Stack creation and is
|
||||
// immutable.
|
||||
unassociatedFactory UnassociatedEndpointFactory
|
||||
|
||||
demux *transportDemuxer
|
||||
@@ -359,10 +357,6 @@ type Stack struct {
|
||||
|
||||
linkAddrCache *linkAddrCache
|
||||
|
||||
// raw indicates whether raw sockets may be created. It is set during
|
||||
// Stack creation and is immutable.
|
||||
raw bool
|
||||
|
||||
mu sync.RWMutex
|
||||
nics map[tcpip.NICID]*NIC
|
||||
forwarding bool
|
||||
@@ -398,6 +392,12 @@ type Stack struct {
|
||||
|
||||
// Options contains optional Stack configuration.
|
||||
type Options struct {
|
||||
// NetworkProtocols lists the network protocols to enable.
|
||||
NetworkProtocols []NetworkProtocol
|
||||
|
||||
// TransportProtocols lists the transport protocols to enable.
|
||||
TransportProtocols []TransportProtocol
|
||||
|
||||
// Clock is an optional clock source used for timestampping packets.
|
||||
//
|
||||
// If no Clock is specified, the clock source will be time.Now.
|
||||
@@ -411,8 +411,9 @@ type Options struct {
|
||||
// stack (false).
|
||||
HandleLocal bool
|
||||
|
||||
// Raw indicates whether raw sockets may be created.
|
||||
Raw bool
|
||||
// UnassociatedFactory produces unassociated endpoints raw endpoints.
|
||||
// Raw endpoints are enabled only if this is non-nil.
|
||||
UnassociatedFactory UnassociatedEndpointFactory
|
||||
}
|
||||
|
||||
// New allocates a new networking stack with only the requested networking and
|
||||
@@ -422,7 +423,7 @@ type Options struct {
|
||||
// SetNetworkProtocolOption/SetTransportProtocolOption methods provided by the
|
||||
// stack. Please refer to individual protocol implementations as to what options
|
||||
// are supported.
|
||||
func New(network []string, transport []string, opts Options) *Stack {
|
||||
func New(opts Options) *Stack {
|
||||
clock := opts.Clock
|
||||
if clock == nil {
|
||||
clock = &tcpip.StdClock{}
|
||||
@@ -438,17 +439,11 @@ func New(network []string, transport []string, opts Options) *Stack {
|
||||
clock: clock,
|
||||
stats: opts.Stats.FillIn(),
|
||||
handleLocal: opts.HandleLocal,
|
||||
raw: opts.Raw,
|
||||
icmpRateLimiter: NewICMPRateLimiter(),
|
||||
}
|
||||
|
||||
// Add specified network protocols.
|
||||
for _, name := range network {
|
||||
netProtoFactory, ok := networkProtocols[name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
netProto := netProtoFactory()
|
||||
for _, netProto := range opts.NetworkProtocols {
|
||||
s.networkProtocols[netProto.Number()] = netProto
|
||||
if r, ok := netProto.(LinkAddressResolver); ok {
|
||||
s.linkAddrResolvers[r.LinkAddressProtocol()] = r
|
||||
@@ -456,18 +451,14 @@ func New(network []string, transport []string, opts Options) *Stack {
|
||||
}
|
||||
|
||||
// Add specified transport protocols.
|
||||
for _, name := range transport {
|
||||
transProtoFactory, ok := transportProtocols[name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
transProto := transProtoFactory()
|
||||
for _, transProto := range opts.TransportProtocols {
|
||||
s.transportProtocols[transProto.Number()] = &transportProtocolState{
|
||||
proto: transProto,
|
||||
}
|
||||
}
|
||||
|
||||
s.unassociatedFactory = unassociatedFactory
|
||||
// Add the factory for unassociated endpoints, if present.
|
||||
s.unassociatedFactory = opts.UnassociatedFactory
|
||||
|
||||
// Create the global transport demuxer.
|
||||
s.demux = newTransportDemuxer(s)
|
||||
@@ -602,7 +593,7 @@ func (s *Stack) NewEndpoint(transport tcpip.TransportProtocolNumber, network tcp
|
||||
// protocol. Raw endpoints receive all traffic for a given protocol regardless
|
||||
// of address.
|
||||
func (s *Stack) NewRawEndpoint(transport tcpip.TransportProtocolNumber, network tcpip.NetworkProtocolNumber, waiterQueue *waiter.Queue, associated bool) (tcpip.Endpoint, *tcpip.Error) {
|
||||
if !s.raw {
|
||||
if s.unassociatedFactory == nil {
|
||||
return nil, tcpip.ErrNotPermitted
|
||||
}
|
||||
|
||||
|
||||
@@ -222,11 +222,17 @@ func (f *fakeNetworkProtocol) Option(option interface{}) *tcpip.Error {
|
||||
}
|
||||
}
|
||||
|
||||
func fakeNetFactory() stack.NetworkProtocol {
|
||||
return &fakeNetworkProtocol{}
|
||||
}
|
||||
|
||||
func TestNetworkReceive(t *testing.T) {
|
||||
// Create a stack with the fake network protocol, one nic, and two
|
||||
// addresses attached to it: 1 & 2.
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
}
|
||||
@@ -370,7 +376,9 @@ func TestNetworkSend(t *testing.T) {
|
||||
// address: 1. The route table sends all packets through the only
|
||||
// existing nic.
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
t.Fatal("NewNIC failed:", err)
|
||||
}
|
||||
@@ -395,7 +403,9 @@ func TestNetworkSendMultiRoute(t *testing.T) {
|
||||
// Create a stack with the fake network protocol, two nics, and two
|
||||
// addresses per nic, the first nic has odd address, the second one has
|
||||
// even addresses.
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep1 := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep1); err != nil {
|
||||
@@ -476,7 +486,9 @@ func TestRoutes(t *testing.T) {
|
||||
// Create a stack with the fake network protocol, two nics, and two
|
||||
// addresses per nic, the first nic has odd address, the second one has
|
||||
// even addresses.
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep1 := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep1); err != nil {
|
||||
@@ -554,7 +566,9 @@ func TestAddressRemoval(t *testing.T) {
|
||||
localAddr := tcpip.Address([]byte{localAddrByte})
|
||||
remoteAddr := tcpip.Address("\x02")
|
||||
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -599,7 +613,9 @@ func TestAddressRemovalWithRouteHeld(t *testing.T) {
|
||||
localAddr := tcpip.Address([]byte{localAddrByte})
|
||||
remoteAddr := tcpip.Address("\x02")
|
||||
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -688,7 +704,9 @@ func TestEndpointExpiration(t *testing.T) {
|
||||
for _, promiscuous := range []bool{true, false} {
|
||||
for _, spoofing := range []bool{true, false} {
|
||||
t.Run(fmt.Sprintf("promiscuous=%t spoofing=%t", promiscuous, spoofing), func(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(nicid, ep); err != nil {
|
||||
@@ -844,7 +862,9 @@ func TestEndpointExpiration(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPromiscuousMode(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -894,7 +914,9 @@ func TestSpoofingWithAddress(t *testing.T) {
|
||||
nonExistentLocalAddr := tcpip.Address("\x02")
|
||||
dstAddr := tcpip.Address("\x03")
|
||||
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -958,7 +980,9 @@ func TestSpoofingNoAddress(t *testing.T) {
|
||||
nonExistentLocalAddr := tcpip.Address("\x01")
|
||||
dstAddr := tcpip.Address("\x02")
|
||||
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -1003,7 +1027,9 @@ func TestSpoofingNoAddress(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBroadcastNeedsNoRoute(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -1074,7 +1100,9 @@ func TestMulticastOrIPv6LinkLocalNeedsNoRoute(t *testing.T) {
|
||||
{"IPv6 Unicast Not Link-Local 7", true, "\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -1130,7 +1158,9 @@ func TestMulticastOrIPv6LinkLocalNeedsNoRoute(t *testing.T) {
|
||||
|
||||
// Add a range of addresses, then check that a packet is delivered.
|
||||
func TestAddressRangeAcceptsMatchingPacket(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -1196,7 +1226,9 @@ func testNicForAddressRange(t *testing.T, nicID tcpip.NICID, s *stack.Stack, sub
|
||||
// existent.
|
||||
func TestCheckLocalAddressForSubnet(t *testing.T) {
|
||||
const nicID tcpip.NICID = 1
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(nicID, ep); err != nil {
|
||||
@@ -1234,7 +1266,9 @@ func TestCheckLocalAddressForSubnet(t *testing.T) {
|
||||
// Set a range of addresses, then send a packet to a destination outside the
|
||||
// range and then check it doesn't get delivered.
|
||||
func TestAddressRangeRejectsNonmatchingPacket(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
@@ -1266,7 +1300,10 @@ func TestAddressRangeRejectsNonmatchingPacket(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNetworkOptions(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, []string{}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
TransportProtocols: []stack.TransportProtocol{},
|
||||
})
|
||||
|
||||
// Try an unsupported network protocol.
|
||||
if err := s.SetNetworkProtocolOption(tcpip.NetworkProtocolNumber(99999), fakeNetGoodOption(false)); err != tcpip.ErrUnknownProtocol {
|
||||
@@ -1319,7 +1356,9 @@ func stackContainsAddressRange(s *stack.Stack, id tcpip.NICID, addrRange tcpip.S
|
||||
}
|
||||
|
||||
func TestAddresRangeAddRemove(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
@@ -1360,7 +1399,9 @@ func TestGetMainNICAddressAddPrimaryNonPrimary(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("canBe=%d", canBe), func(t *testing.T) {
|
||||
for never := 0; never < 3; never++ {
|
||||
t.Run(fmt.Sprintf("never=%d", never), func(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
@@ -1425,7 +1466,9 @@ func TestGetMainNICAddressAddPrimaryNonPrimary(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetMainNICAddressAddRemove(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
@@ -1508,7 +1551,9 @@ func verifyAddresses(t *testing.T, expectedAddresses, gotAddresses []tcpip.Proto
|
||||
|
||||
func TestAddAddress(t *testing.T) {
|
||||
const nicid = 1
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(nicid, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
@@ -1533,7 +1578,9 @@ func TestAddAddress(t *testing.T) {
|
||||
|
||||
func TestAddProtocolAddress(t *testing.T) {
|
||||
const nicid = 1
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(nicid, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
@@ -1565,7 +1612,9 @@ func TestAddProtocolAddress(t *testing.T) {
|
||||
|
||||
func TestAddAddressWithOptions(t *testing.T) {
|
||||
const nicid = 1
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(nicid, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
@@ -1594,7 +1643,9 @@ func TestAddAddressWithOptions(t *testing.T) {
|
||||
|
||||
func TestAddProtocolAddressWithOptions(t *testing.T) {
|
||||
const nicid = 1
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(nicid, ep); err != nil {
|
||||
t.Fatal("CreateNIC failed:", err)
|
||||
@@ -1628,7 +1679,9 @@ func TestAddProtocolAddressWithOptions(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNICStats(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
ep1 := channel.New(10, defaultMTU, "")
|
||||
if err := s.CreateNIC(1, ep1); err != nil {
|
||||
t.Fatal("CreateNIC failed: ", err)
|
||||
@@ -1674,7 +1727,9 @@ func TestNICStats(t *testing.T) {
|
||||
func TestNICForwarding(t *testing.T) {
|
||||
// Create a stack with the fake network protocol, two NICs, each with
|
||||
// an address.
|
||||
s := stack.New([]string{"fakeNet"}, nil, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
})
|
||||
s.SetForwarding(true)
|
||||
|
||||
ep1 := channel.New(10, defaultMTU, "")
|
||||
@@ -1722,9 +1777,3 @@ func TestNICForwarding(t *testing.T) {
|
||||
t.Errorf("got Tx.Bytes.Value() = %d, want = %d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
stack.RegisterNetworkProtocolFactory("fakeNet", func() stack.NetworkProtocol {
|
||||
return &fakeNetworkProtocol{}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -282,9 +282,16 @@ func (f *fakeTransportProtocol) Option(option interface{}) *tcpip.Error {
|
||||
}
|
||||
}
|
||||
|
||||
func fakeTransFactory() stack.TransportProtocol {
|
||||
return &fakeTransportProtocol{}
|
||||
}
|
||||
|
||||
func TestTransportReceive(t *testing.T) {
|
||||
linkEP := channel.New(10, defaultMTU, "")
|
||||
s := stack.New([]string{"fakeNet"}, []string{"fakeTrans"}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
TransportProtocols: []stack.TransportProtocol{fakeTransFactory()},
|
||||
})
|
||||
if err := s.CreateNIC(1, linkEP); err != nil {
|
||||
t.Fatalf("CreateNIC failed: %v", err)
|
||||
}
|
||||
@@ -346,7 +353,10 @@ func TestTransportReceive(t *testing.T) {
|
||||
|
||||
func TestTransportControlReceive(t *testing.T) {
|
||||
linkEP := channel.New(10, defaultMTU, "")
|
||||
s := stack.New([]string{"fakeNet"}, []string{"fakeTrans"}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
TransportProtocols: []stack.TransportProtocol{fakeTransFactory()},
|
||||
})
|
||||
if err := s.CreateNIC(1, linkEP); err != nil {
|
||||
t.Fatalf("CreateNIC failed: %v", err)
|
||||
}
|
||||
@@ -414,7 +424,10 @@ func TestTransportControlReceive(t *testing.T) {
|
||||
|
||||
func TestTransportSend(t *testing.T) {
|
||||
linkEP := channel.New(10, defaultMTU, "")
|
||||
s := stack.New([]string{"fakeNet"}, []string{"fakeTrans"}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
TransportProtocols: []stack.TransportProtocol{fakeTransFactory()},
|
||||
})
|
||||
if err := s.CreateNIC(1, linkEP); err != nil {
|
||||
t.Fatalf("CreateNIC failed: %v", err)
|
||||
}
|
||||
@@ -457,7 +470,10 @@ func TestTransportSend(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTransportOptions(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, []string{"fakeTrans"}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
TransportProtocols: []stack.TransportProtocol{fakeTransFactory()},
|
||||
})
|
||||
|
||||
// Try an unsupported transport protocol.
|
||||
if err := s.SetTransportProtocolOption(tcpip.TransportProtocolNumber(99999), fakeTransportGoodOption(false)); err != tcpip.ErrUnknownProtocol {
|
||||
@@ -498,7 +514,10 @@ func TestTransportOptions(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTransportForwarding(t *testing.T) {
|
||||
s := stack.New([]string{"fakeNet"}, []string{"fakeTrans"}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{fakeNetFactory()},
|
||||
TransportProtocols: []stack.TransportProtocol{fakeTransFactory()},
|
||||
})
|
||||
s.SetForwarding(true)
|
||||
|
||||
// TODO(b/123449044): Change this to a channel NIC.
|
||||
@@ -576,9 +595,3 @@ func TestTransportForwarding(t *testing.T) {
|
||||
t.Errorf("Response packet has incorrect source addresss: got = %d, want = 3", src)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
stack.RegisterTransportProtocolFactory("fakeTrans", func() stack.TransportProtocol {
|
||||
return &fakeTransportProtocol{}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,10 +14,9 @@
|
||||
|
||||
// Package icmp contains the implementation of the ICMP and IPv6-ICMP transport
|
||||
// protocols for use in ping. To use it in the networking stack, this package
|
||||
// must be added to the project, and
|
||||
// activated on the stack by passing icmp.ProtocolName (or "icmp") and/or
|
||||
// icmp.ProtocolName6 (or "icmp6") as one of the transport protocols when
|
||||
// calling stack.New(). Then endpoints can be created by passing
|
||||
// must be added to the project, and activated on the stack by passing
|
||||
// icmp.NewProtocol4() and/or icmp.NewProtocol6() as one of the transport
|
||||
// protocols when calling stack.New(). Then endpoints can be created by passing
|
||||
// icmp.ProtocolNumber or icmp.ProtocolNumber6 as the transport protocol number
|
||||
// when calling Stack.NewEndpoint().
|
||||
package icmp
|
||||
@@ -34,15 +33,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// ProtocolName4 is the string representation of the icmp protocol name.
|
||||
ProtocolName4 = "icmp4"
|
||||
|
||||
// ProtocolNumber4 is the ICMP protocol number.
|
||||
ProtocolNumber4 = header.ICMPv4ProtocolNumber
|
||||
|
||||
// ProtocolName6 is the string representation of the icmp protocol name.
|
||||
ProtocolName6 = "icmp6"
|
||||
|
||||
// ProtocolNumber6 is the IPv6-ICMP protocol number.
|
||||
ProtocolNumber6 = header.ICMPv6ProtocolNumber
|
||||
)
|
||||
@@ -125,12 +118,12 @@ func (p *protocol) Option(option interface{}) *tcpip.Error {
|
||||
return tcpip.ErrUnknownProtocolOption
|
||||
}
|
||||
|
||||
func init() {
|
||||
stack.RegisterTransportProtocolFactory(ProtocolName4, func() stack.TransportProtocol {
|
||||
return &protocol{ProtocolNumber4}
|
||||
})
|
||||
// NewProtocol4 returns an ICMPv4 transport protocol.
|
||||
func NewProtocol4() stack.TransportProtocol {
|
||||
return &protocol{ProtocolNumber4}
|
||||
}
|
||||
|
||||
stack.RegisterTransportProtocolFactory(ProtocolName6, func() stack.TransportProtocol {
|
||||
return &protocol{ProtocolNumber6}
|
||||
})
|
||||
// NewProtocol6 returns an ICMPv6 transport protocol.
|
||||
func NewProtocol6() stack.TransportProtocol {
|
||||
return &protocol{ProtocolNumber6}
|
||||
}
|
||||
|
||||
@@ -20,13 +20,10 @@ import (
|
||||
"gvisor.dev/gvisor/pkg/waiter"
|
||||
)
|
||||
|
||||
type factory struct{}
|
||||
// EndpointFactory implements stack.UnassociatedEndpointFactory.
|
||||
type EndpointFactory struct{}
|
||||
|
||||
// NewUnassociatedRawEndpoint implements stack.UnassociatedEndpointFactory.
|
||||
func (factory) NewUnassociatedRawEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) {
|
||||
func (EndpointFactory) NewUnassociatedRawEndpoint(stack *stack.Stack, netProto tcpip.NetworkProtocolNumber, transProto tcpip.TransportProtocolNumber, waiterQueue *waiter.Queue) (tcpip.Endpoint, *tcpip.Error) {
|
||||
return newEndpoint(stack, netProto, transProto, waiterQueue, false /* associated */)
|
||||
}
|
||||
|
||||
func init() {
|
||||
stack.RegisterUnassociatedFactory(factory{})
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
// Package tcp contains the implementation of the TCP transport protocol. To use
|
||||
// it in the networking stack, this package must be added to the project, and
|
||||
// activated on the stack by passing tcp.ProtocolName (or "tcp") as one of the
|
||||
// activated on the stack by passing tcp.NewProtocol() as one of the
|
||||
// transport protocols when calling stack.New(). Then endpoints can be created
|
||||
// by passing tcp.ProtocolNumber as the transport protocol number when calling
|
||||
// Stack.NewEndpoint().
|
||||
@@ -34,9 +34,6 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// ProtocolName is the string representation of the tcp protocol name.
|
||||
ProtocolName = "tcp"
|
||||
|
||||
// ProtocolNumber is the tcp protocol number.
|
||||
ProtocolNumber = header.TCPProtocolNumber
|
||||
|
||||
@@ -254,13 +251,12 @@ func (p *protocol) Option(option interface{}) *tcpip.Error {
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
stack.RegisterTransportProtocolFactory(ProtocolName, func() stack.TransportProtocol {
|
||||
return &protocol{
|
||||
sendBufferSize: SendBufferSizeOption{MinBufferSize, DefaultSendBufferSize, MaxBufferSize},
|
||||
recvBufferSize: ReceiveBufferSizeOption{MinBufferSize, DefaultReceiveBufferSize, MaxBufferSize},
|
||||
congestionControl: ccReno,
|
||||
availableCongestionControl: []string{ccReno, ccCubic},
|
||||
}
|
||||
})
|
||||
// NewProtocol returns a TCP transport protocol.
|
||||
func NewProtocol() stack.TransportProtocol {
|
||||
return &protocol{
|
||||
sendBufferSize: SendBufferSizeOption{MinBufferSize, DefaultSendBufferSize, MaxBufferSize},
|
||||
recvBufferSize: ReceiveBufferSizeOption{MinBufferSize, DefaultReceiveBufferSize, MaxBufferSize},
|
||||
congestionControl: ccReno,
|
||||
availableCongestionControl: []string{ccReno, ccCubic},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2873,7 +2873,10 @@ func checkSendBufferSize(t *testing.T, ep tcpip.Endpoint, v int) {
|
||||
}
|
||||
|
||||
func TestDefaultBufferSizes(t *testing.T) {
|
||||
s := stack.New([]string{ipv4.ProtocolName}, []string{tcp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
|
||||
})
|
||||
|
||||
// Check the default values.
|
||||
ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
|
||||
@@ -2919,7 +2922,10 @@ func TestDefaultBufferSizes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMinMaxBufferSizes(t *testing.T) {
|
||||
s := stack.New([]string{ipv4.ProtocolName}, []string{tcp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{ipv4.NewProtocol()},
|
||||
TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
|
||||
})
|
||||
|
||||
// Check the default values.
|
||||
ep, err := s.NewEndpoint(tcp.ProtocolNumber, ipv4.ProtocolNumber, &waiter.Queue{})
|
||||
@@ -2965,10 +2971,13 @@ func TestMinMaxBufferSizes(t *testing.T) {
|
||||
}
|
||||
|
||||
func makeStack() (*stack.Stack, *tcpip.Error) {
|
||||
s := stack.New([]string{
|
||||
ipv4.ProtocolName,
|
||||
ipv6.ProtocolName,
|
||||
}, []string{tcp.ProtocolName}, stack.Options{})
|
||||
s := stack.New(stack.Options{
|
||||
NetworkProtocols: []stack.NetworkProtocol{
|
||||
ipv4.NewProtocol(),
|
||||
ipv6.NewProtocol(),
|
||||
},
|
||||
TransportProtocols: []stack.TransportProtocol{tcp.NewProtocol()},
|
||||
})
|
||||
|
||||
id := loopback.New()
|
||||
if testing.Verbose() {
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user