-
Notifications
You must be signed in to change notification settings - Fork 69
/
ethtool_cmd.go
208 lines (177 loc) · 5.12 KB
/
ethtool_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
// Package ethtool aims to provide a library giving a simple access to the
// Linux SIOCETHTOOL ioctl operations. It can be used to retrieve informations
// from a network device like statistics, driver related informations or
// even the peer of a VETH interface.
package ethtool
import (
"math"
"reflect"
"unsafe"
"golang.org/x/sys/unix"
)
type EthtoolCmd struct { /* ethtool.c: struct ethtool_cmd */
Cmd uint32
Supported uint32
Advertising uint32
Speed uint16
Duplex uint8
Port uint8
Phy_address uint8
Transceiver uint8
Autoneg uint8
Mdio_support uint8
Maxtxpkt uint32
Maxrxpkt uint32
Speed_hi uint16
Eth_tp_mdix uint8
Reserved2 uint8
Lp_advertising uint32
Reserved [2]uint32
}
// CmdGet returns the interface settings in the receiver struct
// and returns speed
func (ecmd *EthtoolCmd) CmdGet(intf string) (uint32, error) {
e, err := NewEthtool()
if err != nil {
return 0, err
}
defer e.Close()
return e.CmdGet(ecmd, intf)
}
// CmdSet sets and returns the settings in the receiver struct
// and returns speed
func (ecmd *EthtoolCmd) CmdSet(intf string) (uint32, error) {
e, err := NewEthtool()
if err != nil {
return 0, err
}
defer e.Close()
return e.CmdSet(ecmd, intf)
}
func (f *EthtoolCmd) reflect(retv *map[string]uint64) {
val := reflect.ValueOf(f).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
t := valueField.Interface()
// tt := reflect.TypeOf(t)
// fmt.Printf(" t %T %v tt %T %v\n", t, t, tt, tt)
switch tt := t.(type) {
case uint32:
// fmt.Printf(" t is uint32\n")
(*retv)[typeField.Name] = uint64(tt)
case uint16:
(*retv)[typeField.Name] = uint64(tt)
case uint8:
(*retv)[typeField.Name] = uint64(tt)
case int32:
(*retv)[typeField.Name] = uint64(tt)
case int16:
(*retv)[typeField.Name] = uint64(tt)
case int8:
(*retv)[typeField.Name] = uint64(tt)
default:
(*retv)[typeField.Name+"_unknown_type"] = 0
}
// tag := typeField.Tag
// fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n",
// typeField.Name, valueField.Interface(), tag.Get("tag_name"))
}
}
// CmdGet returns the interface settings in the receiver struct
// and returns speed
func (e *Ethtool) CmdGet(ecmd *EthtoolCmd, intf string) (uint32, error) {
ecmd.Cmd = ETHTOOL_GSET
var name [IFNAMSIZ]byte
copy(name[:], []byte(intf))
ifr := ifreq{
ifr_name: name,
ifr_data: uintptr(unsafe.Pointer(ecmd)),
}
_, _, ep := unix.Syscall(unix.SYS_IOCTL, uintptr(e.fd),
SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
if ep != 0 {
return 0, ep
}
var speedval uint32 = (uint32(ecmd.Speed_hi) << 16) |
(uint32(ecmd.Speed) & 0xffff)
if speedval == math.MaxUint16 {
speedval = math.MaxUint32
}
return speedval, nil
}
// CmdSet sets and returns the settings in the receiver struct
// and returns speed
func (e *Ethtool) CmdSet(ecmd *EthtoolCmd, intf string) (uint32, error) {
ecmd.Cmd = ETHTOOL_SSET
var name [IFNAMSIZ]byte
copy(name[:], []byte(intf))
ifr := ifreq{
ifr_name: name,
ifr_data: uintptr(unsafe.Pointer(ecmd)),
}
_, _, ep := unix.Syscall(unix.SYS_IOCTL, uintptr(e.fd),
SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
if ep != 0 {
return 0, unix.Errno(ep)
}
var speedval uint32 = (uint32(ecmd.Speed_hi) << 16) |
(uint32(ecmd.Speed) & 0xffff)
if speedval == math.MaxUint16 {
speedval = math.MaxUint32
}
return speedval, nil
}
// CmdGetMapped returns the interface settings in a map
func (e *Ethtool) CmdGetMapped(intf string) (map[string]uint64, error) {
ecmd := EthtoolCmd{
Cmd: ETHTOOL_GSET,
}
var name [IFNAMSIZ]byte
copy(name[:], []byte(intf))
ifr := ifreq{
ifr_name: name,
ifr_data: uintptr(unsafe.Pointer(&ecmd)),
}
_, _, ep := unix.Syscall(unix.SYS_IOCTL, uintptr(e.fd),
SIOCETHTOOL, uintptr(unsafe.Pointer(&ifr)))
if ep != 0 {
return nil, ep
}
result := make(map[string]uint64)
// ref https://gist.github.com/drewolson/4771479
// Golang Reflection Example
ecmd.reflect(&result)
var speedval uint32 = (uint32(ecmd.Speed_hi) << 16) |
(uint32(ecmd.Speed) & 0xffff)
result["speed"] = uint64(speedval)
return result, nil
}
func CmdGetMapped(intf string) (map[string]uint64, error) {
e, err := NewEthtool()
if err != nil {
return nil, err
}
defer e.Close()
return e.CmdGetMapped(intf)
}