flexran_agent_mac.c 75.5 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
 * 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
Cedric Roux's avatar
Cedric Roux committed
20
 */
21

22 23
/*! \file flexran_agent_mac.c
 * \brief FlexRAN agent message handler for MAC layer
24
 * \author Xenofon Foukas, Mohamed Kassem and Navid Nikaein
25 26 27 28
 * \date 2016
 * \version 0.1
 */

29 30 31 32
#include "flexran_agent_mac.h"
#include "flexran_agent_extern.h"
#include "flexran_agent_common.h"
#include "flexran_agent_mac_internal.h"
33
#include "flexran_agent_net_comm.h"
34
#include "flexran_agent_timer.h"
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
35
#include "flexran_agent_ran_api.h"
36

37
#include "LAYER2/MAC/mac_proto.h"
38

39 40
#include "liblfds700.h"

41
#include "common/utils/LOG/log.h"
42

43 44
#include <dlfcn.h>

Raymond Knopp's avatar
Raymond Knopp committed
45 46 47
#include "common/ran_context.h"
extern RAN_CONTEXT_t RC;

48 49 50
/*Array containing the Agent-MAC interfaces*/
AGENT_MAC_xface *agent_mac_xface[NUM_MAX_ENB];

51 52 53 54 55 56
/* Ringbuffer related structs used for maintaining the dl mac config messages */
//message_queue_t *dl_mac_config_queue;
struct lfds700_misc_prng_state ps[NUM_MAX_ENB];
struct lfds700_ringbuffer_element *dl_mac_config_array[NUM_MAX_ENB];
struct lfds700_ringbuffer_state ringbuffer_state[NUM_MAX_ENB];

57 58 59 60
/* Ringbuffer-related to slice configuration */
struct lfds700_ringbuffer_element *slice_config_array[NUM_MAX_ENB];
struct lfds700_ringbuffer_state slice_config_ringbuffer_state[NUM_MAX_ENB];

61 62 63 64 65 66 67
/* Ringbuffer related to slice configuration if we need to wait for additional
 * controller information */
struct lfds700_ringbuffer_element *store_slice_config_array[NUM_MAX_ENB];
struct lfds700_ringbuffer_state store_slice_config_ringbuffer_state[NUM_MAX_ENB];
/* stores the (increasing) xid for messages to prevent double use */
int xid = 50;

68 69 70 71
/* Ringbuffer-related to slice configuration */
struct lfds700_ringbuffer_element *ue_assoc_array[NUM_MAX_ENB];
struct lfds700_ringbuffer_state ue_assoc_ringbuffer_state[NUM_MAX_ENB];

72 73 74
/* a list of shared objects that are loaded into the user plane */
SLIST_HEAD(flexran_so_handle, flexran_agent_so_handle_s) flexran_handles[NUM_MAX_ENB];

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
int flexran_agent_mac_stats_reply_ue(mid_t mod_id,
                                    Protocol__FlexUeStatsReport **ue_report,
                                    int      n_ue,
                                    uint32_t ue_flags) {
  const int cc_id = 0; // TODO: make for multiple CCs

  for (int i = 0; i < n_ue; i++) {
    const rnti_t rnti = ue_report[i]->rnti;
    const int UE_id = flexran_get_mac_ue_id_rnti(mod_id, rnti);

    /* buffer status report */
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_BSR) {
      ue_report[i]->n_bsr = 4; //TODO should be automated
      uint32_t *elem = (uint32_t *) malloc(sizeof(uint32_t)*ue_report[i]->n_bsr);
      if (elem == NULL)
        goto error;
      for (int j = 0; j < ue_report[i]->n_bsr; j++) {
        elem[j] = flexran_get_ue_bsr_ul_buffer_info (mod_id, i, j);
      }
      ue_report[i]->bsr = elem;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_BSR;
    }
97

98 99 100 101 102 103
    /* power headroom report */
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PHR) {
      ue_report[i]->phr = flexran_get_ue_phr (mod_id, UE_id);
      ue_report[i]->has_phr = 1;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PHR;
    }
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    /* Check flag for creation of RLC buffer status report */
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RLC_BS) {
      ue_report[i]->n_rlc_report = 3; // Set this to the number of LCs for this UE. This needs to be generalized for for LCs
      Protocol__FlexRlcBsr ** rlc_reports;
      rlc_reports = malloc(sizeof(Protocol__FlexRlcBsr *) * ue_report[i]->n_rlc_report);
      if (rlc_reports == NULL)
        goto error;

      for (int j = 0; j < ue_report[i]->n_rlc_report; j++) {
        rlc_reports[j] = malloc(sizeof(Protocol__FlexRlcBsr));
        if (rlc_reports[j] == NULL) {
          for (int k = 0; k < j; k++) {
            free(rlc_reports[k]);
          }
          free(rlc_reports);
          goto error;
        }
        protocol__flex_rlc_bsr__init(rlc_reports[j]);
        rlc_reports[j]->lc_id = j+1;
        rlc_reports[j]->has_lc_id = 1;
        rlc_reports[j]->tx_queue_size = flexran_get_tx_queue_size(mod_id, UE_id, j + 1);
        rlc_reports[j]->has_tx_queue_size = 1;

        rlc_reports[j]->tx_queue_hol_delay = flexran_get_hol_delay(mod_id, UE_id, j + 1);
        rlc_reports[j]->has_tx_queue_hol_delay = 1;
        //TODO:Set retransmission queue size in bytes
        rlc_reports[j]->retransmission_queue_size = 10;
        rlc_reports[j]->has_retransmission_queue_size = 0;
        //TODO:Set retransmission queue head of line delay in ms
        rlc_reports[j]->retransmission_queue_hol_delay = 100;
        rlc_reports[j]->has_retransmission_queue_hol_delay = 0;
        rlc_reports[j]->status_pdu_size = flexran_get_num_pdus_buffer(mod_id, UE_id, j + 1);
        rlc_reports[j]->has_status_pdu_size = 1;
138

139 140 141 142 143
      }
      if (ue_report[i]->n_rlc_report > 0)
        ue_report[i]->rlc_report = rlc_reports;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_RLC_BS;
    }
144

145 146 147 148 149 150 151 152 153 154 155
    /* MAC CE buffer status report */
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_CE_BS) {
      // TODO: Fill in the actual MAC CE buffer status report
      ue_report[i]->pending_mac_ces = (flexran_get_MAC_CE_bitmap_TA(mod_id, UE_id, 0) | (0 << 1) | (0 << 2) | (0 << 3)) & 15;
      // Use as bitmap. Set one or more of the;
      // PROTOCOL__FLEX_CE_TYPE__FLPCET_ values
      // found in stats_common.pb-c.h. See
      // flex_ce_type in FlexRAN specification
      ue_report[i]->has_pending_mac_ces = 1;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_CE_BS;
    }
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    /* DL CQI report */
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI) {
      // TODO: Fill in the actual DL CQI report for the UE based on its configuration
      Protocol__FlexDlCqiReport * dl_report;
      dl_report = malloc(sizeof(Protocol__FlexDlCqiReport));
      if (dl_report == NULL)
        goto error;
      protocol__flex_dl_cqi_report__init(dl_report);

      dl_report->sfn_sn = flexran_get_sfn_sf(mod_id);
      dl_report->has_sfn_sn = 1;
      dl_report->n_csi_report = flexran_get_active_CC(mod_id, UE_id);
      dl_report->n_csi_report = 1 ;
      //Create the actual CSI reports.
      Protocol__FlexDlCsi **csi_reports;
      csi_reports = malloc(sizeof(Protocol__FlexDlCsi *)*dl_report->n_csi_report);
      if (csi_reports == NULL) {
        free(dl_report);
        goto error;
      }
      for (int j = 0; j < dl_report->n_csi_report; j++) {
        csi_reports[j] = malloc(sizeof(Protocol__FlexDlCsi));
        if (csi_reports[j] == NULL) {
          for (int k = 0; k < j; k++) {
            free(csi_reports[k]);
          }
          free(csi_reports);
          csi_reports = NULL;
          goto error;
        }
        protocol__flex_dl_csi__init(csi_reports[j]);
        csi_reports[j]->serv_cell_index = j;
        csi_reports[j]->has_serv_cell_index = 1;
        csi_reports[j]->ri = flexran_get_current_RI(mod_id, UE_id, j);
        csi_reports[j]->has_ri = 1;
        //TODO: the type of CSI report based on the configuration of the UE
        //For now we only support type P10, which only needs a wideband value
        csi_reports[j]->type =  PROTOCOL__FLEX_CSI_TYPE__FLCSIT_P10;
        csi_reports[j]->has_type = 1;
        csi_reports[j]->report_case = PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI;

        if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI) {
          Protocol__FlexCsiP10 *csi10;
          csi10 = malloc(sizeof(Protocol__FlexCsiP10));
          if (csi10 == NULL) {
            for (int k = 0; k <= j; k++) {
              free(csi_reports[k]);
            }
            free(csi_reports);
            csi_reports = NULL;
            goto error;
          }
          protocol__flex_csi_p10__init(csi10);
          csi10->wb_cqi = flexran_get_ue_wcqi(mod_id, UE_id);
          csi10->has_wb_cqi = 1;
          csi_reports[j]->p10csi = csi10;
        }
214

215 216
        /*
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P11CSI) {
217 218


219 220 221 222 223 224 225 226 227 228 229
          Protocol__FlexCsiP11 *csi11;
          csi11 = malloc(sizeof(Protocol__FlexCsiP11));
          if (csi11 == NULL) {
            for (int k = 0; k <= j; k++) {
              free(csi_reports[k]);
            }
            free(csi_reports);
            csi_reports = NULL;
            goto error;
          }
          protocol__flex_csi_p11__init(csi11);
230

231 232 233 234
          csi11->wb_cqi = malloc(sizeof(csi11->wb_cqi));
          csi11->n_wb_cqi = 1;
          csi11->wb_cqi[0] = flexran_get_ue_wcqi (mod_id, UE_id);
          // According To spec 36.213
235

236 237 238 239
          if (flexran_get_antenna_ports(mod_id, j) == 2 && csi_reports[j]->ri == 1) {
            // TODO PMI
            csi11->wb_pmi = flexran_get_ue_wpmi(mod_id, UE_id, 0);
            csi11->has_wb_pmi = 1;
240

241
          }
242

243 244 245 246
          else if (flexran_get_antenna_ports(mod_id, j) == 2 && csi_reports[j]->ri == 2) {
            // TODO PMI
            csi11->wb_pmi = flexran_get_ue_wpmi(mod_id, UE_id, 0);
            csi11->has_wb_pmi = 1;
247

248
          }
249

250 251 252 253
          else if (flexran_get_antenna_ports(mod_id, j) == 4 && csi_reports[j]->ri == 2) {
            // TODO PMI
            csi11->wb_pmi = flexran_get_ue_wpmi(mod_id, UE_id, 0);
            csi11->has_wb_pmi = 1;
Cedric Roux's avatar
Cedric Roux committed
254

255

256
          }
Cedric Roux's avatar
Cedric Roux committed
257

258 259 260 261
          csi11->has_wb_pmi = 0;
          csi_reports[j]->p11csi = csi11;
        }
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P20CSI) {
262

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
          Protocol__FlexCsiP20 *csi20;
          csi20 = malloc(sizeof(Protocol__FlexCsiP20));
          if (csi20 == NULL) {
            for (int k = 0; k <= j; k++) {
              free(csi_reports[k]);
            }
            free(csi_reports);
            csi_reports = NULL;
            goto error;
          }
          protocol__flex_csi_p20__init(csi20);

          csi20->wb_cqi = flexran_get_ue_wcqi (mod_id, UE_id);
          csi20->has_wb_cqi = 1;
          csi20->bandwidth_part_index = 1 ; //TODO
          csi20->has_bandwidth_part_index = 0;
          csi20->sb_index = 1; //TODO
          csi20->has_sb_index = 1;
          csi_reports[j]->p20csi = csi20;
        }
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_P21CSI) {
284

285 286 287 288 289
          // Protocol__FlexCsiP21 *csi21;
          // csi21 = malloc(sizeof(Protocol__FlexCsiP21));
          // if (csi21 == NULL)
          // goto error;
          // protocol__flex_csi_p21__init(csi21);
290

291
          // csi21->wb_cqi = flexran_get_ue_wcqi (mod_id, UE_id);
292 293


294 295
          // csi21->wb_pmi = flexran_get_ue_pmi(mod_id); //TDO inside
          // csi21->has_wb_pmi = 1;
296

297
          // csi21->sb_cqi = 1; // TODO
298

299 300
          // csi21->bandwidth_part_index = 1 ; //TDO inside
          // csi21->has_bandwidth_part_index = 1 ;
301

302 303
          // csi21->sb_index = 1 ;//TODO
          // csi21->has_sb_index = 1 ;
304 305


306
          // csi_reports[j]->p20csi = csi21;
307

308
        }
309

310
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A12CSI) {
311 312


313 314 315 316 317
          // Protocol__FlexCsiA12 *csi12;
          // csi12 = malloc(sizeof(Protocol__FlexCsiA12));
          // if (csi12 == NULL)
          // goto error;
          // protocol__flex_csi_a12__init(csi12);
318

319
          // csi12->wb_cqi = flexran_get_ue_wcqi (mod_id, UE_id);
320

321
          // csi12->sb_pmi = 1 ; //TODO inside
322

323 324
          // TODO continou
        }
325

326
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A22CSI) {
327

328 329 330 331 332
          // Protocol__FlexCsiA22 *csi22;
          // csi22 = malloc(sizeof(Protocol__FlexCsiA22));
          // if (csi22 == NULL)
          // goto error;
          // protocol__flex_csi_a22__init(csi22);
333

334
          // csi22->wb_cqi = flexran_get_ue_wcqi (mod_id, UE_id);
335

336
          // csi22->sb_cqi = 1 ; //TODO inside
Cedric Roux's avatar
Cedric Roux committed
337

338 339
          // csi22->wb_pmi = flexran_get_ue_wcqi (mod_id, UE_id);
          // csi22->has_wb_pmi = 1;
340

341 342
          // csi22->sb_pmi = 1 ; //TODO inside
          // csi22->has_wb_pmi = 1;
Cedric Roux's avatar
Cedric Roux committed
343

344
          // csi22->sb_list = flexran_get_ue_wcqi (mod_id, UE_id);
345 346


347
        }
348

349
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A20CSI) {
350

351 352 353 354 355
          // Protocol__FlexCsiA20 *csi20;
          // csi20 = malloc(sizeof(Protocol__FlexCsiA20));
          // if (csi20 == NULL)
          // goto error;
          // protocol__flex_csi_a20__init(csi20);
356

357 358
          // csi20->wb_cqi = flexran_get_ue_wcqi (mod_id, UE_id);
          // csi20->has_wb_cqi = 1;
359

360 361
          // csi20>sb_cqi = 1 ; //TODO inside
          // csi20>has_sb_cqi = 1 ;
Cedric Roux's avatar
Cedric Roux committed
362

363
          // csi20->sb_list = 1; // TODO inside
Cedric Roux's avatar
Cedric Roux committed
364 365


366
        }
Cedric Roux's avatar
Cedric Roux committed
367

368
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A30CSI) {
Cedric Roux's avatar
Cedric Roux committed
369

370
        }
371

372
        else if (csi_reports[j]->report_case == PROTOCOL__FLEX_DL_CSI__REPORT_A31CSI) {
373

374 375
        }
        */
376

377 378 379 380 381
      }
      dl_report->csi_report = csi_reports;
      ue_report[i]->dl_cqi_report = dl_report;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI;
    }
