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