handle_linux.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // NewHandleAt returns a netlink handle on the network namespace
  98. // specified by ns. If ns=netns.None(), current network namespace
  99. // will be assumed
  100. func NewHandleAt(ns netns.NsHandle, nlFamilies ...int) (*Handle, error) {
  101. return newHandle(ns, netns.None(), nlFamilies...)
  102. }
  103. // NewHandleAtFrom works as NewHandle but allows client to specify the
  104. // new and the origin netns Handle.
  105. func NewHandleAtFrom(newNs, curNs netns.NsHandle) (*Handle, error) {
  106. return newHandle(newNs, curNs)
  107. }
  108. func newHandle(newNs, curNs netns.NsHandle, nlFamilies ...int) (*Handle, error) {
  109. h := &Handle{sockets: map[int]*nl.SocketHandle{}}
  110. fams := nl.SupportedNlFamilies
  111. if len(nlFamilies) != 0 {
  112. fams = nlFamilies
  113. }
  114. for _, f := range fams {
  115. s, err := nl.GetNetlinkSocketAt(newNs, curNs, f)
  116. if err != nil {
  117. return nil, err
  118. }
  119. h.sockets[f] = &nl.SocketHandle{Socket: s}
  120. }
  121. return h, nil
  122. }
  123. // Close releases the resources allocated to this handle
  124. func (h *Handle) Close() {
  125. for _, sh := range h.sockets {
  126. sh.Close()
  127. }
  128. h.sockets = nil
  129. }
  130. // Delete releases the resources allocated to this handle
  131. //
  132. // Deprecated: use Close instead which is in line with typical resource release
  133. // patterns for files and other resources.
  134. func (h *Handle) Delete() {
  135. h.Close()
  136. }
  137. func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
  138. // Do this so that package API still use nl package variable nextSeqNr
  139. if h.sockets == nil {
  140. return nl.NewNetlinkRequest(proto, flags)
  141. }
  142. return &nl.NetlinkRequest{
  143. NlMsghdr: unix.NlMsghdr{
  144. Len: uint32(unix.SizeofNlMsghdr),
  145. Type: uint16(proto),
  146. Flags: unix.NLM_F_REQUEST | uint16(flags),
  147. },
  148. Sockets: h.sockets,
  149. }
  150. }