Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor and update util to use new log formatter setting #19

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 53 additions & 28 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,94 @@
package main

import (
"fmt"
"os"
"path/filepath"
"runtime/debug"

"github.com/asaskevich/govalidator"
"github.com/urfave/cli"

"github.com/free5gc/n3iwf/internal/logger"
"github.com/free5gc/n3iwf/pkg/factory"
"github.com/free5gc/n3iwf/pkg/service"
logger_util "github.com/free5gc/util/logger"
"github.com/free5gc/util/version"
)

var N3IWF = &service.N3IWF{}
var N3IWF *service.N3iwfApp

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

app := cli.NewApp()
app.Name = "n3iwf"
app.Usage = "Non-3GPP Interworking Function (N3IWF)"
app.Action = action
app.Flags = N3IWF.GetCliCmd()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
},
cli.StringSliceFlag{
Name: "log, l",
Usage: "Output NF log to `FILE`",
},
}
if err := app.Run(os.Args); err != nil {
logger.AppLog.Errorf("N3IWF Run Error: %v\n", err)
logger.MainLog.Errorf("N3IWF Run Error: %v\n", err)
}
}

func action(c *cli.Context) error {
if err := initLogFile(c.String("log"), c.String("log5gc")); err != nil {
logger.AppLog.Errorf("%+v", err)
func action(cliCtx *cli.Context) error {
tlsKeyLogPath, err := initLogFile(cliCtx.StringSlice("log"))
if err != nil {
return err
}

if err := N3IWF.Initialize(c); err != nil {
switch errType := err.(type) {
case govalidator.Errors:
validErrs := err.(govalidator.Errors).Errors()
for _, validErr := range validErrs {
logger.CfgLog.Errorf("%+v", validErr)
}
default:
logger.CfgLog.Errorf("%+v", errType)
}
logger.CfgLog.Errorf("[-- PLEASE REFER TO SAMPLE CONFIG FILE COMMENTS --]")
return fmt.Errorf("Failed to initialize !!")
logger.MainLog.Infoln("N3IWF version: ", version.GetVersion())

cfg, err := factory.ReadConfig(cliCtx.String("config"))
if err != nil {
return err
}
factory.N3iwfConfig = cfg

logger.AppLog.Infoln(c.App.Name)
logger.AppLog.Infoln("N3IWF version: ", version.GetVersion())
n3iwf, err := service.NewApp(cfg)
if err != nil {
return err
}
N3IWF = n3iwf

N3IWF.Start()
n3iwf.Start(tlsKeyLogPath)

return nil
}

func initLogFile(logNfPath, log5gcPath string) error {
if err := logger.LogFileHook(logNfPath, log5gcPath); err != nil {
return err
func initLogFile(logNfPath []string) (string, error) {
logTlsKeyPath := ""

for _, path := range logNfPath {
if err := logger_util.LogFileHook(logger.Log, path); err != nil {
return "", err
}

if logTlsKeyPath != "" {
continue
}

nfDir, _ := filepath.Split(path)
tmpDir := filepath.Join(nfDir, "key")
if err := os.MkdirAll(tmpDir, 0o775); err != nil {
logger.InitLog.Errorf("Make directory %s failed: %+v", tmpDir, err)
return "", err
}
_, name := filepath.Split(factory.N3iwfDefaultTLSKeyLogPath)
logTlsKeyPath = filepath.Join(tmpDir, name)
}
return nil

return logTlsKeyPath, nil
}
31 changes: 28 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module github.com/free5gc/n3iwf

go 1.14
go 1.17

require (
git.cs.nctu.edu.tw/calee/sctp v1.1.0
github.com/antonfisher/nested-logrus-formatter v1.3.1
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/free5gc/aper v1.0.4
github.com/free5gc/ngap v1.0.6
github.com/free5gc/util v1.0.3
github.com/free5gc/util v1.0.5-0.20230306071612-a52909216bd2
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli v1.22.5
github.com/vishvananda/netlink v1.1.0
Expand All @@ -17,3 +16,29 @@ require (
golang.org/x/sys v0.5.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/antonfisher/nested-logrus-formatter v1.3.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/free5gc/openapi v1.0.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.7.7 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
golang.org/x/crypto v0.1.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
)
19 changes: 14 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ github.com/free5gc/ngap v1.0.6 h1:f9sKqHMNrFZVo9Kp8hAyrCXSoI8l746N5O+DFn7vKHA=
github.com/free5gc/ngap v1.0.6/go.mod h1:TG1kwwU/EyIlJ3bxY591rdxpD5ZeYnLZTzoWjcfvrBM=
github.com/free5gc/openapi v1.0.4 h1:bC6oqXy8Z+3e532xLMFmrTHvdyv4sNGDPezQSslw5gQ=
github.com/free5gc/openapi v1.0.4/go.mod h1:KRCnnp0GeK0Bl4gnrX79cQAidKXNENf8VRdG0y9R0Fc=
github.com/free5gc/util v1.0.3 h1:or/gqHCAi3j2YKd+nzViRnc/tl1tuuJAYxCao6IbOAU=
github.com/free5gc/util v1.0.3/go.mod h1:DL1Dnryh//Ps5B+hfXbhU1R07fVfrmPs4uuTO4g9yTg=
github.com/free5gc/util v1.0.5-0.20230306071612-a52909216bd2 h1:FG8KlJ46Epscj3F9XBAKuDGJD9kSKJdstCL9fttjUjE=
github.com/free5gc/util v1.0.5-0.20230306071612-a52909216bd2/go.mod h1:fgV0hXf5arxAWs8D9LfrrfNByZ1IDCWYlgBzncy5GtE=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.3 h1:aMBzLJ/GMEYmv1UWs2FFTcPISLrQH2mRgL9Glz8xows=
github.com/gin-gonic/gin v1.7.3/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -187,6 +187,7 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -201,6 +202,7 @@ github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0Mw
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -240,6 +242,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tim-ywliu/nested-logrus-formatter v1.3.2 h1:jugNJ2/CNCI79SxOJCOhwUHeN3O7/7/bj+ZRGOFlCSw=
github.com/tim-ywliu/nested-logrus-formatter v1.3.2/go.mod h1:oGPmcxZB65j9Wo7mCnQKSrKEJtVDqyjD666SGmyStXI=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
Expand Down Expand Up @@ -273,8 +277,9 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -338,6 +343,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210501142056-aec3718b3fa0/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -397,11 +403,13 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -411,6 +419,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
38 changes: 19 additions & 19 deletions internal/gtp/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"
"runtime/debug"

"github.com/sirupsen/logrus"
gtp "github.com/wmnsk/go-gtp/gtpv1"
gtpMsg "github.com/wmnsk/go-gtp/gtpv1/message"
"golang.org/x/net/ipv4"
Expand All @@ -15,12 +14,6 @@ import (
n3iwfContext "github.com/free5gc/n3iwf/pkg/context"
)

var gtpLog *logrus.Entry

func init() {
gtpLog = logger.GTPLog
}

// Parse the fields not supported by go-gtp and forward data to UE.
func HandleQoSTPDU(c gtp.Conn, senderAddr net.Addr, msg gtpMsg.Message) error {
pdu := gtpQoSMsg.QoSTPDUPacket{}
Expand All @@ -37,7 +30,7 @@ func forward(packet gtpQoSMsg.QoSTPDUPacket) {
defer func() {
if p := recover(); p != nil {
// Print stack for panic to log. Fatalf() will let program exit.
gtpLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
logger.GTPLog.Fatalf("panic: %v\n%s", p, string(debug.Stack()))
}
}()

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

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

// Find UE information
ue, ok := self.AllocatedUETEIDLoad(packet.GetTEID())
ranUe, ok := self.AllocatedUETEIDLoad(pktTEID)
if !ok {
gtpLog.Error("UE context not found")
logger.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)
return
}

// UE inner IP in IPSec
ueInnerIPAddr := ue.IPSecInnerIPAddr
ueInnerIPAddr := ikeUe.IPSecInnerIPAddr

var cm *ipv4.ControlMessage

for _, childSA := range ue.N3IWFChildSecurityAssociation {
pdusession := ue.FindPDUSession(childSA.PDUSessionIds[0])
for _, childSA := range ikeUe.N3IWFChildSecurityAssociation {
pdusession := ranUe.FindPDUSession(childSA.PDUSessionIds[0])
if pdusession != nil && pdusession.GTPConnection.IncomingTEID == pktTEID {
gtpLog.Tracef("forwarding IPSec xfrm interfaceid : %d", childSA.XfrmIface.Attrs().Index)
logger.GTPLog.Tracef("forwarding IPSec xfrm interfaceid : %d", childSA.XfrmIface.Attrs().Index)
cm = &ipv4.ControlMessage{
IfIndex: childSA.XfrmIface.Attrs().Index,
}
break
}
}

Expand All @@ -80,7 +80,7 @@ func forward(packet gtpQoSMsg.QoSTPDUPacket) {
// QoS Related Parameter
if packet.HasQoS() {
qfi, rqi = packet.GetQoSParameters()
gtpLog.Tracef("QFI: %v, RQI: %v", qfi, rqi)
logger.GTPLog.Tracef("QFI: %v, RQI: %v", qfi, rqi)
}

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

// Send to UE through Nwu
if n, err := NWuConn.WriteTo(forwardData, cm, ueInnerIPAddr); err != nil {
gtpLog.Errorf("Write to UE failed: %+v", err)
logger.GTPLog.Errorf("Write to UE failed: %+v", err)
return
} else {
gtpLog.Trace("Forward NWu <- N3")
gtpLog.Tracef("Wrote %d bytes", n)
logger.GTPLog.Trace("Forward NWu <- N3")
logger.GTPLog.Tracef("Wrote %d bytes", n)
}
}
16 changes: 4 additions & 12 deletions internal/gtp/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"net"

"github.com/sirupsen/logrus"
gtp "github.com/wmnsk/go-gtp/gtpv1"
gtpMsg "github.com/wmnsk/go-gtp/gtpv1/message"

Expand All @@ -14,14 +13,7 @@ import (
n3iwfContext "github.com/free5gc/n3iwf/pkg/context"
)

var gtpLog *logrus.Entry

var gtpContext context.Context

func init() {
gtpLog = logger.GTPLog
gtpContext = context.TODO()
}
var gtpContext context.Context = context.TODO()

// SetupGTPTunnelWithUPF set up GTP connection with UPF
// return *gtp.UPlaneConn, net.Addr and error
Expand All @@ -33,22 +25,22 @@ func SetupGTPTunnelWithUPF(upfIPAddr string) (*gtp.UPlaneConn, net.Addr, error)

remoteUDPAddr, err := net.ResolveUDPAddr("udp", upfUDPAddr)
if err != nil {
gtpLog.Errorf("Resolve UDP address %s failed: %+v", upfUDPAddr, err)
logger.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 {
gtpLog.Errorf("Resolve UDP address %s failed: %+v", n3iwfUDPAddr, err)
logger.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 {
gtpLog.Errorf("Dial to UPF failed: %+v", err)
logger.GTPLog.Errorf("Dial to UPF failed: %+v", err)
return nil, nil, errors.New("Dial failed")
}

Expand Down
Loading