Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[client] Fix controller re-connection #2758

Merged
merged 86 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
9d75cc3
Add pause function for proxies
pappz Oct 2, 2024
acad98e
Code cleaning
pappz Oct 3, 2024
90d9dd4
Remove unused function from eBPF proxy
pappz Oct 7, 2024
f18ae8b
Apply pause logic
pappz Oct 7, 2024
48184ec
Fix eBPF pause handling
pappz Oct 7, 2024
718d952
Fix test
pappz Oct 7, 2024
9e7aac3
Reducate cognitive complexity
pappz Oct 7, 2024
1f25bb0
Reducate cognitive complexity
pappz Oct 7, 2024
ba3cdb3
Remove unnecessary ctx cancel check
pappz Oct 7, 2024
20815c9
Remove unused function
pappz Oct 7, 2024
d294cea
Apply sender for bind proxy
pappz Oct 7, 2024
b04d19b
Fix nil pointer in error handling
pappz Oct 8, 2024
2b81a68
Add read loop to proxy
pappz Oct 9, 2024
fac8869
Add comment
pappz Oct 9, 2024
1814b07
Replace error check to errors.Is
pappz Oct 9, 2024
deeb050
Handle addr resolve error
pappz Oct 9, 2024
e3dfbe5
Add trace log
pappz Oct 9, 2024
c366ddf
- Add pause logic
pappz Oct 9, 2024
9ea3d24
- decrease the buffer copy
pappz Oct 9, 2024
69b1e9e
Rename userspace proxy to udp proxy
pappz Oct 9, 2024
6369706
Merge branch 'main' into relay/fix/wg-roaming
pappz Oct 9, 2024
0ef7e76
Merge branch 'relay/fix/wg-roaming' into eliminate-udp-proxy
pappz Oct 9, 2024
5b2d5f8
Try to force install libpcap
pappz Oct 9, 2024
021eef5
Fix tests
pappz Oct 9, 2024
a5deeda
Revert force install change
pappz Oct 9, 2024
b8026ad
Merge branch 'main' into relay/fix/wg-roaming
pappz Oct 9, 2024
8739b2a
Merge branch 'relay/fix/wg-roaming' into eliminate-udp-proxy
pappz Oct 9, 2024
553bd32
Fix Android build in factory
pappz Oct 9, 2024
4d788a8
Fix darwin
pappz Oct 9, 2024
2054944
Fix windows build
pappz Oct 9, 2024
25693e0
Lint fix
pappz Oct 9, 2024
4ac34c8
Use USP proxy on all platform
pappz Oct 10, 2024
53e7f08
Fix test
pappz Oct 10, 2024
d62b348
Add freebsd proxy factory
pappz Oct 11, 2024
4325a2d
Sonar lint fix
pappz Oct 11, 2024
7fb10d2
Comments
pappz Oct 11, 2024
d64e973
Merge branch 'main' into eliminate-udp-proxy
pappz Oct 11, 2024
e191f35
fix merge conflict
pappz Oct 11, 2024
4a21eb5
Store the endpoint info with netip.Addr key
pappz Oct 14, 2024
78ef8a6
Fix remove function
pappz Oct 14, 2024
02f08a5
Remove code duplication
pappz Oct 14, 2024
46d1d1a
Apply new logic
pappz Oct 17, 2024
39f4ace
Remove handshaker from guard code
pappz Oct 18, 2024
9a83fe9
Remove unused code
pappz Oct 18, 2024
d2e673f
Rename function
pappz Oct 18, 2024
086845f
Code format engine.go
pappz Oct 18, 2024
f387d96
Implement close function for sr_watcher
pappz Oct 18, 2024
83c0761
Fix pkg name
pappz Oct 18, 2024
bed5f5b
Fix parameters of test
pappz Oct 18, 2024
f332acf
Fix signal moc client
pappz Oct 18, 2024
1203f04
Remove unused codes
pappz Oct 18, 2024
5b43c56
Fix map creation
pappz Oct 18, 2024
4428d00
Fix function call in bg
pappz Oct 18, 2024
df74d79
Fix ticker timeout
pappz Oct 18, 2024
17e460e
Ignore unnecessary error logging
pappz Oct 18, 2024
cac6ad9
Add log
pappz Oct 18, 2024
c60dfba
Fix ticker creation
pappz Oct 18, 2024
01e42c9
Fix hardcoded controller logic
pappz Oct 18, 2024
dbb280f
Fix test timeout period
pappz Oct 18, 2024
abfea86
Fix service is ready eval
pappz Oct 18, 2024
6d4c068
Code cleaning
pappz Oct 19, 2024
4aeca0e
Code cleaning
pappz Oct 20, 2024
022413e
Fix sonar issue
pappz Oct 20, 2024
c78355a
Add fast reconnection try to Relay lib
pappz Oct 20, 2024
d9ddd29
Lint fix
pappz Oct 20, 2024
8e38a85
Add comment
pappz Oct 20, 2024
21be242
Remove debug line
pappz Oct 20, 2024
2ee4840
Fix CI test
pappz Oct 20, 2024
dbcd221
Fix sonar lint
pappz Oct 20, 2024
277db47
Reduce complexity
pappz Oct 20, 2024
0eb5f7b
Fix test
pappz Oct 20, 2024
a9597d1
Fix engine test
pappz Oct 20, 2024
f4f0820
Fix iface tests in CI
pappz Oct 21, 2024
29e0ae0
Remove -t from docker cmd
pappz Oct 21, 2024
c43f142
Remove -i
pappz Oct 21, 2024
c48d331
Try to disable parallel run for iface tests
pappz Oct 21, 2024
0f5c024
Revert parallel test
pappz Oct 21, 2024
bf6bf68
Workaround for test
pappz Oct 21, 2024
9b36694
Add log
pappz Oct 21, 2024
b44a912
Fix FreeBSD build
pappz Oct 21, 2024
72be6dc
Remove debug log line
pappz Oct 22, 2024
5b01236
Change log messages
pappz Oct 22, 2024
63b0f79
Merge branch 'main' into relay/fix-controller-reconnection
pappz Oct 22, 2024
be79bff
Replace to errors.Is
pappz Oct 23, 2024
9f2e238
Merge branch 'main' into relay/fix-controller-reconnection
pappz Oct 24, 2024
ce11075
Merge branch 'main' into relay/fix-controller-reconnection
pappz Oct 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .github/workflows/golang-test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ jobs:
- name: check git status
run: git --no-pager diff --exit-code

