enb_app.c 14.2 KB
Newer Older
winckel's avatar
winckel committed
1
/*******************************************************************************
nikaeinn's avatar
nikaeinn committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
nikaeinn's avatar
nikaeinn committed
27

28
*******************************************************************************/
nikaeinn's avatar
nikaeinn committed
29

30
31
32
/*
                                enb_app.c
                             -------------------
nikaeinn's avatar
nikaeinn committed
33
  AUTHOR  : Laurent Winckel, Sebastien ROUX, Lionel GAUTHIER, Navid Nikaein
34
  COMPANY : EURECOM
nikaeinn's avatar
nikaeinn committed
35
  EMAIL   : Lionel.Gauthier@eurecom.fr and Navid Nikaein
36
*/
winckel's avatar
winckel committed
37
38

#include <string.h>
gauthier's avatar
   
gauthier committed
39
#include <stdio.h>
winckel's avatar
winckel committed
40
41

#include "enb_app.h"
42
#include "enb_config.h"
winckel's avatar
winckel committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "assertions.h"

#include "log.h"
#if defined(OAI_EMU)
# include "OCG.h"
# include "OCG_extern.h"
#endif

#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
# include "timer.h"
# if defined(ENABLE_USE_MME)
#   include "s1ap_eNB.h"
#   include "sctp_eNB_task.h"
57
#   include "gtpv1u_eNB_task.h"
winckel's avatar
winckel committed
58
# endif
59
60

extern unsigned char NB_eNB_INST;
winckel's avatar
winckel committed
61
62
63
64
#endif

#if defined(ENABLE_ITTI)

65
/*------------------------------------------------------------------------------*/
66
67
68
# if defined(ENABLE_USE_MME)
#   define ENB_REGISTER_RETRY_DELAY 10
# endif
winckel's avatar
winckel committed
69

70
/*------------------------------------------------------------------------------*/
winckel's avatar
winckel committed
71
static void configure_phy(uint32_t enb_id, const Enb_properties_array_t *enb_properties)
72
73
{
    MessageDef *msg_p;
knopp's avatar
   
knopp committed
74
    int CC_id;
75
76

    msg_p = itti_alloc_new_message (TASK_ENB_APP, PHY_CONFIGURATION_REQ);
knopp's avatar
   
knopp committed
77
78
79
80
81
82
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
      PHY_CONFIGURATION_REQ (msg_p).frame_type[CC_id] =              enb_properties->properties[enb_id]->frame_type[CC_id];
      PHY_CONFIGURATION_REQ (msg_p).prefix_type[CC_id] =             enb_properties->properties[enb_id]->prefix_type[CC_id];
      PHY_CONFIGURATION_REQ (msg_p).downlink_frequency[CC_id] =      enb_properties->properties[enb_id]->downlink_frequency[CC_id];
      PHY_CONFIGURATION_REQ (msg_p).uplink_frequency_offset[CC_id] = enb_properties->properties[enb_id]->uplink_frequency_offset[CC_id];
    }
83
84
    itti_send_msg_to_task (TASK_PHY_ENB, enb_id, msg_p);
}
85
86

/*------------------------------------------------------------------------------*/
winckel's avatar
winckel committed
87
static void configure_rrc(uint32_t enb_id, const Enb_properties_array_t *enb_properties)
winckel's avatar
winckel committed
88
{
nikaeinn's avatar
nikaeinn committed
89
    MessageDef *msg_p = NULL;
knopp's avatar
   
knopp committed
90
    int CC_id;
winckel's avatar
winckel committed
91

92
    msg_p = itti_alloc_new_message (TASK_ENB_APP, RRC_CONFIGURATION_REQ);
winckel's avatar
winckel committed
93

winckel's avatar
winckel committed
94
95
96
97
    RRC_CONFIGURATION_REQ (msg_p).cell_identity =   enb_properties->properties[enb_id]->eNB_id;
    RRC_CONFIGURATION_REQ (msg_p).tac =             enb_properties->properties[enb_id]->tac;
    RRC_CONFIGURATION_REQ (msg_p).mcc =             enb_properties->properties[enb_id]->mcc;
    RRC_CONFIGURATION_REQ (msg_p).mnc =             enb_properties->properties[enb_id]->mnc;
nikaeinn's avatar
nikaeinn committed
98
    RRC_CONFIGURATION_REQ (msg_p).mnc_digit_length = enb_properties->properties[enb_id]->mnc_digit_length;
knopp's avatar
   
knopp committed
99
100
101
102
103
104
105
    for (CC_id=0;CC_id<MAX_NUM_CCs;CC_id++) {
      RRC_CONFIGURATION_REQ (msg_p).pcch_defaultPagingCycle[CC_id] =     enb_properties->properties[enb_id]->pcch_defaultPagingCycle[CC_id];
      RRC_CONFIGURATION_REQ (msg_p).frame_type[CC_id] =      enb_properties->properties[enb_id]->frame_type[CC_id];
      RRC_CONFIGURATION_REQ (msg_p).tdd_config[CC_id] =      enb_properties->properties[enb_id]->tdd_config[CC_id];
      RRC_CONFIGURATION_REQ (msg_p).tdd_config_s[CC_id] =    enb_properties->properties[enb_id]->tdd_config_s[CC_id];
      RRC_CONFIGURATION_REQ (msg_p).eutra_band[CC_id] =      enb_properties->properties[enb_id]->eutra_band[CC_id];
    }
106
    itti_send_msg_to_task (TASK_RRC_ENB, enb_id, msg_p);
winckel's avatar
winckel committed
107
108
}

