Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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) 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 | /* Turn data structures into byte streams for saving or transport. */ |
| 39 | |
| 40 | #include <vppinfra/heap.h> |
| 41 | #include <vppinfra/pool.h> |
| 42 | #include <vppinfra/serialize.h> |
| 43 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 44 | void |
| 45 | serialize_64 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | { |
| 47 | u64 x = va_arg (*va, u64); |
| 48 | u32 lo, hi; |
| 49 | lo = x; |
| 50 | hi = x >> 32; |
| 51 | serialize_integer (m, lo, sizeof (lo)); |
| 52 | serialize_integer (m, hi, sizeof (hi)); |
| 53 | } |
| 54 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | void |
| 56 | serialize_32 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | { |
| 58 | u32 x = va_arg (*va, u32); |
| 59 | serialize_integer (m, x, sizeof (x)); |
| 60 | } |
| 61 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 62 | void |
| 63 | serialize_16 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 64 | { |
| 65 | u32 x = va_arg (*va, u32); |
| 66 | serialize_integer (m, x, sizeof (u16)); |
| 67 | } |
| 68 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 69 | void |
| 70 | serialize_8 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | { |
| 72 | u32 x = va_arg (*va, u32); |
| 73 | serialize_integer (m, x, sizeof (u8)); |
| 74 | } |
| 75 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | void |
| 77 | unserialize_64 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 79 | u64 *x = va_arg (*va, u64 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 80 | u32 lo, hi; |
| 81 | unserialize_integer (m, &lo, sizeof (lo)); |
| 82 | unserialize_integer (m, &hi, sizeof (hi)); |
| 83 | *x = ((u64) hi << 32) | (u64) lo; |
| 84 | } |
| 85 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 86 | void |
| 87 | unserialize_32 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 89 | u32 *x = va_arg (*va, u32 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | unserialize_integer (m, x, sizeof (x[0])); |
| 91 | } |
| 92 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 93 | void |
| 94 | unserialize_16 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 96 | u16 *x = va_arg (*va, u16 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | u32 t; |
| 98 | unserialize_integer (m, &t, sizeof (x[0])); |
| 99 | x[0] = t; |
| 100 | } |
| 101 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 102 | void |
| 103 | unserialize_8 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 105 | u8 *x = va_arg (*va, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | u32 t; |
| 107 | unserialize_integer (m, &t, sizeof (x[0])); |
| 108 | x[0] = t; |
| 109 | } |
| 110 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 111 | void |
| 112 | serialize_f64 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | { |
| 114 | f64 x = va_arg (*va, f64); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 115 | union |
| 116 | { |
| 117 | f64 f; |
| 118 | u64 i; |
| 119 | } y; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | y.f = x; |
| 121 | serialize (m, serialize_64, y.i); |
| 122 | } |
| 123 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 124 | void |
| 125 | serialize_f32 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | { |
| 127 | f32 x = va_arg (*va, f64); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 128 | union |
| 129 | { |
| 130 | f32 f; |
| 131 | u32 i; |
| 132 | } y; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | y.f = x; |
| 134 | serialize_integer (m, y.i, sizeof (y.i)); |
| 135 | } |
| 136 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 137 | void |
| 138 | unserialize_f64 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 140 | f64 *x = va_arg (*va, f64 *); |
| 141 | union |
| 142 | { |
| 143 | f64 f; |
| 144 | u64 i; |
| 145 | } y; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | unserialize (m, unserialize_64, &y.i); |
| 147 | *x = y.f; |
| 148 | } |
| 149 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 150 | void |
| 151 | unserialize_f32 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 153 | f32 *x = va_arg (*va, f32 *); |
| 154 | union |
| 155 | { |
| 156 | f32 f; |
| 157 | u32 i; |
| 158 | } y; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | unserialize_integer (m, &y.i, sizeof (y.i)); |
| 160 | *x = y.f; |
| 161 | } |
| 162 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 163 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 164 | serialize_cstring (serialize_main_t * m, char *s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | { |
| 166 | u32 len = s ? strlen (s) : 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 167 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | |
| 169 | serialize_likely_small_unsigned_integer (m, len); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 170 | if (len > 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | { |
| 172 | p = serialize_get (m, len); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 173 | clib_memcpy_fast (p, s, len); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 177 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 178 | unserialize_cstring (serialize_main_t * m, char **s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 180 | char *p, *r = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | u32 len; |
| 182 | |
| 183 | len = unserialize_likely_small_unsigned_integer (m); |
| 184 | |
| 185 | /* |
| 186 | * Given broken enough data, we could get len = 0xFFFFFFFF. |
| 187 | * Add one, it overflows, we call vec_new (char, 0), then |
| 188 | * memcpy until we bus error. |
| 189 | */ |
| 190 | if (len > 0 && len != 0xFFFFFFFF) |
| 191 | { |
| 192 | r = vec_new (char, len + 1); |
| 193 | p = unserialize_get (m, len); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 194 | clib_memcpy_fast (r, p, len); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 195 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | /* Null terminate. */ |
| 197 | r[len] = 0; |
| 198 | } |
| 199 | *s = r; |
| 200 | } |
| 201 | |
| 202 | /* vec_serialize/vec_unserialize helper functions for basic vector types. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 203 | void |
| 204 | serialize_vec_8 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 206 | u8 *s = va_arg (*va, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 207 | u32 n = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 208 | u8 *p = serialize_get (m, n * sizeof (u8)); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 209 | clib_memcpy_fast (p, s, n * sizeof (u8)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 212 | void |
| 213 | unserialize_vec_8 (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 214 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 215 | u8 *s = va_arg (*va, u8 *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | u32 n = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 217 | u8 *p = unserialize_get (m, n); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 218 | clib_memcpy_fast (s, p, n); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | #define _(n_bits) \ |
| 222 | void serialize_vec_##n_bits (serialize_main_t * m, va_list * va) \ |
| 223 | { \ |
| 224 | u##n_bits * s = va_arg (*va, u##n_bits *); \ |
| 225 | u32 n = va_arg (*va, u32); \ |
| 226 | u##n_bits * p = serialize_get (m, n * sizeof (s[0])); \ |
| 227 | \ |
| 228 | while (n >= 4) \ |
| 229 | { \ |
| 230 | p[0] = clib_host_to_net_u##n_bits (s[0]); \ |
| 231 | p[1] = clib_host_to_net_u##n_bits (s[1]); \ |
| 232 | p[2] = clib_host_to_net_u##n_bits (s[2]); \ |
| 233 | p[3] = clib_host_to_net_u##n_bits (s[3]); \ |
| 234 | s += 4; \ |
| 235 | p += 4; \ |
| 236 | n -= 4; \ |
| 237 | } \ |
| 238 | \ |
| 239 | while (n >= 1) \ |
| 240 | { \ |
| 241 | p[0] = clib_host_to_net_u##n_bits (s[0]); \ |
| 242 | s += 1; \ |
| 243 | p += 1; \ |
| 244 | n -= 1; \ |
| 245 | } \ |
| 246 | } \ |
| 247 | \ |
| 248 | void unserialize_vec_##n_bits (serialize_main_t * m, va_list * va) \ |
| 249 | { \ |
| 250 | u##n_bits * s = va_arg (*va, u##n_bits *); \ |
| 251 | u32 n = va_arg (*va, u32); \ |
| 252 | u##n_bits * p = unserialize_get (m, n * sizeof (s[0])); \ |
| 253 | \ |
| 254 | while (n >= 4) \ |
| 255 | { \ |
| 256 | s[0] = clib_net_to_host_mem_u##n_bits (&p[0]); \ |
| 257 | s[1] = clib_net_to_host_mem_u##n_bits (&p[1]); \ |
| 258 | s[2] = clib_net_to_host_mem_u##n_bits (&p[2]); \ |
| 259 | s[3] = clib_net_to_host_mem_u##n_bits (&p[3]); \ |
| 260 | s += 4; \ |
| 261 | p += 4; \ |
| 262 | n -= 4; \ |
| 263 | } \ |
| 264 | \ |
| 265 | while (n >= 1) \ |
| 266 | { \ |
| 267 | s[0] = clib_net_to_host_mem_u##n_bits (&p[0]); \ |
| 268 | s += 1; \ |
| 269 | p += 1; \ |
| 270 | n -= 1; \ |
| 271 | } \ |
| 272 | } |
| 273 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 274 | _(16); |
| 275 | _(32); |
| 276 | _(64); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | |
| 278 | #undef _ |
| 279 | |
| 280 | #define SERIALIZE_VECTOR_CHUNK_SIZE 64 |
| 281 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 282 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 283 | serialize_vector (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 285 | void *vec = va_arg (*va, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 286 | u32 elt_bytes = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 287 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | u32 l = vec_len (vec); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 289 | void *p = vec; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | |
| 291 | serialize_integer (m, l, sizeof (l)); |
| 292 | |
| 293 | /* Serialize vector in chunks for cache locality. */ |
| 294 | while (l != 0) |
| 295 | { |
| 296 | u32 n = clib_min (SERIALIZE_VECTOR_CHUNK_SIZE, l); |
| 297 | serialize (m, f, p, n); |
| 298 | l -= n; |
| 299 | p += SERIALIZE_VECTOR_CHUNK_SIZE * elt_bytes; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 304 | unserialize_vector_ha (serialize_main_t * m, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 305 | u32 elt_bytes, |
| 306 | u32 header_bytes, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 307 | u32 align, u32 max_length, serialize_function_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 308 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 309 | void *v, *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | u32 l; |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame^] | 311 | vec_attr_t va = { .align = align, |
| 312 | .elt_sz = elt_bytes, |
| 313 | .hdr_sz = header_bytes }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 314 | |
| 315 | unserialize_integer (m, &l, sizeof (l)); |
| 316 | if (l > max_length) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 317 | serialize_error (&m->header, |
| 318 | clib_error_create ("bad vector length %d", l)); |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame^] | 319 | |
| 320 | p = v = _vec_alloc_internal (l, &va); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | |
| 322 | while (l != 0) |
| 323 | { |
| 324 | u32 n = clib_min (SERIALIZE_VECTOR_CHUNK_SIZE, l); |
| 325 | unserialize (m, f, p, n); |
| 326 | l -= n; |
| 327 | p += SERIALIZE_VECTOR_CHUNK_SIZE * elt_bytes; |
| 328 | } |
| 329 | return v; |
| 330 | } |
| 331 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 332 | void |
| 333 | unserialize_aligned_vector (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 335 | void **vec = va_arg (*va, void **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 336 | u32 elt_bytes = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 337 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | u32 align = va_arg (*va, u32); |
| 339 | |
| 340 | *vec = unserialize_vector_ha (m, elt_bytes, |
| 341 | /* header_bytes */ 0, |
| 342 | /* align */ align, |
| 343 | /* max_length */ ~0, |
| 344 | f); |
| 345 | } |
| 346 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 347 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 348 | unserialize_vector (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 349 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 350 | void **vec = va_arg (*va, void **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | u32 elt_bytes = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 352 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 353 | |
| 354 | *vec = unserialize_vector_ha (m, elt_bytes, |
| 355 | /* header_bytes */ 0, |
| 356 | /* align */ 0, |
| 357 | /* max_length */ ~0, |
| 358 | f); |
| 359 | } |
| 360 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 361 | void |
| 362 | serialize_bitmap (serialize_main_t * m, uword * b) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | { |
| 364 | u32 l, i, n_u32s; |
| 365 | |
| 366 | l = vec_len (b); |
| 367 | n_u32s = l * sizeof (b[0]) / sizeof (u32); |
| 368 | serialize_integer (m, n_u32s, sizeof (n_u32s)); |
| 369 | |
| 370 | /* Send 32 bit words, low-order word first on 64 bit. */ |
| 371 | for (i = 0; i < l; i++) |
| 372 | { |
| 373 | serialize_integer (m, b[i], sizeof (u32)); |
| 374 | if (BITS (uword) == 64) |
| 375 | serialize_integer (m, (u64) b[i] >> (u64) 32, sizeof (u32)); |
| 376 | } |
| 377 | } |
| 378 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 379 | uword * |
| 380 | unserialize_bitmap (serialize_main_t * m) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 381 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 382 | uword *b = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | u32 i, n_u32s; |
| 384 | |
| 385 | unserialize_integer (m, &n_u32s, sizeof (n_u32s)); |
| 386 | if (n_u32s == 0) |
| 387 | return b; |
| 388 | |
| 389 | i = (n_u32s * sizeof (u32) + sizeof (b[0]) - 1) / sizeof (b[0]); |
| 390 | vec_resize (b, i); |
| 391 | for (i = 0; i < n_u32s; i++) |
| 392 | { |
| 393 | u32 data; |
| 394 | unserialize_integer (m, &data, sizeof (u32)); |
| 395 | |
| 396 | /* Low-word is first on 64 bit. */ |
| 397 | if (BITS (uword) == 64) |
| 398 | { |
| 399 | if ((i % 2) == 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 400 | b[i / 2] |= (u64) data << (u64) 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 402 | b[i / 2] |= (u64) data << (u64) 32; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 403 | } |
| 404 | else |
| 405 | { |
| 406 | b[i] = data; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return b; |
| 411 | } |
| 412 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 413 | void |
| 414 | serialize_pool (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 416 | void *pool = va_arg (*va, void *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | u32 elt_bytes = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 418 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 419 | u32 l, lo, hi; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 420 | pool_header_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | |
| 422 | l = vec_len (pool); |
| 423 | serialize_integer (m, l, sizeof (u32)); |
| 424 | if (l == 0) |
| 425 | return; |
| 426 | p = pool_header (pool); |
| 427 | |
| 428 | /* No need to send free bitmap. Need to send index vector |
| 429 | to guarantee that unserialized pool will be identical. */ |
| 430 | vec_serialize (m, p->free_indices, serialize_vec_32); |
| 431 | |
| 432 | pool_foreach_region (lo, hi, pool, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 433 | serialize (m, f, pool + lo * elt_bytes, hi - lo)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static void * |
| 437 | unserialize_pool_helper (serialize_main_t * m, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 438 | u32 elt_bytes, u32 align, serialize_function_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 439 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 440 | void *v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 441 | u32 i, l, lo, hi; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 442 | pool_header_t *p; |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame^] | 443 | vec_attr_t va = { .align = align, |
| 444 | .elt_sz = elt_bytes, |
| 445 | .hdr_sz = sizeof (pool_header_t) }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | |
| 447 | unserialize_integer (m, &l, sizeof (l)); |
| 448 | if (l == 0) |
| 449 | { |
| 450 | return 0; |
| 451 | } |
| 452 | |
Damjan Marion | e4fa1d2 | 2022-04-11 18:41:49 +0200 | [diff] [blame^] | 453 | v = _vec_alloc_internal (l, &va); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | p = pool_header (v); |
| 455 | |
| 456 | vec_unserialize (m, &p->free_indices, unserialize_vec_32); |
| 457 | |
| 458 | /* Construct free bitmap. */ |
| 459 | p->free_bitmap = 0; |
| 460 | for (i = 0; i < vec_len (p->free_indices); i++) |
| 461 | p->free_bitmap = clib_bitmap_ori (p->free_bitmap, p->free_indices[i]); |
| 462 | |
| 463 | pool_foreach_region (lo, hi, v, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 464 | unserialize (m, f, v + lo * elt_bytes, hi - lo)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 465 | |
| 466 | return v; |
| 467 | } |
| 468 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 469 | void |
| 470 | unserialize_pool (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 472 | void **result = va_arg (*va, void **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 473 | u32 elt_bytes = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 474 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | *result = unserialize_pool_helper (m, elt_bytes, /* align */ 0, f); |
| 476 | } |
| 477 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 478 | void |
| 479 | unserialize_aligned_pool (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 481 | void **result = va_arg (*va, void **); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 482 | u32 elt_bytes = va_arg (*va, u32); |
| 483 | u32 align = va_arg (*va, u32); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 484 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 485 | *result = unserialize_pool_helper (m, elt_bytes, align, f); |
| 486 | } |
| 487 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 488 | static void |
| 489 | serialize_vec_heap_elt (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 490 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 491 | heap_elt_t *e = va_arg (*va, heap_elt_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 492 | u32 i, n = va_arg (*va, u32); |
| 493 | for (i = 0; i < n; i++) |
| 494 | { |
| 495 | serialize_integer (m, e[i].offset, sizeof (e[i].offset)); |
| 496 | serialize_integer (m, e[i].next, sizeof (e[i].next)); |
| 497 | serialize_integer (m, e[i].prev, sizeof (e[i].prev)); |
| 498 | } |
| 499 | } |
| 500 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 501 | static void |
| 502 | unserialize_vec_heap_elt (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 504 | heap_elt_t *e = va_arg (*va, heap_elt_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 505 | u32 i, n = va_arg (*va, u32); |
| 506 | for (i = 0; i < n; i++) |
| 507 | { |
| 508 | unserialize_integer (m, &e[i].offset, sizeof (e[i].offset)); |
| 509 | unserialize_integer (m, &e[i].next, sizeof (e[i].next)); |
| 510 | unserialize_integer (m, &e[i].prev, sizeof (e[i].prev)); |
| 511 | } |
| 512 | } |
| 513 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 514 | void |
| 515 | serialize_heap (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 517 | void *heap = va_arg (*va, void *); |
| 518 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | u32 i, l; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 520 | heap_header_t *h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | |
| 522 | l = vec_len (heap); |
| 523 | serialize_integer (m, l, sizeof (u32)); |
| 524 | if (l == 0) |
| 525 | return; |
| 526 | |
| 527 | h = heap_header (heap); |
| 528 | |
| 529 | #define foreach_serialize_heap_header_integer \ |
| 530 | _ (head) _ (tail) _ (used_count) _ (max_len) _ (flags) _ (elt_bytes) |
| 531 | |
| 532 | #define _(f) serialize_integer (m, h->f, sizeof (h->f)); |
| 533 | foreach_serialize_heap_header_integer; |
| 534 | #undef _ |
| 535 | |
| 536 | serialize_integer (m, vec_len (h->free_lists), sizeof (u32)); |
| 537 | for (i = 0; i < vec_len (h->free_lists); i++) |
| 538 | vec_serialize (m, h->free_lists[i], serialize_vec_32); |
| 539 | |
| 540 | vec_serialize (m, h->elts, serialize_vec_heap_elt); |
| 541 | vec_serialize (m, h->small_free_elt_free_index, serialize_vec_32); |
| 542 | vec_serialize (m, h->free_elts, serialize_vec_32); |
| 543 | |
| 544 | /* Serialize data in heap. */ |
| 545 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 546 | heap_elt_t *e, *end; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 547 | e = h->elts + h->head; |
| 548 | end = h->elts + h->tail; |
| 549 | while (1) |
| 550 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 551 | if (!heap_is_free (e)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 552 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 553 | void *v = heap + heap_offset (e) * h->elt_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 554 | u32 n = heap_elt_size (heap, e); |
| 555 | serialize (m, f, v, n); |
| 556 | } |
| 557 | if (e == end) |
| 558 | break; |
| 559 | e = heap_next (e); |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 564 | void |
| 565 | unserialize_heap (serialize_main_t * m, va_list * va) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 567 | void **result = va_arg (*va, void **); |
| 568 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 569 | u32 i, vl, fl; |
| 570 | heap_header_t h; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 571 | void *heap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 572 | |
| 573 | unserialize_integer (m, &vl, sizeof (u32)); |
| 574 | if (vl == 0) |
| 575 | { |
| 576 | *result = 0; |
| 577 | return; |
| 578 | } |
| 579 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 580 | clib_memset (&h, 0, sizeof (h)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 581 | #define _(f) unserialize_integer (m, &h.f, sizeof (h.f)); |
| 582 | foreach_serialize_heap_header_integer; |
| 583 | #undef _ |
| 584 | |
| 585 | unserialize_integer (m, &fl, sizeof (u32)); |
| 586 | vec_resize (h.free_lists, fl); |
| 587 | |
| 588 | for (i = 0; i < vec_len (h.free_lists); i++) |
| 589 | vec_unserialize (m, &h.free_lists[i], unserialize_vec_32); |
| 590 | |
| 591 | vec_unserialize (m, &h.elts, unserialize_vec_heap_elt); |
| 592 | vec_unserialize (m, &h.small_free_elt_free_index, unserialize_vec_32); |
| 593 | vec_unserialize (m, &h.free_elts, unserialize_vec_32); |
| 594 | |
| 595 | /* Re-construct used elt bitmap. */ |
| 596 | if (CLIB_DEBUG > 0) |
| 597 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 598 | heap_elt_t *e; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | vec_foreach (e, h.elts) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 600 | { |
| 601 | if (!heap_is_free (e)) |
| 602 | h.used_elt_bitmap = clib_bitmap_ori (h.used_elt_bitmap, e - h.elts); |
| 603 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | heap = *result = _heap_new (vl, h.elt_bytes); |
| 607 | heap_header (heap)[0] = h; |
| 608 | |
| 609 | /* Unserialize data in heap. */ |
| 610 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 611 | heap_elt_t *e, *end; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 612 | e = h.elts + h.head; |
| 613 | end = h.elts + h.tail; |
| 614 | while (1) |
| 615 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 616 | if (!heap_is_free (e)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 617 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 618 | void *v = heap + heap_offset (e) * h.elt_bytes; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 619 | u32 n = heap_elt_size (heap, e); |
| 620 | unserialize (m, f, v, n); |
| 621 | } |
| 622 | if (e == end) |
| 623 | break; |
| 624 | e = heap_next (e); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 629 | void |
| 630 | serialize_magic (serialize_main_t * m, void *magic, u32 magic_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 631 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 632 | void *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 633 | serialize_integer (m, magic_bytes, sizeof (magic_bytes)); |
| 634 | p = serialize_get (m, magic_bytes); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 635 | clib_memcpy_fast (p, magic, magic_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 638 | void |
| 639 | unserialize_check_magic (serialize_main_t * m, void *magic, u32 magic_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 640 | { |
| 641 | u32 l; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 642 | void *d; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 643 | |
| 644 | unserialize_integer (m, &l, sizeof (l)); |
| 645 | if (l != magic_bytes) |
| 646 | { |
| 647 | bad: |
| 648 | serialize_error_return (m, "bad magic number"); |
| 649 | } |
| 650 | d = serialize_get (m, magic_bytes); |
| 651 | if (memcmp (magic, d, magic_bytes)) |
| 652 | goto bad; |
| 653 | } |
| 654 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 655 | __clib_export clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | va_serialize (serialize_main_t * sm, va_list * va) |
| 657 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 658 | serialize_main_header_t *m = &sm->header; |
| 659 | serialize_function_t *f = va_arg (*va, serialize_function_t *); |
| 660 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | |
| 662 | m->recursion_level += 1; |
| 663 | if (m->recursion_level == 1) |
| 664 | { |
| 665 | uword r = clib_setjmp (&m->error_longjmp, 0); |
| 666 | error = uword_to_pointer (r, clib_error_t *); |
| 667 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 668 | |
| 669 | if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 670 | f (sm, va); |
| 671 | |
| 672 | m->recursion_level -= 1; |
| 673 | return error; |
| 674 | } |
| 675 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 676 | __clib_export clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 677 | serialize (serialize_main_t * m, ...) |
| 678 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 679 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 680 | va_list va; |
| 681 | |
| 682 | va_start (va, m); |
| 683 | error = va_serialize (m, &va); |
| 684 | va_end (va); |
| 685 | return error; |
| 686 | } |
| 687 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 688 | __clib_export clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 689 | unserialize (serialize_main_t * m, ...) |
| 690 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 691 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 692 | va_list va; |
| 693 | |
| 694 | va_start (va, m); |
| 695 | error = va_serialize (m, &va); |
| 696 | va_end (va); |
| 697 | return error; |
| 698 | } |
| 699 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 700 | static void * |
| 701 | serialize_write_not_inline (serialize_main_header_t * m, |
| 702 | serialize_stream_t * s, |
| 703 | uword n_bytes_to_write, uword flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 704 | { |
| 705 | uword cur_bi, n_left_b, n_left_o; |
| 706 | |
| 707 | ASSERT (s->current_buffer_index <= s->n_buffer_bytes); |
| 708 | cur_bi = s->current_buffer_index; |
| 709 | n_left_b = s->n_buffer_bytes - cur_bi; |
| 710 | n_left_o = vec_len (s->overflow_buffer); |
| 711 | |
| 712 | /* Prepend overflow buffer if present. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 713 | do |
| 714 | { |
| 715 | if (n_left_o > 0 && n_left_b > 0) |
| 716 | { |
| 717 | uword n = clib_min (n_left_b, n_left_o); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 718 | clib_memcpy_fast (s->buffer + cur_bi, s->overflow_buffer, n); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 719 | cur_bi += n; |
| 720 | n_left_b -= n; |
| 721 | n_left_o -= n; |
| 722 | if (n_left_o == 0) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 723 | vec_set_len (s->overflow_buffer, 0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 724 | else |
| 725 | vec_delete (s->overflow_buffer, n, 0); |
| 726 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 727 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 728 | /* Call data function when buffer is complete. Data function should |
| 729 | dispatch with current buffer and give us a new one to write more |
| 730 | data into. */ |
| 731 | if (n_left_b == 0) |
| 732 | { |
| 733 | s->current_buffer_index = cur_bi; |
| 734 | m->data_function (m, s); |
| 735 | cur_bi = s->current_buffer_index; |
| 736 | n_left_b = s->n_buffer_bytes - cur_bi; |
| 737 | } |
| 738 | } |
| 739 | while (n_left_o > 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | |
| 741 | if (n_left_o > 0 || n_left_b < n_bytes_to_write) |
| 742 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 743 | u8 *r; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 744 | vec_add2 (s->overflow_buffer, r, n_bytes_to_write); |
| 745 | return r; |
| 746 | } |
| 747 | else |
| 748 | { |
| 749 | s->current_buffer_index = cur_bi + n_bytes_to_write; |
| 750 | return s->buffer + cur_bi; |
| 751 | } |
| 752 | } |
| 753 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 754 | static void * |
| 755 | serialize_read_not_inline (serialize_main_header_t * m, |
| 756 | serialize_stream_t * s, |
| 757 | uword n_bytes_to_read, uword flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 758 | { |
| 759 | uword cur_bi, cur_oi, n_left_b, n_left_o, n_left_to_read; |
| 760 | |
| 761 | ASSERT (s->current_buffer_index <= s->n_buffer_bytes); |
| 762 | |
| 763 | cur_bi = s->current_buffer_index; |
| 764 | cur_oi = s->current_overflow_index; |
| 765 | |
| 766 | n_left_b = s->n_buffer_bytes - cur_bi; |
| 767 | n_left_o = vec_len (s->overflow_buffer) - cur_oi; |
| 768 | |
| 769 | /* Read from overflow? */ |
| 770 | if (n_left_o >= n_bytes_to_read) |
| 771 | { |
| 772 | s->current_overflow_index = cur_oi + n_bytes_to_read; |
| 773 | return vec_elt_at_index (s->overflow_buffer, cur_oi); |
| 774 | } |
| 775 | |
| 776 | /* Reset overflow buffer. */ |
| 777 | if (n_left_o == 0 && s->overflow_buffer) |
| 778 | { |
| 779 | s->current_overflow_index = 0; |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 780 | vec_set_len (s->overflow_buffer, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | n_left_to_read = n_bytes_to_read; |
| 784 | while (n_left_to_read > 0) |
| 785 | { |
| 786 | uword n; |
| 787 | |
| 788 | /* If we don't have enough data between overflow and normal buffer |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 789 | call read function. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 790 | if (n_left_o + n_left_b < n_bytes_to_read) |
| 791 | { |
| 792 | /* Save any left over buffer in overflow vector. */ |
| 793 | if (n_left_b > 0) |
| 794 | { |
| 795 | vec_add (s->overflow_buffer, s->buffer + cur_bi, n_left_b); |
| 796 | n_left_o += n_left_b; |
| 797 | n_left_to_read -= n_left_b; |
| 798 | /* Advance buffer to end --- even if |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 799 | SERIALIZE_FLAG_NO_ADVANCE_CURRENT_BUFFER_INDEX is set. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 800 | cur_bi = s->n_buffer_bytes; |
| 801 | n_left_b = 0; |
| 802 | } |
| 803 | |
| 804 | if (m->data_function) |
| 805 | { |
| 806 | m->data_function (m, s); |
| 807 | cur_bi = s->current_buffer_index; |
| 808 | n_left_b = s->n_buffer_bytes - cur_bi; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /* For first time through loop return if we have enough data |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 813 | in normal buffer and overflow vector is empty. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 814 | if (n_left_o == 0 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 815 | && n_left_to_read == n_bytes_to_read && n_left_b >= n_left_to_read) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 816 | { |
| 817 | s->current_buffer_index = cur_bi + n_bytes_to_read; |
| 818 | return s->buffer + cur_bi; |
| 819 | } |
| 820 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 821 | if (!m->data_function || serialize_stream_is_end_of_stream (s)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 822 | { |
| 823 | /* This can happen for a peek at end of file. |
| 824 | Pad overflow buffer with 0s. */ |
| 825 | vec_resize (s->overflow_buffer, n_left_to_read); |
| 826 | n_left_o += n_left_to_read; |
| 827 | n_left_to_read = 0; |
| 828 | } |
| 829 | else |
| 830 | { |
| 831 | /* Copy from buffer to overflow vector. */ |
| 832 | n = clib_min (n_left_to_read, n_left_b); |
| 833 | vec_add (s->overflow_buffer, s->buffer + cur_bi, n); |
| 834 | cur_bi += n; |
| 835 | n_left_b -= n; |
| 836 | n_left_o += n; |
| 837 | n_left_to_read -= n; |
| 838 | } |
| 839 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 840 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 841 | s->current_buffer_index = cur_bi; |
| 842 | s->current_overflow_index = cur_oi + n_bytes_to_read; |
| 843 | return vec_elt_at_index (s->overflow_buffer, cur_oi); |
| 844 | } |
| 845 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 846 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 847 | serialize_read_write_not_inline (serialize_main_header_t * m, |
| 848 | serialize_stream_t * s, |
| 849 | uword n_bytes, uword flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 850 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 851 | return (((flags & SERIALIZE_FLAG_IS_READ) ? serialize_read_not_inline : |
| 852 | serialize_write_not_inline) (m, s, n_bytes, flags)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 855 | static void |
| 856 | serialize_read_write_close (serialize_main_header_t * m, |
| 857 | serialize_stream_t * s, uword flags) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 858 | { |
| 859 | if (serialize_stream_is_end_of_stream (s)) |
| 860 | return; |
| 861 | |
| 862 | if (flags & SERIALIZE_FLAG_IS_WRITE) |
| 863 | /* "Write" 0 bytes to flush overflow vector. */ |
| 864 | serialize_write_not_inline (m, s, /* n bytes */ 0, flags); |
| 865 | |
| 866 | serialize_stream_set_end_of_stream (s); |
| 867 | |
| 868 | /* Call it one last time to flush buffer and close. */ |
| 869 | m->data_function (m, s); |
| 870 | |
| 871 | vec_free (s->overflow_buffer); |
| 872 | } |
| 873 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 874 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 875 | serialize_close (serialize_main_t * m) |
| 876 | { |
| 877 | serialize_read_write_close (&m->header, &m->stream, |
| 878 | SERIALIZE_FLAG_IS_WRITE); |
| 879 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 880 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 881 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 882 | unserialize_close (serialize_main_t * m) |
| 883 | { |
| 884 | serialize_read_write_close (&m->header, &m->stream, SERIALIZE_FLAG_IS_READ); |
| 885 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 886 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 887 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 888 | serialize_open_data (serialize_main_t * m, u8 * data, uword n_data_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 889 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 890 | clib_memset (m, 0, sizeof (m[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 891 | m->stream.buffer = data; |
| 892 | m->stream.n_buffer_bytes = n_data_bytes; |
| 893 | } |
| 894 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 895 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 896 | unserialize_open_data (serialize_main_t * m, u8 * data, uword n_data_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 897 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 898 | serialize_open_data (m, data, n_data_bytes); |
| 899 | } |
| 900 | |
| 901 | static void |
| 902 | serialize_vector_write (serialize_main_header_t * m, serialize_stream_t * s) |
| 903 | { |
| 904 | if (!serialize_stream_is_end_of_stream (s)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 905 | { |
| 906 | /* Double buffer size. */ |
| 907 | uword l = vec_len (s->buffer); |
| 908 | vec_resize (s->buffer, l > 0 ? l : 64); |
| 909 | s->n_buffer_bytes = vec_len (s->buffer); |
| 910 | } |
| 911 | } |
| 912 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 913 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 914 | serialize_open_vector (serialize_main_t * m, u8 * vector) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 915 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 916 | clib_memset (m, 0, sizeof (m[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 917 | m->header.data_function = serialize_vector_write; |
| 918 | m->stream.buffer = vector; |
| 919 | m->stream.current_buffer_index = 0; |
| 920 | m->stream.n_buffer_bytes = vec_len (vector); |
| 921 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 922 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 923 | __clib_export void * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 924 | serialize_close_vector (serialize_main_t * m) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 925 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 926 | serialize_stream_t *s = &m->stream; |
| 927 | void *result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 928 | |
| 929 | serialize_close (m); /* frees overflow buffer */ |
| 930 | |
| 931 | if (s->buffer) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 932 | vec_set_len (s->buffer, s->current_buffer_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 933 | result = s->buffer; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 934 | clib_memset (m, 0, sizeof (m[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 935 | return result; |
| 936 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 937 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 938 | __clib_export void |
| 939 | serialize_multiple_1 (serialize_main_t *m, void *data, uword data_stride, |
| 940 | uword n_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 941 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 942 | u8 *d = data; |
| 943 | u8 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 944 | uword n_left = n_data; |
| 945 | |
| 946 | while (n_left >= 4) |
| 947 | { |
| 948 | p = serialize_get (m, 4 * sizeof (d[0])); |
| 949 | p[0] = d[0 * data_stride]; |
| 950 | p[1] = d[1 * data_stride]; |
| 951 | p[2] = d[2 * data_stride]; |
| 952 | p[3] = d[3 * data_stride]; |
| 953 | n_left -= 4; |
| 954 | d += 4 * data_stride; |
| 955 | } |
| 956 | |
| 957 | if (n_left > 0) |
| 958 | { |
| 959 | p = serialize_get (m, n_left * sizeof (p[0])); |
| 960 | while (n_left > 0) |
| 961 | { |
| 962 | p[0] = d[0]; |
| 963 | p += 1; |
| 964 | d += 1 * data_stride; |
| 965 | n_left -= 1; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 970 | __clib_export void |
| 971 | serialize_multiple_2 (serialize_main_t *m, void *data, uword data_stride, |
| 972 | uword n_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 974 | void *d = data; |
| 975 | u16 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 976 | uword n_left = n_data; |
| 977 | |
| 978 | while (n_left >= 4) |
| 979 | { |
| 980 | p = serialize_get (m, 4 * sizeof (p[0])); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 981 | clib_mem_unaligned (p + 0, u16) = |
| 982 | clib_host_to_net_mem_u16 (d + 0 * data_stride); |
| 983 | clib_mem_unaligned (p + 1, u16) = |
| 984 | clib_host_to_net_mem_u16 (d + 1 * data_stride); |
| 985 | clib_mem_unaligned (p + 2, u16) = |
| 986 | clib_host_to_net_mem_u16 (d + 2 * data_stride); |
| 987 | clib_mem_unaligned (p + 3, u16) = |
| 988 | clib_host_to_net_mem_u16 (d + 3 * data_stride); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 989 | n_left -= 4; |
| 990 | d += 4 * data_stride; |
| 991 | } |
| 992 | |
| 993 | if (n_left > 0) |
| 994 | { |
| 995 | p = serialize_get (m, n_left * sizeof (p[0])); |
| 996 | while (n_left > 0) |
| 997 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 998 | clib_mem_unaligned (p + 0, u16) = |
| 999 | clib_host_to_net_mem_u16 (d + 0 * data_stride); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1000 | p += 1; |
| 1001 | d += 1 * data_stride; |
| 1002 | n_left -= 1; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 1007 | __clib_export void |
| 1008 | serialize_multiple_4 (serialize_main_t *m, void *data, uword data_stride, |
| 1009 | uword n_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1010 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1011 | void *d = data; |
| 1012 | u32 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1013 | uword n_left = n_data; |
| 1014 | |
| 1015 | while (n_left >= 4) |
| 1016 | { |
| 1017 | p = serialize_get (m, 4 * sizeof (p[0])); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1018 | clib_mem_unaligned (p + 0, u32) = |
| 1019 | clib_host_to_net_mem_u32 (d + 0 * data_stride); |
| 1020 | clib_mem_unaligned (p + 1, u32) = |
| 1021 | clib_host_to_net_mem_u32 (d + 1 * data_stride); |
| 1022 | clib_mem_unaligned (p + 2, u32) = |
| 1023 | clib_host_to_net_mem_u32 (d + 2 * data_stride); |
| 1024 | clib_mem_unaligned (p + 3, u32) = |
| 1025 | clib_host_to_net_mem_u32 (d + 3 * data_stride); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1026 | n_left -= 4; |
| 1027 | d += 4 * data_stride; |
| 1028 | } |
| 1029 | |
| 1030 | if (n_left > 0) |
| 1031 | { |
| 1032 | p = serialize_get (m, n_left * sizeof (p[0])); |
| 1033 | while (n_left > 0) |
| 1034 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1035 | clib_mem_unaligned (p + 0, u32) = |
| 1036 | clib_host_to_net_mem_u32 (d + 0 * data_stride); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1037 | p += 1; |
| 1038 | d += 1 * data_stride; |
| 1039 | n_left -= 1; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 1044 | __clib_export void |
| 1045 | unserialize_multiple_1 (serialize_main_t *m, void *data, uword data_stride, |
| 1046 | uword n_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1047 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1048 | u8 *d = data; |
| 1049 | u8 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1050 | uword n_left = n_data; |
| 1051 | |
| 1052 | while (n_left >= 4) |
| 1053 | { |
| 1054 | p = unserialize_get (m, 4 * sizeof (d[0])); |
| 1055 | d[0 * data_stride] = p[0]; |
| 1056 | d[1 * data_stride] = p[1]; |
| 1057 | d[2 * data_stride] = p[2]; |
| 1058 | d[3 * data_stride] = p[3]; |
| 1059 | n_left -= 4; |
| 1060 | d += 4 * data_stride; |
| 1061 | } |
| 1062 | |
| 1063 | if (n_left > 0) |
| 1064 | { |
| 1065 | p = unserialize_get (m, n_left * sizeof (p[0])); |
| 1066 | while (n_left > 0) |
| 1067 | { |
| 1068 | d[0] = p[0]; |
| 1069 | p += 1; |
| 1070 | d += 1 * data_stride; |
| 1071 | n_left -= 1; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 1076 | __clib_export void |
| 1077 | unserialize_multiple_2 (serialize_main_t *m, void *data, uword data_stride, |
| 1078 | uword n_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1079 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1080 | void *d = data; |
| 1081 | u16 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1082 | uword n_left = n_data; |
| 1083 | |
| 1084 | while (n_left >= 4) |
| 1085 | { |
| 1086 | p = unserialize_get (m, 4 * sizeof (p[0])); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1087 | clib_mem_unaligned (d + 0 * data_stride, u16) = |
| 1088 | clib_net_to_host_mem_u16 (p + 0); |
| 1089 | clib_mem_unaligned (d + 1 * data_stride, u16) = |
| 1090 | clib_net_to_host_mem_u16 (p + 1); |
| 1091 | clib_mem_unaligned (d + 2 * data_stride, u16) = |
| 1092 | clib_net_to_host_mem_u16 (p + 2); |
| 1093 | clib_mem_unaligned (d + 3 * data_stride, u16) = |
| 1094 | clib_net_to_host_mem_u16 (p + 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1095 | n_left -= 4; |
| 1096 | d += 4 * data_stride; |
| 1097 | } |
| 1098 | |
| 1099 | if (n_left > 0) |
| 1100 | { |
| 1101 | p = unserialize_get (m, n_left * sizeof (p[0])); |
| 1102 | while (n_left > 0) |
| 1103 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1104 | clib_mem_unaligned (d + 0 * data_stride, u16) = |
| 1105 | clib_net_to_host_mem_u16 (p + 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1106 | p += 1; |
| 1107 | d += 1 * data_stride; |
| 1108 | n_left -= 1; |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 1113 | __clib_export void |
| 1114 | unserialize_multiple_4 (serialize_main_t *m, void *data, uword data_stride, |
| 1115 | uword n_data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1116 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1117 | void *d = data; |
| 1118 | u32 *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1119 | uword n_left = n_data; |
| 1120 | |
| 1121 | while (n_left >= 4) |
| 1122 | { |
| 1123 | p = unserialize_get (m, 4 * sizeof (p[0])); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1124 | clib_mem_unaligned (d + 0 * data_stride, u32) = |
| 1125 | clib_net_to_host_mem_u32 (p + 0); |
| 1126 | clib_mem_unaligned (d + 1 * data_stride, u32) = |
| 1127 | clib_net_to_host_mem_u32 (p + 1); |
| 1128 | clib_mem_unaligned (d + 2 * data_stride, u32) = |
| 1129 | clib_net_to_host_mem_u32 (p + 2); |
| 1130 | clib_mem_unaligned (d + 3 * data_stride, u32) = |
| 1131 | clib_net_to_host_mem_u32 (p + 3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1132 | n_left -= 4; |
| 1133 | d += 4 * data_stride; |
| 1134 | } |
| 1135 | |
| 1136 | if (n_left > 0) |
| 1137 | { |
| 1138 | p = unserialize_get (m, n_left * sizeof (p[0])); |
| 1139 | while (n_left > 0) |
| 1140 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1141 | clib_mem_unaligned (d + 0 * data_stride, u32) = |
| 1142 | clib_net_to_host_mem_u32 (p + 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1143 | p += 1; |
| 1144 | d += 1 * data_stride; |
| 1145 | n_left -= 1; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | #ifdef CLIB_UNIX |
| 1151 | |
| 1152 | #include <unistd.h> |
| 1153 | #include <fcntl.h> |
| 1154 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1155 | static void |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1156 | clib_file_write (serialize_main_header_t * m, serialize_stream_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1157 | { |
| 1158 | int fd, n; |
| 1159 | |
| 1160 | fd = s->data_function_opaque; |
| 1161 | n = write (fd, s->buffer, s->current_buffer_index); |
| 1162 | if (n < 0) |
| 1163 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1164 | if (!unix_error_is_fatal (errno)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1165 | n = 0; |
| 1166 | else |
| 1167 | serialize_error (m, clib_error_return_unix (0, "write")); |
| 1168 | } |
| 1169 | if (n == s->current_buffer_index) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1170 | vec_set_len (s->buffer, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1171 | else |
| 1172 | vec_delete (s->buffer, n, 0); |
| 1173 | s->current_buffer_index = vec_len (s->buffer); |
| 1174 | } |
| 1175 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1176 | static void |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1177 | clib_file_read (serialize_main_header_t * m, serialize_stream_t * s) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1178 | { |
| 1179 | int fd, n; |
| 1180 | |
| 1181 | fd = s->data_function_opaque; |
| 1182 | n = read (fd, s->buffer, vec_len (s->buffer)); |
| 1183 | if (n < 0) |
| 1184 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1185 | if (!unix_error_is_fatal (errno)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1186 | n = 0; |
| 1187 | else |
| 1188 | serialize_error (m, clib_error_return_unix (0, "read")); |
| 1189 | } |
| 1190 | else if (n == 0) |
| 1191 | serialize_stream_set_end_of_stream (s); |
| 1192 | s->current_buffer_index = 0; |
| 1193 | s->n_buffer_bytes = n; |
| 1194 | } |
| 1195 | |
| 1196 | static void |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1197 | serialize_open_clib_file_descriptor_helper (serialize_main_t * m, int fd, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1198 | uword is_read) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1199 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1200 | clib_memset (m, 0, sizeof (m[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1201 | vec_resize (m->stream.buffer, 4096); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1202 | |
| 1203 | if (!is_read) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1204 | { |
| 1205 | m->stream.n_buffer_bytes = vec_len (m->stream.buffer); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1206 | vec_set_len (m->stream.buffer, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1209 | m->header.data_function = is_read ? clib_file_read : clib_file_write; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1210 | m->stream.data_function_opaque = fd; |
| 1211 | } |
| 1212 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 1213 | __clib_export void |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1214 | serialize_open_clib_file_descriptor (serialize_main_t * m, int fd) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1215 | { |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1216 | serialize_open_clib_file_descriptor_helper (m, fd, /* is_read */ 0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1217 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1218 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 1219 | __clib_export void |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1220 | unserialize_open_clib_file_descriptor (serialize_main_t * m, int fd) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1221 | { |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1222 | serialize_open_clib_file_descriptor_helper (m, fd, /* is_read */ 1); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1223 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1224 | |
| 1225 | static clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1226 | serialize_open_clib_file_helper (serialize_main_t * m, char *file, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1227 | uword is_read) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1228 | { |
| 1229 | int fd, mode; |
| 1230 | |
| 1231 | mode = is_read ? O_RDONLY : O_RDWR | O_CREAT | O_TRUNC; |
| 1232 | fd = open (file, mode, 0666); |
| 1233 | if (fd < 0) |
| 1234 | return clib_error_return_unix (0, "open `%s'", file); |
| 1235 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1236 | serialize_open_clib_file_descriptor_helper (m, fd, is_read); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1237 | return 0; |
| 1238 | } |
| 1239 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 1240 | __clib_export clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1241 | serialize_open_clib_file (serialize_main_t * m, char *file) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1242 | { |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1243 | return serialize_open_clib_file_helper (m, file, /* is_read */ 0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1244 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1245 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 1246 | __clib_export clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1247 | unserialize_open_clib_file (serialize_main_t * m, char *file) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1248 | { |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 1249 | return serialize_open_clib_file_helper (m, file, /* is_read */ 1); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1250 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1251 | |
| 1252 | #endif /* CLIB_UNIX */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1253 | |
| 1254 | /* |
| 1255 | * fd.io coding-style-patch-verification: ON |
| 1256 | * |
| 1257 | * Local Variables: |
| 1258 | * eval: (c-set-style "gnu") |
| 1259 | * End: |
| 1260 | */ |