pre_processor.c 75.1 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
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */
21 22

/*! \file pre_processor.c
23
 * \brief eNB scheduler preprocessing fuction prior to scheduling
24
 * \author Navid Nikaein and Ankit Bhamri
nikaeinn's avatar
nikaeinn committed
25
 * \date 2013 - 2014
26
 * \email navid.nikaein@eurecom.fr
nikaeinn's avatar
nikaeinn committed
27
 * \version 1.0
28 29 30 31
 * @ingroup _mac

 */

32 33 34
#define _GNU_SOURCE
#include <stdlib.h>

35
#include "assertions.h"
36 37 38
#include "LAYER2/MAC/mac.h"
#include "LAYER2/MAC/mac_proto.h"
#include "LAYER2/MAC/mac_extern.h"
39 40
#include "common/utils/LOG/log.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
41 42 43
#include "UTIL/OPT/opt.h"
#include "OCG.h"
#include "OCG_extern.h"
44
#include "RRC/LTE/rrc_extern.h"
45
#include "RRC/L2_INTERFACE/openair_rrc_L2_interface.h"
46
#include "rlc.h"
Cedric Roux's avatar
Cedric Roux committed
47
#include "PHY/LTE_TRANSPORT/transport_common_proto.h"
48

49
#include "common/ran_context.h"
50

51
extern RAN_CONTEXT_t RC;
gauthier's avatar
gauthier committed
52

53 54 55 56 57 58
#define DEBUG_eNB_SCHEDULER 1
#define DEBUG_HEADER_PARSING 1
//#define DEBUG_PACKET_TRACE 1

//#define ICIC 0

59
/* this function checks that get_eNB_UE_stats returns
Cedric Roux's avatar
Cedric Roux committed
60
 * a non-NULL pointer for all the active CCs of an UE
61
 */
62
/*
63
int phy_stats_exist(module_id_t Mod_id, int rnti)
64 65
{
  int CC_id;
Cedric Roux's avatar
Cedric Roux committed
66 67
  int i;
  int UE_id          = find_UE_id(Mod_id, rnti);
68
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
Cedric Roux's avatar
Cedric Roux committed
69 70 71 72 73 74 75 76 77 78 79 80
  if (UE_id == -1) {
    LOG_W(MAC, "[eNB %d] UE %x not found, should be there (in phy_stats_exist)\n",
	  Mod_id, rnti);
    return 0;
  }
  if (UE_list->numactiveCCs[UE_id] == 0) {
    LOG_W(MAC, "[eNB %d] UE %x has no active CC (in phy_stats_exist)\n",
	  Mod_id, rnti);
    return 0;
  }
  for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
    CC_id = UE_list->ordered_CCids[i][UE_id];
81 82
    if (mac_xface->get_eNB_UE_stats(Mod_id, CC_id, rnti) == NULL)
      return 0;
83
  }
84 85
  return 1;
}
86
*/
87

88
// This function stores the downlink buffer for all the logical channels
89
void
90
store_dlsch_buffer(module_id_t Mod_id,
91
                   int slice_idx,
92 93
                   frame_t frameP,
                   sub_frame_t subframeP) {
Cedric Roux's avatar
Cedric Roux committed
94

95
  int UE_id, lcid;
96 97 98 99
  rnti_t rnti;
  mac_rlc_status_resp_t rlc_status;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  UE_TEMPLATE *UE_template;
100

101 102
  for (UE_id = 0; UE_id < MAX_MOBILES_PER_ENB; UE_id++) {
    if (UE_list->active[UE_id] != TRUE)
103 104
	    continue;

105
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx))
106
      continue;
107

108
    UE_template = &UE_list->UE_template[UE_PCCID(Mod_id, UE_id)][UE_id];
109

110 111 112
    // clear logical channel interface variables
    UE_template->dl_buffer_total = 0;
    UE_template->dl_pdus_total = 0;
113

114 115 116 117 118
    for (lcid = 0; lcid < MAX_NUM_LCID; ++lcid) {
      UE_template->dl_buffer_info[lcid] = 0;
      UE_template->dl_pdus_in_buffer[lcid] = 0;
      UE_template->dl_buffer_head_sdu_creation_time[lcid] = 0;
      UE_template->dl_buffer_head_sdu_remaining_size_to_send[lcid] = 0;
119
    }
120

121
    rnti = UE_RNTI(Mod_id, UE_id);
122

