m2ap_eNB.c 29.4 KB
Newer Older
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
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * 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.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file m2ap_eNB.c
 * \brief m2ap tasks for eNB
 * \author Javier Morgade  <javier.morgade@ieee.org>
 * \date 2019
 * \version 0.1
 */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <arpa/inet.h>

#include "intertask_interface.h"

#include "m2ap_eNB.h"
#include "m2ap_eNB_defs.h"
#include "m2ap_eNB_management_procedures.h"
#include "m2ap_eNB_handler.h"
#include "m2ap_eNB_generate_messages.h"
#include "m2ap_common.h"
#include "m2ap_eNB_interface_management.h"
#include "m2ap_ids.h"
#include "m2ap_timers.h"

#include "queue.h"
#include "assertions.h"
#include "conversions.h"

struct m2ap_enb_map;
struct m2ap_eNB_data_s;

m2ap_setup_req_t * m2ap_enb_data_g;

RB_PROTOTYPE(m2ap_enb_map, m2ap_eNB_data_s, entry, m2ap_eNB_compare_assoc_id);

static
void m2ap_eNB_handle_sctp_data_ind(instance_t instance, sctp_data_ind_t *sctp_data_ind);

static
void m2ap_eNB_handle_sctp_association_resp(instance_t instance, sctp_new_association_resp_t *sctp_new_association_resp);

static
void m2ap_eNB_handle_sctp_association_ind(instance_t instance, sctp_new_association_ind_t *sctp_new_association_ind);

static
void m2ap_eNB_handle_register_eNB(instance_t instance,
                                  m2ap_register_enb_req_t *m2ap_register_eNB);
static
void m2ap_eNB_register_eNB(m2ap_eNB_instance_t *instance_p,
                           net_ip_address_t    *target_eNB_ip_addr,
                           net_ip_address_t    *local_ip_addr,
                           uint16_t             in_streams,
                           uint16_t             out_streams,
                           uint32_t             enb_port_for_M2C,
                           int                  multi_sd);

//static
//void m2ap_eNB_handle_handover_req(instance_t instance,
//                                  m2ap_handover_req_t *m2ap_handover_req);
//
//static
//void m2ap_eNB_handle_handover_req_ack(instance_t instance,
//                                      m2ap_handover_req_ack_t *m2ap_handover_req_ack);
//
//static
//void m2ap_eNB_ue_context_release(instance_t instance,
//                                 m2ap_ue_context_release_t *m2ap_ue_context_release);
//

static
void m2ap_eNB_handle_sctp_data_ind(instance_t instance, sctp_data_ind_t *sctp_data_ind) {
  int result;
  DevAssert(sctp_data_ind != NULL);
  m2ap_eNB_handle_message(instance, sctp_data_ind->assoc_id, sctp_data_ind->stream,
                          sctp_data_ind->buffer, sctp_data_ind->buffer_length);
  result = itti_free(TASK_UNKNOWN, sctp_data_ind->buffer);
  AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
}

