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

Merge pull request #10 from bio-routing/feature/route_filter

added local-pref and next-hop and as-path modifications to filters
parents 95f9f987 e52ad631
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,8 @@ package route
import (
"fmt"
"strconv"
"strings"
"github.com/taktv6/tflow2/convert"
)
......@@ -154,6 +156,32 @@ func (b *BGPPath) Print() string {
return ret
}
func (b *BGPPath) Prepend(asn uint32, times uint16) {
if times == 0 {
return
}
asnStr := strconv.FormatUint(uint64(asn), 10)
path := make([]string, times+1)
for i := 0; uint16(i) < times; i++ {
path[i] = asnStr
}
path[times] = b.ASPath
b.ASPath = strings.TrimSuffix(strings.Join(path, " "), " ")
b.ASPathLen = b.ASPathLen + times
}
func (p *BGPPath) Copy() *BGPPath {
if p == nil {
return nil
}
cp := *p
return &cp
}
func uint32To4Byte(addr uint32) [4]byte {
slice := convert.Uint32Byte(addr)
ret := [4]byte{
......
......@@ -100,3 +100,15 @@ func (p *Path) Print() string {
return ret
}
func (p *Path) Copy() *Path {
if p == nil {
return nil
}
cp := *p
cp.BGPPath = cp.BGPPath.Copy()
cp.StaticPath = cp.StaticPath.Copy()
return &cp
}
......@@ -23,3 +23,12 @@ func (s *StaticPath) Compare(t *StaticPath) int8 {
func (s *StaticPath) ECMP(t *StaticPath) bool {
return true
}
func (s *StaticPath) Copy() *StaticPath {
if s == nil {
return nil
}
cp := *s
return &cp
}
package actions
import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
type ASPathPrependAction struct {
asn uint32
times uint16
}
func NewASPathPrependAction(asn uint32, times uint16) *ASPathPrependAction {
return &ASPathPrependAction{
asn: asn,
times: times,
}
}
func (a *ASPathPrependAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) {
if pa.BGPPath == nil {
return pa, false
}
modified := pa.Copy()
modified.BGPPath.Prepend(a.asn, a.times)
return modified, false
}
package actions
import (
"testing"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert"
)
func TestAppendPath(t *testing.T) {
tests := []struct {
name string
times uint16
bgpPath *route.BGPPath
expectedPath string
expectedLength uint16
}{
{
name: "BGPPath is nil",
},
{
name: "append 0",
times: 0,
bgpPath: &route.BGPPath{
ASPath: "12345 12345",
ASPathLen: 2,
},
expectedPath: "12345 12345",
expectedLength: 2,
},
{
name: "append 3",
times: 3,
bgpPath: &route.BGPPath{
ASPath: "12345 15169",
ASPathLen: 2,
},
expectedPath: "12345 12345 12345 12345 15169",
expectedLength: 5,
},
}
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
a := NewASPathPrependAction(12345, test.times)
p, _ := a.Do(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
BGPPath: test.bgpPath,
})
if test.bgpPath == nil {
return
}
assert.Equal(te, test.expectedPath, p.BGPPath.ASPath, "ASPath")
assert.Equal(te, test.expectedLength, p.BGPPath.ASPathLen, "ASPathLen")
})
}
}
package actions
import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
type FilterAction interface {
Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool)
}
package actions
import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
type SetLocalPrefAction struct {
pref uint32
}
func NewSetLocalPrefAction(pref uint32) *SetLocalPrefAction {
return &SetLocalPrefAction{
pref: pref,
}
}
func (a *SetLocalPrefAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) {
if pa.BGPPath == nil {
return pa, false
}
modified := *pa
modified.BGPPath.LocalPref = a.pref
return &modified, false
}
package actions
import (
"testing"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert"
)
func TestSetLocalPref(t *testing.T) {
tests := []struct {
name string
bgpPath *route.BGPPath
expectedLocalPref uint32
}{
{
name: "BGPPath is nil",
},
{
name: "modify path",
bgpPath: &route.BGPPath{
LocalPref: 100,
},
expectedLocalPref: 150,
},
}
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
a := NewSetLocalPrefAction(150)
p, _ := a.Do(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
BGPPath: test.bgpPath,
})
if test.expectedLocalPref > 0 {
assert.Equal(te, test.expectedLocalPref, p.BGPPath.LocalPref)
}
})
}
}
func strAddr(s string) uint32 {
ret, _ := net.StrToAddr(s)
return ret
}
package actions
import (
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
)
type SetNextHopAction struct {
addr uint32
}
func NewSetNextHopAction(addr uint32) *SetNextHopAction {
return &SetNextHopAction{
addr: addr,
}
}
func (a *SetNextHopAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) {
if pa.BGPPath == nil {
return pa, false
}
modified := pa.Copy()
modified.BGPPath.NextHop = a.addr
return modified, false
}
package actions
import (
"testing"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert"
)
func TestSetNextHopTest(t *testing.T) {
tests := []struct {
name string
bgpPath *route.BGPPath
expected uint32
}{
{
name: "BGPPath is nil",
},
{
name: "modify path",
bgpPath: &route.BGPPath{
NextHop: strAddr("100.64.2.1"),
},
expected: strAddr("100.64.2.1"),
},
}
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
a := NewSetNextHopAction(strAddr("100.64.2.1"))
p, _ := a.Do(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{
BGPPath: test.bgpPath,
})
if test.expected > 0 {
assert.Equal(te, test.expected, p.BGPPath.NextHop)
}
})
}
}
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