summaryrefslogtreecommitdiffhomepage
path: root/table/path.go
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-03-26 08:54:56 +0000
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-04-04 20:47:52 +0900
commitef65699c8c8bfc2b0177a645ea9e12df097285ab (patch)
tree34932112eb289eb2d114d96f1366f736abf4af98 /table/path.go
parent3febf8129ee052adf013b6f8559e9cb950f3cdd5 (diff)
server: remove nexthop field from Path struct and make Path.setNexthop public
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'table/path.go')
-rw-r--r--table/path.go95
1 files changed, 37 insertions, 58 deletions
diff --git a/table/path.go b/table/path.go
index 71bf2c1e..c7329587 100644
--- a/table/path.go
+++ b/table/path.go
@@ -34,8 +34,8 @@ type Path interface {
GetRouteFamily() bgp.RouteFamily
setSource(source *PeerInfo)
GetSource() *PeerInfo
- setNexthop(nexthop net.IP)
GetNexthop() net.IP
+ SetNexthop(net.IP)
setWithdraw(withdraw bool)
IsWithdraw() bool
GetNlri() bgp.AddrPrefixInterface
@@ -51,7 +51,6 @@ type Path interface {
type PathDefault struct {
routeFamily bgp.RouteFamily
source *PeerInfo
- nexthop net.IP
withdraw bool
nlri bgp.AddrPrefixInterface
pathAttrs []bgp.PathAttributeInterface
@@ -59,14 +58,13 @@ type PathDefault struct {
timestamp time.Time
}
-func NewPathDefault(rf bgp.RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInterface, nexthop net.IP, isWithdraw bool, pattrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool, now time.Time) *PathDefault {
+func NewPathDefault(rf bgp.RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, pattrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool, now time.Time) *PathDefault {
if !isWithdraw && pattrs == nil {
log.WithFields(log.Fields{
- "Topic": "Table",
- "Key": nlri.String(),
- "Peer": source.Address.String(),
- "Nexthop": nexthop.String(),
- }).Error("Need to provide nexthop and patattrs for the path that is not withdraw.")
+ "Topic": "Table",
+ "Key": nlri.String(),
+ "Peer": source.Address.String(),
+ }).Error("Need to provide patattrs for the path that is not withdraw.")
return nil
}
@@ -75,7 +73,6 @@ func NewPathDefault(rf bgp.RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInt
path.pathAttrs = pattrs
path.nlri = nlri
path.source = source
- path.nexthop = nexthop
path.withdraw = isWithdraw
path.medSetByTargetNeighbor = medSetByTargetNeighbor
path.timestamp = now
@@ -101,26 +98,7 @@ func (pd *PathDefault) updatePathAttrs(global *config.Global, peer *config.Neigh
if peer.PeerType == config.PEER_TYPE_EXTERNAL {
// NEXTHOP handling
- switch pd.routeFamily {
- case bgp.RF_IPv4_UC:
- idx, _ := pd.getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP)
- if idx < 0 {
- log.Warn("missing NEXTHOP mandatory attribute, check MP_REACH_NLRI instead.")
- } else {
- newNexthop := bgp.NewPathAttributeNextHop(peer.LocalAddress.String())
- pd.pathAttrs[idx] = newNexthop
- break
- }
- fallthrough
- default:
- idx, attr := pd.getPathAttr(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI)
- if attr == nil {
- log.Fatal("missing MP_REACH_NLRI mandatory attribute")
- }
- oldNlri := attr.(*bgp.PathAttributeMpReachNLRI)
- newNlri := bgp.NewPathAttributeMpReachNLRI(peer.LocalAddress.String(), oldNlri.Value)
- pd.pathAttrs[idx] = newNlri
- }
+ pd.SetNexthop(peer.LocalAddress)
// AS_PATH handling
//
@@ -200,7 +178,7 @@ func (pd *PathDefault) MarshalJSON() ([]byte, error) {
Age float64
}{
Network: pd.getPrefix(),
- Nexthop: pd.nexthop.String(),
+ Nexthop: pd.GetNexthop().String(),
Attrs: pd.getPathAttrs(),
Age: time.Now().Sub(pd.timestamp).Seconds(),
})
@@ -236,12 +214,30 @@ func (pd *PathDefault) GetSource() *PeerInfo {
return pd.source
}
-func (pd *PathDefault) setNexthop(nexthop net.IP) {
- pd.nexthop = nexthop
+func (pd *PathDefault) GetNexthop() net.IP {
+ _, attr := pd.getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP)
+ if attr != nil {
+ return attr.(*bgp.PathAttributeNextHop).Value
+ }
+ _, attr = pd.getPathAttr(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI)
+ if attr != nil {
+ return attr.(*bgp.PathAttributeMpReachNLRI).Nexthop
+ }
+ return net.IP{}
}
-func (pd *PathDefault) GetNexthop() net.IP {
- return pd.nexthop
+func (pd *PathDefault) SetNexthop(nexthop net.IP) {
+ idx, attr := pd.getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP)
+ if attr != nil {
+ newNexthop := bgp.NewPathAttributeNextHop(nexthop.String())
+ pd.pathAttrs[idx] = newNexthop
+ }
+ idx, attr = pd.getPathAttr(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI)
+ if attr != nil {
+ oldNlri := attr.(*bgp.PathAttributeMpReachNLRI)
+ newNlri := bgp.NewPathAttributeMpReachNLRI(nexthop.String(), oldNlri.Value)
+ pd.pathAttrs[idx] = newNlri
+ }
}
func (pd *PathDefault) setWithdraw(withdraw bool) {
@@ -349,11 +345,7 @@ type IPv4Path struct {
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, now)
- if !isWithdraw {
- _, nexthop_attr := ipv4Path.getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP)
- ipv4Path.nexthop = nexthop_attr.(*bgp.PathAttributeNextHop).Value
- }
+ ipv4Path.PathDefault = NewPathDefault(bgp.RF_IPv4_UC, source, nlri, isWithdraw, attrs, medSetByTargetNeighbor, now)
return ipv4Path
}
@@ -370,11 +362,7 @@ type IPv6Path struct {
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, now)
- if !isWithdraw {
- _, mpattr := ipv6Path.getPathAttr(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI)
- ipv6Path.nexthop = mpattr.(*bgp.PathAttributeMpReachNLRI).Nexthop
- }
+ ipv6Path.PathDefault = NewPathDefault(bgp.RF_IPv6_UC, source, nlri, isWithdraw, attrs, medSetByTargetNeighbor, now)
return ipv6Path
}
@@ -414,7 +402,7 @@ func (ipv6p *IPv6Path) MarshalJSON() ([]byte, error) {
Age float64
}{
Network: ipv6p.getPrefix(),
- Nexthop: ipv6p.PathDefault.nexthop.String(),
+ Nexthop: ipv6p.PathDefault.GetNexthop().String(),
Attrs: ipv6p.PathDefault.getPathAttrs(),
Age: time.Now().Sub(ipv6p.PathDefault.timestamp).Seconds(),
})
@@ -426,11 +414,7 @@ type IPv4VPNPath struct {
func NewIPv4VPNPath(source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool, now time.Time) *IPv4VPNPath {
ipv4VPNPath := &IPv4VPNPath{}
- ipv4VPNPath.PathDefault = NewPathDefault(bgp.RF_IPv4_VPN, source, nlri, nil, isWithdraw, attrs, medSetByTargetNeighbor, now)
- if !isWithdraw {
- _, mpattr := ipv4VPNPath.getPathAttr(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI)
- ipv4VPNPath.nexthop = mpattr.(*bgp.PathAttributeMpReachNLRI).Nexthop
- }
+ ipv4VPNPath.PathDefault = NewPathDefault(bgp.RF_IPv4_VPN, source, nlri, isWithdraw, attrs, medSetByTargetNeighbor, now)
return ipv4VPNPath
}
@@ -470,7 +454,7 @@ func (ipv4vpnp *IPv4VPNPath) MarshalJSON() ([]byte, error) {
Age float64
}{
Network: ipv4vpnp.getPrefix(),
- Nexthop: ipv4vpnp.PathDefault.nexthop.String(),
+ Nexthop: ipv4vpnp.PathDefault.GetNexthop().String(),
Attrs: ipv4vpnp.PathDefault.getPathAttrs(),
Age: time.Now().Sub(ipv4vpnp.PathDefault.timestamp).Seconds(),
})
@@ -482,12 +466,7 @@ type EVPNPath struct {
func NewEVPNPath(source *PeerInfo, nlri bgp.AddrPrefixInterface, isWithdraw bool, attrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool, now time.Time) *EVPNPath {
EVPNPath := &EVPNPath{}
- EVPNPath.PathDefault = NewPathDefault(bgp.RF_EVPN, source, nlri, nil, isWithdraw, attrs, medSetByTargetNeighbor, now)
- if !isWithdraw {
- _, mpattr := EVPNPath.getPathAttr(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI)
- EVPNPath.nexthop = mpattr.(*bgp.PathAttributeMpReachNLRI).Nexthop
- }
-
+ EVPNPath.PathDefault = NewPathDefault(bgp.RF_EVPN, source, nlri, isWithdraw, attrs, medSetByTargetNeighbor, now)
return EVPNPath
}
@@ -527,7 +506,7 @@ func (evpnp *EVPNPath) MarshalJSON() ([]byte, error) {
Age float64
}{
Network: evpnp.getPrefix(),
- Nexthop: evpnp.PathDefault.nexthop.String(),
+ Nexthop: evpnp.PathDefault.GetNexthop().String(),
Attrs: evpnp.PathDefault.getPathAttrs(),
Age: time.Now().Sub(evpnp.PathDefault.timestamp).Seconds(),
})