s6-netdev/navi-desktop/main.go

127 lines
2.7 KiB
Go

package main
import (
// "net/netip"
"fmt"
"log"
"net"
"os"
"git.redxen.eu/nnd/s6-netdev"
)
func main() {
var (
err error
l = log.New(os.Stderr, "s6-netdev: ", log.Ltime|log.Lshortfile|log.Lmsgprefix)
ifs = make(s6netdev.IfMap)
)
t := s6netdev.S6NewTree()
// Loopback
ifs.AddIf(&s6netdev.Iface{
Name: "lo",
Type: &s6netdev.NetdevIfTypes.Loopback,
})
// Physical bridged interfaces
ifs.AddIf(&s6netdev.Iface{
Name: "enp4s0",
Type: &s6netdev.NetdevIfTypes.Phys,
})
// Bridge interfaces
ifs.AddIf(&s6netdev.Iface{
Name: "phys",
Type: &s6netdev.NetdevIfTypes.Bridge,
MACAddr: net.HardwareAddr{0x24, 0x4b, 0xfe, 0x52, 0x51, 0xac},
DHCP: s6netdev.DHCP_IP{V4: true},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "0", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "0", Default: "0"},
},
},
})
// Bridge interfaces
ifs.AddIf(&s6netdev.Iface{
Name: "phys",
Type: &s6netdev.NetdevIfTypes.Bridge,
MACAddr: net.HardwareAddr{0x24, 0x4b, 0xfe, 0x52, 0x51, 0xac},
DHCP: s6netdev.DHCP_IP{V4: true},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "0", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "0", Default: "0"},
},
},
})
ifs.AddIf(&s6netdev.Iface{
Name: "b00b",
Type: &s6netdev.NetdevIfTypes.Bridge,
MACAddr: net.HardwareAddr{0x24, 0x4b, 0xfe, 0x52, 0x51, 0xac},
Properties: []s6netdev.Property{
{Key: "stp_state", Value: "0", Default: "0"},
{Key: "mcast_snooping", Value: "0", Default: "1"},
},
Sysctls: s6netdev.Sysctl_IP{
V6: []s6netdev.Property{
{Key: "forwarding", Value: "0", Default: "0"},
},
},
})
// VLAN Interfaces
ifs.AddIf(&s6netdev.Iface{
Name: fmt.Sprintf("enp4s0.%d", 66),
Type: &s6netdev.NetdevIfTypes.Vlan,
VlanId: s6netdev.VLAN(66),
})
// Parent of VLANs
ifs[fmt.Sprintf("enp4s0.%d", 66)].Parent = ifs["enp4s0"]
for _, m := range []struct {
Master string
Slaves []string
}{
{
Master: "phys",
Slaves: []string{
"enp4s0",
},
},
{
Master: "b00b",
Slaves: []string{
"enp4s0.66",
},
},
} {
master := ifs[m.Master]
for _, slave := range m.Slaves {
master.Slaves = append(master.Slaves, ifs[slave])
}
ifs[m.Master] = master
}
for _, v := range ifs {
t.Services(*v)
}
for _, v := range t.S6Services() {
if s6netdev.NetdevIsDummy(v.Name) {
continue
}
l.Printf("Commiting %s\n", v.Name)
if err = t.S6CommitService(v); err != nil {
l.Fatalf("Failed to commit %s, %s\n", v.Name, err)
}
}
}