-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfib.go
69 lines (65 loc) · 1.7 KB
/
fib.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
package main
import (
"bytes"
"time"
"github.com/j-keck/arping"
"github.com/prometheus/common/log"
"github.com/vishvananda/netlink"
)
// we need to keep the fib table up to date
// otherwise eBPF fib_lookup will fail and packets will not be forwarded
func updateFIB(cfg config, link netlink.Link) {
for {
select {
default:
neighList, err := netlink.NeighList(link.Attrs().Index, netlink.FAMILY_V4)
if err != nil {
log.Warnf("err fetching neighbors: %s", err)
}
for _, entry := range cfg {
for _, u := range entry.Upstream {
updateNeigh(u, link, neighList)
}
}
<-time.After(2 * time.Second)
}
}
}
// updateNeigh issues an arp request to find out the hw address of the destination ip
// the kernel does not touch the fib tables automatically, we have to tell him the new address
func updateNeigh(u Upstream, link netlink.Link, neighList []netlink.Neigh) {
log.Debugf("fetching upstream's hw address %s", u.IP())
hw, _, err := arping.Ping(u.IP())
if err != nil {
log.Warnf("error ping %s: %s", u.IP(), err)
return
}
log.Debugf("found hw addr: %s", hw)
for _, neigh := range neighList {
if neigh.IP.Equal(u.IP()) {
log.Debugf("found match: %v", neigh)
if bytes.Equal(neigh.HardwareAddr, hw) {
log.Debugf("hw addr is up to date")
return
}
neigh.HardwareAddr = hw
err = netlink.NeighSet(&neigh)
if err != nil {
log.Warnf("err: %s", err)
return
}
log.Debugf("updated hw: %v", neigh)
return
}
}
err = netlink.NeighAdd(&netlink.Neigh{
Family: netlink.FAMILY_V4,
HardwareAddr: hw,
IP: u.IP(),
LinkIndex: link.Attrs().Index,
})
if err != nil {
log.Warnf("err: %s", err)
}
log.Debugf("added hw: %s", hw)
}