labctl: segments sur des bridges de l'hôte, routeurs et passerelle VRRP #2
All checks were successful
Tests / test (push) Successful in 3m46s

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-13 09:57:09 +02:00
commit e9a0c19b72
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
24 changed files with 1766 additions and 578 deletions

View file

@ -12,9 +12,10 @@ import (
)
const (
SwitchScript = "/usr/local/sbin/lab-switch"
SwitchNFT = "/etc/lab-switch.nft"
SwitchUnit = "/etc/systemd/system/lab-switch.service"
RouterScript = "/usr/local/sbin/lab-router"
RouterNFT = "/etc/lab-router.nft"
RouterUnit = "/etc/systemd/system/lab-router.service"
VRRPPrefix = "vr"
NodeScript = "/usr/local/sbin/lab-node"
NodeUnit = "/etc/systemd/system/lab-node.service"
@ -99,14 +100,14 @@ func userData(p *topology.Plan, n topology.NodePlan, o Options) ([]byte, error)
}
var steps []string
switch {
case n.Role == topology.RoleSwitch:
case n.Role == topology.RoleRouter:
cfg.Packages = []string{"nftables"}
cfg.WriteFiles = []writeFile{
{Path: SwitchScript, Permissions: "0755", Content: switchScript(p, n)},
{Path: SwitchNFT, Permissions: "0644", Content: switchNFT(p, n.Name)},
{Path: SwitchUnit, Permissions: "0644", Content: unit("Lab switch: bridges, gateways and NAT", SwitchScript)},
{Path: RouterScript, Permissions: "0755", Content: routerScript(p, n)},
{Path: RouterNFT, Permissions: "0644", Content: routerNFT(p, n.Name)},
{Path: RouterUnit, Permissions: "0644", Content: unit("Lab router: gateways and NAT", RouterScript)},
}
steps = append(steps, "systemctl daemon-reload", "systemctl enable --now lab-switch.service")
steps = append(steps, "systemctl daemon-reload", "systemctl enable --now lab-router.service")
case n.Loopback.IsValid():
cfg.WriteFiles = []writeFile{
{Path: NodeScript, Permissions: "0755", Content: "#!/bin/sh\nset -eu\n" + loopbackLines(n)},
@ -126,11 +127,11 @@ func userData(p *topology.Plan, n topology.NodePlan, o Options) ([]byte, error)
cfg.WriteFiles = append(cfg.WriteFiles, writeFile{Path: NodeProvision, Permissions: "0755", Encoding: "b64", Content: base64.StdEncoding.EncodeToString([]byte(o.Provision[n.Name]))})
steps = append(steps, provisionEnv(p, n)+NodeProvision)
}
if n.FRR != "" {
if NeedsFRR(p, n) {
cfg.WriteFiles = append(cfg.WriteFiles,
writeFile{Path: FRRKey, Permissions: "0644", Encoding: "b64", Content: base64.StdEncoding.EncodeToString(frrKey)},
writeFile{Path: FRRConfig, Permissions: "0640", Content: o.FRR[n.Name]},
writeFile{Path: FRRScript, Permissions: "0755", Content: frrScript(n)},
writeFile{Path: FRRConfig, Permissions: "0640", Content: frrConfig(p, n, o.FRR[n.Name])},
writeFile{Path: FRRScript, Permissions: "0755", Content: frrScript(p, n)},
)
steps = append(steps, FRRScript)
}
@ -156,27 +157,77 @@ func retry(command string) string {
}
func provisionEnv(p *topology.Plan, n topology.NodePlan) string {
uplink := ""
if cables := nodeCables(p, n.Name); len(cables) > 0 {
uplink = cables[0].NodeInterface
port, _ := uplink(p, n.Name)
up := port.Interface
if n.Role == topology.RoleRouter {
up = AdminInterface
}
return fmt.Sprintf("LAB_NODE=%s LAB_UPLINK=%s LAB_FILES=%s ", n.Name, uplink, FilesDir)
return fmt.Sprintf("LAB_NODE=%s LAB_UPLINK=%s LAB_FILES=%s ", n.Name, up, FilesDir)
}
func frrDaemons(n topology.NodePlan) []string {
if n.Role == topology.RoleHost {
return []string{"bgpd"}
func vrrpSegments(p *topology.Plan, name string) []topology.SegmentPlan {
var out []topology.SegmentPlan
for _, s := range routedSegments(p, name) {
if s.VRID != 0 {
out = append(out, s)
}
}
return []string{"bgpd", "bfdd"}
return out
}
func frrScript(n topology.NodePlan) string {
func NeedsFRR(p *topology.Plan, n topology.NodePlan) bool {
return n.FRR != "" || len(vrrpSegments(p, n.Name)) > 0
}
func frrDaemons(p *topology.Plan, n topology.NodePlan) []string {
daemons := []string{"bgpd"}
if n.Role != topology.RoleHost {
daemons = append(daemons, "bfdd")
}
if len(vrrpSegments(p, n.Name)) > 0 {
daemons = append(daemons, "vrrpd")
}
return daemons
}
func frrConfig(p *topology.Plan, n topology.NodePlan, given string) string {
segments := vrrpSegments(p, n.Name)
if len(segments) == 0 {
return given
}
var b strings.Builder
if given == "" {
fmt.Fprintf(&b, "frr defaults traditional\nhostname %s\n!\n", n.Name)
} else {
b.WriteString(strings.TrimRight(given, "\n") + "\n")
}
for _, s := range segments {
fmt.Fprintf(&b, "interface %s\n vrrp %d version 3\n vrrp %d priority %d\n no vrrp %d preempt\n vrrp %d ip %s\nexit\n!\n",
s.Name, s.VRID, s.VRID, vrrpPriority(s, n.Name), s.VRID, s.VRID, s.Gateway)
}
return b.String()
}
func vrrpPriority(s topology.SegmentPlan, name string) int {
for i, r := range s.Routers {
if r == name {
return max(200-10*i, 1)
}
}
return 1
}
func vrrpMAC(vrid int) string {
return fmt.Sprintf("00:00:5e:00:01:%02x", vrid)
}
func frrScript(p *topology.Plan, n topology.NodePlan) string {
var b strings.Builder
b.WriteString("#!/bin/sh\nset -eu\nexport DEBIAN_FRONTEND=noninteractive\n. /etc/os-release\n")
fmt.Fprintf(&b, "echo \"deb [signed-by=%s] %s ${VERSION_CODENAME} %s\" > /etc/apt/sources.list.d/frr.list\n", FRRKey, FRRRepo, FRRSuite)
b.WriteString(retry("apt-get update -qq --error-on=any"))
fmt.Fprintf(&b, "apt-get install -y -qq --no-install-recommends %s\n", FRRPackages)
for _, d := range frrDaemons(n) {
for _, d := range frrDaemons(p, n) {
fmt.Fprintf(&b, "sed -i 's/^%s=no/%s=yes/' /etc/frr/daemons\n", d, d)
}
fmt.Fprintf(&b, "install -o frr -g frr -m 0640 %s /etc/frr/frr.conf\n", FRRConfig)
@ -199,77 +250,62 @@ func networkConfig(p *topology.Plan, n topology.NodePlan, index int) ([]byte, er
SetName: AdminInterface,
Addresses: []string{AdminAddress},
}
if n.Role == topology.RoleSwitch {
router := n.Role == topology.RoleRouter
if router {
admin.Routes = []route{{To: "0.0.0.0/0", Via: AdminGateway}}
admin.Nameservers = &nameservers{Addresses: []string{AdminDNS}}
for _, c := range switchCables(p, n.Name) {
doc.Ethernets[c.SwitchInterface] = ethernet{
Match: match{MACAddress: c.SwitchMAC.String()},
SetName: c.SwitchInterface,
MTU: c.MTU,
}
}
}
doc.Ethernets[AdminInterface] = admin
for i, c := range nodeCables(p, n.Name) {
up, routed := uplink(p, n.Name)
for _, port := range nodePorts(p, n.Name) {
e := ethernet{
Match: match{MACAddress: c.NodeMAC.String()},
SetName: c.NodeInterface,
MTU: c.MTU,
Addresses: []string{c.NodeAddress.String()},
Match: match{MACAddress: port.MAC.String()},
SetName: port.Interface,
MTU: port.MTU,
Addresses: []string{port.Address.String()},
}
for _, prefix := range n.Secondary[c.Segment] {
for _, prefix := range n.Secondary[port.Segment] {
e.Addresses = append(e.Addresses, prefix.String())
}
if i == 0 {
e.Routes = []route{{To: "0.0.0.0/0", Via: gatewayOf(p, c.Segment)}}
if !router && routed && port.Segment == up.Segment {
e.Routes = []route{{To: "0.0.0.0/0", Via: segmentNamed(p, port.Segment).Gateway.String()}}
e.Nameservers = &nameservers{Addresses: Nameservers}
}
doc.Ethernets[c.NodeInterface] = e
doc.Ethernets[port.Interface] = e
}
return yaml.Marshal(doc)
}
func gatewayOf(p *topology.Plan, segment string) string {
for _, s := range p.Segments {
if s.Name == segment {
return s.Gateway.String()
}
}
return ""
}
func switchScript(p *topology.Plan, n topology.NodePlan) string {
name := n.Name
func routerScript(p *topology.Plan, n topology.NodePlan) string {
var b strings.Builder
b.WriteString("#!/bin/sh\nset -eu\nsysctl -qw net.ipv4.ip_forward=1\n")
for _, s := range switchSegments(p, name) {
fmt.Fprintf(&b, "ip link add %s type bridge stp_state 0 2>/dev/null || true\n", s.Bridge)
for _, c := range switchCables(p, name) {
if c.Segment != s.Name {
continue
}
fmt.Fprintf(&b, "ip link set dev %s master %s\n", c.SwitchInterface, s.Bridge)
fmt.Fprintf(&b, "ip link set dev %s up\n", c.SwitchInterface)
for _, s := range routedSegments(p, n.Name) {
gateway := fmt.Sprintf("%s/%d", s.Gateway, s.Network.Bits())
if s.VRID == 0 {
fmt.Fprintf(&b, "ip addr replace %s dev %s\n", gateway, s.Name)
continue
}
fmt.Fprintf(&b, "ip link set dev %s mtu %d\n", s.Bridge, s.MTU)
fmt.Fprintf(&b, "ip addr replace %s/%d dev %s\n", s.Gateway, s.Network.Bits(), s.Bridge)
for _, prefix := range n.Secondary[s.Name] {
fmt.Fprintf(&b, "ip addr replace %s dev %s\n", prefix, s.Bridge)
}
fmt.Fprintf(&b, "ip link set dev %s up\n", s.Bridge)
vrrp := VRRPPrefix + s.Name
fmt.Fprintf(&b, "sysctl -qw net.ipv4.conf.%s.arp_ignore=1 net.ipv4.conf.%s.arp_announce=2\n", s.Name, s.Name)
fmt.Fprintf(&b, "ip link show dev %s >/dev/null 2>&1 || { ip link add %s link %s type macvlan mode bridge && ip link set dev %s protodown on; }\n", vrrp, vrrp, s.Name, vrrp)
fmt.Fprintf(&b, "ip link set dev %s addrgenmode random address %s\n", vrrp, vrrpMAC(s.VRID))
fmt.Fprintf(&b, "ip addr replace %s dev %s\n", gateway, vrrp)
fmt.Fprintf(&b, "ip link set dev %s up\n", vrrp)
}
b.WriteString(loopbackLines(n))
fmt.Fprintf(&b, "nft -f %s\n", SwitchNFT)
fmt.Fprintf(&b, "nft -f %s\n", RouterNFT)
return b.String()
}
func switchNFT(p *topology.Plan, name string) string {
func routerNFT(p *topology.Plan, name string) string {
var networks []string
for _, s := range switchSegments(p, name) {
for _, s := range routedSegments(p, name) {
networks = append(networks, s.Network.String())
}
if len(networks) == 0 {
return "add table ip lab_nat\ndelete table ip lab_nat\n"
}
return fmt.Sprintf(`add table ip lab_nat
delete table ip lab_nat
table ip lab_nat {