diff --git a/cmd/ris-mirror/rismirror/metrics/ris_mirror_metrics.go b/cmd/ris-mirror/rismirror/metrics/ris_mirror_metrics.go index 3d8948cb..342a15b3 100644 --- a/cmd/ris-mirror/rismirror/metrics/ris_mirror_metrics.go +++ b/cmd/ris-mirror/rismirror/metrics/ris_mirror_metrics.go @@ -16,8 +16,8 @@ type RISMirrorMetrics struct { type RISMirrorRouterMetrics struct { Address net.IP SysName string - VRFMetrics []*vrf_metrics.VRFMetrics InternalVRFMetrics []*InternalVRFMetrics + VRFMetrics []*vrf_metrics.VRFMetrics } // InternalVRFMetrics represents internal VRF metrics (_vrf) diff --git a/cmd/ris-mirror/rismirror/rismirror.go b/cmd/ris-mirror/rismirror/rismirror.go index 3fed598f..90a343a0 100644 --- a/cmd/ris-mirror/rismirror/rismirror.go +++ b/cmd/ris-mirror/rismirror/rismirror.go @@ -71,8 +71,8 @@ func (rism *RISMirror) Metrics() *metrics.RISMirrorMetrics { rm := &metrics.RISMirrorRouterMetrics{ Address: r.Address(), SysName: r.Name(), - VRFMetrics: vrf.Metrics(r.(*Router).vrfRegistry), InternalVRFMetrics: make([]*metrics.InternalVRFMetrics, 0), + VRFMetrics: vrf.Metrics(r.(*Router).vrfRegistry), } for rd, v := range r.(*Router).vrfs { diff --git a/cmd/ris-mirror/rismirror/router.go b/cmd/ris-mirror/rismirror/router.go index 05be6cfe..7555e2a1 100644 --- a/cmd/ris-mirror/rismirror/router.go +++ b/cmd/ris-mirror/rismirror/router.go @@ -2,6 +2,7 @@ package rismirror import ( "net" + "sync" "github.com/bio-routing/bio-rd/risclient" "github.com/bio-routing/bio-rd/routingtable/vrf" @@ -14,16 +15,17 @@ import ( type Router struct { name string address net.IP - vrfRegistry *vrf.VRFRegistry - vrfs map[uint64]*_vrf + vrfs map[uint64]*vrfWithMergedLocRIBs // this is the authoritative data store for VRFs + vrfsMu sync.RWMutex + vrfRegistry *vrf.VRFRegistry // this is only there so that the metrics functionality of the vrf package can be used } func newRouter(name string, address net.IP) *Router { return &Router{ name: name, address: address, + vrfs: make(map[uint64]*vrfWithMergedLocRIBs), vrfRegistry: vrf.NewVRFRegistry(), - vrfs: make(map[uint64]*_vrf), } } @@ -43,18 +45,36 @@ func (r *Router) Ready(vrf uint64, afi uint16) (bool, error) { // GetVRF gets a VRF by its RD func (r *Router) GetVRF(rd uint64) *vrf.VRF { - return r.vrfRegistry.GetVRFByName(vrf.RouteDistinguisherHumanReadable(rd)) + r.vrfsMu.RLock() + defer r.vrfsMu.RUnlock() + + _vrf := r.vrfs[rd] + if _vrf == nil { + return nil + } + + return _vrf.vrf } // GetVRFs gets all VRFs func (r *Router) GetVRFs() []*vrf.VRF { - return r.vrfRegistry.List() + r.vrfsMu.RLock() + defer r.vrfsMu.RUnlock() + + ret := make([]*vrf.VRF, 0, len(r.vrfs)) + for _, v := range r.vrfs { + ret = append(ret, v.vrf) + } + + return ret } func (r *Router) addVRF(rd uint64, sources []*grpc.ClientConn) { - v := r.vrfRegistry.CreateVRFIfNotExists(vrf.RouteDistinguisherHumanReadable(rd), rd) + r.vrfsMu.Lock() + defer r.vrfsMu.Unlock() - r.vrfs[rd] = newVRF(v.IPv4UnicastRIB(), v.IPv6UnicastRIB()) + v := r.vrfRegistry.CreateVRFIfNotExists(vrf.RouteDistinguisherHumanReadable(rd), rd) + r.vrfs[rd] = newVRFWithMergedLocRIBs(v.IPv4UnicastRIB(), v.IPv6UnicastRIB()) for _, src := range sources { r.connectVRF(rd, src, 4) diff --git a/cmd/ris-mirror/rismirror/vrf.go b/cmd/ris-mirror/rismirror/vrf.go index 40ddfb16..dd55b8b0 100644 --- a/cmd/ris-mirror/rismirror/vrf.go +++ b/cmd/ris-mirror/rismirror/vrf.go @@ -3,21 +3,23 @@ package rismirror import ( "github.com/bio-routing/bio-rd/routingtable/locRIB" "github.com/bio-routing/bio-rd/routingtable/mergedlocrib" + "github.com/bio-routing/bio-rd/routingtable/vrf" ) -type _vrf struct { +type vrfWithMergedLocRIBs struct { + vrf *vrf.VRF ipv4Unicast *mergedlocrib.MergedLocRIB ipv6Unicast *mergedlocrib.MergedLocRIB } -func newVRF(locRIBIPv4Unicast, locRIBIPv6Unicast *locRIB.LocRIB) *_vrf { - return &_vrf{ +func newVRFWithMergedLocRIBs(locRIBIPv4Unicast, locRIBIPv6Unicast *locRIB.LocRIB) *vrfWithMergedLocRIBs { + return &vrfWithMergedLocRIBs{ ipv4Unicast: mergedlocrib.New(locRIBIPv4Unicast), ipv6Unicast: mergedlocrib.New(locRIBIPv6Unicast), } } -func (v *_vrf) getRIB(afi uint8) *mergedlocrib.MergedLocRIB { +func (v *vrfWithMergedLocRIBs) getRIB(afi uint8) *mergedlocrib.MergedLocRIB { if afi == 6 { return v.ipv6Unicast }