Newer
Older
"github.com/bio-routing/bio-rd/config"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
type PeerInfo struct {
PeerASN uint32
LocalASN uint32
}
type peer struct {
server *bgpServer
peerASN uint32
localASN uint32
// 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
clusterID uint32
ipv4 *familyParameters
ipv6 *familyParameters
}
type familyParameters struct {
importFilter *filter.Filter
exportFilter *filter.Filter
addPathSend routingtable.ClientOptions
addPathRecv bool
func (p *peer) snapshot() PeerInfo {
return PeerInfo{
PeerAddr: p.addr,
PeerASN: p.peerASN,
LocalASN: p.localASN,
}
}
func (p *peer) collisionHandling(callingFSM *FSM) bool {
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
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 config.Peer, server *bgpServer) (*peer, error) {
if c.LocalAS == 0 {
c.LocalAS = server.localASN
}
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
if c.IPv4 != nil {
p.ipv4 = &familyParameters{
rib: c.IPv4.RIB,
importFilter: filterOrDefault(c.IPv4.ImportFilter),
exportFilter: filterOrDefault(c.IPv4.ExportFilter),
addPathRecv: c.IPv4.AddPathRecv,
addPathSend: c.IPv4.AddPathSend,
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.IPv6 != nil {
p.ipv6 = &familyParameters{
rib: c.IPv6.RIB,
importFilter: filterOrDefault(c.IPv6.ImportFilter),
exportFilter: filterOrDefault(c.IPv6.ExportFilter),
addPathRecv: c.IPv6.AddPathRecv,
addPathSend: c.IPv6.AddPathSend,
caps = append(caps, multiProtocolCapability(packet.IPv6AFI))
}
p.optOpenParams = append(p.optOpenParams, packet.OptParam{
Type: packet.CapabilitiesParamType,
Value: caps,
})
p.fsms = append(p.fsms, NewActiveFSM2(p))
func asn4Capability(c config.Peer) 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 config.Peer) []packet.Capability {
caps := make([]packet.Capability, 0)
addPath := uint8(0)
if c.AddPathRecv {
addPath += packet.AddPathReceive
}
if !c.AddPathSend.BestOnly {
addPath += packet.AddPathSend
}
if addPath == 0 {
Code: packet.AddPathCapabilityCode,
Value: packet.AddPathCapability{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
SendReceive: addPath,
},
caps = append(caps, packet.Capability{
Code: packet.AddPathCapabilityCode,
Value: packet.AddPathCapability{
AFI: packet.IPv6AFI,
SAFI: packet.UnicastSAFI,
SendReceive: addPath,
},
})
func filterOrDefault(f *filter.Filter) *filter.Filter {
if f != nil {
return f
}
return filter.NewDrainFilter()
}
// GetAddr returns the IP address of the peer
func (p *peer) GetAddr() bnet.IP {
func (p *peer) Start() {