flexran_agent.c 9.51 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
shahab's avatar
shahab committed
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
  void *data;
  int size;
50
  err_code_t err_code=0;
51
  int                   priority = 0;
52

53 54
  MessageDef                     *msg_p           = NULL;
  int                             result;
55
  struct flexran_agent_timer_element_s * elem = NULL;
56

57
  itti_mark_task_ready(TASK_FLEXRAN_AGENT);
58 59 60

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

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

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

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

    default:
89
      LOG_E(FLEXRAN_AGENT, "Received unexpected message %s\n", ITTI_MSG_NAME (msg_p));
90 91 92 93 94
      break;
    }

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

101 102
  return NULL;
}
103

104
void *receive_thread(void *args) {
105

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

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

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

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

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

error:
142 143
  if (err_code != 0)
     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 189 190 191 192

  /* 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;
  }
193

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

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

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

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

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

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

shahab's avatar
shahab committed
228 229
  /*Initialize the continuous stats update mechanism*/
  flexran_agent_init_cont_stats_update(mod_id);
230
  
231
  new_thread(receive_thread, flexran);
232 233 234 235 236

  /*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));
237
  flexran_agent_register_mac_xface(mod_id, mac_agent_xface);
238
  
shahab's avatar
shahab committed
239
  AGENT_RRC_xface *rrc_agent_xface = (AGENT_RRC_xface *) malloc(sizeof(AGENT_RRC_xface));
shahab's avatar
shahab committed
240 241
  flexran_agent_register_rrc_xface(mod_id, rrc_agent_xface);

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

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

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

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

288 289 290
  return 0;

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

}

296
Protocol__FlexranMessage *flexran_agent_timeout(void* args){
297

298
  //  flexran_agent_timer_args_t *timer_args = calloc(1, sizeof(*timer_args));
299
  //memcpy (timer_args, args, sizeof(*timer_args));
300
  flexran_agent_timer_args_t *timer_args = (flexran_agent_timer_args_t *) args;
301
  
302
  LOG_UI(FLEXRAN_AGENT, "flexran_agent %d timeout\n", timer_args->mod_id);
303 304
  //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);
305
  
306
  return NULL;
307
}