Update to odulow per maintenance bronze
[o-du/phy.git] / fhi_lib / lib / src / xran_common.c
index 67079e2..c4cb3fb 100644 (file)
 *
 *******************************************************************************/
 
-#ifndef _XRAN_COMMON_
-#define _XRAN_COMMON_
-
 /**
- * @brief XRAN layer common functionality for both lls-CU and RU as well as C-plane and
+ * @brief XRAN layer common functionality for both O-DU and O-RU as well as C-plane and
  *    U-plane
  * @file xran_common.c
  * @ingroup group_source_xran
 #include "ethdi.h"
 #include "xran_pkt.h"
 #include "xran_pkt_up.h"
-#include "xran_cp_api.h"
 #include "xran_up_api.h"
+#include "xran_lib_mlog_tasks_id.h"
+
 #include "../src/xran_printf.h"
+#include <rte_mbuf.h>
+#include "xran_mlog_lnx.h"
 
-#ifndef MLOG_ENABLED
-#include "mlog_lnx_xRAN.h"
-#else
-#include "mlog_lnx.h"
-#endif
+static struct timespec sleeptime = {.tv_nsec = 1E3 }; /* 1 us */
 
-#define MBUFS_CNT 256
+#define MBUFS_CNT 16
+
+extern long interval_us;
 
 extern int xran_process_rx_sym(void *arg,
+                        struct rte_mbuf *mbuf,
                         void *iq_data_start,
                         uint16_t size,
                         uint8_t CC_ID,
@@ -57,16 +56,94 @@ extern int xran_process_rx_sym(void *arg,
                         uint8_t frame_id,
                         uint8_t subframe_id,
                         uint8_t slot_id,
-                        uint8_t symb_id);
+                        uint8_t symb_id,
+                        uint16_t num_prbu,
+                        uint16_t start_prbu,
+                        uint16_t sym_inc,
+                        uint16_t rb,
+                        uint16_t sect_id,
+                        uint32_t *mb_free);
 
 
+extern int xran_process_prach_sym(void *arg,
+                        struct rte_mbuf *mbuf,
+                        void *iq_data_start,
+                        uint16_t size,
+                        uint8_t CC_ID,
+                        uint8_t Ant_ID,
+                        uint8_t frame_id,
+                        uint8_t subframe_id,
+                        uint8_t slot_id,
+                        uint8_t symb_id,
+                        uint16_t num_prbu,
+                        uint16_t start_prbu,
+                        uint16_t sym_inc,
+                        uint16_t rb,
+                        uint16_t sect_id,
+                        uint32_t *mb_free);
+
+extern int32_t xran_process_srs_sym(void *arg,
+                        struct rte_mbuf *mbuf,
+                        void *iq_data_start,
+                        uint16_t size,
+                        uint8_t CC_ID,
+                        uint8_t Ant_ID,
+                        uint8_t frame_id,
+                        uint8_t subframe_id,
+                        uint8_t slot_id,
+                        uint8_t symb_id,
+                        uint16_t num_prbu,
+                        uint16_t start_prbu,
+                        uint16_t sym_inc,
+                        uint16_t rb,
+                        uint16_t sect_id,
+                        uint32_t *mb_free);
+
+extern int32_t xran_pkt_validate(void *arg,
+                        struct rte_mbuf *mbuf,
+                        void *iq_data_start,
+                        uint16_t size,
+                        uint8_t CC_ID,
+                        uint8_t Ant_ID,
+                        uint8_t frame_id,
+                        uint8_t subframe_id,
+                        uint8_t slot_id,
+                        uint8_t symb_id,
+                        struct ecpri_seq_id *seq_id,
+                        uint16_t num_prbu,
+                        uint16_t start_prbu,
+                        uint16_t sym_inc,
+                        uint16_t rb,
+                        uint16_t sect_id);
+
+
+struct cb_elem_entry *xran_create_cb(XranSymCallbackFn cb_fn, void *cb_data)
+{
+        struct cb_elem_entry * cb_elm = (struct cb_elem_entry *)malloc(sizeof(struct cb_elem_entry));
+        if(cb_elm){
+            cb_elm->pSymCallback    = cb_fn;
+            cb_elm->pSymCallbackTag = cb_data;
+        }
+
+        return cb_elm;
+}
+
+int xran_destroy_cb(struct cb_elem_entry * cb_elm)
+{
+    if(cb_elm)
+        free(cb_elm);
+    return 0;
+}
+
 int process_mbuf(struct rte_mbuf *pkt)
 {
     void *iq_samp_buf;
     struct ecpri_seq_id seq;
     static int symbol_total_bytes = 0;
     int num_bytes = 0;
-    struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
+    struct xran_device_ctx * p_x_ctx = xran_dev_get_ctx();
+    struct xran_common_counters *pCnt = &p_x_ctx->fh_counters;
+
     uint8_t CC_ID = 0;
     uint8_t Ant_ID = 0;
     uint8_t frame_id = 0;
@@ -74,6 +151,25 @@ int process_mbuf(struct rte_mbuf *pkt)
     uint8_t slot_id = 0;
     uint8_t symb_id = 0;
 
+    uint16_t num_prbu;
+    uint16_t start_prbu;
+    uint16_t sym_inc;
+    uint16_t rb;
+    uint16_t sect_id;
+
+    uint8_t compMeth = 0;
+    uint8_t iqWidth = 0;
+
+    void *pHandle = NULL;
+    int ret = MBUF_FREE;
+    uint32_t mb_free = 0;
+    int32_t valid_res = 0;
+    int expect_comp  = (p_x_ctx->fh_cfg.ru_conf.compMeth != XRAN_COMPMETHOD_NONE);
+
+
+    if(p_x_ctx->xran2phy_mem_ready == 0)
+        return MBUF_FREE;
+
     num_bytes = xran_extract_iq_samples(pkt,
                                         &iq_samp_buf,
                                         &CC_ID,
@@ -82,30 +178,157 @@ int process_mbuf(struct rte_mbuf *pkt)
                                         &subframe_id,
                                         &slot_id,
                                         &symb_id,
-                                        &seq);
-    if (num_bytes <= 0)
-        return -1;
+                                        &seq,
+                                        &num_prbu,
+                                        &start_prbu,
+                                        &sym_inc,
+                                        &rb,
+                                        &sect_id,
+                                        expect_comp,
+                                        &compMeth,
+                                        &iqWidth);
+    if (num_bytes <= 0){
+        print_err("num_bytes is wrong [%d]\n", num_bytes);
+        return MBUF_FREE;
+    }
 
