This repository has been archived by the owner on Apr 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
user_plane_information.go
422 lines (348 loc) · 11.4 KB
/
user_plane_information.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package smf_context
import (
"fmt"
"free5gc/lib/pfcp/pfcpType"
"free5gc/lib/util_3gpp"
"free5gc/src/smf/factory"
"free5gc/src/smf/logger"
"net"
"reflect"
)
// UserPlaneInformation store userplane topology
type UserPlaneInformation struct {
UPNodes map[string]*UPNode
UPFs map[string]*UPNode
AccessNetwork map[string]*UPNode
UPFIPToName map[string]string
UPFsID map[string]string // name to id
UPFsIPtoID map[string]string // ip->id table, for speed optimization
DefaultUserPlanePath map[string][]*UPNode // DNN to Default Path
}
type UPNodeType string
const (
UPNODE_UPF UPNodeType = "UPF"
UPNODE_AN UPNodeType = "AN"
)
// UPNode represent the user plane node topology
type UPNode struct {
Type UPNodeType
NodeID pfcpType.NodeID
UPResourceIP net.IP
ANIP net.IP
Dnn string
Links []*UPNode
UPF *UPF
}
// UPPath represent User Plane Sequence of this path
type UPPath []*UPNode
func AllocateUPFID() {
UPFsID := smfContext.UserPlaneInformation.UPFsID
UPFsIPtoID := smfContext.UserPlaneInformation.UPFsIPtoID
for upfName, upfNode := range smfContext.UserPlaneInformation.UPFs {
upfid := upfNode.UPF.UUID()
upfip := upfNode.NodeID.ResolveNodeIdToIp().String()
UPFsID[upfName] = upfid
UPFsIPtoID[upfip] = upfid
}
}
func processUPTopology(upTopology *factory.UserPlaneInformation) {
nodePool := make(map[string]*UPNode)
upfPool := make(map[string]*UPNode)
anPool := make(map[string]*UPNode)
upfIPMap := make(map[string]string)
for name, node := range upTopology.UPNodes {
upNode := new(UPNode)
upNode.Type = UPNodeType(node.Type)
switch upNode.Type {
case UPNODE_AN:
upNode.ANIP = net.ParseIP(node.ANIP)
anPool[name] = upNode
case UPNODE_UPF:
//ParseIp() always return 16 bytes
//so we can't use the length of return ip to seperate IPv4 and IPv6
//This is just a work around
var ip net.IP
if net.ParseIP(node.NodeID).To4() == nil {
ip = net.ParseIP(node.NodeID)
} else {
ip = net.ParseIP(node.NodeID).To4()
}
switch len(ip) {
case net.IPv4len:
upNode.NodeID = pfcpType.NodeID{
NodeIdType: pfcpType.NodeIdTypeIpv4Address,
NodeIdValue: ip,
}
case net.IPv6len:
upNode.NodeID = pfcpType.NodeID{
NodeIdType: pfcpType.NodeIdTypeIpv6Address,
NodeIdValue: ip,
}
default:
upNode.NodeID = pfcpType.NodeID{
NodeIdType: pfcpType.NodeIdTypeFqdn,
NodeIdValue: []byte(node.NodeID),
}
}
upfPool[name] = upNode
default:
logger.InitLog.Warningf("invalid UPNodeType: %s\n", upNode.Type)
}
nodePool[name] = upNode
ipStr := upNode.NodeID.ResolveNodeIdToIp().String()
upfIPMap[ipStr] = name
}
for _, link := range upTopology.Links {
nodeA := nodePool[link.A]
nodeB := nodePool[link.B]
if nodeA == nil || nodeB == nil {
logger.InitLog.Warningf("UPLink [%s] <=> [%s] not establish\n", link.A, link.B)
continue
}
nodeA.Links = append(nodeA.Links, nodeB)
nodeB.Links = append(nodeB.Links, nodeA)
}
//Initialize each UPF
for _, upfNode := range upfPool {
upfNode.UPF = NewUPF(&upfNode.NodeID)
}
smfContext.UserPlaneInformation.UPNodes = nodePool
smfContext.UserPlaneInformation.UPFs = upfPool
smfContext.UserPlaneInformation.AccessNetwork = anPool
smfContext.UserPlaneInformation.UPFIPToName = upfIPMap
smfContext.UserPlaneInformation.UPFsID = make(map[string]string)
smfContext.UserPlaneInformation.UPFsIPtoID = make(map[string]string)
smfContext.UserPlaneInformation.DefaultUserPlanePath = make(map[string][]*UPNode)
}
func (upi *UserPlaneInformation) GetUPFNameByIp(ip string) string {
return upi.UPFIPToName[ip]
}
func (upi *UserPlaneInformation) GetUPFNodeIDByName(name string) pfcpType.NodeID {
return upi.UPFs[name].NodeID
}
func (upi *UserPlaneInformation) GetUPFNodeByIP(ip string) *UPNode {
upfName := upi.GetUPFNameByIp(ip)
return upi.UPFs[upfName]
}
func (upi *UserPlaneInformation) GetUPFIDByIP(ip string) string {
return upi.UPFsIPtoID[ip]
}
func (upi *UserPlaneInformation) GetDefaultUserPlanePathByDNN(dnn string) (path UPPath) {
path, pathExist := upi.DefaultUserPlanePath[dnn]
if pathExist {
return
} else {
pathExist = upi.GenerateDefaultPath(dnn)
if pathExist {
return upi.DefaultUserPlanePath[dnn]
}
}
return nil
}
func (upi *UserPlaneInformation) ExistDefaultPath(dnn string) bool {
_, exist := upi.DefaultUserPlanePath[dnn]
return exist
}
func GenerateDataPath(upPath UPPath, smContext *SMContext) (root *DataPathNode) {
if len(upPath) < 1 {
logger.CtxLog.Errorf("invalid path")
}
var lowerBound = 0
var upperBound = len(upPath) - 1
var curDataPathNode *DataPathNode
var prevDataPathNode *DataPathNode
for idx, upNode := range upPath {
curDataPathNode := NewDataPathNode()
curDataPathNode.UPF = upNode.UPF
curDataPathNode.InUse = true
if idx == lowerBound {
root = curDataPathNode
root.DataPathToAN = NewDataPathDownLink()
root.SetUpLinkSrcNode(nil)
}
if idx == upperBound {
curDataPathNode.SetDownLinkSrcNode(nil)
// curDataPathNode.AddParent(prevDataPathNode)
curDataPathNode.DLDataPathLinkForPSA = NewDataPathUpLink()
// prevDataPathNode.AddChild(curDataPathNode)
}
if prevDataPathNode != nil {
prevDataPathNode.SetDownLinkSrcNode(curDataPathNode)
curDataPathNode.SetUpLinkSrcNode(prevDataPathNode)
curDataPathNode.AddParent(prevDataPathNode)
prevDataPathNode.AddChild(curDataPathNode)
}
prevDataPathNode = curDataPathNode
}
curDataPathNode = root
for curDataPathNode != nil {
fmt.Println("calculate ", curDataPathNode.UPF.PFCPAddr().String())
curULTunnel := curDataPathNode.UpLinkTunnel
curDLTunnel := curDataPathNode.DownLinkTunnel
// Setup UpLink PDR
if curULTunnel != nil {
ULPDR := curULTunnel.MatchedPDR
ULDestUPF := curULTunnel.DestEndPoint.UPF
ULPDR.Precedence = 32
ULPDR.PDI = PDI{
SourceInterface: pfcpType.SourceInterface{InterfaceValue: pfcpType.SourceInterfaceAccess},
LocalFTeid: &pfcpType.FTEID{
V4: true,
Ipv4Address: ULDestUPF.UPIPInfo.Ipv4Address,
Teid: curULTunnel.TEID,
},
UEIPAddress: &pfcpType.UEIPAddress{
V4: true,
Ipv4Address: smContext.PDUAddress.To4(),
},
}
ULPDR.OuterHeaderRemoval = &pfcpType.OuterHeaderRemoval{OuterHeaderRemovalDescription: pfcpType.OuterHeaderRemovalGtpUUdpIpv4}
ULFAR := ULPDR.FAR
if curDLTunnel != nil {
if nextULDest := curDLTunnel.SrcEndPoint; nextULDest != nil {
nextULTunnel := nextULDest.UpLinkTunnel
ULFAR.ApplyAction = pfcpType.ApplyAction{Buff: false, Drop: false, Dupl: false, Forw: true, Nocp: false}
ULFAR.ForwardingParameters = &ForwardingParameters{
DestinationInterface: pfcpType.DestinationInterface{InterfaceValue: pfcpType.DestinationInterfaceCore},
OuterHeaderCreation: &pfcpType.OuterHeaderCreation{
OuterHeaderCreationDescription: pfcpType.OuterHeaderCreationGtpUUdpIpv4,
Ipv4Address: nextULTunnel.DestEndPoint.UPF.UPIPInfo.Ipv4Address,
Teid: nextULTunnel.TEID,
},
}
}
}
}
// Setup DownLink
if curDLTunnel != nil {
DLPDR := curDLTunnel.MatchedPDR
DLDestUPF := curDLTunnel.DestEndPoint.UPF
DLPDR.Precedence = 32
DLPDR.PDI = PDI{
SourceInterface: pfcpType.SourceInterface{InterfaceValue: pfcpType.SourceInterfaceCore},
LocalFTeid: &pfcpType.FTEID{
V4: true,
Ipv4Address: DLDestUPF.UPIPInfo.Ipv4Address,
Teid: curDLTunnel.TEID,
},
// TODO: Should Uncomment this after FR5GC-1029 is solved
// UEIPAddress: &pfcpType.UEIPAddress{
// V4: true,
// Ipv4Address: smContext.PDUAddress.To4(),
// },
}
// TODO: Should delete this after FR5GC-1029 is solved
if curDataPathNode.IsAnchorUPF() {
DLPDR.PDI.UEIPAddress = &pfcpType.UEIPAddress{
V4: true,
Ipv4Address: smContext.PDUAddress.To4(),
}
}
fmt.Println("In GenerateDataPath")
fmt.Println("curDataPathNode IP: ", curDataPathNode.GetNodeIP())
fmt.Println("Is anchor point: ", curDataPathNode.IsAnchorUPF())
if !curDataPathNode.IsAnchorUPF() {
DLPDR.OuterHeaderRemoval = &pfcpType.OuterHeaderRemoval{OuterHeaderRemovalDescription: pfcpType.OuterHeaderRemovalGtpUUdpIpv4}
}
DLFAR := DLPDR.FAR
nextDLTunnel := curDLTunnel.DestEndPoint.DownLinkTunnel
fmt.Println("DestEndPoint TEID", nextDLTunnel.TEID)
fmt.Println("SrcEndPoint TEID", curDLTunnel.DestEndPoint.UpLinkTunnel.TEID)
//fmt.Println("SrcEndPoint IP", curDLTunnel.DestEndPoint.UpLinkTunnel.SrcEndPoint.GetNodeIP())
if nextDLDest := curULTunnel.SrcEndPoint; nextDLDest != nil {
fmt.Println("In GenerateDataPath")
fmt.Println(nextDLDest.GetNodeIP())
DLFAR.ApplyAction = pfcpType.ApplyAction{Buff: false, Drop: false, Dupl: false, Forw: true, Nocp: false}
DLFAR.ForwardingParameters = &ForwardingParameters{
DestinationInterface: pfcpType.DestinationInterface{InterfaceValue: pfcpType.DestinationInterfaceAccess},
OuterHeaderCreation: &pfcpType.OuterHeaderCreation{
OuterHeaderCreationDescription: pfcpType.OuterHeaderCreationGtpUUdpIpv4,
Ipv4Address: nextDLDest.UPF.NodeID.ResolveNodeIdToIp(),
Teid: curDLTunnel.DestEndPoint.UpLinkTunnel.TEID,
},
}
}
}
if curDataPathNode.DownLinkTunnel != nil {
if curDataPathNode.DownLinkTunnel.SrcEndPoint == nil {
DNDLPDR := curDataPathNode.DownLinkTunnel.MatchedPDR
DNDLPDR.PDI = PDI{
SourceInterface: pfcpType.SourceInterface{InterfaceValue: pfcpType.SourceInterfaceCore},
NetworkInstance: util_3gpp.Dnn(smContext.Dnn),
UEIPAddress: &pfcpType.UEIPAddress{
V4: true,
Ipv4Address: smContext.PDUAddress.To4(),
},
}
break
}
}
if curDataPathNode.DownLinkTunnel == nil {
break
}
curDataPathNode = curDataPathNode.DownLinkTunnel.SrcEndPoint
}
return
}
func (upi *UserPlaneInformation) GenerateDefaultPath(dnn string) (pathExist bool) {
var source *UPNode
var destination *UPNode
for _, node := range upi.AccessNetwork {
if node.Type == UPNODE_AN {
source = node
break
}
}
if source == nil {
logger.CtxLog.Errorf("There is no AN Node in config file!")
return false
}
for _, node := range upi.UPFs {
if node.UPF.UPIPInfo.NetworkInstance != nil {
node_dnn := string(node.UPF.UPIPInfo.NetworkInstance)
if node_dnn == dnn {
destination = node
break
}
}
}
if destination == nil {
logger.CtxLog.Errorf("Can't find UPF with DNN [%s]\n", dnn)
return false
}
//Run DFS
var visited map[*UPNode]bool
visited = make(map[*UPNode]bool)
for _, upNode := range upi.UPNodes {
visited[upNode] = false
}
var path []*UPNode
path, pathExist = getPathBetween(source, destination, visited)
if path[0].Type == UPNODE_AN {
path = path[1:]
}
upi.DefaultUserPlanePath[dnn] = path
return
}
func getPathBetween(cur *UPNode, dest *UPNode, visited map[*UPNode]bool) (path []*UPNode, pathExist bool) {
visited[cur] = true
if reflect.DeepEqual(*cur, *dest) {
path = make([]*UPNode, 0)
path = append(path, cur)
pathExist = true
return
}
for _, nodes := range cur.Links {
if !visited[nodes] {
path_tail, path_exist := getPathBetween(nodes, dest, visited)
if path_exist {
path = make([]*UPNode, 0)
path = append(path, cur)
path = append(path, path_tail...)
pathExist = true
return
}
}
}
return nil, false
}