Skip to content

Commit

Permalink
Use random name to skip name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
jellonek committed Apr 17, 2018
1 parent 04889d7 commit 7180321
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/nettools/nettools.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"fmt"
"io/ioutil"
"log"
mrand "math/rand"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -974,6 +975,10 @@ func Teardown(csn *network.ContainerSideNetwork) error {
return nil
}

func randomIfaceName() string {
return strconv.Itoa(mrand.Int)
}

// ReconstructVFs iterates over stored PCI addresses, rebinding each
// corresponding interface to its host driver, changing its MAC address
// to the stored value and then moving it into the container namespace
Expand All @@ -999,14 +1004,19 @@ func ReconstructVFs(csn *network.ContainerSideNetwork, netns ns.NetNS) error {
if err := netlink.LinkSetHardwareAddr(link, iface.HardwareAddr); err != nil {
return fmt.Errorf("can't set hwaddr %q on device %q: %v", iface.HardwareAddr, devName, err)
}
tmpName := randomIfaceName()
if err := netlink.LinkSetName(link, tmpName); err != nil {
return fmt.Errorf("can't set random name %q on interface %q: %v", tmpName, iface.Name, err)
}
if link, err = netlink.LinByName(tmpName); err != nil {
return fmt.Errorf("can't reread link info: %v", err)
}
if err := netlink.LinkSetNsFd(link, int(netns.Fd())); err != nil {
return fmt.Errorf("can't move link %q to netns %q: %v", iface.Name, netns.Path(), err)
}
if err := netns.Do(func(ns.NetNS) error {
if link.Attrs().Name != iface.Name {
if err := netlink.LinkSetName(link, iface.Name); err != nil {
return fmt.Errorf("can't rename device %q to %q: %v", devName, iface.Name, err)
}
if err := netlink.LinkSetName(link, iface.Name); err != nil {
return fmt.Errorf("can't rename device %q to %q: %v", devName, iface.Name, err)
}
return nil
}); err != nil {
Expand Down

0 comments on commit 7180321

Please sign in to comment.