mirror of https://github.com/vishvananda/netlink
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:
parent
8eb6423887
commit
1fd3169564
6
link.go
6
link.go
|
@ -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
|
||||
|
|
|
@ -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()))
|
||||
|
||||
|
|
Loading…
Reference in New Issue