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

Go lint all #316

Merged
merged 9 commits into from
Aug 10, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
222 changes: 146 additions & 76 deletions pfcpiface/bess.go

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions pfcpiface/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (
"github.com/wmnsk/go-pfcp/message"
)

// PktBufSz : buffer size for incoming pkt
// PktBufSz : buffer size for incoming pkt.
const (
PktBufSz = 1500
PFCPPort = "8805"
MaxItems = 10
readTimeout = 25 * time.Second
)

//Timeout : connection timeout
// Timeout : connection timeout.
var Timeout = 1000 * time.Millisecond

// PFCPConn represents a PFCP connection
// PFCPConn represents a PFCP connection.
type PFCPConn struct {
seqNum sequenceNumber
mgr *PFCPSessionMgr
Expand All @@ -39,16 +39,19 @@ func (c *PFCPConn) getSeqNum() uint32 {
c.seqNum.mux.Lock()
defer c.seqNum.mux.Unlock()
c.seqNum.seq++

return c.seqNum.seq
}

func pfcpifaceMainLoop(upf *upf, accessIP, coreIP, sourceIP, smfName string) {
var pconn PFCPConn
pconn.mgr = NewPFCPSessionMgr(100)

rTimeout := readTimeout
if upf.readTimeout != 0 {
rTimeout = time.Duration(upf.readTimeout)
rTimeout = upf.readTimeout
}

if upf.connTimeout != 0 {
Timeout = upf.connTimeout
}
Expand Down Expand Up @@ -82,14 +85,18 @@ func pfcpifaceMainLoop(upf *upf, accessIP, coreIP, sourceIP, smfName string) {
if upf.simInfo != nil {
return
}

sendDeleteAllSessionsMsgtoUPF(upf)

cpConnected = false
}
// initiate connection if smf address available
log.Println("calling manageSmfConnection smf service name ", smfName)

manageConnection := false
if smfName != "" {
manageConnection = true

go pconn.manageSmfConnection(upf.nodeIP.String(), accessIP, smfName, conn, cpConnectionStatus, upf.recoveryTime)
}

Expand All @@ -107,13 +114,18 @@ func pfcpifaceMainLoop(upf *upf, accessIP, coreIP, sourceIP, smfName string) {
if err, ok := err.(net.Error); ok && err.Timeout() {
// do nothing for the time being
log.Println(err)

cpConnected = false

if manageConnection {
cpConnectionStatus <- cpConnected
}

cleanupSessions()

continue
}

log.Fatalln("Read error:", err)
}

Expand All @@ -125,14 +137,14 @@ func pfcpifaceMainLoop(upf *upf, accessIP, coreIP, sourceIP, smfName string) {
}

// if sourceIP is not set, fetch it from the msg header
if sourceIP == "0.0.0.0" {
if sourceIP == net.IPv4zero.String() {
addrString := strings.Split(addr.String(), ":")
sourceIP = getLocalIP(addrString[0]).String()
log.Println("Source IP address is now: ", sourceIP)
}

// if nodeIP is not set, fetch it from the msg header
if upf.nodeIP.String() == "0.0.0.0" {
if upf.nodeIP.String() == net.IPv4zero.String() {
addrString := strings.Split(addr.String(), ":")
upf.nodeIP = getLocalIP(addrString[0])
log.Println("Node IP address is now: ", upf.nodeIP.String())
Expand All @@ -142,21 +154,27 @@ func pfcpifaceMainLoop(upf *upf, accessIP, coreIP, sourceIP, smfName string) {

// handle message
var outgoingMessage []byte

switch msg.MessageType() {
case message.MsgTypeAssociationSetupRequest:
cleanupSessions()

go readReportNotification(upf.reportNotifyChan, &pconn, conn, addr)

upf.setInfo(conn, addr, &pconn)

outgoingMessage = pconn.handleAssociationSetupRequest(upf, msg, addr, sourceIP, accessIP, coreIP)
if outgoingMessage != nil {
cpConnected = true

if manageConnection {
// if we initiated connection, inform go routine
cpConnectionStatus <- cpConnected
}
}
case message.MsgTypeAssociationSetupResponse:
cpConnected = handleAssociationSetupResponse(msg, addr, sourceIP, accessIP)

if manageConnection {
// pass on information to go routine that result of association response
cpConnectionStatus <- cpConnected
Expand All @@ -173,6 +191,7 @@ func pfcpifaceMainLoop(upf *upf, accessIP, coreIP, sourceIP, smfName string) {
outgoingMessage = pconn.handleSessionDeletionRequest(upf, msg, addr, sourceIP)
case message.MsgTypeAssociationReleaseRequest:
outgoingMessage = handleAssociationReleaseRequest(upf, msg, addr, sourceIP, accessIP, upf.recoveryTime)

cleanupSessions()
case message.MsgTypeSessionReportResponse:
pconn.handleSessionReportResponse(upf, msg, addr)
Expand All @@ -187,6 +206,5 @@ func pfcpifaceMainLoop(upf *upf, accessIP, coreIP, sourceIP, smfName string) {
log.Fatalln("Unable to transmit association setup response", err)
}
}

}
}
14 changes: 12 additions & 2 deletions pfcpiface/fastpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@
package main

import (
"github.com/prometheus/client_golang/prometheus"
"net"

"github.com/prometheus/client_golang/prometheus"
)

type upfMsgType int

const (
upfMsgTypeAdd upfMsgType = iota
upfMsgTypeMod
upfMsgTypeDel
upfMsgTypeClear
)

type fastPath interface {
Expand All @@ -20,7 +30,7 @@ type fastPath interface {
/* write endMarker to fastpath */
sendEndMarkers(endMarkerList *[][]byte) error
/* write pdr/far/qer to fastpath */
sendMsgToUPF(method string, pdrs []pdr, fars []far, qers []qer) uint8
sendMsgToUPF(method upfMsgType, pdrs []pdr, fars []far, qers []qer) uint8
/* delete all pdrs/fars/qers/ installed in fastpath tabled */
sendDeleteAllSessionsMsgtoUPF()
/* check of communication channel to fastpath is setup */
Expand Down
6 changes: 5 additions & 1 deletion pfcpiface/ip_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ func (ipp *ipPool) allocIPV4() (net.IP, error) {
err := errors.New("ip pool empty")
return nil, err
}

element := ipp.freePool[0] // The first element is the one to be dequeued.
log.Println("Dequeued:", element)

ipp.freePool = ipp.freePool[1:] // Slice off the element once it is dequeued.
ipVal := net.ParseIP(element).To4()

return ipVal, nil
}

Expand All @@ -40,8 +43,9 @@ func (ipp *ipPool) initPool(cidr string) error {
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ipp.freePool = append(ipp.freePool, ip.String())
}
// remove network address and broadcast address

// remove network address and broadcast address
ipp.freePool = ipp.freePool[1 : len(ipp.freePool)-1]

return nil
}
34 changes: 23 additions & 11 deletions pfcpiface/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
pfcpsim = flag.Bool("pfcpsim", false, "simulate PFCP")
)

// Conf : Json conf struct
// Conf : Json conf struct.
type Conf struct {
Mode string `json:"mode"`
MaxSessions uint32 `json:"max_sessions"`
Expand All @@ -42,7 +42,7 @@ type Conf struct {
LogLevel string `json:"log_level"`
}

// SimModeInfo : Sim mode attributes
// SimModeInfo : Sim mode attributes.
type SimModeInfo struct {
StartUEIP net.IP `json:"start_ue_ip"`
StartENBIP net.IP `json:"start_enb_ip"`
Expand All @@ -53,7 +53,7 @@ type SimModeInfo struct {
StartN9TEID string `json:"start_n9_teid"`
}

// CPIfaceInfo : CPIface interface settings
// CPIfaceInfo : CPIface interface settings.
type CPIfaceInfo struct {
DestIP string `json:"nb_dst_ip"`
SrcIP string `json:"nb_src_ip"`
Expand All @@ -64,16 +64,15 @@ type CPIfaceInfo struct {
Dnn string `json:"dnn"`
}

// IfaceType : Gateway interface struct
// IfaceType : Gateway interface struct.
type IfaceType struct {
IfName string `json:"ifname"`
}

// ParseJSON : parse json file and populate corresponding struct
// ParseJSON : parse json file and populate corresponding struct.
func ParseJSON(filepath *string, conf *Conf) {
/* Open up file */
jsonFile, err := os.Open(*filepath)

if err != nil {
log.Fatalln("Error opening file: ", err)
}
Expand All @@ -96,20 +95,21 @@ func ParseJSON(filepath *string, conf *Conf) {
}
}

// ParseStrIP : parse IP address from config
// ParseStrIP : parse IP address from config.
func ParseStrIP(n3name string) (net.IP, net.IPMask) {
ip, ipNet, err := net.ParseCIDR(n3name)
if err != nil {
log.Fatalln("Unable to parse IP: ", err)
}

log.Println("IP: ", ip)

return ip, (ipNet).Mask
}

// ParseIP : parse IP address from the interface name
// ParseIP : parse IP address from the interface name.
func ParseIP(name string, iface string) net.IP {
byNameInterface, err := net.InterfaceByName(name)

if err != nil {
log.Fatalln("Unable to get info on interface name:", name, err)
}
Expand All @@ -123,15 +123,21 @@ func ParseIP(name string, iface string) net.IP {
if err != nil {
log.Fatalln("Unable to parse", iface, " IP: ", err)
}

log.Println(iface, " IP: ", ip)

return ip
}

func main() {
// cmdline args
flag.Parse()
var conf Conf
var intf fastPath

var (
conf Conf
intf fastPath
)

// read and parse json startup file
ParseJSON(configPath, &conf)

Expand All @@ -140,11 +146,13 @@ func main() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})

if level, err := log.ParseLevel(conf.LogLevel); err != nil {
log.Fatalln(err)
} else {
log.SetLevel(level)
}

log.Infoln(conf)

if conf.EnableP4rt {
Expand Down Expand Up @@ -187,16 +195,20 @@ func main() {

log.Println(*simulate, "sessions:", conf.MaxSessions)
upf.sim(*simulate)

return
}

log.Println("N4 local IP: ", upf.n4SrcIP.String())
log.Println("Access IP: ", upf.accessIP.String())
log.Println("Core IP: ", upf.coreIP.String())

if conf.CPIface.PromPort != "" {
*httpAddr = string("0.0.0.0:") + conf.CPIface.PromPort
}

log.Println("httpAddr: ", httpAddr)

go pfcpifaceMainLoop(
upf, upf.accessIP.String(),
upf.coreIP.String(), upf.n4SrcIP.String(),
Expand Down