flexran_agent.c 9.68 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
#include "flexran_agent.h"
30

31 32
#include <arpa/inet.h>

33
void *send_thread(void *args);
34
void *receive_thread(void *args);
35
pthread_t new_thread(void *(*f)(void *), void *b);
36
Protocol__FlexranMessage *flexran_agent_timeout(void* args);
37

38 39

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

46
  //flexran_agent_info_t         *d = (flexran_agent_info_t *) args;
47
  Protocol__FlexranMessage *msg;
48 49 50
  void *data;
  int size;
  err_code_t err_code;
51
  int                   priority = 0;
52

53 54 55
  MessageDef                     *msg_p           = NULL;
  const char                     *msg_name        = NULL;
  int                             result;
56
  struct flexran_agent_timer_element_s * elem = NULL;
57

58
  itti_mark_task_ready(TASK_FLEXRAN_AGENT);
59 60 61

  do {
    // Wait for a message
62
    itti_receive_msg (TASK_FLEXRAN_AGENT, &msg_p);
63 64 65 66 67
    DevAssert(msg_p != NULL);
    msg_name = ITTI_MSG_NAME (msg_p);

    switch (ITTI_MSG_ID(msg_p)) {
    case TERMINATE_MESSAGE:
68
      LOG_W(FLEXRAN_AGENT, " *** Exiting FLEXRAN thread\n");
69 70 71 72
      itti_exit_task ();
      break;

    case MESSAGE_TEST:
73
      LOG_I(FLEXRAN_AGENT, "Received %s\n", ITTI_MSG_NAME(msg_p));
74 75 76
      break;
    
    case TIMER_HAS_EXPIRED:
77
      msg = flexran_agent_process_timeout(msg_p->ittiMsg.timer_has_expired.timer_id, msg_p->ittiMsg.timer_has_expired.arg);
78
      if (msg != NULL){
79
	data=flexran_agent_pack_message(msg,&size);
80
	elem = get_timer_entry(msg_p->ittiMsg.timer_has_expired.timer_id);
81
	if (flexran_agent_msg_send(elem->agent_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
82
	  err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
83 84
	  goto error;
	}
85

86
	LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
87
      }
88 89 90
      break;

    default:
91
      LOG_E(FLEXRAN_AGENT, "Received unexpected message %s\n", msg_name);
92 93 94 95 96
      break;
    }

    result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
97 98
    continue;
  error:
99
    LOG_E(FLEXRAN_AGENT,"flexran_agent_task: error %d occured\n",err_code);
100
  } while (1);
101

102 103
  return NULL;
}
104

