Add unit tests
This commit is contained in:
204
vendor/github.com/osrg/gobgp/internal/pkg/table/adj.go
generated
vendored
204
vendor/github.com/osrg/gobgp/internal/pkg/table/adj.go
generated
vendored
@@ -23,16 +23,16 @@ import (
|
||||
|
||||
type AdjRib struct {
|
||||
accepted map[bgp.RouteFamily]int
|
||||
table map[bgp.RouteFamily]map[string]*Path
|
||||
table map[bgp.RouteFamily]*Table
|
||||
}
|
||||
|
||||
func NewAdjRib(rfList []bgp.RouteFamily) *AdjRib {
|
||||
table := make(map[bgp.RouteFamily]map[string]*Path)
|
||||
for _, rf := range rfList {
|
||||
table[rf] = make(map[string]*Path)
|
||||
m := make(map[bgp.RouteFamily]*Table)
|
||||
for _, f := range rfList {
|
||||
m[f] = NewTable(f)
|
||||
}
|
||||
return &AdjRib{
|
||||
table: table,
|
||||
table: m,
|
||||
accepted: make(map[bgp.RouteFamily]int),
|
||||
}
|
||||
}
|
||||
@@ -43,56 +43,100 @@ func (adj *AdjRib) Update(pathList []*Path) {
|
||||
continue
|
||||
}
|
||||
rf := path.GetRouteFamily()
|
||||
key := fmt.Sprintf("%d:%s", path.GetNlri().PathIdentifier(), path.getPrefix())
|
||||
t := adj.table[path.GetRouteFamily()]
|
||||
d := t.getOrCreateDest(path.GetNlri(), 0)
|
||||
var old *Path
|
||||
idx := -1
|
||||
for i, p := range d.knownPathList {
|
||||
if p.GetNlri().PathIdentifier() == path.GetNlri().PathIdentifier() {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx != -1 {
|
||||
old = d.knownPathList[idx]
|
||||
}
|
||||
|
||||
old, found := adj.table[rf][key]
|
||||
if path.IsWithdraw {
|
||||
if found {
|
||||
delete(adj.table[rf], key)
|
||||
if !old.IsAsLooped() {
|
||||
if idx != -1 {
|
||||
d.knownPathList = append(d.knownPathList[:idx], d.knownPathList[idx+1:]...)
|
||||
if len(d.knownPathList) == 0 {
|
||||
t.deleteDest(d)
|
||||
}
|
||||
if !old.IsRejected() {
|
||||
adj.accepted[rf]--
|
||||
}
|
||||
}
|
||||
path.SetDropped(true)
|
||||
} else {
|
||||
if found {
|
||||
if old.IsAsLooped() && !path.IsAsLooped() {
|
||||
if idx != -1 {
|
||||
if old.IsRejected() && !path.IsRejected() {
|
||||
adj.accepted[rf]++
|
||||
} else if !old.IsAsLooped() && path.IsAsLooped() {
|
||||
} else if !old.IsRejected() && path.IsRejected() {
|
||||
adj.accepted[rf]--
|
||||
}
|
||||
if old.Equal(path) {
|
||||
path.setTimestamp(old.GetTimestamp())
|
||||
}
|
||||
d.knownPathList[idx] = path
|
||||
} else {
|
||||
if !path.IsAsLooped() {
|
||||
d.knownPathList = append(d.knownPathList, path)
|
||||
if !path.IsRejected() {
|
||||
adj.accepted[rf]++
|
||||
}
|
||||
}
|
||||
if found && old.Equal(path) {
|
||||
path.setTimestamp(old.GetTimestamp())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The provided pathList is expected to be the real candidate routes after policy evaluation.
|
||||
For routes that are filtered by policy, there could be a mismatch between display
|
||||
and actual rib sent to the peer (if softreset out was not run).
|
||||
Only used to display adj-out because we do not maintain a separate adj-out table
|
||||
*/
|
||||
func (adj *AdjRib) UpdateAdjRibOut(pathList []*Path) {
|
||||
for _, path := range pathList {
|
||||
if path == nil || path.IsEOR() {
|
||||
continue
|
||||
}
|
||||
t := adj.table[path.GetRouteFamily()]
|
||||
d := t.getOrCreateDest(path.GetNlri(), 0)
|
||||
d.knownPathList = append(d.knownPathList, path)
|
||||
}
|
||||
}
|
||||
|
||||
func (adj *AdjRib) walk(families []bgp.RouteFamily, fn func(*Destination) bool) {
|
||||
for _, f := range families {
|
||||
if t, ok := adj.table[f]; ok {
|
||||
for _, d := range t.destinations {
|
||||
if fn(d) {
|
||||
return
|
||||
}
|
||||
}
|
||||
adj.table[rf][key] = path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (adj *AdjRib) PathList(rfList []bgp.RouteFamily, accepted bool) []*Path {
|
||||
pathList := make([]*Path, 0, adj.Count(rfList))
|
||||
for _, rf := range rfList {
|
||||
for _, rr := range adj.table[rf] {
|
||||
if accepted && rr.IsAsLooped() {
|
||||
adj.walk(rfList, func(d *Destination) bool {
|
||||
for _, p := range d.knownPathList {
|
||||
if accepted && p.IsRejected() {
|
||||
continue
|
||||
}
|
||||
pathList = append(pathList, rr)
|
||||
pathList = append(pathList, p)
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
return pathList
|
||||
}
|
||||
|
||||
func (adj *AdjRib) Count(rfList []bgp.RouteFamily) int {
|
||||
count := 0
|
||||
for _, rf := range rfList {
|
||||
if table, ok := adj.table[rf]; ok {
|
||||
count += len(table)
|
||||
}
|
||||
}
|
||||
adj.walk(rfList, func(d *Destination) bool {
|
||||
count += len(d.knownPathList)
|
||||
return false
|
||||
})
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -106,70 +150,88 @@ func (adj *AdjRib) Accepted(rfList []bgp.RouteFamily) int {
|
||||
return count
|
||||
}
|
||||
|
||||
func (adj *AdjRib) Drop(rfList []bgp.RouteFamily) {
|
||||
for _, rf := range rfList {
|
||||
if _, ok := adj.table[rf]; ok {
|
||||
adj.table[rf] = make(map[string]*Path)
|
||||
adj.accepted[rf] = 0
|
||||
func (adj *AdjRib) Drop(rfList []bgp.RouteFamily) []*Path {
|
||||
l := make([]*Path, 0, adj.Count(rfList))
|
||||
adj.walk(rfList, func(d *Destination) bool {
|
||||
for _, p := range d.knownPathList {
|
||||
w := p.Clone(true)
|
||||
w.SetDropped(true)
|
||||
l = append(l, w)
|
||||
}
|
||||
return false
|
||||
})
|
||||
for _, rf := range rfList {
|
||||
adj.table[rf] = NewTable(rf)
|
||||
adj.accepted[rf] = 0
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func (adj *AdjRib) DropStale(rfList []bgp.RouteFamily) []*Path {
|
||||
pathList := make([]*Path, 0, adj.Count(rfList))
|
||||
for _, rf := range rfList {
|
||||
if table, ok := adj.table[rf]; ok {
|
||||
for k, p := range table {
|
||||
if p.IsStale() {
|
||||
delete(table, k)
|
||||
if !p.IsAsLooped() {
|
||||
adj.accepted[rf]--
|
||||
}
|
||||
pathList = append(pathList, p.Clone(true))
|
||||
}
|
||||
adj.walk(rfList, func(d *Destination) bool {
|
||||
for _, p := range d.knownPathList {
|
||||
if p.IsStale() {
|
||||
w := p.Clone(true)
|
||||
w.SetDropped(true)
|
||||
pathList = append(pathList, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
adj.Update(pathList)
|
||||
return pathList
|
||||
}
|
||||
|
||||
func (adj *AdjRib) StaleAll(rfList []bgp.RouteFamily) []*Path {
|
||||
pathList := make([]*Path, 0)
|
||||
for _, rf := range rfList {
|
||||
if table, ok := adj.table[rf]; ok {
|
||||
l := make([]*Path, 0, len(table))
|
||||
for k, p := range table {
|
||||
n := p.Clone(false)
|
||||
n.MarkStale(true)
|
||||
table[k] = n
|
||||
l = append(l, n)
|
||||
}
|
||||
if len(l) > 0 {
|
||||
pathList = append(pathList, l...)
|
||||
pathList := make([]*Path, 0, adj.Count(rfList))
|
||||
adj.walk(rfList, func(d *Destination) bool {
|
||||
for i, p := range d.knownPathList {
|
||||
n := p.Clone(false)
|
||||
n.MarkStale(true)
|
||||
n.SetRejected(p.IsRejected())
|
||||
d.knownPathList[i] = n
|
||||
if !n.IsRejected() {
|
||||
pathList = append(pathList, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
return pathList
|
||||
}
|
||||
|
||||
func (adj *AdjRib) MarkLLGRStaleOrDrop(rfList []bgp.RouteFamily) []*Path {
|
||||
pathList := make([]*Path, 0, adj.Count(rfList))
|
||||
adj.walk(rfList, func(d *Destination) bool {
|
||||
for i, p := range d.knownPathList {
|
||||
if p.HasNoLLGR() {
|
||||
n := p.Clone(true)
|
||||
n.SetDropped(true)
|
||||
pathList = append(pathList, n)
|
||||
} else {
|
||||
n := p.Clone(false)
|
||||
n.SetRejected(p.IsRejected())
|
||||
n.SetCommunities([]uint32{uint32(bgp.COMMUNITY_LLGR_STALE)}, false)
|
||||
if p.IsRejected() {
|
||||
d.knownPathList[i] = n
|
||||
} else {
|
||||
pathList = append(pathList, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
adj.Update(pathList)
|
||||
return pathList
|
||||
}
|
||||
|
||||
func (adj *AdjRib) Select(family bgp.RouteFamily, accepted bool, option ...TableSelectOption) (*Table, error) {
|
||||
m := make(map[string][]*Path)
|
||||
pl := adj.PathList([]bgp.RouteFamily{family}, accepted)
|
||||
for _, path := range pl {
|
||||
key := path.GetNlri().String()
|
||||
if _, y := m[key]; y {
|
||||
m[key] = append(m[key], path)
|
||||
} else {
|
||||
m[key] = []*Path{path}
|
||||
}
|
||||
t, ok := adj.table[family]
|
||||
if !ok {
|
||||
t = NewTable(family)
|
||||
}
|
||||
d := make([]*Destination, 0, len(pl))
|
||||
for _, l := range m {
|
||||
d = append(d, NewDestination(l[0].GetNlri(), 0, l...))
|
||||
}
|
||||
tbl := NewTable(family, d...)
|
||||
option = append(option, TableSelectOption{adj: true})
|
||||
return tbl.Select(option...)
|
||||
return t.Select(option...)
|
||||
}
|
||||
|
||||
func (adj *AdjRib) TableInfo(family bgp.RouteFamily) (*TableInfo, error) {
|
||||
|
||||
51
vendor/github.com/osrg/gobgp/internal/pkg/table/destination.go
generated
vendored
51
vendor/github.com/osrg/gobgp/internal/pkg/table/destination.go
generated
vendored
@@ -74,38 +74,6 @@ func (r *BestPathReason) String() string {
|
||||
return BestPathReasonStringMap[*r]
|
||||
}
|
||||
|
||||
func IpToRadixkey(b []byte, max uint8) string {
|
||||
var buffer bytes.Buffer
|
||||
for i := 0; i < len(b) && i < int(max); i++ {
|
||||
fmt.Fprintf(&buffer, "%08b", b[i])
|
||||
}
|
||||
return buffer.String()[:max]
|
||||
}
|
||||
|
||||
func CidrToRadixkey(cidr string) string {
|
||||
_, n, _ := net.ParseCIDR(cidr)
|
||||
ones, _ := n.Mask.Size()
|
||||
return IpToRadixkey(n.IP, uint8(ones))
|
||||
}
|
||||
|
||||
func AddrToRadixkey(addr bgp.AddrPrefixInterface) string {
|
||||
var (
|
||||
ip net.IP
|
||||
size uint8
|
||||
)
|
||||
switch T := addr.(type) {
|
||||
case *bgp.IPAddrPrefix:
|
||||
mask := net.CIDRMask(int(T.Length), net.IPv4len*8)
|
||||
ip, size = T.Prefix.Mask(mask).To4(), uint8(T.Length)
|
||||
case *bgp.IPv6AddrPrefix:
|
||||
mask := net.CIDRMask(int(T.Length), net.IPv6len*8)
|
||||
ip, size = T.Prefix.Mask(mask).To16(), uint8(T.Length)
|
||||
default:
|
||||
return CidrToRadixkey(addr.String())
|
||||
}
|
||||
return IpToRadixkey(ip, size)
|
||||
}
|
||||
|
||||
type PeerInfo struct {
|
||||
AS uint32
|
||||
ID net.IP
|
||||
@@ -265,7 +233,7 @@ func (dest *Destination) Calculate(newPath *Path) *Update {
|
||||
|
||||
if newPath.IsWithdraw {
|
||||
p := dest.explicitWithdraw(newPath)
|
||||
if p != nil {
|
||||
if p != nil && newPath.IsDropped() {
|
||||
if id := p.GetNlri().PathLocalIdentifier(); id != 0 {
|
||||
dest.localIdMap.Unflag(uint(id))
|
||||
}
|
||||
@@ -405,17 +373,19 @@ func (dest *Destination) computeKnownBestPath() (*Path, BestPathReason, error) {
|
||||
}
|
||||
return dest.knownPathList[0], BPR_ONLY_PATH, nil
|
||||
}
|
||||
dest.sort()
|
||||
reason := dest.sort()
|
||||
newBest := dest.knownPathList[0]
|
||||
// If the first path has the invalidated next-hop, which evaluated by IGP,
|
||||
// returns no path with the reason of the next-hop reachability.
|
||||
if dest.knownPathList[0].IsNexthopInvalid {
|
||||
return nil, BPR_REACHABLE_NEXT_HOP, nil
|
||||
}
|
||||
return newBest, newBest.reason, nil
|
||||
return newBest, reason, nil
|
||||
}
|
||||
|
||||
func (dst *Destination) sort() {
|
||||
func (dst *Destination) sort() BestPathReason {
|
||||
reason := BPR_UNKNOWN
|
||||
|
||||
sort.SliceStable(dst.knownPathList, func(i, j int) bool {
|
||||
//Compares given paths and returns best path.
|
||||
//
|
||||
@@ -451,7 +421,6 @@ func (dst *Destination) sort() {
|
||||
path2 := dst.knownPathList[j]
|
||||
|
||||
var better *Path
|
||||
reason := BPR_UNKNOWN
|
||||
|
||||
// draft-uttaro-idr-bgp-persistence-02
|
||||
if better == nil {
|
||||
@@ -501,7 +470,7 @@ func (dst *Destination) sort() {
|
||||
reason = BPR_OLDER
|
||||
}
|
||||
if better == nil {
|
||||
var e error = nil
|
||||
var e error
|
||||
better, e = compareByRouterID(path1, path2)
|
||||
if e != nil {
|
||||
log.WithFields(log.Fields{
|
||||
@@ -515,11 +484,9 @@ func (dst *Destination) sort() {
|
||||
reason = BPR_UNKNOWN
|
||||
better = path1
|
||||
}
|
||||
|
||||
better.reason = reason
|
||||
|
||||
return better == path1
|
||||
})
|
||||
return reason
|
||||
}
|
||||
|
||||
type Update struct {
|
||||
@@ -933,7 +900,7 @@ func compareByRouterID(path1, path2 *Path) (*Path, error) {
|
||||
}
|
||||
|
||||
if !SelectionOptions.ExternalCompareRouterId && path1.IsIBGP() != path2.IsIBGP() {
|
||||
return nil, fmt.Errorf("This method does not support comparing ebgp with ibgp path")
|
||||
return nil, fmt.Errorf("this method does not support comparing ebgp with ibgp path")
|
||||
}
|
||||
|
||||
// At least one path is not coming from NC, so we get local bgp id.
|
||||
|
||||
18
vendor/github.com/osrg/gobgp/internal/pkg/table/message.go
generated
vendored
18
vendor/github.com/osrg/gobgp/internal/pkg/table/message.go
generated
vendored
@@ -85,9 +85,9 @@ func UpdatePathAttrs4ByteAs(msg *bgp.BGPUpdate) error {
|
||||
asAttrPos := 0
|
||||
as4AttrPos := 0
|
||||
for i, attr := range msg.PathAttributes {
|
||||
switch attr.(type) {
|
||||
switch a := attr.(type) {
|
||||
case *bgp.PathAttributeAsPath:
|
||||
asAttr = attr.(*bgp.PathAttributeAsPath)
|
||||
asAttr = a
|
||||
for j, param := range asAttr.Value {
|
||||
as2Param, ok := param.(*bgp.AsPathParam)
|
||||
if ok {
|
||||
@@ -103,7 +103,7 @@ func UpdatePathAttrs4ByteAs(msg *bgp.BGPUpdate) error {
|
||||
msg.PathAttributes[i] = asAttr
|
||||
case *bgp.PathAttributeAs4Path:
|
||||
as4AttrPos = i
|
||||
as4Attr = attr.(*bgp.PathAttributeAs4Path)
|
||||
as4Attr = a
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,8 +130,9 @@ func UpdatePathAttrs4ByteAs(msg *bgp.BGPUpdate) error {
|
||||
}
|
||||
|
||||
as4Len := 0
|
||||
as4Params := make([]bgp.AsPathParamInterface, 0, len(as4Attr.Value))
|
||||
var as4Params []bgp.AsPathParamInterface
|
||||
if as4Attr != nil {
|
||||
as4Params = make([]bgp.AsPathParamInterface, 0, len(as4Attr.Value))
|
||||
for _, p := range as4Attr.Value {
|
||||
// RFC 6793 6. Error Handling
|
||||
//
|
||||
@@ -211,9 +212,8 @@ func UpdatePathAggregator2ByteAs(msg *bgp.BGPUpdate) {
|
||||
as := uint32(0)
|
||||
var addr string
|
||||
for i, attr := range msg.PathAttributes {
|
||||
switch attr.(type) {
|
||||
switch agg := attr.(type) {
|
||||
case *bgp.PathAttributeAggregator:
|
||||
agg := attr.(*bgp.PathAttributeAggregator)
|
||||
addr = agg.Value.Address.String()
|
||||
if agg.Value.AS > (1<<16)-1 {
|
||||
as = agg.Value.AS
|
||||
@@ -233,15 +233,15 @@ func UpdatePathAggregator4ByteAs(msg *bgp.BGPUpdate) error {
|
||||
var agg4Attr *bgp.PathAttributeAs4Aggregator
|
||||
agg4AttrPos := 0
|
||||
for i, attr := range msg.PathAttributes {
|
||||
switch attr.(type) {
|
||||
switch agg := attr.(type) {
|
||||
case *bgp.PathAttributeAggregator:
|
||||
attr := attr.(*bgp.PathAttributeAggregator)
|
||||
attr := agg
|
||||
if attr.Value.Askind == reflect.Uint16 {
|
||||
aggAttr = attr
|
||||
aggAttr.Value.Askind = reflect.Uint32
|
||||
}
|
||||
case *bgp.PathAttributeAs4Aggregator:
|
||||
agg4Attr = attr.(*bgp.PathAttributeAs4Aggregator)
|
||||
agg4Attr = agg
|
||||
agg4AttrPos = i
|
||||
}
|
||||
}
|
||||
|
||||
134
vendor/github.com/osrg/gobgp/internal/pkg/table/path.go
generated
vendored
134
vendor/github.com/osrg/gobgp/internal/pkg/table/path.go
generated
vendored
@@ -89,7 +89,6 @@ type originInfo struct {
|
||||
nlri bgp.AddrPrefixInterface
|
||||
source *PeerInfo
|
||||
timestamp int64
|
||||
validation *Validation
|
||||
noImplicitWithdraw bool
|
||||
isFromExternal bool
|
||||
eor bool
|
||||
@@ -138,15 +137,21 @@ type Path struct {
|
||||
pathAttrs []bgp.PathAttributeInterface
|
||||
dels []bgp.BGPAttrType
|
||||
attrsHash uint32
|
||||
aslooped bool
|
||||
reason BestPathReason
|
||||
rejected bool
|
||||
// doesn't exist in the adj
|
||||
dropped bool
|
||||
|
||||
// For BGP Nexthop Tracking, this field shows if nexthop is invalidated by IGP.
|
||||
IsNexthopInvalid bool
|
||||
IsWithdraw bool
|
||||
}
|
||||
|
||||
var localSource = &PeerInfo{}
|
||||
|
||||
func NewPath(source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, pattrs []bgp.PathAttributeInterface, timestamp time.Time, noImplicitWithdraw bool) *Path {
|
||||
if source == nil {
|
||||
source = localSource
|
||||
}
|
||||
if !isWithdraw && pattrs == nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Table",
|
||||
@@ -323,7 +328,8 @@ func (path *Path) IsLocal() bool {
|
||||
}
|
||||
|
||||
func (path *Path) IsIBGP() bool {
|
||||
return path.GetSource().AS == path.GetSource().LocalAS
|
||||
as := path.GetSource().AS
|
||||
return (as == path.GetSource().LocalAS) && as != 0
|
||||
}
|
||||
|
||||
// create new PathAttributes
|
||||
@@ -352,22 +358,6 @@ func (path *Path) NoImplicitWithdraw() bool {
|
||||
return path.OriginInfo().noImplicitWithdraw
|
||||
}
|
||||
|
||||
func (path *Path) Validation() *Validation {
|
||||
return path.OriginInfo().validation
|
||||
}
|
||||
|
||||
func (path *Path) ValidationStatus() config.RpkiValidationResultType {
|
||||
if v := path.OriginInfo().validation; v != nil {
|
||||
return v.Status
|
||||
} else {
|
||||
return config.RPKI_VALIDATION_RESULT_TYPE_NONE
|
||||
}
|
||||
}
|
||||
|
||||
func (path *Path) SetValidation(v *Validation) {
|
||||
path.OriginInfo().validation = v
|
||||
}
|
||||
|
||||
func (path *Path) IsFromExternal() bool {
|
||||
return path.OriginInfo().isFromExternal
|
||||
}
|
||||
@@ -380,9 +370,6 @@ func (path *Path) GetRouteFamily() bgp.RouteFamily {
|
||||
return bgp.AfiSafiToRouteFamily(path.OriginInfo().nlri.AFI(), path.OriginInfo().nlri.SAFI())
|
||||
}
|
||||
|
||||
func (path *Path) SetSource(source *PeerInfo) {
|
||||
path.OriginInfo().source = source
|
||||
}
|
||||
func (path *Path) GetSource() *PeerInfo {
|
||||
return path.OriginInfo().source
|
||||
}
|
||||
@@ -395,12 +382,29 @@ func (path *Path) IsStale() bool {
|
||||
return path.OriginInfo().stale
|
||||
}
|
||||
|
||||
func (path *Path) IsAsLooped() bool {
|
||||
return path.aslooped
|
||||
func (path *Path) IsRejected() bool {
|
||||
return path.rejected
|
||||
}
|
||||
|
||||
func (path *Path) SetAsLooped(y bool) {
|
||||
path.aslooped = y
|
||||
func (path *Path) SetRejected(y bool) {
|
||||
path.rejected = y
|
||||
}
|
||||
|
||||
func (path *Path) IsDropped() bool {
|
||||
return path.dropped
|
||||
}
|
||||
|
||||
func (path *Path) SetDropped(y bool) {
|
||||
path.dropped = y
|
||||
}
|
||||
|
||||
func (path *Path) HasNoLLGR() bool {
|
||||
for _, c := range path.GetCommunities() {
|
||||
if c == uint32(bgp.COMMUNITY_NO_LLGR) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (path *Path) IsLLGRStale() bool {
|
||||
@@ -897,6 +901,12 @@ func (path *Path) GetLargeCommunities() []*bgp.LargeCommunity {
|
||||
}
|
||||
|
||||
func (path *Path) SetLargeCommunities(cs []*bgp.LargeCommunity, doReplace bool) {
|
||||
if len(cs) == 0 && doReplace {
|
||||
// clear large communities
|
||||
path.delPathAttr(bgp.BGP_ATTR_TYPE_LARGE_COMMUNITY)
|
||||
return
|
||||
}
|
||||
|
||||
a := path.getPathAttr(bgp.BGP_ATTR_TYPE_LARGE_COMMUNITY)
|
||||
if a == nil || doReplace {
|
||||
path.setPathAttr(bgp.NewPathAttributeLargeCommunities(cs))
|
||||
@@ -1016,7 +1026,6 @@ func (path *Path) MarshalJSON() ([]byte, error) {
|
||||
PathAttrs: path.GetPathAttrs(),
|
||||
Age: path.GetTimestamp().Unix(),
|
||||
Withdrawal: path.IsWithdraw,
|
||||
Validation: string(path.ValidationStatus()),
|
||||
SourceID: path.GetSource().ID,
|
||||
NeighborIP: path.GetSource().Address,
|
||||
Stale: path.IsStale(),
|
||||
@@ -1066,12 +1075,22 @@ func (v *Vrf) ToGlobalPath(path *Path) error {
|
||||
case bgp.RF_IPv4_UC:
|
||||
n := nlri.(*bgp.IPAddrPrefix)
|
||||
pathIdentifier := path.GetNlri().PathIdentifier()
|
||||
path.OriginInfo().nlri = bgp.NewLabeledVPNIPAddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(0), v.Rd)
|
||||
path.OriginInfo().nlri = bgp.NewLabeledVPNIPAddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(v.MplsLabel), v.Rd)
|
||||
path.GetNlri().SetPathIdentifier(pathIdentifier)
|
||||
case bgp.RF_FS_IPv4_UC:
|
||||
n := nlri.(*bgp.FlowSpecIPv4Unicast)
|
||||
pathIdentifier := path.GetNlri().PathIdentifier()
|
||||
path.OriginInfo().nlri = bgp.NewFlowSpecIPv4VPN(v.Rd, n.FlowSpecNLRI.Value)
|
||||
path.GetNlri().SetPathIdentifier(pathIdentifier)
|
||||
case bgp.RF_IPv6_UC:
|
||||
n := nlri.(*bgp.IPv6AddrPrefix)
|
||||
pathIdentifier := path.GetNlri().PathIdentifier()
|
||||
path.OriginInfo().nlri = bgp.NewLabeledVPNIPv6AddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(0), v.Rd)
|
||||
path.OriginInfo().nlri = bgp.NewLabeledVPNIPv6AddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(v.MplsLabel), v.Rd)
|
||||
path.GetNlri().SetPathIdentifier(pathIdentifier)
|
||||
case bgp.RF_FS_IPv6_UC:
|
||||
n := nlri.(*bgp.FlowSpecIPv6Unicast)
|
||||
pathIdentifier := path.GetNlri().PathIdentifier()
|
||||
path.OriginInfo().nlri = bgp.NewFlowSpecIPv6VPN(v.Rd, n.FlowSpecNLRI.Value)
|
||||
path.GetNlri().SetPathIdentifier(pathIdentifier)
|
||||
case bgp.RF_EVPN:
|
||||
n := nlri.(*bgp.EVPNNLRI)
|
||||
@@ -1095,11 +1114,11 @@ func (p *Path) ToGlobal(vrf *Vrf) *Path {
|
||||
switch rf := p.GetRouteFamily(); rf {
|
||||
case bgp.RF_IPv4_UC:
|
||||
n := nlri.(*bgp.IPAddrPrefix)
|
||||
nlri = bgp.NewLabeledVPNIPAddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(0), vrf.Rd)
|
||||
nlri = bgp.NewLabeledVPNIPAddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(vrf.MplsLabel), vrf.Rd)
|
||||
nlri.SetPathIdentifier(pathId)
|
||||
case bgp.RF_IPv6_UC:
|
||||
n := nlri.(*bgp.IPv6AddrPrefix)
|
||||
nlri = bgp.NewLabeledVPNIPv6AddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(0), vrf.Rd)
|
||||
nlri = bgp.NewLabeledVPNIPv6AddrPrefix(n.Length, n.Prefix.String(), *bgp.NewMPLSLabelStack(vrf.MplsLabel), vrf.Rd)
|
||||
nlri.SetPathIdentifier(pathId)
|
||||
case bgp.RF_EVPN:
|
||||
n := nlri.(*bgp.EVPNNLRI)
|
||||
@@ -1134,7 +1153,6 @@ func (p *Path) ToGlobal(vrf *Vrf) *Path {
|
||||
path.SetExtCommunities(vrf.ExportRt, false)
|
||||
path.delPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP)
|
||||
path.setPathAttr(bgp.NewPathAttributeMpReachNLRI(nh.String(), []bgp.AddrPrefixInterface{nlri}))
|
||||
path.IsNexthopInvalid = p.IsNexthopInvalid
|
||||
return path
|
||||
}
|
||||
|
||||
@@ -1149,17 +1167,39 @@ func (p *Path) ToLocal() *Path {
|
||||
ones, _ := c.Mask.Size()
|
||||
nlri = bgp.NewIPAddrPrefix(uint8(ones), c.IP.String())
|
||||
nlri.SetPathLocalIdentifier(pathId)
|
||||
case bgp.RF_FS_IPv4_VPN:
|
||||
n := nlri.(*bgp.FlowSpecIPv4VPN)
|
||||
nlri = bgp.NewFlowSpecIPv4Unicast(n.FlowSpecNLRI.Value)
|
||||
nlri.SetPathLocalIdentifier(pathId)
|
||||
case bgp.RF_IPv6_VPN:
|
||||
n := nlri.(*bgp.LabeledVPNIPv6AddrPrefix)
|
||||
_, c, _ := net.ParseCIDR(n.IPPrefix())
|
||||
ones, _ := c.Mask.Size()
|
||||
nlri = bgp.NewIPv6AddrPrefix(uint8(ones), c.IP.String())
|
||||
nlri.SetPathLocalIdentifier(pathId)
|
||||
case bgp.RF_FS_IPv6_VPN:
|
||||
n := nlri.(*bgp.FlowSpecIPv6VPN)
|
||||
nlri = bgp.NewFlowSpecIPv6Unicast(n.FlowSpecNLRI.Value)
|
||||
nlri.SetPathLocalIdentifier(pathId)
|
||||
default:
|
||||
return p
|
||||
}
|
||||
path := NewPath(p.OriginInfo().source, nlri, p.IsWithdraw, p.GetPathAttrs(), p.GetTimestamp(), false)
|
||||
path.delPathAttr(bgp.BGP_ATTR_TYPE_EXTENDED_COMMUNITIES)
|
||||
switch f {
|
||||
case bgp.RF_IPv4_VPN, bgp.RF_IPv6_VPN:
|
||||
path.delPathAttr(bgp.BGP_ATTR_TYPE_EXTENDED_COMMUNITIES)
|
||||
case bgp.RF_FS_IPv4_VPN, bgp.RF_FS_IPv6_VPN:
|
||||
extcomms := path.GetExtCommunities()
|
||||
newExtComms := make([]bgp.ExtendedCommunityInterface, 0, len(extcomms))
|
||||
for _, extComm := range extcomms {
|
||||
_, subType := extComm.GetTypes()
|
||||
if subType == bgp.EC_SUBTYPE_ROUTE_TARGET {
|
||||
continue
|
||||
}
|
||||
newExtComms = append(newExtComms, extComm)
|
||||
}
|
||||
path.SetExtCommunities(newExtComms, true)
|
||||
}
|
||||
|
||||
if f == bgp.RF_IPv4_VPN {
|
||||
nh := path.GetNexthop()
|
||||
@@ -1177,3 +1217,29 @@ func (p *Path) SetHash(v uint32) {
|
||||
func (p *Path) GetHash() uint32 {
|
||||
return p.attrsHash
|
||||
}
|
||||
|
||||
func nlriToIPNet(nlri bgp.AddrPrefixInterface) *net.IPNet {
|
||||
switch T := nlri.(type) {
|
||||
case *bgp.IPAddrPrefix:
|
||||
return &net.IPNet{
|
||||
IP: net.IP(T.Prefix.To4()),
|
||||
Mask: net.CIDRMask(int(T.Length), 32),
|
||||
}
|
||||
case *bgp.IPv6AddrPrefix:
|
||||
return &net.IPNet{
|
||||
IP: net.IP(T.Prefix.To16()),
|
||||
Mask: net.CIDRMask(int(T.Length), 128),
|
||||
}
|
||||
case *bgp.LabeledIPAddrPrefix:
|
||||
return &net.IPNet{
|
||||
IP: net.IP(T.Prefix.To4()),
|
||||
Mask: net.CIDRMask(int(T.Length), 32),
|
||||
}
|
||||
case *bgp.LabeledIPv6AddrPrefix:
|
||||
return &net.IPNet{
|
||||
IP: net.IP(T.Prefix.To4()),
|
||||
Mask: net.CIDRMask(int(T.Length), 128),
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
525
vendor/github.com/osrg/gobgp/internal/pkg/table/policy.go
generated
vendored
525
vendor/github.com/osrg/gobgp/internal/pkg/table/policy.go
generated
vendored
@@ -16,7 +16,6 @@
|
||||
package table
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -27,17 +26,17 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/k-sone/critbitgo"
|
||||
api "github.com/osrg/gobgp/api"
|
||||
"github.com/osrg/gobgp/internal/pkg/config"
|
||||
"github.com/osrg/gobgp/pkg/packet/bgp"
|
||||
|
||||
radix "github.com/armon/go-radix"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type PolicyOptions struct {
|
||||
Info *PeerInfo
|
||||
ValidationResult *Validation
|
||||
OldNextHop net.IP
|
||||
Info *PeerInfo
|
||||
OldNextHop net.IP
|
||||
Validate func(*Path) *Validation
|
||||
}
|
||||
|
||||
type DefinedType int
|
||||
@@ -353,7 +352,7 @@ func NewPrefix(c config.Prefix) (*Prefix, error) {
|
||||
|
||||
type PrefixSet struct {
|
||||
name string
|
||||
tree *radix.Tree
|
||||
tree *critbitgo.Net
|
||||
family bgp.RouteFamily
|
||||
}
|
||||
|
||||
@@ -370,29 +369,25 @@ func (lhs *PrefixSet) Append(arg DefinedSet) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("type cast failed")
|
||||
}
|
||||
// if either is empty, family can be ignored.
|
||||
if lhs.tree.Len() != 0 && rhs.tree.Len() != 0 {
|
||||
_, w, _ := lhs.tree.Minimum()
|
||||
l := w.([]*Prefix)
|
||||
_, v, _ := rhs.tree.Minimum()
|
||||
r := v.([]*Prefix)
|
||||
if l[0].AddressFamily != r[0].AddressFamily {
|
||||
return fmt.Errorf("can't append different family")
|
||||
}
|
||||
|
||||
if rhs.tree.Size() == 0 {
|
||||
// if try to append an empty set, then return directly
|
||||
return nil
|
||||
} else if lhs.tree.Size() != 0 && rhs.family != lhs.family {
|
||||
return fmt.Errorf("can't append different family")
|
||||
}
|
||||
rhs.tree.Walk(func(key string, v interface{}) bool {
|
||||
w, ok := lhs.tree.Get(key)
|
||||
rhs.tree.Walk(nil, func(r *net.IPNet, v interface{}) bool {
|
||||
w, ok, _ := lhs.tree.Get(r)
|
||||
if ok {
|
||||
r := v.([]*Prefix)
|
||||
l := w.([]*Prefix)
|
||||
lhs.tree.Insert(key, append(l, r...))
|
||||
rp := v.([]*Prefix)
|
||||
lp := w.([]*Prefix)
|
||||
lhs.tree.Add(r, append(lp, rp...))
|
||||
} else {
|
||||
lhs.tree.Insert(key, v)
|
||||
lhs.tree.Add(r, v)
|
||||
}
|
||||
return false
|
||||
return true
|
||||
})
|
||||
_, w, _ := lhs.tree.Minimum()
|
||||
lhs.family = w.([]*Prefix)[0].AddressFamily
|
||||
lhs.family = rhs.family
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -401,17 +396,17 @@ func (lhs *PrefixSet) Remove(arg DefinedSet) error {
|
||||
if !ok {
|
||||
return fmt.Errorf("type cast failed")
|
||||
}
|
||||
rhs.tree.Walk(func(key string, v interface{}) bool {
|
||||
w, ok := lhs.tree.Get(key)
|
||||
rhs.tree.Walk(nil, func(r *net.IPNet, v interface{}) bool {
|
||||
w, ok, _ := lhs.tree.Get(r)
|
||||
if !ok {
|
||||
return false
|
||||
return true
|
||||
}
|
||||
r := v.([]*Prefix)
|
||||
l := w.([]*Prefix)
|
||||
new := make([]*Prefix, 0, len(l))
|
||||
for _, lp := range l {
|
||||
rp := v.([]*Prefix)
|
||||
lp := w.([]*Prefix)
|
||||
new := make([]*Prefix, 0, len(lp))
|
||||
for _, lp := range lp {
|
||||
delete := false
|
||||
for _, rp := range r {
|
||||
for _, rp := range rp {
|
||||
if lp.Equal(rp) {
|
||||
delete = true
|
||||
break
|
||||
@@ -422,11 +417,11 @@ func (lhs *PrefixSet) Remove(arg DefinedSet) error {
|
||||
}
|
||||
}
|
||||
if len(new) == 0 {
|
||||
lhs.tree.Delete(key)
|
||||
lhs.tree.Delete(r)
|
||||
} else {
|
||||
lhs.tree.Insert(key, new)
|
||||
lhs.tree.Add(r, new)
|
||||
}
|
||||
return false
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
}
|
||||
@@ -443,24 +438,24 @@ func (lhs *PrefixSet) Replace(arg DefinedSet) error {
|
||||
|
||||
func (s *PrefixSet) List() []string {
|
||||
var list []string
|
||||
s.tree.Walk(func(s string, v interface{}) bool {
|
||||
s.tree.Walk(nil, func(_ *net.IPNet, v interface{}) bool {
|
||||
ps := v.([]*Prefix)
|
||||
for _, p := range ps {
|
||||
list = append(list, fmt.Sprintf("%s %d..%d", p.PrefixString(), p.MasklengthRangeMin, p.MasklengthRangeMax))
|
||||
}
|
||||
return false
|
||||
return true
|
||||
})
|
||||
return list
|
||||
}
|
||||
|
||||
func (s *PrefixSet) ToConfig() *config.PrefixSet {
|
||||
list := make([]config.Prefix, 0, s.tree.Len())
|
||||
s.tree.Walk(func(s string, v interface{}) bool {
|
||||
list := make([]config.Prefix, 0, s.tree.Size())
|
||||
s.tree.Walk(nil, func(_ *net.IPNet, v interface{}) bool {
|
||||
ps := v.([]*Prefix)
|
||||
for _, p := range ps {
|
||||
list = append(list, config.Prefix{IpPrefix: p.PrefixString(), MasklengthRange: fmt.Sprintf("%d..%d", p.MasklengthRangeMin, p.MasklengthRangeMax)})
|
||||
}
|
||||
return false
|
||||
return true
|
||||
})
|
||||
return &config.PrefixSet{
|
||||
PrefixSetName: s.name,
|
||||
@@ -480,7 +475,7 @@ func NewPrefixSetFromApiStruct(name string, prefixes []*Prefix) (*PrefixSet, err
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("empty prefix set name")
|
||||
}
|
||||
tree := radix.New()
|
||||
tree := critbitgo.NewNet()
|
||||
var family bgp.RouteFamily
|
||||
for i, x := range prefixes {
|
||||
if i == 0 {
|
||||
@@ -488,13 +483,12 @@ func NewPrefixSetFromApiStruct(name string, prefixes []*Prefix) (*PrefixSet, err
|
||||
} else if family != x.AddressFamily {
|
||||
return nil, fmt.Errorf("multiple families")
|
||||
}
|
||||
key := CidrToRadixkey(x.Prefix.String())
|
||||
d, ok := tree.Get(key)
|
||||
d, ok, _ := tree.Get(x.Prefix)
|
||||
if ok {
|
||||
ps := d.([]*Prefix)
|
||||
tree.Insert(key, append(ps, x))
|
||||
tree.Add(x.Prefix, append(ps, x))
|
||||
} else {
|
||||
tree.Insert(key, []*Prefix{x})
|
||||
tree.Add(x.Prefix, []*Prefix{x})
|
||||
}
|
||||
}
|
||||
return &PrefixSet{
|
||||
@@ -512,7 +506,7 @@ func NewPrefixSet(c config.PrefixSet) (*PrefixSet, error) {
|
||||
}
|
||||
return nil, fmt.Errorf("empty prefix set name")
|
||||
}
|
||||
tree := radix.New()
|
||||
tree := critbitgo.NewNet()
|
||||
var family bgp.RouteFamily
|
||||
for i, x := range c.PrefixList {
|
||||
y, err := NewPrefix(x)
|
||||
@@ -524,13 +518,12 @@ func NewPrefixSet(c config.PrefixSet) (*PrefixSet, error) {
|
||||
} else if family != y.AddressFamily {
|
||||
return nil, fmt.Errorf("multiple families")
|
||||
}
|
||||
key := CidrToRadixkey(y.Prefix.String())
|
||||
d, ok := tree.Get(key)
|
||||
d, ok, _ := tree.Get(y.Prefix)
|
||||
if ok {
|
||||
ps := d.([]*Prefix)
|
||||
tree.Insert(key, append(ps, y))
|
||||
tree.Add(y.Prefix, append(ps, y))
|
||||
} else {
|
||||
tree.Insert(key, []*Prefix{y})
|
||||
tree.Add(y.Prefix, []*Prefix{y})
|
||||
}
|
||||
}
|
||||
return &PrefixSet{
|
||||
@@ -819,7 +812,7 @@ func (m *singleAsPathMatch) Match(aspath []uint32) bool {
|
||||
}
|
||||
|
||||
var (
|
||||
_regexpLeftMostRe = regexp.MustCompile(`$\^([0-9]+)_^`)
|
||||
_regexpLeftMostRe = regexp.MustCompile(`^\^([0-9]+)_$`)
|
||||
_regexpOriginRe = regexp.MustCompile(`^_([0-9]+)\$$`)
|
||||
_regexpIncludeRe = regexp.MustCompile("^_([0-9]+)_$")
|
||||
_regexpOnlyRe = regexp.MustCompile(`^\^([0-9]+)\$$`)
|
||||
@@ -1446,33 +1439,15 @@ func (c *PrefixCondition) Option() MatchOption {
|
||||
// subsequent comparison is skipped if that matches the conditions.
|
||||
// If PrefixList's length is zero, return true.
|
||||
func (c *PrefixCondition) Evaluate(path *Path, _ *PolicyOptions) bool {
|
||||
var key string
|
||||
var masklen uint8
|
||||
keyf := func(ip net.IP, ones int) string {
|
||||
var buffer bytes.Buffer
|
||||
for i := 0; i < len(ip) && i < ones; i++ {
|
||||
buffer.WriteString(fmt.Sprintf("%08b", ip[i]))
|
||||
}
|
||||
return buffer.String()[:ones]
|
||||
}
|
||||
family := path.GetRouteFamily()
|
||||
switch family {
|
||||
case bgp.RF_IPv4_UC:
|
||||
masklen = path.GetNlri().(*bgp.IPAddrPrefix).Length
|
||||
key = keyf(path.GetNlri().(*bgp.IPAddrPrefix).Prefix, int(masklen))
|
||||
case bgp.RF_IPv6_UC:
|
||||
masklen = path.GetNlri().(*bgp.IPv6AddrPrefix).Length
|
||||
key = keyf(path.GetNlri().(*bgp.IPv6AddrPrefix).Prefix, int(masklen))
|
||||
default:
|
||||
return false
|
||||
}
|
||||
if family != c.set.family {
|
||||
if path.GetRouteFamily() != c.set.family {
|
||||
return false
|
||||
}
|
||||
|
||||
r := nlriToIPNet(path.GetNlri())
|
||||
ones, _ := r.Mask.Size()
|
||||
masklen := uint8(ones)
|
||||
result := false
|
||||
_, ps, ok := c.set.tree.LongestPrefix(key)
|
||||
if ok {
|
||||
if _, ps, _ := c.set.tree.Match(r); ps != nil {
|
||||
for _, p := range ps.([]*Prefix) {
|
||||
if p.MasklengthRangeMin <= masklen && masklen <= p.MasklengthRangeMax {
|
||||
result = true
|
||||
@@ -1899,8 +1874,8 @@ func (c *RpkiValidationCondition) Type() ConditionType {
|
||||
}
|
||||
|
||||
func (c *RpkiValidationCondition) Evaluate(path *Path, options *PolicyOptions) bool {
|
||||
if options != nil && options.ValidationResult != nil {
|
||||
return c.result == options.ValidationResult.Status
|
||||
if options != nil && options.Validate != nil {
|
||||
return c.result == options.Validate(path).Status
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -2536,7 +2511,7 @@ func (a *AsPathPrependAction) Apply(path *Path, option *PolicyOptions) *Path {
|
||||
asn = a.asn
|
||||
}
|
||||
|
||||
confed := option != nil && option.Info.Confederation
|
||||
confed := option != nil && option.Info != nil && option.Info.Confederation
|
||||
path.PrependAsn(asn, a.repeat, confed)
|
||||
|
||||
return path
|
||||
@@ -2580,7 +2555,7 @@ func NewAsPathPrependAction(action config.SetAsPathPrepend) (*AsPathPrependActio
|
||||
default:
|
||||
asn, err := strconv.ParseUint(action.As, 10, 32)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("As number string invalid")
|
||||
return nil, fmt.Errorf("AS number string invalid")
|
||||
}
|
||||
a.asn = uint32(asn)
|
||||
}
|
||||
@@ -2686,39 +2661,28 @@ func (s *Statement) ToConfig() *config.Statement {
|
||||
Conditions: func() config.Conditions {
|
||||
cond := config.Conditions{}
|
||||
for _, c := range s.Conditions {
|
||||
switch c.(type) {
|
||||
switch v := c.(type) {
|
||||
case *PrefixCondition:
|
||||
v := c.(*PrefixCondition)
|
||||
cond.MatchPrefixSet = config.MatchPrefixSet{PrefixSet: v.set.Name(), MatchSetOptions: v.option.ConvertToMatchSetOptionsRestrictedType()}
|
||||
case *NeighborCondition:
|
||||
v := c.(*NeighborCondition)
|
||||
cond.MatchNeighborSet = config.MatchNeighborSet{NeighborSet: v.set.Name(), MatchSetOptions: v.option.ConvertToMatchSetOptionsRestrictedType()}
|
||||
case *AsPathLengthCondition:
|
||||
v := c.(*AsPathLengthCondition)
|
||||
cond.BgpConditions.AsPathLength = config.AsPathLength{Operator: config.IntToAttributeComparisonMap[int(v.operator)], Value: v.length}
|
||||
case *AsPathCondition:
|
||||
v := c.(*AsPathCondition)
|
||||
cond.BgpConditions.MatchAsPathSet = config.MatchAsPathSet{AsPathSet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsTypeMap[int(v.option)]}
|
||||
case *CommunityCondition:
|
||||
v := c.(*CommunityCondition)
|
||||
cond.BgpConditions.MatchCommunitySet = config.MatchCommunitySet{CommunitySet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsTypeMap[int(v.option)]}
|
||||
case *ExtCommunityCondition:
|
||||
v := c.(*ExtCommunityCondition)
|
||||
cond.BgpConditions.MatchExtCommunitySet = config.MatchExtCommunitySet{ExtCommunitySet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsTypeMap[int(v.option)]}
|
||||
case *LargeCommunityCondition:
|
||||
v := c.(*LargeCommunityCondition)
|
||||
cond.BgpConditions.MatchLargeCommunitySet = config.MatchLargeCommunitySet{LargeCommunitySet: v.set.Name(), MatchSetOptions: config.IntToMatchSetOptionsTypeMap[int(v.option)]}
|
||||
case *NextHopCondition:
|
||||
v := c.(*NextHopCondition)
|
||||
cond.BgpConditions.NextHopInList = v.set.List()
|
||||
case *RpkiValidationCondition:
|
||||
v := c.(*RpkiValidationCondition)
|
||||
cond.BgpConditions.RpkiValidationResult = v.result
|
||||
case *RouteTypeCondition:
|
||||
v := c.(*RouteTypeCondition)
|
||||
cond.BgpConditions.RouteType = v.typ
|
||||
case *AfiSafiInCondition:
|
||||
v := c.(*AfiSafiInCondition)
|
||||
res := make([]config.AfiSafiType, 0, len(v.routeFamilies))
|
||||
for _, rf := range v.routeFamilies {
|
||||
res = append(res, config.AfiSafiType(rf.String()))
|
||||
@@ -2741,21 +2705,21 @@ func (s *Statement) ToConfig() *config.Statement {
|
||||
act.RouteDisposition = config.ROUTE_DISPOSITION_NONE
|
||||
}
|
||||
for _, a := range s.ModActions {
|
||||
switch a.(type) {
|
||||
switch v := a.(type) {
|
||||
case *AsPathPrependAction:
|
||||
act.BgpActions.SetAsPathPrepend = *a.(*AsPathPrependAction).ToConfig()
|
||||
act.BgpActions.SetAsPathPrepend = *v.ToConfig()
|
||||
case *CommunityAction:
|
||||
act.BgpActions.SetCommunity = *a.(*CommunityAction).ToConfig()
|
||||
act.BgpActions.SetCommunity = *v.ToConfig()
|
||||
case *ExtCommunityAction:
|
||||
act.BgpActions.SetExtCommunity = *a.(*ExtCommunityAction).ToConfig()
|
||||
act.BgpActions.SetExtCommunity = *v.ToConfig()
|
||||
case *LargeCommunityAction:
|
||||
act.BgpActions.SetLargeCommunity = *a.(*LargeCommunityAction).ToConfig()
|
||||
act.BgpActions.SetLargeCommunity = *v.ToConfig()
|
||||
case *MedAction:
|
||||
act.BgpActions.SetMed = a.(*MedAction).ToConfig()
|
||||
act.BgpActions.SetMed = v.ToConfig()
|
||||
case *LocalPrefAction:
|
||||
act.BgpActions.SetLocalPref = a.(*LocalPrefAction).ToConfig()
|
||||
act.BgpActions.SetLocalPref = v.ToConfig()
|
||||
case *NexthopAction:
|
||||
act.BgpActions.SetNextHop = a.(*NexthopAction).ToConfig()
|
||||
act.BgpActions.SetNextHop = v.ToConfig()
|
||||
}
|
||||
}
|
||||
return act
|
||||
@@ -3114,9 +3078,6 @@ type RoutingPolicy struct {
|
||||
}
|
||||
|
||||
func (r *RoutingPolicy) ApplyPolicy(id string, dir PolicyDirection, before *Path, options *PolicyOptions) *Path {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
if before == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -3126,6 +3087,10 @@ func (r *RoutingPolicy) ApplyPolicy(id string, dir PolicyDirection, before *Path
|
||||
}
|
||||
result := ROUTE_TYPE_NONE
|
||||
after := before
|
||||
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
for _, p := range r.getPolicy(id, dir) {
|
||||
result, after = p.Apply(after, options)
|
||||
if result != ROUTE_TYPE_NONE {
|
||||
@@ -3408,13 +3373,13 @@ func (r *RoutingPolicy) reload(c config.RoutingPolicy) error {
|
||||
return err
|
||||
}
|
||||
if _, ok := pmap[y.Name]; ok {
|
||||
return fmt.Errorf("duplicated policy name. policy name must be unique.")
|
||||
return fmt.Errorf("duplicated policy name. policy name must be unique")
|
||||
}
|
||||
pmap[y.Name] = y
|
||||
for _, s := range y.Statements {
|
||||
_, ok := smap[s.Name]
|
||||
if ok {
|
||||
return fmt.Errorf("duplicated statement name. statement name must be unique.")
|
||||
return fmt.Errorf("duplicated statement name. statement name must be unique")
|
||||
}
|
||||
smap[s.Name] = s
|
||||
}
|
||||
@@ -3445,18 +3410,24 @@ func (r *RoutingPolicy) reload(c config.RoutingPolicy) error {
|
||||
}
|
||||
|
||||
func (r *RoutingPolicy) GetDefinedSet(typ DefinedType, name string) (*config.DefinedSets, error) {
|
||||
r.mu.RLock()
|
||||
dl, err := func() (DefinedSetList, error) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
set, ok := r.definedSetMap[typ]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid defined-set type: %d", typ)
|
||||
}
|
||||
set, ok := r.definedSetMap[typ]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid defined-set type: %d", typ)
|
||||
}
|
||||
|
||||
var dl DefinedSetList
|
||||
for _, s := range set {
|
||||
dl = append(dl, s)
|
||||
var dl DefinedSetList
|
||||
for _, s := range set {
|
||||
dl = append(dl, s)
|
||||
}
|
||||
return dl, nil
|
||||
}()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
|
||||
sort.Sort(dl)
|
||||
|
||||
@@ -3474,19 +3445,19 @@ func (r *RoutingPolicy) GetDefinedSet(typ DefinedType, name string) (*config.Def
|
||||
if name != "" && s.Name() != name {
|
||||
continue
|
||||
}
|
||||
switch s.(type) {
|
||||
switch v := s.(type) {
|
||||
case *PrefixSet:
|
||||
sets.PrefixSets = append(sets.PrefixSets, *s.(*PrefixSet).ToConfig())
|
||||
sets.PrefixSets = append(sets.PrefixSets, *v.ToConfig())
|
||||
case *NeighborSet:
|
||||
sets.NeighborSets = append(sets.NeighborSets, *s.(*NeighborSet).ToConfig())
|
||||
sets.NeighborSets = append(sets.NeighborSets, *v.ToConfig())
|
||||
case *CommunitySet:
|
||||
sets.BgpDefinedSets.CommunitySets = append(sets.BgpDefinedSets.CommunitySets, *s.(*CommunitySet).ToConfig())
|
||||
sets.BgpDefinedSets.CommunitySets = append(sets.BgpDefinedSets.CommunitySets, *v.ToConfig())
|
||||
case *ExtCommunitySet:
|
||||
sets.BgpDefinedSets.ExtCommunitySets = append(sets.BgpDefinedSets.ExtCommunitySets, *s.(*ExtCommunitySet).ToConfig())
|
||||
sets.BgpDefinedSets.ExtCommunitySets = append(sets.BgpDefinedSets.ExtCommunitySets, *v.ToConfig())
|
||||
case *LargeCommunitySet:
|
||||
sets.BgpDefinedSets.LargeCommunitySets = append(sets.BgpDefinedSets.LargeCommunitySets, *s.(*LargeCommunitySet).ToConfig())
|
||||
sets.BgpDefinedSets.LargeCommunitySets = append(sets.BgpDefinedSets.LargeCommunitySets, *v.ToConfig())
|
||||
case *AsPathSet:
|
||||
sets.BgpDefinedSets.AsPathSets = append(sets.BgpDefinedSets.AsPathSets, *s.(*AsPathSet).ToConfig())
|
||||
sets.BgpDefinedSets.AsPathSets = append(sets.BgpDefinedSets.AsPathSets, *v.ToConfig())
|
||||
}
|
||||
}
|
||||
return sets, nil
|
||||
@@ -3591,16 +3562,19 @@ func (r *RoutingPolicy) DeleteStatement(st *Statement, all bool) (err error) {
|
||||
}
|
||||
|
||||
func (r *RoutingPolicy) GetPolicy(name string) []*config.PolicyDefinition {
|
||||
r.mu.RLock()
|
||||
ps := func() Policies {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
var ps Policies
|
||||
for _, p := range r.policyMap {
|
||||
if name != "" && name != p.Name {
|
||||
continue
|
||||
var ps Policies
|
||||
for _, p := range r.policyMap {
|
||||
if name != "" && name != p.Name {
|
||||
continue
|
||||
}
|
||||
ps = append(ps, p)
|
||||
}
|
||||
ps = append(ps, p)
|
||||
}
|
||||
r.mu.RUnlock()
|
||||
return ps
|
||||
}()
|
||||
|
||||
sort.Sort(ps)
|
||||
|
||||
@@ -3827,32 +3801,59 @@ func (r *RoutingPolicy) SetPolicyAssignment(id string, dir PolicyDirection, poli
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *RoutingPolicy) Reset(rp *config.RoutingPolicy, ap map[string]config.ApplyPolicy) error {
|
||||
func (r *RoutingPolicy) Initialize() error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if rp != nil {
|
||||
if err := r.reload(*rp); err != nil {
|
||||
if err := r.reload(config.RoutingPolicy{}); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Policy",
|
||||
}).Errorf("failed to create routing policy: %s", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RoutingPolicy) setPeerPolicy(id string, c config.ApplyPolicy) {
|
||||
for _, dir := range []PolicyDirection{POLICY_DIRECTION_IMPORT, POLICY_DIRECTION_EXPORT} {
|
||||
ps, def, err := r.getAssignmentFromConfig(dir, c)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Policy",
|
||||
}).Errorf("failed to create routing policy: %s", err)
|
||||
return err
|
||||
"Dir": dir,
|
||||
}).Errorf("failed to get policy info: %s", err)
|
||||
continue
|
||||
}
|
||||
r.setDefaultPolicy(id, dir, def)
|
||||
r.setPolicy(id, dir, ps)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RoutingPolicy) SetPeerPolicy(peerId string, c config.ApplyPolicy) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
r.setPeerPolicy(peerId, c)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RoutingPolicy) Reset(rp *config.RoutingPolicy, ap map[string]config.ApplyPolicy) error {
|
||||
if rp == nil {
|
||||
return fmt.Errorf("routing Policy is nil in call to Reset")
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if err := r.reload(*rp); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Policy",
|
||||
}).Errorf("failed to create routing policy: %s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for id, c := range ap {
|
||||
for _, dir := range []PolicyDirection{POLICY_DIRECTION_IMPORT, POLICY_DIRECTION_EXPORT} {
|
||||
ps, def, err := r.getAssignmentFromConfig(dir, c)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Policy",
|
||||
"Dir": dir,
|
||||
}).Errorf("failed to get policy info: %s", err)
|
||||
continue
|
||||
}
|
||||
r.setDefaultPolicy(id, dir, def)
|
||||
r.setPolicy(id, dir, ps)
|
||||
}
|
||||
r.setPeerPolicy(id, c)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -3893,3 +3894,237 @@ type PolicyAssignment struct {
|
||||
Policies []*Policy
|
||||
Default RouteType
|
||||
}
|
||||
|
||||
var _regexpMedActionType = regexp.MustCompile(`([+-]?)(\d+)`)
|
||||
|
||||
func toStatementApi(s *config.Statement) *api.Statement {
|
||||
cs := &api.Conditions{}
|
||||
o, _ := NewMatchOption(s.Conditions.MatchPrefixSet.MatchSetOptions)
|
||||
if s.Conditions.MatchPrefixSet.PrefixSet != "" {
|
||||
cs.PrefixSet = &api.MatchSet{
|
||||
MatchType: api.MatchType(o),
|
||||
Name: s.Conditions.MatchPrefixSet.PrefixSet,
|
||||
}
|
||||
}
|
||||
if s.Conditions.MatchNeighborSet.NeighborSet != "" {
|
||||
o, _ := NewMatchOption(s.Conditions.MatchNeighborSet.MatchSetOptions)
|
||||
cs.NeighborSet = &api.MatchSet{
|
||||
MatchType: api.MatchType(o),
|
||||
Name: s.Conditions.MatchNeighborSet.NeighborSet,
|
||||
}
|
||||
}
|
||||
if s.Conditions.BgpConditions.AsPathLength.Operator != "" {
|
||||
cs.AsPathLength = &api.AsPathLength{
|
||||
Length: s.Conditions.BgpConditions.AsPathLength.Value,
|
||||
LengthType: api.AsPathLengthType(s.Conditions.BgpConditions.AsPathLength.Operator.ToInt()),
|
||||
}
|
||||
}
|
||||
if s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet != "" {
|
||||
cs.AsPathSet = &api.MatchSet{
|
||||
MatchType: api.MatchType(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions.ToInt()),
|
||||
Name: s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet,
|
||||
}
|
||||
}
|
||||
if s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet != "" {
|
||||
cs.CommunitySet = &api.MatchSet{
|
||||
MatchType: api.MatchType(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions.ToInt()),
|
||||
Name: s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet,
|
||||
}
|
||||
}
|
||||
if s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet != "" {
|
||||
cs.ExtCommunitySet = &api.MatchSet{
|
||||
MatchType: api.MatchType(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions.ToInt()),
|
||||
Name: s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet,
|
||||
}
|
||||
}
|
||||
if s.Conditions.BgpConditions.MatchLargeCommunitySet.LargeCommunitySet != "" {
|
||||
cs.LargeCommunitySet = &api.MatchSet{
|
||||
MatchType: api.MatchType(s.Conditions.BgpConditions.MatchLargeCommunitySet.MatchSetOptions.ToInt()),
|
||||
Name: s.Conditions.BgpConditions.MatchLargeCommunitySet.LargeCommunitySet,
|
||||
}
|
||||
}
|
||||
if s.Conditions.BgpConditions.RouteType != "" {
|
||||
cs.RouteType = api.Conditions_RouteType(s.Conditions.BgpConditions.RouteType.ToInt())
|
||||
}
|
||||
if len(s.Conditions.BgpConditions.NextHopInList) > 0 {
|
||||
cs.NextHopInList = s.Conditions.BgpConditions.NextHopInList
|
||||
}
|
||||
if s.Conditions.BgpConditions.AfiSafiInList != nil {
|
||||
afiSafiIn := make([]*api.Family, 0)
|
||||
for _, afiSafiType := range s.Conditions.BgpConditions.AfiSafiInList {
|
||||
if mapped, ok := bgp.AddressFamilyValueMap[string(afiSafiType)]; ok {
|
||||
afi, safi := bgp.RouteFamilyToAfiSafi(mapped)
|
||||
afiSafiIn = append(afiSafiIn, &api.Family{Afi: api.Family_Afi(afi), Safi: api.Family_Safi(safi)})
|
||||
}
|
||||
}
|
||||
cs.AfiSafiIn = afiSafiIn
|
||||
}
|
||||
cs.RpkiResult = int32(s.Conditions.BgpConditions.RpkiValidationResult.ToInt())
|
||||
as := &api.Actions{
|
||||
RouteAction: func() api.RouteAction {
|
||||
switch s.Actions.RouteDisposition {
|
||||
case config.ROUTE_DISPOSITION_ACCEPT_ROUTE:
|
||||
return api.RouteAction_ACCEPT
|
||||
case config.ROUTE_DISPOSITION_REJECT_ROUTE:
|
||||
return api.RouteAction_REJECT
|
||||
}
|
||||
return api.RouteAction_NONE
|
||||
}(),
|
||||
Community: func() *api.CommunityAction {
|
||||
if len(s.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &api.CommunityAction{
|
||||
ActionType: api.CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetCommunity.Options)]),
|
||||
Communities: s.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList}
|
||||
}(),
|
||||
Med: func() *api.MedAction {
|
||||
medStr := strings.TrimSpace(string(s.Actions.BgpActions.SetMed))
|
||||
if len(medStr) == 0 {
|
||||
return nil
|
||||
}
|
||||
matches := _regexpMedActionType.FindStringSubmatch(medStr)
|
||||
if len(matches) == 0 {
|
||||
return nil
|
||||
}
|
||||
action := api.MedActionType_MED_REPLACE
|
||||
switch matches[1] {
|
||||
case "+", "-":
|
||||
action = api.MedActionType_MED_MOD
|
||||
}
|
||||
value, err := strconv.ParseInt(matches[1]+matches[2], 10, 64)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return &api.MedAction{
|
||||
Value: value,
|
||||
ActionType: action,
|
||||
}
|
||||
}(),
|
||||
AsPrepend: func() *api.AsPrependAction {
|
||||
if len(s.Actions.BgpActions.SetAsPathPrepend.As) == 0 {
|
||||
return nil
|
||||
}
|
||||
var asn uint64
|
||||
useleft := false
|
||||
if s.Actions.BgpActions.SetAsPathPrepend.As != "last-as" {
|
||||
asn, _ = strconv.ParseUint(s.Actions.BgpActions.SetAsPathPrepend.As, 10, 32)
|
||||
} else {
|
||||
useleft = true
|
||||
}
|
||||
return &api.AsPrependAction{
|
||||
Asn: uint32(asn),
|
||||
Repeat: uint32(s.Actions.BgpActions.SetAsPathPrepend.RepeatN),
|
||||
UseLeftMost: useleft,
|
||||
}
|
||||
}(),
|
||||
ExtCommunity: func() *api.CommunityAction {
|
||||
if len(s.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &api.CommunityAction{
|
||||
ActionType: api.CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetExtCommunity.Options)]),
|
||||
Communities: s.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList,
|
||||
}
|
||||
}(),
|
||||
LargeCommunity: func() *api.CommunityAction {
|
||||
if len(s.Actions.BgpActions.SetLargeCommunity.SetLargeCommunityMethod.CommunitiesList) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &api.CommunityAction{
|
||||
ActionType: api.CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetLargeCommunity.Options)]),
|
||||
Communities: s.Actions.BgpActions.SetLargeCommunity.SetLargeCommunityMethod.CommunitiesList,
|
||||
}
|
||||
}(),
|
||||
Nexthop: func() *api.NexthopAction {
|
||||
if len(string(s.Actions.BgpActions.SetNextHop)) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if string(s.Actions.BgpActions.SetNextHop) == "self" {
|
||||
return &api.NexthopAction{
|
||||
Self: true,
|
||||
}
|
||||
}
|
||||
return &api.NexthopAction{
|
||||
Address: string(s.Actions.BgpActions.SetNextHop),
|
||||
}
|
||||
}(),
|
||||
LocalPref: func() *api.LocalPrefAction {
|
||||
if s.Actions.BgpActions.SetLocalPref == 0 {
|
||||
return nil
|
||||
}
|
||||
return &api.LocalPrefAction{Value: s.Actions.BgpActions.SetLocalPref}
|
||||
}(),
|
||||
}
|
||||
return &api.Statement{
|
||||
Name: s.Name,
|
||||
Conditions: cs,
|
||||
Actions: as,
|
||||
}
|
||||
}
|
||||
|
||||
func NewAPIPolicyFromTableStruct(p *Policy) *api.Policy {
|
||||
return ToPolicyApi(p.ToConfig())
|
||||
}
|
||||
|
||||
func ToPolicyApi(p *config.PolicyDefinition) *api.Policy {
|
||||
return &api.Policy{
|
||||
Name: p.Name,
|
||||
Statements: func() []*api.Statement {
|
||||
l := make([]*api.Statement, 0)
|
||||
for _, s := range p.Statements {
|
||||
l = append(l, toStatementApi(&s))
|
||||
}
|
||||
return l
|
||||
}(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewAPIPolicyAssignmentFromTableStruct(t *PolicyAssignment) *api.PolicyAssignment {
|
||||
return &api.PolicyAssignment{
|
||||
Direction: func() api.PolicyDirection {
|
||||
switch t.Type {
|
||||
case POLICY_DIRECTION_IMPORT:
|
||||
return api.PolicyDirection_IMPORT
|
||||
case POLICY_DIRECTION_EXPORT:
|
||||
return api.PolicyDirection_EXPORT
|
||||
}
|
||||
log.Errorf("invalid policy-type: %s", t.Type)
|
||||
return api.PolicyDirection_UNKNOWN
|
||||
}(),
|
||||
DefaultAction: func() api.RouteAction {
|
||||
switch t.Default {
|
||||
case ROUTE_TYPE_ACCEPT:
|
||||
return api.RouteAction_ACCEPT
|
||||
case ROUTE_TYPE_REJECT:
|
||||
return api.RouteAction_REJECT
|
||||
}
|
||||
return api.RouteAction_NONE
|
||||
}(),
|
||||
Name: t.Name,
|
||||
Policies: func() []*api.Policy {
|
||||
l := make([]*api.Policy, 0)
|
||||
for _, p := range t.Policies {
|
||||
l = append(l, NewAPIPolicyFromTableStruct(p))
|
||||
}
|
||||
return l
|
||||
}(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewAPIRoutingPolicyFromConfigStruct(c *config.RoutingPolicy) (*api.RoutingPolicy, error) {
|
||||
definedSets, err := config.NewAPIDefinedSetsFromConfigStruct(&c.DefinedSets)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
policies := make([]*api.Policy, 0, len(c.PolicyDefinitions))
|
||||
for _, policy := range c.PolicyDefinitions {
|
||||
policies = append(policies, ToPolicyApi(&policy))
|
||||
}
|
||||
|
||||
return &api.RoutingPolicy{
|
||||
DefinedSets: definedSets,
|
||||
Policies: policies,
|
||||
}, nil
|
||||
}
|
||||
|
||||
275
vendor/github.com/osrg/gobgp/internal/pkg/table/roa.go
generated
vendored
275
vendor/github.com/osrg/gobgp/internal/pkg/table/roa.go
generated
vendored
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
|
||||
// Copyright (C) 2016-2019 Nippon Telegraph and Telephone Corporation.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -16,35 +16,35 @@
|
||||
package table
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"sort"
|
||||
|
||||
"github.com/k-sone/critbitgo"
|
||||
"github.com/osrg/gobgp/internal/pkg/config"
|
||||
"github.com/osrg/gobgp/pkg/packet/bgp"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type IPPrefix struct {
|
||||
Prefix net.IP
|
||||
Length uint8
|
||||
}
|
||||
|
||||
func (p *IPPrefix) String() string {
|
||||
return fmt.Sprintf("%s/%d", p.Prefix, p.Length)
|
||||
}
|
||||
|
||||
type ROA struct {
|
||||
Family int
|
||||
Prefix *IPPrefix
|
||||
MaxLen uint8
|
||||
AS uint32
|
||||
Src string
|
||||
Family int
|
||||
Network *net.IPNet
|
||||
MaxLen uint8
|
||||
AS uint32
|
||||
Src string
|
||||
}
|
||||
|
||||
func NewROA(family int, prefixByte []byte, prefixLen uint8, maxLen uint8, as uint32, src string) *ROA {
|
||||
p := make([]byte, len(prefixByte))
|
||||
bits := net.IPv4len * 8
|
||||
if family == bgp.AFI_IP6 {
|
||||
bits = net.IPv6len * 8
|
||||
}
|
||||
copy(p, prefixByte)
|
||||
return &ROA{
|
||||
Family: family,
|
||||
Prefix: &IPPrefix{
|
||||
Prefix: p,
|
||||
Length: prefixLen,
|
||||
Network: &net.IPNet{
|
||||
IP: p,
|
||||
Mask: net.CIDRMask(int(prefixLen), bits),
|
||||
},
|
||||
MaxLen: maxLen,
|
||||
AS: as,
|
||||
@@ -58,3 +58,240 @@ func (r *ROA) Equal(roa *ROA) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type roaBucket struct {
|
||||
network *net.IPNet
|
||||
entries []*ROA
|
||||
}
|
||||
|
||||
func (r *roaBucket) GetEntries() []*ROA {
|
||||
return r.entries
|
||||
}
|
||||
|
||||
type ROATable struct {
|
||||
trees map[bgp.RouteFamily]*critbitgo.Net
|
||||
}
|
||||
|
||||
func NewROATable() *ROATable {
|
||||
m := make(map[bgp.RouteFamily]*critbitgo.Net)
|
||||
m[bgp.RF_IPv4_UC] = critbitgo.NewNet()
|
||||
m[bgp.RF_IPv6_UC] = critbitgo.NewNet()
|
||||
return &ROATable{
|
||||
trees: m,
|
||||
}
|
||||
}
|
||||
|
||||
func (rt *ROATable) roa2tree(roa *ROA) *critbitgo.Net {
|
||||
tree := rt.trees[bgp.RF_IPv4_UC]
|
||||
if roa.Family == bgp.AFI_IP6 {
|
||||
tree = rt.trees[bgp.RF_IPv6_UC]
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
||||
func (rt *ROATable) getBucket(roa *ROA) *roaBucket {
|
||||
tree := rt.roa2tree(roa)
|
||||
b, ok, _ := tree.Get(roa.Network)
|
||||
if !ok {
|
||||
b := &roaBucket{
|
||||
network: roa.Network,
|
||||
entries: make([]*ROA, 0),
|
||||
}
|
||||
tree.Add(roa.Network, b)
|
||||
return b
|
||||
}
|
||||
return b.(*roaBucket)
|
||||
}
|
||||
|
||||
func (rt *ROATable) Add(roa *ROA) {
|
||||
b := rt.getBucket(roa)
|
||||
for _, r := range b.entries {
|
||||
if r.Equal(roa) {
|
||||
// we already have the same one
|
||||
return
|
||||
}
|
||||
}
|
||||
b.entries = append(b.entries, roa)
|
||||
sort.Slice(b.entries, func(i, j int) bool {
|
||||
r1 := b.entries[i]
|
||||
r2 := b.entries[j]
|
||||
|
||||
if r1.MaxLen < r2.MaxLen {
|
||||
return true
|
||||
} else if r1.MaxLen > r2.MaxLen {
|
||||
return false
|
||||
}
|
||||
|
||||
if r1.AS < r2.AS {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
func (rt *ROATable) Delete(roa *ROA) {
|
||||
tree := rt.roa2tree(roa)
|
||||
if b, ok, _ := tree.Get(roa.Network); ok {
|
||||
bucket := b.(*roaBucket)
|
||||
for i, r := range bucket.entries {
|
||||
if r.Equal(roa) {
|
||||
bucket.entries = append(bucket.entries[:i], bucket.entries[i+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "rpki",
|
||||
"Network": roa.Network.String(),
|
||||
"AS": roa.AS,
|
||||
"Max Length": roa.MaxLen,
|
||||
}).Info("Can't withdraw a ROA")
|
||||
}
|
||||
|
||||
func (rt *ROATable) DeleteAll(network string) {
|
||||
for _, tree := range rt.trees {
|
||||
deleteNetworks := make([]*net.IPNet, 0, tree.Size())
|
||||
tree.Walk(nil, func(n *net.IPNet, v interface{}) bool {
|
||||
b, _ := v.(*roaBucket)
|
||||
newEntries := make([]*ROA, 0, len(b.entries))
|
||||
for _, r := range b.entries {
|
||||
if r.Src != network {
|
||||
newEntries = append(newEntries, r)
|
||||
}
|
||||
}
|
||||
if len(newEntries) > 0 {
|
||||
b.entries = newEntries
|
||||
} else {
|
||||
deleteNetworks = append(deleteNetworks, n)
|
||||
}
|
||||
return true
|
||||
})
|
||||
for _, key := range deleteNetworks {
|
||||
tree.Delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rt *ROATable) Validate(path *Path) *Validation {
|
||||
if path.IsWithdraw || path.IsEOR() {
|
||||
// RPKI isn't enabled or invalid path
|
||||
return nil
|
||||
}
|
||||
tree, ok := rt.trees[path.GetRouteFamily()]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
ownAs := path.OriginInfo().source.LocalAS
|
||||
asPath := path.GetAsPath()
|
||||
var as uint32
|
||||
|
||||
validation := &Validation{
|
||||
Status: config.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND,
|
||||
Reason: RPKI_VALIDATION_REASON_TYPE_NONE,
|
||||
Matched: make([]*ROA, 0),
|
||||
UnmatchedLength: make([]*ROA, 0),
|
||||
UnmatchedAs: make([]*ROA, 0),
|
||||
}
|
||||
|
||||
if asPath == nil || len(asPath.Value) == 0 {
|
||||
as = ownAs
|
||||
} else {
|
||||
param := asPath.Value[len(asPath.Value)-1]
|
||||
switch param.GetType() {
|
||||
case bgp.BGP_ASPATH_ATTR_TYPE_SEQ:
|
||||
asList := param.GetAS()
|
||||
if len(asList) == 0 {
|
||||
as = ownAs
|
||||
} else {
|
||||
as = asList[len(asList)-1]
|
||||
}
|
||||
case bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ:
|
||||
as = ownAs
|
||||
default:
|
||||
return validation
|
||||
}
|
||||
}
|
||||
|
||||
r := nlriToIPNet(path.GetNlri())
|
||||
prefixLen, _ := r.Mask.Size()
|
||||
var bucket *roaBucket
|
||||
tree.WalkMatch(r, func(r *net.IPNet, v interface{}) bool {
|
||||
bucket, _ = v.(*roaBucket)
|
||||
for _, r := range bucket.entries {
|
||||
if prefixLen <= int(r.MaxLen) {
|
||||
if r.AS != 0 && r.AS == as {
|
||||
validation.Matched = append(validation.Matched, r)
|
||||
} else {
|
||||
validation.UnmatchedAs = append(validation.UnmatchedAs, r)
|
||||
}
|
||||
} else {
|
||||
validation.UnmatchedLength = append(validation.UnmatchedLength, r)
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if len(validation.Matched) != 0 {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_VALID
|
||||
validation.Reason = RPKI_VALIDATION_REASON_TYPE_NONE
|
||||
} else if len(validation.UnmatchedAs) != 0 {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_INVALID
|
||||
validation.Reason = RPKI_VALIDATION_REASON_TYPE_AS
|
||||
} else if len(validation.UnmatchedLength) != 0 {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_INVALID
|
||||
validation.Reason = RPKI_VALIDATION_REASON_TYPE_LENGTH
|
||||
} else {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND
|
||||
validation.Reason = RPKI_VALIDATION_REASON_TYPE_NONE
|
||||
}
|
||||
|
||||
return validation
|
||||
}
|
||||
|
||||
func (rt *ROATable) Info(family bgp.RouteFamily) (map[string]uint32, map[string]uint32) {
|
||||
records := make(map[string]uint32)
|
||||
prefixes := make(map[string]uint32)
|
||||
|
||||
if tree, ok := rt.trees[family]; ok {
|
||||
tree.Walk(nil, func(_ *net.IPNet, v interface{}) bool {
|
||||
b, _ := v.(*roaBucket)
|
||||
tmpRecords := make(map[string]uint32)
|
||||
for _, roa := range b.entries {
|
||||
tmpRecords[roa.Src]++
|
||||
}
|
||||
|
||||
for src, r := range tmpRecords {
|
||||
if r > 0 {
|
||||
records[src] += r
|
||||
prefixes[src]++
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
return records, prefixes
|
||||
}
|
||||
|
||||
func (rt *ROATable) List(family bgp.RouteFamily) ([]*ROA, error) {
|
||||
var rfList []bgp.RouteFamily
|
||||
switch family {
|
||||
case bgp.RF_IPv4_UC:
|
||||
rfList = []bgp.RouteFamily{bgp.RF_IPv4_UC}
|
||||
case bgp.RF_IPv6_UC:
|
||||
rfList = []bgp.RouteFamily{bgp.RF_IPv6_UC}
|
||||
default:
|
||||
rfList = []bgp.RouteFamily{bgp.RF_IPv4_UC, bgp.RF_IPv6_UC}
|
||||
}
|
||||
l := make([]*ROA, 0)
|
||||
for _, rf := range rfList {
|
||||
if tree, ok := rt.trees[rf]; ok {
|
||||
tree.Walk(nil, func(_ *net.IPNet, v interface{}) bool {
|
||||
b, _ := v.(*roaBucket)
|
||||
l = append(l, b.entries...)
|
||||
return true
|
||||
})
|
||||
}
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
81
vendor/github.com/osrg/gobgp/internal/pkg/table/table.go
generated
vendored
81
vendor/github.com/osrg/gobgp/internal/pkg/table/table.go
generated
vendored
@@ -17,11 +17,12 @@ package table
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/bits"
|
||||
"net"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/armon/go-radix"
|
||||
"github.com/k-sone/critbitgo"
|
||||
"github.com/osrg/gobgp/pkg/packet/bgp"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -75,13 +76,13 @@ func (t *Table) deletePathsByVrf(vrf *Vrf) []*Path {
|
||||
for _, p := range dest.knownPathList {
|
||||
var rd bgp.RouteDistinguisherInterface
|
||||
nlri := p.GetNlri()
|
||||
switch nlri.(type) {
|
||||
switch v := nlri.(type) {
|
||||
case *bgp.LabeledVPNIPAddrPrefix:
|
||||
rd = nlri.(*bgp.LabeledVPNIPAddrPrefix).RD
|
||||
rd = v.RD
|
||||
case *bgp.LabeledVPNIPv6AddrPrefix:
|
||||
rd = nlri.(*bgp.LabeledVPNIPv6AddrPrefix).RD
|
||||
rd = v.RD
|
||||
case *bgp.EVPNNLRI:
|
||||
rd = nlri.(*bgp.EVPNNLRI).RD()
|
||||
rd = v.RD()
|
||||
default:
|
||||
return pathList
|
||||
}
|
||||
@@ -117,15 +118,14 @@ func (t *Table) deleteRTCPathsByVrf(vrf *Vrf, vrfs map[string]*Vrf) []*Path {
|
||||
return pathList
|
||||
}
|
||||
|
||||
func (t *Table) deleteDestByNlri(nlri bgp.AddrPrefixInterface) *Destination {
|
||||
if dst := t.GetDestination(nlri); dst != nil {
|
||||
t.deleteDest(dst)
|
||||
return dst
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Table) deleteDest(dest *Destination) {
|
||||
count := 0
|
||||
for _, v := range dest.localIdMap.bitmap {
|
||||
count += bits.OnesCount64(v)
|
||||
}
|
||||
if len(dest.localIdMap.bitmap) != 0 && count != 1 {
|
||||
return
|
||||
}
|
||||
destinations := t.GetDestinations()
|
||||
delete(destinations, t.tableKey(dest.GetNlri()))
|
||||
if len(destinations) == 0 {
|
||||
@@ -175,7 +175,7 @@ func (t *Table) validatePath(path *Path) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Table) getOrCreateDest(nlri bgp.AddrPrefixInterface) *Destination {
|
||||
func (t *Table) getOrCreateDest(nlri bgp.AddrPrefixInterface, size int) *Destination {
|
||||
dest := t.GetDestination(nlri)
|
||||
// If destination for given prefix does not exist we create it.
|
||||
if dest == nil {
|
||||
@@ -183,7 +183,7 @@ func (t *Table) getOrCreateDest(nlri bgp.AddrPrefixInterface) *Destination {
|
||||
"Topic": "Table",
|
||||
"Nlri": nlri,
|
||||
}).Debugf("create Destination")
|
||||
dest = NewDestination(nlri, 64)
|
||||
dest = NewDestination(nlri, size)
|
||||
t.setDestination(dest)
|
||||
}
|
||||
return dest
|
||||
@@ -212,14 +212,13 @@ func (t *Table) GetLongerPrefixDestinations(key string) ([]*Destination, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k := CidrToRadixkey(prefix.String())
|
||||
r := radix.New()
|
||||
r := critbitgo.NewNet()
|
||||
for _, dst := range t.GetDestinations() {
|
||||
r.Insert(AddrToRadixkey(dst.nlri), dst)
|
||||
r.Add(nlriToIPNet(dst.nlri), dst)
|
||||
}
|
||||
r.WalkPrefix(k, func(s string, v interface{}) bool {
|
||||
r.WalkPrefix(prefix, func(_ *net.IPNet, v interface{}) bool {
|
||||
results = append(results, v.(*Destination))
|
||||
return false
|
||||
return true
|
||||
})
|
||||
default:
|
||||
for _, dst := range t.GetDestinations() {
|
||||
@@ -435,13 +434,45 @@ type TableInfo struct {
|
||||
NumAccepted int
|
||||
}
|
||||
|
||||
func (t *Table) Info(id string, as uint32) *TableInfo {
|
||||
type TableInfoOptions struct {
|
||||
ID string
|
||||
AS uint32
|
||||
VRF *Vrf
|
||||
}
|
||||
|
||||
func (t *Table) Info(option ...TableInfoOptions) *TableInfo {
|
||||
var numD, numP int
|
||||
|
||||
id := GLOBAL_RIB_NAME
|
||||
var vrf *Vrf
|
||||
as := uint32(0)
|
||||
|
||||
for _, o := range option {
|
||||
if o.ID != "" {
|
||||
id = o.ID
|
||||
}
|
||||
if o.VRF != nil {
|
||||
vrf = o.VRF
|
||||
}
|
||||
as = o.AS
|
||||
}
|
||||
|
||||
for _, d := range t.destinations {
|
||||
ps := d.GetKnownPathList(id, as)
|
||||
if len(ps) > 0 {
|
||||
numD += 1
|
||||
numP += len(ps)
|
||||
paths := d.GetKnownPathList(id, as)
|
||||
n := len(paths)
|
||||
|
||||
if vrf != nil {
|
||||
ps := make([]*Path, 0, len(paths))
|
||||
for _, p := range paths {
|
||||
if CanImportToVrf(vrf, p) {
|
||||
ps = append(ps, p.ToLocal())
|
||||
}
|
||||
}
|
||||
n = len(ps)
|
||||
}
|
||||
if n != 0 {
|
||||
numD++
|
||||
numP += n
|
||||
}
|
||||
}
|
||||
return &TableInfo{
|
||||
|
||||
56
vendor/github.com/osrg/gobgp/internal/pkg/table/table_manager.go
generated
vendored
56
vendor/github.com/osrg/gobgp/internal/pkg/table/table_manager.go
generated
vendored
@@ -166,11 +166,12 @@ func (manager *TableManager) DeleteVrf(name string) ([]*Path, error) {
|
||||
msgs = append(msgs, t.deletePathsByVrf(vrf)...)
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Vrf",
|
||||
"Key": vrf.Name,
|
||||
"Rd": vrf.Rd,
|
||||
"ImportRt": vrf.ImportRt,
|
||||
"ExportRt": vrf.ExportRt,
|
||||
"Topic": "Vrf",
|
||||
"Key": vrf.Name,
|
||||
"Rd": vrf.Rd,
|
||||
"ImportRt": vrf.ImportRt,
|
||||
"ExportRt": vrf.ExportRt,
|
||||
"MplsLabel": vrf.MplsLabel,
|
||||
}).Debugf("delete vrf")
|
||||
delete(manager.Vrfs, name)
|
||||
rtcTable := manager.Tables[bgp.RF_RTC_UC]
|
||||
@@ -181,7 +182,7 @@ func (manager *TableManager) DeleteVrf(name string) ([]*Path, error) {
|
||||
func (tm *TableManager) update(newPath *Path) *Update {
|
||||
t := tm.Tables[newPath.GetRouteFamily()]
|
||||
t.validatePath(newPath)
|
||||
dst := t.getOrCreateDest(newPath.GetNlri())
|
||||
dst := t.getOrCreateDest(newPath.GetNlri(), 64)
|
||||
u := dst.Calculate(newPath)
|
||||
if len(dst.knownPathList) == 0 {
|
||||
t.deleteDest(dst)
|
||||
@@ -189,21 +190,6 @@ func (tm *TableManager) update(newPath *Path) *Update {
|
||||
return u
|
||||
}
|
||||
|
||||
func (manager *TableManager) GetPathListByPeer(info *PeerInfo, rf bgp.RouteFamily) []*Path {
|
||||
if t, ok := manager.Tables[rf]; ok {
|
||||
pathList := make([]*Path, 0, len(t.destinations))
|
||||
for _, dst := range t.destinations {
|
||||
for _, p := range dst.knownPathList {
|
||||
if p.GetSource().Equal(info) {
|
||||
pathList = append(pathList, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
return pathList
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (manager *TableManager) Update(newPath *Path) []*Update {
|
||||
if newPath == nil || newPath.IsEOR() {
|
||||
return nil
|
||||
@@ -232,6 +218,12 @@ func (manager *TableManager) Update(newPath *Path) []*Update {
|
||||
// different Ethernet segment identifier and a higher sequence number
|
||||
// than that which it had previously advertised withdraws its MAC/IP
|
||||
// Advertisement route.
|
||||
// ......
|
||||
// If the PE is the originator of the MAC route and it receives the same
|
||||
// MAC address with the same sequence number that it generated, it will
|
||||
// compare its own IP address with the IP address of the remote PE and
|
||||
// will select the lowest IP. If its own route is not the best one, it
|
||||
// will withdraw the route.
|
||||
func (manager *TableManager) handleMacMobility(path *Path) []*Path {
|
||||
pathList := make([]*Path, 0)
|
||||
nlri := path.GetNlri().(*bgp.EVPNNLRI)
|
||||
@@ -242,7 +234,7 @@ func (manager *TableManager) handleMacMobility(path *Path) []*Path {
|
||||
if !path2.IsLocal() || path2.GetNlri().(*bgp.EVPNNLRI).RouteType != bgp.EVPN_ROUTE_TYPE_MAC_IP_ADVERTISEMENT {
|
||||
continue
|
||||
}
|
||||
f := func(p *Path) (bgp.EthernetSegmentIdentifier, net.HardwareAddr, int) {
|
||||
f := func(p *Path) (bgp.EthernetSegmentIdentifier, uint32, net.HardwareAddr, int, net.IP) {
|
||||
nlri := p.GetNlri().(*bgp.EVPNNLRI)
|
||||
d := nlri.RouteTypeData.(*bgp.EVPNMacIPAdvertisementRoute)
|
||||
ecs := p.GetExtCommunities()
|
||||
@@ -253,12 +245,14 @@ func (manager *TableManager) handleMacMobility(path *Path) []*Path {
|
||||
break
|
||||
}
|
||||
}
|
||||
return d.ESI, d.MacAddress, seq
|
||||
return d.ESI, d.ETag, d.MacAddress, seq, p.info.source.Address
|
||||
}
|
||||
e1, m1, s1 := f(path)
|
||||
e2, m2, s2 := f(path2)
|
||||
if bytes.Equal(m1, m2) && !bytes.Equal(e1.Value, e2.Value) && s1 > s2 {
|
||||
pathList = append(pathList, path2.Clone(true))
|
||||
e1, et1, m1, s1, i1 := f(path)
|
||||
e2, et2, m2, s2, i2 := f(path2)
|
||||
if et1 == et2 && bytes.Equal(m1, m2) && !bytes.Equal(e1.Value, e2.Value) {
|
||||
if s1 > s2 || s1 == s2 && bytes.Compare(i1, i2) < 0 {
|
||||
pathList = append(pathList, path2.Clone(true))
|
||||
}
|
||||
}
|
||||
}
|
||||
return pathList
|
||||
@@ -360,11 +354,3 @@ func (manager *TableManager) GetDestination(path *Path) *Destination {
|
||||
}
|
||||
return t.GetDestination(path.GetNlri())
|
||||
}
|
||||
|
||||
func (manager *TableManager) TableInfo(id string, as uint32, family bgp.RouteFamily) (*TableInfo, error) {
|
||||
t, ok := manager.Tables[family]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("address family %s is not configured", family)
|
||||
}
|
||||
return t.Info(id, as), nil
|
||||
}
|
||||
|
||||
22
vendor/github.com/osrg/gobgp/internal/pkg/table/vrf.go
generated
vendored
22
vendor/github.com/osrg/gobgp/internal/pkg/table/vrf.go
generated
vendored
@@ -20,11 +20,12 @@ import (
|
||||
)
|
||||
|
||||
type Vrf struct {
|
||||
Name string
|
||||
Id uint32
|
||||
Rd bgp.RouteDistinguisherInterface
|
||||
ImportRt []bgp.ExtendedCommunityInterface
|
||||
ExportRt []bgp.ExtendedCommunityInterface
|
||||
Name string
|
||||
Id uint32
|
||||
Rd bgp.RouteDistinguisherInterface
|
||||
ImportRt []bgp.ExtendedCommunityInterface
|
||||
ExportRt []bgp.ExtendedCommunityInterface
|
||||
MplsLabel uint32
|
||||
}
|
||||
|
||||
func (v *Vrf) Clone() *Vrf {
|
||||
@@ -33,11 +34,12 @@ func (v *Vrf) Clone() *Vrf {
|
||||
return append(l, rt...)
|
||||
}
|
||||
return &Vrf{
|
||||
Name: v.Name,
|
||||
Id: v.Id,
|
||||
Rd: v.Rd,
|
||||
ImportRt: f(v.ImportRt),
|
||||
ExportRt: f(v.ExportRt),
|
||||
Name: v.Name,
|
||||
Id: v.Id,
|
||||
Rd: v.Rd,
|
||||
ImportRt: f(v.ImportRt),
|
||||
ExportRt: f(v.ExportRt),
|
||||
MplsLabel: v.MplsLabel,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user