Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

routing: Fix incorrect interface selection for egress pod routes #17169

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 28 additions & 23 deletions pkg/datapath/linux/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,38 +275,43 @@ func deleteRule(r route.Rule) error {
}

// retrieveIfIndexFromMAC finds the corresponding device index (ifindex) for a
// given MAC address. This is useful for creating rules and routes in order to
// specify the table. When the ifindex is found, the device is brought up and
// its MTU is set.
func retrieveIfIndexFromMAC(mac mac.MAC, mtu int) (index int, err error) {
var links []netlink.Link
// given MAC address, excluding Linux slave devices. This is useful for
// creating rules and routes in order to specify the table. When the ifindex is
// found, the device is brought up and its MTU is set.
func retrieveIfIndexFromMAC(mac mac.MAC, mtu int) (int, error) {
var link netlink.Link

links, err = netlink.LinkList()
links, err := netlink.LinkList()
if err != nil {
err = fmt.Errorf("unable to list interfaces: %s", err)
return
return -1, fmt.Errorf("unable to list interfaces: %w", err)
}

for _, link := range links {
if link.Attrs().HardwareAddr.String() == mac.String() {
index = link.Attrs().Index

if err = netlink.LinkSetMTU(link, mtu); err != nil {
err = fmt.Errorf("unable to change MTU of link %s to %d: %s", link.Attrs().Name, mtu, err)
return
for _, l := range links {
// Linux slave devices have the same MAC address as their master
// device, but we want the master device.
if l.Attrs().Slave != nil {
continue
}
if l.Attrs().HardwareAddr.String() == mac.String() {
if link != nil {
return -1, fmt.Errorf("several interfaces found with MAC %s: %s and %s", mac, link.Attrs().Name, l.Attrs().Name)
}
link = l
}
}

if err = netlink.LinkSetUp(link); err != nil {
err = fmt.Errorf("unable to up link %s: %s", link.Attrs().Name, err)
return
}
if link == nil {
return -1, fmt.Errorf("interface with MAC %s not found", mac)
}

return
}
if err = netlink.LinkSetMTU(link, mtu); err != nil {
return -1, fmt.Errorf("unable to change MTU of link %s to %d: %w", link.Attrs().Name, mtu, err)
}
if err = netlink.LinkSetUp(link); err != nil {
return -1, fmt.Errorf("unable to up link %s: %w", link.Attrs().Name, err)
}

err = fmt.Errorf("interface with MAC %s not found", mac)
return
return link.Attrs().Index, nil
}

// computeTableIDFromIfaceNumber returns a computed per-ENI route table ID for the given
Expand Down