Skip to content

Commit

Permalink
[client] Add NB_SKIP_SOCKET_MARK & fix crash instead of returing an e…
Browse files Browse the repository at this point in the history
…rror (#2899)

* dialer: fix crash instead of returning error

* add NB_SKIP_SOCKET_MARK
  • Loading branch information
nazarewk authored Nov 19, 2024
1 parent 52ea2e8 commit eb5d056
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client/internal/routemanager/systemops/systemops_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type ruleParams struct {

// isLegacy determines whether to use the legacy routing setup
func isLegacy() bool {
return os.Getenv("NB_USE_LEGACY_ROUTING") == "true" || nbnet.CustomRoutingDisabled()
return os.Getenv("NB_USE_LEGACY_ROUTING") == "true" || nbnet.CustomRoutingDisabled() || os.Getenv(nbnet.EnvSkipSocketMark) == "true"
}

// setIsLegacy sets the legacy routing setup
Expand Down
9 changes: 7 additions & 2 deletions util/grpc/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package grpc
import (
"context"
"crypto/tls"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net"
"os/user"
"runtime"
Expand All @@ -23,20 +26,22 @@ func WithCustomDialer() grpc.DialOption {
if runtime.GOOS == "linux" {
currentUser, err := user.Current()
if err != nil {
log.Fatalf("failed to get current user: %v", err)
return nil, status.Errorf(codes.FailedPrecondition, "failed to get current user: %v", err)
}

// the custom dialer requires root permissions which are not required for use cases run as non-root
if currentUser.Uid != "0" {
log.Debug("Not running as root, using standard dialer")
dialer := &net.Dialer{}
return dialer.DialContext(ctx, "tcp", addr)
}
}

log.Debug("Using nbnet.NewDialer()")
conn, err := nbnet.NewDialer().DialContext(ctx, "tcp", addr)
if err != nil {
log.Errorf("Failed to dial: %s", err)
return nil, err
return nil, fmt.Errorf("nbnet.NewDialer().DialContext: %w", err)
}
return conn, nil
})
Expand Down
2 changes: 1 addition & 1 deletion util/net/dialer_nonios.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.

conn, err := d.Dialer.DialContext(ctx, network, address)
if err != nil {
return nil, fmt.Errorf("dial: %w", err)
return nil, fmt.Errorf("d.Dialer.DialContext: %w", err)
}

// Wrap the connection in Conn to handle Close with hooks
Expand Down
12 changes: 12 additions & 0 deletions util/net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ package net

import (
"fmt"
"os"
"syscall"

log "github.com/sirupsen/logrus"
)

const EnvSkipSocketMark = "NB_SKIP_SOCKET_MARK"

// SetSocketMark sets the SO_MARK option on the given socket connection
func SetSocketMark(conn syscall.Conn) error {
sysconn, err := conn.SyscallConn()
Expand Down Expand Up @@ -36,6 +41,13 @@ func SetRawSocketMark(conn syscall.RawConn) error {

func SetSocketOpt(fd int) error {
if CustomRoutingDisabled() {
log.Infof("Custom routing is disabled, skipping SO_MARK")
return nil
}

// Check for the new environment variable
if skipSocketMark := os.Getenv(EnvSkipSocketMark); skipSocketMark == "true" {
log.Info("NB_SKIP_SOCKET_MARK is set to true, skipping SO_MARK")
return nil
}

Expand Down

0 comments on commit eb5d056

Please sign in to comment.