Skip to content

Commit 06484ad

Browse files
authored
Merge pull request #3349 from gravitl/release-v0.30.0
Release v0.30.0
2 parents 68345bb + 225bf37 commit 06484ad

File tree

11 files changed

+293
-71
lines changed

11 files changed

+293
-71
lines changed

controllers/network.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
558558
logic.CreateDefaultNetworkRolesAndGroups(models.NetworkID(network.NetID))
559559
logic.CreateDefaultAclNetworkPolicies(models.NetworkID(network.NetID))
560560
logic.CreateDefaultTags(models.NetworkID(network.NetID))
561-
//add new network to allocated ip map
561+
562562
go logic.AddNetworkToAllocatedIpMap(network.NetID)
563563

564564
go func() {

logic/acls.go

+52-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
var (
1818
aclCacheMutex = &sync.RWMutex{}
1919
aclCacheMap = make(map[string]models.Acl)
20-
aclTagsMutex = &sync.RWMutex{}
2120
)
2221

2322
func MigrateAclPolicies() {
@@ -577,10 +576,22 @@ func IsPeerAllowed(node, peer models.Node, checkDefaultPolicy bool) bool {
577576
if peer.IsStatic {
578577
peer = peer.StaticNode.ConvertToStaticNode()
579578
}
580-
aclTagsMutex.RLock()
581-
peerTags := maps.Clone(peer.Tags)
582-
nodeTags := maps.Clone(node.Tags)
583-
aclTagsMutex.RUnlock()
579+
var nodeTags, peerTags map[models.TagID]struct{}
580+
if node.Mutex != nil {
581+
node.Mutex.Lock()
582+
nodeTags = maps.Clone(node.Tags)
583+
node.Mutex.Unlock()
584+
} else {
585+
nodeTags = node.Tags
586+
}
587+
if peer.Mutex != nil {
588+
peer.Mutex.Lock()
589+
peerTags = maps.Clone(peer.Tags)
590+
peer.Mutex.Unlock()
591+
} else {
592+
peerTags = peer.Tags
593+
}
594+
584595
if checkDefaultPolicy {
585596
// check default policy if all allowed return true
586597
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
@@ -663,10 +674,21 @@ func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool)
663674
if peer.IsStatic {
664675
peer = peer.StaticNode.ConvertToStaticNode()
665676
}
666-
aclTagsMutex.RLock()
667-
peerTags := maps.Clone(peer.Tags)
668-
nodeTags := maps.Clone(node.Tags)
669-
aclTagsMutex.RUnlock()
677+
var nodeTags, peerTags map[models.TagID]struct{}
678+
if node.Mutex != nil {
679+
node.Mutex.Lock()
680+
nodeTags = maps.Clone(node.Tags)
681+
node.Mutex.Unlock()
682+
} else {
683+
nodeTags = node.Tags
684+
}
685+
if peer.Mutex != nil {
686+
peer.Mutex.Lock()
687+
peerTags = maps.Clone(peer.Tags)
688+
peer.Mutex.Unlock()
689+
} else {
690+
peerTags = peer.Tags
691+
}
670692
if checkDefaultPolicy {
671693
// check default policy if all allowed return true
672694
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
@@ -864,7 +886,15 @@ func getUserAclRulesForNode(targetnode *models.Node,
864886
userGrpMap := GetUserGrpMap()
865887
allowedUsers := make(map[string][]models.Acl)
866888
acls := listUserPolicies(models.NetworkID(targetnode.Network))
867-
for nodeTag := range targetnode.Tags {
889+
var targetNodeTags = make(map[models.TagID]struct{})
890+
if targetnode.Mutex != nil {
891+
targetnode.Mutex.Lock()
892+
targetNodeTags = maps.Clone(targetnode.Tags)
893+
targetnode.Mutex.Unlock()
894+
} else {
895+
targetNodeTags = maps.Clone(targetnode.Tags)
896+
}
897+
for nodeTag := range targetNodeTags {
868898
for _, acl := range acls {
869899
if !acl.Enabled {
870900
continue
@@ -888,6 +918,7 @@ func getUserAclRulesForNode(targetnode *models.Node,
888918
}
889919
}
890920
}
921+
891922
for _, userNode := range userNodes {
892923
if !userNode.StaticNode.Enabled {
893924
continue
@@ -944,8 +975,17 @@ func GetAclRulesForNode(targetnode *models.Node) (rules map[string]models.AclRul
944975
}
945976

946977
acls := listDevicePolicies(models.NetworkID(targetnode.Network))
947-
targetnode.Tags["*"] = struct{}{}
948-
for nodeTag := range targetnode.Tags {
978+
979+
var targetNodeTags = make(map[models.TagID]struct{})
980+
if targetnode.Mutex != nil {
981+
targetnode.Mutex.Lock()
982+
targetNodeTags = maps.Clone(targetnode.Tags)
983+
targetnode.Mutex.Unlock()
984+
} else {
985+
targetNodeTags = maps.Clone(targetnode.Tags)
986+
}
987+
targetNodeTags["*"] = struct{}{}
988+
for nodeTag := range targetNodeTags {
949989
for _, acl := range acls {
950990
if !acl.Enabled {
951991
continue

logic/extpeers.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var (
2828
func getAllExtClientsFromCache() (extClients []models.ExtClient) {
2929
extClientCacheMutex.RLock()
3030
for _, extclient := range extClientCacheMap {
31+
if extclient.Mutex == nil {
32+
extclient.Mutex = &sync.Mutex{}
33+
}
3134
extClients = append(extClients, extclient)
3235
}
3336
extClientCacheMutex.RUnlock()
@@ -43,12 +46,18 @@ func deleteExtClientFromCache(key string) {
4346
func getExtClientFromCache(key string) (extclient models.ExtClient, ok bool) {
4447
extClientCacheMutex.RLock()
4548
extclient, ok = extClientCacheMap[key]
49+
if extclient.Mutex == nil {
50+
extclient.Mutex = &sync.Mutex{}
51+
}
4652
extClientCacheMutex.RUnlock()
4753
return
4854
}
4955

5056
func storeExtClientInCache(key string, extclient models.ExtClient) {
5157
extClientCacheMutex.Lock()
58+
if extclient.Mutex == nil {
59+
extclient.Mutex = &sync.Mutex{}
60+
}
5261
extClientCacheMap[key] = extclient
5362
extClientCacheMutex.Unlock()
5463
}
@@ -96,14 +105,14 @@ func DeleteExtClient(network string, clientid string) error {
96105
if err != nil {
97106
return err
98107
}
99-
//recycle ip address
100-
if extClient.Address != "" {
101-
RemoveIpFromAllocatedIpMap(network, extClient.Address)
102-
}
103-
if extClient.Address6 != "" {
104-
RemoveIpFromAllocatedIpMap(network, extClient.Address6)
105-
}
106108
if servercfg.CacheEnabled() {
109+
// recycle ip address
110+
if extClient.Address != "" {
111+
RemoveIpFromAllocatedIpMap(network, extClient.Address)
112+
}
113+
if extClient.Address6 != "" {
114+
RemoveIpFromAllocatedIpMap(network, extClient.Address6)
115+
}
107116
deleteExtClientFromCache(key)
108117
}
109118
return nil
@@ -333,15 +342,16 @@ func SaveExtClient(extclient *models.ExtClient) error {
333342
}
334343
if servercfg.CacheEnabled() {
335344
storeExtClientInCache(key, *extclient)
336-
}
337-
if _, ok := allocatedIpMap[extclient.Network]; ok {
338-
if extclient.Address != "" {
339-
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address))
340-
}
341-
if extclient.Address6 != "" {
342-
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address6))
345+
if _, ok := allocatedIpMap[extclient.Network]; ok {
346+
if extclient.Address != "" {
347+
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address))
348+
}
349+
if extclient.Address6 != "" {
350+
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address6))
351+
}
343352
}
344353
}
354+
345355
return SetNetworkNodesLastModified(extclient.Network)
346356
}
347357

logic/networks.go

+122-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ var (
3030

3131
// SetAllocatedIpMap - set allocated ip map for networks
3232
func SetAllocatedIpMap() error {
33+
if !servercfg.CacheEnabled() {
34+
return nil
35+
}
3336
logger.Log(0, "start setting up allocated ip map")
3437
if allocatedIpMap == nil {
3538
allocatedIpMap = map[string]map[string]net.IP{}
@@ -84,30 +87,46 @@ func SetAllocatedIpMap() error {
8487

8588
// ClearAllocatedIpMap - set allocatedIpMap to nil
8689
func ClearAllocatedIpMap() {
90+
if !servercfg.CacheEnabled() {
91+
return
92+
}
8793
allocatedIpMap = nil
8894
}
8995

9096
func AddIpToAllocatedIpMap(networkName string, ip net.IP) {
97+
if !servercfg.CacheEnabled() {
98+
return
99+
}
91100
networkCacheMutex.Lock()
92101
allocatedIpMap[networkName][ip.String()] = ip
93102
networkCacheMutex.Unlock()
94103
}
95104

96105
func RemoveIpFromAllocatedIpMap(networkName string, ip string) {
106+
if !servercfg.CacheEnabled() {
107+
return
108+
}
97109
networkCacheMutex.Lock()
98110
delete(allocatedIpMap[networkName], ip)
99111
networkCacheMutex.Unlock()
100112
}
101113

102114
// AddNetworkToAllocatedIpMap - add network to allocated ip map when network is added
103115
func AddNetworkToAllocatedIpMap(networkName string) {
116+
//add new network to allocated ip map
117+
if !servercfg.CacheEnabled() {
118+
return
119+
}
104120
networkCacheMutex.Lock()
105121
allocatedIpMap[networkName] = make(map[string]net.IP)
106122
networkCacheMutex.Unlock()
107123
}
108124

109125
// RemoveNetworkFromAllocatedIpMap - remove network from allocated ip map when network is deleted
110126
func RemoveNetworkFromAllocatedIpMap(networkName string) {
127+
if !servercfg.CacheEnabled() {
128+
return
129+
}
111130
networkCacheMutex.Lock()
112131
delete(allocatedIpMap, networkName)
113132
networkCacheMutex.Unlock()
@@ -354,7 +373,7 @@ func GetNetworkSettings(networkname string) (models.Network, error) {
354373
}
355374

356375
// UniqueAddress - get a unique ipv4 address
357-
func UniqueAddress(networkName string, reverse bool) (net.IP, error) {
376+
func UniqueAddressCache(networkName string, reverse bool) (net.IP, error) {
358377
add := net.IP{}
359378
var network models.Network
360379
network, err := GetParentNetwork(networkName)
@@ -396,6 +415,49 @@ func UniqueAddress(networkName string, reverse bool) (net.IP, error) {
396415
return add, errors.New("ERROR: No unique addresses available. Check network subnet")
397416
}
398417

418+
// UniqueAddress - get a unique ipv4 address
419+
func UniqueAddressDB(networkName string, reverse bool) (net.IP, error) {
420+
add := net.IP{}
421+
var network models.Network
422+
network, err := GetParentNetwork(networkName)
423+
if err != nil {
424+
logger.Log(0, "UniqueAddressServer encountered an error")
425+
return add, err
426+
}
427+
428+
if network.IsIPv4 == "no" {
429+
return add, fmt.Errorf("IPv4 not active on network " + networkName)
430+
}
431+
//ensure AddressRange is valid
432+
if _, _, err := net.ParseCIDR(network.AddressRange); err != nil {
433+
logger.Log(0, "UniqueAddress encountered an error")
434+
return add, err
435+
}
436+
net4 := iplib.Net4FromStr(network.AddressRange)
437+
newAddrs := net4.FirstAddress()
438+
439+
if reverse {
440+
newAddrs = net4.LastAddress()
441+
}
442+
443+
for {
444+
if IsIPUnique(networkName, newAddrs.String(), database.NODES_TABLE_NAME, false) &&
445+
IsIPUnique(networkName, newAddrs.String(), database.EXT_CLIENT_TABLE_NAME, false) {
446+
return newAddrs, nil
447+
}
448+
if reverse {
449+
newAddrs, err = net4.PreviousIP(newAddrs)
450+
} else {
451+
newAddrs, err = net4.NextIP(newAddrs)
452+
}
453+
if err != nil {
454+
break
455+
}
456+
}
457+
458+
return add, errors.New("ERROR: No unique addresses available. Check network subnet")
459+
}
460+
399461
// IsIPUnique - checks if an IP is unique
400462
func IsIPUnique(network string, ip string, tableName string, isIpv6 bool) bool {
401463

@@ -439,9 +501,67 @@ func IsIPUnique(network string, ip string, tableName string, isIpv6 bool) bool {
439501

440502
return isunique
441503
}
504+
func UniqueAddress(networkName string, reverse bool) (net.IP, error) {
505+
if servercfg.CacheEnabled() {
506+
return UniqueAddressCache(networkName, reverse)
507+
}
508+
return UniqueAddressDB(networkName, reverse)
509+
}
442510

443-
// UniqueAddress6 - see if ipv6 address is unique
444511
func UniqueAddress6(networkName string, reverse bool) (net.IP, error) {
512+
if servercfg.CacheEnabled() {
513+
return UniqueAddress6Cache(networkName, reverse)
514+
}
515+
return UniqueAddress6DB(networkName, reverse)
516+
}
517+
518+
// UniqueAddress6DB - see if ipv6 address is unique
519+
func UniqueAddress6DB(networkName string, reverse bool) (net.IP, error) {
520+
add := net.IP{}
521+
var network models.Network
522+
network, err := GetParentNetwork(networkName)
523+
if err != nil {
524+
fmt.Println("Network Not Found")
525+
return add, err
526+
}
527+
if network.IsIPv6 == "no" {
528+
return add, fmt.Errorf("IPv6 not active on network " + networkName)
529+
}
530+
531+
//ensure AddressRange is valid
532+
if _, _, err := net.ParseCIDR(network.AddressRange6); err != nil {
533+
return add, err
534+
}
535+
net6 := iplib.Net6FromStr(network.AddressRange6)
536+
537+
newAddrs, err := net6.NextIP(net6.FirstAddress())
538+
if reverse {
539+
newAddrs, err = net6.PreviousIP(net6.LastAddress())
540+
}
541+
if err != nil {
542+
return add, err
543+
}
544+
545+
for {
546+
if IsIPUnique(networkName, newAddrs.String(), database.NODES_TABLE_NAME, true) &&
547+
IsIPUnique(networkName, newAddrs.String(), database.EXT_CLIENT_TABLE_NAME, true) {
548+
return newAddrs, nil
549+
}
550+
if reverse {
551+
newAddrs, err = net6.PreviousIP(newAddrs)
552+
} else {
553+
newAddrs, err = net6.NextIP(newAddrs)
554+
}
555+
if err != nil {
556+
break
557+
}
558+
}
559+
560+
return add, errors.New("ERROR: No unique IPv6 addresses available. Check network subnet")
561+
}
562+
563+
// UniqueAddress6Cache - see if ipv6 address is unique using cache
564+
func UniqueAddress6Cache(networkName string, reverse bool) (net.IP, error) {
445565
add := net.IP{}
446566
var network models.Network
447567
network, err := GetParentNetwork(networkName)

0 commit comments

Comments
 (0)