FD.io VPP  v21.01.1
Vector Packet Processing
pool.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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  Copyright (c) 2001, 2002, 2003, 2004 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #include <vppinfra/pool.h>
39 
40 __clib_export void
41 _pool_init_fixed (void **pool_ptr, u32 elt_size, u32 max_elts)
42 {
43  u8 *mmap_base;
44  u64 vector_size;
45  u64 free_index_size;
46  u64 total_size;
47  u64 page_size;
48  pool_header_t *fh;
49  vec_header_t *vh;
50  u8 *v;
51  u32 *fi;
52  u32 i;
53  u32 set_bits;
54 
55  ASSERT (elt_size);
56  ASSERT (max_elts);
57 
58  vector_size = pool_aligned_header_bytes + (u64) elt_size *max_elts;
59  free_index_size = vec_header_bytes (0) + sizeof (u32) * max_elts;
60 
61  /* Round up to a cache line boundary */
62  vector_size = (vector_size + CLIB_CACHE_LINE_BYTES - 1)
63  & ~(CLIB_CACHE_LINE_BYTES - 1);
64 
65  free_index_size = (free_index_size + CLIB_CACHE_LINE_BYTES - 1)
66  & ~(CLIB_CACHE_LINE_BYTES - 1);
67 
68  total_size = vector_size + free_index_size;
69 
70  /* Round up to an even number of pages */
71  page_size = clib_mem_get_page_size ();
72  total_size = (total_size + page_size - 1) & ~(page_size - 1);
73 
74  /* mmap demand zero memory */
75 
76  mmap_base = mmap (0, total_size, PROT_READ | PROT_WRITE,
77  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
78 
79  if (mmap_base == MAP_FAILED)
80  {
81  clib_unix_warning ("mmap");
82  *pool_ptr = 0;
83  }
84 
85  /* First comes the pool header */
86  fh = (pool_header_t *) mmap_base;
87  /* Find the user vector pointer */
88  v = (u8 *) (mmap_base + pool_aligned_header_bytes);
89  /* Finally, the vector header */
90  vh = _vec_find (v);
91 
92  fh->free_bitmap = 0; /* No free elts (yet) */
93  fh->max_elts = max_elts;
94  fh->mmap_base = mmap_base;
95  fh->mmap_size = total_size;
96 
97  vh->len = max_elts;
98 
99  /* Build the free-index vector */
100  vh = (vec_header_t *) (v + vector_size);
101  vh->len = max_elts;
102  fi = (u32 *) (vh + 1);
103 
104  fh->free_indices = fi;
105 
106  /* Set the entire free bitmap */
107  clib_bitmap_alloc (fh->free_bitmap, max_elts);
108  clib_memset (fh->free_bitmap, 0xff,
109  vec_len (fh->free_bitmap) * sizeof (uword));
110 
111  /* Clear any extraneous set bits */
112  set_bits = vec_len (fh->free_bitmap) * BITS (uword);
113 
114  for (i = max_elts; i < set_bits; i++)
115  fh->free_bitmap = clib_bitmap_set (fh->free_bitmap, i, 0);
116 
117  /* Create the initial free vector */
118  for (i = 0; i < max_elts; i++)
119  fi[i] = (max_elts - 1) - i;
120 
121  *pool_ptr = v;
122 }
123 
124 /*
125  * fd.io coding-style-patch-verification: ON
126  *
127  * Local Variables:
128  * eval: (c-set-style "gnu")
129  * End:
130  */
u64 mmap_size
Definition: pool.h:66
unsigned long u64
Definition: types.h:89
Fixed length block allocator.
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 * free_indices
Vector of free indices.
Definition: pool.h:57
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
Definition: bitmap.h:167
static_always_inline uword clib_mem_get_page_size(void)
Definition: mem.h:468
unsigned char u8
Definition: types.h:56
static uword vec_header_bytes(uword header_bytes)
Definition: vec_bootstrap.h:79
unsigned int u32
Definition: types.h:88
#define clib_bitmap_alloc(v, n_bits)
Allocate a bitmap with the supplied number of bits.
Definition: bitmap.h:109
#define pool_aligned_header_bytes
Align pool header so that pointers are naturally aligned.
Definition: pool.h:71
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
u32 len
Number of elements in vector (NOT its allocated length).
Definition: vec_bootstrap.h:57
#define ASSERT(truth)
vector header structure
Definition: vec_bootstrap.h:55
uword * free_bitmap
Bitmap of indices of free objects.
Definition: pool.h:54
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
#define clib_unix_warning(format, args...)
Definition: error.h:68
u32 max_elts
Maximum size of the pool, in elements.
Definition: pool.h:62
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
#define BITS(x)
Definition: clib.h:66
u8 * mmap_base
mmap segment info: base + length
Definition: pool.h:65