return err
}
-type LinkAttrs struct {
- Name string
- HardwareAddr net.HardwareAddr
- Index int
-}
-
-type link struct {
- netlink.Link
-}
-
-func (l *link) Attrs() LinkAttrs {
- a := l.Link.Attrs()
- return LinkAttrs{
+func ifaceFromNetlinkLink(l netlink.Link) net.Interface {
+ a := l.Attrs()
+ return net.Interface{
+ Index: a.Index,
+ MTU: a.MTU,
Name: a.Name,
HardwareAddr: a.HardwareAddr,
- Index: a.Index,
+ Flags: a.Flags,
}
}
-type Link interface {
- Attrs() LinkAttrs
-}
-
// SetupVeth sets up a virtual ethernet link.
// Should be in container netns, and will switch back to hostNS to set the host
// 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)
if err != nil {
- return nil, nil, err
+ return net.Interface{}, net.Interface{}, err
}
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)
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 {
- 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 {
return 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.
hostNetNS ns.NetNS
containerNetNS ns.NetNS
ifaceCounter int = 0
- hostVeth ip.Link
- containerVeth ip.Link
+ hostVeth net.Interface
+ containerVeth net.Interface
hostVethName string
containerVethName string
}
Expect(err).NotTo(HaveOccurred())
- hostVethName = hostVeth.Attrs().Name
- containerVethName = containerVeth.Attrs().Name
+ hostVethName = hostVeth.Name
+ containerVethName = containerVeth.Name
return nil
})
containerVethFromName, err := netlink.LinkByName(containerVethName)
Expect(err).NotTo(HaveOccurred())
- Expect(containerVethFromName.Attrs().Index).To(Equal(containerVeth.Attrs().Index))
+ Expect(containerVethFromName.Attrs().Index).To(Equal(containerVeth.Index))
return nil
})
hostVethFromName, err := netlink.LinkByName(hostVethName)
Expect(err).NotTo(HaveOccurred())
- Expect(hostVethFromName.Attrs().Index).To(Equal(hostVeth.Attrs().Index))
+ Expect(hostVethFromName.Attrs().Index).To(Equal(hostVeth.Index))
return nil
})
hostVeth, _, err := ip.SetupVeth(containerVethName, mtu, hostNetNS)
Expect(err).NotTo(HaveOccurred())
- hostVethName = hostVeth.Attrs().Name
+ hostVethName = hostVeth.Name
return nil
})
if err != nil {
return err
}
- contIface.Name = containerVeth.Attrs().Name
- contIface.Mac = containerVeth.Attrs().HardwareAddr.String()
+ contIface.Name = containerVeth.Name
+ contIface.Mac = containerVeth.HardwareAddr.String()
contIface.Sandbox = netns.Path()
- hostIface.Name = hostVeth.Attrs().Name
+ hostIface.Name = hostVeth.Name
return nil
})
if err != nil {
if err != nil {
return err
}
- hostInterface.Name = hostVeth.Attrs().Name
- hostInterface.Mac = hostVeth.Attrs().HardwareAddr.String()
- containerInterface.Name = contVeth0.Attrs().Name
- containerInterface.Mac = contVeth0.Attrs().HardwareAddr.String()
+ hostInterface.Name = hostVeth.Name
+ hostInterface.Mac = hostVeth.HardwareAddr.String()
+ containerInterface.Name = contVeth0.Name
+ containerInterface.Mac = contVeth0.HardwareAddr.String()
containerInterface.Sandbox = netns.Path()
var firstV4Addr net.IP
if firstV4Addr != nil {
err = hostNS.Do(func(_ ns.NetNS) error {
- hostVethName := hostVeth.Attrs().Name
+ hostVethName := hostVeth.Name
if err := ip.SetHWAddrByIP(hostVethName, firstV4Addr, nil /* TODO IPv6 */); err != nil {
return fmt.Errorf("failed to set hardware addr by IP: %v", err)
}
return err
}
- if err := ip.SetHWAddrByIP(contVeth0.Attrs().Name, firstV4Addr, nil /* TODO IPv6 */); err != nil {
+ if err := ip.SetHWAddrByIP(contVeth0.Name, firstV4Addr, nil /* TODO IPv6 */); err != nil {
return fmt.Errorf("failed to set hardware addr by IP: %v", err)
}