-
Notifications
You must be signed in to change notification settings - Fork 128
/
route_config.go
784 lines (695 loc) · 26.8 KB
/
route_config.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
// Copyright (c) 2017 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.
//go:generate protoc --proto_path=../common/model/l3 --gogo_out=../common/model/l3 ../common/model/l3/l3.proto
package l3plugin
import (
"bytes"
"fmt"
"net"
"strconv"
"strings"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/measure"
"github.com/ligato/vpp-agent/plugins/linuxplugin/common/model/interfaces"
"github.com/ligato/vpp-agent/plugins/linuxplugin/common/model/l3"
"github.com/ligato/vpp-agent/plugins/linuxplugin/ifplugin/ifaceidx"
"github.com/ligato/vpp-agent/plugins/linuxplugin/l3plugin/l3idx"
"github.com/ligato/vpp-agent/plugins/linuxplugin/l3plugin/linuxcalls"
"github.com/ligato/vpp-agent/plugins/linuxplugin/nsplugin"
"github.com/vishvananda/netlink"
)
const (
ipv4AddrAny = "0.0.0.0/0"
ipv6AddrAny = "::/0"
)
// LinuxRouteConfigurator watches for any changes in the configuration of static routes as modelled by the proto file
// "model/l3/l3.proto" and stored in ETCD under the key "/vnf-agent/{vnf-agent}/linux/config/v1/route".
// Updates received from the northbound API are compared with the Linux network configuration and differences
// are applied through the Netlink AP
type LinuxRouteConfigurator struct {
Log logging.Logger
NsHandler *nsplugin.NsHandler
LinuxIfIdx ifaceidx.LinuxIfIndexRW
RouteIdxSeq uint32
// Index mapping for ETCD route configuration
rtIndexes l3idx.LinuxRouteIndexRW
// Index mapping for automatic interface routes (sometimes needed to evaluate network accessibility)
rtAutoIndexes l3idx.LinuxRouteIndexRW
// Cache for routes requiring interface which is missing
rtCachedIfRoutes l3idx.LinuxRouteIndexRW
// Cache for gateway routes which cannot be created at the time due to unreachable network
rtCachedGwRoutes map[string]*l3.LinuxStaticRoutes_Route
// Time measurement
Stopwatch *measure.Stopwatch // timer used to measure and store time
}
// Init initializes static route configurator and starts goroutines
func (plugin *LinuxRouteConfigurator) Init(rtIndices, rtAutoIndices, rtCachedIndexes l3idx.LinuxRouteIndexRW) error {
plugin.Log.Debug("Initializing Linux Route configurator")
plugin.rtIndexes = rtIndices
plugin.rtAutoIndexes = rtAutoIndices
plugin.rtCachedIfRoutes = rtCachedIndexes
// Default route cache
plugin.rtCachedGwRoutes = make(map[string]*l3.LinuxStaticRoutes_Route)
return nil
}
// Close closes all goroutines started during Init
func (plugin *LinuxRouteConfigurator) Close() error {
return nil
}
// ConfigureLinuxStaticRoute reacts to a new northbound Linux static route config by creating and configuring
// the route in the host network stack through Netlink API.
func (plugin *LinuxRouteConfigurator) ConfigureLinuxStaticRoute(route *l3.LinuxStaticRoutes_Route) error {
plugin.Log.Infof("Configuring linux static route %s", route.Name)
// Prepare route object
netLinkRoute := &netlink.Route{}
if route.Interface != "" {
// Find interface
_, ifData, foundIface := plugin.LinuxIfIdx.LookupIdx(route.Interface)
if !foundIface || ifData == nil {
plugin.Log.Infof("Static route %s requires non-existing interface %s, moving to cache", route.Name, route.Interface)
plugin.rtCachedIfRoutes.RegisterName(route.Name, plugin.RouteIdxSeq, route)
plugin.RouteIdxSeq++
return nil
}
netLinkRoute.LinkIndex = int(ifData.Index)
}
// Check gateway reachability
if route.Default || route.GwAddr != "" {
if !plugin.networkReachable(route.Namespace, route.GwAddr) {
plugin.rtCachedGwRoutes[route.Name] = route
plugin.Log.Debugf("Default/Gateway route %s cached, gateway address %s is currently unreachable",
route.Name, route.GwAddr)
return nil
}
}
// Check if route was not cached before, eventually remove it
_, ok := plugin.rtCachedGwRoutes[route.Name]
if ok {
delete(plugin.rtCachedGwRoutes, route.Name)
}
// Default route
if route.Default {
err := plugin.createDefaultRoute(netLinkRoute, route)
if err != nil {
plugin.Log.Error(err)
return err
}
} else {
// Static route
err := plugin.createStaticRoute(netLinkRoute, route)
if err != nil {
plugin.Log.Error(err)
return err
}
}
// Prepare and switch to namespace where the route belongs
nsMgmtCtx := nsplugin.NewNamespaceMgmtCtx()
routeNs := plugin.NsHandler.RouteNsToGeneric(route.Namespace)
revertNs, err := plugin.NsHandler.SwitchNamespace(routeNs, nsMgmtCtx, plugin.Log)
if err != nil {
plugin.Log.Error(err)
return err
}
defer revertNs()
err = linuxcalls.AddStaticRoute(route.Name, netLinkRoute, plugin.Log, measure.GetTimeLog("add-linux-route", plugin.Stopwatch))
if err != nil {
plugin.Log.Errorf("adding static route %s failed: %v (%+v)", route.Name, err, netLinkRoute)
return err
}
plugin.rtIndexes.RegisterName(routeIdentifier(netLinkRoute), plugin.RouteIdxSeq, route)
plugin.RouteIdxSeq++
plugin.Log.Debugf("Route %s registered", route.Name)
plugin.Log.Infof("Linux static route %s configured", route.Name)
// Retry default routes if some of them is not configurable now
if !route.Default {
plugin.retryDefaultRoutes(route)
}
return nil
}
// ModifyLinuxStaticRoute applies changes in the NB configuration of a Linux static route into the host network stack
// through Netlink API.
func (plugin *LinuxRouteConfigurator) ModifyLinuxStaticRoute(newRoute *l3.LinuxStaticRoutes_Route, oldRoute *l3.LinuxStaticRoutes_Route) error {
plugin.Log.Infof("Modifying linux static route %s", newRoute.Name)
var err error
// Prepare route object
netLinkRoute := &netlink.Route{}
if newRoute.Interface != "" {
// Find interface
_, ifData, foundIface := plugin.LinuxIfIdx.LookupIdx(newRoute.Interface)
if !foundIface || ifData == nil {
plugin.Log.Infof("Modified static route %s requires non-existing interface %s, moving to cache", newRoute.Name, newRoute.Interface)
plugin.rtCachedIfRoutes.RegisterName(newRoute.Name, plugin.RouteIdxSeq, newRoute)
plugin.RouteIdxSeq++
return nil
}
netLinkRoute.LinkIndex = int(ifData.Index)
}
// Check gateway reachability
if newRoute.Default || newRoute.GwAddr != "" {
if !plugin.networkReachable(newRoute.Namespace, newRoute.GwAddr) {
plugin.rtCachedGwRoutes[newRoute.Name] = newRoute
plugin.Log.Debugf("Default/Gateway route %s cached, gateway address %s is currently unreachable",
newRoute.Name, newRoute.GwAddr)
return nil
}
}
// Check if route was not cached before, eventually remove it
_, ok := plugin.rtCachedGwRoutes[newRoute.Name]
if ok {
delete(plugin.rtCachedGwRoutes, newRoute.Name)
}
// If the namespace of the new route was changed, the old route needs to be removed and the new one created in the
// new namespace
// If interface or destination IP address was changed, the old entry needs to be removed and recreated as well.
// Otherwise, ModifyRouteEntry (analogy to 'ip route replace') would create a new route instead of modifying
// the existing one
var replace bool
oldRouteNs := plugin.NsHandler.RouteNsToGeneric(oldRoute.Namespace)
newRouteNs := plugin.NsHandler.RouteNsToGeneric(newRoute.Namespace)
result := oldRouteNs.CompareNamespaces(newRouteNs)
if result != 0 || oldRoute.Interface != newRoute.Interface {
replace = true
}
// Default route
if newRoute.Default {
if !oldRoute.Default {
// In this case old route has to be removed
replace = true
}
err := plugin.createDefaultRoute(netLinkRoute, newRoute)
if err != nil {
plugin.Log.Error(err)
return err
}
} else {
if oldRoute.DstIpAddr != newRoute.Interface {
replace = true
}
if err = plugin.createStaticRoute(netLinkRoute, newRoute); err != nil {
plugin.Log.Error(err)
return err
}
}
// Static route will be removed and created anew
if replace {
return plugin.recreateLinuxStaticRoute(netLinkRoute, newRoute)
}
// Prepare namespace of related interface
nsMgmtCtx := nsplugin.NewNamespaceMgmtCtx()
routeNs := plugin.NsHandler.RouteNsToGeneric(newRoute.Namespace)
// route has to be created in the same namespace as the interface
revertNs, err := plugin.NsHandler.SwitchNamespace(routeNs, nsMgmtCtx, plugin.Log)
if err != nil {
plugin.Log.Error(err)
return err
}
defer revertNs()
// Remove old route and create a new one
if err = plugin.DeleteLinuxStaticRoute(oldRoute); err != nil {
plugin.Log.Errorf("deleting static route %s failed: %v (%+v)", oldRoute.Name, err, oldRoute)
return err
}
if err = linuxcalls.AddStaticRoute(newRoute.Name, netLinkRoute, plugin.Log, measure.GetTimeLog("add-linux-route", plugin.Stopwatch)); err != nil {
plugin.Log.Errorf("adding static route %s failed: %v (%+v)", newRoute.Name, err, netLinkRoute)
return err
}
plugin.Log.Infof("Linux static route %s modified", newRoute.Name)
// Retry default routes if some of them is not configurable
if !newRoute.Default {
plugin.retryDefaultRoutes(newRoute)
}
return nil
}
// DeleteLinuxStaticRoute reacts to a removed NB configuration of a Linux static route entry.
func (plugin *LinuxRouteConfigurator) DeleteLinuxStaticRoute(route *l3.LinuxStaticRoutes_Route) error {
plugin.Log.Infof("Removing linux static route %s", route.Name)
var err error
// Check if route is in cache waiting on interface
if _, _, found := plugin.rtCachedIfRoutes.LookupIdx(route.Name); found {
plugin.rtCachedIfRoutes.UnregisterName(route.Name)
plugin.Log.Debugf("Route %s removed from interface cache", route.Name)
return nil
}
// Check if route is in cache waiting for gateway address reachability
for _, cachedRoute := range plugin.rtCachedGwRoutes {
if cachedRoute.Name == route.Name {
delete(plugin.rtCachedGwRoutes, cachedRoute.Name)
plugin.Log.Debugf("Route %s removed from gw cache", route.Name)
return nil
}
}
// Prepare route object
netLinkRoute := &netlink.Route{}
if route.Interface != "" {
// Find interface
_, ifData, foundIface := plugin.LinuxIfIdx.LookupIdx(route.Interface)
if !foundIface || ifData == nil {
return fmt.Errorf("cannot delete static route %s, interface %s not found", route.Name, route.Interface)
}
netLinkRoute.LinkIndex = int(ifData.Index)
}
// Destination IP address
if route.DstIpAddr != "" {
addressWithPrefix := strings.Split(route.DstIpAddr, "/")
if len(addressWithPrefix) > 1 {
dstIPAddr := &net.IPNet{}
_, dstIPAddr, err = net.ParseCIDR(route.DstIpAddr)
if err != nil {
plugin.Log.Error(err)
return err
}
netLinkRoute.Dst = dstIPAddr
} else {
plugin.Log.Error("static route's dst address mask not set, route %s may not be removed", route.Name)
}
}
// Gateway IP address
if route.GwAddr != "" {
gateway := net.ParseIP(route.GwAddr)
if gateway != nil {
netLinkRoute.Gw = gateway
} else {
plugin.Log.Error("static route's gateway address %s has incorrect format, route %s may not be removed",
route.GwAddr, route.Name)
}
}
if netLinkRoute.Dst == nil && netLinkRoute.Gw == nil {
return fmt.Errorf("cannot delete static route %s, requred at least destination or gateway address", route.Name)
}
// Scope
if route.Scope != nil {
netLinkRoute.Scope = plugin.parseRouteScope(route.Scope)
}
// Prepare and switch to the namespace where the route belongs
nsMgmtCtx := nsplugin.NewNamespaceMgmtCtx()
routeNs := plugin.NsHandler.RouteNsToGeneric(route.Namespace)
revertNs, err := plugin.NsHandler.SwitchNamespace(routeNs, nsMgmtCtx, plugin.Log)
if err != nil {
plugin.Log.Error(err)
return err
}
defer revertNs()
err = linuxcalls.DeleteStaticRoute(route.Name, netLinkRoute, plugin.Log, measure.GetTimeLog("del-linux-route", plugin.Stopwatch))
if err != nil {
plugin.Log.Errorf("deleting static route %q failed: %v (%+v)", route.Name, err, netLinkRoute)
return err
}
_, _, found := plugin.rtIndexes.UnregisterName(routeIdentifier(netLinkRoute))
if !found {
plugin.Log.Warnf("Attempt to unregister non-registered route %s", route.Name)
}
plugin.Log.Debugf("Route %s unregistered", route.Name)
plugin.Log.Infof("Linux static route %s removed", route.Name)
return nil
}
// ResolveCreatedInterface manages static routes for new interface. Linux interface also creates its own route which
// can make other routes accessible and ready to create - the case is also resolved here.
func (plugin *LinuxRouteConfigurator) ResolveCreatedInterface(name string, index uint32) error {
plugin.Log.Infof("Linux static route configurator: resolve new interface %s (idx %d)", name, index)
// Search mapping for cached routes using the new interface
cachedIfRoutes := plugin.rtCachedIfRoutes.LookupNamesByInterface(name)
if len(cachedIfRoutes) > 0 {
plugin.Log.Debugf("Found %d cached routes for interface %s", len(cachedIfRoutes), name)
// Store default routes, they have to be configured as the last ones
var defRoutes []*l3.LinuxStaticRoutes_Route
// Static routes
for _, cachedRoute := range cachedIfRoutes {
if cachedRoute.Default {
defRoutes = append(defRoutes, cachedRoute)
continue
}
if err := plugin.ConfigureLinuxStaticRoute(cachedRoute); err != nil {
plugin.Log.Warn(err)
return err
}
// Remove from cache
plugin.rtCachedIfRoutes.UnregisterName(cachedRoute.Name)
}
// Default routes
for _, cachedDefaultRoute := range defRoutes {
if err := plugin.ConfigureLinuxStaticRoute(cachedDefaultRoute); err != nil {
plugin.Log.Warn(err)
return err
}
// Remove from cache
plugin.rtCachedIfRoutes.UnregisterName(cachedDefaultRoute.Name)
}
}
// Interface also created its own route, so try to re-configure default routes
err := plugin.processAutoRoutes(name, index)
if err != nil {
plugin.Log.Error(err)
}
// Try to reconfigure cached gateway routes
if len(plugin.rtCachedGwRoutes) > 0 {
plugin.Log.Debugf("Found %d cached gateway routes", len(cachedIfRoutes))
// Store default routes, they have to be configured as the last ones
defRoutes := make(map[string]*l3.LinuxStaticRoutes_Route)
for _, cachedRoute := range plugin.rtCachedGwRoutes {
// Check accessibility
if !plugin.networkReachable(cachedRoute.Namespace, cachedRoute.GwAddr) {
continue
} else {
}
if cachedRoute.Default {
defRoutes[cachedRoute.Name] = cachedRoute
continue
}
if err := plugin.ConfigureLinuxStaticRoute(cachedRoute); err != nil {
plugin.Log.Warn(err)
return err
}
// Remove from cache
delete(plugin.rtCachedGwRoutes, cachedRoute.Name)
}
// Default routes
for _, cachedDefaultRoute := range defRoutes {
if err := plugin.ConfigureLinuxStaticRoute(cachedDefaultRoute); err != nil {
plugin.Log.Warn(err)
return err
}
// Remove from cache
delete(plugin.rtCachedGwRoutes, cachedDefaultRoute.Name)
}
}
return err
}
// ResolveDeletedInterface manages static routes for removed interface
func (plugin *LinuxRouteConfigurator) ResolveDeletedInterface(name string, index uint32) error {
plugin.Log.Infof("Linux static route configurator: resolve deleted interface %v (idx %d)", name, index)
// Search mapping for configured linux routes using the new interface
confRoutes := plugin.rtIndexes.LookupNamesByInterface(name)
if len(confRoutes) > 0 {
plugin.Log.Debugf("Found %d routes belonging to the removed interface %s", len(confRoutes), name)
for _, rt := range confRoutes {
// Add to un-configured. If the interface will be recreated, all routes are configured back
plugin.rtCachedIfRoutes.RegisterName(rt.Name, plugin.RouteIdxSeq, rt)
plugin.RouteIdxSeq++
}
}
return nil
}
// Create default route object with gateway address. Destination address has to be set in such a case
func (plugin *LinuxRouteConfigurator) createDefaultRoute(netLinkRoute *netlink.Route, route *l3.LinuxStaticRoutes_Route) (err error) {
// Gateway
gateway := net.ParseIP(route.GwAddr)
if gateway == nil {
fmt.Errorf("unable to create route %s as default, gateway is nil", route.Name)
}
netLinkRoute.Gw = gateway
// Destination address
dstIPAddr := route.DstIpAddr
if dstIPAddr == "" {
dstIPAddr = ipv4AddrAny
}
if dstIPAddr != ipv4AddrAny && dstIPAddr != ipv6AddrAny {
plugin.Log.Warnf("route marked as default has dst address set to %s. The address will be ignored", dstIPAddr)
dstIPAddr = ipv4AddrAny
}
_, netLinkRoute.Dst, err = net.ParseCIDR(dstIPAddr)
if err != nil {
plugin.Log.Error(err)
return err
}
// Priority
if route.Metric != 0 {
netLinkRoute.Priority = int(route.Metric)
}
plugin.Log.Debugf("Creating default route with gw ip %s", netLinkRoute.Gw)
return nil
}
// Create static route from provided data
func (plugin *LinuxRouteConfigurator) createStaticRoute(netLinkRoute *netlink.Route, route *l3.LinuxStaticRoutes_Route) error {
var err error
// Destination IP address
if route.DstIpAddr != "" {
addressWithPrefix := strings.Split(route.DstIpAddr, "/")
dstIPAddr := &net.IPNet{}
if len(addressWithPrefix) > 1 {
_, dstIPAddr, err = net.ParseCIDR(route.DstIpAddr)
if err != nil {
plugin.Log.Error(err)
return err
}
} else {
return fmt.Errorf("cannot create static route %s, dst address net mask not set", route.Name)
}
plugin.Log.Infof("IP address %s set as dst for route %s", route.DstIpAddr, route.Name)
netLinkRoute.Dst = dstIPAddr
} else {
return fmt.Errorf("cannot create static route %s, destination addres not set", route.Name)
}
// Set gateway if exists
gateway := net.ParseIP(route.GwAddr)
if gateway != nil {
netLinkRoute.Gw = gateway
plugin.Log.Infof("Gateway address %s set for route %s", route.GwAddr, route.Name)
}
// Source IP address is exists
srcIPAddr := net.ParseIP(route.SrcIpAddr)
if srcIPAddr != nil {
netLinkRoute.Src = srcIPAddr
plugin.Log.Infof("IP address %s set as src for route %s", route.SrcIpAddr, route.Name)
}
// Scope
if route.Scope != nil {
netLinkRoute.Scope = plugin.parseRouteScope(route.Scope)
}
// Priority
if route.Metric != 0 {
netLinkRoute.Priority = int(route.Metric)
}
// Table
netLinkRoute.Table = int(route.Table)
plugin.Log.Debugf("Creating static route with destination ip %s", netLinkRoute.Dst)
return nil
}
// Update linux static route using modify (analogy to 'ip route replace')
func (plugin *LinuxRouteConfigurator) recreateLinuxStaticRoute(netLinkRoute *netlink.Route, route *l3.LinuxStaticRoutes_Route) error {
plugin.Log.Debugf("Route %s modification caused the route to be removed and crated again", route.Name)
// Prepare namespace of related interface
nsMgmtCtx := nsplugin.NewNamespaceMgmtCtx()
routeNs := plugin.NsHandler.RouteNsToGeneric(route.Namespace)
// route has to be created in the same namespace as the interface
revertNs, err := plugin.NsHandler.SwitchNamespace(routeNs, nsMgmtCtx, plugin.Log)
if err != nil {
plugin.Log.Error(err)
return err
}
defer revertNs()
// Update existing route
return linuxcalls.ModifyStaticRoute(route.Name, netLinkRoute, plugin.Log, measure.GetTimeLog("modify-linux-route", plugin.Stopwatch))
}
// Tries to configure again cached default/gateway routes (as a reaction to the new route)
func (plugin *LinuxRouteConfigurator) retryDefaultRoutes(route *l3.LinuxStaticRoutes_Route) {
plugin.Log.Debug("Retrying to configure default routes")
for _, defRoute := range plugin.rtCachedGwRoutes {
// Filter routes from different namespaces
if defRoute.Namespace != nil && route.Namespace == nil || defRoute.Namespace == nil && route.Namespace != nil {
continue
}
if defRoute.Namespace != nil && route.Namespace != nil && defRoute.Namespace.Name != route.Namespace.Name {
continue
}
// Parse gateway and default address
gwIPParsed := net.ParseIP(defRoute.GwAddr)
_, dstNet, err := net.ParseCIDR(route.DstIpAddr)
if err != nil {
plugin.Log.Errorf("Error parsing IP address %s: %v", route.DstIpAddr, err)
continue
}
if dstNet.Contains(gwIPParsed) {
// Default/Gateway route can be now configured
if err := plugin.ConfigureLinuxStaticRoute(defRoute); err != nil {
plugin.Log.Errorf("Error while configuring route %s: %v", route.Name, err)
}
delete(plugin.rtCachedGwRoutes, defRoute.Name)
} else {
plugin.Log.Debugf("%s is not within %s", defRoute.GwAddr, dstNet.IP)
}
}
}
// Handles automatic route created by adding interface. Method look for routes related to the interface and its
// IP address in its namespace.
// Note: read route's destination address does not contain mask. This value is determined from interfaces' IP address.
// Automatic routes are store in separate mapping and their names are generated.
func (plugin *LinuxRouteConfigurator) processAutoRoutes(ifName string, ifIdx uint32) error {
plugin.Log.Debugf("Processing automatic interfaces for %s", ifName)
// Look for metadata
_, ifData, found := plugin.LinuxIfIdx.LookupIdx(ifName)
if !found {
return fmt.Errorf("interface %s not found in the mapping", ifName)
}
if ifData == nil || ifData.Data == nil {
return fmt.Errorf("interface %s data not found in the mapping", ifName)
}
// Move to interface with the interface
if ifData.Data.Namespace != nil {
nsMgmtCtx := nsplugin.NewNamespaceMgmtCtx()
// Switch to namespace
ifNs := plugin.NsHandler.IfNsToGeneric(ifData.Data.Namespace)
revertNs, err := plugin.NsHandler.SwitchNamespace(ifNs, nsMgmtCtx, plugin.Log)
if err != nil {
return fmt.Errorf("RESYNC Linux route %s: failed to switch to namespace %s: %v",
ifData.Data.Name, ifData.Data.Namespace.Name, err)
}
defer revertNs()
}
// Get interface
link, err := netlink.LinkByName(ifData.Data.HostIfName)
if err != nil {
return fmt.Errorf("cannot read linux interface %s (host %s): %v", ifName, ifData.Data.HostIfName, err)
}
// Read all routes belonging to the interface
linuxRts, err := netlink.RouteList(link, netlink.FAMILY_ALL)
if err != nil {
return fmt.Errorf("cannot read linux routes for interface %s (host %s): %v", ifName, ifData.Data.HostIfName, err)
}
// Iterate over link addresses and look for ones related to t
for rtIdx, linuxRt := range linuxRts {
if linuxRt.Dst == nil {
continue
}
route := plugin.transformRoute(linuxRt, ifData.Data.HostIfName)
// Route's destination address is read without mask. Use interface data to fill it.
var routeFound bool
for ipIdx, ifIP := range ifData.Data.IpAddresses {
_, ifDst, err := net.ParseCIDR(ifIP)
if err != nil {
return err
}
if bytes.Compare(linuxRt.Dst.IP, ifDst.IP) == 0 {
// Transform destination IP and namespace
route.DstIpAddr = ifData.Data.IpAddresses[ipIdx]
route.Namespace = transformNamespace(ifData.Data.Namespace)
routeFound = true
}
}
if !routeFound {
plugin.Log.Debugf("Route with IP %s skipped", linuxRt.Dst.IP.String())
continue
}
// Generate name
route.Name = ifName + strconv.Itoa(rtIdx)
// In case there is obsolete route with the same name, remove it
plugin.rtAutoIndexes.UnregisterName(route.Name)
plugin.rtAutoIndexes.RegisterName(route.Name, plugin.RouteIdxSeq, route)
plugin.RouteIdxSeq++
// Also try to configure default routes
plugin.retryDefaultRoutes(route)
}
return nil
}
// Transform linux netlink route type to proto message type
func (plugin *LinuxRouteConfigurator) transformRoute(linuxRt netlink.Route, ifName string) *l3.LinuxStaticRoutes_Route {
var dstAddr, srcAddr, gwAddr string
// Destination address
if linuxRt.Dst != nil {
// Transform only IP (without mask)
dstAddr = linuxRt.Dst.IP.String()
}
// Source address
if linuxRt.Src != nil {
srcAddr = linuxRt.Src.String()
}
// Gateway address
if linuxRt.Gw != nil {
gwAddr = linuxRt.Gw.String()
}
if dstAddr == "" || dstAddr == ipv4AddrAny || dstAddr == ipv6AddrAny {
// Default route
return &l3.LinuxStaticRoutes_Route{
Default: true,
Interface: ifName,
GwAddr: gwAddr,
Metric: uint32(linuxRt.Priority),
}
}
// Static route
return &l3.LinuxStaticRoutes_Route{
Interface: ifName,
DstIpAddr: dstAddr,
SrcIpAddr: srcAddr,
GwAddr: gwAddr,
Scope: plugin.parseLinuxRouteScope(linuxRt.Scope),
Metric: uint32(linuxRt.Priority),
Table: uint32(linuxRt.Table),
}
}
// Interface namespace type -> route namespace type
func transformNamespace(ifNs *interfaces.LinuxInterfaces_Interface_Namespace) *l3.LinuxStaticRoutes_Route_Namespace {
if ifNs == nil {
return nil
}
return &l3.LinuxStaticRoutes_Route_Namespace{
Type: func(ifType interfaces.LinuxInterfaces_Interface_Namespace_NamespaceType) l3.LinuxStaticRoutes_Route_Namespace_NamespaceType {
switch ifType {
case interfaces.LinuxInterfaces_Interface_Namespace_PID_REF_NS:
return l3.LinuxStaticRoutes_Route_Namespace_PID_REF_NS
case interfaces.LinuxInterfaces_Interface_Namespace_MICROSERVICE_REF_NS:
return l3.LinuxStaticRoutes_Route_Namespace_MICROSERVICE_REF_NS
case interfaces.LinuxInterfaces_Interface_Namespace_NAMED_NS:
return l3.LinuxStaticRoutes_Route_Namespace_NAMED_NS
case interfaces.LinuxInterfaces_Interface_Namespace_FILE_REF_NS:
return l3.LinuxStaticRoutes_Route_Namespace_FILE_REF_NS
default:
return l3.LinuxStaticRoutes_Route_Namespace_PID_REF_NS
}
}(ifNs.Type),
Pid: ifNs.Pid,
Microservice: ifNs.Microservice,
Name: ifNs.Name,
Filepath: ifNs.Filepath,
}
}
// Agent route scope -> netlink route scope
func (plugin *LinuxRouteConfigurator) parseRouteScope(scope *l3.LinuxStaticRoutes_Route_Scope) netlink.Scope {
switch scope.Type {
case l3.LinuxStaticRoutes_Route_Scope_GLOBAL:
return netlink.SCOPE_UNIVERSE
case l3.LinuxStaticRoutes_Route_Scope_HOST:
return netlink.SCOPE_HOST
case l3.LinuxStaticRoutes_Route_Scope_LINK:
return netlink.SCOPE_LINK
case l3.LinuxStaticRoutes_Route_Scope_SITE:
return netlink.SCOPE_SITE
default:
plugin.Log.Infof("Unknown scope type, setting to default (link): %v", scope.Type)
return netlink.SCOPE_LINK
}
}
// Verifies whether address network is reachable.
func (plugin *LinuxRouteConfigurator) networkReachable(ns *l3.LinuxStaticRoutes_Route_Namespace, ipAddress string) bool {
// Try for registered configuration routes
registeredRoute, err := plugin.rtIndexes.LookupRouteByIP(ns, ipAddress)
if err != nil {
plugin.Log.Errorf("Failed to resolve accessibility of %s (registered): %v", ipAddress, err)
}
// Try for registered automatic (interface-added) routes
autoRoute, err := plugin.rtAutoIndexes.LookupRouteByIP(ns, ipAddress)
if err != nil {
plugin.Log.Errorf("Failed to resolve accessibility of %s (auto): %v", ipAddress, err)
}
if registeredRoute != nil || autoRoute != nil {
plugin.Log.Debugf("Network %s is reachable", ipAddress)
return true
}
return false
}
func routeIdentifier(route *netlink.Route) string {
if route.Dst == nil {
return fmt.Sprintf("default-iface%d-table%v-%s", route.LinkIndex, route.Table, route.Gw.String())
}
return fmt.Sprintf("dst%s-iface%d-table%v-%s", route.Dst.IP.String(), route.LinkIndex, route.Table, route.Gw.String())
}