-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevmon.go
173 lines (153 loc) · 3.88 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Package devmon contains the device monitor.
package devmon
import (
"fmt"
"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 {
events chan netlink.LinkUpdate
updates chan *Update
upsDone chan struct{}
done chan struct{}
closed 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.
// this check only works when device was added. when device was
// removed, the symlink does not exist anymore. thus, we cannot be sure
// device is really a "device" when it was removed.
if add && 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, error) {
// 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 {
return nil, fmt.Errorf("could not subscribe to link updates: %w", err)
}
return events, nil
}
// start starts the device monitor.
func (d *DevMon) start() {
defer close(d.closed)
defer close(d.updates)
defer close(d.upsDone)
// handle link update events
for {
select {
case e, ok := <-d.events:
if !ok {
// unexpected close of events, try to re-open
log.Error("DevMon got unexpected close of link events")
events, err := RegisterLinkUpdates(d)
if err != nil {
log.WithError(err).Error("DevMon register link update error")
}
d.events = events
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 d.events {
// wait for channel shutdown
log.Debug("DevMon dropping event after stop")
}
}()
return
}
}
}
// Start starts the device monitor.
func (d *DevMon) Start() error {
// register for link update events
events, err := RegisterLinkUpdates(d)
if err != nil {
return err
}
d.events = events
go d.start()
return nil
}
// Stop stops the device monitor.
func (d *DevMon) Stop() {
close(d.done)
<-d.closed
}
// 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{}),
closed: make(chan struct{}),
}
}