diff --git a/protocols/bgp/server/fsm_established.go b/protocols/bgp/server/fsm_established.go index 8462b2c5db90a073c8dcc1119b35c63600342df7..2f73297cd299837c9c5425c546bf03d751b50e16 100644 --- a/protocols/bgp/server/fsm_established.go +++ b/protocols/bgp/server/fsm_established.go @@ -154,10 +154,8 @@ func (s *establishedState) keepaliveTimerExpired() (state, string) { } func (s *establishedState) msgReceived(data []byte) (state, string) { - fmt.Printf("Processing MSG\n") msg, err := packet.Decode(bytes.NewBuffer(data)) if err != nil { - fmt.Printf("Decode failure: %v\n", err) switch bgperr := err.(type) { case packet.BGPError: s.fsm.sendNotification(bgperr.ErrorCode, bgperr.ErrorSubCode) @@ -167,7 +165,6 @@ func (s *establishedState) msgReceived(data []byte) (state, string) { s.fsm.connectRetryCounter++ return newIdleState(s.fsm), "Failed to decode BGP message" } - fmt.Printf("Msg type: %d\n", msg.Header.Type) switch msg.Header.Type { case packet.NotificationMsg: return s.notification() @@ -194,12 +191,9 @@ func (s *establishedState) update(msg *packet.BGPMessage) (state, string) { } u := msg.Body.(*packet.BGPUpdate) - fmt.Printf("Processing withdraws\n") s.withdraws(u) - fmt.Printf("Processing advertisements\n") s.updates(u) - fmt.Printf("update done\n") return newEstablishedState(s.fsm), s.fsm.reason } @@ -240,6 +234,7 @@ func (s *establishedState) updates(u *packet.BGPUpdate) { path.BGPPath.LargeCommunities = pa.LargeCommunityString() } } + fmt.Printf("Adding path for pfx: %s\n", pfx.String()) s.fsm.adjRIBIn.AddPath(pfx, path) } } diff --git a/protocols/bgp/server/fsm_test.go b/protocols/bgp/server/fsm_test.go index eeab5a60e8b25b86bd135254e5d67b08ada7d7b5..7b4579bed6bc771063831a277f48f4dd9261b9ea 100644 --- a/protocols/bgp/server/fsm_test.go +++ b/protocols/bgp/server/fsm_test.go @@ -34,7 +34,6 @@ func TestFSM(t *testing.T) { nextState, reason := fsmA.state.run() fsmA.state = nextState stateName := stateName(nextState) - fmt.Printf("New state: %s\n", stateName) switch stateName { case "idle": wg.Done() @@ -55,7 +54,7 @@ func TestFSM(t *testing.T) { }() for i := uint8(0); i < 255; i++ { - fsmA.msgRecvCh <- []byte{ + update := []byte{ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 53, 2, @@ -84,12 +83,39 @@ func TestFSM(t *testing.T) { 10, 11, 12, 13, // Next Hop 24, 169, 254, i, } + + fsmA.msgRecvCh <- update + } + + ribRouteCount := fsmA.rib.RouteCount() + if ribRouteCount != 255 { + t.Errorf("Unexpected route count in LocRIB: %d", ribRouteCount) + } + + fmt.Printf("Route count in RIB: %d\n", fsmA.rib.RouteCount()) + + for i := uint8(0); i < 255; i++ { + update := []byte{ + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 0, 27, + 2, + 0, 4, + 24, 169, 254, i, + 0, 0, + } + fsmA.msgRecvCh <- update + } + time.Sleep(time.Second) + + ribRouteCount = fsmA.rib.RouteCount() + if ribRouteCount != 0 { + t.Errorf("Unexpected route count in LocRIB: %d", ribRouteCount) } fmt.Printf("Route count in RIB: %d\n", fsmA.rib.RouteCount()) fmt.Printf("Stopping FSM\n") fsmA.eventCh <- ManualStop - fmt.Printf("WAINTING\n") + fmt.Printf("WAITING\n") wg.Wait() } diff --git a/protocols/bgp/server/update_sender.go b/protocols/bgp/server/update_sender.go index 67fb6662febaec64942dd48c6164ad00b0441bc4..b389ae117076e0df9e4755ac173a95f02173b28f 100644 --- a/protocols/bgp/server/update_sender.go +++ b/protocols/bgp/server/update_sender.go @@ -57,6 +57,12 @@ func (u *UpdateSender) UpdateNewClient(client routingtable.RouteTableClient) err return nil } +// RouteCount does nothing +func (u *UpdateSender) RouteCount() int64 { + log.Warningf("BGP Update Sender: RouteCount() not supported") + return 0 +} + func asPathString(iBGP bool, localASN uint16, asPath string) string { ret := "" if iBGP { diff --git a/protocols/bgp/server/update_sender_add_path.go b/protocols/bgp/server/update_sender_add_path.go index 7abef161e4f5accd6b49b423849fbbd3d4a56742..2414e14d8fe879c68d6b6e249f6d74b8f1e4e3aa 100644 --- a/protocols/bgp/server/update_sender_add_path.go +++ b/protocols/bgp/server/update_sender_add_path.go @@ -53,3 +53,9 @@ func (u *UpdateSenderAddPath) UpdateNewClient(client routingtable.RouteTableClie log.Warningf("BGP Update Sender: UpdateNewClient not implemented") return nil } + +// RouteCount returns the number of stored routes +func (u *UpdateSenderAddPath) RouteCount() int64 { + log.Warningf("BGP Update Sender: RouteCount not implemented") + return 0 +} diff --git a/routingtable/adjRIBIn/adj_rib_in.go b/routingtable/adjRIBIn/adj_rib_in.go index ca9d0bb07570811beec3ffa3eb99ec12423f6041..ac58e1399b3d5321f151153f5d839ce4e1667174 100644 --- a/routingtable/adjRIBIn/adj_rib_in.go +++ b/routingtable/adjRIBIn/adj_rib_in.go @@ -52,6 +52,11 @@ func (a *AdjRIBIn) UpdateNewClient(client routingtable.RouteTableClient) error { return nil } +// RouteCount returns the number of stored routes +func (a *AdjRIBIn) RouteCount() int64 { + return a.rt.GetRouteCount() +} + // AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added. func (a *AdjRIBIn) AddPath(pfx net.Prefix, p *route.Path) error { a.mu.Lock() diff --git a/routingtable/adjRIBOut/adj_rib_out.go b/routingtable/adjRIBOut/adj_rib_out.go index 19b44b8bd2595cc550a960ace7c9c00f1fdab21a..1c1e8cef6d3def34e53b8fd2a5cc86c08c8f72b9 100644 --- a/routingtable/adjRIBOut/adj_rib_out.go +++ b/routingtable/adjRIBOut/adj_rib_out.go @@ -39,6 +39,11 @@ func (a *AdjRIBOut) UpdateNewClient(client routingtable.RouteTableClient) error return nil } +// RouteCount returns the number of stored routes +func (a *AdjRIBOut) RouteCount() int64 { + return a.rt.GetRouteCount() +} + // 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) { diff --git a/routingtable/locRIB/loc_rib.go b/routingtable/locRIB/loc_rib.go index bc73817555e146681e347d546099e8972d87d58d..e8788c5eb3b3c2b1e3e7397d52a4ed6f04feb21b 100644 --- a/routingtable/locRIB/loc_rib.go +++ b/routingtable/locRIB/loc_rib.go @@ -48,8 +48,9 @@ func (a *LocRIB) UpdateNewClient(client routingtable.RouteTableClient) error { return nil } +// RouteCount returns the number of stored routes func (a *LocRIB) RouteCount() int64 { - return a.rt. + return a.rt.GetRouteCount() } // AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added. diff --git a/routingtable/table.go b/routingtable/table.go index 9ccb10161cde2c6f1424c0fbce851a0d1bda19dc..16317e6fc50b7481c8e259be161b8a9bcba644a3 100644 --- a/routingtable/table.go +++ b/routingtable/table.go @@ -40,7 +40,11 @@ func (rt *RoutingTable) addPath(pfx net.Prefix, p *route.Path) error { return nil } - rt.root = rt.root.addPath(pfx, p) + root, isNew := rt.root.addPath(pfx, p) + rt.root = root + if isNew { + atomic.AddInt64(&rt.routeCount, 1) + } return nil } diff --git a/routingtable/trie.go b/routingtable/trie.go index 6fe5f4853cd4bd795c21926f6835ca5e711eeced..a1be18251c82b019d3967cb239135ca24ac631fa 100644 --- a/routingtable/trie.go +++ b/routingtable/trie.go @@ -118,21 +118,21 @@ func (n *node) get(pfx net.Prefix) *node { return n.h.get(pfx) } -func (n *node) addPath(pfx net.Prefix, p *route.Path) *node { +func (n *node) addPath(pfx net.Prefix, p *route.Path) (*node, bool) { currentPfx := n.route.Prefix() if currentPfx == pfx { n.route.AddPath(p) n.dummy = false - return n + return n, true } // is pfx NOT a subnet of this node? if !currentPfx.Contains(pfx) { if pfx.Contains(currentPfx) { - return n.insertBefore(pfx, p, n.route.Pfxlen()-n.skip-1) + return n.insertBefore(pfx, p, n.route.Pfxlen()-n.skip-1), true } - return n.newSuperNode(pfx, p) + return n.newSuperNode(pfx, p), true } // pfx is a subnet of this node @@ -143,22 +143,24 @@ func (n *node) addPath(pfx net.Prefix, p *route.Path) *node { return n.insertHigh(pfx, p, n.route.Pfxlen()) } -func (n *node) insertLow(pfx net.Prefix, p *route.Path, parentPfxLen uint8) *node { +func (n *node) insertLow(pfx net.Prefix, p *route.Path, parentPfxLen uint8) (*node, bool) { if n.l == nil { n.l = newNode(pfx, p, pfx.Pfxlen()-parentPfxLen-1, false) - return n + return n, true } - n.l = n.l.addPath(pfx, p) - return n + newRoot, isNew := n.l.addPath(pfx, p) + n.l = newRoot + return n, isNew } -func (n *node) insertHigh(pfx net.Prefix, p *route.Path, parentPfxLen uint8) *node { +func (n *node) insertHigh(pfx net.Prefix, p *route.Path, parentPfxLen uint8) (*node, bool) { if n.h == nil { n.h = newNode(pfx, p, pfx.Pfxlen()-parentPfxLen-1, false) - return n + return n, true } - n.h = n.h.addPath(pfx, p) - return n + newRoot, isNew := n.h.addPath(pfx, p) + n.h = newRoot + return n, isNew } func (n *node) newSuperNode(pfx net.Prefix, p *route.Path) *node {