Newer
Older
"github.com/bio-routing/bio-rd/routingtable/vrf"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
type peer struct {
// guarded by fsmsMu
fsms []*FSM
fsmsMu sync.Mutex
routerID uint32
reconnectInterval time.Duration
keepaliveTime time.Duration
holdTime time.Duration
optOpenParams []packet.OptParam
routeServerClient bool
routeReflectorClient bool
ipv4MultiProtocolAdvertised bool
clusterID uint32
ipv4 *peerAddressFamily
ipv6 *peerAddressFamily
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
// PeerConfig defines the configuration for a BGP session
type PeerConfig struct {
AdminEnabled bool
ReconnectInterval time.Duration
KeepAlive time.Duration
HoldTime time.Duration
LocalAddress bnet.IP
PeerAddress bnet.IP
LocalAS uint32
PeerAS uint32
Passive bool
RouterID uint32
RouteServerClient bool
RouteReflectorClient bool
RouteReflectorClusterID uint32
AdvertiseIPv4MultiProtocol bool
IPv4 *AddressFamilyConfig
IPv6 *AddressFamilyConfig
VRF *vrf.VRF
}
// AddressFamilyConfig represents all configuration parameters specific for an address family
type AddressFamilyConfig struct {
ImportFilterChain filter.Chain
ExportFilterChain filter.Chain
AddPathSend routingtable.ClientOptions
AddPathRecv bool
}
func (pc *PeerConfig) NeedsRestart(x *PeerConfig) bool {
if pc.LocalAS != x.LocalAS {
return true
}
if pc.PeerAS != x.PeerAS {
return true
}
if pc.LocalAddress != x.LocalAddress {
return true
}
if pc.HoldTime != x.HoldTime {
return true
}
if pc.RouteReflectorClient != x.RouteReflectorClient {
return true
}
if pc.RouteServerClient != x.RouteServerClient {
return true
}
if pc.VRF != x.VRF {
return true
}
if pc.RouterID != x.RouterID {
return true
}
if pc.Passive != x.Passive {
return true
}
return false
}
// replaceImportFilterChain replaces a peers import filter chain
func (p *peer) replaceImportFilterChain(c filter.Chain) {
p.fsmsMu.Lock()
defer p.fsmsMu.Unlock()
for _, fsm := range p.fsms {
fsm.replaceImportFilterChain(c)
}
}
// replaceExportFilterChain replaces a peers import filter chain
func (p *peer) replaceExportFilterChain(c filter.Chain) {
p.fsmsMu.Lock()
defer p.fsmsMu.Unlock()
for _, fsm := range p.fsms {
fsm.replaceExportFilterChain(c)
}
}
type peerAddressFamily struct {
importFilterChain filter.Chain
exportFilterChain filter.Chain
addPathSend routingtable.ClientOptions
addPathReceive bool
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
func (p *peer) dumpRIBIn(afi uint16, safi uint8) []*route.Route {
if len(p.fsms) != 1 {
return nil
}
fsm := p.fsms[0]
f := fsm.addressFamily(afi, safi)
if f == nil {
return nil
}
return f.dumpRIBIn()
}
func (p *peer) dumpRIBOut(afi uint16, safi uint8) []*route.Route {
if len(p.fsms) != 1 {
return nil
}
fsm := p.fsms[0]
f := fsm.addressFamily(afi, safi)
if f == nil {
return nil
}
return f.dumpRIBOut()
}
func (p *peer) addressFamily(afi uint16, safi uint8) *peerAddressFamily {
if safi != packet.UnicastSAFI {
return nil
}
switch afi {
case packet.IPv4AFI:
return p.ipv4
case packet.IPv6AFI:
return p.ipv6
default:
return nil
}
}
func (p *peer) collisionHandling(callingFSM *FSM) bool {
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
p.fsmsMu.Lock()
defer p.fsmsMu.Unlock()
for _, fsm := range p.fsms {
if callingFSM == fsm {
continue
}
fsm.stateMu.RLock()
isEstablished := isEstablishedState(fsm.state)
isOpenConfirm := isOpenConfirmState(fsm.state)
fsm.stateMu.RUnlock()
if isEstablished {
return true
}
if !isOpenConfirm {
continue
}
if p.routerID < callingFSM.neighborID {
fsm.cease()
} else {
return true
}
}
return false
}
func isOpenConfirmState(s state) bool {
switch s.(type) {
case openConfirmState:
return true
}
return false
}
func isEstablishedState(s state) bool {
switch s.(type) {
case establishedState:
return true
}
return false
// NewPeer creates a new peer with the given config. If an connection is established, the adjRIBIN of the peer is connected
// to the given rib. To actually connect the peer, call Start() on the returned peer.
func newPeer(c PeerConfig, server *bgpServer) (*peer, error) {
server: server,
addr: c.PeerAddress,
peerASN: c.PeerAS,
localASN: c.LocalAS,
fsms: make([]*FSM, 0),
reconnectInterval: c.ReconnectInterval,
keepaliveTime: c.KeepAlive,
holdTime: c.HoldTime,
optOpenParams: make([]packet.OptParam, 0),
routeServerClient: c.RouteServerClient,
routeReflectorClient: c.RouteReflectorClient,
clusterID: c.RouteReflectorClusterID,
Maximilian Wilhelm
committed
rib: c.VRF.IPv4UnicastRIB(),
importFilterChain: filterOrDefault(c.IPv4.ImportFilterChain),
exportFilterChain: filterOrDefault(c.IPv4.ExportFilterChain),
addPathReceive: c.IPv4.AddPathRecv,
addPathSend: c.IPv4.AddPathSend,
if p.ipv4.rib == nil {
return nil, fmt.Errorf("No RIB for IPv4 unicast configured")
}
Maximilian Wilhelm
committed
// If we are a route reflector and no ClusterID was set, use our RouterID
if p.routeReflectorClient && p.clusterID == 0 {
p.clusterID = c.RouterID
}
caps = append(caps, addPathCapabilities(c)...)
if c.IPv4 != nil && c.AdvertiseIPv4MultiProtocol {
caps = append(caps, multiProtocolCapability(packet.IPv4AFI))
p.ipv4MultiProtocolAdvertised = true
}
rib: c.VRF.IPv6UnicastRIB(),
importFilterChain: filterOrDefault(c.IPv6.ImportFilterChain),
exportFilterChain: filterOrDefault(c.IPv6.ExportFilterChain),
addPathReceive: c.IPv6.AddPathRecv,
addPathSend: c.IPv6.AddPathSend,
caps = append(caps, multiProtocolCapability(packet.IPv6AFI))
if p.ipv6.rib == nil {
return nil, fmt.Errorf("No RIB for IPv6 unicast configured")
}
p.optOpenParams = append(p.optOpenParams, packet.OptParam{
Type: packet.CapabilitiesParamType,
Value: caps,
})
if !p.passive {
p.fsms = append(p.fsms, NewActiveFSM(p))
}
func asn4Capability(c PeerConfig) packet.Capability {
return packet.Capability{
Code: packet.ASN4CapabilityCode,
Value: packet.ASN4Capability{
ASN4: c.LocalAS,
},
}
}
func multiProtocolCapability(afi uint16) packet.Capability {
return packet.Capability{
Code: packet.MultiProtocolCapabilityCode,
Value: packet.MultiProtocolCapability{
AFI: afi,
SAFI: packet.UnicastSAFI,
},
}
}
func addPathCapabilities(c PeerConfig) []packet.Capability {
enabled, cap := addPathCapabilityForFamily(c.IPv4, packet.IPv4AFI, packet.UnicastSAFI)
if enabled {
caps = append(caps, cap)
}
enabled, cap = addPathCapabilityForFamily(c.IPv6, packet.IPv6AFI, packet.UnicastSAFI)
if enabled {
caps = append(caps, cap)
}
return caps
}
func addPathCapabilityForFamily(f *AddressFamilyConfig, afi uint16, safi uint8) (enabled bool, cap packet.Capability) {
if f == nil {
return false, packet.Capability{}
}
addPath += packet.AddPathSend
}
if addPath == 0 {
Code: packet.AddPathCapabilityCode,
Value: packet.AddPathCapability{
SendReceive: addPath,
},
}
}
func filterOrDefault(c filter.Chain) filter.Chain {
if len(c) != 0 {
return c
return filter.NewDrainFilterChain()
// GetAddr returns the IP address of the peer
func (p *peer) GetAddr() bnet.IP {
func (p *peer) Start() {