All checks were successful
Tests / test (push) Successful in 3m46s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
219 lines
4.9 KiB
Go
219 lines
4.9 KiB
Go
package topology
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"strconv"
|
|
)
|
|
|
|
const (
|
|
SSHBasePort = 2200
|
|
|
|
BridgePrefix = "lb-"
|
|
TapPrefix = "lt"
|
|
|
|
maxNodes = 1000
|
|
maxSegments = 256
|
|
)
|
|
|
|
type Plan struct {
|
|
Name string
|
|
Images []Image
|
|
Segments []SegmentPlan
|
|
Nodes []NodePlan
|
|
Ports []Port
|
|
}
|
|
|
|
type SegmentPlan struct {
|
|
Name string
|
|
Bridge string
|
|
Network netip.Prefix
|
|
Gateway netip.Addr
|
|
Routers []string
|
|
VRID int
|
|
MTU int
|
|
}
|
|
|
|
type NodePlan struct {
|
|
Name string
|
|
Role string
|
|
Image string
|
|
CPUs int
|
|
Memory int
|
|
SSHPort int
|
|
Secondary map[string][]netip.Prefix
|
|
Loopback netip.Prefix
|
|
FRR string
|
|
Provision string
|
|
PostProvision string
|
|
Files map[string]string
|
|
Services []string
|
|
}
|
|
|
|
type Port struct {
|
|
Segment string
|
|
MTU int
|
|
Node string
|
|
Interface string
|
|
MAC net.HardwareAddr
|
|
Address netip.Prefix
|
|
Tap string
|
|
}
|
|
|
|
func Compute(t *Topology) (*Plan, error) {
|
|
if err := t.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
p := &Plan{Name: t.Name, Images: append([]Image(nil), t.Images...)}
|
|
for i, n := range t.Nodes {
|
|
node := NodePlan{
|
|
Name: n.Name,
|
|
Role: n.Role,
|
|
Image: n.Image,
|
|
CPUs: n.CPUs,
|
|
Memory: n.Memory,
|
|
SSHPort: SSHBasePort + i,
|
|
FRR: n.FRR,
|
|
Provision: n.Provision,
|
|
PostProvision: n.PostProvision,
|
|
Files: n.Files,
|
|
Services: n.Services,
|
|
}
|
|
for segment, raws := range n.Secondary {
|
|
for _, raw := range raws {
|
|
if node.Secondary == nil {
|
|
node.Secondary = map[string][]netip.Prefix{}
|
|
}
|
|
node.Secondary[segment] = append(node.Secondary[segment], netip.MustParsePrefix(raw))
|
|
}
|
|
}
|
|
if n.Loopback != "" {
|
|
node.Loopback = netip.MustParsePrefix(n.Loopback)
|
|
}
|
|
p.Nodes = append(p.Nodes, node)
|
|
}
|
|
|
|
var errs []error
|
|
for segIndex, s := range t.Segments {
|
|
network := netip.MustParsePrefix(s.CIDR)
|
|
segment := SegmentPlan{
|
|
Name: s.Name,
|
|
Bridge: BridgePrefix + s.Name,
|
|
Network: network,
|
|
Routers: append([]string(nil), s.Routers...),
|
|
MTU: s.MTU,
|
|
}
|
|
if len(s.Routers) > 0 {
|
|
segment.Gateway = network.Addr().Next()
|
|
}
|
|
if len(s.Routers) > 1 {
|
|
segment.VRID = segIndex + 1
|
|
}
|
|
p.Segments = append(p.Segments, segment)
|
|
|
|
addresses, err := allocate(t, s, network, segment.Gateway)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
|
|
for nodeIndex, n := range t.Nodes {
|
|
address, ok := addresses[n.Name]
|
|
if !ok {
|
|
continue
|
|
}
|
|
p.Ports = append(p.Ports, Port{
|
|
Segment: s.Name,
|
|
MTU: s.MTU,
|
|
Node: n.Name,
|
|
Interface: s.Name,
|
|
MAC: mac(nodeIndex, segIndex),
|
|
Address: netip.PrefixFrom(address, network.Bits()),
|
|
Tap: TapPrefix + strconv.Itoa(len(p.Ports)),
|
|
})
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
return nil, errors.Join(errs...)
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func allocate(t *Topology, s Segment, network netip.Prefix, gateway netip.Addr) (map[string]netip.Addr, error) {
|
|
broadcast := lastAddr(network)
|
|
used := map[netip.Addr]string{}
|
|
first := network.Addr().Next()
|
|
if gateway.IsValid() {
|
|
used[gateway] = "the gateway"
|
|
first = gateway.Next()
|
|
}
|
|
result := map[string]netip.Addr{}
|
|
var errs []error
|
|
|
|
var auto []string
|
|
for _, n := range t.Nodes {
|
|
if !contains(n.Segments, s.Name) {
|
|
continue
|
|
}
|
|
raw, ok := n.Addresses[s.Name]
|
|
if !ok {
|
|
auto = append(auto, n.Name)
|
|
continue
|
|
}
|
|
addr := netip.MustParseAddr(raw)
|
|
switch {
|
|
case !network.Contains(addr):
|
|
errs = append(errs, fmt.Errorf("node %s: address %s is outside segment %s (%s)", n.Name, addr, s.Name, network))
|
|
case addr == network.Addr() || addr == broadcast:
|
|
errs = append(errs, fmt.Errorf("node %s: address %s is the network or broadcast address of segment %s", n.Name, addr, s.Name))
|
|
case used[addr] != "":
|
|
errs = append(errs, fmt.Errorf("node %s: address %s on segment %s is already taken by %s", n.Name, addr, s.Name, used[addr]))
|
|
default:
|
|
used[addr] = n.Name
|
|
result[n.Name] = addr
|
|
}
|
|
}
|
|
|
|
next := first
|
|
for _, name := range auto {
|
|
for next != broadcast && used[next] != "" {
|
|
next = next.Next()
|
|
}
|
|
if next == broadcast {
|
|
errs = append(errs, fmt.Errorf("segment %s: no address left in %s for node %s", s.Name, network, name))
|
|
break
|
|
}
|
|
used[next] = name
|
|
result[name] = next
|
|
next = next.Next()
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
return nil, errors.Join(errs...)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func lastAddr(network netip.Prefix) netip.Addr {
|
|
a := network.Addr().As4()
|
|
host := uint32(1)<<(32-network.Bits()) - 1
|
|
v := (uint32(a[0])<<24 | uint32(a[1])<<16 | uint32(a[2])<<8 | uint32(a[3])) | host
|
|
return netip.AddrFrom4([4]byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
|
|
}
|
|
|
|
func mac(node, segment int) net.HardwareAddr {
|
|
return net.HardwareAddr{0x02, 0x4c, byte(node >> 8), byte(node), byte(segment), 0x00}
|
|
}
|
|
|
|
func contains(list []string, value string) bool {
|
|
for _, v := range list {
|
|
if v == value {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|