Cedric Roux's avatar
Cedric Roux committed
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    /* paging buffer status report */
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PBS) {
      //TODO: Fill in the actual paging buffer status report. For this field to be valid, the RNTI
      //set in the report must be a P-RNTI
      Protocol__FlexPagingBufferReport *paging_report;
      paging_report = malloc(sizeof(Protocol__FlexPagingBufferReport));
      if (paging_report == NULL)
        goto error;
      protocol__flex_paging_buffer_report__init(paging_report);
      paging_report->n_paging_info = 1;
      Protocol__FlexPagingInfo **p_info;
      p_info = malloc(sizeof(Protocol__FlexPagingInfo *) * paging_report->n_paging_info);
      if (p_info == NULL) {
        free(paging_report);
        goto error;
      }
Cedric Roux's avatar
Cedric Roux committed
399

400
      for (int j = 0; j < paging_report->n_paging_info; j++) {
401

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        p_info[j] = malloc(sizeof(Protocol__FlexPagingInfo));
        if (p_info[j] == NULL) {
          for (int k = 0; k < j; k++) {
            free(p_info[k]);
          }
          free(p_info);
          p_info = NULL;
          free(paging_report);
          goto error;
        }
        protocol__flex_paging_info__init(p_info[j]);
        //TODO: Set paging index. This index is the same that will be used for the scheduling of the
        //paging message by the controller
        p_info[j]->paging_index = 10;
        p_info[j]->has_paging_index = 0;
        //TODO:Set the paging message size
        p_info[j]->paging_message_size = 100;
        p_info[j]->has_paging_message_size = 0;
        //TODO: Set the paging subframe
        p_info[j]->paging_subframe = 10;
        p_info[j]->has_paging_subframe = 0;
        //TODO: Set the carrier index for the pending paging message
        p_info[j]->carrier_index = 0;
        p_info[j]->has_carrier_index = 0;
Cedric Roux's avatar
Cedric Roux committed
426

427 428 429 430 431 432 433
      }
      //Add all paging info to the paging buffer rerport
      paging_report->paging_info = p_info;
      //Add the paging report to the UE report
      ue_report[i]->pbr = paging_report;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PBS;
    }
Cedric Roux's avatar
Cedric Roux committed
434

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    /* Check flag for creation of UL CQI report */
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI) {

      //Fill in the full UL CQI report of the UE
      Protocol__FlexUlCqiReport *full_ul_report;
      full_ul_report = malloc(sizeof(Protocol__FlexUlCqiReport));
      if (full_ul_report == NULL)
        goto error;
      protocol__flex_ul_cqi_report__init(full_ul_report);
      full_ul_report->sfn_sn = flexran_get_sfn_sf(mod_id);
      full_ul_report->has_sfn_sn = 1;
      //TODO:Set the number of UL measurement reports based on the types of measurements
      //configured for this UE and on the servCellIndex
      full_ul_report->n_cqi_meas = 1;
      Protocol__FlexUlCqi **ul_report;
      ul_report = malloc(sizeof(Protocol__FlexUlCqi *) * full_ul_report->n_cqi_meas);
      if (ul_report == NULL) {
        free(full_ul_report);
        goto error;
      }
      //Fill each UL report of the UE for each of the configured report types
      for (int j = 0; j < full_ul_report->n_cqi_meas; j++) {
457

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
        ul_report[j] = malloc(sizeof(Protocol__FlexUlCqi));
        if (ul_report[j] == NULL) {
          for (int k = 0; k < j; k++) {
            free(ul_report[k]);
          }
          free(ul_report);
          free(full_ul_report);
          goto error;
        }
        protocol__flex_ul_cqi__init(ul_report[j]);
        //TODO: Set the type of the UL report. As an example set it to SRS UL report
        ul_report[j]->type = PROTOCOL__FLEX_UL_CQI_TYPE__FLUCT_SRS;
        ul_report[j]->has_type = 1;
        //TODO:Set the number of SINR measurements based on the report type
        ul_report[j]->n_sinr = 0;
        uint32_t *sinr_meas;
        sinr_meas = (uint32_t *) malloc(sizeof(uint32_t) * ul_report[j]->n_sinr);
        if (sinr_meas == NULL) {
          for (int k = 0; k < j; k++) {
            free(ul_report[k]);
          }
          free(ul_report);
          free(full_ul_report);
          goto error;
        }
        //TODO:Set the SINR measurements for the specified type
        for (int k = 0; k < ul_report[j]->n_sinr; k++) {
          sinr_meas[k] = 10;
        }
        ul_report[j]->sinr = sinr_meas;
        //TODO: Set the servCellIndex for this report
        ul_report[j]->serv_cell_index = 0;
        ul_report[j]->has_serv_cell_index = 1;
491

492 493
        //Set the list of UL reports of this UE to the full UL report
        full_ul_report->cqi_meas = ul_report;
494

495 496
        full_ul_report->n_pucch_dbm = MAX_NUM_CCs;
        full_ul_report->pucch_dbm = malloc(sizeof(Protocol__FlexPucchDbm *) * full_ul_report->n_pucch_dbm);
497

498
        for (int j = 0; j < MAX_NUM_CCs; j++) {
Cedric Roux's avatar
Cedric Roux committed
499

500 501 502 503
          full_ul_report->pucch_dbm[j] = malloc(sizeof(Protocol__FlexPucchDbm));
          protocol__flex_pucch_dbm__init(full_ul_report->pucch_dbm[j]);
          full_ul_report->pucch_dbm[j]->has_serv_cell_index = 1;
          full_ul_report->pucch_dbm[j]->serv_cell_index = j;
Cedric Roux's avatar
Cedric Roux committed
504

505 506 507 508 509
          if (flexran_get_p0_pucch_dbm(mod_id, UE_id, j) != -1) {
            full_ul_report->pucch_dbm[j]->p0_pucch_dbm = flexran_get_p0_pucch_dbm(mod_id, UE_id, j);
            full_ul_report->pucch_dbm[j]->has_p0_pucch_dbm = 1;
          }
        }
Cedric Roux's avatar
Cedric Roux committed
510

511

512 513 514 515
      }
      //  Add full UL CQI report to the UE report
      ue_report[i]->ul_cqi_report = full_ul_report;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI;
516

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    }
    if (ue_flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_STATS) {

      Protocol__FlexMacStats *macstats;
      macstats = malloc(sizeof(Protocol__FlexMacStats));
      if (macstats == NULL)
        goto error;
      protocol__flex_mac_stats__init(macstats);
      macstats->total_bytes_sdus_dl = flexran_get_total_size_dl_mac_sdus(mod_id, UE_id, cc_id);
      macstats->has_total_bytes_sdus_dl = 1;
      macstats->total_bytes_sdus_ul = flexran_get_total_size_ul_mac_sdus(mod_id, UE_id, cc_id);
      macstats->has_total_bytes_sdus_ul = 1;
      macstats->tbs_dl = flexran_get_TBS_dl(mod_id, UE_id, cc_id);
      macstats->has_tbs_dl = 1;
      macstats->tbs_ul = flexran_get_TBS_ul(mod_id, UE_id, cc_id);
      macstats->has_tbs_ul = 1;
      macstats->prb_retx_dl = flexran_get_num_prb_retx_dl_per_ue(mod_id, UE_id, cc_id);
      macstats->has_prb_retx_dl = 1;
      macstats->prb_retx_ul = flexran_get_num_prb_retx_ul_per_ue(mod_id, UE_id, cc_id);
      macstats->has_prb_retx_ul = 1;
      macstats->prb_dl = flexran_get_num_prb_dl_tx_per_ue(mod_id, UE_id, cc_id);
      macstats->has_prb_dl = 1;
      macstats->prb_ul = flexran_get_num_prb_ul_rx_per_ue(mod_id, UE_id, cc_id);
      macstats->has_prb_ul = 1;
      macstats->mcs1_dl = flexran_get_mcs1_dl(mod_id, UE_id, cc_id);
      macstats->has_mcs1_dl = 1;
      macstats->mcs2_dl = flexran_get_mcs2_dl(mod_id, UE_id, cc_id);
      macstats->has_mcs2_dl = 1;
      macstats->mcs1_ul = flexran_get_mcs1_ul(mod_id, UE_id, cc_id);
      macstats->has_mcs1_ul = 1;
      macstats->mcs2_ul = flexran_get_mcs2_ul(mod_id, UE_id, cc_id);
      macstats->has_mcs2_ul = 1;
      macstats->total_prb_dl = flexran_get_total_prb_dl_tx_per_ue(mod_id, UE_id, cc_id);
      macstats->has_total_prb_dl = 1;
      macstats->total_prb_ul = flexran_get_total_prb_ul_rx_per_ue(mod_id, UE_id, cc_id);
      macstats->has_total_prb_ul = 1;
      macstats->total_pdu_dl = flexran_get_total_num_pdu_dl(mod_id, UE_id, cc_id);
      macstats->has_total_pdu_dl = 1;
      macstats->total_pdu_ul = flexran_get_total_num_pdu_ul(mod_id, UE_id, cc_id);
      macstats->has_total_pdu_ul = 1;
      macstats->total_tbs_dl = flexran_get_total_TBS_dl(mod_id, UE_id, cc_id);
      macstats->has_total_tbs_dl = 1;
      macstats->total_tbs_ul = flexran_get_total_TBS_ul(mod_id, UE_id, cc_id);
      macstats->has_total_tbs_ul = 1;
      macstats->harq_round = flexran_get_harq_round(mod_id, cc_id, UE_id);
      macstats->has_harq_round = 1;

      Protocol__FlexMacSdusDl ** mac_sdus;
      mac_sdus = malloc(sizeof(Protocol__FlexMacSdusDl) * flexran_get_num_mac_sdu_tx(mod_id, UE_id, cc_id));
      if (mac_sdus == NULL) {
        free(macstats);
        goto error;
      }
570

571 572 573 574 575 576 577 578
      macstats->n_mac_sdus_dl = flexran_get_num_mac_sdu_tx(mod_id, UE_id, cc_id);
      for (int j = 0; j < macstats->n_mac_sdus_dl; j++) {
        mac_sdus[j] = malloc(sizeof(Protocol__FlexMacSdusDl));
        protocol__flex_mac_sdus_dl__init(mac_sdus[j]);
        mac_sdus[j]->lcid = flexran_get_mac_sdu_lcid_index(mod_id, UE_id, cc_id, j);
        mac_sdus[j]->has_lcid = 1;
        mac_sdus[j]->sdu_length = flexran_get_mac_sdu_size(mod_id, UE_id, cc_id, mac_sdus[j]->lcid);
        mac_sdus[j]->has_sdu_length = 1;
579
      }
580 581 582
      macstats->mac_sdus_dl = mac_sdus;
      ue_report[i]->mac_stats = macstats;
      ue_report[i]->flags |= PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_STATS;
583 584
    }
  }
