Skip to content

Commit

Permalink
Merge pull request #609 from TrekkieCoder/main
Browse files Browse the repository at this point in the history
PR - Support for static interface routes
  • Loading branch information
UltraInstinct14 authored Mar 30, 2024
2 parents bc3b914 + 1660a0f commit d66fd89
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 72 deletions.
16 changes: 8 additions & 8 deletions api/loxinlp/ipvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ type ipVSEntry struct {
EndPoints []ipvsEndPoint
}

type IpVSH struct {
type IPVSH struct {
RMap map[ipVSKey]*ipVSEntry
ticker *time.Ticker
tDone chan bool
handle *ipvs.Handle
}

var ipVSCtx *IpVSH
var ipVSCtx *IPVSH

func (ctx *IpVSH) BuildIpVSDB() []*ipVSEntry {
func (ctx *IPVSH) buildIPVSDB() []*ipVSEntry {

var ipVSList []*ipVSEntry
svcs, err := ctx.handle.GetServices()
Expand Down Expand Up @@ -128,7 +128,7 @@ func (ctx *IpVSH) BuildIpVSDB() []*ipVSEntry {
return ipVSList
}

func IpVSSync() {
func IPVSSync() {
for {
select {
case <-ipVSCtx.tDone:
Expand All @@ -139,7 +139,7 @@ func IpVSSync() {
ent.InValid = true
}

ipVSList := ipVSCtx.BuildIpVSDB()
ipVSList := ipVSCtx.buildIPVSDB()

for _, ent := range ipVSCtx.RMap {
if ent.InValid {
Expand Down Expand Up @@ -173,8 +173,8 @@ func IpVSSync() {
}
}

func IpVSInit() {
ipVSCtx = new(IpVSH)
func IPVSInit() {
ipVSCtx = new(IPVSH)
ipVSCtx.ticker = time.NewTicker(10 * time.Second)
ipVSCtx.RMap = make(map[ipVSKey]*ipVSEntry)
ipVSCtx.tDone = make(chan bool)
Expand All @@ -184,5 +184,5 @@ func IpVSInit() {
os.Exit(1)
}
ipVSCtx.handle = handle
go IpVSSync()
go IPVSSync()
}
28 changes: 18 additions & 10 deletions api/loxinlp/nlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import (
"encoding/json"
"errors"
"fmt"
cmn "github.com/loxilb-io/loxilb/common"
tk "github.com/loxilb-io/loxilib"
nlp "github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
"net"
"os"
"os/exec"
Expand All @@ -32,6 +28,11 @@ import (
"strings"
"syscall"
"time"

cmn "github.com/loxilb-io/loxilb/common"
tk "github.com/loxilb-io/loxilib"
nlp "github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
)

const (
Expand Down Expand Up @@ -86,7 +87,10 @@ type NlH struct {
BLRgx *regexp.Regexp
}

var hooks cmn.NetHookInterface
var (
skipIfRoute bool
hooks cmn.NetHookInterface
)

func NlpRegister(hook cmn.NetHookInterface) {
hooks = hook
Expand Down Expand Up @@ -1342,8 +1346,10 @@ func RUWorkSingle(m nlp.RouteUpdate) int {
return -1
}

if m.Route.Scope.String() == "link" && tk.IsNetIPv4(m.Dst.IP.String()) {
return -1
if skipIfRoute {
if m.Route.Scope.String() == "link" && tk.IsNetIPv4(m.Dst.IP.String()) {
return -1
}
}

if m.Type == syscall.RTM_NEWROUTE {
Expand Down Expand Up @@ -1550,8 +1556,10 @@ func NlpGet(ch chan bool) int {
tk.LogIt(tk.LogDebug, "[NLP] No STATIC routes found for intf %s\n", link.Attrs().Name)
} else {
for _, route := range routes {
if route.Scope.String() == "link" && tk.IsNetIPv4(route.Dst.IP.String()) {
continue
if skipIfRoute {
if route.Scope.String() == "link" && tk.IsNetIPv4(route.Dst.IP.String()) {
continue
}
}

AddRoute(route)
Expand Down Expand Up @@ -1690,7 +1698,7 @@ func NlpInit(bgpPeerMode bool, blackList string, ipvsCompat bool) *NlH {
go LbSessionGet(done)

if ipvsCompat {
IpVSInit()
IPVSInit()
}

return nNl
Expand Down
11 changes: 10 additions & 1 deletion loxinet/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,19 @@ func (na *NetAPIStruct) NetRouteAdd(rm *cmn.RouteMod) (int, error) {
if na.BgpPeerMode {
return RtNhErr, errors.New("running in bgp only mode")
}
intfRt := false
mlen, _ := rm.Dst.Mask.Size()
if rm.Gw == nil {
// This is an interface route
if (tk.IsNetIPv4(rm.Dst.IP.String()) && mlen == 32) || (tk.IsNetIPv6(rm.Dst.IP.String()) && mlen == 128) {
intfRt = true
rm.Gw = rm.Dst.IP
}
}
mh.mtx.Lock()
defer mh.mtx.Unlock()

ra := RtAttr{rm.Protocol, rm.Flags, false, rm.LinkIndex}
ra := RtAttr{Protocol: rm.Protocol, OSFlags: rm.Flags, HostRoute: false, Ifi: rm.LinkIndex, IfRoute: intfRt}
if rm.Gw != nil {
na := []RtNhAttr{{rm.Gw, rm.LinkIndex}}
ret, err = mh.zr.Rt.RtAdd(rm.Dst, RootZone, ra, na)
Expand Down
4 changes: 2 additions & 2 deletions loxinet/layer3.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (l3 *L3H) IfaAdd(Obj string, Cidr string) (int, error) {
l3.IfaMap[key] = ifa

// ifa needs related self-routes
ra := RtAttr{0, 0, false, ifObjID}
ra := RtAttr{0, 0, false, ifObjID, false}
_, err = mh.zr.Rt.RtAdd(*network, RootZone, ra, nil)
if err != nil {
tk.LogIt(tk.LogDebug, "ifa add - %s:%s subnet-rt error\n", addr.String(), Obj)
Expand Down Expand Up @@ -155,7 +155,7 @@ func (l3 *L3H) IfaAdd(Obj string, Cidr string) (int, error) {

// ifa needs to related self-routes
// FIXME - Code duplication with primary address route above
ra := RtAttr{0, 0, false, ifObjID}
ra := RtAttr{0, 0, false, ifObjID, false}
_, err = mh.zr.Rt.RtAdd(*network, RootZone, ra, nil)
if err != nil {
tk.LogIt(tk.LogDebug, " - %s:%s subnet-rt error\n", addr.String(), Obj)
Expand Down
2 changes: 1 addition & 1 deletion loxinet/loxinettest.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestLoxinet(t *testing.T) {
mask := net.CIDRMask(24, 32)
route = route.Mask(mask)
ipnet := net.IPNet{IP: route, Mask: mask}
ra := RtAttr{0, 0, false, -1}
ra := RtAttr{0, 0, false, -1, false}
na := []RtNhAttr{{net.IPv4(8, 8, 8, 8), 12}}
_, err = mh.zr.Rt.RtAdd(ipnet, "default", ra, na)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions loxinet/neighbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (ne *Neigh) NeighRemoveTunEP(i int) []*NeighTunEp {

// NeighDelAllTunEP - delete all tun-eps from a neighbor
func (n *NeighH) NeighDelAllTunEP(ne *Neigh) int {
var i int = 0
i := 0
for _, tep := range ne.TunEps {
tep.DP(DpRemove)
n.NeighTID.PutCounter(tep.Mark)
Expand Down Expand Up @@ -385,7 +385,7 @@ func (n *NeighH) NeighAdd(Addr net.IP, Zone string, Attr NeighAttr) (int, error)
mask = net.CIDRMask(128, 128)
}
ipnet := net.IPNet{IP: Addr, Mask: mask}
ra := RtAttr{0, 0, true, Attr.OSLinkIndex}
ra := RtAttr{0, 0, true, Attr.OSLinkIndex, false}
na := []RtNhAttr{{Addr, Attr.OSLinkIndex}}

if found == true {
Expand Down Expand Up @@ -462,7 +462,7 @@ NhExist:

code, err := n.Zone.L2.L2FdbAdd(fdbKey, fdbAttr)
if err != nil && code != L2SameFdbErr {
n.Zone.Rt.RtDelete(ipnet, Zone)
n.Zone.Rt.RtDeleteHost(ipnet, Zone)
n.NeighDelete(Addr, Zone)
tk.LogIt(tk.LogError, "neigh add - %s:%s mac fail\n", Addr.String(), Zone)
return NeighMacErr, errors.New("nh-mac error")
Expand Down Expand Up @@ -525,7 +525,7 @@ func (n *NeighH) NeighDelete(Addr net.IP, Zone string) (int, error) {

// Delete the host specific to this NH
ipnet := net.IPNet{IP: Addr, Mask: mask}
_, err := n.Zone.Rt.RtDelete(ipnet, Zone)
_, err := n.Zone.Rt.RtDeleteHost(ipnet, Zone)
if err != nil {
tk.LogIt(tk.LogError, "neigh delete - %s:%s host-rt fail\n", Addr.String(), Zone)
/*return NeighHostRtErr, errors.New("nh-hostrt error" + err.Error())*/
Expand Down Expand Up @@ -582,7 +582,7 @@ func (n *NeighH) NeighFind(Addr net.IP, Zone string) (*Neigh, int) {
return nil, -1
}

return ne, -1
return ne, 0
}

// NeighPairRt - Associate a route with the given neighbor
Expand Down
Loading

0 comments on commit d66fd89

Please sign in to comment.