Skip to content
Snippets Groups Projects
adj_rib_in.go 4 KiB
Newer Older
package adjRIBIn

import (
Oliver Herms's avatar
Oliver Herms committed
	"fmt"
Oliver Herms's avatar
Oliver Herms committed
	"sync"

	"github.com/bio-routing/bio-rd/routingtable/filter"

	"github.com/bio-routing/bio-rd/net"
	"github.com/bio-routing/bio-rd/route"
	"github.com/bio-routing/bio-rd/routingtable"
	log "github.com/sirupsen/logrus"
)

// AdjRIBIn represents an Adjacency RIB In as described in RFC4271
type AdjRIBIn struct {
	routingtable.ClientManager
	rt               *routingtable.RoutingTable
	mu               sync.RWMutex
	exportFilter     *filter.Filter
	contributingASNs *routingtable.ContributingASNs
Oliver Herms's avatar
Oliver Herms committed
	addPathRX        bool
Oliver Herms's avatar
Oliver Herms committed
// New creates a new Adjacency RIB In
Oliver Herms's avatar
Oliver Herms committed
func New(exportFilter *filter.Filter, contributingASNs *routingtable.ContributingASNs, routerID uint32, clusterID uint32, addPathRX bool) *AdjRIBIn {
		rt:               routingtable.NewRoutingTable(),
		exportFilter:     exportFilter,
		contributingASNs: contributingASNs,
Oliver Herms's avatar
Oliver Herms committed
		addPathRX:        addPathRX,
	a.ClientManager = routingtable.NewClientManager(a)
	return a
}

// UpdateNewClient sends current state to a new client
func (a *AdjRIBIn) UpdateNewClient(client routingtable.RouteTableClient) error {
	a.mu.RLock()
	defer a.mu.RUnlock()

	routes := a.rt.Dump()
	for _, route := range routes {
		paths := route.Paths()
		for _, path := range paths {
			path, reject := a.exportFilter.ProcessTerms(route.Prefix(), path)
			if reject {
				continue
			}

			err := client.AddPath(route.Prefix(), path)
			if err != nil {
				log.WithField("Sender", "AdjRIBOutAddPath").WithError(err).Error("Could not send update to client")
			}
		}
	}
	return nil
// RouteCount returns the number of stored routes
func (a *AdjRIBIn) RouteCount() int64 {
	return a.rt.GetRouteCount()
}

// AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added.
func (a *AdjRIBIn) AddPath(pfx net.Prefix, p *route.Path) error {
Oliver Herms's avatar
Oliver Herms committed
	a.mu.Lock()
	defer a.mu.Unlock()

	// RFC4456 Sect. 8: Ignore route with our RouterID as OriginatorID
	if p.BGPPath.OriginatorID == a.routerID {
		return nil
	}

	// RFC4456 Sect. 8: Ignore routes which contian our ClusterID in their ClusterList
	if len(p.BGPPath.ClusterList) > 0 {
		for _, cid := range p.BGPPath.ClusterList {
			if cid == a.clusterID {
				return nil
			}
		}
	}

Oliver Herms's avatar
Oliver Herms committed
	if a.addPathRX {
		a.rt.AddPath(pfx, p)
	} else {
		oldPaths := a.rt.ReplacePath(pfx, p)
		a.removePathsFromClients(pfx, oldPaths)
	}
	p, reject := a.exportFilter.ProcessTerms(pfx, p)
	if reject {
		return nil
	}

	// Bail out - for all clients for now - if any of our ASNs is within the path
	if a.ourASNsInPath(p) {
		return nil
	}

	for _, client := range a.ClientManager.Clients() {
		client.AddPath(pfx, p)
	}
func (a *AdjRIBIn) ourASNsInPath(p *route.Path) bool {
Daniel Czerwonk's avatar
Daniel Czerwonk committed
	for _, pathSegment := range p.BGPPath.ASPath {
		for _, asn := range pathSegment.ASNs {
			if a.contributingASNs.IsContributingASN(asn) {
				return true
			}
		}
	}

	return false
}

// RemovePath removes the path for prefix `pfx`
func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) bool {
Oliver Herms's avatar
Oliver Herms committed
	a.mu.Lock()
	defer a.mu.Unlock()

Oliver Herms's avatar
Oliver Herms committed
	r := a.rt.Get(pfx)
	if r == nil {
Oliver Herms's avatar
Oliver Herms committed
	removed := make([]*route.Path, 0)
Oliver Herms's avatar
Oliver Herms committed
	oldPaths := r.Paths()
	for _, path := range oldPaths {
Oliver Herms's avatar
Oliver Herms committed
		if a.addPathRX {
			fmt.Printf("Add Path RX!\n")
			if path.BGPPath.PathIdentifier != p.BGPPath.PathIdentifier {
				fmt.Printf("Path ID %v != %v. Ignoring.\n", path.BGPPath.PathIdentifier, p.BGPPath.PathIdentifier)
				continue
			}
		}

		fmt.Printf("Removing: %v => %v\n", pfx, *path.BGPPath)
Oliver Herms's avatar
Oliver Herms committed
		a.rt.RemovePath(pfx, path)
Oliver Herms's avatar
Oliver Herms committed
		removed = append(removed, path)
Oliver Herms's avatar
Oliver Herms committed
	a.removePathsFromClients(pfx, removed)
Oliver Herms's avatar
Oliver Herms committed

func (a *AdjRIBIn) removePathsFromClients(pfx net.Prefix, paths []*route.Path) {
	for _, path := range paths {
		path, reject := a.exportFilter.ProcessTerms(pfx, path)
		if reject {
			continue
		}
Oliver Herms's avatar
Oliver Herms committed
		for _, client := range a.ClientManager.Clients() {
			client.RemovePath(pfx, path)
		}
	}
}
Oliver Herms's avatar
Oliver Herms committed

func (a *AdjRIBIn) RT() *routingtable.RoutingTable {
	return a.rt
}