From 5b0efd5b83cc42b6effa23d01b10bc77c54883c9 Mon Sep 17 00:00:00 2001
From: Cedric Roux <cedric.roux@eurecom.fr>
Date: Thu, 10 Sep 2020 17:35:44 +0200
Subject: [PATCH] NR: fix time keeping in RLC for gNB

RLC has timers. We need to keep track of time. We chose to not use
realtime but 'processing time'. That is we send a tick to RLC for every
subframe when it is processed by the softmodem. Before this commit we
used to increase time in the function mac_rlc_status_ind which is called
for every subframe in the eNB, but not in the gNB (at least not in the
current gNB). So we need another way to keep track of time.

The function gNB_dlsch_ulsch_scheduler is called for every slot. It seems
to be the perfect candidate to send a tick to RLC for every subframe. We
take care of subcarrier spacing to send every subframe (one subframe is
always 1ms), not every slot (slot duration depends on subcarrier spacing).

It may not be the best solution. To be revised if needed.

Note: the UE has not been fixed. Some modification is needed for the UE
too, most probably.
---
 openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c |  5 +++++
 openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c    | 15 +++++++--------
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c
index 548845eae5..1c36c474d8 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler.c
@@ -442,6 +442,11 @@ void gNB_dlsch_ulsch_scheduler(module_id_t module_idP,
 
   pdcp_run(&ctxt);
   //rrc_rx_tx(&ctxt, CC_id);
+  /* send tick to RLC every ms */
+  if ((slot & ((1 << *scc->ssbSubcarrierSpacing) - 1)) == 0) {
+    void nr_rlc_tick(int frame, int subframe);
+    nr_rlc_tick(frame, slot >> *scc->ssbSubcarrierSpacing);
+  }
 
   dlsch_in_slot_bitmap = &RC.nrmac[module_idP]->UE_list.UE_sched_ctrl[UE_id].dlsch_in_slot_bitmap;  // static bitmap signaling which slot in a tdd period contains dlsch
   ulsch_in_slot_bitmap = &RC.nrmac[module_idP]->UE_list.UE_sched_ctrl[UE_id].ulsch_in_slot_bitmap;  // static bitmap signaling which slot in a tdd period contains ulsch
diff --git a/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c b/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c
index 919cf1a46a..5048dd245e 100644
--- a/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c
+++ b/openair2/LAYER2/nr_rlc/nr_rlc_oai_api.c
@@ -158,14 +158,6 @@ mac_rlc_status_resp_t mac_rlc_status_ind(
   mac_rlc_status_resp_t ret;
   nr_rlc_entity_t *rb;
 
-  /* TODO: handle time a bit more properly */
-  if (nr_rlc_current_time_last_frame != frameP ||
-      nr_rlc_current_time_last_subframe != subframeP) {
-    nr_rlc_current_time++;
-    nr_rlc_current_time_last_frame = frameP;
-    nr_rlc_current_time_last_subframe = subframeP;
-  }
-
   nr_rlc_manager_lock(nr_rlc_ue_manager);
   ue = nr_rlc_manager_get_ue(nr_rlc_ue_manager, rntiP);
 
@@ -921,3 +913,10 @@ rlc_op_status_t rrc_rlc_remove_ue (const protocol_ctxt_t* const x)
 
   return RLC_OP_STATUS_OK;
 }
+
+void nr_rlc_tick(int frame, int subframe)
+{
+  if (frame != nr_rlc_current_time_last_frame ||
+      subframe != nr_rlc_current_time_last_subframe)
+    nr_rlc_current_time++;
+}
-- 
2.26.2