diff --git a/bird/bird.go b/bird/bird.go index e8f23f3..8526050 100644 --- a/bird/bird.go +++ b/bird/bird.go @@ -242,3 +242,61 @@ func RoutesLookupProtocol(net string, protocol string) (Parsed, bool) { func RoutesPeer(peer string) (Parsed, bool) { return RunAndParse("route export '"+peer+"'", parseRoutes) } + +func RoutesDump() (Parsed, bool) { + if ParserConf.PerPeerTables { + return RoutesDumpPerPeerTable() + } + return RoutesDumpSingleTable() +} + +func RoutesDumpSingleTable() (Parsed, bool) { + exportedRes, cached := RunAndParse("route all", parseRoutes) + filteredRes, _ := RunAndParse("route filtered all", parseRoutes) + + exported := exportedRes["routes"] + filtered := filteredRes["routes"] + + result := Parsed{ + "exported": exported, + "filtered": filtered, + } + return result, cached +} + +func RoutesDumpPerPeerTable() (Parsed, bool) { + exportedRes, cached := RunAndParse("route all", parseRoutes) + exported := exportedRes["routes"] + filtered := []Parsed{} + + // Get protocols with filtered routes + protocols, _ := ProtocolsBgp() + for protocol, details := range protocols { + details, ok := details.(Parsed) + counters, ok := details["routes"].(map[string]int) + if !ok { + continue + } + filterCount := counters["filtered"] + if filterCount == 0 { + continue // nothing to do here. + } + // Lookup filtered routes + pfilteredRes, _ := RunAndParse( + "route filtered protocol '"+protocol+"' all", + parseRoutes) + + pfiltered, ok := pfilteredRes["routes"].([]Parsed) + if !ok { + continue // something went wrong... + } + + filtered = append(filtered, pfiltered...) + } + + result := Parsed{ + "exported": exported, + "filtered": filtered, + } + return result, cached +} diff --git a/birdwatcher.go b/birdwatcher.go index c5a6808..917647a 100644 --- a/birdwatcher.go +++ b/birdwatcher.go @@ -75,6 +75,9 @@ func makeRouter(config endpoints.ServerConfig) *httprouter.Router { if isModuleEnabled("routes_peer", whitelist) { r.GET("/routes/peer", endpoints.Endpoint(endpoints.RoutesPeer)) } + if isModuleEnabled("routes_dump", whitelist) { + r.GET("/routes/dump", endpoints.Endpoint(endpoints.RoutesDump)) + } return r } diff --git a/endpoints/routes.go b/endpoints/routes.go index 5039626..1a17ffa 100644 --- a/endpoints/routes.go +++ b/endpoints/routes.go @@ -83,3 +83,7 @@ func RoutesPeer(r *http.Request, ps httprouter.Params) (bird.Parsed, bool) { } return bird.RoutesPeer(peer) } + +func RoutesDump(r *http.Request, ps httprouter.Params) (bird.Parsed, bool) { + return bird.RoutesDump() +}