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