Skip to content

Commit

Permalink
Merge branch 'jof/hidden-neighbors' into jof/sfmix
Browse files Browse the repository at this point in the history
  • Loading branch information
jof committed Feb 19, 2024
2 parents 7f66af0 + 82602a5 commit ca24c09
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 33 deletions.
2 changes: 2 additions & 0 deletions etc/alice-lg/alice.example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ name = rs1.example.com (IPv4)
# Optional: a group for the routeservers list
group = FRA
blackholes = 10.23.6.666, 10.23.6.665
# Optional: hidden_neighbors can list strings of IPs, CIDRs, or Regexes to filter from being shown
hidden_neighbors = 10.0.0.0/8, 192.168.0.0/16, AS64496_.*

[source.rs0-example-v4.birdwatcher]
api = http://rs1.example.com:29184/
Expand Down
34 changes: 21 additions & 13 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ type SourceConfig struct {
// Blackhole IPs
Blackholes []string

HiddenNeighbors []string

// Source configurations
Type string
Backend string
Expand Down Expand Up @@ -748,15 +750,17 @@ func getSources(config *ini.File) ([]*SourceConfig, error) {
sourceGroup := section.Key("group").MustString("")
sourceBlackholes := decoders.TrimmedCSVStringList(
section.Key("blackholes").MustString(""))
sourceHiddenNeighbors := decoders.TrimmedCSVStringList(section.Key("hidden_neighbors").MustString(""))

srcCfg := &SourceConfig{
ID: sourceID,
Order: order,
Name: sourceName,
Group: sourceGroup,
Blackholes: sourceBlackholes,
Backend: backendType,
Type: sourceType,
ID: sourceID,
Order: order,
Name: sourceName,
Group: sourceGroup,
Blackholes: sourceBlackholes,
HiddenNeighbors: sourceHiddenNeighbors,
Backend: backendType,
Type: sourceType,
}

// Register route server ID with pool
Expand All @@ -776,8 +780,9 @@ func getSources(config *ini.File) ([]*SourceConfig, error) {
}

c := birdwatcher.Config{
ID: srcCfg.ID,
Name: srcCfg.Name,
ID: srcCfg.ID,
Name: srcCfg.Name,
HiddenNeighbors: srcCfg.HiddenNeighbors,

Timezone: "UTC",
ServerTime: "2006-01-02T15:04:05.999999999Z07:00",
Expand Down Expand Up @@ -808,8 +813,9 @@ func getSources(config *ini.File) ([]*SourceConfig, error) {

case SourceBackendGoBGP:
c := gobgp.Config{
ID: srcCfg.ID,
Name: srcCfg.Name,
ID: srcCfg.ID,
Name: srcCfg.Name,
HiddenNeighbors: srcCfg.HiddenNeighbors,
}

if err := backendConfig.MapTo(&c); err != nil {
Expand All @@ -836,6 +842,7 @@ func getSources(config *ini.File) ([]*SourceConfig, error) {
c := openbgpd.Config{
ID: srcCfg.ID,
Name: srcCfg.Name,
HiddenNeighbors: srcCfg.HiddenNeighbors,
CacheTTL: cacheTTL,
RoutesCacheSize: routesCacheSize,
RejectCommunities: rejectComms,
Expand All @@ -858,6 +865,7 @@ func getSources(config *ini.File) ([]*SourceConfig, error) {
c := openbgpd.Config{
ID: srcCfg.ID,
Name: srcCfg.Name,
HiddenNeighbors: srcCfg.HiddenNeighbors,
CacheTTL: cacheTTL,
RoutesCacheSize: routesCacheSize,
RejectCommunities: rejectComms,
Expand Down Expand Up @@ -1007,9 +1015,9 @@ func (cfg *SourceConfig) GetInstance() sources.Source {
var instance sources.Source
switch cfg.Backend {
case SourceBackendBirdwatcher:
instance = birdwatcher.NewBirdwatcher(cfg.Birdwatcher)
instance = birdwatcher.NewBirdwatcher(&cfg.Birdwatcher)
case SourceBackendGoBGP:
instance = gobgp.NewGoBGP(cfg.GoBGP)
instance = gobgp.NewGoBGP(&cfg.GoBGP)
case SourceBackendOpenBGPDStateServer:
instance = openbgpd.NewStateServerSource(&cfg.OpenBGPD)
case SourceBackendOpenBGPDBgplgd:
Expand Down
11 changes: 3 additions & 8 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package config

import (
"testing"

"github.com/alice-lg/alice-lg/pkg/sources/birdwatcher"
"github.com/alice-lg/alice-lg/pkg/sources/gobgp"
)

// Test configuration loading and parsing
Expand Down Expand Up @@ -58,14 +55,13 @@ func TestSourceConfig(t *testing.T) {
rs2 := config.Sources[1] // Birdwatcher v6
rs3 := config.Sources[2] // GoBGP

nilBirdwatcherConfig := birdwatcher.Config{}
if rs1.Birdwatcher == nilBirdwatcherConfig {
if rs1.Backend == SourceBackendBirdwatcher {
t.Errorf(
"Example routeserver %s should have been identified as a birdwatcher source but was not",
rs1.Name,
)
}
if rs2.Birdwatcher == nilBirdwatcherConfig {
if rs2.Backend == SourceBackendBirdwatcher {
t.Errorf(
"Example routeserver %s should have been identified as a birdwatcher source but was not",
rs2.Name,
Expand All @@ -78,8 +74,7 @@ func TestSourceConfig(t *testing.T) {
t.Error("Unexpected StreamParserThrottle", rs2.Birdwatcher.StreamParserThrottle)
}
}
nilGoBGPConfig := gobgp.Config{}
if rs3.GoBGP == nilGoBGPConfig {
if rs3.Backend == SourceBackendGoBGP {
t.Errorf(
"Example routeserver %s should have been identified as a gobgp source but was not",
rs3.Name,
Expand Down
5 changes: 3 additions & 2 deletions pkg/sources/birdwatcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package birdwatcher
// Config contains all configuration attributes
// for a birdwatcher based source.
type Config struct {
ID string
Name string
ID string
Name string
HiddenNeighbors []string

API string `ini:"api"`
Timezone string `ini:"timezone"`
Expand Down
10 changes: 7 additions & 3 deletions pkg/sources/birdwatcher/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type GenericBirdwatcher struct {

// NewBirdwatcher creates a new Birdwatcher instance.
// This might be either a GenericBirdWatcher or a MultiTableBirdwatcher.
func NewBirdwatcher(config Config) Birdwatcher {
func NewBirdwatcher(config *Config) Birdwatcher {
client := NewClient(config.API)

// Cache settings:
Expand All @@ -58,7 +58,7 @@ func NewBirdwatcher(config Config) Birdwatcher {
if config.Type == "single_table" {
singleTableBirdwatcher := new(SingleTableBirdwatcher)

singleTableBirdwatcher.config = config
singleTableBirdwatcher.config = *config
singleTableBirdwatcher.client = client

singleTableBirdwatcher.neighborsCache = neighborsCache
Expand All @@ -72,7 +72,7 @@ func NewBirdwatcher(config Config) Birdwatcher {
} else if config.Type == "multi_table" {
multiTableBirdwatcher := new(MultiTableBirdwatcher)

multiTableBirdwatcher.config = config
multiTableBirdwatcher.config = *config
multiTableBirdwatcher.client = client

multiTableBirdwatcher.neighborsCache = neighborsCache
Expand Down Expand Up @@ -250,6 +250,10 @@ func (b *GenericBirdwatcher) NeighborsStatus(ctx context.Context) (
if err != nil {
return nil, err
}
neighbors, err = sources.FilterHiddenNeighborsStatus(neighbors, b.config.HiddenNeighbors)
if err != nil {
return nil, err
}

response := &api.NeighborsStatusResponse{
Response: api.Response{
Expand Down
6 changes: 6 additions & 0 deletions pkg/sources/birdwatcher/source_multitable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/decoders"
"github.com/alice-lg/alice-lg/pkg/pools"
"github.com/alice-lg/alice-lg/pkg/sources"
)

// MultiTableBirdwatcher implements a birdwatcher with
Expand Down Expand Up @@ -512,6 +513,11 @@ func (src *MultiTableBirdwatcher) Neighbors(
}
}

neighbors, err = sources.FilterHiddenNeighbors(neighbors, src.config.HiddenNeighbors)
if err != nil {
return nil, err
}

response = &api.NeighborsResponse{
Response: api.Response{
Meta: apiStatus,
Expand Down
6 changes: 6 additions & 0 deletions pkg/sources/birdwatcher/source_singletable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/sources"
)

// SingleTableBirdwatcher is an Alice Source
Expand Down Expand Up @@ -159,6 +160,11 @@ func (src *SingleTableBirdwatcher) Neighbors(
return nil, err
}

neighbors, err = sources.FilterHiddenNeighbors(neighbors, src.config.HiddenNeighbors)
if err != nil {
return nil, err
}

response = &api.NeighborsResponse{
Response: api.Response{
Meta: apiStatus,
Expand Down
5 changes: 3 additions & 2 deletions pkg/sources/gobgp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package gobgp

// Config is a GoBGP source config
type Config struct {
ID string
Name string
ID string
Name string
HiddenNeighbors []string

Host string `ini:"host"`
Insecure bool `ini:"insecure"`
Expand Down
13 changes: 11 additions & 2 deletions pkg/sources/gobgp/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type GoBGP struct {
}

// NewGoBGP creates a new GoBGP source instance
func NewGoBGP(config Config) *GoBGP {
func NewGoBGP(config *Config) *GoBGP {
dialOpts := make([]grpc.DialOption, 0)
if config.Insecure {
dialOpts = append(dialOpts, grpc.WithInsecure())
Expand Down Expand Up @@ -74,7 +74,7 @@ func NewGoBGP(config Config) *GoBGP {
routesCacheDisabled, routesCacheMaxSize)

return &GoBGP{
config: config,
config: *config,
client: client,

neighborsCache: neighborsCache,
Expand Down Expand Up @@ -132,6 +132,10 @@ func (gobgp *GoBGP) NeighborsStatus(
}

}
response.Neighbors, err = sources.FilterHiddenNeighborsStatus(response.Neighbors, gobgp.config.HiddenNeighbors)
if err != nil {
return nil, err
}
return &response, nil
}

Expand Down Expand Up @@ -206,6 +210,11 @@ func (gobgp *GoBGP) Neighbors(

}

response.Neighbors, err = sources.FilterHiddenNeighbors(response.Neighbors, gobgp.config.HiddenNeighbors)
if err != nil {
return nil, err
}

return &response, nil
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/sources/openbgpd/bgplgd_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ func (src *BgplgdSource) Neighbors(
if err != nil {
return nil, err
}
nb, err = sources.FilterHiddenNeighbors(nb, src.cfg.HiddenNeighbors)
if err != nil {
return nil, err
}
// Set route server id (sourceID) for all neighbors and
// calculate the filtered routes.
for _, n := range nb {
Expand Down Expand Up @@ -211,6 +215,11 @@ func (src *BgplgdSource) NeighborsSummary(
if err != nil {
return nil, err
}
nb, err = sources.FilterHiddenNeighbors(nb, src.cfg.HiddenNeighbors)
if err != nil {
return nil, err
}

// Set route server id (sourceID) for all neighbors and
// calculate the filtered routes.
for _, n := range nb {
Expand Down Expand Up @@ -252,6 +261,10 @@ func (src *BgplgdSource) NeighborsStatus(
if err != nil {
return nil, err
}
nb, err = sources.FilterHiddenNeighborsStatus(nb, src.cfg.HiddenNeighbors)
if err != nil {
return nil, err
}

response := &api.NeighborsStatusResponse{
Response: api.Response{
Expand Down
5 changes: 3 additions & 2 deletions pkg/sources/openbgpd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

// Config is a OpenBGPD source config
type Config struct {
ID string
Name string
ID string
Name string
HiddenNeighbors []string

CacheTTL time.Duration
RoutesCacheSize int
Expand Down
12 changes: 12 additions & 0 deletions pkg/sources/openbgpd/state_server_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ func (src *StateServerSource) Neighbors(
},
Neighbors: nb,
}
response.Neighbors, err = sources.FilterHiddenNeighbors(response.Neighbors, src.cfg.HiddenNeighbors)
if err != nil {
return nil, err
}
src.neighborsCache.Set(response)

return response, nil
Expand Down Expand Up @@ -235,6 +239,10 @@ func (src *StateServerSource) NeighborsSummary(
if err != nil {
return nil, err
}
nb, err = sources.FilterHiddenNeighbors(nb, src.cfg.HiddenNeighbors)
if err != nil {
return nil, err
}
// Set route server id (sourceID) for all neighbors
for _, n := range nb {
n.RouteServerID = src.cfg.ID
Expand Down Expand Up @@ -275,6 +283,10 @@ func (src *StateServerSource) NeighborsStatus(
if err != nil {
return nil, err
}
nb, err = sources.FilterHiddenNeighborsStatus(nb, src.cfg.HiddenNeighbors)
if err != nil {
return nil, err
}

response := &api.NeighborsStatusResponse{
Response: api.Response{
Expand Down
Loading

0 comments on commit ca24c09

Please sign in to comment.