/
prepare_request_for_smf.cpp
446 lines (386 loc) · 18.8 KB
/
prepare_request_for_smf.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* Copyright 2020 The Magma Authors.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "lte/gateway/c/core/oai/common/log.h"
#include "lte/gateway/c/core/oai/common/conversions.h"
#include "lte/gateway/c/core/oai/lib/3gpp/3gpp_38.401.h"
#include "lte/gateway/c/core/oai/include/nas/networkDef.h"
#ifdef __cplusplus
}
#endif
#include "lte/gateway/c/core/common/common_defs.h"
#include <cstdint>
#include <cstring>
#include <string>
#include "lte/gateway/c/core/oai/lib/mobility_client/MobilityServiceClient.hpp"
#include <unistd.h>
#include <thread>
#include "lte/gateway/c/core/oai/lib/n11/SmfServiceClient.hpp"
#include "lte/gateway/c/core/oai/tasks/amf/amf_smfDefs.hpp"
#include "lte/gateway/c/core/oai/common/conversions.h"
#include "lte/protos/session_manager.pb.h"
#include "lte/gateway/c/core/oai/lib/n11/M5GMobilityServiceClient.hpp"
#include "lte/gateway/c/core/oai/tasks/amf/amf_app_ue_context_and_proc.hpp"
#include "lte/gateway/c/core/oai/tasks/amf/include/amf_client_servicer.hpp"
#define VERSION_0 0
using grpc::Channel;
using grpc::ChannelCredentials;
using grpc::CreateChannel;
using grpc::InsecureChannelCredentials;
using grpc::Status;
using magma::lte::AllocateIPAddressResponse;
using magma::lte::IPAddress;
using magma::lte::MobilityServiceClient;
using magma::lte::QosPolicy;
using magma::lte::SetSMSessionContext;
using magma::lte::TeidSet;
using magma5g::AsyncSmfServiceClient;
namespace magma5g {
/***************************************************************************
** **
** Name: create_session_grpc_req_on_gnb_setup_rsp() **
** **
** Description: **
** Creating session grpc req upon receiving resource setup response **
** from gNB which have gNB IP and TEID, and sends to UPF through SMF **
** **
** **
***************************************************************************/
status_code_e create_session_grpc_req_on_gnb_setup_rsp(
amf_smf_establish_t* message, char* imsi, uint32_t version,
std::shared_ptr<smf_context_t> smf_ctx) {
status_code_e rc = RETURNerror;
magma::lte::SetSMSessionContext req;
auto imsi_str = std::string(imsi);
auto* req_common = req.mutable_common_context();
auto* req_rat_specific =
req.mutable_rat_specific_context()->mutable_m5gsm_session_context();
// IMSI retrieved from amf context
req_common->mutable_sid()->set_type(
magma::lte::SubscriberID_IDType::SubscriberID_IDType_IMSI);
req_common->mutable_sid()->set_id("IMSI" + imsi_str);
req_common->set_rat_type(magma::lte::RATType::TGPP_NR);
// PDU session state to CREATING
req_common->set_sm_session_state(magma::lte::SMSessionFSMState::CREATING_0);
// Version Added
req_common->set_sm_session_version(version);
req_rat_specific->set_pdu_session_id((message->pdu_session_id));
req_rat_specific->set_request_type(magma::lte::RequestType::INITIAL_REQUEST);
TeidSet* gnode_endpoint = req_rat_specific->mutable_gnode_endpoint();
gnode_endpoint->set_teid(message->gnb_gtp_teid);
char ipv4_str[INET_ADDRSTRLEN] = {0};
inet_ntop(AF_INET, message->gnb_gtp_teid_ip_addr, ipv4_str, INET_ADDRSTRLEN);
req_rat_specific->mutable_gnode_endpoint()->set_end_ipv4_addr(ipv4_str);
#if 0
for (int i = 0; i < smf_ctx->qos_flow_list.maxNumOfQosFlows; i++) {
QosPolicy* qosPolicy = req_rat_specific->add_qos_policy();
qosPolicy->set_version(
smf_ctx->qos_flow_list.item[i].qos_flow_req_item.qos_flow_version);
qosPolicy->set_policy_state(QosPolicy::INSTALL);
magma::lte::PolicyRule* rule = qosPolicy->mutable_qos();
rule->set_id(
(const char*)smf_ctx->qos_flow_list.item[i].qos_flow_req_item.rule_id);
}
#endif
OAILOG_DEBUG(LOG_AMF_APP, "Sending PDU session Setup Request to SMF");
OAILOG_INFO(LOG_AMF_APP, "Sending msg(grpc) to :[sessiond] for ue: [%s]\n",
imsi);
AMFClientServicer::getInstance().set_smf_session(req);
return rc;
}
/***************************************************************************
** **
** Name: amf_send_grpc_req_on_gnb_pdu_sess_mod_rsp() **
** **
** Description: **
** Creating session grpc req upon receiving resource modify response **
** from gNB sends qos flow info to UPF through SMF **
** **
** **
***************************************************************************/
int amf_send_grpc_req_on_gnb_pdu_sess_mod_rsp(
amf_smf_establish_t* message, char* imsi, uint32_t version,
std::shared_ptr<smf_context_t> smf_ctx) {
int rc = RETURNerror;
magma::lte::SetSMSessionContext req;
auto* req_common = req.mutable_common_context();
auto* req_rat_specific =
req.mutable_rat_specific_context()->mutable_m5gsm_session_context();
// IMSI retrieved from amf context
req_common->mutable_sid()->mutable_id()->assign(
imsi); // string id
// IMSI retrieved from amf context
auto imsi_str = std::string(imsi);
req_common->mutable_sid()->set_type(
magma::lte::SubscriberID_IDType::SubscriberID_IDType_IMSI);
req_common->mutable_sid()->set_id("IMSI" + imsi_str);
req_common->set_rat_type(magma::lte::RATType::TGPP_NR);
// PDU session state to ACTIVE_2
req_common->set_sm_session_state(magma::lte::SMSessionFSMState::ACTIVE_2);
// Version Added
req_common->set_sm_session_version(version);
req_rat_specific->set_pdu_session_id((message->pdu_session_id));
req_rat_specific->set_request_type(
magma::lte::RequestType::EXISTING_PDU_SESSION);
TeidSet* gnode_endpoint = req_rat_specific->mutable_gnode_endpoint();
gnode_endpoint->set_teid(message->gnb_gtp_teid);
char ipv4_str[INET_ADDRSTRLEN] = {0};
inet_ntop(AF_INET, message->gnb_gtp_teid_ip_addr, ipv4_str, INET_ADDRSTRLEN);
req_rat_specific->mutable_gnode_endpoint()->set_end_ipv4_addr(ipv4_str);
qos_flow_list_t* pti_flow_list = smf_ctx->get_proc_flow_list();
for (int i = 0; i < pti_flow_list->maxNumOfQosFlows; i++) {
QosPolicy* qosPolicy = req_rat_specific->add_qos_policy();
qosPolicy->set_version(
pti_flow_list->item[i].qos_flow_req_item.qos_flow_version);
if (SMF_CAUSE_FAILURE == message->cause_value) {
qosPolicy->set_policy_state(QosPolicy::REJECT);
} else {
qosPolicy->set_policy_state(QosPolicy::INSTALL);
}
magma::lte::PolicyRule* rule = qosPolicy->mutable_qos();
rule->set_id((const char*)pti_flow_list->item[i].qos_flow_req_item.rule_id);
}
OAILOG_DEBUG(LOG_AMF_APP, "Sending PDU Session Modification Response to SMF");
OAILOG_INFO(
LOG_AMF_APP,
"Sending msg(grpc) to :[sessiond] for ue: [%s] pdu session :[%u]\n", imsi,
message->pdu_session_id);
AMFClientServicer::getInstance().set_smf_session(req);
OAILOG_FUNC_RETURN(LOG_AMF_APP, rc);
}
/***************************************************************************
** **
** Name: amf_smf_create_session_req() **
** **
** Description: Fill session establishment gRPC request to SMF **
** **
** **
***************************************************************************/
status_code_e amf_smf_create_session_req(
char* imsi, uint8_t* apn, uint32_t pdu_session_id,
uint32_t pdu_session_type, uint32_t gnb_gtp_teid, uint8_t pti,
uint8_t* gnb_gtp_teid_ip_addr, char* ue_ipv4_addr, char* ue_ipv6_addr,
const ambr_t& state_ambr, const eps_subscribed_qos_profile_t& qos_profile) {
imsi64_t imsi64 = INVALID_IMSI64;
ue_m5gmm_context_s* ue_mm_context = NULL;
amf_context_t* amf_ctxt_p = NULL;
OAILOG_FUNC_IN(LOG_AMF_APP);
OAILOG_INFO(
LOG_AMF_APP,
"Sending msg(grpc) to :[sessiond] for ue: [%s] pdu session: [%u]\n", imsi,
pdu_session_id);
IMSI_STRING_TO_IMSI64((char*)imsi, &imsi64);
ue_mm_context = lookup_ue_ctxt_by_imsi(imsi64);
if (ue_mm_context) {
amf_ctxt_p = &ue_mm_context->amf_context;
}
if (!(amf_ctxt_p)) {
OAILOG_ERROR(LOG_NAS_AMF, "IMSI is invalid\n");
OAILOG_FUNC_RETURN(LOG_NAS_AMF, RETURNerror);
}
OAILOG_FUNC_RETURN(
LOG_AMF_APP, AMFClientServicer::getInstance().amf_smf_create_pdu_session(
imsi, apn, pdu_session_id, pdu_session_type,
gnb_gtp_teid, pti, gnb_gtp_teid_ip_addr, ue_ipv4_addr,
ue_ipv6_addr, state_ambr, VERSION_0, qos_profile));
}
/***************************************************************************
* ** **
* ** Name: amf_smf_initiate_pdu_session_creation() **
* ** **
* ** Description: Initiate PDU Session Creation process **
* ** **
* ** **
* ***************************************************************************/
status_code_e amf_smf_initiate_pdu_session_creation(
amf_smf_establish_t* message, char* imsi, uint32_t version) {
OAILOG_FUNC_IN(LOG_AMF_APP);
imsi64_t imsi64 = INVALID_IMSI64;
amf_context_t* amf_ctxt_p = NULL;
ue_m5gmm_context_s* ue_mm_context = NULL;
std::shared_ptr<smf_context_t> smf_ctx;
IMSI_STRING_TO_IMSI64((char*)imsi, &imsi64);
ue_mm_context = lookup_ue_ctxt_by_imsi(imsi64);
smf_ctx = amf_get_smf_context_by_pdu_session_id(ue_mm_context,
message->pdu_session_id);
if (ue_mm_context) {
amf_ctxt_p = &ue_mm_context->amf_context;
}
if (!(amf_ctxt_p)) {
OAILOG_ERROR(LOG_NAS_AMF, "IMSI is invalid\n");
OAILOG_FUNC_RETURN(LOG_NAS_AMF, RETURNerror);
}
OAILOG_INFO(
LOG_AMF_APP,
"Sending msg(grpc) to :[mobilityd] for ue: [%s] ip-addr pdu session: "
"[%u]\n",
imsi, message->pdu_session_id);
if (message->pdu_session_type == NET_PDN_TYPE_IPV4) {
AMFClientServicer::getInstance().allocate_ipv4_address(
imsi, smf_ctx->dnn.c_str(), message->pdu_session_id, message->pti,
NET_PDN_TYPE_IPV4, message->gnb_gtp_teid, message->gnb_gtp_teid_ip_addr,
4);
} else if (message->pdu_session_type == NET_PDN_TYPE_IPV6) {
AMFClientServicer::getInstance().allocate_ipv6_address(
imsi, smf_ctx->dnn.c_str(), message->pdu_session_id, message->pti,
NET_PDN_TYPE_IPV6, message->gnb_gtp_teid, message->gnb_gtp_teid_ip_addr,
4);
} else if (message->pdu_session_type == NET_PDN_TYPE_IPV4V6) {
AMFClientServicer::getInstance().allocate_ipv4v6_address(
imsi, smf_ctx->dnn.c_str(), message->pdu_session_id, message->pti,
NET_PDN_TYPE_IPV4V6, message->gnb_gtp_teid,
message->gnb_gtp_teid_ip_addr, 4);
}
OAILOG_FUNC_RETURN(LOG_AMF_APP, RETURNok);
}
/***************************************************************************
** **
** Name: release_session_gprc_req() **
** **
** Description: Sends session release request to SMF **
** **
** **
***************************************************************************/
status_code_e release_session_gprc_req(amf_smf_release_t* message, char* imsi) {
OAILOG_FUNC_IN(LOG_AMF_APP);
magma::lte::SetSMSessionContext req;
auto imsi_str = std::string(imsi);
auto* req_common = req.mutable_common_context();
// Encode subscriber as IMSI
req_common->mutable_sid()->set_type(
magma::lte::SubscriberID_IDType::SubscriberID_IDType_IMSI);
req_common->mutable_sid()->set_id("IMSI" + imsi_str);
req_common->set_sm_session_state(magma::lte::SMSessionFSMState::RELEASED_4);
req_common->set_sm_session_version(1); // uint32
auto* req_rat_specific =
req.mutable_rat_specific_context()->mutable_m5gsm_session_context();
req_rat_specific->set_pdu_session_id(message->pdu_session_id);
req_rat_specific->set_procedure_trans_identity(message->pti);
req_common->set_rat_type(magma::lte::RATType::TGPP_NR);
OAILOG_INFO(
LOG_AMF_APP,
"Sending msg(grpc) to :[sessiond] for ue: [%s] release, pdu session: "
"[%d]\n",
imsi, message->pdu_session_id);
AMFClientServicer::getInstance().set_smf_session(req);
OAILOG_FUNC_RETURN(LOG_AMF_APP, RETURNok);
}
/***************************************************************************
* ** **
* ** Name: amf_app_pdu_session_modification_complete() **
* ** **
* ** Description: Handle PDU Session Modification Complete Msg **
* ** **
* ** **
* ***************************************************************************/
int amf_app_pdu_session_modification_complete(amf_smf_establish_t* message,
char* imsi, uint32_t version) {
imsi64_t imsi64 = INVALID_IMSI64;
amf_context_t* amf_ctxt_p = NULL;
ue_m5gmm_context_s* ue_mm_context = NULL;
std::shared_ptr<smf_context_t> smf_ctx;
amf_smf_establish_t amf_smf_grpc_ies;
IMSI_STRING_TO_IMSI64((char*)imsi, &imsi64);
ue_mm_context = lookup_ue_ctxt_by_imsi(imsi64);
smf_ctx = amf_get_smf_context_by_pdu_session_id(ue_mm_context,
message->pdu_session_id);
if (ue_mm_context) {
amf_ctxt_p = &ue_mm_context->amf_context;
}
if (!(amf_ctxt_p)) {
OAILOG_ERROR(LOG_NAS_AMF, "IMSI is invalid\n");
OAILOG_FUNC_RETURN(LOG_NAS_AMF, RETURNerror);
}
if (!smf_ctx) {
OAILOG_ERROR(LOG_NAS_AMF,
"session context not found using session id [%u]\n",
message->pdu_session_id);
OAILOG_FUNC_RETURN(LOG_NAS_AMF, RETURNerror);
}
OAILOG_INFO(LOG_AMF_APP, "Received PDU Session Modification Complete [%s] \n",
imsi);
// Incrementing the pdu session version
smf_ctx->pdu_session_version++;
amf_smf_grpc_ies.pdu_session_id = message->pdu_session_id;
amf_smf_grpc_ies.cause_value = SMF_CAUSE_SUCCESS;
// gnb tunnel info
memset(&amf_smf_grpc_ies.gnb_gtp_teid_ip_addr, '\0',
sizeof(amf_smf_grpc_ies.gnb_gtp_teid_ip_addr));
memset(&amf_smf_grpc_ies.gnb_gtp_teid, '\0',
sizeof(amf_smf_grpc_ies.gnb_gtp_teid));
memcpy(&amf_smf_grpc_ies.gnb_gtp_teid_ip_addr,
&smf_ctx->gtp_tunnel_id.gnb_gtp_teid_ip_addr, 4);
memcpy(&amf_smf_grpc_ies.gnb_gtp_teid, &smf_ctx->gtp_tunnel_id.gnb_gtp_teid,
4);
IMSI64_TO_STRING(ue_mm_context->amf_context.imsi64, imsi, 15);
// Prepare and send modify setup response message to SMF through gRPC
amf_send_grpc_req_on_gnb_pdu_sess_mod_rsp(
&amf_smf_grpc_ies, imsi, smf_ctx->pdu_session_version, smf_ctx);
return (RETURNok);
}
/***************************************************************************
* ** **
* ** Name: amf_app_pdu_session_modification_command_reject() **
* ** **
* ** Description: Handle PDU Session Modification Command reject Msg **
* ** **
* ** **
* ***************************************************************************/
int amf_app_pdu_session_modification_command_reject(
amf_smf_establish_t* message, char* imsi, uint32_t version) {
imsi64_t imsi64 = INVALID_IMSI64;
amf_context_t* amf_ctxt_p = NULL;
ue_m5gmm_context_s* ue_mm_context = NULL;
std::shared_ptr<smf_context_t> smf_ctx;
amf_smf_establish_t amf_smf_grpc_ies;
IMSI_STRING_TO_IMSI64((char*)imsi, &imsi64);
ue_mm_context = lookup_ue_ctxt_by_imsi(imsi64);
smf_ctx = amf_get_smf_context_by_pdu_session_id(ue_mm_context,
message->pdu_session_id);
if (ue_mm_context) {
amf_ctxt_p = &ue_mm_context->amf_context;
}
if (!(amf_ctxt_p)) {
OAILOG_ERROR(LOG_NAS_AMF, "IMSI is invalid\n");
OAILOG_FUNC_RETURN(LOG_NAS_AMF, RETURNerror);
}
if (!smf_ctx) {
OAILOG_ERROR(LOG_NAS_AMF,
"session context not found using session id [%u]\n",
message->pdu_session_id);
OAILOG_FUNC_RETURN(LOG_NAS_AMF, RETURNerror);
}
OAILOG_INFO(LOG_AMF_APP,
"Received PDU Session Modification Command Reject [%s] \n", imsi);
// Incrementing the pdu session version
smf_ctx->pdu_session_version++;
amf_smf_grpc_ies.pdu_session_id = message->pdu_session_id;
amf_smf_grpc_ies.cause_value = SMF_CAUSE_FAILURE;
// gnb tunnel info
memset(&amf_smf_grpc_ies.gnb_gtp_teid_ip_addr, '\0',
sizeof(amf_smf_grpc_ies.gnb_gtp_teid_ip_addr));
memset(&amf_smf_grpc_ies.gnb_gtp_teid, '\0',
sizeof(amf_smf_grpc_ies.gnb_gtp_teid));
memcpy(&amf_smf_grpc_ies.gnb_gtp_teid_ip_addr,
&smf_ctx->gtp_tunnel_id.gnb_gtp_teid_ip_addr, 4);
memcpy(&amf_smf_grpc_ies.gnb_gtp_teid, &smf_ctx->gtp_tunnel_id.gnb_gtp_teid,
4);
IMSI64_TO_STRING(ue_mm_context->amf_context.imsi64, imsi, 15);
// Prepare and send modify setup response message to SMF through gRPC
amf_send_grpc_req_on_gnb_pdu_sess_mod_rsp(
&amf_smf_grpc_ies, imsi, smf_ctx->pdu_session_version, smf_ctx);
return (RETURNok);
}
} // namespace magma5g