From 7d110dd08ca56835c79d9bed739bd21dbe946f9c Mon Sep 17 00:00:00 2001 From: OrangeBao Date: Wed, 6 Dec 2023 17:56:50 +0800 Subject: [PATCH] fix: add VtepDevIndex param for vxlan Signed-off-by: OrangeBao --- pkg/clusterlink/network/device.go | 86 ++++++++----------------------- 1 file changed, 21 insertions(+), 65 deletions(-) diff --git a/pkg/clusterlink/network/device.go b/pkg/clusterlink/network/device.go index 04193e5fc..e4ae3e1f8 100644 --- a/pkg/clusterlink/network/device.go +++ b/pkg/clusterlink/network/device.go @@ -17,11 +17,11 @@ import ( ) type IfaceInfo struct { - MTU int - name string - // index int - ip string - ip6 string + MTU int + name string + index int + ip string + ip6 string } func getIfaceIPByName(name string) (*IfaceInfo, error) { @@ -35,6 +35,7 @@ func getIfaceIPByName(name string) (*IfaceInfo, error) { devIface.MTU = iface.Attrs().MTU devIface.name = iface.Attrs().Name + devIface.index = iface.Attrs().Index addrListV4, err := getFristScopeIPInLink(iface, name, netlink.FAMILY_V4) if err == nil { @@ -52,7 +53,7 @@ func getIfaceIPByName(name string) (*IfaceInfo, error) { return devIface, nil } -func createNewVxlanIface(name string, addrIPWithMask *netlink.Addr, vxlanId int, vxlanPort int, hardwareAddr net.HardwareAddr, rIface *IfaceInfo, deviceIP string) error { +func createNewVxlanIface(name string, addrIPWithMask *netlink.Addr, vxlanId int, vxlanPort int, hardwareAddr net.HardwareAddr, rIface *IfaceInfo, deviceIP string, vtepDevIndex int) error { // srcAddr := rIface.ip klog.Infof("name %v ------------------------- %v", name, deviceIP) @@ -63,10 +64,11 @@ func createNewVxlanIface(name string, addrIPWithMask *netlink.Addr, vxlanId int, Flags: net.FlagUp, HardwareAddr: hardwareAddr, }, - SrcAddr: net.ParseIP(deviceIP), - VxlanId: vxlanId, - Port: vxlanPort, - Learning: false, + SrcAddr: net.ParseIP(deviceIP), + VxlanId: vxlanId, + Port: vxlanPort, + Learning: false, + VtepDevIndex: vtepDevIndex, } err := netlink.LinkAdd(iface) @@ -101,30 +103,6 @@ func createNewVxlanIface(name string, addrIPWithMask *netlink.Addr, vxlanId int, return nil } -func CIDRIPGenerator(ipcidr string, internalIP string) (*net.IP, error) { - cidrip, ipNet, err := net.ParseCIDR(ipcidr) - if err != nil { - return nil, fmt.Errorf("CIDRIPGenerator err: %v", err) - } - - nodeIP := net.ParseIP(internalIP) - - ret := net.ParseIP("0.0.0.0") - for i := range ipNet.Mask { - ret[len(ret)-i-1] = ^byte(ipNet.Mask[len(ipNet.Mask)-i-1]) - } - - for i := range nodeIP { - ret[i] = byte(ret[i]) & byte(nodeIP[i]) - } - - for i := range cidrip { - ret[i] = byte(ret[i]) | byte(cidrip[i]) - } - - return &ret, nil -} - // load device info from environment func loadDevices() ([]clusterlinkv1alpha1.Device, error) { ret := []clusterlinkv1alpha1.Device{} @@ -179,39 +157,16 @@ func loadDevices() ([]clusterlinkv1alpha1.Device, error) { continue } - addrListAll, err := netlink.AddrList(nil, d.family) - - if err != nil { - return nil, err - } - - interfaceIndex := -1 - vxlanIPAddr := vxlanNet.IP.String() - - for _, r := range addrListAll { - if r.LinkIndex != vxlanIface.Attrs().Index { - if ip, err := CIDRIPGenerator(d.cidr, r.IP.String()); err == nil { - if ip.String() == vxlanIPAddr { - interfaceIndex = r.LinkIndex - break - } - } - } - } - + interfaceIndex := vxlan.VtepDevIndex bindDev := "" - if interfaceIndex == -1 { - klog.Warningf("can not find the phys_dev for vxlan, name: %s", d.name) + defaultIface, err := netlink.LinkByIndex(interfaceIndex) + if err != nil { + klog.Errorf("When we get device by linkinx, get error : %v", err) + ret = append(ret, createNoneDevice()) + continue } else { - defaultIface, err := netlink.LinkByIndex(interfaceIndex) - if err != nil { - klog.Errorf("When we get device by linkinx, get error : %v", err) - ret = append(ret, createNoneDevice()) - continue - } else { - bindDev = defaultIface.Attrs().Name - } + bindDev = defaultIface.Attrs().Name } ret = append(ret, clusterlinkv1alpha1.Device{ @@ -262,8 +217,9 @@ func addDevice(d clusterlinkv1alpha1.Device) error { deviceIP = currentIfaceInfo.ip6 family = netlink.FAMILY_V6 } + vtepDevIndex := currentIfaceInfo.index - if err = createNewVxlanIface(d.Name, addrIPvWithMask, id, port, hardwareAddr, currentIfaceInfo, deviceIP); err != nil { + if err = createNewVxlanIface(d.Name, addrIPvWithMask, id, port, hardwareAddr, currentIfaceInfo, deviceIP, vtepDevIndex); err != nil { klog.Errorf("ipv4 err: %v", err) return err }