From c4885a13d41d7bb4f407155933590d616316dd18 Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Mon, 16 Oct 2023 13:43:44 +0900 Subject: [PATCH 1/4] incluster gobgp fixes --- cicd/k3s-flannel-incluster/rmconfig.sh | 1 - loxinet/gobgpclient.go | 164 ++++++++++++++++++++++--- 2 files changed, 149 insertions(+), 16 deletions(-) diff --git a/cicd/k3s-flannel-incluster/rmconfig.sh b/cicd/k3s-flannel-incluster/rmconfig.sh index bd4b79e81..83b1f95a3 100755 --- a/cicd/k3s-flannel-incluster/rmconfig.sh +++ b/cicd/k3s-flannel-incluster/rmconfig.sh @@ -1,5 +1,4 @@ #!/bin/bash -sudo ip route del 123.123.123.1 via 192.168.90.10 || true vagrant destroy -f worker1 vagrant destroy -f worker2 vagrant destroy -f master1 diff --git a/loxinet/gobgpclient.go b/loxinet/gobgpclient.go index 433300463..728abbb46 100644 --- a/loxinet/gobgpclient.go +++ b/loxinet/gobgpclient.go @@ -77,6 +77,13 @@ type goCI struct { rules map[string]int } +// goBGPNeigh - goBGP neighbor +type goBGPNeigh struct { + name string + remoteAS uint32 + remotePort uint32 +} + // GoBgpH - context container type GoBgpH struct { eventCh chan goBgpEvent @@ -90,6 +97,17 @@ type GoBgpH struct { noNlp bool localAs uint32 ciMap map[string]*goCI + nbMap map[string]*goBGPNeigh +} + +func (gbh *GoBgpH) getGlobalConfig() error { + r, err := gbh.client.GetBgp(context.Background(), &api.GetBgpRequest{}) + if err != nil { + return err + } + + gbh.localAs = r.Global.Asn + return nil } func (gbh *GoBgpH) getPathAttributeString(nlri bgp.AddrPrefixInterface, attrs []bgp.PathAttributeInterface) string { @@ -239,8 +257,10 @@ func (gbh *GoBgpH) syncRoute(p *goBgpRouteInfo, showIdentifier bgp.BGPAddPathMod func (gbh *GoBgpH) processRoute(pathList []*api.Path) { for _, p := range pathList { - if !p.Best || p.IsNexthopInvalid { - continue + if !p.GetIsWithdraw() { + if !p.Best || p.IsNexthopInvalid { + continue + } } // NLRI have destination CIDR info nlri, err := apiutil.GetNativeNlri(p) @@ -264,8 +284,8 @@ func (gbh *GoBgpH) processRoute(pathList []*api.Path) { } } -// GetRoutes - get routes in goBGP -func (gbh *GoBgpH) GetRoutes(client api.GobgpApiClient) int { +// GetgoBGPRoutesEvents - get routes in goBGP +func (gbh *GoBgpH) GetgoBGPRoutesEvents(client api.GobgpApiClient) int { processRoutes := func(recver interface { Recv() (*api.WatchEventResponse, error) @@ -313,6 +333,13 @@ func (gbh *GoBgpH) GetRoutes(client api.GobgpApiClient) int { // AdvertiseRoute - advertise a new route using goBGP func (gbh *GoBgpH) AdvertiseRoute(rtPrefix string, pLen int, nh string, pref uint32, med uint32, ipv4 bool) int { var apiFamily *api.Family + + if gbh.localAs == 0 { + if err := gbh.getGlobalConfig(); err != nil { + tk.LogIt(tk.LogError, "[GoBGP] Can't get localAS\n") + return -1 + } + } // add routes //tk.LogIt(tk.LogDebug, "\n\n\n Advertising Route : %v via %v\n\n\n", rtPrefix, nh) nlri, _ := apb.New(&api.IPAddressPrefix{ @@ -429,7 +456,12 @@ func GoBgpInit(bgpPeerMode bool) *GoBgpH { gbh.eventCh = make(chan goBgpEvent, cmn.RuWorkQLen) gbh.host = "127.0.0.1:50052" - gbh.ciMap = make(map[string]*goCI) + if gbh.ciMap = make(map[string]*goCI); gbh.ciMap == nil { + panic("gbh.ciMap alloc failure") + } + if gbh.nbMap = make(map[string]*goBGPNeigh); gbh.nbMap == nil { + panic("gbh.nbMap alloc failure") + } gbh.state = BGPDisconnected gbh.tDone = make(chan bool) gbh.ticker = time.NewTicker(30 * time.Second) @@ -603,8 +635,8 @@ func (gbh *GoBgpH) DelBGPRule(instance string, IP []string) { } } -// AddCurrentBgpRoutesToIPRoute - add bgp routes to OS -func (gbh *GoBgpH) AddCurrentBgpRoutesToIPRoute() error { +// AddCurrBgpRoutesToIPRoute - add bgp routes to OS +func (gbh *GoBgpH) AddCurrBgpRoutesToIPRoute() error { ipv4UC := &api.Family{ Afi: api.Family_AFI_IP, Safi: api.Family_SAFI_UNICAST, @@ -676,7 +708,7 @@ func (gbh *GoBgpH) AddCurrentBgpRoutesToIPRoute() error { return nil } -func (gbh *GoBgpH) advertiseAllRoutes(instance string) { +func (gbh *GoBgpH) advertiseAllVIPs(instance string) { var pref uint32 var med uint32 add := true @@ -719,24 +751,32 @@ func (gbh *GoBgpH) advertiseAllRoutes(instance string) { } } -func (gbh *GoBgpH) getGoBgpRoutes() { +func (gbh *GoBgpH) initBgpClient() { gbh.mtx.Lock() for ciname, ci := range gbh.ciMap { if ci != nil { - gbh.advertiseAllRoutes(ciname) + gbh.advertiseAllVIPs(ciname) + } + + if ciname == cmn.CIDefault { + if ci.hastate == cmn.CIStateBackup { + gbh.resetNeighAdj(true) + } else if ci.hastate == cmn.CIStateMaster { + gbh.resetNeighAdj(false) + } } } /* Get local routes and advertise */ //getRoutesAndAdvertise() - if err := gbh.AddCurrentBgpRoutesToIPRoute(); err != nil { + if err := gbh.AddCurrBgpRoutesToIPRoute(); err != nil { tk.LogIt(tk.LogError, "[GoBGP] AddCurrentBgpRoutesToIpRoute() return err: %s\n", err.Error()) } gbh.mtx.Unlock() - go gbh.GetRoutes(gbh.client) + go gbh.GetgoBGPRoutesEvents(gbh.client) } func (gbh *GoBgpH) processBgpEvent(e goBgpEvent) { @@ -752,7 +792,7 @@ func (gbh *GoBgpH) processBgpEvent(e goBgpEvent) { tk.LogIt(tk.LogNotice, "******************* BGP %s connected *******************\n", gbh.host) gbh.conn = e.conn gbh.state = BGPConnected - gbh.getGoBgpRoutes() + gbh.initBgpClient() case bgpRtRecvd: gbh.processRouteSingle(&e.Data, bgp.BGP_ADD_PATH_RECEIVE) } @@ -775,20 +815,61 @@ func (gbh *GoBgpH) goBgpMonitor() { // UpdateCIState - Routine to update CI state for this module and re-advertise with appropriate priority func (gbh *GoBgpH) UpdateCIState(instance string, state int, vip net.IP) { + update := false ci := gbh.ciMap[instance] if ci == nil { ci = new(goCI) ci.rules = make(map[string]int) + } else { + if ci.hastate != state { + update = true + } } ci.name = instance ci.hastate = state ci.vip = vip gbh.ciMap[instance] = ci - gbh.advertiseAllRoutes(instance) + gbh.advertiseAllVIPs(instance) + if update { + if instance == cmn.CIDefault { + if ci.hastate == cmn.CIStateBackup { + gbh.resetNeighAdj(true) + } else if ci.hastate == cmn.CIStateMaster { + gbh.resetNeighAdj(false) + } + } + } tk.LogIt(tk.LogNotice, "[BGP] Instance %s(%v) HA state updated : %d\n", instance, vip, state) } +// resetNeighAdj - Reset BGP Neighbor's adjacencies +func (gbh *GoBgpH) resetNeighAdj(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()) + // return err + } else { + tk.LogIt(tk.LogInfo, "[GoBGP] Removed set-med-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()) + //return err + } else { + tk.LogIt(tk.LogInfo, "[GoBGP] Applied set-med-export policy\n") + } + } + + for _, nb := range gbh.nbMap { + if nb.remoteAS != gbh.localAs { + gbh.resetSingeNeighAdj(nb.name) + } + } + return nil +} + // BGPNeighMod - Routine to add BGP neigh to goBGP server func (gbh *GoBgpH) BGPNeighMod(add bool, neigh net.IP, ras uint32, rPort uint32) (int, error) { var peer *api.Peer @@ -804,6 +885,7 @@ func (gbh *GoBgpH) BGPNeighMod(add bool, neigh net.IP, ras uint32, rPort uint32) peer.Conf.NeighborAddress = neigh.String() peer.State.NeighborAddress = neigh.String() peer.Conf.PeerAsn = ras + peer.Conf.AllowOwnAsn = 1 if rPort != 0 { peer.Transport.RemotePort = rPort } else { @@ -825,6 +907,26 @@ func (gbh *GoBgpH) BGPNeighMod(add bool, neigh net.IP, ras uint32, rPort uint32) if err != nil { return -1, err } + + gbh.mtx.Lock() + defer gbh.mtx.Unlock() + + if add { + nbr := gbh.nbMap[neigh.String()] + if nbr != nil { + nbr = new(goBGPNeigh) + nbr.name = neigh.String() + nbr.remoteAS = ras + nbr.remotePort = rPort + gbh.nbMap[neigh.String()] = nbr + } else { + nbr.remoteAS = ras + nbr.remotePort = rPort + } + } else { + delete(gbh.nbMap, neigh.String()) + } + return 0, nil } @@ -908,6 +1010,22 @@ func (gbh *GoBgpH) removeExportPolicy(remoteIP string, name string) (int, error) return 0, err } +// resetSingeNeighAdj - Routine to reset a bgp neighbor +func (gbh *GoBgpH) resetSingeNeighAdj(remoteIP string) error { + var comm string + soft := true + dir := api.ResetPeerRequest_OUT + _, err := gbh.client.ResetPeer(context.Background(), &api.ResetPeerRequest{ + Address: remoteIP, + Communication: comm, + Soft: soft, + Direction: dir, + }) + + tk.LogIt(tk.LogInfo, "[GoBGP] Soft reset neigh %s:%s\n", remoteIP, err.Error()) + return err +} + // BGPNeighMod - Routine to add BGP neigh to goBGP server func (gbh *GoBgpH) BGPGlobalConfigAdd(config cmn.GoBGPGlobalConfig) (int, error) { lalist := make([]string, 0, 1) @@ -948,6 +1066,22 @@ func (gbh *GoBgpH) BGPGlobalConfigAdd(config cmn.GoBGPGlobalConfig) (int, error) } } + // Create the set-med policy statement + if _, err := gbh.createSetMedPolicy("set-med-export-gstmt", 50); err != nil { + tk.LogIt(tk.LogError, "[GoBGP] Error creating set-med-export-gstmt stmt %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 { + tk.LogIt(tk.LogError, "[GoBGP] Error creating set-med-export 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()) + // return 0, err + //} + return 0, err } @@ -1005,7 +1139,7 @@ func (gbh *GoBgpH) goBGPRoutesSync() { gbh.mtx.Lock() defer gbh.mtx.Unlock() - if err := gbh.AddCurrentBgpRoutesToIPRoute(); err != nil { + if err := gbh.AddCurrBgpRoutesToIPRoute(); err != nil { tk.LogIt(tk.LogError, "[GoBGP] AddCurrentBgpRoutesToIpRoute() return err: %s\n", err.Error()) } From 754b86bcf266a6a3a426785ea02e9eec928c7078 Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Mon, 16 Oct 2023 19:23:42 +0900 Subject: [PATCH 2/4] incluster gobgp fixes --- loxinet/gobgpclient.go | 107 +++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/loxinet/gobgpclient.go b/loxinet/gobgpclient.go index 728abbb46..74e25715e 100644 --- a/loxinet/gobgpclient.go +++ b/loxinet/gobgpclient.go @@ -77,13 +77,6 @@ type goCI struct { rules map[string]int } -// goBGPNeigh - goBGP neighbor -type goBGPNeigh struct { - name string - remoteAS uint32 - remotePort uint32 -} - // GoBgpH - context container type GoBgpH struct { eventCh chan goBgpEvent @@ -97,7 +90,8 @@ type GoBgpH struct { noNlp bool localAs uint32 ciMap map[string]*goCI - nbMap map[string]*goBGPNeigh + reqRst bool + resetTS time.Time } func (gbh *GoBgpH) getGlobalConfig() error { @@ -459,9 +453,6 @@ func GoBgpInit(bgpPeerMode bool) *GoBgpH { if gbh.ciMap = make(map[string]*goCI); gbh.ciMap == nil { panic("gbh.ciMap alloc failure") } - if gbh.nbMap = make(map[string]*goBGPNeigh); gbh.nbMap == nil { - panic("gbh.nbMap alloc failure") - } gbh.state = BGPDisconnected gbh.tDone = make(chan bool) gbh.ticker = time.NewTicker(30 * time.Second) @@ -752,7 +743,9 @@ func (gbh *GoBgpH) advertiseAllVIPs(instance string) { } func (gbh *GoBgpH) initBgpClient() { + gbh.mtx.Lock() + defer gbh.mtx.Unlock() for ciname, ci := range gbh.ciMap { if ci != nil { @@ -761,9 +754,9 @@ func (gbh *GoBgpH) initBgpClient() { if ciname == cmn.CIDefault { if ci.hastate == cmn.CIStateBackup { - gbh.resetNeighAdj(true) + gbh.resetBGPMed(true) } else if ci.hastate == cmn.CIStateMaster { - gbh.resetNeighAdj(false) + gbh.resetBGPMed(false) } } } @@ -774,7 +767,6 @@ func (gbh *GoBgpH) initBgpClient() { if err := gbh.AddCurrBgpRoutesToIPRoute(); err != nil { tk.LogIt(tk.LogError, "[GoBGP] AddCurrentBgpRoutesToIpRoute() return err: %s\n", err.Error()) } - gbh.mtx.Unlock() go gbh.GetgoBGPRoutesEvents(gbh.client) } @@ -834,9 +826,9 @@ func (gbh *GoBgpH) UpdateCIState(instance string, state int, vip net.IP) { if update { if instance == cmn.CIDefault { if ci.hastate == cmn.CIStateBackup { - gbh.resetNeighAdj(true) + gbh.resetBGPMed(true) } else if ci.hastate == cmn.CIStateMaster { - gbh.resetNeighAdj(false) + gbh.resetBGPMed(false) } } } @@ -844,7 +836,41 @@ func (gbh *GoBgpH) UpdateCIState(instance string, state int, vip net.IP) { } // resetNeighAdj - Reset BGP Neighbor's adjacencies -func (gbh *GoBgpH) resetNeighAdj(toLow bool) error { +func (gbh *GoBgpH) resetNeighAdj() error { + + stream, err := gbh.client.ListPeer(context.Background(), &api.ListPeerRequest{ + Address: "", + EnableAdvertised: false, + }) + if err != nil { + return err + } + + l := make([]*api.Peer, 0, 1024) + for { + r, err := stream.Recv() + if err == io.EOF { + break + } else if err != nil { + return err + } + l = append(l, r.Peer) + } + + if len(l) == 0 { + return nil + } + + for _, nb := range l { + if nb.Conf.PeerAsn != gbh.localAs { + gbh.resetSingleNeighAdj(nb.Conf.NeighborAddress) + } + } + return nil +} + +// resetBGPMed - Reset BGP Med attribute +func (gbh *GoBgpH) resetBGPMed(toLow bool) error { if !toLow { if _, err := gbh.removeExportPolicy("global", "set-med-export-gpolicy"); err != nil { @@ -862,11 +888,9 @@ func (gbh *GoBgpH) resetNeighAdj(toLow bool) error { } } - for _, nb := range gbh.nbMap { - if nb.remoteAS != gbh.localAs { - gbh.resetSingeNeighAdj(nb.name) - } - } + gbh.reqRst = true + gbh.resetTS = time.Now() + return nil } @@ -907,26 +931,6 @@ func (gbh *GoBgpH) BGPNeighMod(add bool, neigh net.IP, ras uint32, rPort uint32) if err != nil { return -1, err } - - gbh.mtx.Lock() - defer gbh.mtx.Unlock() - - if add { - nbr := gbh.nbMap[neigh.String()] - if nbr != nil { - nbr = new(goBGPNeigh) - nbr.name = neigh.String() - nbr.remoteAS = ras - nbr.remotePort = rPort - gbh.nbMap[neigh.String()] = nbr - } else { - nbr.remoteAS = ras - nbr.remotePort = rPort - } - } else { - delete(gbh.nbMap, neigh.String()) - } - return 0, nil } @@ -1010,8 +1014,8 @@ func (gbh *GoBgpH) removeExportPolicy(remoteIP string, name string) (int, error) return 0, err } -// resetSingeNeighAdj - Routine to reset a bgp neighbor -func (gbh *GoBgpH) resetSingeNeighAdj(remoteIP string) error { +// resetSingleNeighAdj - Routine to reset a bgp neighbor +func (gbh *GoBgpH) resetSingleNeighAdj(remoteIP string) error { var comm string soft := true dir := api.ResetPeerRequest_OUT @@ -1026,7 +1030,7 @@ func (gbh *GoBgpH) resetSingeNeighAdj(remoteIP string) error { return err } -// BGPNeighMod - Routine to add BGP neigh to goBGP server +// BGPGlobalConfigAdd - Routine to add global config in goBGP server func (gbh *GoBgpH) BGPGlobalConfigAdd(config cmn.GoBGPGlobalConfig) (int, error) { lalist := make([]string, 0, 1) lalist = append(lalist, "0.0.0.0") @@ -1134,8 +1138,8 @@ func getRoutesAndAdvertise() { } } -// goBGPRoutesSync - Sync gobgp routes with sys routes -func (gbh *GoBgpH) goBGPRoutesSync() { +// goBGPHouseKeeper - Periodic house keeping operations +func (gbh *GoBgpH) goBGPHouseKeeper() { gbh.mtx.Lock() defer gbh.mtx.Unlock() @@ -1143,6 +1147,13 @@ func (gbh *GoBgpH) goBGPRoutesSync() { tk.LogIt(tk.LogError, "[GoBGP] AddCurrentBgpRoutesToIpRoute() return err: %s\n", err.Error()) } + if gbh.reqRst { + if time.Duration(time.Since(gbh.resetTS).Seconds()) > time.Duration(4) { + gbh.reqRst = false + //gbh.resetNeighAdj() + } + } + } // goBGPTicker - Perform periodic operations related to gobgp @@ -1152,7 +1163,7 @@ func (gbh *GoBgpH) goBGPTicker() { case <-gbh.tDone: return case <-gbh.ticker.C: - gbh.goBGPRoutesSync() + gbh.goBGPHouseKeeper() } } } From e8eadec428af378cfe0584d103b071d99963b65e Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Mon, 16 Oct 2023 19:59:39 +0900 Subject: [PATCH 3/4] incluster gobgp fixes --- loxinet/gobgpclient.go | 65 +++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/loxinet/gobgpclient.go b/loxinet/gobgpclient.go index 74e25715e..ef119f07e 100644 --- a/loxinet/gobgpclient.go +++ b/loxinet/gobgpclient.go @@ -95,7 +95,9 @@ type GoBgpH struct { } func (gbh *GoBgpH) getGlobalConfig() error { - r, err := gbh.client.GetBgp(context.Background(), &api.GetBgpRequest{}) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + r, err := gbh.client.GetBgp(ctx, &api.GetBgpRequest{}) if err != nil { return err } @@ -374,7 +376,9 @@ func (gbh *GoBgpH) AdvertiseRoute(rtPrefix string, pLen int, nh string, pref uin attrs := []*apb.Any{a1, a2, a3, a4, a5} - _, err := gbh.client.AddPath(context.Background(), &api.AddPathRequest{ + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + _, err := gbh.client.AddPath(ctx, &api.AddPathRequest{ Path: &api.Path{ Family: apiFamily, Nlri: nlri, @@ -426,7 +430,9 @@ func (gbh *GoBgpH) DelAdvertiseRoute(rtPrefix string, pLen int, nh string, pref attrs := []*apb.Any{a1, a2, a3, a4, a5} - _, err := gbh.client.DeletePath(context.Background(), &api.DeletePathRequest{ + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + _, err := gbh.client.DeletePath(ctx, &api.DeletePathRequest{ Path: &api.Path{ Family: &api.Family{Afi: api.Family_AFI_IP, Safi: api.Family_SAFI_UNICAST}, Nlri: nlri, @@ -514,14 +520,17 @@ func (gbh *GoBgpH) goBgpConnect(host string) { gbh.client = api.NewGobgpApiClient(conn) gbh.mtx.Unlock() for { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) gbh.mtx.Lock() - r, err := gbh.client.GetBgp(context.TODO(), &api.GetBgpRequest{}) + r, err := gbh.client.GetBgp(ctx, &api.GetBgpRequest{}) if err != nil { tk.LogIt(tk.LogInfo, "BGP session %s not ready. Will Retry!\n", gbh.host) gbh.mtx.Unlock() + cancel() time.Sleep(2000 * time.Millisecond) continue } + cancel() tk.LogIt(tk.LogNotice, "BGP server %s UP!\n", gbh.host) if r.Global.Asn == 0 { tk.LogIt(tk.LogInfo, "BGP Global Config %s not done. Will wait!\n", gbh.host) @@ -633,7 +642,10 @@ func (gbh *GoBgpH) AddCurrBgpRoutesToIPRoute() error { Safi: api.Family_SAFI_UNICAST, } - stream, err := gbh.client.ListPath(context.TODO(), &api.ListPathRequest{ + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + + stream, err := gbh.client.ListPath(ctx, &api.ListPathRequest{ TableType: api.TableType_GLOBAL, Family: ipv4UC, }) @@ -837,8 +849,10 @@ func (gbh *GoBgpH) UpdateCIState(instance string, state int, vip net.IP) { // resetNeighAdj - Reset BGP Neighbor's adjacencies func (gbh *GoBgpH) resetNeighAdj() error { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() - stream, err := gbh.client.ListPeer(context.Background(), &api.ListPeerRequest{ + stream, err := gbh.client.ListPeer(ctx, &api.ListPeerRequest{ Address: "", EnableAdvertised: false, }) @@ -916,14 +930,17 @@ func (gbh *GoBgpH) BGPNeighMod(add bool, neigh net.IP, ras uint32, rPort uint32) peer.Transport.RemotePort = 179 } + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + if add { - _, err = gbh.client.AddPeer(context.Background(), + _, err = gbh.client.AddPeer(ctx, &api.AddPeerRequest{ Peer: peer, }) } else { - _, err = gbh.client.DeletePeer(context.Background(), + _, err = gbh.client.DeletePeer(ctx, &api.DeletePeerRequest{ Address: neigh.String(), }) @@ -936,13 +953,15 @@ func (gbh *GoBgpH) BGPNeighMod(add bool, neigh net.IP, ras uint32, rPort uint32) // createSelfNHpolicy - Routine to create policy statement func (gbh *GoBgpH) createNHpolicyStmt(name string, addr string) (int, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() st := &api.Statement{ Name: name, Actions: &api.Actions{}, } st.Actions.Nexthop = &api.NexthopAction{} st.Actions.Nexthop.Address = addr - _, err := gbh.client.AddStatement(context.Background(), + _, err := gbh.client.AddStatement(ctx, &api.AddStatementRequest{ Statement: st, }) @@ -951,6 +970,8 @@ func (gbh *GoBgpH) createNHpolicyStmt(name string, addr string) (int, error) { // createSetMedPolicy - Routine to create set med-policy statement func (gbh *GoBgpH) createSetMedPolicy(name string, val int64) (int, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() st := &api.Statement{ Name: name, Actions: &api.Actions{}, @@ -958,7 +979,7 @@ func (gbh *GoBgpH) createSetMedPolicy(name string, val int64) (int, error) { st.Actions.Med = &api.MedAction{} st.Actions.Med.Type = api.MedAction_MOD st.Actions.Med.Value = val - _, err := gbh.client.AddStatement(context.Background(), + _, err := gbh.client.AddStatement(ctx, &api.AddStatementRequest{ Statement: st, }) @@ -967,6 +988,8 @@ func (gbh *GoBgpH) createSetMedPolicy(name string, val int64) (int, error) { // 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) + defer cancel() stmts := make([]*api.Statement, 0, 1) stmts = append(stmts, &api.Statement{Name: stmt}) p := &api.Policy{ @@ -974,7 +997,7 @@ func (gbh *GoBgpH) addPolicy(name string, stmt string) (int, error) { Statements: stmts, } - _, err := gbh.client.AddPolicy(context.Background(), + _, err := gbh.client.AddPolicy(ctx, &api.AddPolicyRequest{ Policy: p, ReferExistingStatements: true, @@ -984,13 +1007,15 @@ func (gbh *GoBgpH) addPolicy(name string, stmt string) (int, error) { // addPolicy - Routine to apply global policy statement func (gbh *GoBgpH) applyExportPolicy(remoteIP string, name string) (int, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() assign := &api.PolicyAssignment{Name: remoteIP} assign.Direction = api.PolicyDirection_EXPORT assign.DefaultAction = api.RouteAction_NONE ps := make([]*api.Policy, 0, 1) ps = append(ps, &api.Policy{Name: name}) assign.Policies = ps - _, err := gbh.client.AddPolicyAssignment(context.Background(), + _, err := gbh.client.AddPolicyAssignment(ctx, &api.AddPolicyAssignmentRequest{ Assignment: assign, }) @@ -1000,13 +1025,15 @@ func (gbh *GoBgpH) applyExportPolicy(remoteIP string, name string) (int, error) // removePolicy - Routine to apply global policy statement func (gbh *GoBgpH) removeExportPolicy(remoteIP string, name string) (int, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() assign := &api.PolicyAssignment{Name: remoteIP} assign.Direction = api.PolicyDirection_EXPORT assign.DefaultAction = api.RouteAction_NONE ps := make([]*api.Policy, 0, 1) ps = append(ps, &api.Policy{Name: name}) assign.Policies = ps - _, err := gbh.client.DeletePolicyAssignment(context.Background(), + _, err := gbh.client.DeletePolicyAssignment(ctx, &api.DeletePolicyAssignmentRequest{ Assignment: assign, }) @@ -1016,26 +1043,30 @@ func (gbh *GoBgpH) removeExportPolicy(remoteIP string, name string) (int, error) // resetSingleNeighAdj - Routine to reset a bgp neighbor func (gbh *GoBgpH) resetSingleNeighAdj(remoteIP string) error { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() var comm string soft := true dir := api.ResetPeerRequest_OUT - _, err := gbh.client.ResetPeer(context.Background(), &api.ResetPeerRequest{ + _, err := gbh.client.ResetPeer(ctx, &api.ResetPeerRequest{ Address: remoteIP, Communication: comm, Soft: soft, Direction: dir, }) - tk.LogIt(tk.LogInfo, "[GoBGP] Soft reset neigh %s:%s\n", remoteIP, err.Error()) + tk.LogIt(tk.LogInfo, "[GoBGP] Soft reset neigh %s\n", remoteIP) return err } // BGPGlobalConfigAdd - Routine to add global config in goBGP server func (gbh *GoBgpH) BGPGlobalConfigAdd(config cmn.GoBGPGlobalConfig) (int, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + defer cancel() lalist := make([]string, 0, 1) lalist = append(lalist, "0.0.0.0") - _, err := gbh.client.StartBgp(context.Background(), &api.StartBgpRequest{ + _, err := gbh.client.StartBgp(ctx, &api.StartBgpRequest{ Global: &api.Global{ Asn: uint32(config.LocalAs), RouterId: config.RouterID, @@ -1150,7 +1181,7 @@ func (gbh *GoBgpH) goBGPHouseKeeper() { if gbh.reqRst { if time.Duration(time.Since(gbh.resetTS).Seconds()) > time.Duration(4) { gbh.reqRst = false - //gbh.resetNeighAdj() + gbh.resetNeighAdj() } } From b8953c172db0adb0ef8d4e61f9dc13b4e009b24f Mon Sep 17 00:00:00 2001 From: Trekkie Coder Date: Mon, 16 Oct 2023 20:03:47 +0900 Subject: [PATCH 4/4] incluster gobgp fixes --- cicd/k3s-flannel-incluster/host.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cicd/k3s-flannel-incluster/host.sh b/cicd/k3s-flannel-incluster/host.sh index d900fb50d..c436473e5 100755 --- a/cicd/k3s-flannel-incluster/host.sh +++ b/cicd/k3s-flannel-incluster/host.sh @@ -4,8 +4,7 @@ chmod 777 sctp_client chmod 777 udp_client echo "123.123.123.1 k8s-svc" >> /etc/hosts -# Install Bird to work with k3s -sudo apt install -y bird2 +sudo apt install -y bird2 socat sleep 5