105
void *receive_thread(void *args) {
106

107
  flexran_agent_info_t  *d = args;
108 109 110 111 112
  void                  *data;
  int                   size;
  int                   priority;
  err_code_t             err_code;

113
  Protocol__FlexranMessage *msg;
114 115
  
  while (1) {
116

117
    while (flexran_agent_msg_recv(d->enb_id, FLEXRAN_AGENT_DEFAULT, &data, &size, &priority) == 0) {
118
      
119
      LOG_D(FLEXRAN_AGENT,"received message with size %d\n", size);
120
  
121
      // Invoke the message handler
122
      msg=flexran_agent_handle_message(d->enb_id, data, size);
123

124
      free(data);
125
    
126 127
      // check if there is something to send back to the controller
      if (msg != NULL){
128
	data=flexran_agent_pack_message(msg,&size);
129

130
	if (flexran_agent_msg_send(d->enb_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
131
	  err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
132 133
	  goto error;
	}
134
      
135
	LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
136
      } 
137 138
    }
  }
139
    
140 141 142
  return NULL;

error:
143
  LOG_E(FLEXRAN_AGENT,"receive_thread: error %d occured\n",err_code);
144 145 146 147 148 149 150 151 152 153 154 155 156
  return NULL;
}


/* utility function to create a thread */
pthread_t new_thread(void *(*f)(void *), void *b) {
  pthread_t t;
  pthread_attr_t att;

  if (pthread_attr_init(&att)){ 
    fprintf(stderr, "pthread_attr_init err\n"); 
    exit(1); 
  }
157 158 159 160 161 162 163

  struct sched_param sched_param_recv_thread;

  sched_param_recv_thread.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
  pthread_attr_setschedparam(&att, &sched_param_recv_thread);
  pthread_attr_setschedpolicy(&att, SCHED_FIFO);

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  if (pthread_attr_setdetachstate(&att, PTHREAD_CREATE_DETACHED)) { 
    fprintf(stderr, "pthread_attr_setdetachstate err\n"); 
    exit(1); 
  }
  if (pthread_create(&t, &att, f, b)) { 
    fprintf(stderr, "pthread_create err\n"); 
    exit(1); 
  }
  if (pthread_attr_destroy(&att)) { 
    fprintf(stderr, "pthread_attr_destroy err\n"); 
    exit(1); 
  }

  return t;
}

180 181 182
int channel_container_init = 0;
int flexran_agent_start(mid_t mod_id)
{
183
  flexran_agent_info_t *flexran = RC.flexran[mod_id];
184
  int channel_id;
185 186
  char *in_ip = flexran->remote_ipv4_addr;
  uint16_t in_port = flexran->remote_port;
187
  
188
  flexran->enb_id = mod_id;
189 190
  /* assume for the moment the monolithic case, i.e. agent can provide
   * information for all layers */
191 192 193
  flexran->capability_mask = FLEXRAN_CAP_LOL1 | FLEXRAN_CAP_HIL1
                           | FLEXRAN_CAP_LOL2 | FLEXRAN_CAP_HIL2
                           | FLEXRAN_CAP_PDCP | FLEXRAN_CAP_RRC;
194

195 196 197
  /*
   * Initialize the channel container
   */
198
  if (!channel_container_init) {
199
    flexran_agent_init_channel_container();
200 201
    channel_container_init = 1;
  }
202
  /*Create the async channel info*/
203
  flexran_agent_async_channel_t *channel_info = flexran_agent_async_channel_info(mod_id, in_ip, in_port);
204 205

  /*Create a channel using the async channel info*/
206 207 208 209
  channel_id = flexran_agent_create_channel((void *) channel_info, 
					flexran_agent_async_msg_send, 
					flexran_agent_async_msg_recv,
					flexran_agent_async_release);
210 211

  
212 213 214 215
  if (channel_id <= 0) {
    goto error;
  }

216
  flexran_agent_channel_t *channel = get_channel(channel_id);
217
  
218 219 220 221
  if (channel == NULL) {
    goto error;
  }

222 223
  /*Register the channel for all underlying agents (use FLEXRAN_AGENT_MAX)*/
  flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAX);
224 225

  /*Example of registration for a specific agent(MAC):
226
   *flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAC);
227 228
   */

229 230
  /*Initialize the continuous stats update mechanism*/
  flexran_agent_init_cont_stats_update(mod_id);
231
  
232
  new_thread(receive_thread, flexran);
233 234 235 236 237

  /*Initialize and register the mac xface. Must be modified later
   *for more flexibility in agent management */

  AGENT_MAC_xface *mac_agent_xface = (AGENT_MAC_xface *) malloc(sizeof(AGENT_MAC_xface));
238
  flexran_agent_register_mac_xface(mod_id, mac_agent_xface);
239
  
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
240
  AGENT_RRC_xface *rrc_agent_xface = (AGENT_RRC_xface *) malloc(sizeof(AGENT_RRC_xface));
241 242
  flexran_agent_register_rrc_xface(mod_id, rrc_agent_xface);

243 244
  AGENT_PDCP_xface *pdcp_agent_xface = (AGENT_PDCP_xface *) malloc(sizeof(AGENT_PDCP_xface));
  flexran_agent_register_pdcp_xface(mod_id, pdcp_agent_xface);
245

246 247 248 249
  /* 
   * initilize a timer 
   */ 
  
250
  flexran_agent_init_timer();
251 252 253 254

  /*
   * Initialize the mac agent
   */
255
  flexran_agent_init_mac_agent(mod_id);
256 257 258 259
  
  /* 
   * start the enb agent task for tx and interaction with the underlying network function
   */ 
260
  if (!agent_task_created) {
261
    if (itti_create_task (TASK_FLEXRAN_AGENT, flexran_agent_task, flexran) < 0) {
262
      LOG_E(FLEXRAN_AGENT, "Create task for FlexRAN Agent failed\n");
263 264 265 266
      return -1;
    }
    agent_task_created = 1;
  }
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  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 */
    MessageDef *msg_p = itti_alloc_new_message(TASK_ENB_APP, RRC_CONFIGURATION_REQ);
    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);
  }

289 290 291
  return 0;

error:
292
  LOG_I(FLEXRAN_AGENT,"there was an error\n");
293 294 295 296
  return 1;

}

297
Protocol__FlexranMessage *flexran_agent_timeout(void* args){
298

299
  //  flexran_agent_timer_args_t *timer_args = calloc(1, sizeof(*timer_args));
300
  //memcpy (timer_args, args, sizeof(*timer_args));
301
  flexran_agent_timer_args_t *timer_args = (flexran_agent_timer_args_t *) args;
302
  
303 304 305
  LOG_I(FLEXRAN_AGENT, "flexran_agent %d timeout\n", timer_args->mod_id);
  //LOG_I(FLEXRAN_AGENT, "eNB action %d ENB flags %d \n", timer_args->cc_actions,timer_args->cc_report_flags);
  //LOG_I(FLEXRAN_AGENT, "UE action %d UE flags %d \n", timer_args->ue_actions,timer_args->ue_report_flags);
306
  
307
  return NULL;
308
}