All checks were successful
Documentation / publish (push) Successful in 2m55s
Release Pipeline / set-release-target (push) Successful in 3s
Release Pipeline / upload-assets (run-dnsmasq-in-netns.sh, scripts/run-dnsmasq-in-netns.sh) (push) Successful in 7s
Release Pipeline / checksums (push) Successful in 9s
Release Pipeline / upload-assets (agent.service, systemd/agent.service) (push) Successful in 8s
Release Pipeline / upload-assets (dhcp@.service, systemd/dhcp@.service) (push) Successful in 7s
Release Pipeline / upload-assets (dnsmasq@.service, systemd/dnsmasq@.service) (push) Successful in 7s
Release Pipeline / upload-assets (metadata@.service, systemd/metadata@.service) (push) Successful in 7s
Release Pipeline / upload-assets (run-dhcp-in-netns.sh, scripts/run-dhcp-in-netns.sh) (push) Successful in 7s
Release Pipeline / build (agent, amd64, linux) (push) Successful in 0s
Release Pipeline / build (dhcp, amd64, linux) (push) Successful in 0s
Release Pipeline / build (metadata, amd64, linux) (push) Successful in 0s
Release Pipeline / release (push) Successful in 32s
Release Pipeline / publish (push) Successful in 0s
Release Pipeline / build (push) Successful in 2m12s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
51 lines
1 KiB
Go
51 lines
1 KiB
Go
package netif
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"syscall"
|
|
|
|
"github.com/vishvananda/netlink"
|
|
)
|
|
|
|
const (
|
|
scopeUniverse = 0
|
|
addressSecondary = 0x01
|
|
)
|
|
|
|
func CreateVxlan(name string, vxlanID int, localIface string, mtu int) error {
|
|
link, err := netlink.LinkByName(localIface)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
addrs, err := netlink.AddrList(link, syscall.AF_INET)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
local, err := vtepAddress(localIface, addrs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
vxlan := &netlink.Vxlan{
|
|
LinkAttrs: netlink.LinkAttrs{
|
|
Name: name,
|
|
MTU: mtu,
|
|
},
|
|
VxlanId: vxlanID,
|
|
Port: 4789,
|
|
VtepDevIndex: link.Attrs().Index,
|
|
SrcAddr: local,
|
|
Learning: false,
|
|
}
|
|
return netlink.LinkAdd(vxlan)
|
|
}
|
|
|
|
func vtepAddress(iface string, addrs []netlink.Addr) (net.IP, error) {
|
|
for _, a := range addrs {
|
|
if a.IPNet == nil || a.IP.To4() == nil || a.Scope != scopeUniverse || a.Flags&addressSecondary != 0 {
|
|
continue
|
|
}
|
|
return a.IP.To4(), nil
|
|
}
|
|
return nil, fmt.Errorf("no primary global IPv4 address on %s for the VXLAN local endpoint", iface)
|
|
}
|