flexran_agent.c 8.44 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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
 */ 
21

22 23
/*! \file flexran_agent.h
 * \brief top level flexran agent receive thread and itti task
24 25
 * \author Xenofon Foukas and Navid Nikaein and shahab SHARIAT BAGHERI
 * \date 2017
26 27 28
 * \version 0.1
 */

29
#define _GNU_SOURCE
30
#include "flexran_agent.h"
laurent's avatar
laurent committed
31
#include <common/utils/system.h>
32

33
#include <pthread.h>
34 35
#include <arpa/inet.h>

36
int agent_task_created = 0;
37 38 39 40
/* 
 * enb agent task mainly wakes up the tx thread for periodic and oneshot messages to the controller 
 * and can interact with other itti tasks
*/
41
void *flexran_agent_task(void *args){
42 43
  MessageDef *msg_p = NULL;
  int         result;
44

45
  itti_mark_task_ready(TASK_FLEXRAN_AGENT);
46 47 48

  do {
    // Wait for a message
49
    itti_receive_msg (TASK_FLEXRAN_AGENT, &msg_p);
50 51 52 53
    DevAssert(msg_p != NULL);

    switch (ITTI_MSG_ID(msg_p)) {
    case TERMINATE_MESSAGE:
54
      LOG_W(FLEXRAN_AGENT, " *** Exiting FLEXRAN thread\n");
55 56 57 58
      itti_exit_task ();
      break;

    case MESSAGE_TEST:
59
      LOG_I(FLEXRAN_AGENT, "Received %s\n", ITTI_MSG_NAME(msg_p));
60 61 62
      break;

    default:
63
      LOG_E(FLEXRAN_AGENT, "Received unexpected message %s\n", ITTI_MSG_NAME (msg_p));
64 65 66 67 68 69
      break;
    }

    result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
  } while (1);
70

71 72
  return NULL;
}
73

