Skip to content

Commit

Permalink
refactor logger
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-ywliu committed Jul 12, 2024
1 parent 2687db9 commit b0e849b
Show file tree
Hide file tree
Showing 21 changed files with 1,547 additions and 1,377 deletions.
20 changes: 11 additions & 9 deletions internal/gtp/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func HandleQoSTPDU(c gtp.Conn, senderAddr net.Addr, msg gtpMsg.Message) error {

// Forward user plane packets from N3 to UE with GRE header and new IP header encapsulated
func forward(packet gtpQoSMsg.QoSTPDUPacket) {
gtpLog := logger.GTPLog

defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
logger.GTPLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
gtpLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
}()

Expand All @@ -41,18 +43,18 @@ func forward(packet gtpQoSMsg.QoSTPDUPacket) {
NWuConn := self.NWuIPv4PacketConn

pktTEID := packet.GetTEID()
logger.GTPLog.Tracef("pkt teid : %d", pktTEID)
gtpLog.Tracef("pkt teid : %d", pktTEID)

// Find UE information
ranUe, ok := self.AllocatedUETEIDLoad(pktTEID)
if !ok {
logger.GTPLog.Errorf("Cannot find RanUE context from QosPacket TEID : %+v", pktTEID)
gtpLog.Errorf("Cannot find RanUE context from QosPacket TEID : %+v", pktTEID)
return
}

ikeUe, err := self.IkeUeLoadFromNgapId(ranUe.RanUeNgapId)
if err != nil {
logger.GTPLog.Errorf("Cannot find IkeUe context from RanUe , NgapID : %+v", ranUe.RanUeNgapId)
gtpLog.Errorf("Cannot find IkeUe context from RanUe , NgapID : %+v", ranUe.RanUeNgapId)
return
}

Expand All @@ -64,7 +66,7 @@ func forward(packet gtpQoSMsg.QoSTPDUPacket) {
for _, childSA := range ikeUe.N3IWFChildSecurityAssociation {
pdusession := ranUe.FindPDUSession(childSA.PDUSessionIds[0])
if pdusession != nil && pdusession.GTPConnection.IncomingTEID == pktTEID {
logger.GTPLog.Tracef("forwarding IPSec xfrm interfaceid : %d", childSA.XfrmIface.Attrs().Index)
gtpLog.Tracef("forwarding IPSec xfrm interfaceid : %d", childSA.XfrmIface.Attrs().Index)
cm = &ipv4.ControlMessage{
IfIndex: childSA.XfrmIface.Attrs().Index,
}
Expand All @@ -80,7 +82,7 @@ func forward(packet gtpQoSMsg.QoSTPDUPacket) {
// QoS Related Parameter
if packet.HasQoS() {
qfi, rqi = packet.GetQoSParameters()
logger.GTPLog.Tracef("QFI: %v, RQI: %v", qfi, rqi)
gtpLog.Tracef("QFI: %v, RQI: %v", qfi, rqi)
}

// Encasulate IPv4 packet with GRE header before forward to UE through IPsec
Expand All @@ -93,10 +95,10 @@ func forward(packet gtpQoSMsg.QoSTPDUPacket) {

// Send to UE through Nwu
if n, err := NWuConn.WriteTo(forwardData, cm, ueInnerIPAddr); err != nil {
logger.GTPLog.Errorf("Write to UE failed: %+v", err)
gtpLog.Errorf("Write to UE failed: %+v", err)
return
} else {
logger.GTPLog.Trace("Forward NWu <- N3")
logger.GTPLog.Tracef("Wrote %d bytes", n)
gtpLog.Trace("Forward NWu <- N3")
gtpLog.Tracef("Wrote %d bytes", n)
}
}
6 changes: 4 additions & 2 deletions internal/gtp/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ func (p *QoSTPDUPacket) Unmarshal(pdu *gtpMessage.TPDU) error {
// [TS 38.415]
// Define PDU Session User Plane protocol
func (p *QoSTPDUPacket) unmarshalExtensionHeader() error {
gtpLog := logger.GTPLog

for _, eh := range p.tPDU.ExtensionHeaders {
switch eh.Type {
case gtpMessage.ExtHeaderTypePDUSessionContainer:
p.qos = true
p.rqi = ((int(eh.Content[1]) >> 6) & 0x1) == 1
p.qfi = eh.Content[1] & 0x3F
logger.GTPLog.Tracef("Parsed Extension Header: Len=%d, Next Type=%d, Content Dump:\n%s",
gtpLog.Tracef("Parsed Extension Header: Len=%d, Next Type=%d, Content Dump:\n%s",
eh.Length, eh.NextType, hex.Dump(eh.Content))
default:
logger.GTPLog.Warningf("Unsupported Extension Header Field Value: %x", eh.Type)
gtpLog.Warningf("Unsupported Extension Header Field Value: %x", eh.Type)
}
}

Expand Down
7 changes: 4 additions & 3 deletions internal/gtp/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,30 @@ var gtpContext context.Context = context.TODO()
// SetupGTPTunnelWithUPF set up GTP connection with UPF
// return *gtp.UPlaneConn, net.Addr and error
func SetupGTPTunnelWithUPF(upfIPAddr string) (*gtp.UPlaneConn, net.Addr, error) {
gtpLog := logger.GTPLog
n3iwfSelf := n3iwfContext.N3IWFSelf()

// Set up GTP connection
upfUDPAddr := upfIPAddr + gtp.GTPUPort

remoteUDPAddr, err := net.ResolveUDPAddr("udp", upfUDPAddr)
if err != nil {
logger.GTPLog.Errorf("Resolve UDP address %s failed: %+v", upfUDPAddr, err)
gtpLog.Errorf("Resolve UDP address %s failed: %+v", upfUDPAddr, err)
return nil, nil, errors.New("Resolve Address Failed")
}

n3iwfUDPAddr := n3iwfSelf.GTPBindAddress + gtp.GTPUPort

localUDPAddr, err := net.ResolveUDPAddr("udp", n3iwfUDPAddr)
if err != nil {
logger.GTPLog.Errorf("Resolve UDP address %s failed: %+v", n3iwfUDPAddr, err)
gtpLog.Errorf("Resolve UDP address %s failed: %+v", n3iwfUDPAddr, err)
return nil, nil, errors.New("Resolve Address Failed")
}

// Dial to UPF
userPlaneConnection, err := gtp.DialUPlane(gtpContext, localUDPAddr, remoteUDPAddr)
if err != nil {
logger.GTPLog.Errorf("Dial to UPF failed: %+v", err)
gtpLog.Errorf("Dial to UPF failed: %+v", err)
return nil, nil, errors.New("Dial failed")
}

Expand Down
18 changes: 10 additions & 8 deletions internal/ngap/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (
)

func NGAPDispatch(conn *sctp.SCTPConn, msg []byte) {
ngapLog := logger.NgapLog

defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
logger.NgapLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
ngapLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
}()

Expand All @@ -26,15 +28,15 @@ func NGAPDispatch(conn *sctp.SCTPConn, msg []byte) {
// Decode
pdu, err := ngap.Decoder(msg)
if err != nil {
logger.NgapLog.Errorf("NGAP decode error: %+v\n", err)
ngapLog.Errorf("NGAP decode error: %+v\n", err)
return
}

switch pdu.Present {
case ngapType.NGAPPDUPresentInitiatingMessage:
initiatingMessage := pdu.InitiatingMessage
if initiatingMessage == nil {
logger.NgapLog.Errorln("Initiating Message is nil")
ngapLog.Errorln("Initiating Message is nil")
return
}

Expand Down Expand Up @@ -76,13 +78,13 @@ func NGAPDispatch(conn *sctp.SCTPConn, msg []byte) {
case ngapType.ProcedureCodeOverloadStop:
handler.HandleOverloadStop(amf, pdu)
default:
logger.NgapLog.Warnf("Not implemented NGAP message(initiatingMessage), procedureCode:%d]\n",
ngapLog.Warnf("Not implemented NGAP message(initiatingMessage), procedureCode:%d]\n",
initiatingMessage.ProcedureCode.Value)
}
case ngapType.NGAPPDUPresentSuccessfulOutcome:
successfulOutcome := pdu.SuccessfulOutcome
if successfulOutcome == nil {
logger.NgapLog.Errorln("Successful Outcome is nil")
ngapLog.Errorln("Successful Outcome is nil")
return
}

Expand All @@ -96,13 +98,13 @@ func NGAPDispatch(conn *sctp.SCTPConn, msg []byte) {
case ngapType.ProcedureCodeRANConfigurationUpdate:
handler.HandleRANConfigurationUpdateAcknowledge(amf, pdu)
default:
logger.NgapLog.Warnf("Not implemented NGAP message(successfulOutcome), procedureCode:%d]\n",
ngapLog.Warnf("Not implemented NGAP message(successfulOutcome), procedureCode:%d]\n",
successfulOutcome.ProcedureCode.Value)
}
case ngapType.NGAPPDUPresentUnsuccessfulOutcome:
unsuccessfulOutcome := pdu.UnsuccessfulOutcome
if unsuccessfulOutcome == nil {
logger.NgapLog.Errorln("Unsuccessful Outcome is nil")
ngapLog.Errorln("Unsuccessful Outcome is nil")
return
}

Expand All @@ -112,7 +114,7 @@ func NGAPDispatch(conn *sctp.SCTPConn, msg []byte) {
case ngapType.ProcedureCodeRANConfigurationUpdate:
handler.HandleRANConfigurationUpdateFailure(amf, pdu)
default:
logger.NgapLog.Warnf("Not implemented NGAP message(unsuccessfulOutcome), procedureCode:%d]\n",
ngapLog.Warnf("Not implemented NGAP message(unsuccessfulOutcome), procedureCode:%d]\n",
unsuccessfulOutcome.ProcedureCode.Value)
}
}
Expand Down
Loading

0 comments on commit b0e849b

Please sign in to comment.