rrc_gNB_UE_context.c 8.37 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*
 * 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
 */

/*! \file rrc_gNB_UE_context.h
 * \brief rrc procedures for UE context
 * \author Lionel GAUTHIER
 * \date 2015
 * \version 1.0
 * \company Eurecom
 * \email: lionel.gauthier@eurecom.fr
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include "common/utils/LOG/log.h"
#include "rrc_gNB_UE_context.h"


//------------------------------------------------------------------------------
int rrc_gNB_compare_ue_rnti_id(
  struct rrc_gNB_ue_context_s *c1_pP, struct rrc_gNB_ue_context_s *c2_pP)
//------------------------------------------------------------------------------
{
  if (c1_pP->ue_id_rnti > c2_pP->ue_id_rnti) {
    return 1;
  }

  if (c1_pP->ue_id_rnti < c2_pP->ue_id_rnti) {
    return -1;
  }

  return 0;
}

/* Generate the tree management functions */
RB_GENERATE(rrc_nr_ue_tree_s, rrc_gNB_ue_context_s, entries,
            rrc_gNB_compare_ue_rnti_id);



//------------------------------------------------------------------------------
struct rrc_gNB_ue_context_s *
rrc_gNB_allocate_new_UE_context(
  gNB_RRC_INST *rrc_instance_pP
)
//------------------------------------------------------------------------------
{
  struct rrc_gNB_ue_context_s *new_p;
  new_p = (struct rrc_gNB_ue_context_s * )malloc(sizeof(struct rrc_gNB_ue_context_s));

  if (new_p == NULL) {
    LOG_E(RRC, "Cannot allocate new ue context\n");
    return NULL;
  }

  memset(new_p, 0, sizeof(struct rrc_gNB_ue_context_s));
78
  new_p->local_uid = uid_linear_allocator_new(&rrc_instance_pP->uid_allocator);
79 80

  for(int i = 0; i < NB_RB_MAX; i++) {
81
    new_p->ue_context.e_rab[i].xid = -1;
82
    new_p->ue_context.pduSession[i].xid = -1;
83 84 85
    new_p->ue_context.modify_e_rab[i].xid = -1;
  }

rmagueta's avatar
rmagueta committed
86
  LOG_D(NR_RRC,"Returning new UE context at %p\n",new_p);
Raymond Knopp's avatar
Raymond Knopp committed
87
  return(new_p);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}


//------------------------------------------------------------------------------
struct rrc_gNB_ue_context_s *
rrc_gNB_get_ue_context(
  gNB_RRC_INST *rrc_instance_pP,
  rnti_t rntiP)
//------------------------------------------------------------------------------
{
  rrc_gNB_ue_context_t temp;
  memset(&temp, 0, sizeof(struct rrc_gNB_ue_context_s));
  /* gNB ue rrc id = 24 bits wide */
  temp.ue_id_rnti = rntiP;
  struct rrc_gNB_ue_context_s   *ue_context_p = NULL;
  ue_context_p = RB_FIND(rrc_nr_ue_tree_s, &rrc_instance_pP->rrc_ue_head, &temp);

  if ( ue_context_p != NULL) {
    return ue_context_p;
  } else {
    RB_FOREACH(ue_context_p, rrc_nr_ue_tree_s, &(rrc_instance_pP->rrc_ue_head)) {
      if (ue_context_p->ue_context.rnti == rntiP) {
        return ue_context_p;
      }
    }
    return NULL;
  }
}

Francesco Mani's avatar
Francesco Mani committed
117
void rrc_gNB_free_mem_UE_context(
Raymond Knopp's avatar
Raymond Knopp committed
118 119 120 121 122
  const protocol_ctxt_t               *const ctxt_pP,
  struct rrc_gNB_ue_context_s         *const ue_context_pP
)
//-----------------------------------------------------------------------------
{
Francesco Mani's avatar
Francesco Mani committed
123

Raymond Knopp's avatar
Raymond Knopp committed
124
  LOG_T(RRC,
125 126
        PROTOCOL_NR_RRC_CTXT_UE_FMT" Clearing UE context 0x%p (free internal structs)\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
Raymond Knopp's avatar
Raymond Knopp committed
127 128
        ue_context_pP);

129 130
  //ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_LTE_SCellToAddMod_r10, &ue_context_pP->ue_context.sCell_config[0]);
  //ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_LTE_SCellToAddMod_r10, &ue_context_pP->ue_context.sCell_config[1]);
Raymond Knopp's avatar
Raymond Knopp committed
131 132 133

  // empty the internal fields of the UE context here
}
134 135 136

//------------------------------------------------------------------------------
void rrc_gNB_remove_ue_context(
Xue Song's avatar
Xue Song committed
137
  const protocol_ctxt_t       *const ctxt_pP,
138 139 140 141 142
  gNB_RRC_INST                *rrc_instance_pP,
  struct rrc_gNB_ue_context_s *ue_context_pP)
