diff --git a/pkg/ip/link_linux.go b/pkg/ip/link_linux.go index 909afd04..873204dd 100644 --- a/pkg/ip/link_linux.go +++ b/pkg/ip/link_linux.go @@ -60,11 +60,15 @@ func peerExists(name string) bool { return true } -func makeVeth(name string, mtu int) (peerName string, veth netlink.Link, err error) { +func makeVeth(name, vethPeerName string, mtu int) (peerName string, veth netlink.Link, err error) { for i := 0; i < 10; i++ { - peerName, err = RandomVethName() - if err != nil { - return + if vethPeerName != "" { + peerName = vethPeerName + } else { + peerName, err = RandomVethName() + if err != nil { + return + } } veth, err = makeVethPair(name, peerName, mtu) @@ -124,9 +128,10 @@ func ifaceFromNetlinkLink(l netlink.Link) net.Interface { // SetupVeth sets up a pair of virtual ethernet devices. // Call SetupVeth from inside the container netns. It will create both veth // devices and move the host-side veth into the provided hostNS namespace. +// hostVethName: If hostVethName is not specified, the host-side veth name will use a random string. // On success, SetupVeth returns (hostVeth, containerVeth, nil) -func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) { - hostVethName, contVeth, err := makeVeth(contVethName, mtu) +func SetupVeth(contVethName, hostVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) { + hostVethName, contVeth, err := makeVeth(contVethName, hostVethName, mtu) if err != nil { return net.Interface{}, net.Interface{}, err } diff --git a/pkg/ip/link_linux_test.go b/pkg/ip/link_linux_test.go index 58905455..e0dabf2c 100644 --- a/pkg/ip/link_linux_test.go +++ b/pkg/ip/link_linux_test.go @@ -72,7 +72,7 @@ var _ = Describe("Link", func() { _ = containerNetNS.Do(func(ns.NetNS) error { defer GinkgoRecover() - hostVeth, containerVeth, err = ip.SetupVeth(fmt.Sprintf(ifaceFormatString, ifaceCounter), mtu, hostNetNS) + hostVeth, containerVeth, err = ip.SetupVeth(fmt.Sprintf(ifaceFormatString, ifaceCounter), "", mtu, hostNetNS) if err != nil { return err } @@ -159,7 +159,7 @@ var _ = Describe("Link", func() { _ = containerNetNS.Do(func(ns.NetNS) error { defer GinkgoRecover() - _, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS) + _, _, err := ip.SetupVeth(containerVethName, "", mtu, hostNetNS) Expect(err.Error()).To(Equal(fmt.Sprintf("container veth name provided (%s) already exists", containerVethName))) return nil @@ -190,7 +190,7 @@ var _ = Describe("Link", func() { _ = containerNetNS.Do(func(ns.NetNS) error { defer GinkgoRecover() - _, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS) + _, _, err := ip.SetupVeth(containerVethName, "", mtu, hostNetNS) Expect(err.Error()).To(Equal("failed to move veth to host netns: file exists")) return nil @@ -208,7 +208,7 @@ var _ = Describe("Link", func() { _ = containerNetNS.Do(func(ns.NetNS) error { defer GinkgoRecover() - hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS) + hostVeth, _, err := ip.SetupVeth(containerVethName, "", mtu, hostNetNS) Expect(err).NotTo(HaveOccurred()) hostVethName = hostVeth.Name return nil diff --git a/plugins/main/bridge/bridge.go b/plugins/main/bridge/bridge.go index fb78a5f8..73681aff 100644 --- a/plugins/main/bridge/bridge.go +++ b/plugins/main/bridge/bridge.go @@ -283,7 +283,7 @@ func setupVeth(netns ns.NetNS, br *netlink.Bridge, ifName string, mtu int, hairp err := netns.Do(func(hostNS ns.NetNS) error { // create the veth pair in the container and move host end into host netns - hostVeth, containerVeth, err := ip.SetupVeth(ifName, mtu, hostNS) + hostVeth, containerVeth, err := ip.SetupVeth(ifName, "", mtu, hostNS) if err != nil { return err } diff --git a/plugins/main/ptp/ptp.go b/plugins/main/ptp/ptp.go index a748d7f6..ff085fe7 100644 --- a/plugins/main/ptp/ptp.go +++ b/plugins/main/ptp/ptp.go @@ -66,7 +66,7 @@ func setupContainerVeth(netns ns.NetNS, ifName string, mtu int, pr *current.Resu containerInterface := ¤t.Interface{} err := netns.Do(func(hostNS ns.NetNS) error { - hostVeth, contVeth0, err := ip.SetupVeth(ifName, mtu, hostNS) + hostVeth, contVeth0, err := ip.SetupVeth(ifName, "", mtu, hostNS) if err != nil { return err }