diff options
author | Hiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp> | 2015-07-03 17:39:44 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-07-11 21:40:05 +0900 |
commit | c2bf9c40c3ac8d0a7ef748c2cbd3c99ae469a6ca (patch) | |
tree | 14e24e8359a2d9664f525c49148c59ae2437b8b5 /policy | |
parent | 4a4e711a0824cf5e970d77bb355818e19876394a (diff) |
policy: support aspath prepend action
Diffstat (limited to 'policy')
-rw-r--r-- | policy/policy.go | 82 | ||||
-rw-r--r-- | policy/policy_test.go | 149 |
2 files changed, 225 insertions, 6 deletions
diff --git a/policy/policy.go b/policy/policy.go index d3e4ef03..300fcadf 100644 --- a/policy/policy.go +++ b/policy/policy.go @@ -17,16 +17,17 @@ package policy import ( "fmt" - log "github.com/Sirupsen/logrus" - "github.com/osrg/gobgp/api" - "github.com/osrg/gobgp/config" - "github.com/osrg/gobgp/packet" - "github.com/osrg/gobgp/table" "net" "reflect" "regexp" "strconv" "strings" + + log "github.com/Sirupsen/logrus" + "github.com/osrg/gobgp/api" + "github.com/osrg/gobgp/config" + "github.com/osrg/gobgp/packet" + "github.com/osrg/gobgp/table" ) type RouteType int @@ -118,6 +119,12 @@ func NewPolicy(pd config.PolicyDefinition, ds config.DefinedSets) *Policy { mda = append(mda, med) } + //AsPathPrependAction + ppa := NewAsPathPrependAction(statement.Actions.BgpActions.SetAsPathPrepend) + if ppa != nil { + mda = append(mda, ppa) + } + s := &Statement{ Name: statement.Name, Conditions: conditions, @@ -870,6 +877,69 @@ func (a *MedAction) apply(path *table.Path) *table.Path { return path } +type AsPathPrependAction struct { + DefaultAction + asn uint32 + useLeftMost bool + repeat uint8 +} + +// NewAsPathPrependAction creates AsPathPrependAction object. +// If ASN cannot be parsed, nil will be returned. +func NewAsPathPrependAction(action config.SetAsPathPrepend) *AsPathPrependAction { + + a := &AsPathPrependAction{} + + if action.As == "" { + return nil + } + + if action.As == "last-as" { + a.useLeftMost = true + } else { + asn, err := strconv.ParseUint(action.As, 10, 32) + if err != nil { + log.WithFields(log.Fields{ + "Topic": "Policy", + "Type": "AsPathPrepend Action", + "Value": action.As, + }).Error("As number string invalid.") + return nil + } + a.asn = uint32(asn) + } + a.repeat = action.RepeatN + + return a +} + +func (a *AsPathPrependAction) apply(path *table.Path) *table.Path { + + var asn uint32 + if a.useLeftMost { + asns := path.GetAsSeqList() + if len(asns) == 0 { + log.WithFields(log.Fields{ + "Topic": "Policy", + "Type": "AsPathPrepend Action", + }).Error("aspath length is zero.") + return path + } + asn = asns[0] + log.WithFields(log.Fields{ + "Topic": "Policy", + "Type": "AsPathPrepend Action", + "LastAs": asn, + "Repeat": a.repeat, + }).Debug("use last AS.") + } else { + asn = a.asn + } + + path.PrependAsn(asn, a.repeat) + return path +} + type Prefix struct { Address net.IP AddressFamily bgp.RouteFamily @@ -1349,7 +1419,7 @@ func ActionsToApiStruct(conActions config.Actions) *api.Actions { func ActionsToConfigStruct(reqActions *api.Actions) config.Actions { actions := config.Actions{} - if reqActions == nil{ + if reqActions == nil { return actions } if reqActions.Community != nil { diff --git a/policy/policy_test.go b/policy/policy_test.go index ae615935..d1155335 100644 --- a/policy/policy_test.go +++ b/policy/policy_test.go @@ -1856,3 +1856,152 @@ func TestPolicyMatchWhenPathHaveNotMed(t *testing.T) { _, err := newPath.GetMed() assert.NotNil(t, err) } + + +func TestPolicyAsPathPrepend(t *testing.T) { + + assert := assert.New(t) + + // create path + peer := &table.PeerInfo{AS: 65001, Address: net.ParseIP("10.0.0.1")} + origin := bgp.NewPathAttributeOrigin(0) + aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65001, 65000})} + aspath := bgp.NewPathAttributeAsPath(aspathParam) + nexthop := bgp.NewPathAttributeNextHop("10.0.0.1") + med := bgp.NewPathAttributeMultiExitDisc(0) + + pathAttributes := []bgp.PathAttributeInterface{origin, aspath, nexthop, med} + nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} + withdrawnRoutes := []bgp.WithdrawnRoute{} + updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) + + body := updateMsg.Body.(*bgp.BGPUpdate) + table.UpdatePathAttrs4ByteAs(body) + path := table.ProcessMessage(updateMsg, peer)[0] + + + // create policy + ps := config.PrefixSet{ + PrefixSetName: "ps1", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("10.10.0.0"), + Masklength: 16, + MasklengthRange: "21..24", + }}, + } + ns := config.NeighborSet{ + NeighborSetName: "ns1", + NeighborInfoList: []config.NeighborInfo{ + config.NeighborInfo{ + Address: net.ParseIP("10.0.0.1"), + }}, + } + ds := config.DefinedSets{ + PrefixSetList: []config.PrefixSet{ps}, + NeighborSetList: []config.NeighborSet{ns}, + } + + s := config.Statement{ + Name: "statement1", + Conditions: config.Conditions{ + MatchPrefixSet: "ps1", + MatchNeighborSet: "ns1", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: true, + BgpActions: config.BgpActions{ + SetAsPathPrepend: config.SetAsPathPrepend{ + As: "65002", + RepeatN: 10, + }, + }, + }, + } + + pd := config.PolicyDefinition{"pd1", []config.Statement{s}} + pl := config.RoutingPolicy{ds, []config.PolicyDefinition{pd}} + //test + df := pl.DefinedSets + p := NewPolicy(pl.PolicyDefinitionList[0], df) + match, pType, newPath := p.Apply(path) + assert.Equal(true, match) + assert.Equal(ROUTE_TYPE_ACCEPT, pType) + assert.NotEqual(nil, newPath) + assert.Equal([]uint32{65002, 65002, 65002, 65002, 65002, 65002, 65002, 65002, 65002, 65002, 65001, 65000}, newPath.GetAsSeqList()) +} + + +func TestPolicyAsPathPrependLastAs(t *testing.T) { + + assert := assert.New(t) + // create path + peer := &table.PeerInfo{AS: 65001, Address: net.ParseIP("10.0.0.1")} + origin := bgp.NewPathAttributeOrigin(0) + aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65002, 65001, 65000})} + aspath := bgp.NewPathAttributeAsPath(aspathParam) + nexthop := bgp.NewPathAttributeNextHop("10.0.0.1") + med := bgp.NewPathAttributeMultiExitDisc(0) + + pathAttributes := []bgp.PathAttributeInterface{origin, aspath, nexthop, med} + nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} + withdrawnRoutes := []bgp.WithdrawnRoute{} + updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) + + body := updateMsg.Body.(*bgp.BGPUpdate) + table.UpdatePathAttrs4ByteAs(body) + path := table.ProcessMessage(updateMsg, peer)[0] + + + // create policy + ps := config.PrefixSet{ + PrefixSetName: "ps1", + PrefixList: []config.Prefix{ + config.Prefix{ + Address: net.ParseIP("10.10.0.0"), + Masklength: 16, + MasklengthRange: "21..24", + }}, + } + ns := config.NeighborSet{ + NeighborSetName: "ns1", + NeighborInfoList: []config.NeighborInfo{ + config.NeighborInfo{ + Address: net.ParseIP("10.0.0.1"), + }}, + } + ds := config.DefinedSets{ + PrefixSetList: []config.PrefixSet{ps}, + NeighborSetList: []config.NeighborSet{ns}, + } + + s := config.Statement{ + Name: "statement1", + Conditions: config.Conditions{ + MatchPrefixSet: "ps1", + MatchNeighborSet: "ns1", + MatchSetOptions: config.MATCH_SET_OPTIONS_TYPE_ALL, + }, + Actions: config.Actions{ + AcceptRoute: true, + BgpActions: config.BgpActions{ + SetAsPathPrepend: config.SetAsPathPrepend{ + As: "last-as", + RepeatN: 5, + }, + }, + }, + } + + pd := config.PolicyDefinition{"pd1", []config.Statement{s}} + pl := config.RoutingPolicy{ds, []config.PolicyDefinition{pd}} + //test + df := pl.DefinedSets + p := NewPolicy(pl.PolicyDefinitionList[0], df) + match, pType, newPath := p.Apply(path) + assert.Equal(true, match) + assert.Equal(ROUTE_TYPE_ACCEPT, pType) + assert.NotEqual(nil, newPath) + assert.Equal([]uint32{65002, 65002, 65002, 65002, 65002, 65002, 65001, 65000}, newPath.GetAsSeqList()) +} |