static
void m2ap_eNB_handle_sctp_association_resp(instance_t instance, sctp_new_association_resp_t *sctp_new_association_resp) {
  m2ap_eNB_instance_t *instance_p;
  m2ap_eNB_data_t *m2ap_enb_data_p;
  DevAssert(sctp_new_association_resp != NULL);
 // printf("m2ap_eNB_handle_sctp_association_resp at 1\n");
 // dump_trees_m2();
  instance_p = m2ap_eNB_get_instance(instance);
  DevAssert(instance_p != NULL);

  /* if the assoc_id is already known, it is certainly because an IND was received
   * before. In this case, just update streams and return
   */
  if (sctp_new_association_resp->assoc_id != -1) {
    m2ap_enb_data_p = m2ap_get_eNB(instance_p, sctp_new_association_resp->assoc_id,
                                   sctp_new_association_resp->ulp_cnx_id);

    if (m2ap_enb_data_p != NULL) {
      /* some sanity check - to be refined at some point */
      if (sctp_new_association_resp->sctp_state != SCTP_STATE_ESTABLISHED) {
        M2AP_ERROR("m2ap_enb_data_p not NULL and sctp state not SCTP_STATE_ESTABLISHED, what to do?\n");
        abort();
      }

      m2ap_enb_data_p->in_streams  = sctp_new_association_resp->in_streams;
      m2ap_enb_data_p->out_streams = sctp_new_association_resp->out_streams;
      return;
    }
  }

  m2ap_enb_data_p = m2ap_get_eNB(instance_p, -1,
                                 sctp_new_association_resp->ulp_cnx_id);
  DevAssert(m2ap_enb_data_p != NULL);
  //printf("m2ap_eNB_handle_sctp_association_resp at 2\n");
  //dump_trees_m2();

  if (sctp_new_association_resp->sctp_state != SCTP_STATE_ESTABLISHED) {
139
    M2AP_WARN("Received unsuccessful result for SCTP association (%u), instance %ld, cnx_id %u\n",
140 141 142 143 144
              sctp_new_association_resp->sctp_state,
              instance,
              sctp_new_association_resp->ulp_cnx_id);
    //m2ap_handle_m2_setup_message(instance_p, m2ap_enb_data_p,
    //                             sctp_new_association_resp->sctp_state == SCTP_STATE_SHUTDOWN);
Javier Morgade's avatar
Javier Morgade committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	  sleep(4);
	  int index;
	  /* Trying to connect to the provided list of eNB ip address */
	  for (index = 0; index < instance_p->nb_m2; index++) {
	    //M2AP_INFO("eNB[%d] eNB id %u acting as an initiator (client)\n",
	     //         instance_id, instance->eNB_id);
	    m2ap_eNB_register_eNB(instance_p,
				  &instance_p->target_mce_m2_ip_address[index],
				  &instance_p->enb_m2_ip_address,
				  instance_p->sctp_in_streams,
				  instance_p->sctp_out_streams,
				  instance_p->enb_port_for_M2C,
				  instance_p->multi_sd);
	  }

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
    return;
  }

  //printf("m2ap_eNB_handle_sctp_association_resp at 3\n");
  //dump_trees_m2();
  /* Update parameters */
  m2ap_enb_data_p->assoc_id    = sctp_new_association_resp->assoc_id;
  m2ap_enb_data_p->in_streams  = sctp_new_association_resp->in_streams;
  m2ap_enb_data_p->out_streams = sctp_new_association_resp->out_streams;
  //printf("m2ap_eNB_handle_sctp_association_resp at 4\n");
  //dump_trees_m2();

  m2ap_enb_data_g->assoc_id    = sctp_new_association_resp->assoc_id;
  m2ap_enb_data_g->sctp_in_streams  = sctp_new_association_resp->in_streams;
  m2ap_enb_data_g->sctp_out_streams = sctp_new_association_resp->out_streams;

  /* Prepare new m2 Setup Request */
  //m2ap_eNB_generate_m2_setup_request(instance_p, m2ap_enb_data_p);
  eNB_send_M2_SETUP_REQUEST(instance_p, m2ap_enb_data_p);
  //eNB_send_M2_SETUP_REQUEST(instance_p, m2ap_enb_data_g);
  
}

static
void m2ap_eNB_handle_sctp_association_ind(instance_t instance, sctp_new_association_ind_t *sctp_new_association_ind) {
  m2ap_eNB_instance_t *instance_p;
  m2ap_eNB_data_t *m2ap_enb_data_p;
  //printf("m2ap_eNB_handle_sctp_association_ind at 1 (called for instance %d)\n", instance);
  //dump_trees_m2();
  DevAssert(sctp_new_association_ind != NULL);
  instance_p = m2ap_eNB_get_instance(instance);
  DevAssert(instance_p != NULL);
  m2ap_enb_data_p = m2ap_get_eNB(instance_p, sctp_new_association_ind->assoc_id, -1);

  if (m2ap_enb_data_p != NULL) abort();

  //  DevAssert(m2ap_enb_data_p != NULL);
  if (m2ap_enb_data_p == NULL) {
    /* Create new eNB descriptor */
    m2ap_enb_data_p = calloc(1, sizeof(*m2ap_enb_data_p));
    DevAssert(m2ap_enb_data_p != NULL);
    m2ap_enb_data_p->cnx_id                = m2ap_eNB_fetch_add_global_cnx_id();
    m2ap_enb_data_p->m2ap_eNB_instance = instance_p;
    /* Insert the new descriptor in list of known eNB
     * but not yet associated.
     */
    RB_INSERT(m2ap_enb_map, &instance_p->m2ap_enb_head, m2ap_enb_data_p);
    m2ap_enb_data_p->state = M2AP_ENB_STATE_CONNECTED;
    instance_p->m2_target_enb_nb++;

    if (instance_p->m2_target_enb_pending_nb > 0) {
      instance_p->m2_target_enb_pending_nb--;
    }
  } else {
    M2AP_WARN("m2ap_enb_data_p already exists\n");
  }

  //printf("m2ap_eNB_handle_sctp_association_ind at 2\n");
  //dump_trees_m2();
  /* Update parameters */
  m2ap_enb_data_p->assoc_id    = sctp_new_association_ind->assoc_id;
  m2ap_enb_data_p->in_streams  = sctp_new_association_ind->in_streams;
  m2ap_enb_data_p->out_streams = sctp_new_association_ind->out_streams;
  //printf("m2ap_eNB_handle_sctp_association_ind at 3\n");
  //dump_trees_m2();
}

int m2ap_eNB_init_sctp (m2ap_eNB_instance_t *instance_p,
                        net_ip_address_t    *local_ip_addr,
                        uint32_t enb_port_for_M2C) {
  // Create and alloc new message
  MessageDef                             *message;
  sctp_init_t                            *sctp_init  = NULL;
  DevAssert(instance_p != NULL);
  DevAssert(local_ip_addr != NULL);
236
  message = itti_alloc_new_message (TASK_M2AP_ENB, 0, SCTP_INIT_MSG_MULTI_REQ);
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
  sctp_init = &message->ittiMsg.sctp_init_multi;
  sctp_init->port = enb_port_for_M2C;
  sctp_init->ppid = M2AP_SCTP_PPID;
  sctp_init->ipv4 = 1;
  sctp_init->ipv6 = 0;
  sctp_init->nb_ipv4_addr = 1;
#if 0
  memcpy(&sctp_init->ipv4_address,
         local_ip_addr,
         sizeof(*local_ip_addr));
#endif
  sctp_init->ipv4_address[0] = inet_addr(local_ip_addr->ipv4_address);
  /*
   * SR WARNING: ipv6 multi-homing fails sometimes for localhost.
   * * * * Disable it for now.
   */
  sctp_init->nb_ipv6_addr = 0;
  sctp_init->ipv6_address[0] = "0:0:0:0:0:0:0:1";
  return itti_send_msg_to_task (TASK_SCTP, instance_p->instance, message);
}

static void m2ap_eNB_register_eNB(m2ap_eNB_instance_t *instance_p,
                                  net_ip_address_t    *target_eNB_ip_address,
                                  net_ip_address_t    *local_ip_addr,
                                  uint16_t             in_streams,
                                  uint16_t             out_streams,
                                  uint32_t         enb_port_for_M2C,
                                  int                  multi_sd) {
  MessageDef                       *message                   = NULL;
  sctp_new_association_req_multi_t *sctp_new_association_req  = NULL;
  m2ap_eNB_data_t                  *m2ap_enb_data             = NULL;
  DevAssert(instance_p != NULL);
  DevAssert(target_eNB_ip_address != NULL);
270
  message = itti_alloc_new_message(TASK_M2AP_ENB, 0, SCTP_NEW_ASSOCIATION_REQ_MULTI);
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
  sctp_new_association_req = &message->ittiMsg.sctp_new_association_req_multi;
  sctp_new_association_req->port = enb_port_for_M2C;
  sctp_new_association_req->ppid = M2AP_SCTP_PPID;
  sctp_new_association_req->in_streams  = in_streams;
  sctp_new_association_req->out_streams = out_streams;
  sctp_new_association_req->multi_sd = multi_sd;
  memcpy(&sctp_new_association_req->remote_address,
         target_eNB_ip_address,
         sizeof(*target_eNB_ip_address));
  memcpy(&sctp_new_association_req->local_address,
         local_ip_addr,
         sizeof(*local_ip_addr));
  /* Create new eNB descriptor */
  m2ap_enb_data = calloc(1, sizeof(*m2ap_enb_data));
  DevAssert(m2ap_enb_data != NULL);
  m2ap_enb_data->cnx_id                = m2ap_eNB_fetch_add_global_cnx_id();
  sctp_new_association_req->ulp_cnx_id = m2ap_enb_data->cnx_id;
  m2ap_enb_data->assoc_id          = -1;
  m2ap_enb_data->m2ap_eNB_instance = instance_p;


  m2ap_enb_data_g = (m2ap_setup_req_t*)calloc(1,sizeof(m2ap_setup_req_t));
  

  //
  m2ap_enb_data->eNB_name = "enb_name";
  /* Insert the new descriptor in list of known eNB
   * but not yet associated.
   */
  RB_INSERT(m2ap_enb_map, &instance_p->m2ap_enb_head, m2ap_enb_data);
  m2ap_enb_data->state = M2AP_ENB_STATE_WAITING;
  instance_p->m2_target_enb_nb ++;
  instance_p->m2_target_enb_pending_nb ++;
  itti_send_msg_to_task(TASK_SCTP, instance_p->instance, message);
}

static
void m2ap_eNB_handle_register_eNB(instance_t instance,
                                  m2ap_register_enb_req_t *m2ap_register_eNB) {
  m2ap_eNB_instance_t *new_instance;
  DevAssert(m2ap_register_eNB != NULL);
  /* Look if the provided instance already exists */
  new_instance = m2ap_eNB_get_instance(instance);

  if (new_instance != NULL) {
    /* Checks if it is a retry on the same eNB */
    DevCheck(new_instance->eNB_id == m2ap_register_eNB->eNB_id, new_instance->eNB_id, m2ap_register_eNB->eNB_id, 0);
    DevCheck(new_instance->cell_type == m2ap_register_eNB->cell_type, new_instance->cell_type, m2ap_register_eNB->cell_type, 0);
    DevCheck(new_instance->tac == m2ap_register_eNB->tac, new_instance->tac, m2ap_register_eNB->tac, 0);
    DevCheck(new_instance->mcc == m2ap_register_eNB->mcc, new_instance->mcc, m2ap_register_eNB->mcc, 0);
    DevCheck(new_instance->mnc == m2ap_register_eNB->mnc, new_instance->mnc, m2ap_register_eNB->mnc, 0);
322
    M2AP_WARN("eNB[%ld] already registered\n", instance);
Javier Morgade's avatar
Javier Morgade committed
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
  } else {
    new_instance = calloc(1, sizeof(m2ap_eNB_instance_t));
    DevAssert(new_instance != NULL);
    RB_INIT(&new_instance->m2ap_enb_head);
    /* Copy usefull parameters */
    new_instance->instance         = instance;
    new_instance->eNB_name         = m2ap_register_eNB->eNB_name;
    new_instance->eNB_id           = m2ap_register_eNB->eNB_id;
    new_instance->cell_type        = m2ap_register_eNB->cell_type;
    new_instance->tac              = m2ap_register_eNB->tac;
    new_instance->mcc              = m2ap_register_eNB->mcc;
    new_instance->mnc              = m2ap_register_eNB->mnc;
    new_instance->mnc_digit_length = m2ap_register_eNB->mnc_digit_length;
    new_instance->num_cc           = m2ap_register_eNB->num_cc;

    m2ap_id_manager_init(&new_instance->id_manager);
    m2ap_timers_init(&new_instance->timers,
                     m2ap_register_eNB->t_reloc_prep,
                     m2ap_register_eNB->tm2_reloc_overall);

    for (int i = 0; i< m2ap_register_eNB->num_cc; i++) {
      new_instance->eutra_band[i]              = m2ap_register_eNB->eutra_band[i];
      new_instance->downlink_frequency[i]      = m2ap_register_eNB->downlink_frequency[i];
      new_instance->uplink_frequency_offset[i] = m2ap_register_eNB->uplink_frequency_offset[i];
      new_instance->Nid_cell[i]                = m2ap_register_eNB->Nid_cell[i];
      new_instance->N_RB_DL[i]                 = m2ap_register_eNB->N_RB_DL[i];
      new_instance->frame_type[i]              = m2ap_register_eNB->frame_type[i];
      new_instance->fdd_earfcn_DL[i]           = m2ap_register_eNB->fdd_earfcn_DL[i];
      new_instance->fdd_earfcn_UL[i]           = m2ap_register_eNB->fdd_earfcn_UL[i];
    }

    DevCheck(m2ap_register_eNB->nb_m2 <= M2AP_MAX_NB_ENB_IP_ADDRESS,
             M2AP_MAX_NB_ENB_IP_ADDRESS, m2ap_register_eNB->nb_m2, 0);
    memcpy(new_instance->target_mce_m2_ip_address,
           m2ap_register_eNB->target_mce_m2_ip_address,
           m2ap_register_eNB->nb_m2 * sizeof(net_ip_address_t));
    new_instance->nb_m2             = m2ap_register_eNB->nb_m2;
    new_instance->enb_m2_ip_address = m2ap_register_eNB->enb_m2_ip_address;
    new_instance->sctp_in_streams   = m2ap_register_eNB->sctp_in_streams;
    new_instance->sctp_out_streams  = m2ap_register_eNB->sctp_out_streams;
    new_instance->enb_port_for_M2C  = m2ap_register_eNB->enb_port_for_M2C;


    new_instance->num_mbms_configuration_data_list = m2ap_register_eNB->num_mbms_configuration_data_list;
    for(int j=0; j < m2ap_register_eNB->num_mbms_configuration_data_list;j++){
	    new_instance->mbms_configuration_data_list[j].num_mbms_service_area_list = m2ap_register_eNB->mbms_configuration_data_list[j].num_mbms_service_area_list;
	    for(int i=0; i <  m2ap_register_eNB->mbms_configuration_data_list[j].num_mbms_service_area_list; i++ ){
		//strcpy(&new_instance->mbms_configuration_data_list[j].mbms_service_area_list[i],&m2ap_register_eNB->mbms_configuration_data_list[j].mbms_service_area_list[i]);
		new_instance->mbms_configuration_data_list[j].mbms_service_area_list[i]=m2ap_register_eNB->mbms_configuration_data_list[j].mbms_service_area_list[i];
	    }
    }

    /* Add the new instance to the list of eNB (meaningfull in virtual mode) */
    m2ap_eNB_insert_new_instance(new_instance);
379
    M2AP_INFO("Registered new eNB[%ld] and %s eNB id %u\n",
380 381 382 383 384 385 386 387 388 389
              instance,
              m2ap_register_eNB->cell_type == CELL_MACRO_ENB ? "macro" : "home",
              m2ap_register_eNB->eNB_id);

    /* initiate the SCTP listener */
    if (m2ap_eNB_init_sctp(new_instance,&m2ap_register_eNB->enb_m2_ip_address,m2ap_register_eNB->enb_port_for_M2C) <  0 ) {
      M2AP_ERROR ("Error while sending SCTP_INIT_MSG to SCTP \n");
      return;
    }

390
    M2AP_INFO("eNB[%ld] eNB id %u acting as a listner (server)\n",
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
              instance, m2ap_register_eNB->eNB_id);
  }
}

