handle_linux.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package netlink
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/vishvananda/netlink/nl"
  6. "github.com/vishvananda/netns"
  7. "golang.org/x/sys/unix"
  8. )
  9. // Empty handle used by the netlink package methods
  10. var pkgHandle = &Handle{}
  11. // Handle is an handle for the netlink requests on a
  12. // specific network namespace. All the requests on the
  13. // same netlink family share the same netlink socket,
  14. // which gets released when the handle is Close'd.
  15. type Handle struct {
  16. sockets map[int]*nl.SocketHandle
  17. lookupByDump bool
  18. }
  19. // SetSocketTimeout configures timeout for default netlink sockets
  20. func SetSocketTimeout(to time.Duration) error {
  21. if to < time.Microsecond {
  22. return fmt.Errorf("invalid timeout, minimul value is %s", time.Microsecond)
  23. }
  24. nl.SocketTimeoutTv = unix.NsecToTimeval(to.Nanoseconds())
  25. return nil
  26. }
  27. // GetSocketTimeout returns the timeout value used by default netlink sockets
  28. func GetSocketTimeout() time.Duration {
  29. nsec := unix.TimevalToNsec(nl.SocketTimeoutTv)
  30. return time.Duration(nsec) * time.Nanosecond
  31. }
  32. // SupportsNetlinkFamily reports whether the passed netlink family is supported by this Handle
  33. func (h *Handle) SupportsNetlinkFamily(nlFamily int) bool {
  34. _, ok := h.sockets[nlFamily]
  35. return ok
  36. }
  37. // NewHandle returns a netlink handle on the current network namespace.
  38. // Caller may specify the netlink families the handle should support.
  39. // If no families are specified, all the families the netlink package
  40. // supports will be automatically added.
  41. func NewHandle(nlFamilies ...int) (*Handle, error) {
  42. return newHandle(netns.None(), netns.None(), nlFamilies...)
  43. }
  44. // SetSocketTimeout sets the send and receive timeout for each socket in the
  45. // netlink handle. Although the socket timeout has granularity of one
  46. // microsecond, the effective granularity is floored by the kernel timer tick,
  47. // which default value is four milliseconds.
  48. func (h *Handle) SetSocketTimeout(to time.Duration) error {
  49. if to < time.Microsecond {
  50. return fmt.Errorf("invalid timeout, minimul value is %s", time.Microsecond)
  51. }
  52. tv := unix.NsecToTimeval(to.Nanoseconds())
  53. for _, sh := range h.sockets {
  54. if err := sh.Socket.SetSendTimeout(&tv); err != nil {
  55. return err
  56. }
  57. if err := sh.Socket.SetReceiveTimeout(&tv); err != nil {
  58. return err
  59. }
  60. }
  61. return nil
  62. }
  63. // SetSocketReceiveBufferSize sets the receive buffer size for each
  64. // socket in the netlink handle. The maximum value is capped by
  65. // /proc/sys/net/core/rmem_max.
  66. func (h *Handle) SetSocketReceiveBufferSize(size int, force bool) error {
  67. opt := unix.SO_RCVBUF
  68. if force {
  69. opt = unix.SO_RCVBUFFORCE
  70. }
  71. for _, sh := range h.sockets {
  72. fd := sh.Socket.GetFd()
  73. err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, opt, size)
  74. if err != nil {
  75. return err
  76. }
  77. }
  78. return nil
  79. }
  80. // GetSocketReceiveBufferSize gets the receiver buffer size for each
  81. // socket in the netlink handle. The retrieved value should be the
  82. // double to the one set for SetSocketReceiveBufferSize.
  83. func (h *Handle) GetSocketReceiveBufferSize() ([]int, error) {
  84. results := make([]int, len(h.sockets))
  85. i := 0
  86. for _, sh := range h.sockets {
  87. fd := sh.Socket.GetFd()
  88. size, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_RCVBUF)
  89. if err != nil {
  90. return nil, err
  91. }
  92. results[i] = size
  93. i++
  94. }
  95. return results, nil
  96. }
  97. // SetStrictCheck sets the strict check socket option for each socket in the netlink handle. Returns early if any set operation fails
  98. func (h *Handle) SetStrictCheck(state bool) error {
  99. for _, sh := range h.sockets {
  100. var stateInt int = 0
  101. if state {
  102. stateInt = 1
  103. }
  104. err := unix.SetsockoptInt(sh.Socket.GetFd(), unix.SOL_NETLINK, unix.NETLINK_GET_STRICT_CHK, stateInt)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. }
  111. // NewHandleAt returns a netlink handle on the network namespace
  112. // specified by ns. If ns=netns.None(), current network namespace
  113. // will be assumed
  114. func NewHandleAt(ns netns.NsHandle, nlFamilies ...int) (*Handle, error) {
  115. return newHandle(ns, netns.None(), nlFamilies...)
  116. }
  117. // NewHandleAtFrom works as NewHandle but allows client to specify the
  118. // new and the origin netns Handle.
  119. func NewHandleAtFrom(newNs, curNs netns.NsHandle) (*Handle, error) {
  120. return newHandle(newNs, curNs)
  121. }
  122. func newHandle(newNs, curNs netns.NsHandle, nlFamilies ...int) (*Handle, error) {
  123. h := &Handle{sockets: map[int]*nl.SocketHandle{}}
  124. fams := nl.SupportedNlFamilies
  125. if len(nlFamilies) != 0 {
  126. fams = nlFamilies
  127. }
  128. for _, f := range fams {
  129. s, err := nl.GetNetlinkSocketAt(newNs, curNs, f)
  130. if err != nil {
  131. return nil, err
  132. }
  133. h.sockets[f] = &nl.SocketHandle{Socket: s}
  134. }
  135. return h, nil
  136. }
  137. // Close releases the resources allocated to this handle
  138. func (h *Handle) Close() {
  139. for _, sh := range h.sockets {
  140. sh.Close()
  141. }
  142. h.sockets = nil
  143. }
  144. // Delete releases the resources allocated to this handle
  145. //
  146. // Deprecated: use Close instead which is in line with typical resource release
  147. // patterns for files and other resources.
  148. func (h *Handle) Delete() {
  149. h.Close()
  150. }
  151. func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
  152. // Do this so that package API still use nl package variable nextSeqNr
  153. if h.sockets == nil {
  154. return nl.NewNetlinkRequest(proto, flags)
  155. }
  156. return &nl.NetlinkRequest{
  157. NlMsghdr: unix.NlMsghdr{
  158. Len: uint32(unix.SizeofNlMsghdr),
  159. Type: uint16(proto),
  160. Flags: unix.NLM_F_REQUEST | uint16(flags),
  161. },
  162. Sockets: h.sockets,
  163. }
  164. }