From cb7c98409aaac3468171d2c7f8de64363f69ddf7 Mon Sep 17 00:00:00 2001 From: sukun Date: Mon, 20 Jan 2025 01:33:50 +0530 Subject: [PATCH] tcpreuse: fix rcmgr accounting when tcp metrics are enabled Yet another interface embedding bug. Running all transport integration tests is a bit overkill, but the rcmgr transport integration test is the only one that tests for this behavior. --- p2p/test/transport/transport_test.go | 37 +++++++++++++++++++++++++++- p2p/transport/tcp/metrics.go | 11 +++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/p2p/test/transport/transport_test.go b/p2p/test/transport/transport_test.go index 60f8ca0c06..a08b2fffc0 100644 --- a/p2p/test/transport/transport_test.go +++ b/p2p/test/transport/transport_test.go @@ -31,6 +31,7 @@ import ( "github.com/libp2p/go-libp2p/p2p/protocol/ping" "github.com/libp2p/go-libp2p/p2p/security/noise" tls "github.com/libp2p/go-libp2p/p2p/security/tls" + "github.com/libp2p/go-libp2p/p2p/transport/tcp" libp2pwebrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc" "go.uber.org/mock/gomock" @@ -100,12 +101,46 @@ var transportsToTest = []TransportTestCase{ }, }, { - Name: "TCP-Shared / TLS / Yamux", + Name: "TCP / TLS / Yamux", + HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host { + libp2pOpts := transformOpts(opts) + libp2pOpts = append(libp2pOpts, libp2p.Security(tls.ID, tls.New)) + libp2pOpts = append(libp2pOpts, libp2p.Muxer(yamux.ID, yamux.DefaultTransport)) + if opts.NoListen { + libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs) + } else { + libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) + } + h, err := libp2p.New(libp2pOpts...) + require.NoError(t, err) + return h + }, + }, + { + Name: "TCP-Shared-WithMetrics / TLS / Yamux", HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host { libp2pOpts := transformOpts(opts) libp2pOpts = append(libp2pOpts, libp2p.ShareTCPListener()) libp2pOpts = append(libp2pOpts, libp2p.Security(tls.ID, tls.New)) libp2pOpts = append(libp2pOpts, libp2p.Muxer(yamux.ID, yamux.DefaultTransport)) + libp2pOpts = append(libp2pOpts, libp2p.Transport(tcp.NewTCPTransport, tcp.WithMetrics())) + if opts.NoListen { + libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs) + } else { + libp2pOpts = append(libp2pOpts, libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) + } + h, err := libp2p.New(libp2pOpts...) + require.NoError(t, err) + return h + }, + }, + { + Name: "TCP-WithMetrics / TLS / Yamux", + HostGenerator: func(t *testing.T, opts TransportTestCaseOpts) host.Host { + libp2pOpts := transformOpts(opts) + libp2pOpts = append(libp2pOpts, libp2p.Security(tls.ID, tls.New)) + libp2pOpts = append(libp2pOpts, libp2p.Muxer(yamux.ID, yamux.DefaultTransport)) + libp2pOpts = append(libp2pOpts, libp2p.Transport(tcp.NewTCPTransport, tcp.WithMetrics())) if opts.NoListen { libp2pOpts = append(libp2pOpts, libp2p.NoListenAddrs) } else { diff --git a/p2p/transport/tcp/metrics.go b/p2p/transport/tcp/metrics.go index 50820d870c..cbd2f92f73 100644 --- a/p2p/transport/tcp/metrics.go +++ b/p2p/transport/tcp/metrics.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/libp2p/go-libp2p/core/network" "github.com/marten-seemann/tcp" "github.com/mikioh/tcpinfo" manet "github.com/multiformats/go-multiaddr/net" @@ -252,6 +253,16 @@ func (c *tracingConn) Close() error { return c.closeErr } +func (c *tracingConn) Scope() network.ConnManagementScope { + if cs, ok := c.Conn.(interface { + Scope() network.ConnManagementScope + }); ok { + return cs.Scope() + } + // upgrader is expected to handle this + return nil +} + func (c *tracingConn) getTCPInfo() (*tcpinfo.Info, error) { var o tcpinfo.Info var b [256]byte