Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | :: # Copyright 2013, Big Switch Networks, Inc. |
| 2 | :: # |
| 3 | :: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | :: # the following special exception: |
| 5 | :: # |
| 6 | :: # LOXI Exception |
| 7 | :: # |
| 8 | :: # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | :: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | :: # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | :: # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | :: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | :: # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | :: # |
| 15 | :: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | :: # |
| 17 | :: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | :: # a copy of the EPL at: |
| 19 | :: # |
| 20 | :: # http://www.eclipse.org/legal/epl-v10.html |
| 21 | :: # |
| 22 | :: # Unless required by applicable law or agreed to in writing, software |
| 23 | :: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | :: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | :: # EPL for the specific language governing permissions and limitations |
| 26 | :: # under the EPL. |
| 27 | :: |
Rich Lane | d983aa5 | 2013-06-13 11:48:37 -0700 | [diff] [blame] | 28 | :: include('_copyright.c') |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 29 | |
| 30 | /**************************************************************** |
| 31 | * |
| 32 | * of_object.c |
| 33 | * |
| 34 | * These are the low level object constructor/destructor operators. |
| 35 | * |
| 36 | ****************************************************************/ |
| 37 | |
| 38 | #include "loci_log.h" |
| 39 | #include <loci/loci.h> |
| 40 | #include <loci/loci_validator.h> |
| 41 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Create a generic new object and possibly underlying wire buffer |
| 44 | * @param bytes The number of bytes to allocate in the underlying buffer |
| 45 | * |
| 46 | * If bytes <= 0, do not allocate a wire buffer. |
| 47 | * |
| 48 | * Note that this is an internal function. The class specific |
| 49 | * new functions should be called to properly initialize and track an |
| 50 | * OF object. |
| 51 | */ |
| 52 | |
| 53 | of_object_t * |
| 54 | of_object_new(int bytes) |
| 55 | { |
| 56 | of_object_t *obj; |
| 57 | |
Rich Lane | 671e772 | 2013-12-15 16:48:54 -0800 | [diff] [blame] | 58 | if ((obj = (of_object_t *)MALLOC(sizeof(*obj))) == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
Rich Lane | 671e772 | 2013-12-15 16:48:54 -0800 | [diff] [blame] | 61 | MEMSET(obj, 0, sizeof(*obj)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 62 | |
| 63 | if (bytes > 0) { |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 64 | if ((obj->wbuf = of_wire_buffer_new(bytes)) == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 65 | FREE(obj); |
| 66 | return NULL; |
| 67 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return obj; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * The delete function for LOCI objects |
| 75 | * |
| 76 | * @param obj Pointer to the object to be deleted |
| 77 | * |
| 78 | * This can be called on any LOCI object; it should not need to be |
| 79 | * overridden. |
| 80 | */ |
| 81 | |
| 82 | void |
| 83 | of_object_delete(of_object_t *obj) |
| 84 | { |
| 85 | if (obj == NULL) { |
| 86 | return; |
| 87 | } |
| 88 | |
Rich Lane | 3e43108 | 2014-04-03 16:21:30 -0700 | [diff] [blame] | 89 | if (obj->parent == NULL) { |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 90 | of_wire_buffer_free(obj->wbuf); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | FREE(obj); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Duplicate an object |
| 98 | * @param src The object to be duplicated |
| 99 | * @returns Pointer to the duplicate or NULL on error. Caller is responsible |
| 100 | * for freeing the returned object. |
| 101 | */ |
| 102 | |
| 103 | of_object_t * |
Rich Lane | cd6ef15 | 2013-12-15 16:42:18 -0800 | [diff] [blame] | 104 | of_object_dup(of_object_t *src) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 105 | { |
| 106 | of_object_t *dst; |
| 107 | of_object_init_f init_fn; |
| 108 | |
Rich Lane | 671e772 | 2013-12-15 16:48:54 -0800 | [diff] [blame] | 109 | if ((dst = (of_object_t *)MALLOC(sizeof(*dst))) == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | MEMSET(dst, 0, sizeof(*dst)); |
| 114 | |
| 115 | /* Allocate a minimal wire buffer assuming we will not write to it. */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 116 | if ((dst->wbuf = of_wire_buffer_new(src->length)) == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 117 | FREE(dst); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 121 | init_fn = of_object_init_map[src->object_id]; |
| 122 | init_fn(dst, src->version, src->length, 0); |
| 123 | |
| 124 | MEMCPY(OF_OBJECT_BUFFER_INDEX(dst, 0), |
| 125 | OF_OBJECT_BUFFER_INDEX(src, 0), |
| 126 | src->length); |
| 127 | |
| 128 | return dst; |
| 129 | } |
| 130 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Generic new from message call |
| 133 | */ |
| 134 | |
| 135 | of_object_t * |
| 136 | of_object_new_from_message(of_message_t msg, int len) |
| 137 | { |
| 138 | of_object_id_t object_id; |
| 139 | of_object_t *obj; |
| 140 | of_version_t version; |
| 141 | |
| 142 | version = of_message_version_get(msg); |
| 143 | if (!OF_VERSION_OKAY(version)) { |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | if (of_validate_message(msg, len) != 0) { |
| 148 | LOCI_LOG_ERROR("message validation failed\n"); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 152 | if ((obj = of_object_new(-1)) == NULL) { |
| 153 | return NULL; |
| 154 | } |
| 155 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 156 | if (of_object_buffer_bind(obj, OF_MESSAGE_TO_BUFFER(msg), len, |
| 157 | OF_MESSAGE_FREE_FUNCTION) < 0) { |
| 158 | FREE(obj); |
| 159 | return NULL; |
| 160 | } |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 161 | obj->version = version; |
| 162 | |
Rich Lane | 76f181e | 2014-03-04 23:23:36 -0800 | [diff] [blame] | 163 | of_header_wire_object_id_get(obj, &object_id); |
| 164 | of_object_init_map[object_id](obj, version, len, 0); |
| 165 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 166 | return obj; |
| 167 | } |
| 168 | |
| 169 | /** |
Rich Lane | c73680c | 2014-02-22 10:44:28 -0800 | [diff] [blame] | 170 | * Parse a message without allocating memory |
| 171 | * |
| 172 | * @param storage Pointer to an uninitialized of_object_storage_t |
| 173 | * @param buf Pointer to the buffer |
| 174 | * @param length Length of buf |
| 175 | * @returns Pointer to an initialized of_object_t |
| 176 | * |
| 177 | * The lifetime of the returned object is the minimum of the lifetimes of |
| 178 | * 'buf' and 'storage'. |
| 179 | */ |
| 180 | |
| 181 | of_object_t * |
| 182 | of_object_new_from_message_preallocated(of_object_storage_t *storage, |
| 183 | uint8_t *buf, int len) |
| 184 | { |
| 185 | of_object_t *obj = &storage->obj; |
| 186 | of_wire_buffer_t *wbuf = &storage->wbuf; |
| 187 | of_message_t msg = buf; |
| 188 | of_version_t version; |
| 189 | of_object_id_t object_id; |
| 190 | |
| 191 | memset(storage, 0, sizeof(*storage)); |
| 192 | |
| 193 | version = of_message_version_get(msg); |
| 194 | if (!OF_VERSION_OKAY(version)) { |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | if (of_validate_message(msg, len) != 0) { |
| 199 | LOCI_LOG_ERROR("message validation failed\n"); |
| 200 | return NULL; |
| 201 | } |
| 202 | |
Rich Lane | 76f181e | 2014-03-04 23:23:36 -0800 | [diff] [blame] | 203 | obj->version = version; |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 204 | obj->wbuf = wbuf; |
Rich Lane | c73680c | 2014-02-22 10:44:28 -0800 | [diff] [blame] | 205 | wbuf->buf = msg; |
| 206 | wbuf->alloc_bytes = len; |
| 207 | wbuf->current_bytes = len; |
| 208 | |
Rich Lane | 76f181e | 2014-03-04 23:23:36 -0800 | [diff] [blame] | 209 | of_header_wire_object_id_get(obj, &object_id); |
| 210 | of_object_init_map[object_id](obj, version, len, 0); |
| 211 | |
Rich Lane | c73680c | 2014-02-22 10:44:28 -0800 | [diff] [blame] | 212 | return obj; |
| 213 | } |
| 214 | |
| 215 | /** |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 216 | * Bind an existing buffer to an LOCI object |
| 217 | * |
| 218 | * @param obj Pointer to the object to be updated |
| 219 | * @param buf Pointer to the buffer to bind to obj |
| 220 | * @param bytes Length of buf |
| 221 | * @param buf_free An optional free function to be applied to |
| 222 | * buf on deallocation |
| 223 | * |
| 224 | * This can be called on any LOCI object; it should not need to be |
| 225 | * overridden. |
| 226 | */ |
| 227 | |
| 228 | int |
| 229 | of_object_buffer_bind(of_object_t *obj, uint8_t *buf, int bytes, |
| 230 | of_buffer_free_f buf_free) |
| 231 | { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 232 | of_wire_buffer_t *wbuf; |
| 233 | |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 234 | LOCI_ASSERT(buf != NULL); |
| 235 | LOCI_ASSERT(bytes > 0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 236 | |
| 237 | wbuf = of_wire_buffer_new_bind(buf, bytes, buf_free); |
| 238 | if (wbuf == NULL) { |
| 239 | return OF_ERROR_RESOURCE; |
| 240 | } |
| 241 | |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 242 | obj->wbuf = wbuf; |
| 243 | obj->obj_offset = 0; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 244 | obj->length = bytes; |
| 245 | |
| 246 | return OF_ERROR_NONE; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Connect a child to a parent at the wire buffer level |
| 251 | * |
| 252 | * @param parent The top level object to bind to |
| 253 | * @param child The sub-object connecting to the parent |
| 254 | * @param offset The offset at which to attach the child RELATIVE |
| 255 | * TO THE PARENT in the buffer |
| 256 | * @param bytes The amount of the buffer dedicated to the child; see below |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 257 | * |
| 258 | * This is used for 'get' accessors for composite types as well as |
| 259 | * iterator functions for lists, both read (first/next) and write |
| 260 | * (append_init, append_advance). |
| 261 | * |
| 262 | * Connect a child object to a parent by setting up the child's |
| 263 | * wire_object to point to the parent's underlying buffer. The value |
| 264 | * of the parameter bytes is important in determining how the child |
| 265 | * is initialized: |
| 266 | * @li If bytes <= 0, the length and type of the child are not modified; |
| 267 | * no additional space is added to the buffer. |
| 268 | * @li If bytes > 0, the current wire buffer is grown to |
| 269 | * accomodate this many bytes. This is to support append operations. |
| 270 | * |
| 271 | * If an error is returned, future references to the child object |
| 272 | * (until it is reinitialized) are undefined. |
| 273 | */ |
| 274 | static void |
| 275 | object_child_attach(of_object_t *parent, of_object_t *child, |
| 276 | int offset, int bytes) |
| 277 | { |
Rich Lane | d1fe697 | 2014-06-12 14:53:24 -0700 | [diff] [blame] | 278 | of_object_attach(parent, child, offset, bytes); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * bytes determines if this is a read or write setup. |
| 282 | * If > 0, grow the buffer to accomodate the space |
| 283 | * Otherwise do nothing |
| 284 | */ |
| 285 | if (bytes > 0) { /* Set internal length, request buffer space */ |
| 286 | int tot_bytes; /* Total bytes to request for buffer if updated */ |
| 287 | |
| 288 | /* Set up space for the child in the parent's buffer */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 289 | tot_bytes = parent->obj_offset + offset + bytes; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 290 | |
Rich Lane | d1fe697 | 2014-06-12 14:53:24 -0700 | [diff] [blame] | 291 | of_wire_buffer_grow(parent->wbuf, tot_bytes); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 292 | } |
| 293 | /* if bytes == 0 don't do anything */ |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Check for room in an object's wire buffer. |
| 298 | * @param obj The object being checked |
| 299 | * @param new_len The desired length |
| 300 | * @return Boolean |
| 301 | */ |
| 302 | |
| 303 | int |
| 304 | of_object_can_grow(of_object_t *obj, int new_len) |
| 305 | { |
| 306 | return OF_OBJECT_ABSOLUTE_OFFSET(obj, new_len) <= |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 307 | WBUF_ALLOC_BYTES(obj->wbuf); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Set the xid of a message object |
| 312 | * @param obj The object being accessed |
| 313 | * @param xid The xid value to store in the wire buffer |
| 314 | * @return OF_ERROR_ |
| 315 | * Since the XID is common across all versions, this is used |
| 316 | * for all XID accessors. |
| 317 | */ |
| 318 | |
| 319 | int |
| 320 | of_object_xid_set(of_object_t *obj, uint32_t xid) |
| 321 | { |
| 322 | of_wire_buffer_t *wbuf; |
| 323 | |
| 324 | if ((wbuf = OF_OBJECT_TO_WBUF(obj)) == NULL) { |
| 325 | return OF_ERROR_PARAM; |
| 326 | } |
| 327 | of_wire_buffer_u32_set(wbuf, |
| 328 | OF_OBJECT_ABSOLUTE_OFFSET(obj, OF_MESSAGE_XID_OFFSET), xid); |
| 329 | return OF_ERROR_NONE; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get the xid of a message object |
| 334 | * @param obj The object being accessed |
| 335 | * @param xid Pointer to where to store the xid value |
| 336 | * @return OF_ERROR_ |
| 337 | * Since the XID is common across all versions, this is used |
| 338 | * for all XID accessors. |
| 339 | */ |
| 340 | |
| 341 | int |
| 342 | of_object_xid_get(of_object_t *obj, uint32_t *xid) |
| 343 | { |
| 344 | of_wire_buffer_t *wbuf; |
| 345 | |
| 346 | if ((wbuf = OF_OBJECT_TO_WBUF(obj)) == NULL) { |
| 347 | return OF_ERROR_PARAM; |
| 348 | } |
| 349 | of_wire_buffer_u32_get(wbuf, |
| 350 | OF_OBJECT_ABSOLUTE_OFFSET(obj, OF_MESSAGE_XID_OFFSET), xid); |
| 351 | return OF_ERROR_NONE; |
| 352 | } |
| 353 | |
| 354 | /**************************************************************** |
| 355 | * |
| 356 | * Generic list operation implementations |
| 357 | * |
| 358 | ****************************************************************/ |
| 359 | |
| 360 | /** |
| 361 | * Set up a child for appending to a parent list |
| 362 | * @param parent The parent; must be a list object |
| 363 | * @param child The child object; must be of type list element |
| 364 | * @return OF_ERROR_ |
| 365 | * |
| 366 | * Attaches the wire buffer of the parent to the child by pointing |
| 367 | * the child to the end of the parent. |
| 368 | * |
| 369 | * Set the wire length and type from the child. |
| 370 | * Update the parent length adding the current child length |
| 371 | * |
| 372 | * After calling this function, the child object may be updated |
| 373 | * resulting in changes to the parent's wire buffer |
| 374 | * |
| 375 | */ |
| 376 | |
| 377 | int |
| 378 | of_list_append_bind(of_object_t *parent, of_object_t *child) |
| 379 | { |
| 380 | if (parent == NULL || child == NULL || |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 381 | parent->wbuf == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 382 | return OF_ERROR_PARAM; |
| 383 | } |
| 384 | |
| 385 | if (!of_object_can_grow(parent, parent->length + child->length)) { |
| 386 | return OF_ERROR_RESOURCE; |
| 387 | } |
| 388 | |
| 389 | object_child_attach(parent, child, parent->length, |
| 390 | child->length); |
| 391 | |
| 392 | /* Update the wire length and type if needed */ |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 393 | of_object_wire_length_set(child, child->length); |
| 394 | of_object_wire_type_set(child); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 395 | |
| 396 | /* Update the parent's length */ |
| 397 | of_object_parent_length_update(parent, child->length); |
| 398 | |
| 399 | OF_LENGTH_CHECK_ASSERT(parent); |
| 400 | |
| 401 | return OF_ERROR_NONE; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Generic atomic list append operation |
| 406 | * @param list The list to which an item is being appended |
| 407 | * @param item THe item to append to the list |
| 408 | * |
| 409 | * The contents of the item are copied to the end of the list. |
| 410 | * Currently assumes the list is at the end of its parent. |
| 411 | */ |
| 412 | int |
| 413 | of_list_append(of_object_t *list, of_object_t *item) |
| 414 | { |
| 415 | int new_len; |
| 416 | |
| 417 | new_len = list->length + item->length; |
| 418 | |
| 419 | if (!of_object_can_grow(list, new_len)) { |
| 420 | return OF_ERROR_RESOURCE; |
| 421 | } |
| 422 | |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 423 | of_wire_buffer_grow(list->wbuf, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 424 | OF_OBJECT_ABSOLUTE_OFFSET(list, new_len)); |
| 425 | |
| 426 | MEMCPY(OF_OBJECT_BUFFER_INDEX(list, list->length), |
| 427 | OF_OBJECT_BUFFER_INDEX(item, 0), item->length); |
| 428 | |
| 429 | /* Update the list's length */ |
| 430 | of_object_parent_length_update(list, item->length); |
| 431 | |
| 432 | OF_LENGTH_CHECK_ASSERT(list); |
| 433 | |
| 434 | return OF_ERROR_NONE; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Generic list first function |
| 439 | * @param parent The parent; must be a list object |
| 440 | * @param child The child object; must be of type list element |
| 441 | * @return OF_ERROR_RANGE if list is empty |
| 442 | * @return OF_ERROR_ |
| 443 | * |
| 444 | * Sets up the child to point to the first element in the list |
| 445 | * |
| 446 | * Child init must be called before this is called. |
| 447 | * |
| 448 | * @note TREAT AS PRIVATE |
| 449 | * Does not fully initialized object |
| 450 | */ |
| 451 | int |
| 452 | of_list_first(of_object_t *parent, of_object_t *child) |
| 453 | { |
| 454 | if (parent->length == 0) { /* Empty list */ |
| 455 | return OF_ERROR_RANGE; |
| 456 | } |
| 457 | |
Rich Lane | b5b697d | 2014-06-25 22:18:26 -0700 | [diff] [blame] | 458 | of_object_attach(parent, child, 0, child->length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 459 | |
| 460 | return OF_ERROR_NONE; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Return boolean indicating if child is pointing to last entry in parent |
| 465 | * @param parent The parent; must be a list object |
| 466 | * @param child The child object; must be of type list element |
| 467 | * @return OF_ERROR_RANGE if list is empty |
| 468 | * @return OF_ERROR_ |
| 469 | * |
| 470 | */ |
| 471 | static int |
| 472 | of_list_is_last(of_object_t *parent, of_object_t *child) |
| 473 | { |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 474 | if (child->obj_offset + child->length >= |
| 475 | parent->obj_offset + parent->length) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Generic list next function |
| 484 | * @param parent The parent; must be a list object |
| 485 | * @param child The child object; must be of type list element |
| 486 | * @return OF_ERROR_RANGE if at end of list |
| 487 | * @return OF_ERROR_ |
| 488 | * |
| 489 | * Advances the child to point to the subsequent element in the list. |
| 490 | * The wire buffer object must not have been modified since the |
| 491 | * previous call to _first or _next. |
| 492 | * |
| 493 | * @note TREAT AS PRIVATE |
| 494 | * Does not fully initialized object |
| 495 | */ |
| 496 | int |
| 497 | of_list_next(of_object_t *parent, of_object_t *child) |
| 498 | { |
| 499 | int offset; |
| 500 | |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 501 | LOCI_ASSERT(child->length > 0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 502 | |
| 503 | /* Get offset of parent */ |
| 504 | if (of_list_is_last(parent, child)) { |
| 505 | return OF_ERROR_RANGE; /* We were on the last object */ |
| 506 | } |
| 507 | |
| 508 | /* Offset is relative to parent start */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 509 | offset = (child->obj_offset - parent->obj_offset) + |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 510 | child->length; |
Rich Lane | b5b697d | 2014-06-25 22:18:26 -0700 | [diff] [blame] | 511 | of_object_attach(parent, child, offset, child->length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 512 | |
| 513 | return OF_ERROR_NONE; |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | of_object_wire_buffer_steal(of_object_t *obj, uint8_t **buffer) |
| 518 | { |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 519 | LOCI_ASSERT(obj != NULL); |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 520 | of_wire_buffer_steal(obj->wbuf, buffer); |
| 521 | obj->wbuf = NULL; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Rich Lane | 2a87913 | 2015-01-20 15:41:07 -0800 | [diff] [blame] | 524 | #define _MAX_PARENT_ITERATIONS 8 |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 525 | /** |
| 526 | * Iteratively update parent lengths thru hierarchy |
| 527 | * @param obj The object whose length is being updated |
| 528 | * @param delta The difference between the current and new lengths |
| 529 | * |
| 530 | * Note that this includes updating the object itself. It will |
| 531 | * iterate thru parents. |
| 532 | * |
| 533 | * Assumes delta > 0. |
| 534 | */ |
| 535 | void |
| 536 | of_object_parent_length_update(of_object_t *obj, int delta) |
| 537 | { |
| 538 | #ifndef NDEBUG |
| 539 | int count = 0; |
| 540 | of_wire_buffer_t *wbuf; /* For debug asserts only */ |
| 541 | #endif |
| 542 | |
| 543 | while (obj != NULL) { |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 544 | LOCI_ASSERT(count++ < _MAX_PARENT_ITERATIONS); |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 545 | obj->length += delta; |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 546 | of_object_wire_length_set(obj, obj->length); |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 547 | #ifndef NDEBUG |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 548 | wbuf = obj->wbuf; |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 549 | #endif |
| 550 | |
| 551 | /* Asserts for wire length checking */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 552 | LOCI_ASSERT(obj->length + obj->obj_offset <= |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 553 | WBUF_CURRENT_BYTES(wbuf)); |
| 554 | if (obj->parent == NULL) { |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 555 | LOCI_ASSERT(obj->length + obj->obj_offset == |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 556 | WBUF_CURRENT_BYTES(wbuf)); |
| 557 | } |
| 558 | |
| 559 | obj = obj->parent; |
| 560 | } |
| 561 | } |
| 562 | |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 563 | /** |
| 564 | * Use the type/length from the wire buffer and init the object |
| 565 | * @param obj The object being initialized |
| 566 | * @param base_object_id If > 0, this indicates the base object |
| 567 | * @param max_len If > 0, the max length to expect for the obj |
| 568 | * type for inheritance checking |
| 569 | * @return OF_ERROR_ |
| 570 | * |
| 571 | * Used for inheritance type objects such as actions and OXMs |
| 572 | * The type is checked and if valid, the object is initialized. |
| 573 | * Then the length is taken from the buffer. |
| 574 | * |
| 575 | * Note that the object version must already be properly set. |
| 576 | */ |
| 577 | int |
| 578 | of_object_wire_init(of_object_t *obj, of_object_id_t base_object_id, |
| 579 | int max_len) |
| 580 | { |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 581 | if (loci_class_metadata[obj->object_id].wire_type_get != NULL) { |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 582 | of_object_id_t id; |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 583 | loci_class_metadata[obj->object_id].wire_type_get(obj, &id); |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 584 | obj->object_id = id; |
| 585 | /* Call the init function for this object type; do not push to wire */ |
| 586 | of_object_init_map[id]((of_object_t *)(obj), obj->version, -1, 0); |
| 587 | } |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 588 | if (loci_class_metadata[obj->object_id].wire_length_get != NULL) { |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 589 | int length; |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 590 | loci_class_metadata[obj->object_id].wire_length_get(obj, &length); |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 591 | if (length < 0 || (max_len > 0 && length > max_len)) { |
| 592 | return OF_ERROR_PARSE; |
| 593 | } |
| 594 | obj->length = length; |
| 595 | } else { |
| 596 | /* @fixme Does this cover everything else? */ |
| 597 | obj->length = of_object_fixed_len[obj->version][base_object_id]; |
| 598 | } |
| 599 | |
| 600 | return OF_ERROR_NONE; |
| 601 | } |
Rich Lane | 9f00eb3 | 2015-03-03 15:49:07 -0800 | [diff] [blame] | 602 | |
| 603 | /* |
| 604 | * Truncate an object to its initial length. |
| 605 | * |
| 606 | * This allows the caller to reuse a single allocated object even if |
| 607 | * it has been appended to. |
| 608 | */ |
| 609 | void |
| 610 | of_object_truncate(of_object_t *obj) |
| 611 | { |
| 612 | of_object_init_map[obj->object_id](obj, obj->version, -1, 0); |
| 613 | obj->wbuf->current_bytes = obj->length; |
| 614 | } |