Skip to content

Commit

Permalink
p2p: add disable reuseport option (#4539)
Browse files Browse the repository at this point in the history
reuseport https://lwn.net/Articles/542629/ .
we know that some nodes can't reach bootnodes, behavior matches what is described in the comment below https://github.com/ipfs/kubo/issues/6548#issuecomment-517372924https://github.com/ipfs/kubo/issues/6548#issuecomment-517372924

> Meaning that you've exceeded the 10s timeout for the connection. Your ISP may be blocking or rate-limiting connections from low-numbered ports.
  • Loading branch information
dshulyak committed Jun 19, 2023
1 parent 72076af commit efb9191
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func AddCommands(cmd *cobra.Command) {

cmd.PersistentFlags().IntVar(&cfg.DatabaseConnections, "db-connections",
cfg.DatabaseConnections, "configure number of active connections to enable parallel read requests")
cmd.PersistentFlags().BoolVar(&cfg.P2P.Flood, "db-latency-metering",
cmd.PersistentFlags().BoolVar(&cfg.DatabaseLatencyMetering, "db-latency-metering",
cfg.DatabaseLatencyMetering, "if enabled collect latency histogram for every database query")

/** ======================== P2P Flags ========================== **/

cmd.PersistentFlags().StringVar(&cfg.P2P.Listen, "listen",
Expand All @@ -90,6 +91,11 @@ func AddCommands(cmd *cobra.Command) {
cfg.P2P.Flood, "flood created messages to all peers")
cmd.PersistentFlags().BoolVar(&cfg.P2P.DisableNatPort, "disable-natport",
cfg.P2P.DisableNatPort, "disable nat port-mapping (if enabled upnp protocol is used to negotiate external port with router)")
cmd.PersistentFlags().BoolVar(&cfg.P2P.DisableReusePort,
"disable-reuseport",
cfg.P2P.DisableReusePort,
"disables SO_REUSEPORT for tcp sockets. Try disabling this if your node can't reach bootnodes in the network",
)
cmd.PersistentFlags().IntVar(&cfg.P2P.LowPeers, "low-peers",
cfg.P2P.LowPeers, "low watermark for the number of connections")
cmd.PersistentFlags().IntVar(&cfg.P2P.HighPeers, "high-peers",
Expand Down
12 changes: 11 additions & 1 deletion p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
lp2plog "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/transport"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
Expand Down Expand Up @@ -40,6 +42,8 @@ type Config struct {
GracePeersShutdown time.Duration
MaxMessageSize int

// see https://lwn.net/Articles/542629/ for reuseport explanation
DisableReusePort bool `mapstructure:"disable-reuseport"`
DisableNatPort bool `mapstructure:"disable-natport"`
Flood bool `mapstructure:"flood"`
Listen string `mapstructure:"listen"`
Expand Down Expand Up @@ -74,7 +78,13 @@ func New(_ context.Context, logger log.Log, cfg Config, prologue []byte, opts ..
libp2p.UserAgent("go-spacemesh"),
libp2p.DisableRelay(),

libp2p.Transport(tcp.NewTCPTransport),
libp2p.Transport(func(upgrader transport.Upgrader, rcmgr network.ResourceManager) (transport.Transport, error) {
opts := []tcp.Option{}
if cfg.DisableReusePort {
opts = append(opts, tcp.DisableReuseport())
}
return tcp.NewTCPTransport(upgrader, rcmgr, opts...)
}),
libp2p.Security(noise.ID, func(id protocol.ID, privkey crypto.PrivKey, muxers []tptu.StreamMuxer) (*noise.SessionTransport, error) {
tp, err := noise.New(id, privkey, muxers)
if err != nil {
Expand Down

0 comments on commit efb9191

Please sign in to comment.