From f8dc571bd99544815cfb5378ba79e1d831f548bf Mon Sep 17 00:00:00 2001
From: Wu Jing <wu.jing@cn.fujitsu.com>
Date: Thu, 13 Jun 2019 11:34:08 +0900
Subject: [PATCH] fix assert issue when uci array is full if doing
 Attach/Detach

---
 openair1/PHY/LTE_TRANSPORT/transport_eNB.h |  2 ++
 openair1/PHY/LTE_TRANSPORT/uci_tools.c     |  2 +-
 openair1/PHY/defs_eNB.h                    |  2 +-
 openair1/SCHED/fapi_l1.c                   |  3 +++
 openair1/SCHED/phy_procedures_lte_eNb.c    | 22 +++++++++++-----------
 openair2/RRC/LTE/rrc_eNB.c                 | 22 +++++++++++++---------
 targets/COMMON/openairinterface5g_limits.h |  6 ++++++
 7 files changed, 37 insertions(+), 22 deletions(-)

diff --git a/openair1/PHY/LTE_TRANSPORT/transport_eNB.h b/openair1/PHY/LTE_TRANSPORT/transport_eNB.h
index 6c1f6747886..9e8685732f9 100644
--- a/openair1/PHY/LTE_TRANSPORT/transport_eNB.h
+++ b/openair1/PHY/LTE_TRANSPORT/transport_eNB.h
@@ -329,6 +329,8 @@ typedef struct {
   uint8_t     subframe;
   /// corresponding UE RNTI
   uint16_t    rnti;
+  /// UE ID from Layer2
+  uint16_t    ue_id;
   /// Type (SR, HARQ, CQI, HARQ_SR, HARQ_CQI, SR_CQI, HARQ_SR_CQI)
   UCI_type_t  type;
   /// SRS active flag
diff --git a/openair1/PHY/LTE_TRANSPORT/uci_tools.c b/openair1/PHY/LTE_TRANSPORT/uci_tools.c
index 88c652133b7..c301c8510f0 100644
--- a/openair1/PHY/LTE_TRANSPORT/uci_tools.c
+++ b/openair1/PHY/LTE_TRANSPORT/uci_tools.c
@@ -41,7 +41,7 @@ int16_t find_uci(uint16_t rnti, int frame, int subframe, PHY_VARS_eNB *eNB,find_
   uint16_t i;
   int16_t first_free_index=-1;
   AssertFatal(eNB!=NULL,"eNB is null\n");
-  for (i=0; i<NUMBER_OF_UE_MAX; i++) {
+  for (i=0; i<NUMBER_OF_UCI_VARS_MAX; i++) {
     if ((eNB->uci_vars[i].active >0) &&
 	(eNB->uci_vars[i].rnti==rnti) &&
 	(eNB->uci_vars[i].frame==frame) &&
diff --git a/openair1/PHY/defs_eNB.h b/openair1/PHY/defs_eNB.h
index d3157d5e822..31c759ee361 100644
--- a/openair1/PHY/defs_eNB.h
+++ b/openair1/PHY/defs_eNB.h
@@ -946,7 +946,7 @@ typedef struct PHY_VARS_eNB_s {
   LTE_eNB_PRACH        prach_vars_br;
 #endif
   LTE_eNB_COMMON       common_vars;
-  LTE_eNB_UCI          uci_vars[NUMBER_OF_UE_MAX];
+  LTE_eNB_UCI          uci_vars[NUMBER_OF_UCI_VARS_MAX];
   LTE_eNB_SRS          srs_vars[NUMBER_OF_UE_MAX];
   LTE_eNB_PBCH         pbch;
   LTE_eNB_PUSCH       *pusch_vars[NUMBER_OF_UE_MAX];
diff --git a/openair1/SCHED/fapi_l1.c b/openair1/SCHED/fapi_l1.c
index 09bb458c7e7..f39c5b4f138 100644
--- a/openair1/SCHED/fapi_l1.c
+++ b/openair1/SCHED/fapi_l1.c
@@ -587,6 +587,7 @@ void handle_uci_sr_pdu(PHY_VARS_eNB *eNB,
   uci->frame               = frame;
   uci->subframe            = subframe;
   uci->rnti                = ul_config_pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti;
+  uci->ue_id               = find_dlsch(ul_config_pdu->uci_sr_pdu.ue_information.ue_information_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE);
   uci->type                = SR;
   uci->pucch_fmt           = pucch_format1;
   uci->num_antenna_ports   = 1;
@@ -614,6 +615,7 @@ void handle_uci_sr_harq_pdu(PHY_VARS_eNB *eNB,int UE_id,nfapi_ul_config_request_
   uci->frame               = frame;
   uci->subframe            = subframe;
   uci->rnti                = ul_config_pdu->uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti;
+  uci->ue_id               = find_dlsch(ul_config_pdu->uci_sr_harq_pdu.ue_information.ue_information_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE);
   uci->type                = HARQ_SR;
   uci->num_antenna_ports   = 1;
   uci->num_pucch_resources = 1;
@@ -637,6 +639,7 @@ void handle_uci_harq_pdu(PHY_VARS_eNB *eNB,int UE_id,nfapi_ul_config_request_pdu
   uci->frame             = frame;
   uci->subframe          = subframe;
   uci->rnti              = ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti;
+  uci->ue_id             = find_dlsch(ul_config_pdu->uci_harq_pdu.ue_information.ue_information_rel8.rnti,eNB,SEARCH_EXIST_OR_FREE);
   uci->type              = HARQ;
   uci->srs_active        = srs_active;
   uci->num_antenna_ports = ul_config_pdu->uci_harq_pdu.harq_information.harq_information_rel11.num_ant_ports;
diff --git a/openair1/SCHED/phy_procedures_lte_eNb.c b/openair1/SCHED/phy_procedures_lte_eNb.c
index b6f23956b62..18b917430cb 100644
--- a/openair1/SCHED/phy_procedures_lte_eNb.c
+++ b/openair1/SCHED/phy_procedures_lte_eNb.c
@@ -639,7 +639,7 @@ uci_procedures(PHY_VARS_eNB *eNB,
   LTE_eNB_UCI *uci = NULL;
   LTE_DL_FRAME_PARMS *fp = &(eNB->frame_parms);
 
-  for (int i = 0; i < NUMBER_OF_UE_MAX; i++) {
+  for (int i = 0; i < NUMBER_OF_UCI_VARS_MAX; i++) {
     uci = &(eNB->uci_vars[i]);
 
     if ((uci->active == 1) && (uci->frame == frame) && (uci->subframe == subframe)) {
@@ -687,7 +687,7 @@ uci_procedures(PHY_VARS_eNB *eNB,
           int pucch1_thres = (uci->ue_type == 0) ? eNB->pucch1_DTX_threshold : eNB->pucch1_DTX_threshold_emtc[0];
           metric_SR = rx_pucch(eNB,
                                uci->pucch_fmt,
-                               i,
+                               uci->ue_id,
                                uci->n_pucch_1_0_sr[0],
                                0, // n2_pucch
                                uci->srs_active, // shortened format
@@ -729,7 +729,7 @@ uci_procedures(PHY_VARS_eNB *eNB,
                   SR_payload);
             metric[0] = rx_pucch(eNB,
                                  uci->pucch_fmt,
-                                 i,
+                                 uci->ue_id,
                                  uci->n_pucch_1[0][0],
                                  0, //n2_pucch
                                  uci->srs_active, // shortened format
@@ -753,7 +753,7 @@ uci_procedures(PHY_VARS_eNB *eNB,
               SR_payload = 1;
               metric[0]=rx_pucch(eNB,
                                  uci->pucch_fmt,
-                                 i,
+                                 uci->ue_id,
                                  uci->n_pucch_1_0_sr[0],
                                  0, //n2_pucch
                                  uci->srs_active, // shortened format
@@ -783,7 +783,7 @@ uci_procedures(PHY_VARS_eNB *eNB,
 #if 1
             metric[0] = rx_pucch(eNB,
                                  uci->pucch_fmt,
-                                 i,
+                                 uci->ue_id,
                                  uci->n_pucch_1[0][0],
                                  0, //n2_pucch
                                  uci->srs_active, // shortened format
@@ -803,7 +803,7 @@ uci_procedures(PHY_VARS_eNB *eNB,
               SR_payload = 1;
               metric[0] = rx_pucch(eNB,
                                    pucch_format1b,
-                                   i,
+                                   uci->ue_id,
                                    uci->n_pucch_1_0_sr[0],
                                    0, //n2_pucch
                                    uci->srs_active, // shortened format
@@ -1142,11 +1142,11 @@ uci_procedures(PHY_VARS_eNB *eNB,
           if (SR_payload == 1) {
             LOG_D (PHY, "[eNB %d][SR %x] Frame %d subframe %d Got SR for PUSCH, transmitting to MAC\n", eNB->Mod_id, uci->rnti, frame, subframe);
 
-            if (eNB->first_sr[i] == 1) {    // this is the first request for uplink after Connection Setup, so clear HARQ process 0 use for Msg4
-              eNB->first_sr[i] = 0;
-              eNB->dlsch[i][0]->harq_processes[0]->round = 0;
-              eNB->dlsch[i][0]->harq_processes[0]->status = SCH_IDLE;
-              LOG_D (PHY, "[eNB %d][SR %x] Frame %d subframe %d First SR\n", eNB->Mod_id, eNB->ulsch[i]->rnti, frame, subframe);
+            if (eNB->first_sr[uci->ue_id] == 1) {    // this is the first request for uplink after Connection Setup, so clear HARQ process 0 use for Msg4
+              eNB->first_sr[uci->ue_id] = 0;
+              eNB->dlsch[uci->ue_id][0]->harq_processes[0]->round = 0;
+              eNB->dlsch[uci->ue_id][0]->harq_processes[0]->status = SCH_IDLE;
+              LOG_D (PHY, "[eNB %d][SR %x] Frame %d subframe %d First SR\n", eNB->Mod_id, eNB->ulsch[uci->ue_id]->rnti, frame, subframe);
             }
           }
       }
diff --git a/openair2/RRC/LTE/rrc_eNB.c b/openair2/RRC/LTE/rrc_eNB.c
index 60a9f76a14f..42a15037327 100644
--- a/openair2/RRC/LTE/rrc_eNB.c
+++ b/openair2/RRC/LTE/rrc_eNB.c
@@ -1023,28 +1023,32 @@ void release_UE_in_freeList(module_id_t mod_id) {
 
         for (i=0; i<MAX_MOBILES_PER_ENB; i++) {
           ulsch = eNB_PHY->ulsch[i];
-
           if((ulsch != NULL) && (ulsch->rnti == rnti)) {
             void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch);
             LOG_I(RRC, "clean_eNb_ulsch ulsch[%d] UE %x\n", i, rnti);
             clean_eNb_ulsch(ulsch);
           }
 
-          if(eNB_PHY->uci_vars[i].rnti == rnti) {
-            LOG_I(MAC, "clean eNb uci_vars[%d] UE %x \n",i, rnti);
-            memset(&eNB_PHY->uci_vars[i],0,sizeof(LTE_eNB_UCI));
-          }
-        }
-
-        for (i=0; i<MAX_MOBILES_PER_ENB; i++) {
           dlsch = eNB_PHY->dlsch[i][0];
-
           if((dlsch != NULL) && (dlsch->rnti == rnti)) {
             void clean_eNb_dlsch(LTE_eNB_DLSCH_t *dlsch);
             LOG_I(RRC, "clean_eNb_dlsch dlsch[%d] UE %x \n", i, rnti);
             clean_eNb_dlsch(dlsch);
           }
         }
+        ulsch = eNB_PHY->ulsch[i];
+        if((ulsch != NULL) && (ulsch->rnti == rnti)) {
+          void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch);
+          LOG_I(RRC, "clean_eNb_ulsch ulsch[%d] UE %x\n", i, rnti);
+          clean_eNb_ulsch(ulsch);
+        }
+
+        for (i=0; i<NUMBER_OF_UCI_VARS_MAX; i++) {
+          if(eNB_PHY->uci_vars[i].rnti == rnti) {
+            LOG_I(MAC, "clean eNb uci_vars[%d] UE %x \n",i, rnti);
+            memset(&eNB_PHY->uci_vars[i],0,sizeof(LTE_eNB_UCI));
+          }
+        }
 
         if (flexran_agent_get_rrc_xface(mod_id)) {
           flexran_agent_get_rrc_xface(mod_id)->flexran_agent_notify_ue_state_change(
diff --git a/targets/COMMON/openairinterface5g_limits.h b/targets/COMMON/openairinterface5g_limits.h
index 644952ca794..b81fd091471 100644
--- a/targets/COMMON/openairinterface5g_limits.h
+++ b/targets/COMMON/openairinterface5g_limits.h
@@ -12,13 +12,16 @@
 // This problem will be fixed in the future.
 #            ifndef UESIM_EXPANSION
 #                define NUMBER_OF_UE_MAX 16
+#                define NUMBER_OF_UCI_VARS_MAX 56
 #                define NUMBER_OF_CONNECTED_eNB_MAX 3
 #            else
 #                define NUMBER_OF_UE_MAX 256
+#                define NUMBER_OF_UCI_VARS_MAX 256
 #                define NUMBER_OF_CONNECTED_eNB_MAX 1
 #            endif
 #        else
 #                define NUMBER_OF_UE_MAX 256
+#                define NUMBER_OF_UCI_VARS_MAX 256
 #                define NUMBER_OF_CONNECTED_eNB_MAX 1
 #        endif
 #else
@@ -32,13 +35,16 @@ and the other are using MAX_MOBILES_PER_ENB in for-loop.
 */
 #            ifndef UESIM_EXPANSION
 #                define NUMBER_OF_UE_MAX 16
+#                define NUMBER_OF_UCI_VARS_MAX 56
 #                define NUMBER_OF_CONNECTED_eNB_MAX 3
 #            else
 #                define NUMBER_OF_UE_MAX 256
+#                define NUMBER_OF_UCI_VARS_MAX 256
 #                define NUMBER_OF_CONNECTED_eNB_MAX 1
 #            endif
 #        else
 #                define NUMBER_OF_UE_MAX 256
+#                define NUMBER_OF_UCI_VARS_MAX 256
 #                define NUMBER_OF_CONNECTED_eNB_MAX 1
 #        endif
 #        if defined(STANDALONE) && STANDALONE==1
-- 
GitLab