109
/*------------------------------------------------------------------------------*/
110
# if defined(ENABLE_USE_MME)
winckel's avatar
winckel committed
111
static uint32_t eNB_app_register(uint32_t enb_id_start, uint32_t enb_id_end, const Enb_properties_array_t *enb_properties)
winckel's avatar
winckel committed
112
{
gauthier's avatar
   
gauthier committed
113
114
115
116
117
118
    uint32_t         enb_id;
    uint32_t         mme_id;
    MessageDef      *msg_p;
    uint32_t         register_enb_pending = 0;
    char            *str                  = NULL;
    struct in_addr   addr;
119
120

#   if defined(OAI_EMU)
winckel's avatar
winckel committed
121

winckel's avatar
winckel committed
122
#   endif
123

124
    for (enb_id = enb_id_start; (enb_id < enb_id_end) ; enb_id++)
125
    {
126
#   if defined(OAI_EMU)
127
        if (oai_emulation.info.cli_start_enb[enb_id] == 1)
128
129
#   endif
        {
130
131
132
133
134
135
136
137
            s1ap_register_enb_req_t *s1ap_register_eNB;

            /* note:  there is an implicit relationship between the data structure and the message name */
            msg_p = itti_alloc_new_message (TASK_ENB_APP, S1AP_REGISTER_ENB_REQ);

            s1ap_register_eNB = &S1AP_REGISTER_ENB_REQ(msg_p);

            /* Some default/random parameters */
nikaeinn's avatar
nikaeinn committed
138
139
140
141
142
143
144
            s1ap_register_eNB->eNB_id           = enb_properties->properties[enb_id]->eNB_id;
            s1ap_register_eNB->cell_type        = enb_properties->properties[enb_id]->cell_type;
            s1ap_register_eNB->eNB_name         = enb_properties->properties[enb_id]->eNB_name;
            s1ap_register_eNB->tac              = enb_properties->properties[enb_id]->tac;
            s1ap_register_eNB->mcc              = enb_properties->properties[enb_id]->mcc;
            s1ap_register_eNB->mnc              = enb_properties->properties[enb_id]->mnc;
            s1ap_register_eNB->mnc_digit_length = enb_properties->properties[enb_id]->mnc_digit_length;
knopp's avatar
   
knopp committed
145
            s1ap_register_eNB->default_drx      = enb_properties->properties[enb_id]->pcch_defaultPagingCycle[0];
winckel's avatar
winckel committed
146
147

            s1ap_register_eNB->nb_mme =         enb_properties->properties[enb_id]->nb_mme;
148
            AssertFatal (s1ap_register_eNB->nb_mme <= S1AP_MAX_NB_MME_IP_ADDRESS, "Too many MME for eNB %d (%d/%d)!", enb_id, s1ap_register_eNB->nb_mme, S1AP_MAX_NB_MME_IP_ADDRESS);
149
150
151

            for (mme_id = 0; mme_id < s1ap_register_eNB->nb_mme; mme_id++)
            {
winckel's avatar
winckel committed
152
153
                s1ap_register_eNB->mme_ip_address[mme_id].ipv4 = enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv4;
                s1ap_register_eNB->mme_ip_address[mme_id].ipv6 = enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv6;
154
                strncpy (s1ap_register_eNB->mme_ip_address[mme_id].ipv4_address,
winckel's avatar
winckel committed
155
                         enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv4_address,
156
157
                         sizeof(s1ap_register_eNB->mme_ip_address[0].ipv4_address));
                strncpy (s1ap_register_eNB->mme_ip_address[mme_id].ipv6_address,
winckel's avatar
winckel committed
158
                         enb_properties->properties[enb_id]->mme_ip_address[mme_id].ipv6_address,
159
160
161
                         sizeof(s1ap_register_eNB->mme_ip_address[0].ipv6_address));
            }

gauthier's avatar
   
gauthier committed
162
163
164
165
166
167
            s1ap_register_eNB->enb_ip_address.ipv6 = 0;
            s1ap_register_eNB->enb_ip_address.ipv4 = 1;
            addr.s_addr = enb_properties->properties[enb_id]->enb_ipv4_address_for_S1_MME;
            str = inet_ntoa(addr);
            strcpy(s1ap_register_eNB->enb_ip_address.ipv4_address, str);

168
            itti_send_msg_to_task (TASK_S1AP, enb_id, msg_p);
169
170

            register_enb_pending++;
171
172
        }
    }
winckel's avatar
winckel committed
173

174
    return register_enb_pending;
winckel's avatar
winckel committed
175
176
177
178
}
# endif
#endif

