summaryrefslogtreecommitdiffhomepage
path: root/table
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 /table
parent4a4e711a0824cf5e970d77bb355818e19876394a (diff)
policy: support aspath prepend action
Diffstat (limited to 'table')
-rw-r--r--table/path.go101
-rw-r--r--table/path_test.go160
2 files changed, 219 insertions, 42 deletions
diff --git a/table/path.go b/table/path.go
index bd368ec6..302847f8 100644
--- a/table/path.go
+++ b/table/path.go
@@ -79,48 +79,10 @@ func (path *Path) UpdatePathAttrs(global *config.Global, peer *config.Neighbor)
path.SetNexthop(peer.LocalAddress)
// AS_PATH handling
- //
- // When a given BGP speaker advertises the route to an external
- // peer, the advertising speaker updates the AS_PATH attribute
- // as follows:
- // 1) if the first path segment of the AS_PATH is of type
- // AS_SEQUENCE, the local system prepends its own AS num as
- // the last element of the sequence (put it in the left-most
- // position with respect to the position of octets in the
- // protocol message). If the act of prepending will cause an
- // overflow in the AS_PATH segment (i.e., more than 255
- // ASes), it SHOULD prepend a new segment of type AS_SEQUENCE
- // and prepend its own AS number to this new segment.
- //
- // 2) if the first path segment of the AS_PATH is of type AS_SET
- // , the local system prepends a new path segment of type
- // AS_SEQUENCE to the AS_PATH, including its own AS number in
- // that segment.
- //
- // 3) if the AS_PATH is empty, the local system creates a path
- // segment of type AS_SEQUENCE, places its own AS into that
- // segment, and places that segment into the AS_PATH.
- idx, originalAsPath := path.getPathAttr(bgp.BGP_ATTR_TYPE_AS_PATH)
- if idx < 0 {
- p := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint32{global.As})
- asPath := bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{p})
- path.pathAttrs = append(path.pathAttrs, asPath)
- } else {
- asPath := cloneAsPath(originalAsPath.(*bgp.PathAttributeAsPath))
- path.pathAttrs[idx] = asPath
- fst := asPath.Value[0].(*bgp.As4PathParam)
- if len(asPath.Value) > 0 && fst.Type == bgp.BGP_ASPATH_ATTR_TYPE_SEQ &&
- fst.ASLen() < 255 {
- fst.AS = append([]uint32{global.As}, fst.AS...)
- fst.Num += 1
- } else {
- p := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint32{global.As})
- asPath.Value = append([]bgp.AsPathParamInterface{p}, asPath.Value...)
- }
- }
+ path.PrependAsn(global.As, 1)
// MED Handling
- idx, _ = path.getPathAttr(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC)
+ idx, _ := path.getPathAttr(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC)
if idx >= 0 {
path.pathAttrs = append(path.pathAttrs[:idx], path.pathAttrs[idx+1:]...)
}
@@ -328,6 +290,65 @@ func (path *Path) getAsListofSpecificType(getAsSeq, getAsSet bool) []uint32 {
return asList
}
+// PrependAsn prepends AS number.
+// This function updates the AS_PATH attribute as follows.
+// 1) if the first path segment of the AS_PATH is of type
+// AS_SEQUENCE, the local system prepends the specified AS num as
+// the last element of the sequence (put it in the left-most
+// position with respect to the position of octets in the
+// protocol message) the specified number of times.
+// If the act of prepending will cause an overflow in the AS_PATH
+// segment (i.e., more than 255 ASes),
+// it SHOULD prepend a new segment of type AS_SEQUENCE
+// and prepend its own AS number to this new segment.
+//
+// 2) if the first path segment of the AS_PATH is of other than type
+// AS_SEQUENCE, the local system prepends a new path segment of type
+// AS_SEQUENCE to the AS_PATH, including the specified AS number in
+// that segment.
+//
+// 3) if the AS_PATH is empty, the local system creates a path
+// segment of type AS_SEQUENCE, places the specified AS number
+// into that segment, and places that segment into the AS_PATH.
+func (path *Path) PrependAsn(asn uint32, repeat uint8) {
+
+ idx, aspath := path.getPathAttr(bgp.BGP_ATTR_TYPE_AS_PATH)
+
+ asns := make([]uint32, repeat)
+ for i, _ := range asns {
+ asns[i] = asn
+ }
+
+ if idx < 0 {
+ p := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, asns)
+ asPath := bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{p})
+ path.pathAttrs = append(path.pathAttrs, asPath)
+ } else {
+ aspathClone := cloneAsPath(aspath.(*bgp.PathAttributeAsPath))
+ path.pathAttrs[idx] = aspathClone
+ fst := aspathClone.Value[0].(*bgp.As4PathParam)
+
+ if fst.Type == bgp.BGP_ASPATH_ATTR_TYPE_SEQ && len(fst.AS) < 255 {
+
+ // overflow case
+ if len(fst.AS)+int(repeat) > 255 {
+ rest := 255 - len(fst.AS)
+ fst.AS = append(asns[0:rest], fst.AS...)
+ fst.Num = 255
+
+ p := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, asns[rest:])
+ aspathClone.Value = append([]bgp.AsPathParamInterface{p}, aspathClone.Value...)
+ } else {
+ fst.AS = append(asns, fst.AS...)
+ fst.Num += repeat
+ }
+ } else {
+ p := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, asns)
+ aspathClone.Value = append([]bgp.AsPathParamInterface{p}, aspathClone.Value...)
+ }
+ }
+}
+
func (path *Path) GetCommunities() []uint32 {
communityList := []uint32{}
if _, attr := path.getPathAttr(bgp.BGP_ATTR_TYPE_COMMUNITIES); attr != nil {
diff --git a/table/path_test.go b/table/path_test.go
index 44381fd7..976a5620 100644
--- a/table/path_test.go
+++ b/table/path_test.go
@@ -3,10 +3,12 @@ package table
import (
//"fmt"
- "github.com/osrg/gobgp/packet"
- "github.com/stretchr/testify/assert"
+ "fmt"
"testing"
"time"
+
+ "github.com/osrg/gobgp/packet"
+ "github.com/stretchr/testify/assert"
)
func TestPathNewIPv4(t *testing.T) {
@@ -120,6 +122,160 @@ func TestASPathLen(t *testing.T) {
assert.Equal(10, p.GetAsPathLen())
}
+func TestPathPrependAsnToExistingSeqAttr(t *testing.T) {
+ assert := assert.New(t)
+ origin := bgp.NewPathAttributeOrigin(0)
+ aspathParam := []bgp.AsPathParamInterface{
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint16{65001, 65002, 65003, 65004, 65005}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.50.1")
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ }
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ bgpmsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ update := bgpmsg.Body.(*bgp.BGPUpdate)
+ UpdatePathAttrs4ByteAs(update)
+ peer := PathCreatePeer()
+ p := NewPath(peer[0], &update.NLRI[0], false, update.PathAttributes, false, time.Now())
+
+ p.PrependAsn(65000, 1)
+ assert.Equal([]uint32{65000, 65001, 65002, 65003, 65004, 65005}, p.GetAsSeqList())
+ fmt.Printf("asns: %v", p.GetAsSeqList())
+}
+
+func TestPathPrependAsnToNewAsPathAttr(t *testing.T) {
+ assert := assert.New(t)
+ origin := bgp.NewPathAttributeOrigin(0)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.50.1")
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ nexthop,
+ }
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ bgpmsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ update := bgpmsg.Body.(*bgp.BGPUpdate)
+ UpdatePathAttrs4ByteAs(update)
+ peer := PathCreatePeer()
+ p := NewPath(peer[0], &update.NLRI[0], false, update.PathAttributes, false, time.Now())
+
+ asn := uint32(65000)
+ p.PrependAsn(asn, 1)
+ assert.Equal([]uint32{asn}, p.GetAsSeqList())
+}
+
+func TestPathPrependAsnToNewAsPathSeq(t *testing.T) {
+ assert := assert.New(t)
+ origin := bgp.NewPathAttributeOrigin(0)
+ aspathParam := []bgp.AsPathParamInterface{
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.50.1")
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ }
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ bgpmsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ update := bgpmsg.Body.(*bgp.BGPUpdate)
+ UpdatePathAttrs4ByteAs(update)
+ peer := PathCreatePeer()
+ p := NewPath(peer[0], &update.NLRI[0], false, update.PathAttributes, false, time.Now())
+
+ asn := uint32(65000)
+ p.PrependAsn(asn, 1)
+ assert.Equal([]uint32{asn}, p.GetAsSeqList())
+ fmt.Printf("asns: %v", p.GetAsSeqList())
+}
+
+func TestPathPrependAsnToEmptyAsPathAttr(t *testing.T) {
+ assert := assert.New(t)
+ origin := bgp.NewPathAttributeOrigin(0)
+ aspathParam := []bgp.AsPathParamInterface{
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint16{}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.50.1")
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ }
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ bgpmsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ update := bgpmsg.Body.(*bgp.BGPUpdate)
+ UpdatePathAttrs4ByteAs(update)
+ peer := PathCreatePeer()
+ p := NewPath(peer[0], &update.NLRI[0], false, update.PathAttributes, false, time.Now())
+
+ asn := uint32(65000)
+ p.PrependAsn(asn, 1)
+ assert.Equal([]uint32{asn}, p.GetAsSeqList())
+ fmt.Printf("asns: %v", p.GetAsSeqList())
+}
+
+func TestPathPrependAsnToFullPathAttr(t *testing.T) {
+ assert := assert.New(t)
+ origin := bgp.NewPathAttributeOrigin(0)
+
+ asns := make([]uint16, 255)
+ for i, _ := range asns {
+ asns[i] = 65000 + uint16(i)
+ }
+
+ aspathParam := []bgp.AsPathParamInterface{
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, asns),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101})}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.50.1")
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ }
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ bgpmsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ update := bgpmsg.Body.(*bgp.BGPUpdate)
+ UpdatePathAttrs4ByteAs(update)
+ peer := PathCreatePeer()
+ p := NewPath(peer[0], &update.NLRI[0], false, update.PathAttributes, false, time.Now())
+
+ expected := []uint32{65000, 65000}
+ for _, v := range asns {
+ expected = append(expected, uint32(v))
+ }
+ p.PrependAsn(65000, 2)
+ assert.Equal(expected, p.GetAsSeqList())
+ fmt.Printf("asns: %v", p.GetAsSeqList())
+}
+
func PathCreatePeer() []*PeerInfo {
peerP1 := &PeerInfo{AS: 65000}
peerP2 := &PeerInfo{AS: 65001}