Skip to content
Snippets Groups Projects
Unverified Commit eef8ce77 authored by takt's avatar takt Committed by GitHub
Browse files

Merge pull request #46 from bio-routing/feature/filters

Integrate filters into new FSM
parents 10386691 b9ec2de7
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/bio-routing/bio-rd/routingtable" "github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
) )
type Peer struct { type Peer struct {
...@@ -20,4 +21,6 @@ type Peer struct { ...@@ -20,4 +21,6 @@ type Peer struct {
RouterID uint32 RouterID uint32
AddPathSend routingtable.ClientOptions AddPathSend routingtable.ClientOptions
AddPathRecv bool AddPathRecv bool
ImportFilter *filter.Filter
ExportFilter *filter.Filter
} }
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"github.com/bio-routing/bio-rd/config" "github.com/bio-routing/bio-rd/config"
"github.com/bio-routing/bio-rd/protocols/bgp/server" "github.com/bio-routing/bio-rd/protocols/bgp/server"
"github.com/bio-routing/bio-rd/routingtable" "github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
"github.com/bio-routing/bio-rd/routingtable/locRIB" "github.com/bio-routing/bio-rd/routingtable/locRIB"
) )
...@@ -45,10 +46,10 @@ func main() { ...@@ -45,10 +46,10 @@ func main() {
AddPathSend: routingtable.ClientOptions{ AddPathSend: routingtable.ClientOptions{
MaxPaths: 10, MaxPaths: 10,
}, },
ImportFilter: filter.NewDrainFilter(),
ExportFilter: filter.NewAcceptAllFilter(),
}, rib) }, rib)
//time.Sleep(time.Second * 15)
b.AddPeer(config.Peer{ b.AddPeer(config.Peer{
AdminEnabled: true, AdminEnabled: true,
LocalAS: 65200, LocalAS: 65200,
...@@ -63,7 +64,9 @@ func main() { ...@@ -63,7 +64,9 @@ func main() {
AddPathSend: routingtable.ClientOptions{ AddPathSend: routingtable.ClientOptions{
MaxPaths: 10, MaxPaths: 10,
}, },
AddPathRecv: true, AddPathRecv: true,
ImportFilter: filter.NewAcceptAllFilter(),
ExportFilter: filter.NewDrainFilter(),
}, rib) }, rib)
go func() { go func() {
......
...@@ -53,7 +53,9 @@ func (s establishedState) run() (state, string) { ...@@ -53,7 +53,9 @@ func (s establishedState) run() (state, string) {
func (s *establishedState) init() { func (s *establishedState) init() {
s.fsm.adjRIBIn = adjRIBIn.New() s.fsm.adjRIBIn = adjRIBIn.New()
s.fsm.adjRIBIn.Register(s.fsm.rib)
s.fsm.peer.importFilter.Register(s.fsm.rib)
s.fsm.adjRIBIn.Register(s.fsm.peer.importFilter)
n := &routingtable.Neighbor{ n := &routingtable.Neighbor{
Type: route.BGPPathType, Type: route.BGPPathType,
...@@ -71,15 +73,23 @@ func (s *establishedState) init() { ...@@ -71,15 +73,23 @@ func (s *establishedState) init() {
} }
s.fsm.adjRIBOut.Register(s.fsm.updateSender) s.fsm.adjRIBOut.Register(s.fsm.updateSender)
s.fsm.rib.RegisterWithOptions(s.fsm.adjRIBOut, clientOptions) s.fsm.peer.exportFilter.Register(s.fsm.adjRIBOut)
s.fsm.rib.RegisterWithOptions(s.fsm.peer.exportFilter, clientOptions)
s.fsm.ribsInitialized = true s.fsm.ribsInitialized = true
} }
func (s *establishedState) uninit() { func (s *establishedState) uninit() {
s.fsm.adjRIBOut.Unregister(s.fsm.updateSender) s.fsm.adjRIBIn.Unregister(s.fsm.peer.importFilter)
s.fsm.rib.Unregister(s.fsm.adjRIBOut) s.fsm.peer.importFilter.Unregister(s.fsm.rib)
s.fsm.adjRIBIn.Unregister(s.fsm.rib)
s.fsm.rib.Unregister(s.fsm.peer.exportFilter)
s.fsm.peer.exportFilter.Unregister(s.fsm.adjRIBOut)
s.fsm.updateSender.Unregister(s.fsm.adjRIBOut)
s.fsm.adjRIBIn = nil
s.fsm.adjRIBOut = nil
s.fsm.ribsInitialized = false s.fsm.ribsInitialized = false
} }
......
...@@ -96,6 +96,8 @@ func NewPeer(c config.Peer, rib routingtable.RouteTableClient, server *BGPServer ...@@ -96,6 +96,8 @@ func NewPeer(c config.Peer, rib routingtable.RouteTableClient, server *BGPServer
keepaliveTime: c.KeepAlive, keepaliveTime: c.KeepAlive,
holdTime: c.HoldTime, holdTime: c.HoldTime,
optOpenParams: make([]packet.OptParam, 0), optOpenParams: make([]packet.OptParam, 0),
importFilter: filterOrDefault(c.ImportFilter),
exportFilter: filterOrDefault(c.ExportFilter),
} }
p.fsms = append(p.fsms, NewActiveFSM2(p)) p.fsms = append(p.fsms, NewActiveFSM2(p))
...@@ -130,6 +132,14 @@ func NewPeer(c config.Peer, rib routingtable.RouteTableClient, server *BGPServer ...@@ -130,6 +132,14 @@ func NewPeer(c config.Peer, rib routingtable.RouteTableClient, server *BGPServer
return p, nil return p, nil
} }
func filterOrDefault(f *filter.Filter) *filter.Filter {
if f != nil {
return f
}
return filter.NewDrainFilter()
}
// GetAddr returns the IP address of the peer // GetAddr returns the IP address of the peer
func (p *Peer) GetAddr() net.IP { func (p *Peer) GetAddr() net.IP {
return p.addr return p.addr
......
package filter
import (
"github.com/bio-routing/bio-rd/routingtable/filter/actions"
)
// NewAcceptAllFilter returns a filter accepting any paths/prefixes
func NewAcceptAllFilter() *Filter {
return NewFilter(
[]*Term{
NewTerm(
[]*TermCondition{},
[]FilterAction{
&actions.AcceptAction{},
}),
})
}
// NewDrainFilter returns a filter rejecting any paths/prefixes
func NewDrainFilter() *Filter {
return NewFilter(
[]*Term{
NewTerm(
[]*TermCondition{},
[]FilterAction{
&actions.RejectAction{},
}),
})
}
package filter
import (
"testing"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
func TestNewAcceptAllFilter(t *testing.T) {
f := NewAcceptAllFilter()
m := &clientMock{}
f.Register(m)
f.AddPath(net.NewPfx(0, 0), &route.Path{})
if !m.addPathCalled {
t.Fatalf("expected accepted, but was filtered")
}
}
func TestNewDrainFilter(t *testing.T) {
f := NewDrainFilter()
m := &clientMock{}
f.Register(m)
f.AddPath(net.NewPfx(0, 0), &route.Path{})
if m.addPathCalled {
t.Fatalf("expected filtered, but was accepted")
}
}
...@@ -39,7 +39,7 @@ func (t *Term) Process(p net.Prefix, pa *route.Path) (modPath *route.Path, rejec ...@@ -39,7 +39,7 @@ func (t *Term) Process(p net.Prefix, pa *route.Path) (modPath *route.Path, rejec
} }
} }
return orig, true return orig, false
} }
func (t *Term) processActions(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) { func (t *Term) processActions(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) {
......
...@@ -19,6 +19,18 @@ func NewTermCondition(prefixLists []*PrefixList, routeFilters []*RouteFilter) *T ...@@ -19,6 +19,18 @@ func NewTermCondition(prefixLists []*PrefixList, routeFilters []*RouteFilter) *T
} }
} }
func NewTermConditionWithRouteFilters(filters ...*RouteFilter) *TermCondition {
return &TermCondition{
routeFilters: filters,
}
}
func NewTermConditionWithPrefixLists(filters ...*PrefixList) *TermCondition {
return &TermCondition{
prefixLists: filters,
}
}
func (f *TermCondition) Matches(p net.Prefix, pa *route.Path) bool { func (f *TermCondition) Matches(p net.Prefix, pa *route.Path) bool {
return f.matchesPrefixListFilters(p) && return f.matchesPrefixListFilters(p) &&
f.machtchesRouteFilters(p) && f.machtchesRouteFilters(p) &&
......
...@@ -45,11 +45,8 @@ func TestProcess(t *testing.T) { ...@@ -45,11 +45,8 @@ func TestProcess(t *testing.T) {
prefix: net.NewPfx(strAddr("100.64.0.1"), 8), prefix: net.NewPfx(strAddr("100.64.0.1"), 8),
path: &route.Path{}, path: &route.Path{},
from: []*TermCondition{ from: []*TermCondition{
{ NewTermConditionWithPrefixLists(
prefixLists: []*PrefixList{ NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8))),
NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8)),
},
},
}, },
then: []FilterAction{ then: []FilterAction{
&actions.AcceptAction{}, &actions.AcceptAction{},
...@@ -62,16 +59,13 @@ func TestProcess(t *testing.T) { ...@@ -62,16 +59,13 @@ func TestProcess(t *testing.T) {
prefix: net.NewPfx(strAddr("100.64.0.1"), 8), prefix: net.NewPfx(strAddr("100.64.0.1"), 8),
path: &route.Path{}, path: &route.Path{},
from: []*TermCondition{ from: []*TermCondition{
{ NewTermConditionWithPrefixLists(
prefixLists: []*PrefixList{ NewPrefixList(net.NewPfx(0, 32))),
NewPrefixList(net.NewPfx(0, 32)),
},
},
}, },
then: []FilterAction{ then: []FilterAction{
&actions.AcceptAction{}, &actions.AcceptAction{},
}, },
expectReject: true, expectReject: false,
expectModified: false, expectModified: false,
}, },
{ {
...@@ -79,11 +73,8 @@ func TestProcess(t *testing.T) { ...@@ -79,11 +73,8 @@ func TestProcess(t *testing.T) {
prefix: net.NewPfx(strAddr("100.64.0.1"), 8), prefix: net.NewPfx(strAddr("100.64.0.1"), 8),
path: &route.Path{}, path: &route.Path{},
from: []*TermCondition{ from: []*TermCondition{
{ NewTermConditionWithPrefixLists(
prefixLists: []*PrefixList{ NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8))),
NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8)),
},
},
}, },
then: []FilterAction{ then: []FilterAction{
&mockAction{}, &mockAction{},
...@@ -96,11 +87,8 @@ func TestProcess(t *testing.T) { ...@@ -96,11 +87,8 @@ func TestProcess(t *testing.T) {
prefix: net.NewPfx(strAddr("100.64.0.1"), 8), prefix: net.NewPfx(strAddr("100.64.0.1"), 8),
path: &route.Path{}, path: &route.Path{},
from: []*TermCondition{ from: []*TermCondition{
{ NewTermConditionWithRouteFilters(
prefixLists: []*PrefixList{ NewRouteFilter(net.NewPfx(strAddr("100.64.0.1"), 8), Exact())),
NewPrefixList(net.NewPfx(strAddr("100.64.0.1"), 8)),
},
},
}, },
then: []FilterAction{ then: []FilterAction{
&mockAction{}, &mockAction{},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment