Added ability to specify net ns at link creation

Creating a link in one namespace and then moving can
be problematic since there could be a iface name conflict.
However, it is not always possible to switch into the target
namespace either -- e.g. creating a macvlan interface must
be done in the namespace of the parent interface.
This commit is contained in:
Eugene Yakubovich 2015-02-19 15:29:56 -08:00 committed by Vishvananda Ishaya
parent 8eb6423887
commit 1fd3169564
2 changed files with 22 additions and 2 deletions

View File

@ -10,6 +10,11 @@ type Link interface {
Type() string
}
type (
NsPid int
NsFd int
)
// LinkAttrs represents data shared by most link types
type LinkAttrs struct {
Index int
@ -20,6 +25,7 @@ type LinkAttrs struct {
Flags net.Flags
ParentIndex int // index of the parent link device
MasterIndex int // must be the index of a bridge
Namespace interface{} // nil | NsPid | NsFd
}
// Device links cannot be created via netlink. These links

View File

@ -312,6 +312,20 @@ func LinkAdd(link Link) error {
req.AddData(mtu)
}
if base.Namespace != nil {
var attr *nl.RtAttr
switch base.Namespace.(type) {
case NsPid:
val := nl.Uint32Attr(uint32(base.Namespace.(NsPid)))
attr = nl.NewRtAttr(syscall.IFLA_NET_NS_PID, val)
case NsFd:
val := nl.Uint32Attr(uint32(base.Namespace.(NsFd)))
attr = nl.NewRtAttr(nl.IFLA_NET_NS_FD, val)
}
req.AddData(attr)
}
linkInfo := nl.NewRtAttr(syscall.IFLA_LINKINFO, nil)
nl.NewRtAttrChild(linkInfo, nl.IFLA_INFO_KIND, nl.NonZeroTerminated(link.Type()))