diff options
Diffstat (limited to 'table')
-rw-r--r-- | table/destination_test.go | 3 | ||||
-rw-r--r-- | table/path.go | 25 | ||||
-rw-r--r-- | table/path_test.go | 9 | ||||
-rw-r--r-- | table/table_manager.go | 25 | ||||
-rw-r--r-- | table/table_test.go | 3 |
5 files changed, 37 insertions, 28 deletions
diff --git a/table/destination_test.go b/table/destination_test.go index 2398773e..5dbab4d0 100644 --- a/table/destination_test.go +++ b/table/destination_test.go @@ -21,6 +21,7 @@ import ( "github.com/stretchr/testify/assert" //"net" "testing" + "time" ) func TestDestinationNewIPv4(t *testing.T) { @@ -134,7 +135,7 @@ func DestCreatePath(msgs []*ProcessMessage) []Path { nlriList := updateMsgD.NLRI pathAttributes := updateMsgD.PathAttributes nlri_info := nlriList[0] - pathD[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false) + pathD[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false, time.Now()) } return pathD } diff --git a/table/path.go b/table/path.go index 027630cb..2916be1d 100644 --- a/table/path.go +++ b/table/path.go @@ -22,6 +22,7 @@ import ( "github.com/osrg/gobgp/packet" "net" "reflect" + "time" ) type Path interface { @@ -51,9 +52,10 @@ type PathDefault struct { nlri bgp.AddrPrefixInterface pathAttrs []bgp.PathAttributeInterface medSetByTargetNeighbor bool + timestamp time.Time } -func NewPathDefault(rf bgp.RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInterface, nexthop net.IP, isWithdraw bool, pattrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool) *PathDefault { +func NewPathDefault(rf bgp.RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInterface, nexthop net.IP, isWithdraw bool, pattrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool, now time.Time) *PathDefault { if !isWithdraw && pattrs == nil { log.Error("Need to provide nexthop and patattrs for path that is not a withdraw.") @@ -68,6 +70,7 @@ func NewPathDefault(rf bgp.RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInt path.nexthop = nexthop path.withdraw = isWithdraw path.medSetByTargetNeighbor = medSetByTargetNeighbor + path.timestamp = now return path } @@ -76,10 +79,12 @@ func (pd *PathDefault) MarshalJSON() ([]byte, error) { Network string Nexthop string Attrs []bgp.PathAttributeInterface + Time float64 }{ Network: pd.getPrefix(), Nexthop: pd.nexthop.String(), Attrs: pd.getPathAttrs(), + Time: time.Now().Sub(pd.timestamp).Seconds(), }) } @@ -93,7 +98,7 @@ func (pd *PathDefault) clone(isWithdraw bool) Path { nlri = &bgp.WithdrawnRoute{pd.nlri.(*bgp.NLRInfo).IPAddrPrefix} } } - return CreatePath(pd.source, nlri, pd.pathAttrs, isWithdraw) + return CreatePath(pd.source, nlri, pd.pathAttrs, isWithdraw, pd.timestamp) } func (pd *PathDefault) GetRouteFamily() bgp.RouteFamily { @@ -188,7 +193,7 @@ func (pi *PathDefault) getPrefix() string { } // create Path object based on route family -func CreatePath(source *PeerInfo, nlri bgp.AddrPrefixInterface, attrs []bgp.PathAttributeInterface, isWithdraw bool) Path { +func CreatePath(source *PeerInfo, nlri bgp.AddrPrefixInterface, attrs []bgp.PathAttributeInterface, isWithdraw bool, now time.Time) Path { rf := bgp.RouteFamily(int(nlri.AFI())<<16 | int(nlri.SAFI())) log.Debugf("afi: %d, safi: %d ", int(nlri.AFI()), nlri.SAFI()) @@ -197,10 +202,10 @@ func CreatePath(source *PeerInfo, nlri bgp.AddrPrefixInterface, attrs []bgp.Path switch rf { case bgp.RF_IPv4_UC: log.Debugf("RouteFamily : %s", bgp.RF_IPv4_UC.String()) - path = NewIPv4Path(source, nlri, isWithdraw, attrs, false) + path = NewIPv4Path(source, nlri, isWithdraw, attrs, false, now) case bgp.RF_IPv6_UC: log.Debugf("RouteFamily : %s", bgp.RF_IPv6_UC.String()) - path = NewIPv6Path(source, nlri, isWithdraw, attrs, false) + path = NewIPv6Path(source, nlri, isWithdraw, attrs, false, now) } return path } @@ -212,9 +217,9 @@ type IPv4Path struct { *PathDefault } -func NewIPv4Path(source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool) *IPv4Path { +func NewIPv4Path(source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool, now time.Time) *IPv4Path { ipv4Path := &IPv4Path{} - ipv4Path.PathDefault = NewPathDefault(bgp.RF_IPv4_UC, source, nlri, nil, isWithdraw, attrs, medSetByTargetNeighbor) + ipv4Path.PathDefault = NewPathDefault(bgp.RF_IPv4_UC, source, nlri, nil, isWithdraw, attrs, medSetByTargetNeighbor, now) if !isWithdraw { _, nexthop_attr := ipv4Path.getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP) ipv4Path.nexthop = nexthop_attr.(*bgp.PathAttributeNextHop).Value @@ -233,9 +238,9 @@ type IPv6Path struct { *PathDefault } -func NewIPv6Path(source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool) *IPv6Path { +func NewIPv6Path(source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool, now time.Time) *IPv6Path { ipv6Path := &IPv6Path{} - ipv6Path.PathDefault = NewPathDefault(bgp.RF_IPv6_UC, source, nlri, nil, isWithdraw, attrs, medSetByTargetNeighbor) + ipv6Path.PathDefault = NewPathDefault(bgp.RF_IPv6_UC, source, nlri, nil, isWithdraw, attrs, medSetByTargetNeighbor, now) if !isWithdraw { _, mpattr := ipv6Path.getPathAttr(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI) ipv6Path.nexthop = mpattr.(*bgp.PathAttributeMpReachNLRI).Nexthop @@ -250,7 +255,7 @@ func (ipv6p *IPv6Path) clone(isWithdraw bool) Path { log.Fatal("Withdraw path is not supposed to be cloned") } } - return CreatePath(ipv6p.source, nlri, ipv6p.pathAttrs, isWithdraw) + return CreatePath(ipv6p.source, nlri, ipv6p.pathAttrs, isWithdraw, ipv6p.PathDefault.timestamp) } func (ipv6p *IPv6Path) setPathDefault(pd *PathDefault) { diff --git a/table/path_test.go b/table/path_test.go index 20083785..fe80b3d7 100644 --- a/table/path_test.go +++ b/table/path_test.go @@ -7,20 +7,21 @@ import ( "github.com/stretchr/testify/assert" "net" "testing" + "time" ) func TestPathNewIPv4(t *testing.T) { peerP := PathCreatePeer() msgP := PathCreateMSG(peerP) pathP := PathCreatePath(msgP) - ipv4p := NewIPv4Path(pathP[0].getSource(), pathP[0].getNlri(), true, pathP[0].getPathAttrs(), pathP[0].getMedSetByTargetNeighbor()) + ipv4p := NewIPv4Path(pathP[0].getSource(), pathP[0].getNlri(), true, pathP[0].getPathAttrs(), pathP[0].getMedSetByTargetNeighbor(), time.Now()) assert.NotNil(t, ipv4p) } func TestPathNewIPv6(t *testing.T) { peerP := PathCreatePeer() msgP := PathCreateMSG(peerP) pathP := PathCreatePath(msgP) - ipv6p := NewIPv6Path(pathP[0].getSource(), pathP[0].getNlri(), true, pathP[0].getPathAttrs(), pathP[0].getMedSetByTargetNeighbor()) + ipv6p := NewIPv6Path(pathP[0].getSource(), pathP[0].getNlri(), true, pathP[0].getPathAttrs(), pathP[0].getMedSetByTargetNeighbor(), time.Now()) assert.NotNil(t, ipv6p) } @@ -142,7 +143,7 @@ func TestPathCreatePath(t *testing.T) { nlriList := updateMsgP.NLRI pathAttributes := updateMsgP.PathAttributes nlri_info := nlriList[0] - path := CreatePath(msgP[0].fromPeer, &nlri_info, pathAttributes, false) + path := CreatePath(msgP[0].fromPeer, &nlri_info, pathAttributes, false, time.Now()) assert.NotNil(t, path) } @@ -189,7 +190,7 @@ func PathCreatePath(msgs []*ProcessMessage) []Path { nlriList := updateMsgP.NLRI pathAttributes := updateMsgP.PathAttributes nlri_info := nlriList[0] - pathP[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false) + pathP[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false, time.Now()) } return pathP } diff --git a/table/table_manager.go b/table/table_manager.go index 109cd004..375e7b2b 100644 --- a/table/table_manager.go +++ b/table/table_manager.go @@ -33,7 +33,7 @@ func NewProcessMessage(m *bgp.BGPMessage, peerInfo *PeerInfo) *ProcessMessage { } } -func (p *ProcessMessage) nlri2Path() []Path { +func (p *ProcessMessage) nlri2Path(now time.Time) []Path { updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes pathList := make([]Path, 0) @@ -41,13 +41,13 @@ func (p *ProcessMessage) nlri2Path() []Path { // define local variable to pass nlri's address to CreatePath var nlri bgp.NLRInfo = nlri_info // create Path object - path := CreatePath(p.fromPeer, &nlri, pathAttributes, false) + path := CreatePath(p.fromPeer, &nlri, pathAttributes, false, now) pathList = append(pathList, path) } return pathList } -func (p *ProcessMessage) withdraw2Path() []Path { +func (p *ProcessMessage) withdraw2Path(now time.Time) []Path { updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes pathList := make([]Path, 0) @@ -55,13 +55,13 @@ func (p *ProcessMessage) withdraw2Path() []Path { // define local variable to pass nlri's address to CreatePath var w bgp.WithdrawnRoute = nlriWithdraw // create withdrawn Path object - path := CreatePath(p.fromPeer, &w, pathAttributes, true) + path := CreatePath(p.fromPeer, &w, pathAttributes, true, now) pathList = append(pathList, path) } return pathList } -func (p *ProcessMessage) mpreachNlri2Path() []Path { +func (p *ProcessMessage) mpreachNlri2Path(now time.Time) []Path { updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes attrList := []*bgp.PathAttributeMpReachNLRI{} @@ -78,14 +78,14 @@ func (p *ProcessMessage) mpreachNlri2Path() []Path { for _, mp := range attrList { nlri_info := mp.Value for _, nlri := range nlri_info { - path := CreatePath(p.fromPeer, nlri, pathAttributes, false) + path := CreatePath(p.fromPeer, nlri, pathAttributes, false, now) pathList = append(pathList, path) } } return pathList } -func (p *ProcessMessage) mpunreachNlri2Path() []Path { +func (p *ProcessMessage) mpunreachNlri2Path(now time.Time) []Path { updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes attrList := []*bgp.PathAttributeMpUnreachNLRI{} @@ -103,7 +103,7 @@ func (p *ProcessMessage) mpunreachNlri2Path() []Path { nlri_info := mp.Value for _, nlri := range nlri_info { - path := CreatePath(p.fromPeer, nlri, pathAttributes, true) + path := CreatePath(p.fromPeer, nlri, pathAttributes, true, now) pathList = append(pathList, path) } } @@ -112,10 +112,11 @@ func (p *ProcessMessage) mpunreachNlri2Path() []Path { func (p *ProcessMessage) ToPathList() []Path { pathList := make([]Path, 0) - pathList = append(pathList, p.nlri2Path()...) - pathList = append(pathList, p.withdraw2Path()...) - pathList = append(pathList, p.mpreachNlri2Path()...) - pathList = append(pathList, p.mpunreachNlri2Path()...) + now := time.Now() + pathList = append(pathList, p.nlri2Path(now)...) + pathList = append(pathList, p.withdraw2Path(now)...) + pathList = append(pathList, p.mpreachNlri2Path(now)...) + pathList = append(pathList, p.mpunreachNlri2Path(now)...) return pathList } diff --git a/table/table_test.go b/table/table_test.go index 4af1d221..4a7a3a23 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -19,6 +19,7 @@ import ( "github.com/osrg/gobgp/packet" "github.com/stretchr/testify/assert" "testing" + "time" ) func TestTableCreateDestDefault(t *testing.T) { @@ -130,7 +131,7 @@ func TableCreatePath(msgs []*ProcessMessage) []Path { nlriList := updateMsgT.NLRI pathAttributes := updateMsgT.PathAttributes nlri_info := nlriList[0] - pathT[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false) + pathT[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false, time.Now()) } return pathT } |