1 /******************************************************************************
3 * Copyright (c) 2020 Intel.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 *******************************************************************************/
20 * @brief XRAN memory management
21 * @file xran_mem_mgr.c
22 * @ingroup group_source_xran
23 * @author Intel Corporation
32 #include <sys/queue.h>
38 #include <immintrin.h>
40 #include <rte_common.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>
51 #include "xran_mem_mgr.h"
53 #include "xran_printf.h"
56 xran_mm_init (void * pHandle, uint64_t nMemorySize,
57 uint32_t nMemorySegmentSize)
59 /* we use mbuf from dpdk memory */
64 xran_bm_init (void * pHandle, uint32_t * pPoolIndex, uint32_t nNumberOfBuffers, uint32_t nBufferSize)
66 //printf("nNumberOfBuffers=%u\n", nNumberOfBuffers);
67 if(nNumberOfBuffers == 280)
68 nNumberOfBuffers = 560;
70 XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
71 uint32_t nAllocBufferSize;
73 char pool_name[RTE_MEMPOOL_NAMESIZE];
75 snprintf(pool_name, RTE_MEMPOOL_NAMESIZE, "ru_%d_cc_%d_idx_%d",
76 pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex);
78 nAllocBufferSize = nBufferSize + sizeof(struct rte_ether_hdr) +
79 sizeof (struct xran_ecpri_hdr) +
80 sizeof (struct radio_app_common_hdr) +
81 sizeof(struct data_section_hdr) + 256;
83 if(nAllocBufferSize >= UINT16_MAX) {
84 rte_panic("nAllocBufferSize is failed [ handle %p %d %d ] [nPoolIndex %d] nNumberOfBuffers %d nBufferSize %d nAllocBufferSize %d\n",
85 pXranCc, pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex, nNumberOfBuffers, nBufferSize, nAllocBufferSize);
89 printf("%s: [ handle %p %d %d ] [nPoolIndex %d] nNumberOfBuffers %d nBufferSize %d socket_id %d\n", pool_name,
90 pXranCc, pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex, nNumberOfBuffers, nBufferSize, rte_socket_id());
92 pXranCc->p_bufferPool[pXranCc->nBufferPoolIndex] = rte_pktmbuf_pool_create(pool_name, nNumberOfBuffers,
93 /*MBUF_CACHE*/0, 0, nAllocBufferSize, rte_socket_id());
96 if(pXranCc->p_bufferPool[pXranCc->nBufferPoolIndex] == NULL){
97 rte_panic("rte_pktmbuf_pool_create failed [poolName=%s, handle %p %d %d ] [nPoolIndex %d] nNumberOfBuffers %d nBufferSize %d errno %s\n",
98 pool_name, pXranCc, pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex, nNumberOfBuffers, nBufferSize, rte_strerror(rte_errno));
101 //printf("press enter (RTE_MEMPOOL_NAMESIZE=%u)\n", RTE_MEMPOOL_NAMESIZE);
103 pXranCc->bufferPoolElmSz[pXranCc->nBufferPoolIndex] = nBufferSize;
104 pXranCc->bufferPoolNumElm[pXranCc->nBufferPoolIndex] = nNumberOfBuffers;
106 printf("CC:[ handle %p ru %d cc_idx %d ] [nPoolIndex %d] mb pool %p \n",
107 pXranCc, pXranCc->nXranPort, pXranCc->nIndex,
108 pXranCc->nBufferPoolIndex, pXranCc->p_bufferPool[pXranCc->nBufferPoolIndex]);
110 *pPoolIndex = pXranCc->nBufferPoolIndex++;
116 xran_bm_allocate_buffer(void * pHandle, uint32_t nPoolIndex, void **ppData, void **ppCtrl)
118 XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
122 struct rte_mbuf * mb = rte_pktmbuf_alloc(pXranCc->p_bufferPool[nPoolIndex]);
125 char * start = rte_pktmbuf_append(mb, pXranCc->bufferPoolElmSz[nPoolIndex]);
126 char * ethhdr = rte_pktmbuf_prepend(mb, sizeof(struct rte_ether_hdr));
129 char * iq_offset = rte_pktmbuf_mtod(mb, char * );
131 iq_offset = iq_offset + sizeof(struct rte_ether_hdr) +
132 sizeof (struct xran_ecpri_hdr) +
133 sizeof (struct radio_app_common_hdr) +
134 sizeof(struct data_section_hdr);
136 if (0) /* if compression */
137 iq_offset += sizeof (struct data_section_compression_hdr);
139 *ppData = (void *)iq_offset;
140 *ppCtrl = (void *)mb;
142 print_err("[nPoolIndex %d] start ethhdr failed \n", nPoolIndex );
146 print_err("[nPoolIndex %d] mb alloc failed \n", nPoolIndex );
150 if (*ppData == NULL){
151 print_err("[nPoolIndex %d] rte_pktmbuf_append for %d failed \n", nPoolIndex, pXranCc->bufferPoolElmSz[nPoolIndex]);
159 xran_bm_allocate_ring(void * pHandle, const char *rng_name_prefix, uint16_t cc_id, uint16_t buff_id, uint16_t ant_id, uint16_t symb_id, void **ppRing)
162 XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
163 uint32_t xran_port_id;
164 char ring_name[32] = "";
165 struct rte_ring *ring = NULL;
169 xran_port_id = pXranCc->nXranPort;
171 snprintf(ring_name, RTE_DIM(ring_name), "%srb%dp%dcc%dant%dsym%d", rng_name_prefix, buff_id, xran_port_id, cc_id, ant_id, symb_id);
172 print_dbg("%s\n", ring_name);
173 r_size = rte_ring_get_memsize(XRAN_MAX_MEM_IF_RING_SIZE);
174 ring = (struct rte_ring *)xran_malloc(r_size);
176 print_err("[%srb%dp%dcc%dant%dsym%d] ring alloc failed \n", rng_name_prefix, buff_id, xran_port_id, cc_id, ant_id, symb_id);
179 ret = rte_ring_init(ring, ring_name, XRAN_MAX_MEM_IF_RING_SIZE, /*RING_F_SC_DEQ*/0);
181 print_err("[%srb%dp%dcc%dant%dsym%d] rte_ring_init failed \n", rng_name_prefix, buff_id, xran_port_id, cc_id, ant_id, symb_id);
186 *ppRing = (void *)ring;
188 print_err("[%srb%dp%dcc%dant%dsym%d] ring alloc failed \n", rng_name_prefix, buff_id, xran_port_id, cc_id, ant_id, symb_id);
192 print_err("pHandle failed \n");
200 xran_bm_free_buffer(void * pHandle, void *pData, void *pCtrl)
202 //XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
205 rte_pktmbuf_free(pCtrl);
211 xran_malloc(size_t buf_len)
213 return rte_malloc("External buffer", buf_len, RTE_CACHE_LINE_SIZE);
217 xran_free(void *addr)
219 return rte_free(addr);
223 xran_mm_destroy (void * pHandle)
225 if(xran_get_if_state() == XRAN_RUNNING) {
226 print_err("Please STOP first !!");
230 /* functionality is not yet implemented */