From fac35059e6fb34007ee8f15777068d4e53715b5a Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Wed, 6 Dec 2023 00:27:59 +0900 Subject: [PATCH] Added local-pref to global policy --- api/restapi/handler/conntrack.go | 1 - api/restapi/handler/loadbalancer.go | 3 +- common/common.go | 4 +-- loxinet/gobgpclient.go | 56 +++++++++++++++++++++-------- 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/api/restapi/handler/conntrack.go b/api/restapi/handler/conntrack.go index 9bc26a64..9a36e5c3 100644 --- a/api/restapi/handler/conntrack.go +++ b/api/restapi/handler/conntrack.go @@ -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" diff --git a/api/restapi/handler/loadbalancer.go b/api/restapi/handler/loadbalancer.go index 221ab1b0..e5d6b972 100644 --- a/api/restapi/handler/loadbalancer.go +++ b/api/restapi/handler/loadbalancer.go @@ -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) @@ -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 diff --git a/common/common.go b/common/common.go index 317c690a..2c43bf81 100644 --- a/common/common.go +++ b/common/common.go @@ -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 ) diff --git a/loxinet/gobgpclient.go b/loxinet/gobgpclient.go index abb31d70..81c9ba5e 100644 --- a/loxinet/gobgpclient.go +++ b/loxinet/gobgpclient.go @@ -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) } } } @@ -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) } } } @@ -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") } } @@ -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) @@ -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 //}