labctl: segments sur des bridges de l'hôte, routeurs et passerelle VRRP #2
All checks were successful
Tests / test (push) Successful in 3m46s
All checks were successful
Tests / test (push) Successful in 3m46s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
e0de56a64f
commit
e9a0c19b72
24 changed files with 1766 additions and 578 deletions
|
|
@ -9,8 +9,10 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
SSHBasePort = 2200
|
||||
CableBasePort = 20000
|
||||
SSHBasePort = 2200
|
||||
|
||||
BridgePrefix = "lb-"
|
||||
TapPrefix = "lt"
|
||||
|
||||
maxNodes = 1000
|
||||
maxSegments = 256
|
||||
|
|
@ -21,15 +23,16 @@ type Plan struct {
|
|||
Images []Image
|
||||
Segments []SegmentPlan
|
||||
Nodes []NodePlan
|
||||
Cables []Cable
|
||||
Ports []Port
|
||||
}
|
||||
|
||||
type SegmentPlan struct {
|
||||
Name string
|
||||
Switch string
|
||||
Bridge string
|
||||
Network netip.Prefix
|
||||
Gateway netip.Addr
|
||||
Routers []string
|
||||
VRID int
|
||||
MTU int
|
||||
}
|
||||
|
||||
|
|
@ -49,20 +52,14 @@ type NodePlan struct {
|
|||
Services []string
|
||||
}
|
||||
|
||||
type Cable struct {
|
||||
Segment string
|
||||
MTU int
|
||||
|
||||
Node string
|
||||
NodeInterface string
|
||||
NodeMAC net.HardwareAddr
|
||||
NodeAddress netip.Prefix
|
||||
NodePort int
|
||||
|
||||
Switch string
|
||||
SwitchInterface string
|
||||
SwitchMAC net.HardwareAddr
|
||||
SwitchPort int
|
||||
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) {
|
||||
|
|
@ -102,17 +99,22 @@ func Compute(t *Topology) (*Plan, error) {
|
|||
var errs []error
|
||||
for segIndex, s := range t.Segments {
|
||||
network := netip.MustParsePrefix(s.CIDR)
|
||||
gateway := network.Addr().Next()
|
||||
p.Segments = append(p.Segments, SegmentPlan{
|
||||
segment := SegmentPlan{
|
||||
Name: s.Name,
|
||||
Switch: s.Switch,
|
||||
Bridge: "br-" + s.Name,
|
||||
Bridge: BridgePrefix + s.Name,
|
||||
Network: network,
|
||||
Gateway: gateway,
|
||||
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, gateway)
|
||||
addresses, err := allocate(t, s, network, segment.Gateway)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
|
|
@ -123,26 +125,18 @@ func Compute(t *Topology) (*Plan, error) {
|
|||
if !ok {
|
||||
continue
|
||||
}
|
||||
cable := len(p.Cables)
|
||||
p.Cables = append(p.Cables, Cable{
|
||||
Segment: s.Name,
|
||||
MTU: s.MTU,
|
||||
Node: n.Name,
|
||||
NodeInterface: s.Name,
|
||||
NodeMAC: mac(nodeIndex, segIndex, 0),
|
||||
NodeAddress: netip.PrefixFrom(address, network.Bits()),
|
||||
NodePort: CableBasePort + 2*cable,
|
||||
Switch: s.Switch,
|
||||
SwitchInterface: "p" + strconv.Itoa(cable),
|
||||
SwitchMAC: mac(nodeIndex, segIndex, 1),
|
||||
SwitchPort: CableBasePort + 2*cable + 1,
|
||||
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 last := CableBasePort + 2*len(p.Cables) - 1; last > 65535 {
|
||||
errs = append(errs, fmt.Errorf("cables: %d cables need udp ports up to %d, beyond 65535", len(p.Cables), last))
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return nil, errors.Join(errs...)
|
||||
}
|
||||
|
|
@ -151,7 +145,12 @@ func Compute(t *Topology) (*Plan, error) {
|
|||
|
||||
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{gateway: s.Switch + " (gateway)"}
|
||||
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
|
||||
|
||||
|
|
@ -179,7 +178,7 @@ func allocate(t *Topology, s Segment, network netip.Prefix, gateway netip.Addr)
|
|||
}
|
||||
}
|
||||
|
||||
next := gateway.Next()
|
||||
next := first
|
||||
for _, name := range auto {
|
||||
for next != broadcast && used[next] != "" {
|
||||
next = next.Next()
|
||||
|
|
@ -206,8 +205,8 @@ func lastAddr(network netip.Prefix) netip.Addr {
|
|||
return netip.AddrFrom4([4]byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
|
||||
}
|
||||
|
||||
func mac(node, segment, side int) net.HardwareAddr {
|
||||
return net.HardwareAddr{0x02, 0x4c, byte(node >> 8), byte(node), byte(segment), byte(side)}
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue