-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevmon.go
156 lines (136 loc) · 3.29 KB
/
devmon.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
package devmon
import (
"net"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)
// Update is a device update
type Update struct {
Add bool
Device string
Type string
Index int
}
// DevMon is a device monitor
type DevMon struct {
updates chan *Update
upsDone chan struct{}
done chan struct{}
}
// sendUpdate sends update over the update channel
func (d *DevMon) sendUpdate(update *Update) {
// send update or abort if we are shutting down
select {
case d.updates <- update:
case <-d.done:
}
}
// handleLink handles a link update
func (d *DevMon) handleLink(add bool, link netlink.Link) {
log.WithFields(log.Fields{
"add": add,
"link": link,
}).Debug("DevMon handling link update")
// get attributes and link type
attrs := link.Attrs()
typ := link.Type()
// use special type for loop back device
if attrs.Flags&net.FlagLoopback != 0 {
typ = "loopback"
}
// use special type for device that is actually virtual, e.g., vboxnet
if typ == "device" {
sysfs := filepath.Join("/sys/class/net", attrs.Name)
path, err := filepath.EvalSymlinks(sysfs)
if err != nil {
log.WithError(err).Error("DevMon could not eval device symlink")
} else {
if path == filepath.Join("/sys/devices/virtual/net",
attrs.Name) {
// set device type to virtual
typ = "virtual"
}
}
}
// report device update
update := &Update{
Add: add,
Device: attrs.Name,
Type: typ,
Index: attrs.Index,
}
d.sendUpdate(update)
}
// RegisterLinkUpdates registers for link update events
var RegisterLinkUpdates = func(d *DevMon) chan netlink.LinkUpdate {
// register for link update events
events := make(chan netlink.LinkUpdate)
options := netlink.LinkSubscribeOptions{
ListExisting: true,
}
if err := netlink.LinkSubscribeWithOptions(events, d.upsDone, options); err != nil {
log.WithError(err).Fatal("DevMon link update subscribe error")
}
return events
}
// start starts the device monitor
func (d *DevMon) start() {
defer close(d.updates)
defer close(d.upsDone)
// register for link update events
events := RegisterLinkUpdates(d)
// handle link update events
for {
select {
case e, ok := <-events:
if !ok {
// unexpected close of events, try to re-open
log.Error("DevMon got unexpected close of link events")
events = RegisterLinkUpdates(d)
break
}
switch e.Header.Type {
case unix.RTM_NEWLINK:
d.handleLink(true, e)
case unix.RTM_DELLINK:
d.handleLink(false, e)
default:
log.WithField("event", e).Error("DevMon got unknown link event")
}
case <-d.done:
// drain events and wait for channel shutdown; this
// could take until the next link update
go func() {
for range events {
// wait for channel shutdown
}
}()
return
}
}
}
// Start starts the device monitor
func (d *DevMon) Start() {
go d.start()
}
// Stop stops the device monitor
func (d *DevMon) Stop() {
close(d.done)
for range d.updates {
// wait for channel shutdown
}
}
// Updates returns the Update channel for device updates
func (d *DevMon) Updates() chan *Update {
return d.updates
}
// NewDevMon returns a new device monitor
func NewDevMon() *DevMon {
return &DevMon{
updates: make(chan *Update),
upsDone: make(chan struct{}),
done: make(chan struct{}),
}
}