static
void m2ap_eNB_handle_sctp_init_msg_multi_cnf(
  instance_t instance_id,
  sctp_init_msg_multi_cnf_t *m) {
  m2ap_eNB_instance_t *instance;
  int index;
  DevAssert(m != NULL);
  instance = m2ap_eNB_get_instance(instance_id);
  DevAssert(instance != NULL);
  instance->multi_sd = m->multi_sd;

  /* Exit if CNF message reports failure.
   * Failure means multi_sd < 0.
   */
  if (instance->multi_sd < 0) {
    M2AP_ERROR("Error: be sure to properly configure M2 in your configuration file.\n");
    DevAssert(instance->multi_sd >= 0);
  }

  /* Trying to connect to the provided list of eNB ip address */

  for (index = 0; index < instance->nb_m2; index++) {
417
    M2AP_INFO("eNB[%ld] eNB id %u acting as an initiator (client)\n",
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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
              instance_id, instance->eNB_id);
    m2ap_eNB_register_eNB(instance,
                          &instance->target_mce_m2_ip_address[index],
                          &instance->enb_m2_ip_address,
                          instance->sctp_in_streams,
                          instance->sctp_out_streams,
                          instance->enb_port_for_M2C,
                          instance->multi_sd);
  }
}

//static
//void m2ap_eNB_handle_handover_req(instance_t instance,
//                                  m2ap_handover_req_t *m2ap_handover_req)
//{
//  m2ap_eNB_instance_t *instance_p;
//  m2ap_eNB_data_t     *target;
//  m2ap_id_manager     *id_manager;
//  int                 ue_id;
//
//  int target_pci = m2ap_handover_req->target_physCellId;
//
//  instance_p = m2ap_eNB_get_instance(instance);
//  DevAssert(instance_p != NULL);
//
//  target = m2ap_is_eNB_pci_in_list(target_pci);
//  DevAssert(target != NULL);
//
//  /* allocate m2ap ID */
//  id_manager = &instance_p->id_manager;
//  ue_id = m2ap_allocate_new_id(id_manager);
//  if (ue_id == -1) {
//    M2AP_ERROR("could not allocate a new M2AP UE ID\n");
//    /* TODO: cancel handover: send (to be defined) message to RRC */
//    exit(1);
//  }
//  /* id_source is ue_id, id_target is unknown yet */
//  m2ap_set_ids(id_manager, ue_id, m2ap_handover_req->rnti, ue_id, -1);
//  m2ap_id_set_state(id_manager, ue_id, M2ID_STATE_SOURCE_PREPARE);
//  m2ap_set_reloc_prep_timer(id_manager, ue_id,
//                            m2ap_timer_get_tti(&instance_p->timers));
//  m2ap_id_set_target(id_manager, ue_id, target);
//
//  m2ap_eNB_generate_m2_handover_request(instance_p, target, m2ap_handover_req, ue_id);
//}

