From 59b5ca02f1dc2bd16c41cc9c3b14f575b2135817 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Tue, 17 Jan 2023 19:18:39 +0100 Subject: [PATCH] fix: refuse to start if connmgr is smaller than ressource limits and not using none connmgr Fixes: #9548 --- core/node/libp2p/rcmgr.go | 42 ++++++++++++++++++++++++++++++ test/sharness/t0139-swarm-rcmgr.sh | 28 ++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/core/node/libp2p/rcmgr.go b/core/node/libp2p/rcmgr.go index 5d23a874da8..321ffbf19b4 100644 --- a/core/node/libp2p/rcmgr.go +++ b/core/node/libp2p/rcmgr.go @@ -67,6 +67,10 @@ func ResourceManager(cfg config.SwarmConfig) interface{} { limitConfig = l } + if err := ensureConnMgrMakeSenseVsRessourcesMgr(limitConfig, cfg.ConnMgr); err != nil { + return nil, opts, err + } + limiter := rcmgr.NewFixedLimiter(limitConfig) str, err := rcmgrObs.NewStatsTraceReporter() @@ -598,3 +602,41 @@ func NetResetLimit(mgr network.ResourceManager, repo repo.Repo, scope string) (r return result, nil } + +func ensureConnMgrMakeSenseVsRessourcesMgr(rcm rcmgr.LimitConfig, cmgr config.ConnMgr) error { + if cmgr.Type.WithDefault(config.DefaultConnMgrType) == "none" { + return nil // none connmgr, no checks to do + } + highWater := cmgr.HighWater.WithDefault(config.DefaultConnMgrHighWater) + if rcm.System.ConnsInbound <= rcm.System.Conns { + if int64(rcm.System.ConnsInbound) <= highWater { + // nolint + return fmt.Errorf(` +Unable to initialize libp2p due to conflicting limit configuration: +ResourceMgr.Limits.System.ConnsInbound (%d) must be bigger than ConnMgr.HighWater (%d) +`, rcm.System.ConnsInbound, highWater) + } + } else if int64(rcm.System.Conns) <= highWater { + // nolint + return fmt.Errorf(` +Unable to initialize libp2p due to conflicting limit configuration: +ResourceMgr.Limits.System.Conns (%d) must be bigger than ConnMgr.HighWater (%d) +`, rcm.System.Conns, highWater) + } + if rcm.System.StreamsInbound <= rcm.System.Streams { + if int64(rcm.System.StreamsInbound) <= highWater { + // nolint + return fmt.Errorf(` +Unable to initialize libp2p due to conflicting limit configuration: +ResourceMgr.Limits.System.StreamsInbound (%d) must be bigger than ConnMgr.HighWater (%d) +`, rcm.System.StreamsInbound, highWater) + } + } else if int64(rcm.System.Streams) <= highWater { + // nolint + return fmt.Errorf(` +Unable to initialize libp2p due to conflicting limit configuration: +ResourceMgr.Limits.System.Streams (%d) must be bigger than ConnMgr.HighWater (%d) +`, rcm.System.Streams, highWater) + } + return nil +} diff --git a/test/sharness/t0139-swarm-rcmgr.sh b/test/sharness/t0139-swarm-rcmgr.sh index c36ddd3d8a3..69c5e4600a6 100755 --- a/test/sharness/t0139-swarm-rcmgr.sh +++ b/test/sharness/t0139-swarm-rcmgr.sh @@ -227,4 +227,32 @@ test_expect_success 'stop iptb' ' iptb stop 2 ' +## Test daemon refuse to start if connmgr.highwater < ressources inbound + +test_expect_success "node refuse to start if Swarm.ResourceMgr.Limits.System.Conns <= Swarm.ConnMgr.HighWater" ' + ipfs config --json Swarm.ResourceMgr.Limits.System.Conns 128 && + ipfs config --json Swarm.ConnMgr.HighWater 128 && + ipfs config --json Swarm.ConnMgr.LowWater 64 && + test_expect_code 1 ipfs daemon && + ipfs config --json Swarm.ResourceMgr.Limits.System.Conns 256 +' + +test_expect_success "node refuse to start if Swarm.ResourceMgr.Limits.System.ConnsInbound <= Swarm.ConnMgr.HighWater" ' + ipfs config --json Swarm.ResourceMgr.Limits.System.ConnsInbound 128 && + test_expect_code 1 ipfs daemon && + ipfs config --json Swarm.ResourceMgr.Limits.System.ConnsInbound 256 +' + +test_expect_success "node refuse to start if Swarm.ResourceMgr.Limits.System.Streams <= Swarm.ConnMgr.HighWater" ' + ipfs config --json Swarm.ResourceMgr.Limits.System.Streams 128 && + test_expect_code 1 ipfs daemon && + ipfs config --json Swarm.ResourceMgr.Limits.System.Streams 256 +' + +test_expect_success "node refuse to start if Swarm.ResourceMgr.Limits.System.StreamsInbound <= Swarm.ConnMgr.HighWater" ' + ipfs config --json Swarm.ResourceMgr.Limits.System.StreamsInbound 128 && + test_expect_code 1 ipfs daemon && + ipfs config --json Swarm.ResourceMgr.Limits.System.StreamsInbound 256 +' + test_done