diff --git a/client/local/serve.go b/client/local/serve.go index c6e96ec07..98538e628 100644 --- a/client/local/serve.go +++ b/client/local/serve.go @@ -1,8 +1,6 @@ // Copyright (c) Tailscale Inc & contributors // SPDX-License-Identifier: BSD-3-Clause -//go:build !ts_omit_serve - package local import ( @@ -11,6 +9,8 @@ import ( "fmt" "net/http" + "tailscale.com/feature" + "tailscale.com/feature/buildfeatures" "tailscale.com/ipn" ) @@ -21,6 +21,9 @@ import ( // API maturity: this method is not considered a stable API and is // subject to change between releases. func (lc *Client) GetServeConfig(ctx context.Context) (*ipn.ServeConfig, error) { + if !buildfeatures.HasServe { + return nil, feature.ErrUnavailable + } body, h, err := lc.sendWithHeaders(ctx, "GET", "/localapi/v0/serve-config", 200, nil, nil) if err != nil { return nil, fmt.Errorf("getting serve config: %w", err) @@ -46,6 +49,9 @@ func getServeConfigFromJSON(body []byte) (sc *ipn.ServeConfig, err error) { // SetServeConfig sets or replaces the serving settings. // If config is nil, settings are cleared and serving is disabled. func (lc *Client) SetServeConfig(ctx context.Context, config *ipn.ServeConfig) error { + if !buildfeatures.HasServe { + return feature.ErrUnavailable + } h := make(http.Header) if config != nil { h.Set("If-Match", config.ETag) diff --git a/cmd/featuretags/featuretags.go b/cmd/featuretags/featuretags.go index 0fbafae86..a9aeead08 100644 --- a/cmd/featuretags/featuretags.go +++ b/cmd/featuretags/featuretags.go @@ -24,6 +24,7 @@ var ( list = flag.Bool("list", false, "if true, list all known features and what they do") extraSmall = flag.Bool("extra-small", false, "shorthand for build_dist.sh --extra-small's tags: --min --add="+joinFeatures(featuretags.ExtraSmall)) + tsnet = flag.Bool("tsnet", false, "keep the features tsnet requires ("+joinFeatures(featuretags.TSNetMin)+"): with --min, add them; with --remove, refuse to remove them") ) func joinFeatures(fts []featuretags.FeatureTag) string { @@ -55,6 +56,9 @@ func main() { } var keep []featuretags.FeatureTag + if *tsnet { + keep = append(keep, featuretags.TSNetMin...) + } for t := range strings.SplitSeq(*add, ",") { if t != "" { keep = append(keep, featuretags.FeatureTag(t)) @@ -80,6 +84,11 @@ func main() { removeSet.Add(f) } for ft := range removeSet { + if *tsnet && slices.ContainsFunc(featuretags.TSNetMin, func(need featuretags.FeatureTag) bool { + return featuretags.Requires(need).Contains(ft) + }) { + log.Fatalf("cannot remove %q with --tsnet; tsnet requires it", ft) + } set := featuretags.RequiredBy(ft) for dependent := range set { if !removeSet.Contains(dependent) { diff --git a/feature/README.md b/feature/README.md index a38e67455..0c8689e8f 100644 --- a/feature/README.md +++ b/feature/README.md @@ -402,7 +402,9 @@ Tests come in three flavors: - `cmd/featuretags` constructs valid sets of Go omit build tags. Start from a minimal build and add features with `--min --add=...`, or start from a full build and remove with `--remove=...`; either way it - respects the declared dependency DAG. + respects the declared dependency DAG. Add `--tsnet` to keep the + features `tsnet` requires (`featuretags.TSNetMin`); tsnet's + `TestTypeCheckFeatureSets` locks in that tsnet builds with them. - `build_dist.sh` uses `cmd/featuretags` under the hood, so you do not need to hand-maintain its build-tag lists as new features are added. diff --git a/feature/featuretags/featuretags.go b/feature/featuretags/featuretags.go index fccc6c4f0..6f41ef7d0 100644 --- a/feature/featuretags/featuretags.go +++ b/feature/featuretags/featuretags.go @@ -71,6 +71,11 @@ func RequiredBy(ft FeatureTag) set.Set[FeatureTag] { // useful tailscaled. Pass it to [MinTags] to get its build tags. var ExtraSmall = []FeatureTag{"osrouter", "nattraversal"} +// TSNetMin is the set of features that [tailscale.com/tsnet] requires. +// Pass it to [MinTags] to get the build tags for the smallest tsnet +// build. Omitting any of these breaks tsnet. +var TSNetMin = []FeatureTag{"netstack"} + // MinTags returns the sorted Go build tags for a minimal build that // includes only the features in keep and the features they require. // Every other omittable feature in [Features] gets its ts_omit_ tag, and diff --git a/ipn/ipnlocal/serve_disabled.go b/ipn/ipnlocal/serve_disabled.go index 7123ebe3b..76b0788e8 100644 --- a/ipn/ipnlocal/serve_disabled.go +++ b/ipn/ipnlocal/serve_disabled.go @@ -12,6 +12,7 @@ package ipnlocal import ( + "tailscale.com/feature" "tailscale.com/ipn" "tailscale.com/tailcfg" ) @@ -20,6 +21,14 @@ const serveEnabled = false type localListener = struct{} +func (b *LocalBackend) ServeConfigETag() (ipn.ServeConfigView, string, error) { + return ipn.ServeConfigView{}, "", feature.ErrUnavailable +} + +func (b *LocalBackend) SetServeConfig(config *ipn.ServeConfig, etag string) error { + return feature.ErrUnavailable +} + func (b *LocalBackend) DeleteForegroundSession(sessionID string) error { return nil } diff --git a/tsnet/tsnet.go b/tsnet/tsnet.go index 6bdff020e..2179920c1 100644 --- a/tsnet/tsnet.go +++ b/tsnet/tsnet.go @@ -186,6 +186,7 @@ import ( "tailscale.com/client/local" "tailscale.com/control/controlclient" "tailscale.com/envknob" + "tailscale.com/feature" "tailscale.com/feature/buildfeatures" _ "tailscale.com/feature/c2n" _ "tailscale.com/feature/condregister/netlog" @@ -617,8 +618,10 @@ func (s *Server) Up(ctx context.Context) (*ipnstate.Status, error) { var srvCfgErr error var svcAdErr error s.resetServeStateOnce.Do(func() { - if err := lc.SetServeConfig(ctx, new(ipn.ServeConfig)); err != nil { - srvCfgErr = fmt.Errorf("clearing serve config: %w", err) + if buildfeatures.HasServe { + if err := lc.SetServeConfig(ctx, new(ipn.ServeConfig)); err != nil { + srvCfgErr = fmt.Errorf("clearing serve config: %w", err) + } } _, err := s.lb.EditPrefs(&ipn.MaskedPrefs{ AdvertiseServicesSet: true, @@ -1563,6 +1566,9 @@ func FunnelTLSConfig(conf *tls.Config) FunnelOption { // // It will start the server if it has not been started yet. func (s *Server) ListenFunnel(network, addr string, opts ...FunnelOption) (net.Listener, error) { + if !buildfeatures.HasServe { + return nil, feature.ErrUnavailable + } if network != "tcp" { return nil, fmt.Errorf("ListenFunnel(%q, %q): only tcp is supported", network, addr) } @@ -1924,6 +1930,9 @@ func (s *Server) decrementServiceAdvertisementLocked(name tailcfg.ServiceName) e // // This function will start the server if it is not already started. func (s *Server) ListenService(name string, mode ServiceMode) (*ServiceListener, error) { + if !buildfeatures.HasServe { + return nil, feature.ErrUnavailable + } svcName := tailcfg.ServiceName(name) if err := svcName.Validate(); err != nil { return nil, err diff --git a/tsnet/tsnet_test.go b/tsnet/tsnet_test.go index bfb7725c0..e9cec85f5 100644 --- a/tsnet/tsnet_test.go +++ b/tsnet/tsnet_test.go @@ -43,8 +43,12 @@ import ( "github.com/prometheus/common/model" "github.com/tailscale/wireguard-go/tun" "golang.org/x/net/proxy" + "golang.org/x/tools/go/packages" "tailscale.com/cmd/testwrapper/flakytest" + "tailscale.com/feature" + "tailscale.com/feature/buildfeatures" + "tailscale.com/feature/featuretags" "tailscale.com/internal/client/tailscale" "tailscale.com/ipn" "tailscale.com/ipn/ipnlocal" @@ -926,7 +930,26 @@ func TestStartStopStartGetsSameIP(t *testing.T) { } } +func TestServeUnavailable(t *testing.T) { + if buildfeatures.HasServe { + t.Skip("requires ts_omit_serve") + } + + // Neither call should try to start the server when Serve is omitted. + s := &Server{} + s.initOnce.Do(func() { s.initErr = errors.New("unexpected server startup") }) + if ln, err := s.ListenFunnel("tcp", ":443"); ln != nil || !errors.Is(err, feature.ErrUnavailable) { + t.Errorf("ListenFunnel = %v, %v; want nil, %v", ln, err, feature.ErrUnavailable) + } + if ln, err := s.ListenService("svc:test", ServiceModeTCP{Port: 80}); ln != nil || !errors.Is(err, feature.ErrUnavailable) { + t.Errorf("ListenService = %v, %v; want nil, %v", ln, err, feature.ErrUnavailable) + } +} + func TestFunnel(t *testing.T) { + if !buildfeatures.HasServe { + t.Skip("requires Serve") + } ctx, dialCancel := context.WithTimeout(context.Background(), 30*time.Second) defer dialCancel() @@ -1010,6 +1033,9 @@ func TestFunnel(t *testing.T) { // after itself when closed. Specifically, changes made to the serve config // should be cleared. func TestFunnelClose(t *testing.T) { + if !buildfeatures.HasServe { + t.Skip("requires Serve") + } marshalServeConfig := func(t *testing.T, sc ipn.ServeConfigView) string { t.Helper() @@ -1195,6 +1221,9 @@ func setUpServiceState(t *testing.T, name, ip string, host, client *Server, } func TestListenService(t *testing.T) { + if !buildfeatures.HasServe { + t.Skip("requires Serve") + } type dialFn func(context.Context, string, string) (net.Conn, error) @@ -1590,6 +1619,9 @@ func TestListenService(t *testing.T) { } func TestListenServiceClose(t *testing.T) { + if !buildfeatures.HasServe { + t.Skip("requires Serve") + } const serviceName = "svc:foo" diffServeConfig := func(a, b ipn.ServeConfigView) string { @@ -3549,6 +3581,72 @@ func TestDeps(t *testing.T) { }.Check(t) } +// TestTypeCheckFeatureSets verifies that tsnet type-checks with only +// [featuretags.TSNetMin] and with a few other plausible feature sets. +// It type-checks from source rather than compiling, which is many +// times faster and doesn't depend on a warm build cache. +func TestTypeCheckFeatureSets(t *testing.T) { + // minTags returns the build tags for a tsnet build with only the + // features in keep plus [featuretags.TSNetMin]. + minTags := func(keep ...featuretags.FeatureTag) string { + return strings.Join(featuretags.MinTags(append(keep, featuretags.TSNetMin...)...), ",") + } + // withoutTags returns the build tags for a default build without + // the features in remove or anything that depends on them. + withoutTags := func(remove ...featuretags.FeatureTag) string { + var tags []string + for _, ft := range remove { + for dep := range featuretags.RequiredBy(ft) { + tags = append(tags, dep.OmitTag()) + } + } + slices.Sort(tags) + return strings.Join(slices.Compact(tags), ",") + } + + allPlatforms := []string{"linux/amd64", "darwin/arm64", "windows/amd64", "android/arm64"} + linuxOnly := []string{"linux/amd64"} + tests := []struct { + name string + platforms []string + tags string + }{ + {"min", allPlatforms, minTags()}, + {"min+acme+serve", linuxOnly, minTags("acme", "serve")}, + {"min+nattraversal+portmapper", linuxOnly, minTags("nattraversal", "portmapper")}, + {"no-serve", linuxOnly, withoutTags("serve")}, + } + // Each load type-checks hundreds of packages, std included, using + // all CPUs, so running more than a couple at once only adds memory. + sem := make(chan bool, 2) + for _, tt := range tests { + for _, plat := range tt.platforms { + goos, goarch, _ := strings.Cut(plat, "/") + t.Run(tt.name+"/"+goos, func(t *testing.T) { + t.Parallel() + sem <- true + defer func() { <-sem }() + pkgs, err := packages.Load(&packages.Config{ + Mode: packages.NeedName | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax, + BuildFlags: []string{"-tags=" + tt.tags}, + Env: append(os.Environ(), "GOOS="+goos, "GOARCH="+goarch, "CGO_ENABLED=0"), + }, "tailscale.com/tsnet") + if err != nil { + t.Fatal(err) + } + packages.Visit(pkgs, nil, func(p *packages.Package) { + for _, e := range p.Errors { + t.Error(e) + } + }) + if t.Failed() { + t.Logf("tags: %s", tt.tags) + } + }) + } + } +} + func TestResolveAuthKey(t *testing.T) { tests := []struct { name string