//------------------------------------------------------------------------------
{
  if (rrc_instance_pP == NULL) {
143 144
    LOG_E(RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Bad RRC instance\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
145 146 147 148
    return;
  }

  if (ue_context_pP == NULL) {
149 150
    LOG_E(RRC, PROTOCOL_NR_RRC_CTXT_UE_FMT" Trying to free a NULL UE context\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
151 152 153 154 155
    return;
  }

  RB_REMOVE(rrc_nr_ue_tree_s, &rrc_instance_pP->rrc_ue_head, ue_context_pP);
  rrc_gNB_free_mem_UE_context(ctxt_pP, ue_context_pP);
156
  uid_linear_allocator_free(&rrc_instance_pP->uid_allocator, ue_context_pP->local_uid);
157 158 159
  free(ue_context_pP);
  rrc_instance_pP->Nb_ue --;
  LOG_I(RRC,
160 161
        PROTOCOL_NR_RRC_CTXT_UE_FMT" Removed UE context\n",
        PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
//-----------------------------------------------------------------------------
// return the ue context if there is already an UE with ue_identityP, NULL otherwise
struct rrc_gNB_ue_context_s *
rrc_gNB_ue_context_random_exist(
  gNB_RRC_INST                *rrc_instance_pP,
  const uint64_t               ue_identityP
)
//-----------------------------------------------------------------------------
{
  struct rrc_gNB_ue_context_s        *ue_context_p = NULL;
  RB_FOREACH(ue_context_p, rrc_nr_ue_tree_s, &rrc_instance_pP->rrc_ue_head) {
    if (ue_context_p->ue_context.random_ue_identity == ue_identityP)
      return ue_context_p;
  }
  return NULL;
}
180

Xue Song's avatar
Xue Song committed
181 182 183 184 185 186 187 188 189 190 191
//-----------------------------------------------------------------------------
// return the ue context if there is already an UE with the same S-TMSI, NULL otherwise
struct rrc_gNB_ue_context_s *
rrc_gNB_ue_context_5g_s_tmsi_exist(
    gNB_RRC_INST                *rrc_instance_pP,
    const uint64_t              s_TMSI
)
//-----------------------------------------------------------------------------
{
    struct rrc_gNB_ue_context_s        *ue_context_p = NULL;
    RB_FOREACH(ue_context_p, rrc_nr_ue_tree_s, &rrc_instance_pP->rrc_ue_head) {
192
        LOG_I(NR_RRC,"checking for UE 5G S-TMSI %ld: rnti %d \n",
Xue Song's avatar
Xue Song committed
193 194
              s_TMSI, ue_context_p->ue_context.rnti);

Xue Song's avatar
Xue Song committed
195
        if (ue_context_p->ue_context.ng_5G_S_TMSI_Part1 == s_TMSI) {
Xue Song's avatar
Xue Song committed
196 197 198 199 200 201
            return ue_context_p;
        }
    }
    return NULL;
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
//-----------------------------------------------------------------------------
// return a new ue context structure if ue_identityP, ctxt_pP->rnti not found in collection
struct rrc_gNB_ue_context_s *
rrc_gNB_get_next_free_ue_context(
  const protocol_ctxt_t       *const ctxt_pP,
  gNB_RRC_INST                *rrc_instance_pP,
  const uint64_t               ue_identityP
)
//-----------------------------------------------------------------------------
{
  struct rrc_gNB_ue_context_s        *ue_context_p = NULL;
  ue_context_p = rrc_gNB_get_ue_context(rrc_instance_pP, ctxt_pP->rnti);

  if (ue_context_p == NULL) {
    ue_context_p = rrc_gNB_allocate_new_UE_context(rrc_instance_pP);

    if (ue_context_p == NULL) {
Xue Song's avatar
Xue Song committed
219 220 221
      LOG_E(NR_RRC,
            PROTOCOL_NR_RRC_CTXT_UE_FMT" Cannot create new UE context, no memory\n",
            PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
222 223 224 225 226 227 228
      return NULL;
    }

    ue_context_p->ue_id_rnti                    = ctxt_pP->rnti; // here ue_id_rnti is just a key, may be something else
    ue_context_p->ue_context.rnti               = ctxt_pP->rnti; // yes duplicate, 1 may be removed
    ue_context_p->ue_context.random_ue_identity = ue_identityP;
    RB_INSERT(rrc_nr_ue_tree_s, &rrc_instance_pP->rrc_ue_head, ue_context_p);
Xue Song's avatar
Xue Song committed
229 230 231
    LOG_D(NR_RRC,
          PROTOCOL_NR_RRC_CTXT_UE_FMT" Created new UE context uid %u\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP),
232 233 234
          ue_context_p->local_uid);
    return ue_context_p;
  } else {
Xue Song's avatar
Xue Song committed
235 236 237
    LOG_E(NR_RRC,
          PROTOCOL_NR_RRC_CTXT_UE_FMT" Cannot create new UE context, already exist\n",
          PROTOCOL_NR_RRC_CTXT_UE_ARGS(ctxt_pP));
238 239 240 241 242
    return NULL;
  }

  return(ue_context_p);
}