flexran_agent.c 9.22 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
 */ 
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>

shahab's avatar
shahab committed
33

34
//#define TEST_TIMER
35

36
flexran_agent_instance_t flexran_agent[NUM_MAX_ENB];
37

38 39
char in_ip[40];
static uint16_t in_port;
40
char local_cache[40];
41

42
void *send_thread(void *args);
43
void *receive_thread(void *args);
44
pthread_t new_thread(void *(*f)(void *), void *b);
45
Protocol__FlexranMessage *flexran_agent_timeout(void* args);
46

47 48

int agent_task_created = 0;
49 50 51 52
/* 
 * enb agent task mainly wakes up the tx thread for periodic and oneshot messages to the controller 
 * and can interact with other itti tasks
*/
53
void *flexran_agent_task(void *args){
54

55
  //flexran_agent_instance_t         *d = (flexran_agent_instance_t *) args;
56
  Protocol__FlexranMessage *msg;
57 58 59
  void *data;
  int size;
  err_code_t err_code;
60
  int                   priority = 0;
61

62 63 64
  MessageDef                     *msg_p           = NULL;
  const char                     *msg_name        = NULL;
  int                             result;
65
  struct flexran_agent_timer_element_s * elem = NULL;
66

67
  itti_mark_task_ready(TASK_FLEXRAN_AGENT);
68 69 70

  do {
    // Wait for a message
71
    itti_receive_msg (TASK_FLEXRAN_AGENT, &msg_p);
72 73 74 75 76 77 78 79 80
    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:
81
      LOG_I(FLEXRAN_AGENT, "Received %s\n", ITTI_MSG_NAME(msg_p));
82 83 84
      break;
    
    case TIMER_HAS_EXPIRED:
85
      msg = flexran_agent_process_timeout(msg_p->ittiMsg.timer_has_expired.timer_id, msg_p->ittiMsg.timer_has_expired.arg);
86
      if (msg != NULL){
87
	data=flexran_agent_pack_message(msg,&size);
88
	elem = get_timer_entry(msg_p->ittiMsg.timer_has_expired.timer_id);
89
	if (flexran_agent_msg_send(elem->agent_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
90
	  err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
91 92
	  goto error;
	}
93

94
	LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
95
      }
96 97 98
      break;

    default:
99
      LOG_E(FLEXRAN_AGENT, "Received unexpected message %s\n", msg_name);
100 101 102 103 104
      break;
    }

    result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
    AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
105 106
    continue;
  error:
107
    LOG_E(FLEXRAN_AGENT,"flexran_agent_task: error %d occured\n",err_code);
108
  } while (1);
109

110 111
  return NULL;
}
112

113
void *receive_thread(void *args) {
114

115
  flexran_agent_instance_t         *d = args;
116 117 118 119 120
  void                  *data;
  int                   size;
  int                   priority;
  err_code_t             err_code;

121
  Protocol__FlexranMessage *msg;
122 123
  
  while (1) {
124

125
    while (flexran_agent_msg_recv(d->enb_id, FLEXRAN_AGENT_DEFAULT, &data, &size, &priority) == 0) {
126
      
127
      LOG_D(FLEXRAN_AGENT,"received message with size %d\n", size);
128
  
129
      // Invoke the message handler
130
      msg=flexran_agent_handle_message(d->enb_id, data, size);
131

132
      free(data);
133
    
134 135
      // check if there is something to send back to the controller
      if (msg != NULL){
136
	data=flexran_agent_pack_message(msg,&size);
137

138
	if (flexran_agent_msg_send(d->enb_id, FLEXRAN_AGENT_DEFAULT, data, size, priority)) {
139
	  err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
140 141
	  goto error;
	}
142
      
143
	LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
144
      } 
145 146
    }
  }
147
    
148 149 150
  return NULL;

error:
151
  LOG_E(FLEXRAN_AGENT,"receive_thread: error %d occured\n",err_code);
152 153 154 155 156 157 158 159 160 161 162 163 164
  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); 
  }
165 166 167 168 169 170 171

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

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  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;
}

188
int channel_container_init = 0;
189
int flexran_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
190 191 192
  
  int channel_id;
  
193 194
  flexran_set_enb_vars(mod_id, RAN_LTE_OAI);
  flexran_agent[mod_id].enb_id = mod_id;
