diff --git a/internal/context/upf.go b/internal/context/upf.go index ff25b28e..21c8628e 100644 --- a/internal/context/upf.go +++ b/internal/context/upf.go @@ -378,11 +378,8 @@ func (upf *UPF) GetUPFID() string { } func (upf *UPF) pdrID() (uint16, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return 0, err - default: } var pdrID uint16 @@ -396,11 +393,8 @@ func (upf *UPF) pdrID() (uint16, error) { } func (upf *UPF) farID() (uint32, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return 0, err - default: } var farID uint32 @@ -414,11 +408,8 @@ func (upf *UPF) farID() (uint32, error) { } func (upf *UPF) barID() (uint8, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return 0, err - default: } var barID uint8 @@ -432,11 +423,8 @@ func (upf *UPF) barID() (uint8, error) { } func (upf *UPF) qerID() (uint32, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return 0, err - default: } var qerID uint32 @@ -461,11 +449,8 @@ func (upf *UPF) urrID() (uint32, error) { } func (upf *UPF) AddPDR() (*PDR, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return nil, err - default: } pdr := new(PDR) @@ -486,11 +471,8 @@ func (upf *UPF) AddPDR() (*PDR, error) { } func (upf *UPF) AddFAR() (*FAR, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return nil, err - default: } far := new(FAR) @@ -505,11 +487,8 @@ func (upf *UPF) AddFAR() (*FAR, error) { } func (upf *UPF) AddBAR() (*BAR, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return nil, err - default: } bar := new(BAR) @@ -523,11 +502,8 @@ func (upf *UPF) AddBAR() (*BAR, error) { } func (upf *UPF) AddQER() (*QER, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return nil, err - default: } qer := new(QER) @@ -541,11 +517,8 @@ func (upf *UPF) AddQER() (*QER, error) { } func (upf *UPF) AddURR(urrId uint32, opts ...UrrOpt) (*URR, error) { - select { - case <-upf.AssociationContext.Done(): - err := fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) + if err := upf.IsAssociated(); err != nil { return nil, err - default: } urr := new(URR) @@ -583,10 +556,8 @@ func (upf *UPF) GetQERById(qerId uint32) *QER { // *** add unit test ***// func (upf *UPF) RemovePDR(pdr *PDR) (err error) { - select { - case <-upf.AssociationContext.Done(): - return fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) - default: + if err := upf.IsAssociated(); err != nil { + return err } upf.pdrIDGenerator.FreeID(int64(pdr.PDRID)) @@ -596,10 +567,8 @@ func (upf *UPF) RemovePDR(pdr *PDR) (err error) { // *** add unit test ***// func (upf *UPF) RemoveFAR(far *FAR) (err error) { - select { - case <-upf.AssociationContext.Done(): - return fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) - default: + if err := upf.IsAssociated(); err != nil { + return err } upf.farIDGenerator.FreeID(int64(far.FARID)) @@ -609,10 +578,8 @@ func (upf *UPF) RemoveFAR(far *FAR) (err error) { // *** add unit test ***// func (upf *UPF) RemoveBAR(bar *BAR) (err error) { - select { - case <-upf.AssociationContext.Done(): - return fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) - default: + if err := upf.IsAssociated(); err != nil { + return err } upf.barIDGenerator.FreeID(int64(bar.BARID)) @@ -622,10 +589,8 @@ func (upf *UPF) RemoveBAR(bar *BAR) (err error) { // *** add unit test ***// func (upf *UPF) RemoveQER(qer *QER) (err error) { - select { - case <-upf.AssociationContext.Done(): - return fmt.Errorf("UPF[%s] not associated with SMF", upf.NodeID.ResolveNodeIdToIp().String()) - default: + if err := upf.IsAssociated(); err != nil { + return err } upf.qerIDGenerator.FreeID(int64(qer.QERID)) @@ -651,3 +616,13 @@ func (upf *UPF) ProcEachSMContext(procFunc func(*SMContext)) { return true }) } + +func (upf *UPF) IsAssociated() error { + select { + case <-upf.AssociationContext.Done(): + return fmt.Errorf("UPF[%s] not associated with SMF", + upf.NodeID.ResolveNodeIdToIp().String()) + default: + return nil + } +} diff --git a/internal/context/user_plane_information.go b/internal/context/user_plane_information.go index 4af26731..32e483a8 100644 --- a/internal/context/user_plane_information.go +++ b/internal/context/user_plane_information.go @@ -877,13 +877,11 @@ func (upi *UserPlaneInformation) SelectUPFAndAllocUEIP(selection *UPFSelectionPa for _, upf := range sortedUPFList { logger.CtxLog.Debugf("check start UPF: %s", upi.GetUPFNameByIp(upf.NodeID.ResolveNodeIdToIp().String())) - select { - case <-upf.UPF.AssociationContext.Done(): - logger.CtxLog.Infof("PFCP Association not yet Established with: %s", - upi.GetUPFNameByIp(upf.NodeID.ResolveNodeIdToIp().String())) + if err := upf.UPF.IsAssociated(); err != nil { + logger.CtxLog.Infoln(err) continue - default: } + pools, useStaticIPPool := getUEIPPool(upf, selection) if len(pools) == 0 { continue diff --git a/internal/pfcp/handler/handler.go b/internal/pfcp/handler/handler.go index e8c086b5..7448072c 100644 --- a/internal/pfcp/handler/handler.go +++ b/internal/pfcp/handler/handler.go @@ -124,14 +124,10 @@ func HandlePfcpSessionReportRequest(msg *pfcpUdp.Message) { pfcp_message.SendPfcpSessionReportResponse(msg.RemoteAddr, cause, seqFromUPF, 0) return } - select { - case <-upf.AssociationContext.Done(): - logger.PfcpLog.Warnf("PFCP Session Report Request : Not Associated with UPF[%s], Request Rejected", - upfNodeIDtoIPStr) + if err := upf.IsAssociated(); err != nil { + logger.PfcpLog.Warnf("PFCP Session Report Request rejected: %+v", err) cause.CauseValue = pfcpType.CauseNoEstablishedPfcpAssociation pfcp_message.SendPfcpSessionReportResponse(msg.RemoteAddr, cause, seqFromUPF, 0) - return - default: } if smContext.UpCnxState == models.UpCnxState_DEACTIVATED { diff --git a/internal/pfcp/message/send.go b/internal/pfcp/message/send.go index d38fe5e2..3d68e9d3 100644 --- a/internal/pfcp/message/send.go +++ b/internal/pfcp/message/send.go @@ -140,10 +140,8 @@ func SendPfcpSessionEstablishmentRequest( urrList []*context.URR, ) (resMsg *pfcpUdp.Message, err error) { nodeIDtoIP := upf.NodeID.ResolveNodeIdToIp() - select { - case <-upf.AssociationContext.Done(): - return nil, fmt.Errorf("Not Associated with UPF[%s]", nodeIDtoIP.String()) - default: + if err := upf.IsAssociated(); err != nil { + return nil, err } pfcpMsg, err := BuildPfcpSessionEstablishmentRequest(upf.NodeID, nodeIDtoIP.String(), @@ -225,10 +223,8 @@ func SendPfcpSessionModificationRequest( urrList []*context.URR, ) (resMsg *pfcpUdp.Message, err error) { nodeIDtoIP := upf.NodeID.ResolveNodeIdToIp() - select { - case <-upf.AssociationContext.Done(): - return nil, fmt.Errorf("Not Associated with UPF[%s]", nodeIDtoIP.String()) - default: + if err := upf.IsAssociated(); err != nil { + return nil, err } pfcpMsg, err := BuildPfcpSessionModificationRequest(upf.NodeID, nodeIDtoIP.String(), @@ -302,10 +298,8 @@ func SendPfcpSessionModificationResponse(addr *net.UDPAddr) { func SendPfcpSessionDeletionRequest(upf *context.UPF, ctx *context.SMContext) (resMsg *pfcpUdp.Message, err error) { nodeIDtoIP := upf.NodeID.ResolveNodeIdToIp() - select { - case <-upf.AssociationContext.Done(): - return nil, fmt.Errorf("Not Associated with UPF[%s]", nodeIDtoIP.String()) - default: + if err := upf.IsAssociated(); err != nil { + return nil, err } pfcpMsg, err := BuildPfcpSessionDeletionRequest() diff --git a/internal/sbi/api_upi.go b/internal/sbi/api_upi.go index 61ff2ea8..254cc78d 100644 --- a/internal/sbi/api_upi.go +++ b/internal/sbi/api_upi.go @@ -68,10 +68,8 @@ func (s *Server) PostUpNodesLinks(c *gin.Context) { for _, upf := range upi.UPFs { // only associate new ones - select { - case <-upf.UPF.AssociationContext.Done(): + if err := upf.UPF.IsAssociated(); err != nil { go s.Processor().ToBeAssociatedWithUPF(smf_context.GetSelf().PfcpContext, upf.UPF) - default: } } c.JSON(http.StatusOK, gin.H{"status": "OK"}) diff --git a/internal/sbi/processor/association.go b/internal/sbi/processor/association.go index 20fad749..7a0306f1 100644 --- a/internal/sbi/processor/association.go +++ b/internal/sbi/processor/association.go @@ -135,10 +135,8 @@ func keepHeartbeatTo(upf *smf_context.UPF, upfStr string) { } func doPfcpHeartbeat(upf *smf_context.UPF, upfStr string) error { - select { - case <-upf.AssociationContext.Done(): - return fmt.Errorf("Cancel heartbeat, UPF[%s] is not associted", upfStr) - default: + if err := upf.IsAssociated(); err != nil { + return fmt.Errorf("Cancel heartbeat: %+v", err) } logger.MainLog.Debugf("Sending PFCP Heartbeat Request to UPF%s", upfStr)