-
Notifications
You must be signed in to change notification settings - Fork 127
/
route.go
327 lines (289 loc) · 9.9 KB
/
route.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package descriptor
import (
"bytes"
"fmt"
"net"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/utils/addrs"
netalloc_api "github.com/ligato/vpp-agent/api/models/netalloc"
interfaces "github.com/ligato/vpp-agent/api/models/vpp/interfaces"
l3 "github.com/ligato/vpp-agent/api/models/vpp/l3"
"github.com/ligato/vpp-agent/pkg/models"
kvs "github.com/ligato/vpp-agent/plugins/kvscheduler/api"
"github.com/ligato/vpp-agent/plugins/netalloc"
netalloc_descr "github.com/ligato/vpp-agent/plugins/netalloc/descriptor"
ifdescriptor "github.com/ligato/vpp-agent/plugins/vpp/ifplugin/descriptor"
"github.com/ligato/vpp-agent/plugins/vpp/l3plugin/descriptor/adapter"
"github.com/ligato/vpp-agent/plugins/vpp/l3plugin/vppcalls"
)
const (
// RouteDescriptorName is the name of the descriptor for static routes.
RouteDescriptorName = "vpp-route"
// dependency labels
routeOutInterfaceDep = "interface-exists"
vrfTableDep = "vrf-table-exists"
viaVrfTableDep = "via-vrf-table-exists"
// static route weight by default
defaultWeight = 1
)
// RouteDescriptor teaches KVScheduler how to configure VPP routes.
type RouteDescriptor struct {
log logging.Logger
routeHandler vppcalls.RouteVppAPI
addrAlloc netalloc.AddressAllocator
}
// NewRouteDescriptor creates a new instance of the Route descriptor.
func NewRouteDescriptor(
routeHandler vppcalls.RouteVppAPI, addrAlloc netalloc.AddressAllocator,
log logging.PluginLogger) *kvs.KVDescriptor {
ctx := &RouteDescriptor{
routeHandler: routeHandler,
addrAlloc: addrAlloc,
log: log.NewLogger("static-route-descriptor"),
}
typedDescr := &adapter.RouteDescriptor{
Name: RouteDescriptorName,
NBKeyPrefix: l3.ModelRoute.KeyPrefix(),
ValueTypeName: l3.ModelRoute.ProtoName(),
KeySelector: l3.ModelRoute.IsKeyValid,
ValueComparator: ctx.EquivalentRoutes,
Validate: ctx.Validate,
Create: ctx.Create,
Delete: ctx.Delete,
Retrieve: ctx.Retrieve,
Dependencies: ctx.Dependencies,
RetrieveDependencies: []string{
netalloc_descr.IPAllocDescriptorName,
ifdescriptor.InterfaceDescriptorName,
VrfTableDescriptorName},
}
return adapter.NewRouteDescriptor(typedDescr)
}
// EquivalentRoutes is case-insensitive comparison function for l3.Route.
func (d *RouteDescriptor) EquivalentRoutes(key string, oldRoute, newRoute *l3.Route) bool {
if oldRoute.GetType() != newRoute.GetType() ||
oldRoute.GetVrfId() != newRoute.GetVrfId() ||
oldRoute.GetViaVrfId() != newRoute.GetViaVrfId() ||
oldRoute.GetOutgoingInterface() != newRoute.GetOutgoingInterface() ||
getWeight(oldRoute) != getWeight(newRoute) ||
oldRoute.GetPreference() != newRoute.GetPreference() {
return false
}
// compare dst networks
if !equalNetworks(oldRoute.DstNetwork, newRoute.DstNetwork) {
return false
}
// compare gw addresses (next hop)
if !equalAddrs(getGwAddr(oldRoute), getGwAddr(newRoute)) {
return false
}
return true
}
// Validate validates VPP static route configuration.
func (d *RouteDescriptor) Validate(key string, route *l3.Route) (err error) {
// validate destination network
err = d.addrAlloc.ValidateIPAddress(route.DstNetwork, "", "dst_network",
netalloc.GWRefAllowed)
if err != nil {
return err
}
// validate next hop address (GW)
err = d.addrAlloc.ValidateIPAddress(getGwAddr(route), route.OutgoingInterface,
"gw_addr", netalloc.GWRefRequired)
if err != nil {
return err
}
// validate IP network implied by the IP and prefix length
if !strings.HasPrefix(route.DstNetwork, netalloc_api.AllocRefPrefix) {
_, ipNet, _ := net.ParseCIDR(route.DstNetwork)
if strings.ToLower(ipNet.String()) != strings.ToLower(route.DstNetwork) {
e := fmt.Errorf("DstNetwork (%s) must represent IP network (%s)",
route.DstNetwork, ipNet.String())
return kvs.NewInvalidValueError(e, "dst_network")
}
}
// TODO: validate mix of IP versions?
return nil
}
// Create adds VPP static route.
func (d *RouteDescriptor) Create(key string, route *l3.Route) (metadata interface{}, err error) {
err = d.routeHandler.VppAddRoute(route)
if err != nil {
return nil, err
}
return nil, nil
}
// Delete removes VPP static route.
func (d *RouteDescriptor) Delete(key string, route *l3.Route, metadata interface{}) error {
err := d.routeHandler.VppDelRoute(route)
if err != nil {
return err
}
return nil
}
// Retrieve returns all routes associated with interfaces managed by this agent.
func (d *RouteDescriptor) Retrieve(correlate []adapter.RouteKVWithMetadata) (
retrieved []adapter.RouteKVWithMetadata, err error,
) {
// prepare expected configuration with de-referenced netalloc links
nbCfg := make(map[string]*l3.Route)
expCfg := make(map[string]*l3.Route)
for _, kv := range correlate {
dstNetwork := kv.Value.DstNetwork
parsed, err := d.addrAlloc.GetOrParseIPAddress(kv.Value.DstNetwork,
"", netalloc_api.IPAddressForm_ADDR_NET)
if err == nil {
dstNetwork = parsed.String()
}
nextHop := kv.Value.NextHopAddr
parsed, err = d.addrAlloc.GetOrParseIPAddress(getGwAddr(kv.Value),
kv.Value.OutgoingInterface, netalloc_api.IPAddressForm_ADDR_ONLY)
if err == nil {
nextHop = parsed.IP.String()
}
route := proto.Clone(kv.Value).(*l3.Route)
route.DstNetwork = dstNetwork
route.NextHopAddr = nextHop
key := models.Key(route)
expCfg[key] = route
nbCfg[key] = kv.Value
}
// Retrieve VPP route configuration
routes, err := d.routeHandler.DumpRoutes()
if err != nil {
return nil, errors.Errorf("failed to dump VPP routes: %v", err)
}
for _, route := range routes {
key := models.Key(route.Route)
value := route.Route
origin := kvs.UnknownOrigin
// correlate with the expected configuration
if expCfg, hasExpCfg := expCfg[key]; hasExpCfg {
if d.EquivalentRoutes(key, value, expCfg) {
value = nbCfg[key]
// recreate the key in case the dest. IP or GW IP were replaced with netalloc link
key = models.Key(value)
origin = kvs.FromNB
}
}
retrieved = append(retrieved, adapter.RouteKVWithMetadata{
Key: key,
Value: value,
Origin: origin,
})
}
return retrieved, nil
}
// Dependencies lists dependencies for a VPP route.
func (d *RouteDescriptor) Dependencies(key string, route *l3.Route) []kvs.Dependency {
var dependencies []kvs.Dependency
// the outgoing interface must exist and be UP
if route.OutgoingInterface != "" {
dependencies = append(dependencies, kvs.Dependency{
Label: routeOutInterfaceDep,
Key: interfaces.InterfaceKey(route.OutgoingInterface),
})
}
// non-zero VRFs
var protocol l3.VrfTable_Protocol
_, isIPv6, _ := addrs.ParseIPWithPrefix(route.DstNetwork)
if isIPv6 {
protocol = l3.VrfTable_IPV6
}
if route.VrfId != 0 {
dependencies = append(dependencies, kvs.Dependency{
Label: vrfTableDep,
Key: l3.VrfTableKey(route.VrfId, protocol),
})
}
if route.Type == l3.Route_INTER_VRF && route.ViaVrfId != 0 {
dependencies = append(dependencies, kvs.Dependency{
Label: viaVrfTableDep,
Key: l3.VrfTableKey(route.ViaVrfId, protocol),
})
}
// if destination network is netalloc reference, then the address must be allocated first
allocDep, hasAllocDep := d.addrAlloc.GetAddressAllocDep(route.DstNetwork,
"", "dst_network-")
if hasAllocDep {
dependencies = append(dependencies, allocDep)
}
// if GW is netalloc reference, then the address must be allocated first
allocDep, hasAllocDep = d.addrAlloc.GetAddressAllocDep(route.NextHopAddr,
route.OutgoingInterface, "gw_addr-")
if hasAllocDep {
dependencies = append(dependencies, allocDep)
}
// TODO: perhaps check GW routability
return dependencies
}
// equalAddrs compares two IP addresses for equality.
func equalAddrs(addr1, addr2 string) bool {
if strings.HasPrefix(addr1, netalloc_api.AllocRefPrefix) ||
strings.HasPrefix(addr1, netalloc_api.AllocRefPrefix) {
return addr1 == addr2
}
a1 := net.ParseIP(addr1)
a2 := net.ParseIP(addr2)
if a1 == nil || a2 == nil {
// if parsing fails, compare as strings
return strings.ToLower(addr1) == strings.ToLower(addr2)
}
return a1.Equal(a2)
}
// getGwAddr returns the GW address chosen in the given route, handling the cases
// when it is left undefined.
func getGwAddr(route *l3.Route) string {
if route.GetNextHopAddr() != "" {
return route.GetNextHopAddr()
}
// return zero address
// - with netalloc'd destination network, just assume it is for IPv4
if !strings.HasPrefix(route.GetDstNetwork(), netalloc_api.AllocRefPrefix) {
_, dstIPNet, err := net.ParseCIDR(route.GetDstNetwork())
if err != nil {
return ""
}
if dstIPNet.IP.To4() == nil {
return net.IPv6zero.String()
}
}
return net.IPv4zero.String()
}
// getWeight returns static route weight, handling the cases when it is left undefined.
func getWeight(route *l3.Route) uint32 {
if route.Weight == 0 {
return defaultWeight
}
return route.Weight
}
// equalNetworks compares two IP networks for equality.
func equalNetworks(net1, net2 string) bool {
if strings.HasPrefix(net1, netalloc_api.AllocRefPrefix) ||
strings.HasPrefix(net2, netalloc_api.AllocRefPrefix) {
return net1 == net2
}
_, n1, err1 := net.ParseCIDR(net1)
_, n2, err2 := net.ParseCIDR(net2)
if err1 != nil || err2 != nil {
// if parsing fails, compare as strings
return strings.ToLower(net1) == strings.ToLower(net2)
}
return n1.IP.Equal(n2.IP) && bytes.Equal(n1.Mask, n2.Mask)
}