diff --git a/.github/workflows/build-syncthing.yaml b/.github/workflows/build-syncthing.yaml index 3b974f3c1..db08fa85a 100644 --- a/.github/workflows/build-syncthing.yaml +++ b/.github/workflows/build-syncthing.yaml @@ -737,7 +737,7 @@ jobs: - uses: ruby/setup-ruby@eab2afb99481ca09a4e91171a8e0aee0e89bfedd # v1 with: - ruby-version: '3.0' + ruby-version: '3.4' - name: Install fpm run: | diff --git a/cmd/infra/stdht/main.go b/cmd/infra/stdht/main.go index 5a62186f1..363bef11e 100644 --- a/cmd/infra/stdht/main.go +++ b/cmd/infra/stdht/main.go @@ -28,23 +28,27 @@ import ( "github.com/syncthing/syncthing/internal/db" "github.com/syncthing/syncthing/internal/db/sqlite" "github.com/syncthing/syncthing/internal/dht" + "github.com/syncthing/syncthing/internal/slogutil" "github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/protocol" ) type CLI struct { - Listen []string `help:"QUIC listen multiaddresses" default:"/ip4/0.0.0.0/udp/22026/quic-v1,/ip6/::/udp/22026/quic-v1" env:"STDHT_LISTEN"` - Advertise []string `help:"Advertised multiaddresses (default: detected listener addresses)" env:"STDHT_ADVERTISE"` - Bootstrap []string `help:"Bootstrap multiaddresses including /p2p/peer-ID; empty uses Syncthing defaults" env:"STDHT_BOOTSTRAP"` - KeyFile string `help:"Base64-encoded libp2p private key file" default:"identity.key" env:"STDHT_KEY_FILE"` - Database string `help:"Database directory for DHT values and routing table" default:"/tmp/stdht-db" env:"STDHT_DATABASE"` - MetricsListen string `help:"HTTP metrics and health address; empty disables" default:"127.0.0.1:9010" env:"STDHT_METRICS_LISTEN"` - MaxConnections int `help:"Maximum DHT connections" default:"16000" env:"STDHT_MAX_CONNECTIONS"` - Version bool `short:"v" help:"Print version and exit"` + Listen []string `help:"QUIC listen multiaddresses" default:"/ip4/0.0.0.0/udp/22026/quic-v1,/ip6/::/udp/22026/quic-v1" env:"STDHT_LISTEN"` + Advertise []string `help:"Advertised multiaddresses (default: detected listener addresses)" env:"STDHT_ADVERTISE"` + Bootstrap []string `help:"Bootstrap multiaddresses including /p2p/peer-ID; empty uses Syncthing defaults" env:"STDHT_BOOTSTRAP"` + KeyFile string `help:"Base64-encoded libp2p private key file" default:"identity.key" env:"STDHT_KEY_FILE"` + Database string `help:"Database directory for DHT values and routing table" default:"/tmp/stdht-db" env:"STDHT_DATABASE"` + MetricsListen string `help:"HTTP metrics and health address; empty disables" default:"127.0.0.1:9010" env:"STDHT_METRICS_LISTEN"` + MaxConnections int `help:"Maximum DHT connections" default:"16000" env:"STDHT_MAX_CONNECTIONS"` + MaxConnectionsPerSubnet int `help:"Maximum connections per IPv4 /32 and IPv6 /56; IPv6 /48 allows eight times this (zero uses libp2p defaults)" default:"0" env:"STDHT_MAX_CONNECTIONS_PER_SUBNET"` + Version bool `short:"v" help:"Print version and exit"` } func main() { + slogutil.SetLevelOverrides(os.Getenv("STTRACE")) + var cli CLI kong.Parse(&cli) if cli.Version { @@ -110,21 +114,22 @@ func run(ctx context.Context, cli CLI) error { listen = append(listen, udpAddr.AddrPort()) } dht, err := dht.NewSyncthingDHT(dht.Options{ - Identity: identity, - ListenAddresses: listen, - AdvertiseAddresses: cli.Advertise, - BootstrapPeers: cli.Bootstrap, - Server: true, - MaxConnections: cli.MaxConnections, - ValueDatastore: dht.NewDatastore(db.NewTyped(store, "dht-values")), - RoutingDatastore: dht.NewDatastore(db.NewTyped(store, "dht-routing")), + Identity: identity, + ListenAddresses: listen, + AdvertiseAddresses: cli.Advertise, + BootstrapPeers: cli.Bootstrap, + Server: true, + MaxConnections: cli.MaxConnections, + MaxConnectionsPerSubnet: cli.MaxConnectionsPerSubnet, + ValueDatastore: dht.NewDatastore(db.NewTyped(store, "dht-values")), + RoutingDatastore: dht.NewDatastore(db.NewTyped(store, "dht-routing")), }) if err != nil { return err } defer dht.Close() - slog.InfoContext(ctx, "Discovery DHT server started", "peer", dht.ID()) + slog.InfoContext(ctx, "Discovery DHT server started", "peer", dht.ID(), "maxConnections", cli.MaxConnections, "maxConnectionsPerSubnet", cli.MaxConnectionsPerSubnet) var httpServer *http.Server var httpErrors <-chan error diff --git a/internal/dht/dht.go b/internal/dht/dht.go index 3d3676898..6e6fe5489 100644 --- a/internal/dht/dht.go +++ b/internal/dht/dht.go @@ -69,15 +69,16 @@ func Lookup(ctx context.Context, deviceID protocol.DeviceID, options Options) ([ } type Options struct { - UDP *registry.UDPReuseManager - ListenAddresses []netip.AddrPort - BootstrapPeers []string - AdvertiseAddresses []string - Identity []byte - Server bool - ValueDatastore ds.Batching - RoutingDatastore ds.Batching - MaxConnections int // Zero uses libp2p defaults. + UDP *registry.UDPReuseManager + ListenAddresses []netip.AddrPort + BootstrapPeers []string + AdvertiseAddresses []string + Identity []byte + Server bool + ValueDatastore ds.Batching + RoutingDatastore ds.Batching + MaxConnections int // Zero uses libp2p defaults. + MaxConnectionsPerSubnet int // IPv4 /32 and IPv6 /56; IPv6 /48 allows eight times this. Zero uses libp2p defaults. } type DHT struct { @@ -86,6 +87,9 @@ type DHT struct { } func NewSyncthingDHT(options Options) (*DHT, error) { + if options.MaxConnectionsPerSubnet < 0 || options.MaxConnectionsPerSubnet > int(^uint(0)>>1)/8 { + return nil, errors.New("invalid per-subnet connection limit") + } if options.Server && len(options.ListenAddresses) == 0 { return nil, errors.New("DHT server requires a QUIC listener") } @@ -177,30 +181,47 @@ func NewSyncthingDHT(options Options) (*DHT, error) { if err != nil { return nil, err } - + hostOptions = append(hostOptions, libp2p.ConnectionManager(connections)) + } + if options.MaxConnections > 0 || options.MaxConnectionsPerSubnet > 0 { defaults := rcmgr.DefaultLimits libp2p.SetDefaultServiceLimits(&defaults) - - limits := rcmgr.PartialLimitConfig{ - System: rcmgr.ResourceLimits{ - Conns: rcmgr.LimitVal(limit), - ConnsInbound: rcmgr.LimitVal(limit), - ConnsOutbound: rcmgr.LimitVal(limit), - }, - }.Build(defaults.AutoScale()) - - resources, err = rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(limits)) + limits := defaults.AutoScale() + if limit := options.MaxConnections; limit > 0 { + limits = rcmgr.PartialLimitConfig{ + System: rcmgr.ResourceLimits{ + Conns: rcmgr.LimitVal(limit), + ConnsInbound: rcmgr.LimitVal(limit), + ConnsOutbound: rcmgr.LimitVal(limit), + }, + }.Build(limits) + } + var resourceOptions []rcmgr.Option + if limit := options.MaxConnectionsPerSubnet; limit > 0 { + // Keep libp2p's prefix sizes and the wider IPv6 prefix's 8:1 ratio. + // This resource manager is also used for AutoNAT dial-back connections. + resourceOptions = append(resourceOptions, rcmgr.WithLimitPerSubnet( + []rcmgr.ConnLimitPerSubnet{{PrefixLength: 32, ConnCount: limit}}, + []rcmgr.ConnLimitPerSubnet{{PrefixLength: 56, ConnCount: limit}, {PrefixLength: 48, ConnCount: 8 * limit}}, + )) + } + var err error + resources, err = rcmgr.NewResourceManager(rcmgr.NewFixedLimiter(limits), resourceOptions...) if err != nil { - connections.Close() + if connections != nil { + connections.Close() + } return nil, err } - hostOptions = append(hostOptions, libp2p.ConnectionManager(connections), libp2p.ResourceManager(resources)) + hostOptions = append(hostOptions, libp2p.ResourceManager(resources)) } h, err := libp2p.New(hostOptions...) if err != nil { if connections != nil { connections.Close() + } + if resources != nil { resources.Close() } return nil, err