//static
//void m2ap_eNB_handle_handover_req_ack(instance_t instance,
//                                      m2ap_handover_req_ack_t *m2ap_handover_req_ack)
//{
//  /* TODO: remove this hack (the goal is to find the correct
//   * eNodeB structure for the other end) - we need a proper way for RRC
//   * and M2AP to identify eNodeBs
//   * RRC knows about mod_id and M2AP knows about eNB_id (eNB_ID in
//   * the configuration file)
//   * as far as I understand.. CROUX
//   */
//  m2ap_eNB_instance_t *instance_p;
//  m2ap_eNB_data_t     *target;
//  int source_assoc_id = m2ap_handover_req_ack->source_assoc_id;
//  int                 ue_id;
//  int                 id_source;
//  int                 id_target;
//
//  instance_p = m2ap_eNB_get_instance(instance);
//  DevAssert(instance_p != NULL);
//
//  target = m2ap_get_eNB(NULL, source_assoc_id, 0);
//  DevAssert(target != NULL);
//
//  /* rnti is a new information, save it */
//  ue_id     = m2ap_handover_req_ack->m2_id_target;
//  id_source = m2ap_id_get_id_source(&instance_p->id_manager, ue_id);
//  id_target = ue_id;
//  m2ap_set_ids(&instance_p->id_manager, ue_id, m2ap_handover_req_ack->rnti, id_source, id_target);
//
//  m2ap_eNB_generate_m2_handover_request_ack(instance_p, target, m2ap_handover_req_ack);
//}
//
//static
//void m2ap_eNB_ue_context_release(instance_t instance,
//                                 m2ap_ue_context_release_t *m2ap_ue_context_release)
//{
//  m2ap_eNB_instance_t *instance_p;
//  m2ap_eNB_data_t     *target;
//  int source_assoc_id = m2ap_ue_context_release->source_assoc_id;
//  int ue_id;
//  instance_p = m2ap_eNB_get_instance(instance);
//  DevAssert(instance_p != NULL);
//
//  target = m2ap_get_eNB(NULL, source_assoc_id, 0);
//  DevAssert(target != NULL);
//
//  m2ap_eNB_generate_m2_ue_context_release(instance_p, target, m2ap_ue_context_release);
//
//  /* free the M2AP UE ID */
//  ue_id = m2ap_find_id_from_rnti(&instance_p->id_manager, m2ap_ue_context_release->rnti);
//  if (ue_id == -1) {
//    M2AP_ERROR("could not find UE %x\n", m2ap_ue_context_release->rnti);
//    exit(1);
//  }
//  m2ap_release_id(&instance_p->id_manager, ue_id);
//}