585
  return 0;
586

587
error:
588
  if (ue_report != NULL) {
589
    if (n_ue > 0) {
590
      for (int i = 0; i < n_ue; i++) {
591 592 593 594 595
        if (ue_report[i]->bsr != NULL) {
          free(ue_report[i]->bsr);
          ue_report[i]->bsr = NULL;
        }
        if (ue_report[i]->rlc_report != NULL) {
596
          for (int j = 0; j < ue_report[i]->n_rlc_report; j++) {
597 598 599 600 601 602 603 604 605 606
            if (ue_report[i]->rlc_report[j] != NULL) {
              free(ue_report[i]->rlc_report[j]);
              ue_report[i]->rlc_report[j] = NULL;
            }
          }
          free(ue_report[i]->rlc_report);
          ue_report[i]->rlc_report = NULL;
        }
        if (ue_report[i]->dl_cqi_report != NULL) {
          if (ue_report[i]->dl_cqi_report->csi_report != NULL) {
607
            for (int j = 0; j < ue_report[i]->dl_cqi_report->n_csi_report; j++) {
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
              if (ue_report[i]->dl_cqi_report->csi_report[j] != NULL) {
                if (ue_report[i]->dl_cqi_report->csi_report[j]->p10csi != NULL) {
                  free(ue_report[i]->dl_cqi_report->csi_report[j]->p10csi);
                  ue_report[i]->dl_cqi_report->csi_report[j]->p10csi = NULL;
                }
                if (ue_report[i]->dl_cqi_report->csi_report[j]->p11csi != NULL) {
                  if (ue_report[i]->dl_cqi_report->csi_report[j]->p11csi->wb_cqi != NULL) {
                    free(ue_report[i]->dl_cqi_report->csi_report[j]->p11csi->wb_cqi);
                    ue_report[i]->dl_cqi_report->csi_report[j]->p11csi->wb_cqi = NULL;
                  }
                  free(ue_report[i]->dl_cqi_report->csi_report[j]->p11csi);
                  ue_report[i]->dl_cqi_report->csi_report[j]->p11csi = NULL;
                }
                if (ue_report[i]->dl_cqi_report->csi_report[j]->p20csi != NULL) {
                  free(ue_report[i]->dl_cqi_report->csi_report[j]->p20csi);
                  ue_report[i]->dl_cqi_report->csi_report[j]->p20csi = NULL;
                }
                free(ue_report[i]->dl_cqi_report->csi_report[j]);
                ue_report[i]->dl_cqi_report->csi_report[j] = NULL;
              }
            }
            free(ue_report[i]->dl_cqi_report->csi_report);
            ue_report[i]->dl_cqi_report->csi_report = NULL;
          }
          free(ue_report[i]->dl_cqi_report);
          ue_report[i]->dl_cqi_report = NULL;
        }
        if (ue_report[i]->pbr != NULL) {
          if (ue_report[i]->pbr->paging_info != NULL) {
637
            for (int j = 0; j < ue_report[i]->pbr->n_paging_info; j++) {
638 639 640 641 642 643 644 645 646 647 648
              free(ue_report[i]->pbr->paging_info[j]);
              ue_report[i]->pbr->paging_info[j] = NULL;
            }
            free(ue_report[i]->pbr->paging_info);
            ue_report[i]->pbr->paging_info = NULL;
          }
          free(ue_report[i]->pbr);
          ue_report[i]->pbr = NULL;
        }
        if (ue_report[i]->ul_cqi_report != NULL) {
          if (ue_report[i]->ul_cqi_report->cqi_meas != NULL) {
649
            for (int j = 0; j < ue_report[i]->ul_cqi_report->n_cqi_meas; j++) {
650 651 652 653 654 655 656 657 658 659 660 661 662
              if (ue_report[i]->ul_cqi_report->cqi_meas[j] != NULL) {
                if (ue_report[i]->ul_cqi_report->cqi_meas[j]->sinr != NULL) {
                  free(ue_report[i]->ul_cqi_report->cqi_meas[j]->sinr);
                  ue_report[i]->ul_cqi_report->cqi_meas[j]->sinr = NULL;
                }
                free(ue_report[i]->ul_cqi_report->cqi_meas[j]);
                ue_report[i]->ul_cqi_report->cqi_meas[j] = NULL;
              }
            }
            free(ue_report[i]->ul_cqi_report->cqi_meas);
            ue_report[i]->ul_cqi_report->cqi_meas = NULL;
          }
          if (ue_report[i]->ul_cqi_report->pucch_dbm != NULL) {
663
            for (int j = 0; j < MAX_NUM_CCs; j++) {
664 665 666 667 668 669 670 671 672 673 674 675 676
              if (ue_report[i]->ul_cqi_report->pucch_dbm[j] != NULL) {
                free(ue_report[i]->ul_cqi_report->pucch_dbm[j]);
                ue_report[i]->ul_cqi_report->pucch_dbm[j] = NULL;
              }
            }
            free(ue_report[i]->ul_cqi_report->pucch_dbm);
            ue_report[i]->ul_cqi_report->pucch_dbm = NULL;
          }
          free(ue_report[i]->ul_cqi_report);
          ue_report[i]->ul_cqi_report = NULL;
        }
        if (ue_report[i]->mac_stats != NULL) {
          if (ue_report[i]->mac_stats->mac_sdus_dl != NULL) {
677
            for (int j = 0; j < ue_report[i]->mac_stats->n_mac_sdus_dl; j++) {
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
              if (ue_report[i]->mac_stats->mac_sdus_dl[j] != NULL) {
                free(ue_report[i]->mac_stats->mac_sdus_dl[j]);
                ue_report[i]->mac_stats->mac_sdus_dl[j] = NULL;
              }
            }
            free(ue_report[i]->mac_stats->mac_sdus_dl);
            ue_report[i]->mac_stats->mac_sdus_dl = NULL;
          }
          free(ue_report[i]->mac_stats);
          ue_report[i]->mac_stats = NULL;
        }
      }
    }
    free(ue_report);
  }
693 694 695 696 697 698 699 700 701 702 703 704 705
  return -1;
}

int flexran_agent_mac_stats_reply_cell(mid_t mod_id,
                                       Protocol__FlexCellStatsReport **cell_report,
                                       int      n_cc,
                                       uint32_t cc_flags) {
  for (int i = 0; i < n_cc; i++) {
    /* noise and interference report */
    if(cc_flags & PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE) {
      // TODO: Fill in the actual noise and interference report for this cell
      Protocol__FlexNoiseInterferenceReport *ni_report;
      ni_report = malloc(sizeof(Protocol__FlexNoiseInterferenceReport));
706
      AssertFatal(ni_report, "cannot malloc() ni_report\n");
707 708 709 710 711 712 713 714 715 716 717 718 719 720
      protocol__flex_noise_interference_report__init(ni_report);
      ni_report->sfn_sf = flexran_get_sfn_sf(mod_id);
      ni_report->has_sfn_sf = 1;
      ni_report->rip = 0; //TODO: Received interference power in dbm
      ni_report->has_rip = 0;
      ni_report->tnp = 0; //TODO: Thermal noise power in dbm
      ni_report->has_tnp = 0;
      ni_report->p0_nominal_pucch = flexran_get_p0_nominal_pucch(mod_id, 0);
      ni_report->has_p0_nominal_pucch = 1;
      cell_report[i]->noise_inter_report = ni_report;
      cell_report[i]->flags |= PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE;
    }
  }
  return 0;
721 722
}

723
int flexran_agent_mac_destroy_stats_reply(Protocol__FlexStatsReply *reply) {
724
  int i, j, k;
725

726 727 728
  Protocol__FlexDlCqiReport *dl_report;
  Protocol__FlexUlCqiReport *ul_report;
  Protocol__FlexPagingBufferReport *paging_report;
729 730 731 732 733 734 735 736 737

  // Free the memory for the UE reports
  for (i = 0; i < reply->n_ue_report; i++) {
    free(reply->ue_report[i]->bsr);
    for (j = 0; j < reply->ue_report[i]->n_rlc_report; j++) {
      free(reply->ue_report[i]->rlc_report[j]);
    }
    free(reply->ue_report[i]->rlc_report);
    // If DL CQI report flag was set
738
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_DL_CQI) {
739 740 741 742 743
      dl_report = reply->ue_report[i]->dl_cqi_report;
      // Delete all CSI reports
      for (j = 0; j < dl_report->n_csi_report; j++) {
	//Must free memory based on the type of report
	switch(dl_report->csi_report[j]->report_case) {
744
	case PROTOCOL__FLEX_DL_CSI__REPORT_P10CSI:
745 746
	  free(dl_report->csi_report[j]->p10csi);
	  break;
747
	case PROTOCOL__FLEX_DL_CSI__REPORT_P11CSI:
748 749 750
	  free(dl_report->csi_report[j]->p11csi->wb_cqi);
	  free(dl_report->csi_report[j]->p11csi);
	  break;
751
	case PROTOCOL__FLEX_DL_CSI__REPORT_P20CSI:
752 753
	  free(dl_report->csi_report[j]->p20csi);
	  break;
754
	case PROTOCOL__FLEX_DL_CSI__REPORT_P21CSI:
755 756 757 758
	  free(dl_report->csi_report[j]->p21csi->wb_cqi);
	  free(dl_report->csi_report[j]->p21csi->sb_cqi);
	  free(dl_report->csi_report[j]->p21csi);
	  break;
759
	case PROTOCOL__FLEX_DL_CSI__REPORT_A12CSI:
760 761 762 763
	  free(dl_report->csi_report[j]->a12csi->wb_cqi);
	  free(dl_report->csi_report[j]->a12csi->sb_pmi);
	  free(dl_report->csi_report[j]->a12csi);
	  break;
764
	case PROTOCOL__FLEX_DL_CSI__REPORT_A22CSI:
765 766 767 768 769
	  free(dl_report->csi_report[j]->a22csi->wb_cqi);
	  free(dl_report->csi_report[j]->a22csi->sb_cqi);
	  free(dl_report->csi_report[j]->a22csi->sb_list);
	  free(dl_report->csi_report[j]->a22csi);
	  break;
770
	case PROTOCOL__FLEX_DL_CSI__REPORT_A20CSI:
771 772 773
	  free(dl_report->csi_report[j]->a20csi->sb_list);
	  free(dl_report->csi_report[j]->a20csi);
	  break;
774
	case PROTOCOL__FLEX_DL_CSI__REPORT_A30CSI:
775 776 777
	  free(dl_report->csi_report[j]->a30csi->sb_cqi);
	  free(dl_report->csi_report[j]->a30csi);
	  break;
778
	case PROTOCOL__FLEX_DL_CSI__REPORT_A31CSI:
779 780 781 782 783 784
	  free(dl_report->csi_report[j]->a31csi->wb_cqi);
	  for (k = 0; k < dl_report->csi_report[j]->a31csi->n_sb_cqi; k++) {
	    free(dl_report->csi_report[j]->a31csi->sb_cqi[k]);
	  }
	  free(dl_report->csi_report[j]->a31csi->sb_cqi);
	  break;
785 786
	default:
	  break;
787 788
	}

789 790 791 792 793 794
	free(dl_report->csi_report[j]);
      }
      free(dl_report->csi_report);
      free(dl_report);
    }
    // If Paging buffer report flag was set
795
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_PBS) {
796 797 798 799 800 801 802 803 804
      paging_report = reply->ue_report[i]->pbr;
      // Delete all paging buffer reports
      for (j = 0; j < paging_report->n_paging_info; j++) {
	free(paging_report->paging_info[j]);
      }
      free(paging_report->paging_info);
      free(paging_report);
    }
    // If UL CQI report flag was set
