Skip to content

Commit

Permalink
mv dl/up teid generator to smCtx (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianchen0119 authored Mar 20, 2024
1 parent 73cc42f commit 071c172
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 39 deletions.
2 changes: 2 additions & 0 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 2 additions & 19 deletions internal/context/datapath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion internal/context/ngap_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down
2 changes: 1 addition & 1 deletion internal/context/ngap_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions internal/context/sm_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ type UsageReport struct {
ReportTpye models.TriggerType
}

var TeidGenerator *idgenerator.IDGenerator

type SMContext struct {
*models.SmContextCreateData

Expand All @@ -128,6 +130,9 @@ type SMContext struct {
Identifier string
PDUSessionID int32

LocalULTeid uint32
LocalDLTeid uint32

UpCnxState models.UpCnxState

HoState models.HoState
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions internal/context/upf.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ type UPF struct {
barIDGenerator *idgenerator.IDGenerator
urrIDGenerator *idgenerator.IDGenerator
qerIDGenerator *idgenerator.IDGenerator
teidGenerator *idgenerator.IDGenerator
}

// UPFSelectionParams ... parameters for upf selection
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 071c172

Please sign in to comment.