123
    for (lcid = 0; lcid < MAX_NUM_LCID; ++lcid) {    // loop over all the logical channels
124

125
	    rlc_status = mac_rlc_status_ind(Mod_id, rnti, Mod_id, frameP, subframeP,
126
				   ENB_FLAG_YES, MBMS_FLAG_NO, lcid, 0
127
#if (RRC_VERSION >= MAKE_VERSION(14, 0, 0))
128 129 130
                   ,0, 0
#endif
                   );
131 132 133
      UE_template->dl_buffer_info[lcid] = rlc_status.bytes_in_buffer;    //storing the dlsch buffer for each logical channel
      UE_template->dl_pdus_in_buffer[lcid] = rlc_status.pdus_in_buffer;
      UE_template->dl_buffer_head_sdu_creation_time[lcid] = rlc_status.head_sdu_creation_time;
134 135
      UE_template->dl_buffer_head_sdu_creation_time_max =
              cmax(UE_template->dl_buffer_head_sdu_creation_time_max, rlc_status.head_sdu_creation_time);
136 137 138 139
      UE_template->dl_buffer_head_sdu_remaining_size_to_send[lcid] = rlc_status.head_sdu_remaining_size_to_send;
      UE_template->dl_buffer_head_sdu_is_segmented[lcid] = rlc_status.head_sdu_is_segmented;
      UE_template->dl_buffer_total += UE_template->dl_buffer_info[lcid];    //storing the total dlsch buffer
      UE_template->dl_pdus_total += UE_template->dl_pdus_in_buffer[lcid];
140

Cedric Roux's avatar
Cedric Roux committed
141
#ifdef DEBUG_eNB_SCHEDULER
142

143
      /* note for dl_buffer_head_sdu_remaining_size_to_send[lcid] :
144 145
       * 0 if head SDU has not been segmented (yet), else remaining size not already segmented and sent
       */
146
      if (UE_template->dl_buffer_info[lcid] > 0)
147 148
        LOG_D(MAC,
              "[eNB %d][SLICE %d] Frame %d Subframe %d : RLC status for UE %d in LCID%d: total of %d pdus and size %d, head sdu queuing time %d, remaining size %d, is segmeneted %d \n",
149 150
              Mod_id, RC.mac[Mod_id]->slice_info.dl[slice_idx].id, frameP,
              subframeP, UE_id, lcid, UE_template->dl_pdus_in_buffer[lcid],
151 152 153 154
              UE_template->dl_buffer_info[lcid],
              UE_template->dl_buffer_head_sdu_creation_time[lcid],
              UE_template->dl_buffer_head_sdu_remaining_size_to_send[lcid],
              UE_template->dl_buffer_head_sdu_is_segmented[lcid]);
155

Cedric Roux's avatar
Cedric Roux committed
156
#endif
157

158

159
    }
160

161 162 163 164 165 166 167
    if (UE_template->dl_buffer_total > 0)
      LOG_D(MAC,
            "[eNB %d] Frame %d Subframe %d : RLC status for UE %d : total DL buffer size %d and total number of pdu %d \n",
            Mod_id, frameP, subframeP, UE_id,
            UE_template->dl_buffer_total,
            UE_template->dl_pdus_total);
  }
168 169
}

Niccolò Iardella's avatar
Niccolò Iardella committed
170 171 172
int cqi2mcs(int cqi) {
  return cqi_to_mcs[cqi];
}
173

174
// This function returns the estimated number of RBs required by each UE for downlink scheduling
175 176
void
assign_rbs_required(module_id_t Mod_id,
177
                    int slice_idx,
178 179
                    frame_t frameP,
                    sub_frame_t subframe,
180 181
                    uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
                    int min_rb_unit[NFAPI_CC_MAX])
182
{
183

184
  uint16_t TBS = 0;
185

186 187
  int UE_id, n, i, j, CC_id, pCCid, tmp;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
188
  slice_info_t *sli = &RC.mac[Mod_id]->slice_info;
189 190
  eNB_UE_STATS *eNB_UE_stats, *eNB_UE_stats_i, *eNB_UE_stats_j;
  int N_RB_DL;
191

192 193 194
  // clear rb allocations across all CC_id
  for (UE_id = 0; UE_id < MAX_MOBILES_PER_ENB; UE_id++) {
    if (UE_list->active[UE_id] != TRUE) continue;
195
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
196
    pCCid = UE_PCCID(Mod_id, UE_id);
197

198 199
    //update CQI information across component carriers
    for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
knopp's avatar
knopp committed
200

201 202
      CC_id = UE_list->ordered_CCids[n][UE_id];
      eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
203 204
//      eNB_UE_stats->dlsch_mcs1 = cmin(cqi_to_mcs[UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]], sli->dl[slice_idx].maxmcs);
      eNB_UE_stats->dlsch_mcs1 = cmin(cqi2mcs(UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id]), sli->dl[slice_idx].maxmcs);
205

206
    }
207

208 209 210 211 212 213 214 215 216 217 218 219 220
    // provide the list of CCs sorted according to MCS
    for (i = 0; i < UE_list->numactiveCCs[UE_id]; ++i) {
      eNB_UE_stats_i = &UE_list->eNB_UE_stats[UE_list->ordered_CCids[i][UE_id]][UE_id];
      for (j = i + 1; j < UE_list->numactiveCCs[UE_id]; j++) {
        DevAssert(j < NFAPI_CC_MAX);
        eNB_UE_stats_j = &UE_list->eNB_UE_stats[UE_list->ordered_CCids[j][UE_id]][UE_id];
        if (eNB_UE_stats_j->dlsch_mcs1 > eNB_UE_stats_i->dlsch_mcs1) {
          tmp = UE_list->ordered_CCids[i][UE_id];
          UE_list->ordered_CCids[i][UE_id] = UE_list->ordered_CCids[j][UE_id];
          UE_list->ordered_CCids[j][UE_id] = tmp;
        }
      }
    }
221

222 223
    if (UE_list->UE_template[pCCid][UE_id].dl_buffer_total > 0) {
      LOG_D(MAC, "[preprocessor] assign RB for UE %d\n", UE_id);
224

225 226 227
      for (i = 0; i < UE_list->numactiveCCs[UE_id]; i++) {
        CC_id = UE_list->ordered_CCids[i][UE_id];
        eNB_UE_stats = &UE_list->eNB_UE_stats[CC_id][UE_id];
228

229 230 231 232 233
        if (eNB_UE_stats->dlsch_mcs1 == 0) {
          nb_rbs_required[CC_id][UE_id] = 4;    // don't let the TBS get too small
        } else {
          nb_rbs_required[CC_id][UE_id] = min_rb_unit[CC_id];
        }
234

235
        TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, nb_rbs_required[CC_id][UE_id]);
236

237 238 239 240 241 242
        LOG_D(MAC,
              "[preprocessor] start RB assignement for UE %d CC_id %d dl buffer %d (RB unit %d, MCS %d, TBS %d) \n",
              UE_id, CC_id,
              UE_list->UE_template[pCCid][UE_id].dl_buffer_total,
              nb_rbs_required[CC_id][UE_id],
              eNB_UE_stats->dlsch_mcs1, TBS);
243

244
        N_RB_DL = to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
245

246 247
        UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx] =
                nb_rbs_allowed_slice(sli->dl[slice_idx].pct, N_RB_DL);
248

249 250 251 252
        /* calculating required number of RBs for each UE */
        while (TBS < UE_list->UE_template[pCCid][UE_id].dl_buffer_total) {
          nb_rbs_required[CC_id][UE_id] += min_rb_unit[CC_id];

253 254 255
          if (nb_rbs_required[CC_id][UE_id] > UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx]) {
            TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx]);
            nb_rbs_required[CC_id][UE_id] = UE_list->UE_sched_ctrl[UE_id].max_rbs_allowed_slice[CC_id][slice_idx];
256 257 258 259 260 261 262 263 264 265
            break;
          }
          TBS = get_TBS_DL(eNB_UE_stats->dlsch_mcs1, nb_rbs_required[CC_id][UE_id]);
        } // end of while

        LOG_D(MAC,
              "[eNB %d] Frame %d: UE %d on CC %d: RB unit %d,  nb_required RB %d (TBS %d, mcs %d)\n",
              Mod_id, frameP, UE_id, CC_id, min_rb_unit[CC_id],
              nb_rbs_required[CC_id][UE_id], TBS,
              eNB_UE_stats->dlsch_mcs1);
266

267
        sli->pre_processor_results[slice_idx].mcs[CC_id][UE_id] = eNB_UE_stats->dlsch_mcs1;
268
      }
knopp's avatar
 
knopp committed
269
    }
270
  }
knopp's avatar
 
knopp committed
271
}
gauthier's avatar
gauthier committed
272 273


knopp's avatar
 
knopp committed
274
// This function scans all CC_ids for a particular UE to find the maximum round index of its HARQ processes
275 276
int
maxround(module_id_t Mod_id, uint16_t rnti, int frame,
277
         sub_frame_t subframe, uint8_t ul_flag) {
gauthier's avatar
gauthier committed
278

279 280 281 282
  uint8_t round, round_max = 0, UE_id;
  int CC_id, harq_pid;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  COMMON_channels_t *cc;
283

284 285
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
    cc = &RC.mac[Mod_id]->common_channels[CC_id];
286

287 288
    UE_id = find_UE_id(Mod_id, rnti);
    harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frame ,subframe);
289

290 291 292
    round = UE_list->UE_sched_ctrl[UE_id].round[CC_id][harq_pid];
    if (round > round_max) {
      round_max = round;
293
    }
294
  }
295

296
  return round_max;
knopp's avatar
 
knopp committed
297
}
gauthier's avatar
gauthier committed
298

knopp's avatar
 
knopp committed
299
// This function scans all CC_ids for a particular UE to find the maximum DL CQI
Cedric Roux's avatar
Cedric Roux committed
300
// it returns -1 if the UE is not found in PHY layer (get_eNB_UE_stats gives NULL)
301 302 303 304
int maxcqi(module_id_t Mod_id, int32_t UE_id) {
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  int CC_id, n;
  int CQI = 0;
gauthier's avatar
gauthier committed
305

306 307
  for (n = 0; n < UE_list->numactiveCCs[UE_id]; n++) {
    CC_id = UE_list->ordered_CCids[n][UE_id];
308

309 310
    if (UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id] > CQI) {
      CQI = UE_list->UE_sched_ctrl[UE_id].dl_cqi[CC_id];
311
    }
312
  }
gauthier's avatar
gauthier committed
313

314
  return CQI;
knopp's avatar
 
knopp committed
315
}
gauthier's avatar
gauthier committed
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330
long min_lcgidpriority(module_id_t Mod_id, int32_t UE_id) {
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  int i;
  int pCC_id = UE_PCCID(Mod_id, UE_id);
  long ret = UE_list->UE_template[pCC_id][UE_id].lcgidpriority[0];

  for (i = 1; i < 11; ++i) {
    if (UE_list->UE_template[pCC_id][UE_id].lcgidpriority[i] < ret)
      ret = UE_list->UE_template[pCC_id][UE_id].lcgidpriority[i];
  }

  return ret;
}

331
struct sort_ue_dl_params {
332 333 334
    int Mod_idP;
    int frameP;
    int subframeP;
335
    int slice_idx;
336
};
gauthier's avatar
gauthier committed
337

338 339
static int ue_dl_compare(const void *_a, const void *_b, void *_params)
{
340 341
  struct sort_ue_dl_params *params = _params;
  UE_list_t *UE_list = &RC.mac[params->Mod_idP]->UE_list;
342

343
  int i;
344
  int slice_idx = params->slice_idx;
345 346
  int UE_id1 = *(const int *) _a;
  int UE_id2 = *(const int *) _b;
347

348 349 350
  int rnti1 = UE_RNTI(params->Mod_idP, UE_id1);
  int pCC_id1 = UE_PCCID(params->Mod_idP, UE_id1);
  int round1 = maxround(params->Mod_idP, rnti1, params->frameP, params->subframeP, 1);
351

352 353 354
  int rnti2 = UE_RNTI(params->Mod_idP, UE_id2);
  int pCC_id2 = UE_PCCID(params->Mod_idP, UE_id2);
  int round2 = maxround(params->Mod_idP, rnti2, params->frameP, params->subframeP, 1);
355

356 357
  int cqi1 = maxcqi(params->Mod_idP, UE_id1);
  int cqi2 = maxcqi(params->Mod_idP, UE_id2);
358

359 360 361
  long lcgid1 = min_lcgidpriority(params->Mod_idP, UE_id1);
  long lcgid2 = min_lcgidpriority(params->Mod_idP, UE_id2);

362
  for (i = 0; i < CR_NUM; ++i) {
363
    switch (UE_list->sorting_criteria[slice_idx][i]) {
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

      case CR_ROUND :
        if (round1 > round2)
          return -1;
        if (round1 < round2)
          return 1;
        break;

      case CR_SRB12 :
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] >
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
          return -1;
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_info[2] <
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[1] +
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_info[2])
          return 1;
        break;

      case CR_HOL :
        if (UE_list-> UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max >
            UE_list-> UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
          return -1;
        if (UE_list-> UE_template[pCC_id1][UE_id1].dl_buffer_head_sdu_creation_time_max <
            UE_list-> UE_template[pCC_id2][UE_id2].dl_buffer_head_sdu_creation_time_max)
          return 1;
        break;

      case CR_LC :
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total >
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
          return -1;
        if (UE_list->UE_template[pCC_id1][UE_id1].dl_buffer_total <
            UE_list->UE_template[pCC_id2][UE_id2].dl_buffer_total)
          return 1;
        break;

      case CR_CQI :
        if (cqi1 > cqi2)
          return -1;
        if (cqi1 < cqi2)
          return 1;
Niccolò Iardella's avatar
Niccolò Iardella committed
408
        break;
409

410 411 412 413 414 415
      case CR_LCP :
        if (lcgid1 < lcgid2)
          return -1;
        if (lcgid1 > lcgid2)
          return 1;

416 417 418 419
      default :
        break;
    }
  }
420 421

    return 0;
422
}
gauthier's avatar
gauthier committed
423

424
void decode_sorting_policy(module_id_t Mod_idP, int slice_idx) {
425 426 427
  int i;

  UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
428
  uint32_t policy = RC.mac[Mod_idP]->slice_info.dl[slice_idx].sorting;
429 430 431 432 433 434
  uint32_t mask = 0x0000000F;
  uint16_t criterion;

  for (i = 0; i < CR_NUM; ++i) {
    criterion = (uint16_t) (policy >> 4 * (CR_NUM - 1 - i) & mask);
    if (criterion >= CR_NUM) {
435 436 437 438
      LOG_W(MAC,
            "Invalid criterion in slice index %d ID %d policy, revert to default policy \n",
            slice_idx, RC.mac[Mod_idP]->slice_info.dl[slice_idx].id);
      RC.mac[Mod_idP]->slice_info.dl[slice_idx].sorting = 0x12345;
439 440
      break;
    }
441
    UE_list->sorting_criteria[slice_idx][i] = criterion;
442
  }
443 444
}

445
void decode_slice_positioning(module_id_t Mod_idP,
446 447 448
                              int slice_idx,
                              uint8_t slice_allocation_mask[NFAPI_CC_MAX][N_RBG_MAX])
{
449 450 451 452
  uint8_t CC_id;
  int RBG, start_frequency, end_frequency;

  // Init slice_alloc_mask
453
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_idP]; ++CC_id) {
454 455 456 457 458
    for (RBG = 0; RBG < N_RBG_MAX; ++RBG) {
      slice_allocation_mask[CC_id][RBG] = 0;
    }
  }

459 460
  start_frequency = RC.mac[Mod_idP]->slice_info.dl[slice_idx].pos_low;
  end_frequency = RC.mac[Mod_idP]->slice_info.dl[slice_idx].pos_high;
461
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_idP]; ++CC_id) {
462 463 464 465 466 467
    for (RBG = start_frequency; RBG <= end_frequency; ++RBG) {
      slice_allocation_mask[CC_id][RBG] = 1;
    }
  }
}

468

knopp's avatar
 
knopp committed
469
// This fuction sorts the UE in order their dlsch buffer and CQI
470
void sort_UEs(module_id_t Mod_idP, int slice_idx, int frameP, sub_frame_t subframeP)
471
{
472 473 474
  int i;
  int list[MAX_MOBILES_PER_ENB];
  int list_size = 0;
475
  struct sort_ue_dl_params params = {Mod_idP, frameP, subframeP, slice_idx};
476

477
  UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;
478

479
  for (i = 0; i < MAX_MOBILES_PER_ENB; i++) {
480

481 482 483
    if (UE_list->active[i] == FALSE) continue;
    if (UE_RNTI(Mod_idP, i) == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[i].ul_out_of_sync == 1) continue;
484
    if (!ue_dl_slice_membership(Mod_idP, i, slice_idx)) continue;
485

486 487 488
    list[list_size] = i;
    list_size++;
  }
489

490
  decode_sorting_policy(Mod_idP, slice_idx);
491

492
  qsort_r(list, list_size, sizeof(int), ue_dl_compare, &params);
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 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 570
  if (list_size) {
    for (i = 0; i < list_size - 1; ++i)
      UE_list->next[list[i]] = list[i + 1];
    UE_list->next[list[list_size - 1]] = -1;
    UE_list->head = list[0];
  } else {
    UE_list->head = -1;
  }

#if 0


    int UE_id1, UE_id2;
    int pCC_id1, pCC_id2;
    int cqi1, cqi2, round1, round2;
    int i = 0, ii = 0;		//,j=0;
    rnti_t rnti1, rnti2;

    UE_list_t *UE_list = &RC.mac[Mod_idP]->UE_list;

    for (i = UE_list->head; i >= 0; i = UE_list->next[i]) {

	for (ii = UE_list->next[i]; ii >= 0; ii = UE_list->next[ii]) {

	    UE_id1 = i;
	    rnti1 = UE_RNTI(Mod_idP, UE_id1);
	    if (rnti1 == NOT_A_RNTI)
		continue;
	    if (UE_list->UE_sched_ctrl[UE_id1].ul_out_of_sync == 1)
		continue;
	    pCC_id1 = UE_PCCID(Mod_idP, UE_id1);
	    cqi1 = maxcqi(Mod_idP, UE_id1);	//
	    round1 = maxround(Mod_idP, rnti1, frameP, subframeP, 0);

	    UE_id2 = ii;
	    rnti2 = UE_RNTI(Mod_idP, UE_id2);
	    if (rnti2 == NOT_A_RNTI)
		continue;
	    if (UE_list->UE_sched_ctrl[UE_id2].ul_out_of_sync == 1)
		continue;
	    cqi2 = maxcqi(Mod_idP, UE_id2);
	    round2 = maxround(Mod_idP, rnti2, frameP, subframeP, 0);	//mac_xface->get_ue_active_harq_pid(Mod_id,rnti2,subframe,&harq_pid2,&round2,0);
	    pCC_id2 = UE_PCCID(Mod_idP, UE_id2);

	    if (round2 > round1) {	// Check first if one of the UEs has an active HARQ process which needs service and swap order
		swap_UEs(UE_list, UE_id1, UE_id2, 0);
	    } else if (round2 == round1) {
		// RK->NN : I guess this is for fairness in the scheduling. This doesn't make sense unless all UEs have the same configuration of logical channels.  This should be done on the sum of all information that has to be sent.  And still it wouldn't ensure fairness.  It should be based on throughput seen by each UE or maybe using the head_sdu_creation_time, i.e. swap UEs if one is waiting longer for service.
		//  for(j=0;j<MAX_NUM_LCID;j++){
		//    if (eNB_mac_inst[Mod_id][pCC_id1].UE_template[UE_id1].dl_buffer_info[j] <
		//      eNB_mac_inst[Mod_id][pCC_id2].UE_template[UE_id2].dl_buffer_info[j]){

		// first check the buffer status for SRB1 and SRB2

		if ((UE_list->UE_template[pCC_id1][UE_id1].
		     dl_buffer_info[1] +
		     UE_list->UE_template[pCC_id1][UE_id1].
		     dl_buffer_info[2]) <
		    (UE_list->UE_template[pCC_id2][UE_id2].
		     dl_buffer_info[1] +
		     UE_list->UE_template[pCC_id2][UE_id2].
		     dl_buffer_info[2])) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		} else if (UE_list->UE_template[pCC_id1]
			   [UE_id1].dl_buffer_head_sdu_creation_time_max <
			   UE_list->UE_template[pCC_id2]
			   [UE_id2].dl_buffer_head_sdu_creation_time_max) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		} else if (UE_list->UE_template[pCC_id1][UE_id1].
			   dl_buffer_total <
			   UE_list->UE_template[pCC_id2][UE_id2].
			   dl_buffer_total) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		} else if (cqi1 < cqi2) {
		    swap_UEs(UE_list, UE_id1, UE_id2, 0);
		}
	    }
571
	}
572 573
    }
#endif
574 575
}

576
void dlsch_scheduler_pre_processor_partitioning(module_id_t Mod_id,
577 578 579
                                                int slice_idx,
                                                const uint8_t rbs_retx[NFAPI_CC_MAX])
{
580 581 582
  int UE_id, CC_id, N_RB_DL, i;
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
  UE_sched_ctrl *ue_sched_ctl;
583
  uint16_t available_rbs;
584 585 586 587 588

  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {

    if (UE_RNTI(Mod_id, UE_id) == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
589
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
590 591 592 593 594 595

    ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];

    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); ++i) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
      N_RB_DL = to_prb(RC.mac[Mod_id]->common_channels[CC_id].mib->message.dl_Bandwidth);
596
      available_rbs = nb_rbs_allowed_slice(RC.mac[Mod_id]->slice_info.dl[slice_idx].pct, N_RB_DL);
597
      if (rbs_retx[CC_id] < available_rbs)
598
        ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_idx] = available_rbs - rbs_retx[CC_id];
599
      else
600
        ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_idx] = 0;
601 602 603 604
    }
  }
}

605
void dlsch_scheduler_pre_processor_accounting(module_id_t Mod_id,
606
                                              int slice_idx,
607 608
                                              frame_t frameP,
                                              sub_frame_t subframeP,
609
                                              int min_rb_unit[NFAPI_CC_MAX],
610
                                              uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
611
                                              uint16_t nb_rbs_accounted[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB])
612
{
613
  int UE_id, CC_id;
614
  int i;
knopp's avatar
 
knopp committed
615

616
  rnti_t rnti;
617
  uint8_t harq_pid, round;
618
  uint16_t available_rbs;
619
  uint8_t rbs_retx[NFAPI_CC_MAX];
620
  uint16_t average_rbs_per_user[NFAPI_CC_MAX];
621
  int total_ue_count[NFAPI_CC_MAX];
622 623
  int ue_count_newtx[NFAPI_CC_MAX];
  int ue_count_retx[NFAPI_CC_MAX];
624
  //uint8_t ue_retx_flag[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB];
625

626
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;
627
  UE_sched_ctrl *ue_sched_ctl;
628
  COMMON_channels_t *cc;
629

630
  // Reset
knopp's avatar
knopp committed
631
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
632
    total_ue_count[CC_id] = 0;
633 634 635
    ue_count_newtx[CC_id] = 0;
    ue_count_retx[CC_id] = 0;
    rbs_retx[CC_id] = 0;
636
    average_rbs_per_user[CC_id] = 0;
637 638 639
    //for (UE_id = 0; UE_id < NFAPI_CC_MAX; ++UE_id) {
    //  ue_retx_flag[CC_id][UE_id] = 0;
    //}
640
  }
knopp's avatar
 
knopp committed
641

642
  // Find total UE count, and account the RBs required for retransmissions
643 644
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
    rnti = UE_RNTI(Mod_id, UE_id);
645 646
    if (rnti == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
647
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
648

649
    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); ++i) {
650
      CC_id = UE_list->ordered_CCids[i][UE_id];
651 652
      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
      cc = &RC.mac[Mod_id]->common_channels[CC_id];
653
      // FIXME: Can we use subframe2harqpid() here?
654 655 656 657 658 659
      if (cc->tdd_Config)
        harq_pid = ((frameP * 10) + subframeP) % 10;
      else
        harq_pid = ((frameP * 10) + subframeP) & 7;
      round = ue_sched_ctl->round[CC_id][harq_pid];

660 661 662
      if (nb_rbs_required[CC_id][UE_id] > 0) {
        total_ue_count[CC_id]++;
      }
663 664 665

      if (round != 8) {
        nb_rbs_required[CC_id][UE_id] = UE_list->UE_template[CC_id][UE_id].nb_rb[harq_pid];
666 667
        rbs_retx[CC_id] += nb_rbs_required[CC_id][UE_id];
        ue_count_retx[CC_id]++;
668
        //ue_retx_flag[CC_id][UE_id] = 1;
669 670
      } else {
        ue_count_newtx[CC_id]++;
671 672 673
      }
    }
  }
674

675 676
  // PARTITIONING
  // Reduces the available RBs according to slicing configuration
677
  dlsch_scheduler_pre_processor_partitioning(Mod_id, slice_idx, rbs_retx);
678

679
  switch (RC.mac[Mod_id]->slice_info.dl[slice_idx].accounting) {
680

681
    // If greedy scheduling, try to account all the required RBs
682
    case POL_GREEDY:
683 684 685 686
      for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
        rnti = UE_RNTI(Mod_id, UE_id);
        if (rnti == NOT_A_RNTI) continue;
        if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
687
        if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
knopp's avatar
 
knopp committed
688

689 690 691 692 693 694 695 696 697
        for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
          CC_id = UE_list->ordered_CCids[i][UE_id];
          nb_rbs_accounted[CC_id][UE_id] = nb_rbs_required[CC_id][UE_id];
        }
      }
      break;

    // Use the old, fair algorithm
    // Loop over all active UEs and account the avg number of RBs to each UE, based on all non-retx UEs.
698
    // case POL_FAIR:
699 700 701 702 703 704 705 706
    default:
      // FIXME: This is not ideal, why loop on UEs to find average_rbs_per_user[], that is per-CC?
      // TODO: Look how to loop on active CCs only without using the UE_num_active_CC() function.
      for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
        rnti = UE_RNTI(Mod_id, UE_id);

        if (rnti == NOT_A_RNTI) continue;
        if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
707
        if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
708 709

        for (i = 0; i < UE_num_active_CC(UE_list, UE_id); ++i) {
710

711 712
          CC_id = UE_list->ordered_CCids[i][UE_id];
          ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
713
          available_rbs = ue_sched_ctl->max_rbs_allowed_slice[CC_id][slice_idx];
714

715
          if (ue_count_newtx[CC_id] == 0) {
716
            average_rbs_per_user[CC_id] = 0;
717 718
          } else if (min_rb_unit[CC_id]*ue_count_newtx[CC_id] <= available_rbs) {
            average_rbs_per_user[CC_id] = (uint16_t)floor(available_rbs/ue_count_newtx[CC_id]);
719 720 721 722 723 724
          } else {
            // consider the total number of use that can be scheduled UE
            average_rbs_per_user[CC_id] = (uint16_t)min_rb_unit[CC_id];
          }
        }
      }
725

726 727 728 729 730 731
      // note: nb_rbs_required is assigned according to total_buffer_dl
      // extend nb_rbs_required to capture per LCID RB required
      for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
        rnti = UE_RNTI(Mod_id, UE_id);
        if (rnti == NOT_A_RNTI) continue;
        if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
732
        if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
733 734 735 736 737

        for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
          CC_id = UE_list->ordered_CCids[i][UE_id];
          nb_rbs_accounted[CC_id][UE_id] = cmin(average_rbs_per_user[CC_id], nb_rbs_required[CC_id][UE_id]);
        }
738
      }
739
      break;
740
  }
knopp's avatar
 
knopp committed
741

742 743 744 745 746



  // Check retransmissions
  // TODO: Do this once at the beginning
747 748
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
    rnti = UE_RNTI(Mod_id, UE_id);
749 750
    if (rnti == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
751
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
knopp's avatar
 
knopp committed
752

753 754
    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
755 756
      ue_sched_ctl = &UE_list->UE_sched_ctrl[UE_id];
      cc = &RC.mac[Mod_id]->common_channels[CC_id];
757
      harq_pid = frame_subframe2_dl_harq_pid(cc->tdd_Config,frameP ,subframeP);
758 759 760 761
      round = ue_sched_ctl->round[CC_id][harq_pid];

      // control channel or retransmission
      /* TODO: do we have to check for retransmission? */
762
      if (mac_eNB_get_rrc_status(Mod_id, rnti) < RRC_RECONFIGURED || round != 8) {
763
        nb_rbs_accounted[CC_id][UE_id] = nb_rbs_required[CC_id][UE_id];
764
      }
765
    }
766
  }
767 768 769
}

void dlsch_scheduler_pre_processor_positioning(module_id_t Mod_id,
770
                                               int slice_idx,
771 772
                                               int min_rb_unit[NFAPI_CC_MAX],
                                               uint16_t nb_rbs_required[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
773
                                               uint16_t nb_rbs_accounted[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
774
                                               uint16_t nb_rbs_remaining[NFAPI_CC_MAX][MAX_MOBILES_PER_ENB],
775
                                               uint8_t rballoc_sub[NFAPI_CC_MAX][N_RBG_MAX],
776 777
                                               uint8_t MIMO_mode_indicator[NFAPI_CC_MAX][N_RBG_MAX])
{
778 779
  int UE_id, CC_id;
  int i;
780
#ifdef TM5
781
  uint8_t transmission_mode;
782
#endif
783 784 785 786
  uint8_t slice_allocation_mask[NFAPI_CC_MAX][N_RBG_MAX];
  int N_RBG[NFAPI_CC_MAX];
  UE_list_t *UE_list = &RC.mac[Mod_id]->UE_list;

787
  decode_slice_positioning(Mod_id, slice_idx, slice_allocation_mask);
788

789 790 791 792
  for (CC_id = 0; CC_id < RC.nb_mac_CC[Mod_id]; CC_id++) {
    COMMON_channels_t *cc = &RC.mac[Mod_id]->common_channels[CC_id];
    N_RBG[CC_id] = to_rbg(cc->mib->message.dl_Bandwidth);
  }
Xu Bo's avatar
Xu Bo committed
793

794 795
  // Try to allocate accounted RBs
  for (UE_id = UE_list->head; UE_id >= 0; UE_id = UE_list->next[UE_id]) {
796

797 798
    if (UE_RNTI(Mod_id, UE_id) == NOT_A_RNTI) continue;
    if (UE_list->UE_sched_ctrl[UE_id].ul_out_of_sync == 1) continue;
799
    if (!ue_dl_slice_membership(Mod_id, UE_id, slice_idx)) continue;
800

801 802 803
    for (i = 0; i < UE_num_active_CC(UE_list, UE_id); i++) {
      CC_id = UE_list->ordered_CCids[i][UE_id];
      nb_rbs_remaining[CC_id][UE_id] = nb_rbs_accounted[CC_id][UE_id];
804
#ifdef TM5
805
      transmission_mode = get_tmode(Mod_id, CC_id, UE_id);
806
#endif
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830

      if (nb_rbs_required[CC_id][UE_id] > 0)
        LOG_D(MAC,
              "Step 1: nb_rbs_remaining[%d][%d]= %d (accounted %d, required %d,  pre_nb_available_rbs %d, N_RBG %d, rb_unit %d)\n",
              CC_id,
              UE_id,
              nb_rbs_remaining[CC_id][UE_id],
              nb_rbs_accounted[CC_id][UE_id],
              nb_rbs_required[CC_id][UE_id],
              UE_list->UE_sched_ctrl[UE_id].pre_nb_available_rbs[CC_id],
              N_RBG[CC_id],
              min_rb_unit[CC_id]);

      LOG_T(MAC, "calling dlsch_scheduler_pre_processor_allocate .. \n ");
      dlsch_scheduler_pre_processor_allocate(Mod_id,
                                             UE_id,
                                             CC_id,
                                             N_RBG[CC_id],
                                             min_rb_unit[CC_id],
                                             nb_rbs_required,
                                             nb_rbs_remaining,
                                             rballoc_sub,
                                             slice_allocation_mask,
                                             MIMO_mode_indicator);
831

832
#ifdef TM5
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858