179
180
181
/*------------------------------------------------------------------------------*/
void *eNB_app_task(void *args_p)
{
182
    const Enb_properties_array_t   *enb_properties_p  = NULL;
winckel's avatar
winckel committed
183
#if defined(ENABLE_ITTI)
winckel's avatar
winckel committed
184
185
186
    uint32_t                        enb_nb = 1; /* Default number of eNB is 1 */
    uint32_t                        enb_id_start = 0;
    uint32_t                        enb_id_end = enb_id_start + enb_nb;
winckel's avatar
winckel committed
187
# if defined(ENABLE_USE_MME)
winckel's avatar
winckel committed
188
189
190
    uint32_t                        register_enb_pending;
    uint32_t                        registered_enb;
    long                            enb_register_retry_timer_id;
191
# endif
winckel's avatar
winckel committed
192
    uint32_t                        enb_id;
gauthier's avatar
gauthier committed
193
194
    MessageDef                     *msg_p           = NULL;
    const char                     *msg_name        = NULL;
winckel's avatar
winckel committed
195
196
    instance_t                      instance;
    int                             result;
197

198
199
    itti_mark_task_ready (TASK_ENB_APP);

200
201
# if defined(ENABLE_USE_MME)
#   if defined(OAI_EMU)
202
203
204
205
206
207
208
    enb_nb =        oai_emulation.info.nb_enb_local;
    enb_id_start =  oai_emulation.info.first_enb_local;
    enb_id_end =    oai_emulation.info.first_enb_local + enb_nb;

    AssertFatal (enb_id_end <= NUMBER_OF_eNB_MAX,
                 "Last eNB index is greater or equal to maximum eNB index (%d/%d)!",
                 enb_id_end, NUMBER_OF_eNB_MAX);
209
#   endif
winckel's avatar
winckel committed
210
211
# endif

212
    enb_properties_p = enb_config_get();
213

214
    AssertFatal (enb_nb <= enb_properties_p->number,
215
                 "Number of eNB is greater than eNB defined in configuration file (%d/%d)!",
216
                 enb_nb, enb_properties_p->number);
217
218
219

    for (enb_id = enb_id_start; (enb_id < enb_id_end) ; enb_id++)
    {
220
221
        configure_phy(enb_id, enb_properties_p);
        configure_rrc(enb_id, enb_properties_p);
222
    }
winckel's avatar
winckel committed
223

winckel's avatar
winckel committed
224
# if defined(ENABLE_USE_MME)
225
226
    /* Try to register each eNB */
    registered_enb = 0;
227
    register_enb_pending = eNB_app_register (enb_id_start, enb_id_end, enb_properties_p);
winckel's avatar
winckel committed
228
# else
229
230
231
    /* Start L2L1 task */
    msg_p = itti_alloc_new_message(TASK_ENB_APP, INITIALIZE_MESSAGE);
    itti_send_msg_to_task(TASK_L2L1, INSTANCE_DEFAULT, msg_p);
winckel's avatar
winckel committed
232
233
# endif

234
235
236
237
    do
    {
        // Wait for a message
        itti_receive_msg (TASK_ENB_APP, &msg_p);
winckel's avatar
winckel committed
238

239
240
        msg_name = ITTI_MSG_NAME (msg_p);
        instance = ITTI_MSG_INSTANCE (msg_p);
winckel's avatar
winckel committed
241

242
243
244
245
246
        switch (ITTI_MSG_ID(msg_p))
        {
            case TERMINATE_MESSAGE:
                itti_exit_task ();
                break;
winckel's avatar
winckel committed
247

248
            case MESSAGE_TEST:
249
                LOG_I(ENB_APP, "Received %s\n", ITTI_MSG_NAME(msg_p));
250
                break;
winckel's avatar
winckel committed
251
252

# if defined(ENABLE_USE_MME)
253
            case S1AP_REGISTER_ENB_CNF:
254
255
                LOG_I(ENB_APP, "[eNB %d] Received %s: associated MME %d\n", instance, msg_name,
                      S1AP_REGISTER_ENB_CNF(msg_p).nb_mme);
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

                DevAssert(register_enb_pending > 0);
                register_enb_pending--;

                /* Check if at least eNB is registered with one MME */
                if (S1AP_REGISTER_ENB_CNF(msg_p).nb_mme > 0)
                {
                    registered_enb++;
                }

                /* Check if all register eNB requests have been processed */
                if (register_enb_pending == 0)
                {
                    if (registered_enb == enb_nb)
                    {
                        /* If all eNB are registered, start L2L1 task */
                        MessageDef *msg_init_p;

                        msg_init_p = itti_alloc_new_message (TASK_ENB_APP, INITIALIZE_MESSAGE);
                        itti_send_msg_to_task (TASK_L2L1, INSTANCE_DEFAULT, msg_init_p);
276
277

#   if defined(OAI_EMU)
278
                        /* Also inform all NAS UE tasks */
279
280
281
282
283
284
285
                        for (instance = NB_eNB_INST + oai_emulation.info.first_ue_local;
                            instance < (NB_eNB_INST + oai_emulation.info.first_ue_local + oai_emulation.info.nb_ue_local); instance ++)
                        {
                            msg_init_p = itti_alloc_new_message (TASK_ENB_APP, INITIALIZE_MESSAGE);
                            itti_send_msg_to_task (TASK_NAS_UE, instance, msg_init_p);
                        }
#   endif
286
287
288
289
290
                    }
                    else
                    {
                        uint32_t not_associated = enb_nb - registered_enb;

291
                        LOG_W(ENB_APP, " %d eNB %s not associated with a MME, retrying registration in %d seconds ...\n",
292
293
294
295
296
297
                              not_associated, not_associated > 1 ? "are" : "is", ENB_REGISTER_RETRY_DELAY);

                        /* Restart the eNB registration process in ENB_REGISTER_RETRY_DELAY seconds */
                        if (timer_setup (ENB_REGISTER_RETRY_DELAY, 0, TASK_ENB_APP, INSTANCE_DEFAULT, TIMER_ONE_SHOT,
                                         NULL, &enb_register_retry_timer_id) < 0)
                        {
298
                            LOG_E(ENB_APP, " Can not start eNB register retry timer, use \"sleep\" instead!\n");
299

300
                            sleep(ENB_REGISTER_RETRY_DELAY);
301
302
                            /* Restart the registration process */
                            registered_enb = 0;
303
                            register_enb_pending = eNB_app_register (enb_id_start, enb_id_end, enb_properties_p);
304
305
306
307
308
                        }
                    }
                }
                break;

309
310
311
312
313
314
315
            case S1AP_DEREGISTERED_ENB_IND:
                LOG_W(ENB_APP, "[eNB %d] Received %s: associated MME %d\n", instance, msg_name,
                      S1AP_DEREGISTERED_ENB_IND(msg_p).nb_mme);

                /* TODO handle recovering of registration */
                break;

316
            case TIMER_HAS_EXPIRED:
317
                LOG_I(ENB_APP, " Received %s: timer_id %d\n", msg_name, TIMER_HAS_EXPIRED(msg_p).timer_id);
318
319
320
321
322

                if (TIMER_HAS_EXPIRED (msg_p).timer_id == enb_register_retry_timer_id)
                {
                    /* Restart the registration process */
                    registered_enb = 0;
323
                    register_enb_pending = eNB_app_register (enb_id_start, enb_id_end, enb_properties_p);
324
325
                }
                break;
winckel's avatar
winckel committed
326
327
# endif

328
            default:
329
                LOG_E(ENB_APP, "Received unexpected message %s\n", msg_name);
330
331
                break;
        }
winckel's avatar
winckel committed
332

333
334
        result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
        AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
335
    } while (1);
winckel's avatar
winckel committed
336
337
#endif

338
    return NULL;
winckel's avatar
winckel committed
339
}