blob: 7cccb2549d2a613e371f6fc4f43feb58bc396ea6 [file] [log] [blame]
Harald Welte6fff3642017-07-22 21:36:13 +02001module NS_Emulation {
2 import from NS_Types all;
3 import from BSSGP_Types all;
Harald Weltee0abc472018-02-05 09:13:31 +01004 import from Osmocom_Gb_Types all;
Harald Welte6fff3642017-07-22 21:36:13 +02005 import from NS_CodecPort all;
6 import from NS_CodecPort_CtrlFunct all;
Harald Welte6fff3642017-07-22 21:36:13 +02007 import from IPL4asp_Types all;
8
9 type record NsUnitdataRequest {
10 BssgpBvci bvci,
11 Nsei nsei,
Harald Welte6e594f22017-07-23 16:19:35 +020012 octetstring sdu optional,
Harald Weltee0abc472018-02-05 09:13:31 +010013 PDU_BSSGP bssgp optional
Harald Welte6e594f22017-07-23 16:19:35 +020014 }
15
16 template NsUnitdataRequest t_NsUdReq(template Nsei nsei, template BssgpBvci bvci, template octetstring sdu,
Harald Weltee0abc472018-02-05 09:13:31 +010017 template PDU_BSSGP bssgp) := {
Harald Welte6e594f22017-07-23 16:19:35 +020018 bvci := bvci,
19 nsei := nsei,
20 sdu := sdu,
21 bssgp := bssgp
Harald Welte6fff3642017-07-22 21:36:13 +020022 }
23
24 type record NsUnitdataIndication {
25 BssgpBvci bvci,
26 Nsei nsei,
Harald Welte6e594f22017-07-23 16:19:35 +020027 octetstring sdu optional,
Harald Weltee0abc472018-02-05 09:13:31 +010028 PDU_BSSGP bssgp optional
Harald Welte6e594f22017-07-23 16:19:35 +020029 }
30
31 template NsUnitdataIndication t_NsUdInd(Nsei nsei, BssgpBvci bvci, octetstring sdu) := {
32 bvci := bvci,
33 nsei := nsei,
34 sdu := sdu,
Harald Weltee0abc472018-02-05 09:13:31 +010035 bssgp := dec_PDU_BSSGP(sdu)
Harald Welte6e594f22017-07-23 16:19:35 +020036 }
37
38 type record NsStatusIndication {
39 Nsei nsei,
40 Nsvci nsvci,
41 NseState old_state,
42 NseState new_state
43 }
44
45 template NsStatusIndication t_NsStsInd(Nsei nsei, Nsvci nsvci, NseState old_state, NseState state) := {
46 nsei := nsei,
47 nsvci := nsvci,
48 old_state := old_state,
49 new_state := state
Harald Welte6fff3642017-07-22 21:36:13 +020050 }
51
52 type enumerated NseState {
Harald Welte6e594f22017-07-23 16:19:35 +020053 NSE_S_DEAD_BLOCKED,
54 NSE_S_WAIT_RESET,
55 NSE_S_ALIVE_BLOCKED,
56 NSE_S_ALIVE_UNBLOCKED
57 }
Harald Welte6fff3642017-07-22 21:36:13 +020058
59 /* port from our (internal) point of view */
60 type port NS_SP_PT message {
61 in NsUnitdataRequest;
62 out NsUnitdataIndication,
Harald Welte6e594f22017-07-23 16:19:35 +020063 NsStatusIndication,
Harald Welte6fff3642017-07-22 21:36:13 +020064 ASP_Event;
65 } with { extension "internal" };
66
67 /* port from the user point of view */
68 type port NS_PT message {
69 in ASP_Event,
Harald Welte6e594f22017-07-23 16:19:35 +020070 NsStatusIndication,
Harald Welte6fff3642017-07-22 21:36:13 +020071 NsUnitdataIndication;
72 out NsUnitdataRequest;
73 } with { extension "internal" };
74
Alexander Couzens2c12b242018-07-31 00:30:11 +020075 function NSStart(NSConfiguration init_config) runs on NS_CT {
76 config := init_config;
Harald Welte6fff3642017-07-22 21:36:13 +020077 f_init();
78 f_ScanEvents();
79 }
80
81 private function f_init() runs on NS_CT {
Harald Welte6e594f22017-07-23 16:19:35 +020082 var Result res;
83 /* Connect the UDP socket */
Alexander Couzens2c12b242018-07-31 00:30:11 +020084 res := f_IPL4_connect(NSCP, config.remote_ip, config.remote_udp_port, config.local_ip, config.local_udp_port, 0, { udp := {}});
Harald Welte9220f632018-05-23 20:27:02 +020085 if (not ispresent(res.connId)) {
86 setverdict(fail, "Could not connect NS UDP socket, check your configuration");
Daniel Willmanne4ff5372018-07-05 17:35:03 +020087 mtc.stop;
Harald Welte9220f632018-05-23 20:27:02 +020088 }
Harald Welte6e594f22017-07-23 16:19:35 +020089 g_conn_id := res.connId;
90 f_change_state(NSE_S_DEAD_BLOCKED);
91 /* Send the first NS-ALIVE to test the connection */
92 f_sendReset();
Harald Welte6fff3642017-07-22 21:36:13 +020093 }
94
95 type component NS_CT {
96 /* UDP port towards the bottom (IUT) */
97 port NS_CODEC_PT NSCP;
98 /* NS-User SAP towards the user */
99 port NS_SP_PT NS_SP;
100
Alexander Couzens2c12b242018-07-31 00:30:11 +0200101 var NSConfiguration config;
102
Harald Welte6e594f22017-07-23 16:19:35 +0200103 var NseState g_state := NSE_S_DEAD_BLOCKED;
104 var ConnectionId g_conn_id := -1;
105
106 timer Tns_alive := 3.0;
107 timer Tns_test := 10.0;
108 timer Tns_block := 10.0;
Harald Welte6fff3642017-07-22 21:36:13 +0200109 }
110
Alexander Couzens2c12b242018-07-31 00:30:11 +0200111 type record NSConfiguration {
112 PortNumber local_udp_port,
113 charstring local_ip,
114 PortNumber remote_udp_port,
115 charstring remote_ip,
116 Nsvci nsvci,
117 Nsvci nsei
118 }
Harald Welte6fff3642017-07-22 21:36:13 +0200119
Harald Welte6e594f22017-07-23 16:19:35 +0200120 private function f_change_state(NseState new_state) runs on NS_CT {
121 var NseState old_state := g_state;
122 g_state := new_state;
123 log("NS State Transition: ", old_state, " -> ", new_state);
Alexander Couzens2c12b242018-07-31 00:30:11 +0200124 NS_SP.send(t_NsStsInd(config.nsei, config.nsvci, old_state, new_state));
Harald Welte6fff3642017-07-22 21:36:13 +0200125 }
126
Harald Welte6e594f22017-07-23 16:19:35 +0200127 private function f_sendReset() runs on NS_CT {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200128 NSCP.send(t_NS_Send(g_conn_id, t_NS_RESET(NS_CAUSE_OM_INTERVENTION, config.nsvci, config.nsei)));
Harald Welte6e594f22017-07-23 16:19:35 +0200129 g_state := NSE_S_WAIT_RESET;
Harald Welte6fff3642017-07-22 21:36:13 +0200130 }
131
Harald Welte6e594f22017-07-23 16:19:35 +0200132 private function f_sendAlive() runs on NS_CT {
133 NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE));
134 Tns_alive.start;
135 }
136
137 private function f_sendUnblock() runs on NS_CT {
138 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK));
139 Tns_block.start;
140 }
141
142 private function f_sendBlock(NsCause cause) runs on NS_CT {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200143 NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK(cause, config.nsvci)));
Harald Welte6e594f22017-07-23 16:19:35 +0200144 Tns_block.start;
145 }
146
147 altstep as_allstate() runs on NS_CT {
148 var NS_RecvFrom rf;
149 var ASP_Event evt;
150
151 /* transition to DEAD if t_alive times out */
Harald Weltec27f6842018-03-02 21:40:03 +0100152 [Tns_alive.running] Tns_alive.timeout {
Harald Welte6e594f22017-07-23 16:19:35 +0200153 log("Tns-alive expired: changing to DEAD_BLOCKED + starting Tns-test");
154 f_change_state(NSE_S_DEAD_BLOCKED);
155 Tns_test.start;
Harald Welte6fff3642017-07-22 21:36:13 +0200156 }
Harald Welte6fff3642017-07-22 21:36:13 +0200157
Harald Weltec27f6842018-03-02 21:40:03 +0100158 [Tns_test.running] Tns_test.timeout {
Harald Welte6e594f22017-07-23 16:19:35 +0200159 log("Tns-test expired: sending NS-ALIVE");
160 f_sendAlive();
Harald Welte6fff3642017-07-22 21:36:13 +0200161 }
Harald Welte6fff3642017-07-22 21:36:13 +0200162
Harald Welte6e594f22017-07-23 16:19:35 +0200163 /* Stop t_alive when receiving ALIVE-ACK */
164 [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE_ACK)) {
165 log("NS-ALIVE-ACK received: stopping Tns-alive; starting Tns-test");
166 Tns_alive.stop;
167 Tns_test.start;
168 }
Harald Welte6fff3642017-07-22 21:36:13 +0200169
Harald Welte6e594f22017-07-23 16:19:35 +0200170 /* respond to NS-ALIVE with NS-ALIVE-ACK */
171 [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE)) {
172 NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE_ACK));
173 }
174
175 /* Respond to BLOCK for wrong NSVCI */
176 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, ?))) -> value rf {
177 log("Rx NS-BLOCK for unknown NSVCI");
178 /* FIXME */
179 }
180
181 /* Respond to RESET with correct NSEI/NSVCI */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200182 [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, config.nsvci, config.nsei))) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200183 f_change_state(NSE_S_ALIVE_BLOCKED);
Alexander Couzens2c12b242018-07-31 00:30:11 +0200184 NSCP.send(t_NS_Send(g_conn_id, t_NS_RESET_ACK(config.nsvci, config.nsei)));
Harald Welte6e594f22017-07-23 16:19:35 +0200185 }
186
187 /* Respond to RESET with wrong NSEI/NSVCI */
188 [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, ?, ?))) -> value rf {
189 log("Rx NS-RESET for unknown NSEI/NSVCI");
190 /* FIXME */
191 }
192
193 /* default case of handling unknown PDUs */
194 [] NSCP.receive(t_NS_RecvFrom(?)) -> value rf {
195 log("Rx Unexpected NS PDU ", rf.msg," in state ", g_state);
Harald Weltee0abc472018-02-05 09:13:31 +0100196 NSCP.send(t_NS_Send(g_conn_id, ts_NS_STATUS(NS_CAUSE_PDU_NOT_COMPATIBLE_WITH_PROTOCOL_STATE, rf.msg)));
Harald Welte6e594f22017-07-23 16:19:35 +0200197 }
198 /* Forwarding of ASP_Evet to user */
199 [] NSCP.receive(ASP_Event:?) -> value evt { NS_SP.send(evt); }
Harald Welte6fff3642017-07-22 21:36:13 +0200200 }
201
202 private function f_ScanEvents() runs on NS_CT {
203 var NsUnitdataRequest ud_req;
204 var NS_RecvFrom rf;
Harald Welte6e594f22017-07-23 16:19:35 +0200205 var default d;
206
207 d := activate(as_allstate());
Harald Welte6fff3642017-07-22 21:36:13 +0200208
209 while (true) {
Harald Welte6e594f22017-07-23 16:19:35 +0200210 if (g_state == NSE_S_DEAD_BLOCKED) {
Harald Welte6fff3642017-07-22 21:36:13 +0200211 alt {
Harald Welte6e594f22017-07-23 16:19:35 +0200212 [false] any timer.timeout {}
Harald Welte6fff3642017-07-22 21:36:13 +0200213 }
Harald Welte6e594f22017-07-23 16:19:35 +0200214 } else if (g_state == NSE_S_WAIT_RESET) {
Harald Welte6fff3642017-07-22 21:36:13 +0200215 alt {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200216 [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET_ACK(config.nsvci, config.nsei))) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200217 f_change_state(NSE_S_ALIVE_BLOCKED);
218 f_sendAlive();
219 f_sendUnblock();
Harald Welte6fff3642017-07-22 21:36:13 +0200220 }
Harald Welte6fff3642017-07-22 21:36:13 +0200221 }
Harald Welte6e594f22017-07-23 16:19:35 +0200222 } else if (g_state == NSE_S_ALIVE_BLOCKED) {
Harald Welte6fff3642017-07-22 21:36:13 +0200223 alt {
Harald Welte6e594f22017-07-23 16:19:35 +0200224 /* bogus block, just respond with ACK */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200225 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, config.nsvci))) -> value rf {
226 NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(config.nsvci)));
Harald Welte6e594f22017-07-23 16:19:35 +0200227 }
228 /* Respond to UNBLOCK with UNBLOCK-ACK + change state */
229 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf {
230 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK));
231 Tns_block.stop;
232 f_change_state(NSE_S_ALIVE_UNBLOCKED);
233 }
234 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK_ACK)) -> value rf {
235 Tns_block.stop;
236 f_change_state(NSE_S_ALIVE_UNBLOCKED);
237 }
238 [] Tns_block.timeout {
239 /* repeat unblock transmission */
240 f_sendUnblock();
241 }
242 }
243 } else if (g_state == NSE_S_ALIVE_UNBLOCKED) {
244 alt {
245 /* bogus unblock, just respond with ACK */
246 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf {
247 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK));
248 }
249 /* Respond to BLOCK with BLOCK-ACK + change state */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200250 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, config.nsvci))) -> value rf {
251 NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(config.nsvci)));
Harald Welte6e594f22017-07-23 16:19:35 +0200252 Tns_block.stop;
253 f_change_state(NSE_S_ALIVE_BLOCKED);
254 }
Alexander Couzens2c12b242018-07-31 00:30:11 +0200255 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK_ACK(config.nsvci))) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200256 Tns_block.stop;
257 }
258 /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */
259 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNITDATA(?, ?, ?))) -> value rf {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200260 NS_SP.send(t_NsUdInd(config.nsei,
Harald Weltee0abc472018-02-05 09:13:31 +0100261 oct2int(rf.msg.pDU_NS_Unitdata.bVCI),
262 rf.msg.pDU_NS_Unitdata.nS_SDU));
Harald Welte6e594f22017-07-23 16:19:35 +0200263 }
264 /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200265 [] NS_SP.receive(t_NsUdReq(config.nsei, ?, ?, omit)) -> value ud_req {
Harald Welte6e594f22017-07-23 16:19:35 +0200266 /* using raw octetstring PDU */
267 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu)));
268 }
Alexander Couzens2c12b242018-07-31 00:30:11 +0200269 [] NS_SP.receive(t_NsUdReq(config.nsei, ?, omit, ?)) -> value ud_req {
Harald Welte6e594f22017-07-23 16:19:35 +0200270 /* using decoded BSSGP PDU that we need to encode first */
Harald Weltee0abc472018-02-05 09:13:31 +0100271 var octetstring enc := enc_PDU_BSSGP(ud_req.bssgp);
Harald Welte6e594f22017-07-23 16:19:35 +0200272 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc)));
273 }
Harald Welte6fff3642017-07-22 21:36:13 +0200274 }
275 }
276
Harald Welte6e594f22017-07-23 16:19:35 +0200277 } /* while */
278 //deactivate(d);
Harald Welte6fff3642017-07-22 21:36:13 +0200279 }
280}