From d6d36b59e610e7c3f361d294ea4a61896be71869 Mon Sep 17 00:00:00 2001 From: Dario Faccin <35623244+dariofaccin@users.noreply.github.com> Date: Fri, 23 Aug 2024 05:26:23 +0200 Subject: [PATCH] feat: use DRSM only when database is enabled (#270) * feat: use DRSM only when database is enabled Signed-off-by: Dario Faccin * fix missing clauses Signed-off-by: Dario Faccin * improve handling of the casts Signed-off-by: Dario Faccin * initialize local generators only when database is disabled Signed-off-by: Dario Faccin * Revert "improve handling of the casts" This reverts commit a2d5fe5a402b2f8c2874fdc40610a47201c5f163. Signed-off-by: Dario Faccin * address review comments Signed-off-by: Dario Faccin --------- Signed-off-by: Dario Faccin Co-authored-by: gab-arrobo --- context/amf_ue.go | 9 +++-- context/context.go | 74 ++++++++++++++++++++++++++----------- context/ran_ue.go | 9 +++-- nas/handler.go | 6 +-- ngap/dispatcher.go | 52 +++++++++++++------------- ngap/handler.go | 42 +++++++++++---------- oam/api_purge_ue_context.go | 4 +- service/init.go | 8 ++-- 8 files changed, 124 insertions(+), 80 deletions(-) diff --git a/context/amf_ue.go b/context/amf_ue.go index c196f86e..666b26d7 100644 --- a/context/amf_ue.go +++ b/context/amf_ue.go @@ -483,9 +483,12 @@ func (ue *AmfUe) Remove() { } } - // tmsiGenerator.FreeID(int64(ue.Tmsi)) - if err := AMF_Self().Drsm.ReleaseInt32ID(ue.Tmsi); err != nil { - logger.ContextLog.Errorf("Error releasing RanUe: %v", err) + if AMF_Self().EnableDbStore { + if err := AMF_Self().Drsm.ReleaseInt32ID(ue.Tmsi); err != nil { + logger.ContextLog.Errorf("error releasing RanUe: %v", err) + } + } else { + tmsiGenerator.FreeID(int64(ue.Tmsi)) } if len(ue.Supi) > 0 { diff --git a/context/context.go b/context/context.go index 9fe3e5c6..17d420d1 100644 --- a/context/context.go +++ b/context/context.go @@ -26,11 +26,11 @@ import ( ) var ( - amfContext = AMFContext{} - // tmsiGenerator *idgenerator.IDGenerator = nil - // amfUeNGAPIDGenerator *idgenerator.IDGenerator = nil - // amfStatusSubscriptionIDGenerator *idgenerator.IDGenerator = nil - mutex sync.Mutex + amfContext = AMFContext{} + tmsiGenerator *idgenerator.IDGenerator = nil + amfUeNGAPIDGenerator *idgenerator.IDGenerator = nil + amfStatusSubscriptionIDGenerator *idgenerator.IDGenerator = nil + mutex sync.Mutex ) func init() { @@ -43,9 +43,11 @@ func init() { AMF_Self().PlmnSupportList = make([]factory.PlmnSupportItem, 0, MaxNumOfPLMNs) AMF_Self().NfService = make(map[models.ServiceName]models.NfService) AMF_Self().NetworkName.Full = "free5GC" - // tmsiGenerator = idgenerator.NewGenerator(1, math.MaxInt32) - // amfStatusSubscriptionIDGenerator = idgenerator.NewGenerator(1, math.MaxInt32) - // amfUeNGAPIDGenerator = idgenerator.NewGenerator(1, MaxValueOfAmfUeNgapId) + if !AMF_Self().EnableDbStore { + tmsiGenerator = idgenerator.NewGenerator(1, math.MaxInt32) + amfStatusSubscriptionIDGenerator = idgenerator.NewGenerator(1, math.MaxInt32) + amfUeNGAPIDGenerator = idgenerator.NewGenerator(1, MaxValueOfAmfUeNgapId) + } } type AMFContext struct { @@ -114,8 +116,15 @@ func NewPlmnSupportItem() (item factory.PlmnSupportItem) { } func (context *AMFContext) TmsiAllocate() int32 { - // val, err := AllocateUniqueID(&tmsiGenerator, "tmsi") - val, err := context.Drsm.AllocateInt32ID() + var val int32 + var err error + if context.EnableDbStore { + val, err = context.Drsm.AllocateInt32ID() + } else { + var tmp int64 + tmp, err = AllocateUniqueID(&tmsiGenerator, "tmsi") + val = int32(tmp) + } if err != nil { logger.ContextLog.Errorf("Allocate TMSI error: %+v", err) return -1 @@ -125,15 +134,22 @@ func (context *AMFContext) TmsiAllocate() int32 { } func (context *AMFContext) AllocateAmfUeNgapID() (int64, error) { - // val, err := AllocateUniqueID(&amfUeNGAPIDGenerator, "amfUeNgapID") - val, err := context.Drsm.AllocateInt32ID() + var val int64 + var err error + if context.EnableDbStore { + var tmp int32 + tmp, err = context.Drsm.AllocateInt32ID() + val = int64(tmp) + } else { + val, err = AllocateUniqueID(&amfUeNGAPIDGenerator, "amfUeNgapID") + } if err != nil { logger.ContextLog.Errorf("Allocate NgapID error: %+v", err) return -1, err } logger.ContextLog.Infof("Allocate AmfUeNgapID : %v", val) - return int64(val), nil + return val, nil } func (context *AMFContext) AllocateGutiToUe(ue *AmfUe) { @@ -146,13 +162,16 @@ func (context *AMFContext) AllocateGutiToUe(ue *AmfUe) { } func (context *AMFContext) ReAllocateGutiToUe(ue *AmfUe) { + var err error servedGuami := context.ServedGuamiList[0] - - if err := context.Drsm.ReleaseInt32ID(ue.Tmsi); err != nil { - logger.ContextLog.Errorf("Errro releasing tmsi: %v", err) + if context.EnableDbStore { + err = context.Drsm.ReleaseInt32ID(ue.Tmsi) + } else { + tmsiGenerator.FreeID(int64(ue.Tmsi)) + } + if err != nil { + logger.ContextLog.Errorf("Error releasing tmsi: %v", err) } - // tmsiGenerator.FreeID(int64(ue.Tmsi)) - ue.Tmsi = context.TmsiAllocate() plmnID := servedGuami.PlmnId.Mcc + servedGuami.PlmnId.Mnc @@ -188,8 +207,15 @@ func (context *AMFContext) AllocateRegistrationArea(ue *AmfUe, anType models.Acc } func (context *AMFContext) NewAMFStatusSubscription(subscriptionData models.SubscriptionData) (subscriptionID string) { - // id, err := amfStatusSubscriptionIDGenerator.Allocate() - id, err := context.Drsm.AllocateInt32ID() + var id int32 + var err error + if context.EnableDbStore { + id, err = context.Drsm.AllocateInt32ID() + } else { + var tmp int64 + tmp, err = amfStatusSubscriptionIDGenerator.Allocate() + id = int32(tmp) + } if err != nil { logger.ContextLog.Errorf("Allocate subscriptionID error: %+v", err) return "" @@ -215,8 +241,12 @@ func (context *AMFContext) DeleteAMFStatusSubscription(subscriptionID string) { if id, err := strconv.ParseInt(subscriptionID, 10, 64); err != nil { logger.ContextLog.Error(err) } else { - // amfStatusSubscriptionIDGenerator.FreeID(id) - if err := context.Drsm.ReleaseInt32ID(int32(id)); err != nil { + if context.EnableDbStore { + err = context.Drsm.ReleaseInt32ID(int32(id)) + } else { + amfStatusSubscriptionIDGenerator.FreeID(id) + } + if err != nil { logger.ContextLog.Error(err) } } diff --git a/context/ran_ue.go b/context/ran_ue.go index 1ea9d5d8..eb8790fe 100644 --- a/context/ran_ue.go +++ b/context/ran_ue.go @@ -105,9 +105,12 @@ func (ranUe *RanUe) Remove() error { } self := AMF_Self() self.RanUePool.Delete(ranUe.AmfUeNgapId) - // amfUeNGAPIDGenerator.FreeID(ranUe.AmfUeNgapId) - if err := self.Drsm.ReleaseInt32ID(int32(ranUe.AmfUeNgapId)); err != nil { - logger.ContextLog.Errorf("Error releasing UE: %v", err) + if self.EnableDbStore { + if err := self.Drsm.ReleaseInt32ID(int32(ranUe.AmfUeNgapId)); err != nil { + logger.ContextLog.Errorf("error releasing UE: %v", err) + } + } else { + amfUeNGAPIDGenerator.FreeID(ranUe.AmfUeNgapId) } return nil } diff --git a/nas/handler.go b/nas/handler.go index 4959c0bb..9958507a 100644 --- a/nas/handler.go +++ b/nas/handler.go @@ -34,11 +34,11 @@ func HandleNAS(ue *context.RanUe, procedureCode int64, nasPdu []byte) { if ue.AmfUe == nil { ue.AmfUe = amfSelf.NewAmfUe("") } else { - if amfSelf.EnableSctpLb { + if amfSelf.EnableSctpLb && amfSelf.EnableDbStore { /* checking the guti-ue belongs to this amf instance */ id, err := amfSelf.Drsm.FindOwnerInt32ID(ue.AmfUe.Tmsi) if err != nil { - logger.NasLog.Errorf("Error checking guti-ue: %v", err) + logger.NasLog.Errorf("error checking guti-ue: %v", err) } if id != nil && id.PodName != os.Getenv("HOSTNAME") { rsp := &sdcoreAmfServer.AmfMessage{} @@ -53,7 +53,7 @@ func HandleNAS(ue *context.RanUe, procedureCode int64, nasPdu []byte) { ue.AmfUe.Remove() } else { if err := ue.Remove(); err != nil { - logger.NasLog.Errorf("Error removing ue: %v", err) + logger.NasLog.Errorf("error removing ue: %v", err) } } ue.Ran.Amf2RanMsgChan <- rsp diff --git a/ngap/dispatcher.go b/ngap/dispatcher.go index 0ee048e2..b4d59368 100644 --- a/ngap/dispatcher.go +++ b/ngap/dispatcher.go @@ -66,33 +66,35 @@ func DispatchLb(sctplbMsg *sdcoreAmfServer.SctplbMessage, Amf2RanMsgChan chan *s //ranUe.Log.Debugln("RanUe RanNgapId AmfNgapId: ", ranUe.RanUeNgapId, ranUe.AmfUeNgapId) /* checking whether same AMF instance can handle this message */ /* redirect it to correct owner if required */ - id, err := amfSelf.Drsm.FindOwnerInt32ID(int32(ngapId.Value)) - if id == nil || err != nil { - ran.Log.Warningf("DispatchLb, Couldn't find owner for amfUeNgapid: %v", ngapId.Value) - } else if id.PodName != os.Getenv("HOSTNAME") { - rsp := &sdcoreAmfServer.AmfMessage{} - rsp.VerboseMsg = "Redirect Msg From AMF Pod !" - rsp.Msgtype = sdcoreAmfServer.MsgType_REDIRECT_MSG - rsp.AmfId = os.Getenv("HOSTNAME") - /* TODO set only pod name, for this release setting pod ip to simplify logic in sctplb */ - fmt.Printf("DispatchLb, amfNgapId: %v is not for this amf instance, rediret to amf instance: %v %v", ngapId.Value, id.PodName, id.PodIp) - rsp.RedirectId = id.PodIp - rsp.GnbId = ran.GnbId - rsp.Msg = make([]byte, len(sctplbMsg.Msg)) - copy(rsp.Msg, sctplbMsg.Msg) - ran.Amf2RanMsgChan = Amf2RanMsgChan - ran.Amf2RanMsgChan <- rsp - if ranUe != nil && ranUe.AmfUe != nil { - ranUe.AmfUe.Remove() - } - if ranUe != nil { - if err := ranUe.Remove(); err != nil { - ran.Log.Errorf("Could not remove ranUe: %v", err) + if amfSelf.EnableDbStore { + id, err := amfSelf.Drsm.FindOwnerInt32ID(int32(ngapId.Value)) + if id == nil || err != nil { + ran.Log.Warningf("dispatchLb, Couldn't find owner for amfUeNgapid: %v", ngapId.Value) + } else if id.PodName != os.Getenv("HOSTNAME") { + rsp := &sdcoreAmfServer.AmfMessage{} + rsp.VerboseMsg = "Redirect Msg From AMF Pod !" + rsp.Msgtype = sdcoreAmfServer.MsgType_REDIRECT_MSG + rsp.AmfId = os.Getenv("HOSTNAME") + /* TODO set only pod name, for this release setting pod ip to simplify logic in sctplb */ + fmt.Printf("dispatchLb, amfNgapId: %v is not for this amf instance, redirect to amf instance: %v %v", ngapId.Value, id.PodName, id.PodIp) + rsp.RedirectId = id.PodIp + rsp.GnbId = ran.GnbId + rsp.Msg = make([]byte, len(sctplbMsg.Msg)) + copy(rsp.Msg, sctplbMsg.Msg) + ran.Amf2RanMsgChan = Amf2RanMsgChan + ran.Amf2RanMsgChan <- rsp + if ranUe != nil && ranUe.AmfUe != nil { + ranUe.AmfUe.Remove() } + if ranUe != nil { + if err := ranUe.Remove(); err != nil { + ran.Log.Errorf("could not remove ranUe: %v", err) + } + } + return + } else { + ran.Log.Debugf("DispatchLb, amfNgapId: %v for this amf instance", ngapId.Value) } - return - } else { - ran.Log.Debugf("DispatchLb, amfNgapId: %v for this amf instance", ngapId.Value) } } diff --git a/ngap/handler.go b/ngap/handler.go index 9d5664f3..921e6d4d 100644 --- a/ngap/handler.go +++ b/ngap/handler.go @@ -1525,28 +1525,30 @@ func HandleInitialUEMessage(ran *context.AmfRan, message *ngapType.NGAPPDU, sctp } else { ranUe.Log.Tracef("find AmfUe [GUTI: %s]", guti) /* checking the guti-ue belongs to this amf instance */ - id, err := amfSelf.Drsm.FindOwnerInt32ID(amfUe.Tmsi) - if err != nil { - ranUe.Log.Errorf("Error checking the guti-ue in this instance: %v", err) - } - if id != nil && id.PodName != os.Getenv("HOSTNAME") && amfSelf.EnableSctpLb { - rsp := &sdcoreAmfServer.AmfMessage{} - rsp.VerboseMsg = "Redirect Msg From AMF Pod !" - rsp.Msgtype = sdcoreAmfServer.MsgType_REDIRECT_MSG - rsp.AmfId = os.Getenv("HOSTNAME") - /* TODO for this release setting pod ip to simplify logic in sctplb */ - rsp.RedirectId = id.PodIp - rsp.GnbId = ran.GnbId - rsp.Msg = sctplbMsg.Msg - if ranUe != nil && ranUe.AmfUe != nil { - ranUe.AmfUe.Remove() - } else if ranUe != nil { - if err := ranUe.Remove(); err != nil { - ranUe.Log.Errorf("Could not remove ranUe: %v", err) + if amfSelf.EnableDbStore { + id, err := amfSelf.Drsm.FindOwnerInt32ID(amfUe.Tmsi) + if err != nil { + ranUe.Log.Errorf("error checking the guti-ue in this instance: %v", err) + } + if id != nil && id.PodName != os.Getenv("HOSTNAME") && amfSelf.EnableSctpLb { + rsp := &sdcoreAmfServer.AmfMessage{} + rsp.VerboseMsg = "Redirect Msg From AMF Pod !" + rsp.Msgtype = sdcoreAmfServer.MsgType_REDIRECT_MSG + rsp.AmfId = os.Getenv("HOSTNAME") + /* TODO for this release setting pod ip to simplify logic in sctplb */ + rsp.RedirectId = id.PodIp + rsp.GnbId = ran.GnbId + rsp.Msg = sctplbMsg.Msg + if ranUe != nil && ranUe.AmfUe != nil { + ranUe.AmfUe.Remove() + } else if ranUe != nil { + if err := ranUe.Remove(); err != nil { + ranUe.Log.Errorf("could not remove ranUe: %v", err) + } } + ran.Amf2RanMsgChan <- rsp + return } - ran.Amf2RanMsgChan <- rsp - return } if amfUe.CmConnect(ran.AnType) { diff --git a/oam/api_purge_ue_context.go b/oam/api_purge_ue_context.go index 3d78f1ec..2b331cd3 100644 --- a/oam/api_purge_ue_context.go +++ b/oam/api_purge_ue_context.go @@ -57,7 +57,9 @@ func HTTPAmfInstanceDown(c *gin.Context) { if nfInstanceId, exists := c.Params.Get("nfid"); exists { req.Params["nfid"] = nfInstanceId self := context.AMF_Self() - self.Drsm.DeletePod(nfInstanceId) + if self.EnableDbStore { + self.Drsm.DeletePod(nfInstanceId) + } c.JSON(http.StatusOK, nil) } } diff --git a/service/init.go b/service/init.go index fb0ef5cd..e2a0da28 100644 --- a/service/init.go +++ b/service/init.go @@ -333,9 +333,11 @@ func (amf *AMF) Start() { self := context.AMF_Self() util.InitAmfContext(self) - self.Drsm, err = util.InitDrsm() - if err != nil { - initLog.Errorf("initialise DRSM failed, %v", err.Error()) + if self.EnableDbStore { + self.Drsm, err = util.InitDrsm() + if err != nil { + initLog.Errorf("initialise DRSM failed, %v", err.Error()) + } } addr := fmt.Sprintf("%s:%d", self.BindingIPv4, self.SBIPort)