//void MCE_task_send_sctp_init_req(instance_t enb_id) {
//  // 1. get the itti msg, and retrive the enb_id from the message
//  // 2. use RC.rrc[enb_id] to fill the sctp_init_t with the ip, port
//  // 3. creat an itti message to init
//
//  LOG_I(M2AP, "M2AP_SCTP_REQ(create socket)\n");
//  MessageDef  *message_p = NULL;
//
530
//  message_p = itti_alloc_new_message (M2AP, 0, SCTP_INIT_MSG);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
//  message_p->ittiMsg.sctp_init.port = M2AP_PORT_NUMBER;
//  message_p->ittiMsg.sctp_init.ppid = M2AP_SCTP_PPID;
//  message_p->ittiMsg.sctp_init.ipv4 = 1;
//  message_p->ittiMsg.sctp_init.ipv6 = 0;
//  message_p->ittiMsg.sctp_init.nb_ipv4_addr = 1;
//  //message_p->ittiMsg.sctp_init.ipv4_address[0] = inet_addr(RC.rrc[enb_id]->eth_params_s.my_addr);
//  message_p->ittiMsg.sctp_init.ipv4_address[0] = inet_addr("127.0.0.7");
//  /*
//   * SR WARNING: ipv6 multi-homing fails sometimes for localhost.
//   * * * * Disable it for now.
//   */
//  message_p->ittiMsg.sctp_init.nb_ipv6_addr = 0;
//  message_p->ittiMsg.sctp_init.ipv6_address[0] = "0:0:0:0:0:0:0:1";
//
//  itti_send_msg_to_task(TASK_SCTP, enb_id, message_p);
//}
//
void *m2ap_eNB_task(void *arg) {
  MessageDef *received_msg = NULL;
  int         result;
  M2AP_DEBUG("Starting M2AP layer\n");
  m2ap_eNB_prepare_internal_data();

  itti_mark_task_ready(TASK_M2AP_ENB);

 // MCE_task_send_sctp_init_req(0);

  while (1) {
    itti_receive_msg(TASK_M2AP_ENB, &received_msg);

    switch (ITTI_MSG_ID(received_msg)) {
      case MESSAGE_TEST:
	LOG_D(M2AP,"eNB Received MESSAGE_TEST Message %s\n",itti_get_task_name(ITTI_MSG_ORIGIN_ID(received_msg)));
564
	//MessageDef * message_p = itti_alloc_new_message(TASK_M2AP_ENB, 0, MESSAGE_TEST);
565 566 567 568 569 570 571 572
        //itti_send_msg_to_task(TASK_M3AP, 1/*ctxt_pP->module_id*/, message_p);
	break;
      case TERMINATE_MESSAGE:
        M2AP_WARN(" *** Exiting M2AP thread\n");
        itti_exit_task();
        break;

      case M2AP_SUBFRAME_PROCESS:
573
        m2ap_check_timers(ITTI_MSG_DESTINATION_INSTANCE(received_msg));
574 575 576 577
        break;

      case M2AP_REGISTER_ENB_REQ:
	LOG_I(M2AP,"eNB Received M2AP_REGISTER_ENB_REQ Message\n");
578
        m2ap_eNB_handle_register_eNB(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
579 580 581 582 583 584
                                     &M2AP_REGISTER_ENB_REQ(received_msg));
        break;


      case M2AP_MBMS_SCHEDULING_INFORMATION_RESP:
	LOG_I(M2AP,"eNB M2AP_MBMS_SCHEDULING_INFORMATION_RESP Message\n");
585
        eNB_send_MBMS_SCHEDULING_INFORMATION_RESPONSE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
586 587 588 589
                                     &M2AP_MBMS_SCHEDULING_INFORMATION_RESP(received_msg));
	break;
      case M2AP_MBMS_SESSION_START_RESP:
	LOG_I(M2AP,"eNB M2AP_MBMS_SESSION_START_RESP Message\n");
590
        eNB_send_MBMS_SESSION_START_RESPONSE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
591 592 593 594
                                     &M2AP_MBMS_SESSION_START_RESP(received_msg));
	break;
      case M2AP_MBMS_SESSION_START_FAILURE:
	LOG_I(M2AP,"eNB M2AP_MBMS_SESSION_START_FAILURE Message\n");
595
        eNB_send_MBMS_SESSION_START_FAILURE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
596 597 598 599
                                     &M2AP_MBMS_SESSION_START_FAILURE(received_msg));
	break;
      case M2AP_MBMS_SESSION_STOP_RESP:
	LOG_I(M2AP,"eNB M2AP_MBMS_SESSION_STOP_RESP Message\n");
600
        eNB_send_MBMS_SESSION_STOP_RESPONSE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
601 602 603 604
                                     &M2AP_MBMS_SESSION_STOP_RESP(received_msg));
	break;
      case M2AP_ENB_CONFIGURATION_UPDATE:
	LOG_I(M2AP,"eNB M2AP_ENB_CONFIGURATION_UPDATE Message\n");
605
        eNB_send_eNB_CONFIGURATION_UPDATE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
606 607 608 609
                                     &M2AP_ENB_CONFIGURATION_UPDATE(received_msg));
	break;
      case M2AP_MCE_CONFIGURATION_UPDATE_ACK:
	LOG_I(M2AP,"eNB M2AP_MCE_CONFIGURATION_UPDATE_ACK Message\n");
610
        //eNB_send_MCE_CONFIGURATION_UPDATE_ACKNOWLEDGE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
611 612 613 614
         //                            &M2AP_MCE_CONFIGURATION_UPDATE_ACK(received_msg));
	break;
      case M2AP_MCE_CONFIGURATION_UPDATE_FAILURE:
	LOG_I(M2AP,"eNB M2AP_MCE_CONFIGURATION_UPDATE_FAILURE Message\n");
615
        //(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
616 617 618 619
                                     //&M2AP_MCE_CONFIGURATION_UPDATE_FAILURE(received_msg));
	break;
      case M2AP_MBMS_SESSION_UPDATE_RESP:
	LOG_I(M2AP,"eNB M2AP_MBMS_SESSION_UPDATE_RESP Message\n");
620
        eNB_send_MBMS_SESSION_UPDATE_RESPONSE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
621 622 623 624
                                     &M2AP_MBMS_SESSION_UPDATE_RESP(received_msg));
	break;
      case M2AP_MBMS_SESSION_UPDATE_FAILURE:
	LOG_I(M2AP,"eNB M2AP_MBMS_SESSION_UPDATE_FAILURE Message\n");
