diff options
Diffstat (limited to 'table/path.go')
-rw-r--r-- | table/path.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/table/path.go b/table/path.go index c095ab24..a82c5e59 100644 --- a/table/path.go +++ b/table/path.go @@ -25,6 +25,7 @@ import ( "net" "reflect" "time" + "math" ) type Path interface { @@ -40,6 +41,7 @@ type Path interface { SetCommunities([]uint32, bool) RemoveCommunities([]uint32) int ClearCommunities() + SetMed(uint32, bool, bool) error setSource(source *PeerInfo) GetSource() *PeerInfo GetSourceAs() uint32 @@ -479,6 +481,41 @@ func (pd *PathDefault) ClearCommunities() { } } +// SetMed replace, add or subtraction med with new ones. +func (pd *PathDefault) SetMed(med uint32, doReplace bool, doSubstruction bool) error { + newMed := &bgp.PathAttributeMultiExitDisc{} + idx, attr := pd.getPathAttr(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC) + if attr != nil{ + m := attr.(*bgp.PathAttributeMultiExitDisc) + if doReplace { + newMed = bgp.NewPathAttributeMultiExitDisc(med) + } else { + if doSubstruction { + if m.Value - med < 0 { + return fmt.Errorf("med value invalid. the underflow threshold") + } + newMed = bgp.NewPathAttributeMultiExitDisc(m.Value - med) + } else { + if m.Value + med > math.MaxUint32 { + return fmt.Errorf("med value invalid. the overflow threshold") + } + newMed = bgp.NewPathAttributeMultiExitDisc(m.Value + med) + } + } + pd.pathAttrs[idx] = newMed + } else { + if doReplace { + newMed = bgp.NewPathAttributeMultiExitDisc(med) + } else { + if !doSubstruction { + newMed = bgp.NewPathAttributeMultiExitDisc(med) + } + } + pd.pathAttrs[idx] = newMed + } + return nil +} + // create Path object based on route family func CreatePath(source *PeerInfo, nlri bgp.AddrPrefixInterface, attrs []bgp.PathAttributeInterface, isWithdraw bool, now time.Time) (Path, error) { |