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

Sequentialip+predifined subnet #1

Closed
wants to merge 14 commits into from
91 changes: 60 additions & 31 deletions management/server/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
"net"
"sync"
"time"
"os"
"log"
"fmt"

"github.com/c-robinson/iplib"
"github.com/rs/xid"

nbdns "github.com/netbirdio/netbird/dns"
nbpeer "github.com/netbirdio/netbird/management/server/peer"
"github.com/netbirdio/netbird/management/server/status"

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test_client_on_docker

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (jsonfile)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / android_build

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / ios_build

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, jsonfile)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, jsonfile)

"github.com/netbirdio/netbird/management/server/status" imported and not used

Check failure on line 17 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test

"github.com/netbirdio/netbird/management/server/status" imported and not used
"github.com/netbirdio/netbird/route"
)

Expand Down Expand Up @@ -48,19 +51,35 @@
// NewNetwork creates a new Network initializing it with a Serial=0
// It takes a random /16 subnet from 100.64.0.0/10 (64 different subnets)
func NewNetwork() *Network {

n := iplib.NewNet4(net.ParseIP("100.64.0.0"), NetSize)
sub, _ := n.Subnet(SubnetSize)

s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(sub))

return &Network{
Identifier: xid.New().String(),
Net: sub[intn].IPNet,
Dns: "",
Serial: 0}
// Check if the NETBIRD_SUBNET environment variable is set
customSubnet := os.Getenv("NETBIRD_SUBNET")
if customSubnet != "" {
if _, subnet, err := net.ParseCIDR(customSubnet); err == nil {
return &Network{
Identifier: xid.New().String(),
Net: *subnet,
Dns: "",
Serial: 0,
}
}
// Handle error if parsing custom subnet fails
log.Println("Error parsing custom subnet from NETBIRD_SUBNET variable:", err)

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: err) (typecheck)

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: err) (typecheck)

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

undefined: err) (typecheck)

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test_client_on_docker

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: err) (typecheck)

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: err) (typecheck)

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

undefined: err) (typecheck)

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (jsonfile)

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (sqlite)

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / android_build

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / ios_build

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, jsonfile)

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (386, sqlite)

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, sqlite)

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test (amd64, jsonfile)

undefined: err

Check failure on line 66 in management/server/network.go

View workflow job for this annotation

GitHub Actions / test

undefined: err
}

// If NETBIRD_SUBNET is not set or parsing fails, choose a random subnet
n := iplib.NewNet4(net.ParseIP("100.64.0.0"), NetSize)
sub, _ := n.Subnet(SubnetSize)

s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(sub))

return &Network{
Identifier: xid.New().String(),
Net: sub[intn].IPNet,
Dns: "",
Serial: 0,
}
}

// IncSerial increments Serial by 1 reflecting that the network state has been changed
Expand Down Expand Up @@ -90,24 +109,34 @@
// This method considers already taken IPs and reuses IPs if there are gaps in takenIps
// E.g. if ipNet=100.30.0.0/16 and takenIps=[100.30.0.1, 100.30.0.4] then the result would be 100.30.0.2 or 100.30.0.3
func AllocatePeerIP(ipNet net.IPNet, takenIps []net.IP) (net.IP, error) {
takenIPMap := make(map[string]struct{})
takenIPMap[ipNet.IP.String()] = struct{}{}
for _, ip := range takenIps {
takenIPMap[ip.String()] = struct{}{}
}

ips, _ := generateIPs(&ipNet, takenIPMap)

if len(ips) == 0 {
return nil, status.Errorf(status.PreconditionFailed, "failed allocating new IP for the ipNet %s - network is out of IPs", ipNet.String())
}

// pick a random IP
s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(ips))

return ips[intn], nil
// Check if NETBIRD_ALLOCATE_SEQUENTIAL_IPS is set to 1
allocateSequential := os.Getenv("NETBIRD_ALLOCATE_SEQUENTIAL_IPS") == "1"

takenIPMap := make(map[string]struct{})
takenIPMap[ipNet.IP.String()] = struct{}{}
for _, ip := range takenIps {
takenIPMap[ip.String()] = struct{}{}
}

ips, _ := generateIPs(&ipNet, takenIPMap)

if len(ips) == 0 {
return nil, fmt.Errorf("failed allocating new IP for the ipNet %s - network is out of IPs", ipNet.String())
}

// Pick an IP based on the allocation method
var selectedIP net.IP
if allocateSequential {
selectedIP = ips[0] // Allocate the smallest available IP
} else {
// Pick a random IP
s := rand.NewSource(time.Now().Unix())
r := rand.New(s)
intn := r.Intn(len(ips))
selectedIP = ips[intn]
}

return selectedIP, nil
}

// generateIPs generates a list of all possible IPs of the given network excluding IPs specified in the exclusion list
Expand Down
Loading