74
void *receive_thread(void *args) {
75

76
  flexran_agent_info_t  *d = args;
77 78 79
  void                  *data;
  int                   size;
  int                   priority;
80
  err_code_t             err_code=0;
81

82
  Protocol__FlexranMessage *msg;
83
  pthread_setname_np(pthread_self(), "flexran_rx_thr");
84 85
  
  while (1) {
86

87
    while ((size = flexran_agent_msg_recv(d->mod_id, FLEXRAN_AGENT_DEFAULT, &data, &priority)) > 0) {
88
      
89
      LOG_D(FLEXRAN_AGENT,"received message with size %d\n", size);
90
  
91
      // Invoke the message handler
92
      msg=flexran_agent_handle_message(d->mod_id, data, size);
93

94
      free(data);
95
    
96 97
      // check if there is something to send back to the controller
      if (msg != NULL){
98
	data=flexran_agent_pack_message(msg,&size);
99

100
	if (flexran_agent_msg_send(d->mod_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
101
	  err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
102 103
	  goto error;
	}
104
      
105
	LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
106
      } 
107 108
    }
  }
109
    
110 111 112
  return NULL;

error:
113 114
  if (err_code != 0)
     LOG_E(FLEXRAN_AGENT,"receive_thread: error %d occured\n",err_code);
115 116 117
  return NULL;
}

118 119 120
int channel_container_init = 0;
int flexran_agent_start(mid_t mod_id)
{
121
  flexran_agent_info_t *flexran = RC.flexran[mod_id];
122
  int channel_id;
123 124
  char *in_ip = flexran->remote_ipv4_addr;
  uint16_t in_port = flexran->remote_port;
125 126 127 128 129 130

  /* if this agent is disabled, return and don't do anything */
  if (!flexran->enabled) {
    LOG_I(FLEXRAN_AGENT, "FlexRAN Agent for eNB %d is DISABLED\n", mod_id);
    return 100;
  }
131

132 133 134
  /*
   * Initialize the channel container
   */
135
  if (!channel_container_init) {
136
    flexran_agent_init_channel_container();
137 138
    channel_container_init = 1;
  }
139
  /*Create the async channel info*/
140
  flexran_agent_async_channel_t *channel_info = flexran_agent_async_channel_info(mod_id, in_ip, in_port);
141

142 143 144 145 146
  if (!channel_info) {
    LOG_E(FLEXRAN_AGENT, "could not create channel_info\n");
    exit(1);
  }

147
  /*Create a channel using the async channel info*/
148 149 150 151
  channel_id = flexran_agent_create_channel((void *) channel_info, 
					flexran_agent_async_msg_send, 
					flexran_agent_async_msg_recv,
					flexran_agent_async_release);
152 153

  
154
  if (channel_id <= 0) {
155
    LOG_E(FLEXRAN_AGENT, "could not create channel\n");
156 157 158
    goto error;
  }

159
  flexran_agent_channel_t *channel = get_channel(channel_id);
160
  
161
  if (channel == NULL) {
162
    LOG_E(FLEXRAN_AGENT, "could not get channel for channel_id %d\n", channel_id);
163 164 165
    goto error;
  }

166 167
  /*Register the channel for all underlying agents (use FLEXRAN_AGENT_MAX)*/
  flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAX);
168 169

  /*Example of registration for a specific agent(MAC):
170
   *flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAC);
171 172
   */

laurent's avatar
laurent committed
173
  pthread_t t; 
174
  threadCreate(&t, receive_thread, flexran, "flexran", -1, OAI_PRIORITY_RT);
175

176 177 178
  /* Register and initialize the control modules depending on capabilities.
   * After registering, calling flexran_agent_get_*_xface() tells whether a
   * control module is operational */
179 180
  uint32_t caps = flexran_get_capabilities_mask(mod_id);
  LOG_I(FLEXRAN_AGENT, "Agent handles BS ID %ld, capabilities=0x%x => handling%s%s%s%s%s%s%s%s%s\n",
181 182 183 184 185 186 187 188
        flexran_get_bs_id(mod_id), caps,
        FLEXRAN_CAP_LOPHY(caps) ? " LOPHY" : "",
        FLEXRAN_CAP_HIPHY(caps) ? " HIPHY" : "",
        FLEXRAN_CAP_LOMAC(caps) ? " LOMAC" : "",
        FLEXRAN_CAP_HIMAC(caps) ? " HIMAC" : "",
        FLEXRAN_CAP_RLC(caps)   ? " RLC"   : "",
        FLEXRAN_CAP_PDCP(caps)  ? " PDCP"  : "",
        FLEXRAN_CAP_SDAP(caps)  ? " SDAP"  : "",
189 190
        FLEXRAN_CAP_RRC(caps)   ? " RRC"   : "",
        FLEXRAN_CAP_S1AP(caps)  ? " S1AP"  : "");
191 192 193 194 195

  if (FLEXRAN_CAP_LOPHY(caps) || FLEXRAN_CAP_HIPHY(caps)) {
    flexran_agent_register_phy_xface(mod_id);
    LOG_I(FLEXRAN_AGENT, "registered PHY interface/CM for eNB %d\n", mod_id);
  }
Robert Schmidt's avatar
Robert Schmidt committed
196

197 198 199 200 201
  if (FLEXRAN_CAP_LOMAC(caps) || FLEXRAN_CAP_HIMAC(caps)) {
    flexran_agent_register_mac_xface(mod_id);
    flexran_agent_init_mac_agent(mod_id);
    LOG_I(FLEXRAN_AGENT, "registered MAC interface/CM for eNB %d\n", mod_id);
  }
202

203 204 205 206
  if (FLEXRAN_CAP_RRC(caps)) {
    flexran_agent_register_rrc_xface(mod_id);
    LOG_I(FLEXRAN_AGENT, "registered RRC interface/CM for eNB %d\n", mod_id);
  }
207

208 209 210 211
  if (FLEXRAN_CAP_PDCP(caps)) {
    flexran_agent_register_pdcp_xface(mod_id);
    LOG_I(FLEXRAN_AGENT, "registered PDCP interface/CM for eNB %d\n", mod_id);
  }
212

213 214 215 216 217
  if (FLEXRAN_CAP_S1AP(caps)) {
    flexran_agent_register_s1ap_xface(mod_id);
    LOG_I(FLEXRAN_AGENT, "registered S1AP interface/CM for eNB %d\n", mod_id);
  }

218 219 220 221
  /* 
   * initilize a timer 
   */ 
  
222
  flexran_agent_timer_init(mod_id);
223

224 225 226
  /* 
   * start the enb agent task for tx and interaction with the underlying network function
   */ 
227
  if (!agent_task_created) {
228
    if (itti_create_task (TASK_FLEXRAN_AGENT, flexran_agent_task, flexran) < 0) {
229
      LOG_E(FLEXRAN_AGENT, "Create task for FlexRAN Agent failed\n");
230 231 232 233
      return -1;
    }
    agent_task_created = 1;
  }
234

235 236 237
  /* Init the app sublayer */
  flexran_agent_app_init(mod_id);

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  pthread_mutex_init(&flexran->mutex_node_ctrl, NULL);
  pthread_cond_init(&flexran->cond_node_ctrl, NULL);

  if (flexran->node_ctrl_state == ENB_WAIT) {
    /* wait three seconds before showing message and waiting "for real".
     * This way, the message is (hopefully...) the last one and the user knows
     * what is happening. If the controller sends a reconfiguration message in
     * the meantime, the softmodem will never wait */
    sleep(3);
    LOG_I(ENB_APP, " * eNB %d: Waiting for FlexRAN RTController command *\n", mod_id);
    pthread_mutex_lock(&flexran->mutex_node_ctrl);
    while (ENB_NORMAL_OPERATION != flexran->node_ctrl_state)
      pthread_cond_wait(&flexran->cond_node_ctrl, &flexran->mutex_node_ctrl);
    pthread_mutex_unlock(&flexran->mutex_node_ctrl);

    /* reconfigure RRC again, the agent might have changed the configuration */
254
    MessageDef *msg_p = itti_alloc_new_message(TASK_ENB_APP, 0, RRC_CONFIGURATION_REQ);
255 256 257 258
    RRC_CONFIGURATION_REQ(msg_p) = RC.rrc[mod_id]->configuration;
    itti_send_msg_to_task(TASK_RRC_ENB, ENB_MODULE_ID_TO_INSTANCE(mod_id), msg_p);
  }

259 260 261
  return 0;

error:
262
  LOG_E(FLEXRAN_AGENT, "%s(): there was an error\n", __func__);
263 264 265
  return 1;

}