blob: 0dcff031364c30b34600a7c4436a3f95bcdd5e3d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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) 2005 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/format.h>
39#include <vppinfra/random.h>
40#include <vppinfra/serialize.h>
41#include <vppinfra/os.h>
42
43#define foreach_my_vector_type \
44 _ (u8, a8) \
45 _ (u16, a16) \
46 _ (u32, a32)
47
Dave Barachc3799992016-08-15 11:12:27 -040048typedef struct
49{
Ed Warnickecb9cada2015-12-08 15:45:58 -070050#define _(t,f) t f;
51 foreach_my_vector_type
52#undef _
53} my_vector_type_t;
54
Dave Barachc3799992016-08-15 11:12:27 -040055static void
56serialize_my_vector_type_single (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070057{
Dave Barachc3799992016-08-15 11:12:27 -040058 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070059 u32 n = va_arg (*va, u32);
60 u32 i;
61
62 for (i = 0; i < n; i++)
63 {
64#define _(t,f) serialize_integer (m, v[i].f, sizeof (v[i].f));
65 foreach_my_vector_type;
66 }
67#undef _
68}
69
Dave Barachc3799992016-08-15 11:12:27 -040070static void
71unserialize_my_vector_type_single (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070072{
Dave Barachc3799992016-08-15 11:12:27 -040073 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 u32 n = va_arg (*va, u32);
75 u32 i;
76
77 for (i = 0; i < n; i++)
78 {
79#define _(t,f) { u32 tmp; unserialize_integer (m, &tmp, sizeof (v[i].f)); v[i].f = tmp; }
80 foreach_my_vector_type;
81#undef _
82 }
83}
84
Dave Barachc3799992016-08-15 11:12:27 -040085static void
86serialize_my_vector_type_multiple (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Dave Barachc3799992016-08-15 11:12:27 -040088 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 u32 n = va_arg (*va, u32);
90
91#define _(t,f) \
92 serialize_multiple \
93 (m, \
94 &v[0].f, \
95 STRUCT_SIZE_OF (my_vector_type_t, f), \
96 STRUCT_STRIDE_OF (my_vector_type_t, f), \
97 n);
98
99 foreach_my_vector_type;
100
101#undef _
102}
103
Dave Barachc3799992016-08-15 11:12:27 -0400104static void
105unserialize_my_vector_type_multiple (serialize_main_t * m, va_list * va)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106{
Dave Barachc3799992016-08-15 11:12:27 -0400107 my_vector_type_t *v = va_arg (*va, my_vector_type_t *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108 u32 n = va_arg (*va, u32);
109
110#define _(t,f) \
111 unserialize_multiple \
112 (m, \
113 &v[0].f, \
114 STRUCT_SIZE_OF (my_vector_type_t, f), \
115 STRUCT_STRIDE_OF (my_vector_type_t, f), \
116 n);
117
118 foreach_my_vector_type;
119
120#undef _
121}
122
Dave Barachc3799992016-08-15 11:12:27 -0400123typedef struct
124{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 u32 n_iter;
126 u32 seed;
127 u32 verbose;
128 u32 multiple;
129 u32 max_len;
130
Dave Barachc3799992016-08-15 11:12:27 -0400131 my_vector_type_t **test_vectors;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132
Dave Barachc3799992016-08-15 11:12:27 -0400133 char *dump_file;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
135 serialize_main_t serialize_main;
136 serialize_main_t unserialize_main;
137} test_serialize_main_t;
138
Dave Barach809eb662023-01-12 16:07:26 -0500139u8 *test_pattern;
140
141int
142vl (void *p)
143{
144 return vec_len (p);
145}
146
147void
148test_serialize_not_inline_double_vector_expand (void)
149{
150 serialize_main_t _m, *m = &_m;
151 u8 *serialized = 0;
152 u64 *magic;
153 void *p;
154 int i;
155
156 vec_validate (test_pattern, 1023);
157
158 for (i = 0; i < vec_len (test_pattern); i++)
159 test_pattern[i] = i & 0xff;
160
161 serialize_open_vector (m, serialized);
162 p = serialize_get (m, 61);
163 clib_memcpy_fast (p, test_pattern, 61);
164 serialize_integer (m, 0xDEADBEEFFEEDFACEULL, 8);
165 p = serialize_get (m, vec_len (test_pattern) - 62);
166 clib_memcpy_fast (p, test_pattern + 61, vec_len (test_pattern) - 62);
167 serialized = serialize_close_vector (m);
168
169 magic = (u64 *) (serialized + 61);
170
171 if (*magic != clib_net_to_host_u64 (0xDEADBEEFFEEDFACEULL))
172 {
173 fformat (stderr, "BUG!\n");
174 exit (1);
175 }
176 return;
177}
178
Dave Barachc3799992016-08-15 11:12:27 -0400179int
180test_serialize_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181{
Dave Barachc3799992016-08-15 11:12:27 -0400182 clib_error_t *error = 0;
183 test_serialize_main_t _tm, *tm = &_tm;
184 serialize_main_t *sm = &tm->serialize_main;
185 serialize_main_t *um = &tm->unserialize_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186 uword i;
187
Dave Barachb7b92992018-10-17 10:38:51 -0400188 clib_memset (tm, 0, sizeof (tm[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 tm->n_iter = 100;
190 tm->seed = 1;
191 tm->max_len = 128;
192 tm->verbose = 0;
193 tm->multiple = 1;
194
195 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
196 {
197 if (unformat (input, "iter %d", &tm->n_iter))
198 ;
199 else if (unformat (input, "seed %d", &tm->seed))
200 ;
201 else if (unformat (input, "file %s", &tm->dump_file))
202 ;
203 else if (unformat (input, "max-len %d", &tm->max_len))
204 ;
205 else if (unformat (input, "multiple %=", &tm->multiple, 1))
206 ;
207 else if (unformat (input, "single %=", &tm->multiple, 0))
208 ;
209 else if (unformat (input, "verbose %=", &tm->verbose, 1))
210 ;
Dave Barach809eb662023-01-12 16:07:26 -0500211 else if (unformat (input, "double-expand"))
212 {
213 test_serialize_not_inline_double_vector_expand ();
214 clib_warning ("serialize_not_inline double vector expand OK");
215 exit (0);
216 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700217 else
218 {
219 error = clib_error_create ("unknown input `%U'\n",
220 format_unformat_error, input);
221 goto done;
222 }
223 }
224
225 if (tm->seed == 0)
226 tm->seed = random_default_seed ();
227
Dave Barachc3799992016-08-15 11:12:27 -0400228 clib_warning ("iter %d seed %d max-len %d", tm->n_iter, tm->seed,
229 tm->max_len);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
231#ifdef CLIB_UNIX
232 if (tm->dump_file)
Dave Barach59b25652017-09-10 15:04:27 -0400233 serialize_open_clib_file (sm, tm->dump_file);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234 else
235#endif
236 serialize_open_vector (sm, 0);
237
238 vec_resize (tm->test_vectors, tm->n_iter);
239 for (i = 0; i < tm->n_iter; i++)
240 {
241 uword l = 1 + (random_u32 (&tm->seed) % tm->max_len);
Dave Barachc3799992016-08-15 11:12:27 -0400242 my_vector_type_t *mv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243
244 vec_resize (tm->test_vectors[i], l);
245 vec_foreach (mv, tm->test_vectors[i])
Dave Barachc3799992016-08-15 11:12:27 -0400246 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247#define _(t,f) mv->f = random_u32 (&tm->seed) & pow2_mask (31);
Dave Barachc3799992016-08-15 11:12:27 -0400248 foreach_my_vector_type;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249#undef _
Dave Barachc3799992016-08-15 11:12:27 -0400250 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251
252 vec_serialize (sm, tm->test_vectors[i],
Dave Barachc3799992016-08-15 11:12:27 -0400253 tm->multiple ? serialize_my_vector_type_multiple :
254 serialize_my_vector_type_single);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255 }
256
257 if (tm->verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400258 clib_warning ("overflow vector max bytes %d",
259 vec_max_len (sm->stream.overflow_buffer));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260
261 serialize_close (sm);
262
263#ifdef CLIB_UNIX
264 if (tm->dump_file)
265 {
Dave Barach59b25652017-09-10 15:04:27 -0400266 if ((error = unserialize_open_clib_file (um, tm->dump_file)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267 goto done;
268 }
269 else
270#endif
271 {
Dave Barachc3799992016-08-15 11:12:27 -0400272 u8 *v = serialize_close_vector (sm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273 unserialize_open_data (um, v, vec_len (v));
274 }
275
276 for (i = 0; i < tm->n_iter; i++)
277 {
Dave Barachc3799992016-08-15 11:12:27 -0400278 my_vector_type_t *mv0;
279 my_vector_type_t *mv1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
281 vec_unserialize (um, &mv0,
Dave Barachc3799992016-08-15 11:12:27 -0400282 tm->multiple ? unserialize_my_vector_type_multiple :
283 unserialize_my_vector_type_single);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 mv1 = tm->test_vectors[i];
285
286 if (vec_len (mv0) != vec_len (mv1))
287 os_panic ();
288 if (memcmp (mv0, mv1, vec_len (mv0) * sizeof (mv0[0])))
289 os_panic ();
290
291 vec_free (mv0);
292 }
293
Dave Barachc3799992016-08-15 11:12:27 -0400294done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 if (error)
296 clib_error_report (error);
297 return 0;
298}
299
300#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400301int
302main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303{
304 unformat_input_t i;
305 int r;
306
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200307 clib_mem_init (0, 64ULL << 20);
308
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 unformat_init_command_line (&i, argv);
310 r = test_serialize_main (&i);
311 unformat_free (&i);
312 return r;
313}
314#endif
Dave Barachc3799992016-08-15 11:12:27 -0400315
316/*
317 * fd.io coding-style-patch-verification: ON
318 *
319 * Local Variables:
320 * eval: (c-set-style "gnu")
321 * End:
322 */