Skip to content

Commit

Permalink
Merge pull request #313 from OrangeBao/fix_nic
Browse files Browse the repository at this point in the history
fix: add VtepDevIndex param for vxlan
  • Loading branch information
kosmos-robot authored Dec 7, 2023
2 parents aeb0e09 + 7d110dd commit 03f7523
Showing 1 changed file with 21 additions and 65 deletions.
86 changes: 21 additions & 65 deletions pkg/clusterlink/network/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 03f7523

Please sign in to comment.