diff --git a/config/peer.go b/config/peer.go index 9e057b9da60f6a33632a55bf7a3181c4a850ba70..ded26d7bd89f5d3bde535148284c632cfef14284 100644 --- a/config/peer.go +++ b/config/peer.go @@ -3,19 +3,22 @@ package config import ( "net" + "time" + "github.com/bio-routing/bio-rd/routingtable" ) type Peer struct { - AdminEnabled bool - KeepAlive uint16 - HoldTimer uint16 - LocalAddress net.IP - PeerAddress net.IP - LocalAS uint32 - PeerAS uint32 - Passive bool - RouterID uint32 - AddPathSend routingtable.ClientOptions - AddPathRecv bool + AdminEnabled bool + KeepAlive uint16 + HoldTimer uint16 + LocalAddress net.IP + PeerAddress net.IP + LocalAS uint32 + PeerAS uint32 + Passive bool + RouterID uint32 + AddPathSend routingtable.ClientOptions + AddPathRecv bool + ReconnectInterval time.Duration } diff --git a/protocols/bgp/server/peer.go b/protocols/bgp/server/peer.go index bf18c5d5d869c71a33b9e9aa587bbeaaca405f5f..c891b9ddcea9042980c2d71e6b1475f16018b7b0 100644 --- a/protocols/bgp/server/peer.go +++ b/protocols/bgp/server/peer.go @@ -7,28 +7,34 @@ import ( "github.com/bio-routing/bio-rd/routingtable" "github.com/bio-routing/bio-rd/routingtable/locRIB" + "time" + "github.com/bio-routing/bio-rd/config" ) type Peer struct { - addr net.IP - asn uint32 - fsm *FSM - rib *locRIB.LocRIB - routerID uint32 - addPathSend routingtable.ClientOptions - addPathRecv bool - optOpenParams []packet.OptParam + addr net.IP + asn uint32 + fsm *FSM + rib *locRIB.LocRIB + routerID uint32 + addPathSend routingtable.ClientOptions + addPathRecv bool + optOpenParams []packet.OptParam + reconnectInterval time.Duration } +// 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, rib *locRIB.LocRIB) (*Peer, error) { p := &Peer{ - addr: c.PeerAddress, - asn: c.PeerAS, - rib: rib, - addPathSend: c.AddPathSend, - addPathRecv: c.AddPathRecv, - optOpenParams: make([]packet.OptParam, 0), + addr: c.PeerAddress, + asn: c.PeerAS, + rib: rib, + addPathSend: c.AddPathSend, + addPathRecv: c.AddPathRecv, + optOpenParams: make([]packet.OptParam, 0), + reconnectInterval: c.ReconnectInterval, } p.fsm = NewFSM(p, c, rib) @@ -63,19 +69,29 @@ func NewPeer(c config.Peer, rib *locRIB.LocRIB) (*Peer, error) { return p, nil } +// GetAddr returns the IP address of the peer func (p *Peer) GetAddr() net.IP { return p.addr } +// GetASN returns the configured AS number of the peer func (p *Peer) GetASN() uint32 { return p.asn } +// Start the peers fsm. It starts from the Idle state and will get an ManualStart event. To trigger +// reconnects if the fsm falls back into the Idle state, every reconnectInterval a ManualStart event is send. +// The default value for reconnectInterval is 30 seconds. func (p *Peer) Start() { p.fsm.start() + if p.reconnectInterval == 0 { + p.reconnectInterval = 30 * time.Second + } + t := time.Tick(p.reconnectInterval) go func() { for { + <-t p.fsm.activate() } - }() + }() }