-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaddrmon.go
113 lines (96 loc) · 2.21 KB
/
addrmon.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
package addrmon
import (
"net"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
// Update is an address update
type Update struct {
Add bool
Address net.IPNet
Index int
}
// AddrMon is an address monitor
type AddrMon struct {
updates chan *Update
upsDone chan struct{}
done chan struct{}
}
// sendUpdate sends an address update
func (a *AddrMon) sendUpdate(update *Update) {
select {
case a.updates <- update:
case <-a.done:
}
}
// RegisterAddrUpdates registers for addr update events
var RegisterAddrUpdates = func(a *AddrMon) chan netlink.AddrUpdate {
// register for addr update events
events := make(chan netlink.AddrUpdate)
options := netlink.AddrSubscribeOptions{
ListExisting: true,
}
if err := netlink.AddrSubscribeWithOptions(events, a.upsDone, options); err != nil {
log.WithError(err).Fatal("AddrMon address subscribe error")
}
return events
}
// start starts the address monitor
func (a *AddrMon) start() {
defer close(a.updates)
defer close(a.upsDone)
// register for addr update events
events := RegisterAddrUpdates(a)
// handle events
for {
select {
case e, ok := <-events:
if !ok {
// unexpected close of events, try to re-open
log.Error("AddrMon got unexpected close of addr events")
events = RegisterAddrUpdates(a)
break
}
// forward event as address update
u := &Update{
Address: e.LinkAddress,
Index: e.LinkIndex,
Add: e.NewAddr,
}
a.sendUpdate(u)
case <-a.done:
// drain events and wait for channel shutdown; this
// could take until the next addr update
go func() {
for range events {
// wait for channel shutdown
}
}()
// stop address monitor
return
}
}
}
// Start starts the address monitor
func (a *AddrMon) Start() {
go a.start()
}
// Stop stops the address monitor
func (a *AddrMon) Stop() {
close(a.done)
for range a.updates {
// wait for channel close
}
}
// Updates returns the address updates channel
func (a *AddrMon) Updates() chan *Update {
return a.updates
}
// NewAddrMon returns a new address monitor
func NewAddrMon() *AddrMon {
return &AddrMon{
updates: make(chan *Update),
upsDone: make(chan struct{}),
done: make(chan struct{}),
}
}