-    symbol_total_bytes += num_bytes;
-
-    if (seq.e_bit == 1) {
-        print_dbg("Completed receiving symbol %d, size=%d bytes\n",
-            symb_id, symbol_total_bytes);
-
-        if (symbol_total_bytes)
-            xran_process_rx_sym(NULL,
-                            iq_samp_buf,
-                            symbol_total_bytes,
-                            CC_ID,
-                            Ant_ID,
-                            frame_id,
-                            subframe_id,
-                            slot_id,
-                            symb_id);
-        symbol_total_bytes = 0;
+    valid_res = xran_pkt_validate(NULL,
+                                pkt,
+                                iq_samp_buf,
+                                num_bytes,
+                                CC_ID,
+                                Ant_ID,
+                                frame_id,
+                                subframe_id,
+                                slot_id,
+                                symb_id,
+                                &seq,
+                                num_prbu,
+                                start_prbu,
+                                sym_inc,
+                                rb,
+                                sect_id);
+
+    if(valid_res != 0) {
+        print_dbg("valid_res is wrong [%d] ant %u (%u : %u : %u : %u) seq %u num_bytes %d\n", valid_res, Ant_ID, frame_id, subframe_id, slot_id, symb_id, seq.seq_id, num_bytes);
+        return MBUF_FREE;
     }
 
-    return 0;
+    if (Ant_ID >= p_x_ctx->srs_cfg.eAxC_offset && p_x_ctx->fh_init.srsEnable) {
+        /* SRS packet has ruportid = 2*num_eAxc + ant_id */
+        Ant_ID -= p_x_ctx->srs_cfg.eAxC_offset;
+        symbol_total_bytes += num_bytes;
+
+        if (seq.e_bit == 1) {
+            print_dbg("SRS receiving symbol %d, size=%d bytes\n",
+                symb_id, symbol_total_bytes);
+
+            if (symbol_total_bytes) {
+               int16_t res = xran_process_srs_sym(NULL,
+                                pkt,
+                                iq_samp_buf,
+                                num_bytes,
+                                CC_ID,
+                                Ant_ID,
+                                frame_id,
+                                subframe_id,
+                                slot_id,
+                                symb_id,
+                                num_prbu,
+                                start_prbu,
+                                sym_inc,
+                                rb,
+                                sect_id,
+                                &mb_free);
+
+                if(res == symbol_total_bytes) {
+                    ret = mb_free;
+                } else {
+                    print_err("res != symbol_total_bytes\n");
+                }
+                pCnt->rx_srs_packets++;
+            }
+            symbol_total_bytes = 0;
+        }
+        else {
+            print_dbg("Transport layer fragmentation (eCPRI) is not supported\n");
+        }
+
+    } else if (Ant_ID >= p_x_ctx->PrachCPConfig.eAxC_offset && p_x_ctx->fh_init.prachEnable) {
+        /* PRACH packet has ruportid = num_eAxc + ant_id */
+        Ant_ID -= p_x_ctx->PrachCPConfig.eAxC_offset;
+        symbol_total_bytes += num_bytes;
+        if (seq.e_bit == 1) {
+            print_dbg("Completed receiving PRACH symbol %d, size=%d bytes\n",
+                symb_id, num_bytes);
+
+            if (symbol_total_bytes) {
+                int16_t res =  xran_process_prach_sym(NULL,
+                                                      pkt,
+                                                      iq_samp_buf,
+                                                      num_bytes,
+                                                      CC_ID,
+                                                      Ant_ID,
+                                                      frame_id,
+                                                      subframe_id,
+                                                      slot_id,
+                                                      symb_id,
+                                                      num_prbu,
+                                                      start_prbu,
+                                                      sym_inc,
+                                                      rb,
+                                                      sect_id,
+                                                      &mb_free);
+                if(res == symbol_total_bytes) {
+                    ret = mb_free;
+                } else {
+                    print_err("res != symbol_total_bytes\n");
+                }
+                pCnt->rx_prach_packets[Ant_ID]++;
+            }
+            symbol_total_bytes = 0;
+        } else {
+            print_dbg("Transport layer fragmentation (eCPRI) is not supported\n");
+        }
+
+    } else { /* PUSCH */
+        symbol_total_bytes += num_bytes;
+
+        if (seq.e_bit == 1) {
+            print_dbg("Completed receiving symbol %d, size=%d bytes\n",
+                symb_id, symbol_total_bytes);
+
+            if (symbol_total_bytes) {
+                int res = xran_process_rx_sym(NULL,
+                                pkt,
+                                iq_samp_buf,
+                                symbol_total_bytes,
+                                CC_ID,
+                                Ant_ID,
+                                frame_id,
+                                subframe_id,
+                                slot_id,
+                                symb_id,
+                                num_prbu,
+                                start_prbu,
+                                sym_inc,
+                                rb,
+                                sect_id,
+                                &mb_free);
+                if(res == symbol_total_bytes) {
+                    ret = mb_free;
+                } else {
+                    print_err("res != symbol_total_bytes\n");
+                }
+                pCnt->rx_pusch_packets[Ant_ID]++;
+            }
+            symbol_total_bytes = 0;
+        } else {
+            print_dbg("Transport layer fragmentation (eCPRI) is not supported\n");
+        }
+    }
+
+    return ret;
 }
 
 static int set_iq_bit_width(uint8_t iq_bit_width, struct data_section_compression_hdr *compr_hdr)
