summaryrefslogtreecommitdiffhomepage
path: root/policy/policy.go
diff options
context:
space:
mode:
authorHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2015-07-03 17:39:44 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-07-11 21:40:05 +0900
commitc2bf9c40c3ac8d0a7ef748c2cbd3c99ae469a6ca (patch)
tree14e24e8359a2d9664f525c49148c59ae2437b8b5 /policy/policy.go
parent4a4e711a0824cf5e970d77bb355818e19876394a (diff)
policy: support aspath prepend action
Diffstat (limited to 'policy/policy.go')
-rw-r--r--policy/policy.go82
1 files changed, 76 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 {