Skip to content

Commit

Permalink
Merge pull request #450 from TrekkieCoder/main
Browse files Browse the repository at this point in the history
PR: Added local-pref to loxilb gobgp global policy
  • Loading branch information
UltraInstinct14 authored Dec 5, 2023
2 parents 73f7935 + fac3505 commit 25aab2e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
1 change: 0 additions & 1 deletion api/restapi/handler/conntrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package handler

import (

"github.com/loxilb-io/loxilb/api/models"
"github.com/loxilb-io/loxilb/api/restapi/operations"
tk "github.com/loxilb-io/loxilib"
Expand Down
3 changes: 1 addition & 2 deletions api/restapi/handler/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func ConfigGetLoadbalancer(params operations.GetConfigLoadbalancerAllParams) mid
// Get LB rules
tk.LogIt(tk.LogDebug, "[API] Load balancer %s API called. url : %s\n", params.HTTPRequest.Method, params.HTTPRequest.URL)


res, err := ApiHooks.NetLbRuleGet()
if err != nil {
tk.LogIt(tk.LogDebug, "[API] Error occur : %v\n", err)
Expand All @@ -113,7 +112,7 @@ func ConfigGetLoadbalancer(params operations.GetConfigLoadbalancerAllParams) mid
for _, lb := range res {
var tmpLB models.LoadbalanceEntry
var tmpSvc models.LoadbalanceEntryServiceArguments

// Service Arg match
tmpSvc.ExternalIP = lb.Serv.ServIP
tmpSvc.Bgp = lb.Serv.Bgp
Expand Down
4 changes: 2 additions & 2 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ const (
HighLocalPref = 5000
// LowLocalPref - Low local preference for advertising BGP route(Backup)
LowLocalPref = 100
// HighMed - Low metric means higher probability for selection outiside AS
// HighMed - Low metric means higher probability for selection outside AS
HighMed = 10
// LowMed - High metric means lower probability for selection outiside AS
// LowMed - High metric means lower probability for selection outside AS
LowMed = 20
)

Expand Down
56 changes: 41 additions & 15 deletions loxinet/gobgpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,9 +766,9 @@ func (gbh *GoBgpH) initBgpClient() {

if ciname == cmn.CIDefault {
if ci.hastate == cmn.CIStateBackup {
gbh.resetBGPMed(true)
gbh.resetBGPPolicy(true)
} else if ci.hastate == cmn.CIStateMaster {
gbh.resetBGPMed(false)
gbh.resetBGPPolicy(false)
}
}
}
Expand Down Expand Up @@ -838,9 +838,9 @@ func (gbh *GoBgpH) UpdateCIState(instance string, state int, vip net.IP) {
if update {
if instance == cmn.CIDefault {
if ci.hastate == cmn.CIStateBackup {
gbh.resetBGPMed(true)
gbh.resetBGPPolicy(true)
} else if ci.hastate == cmn.CIStateMaster {
gbh.resetBGPMed(false)
gbh.resetBGPPolicy(false)
}
}
}
Expand Down Expand Up @@ -883,22 +883,22 @@ func (gbh *GoBgpH) resetNeighAdj() error {
return nil
}

// resetBGPMed - Reset BGP Med attribute
func (gbh *GoBgpH) resetBGPMed(toLow bool) error {
// resetBGPPolicy - Reset BGP Policy attributes
func (gbh *GoBgpH) resetBGPPolicy(toLow bool) error {

if !toLow {
if _, err := gbh.removeExportPolicy("global", "set-med-export-gpolicy"); err != nil {
tk.LogIt(tk.LogError, "[GoBGP] Error removing set-med-export policy%s\n", err.Error())
if _, err := gbh.removeExportPolicy("global", "set-llb-export-gpolicy"); err != nil {
tk.LogIt(tk.LogError, "[GoBGP] Error removing set-llb-export policy%s\n", err.Error())
// return err
} else {
tk.LogIt(tk.LogInfo, "[GoBGP] Removed set-med-export policy\n")
tk.LogIt(tk.LogInfo, "[GoBGP] Removed set-llb-export policy\n")
}
} else {
if _, err := gbh.applyExportPolicy("global", "set-med-export-gpolicy"); err != nil {
tk.LogIt(tk.LogError, "[GoBGP] Error applying set-med-export policy%s\n", err.Error())
if _, err := gbh.applyExportPolicy("global", "set-llb-export-gpolicy"); err != nil {
tk.LogIt(tk.LogError, "[GoBGP] Error applying set-llb-export policy%s\n", err.Error())
//return err
} else {
tk.LogIt(tk.LogInfo, "[GoBGP] Applied set-med-export policy\n")
tk.LogIt(tk.LogInfo, "[GoBGP] Applied set-llb-export policy\n")
}
}

Expand Down Expand Up @@ -993,6 +993,23 @@ func (gbh *GoBgpH) createSetMedPolicy(name string, val int64) (int, error) {
return 0, err
}

// createSetLocalPrefPolicy - Routine to create set local-pref statement
func (gbh *GoBgpH) createSetLocalPrefPolicy(name string, val uint32) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
st := &api.Statement{
Name: name,
Actions: &api.Actions{},
}
st.Actions.LocalPref = &api.LocalPrefAction{}
st.Actions.LocalPref.Value = val
_, err := gbh.client.AddStatement(ctx,
&api.AddStatementRequest{
Statement: st,
})
return 0, err
}

// addPolicy - Routine to apply global policy statement
func (gbh *GoBgpH) addPolicy(name string, stmt string) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
Expand Down Expand Up @@ -1113,14 +1130,23 @@ func (gbh *GoBgpH) BGPGlobalConfigAdd(config cmn.GoBGPGlobalConfig) (int, error)
tk.LogIt(tk.LogError, "[GoBGP] Error creating set-med-export-gstmt stmt %s\n", err.Error())
return 0, err
}
// Create the set-local-pref policy statement
if _, err := gbh.createSetLocalPrefPolicy("set-localpref-export-gstmt", 10); err != nil {
tk.LogIt(tk.LogError, "[GoBGP] Error creating set-localpref-export-gstmt %s\n", err.Error())
return 0, err
}
// Create the global policy
if _, err := gbh.addPolicy("set-med-export-gpolicy", "set-med-export-gstmt"); err != nil {
if _, err := gbh.addPolicy("set-llb-export-gpolicy", "set-med-export-gstmt"); err != nil {
tk.LogIt(tk.LogError, "[GoBGP] Error creating set-med-export policy%s\n", err.Error())
return 0, err
}
if _, err := gbh.addPolicy("set-llb-export-gpolicy", "set-localpref-export-gstmt"); err != nil {
tk.LogIt(tk.LogError, "[GoBGP] Error creating set-localpref-export-gstmt policy%s\n", err.Error())
return 0, err
}
// Apply the global policy
//if _, err := gbh.applyExportPolicy("global", "set-med-export-gpolicy"); err != nil {
// tk.LogIt(tk.LogError, "[GoBGP] Error applying set-med-export policy%s\n", err.Error())
//if _, err := gbh.applyExportPolicy("global", "set-llb-export-gpolicy"); err != nil {
// tk.LogIt(tk.LogError, "[GoBGP] Error applying set-llb-export policy%s\n", err.Error())
// return 0, err
//}

Expand Down

0 comments on commit 25aab2e

Please sign in to comment.