ioctl_linux.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package netlink
  2. import (
  3. "syscall"
  4. "unsafe"
  5. "golang.org/x/sys/unix"
  6. )
  7. // ioctl for statistics.
  8. const (
  9. // ETHTOOL_GSSET_INFO gets string set info
  10. ETHTOOL_GSSET_INFO = 0x00000037
  11. // SIOCETHTOOL is Ethtool interface
  12. SIOCETHTOOL = 0x8946
  13. // ETHTOOL_GSTRINGS gets specified string set
  14. ETHTOOL_GSTRINGS = 0x0000001b
  15. // ETHTOOL_GSTATS gets NIC-specific statistics
  16. ETHTOOL_GSTATS = 0x0000001d
  17. )
  18. // string set id.
  19. const (
  20. // ETH_SS_TEST is self-test result names, for use with %ETHTOOL_TEST
  21. ETH_SS_TEST = iota
  22. // ETH_SS_STATS statistic names, for use with %ETHTOOL_GSTATS
  23. ETH_SS_STATS
  24. // ETH_SS_PRIV_FLAGS are driver private flag names
  25. ETH_SS_PRIV_FLAGS
  26. // _ETH_SS_NTUPLE_FILTERS is deprecated
  27. _ETH_SS_NTUPLE_FILTERS
  28. // ETH_SS_FEATURES are device feature names
  29. ETH_SS_FEATURES
  30. // ETH_SS_RSS_HASH_FUNCS is RSS hush function names
  31. ETH_SS_RSS_HASH_FUNCS
  32. )
  33. // IfreqSlave is a struct for ioctl bond manipulation syscalls.
  34. // It is used to assign slave to bond interface with Name.
  35. type IfreqSlave struct {
  36. Name [unix.IFNAMSIZ]byte
  37. Slave [unix.IFNAMSIZ]byte
  38. }
  39. // Ifreq is a struct for ioctl ethernet manipulation syscalls.
  40. type Ifreq struct {
  41. Name [unix.IFNAMSIZ]byte
  42. Data uintptr
  43. }
  44. // ethtoolSset is a string set information
  45. type ethtoolSset struct {
  46. cmd uint32
  47. reserved uint32
  48. mask uint64
  49. data [1]uint32
  50. }
  51. type ethtoolStats struct {
  52. cmd uint32
  53. nStats uint32
  54. // Followed by nStats * []uint64.
  55. }
  56. // newIocltSlaveReq returns filled IfreqSlave with proper interface names
  57. // It is used by ioctl to assign slave to bond master
  58. func newIocltSlaveReq(slave, master string) *IfreqSlave {
  59. ifreq := &IfreqSlave{}
  60. copy(ifreq.Name[:unix.IFNAMSIZ-1], master)
  61. copy(ifreq.Slave[:unix.IFNAMSIZ-1], slave)
  62. return ifreq
  63. }
  64. // newIocltStringSetReq creates request to get interface string set
  65. func newIocltStringSetReq(linkName string) (*Ifreq, *ethtoolSset) {
  66. e := &ethtoolSset{
  67. cmd: ETHTOOL_GSSET_INFO,
  68. mask: 1 << ETH_SS_STATS,
  69. }
  70. ifreq := &Ifreq{Data: uintptr(unsafe.Pointer(e))}
  71. copy(ifreq.Name[:unix.IFNAMSIZ-1], linkName)
  72. return ifreq, e
  73. }
  74. // getSocketUDP returns file descriptor to new UDP socket
  75. // It is used for communication with ioctl interface.
  76. func getSocketUDP() (int, error) {
  77. return syscall.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0)
  78. }