Skip to content

Commit

Permalink
RIS-Mirror: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
taktv6 committed Feb 26, 2023
1 parent ee76fb0 commit 6b9cea0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/ris-mirror/rismirror/rismirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 27 additions & 7 deletions cmd/ris-mirror/rismirror/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rismirror

import (
"net"
"sync"

"github.com/bio-routing/bio-rd/risclient"
"github.com/bio-routing/bio-rd/routingtable/vrf"
Expand All @@ -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),
}
}

Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions cmd/ris-mirror/rismirror/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 6b9cea0

Please sign in to comment.