From d463f4e1d221ef6879b0b5023fb23ed56dc03135 Mon Sep 17 00:00:00 2001 From: Jamie Liu Date: Thu, 15 Aug 2024 17:57:07 -0700 Subject: [PATCH] tcpip: ensure fdbased/mmap_stub.go uses a stateify-recognized suffix Our Bazel build infrastructure explicitly recognizes some suffixes that can appear at the end of filenames (tools/bazeldefs/tags.bzl). It uses these tags to group files passed to go_stateify (tools/defs.bzl:go_library() => calculate_sets()). Each invocation of go_stateify infers build tags from its input files. Before this CL, all Go files in pkg/tcpip/link/fdbased (except for fdbased_unsafe.go) are grouped together to generate fdbased_state_autogen.go, so go_stateify infers builds tags "!linux !amd64,!arm64" (from mmap_stub.go) and "linux ..." (from most other files); these contradictory build tags mean that fdbased_state_autogen.go is never actually compiled, preventing its types from being registered. After this CL, mmap_nonlinux.go independently generates fdbased_nonlinux_state_autogen.go. Fixing this causes fdbased_state_autogen.go to be included for the first time, exposing a second problem: ``` pkg/tcpip/link/fdbased/endpoint_test.go:80:6: context already declared through import of package context ("context") .../pkg/tcpip/link/fdbased/fdbased_state_autogen.go:12:2: other declaration of context ``` (Apparently Go imports are a pessimal hybrid of "shared between files in a package" and "not shared between files in a package".) To fix this, rename the test type to testContext. PiperOrigin-RevId: 663522480 --- pkg/tcpip/link/fdbased/BUILD | 2 +- pkg/tcpip/link/fdbased/endpoint_test.go | 12 ++++++------ .../link/fdbased/{mmap_stub.go => mmap_nonlinux.go} | 0 tools/bazeldefs/tags.bzl | 1 + 4 files changed, 8 insertions(+), 7 deletions(-) rename pkg/tcpip/link/fdbased/{mmap_stub.go => mmap_nonlinux.go} (100%) diff --git a/pkg/tcpip/link/fdbased/BUILD b/pkg/tcpip/link/fdbased/BUILD index 0f82177ce..2f8b0d998 100644 --- a/pkg/tcpip/link/fdbased/BUILD +++ b/pkg/tcpip/link/fdbased/BUILD @@ -11,7 +11,7 @@ go_library( "endpoint.go", "endpoint_unsafe.go", "mmap.go", - "mmap_stub.go", + "mmap_nonlinux.go", "mmap_unsafe.go", "packet_dispatchers.go", "processors.go", diff --git a/pkg/tcpip/link/fdbased/endpoint_test.go b/pkg/tcpip/link/fdbased/endpoint_test.go index 601a6308e..c06bf1c25 100644 --- a/pkg/tcpip/link/fdbased/endpoint_test.go +++ b/pkg/tcpip/link/fdbased/endpoint_test.go @@ -77,7 +77,7 @@ func checkPacketInfoEqual(t *testing.T, got, want packetInfo) { } } -type context struct { +type testContext struct { t *testing.T readFDs []int writeFDs []int @@ -86,7 +86,7 @@ type context struct { done chan struct{} } -func newContext(t *testing.T, opt *Options) *context { +func newContext(t *testing.T, opt *Options) *testContext { firstFDPair, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_SEQPACKET, 0) if err != nil { t.Fatalf("Socketpair failed: %v", err) @@ -107,7 +107,7 @@ func newContext(t *testing.T, opt *Options) *context { t.Fatalf("Failed to create FD endpoint: %v", err) } - c := &context{ + c := &testContext{ t: t, readFDs: []int{firstFDPair[0], secondFDPair[0]}, writeFDs: opt.FDs, @@ -121,7 +121,7 @@ func newContext(t *testing.T, opt *Options) *context { return c } -func (c *context) cleanup() { +func (c *testContext) cleanup() { for _, fd := range c.readFDs { unix.Close(fd) } @@ -132,12 +132,12 @@ func (c *context) cleanup() { } } -func (c *context) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { +func (c *testContext) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { pkt.IncRef() c.ch <- packetInfo{protocol, pkt} } -func (c *context) DeliverLinkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) { +func (c *testContext) DeliverLinkPacket(tcpip.NetworkProtocolNumber, *stack.PacketBuffer) { c.t.Fatal("DeliverLinkPacket not implemented") } diff --git a/pkg/tcpip/link/fdbased/mmap_stub.go b/pkg/tcpip/link/fdbased/mmap_nonlinux.go similarity index 100% rename from pkg/tcpip/link/fdbased/mmap_stub.go rename to pkg/tcpip/link/fdbased/mmap_nonlinux.go diff --git a/tools/bazeldefs/tags.bzl b/tools/bazeldefs/tags.bzl index f2257c784..1fe6987eb 100644 --- a/tools/bazeldefs/tags.bzl +++ b/tools/bazeldefs/tags.bzl @@ -41,6 +41,7 @@ archs = [ oses = [ "_linux", + "_nonlinux", ] generic = [