Commit 1f213b0b authored by nikaeinn's avatar nikaeinn

* add options to enb configuration file for the enb agent

* add timer api for the periodic and oneshot operations
* create enb agent task
parent 48ddbc68
......@@ -94,9 +94,8 @@ int timer_handle_signal(siginfo_t *info)
timer_p = (struct timer_elm_s *)info->si_ptr;
// LG: To many traces for msc timer:
// TMR_DEBUG("Timer with id 0x%lx has expired\n", (long)timer_p->timer);
#if defined(ENABLE_ITTI)
TMR_DEBUG("Timer with id 0x%lx has expired\n", (long)timer_p->timer);
printf("Timer with id 0x%lx has expired\n", (long)timer_p->timer);
task_id = timer_p->task_id;
instance = timer_p->instance;
......@@ -106,7 +105,6 @@ int timer_handle_signal(siginfo_t *info)
timer_expired_p->timer_id = (long)timer_p->timer;
timer_expired_p->arg = timer_p->timer_arg;
#endif
/* Timer is a one shot timer, remove it */
if (timer_p->type == TIMER_ONE_SHOT) {
......@@ -123,13 +121,16 @@ int timer_handle_signal(siginfo_t *info)
TMR_DEBUG("Failed to delete timer 0x%lx\n", (long)timer_p->timer);
}
}
#ifdefined ENABLE_ITTI
/* Notify task of timer expiry */
if (itti_send_msg_to_task(task_id, instance, message_p) < 0) {
TMR_DEBUG("Failed to send msg TIMER_HAS_EXPIRED to task %u\n", task_id);
free(message_p);
return -1;
}
#if defined(ENB_AGENT_SB_IF)
#endif
return 0;
......@@ -209,6 +210,11 @@ int timer_setup(
type == TIMER_PERIODIC ? "periodic" : "single shot",
*timer_id, interval_sec, interval_us);
printf("Requesting new %s timer with id 0x%lx that expires within "
"%d sec and %d usec\n",
type == TIMER_PERIODIC ? "periodic" : "single shot",
*timer_id, interval_sec, interval_us);
timer_p->timer = timer;
/* Lock the queue and insert the timer at the tail */
......
......@@ -63,6 +63,8 @@ TASK_DEF(TASK_X2AP, TASK_PRIORITY_MED, 200)
TASK_DEF(TASK_SCTP, TASK_PRIORITY_MED, 200)
/// eNB APP task
TASK_DEF(TASK_ENB_APP, TASK_PRIORITY_MED, 200)
/// eNB Agent task
TASK_DEF(TASK_ENB_AGENT, TASK_PRIORITY_MED, 200)
// UE tasks and sub-tasks:
//// Layer 2 and Layer 1 sub-tasks
......
......@@ -37,7 +37,7 @@ enum progran_err {
MSG_NOT_VALIDATED = -8;
MSG_OUT_DATED = -9;
// other erros
UNEXPECTED = -100;
}
......
......@@ -27,43 +27,82 @@
*******************************************************************************/
/*! \file
* \brief
* \author
/*! \file enb_agent.h
* \brief top level enb agent receive thread and itti task
* \author Navid Nikaein and Xenofon Foukas
* \date 2016
* \version 0.1
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "enb_agent_common.h"
#include "link_manager.h"
#include "log.h"
#include "enb_agent.h"
typedef uint8_t xid_t;
#include "assertions.h"
enb_agent_instance_t enb_agent[NUM_MAX_ENB_AGENT];
msg_context_t shared_ctxt[NUM_MAX_ENB_AGENT];
/* this could also go into enb_agent struct*/
enb_agent_info_t enb_agent_info;
// tx and rx shared context
typedef struct {
message_queue_t *tx_mq;
message_queue_t *rx_mq;
xid_t tx_xid;
xid_t rx_xid;
} msg_context_t;
msg_context_t shared_ctxt;
char in_ip[40];
static uint16_t in_port;
void *send_thread(void *arg);
void *receive_thread(void *arg);
//void *send_thread(void *args);
void *receive_thread(void *args);
pthread_t new_thread(void *(*f)(void *), void *b);
err_code_t enb_agent_timeout(void* args);
/*
* enb agent task mainly wakes up the tx thread for periodic and oneshot messages to the controller
* and can interact with other itti tasks
*/
void *enb_agent_task(void *args){
MessageDef *msg_p = NULL;
const char *msg_name = NULL;
instance_t instance;
int result;
itti_mark_task_ready(TASK_ENB_AGENT);
do {
// Wait for a message
itti_receive_msg (TASK_ENB_AGENT, &msg_p);
DevAssert(msg_p != NULL);
msg_name = ITTI_MSG_NAME (msg_p);
instance = ITTI_MSG_INSTANCE (msg_p);
switch (ITTI_MSG_ID(msg_p)) {
case TERMINATE_MESSAGE:
itti_exit_task ();
break;
case MESSAGE_TEST:
LOG_I(ENB_AGENT, "Received %s\n", ITTI_MSG_NAME(msg_p));
break;
case TIMER_HAS_EXPIRED:
enb_agent_process_timeout(msg_p->ittiMsg.timer_has_expired.timer_id, &msg_p->ittiMsg.timer_has_expired.arg);
break;
default:
LOG_E(ENB_AGENT, "Received unexpected message %s\n", msg_name);
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);
void *send_thread(void *arg) {
return NULL;
}
/*
void *send_thread(void *args) {
msg_context_t *d = arg;
msg_context_t *d = args;
void *data;
int size;
int priority;
......@@ -81,10 +120,10 @@ error:
printf("receive_thread: there was an error\n");
return NULL;
}
*/
void *receive_thread(void *args) {
void *receive_thread(void *arg) {
msg_context_t *d = arg;
msg_context_t *d = args;
void *data;
int size;
int priority;
......@@ -97,10 +136,10 @@ void *receive_thread(void *arg) {
err_code = PROTOCOL__PROGRAN_ERR__MSG_DEQUEUING;
goto error;
}
LOG_D(ENB_APP,"received message with size %d\n", size);
LOG_D(ENB_AGENT,"received message with size %d\n", size);
msg=enb_agent_handle_message(d->rx_xid, data, size);
msg=enb_agent_handle_message(d->mod_id, d->rx_xid, data, size);
free(data);
......@@ -115,7 +154,7 @@ void *receive_thread(void *arg) {
err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
goto error;
}
LOG_D(ENB_APP,"sent message with size %d\n", size);
LOG_D(ENB_AGENT,"sent message with size %d\n", size);
}
}
......@@ -123,7 +162,7 @@ void *receive_thread(void *arg) {
return NULL;
error:
printf("receive_thread: error %d occured\n",err_code);
LOG_E(ENB_AGENT,"receive_thread: error %d occured\n",err_code);
return NULL;
}
......@@ -153,50 +192,146 @@ pthread_t new_thread(void *(*f)(void *), void *b) {
return t;
}
int enb_agent_start(){
socket_link_t *link;
message_queue_t *send_queue;
message_queue_t *receive_queue;
link_manager_t *manager;
int enb_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties){
LOG_I(ENB_APP,"starting enb agent client\n");
//
set_enb_vars(mod_id, RAN_LTE_OAI);
enb_agent[mod_id].mod_id = mod_id;
enb_agent_info.nb_modules+=1;
/*
* check the configuration
*/
if (enb_properties->properties[mod_id]->enb_agent_ipv4_address != NULL) {
strncpy(in_ip, enb_properties->properties[mod_id]->enb_agent_ipv4_address, sizeof(in_ip) );
in_ip[sizeof(in_ip) - 1] = 0; // terminate string
} else {
strcpy(in_ip, DEFAULT_ENB_AGENT_IPv4_ADDRESS );
}
if (enb_properties->properties[mod_id]->enb_agent_port != 0 ) {
in_port = enb_properties->properties[mod_id]->enb_agent_port;
} else {
in_port = DEFAULT_ENB_AGENT_PORT ;
}
LOG_I(ENB_AGENT,"starting enb agent client for module id %d on ipv4 %s, port %d\n",
enb_agent[mod_id].mod_id,
in_ip,
in_port);
//#define TEST_TIMER 0
#if !defined TEST_TIMER
/*
* create a socket
*/
enb_agent[mod_id].link = new_link_client(in_ip, in_port);
if (enb_agent[mod_id].link == NULL) goto error;
LOG_I(ENB_AGENT,"starting enb agent client for module id %d on ipv4 %s, port %d\n",
enb_agent[mod_id].mod_id,
in_ip,
in_port);
/*
* create a message queue
*/
enb_agent[mod_id].send_queue = new_message_queue();
if (enb_agent[mod_id].send_queue == NULL) goto error;
enb_agent[mod_id].receive_queue = new_message_queue();
if (enb_agent[mod_id].receive_queue == NULL) goto error;
/*
* create a link manager
*/
enb_agent[mod_id].manager = create_link_manager(enb_agent[mod_id].send_queue, enb_agent[mod_id].receive_queue, enb_agent[mod_id].link);
if (enb_agent[mod_id].manager == NULL) goto error;
link = new_link_client("127.0.0.1", 2210);
if (link == NULL) goto error;
memset(&shared_ctxt, 0, sizeof(msg_context_t));
shared_ctxt[mod_id].mod_id = mod_id;
shared_ctxt[mod_id].tx_mq = enb_agent[mod_id].send_queue;
shared_ctxt[mod_id].rx_mq = enb_agent[mod_id].receive_queue;
send_queue = new_message_queue();
if (send_queue == NULL) goto error;
receive_queue = new_message_queue();
if (receive_queue == NULL) goto error;
/*
* start the enb agent rx thread
*/
new_thread(receive_thread, &shared_ctxt[mod_id]);
manager = create_link_manager(send_queue, receive_queue, link);
if (manager == NULL) goto error;
#endif
/*
* initilize a timer
*/
enb_agent_init_timer();
/*
* start the enb agent task for tx and interaction with the underlying network function
*/
if (itti_create_task (TASK_ENB_AGENT, enb_agent_task, NULL) < 0) {
LOG_E(ENB_AGENT, "Create task for eNB Agent failed\n");
return -1;
}
memset(&shared_ctxt, 0, sizeof(msg_context_t));
shared_ctxt.tx_mq = send_queue;
shared_ctxt.rx_mq = receive_queue;
#ifdef TEST_TIMER
long timer_id=0;
enb_agent_timer_args_t timer_args;
memset (&timer_args, 0, sizeof(enb_agent_timer_args_t));
timer_args.mod_id = mod_id;
timer_args.cc_actions= ENB_AGENT_ACTION_APPLY;
timer_args.cc_report_flags = PROTOCOL__PRP_CELL_STATS_TYPE__PRCST_NOISE_INTERFERENCE;
timer_args.ue_actions = ENB_AGENT_ACTION_SEND;
timer_args.ue_report_flags = PROTOCOL__PRP_UE_STATS_TYPE__PRUST_BSR | PROTOCOL__PRP_UE_STATS_TYPE__PRUST_DL_CQI;
enb_agent_create_timer(1, 0, ENB_AGENT_DEFAULT, mod_id, ENB_AGENT_TIMER_TYPE_PERIODIC, enb_agent_timeout,(void*)&timer_args, &timer_id);
#endif
new_thread(receive_thread, &shared_ctxt);
// new_thread(send_thread, &shared_ctxt);
// while (1) pause();
//while (1) pause();
printf("client ends\n");
LOG_I(ENB_AGENT,"client ends\n");
return 0;
error:
printf("there was an error\n");
LOG_I(ENB_AGENT,"there was an error\n");
return 1;
}
int enb_agent_stop(){
int enb_agent_stop(mid_t mod_id){
int i=0;
enb_agent_destroy_timers();
for ( i =0; i < enb_agent_info.nb_modules; i++) {
destroy_link_manager(enb_agent[i].manager);
destroy_message_queue(enb_agent[i].send_queue);
destroy_message_queue(enb_agent[i].receive_queue);
close_link(enb_agent[i].link);
}
}
err_code_t enb_agent_timeout(void* args){
enb_agent_timer_args_t *timer_args = (enb_agent_timer_args_t *) args;
LOG_I(ENB_AGENT, "enb_agent %d timeout\n", timer_args->mod_id);
LOG_I(ENB_AGENT, "eNB action %d ENB flags %d \n", timer_args->cc_actions,timer_args->cc_report_flags);
LOG_I(ENB_AGENT, "UE action %d UE flags %d \n", timer_args->ue_actions,timer_args->ue_report_flags);
return 0;
}
......@@ -28,9 +28,9 @@
*******************************************************************************/
/*! \file
* \brief
* \author
/*! \file enb_agent.h
* \brief top level enb agent
* \author Navid Nikaein and Xenofon Foukas
* \date 2016
* \version 0.1
*/
......@@ -38,10 +38,13 @@
#ifndef ENB_AGENT_H_
#define ENB_AGENT_H_
#include "enb_config.h" // for enb properties
#include "enb_agent_common.h"
int enb_agent_start();
int enb_agent_start(mid_t mod_id, const Enb_properties_array_t* enb_properties);
int enb_agent_stop();
int enb_agent_stop(mid_t mod_id);
void *enb_agent_task(void *args);
#endif
......@@ -27,9 +27,9 @@
*******************************************************************************/
/*! \file
* \brief
* \author
/*! \file enb_agent_common.c
* \brief common primitives for all agents
* \author Navid Nikaein and Xenofon Foukas
* \date 2016
* \version 0.1
*/
......@@ -39,6 +39,12 @@
#include "log.h"
void * enb[NUM_MAX_ENB_AGENT];
void * enb_ue[NUM_MAX_ENB_AGENT];
/*
* message primitives
*/
int enb_agent_serialize_message(Protocol__ProgranMessage *msg, void **buf, int *size) {
*size = protocol__progran_message__get_packed_size(msg);
......@@ -52,7 +58,7 @@ int enb_agent_serialize_message(Protocol__ProgranMessage *msg, void **buf, int *
return 0;
error:
LOG_E(ENB_APP, "an error occured\n"); // change the com
LOG_E(ENB_AGENT, "an error occured\n"); // change the com
return -1;
}
......@@ -74,9 +80,7 @@ int enb_agent_deserialize_message(void *data, int size, Protocol__ProgranMessage
int prp_create_header(uint32_t xid, Protocol__PrpType type, Protocol__PrpHeader **header) {
int prp_create_header(xid_t xid, Protocol__PrpType type, Protocol__PrpHeader **header) {
*header = malloc(sizeof(Protocol__PrpHeader));
if(*header == NULL)
......@@ -98,7 +102,7 @@ int prp_create_header(uint32_t xid, Protocol__PrpType type, Protocol__PrpHeader
}
int enb_agent_hello(uint32_t xid, const void *params, Protocol__ProgranMessage **msg) {
int enb_agent_hello(mid_t mod_id, xid_t xid, const void *params, Protocol__ProgranMessage **msg) {
Protocol__PrpHeader *header;
if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_HELLO, &header) != 0)
......@@ -150,7 +154,7 @@ int enb_agent_destroy_hello(Protocol__ProgranMessage *msg) {
}
int enb_agent_echo_request(uint32_t xid, const void* params, Protocol__ProgranMessage **msg) {
int enb_agent_echo_request(mid_t mod_id, xid_t xid, const void* params, Protocol__ProgranMessage **msg) {
Protocol__PrpHeader *header;
if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_ECHO_REQUEST, &header) != 0)
goto error;
......@@ -199,7 +203,7 @@ int enb_agent_destroy_echo_request(Protocol__ProgranMessage *msg) {
int enb_agent_echo_reply(uint32_t xid, const void *params, Protocol__ProgranMessage **msg) {
int enb_agent_echo_reply(mid_t mod_id, xid_t xid, const void *params, Protocol__ProgranMessage **msg) {
Protocol__PrpHeader *header;
if (prp_create_header(xid, PROTOCOL__PRP_TYPE__PRPT_ECHO_REPLY, &header) != 0)
goto error;
......@@ -246,3 +250,263 @@ int enb_agent_destroy_echo_reply(Protocol__ProgranMessage *msg) {
LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
return -1;
}
/*
* get generic info from RAN
*/
void set_enb_vars(mid_t mod_id, ran_name_t ran){
switch (ran){
case RAN_LTE_OAI :
enb[mod_id] = (void *)&eNB_mac_inst[mod_id];
enb_ue[mod_id] = (void *)&eNB_mac_inst[mod_id].UE_list;
break;
default :
goto error;
}
return;
error:
LOG_E(ENB_AGENT, "unknown RAN name %d\n", ran);
}
int get_current_time_ms (mid_t mod_id, int subframe_flag){
if (subframe_flag == 1){
return ((eNB_MAC_INST *)enb[mod_id])->frame*10 + ((eNB_MAC_INST *)enb[mod_id])->subframe;
}else {
return ((eNB_MAC_INST *)enb[mod_id])->frame*10;
}
}
int get_num_ues (mid_t mod_id){
return ((UE_list_t *)enb_ue[mod_id])->num_UEs;
}
int get_ue_crnti (mid_t mod_id, mid_t ue_id){
return ((UE_list_t *)enb_ue[mod_id])->eNB_UE_stats[UE_PCCID(mod_id,ue_id)][ue_id].crnti;
}
int get_ue_bsr (mid_t mod_id, mid_t ue_id, lcid_t lcid) {
return ((UE_list_t *)enb_ue[mod_id])->UE_template[UE_PCCID(mod_id,ue_id)][ue_id].bsr_info[lcid];
}
int get_ue_phr (mid_t mod_id, mid_t ue_id) {
return ((UE_list_t *)enb_ue[mod_id])->UE_template[UE_PCCID(mod_id,ue_id)][ue_id].phr_info;
}
int get_ue_wcqi (mid_t mod_id, mid_t ue_id) {
return ((UE_list_t *)enb_ue[mod_id])->eNB_UE_stats[UE_PCCID(mod_id,ue_id)][ue_id].dl_cqi;
}
/*
* timer primitives
*/
//struct enb_agent_map agent_map;
/* The timer_id might not be the best choice for the comparison */
int enb_agent_compare_timer(struct enb_agent_timer_element_s *a, struct enb_agent_timer_element_s *b){
//if (a->timer_id) return 0;
//if (b->timer_id) return 0;
if (a->timer_id < b->timer_id) return -1;
if (a->timer_id > b->timer_id) return 1;
// equal timers
return 0;
}
RB_GENERATE(enb_agent_map,enb_agent_timer_element_s, entry, enb_agent_compare_timer);
err_code_t enb_agent_init_timer(void){
RB_INIT(&enb_agent_head);
/*
struct enb_agent_timer_element_s e;
memset(&e, 0, sizeof(enb_agent_timer_element_t));
RB_INSERT(enb_agent_map, &agent_map, &e);
*/
return PROTOCOL__PROGRAN_ERR__NO_ERR;
}
err_code_t enb_agent_create_timer(uint32_t interval_sec,
uint32_t interval_usec,
agent_id_t agent_id,
instance_t instance,
uint32_t timer_type,
enb_agent_timer_callback_t cb,
void* timer_args,
long *timer_id){
struct enb_agent_timer_element_s e;
//uint32_t timer_id;
int ret=-1;
if ((interval_sec == 0) && (interval_usec == 0 ))
return TIMER_NULL;
if (timer_type >= ENB_AGENT_TIMER_TYPE_MAX)
return TIMER_TYPE_INVALIDE;
if (timer_type == ENB_AGENT_TIMER_TYPE_ONESHOT){
ret = timer_setup(interval_sec,
interval_usec,
TASK_ENB_AGENT,
instance,
TIMER_ONE_SHOT,
timer_args,
timer_id);
e.type = TIMER_ONE_SHOT;
}
else if (timer_type == ENB_AGENT_TIMER_TYPE_PERIODIC ){
ret = timer_setup(interval_sec,
interval_usec,
TASK_ENB_AGENT,
instance,
TIMER_PERIODIC,
timer_args,
timer_id);
e.type = TIMER_PERIODIC;
}
if (ret < 0 ) {
return TIMER_SETUP_FAILED;
}
e.agent_id = agent_id;
e.instance = instance;
e.state = ENB_AGENT_TIMER_STATE_ACTIVE;
e.timer_id = *timer_id;
e.timer_args = timer_args;
e.cb = cb;
LOG_I(ENB_AGENT,"created a timer with id 0x%lx for agent %d, instance %d \n",
e.timer_id, e.agent_id, e.instance);
RB_INSERT(enb_agent_map, &enb_agent_head, &e);
/*
struct enb_agent_timer_element_s search;
search.timer_id = *timer_id;
printf("search 1: %p (expected %p)\n", RB_FIND(enb_agent_map, &enb_agent_head, &search), &e);
printf("search 1: %p (expected %p)\n", get_timer_entry(e.timer_id), &e);
*/
return 0;
}
err_code_t enb_agent_destroy_timer(long timer_id){
struct enb_agent_timer_element_s *e = get_timer_entry(timer_id);
if (e != NULL ) {
RB_REMOVE(enb_agent_map, &enb_agent_head, &e);
free(e);
}
if (timer_remove(timer_id) < 0 )
goto error;
return 0;
error:
LOG_E(ENB_AGENT, "timer can't be removed\n");
return TIMER_REMOVED_FAILED ;
}
err_code_t enb_agent_destroy_timers(void){
struct enb_agent_timer_element_s *e = NULL;
RB_FOREACH(e, enb_agent_map, &enb_agent_head) {
RB_REMOVE(enb_agent_map, &enb_agent_head, e);
timer_remove(e->timer_id);
free(e);
}
return 0;
}
struct enb_agent_timer_element_s * get_timer_entry(long timer_id) {
struct enb_agent_timer_element_s search, *e;
memset(&search, 0, sizeof(struct enb_agent_timer_element_s));
search.timer_id = timer_id;
return RB_FIND(enb_agent_map, &enb_agent_head, &search);
}
/*
int i =0;
RB_FOREACH(e, enb_agent_map, &enb_agent_head) {
printf("%d: %p\n", i, e); i++;
}
*/
/*
err_code_t enb_agent_stop_timer(uint32_t timer_id){
struct enb_agent_timer_element_s *e=NULL;
RB_FOREACH(e, enb_agent_map, &enb_agent_head) {
if (e->timer_id == timer_id)
break;
}
if (e != NULL ) {
e->state = ENB_AGENT_TIMER_STATE_STOPPED;
}
timer_remove(timer_id);
return 0;
}
// this will change the timer_id
err_code_t enb_agent_restart_timer(uint32_t *timer_id){
struct enb_agent_timer_element_s *e=NULL;
RB_FOREACH(e, enb_agent_map, &enb_agent_head) {
if (e->timer_id == timer_id)
break;
}
if (e != NULL ) {
e->state = ENB_AGENT_TIMER_STATE_ACTIVE;
}
ret = timer_setup(e->interval_sec,
e->interval_usec,
e->agent_id,