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 |
| 257 | * @param inc_ref_count Should the ref count of the parent be incremented |
| 258 | * |
| 259 | * This is used for 'get' accessors for composite types as well as |
| 260 | * iterator functions for lists, both read (first/next) and write |
| 261 | * (append_init, append_advance). |
| 262 | * |
| 263 | * Connect a child object to a parent by setting up the child's |
| 264 | * wire_object to point to the parent's underlying buffer. The value |
| 265 | * of the parameter bytes is important in determining how the child |
| 266 | * is initialized: |
| 267 | * @li If bytes <= 0, the length and type of the child are not modified; |
| 268 | * no additional space is added to the buffer. |
| 269 | * @li If bytes > 0, the current wire buffer is grown to |
| 270 | * accomodate this many bytes. This is to support append operations. |
| 271 | * |
| 272 | * If an error is returned, future references to the child object |
| 273 | * (until it is reinitialized) are undefined. |
| 274 | */ |
| 275 | static void |
| 276 | object_child_attach(of_object_t *parent, of_object_t *child, |
| 277 | int offset, int bytes) |
| 278 | { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 279 | of_wire_buffer_t *wbuf; /* Pointer to common wire buffer manager */ |
| 280 | |
| 281 | child->parent = parent; |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 282 | wbuf = parent->wbuf; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 283 | |
| 284 | /* Set up the child's wire buf to point to same as parent */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 285 | child->wbuf = wbuf; |
| 286 | child->obj_offset = parent->obj_offset + offset; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 287 | |
| 288 | /* |
| 289 | * bytes determines if this is a read or write setup. |
| 290 | * If > 0, grow the buffer to accomodate the space |
| 291 | * Otherwise do nothing |
| 292 | */ |
| 293 | if (bytes > 0) { /* Set internal length, request buffer space */ |
| 294 | int tot_bytes; /* Total bytes to request for buffer if updated */ |
| 295 | |
| 296 | /* Set up space for the child in the parent's buffer */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 297 | tot_bytes = parent->obj_offset + offset + bytes; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 298 | |
| 299 | of_wire_buffer_grow(wbuf, tot_bytes); |
| 300 | child->length = bytes; |
| 301 | } |
| 302 | /* if bytes == 0 don't do anything */ |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Check for room in an object's wire buffer. |
| 307 | * @param obj The object being checked |
| 308 | * @param new_len The desired length |
| 309 | * @return Boolean |
| 310 | */ |
| 311 | |
| 312 | int |
| 313 | of_object_can_grow(of_object_t *obj, int new_len) |
| 314 | { |
| 315 | return OF_OBJECT_ABSOLUTE_OFFSET(obj, new_len) <= |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 316 | WBUF_ALLOC_BYTES(obj->wbuf); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Set the xid of a message object |
| 321 | * @param obj The object being accessed |
| 322 | * @param xid The xid value to store in the wire buffer |
| 323 | * @return OF_ERROR_ |
| 324 | * Since the XID is common across all versions, this is used |
| 325 | * for all XID accessors. |
| 326 | */ |
| 327 | |
| 328 | int |
| 329 | of_object_xid_set(of_object_t *obj, uint32_t xid) |
| 330 | { |
| 331 | of_wire_buffer_t *wbuf; |
| 332 | |
| 333 | if ((wbuf = OF_OBJECT_TO_WBUF(obj)) == NULL) { |
| 334 | return OF_ERROR_PARAM; |
| 335 | } |
| 336 | of_wire_buffer_u32_set(wbuf, |
| 337 | OF_OBJECT_ABSOLUTE_OFFSET(obj, OF_MESSAGE_XID_OFFSET), xid); |
| 338 | return OF_ERROR_NONE; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Get the xid of a message object |
| 343 | * @param obj The object being accessed |
| 344 | * @param xid Pointer to where to store the xid value |
| 345 | * @return OF_ERROR_ |
| 346 | * Since the XID is common across all versions, this is used |
| 347 | * for all XID accessors. |
| 348 | */ |
| 349 | |
| 350 | int |
| 351 | of_object_xid_get(of_object_t *obj, uint32_t *xid) |
| 352 | { |
| 353 | of_wire_buffer_t *wbuf; |
| 354 | |
| 355 | if ((wbuf = OF_OBJECT_TO_WBUF(obj)) == NULL) { |
| 356 | return OF_ERROR_PARAM; |
| 357 | } |
| 358 | of_wire_buffer_u32_get(wbuf, |
| 359 | OF_OBJECT_ABSOLUTE_OFFSET(obj, OF_MESSAGE_XID_OFFSET), xid); |
| 360 | return OF_ERROR_NONE; |
| 361 | } |
| 362 | |
| 363 | /**************************************************************** |
| 364 | * |
| 365 | * Generic list operation implementations |
| 366 | * |
| 367 | ****************************************************************/ |
| 368 | |
| 369 | /** |
| 370 | * Set up a child for appending to a parent list |
| 371 | * @param parent The parent; must be a list object |
| 372 | * @param child The child object; must be of type list element |
| 373 | * @return OF_ERROR_ |
| 374 | * |
| 375 | * Attaches the wire buffer of the parent to the child by pointing |
| 376 | * the child to the end of the parent. |
| 377 | * |
| 378 | * Set the wire length and type from the child. |
| 379 | * Update the parent length adding the current child length |
| 380 | * |
| 381 | * After calling this function, the child object may be updated |
| 382 | * resulting in changes to the parent's wire buffer |
| 383 | * |
| 384 | */ |
| 385 | |
| 386 | int |
| 387 | of_list_append_bind(of_object_t *parent, of_object_t *child) |
| 388 | { |
| 389 | if (parent == NULL || child == NULL || |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 390 | parent->wbuf == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 391 | return OF_ERROR_PARAM; |
| 392 | } |
| 393 | |
| 394 | if (!of_object_can_grow(parent, parent->length + child->length)) { |
| 395 | return OF_ERROR_RESOURCE; |
| 396 | } |
| 397 | |
| 398 | object_child_attach(parent, child, parent->length, |
| 399 | child->length); |
| 400 | |
| 401 | /* Update the wire length and type if needed */ |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 402 | of_object_wire_length_set(child, child->length); |
| 403 | of_object_wire_type_set(child); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 404 | |
| 405 | /* Update the parent's length */ |
| 406 | of_object_parent_length_update(parent, child->length); |
| 407 | |
| 408 | OF_LENGTH_CHECK_ASSERT(parent); |
| 409 | |
| 410 | return OF_ERROR_NONE; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Generic atomic list append operation |
| 415 | * @param list The list to which an item is being appended |
| 416 | * @param item THe item to append to the list |
| 417 | * |
| 418 | * The contents of the item are copied to the end of the list. |
| 419 | * Currently assumes the list is at the end of its parent. |
| 420 | */ |
| 421 | int |
| 422 | of_list_append(of_object_t *list, of_object_t *item) |
| 423 | { |
| 424 | int new_len; |
| 425 | |
| 426 | new_len = list->length + item->length; |
| 427 | |
| 428 | if (!of_object_can_grow(list, new_len)) { |
| 429 | return OF_ERROR_RESOURCE; |
| 430 | } |
| 431 | |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 432 | of_wire_buffer_grow(list->wbuf, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 433 | OF_OBJECT_ABSOLUTE_OFFSET(list, new_len)); |
| 434 | |
| 435 | MEMCPY(OF_OBJECT_BUFFER_INDEX(list, list->length), |
| 436 | OF_OBJECT_BUFFER_INDEX(item, 0), item->length); |
| 437 | |
| 438 | /* Update the list's length */ |
| 439 | of_object_parent_length_update(list, item->length); |
| 440 | |
| 441 | OF_LENGTH_CHECK_ASSERT(list); |
| 442 | |
| 443 | return OF_ERROR_NONE; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Generic list first function |
| 448 | * @param parent The parent; must be a list object |
| 449 | * @param child The child object; must be of type list element |
| 450 | * @return OF_ERROR_RANGE if list is empty |
| 451 | * @return OF_ERROR_ |
| 452 | * |
| 453 | * Sets up the child to point to the first element in the list |
| 454 | * |
| 455 | * Child init must be called before this is called. |
| 456 | * |
| 457 | * @note TREAT AS PRIVATE |
| 458 | * Does not fully initialized object |
| 459 | */ |
| 460 | int |
| 461 | of_list_first(of_object_t *parent, of_object_t *child) |
| 462 | { |
| 463 | if (parent->length == 0) { /* Empty list */ |
| 464 | return OF_ERROR_RANGE; |
| 465 | } |
| 466 | |
| 467 | object_child_attach(parent, child, 0, 0); |
| 468 | |
| 469 | return OF_ERROR_NONE; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Return boolean indicating if child is pointing to last entry in parent |
| 474 | * @param parent The parent; must be a list object |
| 475 | * @param child The child object; must be of type list element |
| 476 | * @return OF_ERROR_RANGE if list is empty |
| 477 | * @return OF_ERROR_ |
| 478 | * |
| 479 | */ |
| 480 | static int |
| 481 | of_list_is_last(of_object_t *parent, of_object_t *child) |
| 482 | { |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 483 | if (child->obj_offset + child->length >= |
| 484 | parent->obj_offset + parent->length) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 485 | return 1; |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Generic list next function |
| 493 | * @param parent The parent; must be a list object |
| 494 | * @param child The child object; must be of type list element |
| 495 | * @return OF_ERROR_RANGE if at end of list |
| 496 | * @return OF_ERROR_ |
| 497 | * |
| 498 | * Advances the child to point to the subsequent element in the list. |
| 499 | * The wire buffer object must not have been modified since the |
| 500 | * previous call to _first or _next. |
| 501 | * |
| 502 | * @note TREAT AS PRIVATE |
| 503 | * Does not fully initialized object |
| 504 | */ |
| 505 | int |
| 506 | of_list_next(of_object_t *parent, of_object_t *child) |
| 507 | { |
| 508 | int offset; |
| 509 | |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 510 | LOCI_ASSERT(child->length > 0); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 511 | |
| 512 | /* Get offset of parent */ |
| 513 | if (of_list_is_last(parent, child)) { |
| 514 | return OF_ERROR_RANGE; /* We were on the last object */ |
| 515 | } |
| 516 | |
| 517 | /* Offset is relative to parent start */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 518 | offset = (child->obj_offset - parent->obj_offset) + |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 519 | child->length; |
| 520 | object_child_attach(parent, child, offset, 0); |
| 521 | |
| 522 | return OF_ERROR_NONE; |
| 523 | } |
| 524 | |
| 525 | void |
| 526 | of_object_wire_buffer_steal(of_object_t *obj, uint8_t **buffer) |
| 527 | { |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 528 | LOCI_ASSERT(obj != NULL); |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 529 | of_wire_buffer_steal(obj->wbuf, buffer); |
| 530 | obj->wbuf = NULL; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 533 | #define _MAX_PARENT_ITERATIONS 4 |
| 534 | /** |
| 535 | * Iteratively update parent lengths thru hierarchy |
| 536 | * @param obj The object whose length is being updated |
| 537 | * @param delta The difference between the current and new lengths |
| 538 | * |
| 539 | * Note that this includes updating the object itself. It will |
| 540 | * iterate thru parents. |
| 541 | * |
| 542 | * Assumes delta > 0. |
| 543 | */ |
| 544 | void |
| 545 | of_object_parent_length_update(of_object_t *obj, int delta) |
| 546 | { |
| 547 | #ifndef NDEBUG |
| 548 | int count = 0; |
| 549 | of_wire_buffer_t *wbuf; /* For debug asserts only */ |
| 550 | #endif |
| 551 | |
| 552 | while (obj != NULL) { |
Rich Lane | e57f043 | 2014-02-19 10:31:53 -0800 | [diff] [blame] | 553 | LOCI_ASSERT(count++ < _MAX_PARENT_ITERATIONS); |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 554 | obj->length += delta; |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 555 | of_object_wire_length_set(obj, obj->length); |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 556 | #ifndef NDEBUG |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 557 | wbuf = obj->wbuf; |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 558 | #endif |
| 559 | |
| 560 | /* Asserts for wire length checking */ |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 561 | LOCI_ASSERT(obj->length + obj->obj_offset <= |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 562 | WBUF_CURRENT_BYTES(wbuf)); |
| 563 | if (obj->parent == NULL) { |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 564 | LOCI_ASSERT(obj->length + obj->obj_offset == |
Rich Lane | 50aa594 | 2013-12-15 16:20:38 -0800 | [diff] [blame] | 565 | WBUF_CURRENT_BYTES(wbuf)); |
| 566 | } |
| 567 | |
| 568 | obj = obj->parent; |
| 569 | } |
| 570 | } |
| 571 | |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 572 | /** |
| 573 | * Use the type/length from the wire buffer and init the object |
| 574 | * @param obj The object being initialized |
| 575 | * @param base_object_id If > 0, this indicates the base object |
| 576 | * @param max_len If > 0, the max length to expect for the obj |
| 577 | * type for inheritance checking |
| 578 | * @return OF_ERROR_ |
| 579 | * |
| 580 | * Used for inheritance type objects such as actions and OXMs |
| 581 | * The type is checked and if valid, the object is initialized. |
| 582 | * Then the length is taken from the buffer. |
| 583 | * |
| 584 | * Note that the object version must already be properly set. |
| 585 | */ |
| 586 | int |
| 587 | of_object_wire_init(of_object_t *obj, of_object_id_t base_object_id, |
| 588 | int max_len) |
| 589 | { |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 590 | if (loci_class_metadata[obj->object_id].wire_type_get != NULL) { |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 591 | of_object_id_t id; |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 592 | loci_class_metadata[obj->object_id].wire_type_get(obj, &id); |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 593 | if (!of_wire_id_valid(id, base_object_id)) { |
| 594 | return OF_ERROR_PARSE; |
| 595 | } |
| 596 | obj->object_id = id; |
| 597 | /* Call the init function for this object type; do not push to wire */ |
| 598 | of_object_init_map[id]((of_object_t *)(obj), obj->version, -1, 0); |
| 599 | } |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 600 | if (loci_class_metadata[obj->object_id].wire_length_get != NULL) { |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 601 | int length; |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 602 | loci_class_metadata[obj->object_id].wire_length_get(obj, &length); |
Rich Lane | c0e20ff | 2013-12-15 23:40:31 -0800 | [diff] [blame] | 603 | if (length < 0 || (max_len > 0 && length > max_len)) { |
| 604 | return OF_ERROR_PARSE; |
| 605 | } |
| 606 | obj->length = length; |
| 607 | } else { |
| 608 | /* @fixme Does this cover everything else? */ |
| 609 | obj->length = of_object_fixed_len[obj->version][base_object_id]; |
| 610 | } |
| 611 | |
| 612 | return OF_ERROR_NONE; |
| 613 | } |
| 614 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 615 | /* |
| 616 | * Set member: |
| 617 | * get_wbuf_extent |
| 618 | * find offset of start of member |
| 619 | * if offset is at wbuf_extent (append new data) |
| 620 | * copy data at extent |
| 621 | * update parent length |
| 622 | * else |
| 623 | * find length of current entry |
| 624 | * move from end of current to extent to create (or remove) space |
| 625 | * copy data to offset |
| 626 | * update my length -- NEED LOCAL INFO TO DO THIS for some cases |
| 627 | */ |
| 628 | |
| 629 | /* Also need: get offset of member for all combinations */ |
| 630 | /* Also need: get length of member for all combinations */ |
| 631 | #if 0 |
| 632 | /** |
| 633 | * Append the wire buffer data from src to the end of dst's wire buffer |
| 634 | */ |
| 635 | int |
| 636 | of_object_append_buffer(of_object_t *dst, of_object_t *src) |
| 637 | { |
| 638 | of_wire_buffer_t *s_wbuf, *d_wbuf; |
| 639 | int orig_len, dst_offset, src_offset; |
| 640 | |
| 641 | d_wbuf = OF_OBJECT_TO_WBUF(dst); |
| 642 | s_wbuf = OF_OBJECT_TO_WBUF(src); |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 643 | dst_offset = dst->obj_offset + dst_length; |
| 644 | src_offset = src->obj_offset; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 645 | OF_WIRE_BUFFER_INIT_CHECK(d_wbuf, dst_offset + src->length); |
| 646 | MEMCPY(OF_WBUF_BUFFER_POINTER(d_wbuf, dst_offset), |
| 647 | OF_WBUF_BUFFER_POINTER(s_wbuf, 0), src->length); |
| 648 | |
| 649 | orig_len = dst->length; |
| 650 | dst->length += src->length; |
| 651 | |
| 652 | return OF_ERROR_NONE; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Set the length of the actions object in a packet_in object |
| 657 | */ |
| 658 | |
| 659 | int |
| 660 | of_packet_out_actions_length_set(of_packet_t *obj, int len) |
| 661 | { |
| 662 | if (obj == NULL || obj->object_id != OF_PACKET_IN || |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 663 | obj->wbuf == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 664 | return OF_ERROR_PARAM; |
| 665 | } |
| 666 | |
| 667 | obj->actions_len_set(obj, len); |
| 668 | } |
| 669 | |
| 670 | int |
| 671 | _packet_out_data_offset_get(of_packet_t *obj) |
| 672 | { |
| 673 | if (obj == NULL || obj->object_id != OF_PACKET_IN || |
Rich Lane | cdd542d | 2014-04-03 16:13:12 -0700 | [diff] [blame] | 674 | obj->wbuf == NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 675 | return -1; |
| 676 | } |
| 677 | |
| 678 | return OF_PACKET_OUT_FIXED_LENGTH + _packet_out_actions_length_get(obj); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | /** |
| 683 | * Simple length derivation function |
| 684 | * |
| 685 | * Most variable length fields are alone at the end of a structure. |
| 686 | * Their length is a simple calculation, just the total length of |
| 687 | * the parent minus the length of the non-variable part of the |
| 688 | * parent's class type. |
| 689 | * |
| 690 | * @param parent The parent object |
| 691 | * @param length (out) Where to store the length of the final |
| 692 | * variable length member |
| 693 | */ |
| 694 | int |
| 695 | of_object_simple_length_derive(of_object_t *obj, int *length) |
| 696 | { |
| 697 | |
| 698 | } |
| 699 | #endif |