forked from ardzoht/omec-upf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfcpiface.go
121 lines (87 loc) · 1.98 KB
/
pfcpiface.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022-present Open Networking Foundation
package pfcpiface
import (
"context"
"errors"
"flag"
"net/http"
"sync"
"time"
)
var (
simulate = simModeDisable
)
func init() {
flag.Var(&simulate, "simulate", "create|delete|create_continue simulated sessions")
}
type PFCPIface struct {
conf Conf
node *PFCPNode
Dp Datapath
Upf *Upf
httpSrv *http.Server
httpEndpoint string
uc *UpfCollector
nc *PfcpNodeCollector
mu sync.Mutex
}
func NewPFCPIface(conf Conf, dp Datapath) *PFCPIface {
pfcpIface := &PFCPIface{
conf: conf,
}
pfcpIface.Dp = dp
httpPort := "8080"
if conf.CPIface.HTTPPort != "" {
httpPort = conf.CPIface.HTTPPort
}
pfcpIface.httpEndpoint = ":" + httpPort
pfcpIface.Upf = NewUPF(&conf, pfcpIface.Dp)
Zap_init()
return pfcpIface
}
func (p *PFCPIface) mustInit() {
p.mu.Lock()
defer p.mu.Unlock()
p.node = NewPFCPNode(p.Upf)
httpMux := http.NewServeMux()
setupConfigHandler(httpMux, p.Upf)
var err error
p.uc, p.nc, err = setupProm(httpMux, p.Upf, p.node)
if err != nil {
log.Fatal("setupProm failed ", err)
}
p.httpSrv = &http.Server{Addr: p.httpEndpoint, Handler: httpMux}
}
func (p *PFCPIface) Run() {
if simulate.enable() {
p.Upf.sim(simulate, &p.conf.SimInfo)
if !simulate.keepGoing() {
return
}
}
p.mustInit()
go func() {
if err := p.httpSrv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal("http server failed ", err)
}
log.Info("http server closed")
}()
// blocking
p.node.Serve()
}
// Stop sends cancellation signal to main Go routine and waits for shutdown to complete.
func (p *PFCPIface) Stop() {
p.mu.Lock()
defer p.mu.Unlock()
ctxHttpShutdown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer func() {
cancel()
}()
if err := p.httpSrv.Shutdown(ctxHttpShutdown); err != nil {
log.Error("Failed to shutdown http: ", err)
}
p.node.Stop()
// Wait for PFCP node shutdown
p.node.Done()
}