@@ -120,9 +343,13 @@ static int set_iq_bit_width(uint8_t iq_bit_width, struct data_section_compressio
 }
 
 /* Send a single 5G symbol over multiple packets */
-int send_symbol_ex(enum xran_pkt_dir direction,
+int32_t prepare_symbol_ex(enum xran_pkt_dir direction,
                 uint16_t section_id,
+                struct rte_mbuf *mb,
                 struct rb_map *data,
+                uint8_t     compMeth,
+                uint8_t     iqWidth,
+                const enum xran_input_byte_order iq_buf_byte_order,
                 uint8_t frame_id,
                 uint8_t subframe_id,
                 uint8_t slot_id,
@@ -131,12 +358,24 @@ int send_symbol_ex(enum xran_pkt_dir direction,
                 int prb_num,
                 uint8_t CC_ID,
                 uint8_t RU_Port_ID,
-                uint8_t seq_id)
+                uint8_t seq_id,
+                uint32_t do_copy)
 {
-    const int n_bytes = prb_num * N_SC_PER_PRB * sizeof(struct rb_map);
-    int sent;
+    int32_t n_bytes = ((prb_num == 0) ? MAX_N_FULLBAND_SC : prb_num) * N_SC_PER_PRB * sizeof(struct rb_map);
+
+    n_bytes  =   ((iqWidth == 0) || (iqWidth == 16)) ? n_bytes : ((3 * iqWidth + 1 ) * prb_num);
+
+    int32_t prep_bytes;
+
+    int16_t nPktSize = sizeof(struct rte_ether_hdr) + sizeof(struct xran_ecpri_hdr) +
+            sizeof(struct radio_app_common_hdr)+ sizeof(struct data_section_hdr) + n_bytes;
     uint32_t off;
-    struct xran_up_pkt_gen_no_compression_params xp = { 0 };
+    struct xran_up_pkt_gen_params xp = { 0 };
+
+    if(compMeth != XRAN_COMPMETHOD_NONE)
+        nPktSize += sizeof(struct data_section_compression_hdr);
+
+    n_bytes = RTE_MIN(n_bytes, XRAN_MAX_MBUF_LEN);
 
     /* radio app header */
     xp.app_params.data_direction = direction;
@@ -144,7 +383,7 @@ int send_symbol_ex(enum xran_pkt_dir direction,
     xp.app_params.filter_id      = 0;
     xp.app_params.frame_id       = frame_id;
     xp.app_params.sf_slot_sym.subframe_id    = subframe_id;
-    xp.app_params.sf_slot_sym.slot_id        = slot_id;
+    xp.app_params.sf_slot_sym.slot_id        = xran_slotid_convert(slot_id, 0);
     xp.app_params.sf_slot_sym.symb_id        = symbol_no;
 
     /* convert to network byte order */
@@ -155,28 +394,115 @@ int send_symbol_ex(enum xran_pkt_dir direction,
     xp.sec_hdr.fields.start_prbu = (uint8_t)prb_start;
     xp.sec_hdr.fields.sym_inc    = 0;
     xp.sec_hdr.fields.rb         = 0;
+#ifdef FCN_ADAPT
+    xp.sec_hdr.udCompHdr         = 0;
+    xp.sec_hdr.reserved          = 0;
+#endif
+
+    /* compression */
+    xp.compr_hdr_param.ud_comp_hdr.ud_comp_meth = compMeth;
+    xp.compr_hdr_param.ud_comp_hdr.ud_iq_width  = iqWidth;
+    xp.compr_hdr_param.rsrvd                    = 0;
 
     /* network byte order */
     xp.sec_hdr.fields.all_bits  = rte_cpu_to_be_32(xp.sec_hdr.fields.all_bits);
 
-    struct rte_mbuf *mb = xran_ethdi_mbuf_alloc();
-
     if (mb == NULL){
         MLogPrint(NULL);
         errx(1, "out of mbufs after %d packets", 1);
     }
 
-    sent = xran_prepare_iq_symbol_portion_no_comp(mb,
+    prep_bytes = xran_prepare_iq_symbol_portion(mb,
                                                   data,
+                                                  iq_buf_byte_order,
                                                   n_bytes,
                                                   &xp,
                                                   CC_ID,
                                                   RU_Port_ID,
-                                                  seq_id);
-    if (sent <= 0)
+                                                  seq_id,
+                                                  do_copy);
+    if (prep_bytes <= 0)
         errx(1, "failed preparing symbol");
 
-    xran_ethdi_mbuf_send(mb, ETHER_TYPE_ECPRI);
+    rte_pktmbuf_pkt_len(mb)  = nPktSize;
+    rte_pktmbuf_data_len(mb) = nPktSize;
+
+#ifdef DEBUG
+    printf("Symbol %2d prep_bytes (%d packets, %d bytes)\n", symbol_no, i, n_bytes);
+#endif
+
+    return prep_bytes;
+}
+
+/* Send a single 5G symbol over multiple packets */
+int send_symbol_ex(enum xran_pkt_dir direction,
+                uint16_t section_id,
+                struct rte_mbuf *mb,
+                struct rb_map *data,
+                const enum xran_input_byte_order iq_buf_byte_order,
+                uint8_t frame_id,
+                uint8_t subframe_id,
+                uint8_t slot_id,
+                uint8_t symbol_no,
+                int prb_start,
+                int prb_num,
+                uint8_t CC_ID,
+                uint8_t RU_Port_ID,
+                uint8_t seq_id)
+{
+    uint32_t do_copy = 0;
+    int32_t n_bytes = ((prb_num == 0) ? MAX_N_FULLBAND_SC : prb_num) * N_SC_PER_PRB * sizeof(struct rb_map);
+    struct xran_device_ctx *p_x_ctx = xran_dev_get_ctx();
+    struct xran_common_counters *pCnt = &p_x_ctx->fh_counters;
+
+
+    if (mb == NULL){
+        char * pChar = NULL;
+        mb = xran_ethdi_mbuf_alloc(); /* will be freede by ETH */
+        if(mb ==  NULL){
+            MLogPrint(NULL);
+            errx(1, "out of mbufs after %d packets", 1);
+        }
+        pChar = rte_pktmbuf_append(mb, sizeof(struct xran_ecpri_hdr)+ sizeof(struct radio_app_common_hdr)+ sizeof(struct data_section_hdr) + n_bytes);
+        if(pChar == NULL){
+                MLogPrint(NULL);
+                errx(1, "incorrect mbuf size %d packets", 1);
+        }
+        pChar = rte_pktmbuf_prepend(mb, sizeof(struct rte_ether_hdr));
+        if(pChar == NULL){
+                MLogPrint(NULL);
+                errx(1, "incorrect mbuf size %d packets", 1);
+        }
+        do_copy = 1; /* new mbuf hence copy of IQs  */
+    }else {
+        rte_pktmbuf_refcnt_update(mb, 1); /* make sure eth won't free our mbuf */
+    }
+
+    int32_t sent = prepare_symbol_ex(direction,
+                         section_id,
+                         mb,
+                         data,
+                         0,
+                         16,
+                         iq_buf_byte_order,
+                         frame_id,
+                         subframe_id,
+                         slot_id,
+                         symbol_no,
+                         prb_start,
+                         prb_num,
+                         CC_ID,
+                         RU_Port_ID,
+                         seq_id,
+                         do_copy);
+
+    if(sent){
+        pCnt->tx_counter++;
+        pCnt->tx_bytes_counter += rte_pktmbuf_pkt_len(mb);
+        p_x_ctx->send_upmbuf2ring(mb, ETHER_TYPE_ECPRI, xran_map_ecpriPcid_to_vf(direction, CC_ID, RU_Port_ID));
+    } else {
+
+    }
 
 #ifdef DEBUG
     printf("Symbol %2d sent (%d packets, %d bytes)\n", symbol_no, i, n_bytes);
@@ -185,82 +511,105 @@ int send_symbol_ex(enum xran_pkt_dir direction,
     return sent;
 }
 
-int send_cpmsg_dlul(void *pHandle, enum xran_pkt_dir dir,
-                uint8_t frame_id, uint8_t subframe_id, uint8_t slot_id,
-                uint8_t startsym, uint8_t numsym, int prb_num,
-                uint16_t beam_id,
-                uint8_t cc_id, uint8_t ru_port_id,
-                uint8_t seq_id)
+int send_cpmsg(void *pHandle, struct rte_mbuf *mbuf,struct xran_cp_gen_params *params,
+                struct xran_section_gen_info *sect_geninfo, uint8_t cc_id, uint8_t ru_port_id, uint8_t seq_id)
+{
+    int ret = 0, nsection, i;
+    uint8_t subframe_id = params->hdr.subframeId;
+    uint8_t slot_id = params->hdr.slotId;
+    uint8_t dir = params->dir;
+    struct xran_device_ctx *p_x_ctx = xran_dev_get_ctx();
+    struct xran_common_counters *pCnt = &p_x_ctx->fh_counters;
+
+    nsection = params->numSections;
+
+    /* add in the ethernet header */
+    struct rte_ether_hdr *const h = (void *)rte_pktmbuf_prepend(mbuf, sizeof(*h));
+
+    pCnt->tx_counter++;
+    pCnt->tx_bytes_counter += rte_pktmbuf_pkt_len(mbuf);
+    p_x_ctx->send_cpmbuf2ring(mbuf, ETHER_TYPE_ECPRI, xran_map_ecpriRtcid_to_vf(dir, cc_id, ru_port_id));
+    for(i=0; i<nsection; i++)
+        xran_cp_add_section_info(pHandle, dir, cc_id, ru_port_id,
+                (slot_id + subframe_id*SLOTNUM_PER_SUBFRAME)%XRAN_MAX_SECTIONDB_CTX,
+                &sect_geninfo[i].info);
+
+    return (ret);
+}
+
+int generate_cpmsg_dlul(void *pHandle, struct xran_cp_gen_params *params, struct xran_section_gen_info *sect_geninfo, struct rte_mbuf *mbuf,
+    enum xran_pkt_dir dir, uint8_t frame_id, uint8_t subframe_id, uint8_t slot_id,
+    uint8_t startsym, uint8_t numsym, uint16_t prb_start, uint16_t prb_num,int16_t iq_buffer_offset, int16_t iq_buffer_len,
+    uint16_t beam_id, uint8_t cc_id, uint8_t ru_port_id, uint8_t comp_method, uint8_t iqWidth,  uint8_t seq_id, uint8_t symInc)
 {
-  struct xran_cp_gen_params params;
-  struct xran_section_gen_info sect_geninfo[XRAN_MAX_NUM_SECTIONS];
-  struct rte_mbuf *mbuf;
-  int ret, nsection, i;
-
-
-    params.dir                  = dir;
-    params.sectionType          = XRAN_CP_SECTIONTYPE_1;     // Most DL/UL Radio Channels
-    params.hdr.filterIdx        = XRAN_FILTERINDEX_STANDARD;
-    params.hdr.frameId          = frame_id;
-    params.hdr.subframeId       = subframe_id;
-    params.hdr.slotId           = slot_id;
-    params.hdr.startSymId       = startsym;                 // start Symbol ID
-    params.hdr.iqWidth          = xran_get_conf_iqwidth(pHandle);
-    params.hdr.compMeth         = xran_get_conf_compmethod(pHandle);
+    int ret = 0, nsection, loc_sym;
+
+
+    params->dir                  = dir;
+    params->sectionType          = XRAN_CP_SECTIONTYPE_1;        // Most DL/UL Radio Channels
+    params->hdr.filterIdx        = XRAN_FILTERINDEX_STANDARD;
+    params->hdr.frameId          = frame_id;
+    params->hdr.subframeId       = subframe_id;
+    params->hdr.slotId           = slot_id;
+    params->hdr.startSymId       = startsym;                     // start Symbol ID
+    params->hdr.iqWidth          = iqWidth;
+    params->hdr.compMeth         = comp_method;
 
     nsection = 0;
-    sect_geninfo[nsection].info.type        = params.sectionType;
+    sect_geninfo[nsection].info.type        = params->sectionType;       // for database
+    sect_geninfo[nsection].info.startSymId  = params->hdr.startSymId;    // for database
+    sect_geninfo[nsection].info.iqWidth     = params->hdr.iqWidth;       // for database
+    sect_geninfo[nsection].info.compMeth    = params->hdr.compMeth;      // for database
     sect_geninfo[nsection].info.id          = xran_alloc_sectionid(pHandle, dir, cc_id, ru_port_id, slot_id);
     sect_geninfo[nsection].info.rb          = XRAN_RBIND_EVERY;
-    sect_geninfo[nsection].info.symInc      = XRAN_SYMBOLNUMBER_NOTINC;
-    sect_geninfo[nsection].info.startPrbc   = 0;
-    sect_geninfo[nsection].info.numPrbc     = NUM_OF_PRB_IN_FULL_BAND,
+    sect_geninfo[nsection].info.symInc      = symInc;
+    sect_geninfo[nsection].info.startPrbc   = prb_start;
+    sect_geninfo[nsection].info.numPrbc     = prb_num;
     sect_geninfo[nsection].info.numSymbol   = numsym;
     sect_geninfo[nsection].info.reMask      = 0xfff;
     sect_geninfo[nsection].info.beamId      = beam_id;
 
-    sect_geninfo[nsection].info.ef          = 0;      // no extension
+    for (loc_sym = 0; loc_sym < XRAN_NUM_OF_SYMBOL_PER_SLOT; loc_sym++) {
+        sect_geninfo[0].info.sec_desc[loc_sym].iq_buffer_offset = iq_buffer_offset;
+        sect_geninfo[0].info.sec_desc[loc_sym].iq_buffer_len    = iq_buffer_len;
+    }
+
+    sect_geninfo[nsection].info.ef          = 0;
     sect_geninfo[nsection].exDataSize       = 0;
-    sect_geninfo[nsection].exData           = NULL;
+//    sect_geninfo[nsection].exData           = NULL;
     nsection++;
 
-    params.numSections          = nsection;
-    params.sections             = sect_geninfo;
+    params->numSections          = nsection;
+    params->sections             = sect_geninfo;
 
-    mbuf = xran_ethdi_mbuf_alloc();
     if(unlikely(mbuf == NULL)) {
         print_err("Alloc fail!\n");
         return (-1);
-        }
+    }
 
-    ret = xran_prepare_ctrl_pkt(mbuf, &params, cc_id, ru_port_id, seq_id);
-    if(ret < 0) {
+    ret = xran_prepare_ctrl_pkt(mbuf, params, cc_id, ru_port_id, seq_id);
+    if(ret < 0){
         print_err("Fail to build control plane packet - [%d:%d:%d] dir=%d\n",
                     frame_id, subframe_id, slot_id, dir);
-        }
-    else {
-        xran_ethdi_mbuf_send_cp(mbuf, ETHER_TYPE_ECPRI);
-        for(i=0; i<nsection; i++)
-            xran_cp_add_section_info(pHandle,
-                    dir, cc_id, ru_port_id, subframe_id, slot_id,
-                    &sect_geninfo[i].info);
-        }
+        rte_pktmbuf_free(mbuf);
+    }
 
     return (ret);
 }
 
-int send_cpmsg_prach(void *pHandle,
+int generate_cpmsg_prach(void *pHandle, struct xran_cp_gen_params *params, struct xran_section_gen_info *sect_geninfo, struct rte_mbuf *mbuf, struct xran_device_ctx *pxran_lib_ctx,
                 uint8_t frame_id, uint8_t subframe_id, uint8_t slot_id,
-                uint16_t beam_id, uint8_t cc_id, uint8_t prach_port_id,
-                uint8_t seq_id)
+                uint16_t beam_id, uint8_t cc_id, uint8_t prach_port_id, uint8_t seq_id)
 {
-    struct xran_cp_gen_params params;
-    struct xran_section_gen_info sect_geninfo[8];
-    struct rte_mbuf *mbuf;
-    int i, nsection, ret;
-    struct xran_lib_ctx *pxran_lib_ctx = xran_lib_get_ctx();
-    xRANPrachCPConfigStruct *pPrachCPConfig = &(pxran_lib_ctx->PrachCPConfig);
+    int nsection, ret;
+    struct xran_prach_cp_config  *pPrachCPConfig = &(pxran_lib_ctx->PrachCPConfig);
+    uint16_t timeOffset;
+    uint16_t nNumerology = pxran_lib_ctx->fh_cfg.frame_conf.nNumerology;
 
+    if(unlikely(mbuf == NULL)) {
+        print_err("Alloc fail!\n");
+        return (-1);
+    }
 #if 0
     printf("%d:%d:%d:%d - filter=%d, startSym=%d[%d:%d], numSym=%d, occasions=%d, freqOff=%d\n",
                 frame_id, subframe_id, slot_id, prach_port_id,
@@ -272,62 +621,60 @@ int send_cpmsg_prach(void *pHandle,
                 pPrachCPConfig->occassionsInPrachSlot,
                 pPrachCPConfig->freqOffset);
 #endif
-
-    params.dir                  = XRAN_DIR_UL;
-    params.sectionType          = XRAN_CP_SECTIONTYPE_3;
-    params.hdr.filterIdx        = pPrachCPConfig->filterIdx;
-    params.hdr.frameId          = frame_id;
-    params.hdr.subframeId       = subframe_id;
-    params.hdr.slotId           = slot_id;
-    params.hdr.startSymId       = pPrachCPConfig->startSymId;
-    params.hdr.iqWidth          = xran_get_conf_iqwidth(pHandle);
-    params.hdr.compMeth         = xran_get_conf_compmethod(pHandle);
+    timeOffset = pPrachCPConfig->timeOffset; //this is the CP value per 38.211 tab 6.3.3.1-1&2
+    timeOffset = timeOffset >> nNumerology; //original number is Tc, convert to Ts based on mu
+    if (pPrachCPConfig->startSymId > 0)
+    {
+        timeOffset += (pPrachCPConfig->startSymId * 2048) >> nNumerology;
+        if ((slot_id == 0) || (slot_id == (SLOTNUM_PER_SUBFRAME >> 1)))
+            timeOffset += 16;
+    }
+    params->dir                  = XRAN_DIR_UL;
+    params->sectionType          = XRAN_CP_SECTIONTYPE_3;
+    params->hdr.filterIdx        = pPrachCPConfig->filterIdx;
+    params->hdr.frameId          = frame_id;
+    params->hdr.subframeId       = subframe_id;
+    params->hdr.slotId           = slot_id;
+    params->hdr.startSymId       = pPrachCPConfig->startSymId;
+    params->hdr.iqWidth          = xran_get_conf_iqwidth(pHandle);
+    params->hdr.compMeth         = xran_get_conf_compmethod(pHandle);
         /* use timeOffset field for the CP length value for prach sequence */
-    params.hdr.timeOffset       = pPrachCPConfig->timeOffset;
-    params.hdr.fftSize          = xran_get_conf_fftsize(pHandle);
-    params.hdr.scs              = xran_get_conf_prach_scs(pHandle);
-    params.hdr.cpLength         = 0;
+    params->hdr.timeOffset       = timeOffset;
+    params->hdr.fftSize          = xran_get_conf_fftsize(pHandle);
+    params->hdr.scs              = xran_get_conf_prach_scs(pHandle);
+    params->hdr.cpLength         = 0;
 
     nsection = 0;
-    sect_geninfo[nsection].info.type      = params.sectionType;
-    sect_geninfo[nsection].info.id        = xran_alloc_sectionid(pHandle, XRAN_DIR_UL, cc_id, prach_port_id, slot_id);
-    sect_geninfo[nsection].info.rb        = XRAN_RBIND_EVERY;
-    sect_geninfo[nsection].info.symInc    = XRAN_SYMBOLNUMBER_NOTINC;
-    sect_geninfo[nsection].info.startPrbc = pPrachCPConfig->startPrbc;
-    sect_geninfo[nsection].info.numPrbc   = pPrachCPConfig->numPrbc,
-    sect_geninfo[nsection].info.numSymbol = pPrachCPConfig->numSymbol*pPrachCPConfig->occassionsInPrachSlot;
-    sect_geninfo[nsection].info.reMask    = 0xfff;
-    sect_geninfo[nsection].info.beamId    = beam_id;
-    sect_geninfo[nsection].info.freqOffset= pPrachCPConfig->freqOffset;
-
-    sect_geninfo[nsection].info.ef        = 0;      // no extension
-    sect_geninfo[nsection].exDataSize     = 0;
-    sect_geninfo[nsection].exData         = NULL;
-    nsection++;
+    sect_geninfo[nsection].info.type        = params->sectionType;       // for database
+    sect_geninfo[nsection].info.startSymId  = params->hdr.startSymId;    // for database
+    sect_geninfo[nsection].info.iqWidth     = params->hdr.iqWidth;       // for database
+    sect_geninfo[nsection].info.compMeth    = params->hdr.compMeth;      // for database
+    sect_geninfo[nsection].info.id          = xran_alloc_sectionid(pHandle, XRAN_DIR_UL, cc_id, prach_port_id, slot_id);
+    sect_geninfo[nsection].info.rb          = XRAN_RBIND_EVERY;
+    sect_geninfo[nsection].info.symInc      = XRAN_SYMBOLNUMBER_NOTINC;
+    sect_geninfo[nsection].info.startPrbc   = pPrachCPConfig->startPrbc;
+    sect_geninfo[nsection].info.numPrbc     = pPrachCPConfig->numPrbc,
+    sect_geninfo[nsection].info.numSymbol   = pPrachCPConfig->numSymbol*pPrachCPConfig->occassionsInPrachSlot;
+    sect_geninfo[nsection].info.reMask      = 0xfff;
+    sect_geninfo[nsection].info.beamId      = beam_id;
+    sect_geninfo[nsection].info.freqOffset  = pPrachCPConfig->freqOffset;
 
-    params.numSections          = nsection;
-    params.sections             = sect_geninfo;
+    pxran_lib_ctx->prach_last_symbol[cc_id] = sect_geninfo[nsection].info.startSymId + sect_geninfo[nsection].info.numSymbol - 1;
 
-    mbuf = xran_ethdi_mbuf_alloc();
-    if(unlikely(mbuf == NULL)) {
-        print_err("Alloc fail!\n");
-        return (-1);
-        }
+    sect_geninfo[nsection].info.ef          = 0;
+    sect_geninfo[nsection].exDataSize       = 0;
+//    sect_geninfo[nsection].exData           = NULL;
+    nsection++;
 
-    ret = xran_prepare_ctrl_pkt(mbuf, &params, cc_id, prach_port_id, seq_id);
-    if(ret < 0) {
-        print_err("Fail to build prach control packet - [%d:%d:%d]\n", frame_id, subframe_id, slot_id);
-        return (ret);
-        }
-    else {
-        xran_ethdi_mbuf_send_cp(mbuf, ETHER_TYPE_ECPRI);
-        for(i=0; i < nsection; i++)
-            xran_cp_add_section_info(pHandle,
-                    XRAN_DIR_UL, cc_id, prach_port_id, subframe_id, slot_id,
-                    &sect_geninfo[i].info);
-        }
+    params->numSections          = nsection;
+    params->sections             = sect_geninfo;
 
-    return (ret);
+    ret = xran_prepare_ctrl_pkt(mbuf, params, cc_id, prach_port_id, seq_id);
+    if(ret < 0){
+        print_err("Fail to build prach control packet - [%d:%d:%d]\n", frame_id, subframe_id, slot_id);
+        rte_pktmbuf_free(mbuf);
+    }
+    return ret;
 }
 
 
@@ -338,60 +685,95 @@ int process_ring(struct rte_ring *r)
     struct rte_mbuf *mbufs[MBUFS_CNT];
     int i;
     uint32_t remaining;
+    uint64_t t1;
     const uint16_t dequeued = rte_ring_dequeue_burst(r, (void **)mbufs,
         RTE_DIM(mbufs), &remaining);
 
     if (!dequeued)
         return 0;
+
+    t1 = MLogTick();
     for (i = 0; i < dequeued; ++i) {
         if (xran_ethdi_filter_packet(mbufs[i], 0) == MBUF_FREE)
             rte_pktmbuf_free(mbufs[i]);
     }
+    MLogTask(PID_PROCESS_UP_PKT, t1, MLogTick());
 
     return remaining;
 }
 
-int ring_processing_thread(void *args)
+int32_t ring_processing_func(void)
 {
-    struct timespec tv = {0};
-    int64_t prev_nsec = 0;
-    uint8_t is_timer_set = 0;
     struct xran_ethdi_ctx *const ctx = xran_ethdi_get_ctx();
+    struct xran_device_ctx *const pxran_lib_ctx = xran_dev_get_ctx();
+    int16_t retPoll = 0;
+    int32_t i;
+    uint64_t t1, t2;
+
+    rte_timer_manage();
+
+    if (pxran_lib_ctx->bbdev_dec) {
+        t1 = MLogTick();
+        retPoll = pxran_lib_ctx->bbdev_dec();
+        if (retPoll != -1)
+        {
+            t2 = MLogTick();
+            MLogTask(PID_XRAN_BBDEV_UL_POLL + retPoll, t1, t2);
+        }
+    }
+
+    if (pxran_lib_ctx->bbdev_enc) {
+        t1 = MLogTick();
+        retPoll = pxran_lib_ctx->bbdev_enc();
+        if (retPoll != -1)
+        {
+            t2 = MLogTick();
+            MLogTask(PID_XRAN_BBDEV_DL_POLL + retPoll, t1, t2);
+        }
+    }
+
+    /* UP first */
+
+    for (i = 0; i < ctx->io_cfg.num_vfs && i < (XRAN_VF_MAX - 1); i = i+2){
+        if (process_ring(ctx->rx_ring[i]))
+            return 0;
+
+        /* CP next */
+        if(ctx->io_cfg.id == O_RU) /* process CP only on O-RU */
+            if (process_ring(ctx->rx_ring[i+1]))
+                return 0;
+    }
+
+    if (XRAN_STOPPED == xran_if_current_state)
+        return -1;
+
+    return 0;
+}
+
+int ring_processing_thread(void *args)
+{
     struct sched_param sched_param;
+    struct xran_device_ctx *const p_xran_dev_ctx = xran_dev_get_ctx();
     int res = 0;
 
+    memset(&sched_param, 0, sizeof(struct sched_param));
+
     printf("%s [CPU %2d] [PID: %6d]\n", __FUNCTION__,  rte_lcore_id(), getpid());
     sched_param.sched_priority = XRAN_THREAD_DEFAULT_PRIO;
-    if ((res = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched_param)))
-    {
+    if ((res = pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched_param))){
         printf("priority is not changed: coreId = %d, result1 = %d\n",rte_lcore_id(), res);
     }
-    for (;;) {
-        if (!is_timer_set) {
-            if (clock_gettime(CLOCK_REALTIME, &tv) != 0)
-                err(1, "gettimeofday() failed");
-            if (tv.tv_nsec % 125000 < prev_nsec % 125000) { /* crossed an 125ms boundary */
-                rte_timer_manage();     /* timers only run on IO core */
-                is_timer_set = 1;
-            }
-            prev_nsec = tv.tv_nsec;
-        } else {
-            rte_timer_manage();
-        }
 
-        /* UP first */
-        if (process_ring(ctx->rx_ring[ETHDI_UP_VF]))
-            continue;
-        /* CP next */
-        if (process_ring(ctx->rx_ring[ETHDI_CP_VF]))
-            continue;
-
-        if (XRAN_STOPPED == xran_if_current_state)
+    for (;;){
+        if(ring_processing_func() != 0)
             break;
+
+        /* work around for some kernel */
+        if(p_xran_dev_ctx->fh_init.io_cfg.io_sleep)
+            nanosleep(&sleeptime,NULL);
     }
 
     puts("Pkt processing thread finished.");
     return 0;
 }
 
-#endif /* _XRAN_COMMON_ */