open5gs/src/mme/mme_init.c

136 lines
2.9 KiB
C
Raw Normal View History

2017-03-04 14:48:04 +00:00
#define TRACE_MODULE _mme_init
2017-02-02 11:34:37 +00:00
#include "core_debug.h"
2017-02-13 00:58:55 +00:00
#include "core_thread.h"
2017-11-30 11:13:15 +00:00
#include "core_msgq.h"
#include "core_fsm.h"
2018-01-04 11:38:22 +00:00
#include "gtp/gtp_xact.h"
2017-02-13 00:58:55 +00:00
2017-04-04 01:49:19 +00:00
#include "mme_event.h"
2017-02-13 00:58:55 +00:00
2017-08-25 11:31:08 +00:00
#include "mme_fd_path.h"
#include "s1ap_path.h"
2017-03-04 15:20:09 +00:00
2017-11-30 11:13:15 +00:00
#include "mme_sm.h"
2017-08-02 12:11:22 +00:00
static thread_id sm_thread;
static void *THREAD_FUNC sm_main(thread_id id, void *data);
static thread_id net_thread;
static void *THREAD_FUNC net_main(thread_id id, void *data);
2017-02-02 11:34:37 +00:00
static int initialized = 0;
2017-03-04 14:48:04 +00:00
status_t mme_initialize()
2017-02-02 11:34:37 +00:00
{
2017-02-06 11:54:31 +00:00
status_t rv;
2017-02-02 11:34:37 +00:00
2017-04-06 11:10:00 +00:00
rv = mme_context_init();
2017-02-26 12:16:03 +00:00
if (rv != CORE_OK) return rv;
2017-07-14 10:00:26 +00:00
rv = mme_context_parse_config();
if (rv != CORE_OK) return rv;
2017-07-31 13:35:25 +00:00
rv = mme_context_setup_trace_module();
if (rv != CORE_OK) return rv;
2018-01-10 05:15:00 +00:00
rv = mme_fd_init();
if (rv != CORE_OK) return CORE_ERROR;
2017-02-27 04:56:13 +00:00
2017-11-23 14:01:49 +00:00
#define USRSCTP_LOCAL_UDP_PORT 9899
rv = s1ap_init(USRSCTP_LOCAL_UDP_PORT);
if (rv != CORE_OK) return rv;
2017-08-02 12:11:22 +00:00
rv = thread_create(&sm_thread, NULL, sm_main, NULL);
if (rv != CORE_OK) return rv;
rv = thread_create(&net_thread, NULL, net_main, NULL);
2017-03-04 14:48:04 +00:00
if (rv != CORE_OK) return rv;
initialized = 1;
2017-03-04 14:48:04 +00:00
2017-02-02 11:34:37 +00:00
return CORE_OK;
}
2017-03-04 14:48:04 +00:00
void mme_terminate(void)
2017-02-02 11:34:37 +00:00
{
if (!initialized) return;
2017-08-02 12:11:22 +00:00
thread_delete(net_thread);
thread_delete(sm_thread);
2017-03-04 14:48:04 +00:00
2017-08-25 11:31:08 +00:00
mme_fd_final();
2017-03-04 15:20:09 +00:00
2017-04-06 11:10:00 +00:00
mme_context_final();
2017-04-11 13:44:57 +00:00
2017-10-20 07:32:57 +00:00
s1ap_final();
2017-04-11 13:44:57 +00:00
gtp_xact_final();
2017-02-02 11:34:37 +00:00
}
2017-02-13 04:19:53 +00:00
2017-08-02 12:11:22 +00:00
static void *THREAD_FUNC sm_main(thread_id id, void *data)
2017-02-13 04:19:53 +00:00
{
event_t event;
2017-04-11 00:32:34 +00:00
fsm_t mme_sm;
2017-02-13 04:19:53 +00:00
c_time_t prev_tm, now_tm;
2017-03-27 01:22:30 +00:00
status_t rv;
2017-02-13 04:19:53 +00:00
memset(&event, 0, sizeof(event_t));
2017-11-09 05:56:03 +00:00
mme_self()->queue_id = event_create(MSGQ_O_BLOCK);
2017-03-06 08:55:50 +00:00
d_assert(mme_self()->queue_id, return NULL,
"MME event queue creation failed");
2017-03-28 07:35:57 +00:00
tm_service_init(&mme_self()->tm_service);
2017-08-31 11:48:15 +00:00
gtp_xact_init(&mme_self()->tm_service,
2017-09-04 11:06:54 +00:00
MME_EVT_S11_T3_RESPONSE, MME_EVT_S11_T3_HOLDING);
2017-02-13 04:19:53 +00:00
2017-04-11 00:32:34 +00:00
fsm_create(&mme_sm, mme_state_initial, mme_state_final);
fsm_init(&mme_sm, 0);
2017-02-13 04:19:53 +00:00
prev_tm = time_now();
#define EVENT_LOOP_TIMEOUT 50 /* 50ms */
2017-02-15 00:17:44 +00:00
while ((!thread_should_stop()))
2017-02-13 04:19:53 +00:00
{
rv = event_timedrecv(mme_self()->queue_id, &event, EVENT_LOOP_TIMEOUT);
2017-02-13 04:19:53 +00:00
2017-03-27 01:22:30 +00:00
d_assert(rv != CORE_ERROR, continue,
2017-02-13 04:19:53 +00:00
"While receiving a event message, error occurs");
now_tm = time_now();
/* if the gap is over 10 ms, execute preriodic jobs */
if (now_tm - prev_tm > EVENT_LOOP_TIMEOUT * 1000)
2017-02-13 04:19:53 +00:00
{
2017-03-24 05:18:30 +00:00
tm_execute_tm_service(
&mme_self()->tm_service, mme_self()->queue_id);
2017-02-13 04:19:53 +00:00
prev_tm = now_tm;
}
2017-03-27 01:22:30 +00:00
if (rv == CORE_TIMEUP)
2017-02-13 04:19:53 +00:00
{
continue;
}
2017-04-11 00:32:34 +00:00
fsm_dispatch(&mme_sm, (fsm_event_t*)&event);
2017-02-13 04:19:53 +00:00
}
2017-04-11 00:32:34 +00:00
fsm_final(&mme_sm, 0);
fsm_clear(&mme_sm);
2017-02-13 04:19:53 +00:00
2017-03-06 08:55:50 +00:00
event_delete(mme_self()->queue_id);
2017-02-13 04:19:53 +00:00
return NULL;
}
2017-08-02 12:11:22 +00:00
static void *THREAD_FUNC net_main(thread_id id, void *data)
2017-02-13 04:19:53 +00:00
{
while (!thread_should_stop())
{
sock_select_loop(EVENT_LOOP_TIMEOUT);
2017-02-13 04:19:53 +00:00
}
return NULL;
}