FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
test_vec.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 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 #ifndef included_test_vec_h
39 #define included_test_vec_h
40 
41 
42 #include <vppinfra/clib.h>
43 #include <vppinfra/mem.h>
44 #include <vppinfra/format.h>
45 #include <vppinfra/error.h>
46 
47 
48 extern uword g_verbose;
49 extern u32 g_seed;
50 
52 format_u32_binary (u8 * s, va_list * va)
53 {
54  u32 val = va_arg (*va, u32);
55  word i = 0;
56 
57  for (i = BITS (val) - 1; i >= 0; i--)
58  {
59  if (val & (1 << i))
60  s = format (s, "1");
61  else
62  s = format (s, "0");
63  }
64 
65  return s;
66 }
67 
68 #define VERBOSE1(fmt, args...) \
69 do { \
70  if (g_verbose >= 1) \
71  fformat (stdout, fmt, ## args); \
72 } while (0)
73 
74 #define VERBOSE2(fmt, args...) \
75 do { \
76  if (g_verbose >= 2) \
77  fformat (stdout, fmt, ## args); \
78 } while (0)
79 
80 #define VERBOSE3(fmt, args...) \
81 do { \
82  if (g_verbose >= 3) \
83  fformat (stdout, fmt, ## args); \
84 } while (0)
85 
86 #define clib_mem_free_safe(p) \
87 do { \
88  if (p) \
89  { \
90  clib_mem_free (p); \
91  (p) = NULL; \
92  } \
93 } while (0)
94 
95 /* XXX - I get undefined symbol trying to call random_u32() <vppinfra/random.h> */
96 /* Simple random number generator with period 2^31 - 1. */
97 static u32
98 my_random_u32 (u32 * seed_return)
99 {
100  /* Unlikely mask value to XOR into seed.
101  Otherwise small seed values would give
102  non-random seeming smallish numbers. */
103  const u32 mask = 0x12345678;
104  u32 seed, a, b, result;
105 
106  seed = *seed_return;
107  seed ^= mask;
108 
109  a = seed / 127773;
110  b = seed % 127773;
111  seed = 16807 * b - 2836 * a;
112 
113  if ((i32) seed < 0)
114  seed += ((u32) 1 << 31) - 1;
115 
116  result = seed;
117 
118  *seed_return = seed ^ mask;
119 
120  return result;
121 }
122 
123 static u32
125 {
126  if (lo == hi)
127  return lo;
128 
129  ASSERT (lo < hi);
130 
131  return ((my_random_u32 (seed) % (hi - lo + ((hi != ~0) ? (1) : (0)))) + lo);
132 }
133 
134 #define fill_with_random_data(ptr, bytes, seed) \
135 do { \
136  u8 * _v(p) = (u8 *) (ptr); \
137  uword _v(b) = (bytes); \
138  uword _v(i); \
139  \
140  for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
141  _v(p)[_v(i)] = (u8) bounded_random_u32 (&(seed), 0, 255); \
142  \
143 } while (0)
144 
145 #define compute_mem_hash(hash, ptr, bytes) \
146 ({ \
147  u8 * _v(p) = (u8 *) (ptr); \
148  uword _v(b) = (uword) (bytes); \
149  uword _v(i); \
150  uword _v(h) = (u8) (hash); \
151  \
152  if (_v(p) && _v(b) > 0) \
153  { \
154  for (_v(i) = 0; _v(i) < _v(b); _v(i)++) \
155  _v(h) ^= _v(p)[_v(i)]; \
156  } \
157  \
158  _v(h); \
159 })
160 
161 #define log2_align_down(value, align) \
162 ({ \
163  uword _v = (uword) (value); \
164  uword _a = (uword) (align); \
165  uword _m = (1 << _a) - 1; \
166  \
167  _v = _v & ~_m; \
168 })
169 
170 #define log2_align_up(value, align) \
171 ({ \
172  uword _v = (uword) (value); \
173  uword _a = (uword) (align); \
174  uword _m = (1 << _a) - 1; \
175  \
176  _v = (_v + _m) & ~_m; \
177 })
178 
179 #define log2_align_ptr_down(ptr, align) \
180 uword_to_pointer (log2_align_down (pointer_to_uword (ptr), align), void *)
181 
182 #define log2_align_ptr_up(ptr, align) \
183 uword_to_pointer (log2_align_up (pointer_to_uword (ptr), align), void *)
184 
185 #define MAX_LOG2_ALIGN 6
186 #define MAX_UNALIGN_OFFSET ((1 << MAX_LOG2_ALIGN) - 1)
187 
188 /* Allocates pointer to memory whose address is:
189  addr = <log2_align>-aligned address */
190 always_inline void *
191 alloc_aligned (uword size, uword log2_align, void **ptr_to_free)
192 {
193  void *p;
194 
195  if (size <= 0)
196  return NULL;
197 
198  p = (void *) clib_mem_alloc (size + (1 << log2_align) - 1);
199 
200  if (ptr_to_free)
201  *ptr_to_free = p;
202 
203  return (p) ? log2_align_ptr_up (p, log2_align) : (NULL);
204 }
205 
206 /* Allocates pointer to memory whose address is:
207  addr = MAX_LOG2_ALIGN-aligned address + <offset> */
208 always_inline void *
209 alloc_unaligned (uword size, uword offset, void **ptr_to_free)
210 {
211  void *p;
212 
213  if (size <= 0)
214  return NULL;
215 
217 
218  p =
219  alloc_aligned (size + (1 << MAX_LOG2_ALIGN), MAX_LOG2_ALIGN, ptr_to_free);
220 
221  if (!p)
222  return NULL;
223 
224  return (void *) ((u8 *) p + (offset % MAX_UNALIGN_OFFSET));
225 }
226 
227 #define memory_snap() \
228 do { \
229  clib_mem_usage_t _usage = { 0 }; \
230  clib_mem_usage (&_usage); \
231  fformat (stdout, "%U\n", format_clib_mem_usage, _usage, 0); \
232 } while (0)
233 
234 
235 #endif /* included_test_vec_h */
236 
237 /*
238  * fd.io coding-style-patch-verification: ON
239  *
240  * Local Variables:
241  * eval: (c-set-style "gnu")
242  * End:
243  */
my_random_u32
static u32 my_random_u32(u32 *seed_return)
Definition: test_vec.h:98
format_u32_binary
static u8 * format_u32_binary(u8 *s, va_list *va)
Definition: test_vec.h:52
bounded_random_u32
static u32 bounded_random_u32(u32 *seed, uword lo, uword hi)
Definition: test_vec.h:124
MAX_LOG2_ALIGN
#define MAX_LOG2_ALIGN
Definition: test_vec.h:185
clib.h
hi
vl_api_ip4_address_t hi
Definition: arp.api:37
i32
signed int i32
Definition: types.h:77
alloc_aligned
static void * alloc_aligned(uword size, uword log2_align, void **ptr_to_free)
Definition: test_vec.h:191
error.h
BITS
#define BITS(x)
Definition: clib.h:69
uword
u64 uword
Definition: types.h:112
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
g_verbose
uword g_verbose
mask
vl_api_pnat_mask_t mask
Definition: pnat.api:45
format.h
size
u32 size
Definition: vhost_user.h:125
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
clib_bihash_value
template key/value backing page structure
Definition: bihash_doc.h:44
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
MAX_UNALIGN_OFFSET
#define MAX_UNALIGN_OFFSET
Definition: test_vec.h:186
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
a
a
Definition: bitmap.h:544
word
i64 word
Definition: types.h:111
log2_align_ptr_up
#define log2_align_ptr_up(ptr, align)
Definition: test_vec.h:182
mem.h
clib_mem_alloc
static void * clib_mem_alloc(uword size)
Definition: mem.h:253
lo
lo
Definition: vector_altivec.h:95
alloc_unaligned
static void * alloc_unaligned(uword size, uword offset, void **ptr_to_free)
Definition: test_vec.h:209
g_seed
u32 g_seed