O-RAN E Maintenance Release contribution for ODULOW
[o-du/phy.git] / fhi_lib / lib / src / xran_mem_mgr.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @brief XRAN memory management
21  * @file xran_mem_mgr.c
22  * @ingroup group_source_xran
23  * @author Intel Corporation
24  **/
25
26 #define _GNU_SOURCE
27 #include <sched.h>
28 #include <assert.h>
29 #include <err.h>
30 #include <libgen.h>
31 #include <sys/time.h>
32 #include <sys/queue.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <pthread.h>
37 #include <malloc.h>
38 #include <immintrin.h>
39
40 #include <rte_common.h>
41 #include <rte_eal.h>
42 #include <rte_errno.h>
43 #include <rte_lcore.h>
44 #include <rte_cycles.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_mbuf.h>
48 #include <rte_ring.h>
49
50 #include "ethernet.h"
51 #include "xran_mem_mgr.h"
52 #include "xran_dev.h"
53 #include "xran_printf.h"
54
55 int32_t
56 xran_mm_init (void * pHandle, uint64_t nMemorySize,
57             uint32_t nMemorySegmentSize)
58 {
59     /* we use mbuf from dpdk memory */
60     return 0;
61 }
62
63 int32_t
64 xran_bm_init (void * pHandle, uint32_t * pPoolIndex, uint32_t nNumberOfBuffers, uint32_t nBufferSize)
65 {
66     XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
67     uint32_t nAllocBufferSize;
68
69     char pool_name[RTE_MEMPOOL_NAMESIZE];
70
71     snprintf(pool_name, RTE_MEMPOOL_NAMESIZE, "ru_%d_cc_%d_idx_%d",
72         pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex);
73
74     nAllocBufferSize = nBufferSize + sizeof(struct rte_ether_hdr) +
75         sizeof (struct xran_ecpri_hdr) +
76         sizeof (struct radio_app_common_hdr) +
77         sizeof(struct data_section_hdr) + 256;
78
79     if(nAllocBufferSize >= UINT16_MAX) {
80         rte_panic("nAllocBufferSize is failed [ handle %p %d %d ] [nPoolIndex %d] nNumberOfBuffers %d nBufferSize %d nAllocBufferSize %d\n",
81                     pXranCc, pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex, nNumberOfBuffers, nBufferSize, nAllocBufferSize);
82         return -1;
83     }
84
85     printf("%s: [ handle %p %d %d ] [nPoolIndex %d] nNumberOfBuffers %d nBufferSize %d\n", pool_name,
86                         pXranCc, pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex, nNumberOfBuffers, nBufferSize);
87
88     pXranCc->p_bufferPool[pXranCc->nBufferPoolIndex] = rte_pktmbuf_pool_create(pool_name, nNumberOfBuffers,
89                                                                                MBUF_CACHE, 0, nAllocBufferSize, rte_socket_id());
90
91     if(pXranCc->p_bufferPool[pXranCc->nBufferPoolIndex] == NULL){
92         rte_panic("rte_pktmbuf_pool_create failed [ handle %p %d %d ] [nPoolIndex %d] nNumberOfBuffers %d nBufferSize %d errno %s\n",
93                     pXranCc, pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex, nNumberOfBuffers, nBufferSize, rte_strerror(rte_errno));
94         return -1;
95     }
96
97     pXranCc->bufferPoolElmSz[pXranCc->nBufferPoolIndex]  = nBufferSize;
98     pXranCc->bufferPoolNumElm[pXranCc->nBufferPoolIndex] = nNumberOfBuffers;
99
100     printf("CC:[ handle %p ru %d cc_idx %d ] [nPoolIndex %d] mb pool %p \n",
101                 pXranCc, pXranCc->nXranPort, pXranCc->nIndex,
102                     pXranCc->nBufferPoolIndex,  pXranCc->p_bufferPool[pXranCc->nBufferPoolIndex]);
103
104     *pPoolIndex = pXranCc->nBufferPoolIndex++;
105
106     return 0;
107 }
108
109 int32_t
110 xran_bm_allocate_buffer(void * pHandle, uint32_t nPoolIndex, void **ppData,  void **ppCtrl)
111 {
112     XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
113     *ppData = NULL;
114     *ppCtrl = NULL;
115
116     struct rte_mbuf * mb =  rte_pktmbuf_alloc(pXranCc->p_bufferPool[nPoolIndex]);
117
118     if(mb){
119         char * start     = rte_pktmbuf_append(mb, pXranCc->bufferPoolElmSz[nPoolIndex]);
120         char * ethhdr    = rte_pktmbuf_prepend(mb, sizeof(struct rte_ether_hdr));
121
122         if(start && ethhdr){
123             char * iq_offset = rte_pktmbuf_mtod(mb, char * );
124             /* skip headers */
125             iq_offset = iq_offset + sizeof(struct rte_ether_hdr) +
126                                     sizeof (struct xran_ecpri_hdr) +
127                                     sizeof (struct radio_app_common_hdr) +
128                                     sizeof(struct data_section_hdr);
129
130             if (0) /* if compression */
131                 iq_offset += sizeof (struct data_section_compression_hdr);
132
133             *ppData = (void *)iq_offset;
134             *ppCtrl  = (void *)mb;
135         } else {
136             print_err("[nPoolIndex %d] start ethhdr failed \n", nPoolIndex );
137             return -1;
138         }
139     } else {
140         print_err("[nPoolIndex %d] mb alloc failed \n", nPoolIndex );
141         return -1;
142     }
143
144     if (*ppData ==  NULL){
145         print_err("[nPoolIndex %d] rte_pktmbuf_append for %d failed \n", nPoolIndex, pXranCc->bufferPoolElmSz[nPoolIndex]);
146         return -1;
147     }
148
149     return 0;
150 }
151
152 int32_t
153 xran_bm_free_buffer(void * pHandle, void *pData, void *pCtrl)
154 {
155     XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
156
157     if(pCtrl)
158         rte_pktmbuf_free(pCtrl);
159
160     return 0;
161 }
162
163 void*
164 xran_malloc(size_t buf_len)
165 {
166     return rte_malloc("External buffer", buf_len, RTE_CACHE_LINE_SIZE);
167 }
168
169 void
170 xran_free(void *addr)
171 {
172     return rte_free(addr);
173 }
174
175 int32_t
176 xran_mm_destroy (void * pHandle)
177 {
178     if(xran_get_if_state() == XRAN_RUNNING) {
179         print_err("Please STOP first !!");
180         return (-1);
181         }
182
183     /* functionality is not yet implemented */
184     return 0;
185 }