195 196 197 198
  
  /* 
   * check the configuration
   */ 
199 200
  if (enb_properties->properties[mod_id]->flexran_agent_cache != NULL) {
    strncpy(local_cache, enb_properties->properties[mod_id]->flexran_agent_cache, sizeof(local_cache));
201 202
    local_cache[sizeof(local_cache) - 1] = 0;
  } else {
203
    strcpy(local_cache, DEFAULT_FLEXRAN_AGENT_CACHE);
204 205
  }
  
206 207
  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);
208
  } else {
209
    strcpy(in_ip, DEFAULT_FLEXRAN_AGENT_IPv4_ADDRESS ); 
210 211
  }
  
212 213
  if (enb_properties->properties[mod_id]->flexran_agent_port != 0 ) {
    in_port = enb_properties->properties[mod_id]->flexran_agent_port;
214
  } else {
215
    in_port = DEFAULT_FLEXRAN_AGENT_PORT ;
216
  }
217 218
  LOG_I(FLEXRAN_AGENT,"starting enb agent client for module id %d on ipv4 %s, port %d\n",  
	flexran_agent[mod_id].enb_id,
219 220 221
	in_ip,
	in_port);

222 223 224
  /*
   * Initialize the channel container
   */
225
  if (!channel_container_init) {
226
    flexran_agent_init_channel_container();
227 228
    channel_container_init = 1;
  }
229
  /*Create the async channel info*/
230
  flexran_agent_async_channel_t *channel_info = flexran_agent_async_channel_info(mod_id, in_ip, in_port);
231 232

  /*Create a channel using the async channel info*/
233 234 235 236
  channel_id = flexran_agent_create_channel((void *) channel_info, 
					flexran_agent_async_msg_send, 
					flexran_agent_async_msg_recv,
					flexran_agent_async_release);
237 238

  
239 240 241 242
  if (channel_id <= 0) {
    goto error;
  }

243
  flexran_agent_channel_t *channel = get_channel(channel_id);
244
  
245 246 247 248
  if (channel == NULL) {
    goto error;
  }

249 250
  /*Register the channel for all underlying agents (use FLEXRAN_AGENT_MAX)*/
  flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAX);
251 252

  /*Example of registration for a specific agent(MAC):
253
   *flexran_agent_register_channel(mod_id, channel, FLEXRAN_AGENT_MAC);
254 255
   */

shahab's avatar
shahab committed
256 257
  /*Initialize the continuous stats update mechanism*/
  flexran_agent_init_cont_stats_update(mod_id);
258
  
259
  new_thread(receive_thread, &flexran_agent[mod_id]);
260 261 262 263 264

  /*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));
265
  flexran_agent_register_mac_xface(mod_id, mac_agent_xface);
266
  
shahab's avatar
shahab committed
267 268 269
  AGENT_RRC_xface *rrc_agent_xface = (AGENT_MAC_xface *) malloc(sizeof(AGENT_MAC_xface));
  flexran_agent_register_rrc_xface(mod_id, rrc_agent_xface);

270 271 272 273
  /* 
   * initilize a timer 
   */ 
  
274
  flexran_agent_init_timer();
275 276 277 278

  /*
   * Initialize the mac agent
   */
279
  flexran_agent_init_mac_agent(mod_id);
280 281 282 283
  
  /* 
   * start the enb agent task for tx and interaction with the underlying network function
   */ 
284
  if (!agent_task_created) {
285 286
    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");
287 288 289 290
      return -1;
    }
    agent_task_created = 1;
  }
291
  
292
  LOG_I(FLEXRAN_AGENT,"client ends\n");
293 294 295
  return 0;

error:
296
  LOG_I(FLEXRAN_AGENT,"there was an error\n");
297 298 299 300
  return 1;

}

301
Protocol__FlexranMessage *flexran_agent_timeout(void* args){
302

303
  //  flexran_agent_timer_args_t *timer_args = calloc(1, sizeof(*timer_args));
304
  //memcpy (timer_args, args, sizeof(*timer_args));
305
  flexran_agent_timer_args_t *timer_args = (flexran_agent_timer_args_t *) args;
306
  
307 308 309
  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);
310
  
311
  return NULL;
312
}