FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
pmalloc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef included_palloc_h
17 #define included_palloc_h
18 #include <vppinfra/format.h>
19 #include <vppinfra/pool.h>
20 
21 #define PMALLOC_LOG2_BLOCK_SZ CLIB_LOG2_CACHE_LINE_BYTES
22 #define PMALLOC_BLOCK_SZ (1 << 6)
23 
24 #define CLIB_PMALLOC_NUMA_LOCAL 0xffffffff
25 
26 typedef struct
27 {
28  u32 start, prev, next;
29  u32 size:31;
30  u32 used:1;
32 
34 
35 typedef struct
36 {
44 
45 typedef struct
46 {
49 #define CLIB_PMALLOC_ARENA_F_SHARED_MEM (1 << 0)
50  int fd;
56  u8 *name;
59 
60 typedef struct
61 {
62  /* flags */
64 #define CLIB_PMALLOC_F_NO_PAGEMAP (1 << 0)
65 
66  /* base VA address */
67  u8 *base;
68 
69  /* default page size - typically 2M */
71 
72  /* maximum number of pages, limited by VA preallocation size */
74 
75  /* vector of pages - each page have own alloc pool and it can be split
76  into subpages (i.e. 2M page build out of 512 4K pages) */
78 
79  /* hash used to find chunk index out of VA, chunk index is defined
80  per page */
82 
83  /* alloc arenas are group of pages which share same attributes
84  shared arenas are represented by FD and they are not grovable
85  private arenas are growable */
87 
88  /* vector of per numa node alloc arena indices
89  each numa node have own default privat alloc arena */
91 
92  /* VA to PA lookup table */
94 
95  /* lookup page size - equals to smalles subpage used */
97 
98  /* last error */
101 
102 
103 int clib_pmalloc_init (clib_pmalloc_main_t * pm, uword base_addr, uword size);
105  uword size, uword align,
106  u32 numa_node);
108  uword align);
109 void clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va);
110 
112  uword size, u32 log2_page_sz,
113  u32 numa_node);
114 
115 void *clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va,
116  uword size, uword align);
117 
120 
123 {
124  return pm->error;
125 }
126 
129 {
130  uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >>
131  pm->def_log2_page_sz;
132 
133  ASSERT (index < vec_len (pm->pages));
134 
135  return index;
136 }
137 
140 {
142  return pm->arenas + pm->pages[index].arena_index;
143 }
144 
147 {
148  uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >>
150  return pointer_to_uword (va) - pm->lookup_table[index];
151 }
152 
153 
154 #endif /* included_palloc_h */
155 
156 /*
157  * fd.io coding-style-patch-verification: ON
158  *
159  * Local Variables:
160  * eval: (c-set-style "gnu")
161  * End:
162  */
clib_pmalloc_arena_t::log2_subpage_sz
u32 log2_subpage_sz
Definition: pmalloc.h:53
format_pmalloc_map
format_function_t format_pmalloc_map
Definition: pmalloc.h:119
clib_pmalloc_arena_t::subpages_per_page
u32 subpages_per_page
Definition: pmalloc.h:54
clib_pmalloc_page_t::first_chunk_index
u32 first_chunk_index
Definition: pmalloc.h:40
pointer_to_uword
static uword pointer_to_uword(const void *p)
Definition: types.h:131
clib_pmalloc_main_t::lookup_log2_page_sz
u32 lookup_log2_page_sz
Definition: pmalloc.h:96
name
string name[64]
Definition: fib.api:25
format_pmalloc
format_function_t format_pmalloc
Definition: pmalloc.h:118
clib_pmalloc_get_pa
static uword clib_pmalloc_get_pa(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.h:146
next
u16 * next
Definition: nat44_ei_out2in.c:718
clib_pmalloc_page_t::index
u32 index
Definition: pmalloc.h:37
clib_pmalloc_page_t::n_free_blocks
u32 n_free_blocks
Definition: pmalloc.h:42
clib_pmalloc_arena_t
Definition: pmalloc.h:45
clib_pmalloc_get_page_index
static u32 clib_pmalloc_get_page_index(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.h:128
clib_pmalloc_arena_t::fd
int fd
Definition: pmalloc.h:50
clib_pmalloc_arena_t::name
u8 * name
Definition: pmalloc.h:56
clib_pmalloc_page_t::chunks
clib_pmalloc_chunk_t * chunks
Definition: pmalloc.h:39
clib_pmalloc_main_t::arenas
clib_pmalloc_arena_t * arenas
Definition: pmalloc.h:86
clib_pmalloc_page_t::n_free_chunks
u32 n_free_chunks
Definition: pmalloc.h:41
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
STATIC_ASSERT_SIZEOF
STATIC_ASSERT_SIZEOF(clib_pmalloc_chunk_t, 16)
clib_pmalloc_free
void clib_pmalloc_free(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.c:517
clib_pmalloc_main_t::default_arena_for_numa_node
u32 * default_arena_for_numa_node
Definition: pmalloc.h:90
clib_pmalloc_arena_t::first_page_index
u32 first_page_index
Definition: pmalloc.h:52
clib_pmalloc_arena_t::n_pages
u32 n_pages
Definition: pmalloc.h:55
clib_pmalloc_create_shared_arena
void * clib_pmalloc_create_shared_arena(clib_pmalloc_main_t *pm, char *name, uword size, u32 log2_page_sz, u32 numa_node)
Definition: pmalloc.c:371
uword
u64 uword
Definition: types.h:112
clib_pmalloc_main_t::lookup_table
uword * lookup_table
Definition: pmalloc.h:93
pool.h
Fixed length block allocator. Pools are built from clib vectors and bitmaps. Use pools when repeatedl...
clib_pmalloc_page_t
Definition: pmalloc.h:35
clib_pmalloc_main_t::pages
clib_pmalloc_page_t * pages
Definition: pmalloc.h:77
format.h
clib_pmalloc_main_t
Definition: pmalloc.h:60
clib_pmalloc_init
int clib_pmalloc_init(clib_pmalloc_main_t *pm, uword base_addr, uword size)
Definition: pmalloc.c:50
clib_pmalloc_last_error
static clib_error_t * clib_pmalloc_last_error(clib_pmalloc_main_t *pm)
Definition: pmalloc.h:122
format_function_t
u8 *() format_function_t(u8 *s, va_list *args)
Definition: format.h:48
size
u32 size
Definition: vhost_user.h:125
index
u32 index
Definition: flow_types.api:221
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
clib_pmalloc_main_t::base
u8 * base
Definition: pmalloc.h:67
clib_pmalloc_main_t::error
clib_error_t * error
Definition: pmalloc.h:99
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
clib_pmalloc_alloc_aligned_on_numa
void * clib_pmalloc_alloc_aligned_on_numa(clib_pmalloc_main_t *pm, uword size, uword align, u32 numa_node)
Definition: pmalloc.c:473
clib_pmalloc_main_t::def_log2_page_sz
clib_mem_page_sz_t def_log2_page_sz
Definition: pmalloc.h:70
clib_pmalloc_arena_t::index
u32 index
Definition: pmalloc.h:47
clib_pmalloc_arena_t::page_indices
u32 * page_indices
Definition: pmalloc.h:57
clib_pmalloc_main_t::max_pages
u32 max_pages
Definition: pmalloc.h:73
clib_mem_page_sz_t
clib_mem_page_sz_t
Definition: mem.h:57
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
clib_pmalloc_main_t::flags
u32 flags
Definition: pmalloc.h:63
clib_pmalloc_page_t::arena_index
u32 arena_index
Definition: pmalloc.h:38
clib_pmalloc_main_t::chunk_index_by_va
uword * chunk_index_by_va
Definition: pmalloc.h:81
clib_pmalloc_chunk_t
Definition: pmalloc.h:26
clib_pmalloc_alloc_from_arena
void * clib_pmalloc_alloc_from_arena(clib_pmalloc_main_t *pm, void *arena_va, uword size, uword align)
Definition: pmalloc.c:487
clib_pmalloc_chunk_t::start
u32 start
Definition: pmalloc.h:28
clib_pmalloc_arena_t::numa_node
u32 numa_node
Definition: pmalloc.h:51
clib_pmalloc_chunk_t::used
u32 used
Definition: pmalloc.h:30
clib_pmalloc_get_arena
static clib_pmalloc_arena_t * clib_pmalloc_get_arena(clib_pmalloc_main_t *pm, void *va)
Definition: pmalloc.h:139
clib_pmalloc_chunk_t::size
u32 size
Definition: pmalloc.h:29
clib_pmalloc_alloc_aligned
void * clib_pmalloc_alloc_aligned(clib_pmalloc_main_t *pm, uword size, uword align)
Definition: pmalloc.c:480
clib_pmalloc_arena_t::flags
u32 flags
Definition: pmalloc.h:48