Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 1 | module PGW_Tests { |
| 2 | |
| 3 | import from General_Types all; |
| 4 | import from Osmocom_Types all; |
| 5 | import from Native_Functions all; |
| 6 | |
| 7 | import from GTPv2_Types all; |
| 8 | import from GTPv2_Templates all; |
| 9 | import from GTPv2_Emulation all; |
| 10 | |
| 11 | import from UECUPS_Types all; |
| 12 | |
| 13 | import from DNS_Helpers all; |
| 14 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 15 | |
| 16 | import from DIAMETER_Types all; |
| 17 | import from DIAMETER_Templates all; |
| 18 | import from DIAMETER_Emulation all; |
| 19 | |
| 20 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 21 | modulepar { |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 22 | charstring mp_pgw_hostname := "127.0.0.4"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 23 | charstring mp_local_hostname_c := "127.0.0.1"; |
| 24 | charstring mp_local_hostname_u := "127.0.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 25 | |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 26 | charstring mp_run_prog_log_path := "/tmp"; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 27 | charstring mp_run_prog_as_user := "laforge"; |
| 28 | charstring mp_ping_hostname := "10.45.0.1"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 29 | |
Vadim Yanitskiy | 589972f | 2022-01-20 19:38:16 +0600 | [diff] [blame] | 30 | charstring mp_pcrf_local_ip := "127.0.0.9"; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 31 | integer mp_pcrf_local_port := 3868; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* main component, we typically have one per testcase */ |
| 35 | type component PGW_Test_CT { |
| 36 | var GTPv2_Emulation_CT vc_GTP2; |
| 37 | port GTP2EM_PT TEID0; |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 38 | |
| 39 | /* emulated PCRF */ |
| 40 | var DIAMETER_Emulation_CT vc_DIAMETER; |
| 41 | port DIAMETER_PT DIAMETER_UNIT; |
| 42 | port DIAMETEREM_PROC_PT DIAMETER_PROC; |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 43 | /* global test case guard timer (actual timeout value is set in f_init()) */ |
| 44 | timer T_guard; |
| 45 | } |
| 46 | |
| 47 | /* global altstep for global guard timer; */ |
| 48 | altstep as_Tguard() runs on PGW_Test_CT { |
| 49 | [] T_guard.timeout { |
| 50 | setverdict(fail, "Timeout of T_guard"); |
| 51 | mtc.stop; |
| 52 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* per-session component; we typically have 1..N per testcase */ |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 56 | type component PGW_Session_CT extends GTP2_ConnHdlr, DIAMETER_ConnHdlr { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 57 | var SessionPars g_pars; |
| 58 | |
| 59 | /* TEI (Data) local side */ |
| 60 | var OCT4 g_teid; |
| 61 | /* TEI (Control) local side */ |
| 62 | var OCT4 g_teic; |
| 63 | /* TEI (Data) remote side */ |
| 64 | var OCT4 g_teid_remote; |
| 65 | /* TEI (Control) remote side */ |
| 66 | var OCT4 g_teic_remote; |
| 67 | /* GTP-U IPv4 address remote sie */ |
| 68 | var OCT4 g_gtpu4_remote; |
| 69 | var OCT16 g_gtpu6_remote; |
| 70 | |
| 71 | /* Address allocation */ |
| 72 | var OCT4 g_ip4_addr; |
| 73 | var OCT16 g_ip6_addr; |
| 74 | var integer g_ip6_plen; |
| 75 | } |
| 76 | |
| 77 | /* configuration data for a given Session */ |
| 78 | type record SessionPars { |
| 79 | hexstring imsi, |
| 80 | octetstring msisdn optional, |
| 81 | // serving network |
| 82 | integer rat_type, |
| 83 | // flags? |
| 84 | charstring apn, |
| 85 | /* Apn subscribed or non-subscribed */ |
| 86 | boolean selection_mode, |
| 87 | BIT3 pdn_type, |
| 88 | /* PAA */ |
| 89 | /* Max APN Restriction */ |
| 90 | /* APN-AMBR */ |
| 91 | octetstring pco optional, |
| 92 | octetstring epco optional, |
| 93 | /* Bearer Contexts to be created */ |
| 94 | |
| 95 | charstring tun_dev_name, |
| 96 | charstring tun_netns_name optional |
| 97 | } |
| 98 | |
| 99 | template (value) SessionPars |
| 100 | t_SessionPars(hexstring imsi, charstring tundev, integer rat_type := 6, charstring apn := "internet", |
| 101 | boolean selection_mode := false, BIT3 pdn_type := '001'B) := { |
| 102 | imsi := imsi, |
| 103 | msisdn := omit, |
| 104 | rat_type := rat_type, |
| 105 | apn := apn, |
| 106 | selection_mode := selection_mode, |
| 107 | pdn_type := pdn_type, |
| 108 | pco := omit, |
| 109 | epco := omit, |
| 110 | tun_dev_name := tundev, |
| 111 | tun_netns_name := tundev |
| 112 | } |
| 113 | |
| 114 | type record BearerConfig { |
| 115 | integer eps_bearer_id |
| 116 | } |
| 117 | |
| 118 | type function void_fn() runs on PGW_Session_CT; |
| 119 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 120 | friend function DiameterForwardUnitdataCallback(PDU_DIAMETER msg) |
| 121 | runs on DIAMETER_Emulation_CT return template PDU_DIAMETER { |
| 122 | DIAMETER_UNIT.send(msg); |
| 123 | return omit; |
| 124 | } |
| 125 | |
| 126 | friend function f_init_diameter(charstring id) runs on PGW_Test_CT { |
| 127 | var DIAMETEROps ops := { |
| 128 | create_cb := refers(DIAMETER_Emulation.ExpectedCreateCallback), |
Vadim Yanitskiy | b46f01e | 2021-12-06 03:23:13 +0300 | [diff] [blame] | 129 | unitdata_cb := refers(DiameterForwardUnitdataCallback), |
| 130 | raw := false /* handler mode (IMSI based routing) */ |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 131 | }; |
| 132 | var DIAMETER_conn_parameters pars := { |
| 133 | remote_ip := mp_pgw_hostname, |
| 134 | remote_sctp_port := -1, |
| 135 | local_ip := mp_pcrf_local_ip, |
| 136 | local_sctp_port := mp_pcrf_local_port, |
| 137 | origin_host := "pcrf.localdomain", |
| 138 | origin_realm := "localdomain", |
Pau Espin Pedrol | 33b4749 | 2022-03-08 17:43:01 +0100 | [diff] [blame] | 139 | auth_app_id := omit, |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 140 | vendor_app_id := c_DIAMETER_3GPP_Gx_AID |
| 141 | }; |
| 142 | vc_DIAMETER := DIAMETER_Emulation_CT.create(id); |
| 143 | map(vc_DIAMETER:DIAMETER, system:DIAMETER_CODEC_PT); |
| 144 | connect(vc_DIAMETER:DIAMETER_UNIT, self:DIAMETER_UNIT); |
| 145 | connect(vc_DIAMETER:DIAMETER_PROC, self:DIAMETER_PROC); |
| 146 | vc_DIAMETER.start(DIAMETER_Emulation.main(ops, pars, id)); |
| 147 | |
| 148 | f_diameter_wait_capability(DIAMETER_UNIT); |
Pau Espin Pedrol | bb5f45f | 2022-04-11 12:36:55 +0200 | [diff] [blame] | 149 | /* Give some time for our emulation to get out of SUSPECT list of SUT (3 watchdong ping-pongs): |
| 150 | * RFC6733 sec 5.1 |
| 151 | * RFC3539 sec 3.4.1 [5] |
| 152 | * https://github.com/freeDiameter/freeDiameter/blob/master/libfdcore/p_psm.c#L49 |
| 153 | */ |
| 154 | f_sleep(1.0); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 155 | } |
| 156 | |
Pau Espin Pedrol | 08880f1 | 2022-04-07 19:05:15 +0200 | [diff] [blame] | 157 | private function f_init(float guard_timeout := 60.0) runs on PGW_Test_CT { |
| 158 | T_guard.start(guard_timeout); |
| 159 | activate(as_Tguard()); |
| 160 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 161 | var Gtp2EmulationCfg cfg := { |
| 162 | gtpc_bind_ip := mp_local_hostname_c, |
| 163 | gtpc_bind_port := GTP2C_PORT, |
| 164 | gtpc_remote_ip := mp_pgw_hostname, |
| 165 | gtpc_remote_port := GTP2C_PORT, |
| 166 | sgw_role := true, |
| 167 | use_gtpu_daemon := true |
| 168 | }; |
| 169 | |
| 170 | vc_GTP2 := GTPv2_Emulation_CT.create("GTP2_EM"); |
| 171 | map(vc_GTP2:GTP2C, system:GTP2C); |
| 172 | connect(vc_GTP2:TEID0, self:TEID0); |
| 173 | vc_GTP2.start(GTPv2_Emulation.main(cfg)); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 174 | |
| 175 | if (mp_pcrf_local_ip != "") { |
| 176 | f_init_diameter(testcasename()); |
| 177 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | function f_start_handler(void_fn fn, template (omit) SessionPars pars := omit) |
| 181 | runs on PGW_Test_CT return PGW_Session_CT { |
| 182 | var charstring id := testcasename(); |
| 183 | var PGW_Session_CT vc_conn; |
| 184 | vc_conn := PGW_Session_CT.create(id); |
| 185 | connect(vc_conn:GTP2, vc_GTP2:CLIENT); |
| 186 | connect(vc_conn:GTP2_PROC, vc_GTP2:CLIENT_PROC); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 187 | |
| 188 | if (isbound(vc_DIAMETER)) { |
| 189 | connect(vc_conn:DIAMETER, vc_DIAMETER:DIAMETER_CLIENT); |
| 190 | connect(vc_conn:DIAMETER_PROC, vc_DIAMETER:DIAMETER_PROC); |
| 191 | } |
| 192 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 193 | vc_conn.start(f_handler_init(fn, pars)); |
| 194 | return vc_conn; |
| 195 | } |
| 196 | |
| 197 | private function f_handler_init(void_fn fn, template (omit) SessionPars pars := omit) |
| 198 | runs on PGW_Session_CT { |
| 199 | if (isvalue(pars)) { |
| 200 | g_pars := valueof(pars); |
| 201 | } |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 202 | if (DIAMETER_PROC.checkstate("Connected")) { |
| 203 | f_diameter_expect(g_pars.imsi); |
| 204 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 205 | fn.apply(); |
| 206 | } |
| 207 | |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 208 | private function f_concat_pad(integer tot_len, hexstring prefix, integer suffix) return hexstring { |
| 209 | var integer suffix_len := tot_len - lengthof(prefix); |
| 210 | var charstring suffix_ch := int2str(suffix); |
| 211 | var integer pad_len := suffix_len - lengthof(suffix_ch); |
| 212 | |
| 213 | return prefix & int2hex(0, pad_len) & str2hex(suffix_ch); |
| 214 | } |
| 215 | |
| 216 | function f_gen_imei(integer suffix) return hexstring { |
| 217 | return f_concat_pad(14, '49999'H, suffix); |
| 218 | } |
| 219 | |
| 220 | function f_gen_imsi(integer suffix) return hexstring { |
| 221 | return f_concat_pad(15, '26242'H, suffix); |
| 222 | } |
| 223 | |
| 224 | function f_gen_msisdn(integer suffix) return hexstring { |
| 225 | return f_concat_pad(12, '49123'H, suffix); |
| 226 | } |
| 227 | |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 228 | private altstep as_DIA_CCR(DCC_NONE_CC_Request_Type req_type) runs on PGW_Session_CT { |
| 229 | var PDU_DIAMETER rx_dia; |
Pau Espin Pedrol | 77fdd0b | 2022-03-08 14:03:33 +0100 | [diff] [blame] | 230 | [] DIAMETER.receive(tr_DIA_Gx_CCR(req_type := req_type)) -> value rx_dia { |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 231 | var template (omit) AVP avp; |
| 232 | var octetstring sess_id; |
| 233 | var AVP_Unsigned32 req_num; |
| 234 | |
| 235 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_BASE_NONE_Session_Id); |
| 236 | sess_id := valueof(avp.avp_data.avp_BASE_NONE_Session_Id); |
| 237 | |
| 238 | avp := f_DIAMETER_get_avp(rx_dia, c_AVP_Code_DCC_NONE_CC_Request_Number); |
| 239 | req_num := valueof(avp.avp_data.avp_DCC_NONE_CC_Request_Number); |
| 240 | |
Pau Espin Pedrol | 77fdd0b | 2022-03-08 14:03:33 +0100 | [diff] [blame] | 241 | DIAMETER.send(ts_DIA_Gx_CCA(rx_dia.hop_by_hop_id, rx_dia.end_to_end_id, sess_id, |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 242 | req_type, req_num)); |
| 243 | } |
| 244 | [] DIAMETER.receive(PDU_DIAMETER:?) -> value rx_dia { |
| 245 | setverdict(fail, "Received unexpected DIAMETER ", rx_dia); |
| 246 | self.stop; |
| 247 | } |
| 248 | } |
| 249 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 250 | |
| 251 | /* find TEID of given interface type (and optionally instance) */ |
| 252 | private function f_find_teid(FullyQualifiedTEID_List list, |
| 253 | template (present) integer if_type, |
| 254 | template (present) BIT4 instance := ?) |
| 255 | return template (omit) FullyQualifiedTEID |
| 256 | { |
| 257 | var integer i; |
| 258 | for (i := 0; i < lengthof(list); i := i+1) { |
| 259 | if (match(list[i].interfaceType, if_type) and |
| 260 | match(list[i].instance, instance)) { |
| 261 | return list[i]; |
| 262 | } |
| 263 | } |
| 264 | return omit; |
| 265 | } |
| 266 | |
| 267 | /* process one to-be-created bearer context */ |
| 268 | private function process_bctx_create(BearerContextGrouped bctx) runs on PGW_Session_CT |
| 269 | { |
| 270 | /* FIXME: EPS Bearer ID */ |
| 271 | /* FIXME: Cause */ |
| 272 | |
| 273 | /* find F-TEID of the P-GW U side */ |
| 274 | var FullyQualifiedTEID rx_fteid; |
| 275 | rx_fteid := valueof(f_find_teid(bctx.bearerContextIEs.fullyQualifiedTEID, 5, '0010'B)); |
| 276 | g_teid_remote := rx_fteid.tEID_GRE_Key; |
| 277 | if (rx_fteid.v4_Flag == '1'B) { |
| 278 | g_gtpu4_remote := rx_fteid.iPv4_Address; |
| 279 | } |
| 280 | if (rx_fteid.v6_Flag == '1'B) { |
| 281 | g_gtpu6_remote := rx_fteid.iPv6_Address; |
| 282 | } |
| 283 | |
| 284 | var UECUPS_CreateTun uecups_create := { |
| 285 | tx_teid := oct2int(g_teid_remote), |
| 286 | rx_teid := oct2int(g_teid), |
| 287 | user_addr_type := IPV4, |
| 288 | user_addr := '00000000'O, |
| 289 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 290 | remote_gtp_ep := valueof(ts_UECUPS_SockAddr(g_gtpu4_remote)), |
| 291 | tun_dev_name := g_pars.tun_dev_name, |
| 292 | tun_netns_name := g_pars.tun_netns_name |
| 293 | }; |
| 294 | |
| 295 | /* create tunnel in daemon */ |
| 296 | if (isbound(g_ip4_addr)) { |
| 297 | uecups_create.user_addr := g_ip4_addr; |
| 298 | f_gtp2_create_tunnel(uecups_create); |
| 299 | } |
| 300 | if (isbound(g_ip6_addr)) { |
| 301 | uecups_create.user_addr_type := IPV6; |
| 302 | uecups_create.user_addr := g_ip6_addr; |
| 303 | f_gtp2_create_tunnel(uecups_create); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* create a session on the PGW */ |
| 308 | private function f_create_session() runs on PGW_Session_CT { |
| 309 | var PDU_GTPCv2 rx; |
| 310 | |
| 311 | /* allocate + register TEID-C on local side */ |
| 312 | g_teic := f_gtp2_allocate_teid(); |
| 313 | g_teid := g_teic; |
| 314 | |
| 315 | var template (value) FullyQualifiedTEID fteid_c_ie, fteid_u_ie; |
| 316 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 317 | f_inet_addr(mp_local_hostname_c), omit); |
| 318 | fteid_u_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPU, g_teid, 2, |
| 319 | f_inet_addr(mp_local_hostname_u), omit); |
| 320 | var template (value) PDU_GTPCv2 g2c := |
| 321 | ts_GTP2C_CreateSessionReq(imsi := g_pars.imsi, msisdn := omit, rat_type := 6, |
| 322 | sender_fteid := fteid_c_ie, |
| 323 | apn := f_enc_dns_hostname(g_pars.apn), |
| 324 | pdn_type := g_pars.pdn_type, teid_list := { fteid_u_ie }, |
| 325 | chg_car := '0000'O, bearer_id := 1); |
| 326 | /* open5gs up to 1.2.3 won't accept it without ULI, despite not mandatory */ |
| 327 | var template (value) TAI tai := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0001'O }; |
| 328 | var template (value) ECGI ecgi := { '0'H, '0'H, '1'H, 'F'H, '0'H, '1'H, '0'H, 23 }; |
| 329 | g2c.gtpcv2_pdu.createSessionRequest.userLocationInfo := ts_GTP2C_UserLocInfo(tai := tai, ecgi := ecgi); |
Vadim Yanitskiy | bada3c9 | 2022-01-20 18:59:07 +0600 | [diff] [blame] | 330 | g2c.gtpcv2_pdu.createSessionRequest.servingNetwork := ts_GTP2C_ServingNetwork('001'H, '01F'H); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 331 | |
| 332 | GTP2.send(g2c); |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 333 | if (DIAMETER_PROC.checkstate("Connected")) { |
| 334 | as_DIA_CCR(INITIAL_REQUEST); |
| 335 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 336 | alt { |
| 337 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:='10'O)) -> value rx { |
| 338 | /* extract TEIDs */ |
| 339 | var CreateSessionResponse resp := rx.gtpcv2_pdu.createSessionResponse; |
| 340 | g_teic_remote := resp.fullyQualifiedTEID[0].tEID_GRE_Key; |
| 341 | |
| 342 | /* extract allocated address[es] */ |
| 343 | var PDN_Address_and_Prefix paa := resp.pDN_AddressAllocation.pDN_Address_and_Prefix; |
| 344 | if (ischosen(paa.iPv4_Address)) { |
| 345 | g_ip4_addr := paa.iPv4_Address; |
| 346 | } else if (ischosen(paa.iPv6_Address)) { |
| 347 | g_ip6_addr := paa.iPv6_Address.iPv6_Address; |
| 348 | g_ip6_plen := paa.iPv6_Address.prefixLength; |
| 349 | } else if (ischosen(paa.iPv4_IPv6)) { |
| 350 | g_ip4_addr := paa.iPv4_IPv6.iPv4_Address; |
| 351 | g_ip6_addr := paa.iPv4_IPv6.iPv6_Address; |
| 352 | g_ip6_plen := paa.iPv4_IPv6.prefixLength; |
| 353 | } |
| 354 | var integer i; |
| 355 | for (i := 0; i < lengthof(resp.bearerContextGrouped); i := i+1) { |
| 356 | var BearerContextGrouped bctx := resp.bearerContextGrouped[i]; |
| 357 | select (bctx.instance) { |
| 358 | case ('0000'B) { // created |
| 359 | process_bctx_create(bctx); |
| 360 | } |
| 361 | case ('0001'B) { // removed |
| 362 | setverdict(fail, "We don't expect removed bearer contexts yet"); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | [] GTP2.receive(tr_GTP2C_CreateSessionResp(d_teid:=g_teic, cause:=?)) -> value rx { |
| 368 | setverdict(fail, "Unexpected CreateSessionResp(cause=", |
| 369 | rx.gtpcv2_pdu.createSessionResponse.cause.causeValue, ")"); |
| 370 | } |
| 371 | [] GTP2.receive { |
| 372 | setverdict(fail, "Unexpected GTPv2 while waiting for CreateSessionResp"); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | } |
| 377 | |
| 378 | /* delete the session from the PGW */ |
| 379 | private function f_delete_session(template (omit) OCT1 tx_cause := omit, |
| 380 | template (present) OCT4 exp_teid, |
Pau Espin Pedrol | 5b0327b | 2022-04-11 13:02:36 +0200 | [diff] [blame^] | 381 | template (present) OCT1 exp_cause, |
| 382 | boolean expect_diameter := true) runs on PGW_Session_CT { |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 383 | var template (value) FullyQualifiedTEID fteid_c_ie |
| 384 | fteid_c_ie := ts_GTP2C_FTEID(FTEID_IF_S5S8_SGW_GTPC, g_teic, 0, |
| 385 | f_inet_addr(mp_local_hostname_c), omit); |
| 386 | var template PDU_GTPCv2 g2c := |
| 387 | ts_GTP2C_DeleteSessionReq(d_teid := g_teic_remote, cause := tx_cause, |
| 388 | sender_fteid := fteid_c_ie, |
| 389 | teid_list := {}, bearer_id := 1); |
| 390 | |
| 391 | GTP2.send(g2c); |
Pau Espin Pedrol | 5b0327b | 2022-04-11 13:02:36 +0200 | [diff] [blame^] | 392 | if (DIAMETER_PROC.checkstate("Connected") and expect_diameter) { |
Harald Welte | f400151 | 2020-04-26 21:48:34 +0200 | [diff] [blame] | 393 | as_DIA_CCR(TERMINATION_REQUEST); |
| 394 | } |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 395 | alt { |
| 396 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(d_teid := exp_teid, cause := exp_cause)) { |
| 397 | setverdict(pass); |
| 398 | } |
| 399 | [] GTP2.receive(tr_GTP2C_DeleteSessionResp(?, ?)) { |
| 400 | setverdict(fail, "Unexpected DeleteSessionResp"); |
| 401 | } |
| 402 | [] GTP2.receive { |
| 403 | setverdict(fail, "Unexpected GTPv2 while waiting for DeleteSessionResp"); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /* destroy tunnel in daemon */ |
| 408 | if (isbound(g_teid)) { |
| 409 | var UECUPS_DestroyTun uecups_destroy := { |
| 410 | local_gtp_ep := valueof(ts_UECUPS_SockAddr(f_inet_addr(mp_local_hostname_u))), |
| 411 | rx_teid := oct2int(g_teid) |
| 412 | }; |
| 413 | /* FIXME: what about IPv4/IPv6 differentiation? */ |
| 414 | f_gtp2_destroy_tunnel(uecups_destroy); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* start a program on the user plane side; return its PID */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 419 | private function f_start_prog(charstring command, boolean redirect_output := true) |
| 420 | runs on PGW_Session_CT return integer |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 421 | { |
| 422 | var UECUPS_StartProgram sprog := { |
| 423 | command := command, |
| 424 | environment := {}, |
| 425 | run_as_user := mp_run_prog_as_user, |
| 426 | tun_netns_name := g_pars.tun_netns_name |
| 427 | }; |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 428 | |
| 429 | /* Redirect stdout/stderr to the user-specified location */ |
| 430 | if (redirect_output) { |
| 431 | var charstring prefix := mp_run_prog_log_path & "/" & testcasename(); |
| 432 | sprog.command := sprog.command & " 1>>" & prefix & ".prog.stdout"; |
| 433 | sprog.command := sprog.command & " 2>>" & prefix & ".prog.stderr"; |
| 434 | } |
| 435 | |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 436 | log("Starting a program: ", command); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 437 | var UECUPS_StartProgramRes res := f_gtp2_start_program(sprog); |
| 438 | if (res.result != OK) { |
| 439 | setverdict(fail, "Unable to start program '", command, "'"); |
| 440 | } |
| 441 | return res.pid; |
| 442 | } |
| 443 | |
| 444 | /* wait for termination of a given PID with specified exit_code */ |
| 445 | private function f_wait_term(integer pid, template (present) integer exit_code := 0, |
| 446 | float tout := 10.0) runs on PGW_Session_CT |
| 447 | { |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 448 | var UECUPS_ProgramTermInd pti; |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 449 | timer T := tout; |
| 450 | |
| 451 | T.start; |
| 452 | alt { |
| 453 | [] GTP2.receive(UECUPS_ProgramTermInd:{pid := pid, exit_code := exit_code}) { |
| 454 | setverdict(pass); |
| 455 | } |
Vadim Yanitskiy | 36ab797 | 2022-02-01 20:46:05 +0600 | [diff] [blame] | 456 | [] GTP2.receive(UECUPS_ProgramTermInd:?) -> value pti { |
| 457 | setverdict(fail, "Received unexpected ProgramTermInd := ", pti); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 458 | } |
| 459 | [] T.timeout { |
| 460 | setverdict(fail, "timeout waiting for user-plane program termination"); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /* execute a program and wait for result */ |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 466 | private function f_start_prog_wait(charstring command, |
| 467 | template integer exit_code := 0, |
| 468 | float tout := 10.0, |
| 469 | boolean redirect_output := true) |
| 470 | runs on PGW_Session_CT |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 471 | { |
Vadim Yanitskiy | 284c68e | 2022-02-02 22:30:37 +0600 | [diff] [blame] | 472 | var integer pid := f_start_prog(command, redirect_output); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 473 | f_wait_term(pid, exit_code, tout); |
| 474 | } |
| 475 | |
| 476 | /* execute ping command and wait for result */ |
| 477 | private function f_ping4(charstring host, integer interval := 1, integer count := 10) runs on PGW_Session_CT |
| 478 | { |
| 479 | var charstring ping :="ping -c " & int2str(count) & " -i " & int2str(interval); |
| 480 | ping := ping & " -I " & f_inet_ntoa(g_ip4_addr); |
| 481 | ping := ping & " " & host; |
Harald Welte | 8cfdc7c | 2020-04-21 22:48:34 +0200 | [diff] [blame] | 482 | f_start_prog_wait(ping, tout := int2float(5 + interval*count)); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | /* send echo request; expect response */ |
| 489 | testcase TC_tx_echo() runs on PGW_Test_CT { |
| 490 | timer T := 5.0; |
| 491 | |
| 492 | f_init(); |
| 493 | |
| 494 | TEID0.send(ts_GTP2C_EchoReq(0)); |
| 495 | T.start; |
| 496 | alt { |
| 497 | [] TEID0.receive(tr_GTP2C_EchoResp) { |
| 498 | setverdict(pass); |
| 499 | } |
| 500 | [] T.timeout { |
| 501 | setverdict(fail, "timeout waiting for Echo Response"); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* create a session, expect it to succeed */ |
| 507 | private function f_TC_createSession() runs on PGW_Session_CT { |
| 508 | f_create_session(); |
| 509 | setverdict(pass); |
| 510 | } |
| 511 | testcase TC_createSession() runs on PGW_Test_CT { |
| 512 | var PGW_Session_CT vc_conn; |
| 513 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun22")); |
| 514 | f_init(); |
| 515 | vc_conn := f_start_handler(refers(f_TC_createSession), pars); |
| 516 | vc_conn.done; |
| 517 | } |
| 518 | |
| 519 | /* create a session, then execute a ping command on the user plane */ |
| 520 | private function f_TC_createSession_ping4() runs on PGW_Session_CT { |
| 521 | f_create_session(); |
| 522 | f_ping4(mp_ping_hostname); |
| 523 | setverdict(pass); |
| 524 | } |
| 525 | testcase TC_createSession_ping4() runs on PGW_Test_CT { |
| 526 | var PGW_Session_CT vc_conn; |
| 527 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 528 | f_init(); |
| 529 | vc_conn := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 530 | vc_conn.done; |
| 531 | } |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 532 | testcase TC_createSession_ping4_256() runs on PGW_Test_CT { |
| 533 | var PGW_Session_CT vc_conn[256]; |
| 534 | var integer i; |
| 535 | |
| 536 | f_init(); |
| 537 | |
| 538 | for (i := 0; i < sizeof(vc_conn); i:=i+1) { |
| 539 | var charstring tundev := "ping" & int2str(i); |
| 540 | var SessionPars pars := valueof(t_SessionPars(f_gen_imsi(i), tundev)); |
| 541 | vc_conn[i] := f_start_handler(refers(f_TC_createSession_ping4), pars); |
| 542 | } |
| 543 | |
| 544 | for (i := 0; i < lengthof(vc_conn); i:=i+1) { |
| 545 | vc_conn[i].done; |
| 546 | } |
| 547 | } |
| 548 | |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 549 | |
| 550 | /* create a session, then delete it again */ |
| 551 | private function f_TC_createSession_deleteSession() runs on PGW_Session_CT { |
| 552 | f_create_session(); |
| 553 | f_delete_session(omit, g_teic, '10'O); |
| 554 | setverdict(pass); |
| 555 | } |
| 556 | testcase TC_createSession_deleteSession() runs on PGW_Test_CT { |
| 557 | var PGW_Session_CT vc_conn; |
| 558 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 559 | f_init(); |
| 560 | vc_conn := f_start_handler(refers(f_TC_createSession_deleteSession), pars); |
| 561 | vc_conn.done; |
| 562 | } |
| 563 | |
| 564 | /* send a DeleteSessionReq for an unknown/invalid TEID */ |
| 565 | private function f_TC_deleteSession_unknown() runs on PGW_Session_CT { |
| 566 | g_teic := f_gtp2_allocate_teid(); |
| 567 | g_teic_remote := f_rnd_octstring(4); |
Pau Espin Pedrol | 5b0327b | 2022-04-11 13:02:36 +0200 | [diff] [blame^] | 568 | f_delete_session(omit, '00000000'O, '40'O /* Context Unknown */, false); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 569 | setverdict(pass); |
| 570 | } |
| 571 | testcase TC_deleteSession_unknown() runs on PGW_Test_CT { |
| 572 | var PGW_Session_CT vc_conn; |
| 573 | var SessionPars pars := valueof(t_SessionPars('001010123456789'H, "tun23")); |
| 574 | f_init(); |
| 575 | vc_conn := f_start_handler(refers(f_TC_deleteSession_unknown), pars); |
| 576 | vc_conn.done; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | control { |
| 583 | execute( TC_tx_echo() ); |
| 584 | execute( TC_createSession() ); |
| 585 | execute( TC_createSession_ping4() ); |
Harald Welte | fe595e4 | 2020-04-21 22:56:47 +0200 | [diff] [blame] | 586 | execute( TC_createSession_ping4_256() ); |
Harald Welte | 4526da9 | 2020-03-05 23:08:10 +0100 | [diff] [blame] | 587 | execute( TC_createSession_deleteSession() ); |
| 588 | execute( TC_deleteSession_unknown() ); |
| 589 | } |
| 590 | |
| 591 | |
| 592 | } |