From 071c172dc5a24129311bf0f2f3b35e159c661b32 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 20 Mar 2024 12:24:17 +0800 Subject: [PATCH] mv dl/up teid generator to smCtx (#98) --- internal/context/context.go | 2 ++ internal/context/datapath.go | 21 ++------------------ internal/context/ngap_build.go | 2 +- internal/context/ngap_handler.go | 2 +- internal/context/sm_context.go | 34 ++++++++++++++++++++++++++++++++ internal/context/upf.go | 18 ----------------- 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/internal/context/context.go b/internal/context/context.go index b9d4ced5..3da5ecb4 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -258,6 +258,8 @@ func InitSmfContext(config *factory.Config) { SetupNFProfile(config) smfContext.Locality = configuration.Locality + + TeidGenerator = idgenerator.NewGenerator(1, math.MaxUint32) } func InitSMFUERouting(routingConfig *factory.RoutingConfig) { diff --git a/internal/context/datapath.go b/internal/context/datapath.go index d8a31d59..e6611dc5 100644 --- a/internal/context/datapath.go +++ b/internal/context/datapath.go @@ -144,12 +144,7 @@ func (node *DataPathNode) ActivateUpLinkTunnel(smContext *SMContext) error { return err } - if teid, err := destUPF.GenerateTEID(); err != nil { - logger.CtxLog.Errorf("Generate uplink TEID fail: %s", err) - return err - } else { - node.UpLinkTunnel.TEID = teid - } + node.UpLinkTunnel.TEID = smContext.LocalULTeid return nil } @@ -171,12 +166,7 @@ func (node *DataPathNode) ActivateDownLinkTunnel(smContext *SMContext) error { return err } - if teid, err := destUPF.GenerateTEID(); err != nil { - logger.CtxLog.Errorf("Generate downlink TEID fail: %s", err) - return err - } else { - node.DownLinkTunnel.TEID = teid - } + node.DownLinkTunnel.TEID = smContext.LocalDLTeid return nil } @@ -214,9 +204,6 @@ func (node *DataPathNode) DeactivateUpLinkTunnel(smContext *SMContext) { } } } - - teid := node.UpLinkTunnel.TEID - node.UPF.teidGenerator.FreeID(int64(teid)) } func (node *DataPathNode) DeactivateDownLinkTunnel(smContext *SMContext) { @@ -252,9 +239,6 @@ func (node *DataPathNode) DeactivateDownLinkTunnel(smContext *SMContext) { } } } - - teid := node.DownLinkTunnel.TEID - node.UPF.teidGenerator.FreeID(int64(teid)) } func (node *DataPathNode) GetUPFID() (id string, err error) { @@ -771,7 +755,6 @@ func (p *DataPath) AddChargingRules(smContext *SMContext, chgLevel ChargingLevel UpfId: node.UPF.UUID(), } - // urrId, err := node.UPF.urrIDGenerator.Allocate() urrId, err := smContext.UrrIDGenerator.Allocate() if err != nil { logger.PduSessLog.Errorln("Generate URR Id failed") diff --git a/internal/context/ngap_build.go b/internal/context/ngap_build.go index af0c60da..4c25f9f7 100644 --- a/internal/context/ngap_build.go +++ b/internal/context/ngap_build.go @@ -16,7 +16,7 @@ func BuildPDUSessionResourceSetupRequestTransfer(ctx *SMContext) ([]byte, error) ANUPF := ctx.Tunnel.DataPathPool.GetDefaultPath().FirstDPNode UpNode := ANUPF.UPF teidOct := make([]byte, 4) - binary.BigEndian.PutUint32(teidOct, ANUPF.UpLinkTunnel.TEID) + binary.BigEndian.PutUint32(teidOct, ctx.LocalULTeid) resourceSetupRequestTransfer := ngapType.PDUSessionResourceSetupRequestTransfer{} diff --git a/internal/context/ngap_handler.go b/internal/context/ngap_handler.go index 54892bfb..19d5efa4 100644 --- a/internal/context/ngap_handler.go +++ b/internal/context/ngap_handler.go @@ -266,7 +266,7 @@ func HandleHandoverRequestAcknowledgeTransfer(b []byte, ctx *SMContext) (err err originPDR := ctx.Tunnel.DataPathPool.GetDefaultPath().FirstDPNode.UpLinkTunnel.PDR - if teid, err := ANUPF.GenerateTEID(); err != nil { + if teid, err := GenerateTEID(); err != nil { return err } else { ctx.IndirectForwardingTunnel.FirstDPNode.UpLinkTunnel.TEID = teid diff --git a/internal/context/sm_context.go b/internal/context/sm_context.go index 30bbb061..e520d1b8 100644 --- a/internal/context/sm_context.go +++ b/internal/context/sm_context.go @@ -114,6 +114,8 @@ type UsageReport struct { ReportTpye models.TriggerType } +var TeidGenerator *idgenerator.IDGenerator + type SMContext struct { *models.SmContextCreateData @@ -128,6 +130,9 @@ type SMContext struct { Identifier string PDUSessionID int32 + LocalULTeid uint32 + LocalDLTeid uint32 + UpCnxState models.UpCnxState HoState models.HoState @@ -242,6 +247,21 @@ type SMContext struct { SMLock sync.Mutex } +func GenerateTEID() (uint32, error) { + var id uint32 + if tmpID, err := TeidGenerator.Allocate(); err != nil { + return 0, err + } else { + id = uint32(tmpID) + } + + return id, nil +} + +func ReleaseTEID(teid uint32) { + TeidGenerator.FreeID(int64(teid)) +} + func canonicalName(id string, pduSessID int32) string { return fmt.Sprintf("%s-%d", id, pduSessID) } @@ -316,6 +336,17 @@ func NewSMContext(id string, pduSessID int32) *SMContext { } } + var err error + smContext.LocalDLTeid, err = GenerateTEID() + if err != nil { + return nil + } + + smContext.LocalULTeid, err = GenerateTEID() + if err != nil { + return nil + } + return smContext } @@ -361,6 +392,9 @@ func RemoveSMContext(ref string) { seidSMContextMap.Delete(pfcpSessionContext.LocalSEID) } + ReleaseTEID(smContext.LocalULTeid) + ReleaseTEID(smContext.LocalDLTeid) + smContextPool.Delete(ref) canonicalRef.Delete(canonicalName(smContext.Supi, smContext.PDUSessionID)) smContext.Log.Infof("smContext[%s] is deleted from pool", ref) diff --git a/internal/context/upf.go b/internal/context/upf.go index ead327ac..98c59736 100644 --- a/internal/context/upf.go +++ b/internal/context/upf.go @@ -90,7 +90,6 @@ type UPF struct { barIDGenerator *idgenerator.IDGenerator urrIDGenerator *idgenerator.IDGenerator qerIDGenerator *idgenerator.IDGenerator - teidGenerator *idgenerator.IDGenerator } // UPFSelectionParams ... parameters for upf selection @@ -252,7 +251,6 @@ func NewUPF(nodeID *pfcpType.NodeID, ifaces []*factory.InterfaceUpfInfoItem) (up upf.barIDGenerator = idgenerator.NewGenerator(1, math.MaxUint8) upf.qerIDGenerator = idgenerator.NewGenerator(1, math.MaxUint32) upf.urrIDGenerator = idgenerator.NewGenerator(1, math.MaxUint32) - upf.teidGenerator = idgenerator.NewGenerator(1, math.MaxUint32) upf.N3Interfaces = make([]*UPFInterfaceInfo, 0) upf.N9Interfaces = make([]*UPFInterfaceInfo, 0) @@ -295,22 +293,6 @@ func (upf *UPF) GetInterface(interfaceType models.UpInterfaceType, dnn string) * return nil } -func (upf *UPF) GenerateTEID() (uint32, error) { - if upf.UPFStatus != AssociatedSetUpSuccess { - err := fmt.Errorf("this upf not associate with smf") - return 0, err - } - - var id uint32 - if tmpID, err := upf.teidGenerator.Allocate(); err != nil { - return 0, err - } else { - id = uint32(tmpID) - } - - return id, nil -} - func (upf *UPF) PFCPAddr() *net.UDPAddr { return &net.UDPAddr{ IP: upf.NodeID.ResolveNodeIdToIp(),