- name: Generate Iface Test bin
run: CGO_ENABLED=0 go test -c -o iface-testing.bin ./client/iface/

- name: Generate Shared Sock Test bin
run: CGO_ENABLED=0 go test -c -o sharedsock-testing.bin ./sharedsock

Expand All @@ -98,15 +95,15 @@ jobs:
run: CGO_ENABLED=1 go test -c -o engine-testing.bin ./client/internal

- name: Generate Peer Test bin
run: CGO_ENABLED=0 go test -c -o peer-testing.bin ./client/internal/peer/...
run: CGO_ENABLED=0 go test -c -o peer-testing.bin ./client/internal/peer/

- run: chmod +x *testing.bin

- name: Run Shared Sock tests in docker
run: docker run -t --cap-add=NET_ADMIN --privileged --rm -v $PWD:/ci -w /ci/sharedsock --entrypoint /busybox/sh gcr.io/distroless/base:debug -c /ci/sharedsock-testing.bin -test.timeout 5m -test.parallel 1

- name: Run Iface tests in docker
run: docker run -t --cap-add=NET_ADMIN --privileged --rm -v $PWD:/ci -w /ci/iface --entrypoint /busybox/sh gcr.io/distroless/base:debug -c /ci/iface-testing.bin -test.timeout 5m -test.parallel 1
run: docker run -t --cap-add=NET_ADMIN --privileged --rm -v $PWD:/netbird -v /tmp/cache:/tmp/cache -v /tmp/modcache:/tmp/modcache -w /netbird -e GOCACHE=/tmp/cache -e GOMODCACHE=/tmp/modcache -e CGO_ENABLED=0 golang:1.23-alpine go test -test.timeout 5m -test.parallel 1 ./client/iface/...

