flexran_agent.c 9.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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.0  (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
 */ 
nikaeinn's avatar
nikaeinn committed
21

22 23
/*! \file flexran_agent.h
 * \brief top level flexran agent receive thread and itti task
24
 * \author Xenofon Foukas and Navid Nikaein
nikaeinn's avatar
nikaeinn committed
25 26 27 28
 * \date 2016
 * \version 0.1
 */

29
#include "flexran_agent_common.h"
nikaeinn's avatar
nikaeinn committed
30
#include "log.h"
31 32
#include "flexran_agent.h"
#include "flexran_agent_mac_defs.h"
33 34
#include "flexran_agent_mac.h"
#include "flexran_agent_mac_internal.h"
nikaeinn's avatar
nikaeinn committed
35

36
#include "flexran_agent_extern.h"
37

38 39
#include "assertions.h"

40 41
#include "flexran_agent_net_comm.h"
#include "flexran_agent_async.h"
42

43 44
#include <arpa/inet.h>

45
//#define TEST_TIMER
46

47
flexran_agent_instance_t flexran_agent[NUM_MAX_ENB];
nikaeinn's avatar
nikaeinn committed
48

49 50
char in_ip[40];
static uint16_t in_port;
51
char local_cache[40];
nikaeinn's avatar
nikaeinn committed
52

53
void *send_thread(void *args);
54
void *receive_thread(void *args);
nikaeinn's avatar
nikaeinn committed
55
pthread_t new_thread(void *(*f)(void *), void *b);
56
Protocol__FlexranMessage *flexran_agent_timeout(void* args);
57

58 59

int agent_task_created = 0;
60 61 62 63
/* 
 * enb agent task mainly wakes up the tx thread for periodic and oneshot messages to the controller 
 * and can interact with other itti tasks
*/
64
void *flexran_agent_task(void *args){
65

66
  //flexran_agent_instance_t         *d = (flexran_agent_instance_t *) args;
67
  Protocol__FlexranMessage *msg;
68 69 70
  void *data;
  int size;
  err_code_t err_code;
71
  int                   priority = 0;
72

73 74 75
  MessageDef                     *msg_p           = NULL;
  const char                     *msg_name        = NULL;
  int                             result;
76
  struct flexran_agent_timer_element_s * elem = NULL;
77

78
  itti_mark_task_ready(TASK_FLEXRAN_AGENT);
79 80 81

  do {
    // Wait for a message
82
    itti_receive_msg (TASK_FLEXRAN_AGENT, &msg_p);
83 84 85 86 87 88 89 90 91
    DevAssert(msg_p != NULL);
    msg_name = ITTI_MSG_NAME (msg_p);

    switch (ITTI_MSG_ID(msg_p)) {
    case TERMINATE_MESSAGE:
      itti_exit_task ();
      break;

    case MESSAGE_TEST:
92
      LOG_I(FLEXRAN_AGENT, "Received %s\n", ITTI_MSG_NAME(msg_p));
93 94 95
      break;
    
    case TIMER_HAS_EXPIRED:
96
      msg = flexran_agent_process_timeout(msg_p->ittiMsg.timer_has_expired.timer_id, msg_p->ittiMsg.timer_has_expired.arg);
97
      if (msg != NULL){
98
	data=flexran_agent_pack_message(msg,&size);
99
	elem = get_timer_entry(msg_p->ittiMsg.timer_has_expired.timer_id);
100
	if (flexran_agent_msg_send(elem->agent_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
      break;

    default:
110
      LOG_E(FLEXRAN_AGENT, "Received unexpected message %s\n", msg_name);
111 112 113 114 115
      break;
    }

    result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
116 117
    continue;
  error:
118
    LOG_E(FLEXRAN_AGENT,"flexran_agent_task: error %d occured\n",err_code);
119
  } while (1);
nikaeinn's avatar
nikaeinn committed
120

121 122
  return NULL;
}
123

124
void *receive_thread(void *args) {
nikaeinn's avatar
nikaeinn committed
125

126
  flexran_agent_instance_t         *d = args;
nikaeinn's avatar
nikaeinn committed
127 128 129 130 131
  void                  *data;
  int                   size;
  int                   priority;
  err_code_t             err_code;

132
  Protocol__FlexranMessage *msg;
nikaeinn's avatar
nikaeinn committed
133 134
  
  while (1) {
135

136
    while (flexran_agent_msg_recv(d->enb_id, FLEXRAN_AGENT_DEFAULT, &data, &size, &priority) == 0) {
137
      
138
      LOG_D(FLEXRAN_AGENT,"received message with size %d\n", size);
nikaeinn's avatar
nikaeinn committed
139
  
140
      // Invoke the message handler
141
      msg=flexran_agent_handle_message(d->enb_id, data, size);
nikaeinn's avatar
nikaeinn committed
142

143
      free(data);
nikaeinn's avatar
nikaeinn committed
144
    
145 146
      // check if there is something to send back to the controller
      if (msg != NULL){
147
	data=flexran_agent_pack_message(msg,&size);
148

149
	if (flexran_agent_msg_send(d->enb_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
150
	  err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
151 152
	  goto error;
	}
153
      
154
	LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
155
      } 
nikaeinn's avatar
nikaeinn committed
156 157
    }
  }
158
    
nikaeinn's avatar
nikaeinn committed
159 160 161
  return NULL;

error:
162
  LOG_E(FLEXRAN_AGENT,"receive_thread: error %d occured\n",err_code);
nikaeinn's avatar
nikaeinn committed
163 164 165 166 167 168 169 170 171 172 173 174 175
  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); 
  }
176 177 178 179 180 181 182

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

nikaeinn's avatar
nikaeinn committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  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;
}

199
int channel_container_init = 0;
200
int flexran_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
201 202 203
  
  int channel_id;
  
204 205
  flexran_set_enb_vars(mod_id, RAN_LTE_OAI);
  flexran_agent[mod_id].enb_id = mod_id;
206 207 208 209
  
  /* 
   * check the configuration
   */ 
210 211
  if (enb_properties->properties[mod_id]->flexran_agent_cache != NULL) {
    strncpy(local_cache, enb_properties->properties[mod_id]->flexran_agent_cache, sizeof(local_cache));
212 213
    local_cache[sizeof(local_cache) - 1] = 0;
  } else {
214
    strcpy(local_cache, DEFAULT_FLEXRAN_AGENT_CACHE);
215 216
  }
  
217 218
  if (enb_properties->properties[mod_id]->flexran_agent_ipv4_address != 0) {
    inet_ntop(AF_INET, &(enb_properties->properties[mod_id]->flexran_agent_ipv4_address), in_ip, INET_ADDRSTRLEN);
219
  } else {
220
    strcpy(in_ip, DEFAULT_FLEXRAN_AGENT_IPv4_ADDRESS ); 
221 222
  }
  
223 224
  if (enb_properties->properties[mod_id]->flexran_agent_port != 0 ) {
    in_port = enb_properties->properties[mod_id]->flexran_agent_port;
225
  } else {
226
    in_port = DEFAULT_FLEXRAN_AGENT_PORT ;
227
  }
228 229
  LOG_I(FLEXRAN_AGENT,"starting enb agent client for module id %d on ipv4 %s, port %d\n",  
	flexran_agent[mod_id].enb_id,
230 231 232
	in_ip,
	in_port);

233 234 235
  /*
   * Initialize the channel container
   */
236
  if (!channel_container_init) {
237
    flexran_agent_init_channel_container();
238 239
    channel_container_init = 1;
  }
240
  /*Create the async channel info*/
241
  flexran_agent_async_channel_t *channel_info = flexran_agent_async_channel_info(mod_id, in_ip, in_port);
242 243

  /*Create a channel using the async channel info*/
244 245 246 247
  channel_id = flexran_agent_create_channel((void *) channel_info, 
					flexran_agent_async_msg_send, 
					flexran_agent_async_msg_recv,
					flexran_agent_async_release);
248 249

  
250 251 252 253
  if (channel_id <= 0) {
    goto error;
  }

254
  flexran_agent_channel_t *channel = get_channel(channel_id);
255
  
256 257 258 259
  if (channel == NULL) {
    goto error;
  }

260 261
  /*Register the channel for all underlying agents (use FLEXRAN_AGENT_MAX)*/
  flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAX);
262 263

  /*Example of registration for a specific agent(MAC):
264
   *flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAC);
265 266
   */

267
  /*Initialize the continuous MAC stats update mechanism*/
268
  flexran_agent_init_cont_mac_stats_update(mod_id);
269
  
270
  new_thread(receive_thread, &flexran_agent[mod_id]);
271 272 273 274 275

  /*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));
276
  flexran_agent_register_mac_xface(mod_id, mac_agent_xface);
277 278 279 280 281
  
  /* 
   * initilize a timer 
   */ 
  
282
  flexran_agent_init_timer();
283 284 285 286

  /*
   * Initialize the mac agent
   */
287
  flexran_agent_init_mac_agent(mod_id);
288 289 290 291
  
  /* 
   * start the enb agent task for tx and interaction with the underlying network function
   */ 
292
  if (!agent_task_created) {
293 294
    if (itti_create_task (TASK_FLEXRAN_AGENT, flexran_agent_task, (void *) &flexran_agent[mod_id]) < 0) {
      LOG_E(FLEXRAN_AGENT, "Create task for FlexRAN Agent failed\n");
295 296 297 298
      return -1;
    }
    agent_task_created = 1;
  }
299
  
300
  LOG_I(FLEXRAN_AGENT,"client ends\n");
nikaeinn's avatar
nikaeinn committed
301 302 303
  return 0;

error:
304
  LOG_I(FLEXRAN_AGENT,"there was an error\n");
nikaeinn's avatar
nikaeinn committed
305 306 307 308
  return 1;

}

309
Protocol__FlexranMessage *flexran_agent_timeout(void* args){
310

311
  //  flexran_agent_timer_args_t *timer_args = calloc(1, sizeof(*timer_args));
312
  //memcpy (timer_args, args, sizeof(*timer_args));
313
  flexran_agent_timer_args_t *timer_args = (flexran_agent_timer_args_t *) args;
314
  
315 316 317
  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);
318
  
319
  return NULL;
nikaeinn's avatar
nikaeinn committed
320
}