[infra] Split tcpip/integration test targets to aid investigation.

tcpip integration tests have been flaky lately. They usually run in 20 seconds
and have a 60 seconds timeout. Sometimes they timeout which could be due to
a bug or deadlock. To further investigate it might be helpful to split the
targets and see which test is causing the flake.

Added a new tcpip/tests/utils package to hold all common utilities across all
tests.

PiperOrigin-RevId: 358012936
This commit is contained in:
Ayush Ranjan
2021-02-17 12:54:55 -08:00
committed by gVisor bot
parent 2f35fa14ab
commit d8590f6337
9 changed files with 651 additions and 510 deletions
+103 -12
View File
@@ -3,31 +3,57 @@ load("//tools:defs.bzl", "go_test")
package(licenses = ["notice"])
go_test(
name = "integration_test",
name = "forward_test",
size = "small",
srcs = [
"forward_test.go",
"iptables_test.go",
"link_resolution_test.go",
"loopback_test.go",
"multicast_broadcast_test.go",
"route_test.go",
srcs = ["forward_test.go"],
deps = [
"//pkg/tcpip",
"//pkg/tcpip/checker",
"//pkg/tcpip/network/arp",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
"//pkg/tcpip/tests/utils",
"//pkg/tcpip/transport/tcp",
"//pkg/tcpip/transport/udp",
"//pkg/waiter",
"@com_github_google_go_cmp//cmp:go_default_library",
],
)
go_test(
name = "iptables_test",
size = "small",
srcs = ["iptables_test.go"],
deps = [
"//pkg/tcpip",
"//pkg/tcpip/buffer",
"//pkg/tcpip/header",
"//pkg/tcpip/link/channel",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
"//pkg/tcpip/tests/utils",
"//pkg/tcpip/transport/udp",
],
)
go_test(
name = "link_resolution_test",
size = "small",
srcs = ["link_resolution_test.go"],
deps = [
"//pkg/tcpip",
"//pkg/tcpip/buffer",
"//pkg/tcpip/checker",
"//pkg/tcpip/faketime",
"//pkg/tcpip/header",
"//pkg/tcpip/link/channel",
"//pkg/tcpip/link/ethernet",
"//pkg/tcpip/link/loopback",
"//pkg/tcpip/link/nested",
"//pkg/tcpip/link/pipe",
"//pkg/tcpip/network/arp",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
"//pkg/tcpip/tests/utils",
"//pkg/tcpip/transport/icmp",
"//pkg/tcpip/transport/tcp",
"//pkg/tcpip/transport/udp",
@@ -36,3 +62,68 @@ go_test(
"@com_github_google_go_cmp//cmp/cmpopts:go_default_library",
],
)
go_test(
name = "loopback_test",
size = "small",
srcs = ["loopback_test.go"],
deps = [
"//pkg/tcpip",
"//pkg/tcpip/buffer",
"//pkg/tcpip/checker",
"//pkg/tcpip/header",
"//pkg/tcpip/link/loopback",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
"//pkg/tcpip/tests/utils",
"//pkg/tcpip/transport/tcp",
"//pkg/tcpip/transport/udp",
"//pkg/waiter",
"@com_github_google_go_cmp//cmp:go_default_library",
],
)
go_test(
name = "multicast_broadcast_test",
size = "small",
srcs = ["multicast_broadcast_test.go"],
deps = [
"//pkg/tcpip",
"//pkg/tcpip/buffer",
"//pkg/tcpip/checker",
"//pkg/tcpip/header",
"//pkg/tcpip/link/channel",
"//pkg/tcpip/link/loopback",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
"//pkg/tcpip/tests/utils",
"//pkg/tcpip/transport/icmp",
"//pkg/tcpip/transport/udp",
"//pkg/waiter",
"@com_github_google_go_cmp//cmp:go_default_library",
],
)
go_test(
name = "route_test",
size = "small",
srcs = ["route_test.go"],
deps = [
"//pkg/tcpip",
"//pkg/tcpip/buffer",
"//pkg/tcpip/checker",
"//pkg/tcpip/header",
"//pkg/tcpip/link/channel",
"//pkg/tcpip/link/loopback",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
"//pkg/tcpip/tests/utils",
"//pkg/tcpip/transport/icmp",
"//pkg/tcpip/transport/udp",
"//pkg/waiter",
"@com_github_google_go_cmp//cmp:go_default_library",
],
)
+11 -217
View File
@@ -12,231 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package integration_test
package forward_test
import (
"bytes"
"net"
"testing"
"github.com/google/go-cmp/cmp"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/checker"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/ethernet"
"gvisor.dev/gvisor/pkg/tcpip/link/nested"
"gvisor.dev/gvisor/pkg/tcpip/link/pipe"
"gvisor.dev/gvisor/pkg/tcpip/network/arp"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/tests/utils"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/waiter"
)
var _ stack.NetworkDispatcher = (*endpointWithDestinationCheck)(nil)
var _ stack.LinkEndpoint = (*endpointWithDestinationCheck)(nil)
const (
host1NICID = 1
routerNICID1 = 2
routerNICID2 = 3
host2NICID = 4
)
var (
host1IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.0.2").To4()),
PrefixLen: 24,
},
}
routerNIC1IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.0.1").To4()),
PrefixLen: 24,
},
}
routerNIC2IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("10.0.0.1").To4()),
PrefixLen: 8,
},
}
host2IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("10.0.0.2").To4()),
PrefixLen: 8,
},
}
host1IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("a::2").To16()),
PrefixLen: 64,
},
}
routerNIC1IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("a::1").To16()),
PrefixLen: 64,
},
}
routerNIC2IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("b::1").To16()),
PrefixLen: 64,
},
}
host2IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("b::2").To16()),
PrefixLen: 64,
},
}
)
func setupRoutedStacks(t *testing.T, host1Stack, routerStack, host2Stack *stack.Stack) {
host1NIC, routerNIC1 := pipe.New(linkAddr1, linkAddr2)
routerNIC2, host2NIC := pipe.New(linkAddr3, linkAddr4)
if err := host1Stack.CreateNIC(host1NICID, newEthernetEndpoint(host1NIC)); err != nil {
t.Fatalf("host1Stack.CreateNIC(%d, _): %s", host1NICID, err)
}
if err := routerStack.CreateNIC(routerNICID1, newEthernetEndpoint(routerNIC1)); err != nil {
t.Fatalf("routerStack.CreateNIC(%d, _): %s", routerNICID1, err)
}
if err := routerStack.CreateNIC(routerNICID2, newEthernetEndpoint(routerNIC2)); err != nil {
t.Fatalf("routerStack.CreateNIC(%d, _): %s", routerNICID2, err)
}
if err := host2Stack.CreateNIC(host2NICID, newEthernetEndpoint(host2NIC)); err != nil {
t.Fatalf("host2Stack.CreateNIC(%d, _): %s", host2NICID, err)
}
if err := routerStack.SetForwarding(ipv4.ProtocolNumber, true); err != nil {
t.Fatalf("routerStack.SetForwarding(%d): %s", ipv4.ProtocolNumber, err)
}
if err := routerStack.SetForwarding(ipv6.ProtocolNumber, true); err != nil {
t.Fatalf("routerStack.SetForwarding(%d): %s", ipv6.ProtocolNumber, err)
}
if err := host1Stack.AddProtocolAddress(host1NICID, host1IPv4Addr); err != nil {
t.Fatalf("host1Stack.AddProtocolAddress(%d, %#v): %s", host1NICID, host1IPv4Addr, err)
}
if err := routerStack.AddProtocolAddress(routerNICID1, routerNIC1IPv4Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", routerNICID1, routerNIC1IPv4Addr, err)
}
if err := routerStack.AddProtocolAddress(routerNICID2, routerNIC2IPv4Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", routerNICID2, routerNIC2IPv4Addr, err)
}
if err := host2Stack.AddProtocolAddress(host2NICID, host2IPv4Addr); err != nil {
t.Fatalf("host2Stack.AddProtocolAddress(%d, %#v): %s", host2NICID, host2IPv4Addr, err)
}
if err := host1Stack.AddProtocolAddress(host1NICID, host1IPv6Addr); err != nil {
t.Fatalf("host1Stack.AddProtocolAddress(%d, %#v): %s", host1NICID, host1IPv6Addr, err)
}
if err := routerStack.AddProtocolAddress(routerNICID1, routerNIC1IPv6Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", routerNICID1, routerNIC1IPv6Addr, err)
}
if err := routerStack.AddProtocolAddress(routerNICID2, routerNIC2IPv6Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", routerNICID2, routerNIC2IPv6Addr, err)
}
if err := host2Stack.AddProtocolAddress(host2NICID, host2IPv6Addr); err != nil {
t.Fatalf("host2Stack.AddProtocolAddress(%d, %#v): %s", host2NICID, host2IPv6Addr, err)
}
host1Stack.SetRouteTable([]tcpip.Route{
{
Destination: host1IPv4Addr.AddressWithPrefix.Subnet(),
NIC: host1NICID,
},
{
Destination: host1IPv6Addr.AddressWithPrefix.Subnet(),
NIC: host1NICID,
},
{
Destination: host2IPv4Addr.AddressWithPrefix.Subnet(),
Gateway: routerNIC1IPv4Addr.AddressWithPrefix.Address,
NIC: host1NICID,
},
{
Destination: host2IPv6Addr.AddressWithPrefix.Subnet(),
Gateway: routerNIC1IPv6Addr.AddressWithPrefix.Address,
NIC: host1NICID,
},
})
routerStack.SetRouteTable([]tcpip.Route{
{
Destination: routerNIC1IPv4Addr.AddressWithPrefix.Subnet(),
NIC: routerNICID1,
},
{
Destination: routerNIC1IPv6Addr.AddressWithPrefix.Subnet(),
NIC: routerNICID1,
},
{
Destination: routerNIC2IPv4Addr.AddressWithPrefix.Subnet(),
NIC: routerNICID2,
},
{
Destination: routerNIC2IPv6Addr.AddressWithPrefix.Subnet(),
NIC: routerNICID2,
},
})
host2Stack.SetRouteTable([]tcpip.Route{
{
Destination: host2IPv4Addr.AddressWithPrefix.Subnet(),
NIC: host2NICID,
},
{
Destination: host2IPv6Addr.AddressWithPrefix.Subnet(),
NIC: host2NICID,
},
{
Destination: host1IPv4Addr.AddressWithPrefix.Subnet(),
Gateway: routerNIC2IPv4Addr.AddressWithPrefix.Address,
NIC: host2NICID,
},
{
Destination: host1IPv6Addr.AddressWithPrefix.Subnet(),
Gateway: routerNIC2IPv6Addr.AddressWithPrefix.Address,
NIC: host2NICID,
},
})
}
// newEthernetEndpoint returns an ethernet link endpoint that wraps an inner
// link endpoint and checks the destination link address before delivering
// network packets to the network dispatcher.
//
// See ethernet.Endpoint for more details.
func newEthernetEndpoint(ep stack.LinkEndpoint) *endpointWithDestinationCheck {
var e endpointWithDestinationCheck
e.Endpoint.Init(ethernet.New(ep), &e)
return &e
}
// endpointWithDestinationCheck is a link endpoint that checks the destination
// link address before delivering network packets to the network dispatcher.
type endpointWithDestinationCheck struct {
nested.Endpoint
}
// DeliverNetworkPacket implements stack.NetworkDispatcher.
func (e *endpointWithDestinationCheck) DeliverNetworkPacket(src, dst tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
if dst == e.Endpoint.LinkAddress() || dst == header.EthernetBroadcastAddress || header.IsMulticastEthernetAddress(dst) {
e.Endpoint.DeliverNetworkPacket(src, dst, proto, pkt)
}
}
func TestForwarding(t *testing.T) {
const listenPort = 8080
@@ -278,11 +72,11 @@ func TestForwarding(t *testing.T) {
ep2, ep2WECH := newEP(t, host2Stack, proto, ipv4.ProtocolNumber)
return endpointAndAddresses{
serverEP: ep1,
serverAddr: host1IPv4Addr.AddressWithPrefix.Address,
serverAddr: utils.Host1IPv4Addr.AddressWithPrefix.Address,
serverReadableCH: ep1WECH,
clientEP: ep2,
clientAddr: host2IPv4Addr.AddressWithPrefix.Address,
clientAddr: utils.Host2IPv4Addr.AddressWithPrefix.Address,
clientReadableCH: ep2WECH,
}
},
@@ -294,11 +88,11 @@ func TestForwarding(t *testing.T) {
ep2, ep2WECH := newEP(t, host1Stack, proto, ipv6.ProtocolNumber)
return endpointAndAddresses{
serverEP: ep1,
serverAddr: host2IPv6Addr.AddressWithPrefix.Address,
serverAddr: utils.Host2IPv6Addr.AddressWithPrefix.Address,
serverReadableCH: ep1WECH,
clientEP: ep2,
clientAddr: host1IPv6Addr.AddressWithPrefix.Address,
clientAddr: utils.Host1IPv6Addr.AddressWithPrefix.Address,
clientReadableCH: ep2WECH,
}
},
@@ -310,11 +104,11 @@ func TestForwarding(t *testing.T) {
ep2, ep2WECH := newEP(t, routerStack, proto, ipv4.ProtocolNumber)
return endpointAndAddresses{
serverEP: ep1,
serverAddr: host2IPv4Addr.AddressWithPrefix.Address,
serverAddr: utils.Host2IPv4Addr.AddressWithPrefix.Address,
serverReadableCH: ep1WECH,
clientEP: ep2,
clientAddr: routerNIC1IPv4Addr.AddressWithPrefix.Address,
clientAddr: utils.RouterNIC1IPv4Addr.AddressWithPrefix.Address,
clientReadableCH: ep2WECH,
}
},
@@ -326,11 +120,11 @@ func TestForwarding(t *testing.T) {
ep2, ep2WECH := newEP(t, host1Stack, proto, ipv6.ProtocolNumber)
return endpointAndAddresses{
serverEP: ep1,
serverAddr: routerNIC2IPv6Addr.AddressWithPrefix.Address,
serverAddr: utils.RouterNIC2IPv6Addr.AddressWithPrefix.Address,
serverReadableCH: ep1WECH,
clientEP: ep2,
clientAddr: host1IPv6Addr.AddressWithPrefix.Address,
clientAddr: utils.Host1IPv6Addr.AddressWithPrefix.Address,
clientReadableCH: ep2WECH,
}
},
@@ -405,7 +199,7 @@ func TestForwarding(t *testing.T) {
host1Stack := stack.New(stackOpts)
routerStack := stack.New(stackOpts)
host2Stack := stack.New(stackOpts)
setupRoutedStacks(t, host1Stack, routerStack, host2Stack)
utils.SetupRoutedStacks(t, host1Stack, routerStack, host2Stack)
epsAndAddrs := test.epAndAddrs(t, host1Stack, routerStack, host2Stack, subTest.proto)
defer epsAndAddrs.serverEP.Close()
+9 -8
View File
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package integration_test
package iptables_test
import (
"testing"
@@ -24,6 +24,7 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/tests/utils"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
)
@@ -375,7 +376,7 @@ func TestIPTableWritePackets(t *testing.T) {
const (
nicID = 1
dropLocalPort = localPort - 1
dropLocalPort = utils.LocalPort - 1
acceptPackets = 2
dropPackets = 3
)
@@ -411,7 +412,7 @@ func TestIPTableWritePackets(t *testing.T) {
ReserveHeaderBytes: int(r.MaxHeaderLength() + header.UDPMinimumSize),
})
hdr := pkt.TransportHeader().Push(header.UDPMinimumSize)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, localPort, remotePort)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, utils.LocalPort, utils.RemotePort)
pkts.PushFront(pkt)
return pkts
@@ -473,7 +474,7 @@ func TestIPTableWritePackets(t *testing.T) {
ReserveHeaderBytes: int(r.MaxHeaderLength() + header.UDPMinimumSize),
})
hdr := pkt.TransportHeader().Push(header.UDPMinimumSize)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, localPort, remotePort)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, utils.LocalPort, utils.RemotePort)
pkts.PushFront(pkt)
}
for i := 0; i < dropPackets; i++ {
@@ -481,7 +482,7 @@ func TestIPTableWritePackets(t *testing.T) {
ReserveHeaderBytes: int(r.MaxHeaderLength() + header.UDPMinimumSize),
})
hdr := pkt.TransportHeader().Push(header.UDPMinimumSize)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, dropLocalPort, remotePort)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, dropLocalPort, utils.RemotePort)
pkts.PushFront(pkt)
}
@@ -502,7 +503,7 @@ func TestIPTableWritePackets(t *testing.T) {
ReserveHeaderBytes: int(r.MaxHeaderLength() + header.UDPMinimumSize),
})
hdr := pkt.TransportHeader().Push(header.UDPMinimumSize)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, localPort, remotePort)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, utils.LocalPort, utils.RemotePort)
pkts.PushFront(pkt)
return pkts
@@ -564,7 +565,7 @@ func TestIPTableWritePackets(t *testing.T) {
ReserveHeaderBytes: int(r.MaxHeaderLength() + header.UDPMinimumSize),
})
hdr := pkt.TransportHeader().Push(header.UDPMinimumSize)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, localPort, remotePort)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, utils.LocalPort, utils.RemotePort)
pkts.PushFront(pkt)
}
for i := 0; i < dropPackets; i++ {
@@ -572,7 +573,7 @@ func TestIPTableWritePackets(t *testing.T) {
ReserveHeaderBytes: int(r.MaxHeaderLength() + header.UDPMinimumSize),
})
hdr := pkt.TransportHeader().Push(header.UDPMinimumSize)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, dropLocalPort, remotePort)
udpHdr(hdr, r.LocalAddress, r.RemoteAddress, dropLocalPort, utils.RemotePort)
pkts.PushFront(pkt)
}
File diff suppressed because it is too large Load Diff
+18 -17
View File
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package integration_test
package loopback_test
import (
"bytes"
@@ -28,6 +28,7 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/tests/utils"
"gvisor.dev/gvisor/pkg/tcpip/transport/tcp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/waiter"
@@ -109,7 +110,7 @@ func TestLoopbackAcceptAllInSubnetUDP(t *testing.T) {
ipv4ProtocolAddress := tcpip.ProtocolAddress{
Protocol: header.IPv4ProtocolNumber,
AddressWithPrefix: ipv4Addr,
AddressWithPrefix: utils.Ipv4Addr,
}
ipv4Bytes := []byte(ipv4ProtocolAddress.AddressWithPrefix.Address)
ipv4Bytes[len(ipv4Bytes)-1]++
@@ -117,9 +118,9 @@ func TestLoopbackAcceptAllInSubnetUDP(t *testing.T) {
ipv6ProtocolAddress := tcpip.ProtocolAddress{
Protocol: header.IPv6ProtocolNumber,
AddressWithPrefix: ipv6Addr,
AddressWithPrefix: utils.Ipv6Addr,
}
ipv6Bytes := []byte(ipv6Addr.Address)
ipv6Bytes := []byte(utils.Ipv6Addr.Address)
ipv6Bytes[len(ipv6Bytes)-1]++
otherIPv6Address := tcpip.Address(ipv6Bytes)
@@ -145,7 +146,7 @@ func TestLoopbackAcceptAllInSubnetUDP(t *testing.T) {
{
name: "IPv4 bind to wildcard send to other address",
addAddress: ipv4ProtocolAddress,
dstAddr: remoteIPv4Addr,
dstAddr: utils.RemoteIPv4Addr,
expectRx: false,
},
{
@@ -173,8 +174,8 @@ func TestLoopbackAcceptAllInSubnetUDP(t *testing.T) {
{
name: "IPv6 bind and send to assigned address",
addAddress: ipv6ProtocolAddress,
bindAddr: ipv6Addr.Address,
dstAddr: ipv6Addr.Address,
bindAddr: utils.Ipv6Addr.Address,
dstAddr: utils.Ipv6Addr.Address,
expectRx: true,
},
{
@@ -277,9 +278,9 @@ func TestLoopbackSubnetLifetimeBoundToAddr(t *testing.T) {
protoAddr := tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: ipv4Addr,
AddressWithPrefix: utils.Ipv4Addr,
}
addrBytes := []byte(ipv4Addr.Address)
addrBytes := []byte(utils.Ipv4Addr.Address)
addrBytes[len(addrBytes)-1]++
otherAddr := tcpip.Address(addrBytes)
@@ -299,9 +300,9 @@ func TestLoopbackSubnetLifetimeBoundToAddr(t *testing.T) {
},
})
r, err := s.FindRoute(nicID, otherAddr, remoteIPv4Addr, ipv4.ProtocolNumber, false /* multicastLoop */)
r, err := s.FindRoute(nicID, otherAddr, utils.RemoteIPv4Addr, ipv4.ProtocolNumber, false /* multicastLoop */)
if err != nil {
t.Fatalf("s.FindRoute(%d, %s, %s, %d, false): %s", nicID, otherAddr, remoteIPv4Addr, ipv4.ProtocolNumber, err)
t.Fatalf("s.FindRoute(%d, %s, %s, %d, false): %s", nicID, otherAddr, utils.RemoteIPv4Addr, ipv4.ProtocolNumber, err)
}
defer r.Release()
@@ -344,7 +345,7 @@ func TestLoopbackAcceptAllInSubnetTCP(t *testing.T) {
ipv4ProtocolAddress := tcpip.ProtocolAddress{
Protocol: header.IPv4ProtocolNumber,
AddressWithPrefix: ipv4Addr,
AddressWithPrefix: utils.Ipv4Addr,
}
ipv4ProtocolAddress.AddressWithPrefix.PrefixLen = 8
ipv4Bytes := []byte(ipv4ProtocolAddress.AddressWithPrefix.Address)
@@ -353,9 +354,9 @@ func TestLoopbackAcceptAllInSubnetTCP(t *testing.T) {
ipv6ProtocolAddress := tcpip.ProtocolAddress{
Protocol: header.IPv6ProtocolNumber,
AddressWithPrefix: ipv6Addr,
AddressWithPrefix: utils.Ipv6Addr,
}
ipv6Bytes := []byte(ipv6Addr.Address)
ipv6Bytes := []byte(utils.Ipv6Addr.Address)
ipv6Bytes[len(ipv6Bytes)-1]++
otherIPv6Address := tcpip.Address(ipv6Bytes)
@@ -381,7 +382,7 @@ func TestLoopbackAcceptAllInSubnetTCP(t *testing.T) {
{
name: "IPv4 bind to wildcard send to other address",
addAddress: ipv4ProtocolAddress,
dstAddr: remoteIPv4Addr,
dstAddr: utils.RemoteIPv4Addr,
expectAccept: false,
},
{
@@ -409,8 +410,8 @@ func TestLoopbackAcceptAllInSubnetTCP(t *testing.T) {
{
name: "IPv6 bind and send to assigned address",
addAddress: ipv6ProtocolAddress,
bindAddr: ipv6Addr.Address,
dstAddr: ipv6Addr.Address,
bindAddr: utils.Ipv6Addr.Address,
dstAddr: utils.Ipv6Addr.Address,
expectAccept: true,
},
{
@@ -12,11 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package integration_test
package multicast_broadcast_test
import (
"bytes"
"net"
"testing"
"github.com/google/go-cmp/cmp"
@@ -29,6 +28,7 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/tests/utils"
"gvisor.dev/gvisor/pkg/tcpip/transport/icmp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/waiter"
@@ -37,29 +37,6 @@ import (
const (
defaultMTU = 1280
ttl = 255
remotePort = 5555
localPort = 80
)
var (
ipv4Addr = tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.1.58").To4()),
PrefixLen: 24,
}
ipv4Subnet = ipv4Addr.Subnet()
ipv4SubnetBcast = ipv4Subnet.Broadcast()
ipv6Addr = tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("200a::1").To16()),
PrefixLen: 64,
}
ipv6Subnet = ipv6Addr.Subnet()
ipv6SubnetBcast = ipv6Subnet.Broadcast()
// Remote addrs.
remoteIPv4Addr = tcpip.Address(net.ParseIP("10.0.0.1").To4())
remoteIPv6Addr = tcpip.Address(net.ParseIP("200b::1").To16())
)
// TestPingMulticastBroadcast tests that responding to an Echo Request destined
@@ -81,7 +58,7 @@ func TestPingMulticastBroadcast(t *testing.T) {
TotalLength: uint16(totalLen),
Protocol: uint8(icmp.ProtocolNumber4),
TTL: ttl,
SrcAddr: remoteIPv4Addr,
SrcAddr: utils.RemoteIPv4Addr,
DstAddr: dst,
})
ip.SetChecksum(^ip.CalculateChecksum())
@@ -98,13 +75,13 @@ func TestPingMulticastBroadcast(t *testing.T) {
pkt.SetType(header.ICMPv6EchoRequest)
pkt.SetCode(0)
pkt.SetChecksum(0)
pkt.SetChecksum(header.ICMPv6Checksum(pkt, remoteIPv6Addr, dst, buffer.VectorisedView{}))
pkt.SetChecksum(header.ICMPv6Checksum(pkt, utils.RemoteIPv6Addr, dst, buffer.VectorisedView{}))
ip := header.IPv6(hdr.Prepend(header.IPv6MinimumSize))
ip.Encode(&header.IPv6Fields{
PayloadLength: header.ICMPv6MinimumSize,
TransportProtocol: icmp.ProtocolNumber6,
HopLimit: ttl,
SrcAddr: remoteIPv6Addr,
SrcAddr: utils.RemoteIPv6Addr,
DstAddr: dst,
})
@@ -119,11 +96,11 @@ func TestPingMulticastBroadcast(t *testing.T) {
}{
{
name: "IPv4 unicast",
dstAddr: ipv4Addr.Address,
dstAddr: utils.Ipv4Addr.Address,
},
{
name: "IPv4 directed broadcast",
dstAddr: ipv4SubnetBcast,
dstAddr: utils.Ipv4SubnetBcast,
},
{
name: "IPv4 broadcast",
@@ -135,7 +112,7 @@ func TestPingMulticastBroadcast(t *testing.T) {
},
{
name: "IPv6 unicast",
dstAddr: ipv6Addr.Address,
dstAddr: utils.Ipv6Addr.Address,
},
{
name: "IPv6 all-nodes multicast",
@@ -154,11 +131,11 @@ func TestPingMulticastBroadcast(t *testing.T) {
if err := s.CreateNIC(nicID, e); err != nil {
t.Fatalf("CreateNIC(%d, _): %s", nicID, err)
}
ipv4ProtoAddr := tcpip.ProtocolAddress{Protocol: header.IPv4ProtocolNumber, AddressWithPrefix: ipv4Addr}
ipv4ProtoAddr := tcpip.ProtocolAddress{Protocol: header.IPv4ProtocolNumber, AddressWithPrefix: utils.Ipv4Addr}
if err := s.AddProtocolAddress(nicID, ipv4ProtoAddr); err != nil {
t.Fatalf("AddProtocolAddress(%d, %#v): %s", nicID, ipv4ProtoAddr, err)
}
ipv6ProtoAddr := tcpip.ProtocolAddress{Protocol: header.IPv6ProtocolNumber, AddressWithPrefix: ipv6Addr}
ipv6ProtoAddr := tcpip.ProtocolAddress{Protocol: header.IPv6ProtocolNumber, AddressWithPrefix: utils.Ipv6Addr}
if err := s.AddProtocolAddress(nicID, ipv6ProtoAddr); err != nil {
t.Fatalf("AddProtocolAddress(%d, %#v): %s", nicID, ipv6ProtoAddr, err)
}
@@ -183,13 +160,13 @@ func TestPingMulticastBroadcast(t *testing.T) {
switch l := len(test.dstAddr); l {
case header.IPv4AddressSize:
rxICMP = rxIPv4ICMP
expectedSrc = ipv4Addr.Address
expectedDst = remoteIPv4Addr
expectedSrc = utils.Ipv4Addr.Address
expectedDst = utils.RemoteIPv4Addr
protoNum = header.IPv4ProtocolNumber
case header.IPv6AddressSize:
rxICMP = rxIPv6ICMP
expectedSrc = ipv6Addr.Address
expectedDst = remoteIPv6Addr
expectedSrc = utils.Ipv6Addr.Address
expectedDst = utils.RemoteIPv6Addr
protoNum = header.IPv6ProtocolNumber
default:
t.Fatalf("got unexpected address length = %d bytes", l)
@@ -226,8 +203,8 @@ func rxIPv4UDP(e *channel.Endpoint, src, dst tcpip.Address, data []byte) {
hdr := buffer.NewPrependable(totalLen)
u := header.UDP(hdr.Prepend(payloadLen))
u.Encode(&header.UDPFields{
SrcPort: remotePort,
DstPort: localPort,
SrcPort: utils.RemotePort,
DstPort: utils.LocalPort,
Length: uint16(payloadLen),
})
copy(u.Payload(), data)
@@ -255,8 +232,8 @@ func rxIPv6UDP(e *channel.Endpoint, src, dst tcpip.Address, data []byte) {
hdr := buffer.NewPrependable(header.IPv6MinimumSize + payloadLen)
u := header.UDP(hdr.Prepend(payloadLen))
u.Encode(&header.UDPFields{
SrcPort: remotePort,
DstPort: localPort,
SrcPort: utils.RemotePort,
DstPort: utils.LocalPort,
Length: uint16(payloadLen),
})
copy(u.Payload(), data)
@@ -298,68 +275,68 @@ func TestIncomingMulticastAndBroadcast(t *testing.T) {
{
name: "IPv4 unicast binding to unicast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: ipv4Addr.Address,
dstAddr: ipv4Addr.Address,
bindAddr: utils.Ipv4Addr.Address,
dstAddr: utils.Ipv4Addr.Address,
expectRx: true,
},
{
name: "IPv4 unicast binding to broadcast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: header.IPv4Broadcast,
dstAddr: ipv4Addr.Address,
dstAddr: utils.Ipv4Addr.Address,
expectRx: false,
},
{
name: "IPv4 unicast binding to wildcard",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
dstAddr: ipv4Addr.Address,
dstAddr: utils.Ipv4Addr.Address,
expectRx: true,
},
{
name: "IPv4 directed broadcast binding to subnet broadcast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: ipv4SubnetBcast,
dstAddr: ipv4SubnetBcast,
bindAddr: utils.Ipv4SubnetBcast,
dstAddr: utils.Ipv4SubnetBcast,
expectRx: true,
},
{
name: "IPv4 directed broadcast binding to broadcast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: header.IPv4Broadcast,
dstAddr: ipv4SubnetBcast,
dstAddr: utils.Ipv4SubnetBcast,
expectRx: false,
},
{
name: "IPv4 directed broadcast binding to wildcard",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
dstAddr: ipv4SubnetBcast,
dstAddr: utils.Ipv4SubnetBcast,
expectRx: true,
},
{
name: "IPv4 broadcast binding to broadcast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: header.IPv4Broadcast,
dstAddr: header.IPv4Broadcast,
@@ -368,28 +345,28 @@ func TestIncomingMulticastAndBroadcast(t *testing.T) {
{
name: "IPv4 broadcast binding to subnet broadcast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: ipv4SubnetBcast,
bindAddr: utils.Ipv4SubnetBcast,
dstAddr: header.IPv4Broadcast,
expectRx: false,
},
{
name: "IPv4 broadcast binding to wildcard",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
dstAddr: ipv4SubnetBcast,
dstAddr: utils.Ipv4SubnetBcast,
expectRx: true,
},
{
name: "IPv4 all-systems multicast binding to all-systems multicast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: header.IPv4AllSystems,
dstAddr: header.IPv4AllSystems,
@@ -398,8 +375,8 @@ func TestIncomingMulticastAndBroadcast(t *testing.T) {
{
name: "IPv4 all-systems multicast binding to wildcard",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
dstAddr: header.IPv4AllSystems,
expectRx: true,
@@ -407,10 +384,10 @@ func TestIncomingMulticastAndBroadcast(t *testing.T) {
{
name: "IPv4 all-systems multicast binding to unicast",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
bindAddr: ipv4Addr.Address,
bindAddr: utils.Ipv4Addr.Address,
dstAddr: header.IPv4AllSystems,
expectRx: false,
},
@@ -418,19 +395,19 @@ func TestIncomingMulticastAndBroadcast(t *testing.T) {
// IPv6 has no notion of a broadcast.
{
name: "IPv6 unicast binding to wildcard",
dstAddr: ipv6Addr.Address,
dstAddr: utils.Ipv6Addr.Address,
proto: header.IPv6ProtocolNumber,
remoteAddr: remoteIPv6Addr,
localAddr: ipv6Addr,
remoteAddr: utils.RemoteIPv6Addr,
localAddr: utils.Ipv6Addr,
rxUDP: rxIPv6UDP,
expectRx: true,
},
{
name: "IPv6 broadcast-like address binding to wildcard",
dstAddr: ipv6SubnetBcast,
dstAddr: utils.Ipv6SubnetBcast,
proto: header.IPv6ProtocolNumber,
remoteAddr: remoteIPv6Addr,
localAddr: ipv6Addr,
remoteAddr: utils.RemoteIPv6Addr,
localAddr: utils.Ipv6Addr,
rxUDP: rxIPv6UDP,
expectRx: false,
},
@@ -458,7 +435,7 @@ func TestIncomingMulticastAndBroadcast(t *testing.T) {
}
defer ep.Close()
bindAddr := tcpip.FullAddress{Addr: test.bindAddr, Port: localPort}
bindAddr := tcpip.FullAddress{Addr: test.bindAddr, Port: utils.LocalPort}
if err := ep.Bind(bindAddr); err != nil {
t.Fatalf("ep.Bind(%#v): %s", bindAddr, err)
}
@@ -639,16 +616,16 @@ func TestUDPAddRemoveMembershipSocketOption(t *testing.T) {
name: "IPv4 unicast binding to unicast",
multicastAddr: "\xe0\x01\x02\x03",
proto: header.IPv4ProtocolNumber,
remoteAddr: remoteIPv4Addr,
localAddr: ipv4Addr,
remoteAddr: utils.RemoteIPv4Addr,
localAddr: utils.Ipv4Addr,
rxUDP: rxIPv4UDP,
},
{
name: "IPv6 broadcast-like address binding to wildcard",
multicastAddr: "\xff\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04",
proto: header.IPv6ProtocolNumber,
remoteAddr: remoteIPv6Addr,
localAddr: ipv6Addr,
remoteAddr: utils.RemoteIPv6Addr,
localAddr: utils.Ipv6Addr,
rxUDP: rxIPv6UDP,
},
}
@@ -719,7 +696,7 @@ func TestUDPAddRemoveMembershipSocketOption(t *testing.T) {
}
defer ep.Close()
bindAddr := tcpip.FullAddress{Port: localPort}
bindAddr := tcpip.FullAddress{Port: utils.LocalPort}
if err := ep.Bind(bindAddr); err != nil {
t.Fatalf("ep.Bind(%#v): %s", bindAddr, err)
}
+8 -7
View File
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package integration_test
package route_test
import (
"bytes"
@@ -28,6 +28,7 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
"gvisor.dev/gvisor/pkg/tcpip/tests/utils"
"gvisor.dev/gvisor/pkg/tcpip/transport/icmp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/waiter"
@@ -107,7 +108,7 @@ func TestLocalPing(t *testing.T) {
transProto: icmp.ProtocolNumber4,
netProto: ipv4.ProtocolNumber,
linkEndpoint: channelEP,
localAddr: ipv4Addr.Address,
localAddr: utils.Ipv4Addr.Address,
icmpBuf: ipv4ICMPBuf,
checkLinkEndpoint: channelEPCheck,
},
@@ -116,7 +117,7 @@ func TestLocalPing(t *testing.T) {
transProto: icmp.ProtocolNumber6,
netProto: ipv6.ProtocolNumber,
linkEndpoint: channelEP,
localAddr: ipv6Addr.Address,
localAddr: utils.Ipv6Addr.Address,
icmpBuf: ipv6ICMPBuf,
checkLinkEndpoint: channelEPCheck,
},
@@ -253,13 +254,13 @@ func TestLocalUDP(t *testing.T) {
}{
{
name: "IPv4",
canBePrimaryAddr: ipv4Addr1,
firstPrimaryAddr: ipv4Addr2,
canBePrimaryAddr: utils.Ipv4Addr1,
firstPrimaryAddr: utils.Ipv4Addr2,
},
{
name: "IPv6",
canBePrimaryAddr: ipv6Addr1,
firstPrimaryAddr: ipv6Addr2,
canBePrimaryAddr: utils.Ipv6Addr1,
firstPrimaryAddr: utils.Ipv6Addr2,
},
}
+19
View File
@@ -0,0 +1,19 @@
load("//tools:defs.bzl", "go_library")
package(licenses = ["notice"])
go_library(
name = "utils",
srcs = ["utils.go"],
visibility = ["//pkg/tcpip/tests:__subpackages__"],
deps = [
"//pkg/tcpip",
"//pkg/tcpip/header",
"//pkg/tcpip/link/ethernet",
"//pkg/tcpip/link/nested",
"//pkg/tcpip/link/pipe",
"//pkg/tcpip/network/ipv4",
"//pkg/tcpip/network/ipv6",
"//pkg/tcpip/stack",
],
)
+314
View File
@@ -0,0 +1,314 @@
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package utils holds common testing utilities for tcpip.
package utils
import (
"net"
"testing"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/ethernet"
"gvisor.dev/gvisor/pkg/tcpip/link/nested"
"gvisor.dev/gvisor/pkg/tcpip/link/pipe"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
// Common NIC IDs used by tests.
const (
Host1NICID = 1
RouterNICID1 = 2
RouterNICID2 = 3
Host2NICID = 4
)
// Common link addresses used by tests.
const (
LinkAddr1 = tcpip.LinkAddress("\x02\x03\x03\x04\x05\x06")
LinkAddr2 = tcpip.LinkAddress("\x02\x03\x03\x04\x05\x07")
LinkAddr3 = tcpip.LinkAddress("\x02\x03\x03\x04\x05\x08")
LinkAddr4 = tcpip.LinkAddress("\x02\x03\x03\x04\x05\x09")
)
// Common IP addresses used by tests.
var (
Ipv4Addr = tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.1.58").To4()),
PrefixLen: 24,
}
Ipv4Subnet = Ipv4Addr.Subnet()
Ipv4SubnetBcast = Ipv4Subnet.Broadcast()
Ipv6Addr = tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("200a::1").To16()),
PrefixLen: 64,
}
Ipv6Subnet = Ipv6Addr.Subnet()
Ipv6SubnetBcast = Ipv6Subnet.Broadcast()
Ipv4Addr1 = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.0.1").To4()),
PrefixLen: 24,
},
}
Ipv4Addr2 = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.0.2").To4()),
PrefixLen: 8,
},
}
Ipv4Addr3 = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.0.3").To4()),
PrefixLen: 8,
},
}
Ipv6Addr1 = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("a::1").To16()),
PrefixLen: 64,
},
}
Ipv6Addr2 = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("a::2").To16()),
PrefixLen: 64,
},
}
Ipv6Addr3 = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("a::3").To16()),
PrefixLen: 64,
},
}
// Remote addrs.
RemoteIPv4Addr = tcpip.Address(net.ParseIP("10.0.0.1").To4())
RemoteIPv6Addr = tcpip.Address(net.ParseIP("200b::1").To16())
)
// Common ports for testing.
const (
RemotePort = 5555
LocalPort = 80
)
// Common IP addresses used for testing.
var (
Host1IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.0.2").To4()),
PrefixLen: 24,
},
}
RouterNIC1IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("192.168.0.1").To4()),
PrefixLen: 24,
},
}
RouterNIC2IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("10.0.0.1").To4()),
PrefixLen: 8,
},
}
Host2IPv4Addr = tcpip.ProtocolAddress{
Protocol: ipv4.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("10.0.0.2").To4()),
PrefixLen: 8,
},
}
Host1IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("a::2").To16()),
PrefixLen: 64,
},
}
RouterNIC1IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("a::1").To16()),
PrefixLen: 64,
},
}
RouterNIC2IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("b::1").To16()),
PrefixLen: 64,
},
}
Host2IPv6Addr = tcpip.ProtocolAddress{
Protocol: ipv6.ProtocolNumber,
AddressWithPrefix: tcpip.AddressWithPrefix{
Address: tcpip.Address(net.ParseIP("b::2").To16()),
PrefixLen: 64,
},
}
)
// NewEthernetEndpoint returns an ethernet link endpoint that wraps an inner
// link endpoint and checks the destination link address before delivering
// network packets to the network dispatcher.
//
// See ethernet.Endpoint for more details.
func NewEthernetEndpoint(ep stack.LinkEndpoint) *EndpointWithDestinationCheck {
var e EndpointWithDestinationCheck
e.Endpoint.Init(ethernet.New(ep), &e)
return &e
}
// EndpointWithDestinationCheck is a link endpoint that checks the destination
// link address before delivering network packets to the network dispatcher.
type EndpointWithDestinationCheck struct {
nested.Endpoint
}
var _ stack.NetworkDispatcher = (*EndpointWithDestinationCheck)(nil)
var _ stack.LinkEndpoint = (*EndpointWithDestinationCheck)(nil)
// DeliverNetworkPacket implements stack.NetworkDispatcher.
func (e *EndpointWithDestinationCheck) DeliverNetworkPacket(src, dst tcpip.LinkAddress, proto tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
if dst == e.Endpoint.LinkAddress() || dst == header.EthernetBroadcastAddress || header.IsMulticastEthernetAddress(dst) {
e.Endpoint.DeliverNetworkPacket(src, dst, proto, pkt)
}
}
// SetupRoutedStacks creates the NICs, sets forwarding, adds addresses and sets
// the route tables for the passed stacks.
func SetupRoutedStacks(t *testing.T, host1Stack, routerStack, host2Stack *stack.Stack) {
host1NIC, routerNIC1 := pipe.New(LinkAddr1, LinkAddr2)
routerNIC2, host2NIC := pipe.New(LinkAddr3, LinkAddr4)
if err := host1Stack.CreateNIC(Host1NICID, NewEthernetEndpoint(host1NIC)); err != nil {
t.Fatalf("host1Stack.CreateNIC(%d, _): %s", Host1NICID, err)
}
if err := routerStack.CreateNIC(RouterNICID1, NewEthernetEndpoint(routerNIC1)); err != nil {
t.Fatalf("routerStack.CreateNIC(%d, _): %s", RouterNICID1, err)
}
if err := routerStack.CreateNIC(RouterNICID2, NewEthernetEndpoint(routerNIC2)); err != nil {
t.Fatalf("routerStack.CreateNIC(%d, _): %s", RouterNICID2, err)
}
if err := host2Stack.CreateNIC(Host2NICID, NewEthernetEndpoint(host2NIC)); err != nil {
t.Fatalf("host2Stack.CreateNIC(%d, _): %s", Host2NICID, err)
}
if err := routerStack.SetForwarding(ipv4.ProtocolNumber, true); err != nil {
t.Fatalf("routerStack.SetForwarding(%d): %s", ipv4.ProtocolNumber, err)
}
if err := routerStack.SetForwarding(ipv6.ProtocolNumber, true); err != nil {
t.Fatalf("routerStack.SetForwarding(%d): %s", ipv6.ProtocolNumber, err)
}
if err := host1Stack.AddProtocolAddress(Host1NICID, Host1IPv4Addr); err != nil {
t.Fatalf("host1Stack.AddProtocolAddress(%d, %#v): %s", Host1NICID, Host1IPv4Addr, err)
}
if err := routerStack.AddProtocolAddress(RouterNICID1, RouterNIC1IPv4Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", RouterNICID1, RouterNIC1IPv4Addr, err)
}
if err := routerStack.AddProtocolAddress(RouterNICID2, RouterNIC2IPv4Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", RouterNICID2, RouterNIC2IPv4Addr, err)
}
if err := host2Stack.AddProtocolAddress(Host2NICID, Host2IPv4Addr); err != nil {
t.Fatalf("host2Stack.AddProtocolAddress(%d, %#v): %s", Host2NICID, Host2IPv4Addr, err)
}
if err := host1Stack.AddProtocolAddress(Host1NICID, Host1IPv6Addr); err != nil {
t.Fatalf("host1Stack.AddProtocolAddress(%d, %#v): %s", Host1NICID, Host1IPv6Addr, err)
}
if err := routerStack.AddProtocolAddress(RouterNICID1, RouterNIC1IPv6Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", RouterNICID1, RouterNIC1IPv6Addr, err)
}
if err := routerStack.AddProtocolAddress(RouterNICID2, RouterNIC2IPv6Addr); err != nil {
t.Fatalf("routerStack.AddProtocolAddress(%d, %#v): %s", RouterNICID2, RouterNIC2IPv6Addr, err)
}
if err := host2Stack.AddProtocolAddress(Host2NICID, Host2IPv6Addr); err != nil {
t.Fatalf("host2Stack.AddProtocolAddress(%d, %#v): %s", Host2NICID, Host2IPv6Addr, err)
}
host1Stack.SetRouteTable([]tcpip.Route{
{
Destination: Host1IPv4Addr.AddressWithPrefix.Subnet(),
NIC: Host1NICID,
},
{
Destination: Host1IPv6Addr.AddressWithPrefix.Subnet(),
NIC: Host1NICID,
},
{
Destination: Host2IPv4Addr.AddressWithPrefix.Subnet(),
Gateway: RouterNIC1IPv4Addr.AddressWithPrefix.Address,
NIC: Host1NICID,
},
{
Destination: Host2IPv6Addr.AddressWithPrefix.Subnet(),
Gateway: RouterNIC1IPv6Addr.AddressWithPrefix.Address,
NIC: Host1NICID,
},
})
routerStack.SetRouteTable([]tcpip.Route{
{
Destination: RouterNIC1IPv4Addr.AddressWithPrefix.Subnet(),
NIC: RouterNICID1,
},
{
Destination: RouterNIC1IPv6Addr.AddressWithPrefix.Subnet(),
NIC: RouterNICID1,
},
{
Destination: RouterNIC2IPv4Addr.AddressWithPrefix.Subnet(),
NIC: RouterNICID2,
},
{
Destination: RouterNIC2IPv6Addr.AddressWithPrefix.Subnet(),
NIC: RouterNICID2,
},
})
host2Stack.SetRouteTable([]tcpip.Route{
{
Destination: Host2IPv4Addr.AddressWithPrefix.Subnet(),
NIC: Host2NICID,
},
{
Destination: Host2IPv6Addr.AddressWithPrefix.Subnet(),
NIC: Host2NICID,
},
{
Destination: Host1IPv4Addr.AddressWithPrefix.Subnet(),
Gateway: RouterNIC2IPv4Addr.AddressWithPrefix.Address,
NIC: Host2NICID,
},
{
Destination: Host1IPv6Addr.AddressWithPrefix.Subnet(),
Gateway: RouterNIC2IPv6Addr.AddressWithPrefix.Address,
NIC: Host2NICID,
},
})
}