- name: Run RouteManager tests in docker
run: docker run -t --cap-add=NET_ADMIN --privileged --rm -v $PWD:/ci -w /ci/client/internal/routemanager --entrypoint /busybox/sh gcr.io/distroless/base:debug -c /ci/routemanager-testing.bin -test.timeout 5m -test.parallel 1
Expand Down
1 change: 0 additions & 1 deletion client/iface/bind/ice_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func (b *ICEBind) Send(bufs [][]byte, ep wgConn.Endpoint) error {
conn, ok := b.endpoints[ep.DstIP()]
b.endpointsMu.Unlock()
if !ok {
log.Infof("failed to find endpoint for %s", ep.DstIP())
return b.StdNetBind.Send(bufs, ep)
}

Expand Down
4 changes: 2 additions & 2 deletions client/iface/wgproxy/ebpf/portlookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"net"
)

const (
var (
portRangeStart = 3128
portRangeEnd = 3228
portRangeEnd = portRangeStart + 100
)

type portLookup struct {
Expand Down
3 changes: 3 additions & 0 deletions client/iface/wgproxy/ebpf/portlookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func Test_portLookup_searchFreePort(t *testing.T) {
func Test_portLookup_on_allocated(t *testing.T) {
pl := portLookup{}

portRangeStart = 4128
portRangeEnd = portRangeStart + 100

allocatedPort, err := allocatePort(portRangeStart)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions client/iface/wgproxy/factory_kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ func NewKernelFactory(wgPort int) *KernelFactory {

ebpfProxy := ebpf.NewWGEBPFProxy(wgPort)
if err := ebpfProxy.Listen(); err != nil {
log.Infof("WireGuard Proxy Factory will produce UDP proxy")
log.Warnf("failed to initialize ebpf proxy, fallback to user space proxy: %s", err)
return f
}
log.Infof("WireGuard Proxy Factory will produce eBPF proxy")
f.ebpfProxy = ebpfProxy
return f
}
Expand Down
3 changes: 3 additions & 0 deletions client/iface/wgproxy/factory_kernel_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wgproxy

import (
log "github.com/sirupsen/logrus"

udpProxy "github.com/netbirdio/netbird/client/iface/wgproxy/udp"
)

Expand All @@ -10,6 +12,7 @@ type KernelFactory struct {
}

func NewKernelFactory(wgPort int) *KernelFactory {
log.Infof("WireGuard Proxy Factory will produce UDP proxy")
f := &KernelFactory{
wgPort: wgPort,
}
Expand Down
3 changes: 3 additions & 0 deletions client/iface/wgproxy/factory_usp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wgproxy

import (
log "github.com/sirupsen/logrus"

"github.com/netbirdio/netbird/client/iface/bind"
proxyBind "github.com/netbirdio/netbird/client/iface/wgproxy/bind"
)
Expand All @@ -10,6 +12,7 @@ type USPFactory struct {
}

func NewUSPFactory(iceBind *bind.ICEBind) *USPFactory {
log.Infof("WireGuard Proxy Factory will produce bind proxy")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these new logs be Debug level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this log line is really important to know in any case. And it will appear only once time. We support 3 different implementation and good to know what is the active.

f := &USPFactory{
bind: iceBind,
}
Expand Down
28 changes: 20 additions & 8 deletions client/iface/wgproxy/udp/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package udp

import (
"context"
"errors"
"fmt"
"io"
"net"
"sync"

"github.com/hashicorp/go-multierror"
log "github.com/sirupsen/logrus"

"github.com/netbirdio/netbird/client/errors"
cerrors "github.com/netbirdio/netbird/client/errors"
)

// WGUDPProxy proxies
Expand Down Expand Up @@ -121,7 +123,7 @@ func (p *WGUDPProxy) close() error {
if err := p.localConn.Close(); err != nil {
result = multierror.Append(result, fmt.Errorf("local conn: %s", err))
}
return errors.FormatErrorOrNil(result)
return cerrors.FormatErrorOrNil(result)
}

// proxyToRemote proxies from Wireguard to the RemoteKey
Expand Down Expand Up @@ -160,18 +162,16 @@ func (p *WGUDPProxy) proxyToRemote(ctx context.Context) {
func (p *WGUDPProxy) proxyToLocal(ctx context.Context) {
defer func() {
if err := p.close(); err != nil {
log.Warnf("error in proxy to local loop: %s", err)
if !errors.Is(err, io.EOF) {
log.Warnf("error in proxy to local loop: %s", err)
}
}
}()

buf := make([]byte, 1500)
for {
n, err := p.remoteConn.Read(buf)
n, err := p.remoteConnRead(ctx, buf)
if err != nil {
if ctx.Err() != nil {
return
}
log.Errorf("failed to read from remote conn: %s, %s", p.remoteConn.RemoteAddr(), err)
return
}

Expand All @@ -193,3 +193,15 @@ func (p *WGUDPProxy) proxyToLocal(ctx context.Context) {
}
}
}

func (p *WGUDPProxy) remoteConnRead(ctx context.Context, buf []byte) (n int, err error) {
n, err = p.remoteConn.Read(buf)
if err != nil {
if ctx.Err() != nil {
return
lixmal marked this conversation as resolved.
Show resolved Hide resolved
}
log.Errorf("failed to read from remote conn: %s, %s", p.remoteConn.LocalAddr(), err)
return
}
return
}
23 changes: 21 additions & 2 deletions client/internal/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/netbirdio/netbird/client/internal/dns"
"github.com/netbirdio/netbird/client/internal/networkmonitor"
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/client/internal/peer/guard"
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
"github.com/netbirdio/netbird/client/internal/relay"
"github.com/netbirdio/netbird/client/internal/rosenpass"
"github.com/netbirdio/netbird/client/internal/routemanager"
Expand Down Expand Up @@ -168,6 +170,7 @@ type Engine struct {

relayManager *relayClient.Manager
stateManager *statemanager.Manager
srWatcher *guard.SRWatcher
}

// Peer is an instance of the Connection Peer
Expand Down Expand Up @@ -263,6 +266,10 @@ func (e *Engine) Stop() error {
e.routeManager.Stop(e.stateManager)
}

if e.srWatcher != nil {
e.srWatcher.Close()
}

err := e.removeAllPeers()
if err != nil {
return fmt.Errorf("failed to remove all peers: %s", err)
Expand Down Expand Up @@ -389,6 +396,18 @@ func (e *Engine) Start() error {
return fmt.Errorf("initialize dns server: %w", err)
}

iceCfg := icemaker.Config{
StunTurn: &e.stunTurn,
InterfaceBlackList: e.config.IFaceBlackList,
DisableIPv6Discovery: e.config.DisableIPv6Discovery,
UDPMux: e.udpMux.UDPMuxDefault,
UDPMuxSrflx: e.udpMux,
NATExternalIPs: e.parseNATExternalIPMappings(),
}

e.srWatcher = guard.NewSRWatcher(e.signal, e.relayManager, e.mobileDep.IFaceDiscover, iceCfg)
e.srWatcher.Start()

e.receiveSignalEvents()
e.receiveManagementEvents()
e.receiveProbeEvents()
Expand Down Expand Up @@ -971,7 +990,7 @@ func (e *Engine) createPeerConn(pubKey string, allowedIPs string) (*peer.Conn, e
LocalWgPort: e.config.WgPort,
RosenpassPubKey: e.getRosenpassPubKey(),
RosenpassAddr: e.getRosenpassAddr(),
ICEConfig: peer.ICEConfig{
ICEConfig: icemaker.Config{
StunTurn: &e.stunTurn,
InterfaceBlackList: e.config.IFaceBlackList,
DisableIPv6Discovery: e.config.DisableIPv6Discovery,
Expand All @@ -981,7 +1000,7 @@ func (e *Engine) createPeerConn(pubKey string, allowedIPs string) (*peer.Conn, e
},
}

peerConn, err := peer.NewConn(e.ctx, config, e.statusRecorder, e.signaler, e.mobileDep.IFaceDiscover, e.relayManager)
peerConn, err := peer.NewConn(e.ctx, config, e.statusRecorder, e.signaler, e.mobileDep.IFaceDiscover, e.relayManager, e.srWatcher)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions client/internal/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/netbirdio/netbird/client/iface/device"
"github.com/netbirdio/netbird/client/internal/dns"
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/client/internal/peer/guard"
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
"github.com/netbirdio/netbird/client/internal/routemanager"
"github.com/netbirdio/netbird/client/ssh"
"github.com/netbirdio/netbird/client/system"
Expand Down Expand Up @@ -258,6 +260,7 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
}
engine.udpMux = bind.NewUniversalUDPMuxDefault(bind.UniversalUDPMuxParams{UDPConn: conn})
engine.ctx = ctx
engine.srWatcher = guard.NewSRWatcher(nil, nil, nil, icemaker.Config{})

type testCase struct {
name string
Expand Down
Loading
Loading