Repository navigation
Expand file tree
/
Copy pathnetlink.go
More file actions
223 lines (184 loc) · 5.41 KB
/
Copy pathnetlink.go
File metadata and controls
223 lines (184 loc) · 5.41 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
//go:build linux
package metalbond
import (
"errors"
"fmt"
"net"
"net/netip"
"slices"
"sync"
log "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
)
const METALBOND_RT_PROTO netlink.RouteProtocol = 254
type routeKey struct {
vni VNI
dest Destination
}
type NetlinkClient struct {
config NetlinkClientConfig
tunDevice netlink.Link
mtx sync.Mutex
routes map[routeKey][]NextHop
}
type NetlinkClientConfig struct {
VNITableMap map[VNI]int
LinkName string
IPv4Only bool
}
func NewNetlinkClient(config NetlinkClientConfig) (*NetlinkClient, error) {
link, err := netlink.LinkByName(config.LinkName)
if err != nil {
return nil, fmt.Errorf("cannot find tun device '%s': %v", config.LinkName, err)
}
c := &NetlinkClient{
config: config,
tunDevice: link,
routes: make(map[routeKey][]NextHop),
}
// Ensure we have no stale routes in the route tables which were configured.
err = c.Cleanup()
if err != nil {
return nil, fmt.Errorf("unable to clean up stale routes: %w", err)
}
return c, nil
}
func (c *NetlinkClient) AddRoute(vni VNI, dest Destination, hop NextHop) error {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.config.IPv4Only && dest.IPVersion != IPV4 {
log.Infof("Received non-IPv4 route will not be installed in kernel route table (IPv4-only mode)")
return nil
}
table, exists := c.config.VNITableMap[vni]
if !exists {
return fmt.Errorf("no route table ID known for given VNI")
}
key := routeKey{vni: vni, dest: dest}
current := c.routes[key]
if slices.Contains(current, hop) {
return nil
}
next := append(slices.Clone(current), hop)
// Ensure deterministic order to make comparing route tables across machines
// easier (e.g. via `ip route ...`).
slices.SortFunc(next, func(a, b NextHop) int {
return a.TargetAddress.Compare(b.TargetAddress)
})
err := c.replaceRoute(dest.Prefix, next, table)
if err != nil {
return err
}
c.routes[key] = next
return nil
}
func (c *NetlinkClient) RemoveRoute(vni VNI, dest Destination, hop NextHop) error {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.config.IPv4Only && dest.IPVersion != IPV4 {
return nil
}
table, exists := c.config.VNITableMap[vni]
if !exists {
return fmt.Errorf("no route table ID known for given VNI")
}
key := routeKey{vni: vni, dest: dest}
current, exists := c.routes[key]
if !exists {
return fmt.Errorf("no routes known for VNI %d dest %s", vni, dest)
}
next := slices.DeleteFunc(slices.Clone(current), func(e NextHop) bool { return e == hop })
if len(current) == len(next) {
// Delete was a no-op, skip the call to the kernel.
return nil
}
if len(next) == 0 {
if err := c.removeRoute(dest.Prefix, table); err != nil {
return fmt.Errorf("cannot remove route to %s from kernel: %w", dest, err)
}
delete(c.routes, key)
} else {
if err := c.replaceRoute(dest.Prefix, next, table); err != nil {
return fmt.Errorf("cannot install replacement route for %s: %w", dest, err)
}
c.routes[key] = next
}
return nil
}
// Cleanup retrieves all routes that have been installed into one of the
// configured tables and deletes them to prevent stale routes from lingering.
func (c *NetlinkClient) Cleanup() error {
var errs []error
for _, table := range c.config.VNITableMap {
routes, err := netlink.RouteListFiltered(
netlink.FAMILY_ALL,
&netlink.Route{
Table: table,
Protocol: METALBOND_RT_PROTO,
},
netlink.RT_FILTER_TABLE|netlink.RT_FILTER_PROTOCOL,
)
if err != nil {
errs = append(errs, err)
continue
}
for _, r := range routes {
err := netlink.RouteDel(&r)
if err != nil {
errs = append(errs, err)
}
}
}
return errors.Join(errs...)
}
func (c *NetlinkClient) replaceRoute(dst netip.Prefix, hops []NextHop, table int) error {
var nextHopInfos []*netlink.NexthopInfo
for _, hop := range hops {
// TODO: when installing routes with too many next hops netlink
// eventually fails to properly serialize route messages. As a result
// all sorts of things get wedged and investigating failures becomes
// impossible as the routes can no longer be retrieved due to issues
// with the route size. This is a temporary hack to fix the issue we
// have right now. The proper solution is to create next-hop groups
// independent of the routes and reference those in the route.
if len(nextHopInfos) >= 3 {
break
}
nextHopInfos = append(nextHopInfos, &netlink.NexthopInfo{
LinkIndex: c.tunDevice.Attrs().Index,
Encap: &netlink.IP6tnlEncap{
Dst: hop.TargetAddress.AsSlice(),
Src: net.IPv6zero,
},
})
}
route := &netlink.Route{
Table: table,
Protocol: METALBOND_RT_PROTO,
Dst: &net.IPNet{
IP: dst.Addr().AsSlice(),
Mask: net.CIDRMask(dst.Bits(), dst.Addr().BitLen()),
},
MultiPath: nextHopInfos,
}
if err := netlink.RouteReplace(route); err != nil {
return fmt.Errorf("cannot replace route to %s (table %d) in kernel: %w", dst, table, err)
}
return nil
}
func (c *NetlinkClient) removeRoute(dst netip.Prefix, table int) error {
route := &netlink.Route{
Table: table,
Protocol: METALBOND_RT_PROTO,
Dst: &net.IPNet{
IP: dst.Addr().AsSlice(),
Mask: net.CIDRMask(dst.Bits(), dst.Addr().BitLen()),
},
}
if err := netlink.RouteDel(route); err != nil {
return fmt.Errorf("cannot remove route to %s (table %d) from kernel: %w", dst, table, err)
}
return nil
}