Newer
Older
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
package adjRIBOut
import (
"fmt"
"sync"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
bnet "github.com/bio-routing/bio-rd/net"
log "github.com/sirupsen/logrus"
)
// AdjRIBOut represents an Adjacency RIB Out with BGP add path
type AdjRIBOut struct {
routingtable.ClientManager
rt *routingtable.RoutingTable
neighbor *routingtable.Neighbor
pathIDManager *pathIDManager
mu sync.RWMutex
exportFilter *filter.Filter
}
// New creates a new Adjacency RIB Out with BGP add path
func New(neighbor *routingtable.Neighbor, exportFilter *filter.Filter) *AdjRIBOut {
a := &AdjRIBOut{
rt: routingtable.NewRoutingTable(),
neighbor: neighbor,
pathIDManager: newPathIDManager(),
exportFilter: exportFilter,
}
a.ClientManager = routingtable.NewClientManager(a)
return a
}
// UpdateNewClient sends current state to a new client
func (a *AdjRIBOut) UpdateNewClient(client routingtable.RouteTableClient) error {
return nil
}
// AddPath adds path p to prefix `pfx`
func (a *AdjRIBOut) AddPath(pfx bnet.Prefix, p *route.Path) error {
if !routingtable.ShouldPropagateUpdate(pfx, p, a.neighbor) {
return nil
}
p = p.Copy()
if !a.neighbor.IBGP && !a.neighbor.RouteServerClient {
p.BGPPath.ASPath = fmt.Sprintf("%d %s", a.neighbor.LocalASN, p.BGPPath.ASPath)
}
if !a.neighbor.IBGP && !a.neighbor.RouteServerClient {
p.BGPPath.NextHop = a.neighbor.LocalAddress
}
p, reject := a.exportFilter.ProcessTerms(pfx, p)
if reject {
return nil
}
a.mu.Lock()
defer a.mu.Unlock()
if !a.neighbor.CapAddPathRX {
oldPaths := a.rt.ReplacePath(pfx, p)
a.removePathsFromClients(pfx, oldPaths)
}
pathID, err := a.pathIDManager.getNewID()
if err != nil {
return fmt.Errorf("Unable to get path ID: %v", err)
}
p.BGPPath.PathIdentifier = pathID
a.rt.AddPath(pfx, p)
for _, client := range a.ClientManager.Clients() {
err := client.AddPath(pfx, p)
if err != nil {
log.WithField("Sender", "AdjRIBOutAddPath").WithError(err).Error("Could not send update to client")
}
}
return nil
}
// RemovePath removes the path for prefix `pfx`
func (a *AdjRIBOut) RemovePath(pfx bnet.Prefix, p *route.Path) bool {
if !routingtable.ShouldPropagateUpdate(pfx, p, a.neighbor) {
return false
}
a.mu.Lock()
defer a.mu.Unlock()
r := a.rt.Get(pfx)
if r == nil {
return false
}
a.rt.RemovePath(pfx, p)
a.pathIDManager.releaseID(p.BGPPath.PathIdentifier)
a.removePathFromClients(pfx, p)
return true
}
func (a *AdjRIBOut) isOwnPath(p *route.Path) bool {
if p.Type != a.neighbor.Type {
return false
}
switch p.Type {
case route.BGPPathType:
return p.BGPPath.Source == a.neighbor.Address
}
return false
}
func (a *AdjRIBOut) removePathsFromClients(pfx bnet.Prefix, paths []*route.Path) {
for _, p := range paths {
a.removePathFromClients(pfx, p)
}
}
func (a *AdjRIBOut) removePathFromClients(pfx bnet.Prefix, path *route.Path) {
for _, client := range a.ClientManager.Clients() {
client.RemovePath(pfx, path)
}
}
// Print dumps all prefixes in the Adj-RIB
func (a *AdjRIBOut) Print() string {
a.mu.RLock()
defer a.mu.RUnlock()
ret := fmt.Sprintf("DUMPING ADJ-RIB-OUT:\n")
routes := a.rt.Dump()
for _, r := range routes {
ret += fmt.Sprintf("%s\n", r.Prefix().String())
}
return ret
}