2016-05-08 18:35:49 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2016-11-10 22:01:03 +00:00
|
|
|
"fmt"
|
2016-05-08 18:35:49 +00:00
|
|
|
"syscall"
|
2016-11-10 22:01:03 +00:00
|
|
|
"time"
|
2016-05-08 18:35:49 +00:00
|
|
|
|
|
|
|
"github.com/vishvananda/netlink/nl"
|
|
|
|
"github.com/vishvananda/netns"
|
|
|
|
)
|
|
|
|
|
2016-05-09 23:55:00 +00:00
|
|
|
// Empty handle used by the netlink package methods
|
|
|
|
var pkgHandle = &Handle{}
|
|
|
|
|
2016-06-20 16:25:15 +00:00
|
|
|
// Handle is an handle for the netlink requests on a
|
|
|
|
// specific network namespace. All the requests on the
|
|
|
|
// same netlink family share the same netlink socket,
|
|
|
|
// which gets released when the handle is deleted.
|
2016-05-08 18:35:49 +00:00
|
|
|
type Handle struct {
|
2016-06-20 16:25:15 +00:00
|
|
|
sockets map[int]*nl.SocketHandle
|
2016-05-08 18:35:49 +00:00
|
|
|
lookupByDump bool
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:25:15 +00:00
|
|
|
// SupportsNetlinkFamily reports whether the passed netlink family is supported by this Handle
|
|
|
|
func (h *Handle) SupportsNetlinkFamily(nlFamily int) bool {
|
|
|
|
_, ok := h.sockets[nlFamily]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-05-08 18:35:49 +00:00
|
|
|
// NewHandle returns a netlink handle on the current network namespace.
|
2016-06-20 16:25:15 +00:00
|
|
|
// Caller may specify the netlink families the handle should support.
|
|
|
|
// If no families are specified, all the families the netlink package
|
|
|
|
// supports will be automatically added.
|
|
|
|
func NewHandle(nlFamilies ...int) (*Handle, error) {
|
|
|
|
return newHandle(netns.None(), netns.None(), nlFamilies...)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 22:01:03 +00:00
|
|
|
// SetSocketTimeout sets the send and receive timeout for each socket in the
|
|
|
|
// netlink handle. Although the socket timeout has granularity of one
|
|
|
|
// microsecond, the effective granularity is floored by the kernel timer tick,
|
|
|
|
// which default value is four milliseconds.
|
|
|
|
func (h *Handle) SetSocketTimeout(to time.Duration) error {
|
|
|
|
if to < time.Microsecond {
|
|
|
|
return fmt.Errorf("invalid timeout, minimul value is %s", time.Microsecond)
|
|
|
|
}
|
|
|
|
tv := syscall.NsecToTimeval(to.Nanoseconds())
|
|
|
|
for _, sh := range h.sockets {
|
|
|
|
fd := sh.Socket.GetFd()
|
|
|
|
err := syscall.SetsockoptTimeval(fd, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &tv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = syscall.SetsockoptTimeval(fd, syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, &tv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-08 18:35:49 +00:00
|
|
|
// NewHandle returns a netlink handle on the network namespace
|
|
|
|
// specified by ns. If ns=netns.None(), current network namespace
|
|
|
|
// will be assumed
|
2016-06-20 16:25:15 +00:00
|
|
|
func NewHandleAt(ns netns.NsHandle, nlFamilies ...int) (*Handle, error) {
|
|
|
|
return newHandle(ns, netns.None(), nlFamilies...)
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandleAtFrom works as NewHandle but allows client to specify the
|
|
|
|
// new and the origin netns Handle.
|
|
|
|
func NewHandleAtFrom(newNs, curNs netns.NsHandle) (*Handle, error) {
|
|
|
|
return newHandle(newNs, curNs)
|
|
|
|
}
|
|
|
|
|
2016-06-20 16:25:15 +00:00
|
|
|
func newHandle(newNs, curNs netns.NsHandle, nlFamilies ...int) (*Handle, error) {
|
|
|
|
h := &Handle{sockets: map[int]*nl.SocketHandle{}}
|
|
|
|
fams := nl.SupportedNlFamilies
|
|
|
|
if len(nlFamilies) != 0 {
|
|
|
|
fams = nlFamilies
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
2016-06-20 16:25:15 +00:00
|
|
|
for _, f := range fams {
|
|
|
|
s, err := nl.GetNetlinkSocketAt(newNs, curNs, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.sockets[f] = &nl.SocketHandle{Socket: s}
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
2016-06-20 16:25:15 +00:00
|
|
|
return h, nil
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete releases the resources allocated to this handle
|
|
|
|
func (h *Handle) Delete() {
|
2016-06-20 16:25:15 +00:00
|
|
|
for _, sh := range h.sockets {
|
|
|
|
sh.Close()
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
2016-06-20 16:25:15 +00:00
|
|
|
h.sockets = nil
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
|
|
|
|
// Do this so that package API still use nl package variable nextSeqNr
|
2016-06-20 16:25:15 +00:00
|
|
|
if h.sockets == nil {
|
2016-05-08 18:35:49 +00:00
|
|
|
return nl.NewNetlinkRequest(proto, flags)
|
|
|
|
}
|
|
|
|
return &nl.NetlinkRequest{
|
|
|
|
NlMsghdr: syscall.NlMsghdr{
|
|
|
|
Len: uint32(syscall.SizeofNlMsghdr),
|
|
|
|
Type: uint16(proto),
|
|
|
|
Flags: syscall.NLM_F_REQUEST | uint16(flags),
|
|
|
|
},
|
2016-06-20 16:25:15 +00:00
|
|
|
Sockets: h.sockets,
|
2016-05-08 18:35:49 +00:00
|
|
|
}
|
|
|
|
}
|