pkg/ip: SetupVeth returns net.Interface
This commit is contained in:
38
ip/link.go
38
ip/link.go
@ -98,49 +98,37 @@ func RenameLink(curName, newName string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type LinkAttrs struct {
|
func ifaceFromNetlinkLink(l netlink.Link) net.Interface {
|
||||||
Name string
|
a := l.Attrs()
|
||||||
HardwareAddr net.HardwareAddr
|
return net.Interface{
|
||||||
Index int
|
Index: a.Index,
|
||||||
}
|
MTU: a.MTU,
|
||||||
|
|
||||||
type link struct {
|
|
||||||
netlink.Link
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *link) Attrs() LinkAttrs {
|
|
||||||
a := l.Link.Attrs()
|
|
||||||
return LinkAttrs{
|
|
||||||
Name: a.Name,
|
Name: a.Name,
|
||||||
HardwareAddr: a.HardwareAddr,
|
HardwareAddr: a.HardwareAddr,
|
||||||
Index: a.Index,
|
Flags: a.Flags,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Link interface {
|
|
||||||
Attrs() LinkAttrs
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetupVeth sets up a virtual ethernet link.
|
// SetupVeth sets up a virtual ethernet link.
|
||||||
// Should be in container netns, and will switch back to hostNS to set the host
|
// Should be in container netns, and will switch back to hostNS to set the host
|
||||||
// veth end up.
|
// veth end up.
|
||||||
func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (Link, Link, error) {
|
func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (net.Interface, net.Interface, error) {
|
||||||
hostVethName, contVeth, err := makeVeth(contVethName, mtu)
|
hostVethName, contVeth, err := makeVeth(contVethName, mtu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return net.Interface{}, net.Interface{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = netlink.LinkSetUp(contVeth); err != nil {
|
if err = netlink.LinkSetUp(contVeth); err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to set %q up: %v", contVethName, err)
|
return net.Interface{}, net.Interface{}, fmt.Errorf("failed to set %q up: %v", contVethName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hostVeth, err := netlink.LinkByName(hostVethName)
|
hostVeth, err := netlink.LinkByName(hostVethName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to lookup %q: %v", hostVethName, err)
|
return net.Interface{}, net.Interface{}, fmt.Errorf("failed to lookup %q: %v", hostVethName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil {
|
if err = netlink.LinkSetNsFd(hostVeth, int(hostNS.Fd())); err != nil {
|
||||||
return nil, nil, fmt.Errorf("failed to move veth to host netns: %v", err)
|
return net.Interface{}, net.Interface{}, fmt.Errorf("failed to move veth to host netns: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = hostNS.Do(func(_ ns.NetNS) error {
|
err = hostNS.Do(func(_ ns.NetNS) error {
|
||||||
@ -155,9 +143,9 @@ func SetupVeth(contVethName string, mtu int, hostNS ns.NetNS) (Link, Link, error
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return net.Interface{}, net.Interface{}, err
|
||||||
}
|
}
|
||||||
return &link{hostVeth}, &link{contVeth}, nil
|
return ifaceFromNetlinkLink(hostVeth), ifaceFromNetlinkLink(contVeth), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelLinkByName removes an interface link.
|
// DelLinkByName removes an interface link.
|
||||||
|
@ -46,8 +46,8 @@ var _ = Describe("Link", func() {
|
|||||||
hostNetNS ns.NetNS
|
hostNetNS ns.NetNS
|
||||||
containerNetNS ns.NetNS
|
containerNetNS ns.NetNS
|
||||||
ifaceCounter int = 0
|
ifaceCounter int = 0
|
||||||
hostVeth ip.Link
|
hostVeth net.Interface
|
||||||
containerVeth ip.Link
|
containerVeth net.Interface
|
||||||
hostVethName string
|
hostVethName string
|
||||||
containerVethName string
|
containerVethName string
|
||||||
|
|
||||||
@ -78,8 +78,8 @@ var _ = Describe("Link", func() {
|
|||||||
}
|
}
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
hostVethName = hostVeth.Attrs().Name
|
hostVethName = hostVeth.Name
|
||||||
containerVethName = containerVeth.Attrs().Name
|
containerVethName = containerVeth.Name
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -98,7 +98,7 @@ var _ = Describe("Link", func() {
|
|||||||
|
|
||||||
containerVethFromName, err := netlink.LinkByName(containerVethName)
|
containerVethFromName, err := netlink.LinkByName(containerVethName)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(containerVethFromName.Attrs().Index).To(Equal(containerVeth.Attrs().Index))
|
Expect(containerVethFromName.Attrs().Index).To(Equal(containerVeth.Index))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -108,7 +108,7 @@ var _ = Describe("Link", func() {
|
|||||||
|
|
||||||
hostVethFromName, err := netlink.LinkByName(hostVethName)
|
hostVethFromName, err := netlink.LinkByName(hostVethName)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(hostVethFromName.Attrs().Index).To(Equal(hostVeth.Attrs().Index))
|
Expect(hostVethFromName.Attrs().Index).To(Equal(hostVeth.Index))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -156,7 +156,7 @@ var _ = Describe("Link", func() {
|
|||||||
|
|
||||||
hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS)
|
hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS)
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
hostVethName = hostVeth.Attrs().Name
|
hostVethName = hostVeth.Name
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user