Skip to content

Commit

Permalink
IPAssigner UT Vlan
Browse files Browse the repository at this point in the history
Signed-off-by: Rajnish Kumar <[email protected]>
  • Loading branch information
rajnkamr committed Mar 29, 2024
1 parent e366d58 commit d5b8495
Show file tree
Hide file tree
Showing 6 changed files with 1,221 additions and 14 deletions.
1 change: 1 addition & 0 deletions hack/update-codegen-dockerized.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ MOCKGEN_TARGETS=(
"pkg/agent/querier AgentQuerier testing"
"pkg/agent/route Interface testing"
"pkg/agent/ipassigner IPAssigner testing"
"pkg/agent/ipassigner/responder Responder testing"
"pkg/agent/secondarynetwork/podwatch InterfaceConfigurator,IPAMAllocator testing"
"pkg/agent/servicecidr Interface testing"
"pkg/agent/util/ipset Interface testing"
Expand Down
45 changes: 31 additions & 14 deletions pkg/agent/ipassigner/ip_assigner_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ import (
crdv1b1 "antrea.io/antrea/pkg/apis/crd/v1beta1"
)

var (
netlinkAdd = netlink.LinkAdd
netlinkDel = netlink.LinkDel
ensureRPF = util.EnsureRPFilterOnInterface
netlinkSetUp = netlink.LinkSetUp
netInterfaceByName = net.InterfaceByName
netlinkAddrAdd = netlink.AddrAdd
netlinkAddrDel = netlink.AddrDel
netlinkAddrList = netlink.AddrList
)

func advertiseFnc(ip net.IP, externalInterface *net.Interface) {
}

// VLAN interfaces created by antrea-agent will be named with the prefix.
// For example, when VLAN ID is 10, the name will be antrea-ext.10.
// It can be used to determine whether it's safe to delete an interface when it's no longer used.
Expand All @@ -55,7 +69,8 @@ type assignee struct {
// NDP queries itself.
ndpResponder responder.Responder
// ips tracks IPs that have been assigned to this assignee.
ips sets.Set[string]
ips sets.Set[string]
advertiseFn func(ip net.IP, externalInterface *net.Interface)
}

// deletable returns whether this assignee can be safely deleted.
Expand All @@ -79,7 +94,7 @@ func (as *assignee) deletable() bool {
}

func (as *assignee) destroy() error {
if err := netlink.LinkDel(as.link); err != nil {
if err := netlinkDel(as.link); err != nil {
return fmt.Errorf("error deleting interface %v: %w", as.link, err)
}
return nil
Expand All @@ -89,7 +104,7 @@ func (as *assignee) assign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
// If there is a real link, add the IP to its address list.
if as.link != nil {
addr := getIPNet(ip, subnetInfo)
if err := netlink.AddrAdd(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if err := netlinkAddrAdd(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EEXIST) {
return fmt.Errorf("failed to add IP %v to interface %s: %v", addr, as.link.Attrs().Name, err)
} else {
Expand All @@ -111,20 +126,20 @@ func (as *assignee) assign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
}
}
// Always advertise the IP when the IP is newly assigned to this Node.
as.advertise(ip)
as.advertiseFn(ip, as.logicalInterface)
as.ips.Insert(ip.String())
return nil
}

func (as *assignee) advertise(ip net.IP) {
func advertise(ip net.IP, logicalInterface *net.Interface) {
if utilnet.IsIPv4(ip) {
klog.V(2).InfoS("Sending gratuitous ARP", "ip", ip)
if err := arping.GratuitousARPOverIface(ip, as.logicalInterface); err != nil {
if err := arping.GratuitousARPOverIface(ip, logicalInterface); err != nil {
klog.ErrorS(err, "Failed to send gratuitous ARP", "ip", ip)
}
} else {
klog.V(2).InfoS("Sending neighbor advertisement", "ip", ip)
if err := ndp.NeighborAdvertisement(ip, as.logicalInterface); err != nil {
if err := ndp.NeighborAdvertisement(ip, logicalInterface); err != nil {
klog.ErrorS(err, "Failed to send neighbor advertisement", "ip", ip)
}
}
Expand All @@ -134,7 +149,7 @@ func (as *assignee) unassign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
// If there is a real link, delete the IP from its address list.
if as.link != nil {
addr := getIPNet(ip, subnetInfo)
if err := netlink.AddrDel(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if err := netlinkAddrDel(as.link, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EADDRNOTAVAIL) {
return fmt.Errorf("failed to delete IP %v from interface %s: %v", ip, as.link.Attrs().Name, err)
} else {
Expand Down Expand Up @@ -171,7 +186,7 @@ func (as *assignee) getVLANID() (int, bool) {

func (as *assignee) loadIPAddresses() (map[string]*crdv1b1.SubnetInfo, error) {
assignedIPs := map[string]*crdv1b1.SubnetInfo{}
addresses, err := netlink.AddrList(as.link, netlink.FAMILY_ALL)
addresses, err := netlinkAddrList(as.link, netlink.FAMILY_ALL)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -267,6 +282,7 @@ func NewIPAssigner(nodeTransportInterface string, dummyDeviceName string) (IPAss
for _, vlan := range vlans {
a.addVLANAssignee(vlan, int32(vlan.VlanId))
}
a.defaultAssignee.advertiseFn = advertise
return a, nil
}

Expand Down Expand Up @@ -362,7 +378,7 @@ func (a *ipAssigner) AssignIP(ip string, subnetInfo *crdv1b1.SubnetInfo, forceAd
if crdv1b1.CompareSubnetInfo(subnetInfo, oldSubnetInfo, true) {
klog.V(2).InfoS("The IP is already assigned", "ip", ip)
if forceAdvertise {
as.advertise(parsedIP)
as.advertiseFn(parsedIP, as.logicalInterface)
}
return false, nil
}
Expand Down Expand Up @@ -497,15 +513,15 @@ func (a *ipAssigner) getAssignee(subnetInfo *crdv1b1.SubnetInfo, createIfNotExis
},
VlanId: int(subnetInfo.VLAN),
}
if err := netlink.LinkAdd(vlan); err != nil {
if err := netlinkAdd(vlan); err != nil {
if !errors.Is(err, unix.EEXIST) {
return nil, fmt.Errorf("error creating VLAN sub-interface for VLAN %d", subnetInfo.VLAN)
}
}
// Loose mode is needed because incoming traffic received on the interface is expected to be received on the parent
// external interface when looking up the main table. To make it look up the custom table, we will need to restore
// the mark on the reply traffic and turn on src_valid_mark on this interface, which is more complicated.
if err := util.EnsureRPFilterOnInterface(name, 2); err != nil {
if err := ensureRPF(name, 2); err != nil {
return nil, err
}
as, err := a.addVLANAssignee(vlan, subnetInfo.VLAN)
Expand All @@ -516,10 +532,10 @@ func (a *ipAssigner) getAssignee(subnetInfo *crdv1b1.SubnetInfo, createIfNotExis
}

func (a *ipAssigner) addVLANAssignee(link netlink.Link, vlan int32) (*assignee, error) {
if err := netlink.LinkSetUp(link); err != nil {
if err := netlinkSetUp(link); err != nil {
return nil, fmt.Errorf("error setting up interface %v", link)
}
iface, err := net.InterfaceByName(link.Attrs().Name)
iface, err := netInterfaceByName(link.Attrs().Name)
if err != nil {
return nil, err
}
Expand All @@ -528,6 +544,7 @@ func (a *ipAssigner) addVLANAssignee(link netlink.Link, vlan int32) (*assignee,
logicalInterface: iface,
link: link,
ips: sets.New[string](),
advertiseFn: advertiseFnc,
}
a.vlanAssignees[vlan] = as
return as, nil
Expand Down
Loading

0 comments on commit d5b8495

Please sign in to comment.