Skip to content
Snippets Groups Projects
Commit 7150ca7a authored by cedi's avatar cedi
Browse files

move vrf-id to fib implementation

parent d950a782
Branches
No related tags found
No related merge requests found
......@@ -15,6 +15,9 @@ import (
type fibOsAdapter interface {
addPath(pfx bnet.Prefix, paths []*route.FIBPath) error
removePath(pfx bnet.Prefix, path *route.FIBPath) error
getFibName() string
needsVrfID() bool
setVrfID(vrdID string) error
}
// FIB is forwarding information base
......@@ -43,6 +46,22 @@ func New(v *vrf.VRF) (*FIB, error) {
return n, nil
}
// SetVrfID specifies the vrf id for the fib implementation.
// Some fib implementations don't need a VRF-ID.
// This method will return an error, in case the hardware specific fib implementation
// doesn't support or need a vrfId
func (f *FIB) SetVrfID(vrfID string) error {
if f.osAdapter != nil {
return fmt.Errorf("osAdapter is nil")
}
if !f.osAdapter.needsVrfID() {
return fmt.Errorf("The fib implementation for %s doesn't need a routing table identifier", f.osAdapter.getFibName())
}
return f.osAdapter.setVrfID(vrfID)
}
// Start the Netlink module
func (f *FIB) Start() error {
if f.osAdapter == nil {
......@@ -57,15 +76,16 @@ func (f *FIB) Start() error {
}
// register to all ribs in VRF
vrfRIBs := f.vrf.GetRIBNames()
for _, ribName := range vrfRIBs {
rib, found := f.vrf.RIBByName(ribName)
if !found {
continue
}
rib4, found := f.vrf.RIBByName("inet.0")
if found {
// from locRib to FIB
rib4.RegisterWithOptions(f, options)
}
rib6, found := f.vrf.RIBByName("inet6.0")
if found {
// from locRib to FIB
rib.RegisterWithOptions(f, options)
rib6.RegisterWithOptions(f, options)
}
return nil
......
......@@ -15,6 +15,10 @@ type osFibAdapterDarwin struct {
fib *FIB
}
func (fib *osFibAdapterLinux) getFibName() string {
return "darwin"
}
func newOSFIBDarwin(f *FIB) (*osFibAdapterDarwin, error) {
fib := &osFibAdapterDarwin{
fib: f,
......@@ -30,3 +34,11 @@ func (fib *osFibAdapterDarwin) addPath(pfx bnet.Prefix, paths []*route.FIBPath)
func (fib *osFibAdapterDarwin) removePath(pfx bnet.Prefix, path *route.FIBPath) error {
return fmt.Errorf("Not implemented")
}
func (fib *osFibAdapterDarwin) needsVrfID() bool {
return false
}
func (fib *osFibAdapterDarwin) setVrfID(vrdID string) error {
return fmt.Errorf("Not implemented")
}
......@@ -15,7 +15,24 @@ func (f *FIB) loadOSAdapter() {
}
type osFibAdapterLinux struct {
fib *FIB
fib *FIB
vrfID uint64
}
func (fib *osFibAdapterLinux) getFibName() string {
return "rt_netlink"
}
func (fib *osFibAdapterLinux) needsVrfID() bool {
return true
}
func (fib *osFibAdapterLinux) setVrfID(vrdID string) error {
// TODO: cast string to int
//fib.vrfID = (uint64)vrdID
// TODO: this is just a hack:
fib.vrfID = uint64(254)
return nil
}
func newOSFIBLinux(f *FIB) *osFibAdapterLinux {
......@@ -26,8 +43,8 @@ func newOSFIBLinux(f *FIB) *osFibAdapterLinux {
return linuxAdapter
}
func (f *osFibAdapterLinux) addPath(pfx bnet.Prefix, paths []*route.FIBPath) error {
route, err := f.createRoute(pfx, paths)
func (fib *osFibAdapterLinux) addPath(pfx bnet.Prefix, paths []*route.FIBPath) error {
route, err := fib.createRoute(pfx, paths)
if err != nil {
return errors.Wrap(err, "Could not create route from prefix and path: %v")
}
......@@ -45,8 +62,8 @@ func (f *osFibAdapterLinux) addPath(pfx bnet.Prefix, paths []*route.FIBPath) err
return nil
}
func (f *osFibAdapterLinux) removePath(pfx bnet.Prefix, path *route.FIBPath) error {
nlRoute, err := f.createRoute(pfx, []*route.FIBPath{path})
func (fib *osFibAdapterLinux) removePath(pfx bnet.Prefix, path *route.FIBPath) error {
nlRoute, err := fib.createRoute(pfx, []*route.FIBPath{path})
if err != nil {
return errors.Wrap(err, "Could not create route from prefix and path: %v")
}
......@@ -64,10 +81,14 @@ func (f *osFibAdapterLinux) removePath(pfx bnet.Prefix, path *route.FIBPath) err
}
// create a route from a prefix and a path
func (f *osFibAdapterLinux) createRoute(pfx bnet.Prefix, paths []*route.FIBPath) (*netlink.Route, error) {
func (fib *osFibAdapterLinux) createRoute(pfx bnet.Prefix, paths []*route.FIBPath) (*netlink.Route, error) {
if fib.vrfID == 0 {
return nil, fmt.Errorf("VRF-ID is not set")
}
route := &netlink.Route{
Dst: pfx.GetIPNet(),
Table: int(f.fib.vrf.ID()),
Table: int(fib.vrfID),
Protocol: route.ProtoBio,
}
......
......@@ -17,7 +17,7 @@ func TestNew(t *testing.T) {
}
func TestComparePfxPath(t *testing.T) {
v, _ := vrf.NewDefaultVRF()
v, _ := vrf.New("inet.0", 0)
f, _ := New(v)
tests := []struct {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment