blob: ca4d1d61600c0f86dda44f2b658cf0d9da5c471a [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)) {
Max39e6cae2018-11-06 19:00:50 +010086 setverdict(fail, "Could not connect NS UDP socket from " &
87 config.local_ip & ":" & int2str(config.local_udp_port) &
88 " to " & config.remote_ip & ":" & int2str(config.remote_udp_port));
Daniel Willmanne4ff5372018-07-05 17:35:03 +020089 mtc.stop;
Harald Welte9220f632018-05-23 20:27:02 +020090 }
Harald Welte6e594f22017-07-23 16:19:35 +020091 g_conn_id := res.connId;
92 f_change_state(NSE_S_DEAD_BLOCKED);
93 /* Send the first NS-ALIVE to test the connection */
94 f_sendReset();
Harald Welte6fff3642017-07-22 21:36:13 +020095 }
96
97 type component NS_CT {
98 /* UDP port towards the bottom (IUT) */
99 port NS_CODEC_PT NSCP;
100 /* NS-User SAP towards the user */
101 port NS_SP_PT NS_SP;
102
Alexander Couzens2c12b242018-07-31 00:30:11 +0200103 var NSConfiguration config;
104
Harald Welte6e594f22017-07-23 16:19:35 +0200105 var NseState g_state := NSE_S_DEAD_BLOCKED;
106 var ConnectionId g_conn_id := -1;
107
108 timer Tns_alive := 3.0;
109 timer Tns_test := 10.0;
110 timer Tns_block := 10.0;
Harald Welte6fff3642017-07-22 21:36:13 +0200111 }
112
Alexander Couzens2c12b242018-07-31 00:30:11 +0200113 type record NSConfiguration {
114 PortNumber local_udp_port,
115 charstring local_ip,
116 PortNumber remote_udp_port,
117 charstring remote_ip,
118 Nsvci nsvci,
119 Nsvci nsei
120 }
Harald Welte6fff3642017-07-22 21:36:13 +0200121
Harald Welte6e594f22017-07-23 16:19:35 +0200122 private function f_change_state(NseState new_state) runs on NS_CT {
123 var NseState old_state := g_state;
124 g_state := new_state;
125 log("NS State Transition: ", old_state, " -> ", new_state);
Alexander Couzens2c12b242018-07-31 00:30:11 +0200126 NS_SP.send(t_NsStsInd(config.nsei, config.nsvci, old_state, new_state));
Harald Welte6fff3642017-07-22 21:36:13 +0200127 }
128
Harald Welte6e594f22017-07-23 16:19:35 +0200129 private function f_sendReset() runs on NS_CT {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200130 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 +0200131 g_state := NSE_S_WAIT_RESET;
Harald Welte6fff3642017-07-22 21:36:13 +0200132 }
133
Harald Welte6e594f22017-07-23 16:19:35 +0200134 private function f_sendAlive() runs on NS_CT {
135 NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE));
136 Tns_alive.start;
137 }
138
139 private function f_sendUnblock() runs on NS_CT {
140 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK));
141 Tns_block.start;
142 }
143
144 private function f_sendBlock(NsCause cause) runs on NS_CT {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200145 NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK(cause, config.nsvci)));
Harald Welte6e594f22017-07-23 16:19:35 +0200146 Tns_block.start;
147 }
148
149 altstep as_allstate() runs on NS_CT {
150 var NS_RecvFrom rf;
151 var ASP_Event evt;
152
153 /* transition to DEAD if t_alive times out */
Harald Weltec27f6842018-03-02 21:40:03 +0100154 [Tns_alive.running] Tns_alive.timeout {
Harald Welte6e594f22017-07-23 16:19:35 +0200155 log("Tns-alive expired: changing to DEAD_BLOCKED + starting Tns-test");
156 f_change_state(NSE_S_DEAD_BLOCKED);
157 Tns_test.start;
Harald Welte6fff3642017-07-22 21:36:13 +0200158 }
Harald Welte6fff3642017-07-22 21:36:13 +0200159
Harald Weltec27f6842018-03-02 21:40:03 +0100160 [Tns_test.running] Tns_test.timeout {
Harald Welte6e594f22017-07-23 16:19:35 +0200161 log("Tns-test expired: sending NS-ALIVE");
162 f_sendAlive();
Harald Welte6fff3642017-07-22 21:36:13 +0200163 }
Harald Welte6fff3642017-07-22 21:36:13 +0200164
Harald Welte6e594f22017-07-23 16:19:35 +0200165 /* Stop t_alive when receiving ALIVE-ACK */
166 [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE_ACK)) {
167 log("NS-ALIVE-ACK received: stopping Tns-alive; starting Tns-test");
168 Tns_alive.stop;
169 Tns_test.start;
170 }
Harald Welte6fff3642017-07-22 21:36:13 +0200171
Harald Welte6e594f22017-07-23 16:19:35 +0200172 /* respond to NS-ALIVE with NS-ALIVE-ACK */
173 [] NSCP.receive(t_NS_RecvFrom(t_NS_ALIVE)) {
174 NSCP.send(t_NS_Send(g_conn_id, t_NS_ALIVE_ACK));
175 }
176
177 /* Respond to BLOCK for wrong NSVCI */
178 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, ?))) -> value rf {
179 log("Rx NS-BLOCK for unknown NSVCI");
180 /* FIXME */
181 }
182
183 /* Respond to RESET with correct NSEI/NSVCI */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200184 [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, config.nsvci, config.nsei))) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200185 f_change_state(NSE_S_ALIVE_BLOCKED);
Alexander Couzens2c12b242018-07-31 00:30:11 +0200186 NSCP.send(t_NS_Send(g_conn_id, t_NS_RESET_ACK(config.nsvci, config.nsei)));
Harald Welte6e594f22017-07-23 16:19:35 +0200187 }
188
189 /* Respond to RESET with wrong NSEI/NSVCI */
190 [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET(?, ?, ?))) -> value rf {
191 log("Rx NS-RESET for unknown NSEI/NSVCI");
192 /* FIXME */
193 }
194
195 /* default case of handling unknown PDUs */
196 [] NSCP.receive(t_NS_RecvFrom(?)) -> value rf {
197 log("Rx Unexpected NS PDU ", rf.msg," in state ", g_state);
Harald Weltee0abc472018-02-05 09:13:31 +0100198 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 +0200199 }
200 /* Forwarding of ASP_Evet to user */
201 [] NSCP.receive(ASP_Event:?) -> value evt { NS_SP.send(evt); }
Harald Welte6fff3642017-07-22 21:36:13 +0200202 }
203
204 private function f_ScanEvents() runs on NS_CT {
205 var NsUnitdataRequest ud_req;
206 var NS_RecvFrom rf;
Harald Welte6e594f22017-07-23 16:19:35 +0200207 var default d;
208
209 d := activate(as_allstate());
Harald Welte6fff3642017-07-22 21:36:13 +0200210
211 while (true) {
Harald Welte6e594f22017-07-23 16:19:35 +0200212 if (g_state == NSE_S_DEAD_BLOCKED) {
Harald Welte6fff3642017-07-22 21:36:13 +0200213 alt {
Harald Welte6e594f22017-07-23 16:19:35 +0200214 [false] any timer.timeout {}
Harald Welte6fff3642017-07-22 21:36:13 +0200215 }
Harald Welte6e594f22017-07-23 16:19:35 +0200216 } else if (g_state == NSE_S_WAIT_RESET) {
Harald Welte6fff3642017-07-22 21:36:13 +0200217 alt {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200218 [] NSCP.receive(t_NS_RecvFrom(t_NS_RESET_ACK(config.nsvci, config.nsei))) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200219 f_change_state(NSE_S_ALIVE_BLOCKED);
220 f_sendAlive();
221 f_sendUnblock();
Harald Welte6fff3642017-07-22 21:36:13 +0200222 }
Harald Welte6fff3642017-07-22 21:36:13 +0200223 }
Harald Welte6e594f22017-07-23 16:19:35 +0200224 } else if (g_state == NSE_S_ALIVE_BLOCKED) {
Harald Welte6fff3642017-07-22 21:36:13 +0200225 alt {
Harald Welte6e594f22017-07-23 16:19:35 +0200226 /* bogus block, just respond with ACK */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200227 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, config.nsvci))) -> value rf {
228 NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(config.nsvci)));
Harald Welte6e594f22017-07-23 16:19:35 +0200229 }
230 /* Respond to UNBLOCK with UNBLOCK-ACK + change state */
231 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf {
232 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK));
233 Tns_block.stop;
234 f_change_state(NSE_S_ALIVE_UNBLOCKED);
235 }
236 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK_ACK)) -> value rf {
237 Tns_block.stop;
238 f_change_state(NSE_S_ALIVE_UNBLOCKED);
239 }
240 [] Tns_block.timeout {
241 /* repeat unblock transmission */
242 f_sendUnblock();
243 }
244 }
245 } else if (g_state == NSE_S_ALIVE_UNBLOCKED) {
246 alt {
247 /* bogus unblock, just respond with ACK */
248 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNBLOCK)) -> value rf {
249 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNBLOCK_ACK));
250 }
251 /* Respond to BLOCK with BLOCK-ACK + change state */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200252 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK(?, config.nsvci))) -> value rf {
253 NSCP.send(t_NS_Send(g_conn_id, t_NS_BLOCK_ACK(config.nsvci)));
Harald Welte6e594f22017-07-23 16:19:35 +0200254 Tns_block.stop;
255 f_change_state(NSE_S_ALIVE_BLOCKED);
256 }
Alexander Couzens2c12b242018-07-31 00:30:11 +0200257 [] NSCP.receive(t_NS_RecvFrom(t_NS_BLOCK_ACK(config.nsvci))) -> value rf {
Harald Welte6e594f22017-07-23 16:19:35 +0200258 Tns_block.stop;
259 }
260 /* NS-UNITDATA PDU from network to NS-UNITDATA.ind to user */
261 [] NSCP.receive(t_NS_RecvFrom(t_NS_UNITDATA(?, ?, ?))) -> value rf {
Alexander Couzens2c12b242018-07-31 00:30:11 +0200262 NS_SP.send(t_NsUdInd(config.nsei,
Harald Weltee0abc472018-02-05 09:13:31 +0100263 oct2int(rf.msg.pDU_NS_Unitdata.bVCI),
264 rf.msg.pDU_NS_Unitdata.nS_SDU));
Harald Welte6e594f22017-07-23 16:19:35 +0200265 }
266 /* NS-UNITDATA.req from user to NS-UNITDATA PDU on network */
Alexander Couzens2c12b242018-07-31 00:30:11 +0200267 [] NS_SP.receive(t_NsUdReq(config.nsei, ?, ?, omit)) -> value ud_req {
Harald Welte6e594f22017-07-23 16:19:35 +0200268 /* using raw octetstring PDU */
269 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, ud_req.sdu)));
270 }
Alexander Couzens2c12b242018-07-31 00:30:11 +0200271 [] NS_SP.receive(t_NsUdReq(config.nsei, ?, omit, ?)) -> value ud_req {
Harald Welte6e594f22017-07-23 16:19:35 +0200272 /* using decoded BSSGP PDU that we need to encode first */
Harald Weltee0abc472018-02-05 09:13:31 +0100273 var octetstring enc := enc_PDU_BSSGP(ud_req.bssgp);
Harald Welte6e594f22017-07-23 16:19:35 +0200274 NSCP.send(t_NS_Send(g_conn_id, t_NS_UNITDATA(t_SduCtrlB, ud_req.bvci, enc)));
275 }
Harald Welte6fff3642017-07-22 21:36:13 +0200276 }
277 }
278
Harald Welte6e594f22017-07-23 16:19:35 +0200279 } /* while */
280 //deactivate(d);
Harald Welte6fff3642017-07-22 21:36:13 +0200281 }
282}