Skip to content
Snippets Groups Projects
Commit 1bb12c52 authored by cedi's avatar cedi
Browse files

pullrequest comments from @taktv6

parent db54622f
No related branches found
No related tags found
No related merge requests found
...@@ -8,10 +8,10 @@ import ( ...@@ -8,10 +8,10 @@ import (
// Constants for default routing tables in the Linux Kernel // Constants for default routing tables in the Linux Kernel
const ( const (
RtLocal int = 255 // according to man ip-route: 255 is reserved fro built-in use RtLocal uint32 = 255 // according to man ip-route: 255 is reserved for built-in use
RtMain int = 254 // This is the default table where routes are inserted RtMain uint32 = 254 // This is the default table where routes are inserted
RtDefault int = 253 // according to man ip-route: 253 is reserved fro built-in use RtDefault uint32 = 253 // according to man ip-route: 253 is reserved for built-in use
RtUnspec int = 0 // according to man ip-route: 0 is reserved fro built-in use RtUnspec uint32 = 0 // according to man ip-route: 0 is reserved for built-in use
) )
...@@ -19,7 +19,7 @@ const ( ...@@ -19,7 +19,7 @@ const (
type Netlink struct { type Netlink struct {
HoldTime time.Duration HoldTime time.Duration
UpdateInterval time.Duration UpdateInterval time.Duration
RoutingTable int RoutingTable uint32
ImportFilter *filter.Filter // Which routes are imported from the Kernel ImportFilter *filter.Filter // Which routes are imported from the Kernel
ExportFilter *filter.Filter // Which routes are exported to the Kernel ExportFilter *filter.Filter // Which routes are exported to the Kernel
} }
...@@ -3,7 +3,7 @@ package net ...@@ -3,7 +3,7 @@ package net
import ( import (
"fmt" "fmt"
"math" "math"
"net" gonet "net"
"strconv" "strconv"
"strings" "strings"
) )
...@@ -22,8 +22,8 @@ func NewPfx(addr IP, pfxlen uint8) Prefix { ...@@ -22,8 +22,8 @@ func NewPfx(addr IP, pfxlen uint8) Prefix {
} }
} }
// NewPfxFromIPNet creates a Prefix object from an net.IPNet object // NewPfxFromIPNet creates a Prefix object from an gonet.IPNet object
func NewPfxFromIPNet(ipNet *net.IPNet) Prefix { func NewPfxFromIPNet(ipNet *gonet.IPNet) Prefix {
ones, _ := ipNet.Mask.Size() ones, _ := ipNet.Mask.Size()
ip, _ := IPFromBytes(ipNet.IP) ip, _ := IPFromBytes(ipNet.IP)
...@@ -72,16 +72,16 @@ func (pfx Prefix) String() string { ...@@ -72,16 +72,16 @@ func (pfx Prefix) String() string {
return fmt.Sprintf("%s/%d", pfx.addr, pfx.pfxlen) return fmt.Sprintf("%s/%d", pfx.addr, pfx.pfxlen)
} }
// GetIPNet returns the net.IP object for a Prefix object // GetIPNet returns the gonet.IP object for a Prefix object
func (pfx Prefix) GetIPNet() *net.IPNet { func (pfx Prefix) GetIPNet() *gonet.IPNet {
var dstNetwork net.IPNet var dstNetwork gonet.IPNet
dstNetwork.IP = pfx.Addr().Bytes() dstNetwork.IP = pfx.Addr().Bytes()
pfxLen := int(pfx.Pfxlen()) pfxLen := int(pfx.Pfxlen())
if pfx.Addr().IsIPv4() { if pfx.Addr().IsIPv4() {
dstNetwork.Mask = net.CIDRMask(pfxLen, 32) dstNetwork.Mask = gonet.CIDRMask(pfxLen, 32)
} else { } else {
dstNetwork.Mask = net.CIDRMask(pfxLen, 128) dstNetwork.Mask = gonet.CIDRMask(pfxLen, 128)
} }
return &dstNetwork return &dstNetwork
......
...@@ -44,14 +44,12 @@ func NewNetlinkReader(options *config.Netlink) *NetlinkReader { ...@@ -44,14 +44,12 @@ func NewNetlinkReader(options *config.Netlink) *NetlinkReader {
// Read reads routes from the kernel // Read reads routes from the kernel
func (nr *NetlinkReader) Read() { func (nr *NetlinkReader) Read() {
log.WithField("rt_table", nr.options.RoutingTable).Info("Started netlink server")
// Start fetching the kernel routes after the hold time // Start fetching the kernel routes after the hold time
time.Sleep(nr.options.HoldTime) time.Sleep(nr.options.HoldTime)
for { for {
// Family doesn't matter. I only filter by the rt_table here // Family doesn't matter. I only filter by the rt_table here
routes, err := netlink.RouteListFiltered(IPFamily4, &netlink.Route{Table: nr.options.RoutingTable}, netlink.RT_FILTER_TABLE) routes, err := netlink.RouteListFiltered(int(IPFamily4), &netlink.Route{Table: int(nr.options.RoutingTable)}, netlink.RT_FILTER_TABLE)
if err != nil { if err != nil {
log.WithError(err).Panic("Failed to read routes from kernel") log.WithError(err).Panic("Failed to read routes from kernel")
} }
...@@ -96,8 +94,7 @@ func (nr *NetlinkReader) addPathsToClients(routes []netlink.Route) { ...@@ -96,8 +94,7 @@ func (nr *NetlinkReader) addPathsToClients(routes []netlink.Route) {
nr.mu.RUnlock() nr.mu.RUnlock()
for _, r := range advertise { for _, r := range advertise {
// Is it a BIO-Written route? if so, skip it, dont advertise it if isBioRoute(r) {
if r.Protocol == route.ProtoBio {
log.WithFields(routeLogFields(r)).Debug("Skipping bio route") log.WithFields(routeLogFields(r)).Debug("Skipping bio route")
continue continue
} }
...@@ -113,7 +110,6 @@ func (nr *NetlinkReader) addPathsToClients(routes []netlink.Route) { ...@@ -113,7 +110,6 @@ func (nr *NetlinkReader) addPathsToClients(routes []netlink.Route) {
continue continue
} }
// Apply filter (if existing)
if nr.filter != nil { if nr.filter != nil {
var reject bool var reject bool
// TODO: Implement filter that cann handle netlinkRoute objects // TODO: Implement filter that cann handle netlinkRoute objects
...@@ -138,6 +134,11 @@ func (nr *NetlinkReader) addPathsToClients(routes []netlink.Route) { ...@@ -138,6 +134,11 @@ func (nr *NetlinkReader) addPathsToClients(routes []netlink.Route) {
} }
} }
// Is route a BIO-Written route?
func isBioRoute(r netlink.Route) bool {
return uint32(r.Protocol) == route.ProtoBio
}
// Remove given paths from clients // Remove given paths from clients
func (nr *NetlinkReader) removePathsFromClients(routes []netlink.Route) { func (nr *NetlinkReader) removePathsFromClients(routes []netlink.Route) {
nr.mu.RLock() nr.mu.RLock()
...@@ -169,7 +170,6 @@ func (nr *NetlinkReader) removePathsFromClients(routes []netlink.Route) { ...@@ -169,7 +170,6 @@ func (nr *NetlinkReader) removePathsFromClients(routes []netlink.Route) {
continue continue
} }
// Apply filter (if existing)
if nr.filter != nil { if nr.filter != nil {
var reject bool var reject bool
// TODO: Implement filter that cann handle netlinkRoute objects // TODO: Implement filter that cann handle netlinkRoute objects
......
...@@ -19,16 +19,16 @@ type NetlinkWriter struct { ...@@ -19,16 +19,16 @@ type NetlinkWriter struct {
filter *filter.Filter filter *filter.Filter
// Routingtable for buffering, to ensure no double writes (a.k.a rtnetlink: file exists) // Routingtable for buffering, to ensure no double writes (a.k.a rtnetlink: file exists)
mu sync.RWMutex mu sync.RWMutex
pt map[bnet.Prefix][]*route.Path pathTable map[bnet.Prefix][]*route.Path
} }
// NewNetlinkWriter creates a new NetlinkWriter object and returns the pointer to it // NewNetlinkWriter creates a new NetlinkWriter object and returns the pointer to it
func NewNetlinkWriter(options *config.Netlink) *NetlinkWriter { func NewNetlinkWriter(options *config.Netlink) *NetlinkWriter {
return &NetlinkWriter{ return &NetlinkWriter{
options: options, options: options,
filter: options.ExportFilter, filter: options.ExportFilter,
pt: make(map[bnet.Prefix][]*route.Path), pathTable: make(map[bnet.Prefix][]*route.Path),
} }
} }
...@@ -56,19 +56,19 @@ func (nw *NetlinkWriter) Unregister(routingtable.RouteTableClient) { ...@@ -56,19 +56,19 @@ func (nw *NetlinkWriter) Unregister(routingtable.RouteTableClient) {
func (nw *NetlinkWriter) RouteCount() int64 { func (nw *NetlinkWriter) RouteCount() int64 {
nw.mu.RLock() nw.mu.RLock()
defer nw.mu.RUnlock() defer nw.mu.RUnlock()
return int64(len(nw.pt)) return int64(len(nw.pathTable))
} }
// AddPath adds a path to the Kernel using netlink. This function is triggered by the loc_rib, cause we are subscribed as client in the loc_rib // AddPath adds a path to the Kernel using netlink. This function is triggered by the loc_rib, cause we are subscribed as client in the loc_rib
func (nw *NetlinkWriter) AddPath(pfx bnet.Prefix, path *route.Path) error { func (nw *NetlinkWriter) AddPath(pfx bnet.Prefix, path *route.Path) error {
// check if for this prefix already a route is existing // check if for this prefix already a route is existing
existingPaths, ok := nw.pt[pfx] existingPaths, ok := nw.pathTable[pfx]
// if no route exists, add that route // if no route exists, add that route
if existingPaths == nil || !ok { if existingPaths == nil || !ok {
paths := make([]*route.Path, 1) paths := make([]*route.Path, 1)
paths = append(paths, path) paths = append(paths, path)
nw.pt[pfx] = paths nw.pathTable[pfx] = paths
// add the route to kernel // add the route to kernel
return nw.addKernel(pfx, path) return nw.addKernel(pfx, path)
...@@ -82,7 +82,7 @@ func (nw *NetlinkWriter) AddPath(pfx bnet.Prefix, path *route.Path) error { ...@@ -82,7 +82,7 @@ func (nw *NetlinkWriter) AddPath(pfx bnet.Prefix, path *route.Path) error {
} }
existingPaths = append(existingPaths, path) existingPaths = append(existingPaths, path)
nw.pt[pfx] = existingPaths nw.pathTable[pfx] = existingPaths
// now add to netlink // now add to netlink
return nw.addKernel(pfx, path) return nw.addKernel(pfx, path)
...@@ -91,7 +91,7 @@ func (nw *NetlinkWriter) AddPath(pfx bnet.Prefix, path *route.Path) error { ...@@ -91,7 +91,7 @@ func (nw *NetlinkWriter) AddPath(pfx bnet.Prefix, path *route.Path) error {
// RemovePath removes a path from the Kernel using netlink This function is triggered by the loc_rib, cause we are subscribed as client in the loc_rib // RemovePath removes a path from the Kernel using netlink This function is triggered by the loc_rib, cause we are subscribed as client in the loc_rib
func (nw *NetlinkWriter) RemovePath(pfx bnet.Prefix, path *route.Path) bool { func (nw *NetlinkWriter) RemovePath(pfx bnet.Prefix, path *route.Path) bool {
// check if for this prefix already a route is existing // check if for this prefix already a route is existing
existingPaths, ok := nw.pt[pfx] existingPaths, ok := nw.pathTable[pfx]
// if no route exists, nothing to do // if no route exists, nothing to do
if existingPaths == nil || !ok { if existingPaths == nil || !ok {
...@@ -118,7 +118,7 @@ func (nw *NetlinkWriter) RemovePath(pfx bnet.Prefix, path *route.Path) bool { ...@@ -118,7 +118,7 @@ func (nw *NetlinkWriter) RemovePath(pfx bnet.Prefix, path *route.Path) bool {
if remove { if remove {
existingPaths = append(existingPaths[:removeIdx], existingPaths[removeIdx+1:]...) existingPaths = append(existingPaths[:removeIdx], existingPaths[removeIdx+1:]...)
nw.pt[pfx] = existingPaths nw.pathTable[pfx] = existingPaths
} }
return true return true
...@@ -201,8 +201,8 @@ func (nw *NetlinkWriter) createRouteFromNetlink(pfx bnet.Prefix, path *route.Pat ...@@ -201,8 +201,8 @@ func (nw *NetlinkWriter) createRouteFromNetlink(pfx bnet.Prefix, path *route.Pat
Gw: nlPath.NextHop.Bytes(), Gw: nlPath.NextHop.Bytes(),
Priority: nlPath.Priority, Priority: nlPath.Priority,
Type: nlPath.Type, Type: nlPath.Type,
Table: nw.options.RoutingTable, // config dependent Table: int(nw.options.RoutingTable), // config dependent
Protocol: route.ProtoBio, // fix Protocol: route.ProtoBio, // fix
}, nil }, nil
} }
...@@ -220,8 +220,8 @@ func (nw *NetlinkWriter) createRouteFromBGPPath(pfx bnet.Prefix, path *route.Pat ...@@ -220,8 +220,8 @@ func (nw *NetlinkWriter) createRouteFromBGPPath(pfx bnet.Prefix, path *route.Pat
return &netlink.Route{ return &netlink.Route{
Dst: pfx.GetIPNet(), Dst: pfx.GetIPNet(),
Gw: bgpPath.NextHop.Bytes(), Gw: bgpPath.NextHop.Bytes(),
Table: nw.options.RoutingTable, // config dependent Table: int(nw.options.RoutingTable), // config dependent
Protocol: route.ProtoBio, // fix Protocol: route.ProtoBio, // fix
}, nil }, nil
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment