Skip to content

Commit

Permalink
feat: use DRSM only when database is enabled (#270)
Browse files Browse the repository at this point in the history
* feat: use DRSM only when database is enabled

Signed-off-by: Dario Faccin <[email protected]>

* fix missing clauses

Signed-off-by: Dario Faccin <[email protected]>

* improve handling of the casts

Signed-off-by: Dario Faccin <[email protected]>

* initialize local generators only when database is disabled

Signed-off-by: Dario Faccin <[email protected]>

* Revert "improve handling of the casts"

This reverts commit a2d5fe5.

Signed-off-by: Dario Faccin <[email protected]>

* address review comments

Signed-off-by: Dario Faccin <[email protected]>

---------

Signed-off-by: Dario Faccin <[email protected]>
Co-authored-by: gab-arrobo <[email protected]>
  • Loading branch information
dariofaccin and gab-arrobo committed Aug 23, 2024
1 parent c75397b commit d6d36b5
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 80 deletions.
9 changes: 6 additions & 3 deletions context/amf_ue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
74 changes: 52 additions & 22 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 ""
Expand All @@ -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)
}
}
Expand Down
9 changes: 6 additions & 3 deletions context/ran_ue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions nas/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand Down
52 changes: 27 additions & 25 deletions ngap/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
42 changes: 22 additions & 20 deletions ngap/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion oam/api_purge_ue_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 5 additions & 3 deletions service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d6d36b5

Please sign in to comment.