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 XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
67 uint32_t nAllocBufferSize;
69 char pool_name[RTE_MEMPOOL_NAMESIZE];
71 snprintf(pool_name, RTE_MEMPOOL_NAMESIZE, "ru_%d_cc_%d_idx_%d",
72 pXranCc->nXranPort, pXranCc->nIndex, pXranCc->nBufferPoolIndex);
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;
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);
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);
88 pXranCc->p_bufferPool[pXranCc->nBufferPoolIndex] = rte_pktmbuf_pool_create(pool_name, nNumberOfBuffers,
89 MBUF_CACHE, 0, nAllocBufferSize, rte_socket_id());
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));
97 pXranCc->bufferPoolElmSz[pXranCc->nBufferPoolIndex] = nBufferSize;
98 pXranCc->bufferPoolNumElm[pXranCc->nBufferPoolIndex] = nNumberOfBuffers;
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]);
104 *pPoolIndex = pXranCc->nBufferPoolIndex++;
110 xran_bm_allocate_buffer(void * pHandle, uint32_t nPoolIndex, void **ppData, void **ppCtrl)
112 XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
116 struct rte_mbuf * mb = rte_pktmbuf_alloc(pXranCc->p_bufferPool[nPoolIndex]);
119 char * start = rte_pktmbuf_append(mb, pXranCc->bufferPoolElmSz[nPoolIndex]);
120 char * ethhdr = rte_pktmbuf_prepend(mb, sizeof(struct rte_ether_hdr));
123 char * iq_offset = rte_pktmbuf_mtod(mb, char * );
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);
130 if (0) /* if compression */
131 iq_offset += sizeof (struct data_section_compression_hdr);
133 *ppData = (void *)iq_offset;
134 *ppCtrl = (void *)mb;
136 print_err("[nPoolIndex %d] start ethhdr failed \n", nPoolIndex );
140 print_err("[nPoolIndex %d] mb alloc failed \n", nPoolIndex );
144 if (*ppData == NULL){
145 print_err("[nPoolIndex %d] rte_pktmbuf_append for %d failed \n", nPoolIndex, pXranCc->bufferPoolElmSz[nPoolIndex]);
153 xran_bm_free_buffer(void * pHandle, void *pData, void *pCtrl)
155 XranSectorHandleInfo* pXranCc = (XranSectorHandleInfo*) pHandle;
158 rte_pktmbuf_free(pCtrl);
164 xran_malloc(size_t buf_len)
166 return rte_malloc("External buffer", buf_len, RTE_CACHE_LINE_SIZE);
170 xran_free(void *addr)
172 return rte_free(addr);
176 xran_mm_destroy (void * pHandle)
178 if(xran_get_if_state() == XRAN_RUNNING) {
179 print_err("Please STOP first !!");
183 /* functionality is not yet implemented */