-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathtools.go
190 lines (165 loc) · 5.07 KB
/
tools.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
/**
* SPDX-License-Identifier: Apache-2.0
* © Copyright 2023 Hewlett Packard Enterprise Development LP
*/
package tools
import (
"fmt"
"my5G-RANTester/config"
"my5G-RANTester/internal/control_test_engine/gnb"
gnbCxt "my5G-RANTester/internal/control_test_engine/gnb/context"
"my5G-RANTester/internal/control_test_engine/procedures"
"my5G-RANTester/internal/control_test_engine/ue"
ueCtx "my5G-RANTester/internal/control_test_engine/ue/context"
"net"
"strconv"
"sync"
"time"
"errors"
log "github.com/sirupsen/logrus"
)
func CreateGnbs(count int, cfg config.Config, wg *sync.WaitGroup) map[string]*gnbCxt.GNBContext {
gnbs := make(map[string]*gnbCxt.GNBContext)
var err error
// Each gNB have their own IP address on both N2 and N3
// TODO: Limitation for now, these IPs must be sequential, eg:
// gnb[0].n2_ip = 192.168.2.10, gnb[0].n3_ip = 192.168.3.10
// gnb[1].n2_ip = 192.168.2.11, gnb[1].n3_ip = 192.168.3.11
// ...
n2Ip := cfg.GNodeB.ControlIF.Ip
n3Ip := cfg.GNodeB.DataIF.Ip
for i := 1; i <= count; i++ {
cfg.GNodeB.PlmnList.GnbId = gnbIdGenerator(i)
cfg.GNodeB.ControlIF.Ip = n2Ip
cfg.GNodeB.DataIF.Ip = n3Ip
gnbs[cfg.GNodeB.PlmnList.GnbId] = gnb.InitGnb(cfg, wg)
wg.Add(1)
// TODO: We could find the interfaces where N2/N3 are
// and check that the generated IPs, still belong to the interfaces' subnet
n2Ip, err = IncrementIP(n2Ip, "0.0.0.0/0")
if err != nil {
log.Fatal("[GNB][CONFIG] Error while allocating ip for N2: " + err.Error())
}
n3Ip, err = IncrementIP(n3Ip, "0.0.0.0/0")
if err != nil {
log.Fatal("[GNB][CONFIG] Error while allocating ip for N3: " + err.Error())
}
}
return gnbs
}
func IncrementIP(origIP, cidr string) (string, error) {
ip := net.ParseIP(origIP)
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return origIP, err
}
for i := len(ip) - 1; i >= 0; i-- {
ip[i]++
if ip[i] != 0 {
break
}
}
if !ipNet.Contains(ip) {
return origIP, errors.New("Ip is not in provided subnet")
}
return ip.String(), nil
}
func gnbIdGenerator(i int) string {
var base string
switch true {
case i < 10:
base = "00000"
case i < 100:
base = "0000"
case i >= 100:
base = "000"
}
gnbId := base + strconv.Itoa(i)
return gnbId
}
func Contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
type UESimulationConfig struct {
UeId int
Gnbs map[string]*gnbCxt.GNBContext
Cfg config.Config
ScenarioChan chan procedures.UeTesterMessage
TimeBeforeDeregistration int
TimeBeforeHandover int
NumPduSessions int
}
func SimulateSingleUE(simConfig UESimulationConfig, wg *sync.WaitGroup) {
numGnb := len(simConfig.Gnbs)
ueCfg := simConfig.Cfg
ueCfg.Ue.Msin = IncrementMsin(simConfig.UeId, simConfig.Cfg.Ue.Msin)
log.Info("[TESTER] TESTING REGISTRATION USING IMSI ", ueCfg.Ue.Msin, " UE")
ueCfg.GNodeB.PlmnList.GnbId = gnbIdGenerator(simConfig.UeId%numGnb + 1)
// Launch a coroutine to handle UE's individual scenario
go func(scenarioChan chan procedures.UeTesterMessage, ueId int) {
wg.Add(1)
ueRx := make(chan procedures.UeTesterMessage)
// Create a new UE coroutine
// ue.NewUE returns context of the new UE
ueTx := ue.NewUE(ueCfg, uint8(ueId), ueRx, simConfig.Gnbs[ueCfg.GNodeB.PlmnList.GnbId], wg)
// We tell the UE to perform a registration
ueRx <- procedures.UeTesterMessage{Type: procedures.Registration}
var deregistrationChannel <-chan time.Time = nil
if simConfig.TimeBeforeDeregistration != 0 {
deregistrationChannel = time.After(time.Duration(simConfig.TimeBeforeDeregistration) * time.Millisecond)
}
var handoverChannel <-chan time.Time = nil
if simConfig.TimeBeforeHandover != 0 {
handoverChannel = time.After(time.Duration(simConfig.TimeBeforeHandover) * time.Millisecond)
}
loop := true
state := ueCtx.MM5G_NULL
for loop {
select {
case <-deregistrationChannel:
if ueRx != nil {
ueRx <- procedures.UeTesterMessage{Type: procedures.Terminate}
ueRx = nil
}
case <-handoverChannel:
if ueRx != nil {
ueRx <- procedures.UeTesterMessage{Type: procedures.Handover, GnbChan: simConfig.Gnbs[gnbIdGenerator((ueId+1)%numGnb+1)].GetInboundChannel()}
}
case msg := <-scenarioChan:
if ueRx != nil {
ueRx <- msg
if msg.Type == procedures.Terminate || msg.Type == procedures.Kill {
ueRx = nil
}
}
case msg := <-ueTx:
log.Info("[UE] Switched from state ", state, " to state ", msg.StateChange)
switch msg.StateChange {
case ueCtx.MM5G_REGISTERED:
if state != msg.StateChange {
for i := 0; i < simConfig.NumPduSessions; i++ {
ueRx <- procedures.UeTesterMessage{Type: procedures.NewPDUSession}
}
}
case ueCtx.MM5G_NULL:
loop = false
}
state = msg.StateChange
}
}
}(simConfig.ScenarioChan, simConfig.UeId)
}
func IncrementMsin(i int, msin string) string {
msin_int, err := strconv.Atoi(msin)
if err != nil {
log.Fatal("[UE][CONFIG] Given MSIN is invalid")
}
base := msin_int + (i - 1)
imsi := fmt.Sprintf("%010d", base)
return imsi
}