-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterface.go
72 lines (59 loc) · 1.41 KB
/
interface.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
package ethtool
import (
"fmt"
"github.com/pkg/errors"
"github.com/wobcom/go-ethtool/eeprom"
)
const (
// IFNAMSIZ is the maximum length of an interface name
IFNAMSIZ = 16
)
// Interface representation of a network interface
type Interface struct {
Name string
nameBytes [IFNAMSIZ]byte
Eeprom eeprom.EEPROM
DriverInfo *DriverInfo
ethtool *Ethtool
}
// NewInterface retrieves information about a network interface and returns an Interface instance
func (e *Ethtool) NewInterface(ifname string, ignoreEepromReadErrors bool) (*Interface, error) {
if len([]byte(ifname)) >= IFNAMSIZ {
return nil, errors.New(fmt.Sprintf("Interface name must not be longer than %d characters.", IFNAMSIZ))
}
iface := &Interface{
ethtool: e,
Name: ifname,
}
driverInfo, err := iface.getDriverInfo()
if err != nil {
return iface, errors.Wrapf(err, "Could not retrieve driver info")
}
iface.DriverInfo = driverInfo
eeprom, err := iface.getEEPROM()
if err != nil {
if ignoreEepromReadErrors {
return iface, nil
}
return iface, err
}
iface.Eeprom = eeprom
return iface, nil
}
type ifreq struct {
ifrName [IFNAMSIZ]byte
ifrData uintptr
}
func (i *Interface) performIoctl(data uintptr) error {
var name [IFNAMSIZ]byte
copy(name[:], []byte(i.Name))
ifr := ifreq{
ifrName: name,
ifrData: data,
}
return i.ethtool.PerformIoctl(&ifr)
}
type ethtoolArbitraryCommand struct {
cmd uint32
value uint32
}