805
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_UL_CQI) {
806 807 808 809 810 811
      ul_report = reply->ue_report[i]->ul_cqi_report;
      for (j = 0; j < ul_report->n_cqi_meas; j++) {
	free(ul_report->cqi_meas[j]->sinr);
	free(ul_report->cqi_meas[j]);
      }
      free(ul_report->cqi_meas);
812 813 814 815
      for (j = 0; j < ul_report->n_pucch_dbm; j++) {
	free(ul_report->pucch_dbm[j]);
      }
      free(ul_report->pucch_dbm);
816 817 818 819 820 821 822
      free(ul_report);
    }
    if (reply->ue_report[i]->flags & PROTOCOL__FLEX_UE_STATS_TYPE__FLUST_MAC_STATS) {
      for (j = 0; j < reply->ue_report[i]->mac_stats->n_mac_sdus_dl; j++)
        free(reply->ue_report[i]->mac_stats->mac_sdus_dl[j]);
      free(reply->ue_report[i]->mac_stats->mac_sdus_dl);
      free(reply->ue_report[i]->mac_stats);
823 824 825 826 827
    }
  }

  // Free memory for all Cell reports
  for (i = 0; i < reply->n_cell_report; i++) {
828 829 830
    if (reply->cell_report[i]->flags & PROTOCOL__FLEX_CELL_STATS_TYPE__FLCST_NOISE_INTERFERENCE) {
      free(reply->cell_report[i]->noise_inter_report);
    }
831
  }
832

833 834
  return 0;
}
835

836
int flexran_agent_mac_sr_info(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
837
  Protocol__FlexHeader *header = NULL;
838 839 840
  int i;
  const int xid = *((int *)params);

841 842
  Protocol__FlexUlSrInfo *ul_sr_info_msg;
  ul_sr_info_msg = malloc(sizeof(Protocol__FlexUlSrInfo));
843 844 845
  if (ul_sr_info_msg == NULL) {
    goto error;
  }
846
  protocol__flex_ul_sr_info__init(ul_sr_info_msg);
847

848 849 850
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_UL_SR_INFO, &header) != 0)
    goto error;

851 852
  ul_sr_info_msg->header = header;
  ul_sr_info_msg->has_sfn_sf = 1;
853
  ul_sr_info_msg->sfn_sf = flexran_get_sfn_sf(mod_id);
854 855 856 857 858 859 860 861 862 863 864
  /*TODO: Set the number of UEs that sent an SR */
  ul_sr_info_msg->n_rnti = 1;
  ul_sr_info_msg->rnti = (uint32_t *) malloc(ul_sr_info_msg->n_rnti * sizeof(uint32_t));

  if(ul_sr_info_msg->rnti == NULL) {
    goto error;
  }
  /*TODO:Set the rnti of the UEs that sent an SR */
  for (i = 0; i < ul_sr_info_msg->n_rnti; i++) {
    ul_sr_info_msg->rnti[i] = 1;
  }
865

866
  *msg = malloc(sizeof(Protocol__FlexranMessage));
867 868
  if(*msg == NULL)
    goto error;
869 870 871
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_SR_INFO_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
872 873
  (*msg)->ul_sr_info_msg = ul_sr_info_msg;
  return 0;
874

875 876 877 878 879 880 881 882 883 884 885 886 887 888
 error:
  // TODO: Need to make proper error handling
  if (header != NULL)
    free(header);
  if (ul_sr_info_msg != NULL) {
    free(ul_sr_info_msg->rnti);
    free(ul_sr_info_msg);
  }
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

889
int flexran_agent_mac_destroy_sr_info(Protocol__FlexranMessage *msg) {
890
   if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_SR_INFO_MSG)
891 892 893 894 895 896 897 898 899 900 901 902 903
     goto error;

   free(msg->ul_sr_info_msg->header);
   free(msg->ul_sr_info_msg->rnti);
   free(msg->ul_sr_info_msg);
   free(msg);
   return 0;

 error:
   //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
   return -1;
}

904
int flexran_agent_mac_sf_trigger(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
905
  Protocol__FlexHeader *header = NULL;
906
  int i, j, UE_id;
Cedric Roux's avatar
Cedric Roux committed
907

908
  int available_harq[MAX_MOBILES_PER_ENB];
Cedric Roux's avatar
Cedric Roux committed
909

910 911
  const int xid = *((int *)params);

912

913 914
  Protocol__FlexSfTrigger *sf_trigger_msg;
  sf_trigger_msg = malloc(sizeof(Protocol__FlexSfTrigger));
915
  if (sf_trigger_msg == NULL) {
916 917
    goto error;
  }
918
  protocol__flex_sf_trigger__init(sf_trigger_msg);
919

920 921 922
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_SF_TRIGGER, &header) != 0)
    goto error;

923 924 925
  frame_t frame;
  sub_frame_t subframe;

926
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
927 928 929 930
    available_harq[i] = -1;
  }

  int ahead_of_time = 0;
Cedric Roux's avatar
Cedric Roux committed
931

932 933
  frame = (frame_t) flexran_get_current_system_frame_num(mod_id);
  subframe = (sub_frame_t) flexran_get_current_subframe(mod_id);
934 935

  subframe = ((subframe + ahead_of_time) % 10);
Cedric Roux's avatar
Cedric Roux committed
936

937
  if (subframe < flexran_get_current_subframe(mod_id)) {
938
    frame = (frame + 1) % 1024;
939 940
  }

941 942 943
  int additional_frames = ahead_of_time / 10;
  frame = (frame + additional_frames) % 1024;

944 945
  sf_trigger_msg->header = header;
  sf_trigger_msg->has_sfn_sf = 1;
946
  sf_trigger_msg->sfn_sf = flexran_get_future_sfn_sf(mod_id, 3);
947

948 949
  sf_trigger_msg->n_dl_info = 0;

950
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
951
    for (j = 0; j < 8; j++) {
952
      if (RC.mac && RC.mac[mod_id] && RC.mac[mod_id]->UE_info.eNB_UE_stats[UE_PCCID(mod_id,i)][i].harq_pid == 1) {
953 954 955 956 957
	available_harq[i] = j;
	break;
      }
    }
  }
Cedric Roux's avatar
Cedric Roux committed
958

959 960 961

  //  LOG_I(FLEXRAN_AGENT, "Sending subframe trigger for frame %d and subframe %d\n", flexran_get_current_frame(mod_id), (flexran_get_current_subframe(mod_id) + 1) % 10);

962
  sf_trigger_msg->n_dl_info = flexran_get_mac_num_ues(mod_id);
963

964
  Protocol__FlexDlInfo **dl_info = NULL;
965

966
  if (sf_trigger_msg->n_dl_info > 0) {
967
    dl_info = malloc(sizeof(Protocol__FlexDlInfo *) * sf_trigger_msg->n_dl_info);
968 969
    if(dl_info == NULL)
      goto error;
970
    i = -1;
971
    //Fill the status of the current HARQ process for each UE
972 973
    for(i = 0; i < sf_trigger_msg->n_dl_info; i++) {
      if (available_harq[i] < 0)
974
	continue;
975
      dl_info[i] = malloc(sizeof(Protocol__FlexDlInfo));
976 977
      if(dl_info[i] == NULL)
	goto error;
978
      UE_id = flexran_get_mac_ue_id(mod_id, i);
979
      protocol__flex_dl_info__init(dl_info[i]);
980
      dl_info[i]->rnti = flexran_get_mac_ue_crnti(mod_id, UE_id);
981
      dl_info[i]->has_rnti = 1;
982
      /*Fill in the right id of this round's HARQ process for this UE*/
983 984
      //      uint8_t harq_id;
      //uint8_t harq_status;
985
      //      flexran_get_harq(mod_id, UE_PCCID(mod_id, UE_id), i, frame, subframe, &harq_id, &harq_status);
Cedric Roux's avatar
Cedric Roux committed
986 987


988
      dl_info[i]->harq_process_id = available_harq[UE_id];
989
      if (RC.mac && RC.mac[mod_id])
990
        RC.mac[mod_id]->UE_info.eNB_UE_stats[UE_PCCID(mod_id, UE_id)][UE_id].harq_pid = 0;
991
      dl_info[i]->has_harq_process_id = 1;
992
      /* Fill in the status of the HARQ process (2 TBs)*/
993 994
      dl_info[i]->n_harq_status = 2;
      dl_info[i]->harq_status = malloc(sizeof(uint32_t) * dl_info[i]->n_harq_status);
995
      for (j = 0; j < dl_info[i]->n_harq_status; j++) {
996
        dl_info[i]->harq_status[j] = RC.mac[mod_id]->UE_info.UE_sched_ctrl[UE_id].round[UE_PCCID(mod_id, UE_id)][j];
997
        // TODO: This should be different per TB
998 999 1000 1001
      }
      //      LOG_I(FLEXRAN_AGENT, "Sending subframe trigger for frame %d and subframe %d and harq %d (round %d)\n", flexran_get_current_frame(mod_id), (flexran_get_current_subframe(mod_id) + 1) % 10, dl_info[i]->harq_process_id, dl_info[i]->harq_status[0]);
      if(dl_info[i]->harq_status[0] > 0) {
	//	LOG_I(FLEXRAN_AGENT, "[Frame %d][Subframe %d]Need to make a retransmission for harq %d (round %d)\n", flexran_get_current_frame(mod_id), flexran_get_current_subframe(mod_id), dl_info[i]->harq_process_id, dl_info[i]->harq_status[0]);
1002
      }
1003
      /*Fill in the serving cell index for this UE */
1004
      dl_info[i]->serv_cell_index = UE_PCCID(mod_id, UE_id);
1005 1006 1007
      dl_info[i]->has_serv_cell_index = 1;
    }
  }
1008

1009
  sf_trigger_msg->dl_info = dl_info;
1010

1011 1012
  /* Fill in the number of UL reception status related info, based on the number of currently
   * transmitting UEs
1013
   */
1014
  sf_trigger_msg->n_ul_info = flexran_get_mac_num_ues(mod_id);
1015

1016
  Protocol__FlexUlInfo **ul_info = NULL;
1017

1018
  if (sf_trigger_msg->n_ul_info > 0) {
1019
    ul_info = malloc(sizeof(Protocol__FlexUlInfo *) * sf_trigger_msg->n_ul_info);
1020 1021 1022
    if(ul_info == NULL)
      goto error;
    //Fill the reception info for each transmitting UE
1023
    for(i = 0; i < sf_trigger_msg->n_ul_info; i++) {
1024
      ul_info[i] = malloc(sizeof(Protocol__FlexUlInfo));
1025 1026
      if(ul_info[i] == NULL)
	goto error;
1027
      protocol__flex_ul_info__init(ul_info[i]);
1028

1029
      UE_id = flexran_get_mac_ue_id(mod_id, i);
1030

1031
      ul_info[i]->rnti = flexran_get_mac_ue_crnti(mod_id, UE_id);
1032
      ul_info[i]->has_rnti = 1;
Robert Schmidt's avatar
Robert Schmidt committed
1033 1034
      /* Fill in the Tx power control command for this UE (if available),
       * primary carrier */
1035
      if(flexran_get_tpc(mod_id, UE_id, 0) != 1){
Robert Schmidt's avatar
Robert Schmidt committed
1036
          /* assume primary carrier */
1037
          ul_info[i]->tpc = flexran_get_tpc(mod_id, UE_id, 0);
MKassem's avatar
MKassem committed
1038 1039 1040
          ul_info[i]->has_tpc = 1;
      }
      else{
Robert Schmidt's avatar
Robert Schmidt committed
1041
          /* assume primary carrier */
1042
          ul_info[i]->tpc = flexran_get_tpc(mod_id, UE_id, 0);
MKassem's avatar
MKassem committed
1043 1044
    	  ul_info[i]->has_tpc = 0;
      }
1045
      /*TODO: fill in the amount of data in bytes in the MAC SDU received in this subframe for the
1046
	given logical channel*/
1047
      ul_info[i]->n_ul_reception = 0;
1048
      ul_info[i]->ul_reception = malloc(sizeof(uint32_t) * ul_info[i]->n_ul_reception);
1049
      for (j = 0; j < ul_info[i]->n_ul_reception; j++) {
1050 1051 1052
	ul_info[i]->ul_reception[j] = 100;
      }
      /*TODO: Fill in the reception status for each UEs data*/
1053
      ul_info[i]->reception_status = PROTOCOL__FLEX_RECEPTION_STATUS__FLRS_OK;
1054
      ul_info[i]->has_reception_status = 1;
1055
      /*Fill in the serving cell index for this UE */
1056
      ul_info[i]->serv_cell_index = UE_PCCID(mod_id, UE_id);
1057 1058 1059
      ul_info[i]->has_serv_cell_index = 1;
    }
  }
1060

1061 1062
  sf_trigger_msg->ul_info = ul_info;

1063
  *msg = malloc(sizeof(Protocol__FlexranMessage));
1064 1065
  if(*msg == NULL)
    goto error;
1066 1067 1068
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_SF_TRIGGER_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
1069
  (*msg)->sf_trigger_msg = sf_trigger_msg;
1070
  return 0;
1071

1072 1073 1074
 error:
  if (header != NULL)
    free(header);
1075
  if (sf_trigger_msg != NULL) {
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    if (sf_trigger_msg->dl_info != NULL) {
      for (i = 0; i < sf_trigger_msg->n_dl_info; i++) {
        if (sf_trigger_msg->dl_info[i] != NULL) {
          if (sf_trigger_msg->dl_info[i]->harq_status != NULL) {
            free(sf_trigger_msg->dl_info[i]->harq_status);
          }
          free(sf_trigger_msg->dl_info[i]);
        }
      }
      free(sf_trigger_msg->dl_info);
1086
    }
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
    if (sf_trigger_msg->ul_info != NULL) {
      for (i = 0; i < sf_trigger_msg->n_ul_info; i++) {
        if (sf_trigger_msg->ul_info[i] != NULL) {
          if (sf_trigger_msg->ul_info[i]->ul_reception != NULL) {
            free(sf_trigger_msg->ul_info[i]->ul_reception);
          }
          free(sf_trigger_msg->ul_info[i]);
        }
      }
      free(sf_trigger_msg->ul_info);
    }
1098
    free(sf_trigger_msg);
1099 1100 1101 1102 1103 1104 1105
  }
  if(*msg != NULL)
    free(*msg);
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

1106
int flexran_agent_mac_destroy_sf_trigger(Protocol__FlexranMessage *msg) {
1107
  int i;
1108
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_SF_TRIGGER_MSG)
1109
    goto error;
1110

1111 1112 1113
  free(msg->sf_trigger_msg->header);
  for (i = 0; i < msg->sf_trigger_msg->n_dl_info; i++) {
    free(msg->sf_trigger_msg->dl_info[i]->harq_status);
1114
    free(msg->sf_trigger_msg->dl_info[i]);
1115
  }
1116 1117
  free(msg->sf_trigger_msg->dl_info);
  for (i = 0; i < msg->sf_trigger_msg->n_ul_info; i++) {
1118 1119
    free(msg->sf_trigger_msg->ul_info[i]->ul_reception);
    free(msg->sf_trigger_msg->ul_info[i]);
1120 1121 1122
  }
  free(msg->sf_trigger_msg->ul_info);
  free(msg->sf_trigger_msg);
1123
  free(msg);
1124

1125 1126 1127 1128 1129 1130 1131
  return 0;

 error:
  //LOG_E(MAC, "%s: an error occured\n", __FUNCTION__);
  return -1;
}

1132
int flexran_agent_mac_create_empty_dl_config(mid_t mod_id, Protocol__FlexranMessage **msg) {
1133 1134

  int xid = 0;
1135
  Protocol__FlexHeader *header = NULL;
1136
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_DL_MAC_CONFIG, &header) != 0)
1137 1138
    goto error;

1139 1140
  Protocol__FlexDlMacConfig *dl_mac_config_msg;
  dl_mac_config_msg = malloc(sizeof(Protocol__FlexDlMacConfig));
1141
  if (dl_mac_config_msg == NULL) {
1142
    free(header);
1143 1144
    goto error;
  }
1145
  protocol__flex_dl_mac_config__init(dl_mac_config_msg);
1146

1147 1148
  dl_mac_config_msg->header = header;
  dl_mac_config_msg->has_sfn_sf = 1;
1149
  dl_mac_config_msg->sfn_sf = flexran_get_sfn_sf(mod_id);
1150

1151
  *msg = malloc(sizeof(Protocol__FlexranMessage));
1152
  if(*msg == NULL) {
1153
    free(dl_mac_config_msg);
1154
    free(header);
1155
    goto error;
1156
  }
1157 1158 1159
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_DL_MAC_CONFIG_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
1160 1161 1162 1163 1164 1165 1166 1167
  (*msg)->dl_mac_config_msg = dl_mac_config_msg;

  return 0;

 error:
  return -1;
}

1168
int flexran_agent_mac_destroy_dl_config(Protocol__FlexranMessage *msg) {
1169
  int i,j, k;
1170
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_DL_MAC_CONFIG_MSG)
1171 1172
    goto error;

1173
  Protocol__FlexDlDci *dl_dci;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194

  free(msg->dl_mac_config_msg->header);
  for (i = 0; i < msg->dl_mac_config_msg->n_dl_ue_data; i++) {
    free(msg->dl_mac_config_msg->dl_ue_data[i]->ce_bitmap);
    for (j = 0; j < msg->dl_mac_config_msg->dl_ue_data[i]->n_rlc_pdu; j++) {
      for (k = 0; k <  msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->n_rlc_pdu_tb; k++) {
	free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb[k]);
      }
      free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb);
      free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]);
    }
    free(msg->dl_mac_config_msg->dl_ue_data[i]->rlc_pdu);
    dl_dci = msg->dl_mac_config_msg->dl_ue_data[i]->dl_dci;
    free(dl_dci->tbs_size);
    free(dl_dci->mcs);
    free(dl_dci->ndi);
    free(dl_dci->rv);
    free(dl_dci);
    free(msg->dl_mac_config_msg->dl_ue_data[i]);
  }
  free(msg->dl_mac_config_msg->dl_ue_data);
1195

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  for (i = 0; i <  msg->dl_mac_config_msg->n_dl_rar; i++) {
    dl_dci = msg->dl_mac_config_msg->dl_rar[i]->rar_dci;
    free(dl_dci->tbs_size);
    free(dl_dci->mcs);
    free(dl_dci->ndi);
    free(dl_dci->rv);
    free(dl_dci);
    free(msg->dl_mac_config_msg->dl_rar[i]);
  }
  free(msg->dl_mac_config_msg->dl_rar);

  for (i = 0; i < msg->dl_mac_config_msg->n_dl_broadcast; i++) {
    dl_dci = msg->dl_mac_config_msg->dl_broadcast[i]->broad_dci;
    free(dl_dci->tbs_size);
    free(dl_dci->mcs);
    free(dl_dci->ndi);
    free(dl_dci->rv);
    free(dl_dci);
    free(msg->dl_mac_config_msg->dl_broadcast[i]);
  }
  free(msg->dl_mac_config_msg->dl_broadcast);

    for ( i = 0; i < msg->dl_mac_config_msg->n_ofdm_sym; i++) {
    free(msg->dl_mac_config_msg->ofdm_sym[i]);
  }
  free(msg->dl_mac_config_msg->ofdm_sym);
1222 1223
  free(msg->dl_mac_config_msg);
  free(msg);
1224 1225 1226 1227

  return 0;

 error:
1228 1229 1230
  return -1;
}

shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1231 1232 1233
int flexran_agent_mac_create_empty_ul_config(mid_t mod_id, Protocol__FlexranMessage **msg) {

  int xid = 0;
1234
  Protocol__FlexHeader *header = NULL;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  if (flexran_create_header(xid, PROTOCOL__FLEX_TYPE__FLPT_UL_MAC_CONFIG, &header) != 0)
    goto error;

  Protocol__FlexUlMacConfig *ul_mac_config_msg;
  ul_mac_config_msg = malloc(sizeof(Protocol__FlexUlMacConfig));
  if (ul_mac_config_msg == NULL) {
    goto error;
  }
  protocol__flex_ul_mac_config__init(ul_mac_config_msg);

  ul_mac_config_msg->header = header;
  ul_mac_config_msg->has_sfn_sf = 1;
  ul_mac_config_msg->sfn_sf = flexran_get_sfn_sf(mod_id);

  *msg = malloc(sizeof(Protocol__FlexranMessage));
1250 1251
  if(*msg == NULL) {
    free(ul_mac_config_msg);
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1252
    goto error;
1253
  }
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1254 1255 1256 1257 1258 1259 1260 1261
  protocol__flexran_message__init(*msg);
  (*msg)->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_MAC_CONFIG_MSG;
  (*msg)->msg_dir =  PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
  (*msg)->ul_mac_config_msg = ul_mac_config_msg;

  return 0;

 error:
1262 1263 1264 1265
  if(header){
      free(header);
      header = NULL;
  }
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1266 1267 1268 1269
  return -1;
}


shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1270
int flexran_agent_mac_destroy_ul_config(Protocol__FlexranMessage *msg) {
shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1271
  int i; //,j, k;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1272 1273 1274
  if(msg->msg_case != PROTOCOL__FLEXRAN_MESSAGE__MSG_UL_MAC_CONFIG_MSG)
    goto error;

shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1275
  // Protocol__FlexUlDci *ul_dci;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1276 1277 1278 1279

  free(msg->ul_mac_config_msg->header);
  for (i = 0; i < msg->ul_mac_config_msg->n_ul_ue_data; i++) {
    // TODO  uplink rlc ...
Cedric Roux's avatar
Cedric Roux committed
1280
    // free(msg->ul_mac_config_msg->dl_ue_data[i]->ce_bitmap);
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1281 1282 1283 1284 1285 1286 1287 1288
  //   for (j = 0; j < msg->ul_mac_config_msg->ul_ue_data[i]->n_rlc_pdu; j++) {
  //     for (k = 0; k <  msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]->n_rlc_pdu_tb; k++) {
  // free(msg->ul_mac_config_msg->dl_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb[k]);
  //     }
  //     free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]->rlc_pdu_tb);
  //     free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu[j]);
  //   }
    // free(msg->ul_mac_config_msg->ul_ue_data[i]->rlc_pdu);
shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1289
    // ul_dci = msg->ul_mac_config_msg->ul_ue_data[i]->ul_dci;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1290 1291 1292 1293 1294
    // free(dl_dci->tbs_size);
    // free(ul_dci->mcs);
    // free(ul_dci->ndi);
    // free(ul_dci->rv);
    // free(ul_dci);
shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
1295
    // free(msg->ul_mac_config_msg->ul_ue_data[i]);
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1296 1297
  }
  free(msg->ul_mac_config_msg->ul_ue_data);
Cedric Roux's avatar
Cedric Roux committed
1298

shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
  free(msg->ul_mac_config_msg);
  free(msg);

  return 0;

 error:
  return -1;
}


1309
void flexran_agent_get_pending_dl_mac_config(mid_t mod_id, Protocol__FlexranMessage **msg) {
1310 1311

  struct lfds700_misc_prng_state ls;
Cedric Roux's avatar
Cedric Roux committed
1312

1313 1314
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
Cedric Roux's avatar
Cedric Roux committed
1315

1316 1317 1318 1319 1320
  if (lfds700_ringbuffer_read(&ringbuffer_state[mod_id], NULL, (void **) msg, &ls) == 0) {
    *msg = NULL;
  }
}

1321
int flexran_agent_mac_handle_dl_mac_config(mid_t mod_id, const void *params, Protocol__FlexranMessage **msg) {
1322 1323 1324

  struct lfds700_misc_prng_state ls;
  enum lfds700_misc_flag overwrite_occurred_flag;
1325
  Protocol__FlexranMessage *overwritten_dl_config;
Cedric Roux's avatar
Cedric Roux committed
1326

1327 1328
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
Cedric Roux's avatar
Cedric Roux committed
1329

1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
  lfds700_ringbuffer_write( &ringbuffer_state[mod_id],
			    NULL,
			    (void *) params,
			    &overwrite_occurred_flag,
			    NULL,
			    (void **)&overwritten_dl_config,
			    &ls);

  if (overwrite_occurred_flag == LFDS700_MISC_FLAG_RAISED) {
    // Delete unmanaged dl_config
1340
    flexran_agent_mac_destroy_dl_config(overwritten_dl_config);
1341 1342 1343 1344
  }
  *msg = NULL;
  return 2;

1345 1346 1347
  // error:
  //*msg = NULL;
  //return -1;
1348 1349
}

1350
void flexran_agent_init_mac_agent(mid_t mod_id) {
1351
  int i, j;
1352 1353 1354 1355
  lfds700_misc_library_init_valid_on_current_logical_core();
  lfds700_misc_prng_init(&ps[mod_id]);
  int num_elements = RINGBUFFER_SIZE + 1;
  //Allow RINGBUFFER_SIZE messages to be stored in the ringbuffer at any time
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
  /* lfds700_ringbuffer_init_valid_on_current_logical_core()'s second argument
   * must be aligned to LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES. From the
   * documentation: "Heap allocated variables however will by no means be
   * correctly aligned and an aligned malloc must be used." Therefore, we use
   * posix_memalign */
  i = posix_memalign((void **)&dl_mac_config_array[mod_id],
                    LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES,
                    sizeof(struct lfds700_ringbuffer_element) *  num_elements);
  AssertFatal(i == 0, "posix_memalign(): could not allocate aligned memory for lfds library\n");
  lfds700_ringbuffer_init_valid_on_current_logical_core(&ringbuffer_state[mod_id],
      dl_mac_config_array[mod_id], num_elements, &ps[mod_id], NULL);
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

  struct lfds700_misc_prng_state scrng;
  i = posix_memalign((void **) &slice_config_array[mod_id],
                     LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES,
                     sizeof(struct lfds700_ringbuffer_element) * num_elements);
  AssertFatal(i == 0,
              "posix_memalign(): could not allocate aligned memory\n");
  lfds700_ringbuffer_init_valid_on_current_logical_core(
      &slice_config_ringbuffer_state[mod_id],
      slice_config_array[mod_id],
      num_elements,
      &scrng,
      NULL);

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
  struct lfds700_misc_prng_state sscrng;
  i = posix_memalign((void **) &store_slice_config_array[mod_id],
                     LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES,
                     sizeof(struct lfds700_ringbuffer_element) * num_elements);
  AssertFatal(i == 0,
              "posix_memalign(): could not allocate aligned memory\n");
  lfds700_ringbuffer_init_valid_on_current_logical_core(
      &store_slice_config_ringbuffer_state[mod_id],
      store_slice_config_array[mod_id],
      num_elements,
      &sscrng,
      NULL);

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
  struct lfds700_misc_prng_state uarng;
  i = posix_memalign((void **) &ue_assoc_array[mod_id],
                     LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES,
                     sizeof(struct lfds700_ringbuffer_element) * num_elements);
  AssertFatal(i == 0,
              "posix_memalign(): could not allocate aligned memory\n");
  lfds700_ringbuffer_init_valid_on_current_logical_core(
      &ue_assoc_ringbuffer_state[mod_id],
      ue_assoc_array[mod_id],
      num_elements,
      &uarng,
      NULL);

1407
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
1408
    for (j = 0; j < 8; j++) {
1409
      if (RC.mac && RC.mac[mod_id])
1410
        RC.mac[mod_id]->UE_info.eNB_UE_stats[UE_PCCID(mod_id,i)][i].harq_pid = 0;
1411 1412
    }
  }
1413 1414

  SLIST_INIT(&flexran_handles[mod_id]);
1415 1416
}

1417
/***********************************************
1418
 * FlexRAN agent - technology mac API implementation
1419 1420
 ***********************************************/

1421
void flexran_agent_send_sr_info(mid_t mod_id) {
1422
  int size;
1423
  Protocol__FlexranMessage *msg = NULL;
1424
  void *data;
1425
  int priority = 0;
1426 1427
  err_code_t err_code;

1428 1429
  int xid = 0;

1430
  /*TODO: Must use a proper xid*/
1431
  err_code = flexran_agent_mac_sr_info(mod_id, (void *) &xid, &msg);
1432 1433 1434 1435 1436
  if (err_code < 0) {
    goto error;
  }

  if (msg != NULL){
1437
    data=flexran_agent_pack_message(msg, &size);
1438
    /*Send sr info using the MAC channel of the eNB*/
1439
    if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_MAC, data, size, priority)) {
1440
      err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
1441 1442
      goto error;
    }
1443

1444
    LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
1445
    return;
1446 1447
  }
 error:
1448
  LOG_D(FLEXRAN_AGENT, "Could not send sr message\n");
1449 1450
}

1451
void flexran_agent_send_sf_trigger(mid_t mod_id) {
1452
  int size;
1453
  Protocol__FlexranMessage *msg = NULL;
1454
  void *data;
1455
  int priority = 0;
1456 1457
  err_code_t err_code;

1458
  int xid = 0;
1459

1460
  /*TODO: Must use a proper xid*/
1461
  err_code = flexran_agent_mac_sf_trigger(mod_id, (void *) &xid, &msg);
1462 1463 1464 1465 1466
  if (err_code < 0) {
    goto error;
  }

  if (msg != NULL){
1467
    data=flexran_agent_pack_message(msg, &size);
1468
    /*Send sr info using the MAC channel of the eNB*/
1469
    if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_MAC, data, size, priority)) {
1470
      err_code = PROTOCOL__FLEXRAN_ERR__MSG_ENQUEUING;
1471 1472 1473
      goto error;
    }

1474
    LOG_D(FLEXRAN_AGENT,"sent message with size %d\n", size);
1475 1476 1477
    return;
  }
 error:
1478
  LOG_D(FLEXRAN_AGENT, "Could not send sf trigger message\n");
1479 1480
}

1481 1482


1483 1484 1485
int flexran_agent_register_mac_xface(mid_t mod_id)
{
  if (agent_mac_xface[mod_id]) {
1486 1487 1488
    LOG_E(MAC, "MAC agent for eNB %d is already registered\n", mod_id);
    return -1;
  }
1489 1490 1491 1492 1493
  AGENT_MAC_xface *xface = malloc(sizeof(AGENT_MAC_xface));
  if (!xface) {
    LOG_E(FLEXRAN_AGENT, "could not allocate memory for MAC agent xface %d\n", mod_id);
    return -1;
  }
1494

1495 1496 1497
  xface->flexran_agent_send_sr_info = flexran_agent_send_sr_info;
  xface->flexran_agent_send_sf_trigger = flexran_agent_send_sf_trigger;
  xface->flexran_agent_get_pending_dl_mac_config = flexran_agent_get_pending_dl_mac_config;
1498
  xface->flexran_agent_notify_tick = flexran_agent_timer_signal;
Cedric Roux's avatar
Cedric Roux committed
1499

1500
  xface->dl_scheduler_loaded_lib = NULL;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1501
  xface->ul_scheduler_loaded_lib = NULL;
1502
  agent_mac_xface[mod_id] = xface;
1503 1504

  return 0;
1505 1506
}

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
void flexran_agent_fill_mac_cell_config(mid_t mod_id, uint8_t cc_id,
    Protocol__FlexCellConfig *conf) {
  if (!conf->si_config) {
    conf->si_config = malloc(sizeof(Protocol__FlexSiConfig));
    if (conf->si_config)
      protocol__flex_si_config__init(conf->si_config);
  }

  if (conf->si_config) {
    conf->si_config->sfn = flexran_get_current_system_frame_num(mod_id);
    conf->si_config->has_sfn = 1;
  }
1519

1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
  conf->slice_config = malloc(sizeof(Protocol__FlexSliceConfig));
  if (conf->slice_config) {
    protocol__flex_slice_config__init(conf->slice_config);
    Protocol__FlexSliceConfig *sc = conf->slice_config;

    sc->dl = malloc(sizeof(Protocol__FlexSliceDlUlConfig));
    DevAssert(sc->dl);
    protocol__flex_slice_dl_ul_config__init(sc->dl);
    sc->dl->algorithm = flexran_get_dl_slice_algo(mod_id);
    sc->dl->has_algorithm = 1;
    sc->dl->n_slices = flexran_get_num_dl_slices(mod_id);
    if (sc->dl->n_slices > 0) {
      sc->dl->slices = calloc(sc->dl->n_slices, sizeof(Protocol__FlexSlice *));
      DevAssert(sc->dl->slices);
      for (int i = 0; i < sc->dl->n_slices; ++i) {
        sc->dl->slices[i] = malloc(sizeof(Protocol__FlexSlice));
        DevAssert(sc->dl->slices[i]);
        protocol__flex_slice__init(sc->dl->slices[i]);
        flexran_get_dl_slice(mod_id, i, sc->dl->slices[i], sc->dl->algorithm);
      }
    } else {
      sc->dl->scheduler = flexran_get_dl_scheduler_name(mod_id);
    }

    sc->ul = malloc(sizeof(Protocol__FlexSliceDlUlConfig));
    DevAssert(sc->ul);
    protocol__flex_slice_dl_ul_config__init(sc->ul);
    sc->ul->algorithm = flexran_get_ul_slice_algo(mod_id);
    sc->ul->has_algorithm = 1;
    sc->ul->n_slices = flexran_get_num_ul_slices(mod_id);
    if (sc->ul->n_slices > 0) {
      sc->ul->slices = calloc(sc->ul->n_slices, sizeof(Protocol__FlexSlice *));
      DevAssert(sc->ul->slices);
      for (int i = 0; i < sc->ul->n_slices; ++i) {
        sc->ul->slices[i] = malloc(sizeof(Protocol__FlexSlice));
        DevAssert(sc->ul->slices[i]);
        protocol__flex_slice__init(sc->ul->slices[i]);
        flexran_get_ul_slice(mod_id, i, sc->ul->slices[i], sc->ul->algorithm);
      }
    } else {
      sc->ul->scheduler = flexran_get_ul_scheduler_name(mod_id);
    }
  }
}

void flexran_agent_destroy_mac_slice_config(Protocol__FlexCellConfig *conf) {
  Protocol__FlexSliceConfig *sc = conf->slice_config;
  for (int i = 0; i < sc->dl->n_slices; ++i) {
    free(sc->dl->slices[i]);
    sc->dl->slices[i] = NULL;
    /* scheduler names are not freed: we assume we read them directly from the
     * underlying memory and do not dispose it */
  }
  free(sc->dl->slices);
  /* scheduler name is not freed */
  free(sc->dl);
  sc->dl = NULL;
  for (int i = 0; i < sc->ul->n_slices; ++i) {
    free(sc->ul->slices[i]);
    sc->ul->slices[i] = NULL;
    /* scheduler names are not freed */
  }
  free(sc->ul->slices);
  /* scheduler name is not freed */
  free(sc->ul);
  sc->ul = NULL;
  free(conf->slice_config);
  conf->slice_config = NULL;
1588 1589
}

1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
void flexran_agent_fill_mac_ue_config(mid_t mod_id, mid_t ue_id,
    Protocol__FlexUeConfig *ue_conf)
{
  if (ue_conf->has_rnti && ue_conf->rnti != flexran_get_mac_ue_crnti(mod_id, ue_id)) {
    LOG_E(FLEXRAN_AGENT, "ue_config existing RNTI %x does not match MAC RNTI %x\n",
          ue_conf->rnti, flexran_get_mac_ue_crnti(mod_id, ue_id));
    return;
  }
  ue_conf->rnti = flexran_get_mac_ue_crnti(mod_id, ue_id);
  ue_conf->has_rnti = 1;

1601 1602 1603 1604 1605 1606
  int dl_id = flexran_get_ue_dl_slice_id(mod_id, ue_id);
  ue_conf->dl_slice_id = dl_id;
  ue_conf->has_dl_slice_id = dl_id >= 0;
  int ul_id = flexran_get_ue_ul_slice_id(mod_id, ue_id);
  ue_conf->ul_slice_id = ul_id;
  ue_conf->has_ul_slice_id = ul_id >= 0;
1607

1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
  ue_conf->ue_aggregated_max_bitrate_ul = flexran_get_ue_aggregated_max_bitrate_ul(mod_id, ue_id);
  ue_conf->has_ue_aggregated_max_bitrate_ul = 1;

  ue_conf->ue_aggregated_max_bitrate_dl = flexran_get_ue_aggregated_max_bitrate_dl(mod_id, ue_id);
  ue_conf->has_ue_aggregated_max_bitrate_dl = 1;

  /* TODO update through RAN API */
  //config->has_pcell_carrier_index = 1;
  //config->pcell_carrier_index = UE_PCCID(mod_id, i);

  //TODO: Set carrier aggregation support (boolean)
}

1621 1622 1623
void flexran_agent_fill_mac_lc_ue_config(mid_t mod_id, mid_t ue_id,
    Protocol__FlexLcUeConfig *lc_ue_conf)
{
1624
  lc_ue_conf->rnti = flexran_get_mac_ue_crnti(mod_id, ue_id);
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
  lc_ue_conf->has_rnti = 1;

  lc_ue_conf->n_lc_config = flexran_get_num_ue_lcs(mod_id, ue_id);
  if (lc_ue_conf->n_lc_config == 0)
    return;

  Protocol__FlexLcConfig **lc_config =
    calloc(lc_ue_conf->n_lc_config, sizeof(Protocol__FlexLcConfig *));
  if (!lc_config) {
    LOG_E(FLEXRAN_AGENT, "could not allocate memory for lc_config of UE %x\n", lc_ue_conf->rnti);
    lc_ue_conf->n_lc_config = 0;
    return; // can not allocate memory, skip rest
  }
  for (int j = 0; j < lc_ue_conf->n_lc_config; j++) {
    lc_config[j] = malloc(sizeof(Protocol__FlexLcConfig));
    if (!lc_config[j]) continue; // go over this error, try entry
    protocol__flex_lc_config__init(lc_config[j]);

    lc_config[j]->has_lcid = 1;
    lc_config[j]->lcid = j+1;

    const int lcg = flexran_get_lcg(mod_id, ue_id, j+1);
    if (lcg >= 0 && lcg <= 3) {
      lc_config[j]->has_lcg = 1;
      lc_config[j]->lcg = flexran_get_lcg(mod_id, ue_id, j+1);
    }

    lc_config[j]->has_direction = 1;
    lc_config[j]->direction = flexran_get_direction(ue_id, j+1);
    //TODO: Bearer type. One of FLQBT_* values. Currently only default bearer supported
    lc_config[j]->has_qos_bearer_type = 1;
    lc_config[j]->qos_bearer_type = PROTOCOL__FLEX_QOS_BEARER_TYPE__FLQBT_NON_GBR;

    //TODO: Set the QCI defined in TS 23.203, coded as defined in TS 36.413
    // One less than the actual QCI value. Needs to be generalized
    lc_config[j]->has_qci = 1;
    lc_config[j]->qci = 1;
    if (lc_config[j]->direction == PROTOCOL__FLEX_QOS_BEARER_TYPE__FLQBT_GBR) {
      /* TODO all of the need to be taken from API */
      //TODO: Set the max bitrate (UL)
      lc_config[j]->has_e_rab_max_bitrate_ul = 0;
      lc_config[j]->e_rab_max_bitrate_ul = 0;
      //TODO: Set the max bitrate (DL)
      lc_config[j]->has_e_rab_max_bitrate_dl = 0;
      lc_config[j]->e_rab_max_bitrate_dl = 0;
      //TODO: Set the guaranteed bitrate (UL)
      lc_config[j]->has_e_rab_guaranteed_bitrate_ul = 0;
      lc_config[j]->e_rab_guaranteed_bitrate_ul = 0;
      //TODO: Set the guaranteed bitrate (DL)
      lc_config[j]->has_e_rab_guaranteed_bitrate_dl = 0;
      lc_config[j]->e_rab_guaranteed_bitrate_dl = 0;
    }
  }
  lc_ue_conf->lc_config = lc_config;
}

1681 1682 1683 1684 1685 1686
int flexran_agent_unregister_mac_xface(mid_t mod_id)
{
  if (!agent_mac_xface[mod_id]) {
    LOG_E(FLEXRAN_AGENT, "MAC agent CM for eNB %d is not registered\n", mod_id);
    return -1;
  }
1687 1688 1689

  lfds700_ringbuffer_cleanup(&ringbuffer_state[mod_id], NULL );
  free(dl_mac_config_array[mod_id]);
1690 1691
  lfds700_ringbuffer_cleanup(&slice_config_ringbuffer_state[mod_id], NULL );
  free(slice_config_array[mod_id]);
1692 1693
  lfds700_ringbuffer_cleanup(&store_slice_config_ringbuffer_state[mod_id], NULL );
  free(store_slice_config_array[mod_id]);
1694 1695
  lfds700_ringbuffer_cleanup(&ue_assoc_ringbuffer_state[mod_id], NULL );
  free(ue_assoc_array[mod_id]);
1696 1697
  lfds700_misc_library_cleanup();

1698
  AGENT_MAC_xface *xface = agent_mac_xface[mod_id];
1699 1700 1701
  xface->flexran_agent_send_sr_info = NULL;
  xface->flexran_agent_send_sf_trigger = NULL;
  xface->flexran_agent_get_pending_dl_mac_config = NULL;
1702
  xface->flexran_agent_notify_tick = NULL;
1703

1704
  xface->dl_scheduler_loaded_lib = NULL;
shahab SHARIATBAGHERI's avatar
shahab SHARIATBAGHERI committed
1705
  xface->ul_scheduler_loaded_lib = NULL;
1706
  free(xface);
1707
  agent_mac_xface[mod_id] = NULL;
1708

1709 1710 1711
  return 0;
}

1712 1713 1714 1715 1716 1717 1718
void helper_destroy_mac_slice_config(Protocol__FlexSliceConfig *slice_config) {
  if (slice_config->dl) {
    if (slice_config->dl->scheduler)
      free(slice_config->dl->scheduler);
    for (int i = 0; i < slice_config->dl->n_slices; i++)
      if (slice_config->dl->slices[i]->scheduler)
        free(slice_config->dl->slices[i]->scheduler);
1719
  }
1720 1721 1722 1723 1724 1725
  if (slice_config->ul) {
    if (slice_config->ul->scheduler)
      free(slice_config->ul->scheduler);
    for (int i = 0; i < slice_config->ul->n_slices; i++)
      if (slice_config->ul->slices[i]->scheduler)
        free(slice_config->ul->slices[i]->scheduler);
1726 1727
  }

1728 1729 1730
  Protocol__FlexCellConfig helper;
  helper.slice_config = slice_config;
  flexran_agent_destroy_mac_slice_config(&helper);
1731 1732
}

1733
int check_scheduler(mid_t mod_id, char *s) {
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
  if (!s)
    return 1;
  if (dlsym(NULL, s))
    return 1;
  flexran_agent_so_handle_t *so = NULL;
  SLIST_FOREACH(so, &flexran_handles[mod_id], entries) {
    if (strcmp(so->name, s) == 0)
      return 1;
  }
  return 0;
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
}

void request_scheduler(mid_t mod_id, char *s, int xid) {
  LOG_W(FLEXRAN_AGENT,
        "unknown scheduler %s, requesting controller to send it\n",
        s);
  Protocol__FlexControlDelegationRequest *req = malloc(sizeof(Protocol__FlexControlDelegationRequest));
  DevAssert(req);
  protocol__flex_control_delegation_request__init(req);
  Protocol__FlexHeader *header = NULL;
  int rc = flexran_create_header(
      xid, PROTOCOL__FLEX_TYPE__FLPT_DELEGATE_REQUEST, &header);
  AssertFatal(rc == 0, "%s(): cannot create header\n", __func__);
  req->header = header;
  req->delegation_type = PROTOCOL__FLEX_CONTROL_DELEGATION_TYPE__FLCDT_MAC_DL_UE_SCHEDULER;
  req->name = strdup(s);

  Protocol__FlexranMessage *msg = malloc(sizeof(Protocol__FlexranMessage));
  DevAssert(msg);
  protocol__flexran_message__init(msg);
  msg->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_CONTROL_DEL_REQ_MSG;
  msg->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
  msg->control_del_req_msg = req;

  int size = 0;
  void *data = flexran_agent_pack_message(msg, &size);
  if (flexran_agent_msg_send(mod_id, FLEXRAN_AGENT_DEFAULT, data, size, 0) < 0)
    LOG_E(FLEXRAN_AGENT, "%s(): error while sending message\n", __func__);
}

Protocol__FlexranMessage *request_scheduler_timeout(
    mid_t mod_id,
    const Protocol__FlexranMessage *msg) {
  const Protocol__FlexEnbConfigReply *ecr = msg->enb_config_reply_msg;

  struct lfds700_misc_prng_state ls;
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);

  Protocol__FlexranMessage *smsg = NULL;
  struct lfds700_ringbuffer_state *state = &store_slice_config_ringbuffer_state[mod_id];
  if (lfds700_ringbuffer_read(state, NULL, (void **) &smsg, &ls)) {
    AssertFatal(msg == smsg,
                "expected and returned enb_config_reply are not the same\n");
    AssertFatal(ecr->header->xid == smsg->enb_config_reply_msg->header->xid,
                "expected and returned enb_config_reply are not the same\n");
  } else {
    LOG_E(FLEXRAN_AGENT, "%s(): could not read config from ringbuffer\n", __func__);
  }
  /* call the helper so that scheduler names are correctly freed */
  helper_destroy_mac_slice_config(ecr->cell_config[0]->slice_config);
  ecr->cell_config[0]->slice_config = NULL;
  /* we should not call flexran_agent_destroy_enb_config_reply(smsg); since
   * upon timer removal, this is automatically done */
  LOG_W(FLEXRAN_AGENT,
        "remove stored slice config (xid %d) after timeout\n",
        ecr->header->xid);
  flexran_agent_destroy_timer(mod_id, ecr->header->xid);
  return NULL;
}

1805 1806 1807
void prepare_update_slice_config(mid_t mod_id,
                                 Protocol__FlexSliceConfig **slice_config,
                                 int request_objects) {
1808
  if (!*slice_config) return;
1809
  /* just use the memory and set to NULL in original */
1810 1811 1812
  Protocol__FlexSliceConfig *sc = *slice_config;
  *slice_config = NULL;

1813 1814 1815 1816 1817
  struct lfds700_misc_prng_state ls;
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
  enum lfds700_misc_flag overwrite_occurred_flag;

1818
  int put_on_hold = 0;
1819 1820 1821
  if (request_objects
      && sc->dl->scheduler
      && !check_scheduler(mod_id, sc->dl->scheduler)) {
1822 1823 1824
    request_scheduler(mod_id, sc->dl->scheduler, xid++);
    put_on_hold = 1;
  }
1825
  for (int i = 0; request_objects && i < sc->dl->n_slices; ++i) {
1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
    Protocol__FlexSlice *sl = sc->dl->slices[i];
    if (sl->scheduler && !check_scheduler(mod_id, sl->scheduler)) {
      request_scheduler(mod_id, sl->scheduler, xid++);
      put_on_hold = 1;
    }
  }
  if (put_on_hold) {
    Protocol__FlexEnbConfigReply *reply = malloc(sizeof(Protocol__FlexEnbConfigReply));
    DevAssert(reply);
    protocol__flex_enb_config_reply__init(reply);
    /* use the last used xid so we can correlate answer and waiting config */
    int rc = flexran_create_header(
        xid - 1, PROTOCOL__FLEX_TYPE__FLPT_GET_ENB_CONFIG_REPLY, &reply->header);
    AssertFatal(rc == 0, "%s(): cannot create header\n", __func__);
    reply->n_cell_config = 1;
    reply->cell_config = malloc(sizeof(Protocol__FlexCellConfig *));
    DevAssert(reply->cell_config);
    reply->cell_config[0] = malloc(sizeof(Protocol__FlexCellConfig));
    DevAssert(reply->cell_config[0]);
    protocol__flex_cell_config__init(reply->cell_config[0]);
    reply->cell_config[0]->slice_config = sc;

    Protocol__FlexranMessage *msg = malloc(sizeof(Protocol__FlexranMessage));
    DevAssert(msg);
    protocol__flexran_message__init(msg);
    msg->msg_case = PROTOCOL__FLEXRAN_MESSAGE__MSG_ENB_CONFIG_REPLY_MSG;
    msg->msg_dir = PROTOCOL__FLEXRAN_DIRECTION__INITIATING_MESSAGE;
    msg->enb_config_reply_msg = reply;

    Protocol__FlexEnbConfigReply *o;
    lfds700_ringbuffer_write(&store_slice_config_ringbuffer_state[mod_id],
                             NULL,
                             (void *)msg,
                             &overwrite_occurred_flag,
                             NULL,
                             (void **) &o,
                             &ls);
    AssertFatal(overwrite_occurred_flag == LFDS700_MISC_FLAG_LOWERED,
                "unhandled: stored slice config for controller has been overwritten\n");

    flexran_agent_create_timer(mod_id,
                               3000,
                               FLEXRAN_AGENT_TIMER_TYPE_PERIODIC,
                               xid - 1,
                               request_scheduler_timeout,
                               msg); /* need reply for xid */
    return;
  }

1875 1876 1877 1878 1879 1880 1881 1882
  Protocol__FlexSliceConfig *overwritten_sc;
  lfds700_ringbuffer_write(&slice_config_ringbuffer_state[mod_id],
                           NULL,
                           (void *)sc,
                           &overwrite_occurred_flag,
                           NULL,
                           (void **)&overwritten_sc,
                           &ls);
1883

1884 1885 1886 1887
  if (overwrite_occurred_flag == LFDS700_MISC_FLAG_RAISED) {
    helper_destroy_mac_slice_config(overwritten_sc);
    LOG_E(FLEXRAN_AGENT, "lost slice config: too many reconfigurations at once\n");
  }
1888
}
1889

1890
void prepare_ue_slice_assoc_update(mid_t mod_id, Protocol__FlexUeConfig **ue_config) {
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
  struct lfds700_misc_prng_state ls;
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);
  enum lfds700_misc_flag overwrite_occurred_flag;

  Protocol__FlexUeConfig *overwritten_ue_config;
  lfds700_ringbuffer_write(&ue_assoc_ringbuffer_state[mod_id],
                           NULL,
                           (void *) *ue_config,
                           &overwrite_occurred_flag,
                           NULL,
                           (void **)&overwritten_ue_config,
                           &ls);
1904

1905 1906 1907 1908
  if (overwrite_occurred_flag == LFDS700_MISC_FLAG_RAISED) {
    free(overwritten_ue_config);
    LOG_E(FLEXRAN_AGENT, "lost UE-slice association: too many UE-slice associations at once\n");
  }
1909 1910 1911
}

void flexran_agent_slice_update(mid_t mod_id) {
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
  struct lfds700_misc_prng_state ls;
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);

  Protocol__FlexSliceConfig *sc = NULL;
  struct lfds700_ringbuffer_state *state = &slice_config_ringbuffer_state[mod_id];
  if (lfds700_ringbuffer_read(state, NULL, (void **) &sc, &ls) != 0) {
    apply_update_dl_slice_config(mod_id, sc->dl);
    apply_update_ul_slice_config(mod_id, sc->ul);
    helper_destroy_mac_slice_config(sc);
1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984

    flexran_agent_so_handle_t *so = NULL;
    flexran_agent_so_handle_t *prev = NULL; // the previous to current element, if we delete in order to go back
    //SLIST_FOREACH(so, &flexran_handles[mod_id], entries)
    for(so = flexran_handles[mod_id].slh_first; so;) {
      char *name = so->name;
      int in_use = strcmp(flexran_get_dl_scheduler_name(mod_id), name) == 0
                   || strcmp(flexran_get_ul_scheduler_name(mod_id), name) == 0;
      Protocol__FlexSliceAlgorithm dl_algo = flexran_get_dl_slice_algo(mod_id);
      if (!in_use && dl_algo != PROTOCOL__FLEX_SLICE_ALGORITHM__None) {
        int n = flexran_get_num_dl_slices(mod_id);
        for (int i = 0; i < n; ++i) {
          Protocol__FlexSlice s;
          /* NONE so it won't do any allocations */
          flexran_get_dl_slice(mod_id, i, &s, PROTOCOL__FLEX_SLICE_ALGORITHM__None);
          if (strcmp(s.scheduler, name) == 0) {
            in_use = 1;
            break;
          }
        }
      }
      Protocol__FlexSliceAlgorithm ul_algo = flexran_get_ul_slice_algo(mod_id);
      if (!in_use && ul_algo != PROTOCOL__FLEX_SLICE_ALGORITHM__None) {
        int n = flexran_get_num_ul_slices(mod_id);
        for (int i = 0; i < n; ++i) {
          Protocol__FlexSlice s;
          /* NONE so it won't do any allocations */
          flexran_get_ul_slice(mod_id, i, &s, PROTOCOL__FLEX_SLICE_ALGORITHM__None);
          if (strcmp(s.scheduler, name) == 0) {
            in_use = 1;
            break;
          }
        }
      }
      if (!in_use) {
        char s[512];
        int rc = flexran_agent_map_name_to_delegated_object(mod_id, so->name, s, 512);
        LOG_W(FLEXRAN_AGENT,
              "removing %s (library handle %p) since it is not used in the user "
              "plane anymore\n",
              s,
              so->dl_handle);
        dlclose(so->dl_handle);
        if (rc < 0) {
          LOG_E(FLEXRAN_AGENT, "cannot map name %s\n", so->name);
        } else {
          int rc = remove(s);
          if (rc < 0)
            LOG_E(FLEXRAN_AGENT, "cannot remove file %s: %s\n", s, strerror(errno));
        }
        free (so->name);
        if (!prev) { //it's the head, start over
          SLIST_REMOVE_HEAD(&flexran_handles[mod_id], entries);
          so = flexran_handles[mod_id].slh_first;
        } else {
          SLIST_REMOVE(&flexran_handles[mod_id], so, flexran_agent_so_handle_s, entries);
          so = prev->entries.sle_next;
        }
      } else {
        prev = so;
        so = so->entries.sle_next;
      }
    }
1985 1986 1987 1988 1989 1990 1991 1992
  }

  Protocol__FlexUeConfig *ue_config = NULL;
  state = &ue_assoc_ringbuffer_state[mod_id];
  if (lfds700_ringbuffer_read(state, NULL, (void **) &ue_config, &ls) != 0) {
    apply_ue_slice_assoc_update(mod_id, ue_config);
    free(ue_config);
  }
1993
}
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057

void flexran_agent_mac_inform_delegation(mid_t mod_id,
                                         Protocol__FlexControlDelegation *cdm) {
  LOG_W(FLEXRAN_AGENT,
        "received FlexControlDelegation message for object '%s' xid %d\n",
        cdm->name,
        cdm->header->xid);
  if (cdm->header->xid < xid - 1) {
    LOG_I(FLEXRAN_AGENT,
          "waiting for %d more messages (up to xid %d)\n",
          xid - 1 - cdm->header->xid,
          xid - 1);
    return;
  }
  /* should receive up to xid - 1, otherwise it means there was no request */
  AssertFatal(xid > cdm->header->xid,
              "received control delegation with xid %d that we never requested "
              "(last request %d)\n",
              cdm->header->xid,
              xid - 1);

  /* Load the library so the user plane can search it */
  char s[512];
  int rc = flexran_agent_map_name_to_delegated_object(mod_id, cdm->name, s, 512);
  if (rc < 0) {
    LOG_E(FLEXRAN_AGENT, "cannot map name %s\n", cdm->name);
    return;
  }
  /* TODO where to unload/save handle?? */
  void *h = dlopen(s, RTLD_NOW);
  if (!h) {
    LOG_E(FLEXRAN_AGENT, "dlopen(): %s\n", dlerror());
    return;
  }
  LOG_I(FLEXRAN_AGENT, "library handle %p\n", h);

  struct lfds700_misc_prng_state ls;
  LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
  lfds700_misc_prng_init(&ls);

  Protocol__FlexranMessage *msg = NULL;
  struct lfds700_ringbuffer_state *state = &store_slice_config_ringbuffer_state[mod_id];
  if (lfds700_ringbuffer_read(state, NULL, (void **) &msg, &ls) == 0) {
    LOG_E(FLEXRAN_AGENT, "%s(): could not read config from ringbuffer\n", __func__);
    return;
  }
  AssertFatal(cdm->header->xid == msg->enb_config_reply_msg->header->xid,
              "expected and retrieved xid of stored slice configuration does "
              "not match (expected %d, retrieved %d)\n",
              cdm->header->xid,
              msg->enb_config_reply_msg->header->xid);

  /* Since we recover, stop the timeout */
  rc = flexran_agent_destroy_timer(mod_id, cdm->header->xid);

  prepare_update_slice_config(
      mod_id,
      &msg->enb_config_reply_msg->cell_config[0]->slice_config,
      0 /* don't do a request */);
  /* we should not call flexran_agent_destroy_enb_config_reply(smsg); since
   * upon timer removal, this is automatically done.
   * prepare_update_slice_config() takes ownership of the slice config, it is
   * freed inside */

2058 2059 2060 2061 2062 2063
  flexran_agent_so_handle_t *so = malloc(sizeof(flexran_agent_so_handle_t));
  DevAssert(so);
  so->name = strdup(cdm->name);
  DevAssert(so->name);
  so->dl_handle = h;
  SLIST_INSERT_HEAD(&flexran_handles[mod_id], so, entries);
2064
}
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082

void flexran_agent_mac_fill_loaded_mac_objects(
    mid_t mod_id,
    Protocol__FlexEnbConfigReply *reply) {
  int n = 0;
  flexran_agent_so_handle_t *so = NULL;
  SLIST_FOREACH(so, &flexran_handles[mod_id], entries)
    ++n;
  reply->n_loadedmacobjects = n;
  reply->loadedmacobjects = calloc(n, sizeof(char *));
  if (!reply->loadedmacobjects) {
    reply->n_loadedmacobjects = 0;
    return;
  }
  n = 0;
  SLIST_FOREACH(so, &flexran_handles[mod_id], entries)
    reply->loadedmacobjects[n++] = so->name;
}