625
        eNB_send_MBMS_SESSION_UPDATE_FAILURE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
626 627 628 629
                                     &M2AP_MBMS_SESSION_UPDATE_FAILURE(received_msg));
	break;
      case M2AP_MBMS_SERVICE_COUNTING_REPORT:
	LOG_I(M2AP,"eNB M2AP_MBMS_SERVICE_COUNTING_REPORT Message\n");
630
        eNB_send_MBMS_SERVICE_COUNTING_REPORT(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
631 632 633 634
                                     &M2AP_MBMS_SERVICE_COUNTING_REPORT(received_msg));
	break;
      case M2AP_MBMS_OVERLOAD_NOTIFICATION:
	LOG_I(M2AP,"eNB M2AP_MBMS_OVERLOAD_NOTIFICATION Message\n");
635
        eNB_send_MBMS_OVERLOAD_NOTIFICATION(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
636 637 638 639
                                     &M2AP_MBMS_OVERLOAD_NOTIFICATION(received_msg));
	break;
      case M2AP_MBMS_SERVICE_COUNTING_RESP:
	LOG_I(M2AP,"eNB M2AP_MBMS_SERVICE_COUNTING_RESP Message\n");
640
        eNB_send_MBMS_SERVICE_COUNTING_RESP(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
641 642 643 644
                                     &M2AP_MBMS_SERVICE_COUNTING_RESP(received_msg));
	break;
      case M2AP_MBMS_SERVICE_COUNTING_FAILURE:
	LOG_I(M2AP,"eNB  Message\n");
645
        eNB_send_MBMS_SERVICE_COUNTING_FAILURE(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
646 647 648 649 650 651
                                     &M2AP_MBMS_SERVICE_COUNTING_FAILURE(received_msg));
	break;


      case SCTP_INIT_MSG_MULTI_CNF:
	LOG_I(M2AP,"eNB Received SCTP_INIT_MSG_MULTI_CNF Message\n");
652
        m2ap_eNB_handle_sctp_init_msg_multi_cnf(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
653 654 655 656 657
                                                &received_msg->ittiMsg.sctp_init_msg_multi_cnf);
        break;

      case SCTP_NEW_ASSOCIATION_RESP:
	LOG_I(M2AP,"eNB Received SCTP_NEW_ASSOCIATION_RESP Message\n");
658
        m2ap_eNB_handle_sctp_association_resp(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
659 660 661 662 663
                                              &received_msg->ittiMsg.sctp_new_association_resp);
        break;

      case SCTP_NEW_ASSOCIATION_IND:
	LOG_I(M2AP,"eNB Received SCTP_NEW_ASSOCIATION Message\n");
664
        m2ap_eNB_handle_sctp_association_ind(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
665 666 667 668 669
                                             &received_msg->ittiMsg.sctp_new_association_ind);
        break;

      case SCTP_DATA_IND:
	LOG_I(M2AP,"eNB Received SCTP_DATA_IND Message\n");
670
        m2ap_eNB_handle_sctp_data_ind(ITTI_MSG_DESTINATION_INSTANCE(received_msg),
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
                                      &received_msg->ittiMsg.sctp_data_ind);
        break;

      default:
        M2AP_ERROR("eNB Received unhandled message: %d:%s\n",
                   ITTI_MSG_ID(received_msg), ITTI_MSG_NAME(received_msg));
        break;
    }

    result = itti_free (ITTI_MSG_ORIGIN_ID(received_msg), received_msg);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
    received_msg = NULL;
  }

  return NULL;
}

#include "common/config/config_userapi.h"

int is_m2ap_eNB_enabled(void)
{
  static volatile int config_loaded = 0;
  static volatile int enabled = 0;
  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

  if (pthread_mutex_lock(&mutex)) goto mutex_error;

  if (config_loaded) {
    if (pthread_mutex_unlock(&mutex)) goto mutex_error;
    return enabled;
  }

  char *enable_m2 = NULL;
  paramdef_t p[] = {
   { "enable_enb_m2", "yes/no", 0, strptr:&enable_m2, defstrval:"", TYPE_STRING, 0 }
  };

  /* TODO: do it per module - we check only first eNB */
  config_get(p, sizeof(p)/sizeof(paramdef_t), "eNBs.[0]");
  if (enable_m2 != NULL && strcmp(enable_m2, "yes") == 0)
    enabled = 1;

  config_loaded = 1;

  if (pthread_mutex_unlock(&mutex)) goto mutex_error;

  return enabled;

mutex_error:
  LOG_E(M2AP, "mutex error\n");
  exit(1);
}