Skip to content

Commit

Permalink
feat: add bird3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
fsrv-xyz committed Jan 14, 2025
2 parents de74864 + 343f055 commit 238619b
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ require (
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/google/nftables v0.2.1-0.20240923151943-ed578af895ee
github.com/prometheus/client_golang v1.20.5
github.com/stretchr/testify v1.9.0
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand All @@ -26,4 +30,5 @@ require (
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -17,6 +18,10 @@ github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtL
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
Expand All @@ -35,6 +40,8 @@ github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G
github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand All @@ -52,6 +59,8 @@ golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion internal/route/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func ParseFlowSpecRoute(input string) (FlowspecRoute, error) {
return FlowspecRoute{}, fmt.Errorf("invalid flowspec route: (%s): %v\n", header, err)
}

action, arg, err := parseFlowCommunity(inclusiveMatch(input, "BGP.ext_community: (", ")"))
action, arg, err := parseFlowCommunity(inclusiveMatch(input, "ext_community: (", ")"))
if err != nil {
return FlowspecRoute{}, fmt.Errorf("invalid flowspec route: (%s): %v\n", header, err)
}
Expand Down
72 changes: 72 additions & 0 deletions internal/route/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,81 @@
package route

import (
"net"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseFlowSpecRoute(t *testing.T) {
for _, testCase := range []struct {
name string
in string
expectedOut FlowspecRoute
expectedErr bool
}{
{
name: "empty input",
in: "",
expectedOut: FlowspecRoute{},
expectedErr: true,
},
{
name: "bird2 sample route",
in: "flow6 { dst 2001:db8:2::/128; src 2001:db8:1::/128; next header 17; dport 123; } [igp_router3 2025-01-13 from 2001:2::3] * (100) [i]\n\tType: BGP univ\n\tBGP.origin: IGP\n\tBGP.as_path: \n\tBGP.local_pref: 100\n\tBGP.ext_community: (generic, 0x80060000, 0x4ac80000)",
expectedOut: FlowspecRoute{
MatchAttrs: matchAttrs{
Source: func() net.IPNet { _, netw, _ := net.ParseCIDR("2001:db8:1::/128"); return *netw }(),
Destination: func() net.IPNet { _, netw, _ := net.ParseCIDR("2001:db8:2::/128"); return *netw }(),
Protocol: 17,
SourcePort: 0x0,
DestinationPort: 123,
},
SessionAttrs: sessionAttrs{
SessionName: "igp_router3",
NeighborAddress: net.ParseIP("2001:2::3"),
ImportTime: "2025-01-13",
},
Action: ActionTrafficRateBytes,
Argument: func() int64 { f, _ := parseIEEE754Float("0x4ac80000"); return int64(f) }(),
},
expectedErr: false,
},
{
name: "bird3 sample route",
in: "flow6 { dst 2001:db8:2::/128; src 2001:db8:1::/128; next header 17; dport 123; } unknown [igp_rr 2025-01-13 from 2001:2::2] * (100) [i]\n\tpreference: 100\n\tfrom: 2001:2::2\n\tsource: BGP\n\tbgp_origin: IGP\n\tbgp_path: \n\tbgp_local_pref: 100\n\tbgp_originator_id: 188.245.118.170\n\tbgp_cluster_list: 162.55.169.45\n\tbgp_ext_community: (generic, 0x80060000, 0x4ac80000)\n\tInternal route handling values: 0L 7G 1S id 1",
expectedOut: FlowspecRoute{
MatchAttrs: matchAttrs{
Source: func() net.IPNet { _, netw, _ := net.ParseCIDR("2001:db8:1::/128"); return *netw }(),
Destination: func() net.IPNet { _, netw, _ := net.ParseCIDR("2001:db8:2::/128"); return *netw }(),
Protocol: 17,
SourcePort: 0x0,
DestinationPort: 123,
},
SessionAttrs: sessionAttrs{
SessionName: "igp_rr",
NeighborAddress: net.ParseIP("2001:2::2"),
ImportTime: "2025-01-13",
},
Action: ActionTrafficRateBytes,
Argument: func() int64 { f, _ := parseIEEE754Float("0x4ac80000"); return int64(f) }(),
},
expectedErr: false,
},
} {
t.Run(testCase.name, func(t *testing.T) {
out, parseError := ParseFlowSpecRoute(testCase.in)
assert.Equal(t, testCase.expectedOut, out)

if testCase.expectedErr {
assert.Error(t, parseError)
} else {
assert.NoError(t, parseError)
}
})
}
}

func Test_parseIEEE754Float(t *testing.T) {
type args struct {
hexStr string
Expand Down

0 comments on commit 238619b

Please sign in to comment.