From 96bd10f679f5823b44a3272d1243bf04c06f882c Mon Sep 17 00:00:00 2001 From: Koonwah Chen Date: Wed, 26 Jun 2019 10:23:20 -0700 Subject: [PATCH] Add pkg/ip/link_linux.go:SetupVethWithName to support the host-side veth name configuration. --- pkg/ip/link_linux.go | 16 ++++++++++++---- pkg/ip/link_linux_test.go | 8 ++++---- plugins/main/bridge/bridge.go | 2 +- plugins/main/ptp/ptp.go | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/ip/link_linux.go b/pkg/ip/link_linux.go index 6720c726..c0053cab 100644 --- a/pkg/ip/link_linux.go +++ b/pkg/ip/link_linux.go @@ -125,12 +125,12 @@ 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 +// SetupVethWithName sets up a pair of virtual ethernet devices. +// Call SetupVethWithName 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, hostVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) { +// On success, SetupVethWithName returns (hostVeth, containerVeth, nil) +func SetupVethWithName(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 @@ -166,6 +166,14 @@ func SetupVeth(contVethName, hostVethName string, mtu int, hostNS ns.NetNS) (net return ifaceFromNetlinkLink(hostVeth), ifaceFromNetlinkLink(contVeth), nil } +// 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. +// On success, SetupVeth returns (hostVeth, containerVeth, nil) +func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) { + return SetupVethWithName(contVethName, "", mtu, hostNS) +} + // DelLinkByName removes an interface link. func DelLinkByName(ifName string) error { iface, err := netlink.LinkByName(ifName) diff --git a/pkg/ip/link_linux_test.go b/pkg/ip/link_linux_test.go index e0dabf2c..58905455 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 73681aff..fb78a5f8 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 ff085fe7..a748d7f6 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 }