Skip to content
Snippets Groups Projects
Unverified Commit dd7ea66a authored by Daniel Czerwonk's avatar Daniel Czerwonk Committed by GitHub
Browse files

Merge branch 'master' into feature/sleep-between-reconnects

parents ba7c623e 6978b867
No related branches found
No related tags found
No related merge requests found
Showing
with 91 additions and 37 deletions
......@@ -15,7 +15,7 @@ import (
)
func main() {
fmt.Printf("This is a BGP speaker\n")
logrus.Printf("This is a BGP speaker\n")
rib := locRIB.New()
b := server.NewBgpServer()
......
......@@ -16,7 +16,6 @@ import (
"github.com/bio-routing/bio-rd/routingtable/adjRIBIn"
"github.com/bio-routing/bio-rd/routingtable/adjRIBOut"
"github.com/bio-routing/bio-rd/routingtable/adjRIBOutAddPath"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
log "github.com/sirupsen/logrus"
tomb "gopkg.in/tomb.v2"
)
......@@ -92,7 +91,7 @@ type FSM struct {
adjRIBIn *adjRIBIn.AdjRIBIn
adjRIBOut routingtable.RouteTableClient
rib *locRIB.LocRIB
rib routingtable.RouteTableClient
updateSender routingtable.RouteTableClient
capAddPathSend bool
......@@ -109,7 +108,7 @@ type msgRecvErr struct {
con *net.TCPConn
}
func NewFSM(peer *Peer, c config.Peer, rib *locRIB.LocRIB) *FSM {
func NewFSM(peer *Peer, c config.Peer, rib routingtable.RouteTableClient) *FSM {
fsm := &FSM{
peer: peer,
state: Idle,
......@@ -647,7 +646,7 @@ func (fsm *FSM) openConfirm() int {
case recvMsg := <-fsm.msgRecvCh:
msg, err := packet.Decode(bytes.NewBuffer(recvMsg.msg))
if err != nil {
fmt.Printf("Failed to decode message: %v\n", recvMsg.msg)
log.WithError(err).Errorf("Failed to decode BGP message %v\n", recvMsg.msg)
switch bgperr := err.(type) {
case packet.BGPError:
sendNotification(fsm.con, bgperr.ErrorCode, bgperr.ErrorSubCode)
......@@ -790,7 +789,8 @@ func (fsm *FSM) established() int {
case recvMsg := <-fsm.msgRecvCh:
msg, err := packet.Decode(bytes.NewBuffer(recvMsg.msg))
if err != nil {
fmt.Printf("Failed to decode BGP message: %v\n", recvMsg.msg)
log.WithError(err).Errorf("Failed to decode BGP message %v\n", recvMsg.msg)
switch bgperr := err.(type) {
case packet.BGPError:
sendNotification(fsm.con, bgperr.ErrorCode, bgperr.ErrorSubCode)
......@@ -815,14 +815,13 @@ func (fsm *FSM) established() int {
for r := u.WithdrawnRoutes; r != nil; r = r.Next {
pfx := tnet.NewPfx(r.IP, r.Pfxlen)
fmt.Printf("LPM: Removing prefix %s\n", pfx.String())
log.WithField("Prefix", pfx.String()).Debug("LPM: Removing prefix")
fsm.adjRIBIn.RemovePath(pfx, nil)
}
for r := u.NLRI; r != nil; r = r.Next {
pfx := tnet.NewPfx(r.IP, r.Pfxlen)
fmt.Printf("LPM: Adding prefix %s\n", pfx.String())
log.WithField("Prefix", pfx.String()).Debug("LPM: Adding prefix")
path := &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
......@@ -831,7 +830,6 @@ func (fsm *FSM) established() int {
}
for pa := u.PathAttributes; pa != nil; pa = pa.Next {
fmt.Printf("TypeCode: %d\n", pa.TypeCode)
switch pa.TypeCode {
case packet.OriginAttr:
path.BGPPath.Origin = pa.Value.(uint8)
......@@ -840,7 +838,7 @@ func (fsm *FSM) established() int {
case packet.MEDAttr:
path.BGPPath.MED = pa.Value.(uint32)
case packet.NextHopAttr:
fmt.Printf("RECEIVED NEXT_HOP: %d\n", pa.Value.(uint32))
log.WithField("NextHop", pa.Value.(uint32)).Debug("RECEIVED NEXT_HOP")
path.BGPPath.NextHop = pa.Value.(uint32)
case packet.ASPathAttr:
path.BGPPath.ASPath = pa.ASPathString()
......
......@@ -3,9 +3,9 @@ package server
import (
"net"
"github.com/bio-routing/bio-rd/config"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
"time"
......@@ -16,7 +16,7 @@ type Peer struct {
addr net.IP
asn uint32
fsm *FSM
rib *locRIB.LocRIB
rib routingtable.RouteTableClient
routerID uint32
addPathSend routingtable.ClientOptions
addPathRecv bool
......@@ -26,7 +26,7 @@ type Peer struct {
// 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) {
func NewPeer(c config.Peer, rib routingtable.RouteTableClient) (*Peer, error) {
p := &Peer{
addr: c.PeerAddress,
asn: c.PeerAS,
......
......@@ -8,7 +8,7 @@ import (
"github.com/bio-routing/bio-rd/config"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
"github.com/bio-routing/bio-rd/routingtable"
log "github.com/sirupsen/logrus"
)
......@@ -39,7 +39,7 @@ func (b *BGPServer) Start(c *config.Global) error {
return fmt.Errorf("Failed to load defaults: %v", err)
}
fmt.Printf("ROUTER ID: %d\n", c.RouterID)
log.Infof("ROUTER ID: %d\n", c.RouterID)
b.routerID = c.RouterID
if c.Listen {
......@@ -62,8 +62,6 @@ func (b *BGPServer) Start(c *config.Global) error {
func (b *BGPServer) incomingConnectionWorker() {
for {
c := <-b.acceptCh
fmt.Printf("Incoming connection!\n")
fmt.Printf("Connection from: %v\n", c.RemoteAddr())
peerAddr := strings.Split(c.RemoteAddr().String(), ":")[0]
if _, ok := b.peers[peerAddr]; !ok {
......@@ -78,13 +76,13 @@ func (b *BGPServer) incomingConnectionWorker() {
"source": c.RemoteAddr(),
}).Info("Incoming TCP connection")
fmt.Printf("DEBUG: Sending incoming TCP connection to fsm for peer %s\n", peerAddr)
log.WithField("Peer", peerAddr).Debug("Sending incoming TCP connection to fsm for peer")
b.peers[peerAddr].fsm.conCh <- c
fmt.Printf("DEBUG: Sending done\n")
log.Debug("Sending done")
}
}
func (b *BGPServer) AddPeer(c config.Peer, rib *locRIB.LocRIB) error {
func (b *BGPServer) AddPeer(c config.Peer, rib routingtable.RouteTableClient) error {
if c.LocalAS > uint16max || c.PeerAS > uint16max {
return fmt.Errorf("32bit ASNs are not supported yet")
}
......
......@@ -15,18 +15,20 @@ import (
// UpdateSender converts table changes into BGP update messages
type UpdateSender struct {
routingtable.ClientManager
fsm *FSM
fsm *FSM
iBGP bool
}
func newUpdateSender(fsm *FSM) *UpdateSender {
return &UpdateSender{
fsm: fsm,
fsm: fsm,
iBGP: fsm.localASN == fsm.remoteASN,
}
}
// AddPath serializes a new path and sends out a BGP update message
func (u *UpdateSender) AddPath(pfx net.Prefix, p *route.Path) error {
asPathPA, err := packet.ParseASPathStr(strings.TrimRight(fmt.Sprintf("%d %s", u.fsm.localASN, p.BGPPath.ASPath), " "))
asPathPA, err := packet.ParseASPathStr(asPathString(u.iBGP, u.fsm.localASN, p.BGPPath.ASPath))
if err != nil {
return fmt.Errorf("Unable to parse AS path: %v", err)
}
......@@ -41,6 +43,10 @@ func (u *UpdateSender) AddPath(pfx net.Prefix, p *route.Path) error {
Next: &packet.PathAttribute{
TypeCode: packet.NextHopAttr,
Value: p.BGPPath.NextHop,
Next: &packet.PathAttribute{
TypeCode: packet.LocalPrefAttr,
Value: p.BGPPath.LocalPref,
},
},
},
},
......@@ -74,3 +80,12 @@ func (u *UpdateSender) UpdateNewClient(client routingtable.RouteTableClient) err
log.Warningf("BGP Update Sender: UpdateNewClient() not supported")
return nil
}
func asPathString(iBGP bool, localASN uint16, asPath string) string {
ret := ""
if iBGP {
ret = ret + fmt.Sprintf("%d ", localASN)
}
ret = ret + asPath
return strings.TrimRight(ret, " ")
}
......@@ -2,7 +2,6 @@ package server
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
......@@ -15,18 +14,20 @@ import (
// UpdateSenderAddPath converts table changes into BGP update messages with add path
type UpdateSenderAddPath struct {
routingtable.ClientManager
fsm *FSM
fsm *FSM
iBGP bool
}
func newUpdateSenderAddPath(fsm *FSM) *UpdateSenderAddPath {
return &UpdateSenderAddPath{
fsm: fsm,
fsm: fsm,
iBGP: fsm.localASN == fsm.remoteASN,
}
}
// AddPath serializes a new path and sends out a BGP update message
func (u *UpdateSenderAddPath) AddPath(pfx net.Prefix, p *route.Path) error {
asPathPA, err := packet.ParseASPathStr(strings.TrimRight(fmt.Sprintf("%d %s", u.fsm.localASN, p.BGPPath.ASPath), " "))
asPathPA, err := packet.ParseASPathStr(asPathString(u.iBGP, u.fsm.localASN, p.BGPPath.ASPath))
if err != nil {
return fmt.Errorf("Unable to parse AS path: %v", err)
}
......@@ -41,6 +42,10 @@ func (u *UpdateSenderAddPath) AddPath(pfx net.Prefix, p *route.Path) error {
Next: &packet.PathAttribute{
TypeCode: packet.NextHopAttr,
Value: p.BGPPath.NextHop,
Next: &packet.PathAttribute{
TypeCode: packet.LocalPrefAttr,
Value: p.BGPPath.LocalPref,
},
},
},
},
......
......@@ -6,6 +6,7 @@ import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
log "github.com/sirupsen/logrus"
)
// AdjRIBIn represents an Adjacency RIB In as described in RFC4271
......@@ -33,7 +34,11 @@ func (a *AdjRIBIn) UpdateNewClient(client routingtable.RouteTableClient) error {
for _, route := range routes {
paths := route.Paths()
for _, path := range paths {
client.AddPath(route.Prefix(), path)
err := client.AddPath(route.Prefix(), path)
if err != nil {
log.WithField("Sender", "AdjRIBOutAddPath").WithError(err).Error("Could not send update to client")
}
}
}
return nil
......
......@@ -34,6 +34,10 @@ func (m *RTMockClient) Register(routingtable.RouteTableClient) {
return
}
func (m *RTMockClient) RegisterWithOptions(routingtable.RouteTableClient, routingtable.ClientOptions) {
return
}
func (m *RTMockClient) Unregister(routingtable.RouteTableClient) {
return
}
......
......@@ -7,6 +7,7 @@ import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
log "github.com/sirupsen/logrus"
)
// AdjRIBOut represents an Adjacency RIB In as described in RFC4271
......@@ -45,7 +46,10 @@ func (a *AdjRIBOut) AddPath(pfx net.Prefix, p *route.Path) error {
a.removePathsFromClients(pfx, oldPaths)
for _, client := range a.ClientManager.Clients() {
client.AddPath(pfx, p)
err := client.AddPath(pfx, p)
if err != nil {
log.WithField("Sender", "AdjRIBOut").WithError(err).Error("Could not send update to client")
}
}
return nil
}
......
......@@ -7,6 +7,7 @@ import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
log "github.com/sirupsen/logrus"
)
// AdjRIBOutAddPath represents an Adjacency RIB Out with BGP add path
......@@ -52,7 +53,10 @@ func (a *AdjRIBOutAddPath) AddPath(pfx net.Prefix, p *route.Path) error {
a.rt.AddPath(pfx, p)
for _, client := range a.ClientManager.Clients() {
client.AddPath(pfx, p)
err := client.AddPath(pfx, p)
if err != nil {
log.WithField("Sender", "AdjRIBOutAddPath").WithError(err).Error("Could not send update to client")
}
}
return nil
}
......
......@@ -11,5 +11,6 @@ type RouteTableClient interface {
RemovePath(net.Prefix, *route.Path) bool
UpdateNewClient(RouteTableClient) error
Register(RouteTableClient)
RegisterWithOptions(RouteTableClient, ClientOptions)
Unregister(RouteTableClient)
}
......@@ -55,9 +55,8 @@ func (c *ClientManager) Register(client RouteTableClient) {
// RegisterWithOptions registers a client with options for updates
func (c *ClientManager) RegisterWithOptions(client RouteTableClient, opt ClientOptions) {
c.mu.Lock()
defer c.mu.Unlock()
c.clients[client] = opt
c.mu.Unlock()
c.master.UpdateNewClient(client)
}
......
......@@ -25,6 +25,11 @@ func (m MockClient) UpdateNewClient(RouteTableClient) error {
func (m MockClient) Register(RouteTableClient) {
return
}
func (m MockClient) RegisterWithOptions(RouteTableClient, ClientOptions) {
return
}
func (m MockClient) Unregister(RouteTableClient) {
return
}
......
......@@ -10,6 +10,13 @@ type TermCondition struct {
routeFilters []*RouteFilter
}
func NewTermCondition(prefixLists []*PrefixList, routeFilters []*RouteFilter) *TermCondition {
return &TermCondition{
prefixLists: prefixLists,
routeFilters: routeFilters,
}
}
func (f *TermCondition) Matches(p net.Prefix, pa *route.Path) bool {
return f.matchesAnyPrefixList(p) || f.machtchesAnyRouteFilter(p)
}
......
......@@ -109,10 +109,10 @@ func TestMatches(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
f := &TermCondition{
prefixLists: test.prefixLists,
routeFilters: test.routeFilters,
}
f := NewTermCondition(
test.prefixLists,
test.routeFilters,
)
assert.Equal(te, test.expected, f.Matches(test.prefix, &route.Path{}))
})
......
......@@ -8,6 +8,7 @@ import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/sirupsen/logrus"
)
// LocRIB represents a routing information base
......@@ -44,6 +45,10 @@ func (a *LocRIB) AddPath(pfx net.Prefix, p *route.Path) error {
a.mu.Lock()
defer a.mu.Unlock()
logrus.WithFields(map[string]interface{}{
"Prefix": pfx,
"Route": p,
}).Debug("AddPath to locRIB")
routeExisted := false
oldRoute := &route.Route{}
r := a.rt.Get(pfx)
......@@ -70,6 +75,10 @@ func (a *LocRIB) RemovePath(pfx net.Prefix, p *route.Path) bool {
a.mu.Lock()
defer a.mu.Unlock()
logrus.WithFields(map[string]interface{}{
"Prefix": pfx,
"Route": p,
}).Debug("Remove from locRIB")
var oldRoute *route.Route
r := a.rt.Get(pfx)
if r != nil {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment