rrc_gNB_du.c 24.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

#include "rrc_gNB_du.h"
23
#include "rrc_gNB_NGAP.h"
24 25
#include "common/ran_context.h"
#include "nr_rrc_defs.h"
26
#include "rrc_gNB_UE_context.h"
27
#include "openair2/F1AP/f1ap_common.h"
28
#include "openair2/F1AP/f1ap_ids.h"
29
#include "executables/softmodem-common.h"
30 31
#include "common/utils/ds/seq_arr.h"
#include "common/utils/alg/foreach.h"
32
#include "lib/f1ap_interface_management.h"
33

34 35 36 37 38 39 40 41 42 43
int get_dl_band(const struct f1ap_served_cell_info_t *cell_info)
{
  return cell_info->mode == F1AP_MODE_TDD ? cell_info->tdd.freqinfo.band : cell_info->fdd.dl_freqinfo.band;
}

int get_ssb_scs(const struct f1ap_served_cell_info_t *cell_info)
{
  return cell_info->mode == F1AP_MODE_TDD ? cell_info->tdd.tbw.scs : cell_info->fdd.dl_tbw.scs;
}

44
static int ssb_arfcn_mtc(const NR_MeasurementTimingConfiguration_t *mtc)
45 46
{
  /* format has been verified when accepting MeasurementTimingConfiguration */
47
  NR_MeasTimingList_t *mtlist = mtc->criticalExtensions.choice.c1->choice.measTimingConf->measTiming;
48 49
  return mtlist->list.array[0]->frequencyAndTiming->carrierFreq;
}
50

51 52 53 54 55 56
int get_ssb_arfcn(const struct nr_rrc_du_container_t *du)
{
  DevAssert(du != NULL && du->mtc != NULL);
  return ssb_arfcn_mtc(du->mtc);
}

Robert Schmidt's avatar
Robert Schmidt committed
57 58 59 60 61 62 63 64 65 66 67 68
static int du_compare(const nr_rrc_du_container_t *a, const nr_rrc_du_container_t *b)
{
  if (a->assoc_id > b->assoc_id)
    return 1;
  if (a->assoc_id == b->assoc_id)
    return 0;
  return -1; /* a->assoc_id < b->assoc_id */
}

/* Tree management functions */
RB_GENERATE/*_STATIC*/(rrc_du_tree, nr_rrc_du_container_t, entries, du_compare);

69 70 71 72 73
static bool rrc_gNB_plmn_matches(const gNB_RRC_INST *rrc, const f1ap_served_cell_info_t *info)
{
  const gNB_RrcConfigurationReq *conf = &rrc->configuration;
  return conf->num_plmn == 1 // F1 supports only one
    && conf->mcc[0] == info->plmn.mcc
74
    && conf->mnc[0] == info->plmn.mnc;
75 76
}

77
static bool extract_sys_info(const f1ap_gnb_du_system_info_t *sys_info, NR_MIB_t **mib, NR_SIB1_t **sib1)
78 79 80 81 82
{
  DevAssert(sys_info != NULL);
  DevAssert(mib != NULL);
  DevAssert(sib1 != NULL);

83 84 85 86
  asn_dec_rval_t dec_rval = uper_decode_complete(NULL, &asn_DEF_NR_MIB, (void **)mib, sys_info->mib, sys_info->mib_length);
  if (dec_rval.code != RC_OK) {
    LOG_E(RRC, "Failed to decode NR_MIB (%zu bits) of DU\n", dec_rval.consumed);
    ASN_STRUCT_FREE(asn_DEF_NR_MIB, *mib);
87 88 89 90 91 92
    return false;
  }

  if (sys_info->sib1) {
    dec_rval = uper_decode_complete(NULL, &asn_DEF_NR_SIB1, (void **)sib1, sys_info->sib1, sys_info->sib1_length);
    if (dec_rval.code != RC_OK) {
93
      ASN_STRUCT_FREE(asn_DEF_NR_MIB, *mib);
94 95 96 97 98 99
      ASN_STRUCT_FREE(asn_DEF_NR_SIB1, *sib1);
      LOG_E(RRC, "Failed to decode NR_SIB1 (%zu bits), rejecting DU\n", dec_rval.consumed);
      return false;
    }
  }

100 101
  if (LOG_DEBUGFLAG(DEBUG_ASN1)) {
    xer_fprint(stdout, &asn_DEF_NR_MIB, *mib);
102
    xer_fprint(stdout, &asn_DEF_NR_SIB1, *sib1);
103
  }
104 105 106 107

  return true;
}

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static NR_MeasurementTimingConfiguration_t *extract_mtc(uint8_t *buf, int buf_len)
{
  NR_MeasurementTimingConfiguration_t *mtc = NULL;
  asn_dec_rval_t dec_rval = uper_decode_complete(NULL, &asn_DEF_NR_MeasurementTimingConfiguration, (void **)&mtc, buf, buf_len);
  if (dec_rval.code != RC_OK) {
    ASN_STRUCT_FREE(asn_DEF_NR_MeasurementTimingConfiguration, mtc);
    return NULL;
  }
  /* verify that it has the format we need */
  if (mtc->criticalExtensions.present != NR_MeasurementTimingConfiguration__criticalExtensions_PR_c1
      || mtc->criticalExtensions.choice.c1 == NULL
      || mtc->criticalExtensions.choice.c1->present != NR_MeasurementTimingConfiguration__criticalExtensions__c1_PR_measTimingConf
      || mtc->criticalExtensions.choice.c1->choice.measTimingConf == NULL
      || mtc->criticalExtensions.choice.c1->choice.measTimingConf->measTiming == NULL
      || mtc->criticalExtensions.choice.c1->choice.measTimingConf->measTiming->list.count == 0) {
    LOG_E(RRC, "error: measurementTimingConfiguration does not have expected format (at least one measTiming entry\n");
    if (LOG_DEBUGFLAG(DEBUG_ASN1))
      xer_fprint(stdout, &asn_DEF_NR_MeasurementTimingConfiguration, mtc);
    ASN_STRUCT_FREE(asn_DEF_NR_MeasurementTimingConfiguration, mtc);
    return NULL;
  }
  return mtc;
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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
static int nr_cell_id_match(const void *key, const void *element)
{
  const int *key_id = (const int *)key;
  const neighbour_cell_configuration_t *config_element = (const neighbour_cell_configuration_t *)element;

  if (*key_id < config_element->nr_cell_id) {
    return -1;
  } else if (*key_id == config_element->nr_cell_id) {
    return 0;
  }

  return 1;
}

static neighbour_cell_configuration_t *get_cell_neighbour_list(const gNB_RRC_INST *rrc, const f1ap_served_cell_info_t *cell_info)
{
  void *base = seq_arr_front(rrc->neighbour_cell_configuration);
  size_t nmemb = seq_arr_size(rrc->neighbour_cell_configuration);
  size_t size = sizeof(neighbour_cell_configuration_t);

  void *it = bsearch((void *)&cell_info->nr_cellid, base, nmemb, size, nr_cell_id_match);

  return (neighbour_cell_configuration_t *)it;
}

const struct f1ap_served_cell_info_t *get_cell_information_by_phycellId(int phyCellId)
{
  gNB_RRC_INST *rrc = RC.nrrrc[0];
  nr_rrc_du_container_t *it = NULL;
  RB_FOREACH (it, rrc_du_tree, &rrc->dus) {
    for (int cellIdx = 0; cellIdx < it->setup_req->num_cells_available; cellIdx++) {
      const f1ap_served_cell_info_t *cell_info = &(it->setup_req->cell[cellIdx].info);
      if (cell_info->nr_pci == phyCellId) {
        LOG_D(NR_RRC, "HO LOG: Found cell with phyCellId %d\n", phyCellId);
        return cell_info;
      }
    }
  }
  return NULL;
}

static void is_intra_frequency_neighbour(void *ssb_arfcn, void *neighbour_cell)
{
  uint32_t *ssb_arfcn_ptr = (uint32_t *)ssb_arfcn;
  nr_neighbour_gnb_configuration_t *neighbour_cell_ptr = (nr_neighbour_gnb_configuration_t *)neighbour_cell;

  if (*ssb_arfcn_ptr == neighbour_cell_ptr->absoluteFrequencySSB) {
    LOG_D(NR_RRC, "HO LOG: found intra frequency neighbour %lu!\n", neighbour_cell_ptr->nrcell_id);
    neighbour_cell_ptr->isIntraFrequencyNeighbour = true;
  }
}
/**
 * @brief Labels neighbour cells if they are intra frequency to prepare meas config only for intra frequency ho
 * @param[in] rrc     Pointer to RRC instance
 * @param[in] cell_info Pointer to cell information
 */
static void label_intra_frequency_neighbours(gNB_RRC_INST *rrc,
                                             const nr_rrc_du_container_t *du,
                                             const f1ap_served_cell_info_t *cell_info)
{
  if (!rrc->neighbour_cell_configuration)
    return;

  neighbour_cell_configuration_t *neighbour_cell_config = get_cell_neighbour_list(rrc, cell_info);
  if (!neighbour_cell_config)
    return;

  LOG_D(NR_RRC, "HO LOG: Cell: %lu has neighbour cell configuration!\n", cell_info->nr_cellid);
  uint32_t ssb_arfcn = get_ssb_arfcn(du);

  seq_arr_t *cell_neighbour_list = neighbour_cell_config->neighbour_cells;
  for_each(cell_neighbour_list, (void *)&ssb_arfcn, is_intra_frequency_neighbour);
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
static bool valid_du_in_neighbour_configs(const seq_arr_t *neighbour_cell_configuration, const f1ap_served_cell_info_t *cell, int ssb_arfcn)
{
  for (int c = 0; c < neighbour_cell_configuration->size; c++) {
    const neighbour_cell_configuration_t *neighbour_config = seq_arr_at(neighbour_cell_configuration, c);
    for (int ni = 0; ni < neighbour_config->neighbour_cells->size; ni++) {
      const nr_neighbour_gnb_configuration_t *nc = seq_arr_at(neighbour_config->neighbour_cells, ni);
      if (nc->nrcell_id != cell->nr_cellid)
        continue;
      // current cell is in the nc config, check that config matches
      if (nc->physicalCellId != cell->nr_pci) {
        LOG_W(NR_RRC, "Cell %ld in neighbour config: PCI mismatch (%d vs %d)\n", cell->nr_cellid, cell->nr_pci, nc->physicalCellId);
        return false;
      }
      if (cell->tac && nc->tac != *cell->tac) {
        LOG_W(NR_RRC, "Cell %ld in neighbour config: TAC mismatch (%d vs %d)\n", cell->nr_cellid, *cell->tac, nc->tac);
        return false;
      }
      if (ssb_arfcn != nc->absoluteFrequencySSB) {
        LOG_W(NR_RRC,
              "Cell %ld in neighbour config: SSB ARFCN mismatch (%d vs %d)\n",
              cell->nr_cellid,
              ssb_arfcn,
              nc->absoluteFrequencySSB);
        return false;
      }
      if (get_ssb_scs(cell) != nc->subcarrierSpacing) {
        LOG_W(NR_RRC,
              "Cell %ld in neighbour config: SCS mismatch (%d vs %d)\n",
              cell->nr_cellid,
              get_ssb_scs(cell),
              nc->subcarrierSpacing);
        return false;
      }
      if (cell->plmn.mcc != nc->plmn.mcc || cell->plmn.mnc != nc->plmn.mnc
          || cell->plmn.mnc_digit_length != nc->plmn.mnc_digit_length) {
        LOG_W(NR_RRC,
              "Cell %ld in neighbour config: PLMN mismatch (%03d.%0*d vs %03d.%0*d)\n",
              cell->nr_cellid,
              cell->plmn.mcc,
              cell->plmn.mnc_digit_length,
              cell->plmn.mnc,
              nc->plmn.mcc,
              nc->plmn.mnc_digit_length,
              nc->plmn.mnc);
        return false;
      }
      LOG_D(NR_RRC, "Cell %ld is neighbor of cell %ld\n", cell->nr_cellid, neighbour_config->nr_cell_id);
    }
  }
  return true;
}

258 259
void rrc_gNB_process_f1_setup_req(f1ap_setup_req_t *req, sctp_assoc_t assoc_id)
{
Robert Schmidt's avatar
Robert Schmidt committed
260
  AssertFatal(assoc_id != 0, "illegal assoc_id == 0: should be -1 (monolithic) or >0 (split)\n");
261 262 263 264
  gNB_RRC_INST *rrc = RC.nrrrc[0];
  DevAssert(rrc);

  LOG_I(NR_RRC, "Received F1 Setup Request from gNB_DU %lu (%s) on assoc_id %d\n", req->gNB_DU_id, req->gNB_DU_name, assoc_id);
Guido Casati's avatar
Guido Casati committed
265 266 267
  // pre-fill F1 Setup Failure message
  f1ap_setup_failure_t fail = {.transaction_id = F1AP_get_next_transaction_identifier(0, 0)};

268 269 270
  // check:
  // - it is one cell
  // - PLMN and Cell ID matches
Robert Schmidt's avatar
Robert Schmidt committed
271
  // - no previous DU with the same ID
272 273 274
  // else reject
  if (req->num_cells_available != 1) {
    LOG_E(NR_RRC, "can only handle on DU cell, but gNB_DU %ld has %d\n", req->gNB_DU_id, req->num_cells_available);
Guido Casati's avatar
Guido Casati committed
275
    fail.cause = F1AP_CauseRadioNetwork_gNB_CU_Cell_Capacity_Exceeded;
Robert Schmidt's avatar
Robert Schmidt committed
276
    rrc->mac_rrc.f1_setup_failure(assoc_id, &fail);
277 278 279 280 281
    return;
  }
  f1ap_served_cell_info_t *cell_info = &req->cell[0].info;
  if (!rrc_gNB_plmn_matches(rrc, cell_info)) {
    LOG_E(NR_RRC,
282
          "PLMN mismatch: CU %03d.%0*d, DU %03d%0*d\n",
283
          rrc->configuration.mcc[0],
284
          rrc->configuration.mnc_digit_length[0],
285 286
          rrc->configuration.mnc[0],
          cell_info->plmn.mcc,
287
          cell_info->plmn.mnc_digit_length,
288
          cell_info->plmn.mnc);
Guido Casati's avatar
Guido Casati committed
289
    fail.cause = F1AP_CauseRadioNetwork_plmn_not_served_by_the_gNB_CU;
Robert Schmidt's avatar
Robert Schmidt committed
290
    rrc->mac_rrc.f1_setup_failure(assoc_id, &fail);
291 292
    return;
  }
Robert Schmidt's avatar
Robert Schmidt committed
293 294 295 296 297 298 299 300
  nr_rrc_du_container_t *it = NULL;
  RB_FOREACH(it, rrc_du_tree, &rrc->dus) {
    if (it->setup_req->gNB_DU_id == req->gNB_DU_id) {
      LOG_E(NR_RRC,
            "gNB-DU ID: existing DU %s on assoc_id %d already has ID %ld, rejecting requesting gNB-DU\n",
            it->setup_req->gNB_DU_name,
            it->assoc_id,
            it->setup_req->gNB_DU_id);
Guido Casati's avatar
Guido Casati committed
301
      fail.cause = F1AP_CauseMisc_unspecified;
Robert Schmidt's avatar
Robert Schmidt committed
302 303 304
      rrc->mac_rrc.f1_setup_failure(assoc_id, &fail);
      return;
    }
305 306 307 308 309 310 311 312 313 314 315 316 317
    // note: we assume that each DU contains only one cell; otherwise, we would
    // need to check every cell in the requesting DU to any existing cell.
    const f1ap_served_cell_info_t *exist_info = &it->setup_req->cell[0].info;
    const f1ap_served_cell_info_t *new_info = &req->cell[0].info;
    if (exist_info->nr_cellid == new_info->nr_cellid || exist_info->nr_pci == new_info->nr_pci) {
      LOG_E(NR_RRC,
            "existing DU %s on assoc_id %d already has cellID %ld/physCellId %d, rejecting requesting gNB-DU with cellID %ld/physCellId %d\n",
            it->setup_req->gNB_DU_name,
            it->assoc_id,
            exist_info->nr_cellid,
            exist_info->nr_pci,
            new_info->nr_cellid,
            new_info->nr_pci);
Guido Casati's avatar
Guido Casati committed
318
      fail.cause = F1AP_CauseMisc_unspecified;
319 320 321
      rrc->mac_rrc.f1_setup_failure(assoc_id, &fail);
      return;
    }
Robert Schmidt's avatar
Robert Schmidt committed
322 323
  }

324 325
  // MTC is mandatory, but some DUs don't send it in the F1 Setup Request, so
  // "tolerate" this behavior, despite it being mandatory
326 327 328
  NR_MeasurementTimingConfiguration_t *mtc =
      extract_mtc(cell_info->measurement_timing_config, cell_info->measurement_timing_config_len);

329 330 331 332 333 334 335 336 337
  if (rrc->neighbour_cell_configuration
      && !valid_du_in_neighbour_configs(rrc->neighbour_cell_configuration, cell_info, ssb_arfcn_mtc(mtc))) {
    LOG_E(NR_RRC, "problem with DU %ld in neighbor configuration, rejecting DU\n", req->gNB_DU_id);
    f1ap_setup_failure_t fail = {.cause = F1AP_CauseMisc_unspecified};
    rrc->mac_rrc.f1_setup_failure(assoc_id, &fail);
    ASN_STRUCT_FREE(asn_DEF_NR_MeasurementTimingConfiguration, mtc);
    return;
  }

338
  const f1ap_gnb_du_system_info_t *sys_info = req->cell[0].sys_info;
339
  NR_MIB_t *mib = NULL;
340
  NR_SIB1_t *sib1 = NULL;
341 342

  if (sys_info != NULL && sys_info->mib != NULL && !(sys_info->sib1 == NULL && get_softmodem_params()->sa)) {
343 344
    if (!extract_sys_info(sys_info, &mib, &sib1)) {
      LOG_W(RRC, "rejecting DU ID %ld\n", req->gNB_DU_id);
Guido Casati's avatar
Guido Casati committed
345
      fail.cause = F1AP_CauseProtocol_semantic_error;
Robert Schmidt's avatar
Robert Schmidt committed
346
      rrc->mac_rrc.f1_setup_failure(assoc_id, &fail);
347
      ASN_STRUCT_FREE(asn_DEF_NR_MeasurementTimingConfiguration, mtc);
348 349
      return;
    }
350
  }
351
  LOG_I(RRC, "Accepting DU %ld (%s), sending F1 Setup Response\n", req->gNB_DU_id, req->gNB_DU_name);
352
  LOG_I(RRC, "DU uses RRC version %u.%u.%u\n", req->rrc_ver[0], req->rrc_ver[1], req->rrc_ver[2]);
353 354

  // we accept the DU
Robert Schmidt's avatar
Robert Schmidt committed
355
  nr_rrc_du_container_t *du = calloc(1, sizeof(*du));
francescomani's avatar
francescomani committed
356
  AssertFatal(du, "out of memory\n");
Robert Schmidt's avatar
Robert Schmidt committed
357
  du->assoc_id = assoc_id;
358 359 360 361

  /* ITTI will free the setup request message via free(). So the memory
   * "inside" of the message will remain, but the "outside" container no, so
   * allocate memory and copy it in */
362
  du->setup_req = calloc(1,sizeof(*du->setup_req));
francescomani's avatar
francescomani committed
363
  AssertFatal(du->setup_req, "out of memory\n");
364 365
  // Copy F1AP message
  *du->setup_req = cp_f1ap_setup_request(req);
366 367 368
  // MIB can be null and configured later via DU Configuration Update
  du->mib = mib;
  du->sib1 = sib1;
369
  du->mtc = mtc;
Robert Schmidt's avatar
Robert Schmidt committed
370 371
  RB_INSERT(rrc_du_tree, &rrc->dus, du);
  rrc->num_dus++;
372 373 374 375 376 377

  served_cells_to_activate_t cell = {
      .plmn = cell_info->plmn,
      .nr_cellid = cell_info->nr_cellid,
      .nrpci = cell_info->nr_pci,
      .num_SI = 0,
378
  };
379

380 381 382
  if (du->mib != NULL && du->sib1 != NULL)
    label_intra_frequency_neighbours(rrc, du, cell_info);

383 384 385
  f1ap_setup_resp_t resp = {.transaction_id = req->transaction_id,
                            .num_cells_to_activate = 1,
                            .cells_to_activate[0] = cell};
386 387
  // free F1AP message after use
  free_f1ap_setup_request(req);
388 389
  int num = read_version(TO_STRING(NR_RRC_VERSION), &resp.rrc_ver[0], &resp.rrc_ver[1], &resp.rrc_ver[2]);
  AssertFatal(num == 3, "could not read RRC version string %s\n", TO_STRING(NR_RRC_VERSION));
390 391
  if (rrc->node_name != NULL)
    resp.gNB_CU_name = strdup(rrc->node_name);
Robert Schmidt's avatar
Robert Schmidt committed
392
  rrc->mac_rrc.f1_setup_response(assoc_id, &resp);
393 394
}

395 396 397 398 399 400 401 402 403
static int invalidate_du_connections(gNB_RRC_INST *rrc, sctp_assoc_t assoc_id)
{
  int count = 0;
  rrc_gNB_ue_context_t *ue_context_p = NULL;
  RB_FOREACH(ue_context_p, rrc_nr_ue_tree_s, &rrc->rrc_ue_head) {
    uint32_t ue_id = ue_context_p->ue_context.rrc_ue_id;
    f1_ue_data_t ue_data = cu_get_f1_ue_data(ue_id);
    if (ue_data.du_assoc_id == assoc_id) {
      /* this UE belongs to the DU that disconnected, set du_assoc_id to 0,
404
       * meaning DU is offline, then trigger release request */
405
      cu_remove_f1_ue_data(ue_id);
406 407 408 409 410 411
      ue_data.du_assoc_id = 0;
      cu_add_f1_ue_data(ue_id, &ue_data);
      rrc_gNB_send_NGAP_UE_CONTEXT_RELEASE_REQ(0,
                                               ue_context_p,
                                               NGAP_CAUSE_RADIO_NETWORK,
                                               NGAP_CAUSE_RADIO_NETWORK_RADIO_CONNECTION_WITH_UE_LOST);
412 413 414 415 416 417
      count++;
    }
  }
  return count;
}

418 419 420 421 422 423 424 425 426 427 428 429 430
static void update_cell_info(nr_rrc_du_container_t *du, const f1ap_served_cell_info_t *new_ci)
{
  DevAssert(du != NULL);
  DevAssert(new_ci != NULL);

  AssertFatal(du->setup_req->num_cells_available == 1, "expected 1 cell for DU, but has %d\n", du->setup_req->num_cells_available);
  f1ap_served_cell_info_t *ci = &du->setup_req->cell[0].info;

  ci->nr_cellid = new_ci->nr_cellid;
  ci->nr_pci = new_ci->nr_pci;
  if (new_ci->tac != NULL)
    *ci->tac = *new_ci->tac;
  ci->num_ssi = new_ci->num_ssi;
431 432
  for (int s = 0; s < new_ci->num_ssi; ++s)
    ci->nssai[s] = new_ci->nssai[s];
433 434 435 436 437
  ci->mode = new_ci->mode;
  if (ci->mode == F1AP_MODE_TDD)
    ci->tdd = new_ci->tdd;
  else
    ci->fdd = new_ci->fdd;
438 439 440 441 442 443 444 445 446 447

  NR_MeasurementTimingConfiguration_t *new_mtc =
      extract_mtc(new_ci->measurement_timing_config, new_ci->measurement_timing_config_len);
  if (new_mtc != NULL) {
    ASN_STRUCT_FREE(asn_DEF_NR_MeasurementTimingConfiguration, du->mtc);
    du->mtc = new_mtc;
  } else {
    LOG_E(RRC, "error decoding MeasurementTimingConfiguration during cell update, ignoring new config\n");
    ASN_STRUCT_FREE(asn_DEF_NR_MeasurementTimingConfiguration, new_mtc);
  }
448 449 450 451 452 453 454 455 456 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 491 492 493 494 495 496
}

void rrc_gNB_process_f1_du_configuration_update(f1ap_gnb_du_configuration_update_t *conf_up, sctp_assoc_t assoc_id)
{
  AssertFatal(assoc_id != 0, "illegal assoc_id == 0: should be -1 (monolithic) or >0 (split)\n");
  gNB_RRC_INST *rrc = RC.nrrrc[0];
  DevAssert(rrc);

  // check:
  // - it is one cell
  // - PLMN and Cell ID matches
  // - no previous DU with the same ID
  // else reject

  nr_rrc_du_container_t *du = get_du_by_assoc_id(rrc, assoc_id);
  AssertError(du != NULL, return, "no DU found for assoc_id %d\n", assoc_id);

  const f1ap_served_cell_info_t *info = &du->setup_req->cell[0].info;
  if (conf_up->num_cells_to_add > 0) {
    // Here we check if the number of cell limit is respectet, otherwise send failure
    LOG_W(RRC, "du_configuration_update->cells_to_add_list is not supported yet");
  }

  if (conf_up->num_cells_to_modify > 0) {
    // here the old nrcgi is used to find the cell information, if it exist then we modify consequently otherwise we fail
    AssertFatal(conf_up->num_cells_to_modify == 1, "cannot handle more than one cell!\n");

    if (info->nr_cellid != conf_up->cell_to_modify[0].old_nr_cellid) {
      LOG_W(RRC, "no cell with ID %ld found, ignoring gNB-DU configuration update\n", conf_up->cell_to_modify[0].old_nr_cellid);
      return;
    }

    // verify the new plmn of the cell
    if (!rrc_gNB_plmn_matches(rrc, &conf_up->cell_to_modify[0].info)) {
      LOG_W(RRC, "PLMN does not match, ignoring gNB-DU configuration update\n");
      return;
    }

    update_cell_info(du, &conf_up->cell_to_modify[0].info);

    const f1ap_gnb_du_system_info_t *sys_info = conf_up->cell_to_modify[0].sys_info;

    if (sys_info != NULL && sys_info->mib != NULL && !(sys_info->sib1 == NULL && get_softmodem_params()->sa)) {
      // MIB is mandatory, so will be overwritten. SIB1 is optional, so will
      // only be overwritten if present in sys_info
      ASN_STRUCT_FREE(asn_DEF_NR_MIB, du->mib);
      if (sys_info->sib1 != NULL)
        ASN_STRUCT_FREE(asn_DEF_NR_SIB1, du->sib1);

497
      NR_MIB_t *mib = NULL;
498 499 500 501
      if (!extract_sys_info(sys_info, &mib, &du->sib1)) {
        LOG_W(RRC, "cannot update sys_info for DU %ld\n", du->setup_req->gNB_DU_id);
      } else {
        DevAssert(mib != NULL);
502
        du->mib = mib;
503 504 505
        LOG_I(RRC, "update system information of DU %ld\n", du->setup_req->gNB_DU_id);
      }
    }
506 507 508 509
    if (du->mib != NULL && du->sib1 != NULL) {
      const f1ap_served_cell_info_t *cell_info = &du->setup_req->cell[0].info;
      label_intra_frequency_neighbours(rrc, du, cell_info);
    }
510 511 512 513 514 515 516 517 518 519
  }

  if (conf_up->num_cells_to_delete > 0) {
    // delete the cell and send cell to desactive IE in the response.
    LOG_W(RRC, "du_configuration_update->cells_to_delete_list is not supported yet");
  }

  /* Send DU Configuration Acknowledgement */
  f1ap_gnb_du_configuration_update_acknowledge_t ack = {.transaction_id = conf_up->transaction_id};
  rrc->mac_rrc.gnb_du_configuration_update_acknowledge(assoc_id, &ack);
520 521
  /* free F1AP message after use */
  free_f1ap_du_configuration_update(conf_up);
522 523
}

524 525
void rrc_CU_process_f1_lost_connection(gNB_RRC_INST *rrc, f1ap_lost_connection_t *lc, sctp_assoc_t assoc_id)
{
Robert Schmidt's avatar
Robert Schmidt committed
526
  AssertFatal(assoc_id != 0, "illegal assoc_id == 0: should be -1 (monolithic) or >0 (split)\n");
527 528
  (void) lc; // unused for the moment

Robert Schmidt's avatar
Robert Schmidt committed
529 530 531 532 533 534 535
  nr_rrc_du_container_t e = {.assoc_id = assoc_id};
  nr_rrc_du_container_t *du = RB_FIND(rrc_du_tree, &rrc->dus, &e);
  if (du == NULL) {
    LOG_W(NR_RRC, "no DU connected or not found for assoc_id %d: F1 Setup Failed?\n", assoc_id);
    return;
  }

536 537 538 539
  f1ap_setup_req_t *req = du->setup_req;
  LOG_I(RRC, "releasing DU ID %ld (%s) on assoc_id %d\n", req->gNB_DU_id, req->gNB_DU_name, assoc_id);
  ASN_STRUCT_FREE(asn_DEF_NR_MIB, du->mib);
  ASN_STRUCT_FREE(asn_DEF_NR_SIB1, du->sib1);
540
  ASN_STRUCT_FREE(asn_DEF_NR_MeasurementTimingConfiguration, du->mtc);
541
  /* TODO: free setup request */
Robert Schmidt's avatar
Robert Schmidt committed
542 543 544
  nr_rrc_du_container_t *removed = RB_REMOVE(rrc_du_tree, &rrc->dus, du);
  DevAssert(removed != NULL);
  rrc->num_dus--;
545

546 547 548 549
  int num = invalidate_du_connections(rrc, assoc_id);
  if (num > 0) {
    LOG_I(NR_RRC, "%d UEs lost through DU disconnect\n", num);
  }
550 551
}

552 553
nr_rrc_du_container_t *get_du_for_ue(gNB_RRC_INST *rrc, uint32_t ue_id)
{
Robert Schmidt's avatar
Robert Schmidt committed
554 555
  f1_ue_data_t ue_data = cu_get_f1_ue_data(ue_id);
  return get_du_by_assoc_id(rrc, ue_data.du_assoc_id);
556 557 558 559
}

nr_rrc_du_container_t *get_du_by_assoc_id(gNB_RRC_INST *rrc, sctp_assoc_t assoc_id)
{
Robert Schmidt's avatar
Robert Schmidt committed
560 561
  nr_rrc_du_container_t e = {.assoc_id = assoc_id};
  return RB_FIND(rrc_du_tree, &rrc->dus, &e);
562
}
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

void dump_du_info(const gNB_RRC_INST *rrc, FILE *f)
{
  fprintf(f, "%ld connected DUs \n", rrc->num_dus);
  int i = 1;
  nr_rrc_du_container_t *du = NULL;
  /* cast is necessary to eliminate warning "discards ‘const’ qualifier" */
  RB_FOREACH(du, rrc_du_tree, &((gNB_RRC_INST *)rrc)->dus) {
    const f1ap_setup_req_t *sr = du->setup_req;
    fprintf(f, "[%d] DU ID %ld (%s) ", i++, sr->gNB_DU_id, sr->gNB_DU_name);
    if (du->assoc_id == -1) {
      fprintf(f, "integrated DU-CU");
    } else {
      fprintf(f, "assoc_id %d", du->assoc_id);
    }
    const f1ap_served_cell_info_t *info = &sr->cell[0].info;
579
    fprintf(f, ": nrCellID %ld, PCI %d, SSB ARFCN %d\n", info->nr_cellid, info->nr_pci, get_ssb_arfcn(du));
Robert Schmidt's avatar
Robert Schmidt committed
580 581 582 583 584 585 586 587 588 589 590 591 592

    if (info->mode == F1AP_MODE_TDD) {
      const f1ap_nr_frequency_info_t *fi = &info->tdd.freqinfo;
      const f1ap_transmission_bandwidth_t *tb = &info->tdd.tbw;
      fprintf(f, "    TDD: band %d ARFCN %d SCS %d (kHz) PRB %d\n", fi->band, fi->arfcn, 15 * (1 << tb->scs), tb->nrb);
    } else {
      const f1ap_nr_frequency_info_t *dfi = &info->fdd.dl_freqinfo;
      const f1ap_transmission_bandwidth_t *dtb = &info->fdd.dl_tbw;
      fprintf(f, "    FDD: DL band %d ARFCN %d SCS %d (kHz) PRB %d\n", dfi->band, dfi->arfcn, 15 * (1 << dtb->scs), dtb->nrb);
      const f1ap_nr_frequency_info_t *ufi = &info->fdd.ul_freqinfo;
      const f1ap_transmission_bandwidth_t *utb = &info->fdd.ul_tbw;
      fprintf(f, "         UL band %d ARFCN %d SCS %d (kHz) PRB %d\n", ufi->band, ufi->arfcn, 15 * (1 << utb->scs), utb->nrb);
    }
593 594
  }
}
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622

nr_rrc_du_container_t *find_target_du(gNB_RRC_INST *rrc, sctp_assoc_t source_assoc_id)
{
  nr_rrc_du_container_t *target_du = NULL;
  nr_rrc_du_container_t *it = NULL;
  bool next_du = false;
  RB_FOREACH (it, rrc_du_tree, &rrc->dus) {
    if (next_du == false && source_assoc_id != it->assoc_id) {
      continue;
    } else if (source_assoc_id == it->assoc_id) {
      next_du = true;
    } else {
      target_du = it;
      break;
    }
  }
  if (target_du == NULL) {
    RB_FOREACH (it, rrc_du_tree, &rrc->dus) {
      if (source_assoc_id == it->assoc_id) {
        continue;
      } else {
        target_du = it;
        break;
      }
    }
  }
  return target_du;
}