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 | :: |
| 28 | /* Copyright 2013, Big Switch Networks, Inc. */ |
| 29 | |
| 30 | #if !defined(_OF_WIRE_BUF_H_) |
| 31 | #define _OF_WIRE_BUF_H_ |
| 32 | |
| 33 | #include <string.h> |
| 34 | #include <loci/loci_base.h> |
| 35 | #include <loci/of_object.h> |
| 36 | #include <loci/of_match.h> |
| 37 | #include <loci/of_buffer.h> |
| 38 | |
| 39 | /**************************************************************** |
| 40 | * |
| 41 | * Wire buffer declaration, constructor, data alloc, delete |
| 42 | * |
| 43 | ****************************************************************/ |
| 44 | |
| 45 | /* Maximum length of an OpenFlow message. All wire buffers allocated for |
| 46 | * new objects (that don't come from a message) are this length to avoid |
| 47 | * needing to grow the buffers. */ |
| 48 | #define OF_WIRE_BUFFER_MAX_LENGTH 65535 |
| 49 | |
| 50 | /** |
| 51 | * Buffer management structure |
| 52 | */ |
| 53 | typedef struct of_wire_buffer_s { |
| 54 | /** Pointer to a monolithic data buffer */ |
| 55 | uint8_t *buf; |
| 56 | |
| 57 | /** Length of buffer actually allocated */ |
| 58 | int alloc_bytes; |
| 59 | /** Current extent actually used */ |
| 60 | int current_bytes; |
| 61 | /** If not NULL, use this to dealloc buf */ |
| 62 | of_buffer_free_f free; |
| 63 | } of_wire_buffer_t; |
| 64 | |
| 65 | /** |
| 66 | * Decouples object from underlying wire buffer |
| 67 | * |
| 68 | * Called a 'slice' in some places. |
| 69 | */ |
| 70 | typedef struct of_wire_object_s { |
| 71 | /** A pointer to the underlying buffer's management structure. */ |
| 72 | of_wire_buffer_t *wbuf; |
| 73 | /** The start offset for this object relative to the start of the |
| 74 | * underlying buffer */ |
| 75 | int obj_offset; |
| 76 | /* Boolean, whether the object owns the wire buffer. */ |
| 77 | char owned; |
| 78 | } of_wire_object_t; |
| 79 | |
| 80 | #define WBUF_BUF(wbuf) (wbuf)->buf |
| 81 | #define WBUF_ALLOC_BYTES(wbuf) (wbuf)->alloc_bytes |
| 82 | #define WBUF_CURRENT_BYTES(wbuf) (wbuf)->current_bytes |
| 83 | |
| 84 | /** |
| 85 | * For read access, throw an error code if current buffer |
| 86 | * is not big enough. |
| 87 | * @param wbuf Pointer to an of_wire_buffer_t structure |
| 88 | * @param offset The extent of the buffer required |
| 89 | */ |
| 90 | #define OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset) \ |
| 91 | ASSERT(((wbuf) != NULL) && (WBUF_BUF(wbuf) != NULL) && \ |
| 92 | (offset > 0) && (WBUF_CURRENT_BYTES(wbuf) >= offset)) |
| 93 | |
| 94 | /* |
| 95 | * Index a wire buffer |
| 96 | * Index a wire object (from obj_offset) |
| 97 | * Index a LOCI object |
| 98 | */ |
| 99 | |
| 100 | /** |
| 101 | * Return a pointer to a particular offset in a wire buffer's data |
| 102 | * @param wbuf Pointer to an of_wire_buffer_t structure |
| 103 | * @param offset The location to reference |
| 104 | */ |
| 105 | #define OF_WIRE_BUFFER_INDEX(wbuf, offset) (&((WBUF_BUF(wbuf))[offset])) |
| 106 | |
| 107 | /** |
| 108 | * Return a pointer to a particular offset in the underlying buffer |
| 109 | * associated with a wire object |
| 110 | * @param wobj Pointer to an of_wire_object_t structure |
| 111 | * @param offset The location to reference relative to the start of the object |
| 112 | */ |
| 113 | #define OF_WIRE_OBJECT_INDEX(wobj, offset) \ |
| 114 | OF_WIRE_BUFFER_INDEX((wobj)->wbuf, (offset) + (wobj)->obj_offset) |
| 115 | |
| 116 | /**************************************************************** |
| 117 | * Object specific macros; of_object_t includes a wire_object |
| 118 | ****************************************************************/ |
| 119 | |
| 120 | /** |
| 121 | * Return a pointer to a particular offset in the underlying buffer |
| 122 | * associated with a wire object |
| 123 | * @param obj Pointer to an of_object_t object |
| 124 | * @param offset The location to reference relative to the start of the object |
| 125 | */ |
| 126 | #define OF_OBJECT_BUFFER_INDEX(obj, offset) \ |
| 127 | OF_WIRE_OBJECT_INDEX(&((obj)->wire_object), offset) |
| 128 | |
| 129 | /** |
| 130 | * Return the absolute offset as an integer from a object-relative offset |
| 131 | * @param obj Pointer to an of_wire_object_t structure |
| 132 | * @param offset The location to reference relative to the start of the object |
| 133 | */ |
| 134 | #define OF_OBJECT_ABSOLUTE_OFFSET(obj, offset) \ |
| 135 | ((obj)->wire_object.obj_offset + offset) |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Map a generic object to the underlying wire buffer object (not the octets) |
| 140 | * |
| 141 | * Treat as private |
| 142 | */ |
| 143 | #define OF_OBJECT_TO_WBUF(obj) ((obj)->wire_object.wbuf) |
| 144 | |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Minimum allocation size for wire buffer object |
| 149 | */ |
| 150 | #define OF_WIRE_BUFFER_MIN_ALLOC_BYTES 128 |
| 151 | |
| 152 | /** |
| 153 | * Allocate a wire buffer object and the underlying data buffer. |
| 154 | * The wire buffer is initally empty (current_bytes == 0). |
| 155 | * @param a_bytes The number of bytes to allocate. |
| 156 | * @returns A wire buffer object if successful or NULL |
| 157 | */ |
| 158 | static inline of_wire_buffer_t * |
| 159 | of_wire_buffer_new(int a_bytes) |
| 160 | { |
| 161 | of_wire_buffer_t *wbuf; |
| 162 | |
| 163 | wbuf = (of_wire_buffer_t *)MALLOC(sizeof(of_wire_buffer_t)); |
| 164 | if (wbuf == NULL) { |
| 165 | return NULL; |
| 166 | } |
| 167 | MEMSET(wbuf, 0, sizeof(of_wire_buffer_t)); |
| 168 | |
| 169 | if (a_bytes < OF_WIRE_BUFFER_MIN_ALLOC_BYTES) { |
| 170 | a_bytes = OF_WIRE_BUFFER_MIN_ALLOC_BYTES; |
| 171 | } |
| 172 | |
| 173 | if ((wbuf->buf = (uint8_t *)MALLOC(a_bytes)) == NULL) { |
| 174 | FREE(wbuf); |
| 175 | return NULL; |
| 176 | } |
| 177 | MEMSET(wbuf->buf, 0, a_bytes); |
| 178 | wbuf->current_bytes = 0; |
| 179 | wbuf->alloc_bytes = a_bytes; |
| 180 | |
| 181 | return (of_wire_buffer_t *)wbuf; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Allocate a wire buffer object and bind it to an existing buffer. |
| 186 | * @param buf Existing buffer. |
| 187 | * @param bytes Size of buf. |
| 188 | * @param buf_free Function called to deallocate buf. |
| 189 | * @returns A wire buffer object if successful or NULL |
| 190 | */ |
| 191 | static inline of_wire_buffer_t * |
| 192 | of_wire_buffer_new_bind(uint8_t *buf, int bytes, of_buffer_free_f buf_free) |
| 193 | { |
| 194 | of_wire_buffer_t *wbuf; |
| 195 | |
| 196 | wbuf = (of_wire_buffer_t *)MALLOC(sizeof(of_wire_buffer_t)); |
| 197 | if (wbuf == NULL) { |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | wbuf->buf = buf; |
| 202 | wbuf->free = buf_free; |
| 203 | wbuf->current_bytes = bytes; |
| 204 | wbuf->alloc_bytes = bytes; |
| 205 | |
| 206 | return (of_wire_buffer_t *)wbuf; |
| 207 | } |
| 208 | |
| 209 | static inline void |
| 210 | of_wire_buffer_free(of_wire_buffer_t *wbuf) |
| 211 | { |
| 212 | if (wbuf == NULL) return; |
| 213 | |
| 214 | if (wbuf->buf != NULL) { |
| 215 | if (wbuf->free != NULL) { |
| 216 | wbuf->free(wbuf->buf); |
| 217 | } else { |
| 218 | FREE(wbuf->buf); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | FREE(wbuf); |
| 223 | } |
| 224 | |
| 225 | static inline void |
| 226 | of_wire_buffer_steal(of_wire_buffer_t *wbuf, uint8_t **buffer) |
| 227 | { |
| 228 | *buffer = wbuf->buf; |
| 229 | /* Mark underlying data buffer as taken */ |
| 230 | wbuf->buf = NULL; |
| 231 | of_wire_buffer_free(wbuf); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Increase the currently used length of the wire buffer. |
| 236 | * Will fail an assertion if the allocated length is not long enough. |
| 237 | * |
| 238 | * @param wbuf Pointer to the wire buffer structure |
| 239 | * @param bytes Total number of bytes buffer should grow to |
| 240 | */ |
| 241 | |
| 242 | static inline void |
| 243 | of_wire_buffer_grow(of_wire_buffer_t *wbuf, int bytes) |
| 244 | { |
| 245 | ASSERT(wbuf != NULL); |
| 246 | ASSERT(wbuf->alloc_bytes >= bytes); |
| 247 | if (bytes > wbuf->current_bytes) { |
| 248 | wbuf->current_bytes = bytes; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* TBD */ |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * Get a uint8_t scalar from a wire buffer |
| 257 | * @param wbuf The pointer to the wire buffer structure |
| 258 | * @param offset Offset in the wire buffer |
| 259 | * @param value Pointer to where to put value |
| 260 | * |
| 261 | * The underlying buffer accessor funtions handle endian and alignment. |
| 262 | */ |
| 263 | |
| 264 | static inline void |
| 265 | of_wire_buffer_u8_get(of_wire_buffer_t *wbuf, int offset, uint8_t *value) |
| 266 | { |
| 267 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint8_t)); |
| 268 | buf_u8_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Set a uint8_t scalar in a wire buffer |
| 273 | * @param wbuf The pointer to the wire buffer structure |
| 274 | * @param offset Offset in the wire buffer |
| 275 | * @param value The value to store |
| 276 | * |
| 277 | * The underlying buffer accessor funtions handle endian and alignment. |
| 278 | */ |
| 279 | |
| 280 | static inline void |
| 281 | of_wire_buffer_u8_set(of_wire_buffer_t *wbuf, int offset, uint8_t value) |
| 282 | { |
| 283 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint8_t)); |
| 284 | buf_u8_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Get a uint16_t scalar from a wire buffer |
| 289 | * @param wbuf The pointer to the wire buffer structure |
| 290 | * @param offset Offset in the wire buffer |
| 291 | * @param value Pointer to where to put value |
| 292 | * |
| 293 | * The underlying buffer accessor funtions handle endian and alignment. |
| 294 | */ |
| 295 | |
| 296 | static inline void |
| 297 | of_wire_buffer_u16_get(of_wire_buffer_t *wbuf, int offset, uint16_t *value) |
| 298 | { |
| 299 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint16_t)); |
| 300 | buf_u16_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Set a uint16_t scalar in a wire buffer |
| 305 | * @param wbuf The pointer to the wire buffer structure |
| 306 | * @param offset Offset in the wire buffer |
| 307 | * @param value The value to store |
| 308 | * |
| 309 | * The underlying buffer accessor funtions handle endian and alignment. |
| 310 | */ |
| 311 | |
| 312 | static inline void |
| 313 | of_wire_buffer_u16_set(of_wire_buffer_t *wbuf, int offset, uint16_t value) |
| 314 | { |
| 315 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint16_t)); |
| 316 | buf_u16_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Get a uint32_t scalar from a wire buffer |
| 321 | * @param wbuf The pointer to the wire buffer structure |
| 322 | * @param offset Offset in the wire buffer |
| 323 | * @param value Pointer to where to put value |
| 324 | * |
| 325 | * The underlying buffer accessor funtions handle endian and alignment. |
| 326 | */ |
| 327 | |
| 328 | static inline void |
| 329 | of_wire_buffer_u32_get(of_wire_buffer_t *wbuf, int offset, uint32_t *value) |
| 330 | { |
| 331 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint32_t)); |
| 332 | buf_u32_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Set a uint32_t scalar in a wire buffer |
| 337 | * @param wbuf The pointer to the wire buffer structure |
| 338 | * @param offset Offset in the wire buffer |
| 339 | * @param value The value to store |
| 340 | * |
| 341 | * The underlying buffer accessor funtions handle endian and alignment. |
| 342 | */ |
| 343 | |
| 344 | static inline void |
| 345 | of_wire_buffer_u32_set(of_wire_buffer_t *wbuf, int offset, uint32_t value) |
| 346 | { |
| 347 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint32_t)); |
| 348 | buf_u32_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Get a uint64_t scalar from a wire buffer |
| 353 | * @param wbuf The pointer to the wire buffer structure |
| 354 | * @param offset Offset in the wire buffer |
| 355 | * @param value Pointer to where to put value |
| 356 | * |
| 357 | * The underlying buffer accessor funtions handle endian and alignment. |
| 358 | */ |
| 359 | |
| 360 | static inline void |
| 361 | of_wire_buffer_u64_get(of_wire_buffer_t *wbuf, int offset, uint64_t *value) |
| 362 | { |
| 363 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint64_t)); |
| 364 | buf_u64_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Set a uint64_t scalar in a wire buffer |
| 369 | * @param wbuf The pointer to the wire buffer structure |
| 370 | * @param offset Offset in the wire buffer |
| 371 | * @param value The value to store |
| 372 | * |
| 373 | * The underlying buffer accessor funtions handle endian and alignment. |
| 374 | */ |
| 375 | |
| 376 | static inline void |
| 377 | of_wire_buffer_u64_set(of_wire_buffer_t *wbuf, int offset, uint64_t value) |
| 378 | { |
| 379 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint64_t)); |
| 380 | buf_u64_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Get a generic OF match structure from a wire buffer |
| 385 | * @param wbuf The pointer to the wire buffer structure |
| 386 | * @param offset Offset in the wire buffer |
| 387 | * @param value Pointer to the structure to update |
| 388 | * |
| 389 | * NOT IMPLEMENTED. |
| 390 | * |
| 391 | */ |
| 392 | |
| 393 | static inline void |
| 394 | of_wire_buffer_match_get(int version, of_wire_buffer_t *wbuf, int offset, |
| 395 | of_match_t *value) |
| 396 | { |
| 397 | ASSERT(0); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Set a generic OF match structure in a wire buffer |
| 402 | * @param wbuf The pointer to the wire buffer structure |
| 403 | * @param offset Offset in the wire buffer |
| 404 | * @param value Pointer to the structure to store |
| 405 | * |
| 406 | * NOT IMPLEMENTED. |
| 407 | * |
| 408 | */ |
| 409 | |
| 410 | static inline void |
| 411 | of_wire_buffer_match_set(int version, of_wire_buffer_t *wbuf, int offset, |
| 412 | of_match_t *value) |
| 413 | { |
| 414 | ASSERT(0); |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Get a port description object from a wire buffer |
| 419 | * @param wbuf The pointer to the wire buffer structure |
| 420 | * @param offset Offset in the wire buffer |
| 421 | * @param value Pointer to the structure to fill out |
| 422 | * |
| 423 | * NOT IMPLEMENTED. |
| 424 | * |
| 425 | * @fixme Where should this go? |
| 426 | */ |
| 427 | |
| 428 | static inline void |
| 429 | of_wire_buffer_of_port_desc_get(int version, of_wire_buffer_t *wbuf, int offset, |
| 430 | void *value) |
| 431 | { |
| 432 | ASSERT(0); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Set a port description object in a wire buffer |
| 437 | * @param wbuf The pointer to the wire buffer structure |
| 438 | * @param offset Offset in the wire buffer |
| 439 | * @param value Pointer to the structure to fill out |
| 440 | * |
| 441 | * NOT IMPLEMENTED. |
| 442 | * |
| 443 | * @fixme Where should this go? |
| 444 | */ |
| 445 | |
| 446 | static inline void |
| 447 | of_wire_buffer_of_port_desc_set(int version, of_wire_buffer_t *wbuf, int offset, |
| 448 | void *value) |
| 449 | { |
| 450 | ASSERT(0); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Get a port number scalar from a wire buffer |
| 455 | * @param wbuf The pointer to the wire buffer structure |
| 456 | * @param offset Offset in the wire buffer |
| 457 | * @param value Pointer to where to put value |
| 458 | * |
| 459 | * Port numbers are version specific. |
| 460 | */ |
| 461 | |
| 462 | static inline void |
| 463 | of_wire_buffer_port_no_get(int version, of_wire_buffer_t *wbuf, int offset, |
| 464 | of_port_no_t *value) |
| 465 | { |
| 466 | uint16_t v16; |
| 467 | uint32_t v32; |
| 468 | |
| 469 | switch (version) { |
| 470 | case OF_VERSION_1_0: |
| 471 | of_wire_buffer_u16_get(wbuf, offset, &v16); |
| 472 | *value = v16; |
| 473 | break; |
| 474 | case OF_VERSION_1_1: |
| 475 | case OF_VERSION_1_2: |
| 476 | case OF_VERSION_1_3: |
| 477 | of_wire_buffer_u32_get(wbuf, offset, &v32); |
| 478 | *value = v32; |
| 479 | break; |
| 480 | default: |
| 481 | ASSERT(0); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Set a port number scalar from a wire buffer |
| 487 | * @param wbuf The pointer to the wire buffer structure |
| 488 | * @param offset Offset in the wire buffer |
| 489 | * @param value The value to store in the buffer |
| 490 | * |
| 491 | * Port numbers are version specific. |
| 492 | */ |
| 493 | |
| 494 | static inline void |
| 495 | of_wire_buffer_port_no_set(int version, of_wire_buffer_t *wbuf, int offset, |
| 496 | of_port_no_t value) |
| 497 | { |
| 498 | |
| 499 | switch (version) { |
| 500 | case OF_VERSION_1_0: |
| 501 | of_wire_buffer_u16_set(wbuf, offset, (uint16_t)value); |
| 502 | break; |
| 503 | case OF_VERSION_1_1: |
| 504 | case OF_VERSION_1_2: |
| 505 | case OF_VERSION_1_3: |
| 506 | of_wire_buffer_u32_set(wbuf, offset, (uint32_t)value); |
| 507 | break; |
| 508 | default: |
| 509 | ASSERT(0); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Get a flow mod command value from a wire buffer |
| 515 | * @param wbuf The pointer to the wire buffer structure |
| 516 | * @param offset Offset in the wire buffer |
| 517 | * @param value Pointer to where to put value |
| 518 | */ |
| 519 | |
| 520 | static inline void |
| 521 | of_wire_buffer_fm_cmd_get(int version, of_wire_buffer_t *wbuf, int offset, |
| 522 | of_fm_cmd_t *value) |
| 523 | { |
| 524 | uint16_t v16; |
| 525 | uint8_t v8; |
| 526 | |
| 527 | switch (version) { |
| 528 | case OF_VERSION_1_0: |
| 529 | of_wire_buffer_u16_get(wbuf, offset, &v16); |
| 530 | *value = v16; |
| 531 | break; |
| 532 | case OF_VERSION_1_1: |
| 533 | case OF_VERSION_1_2: |
| 534 | case OF_VERSION_1_3: |
| 535 | of_wire_buffer_u8_get(wbuf, offset, &v8); |
| 536 | *value = v8; |
| 537 | break; |
| 538 | default: |
| 539 | ASSERT(0); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Set a flow mod command value in a wire buffer |
| 545 | * @param wbuf The pointer to the wire buffer structure |
| 546 | * @param offset Offset in the wire buffer |
| 547 | * @param value The value to store |
| 548 | */ |
| 549 | |
| 550 | static inline void |
| 551 | of_wire_buffer_fm_cmd_set(int version, of_wire_buffer_t *wbuf, int offset, |
| 552 | of_fm_cmd_t value) |
| 553 | { |
| 554 | switch (version) { |
| 555 | case OF_VERSION_1_0: |
| 556 | of_wire_buffer_u16_set(wbuf, offset, (uint16_t)value); |
| 557 | break; |
| 558 | case OF_VERSION_1_1: |
| 559 | case OF_VERSION_1_2: |
| 560 | case OF_VERSION_1_3: |
| 561 | of_wire_buffer_u8_set(wbuf, offset, (uint8_t)value); |
| 562 | break; |
| 563 | default: |
| 564 | ASSERT(0); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Get a wild card bitmap value from a wire buffer |
| 570 | * @param wbuf The pointer to the wire buffer structure |
| 571 | * @param offset Offset in the wire buffer |
| 572 | * @param value Pointer to where to store the value |
| 573 | */ |
| 574 | |
| 575 | static inline void |
| 576 | of_wire_buffer_wc_bmap_get(int version, of_wire_buffer_t *wbuf, int offset, |
| 577 | of_wc_bmap_t *value) |
| 578 | { |
| 579 | uint32_t v32; |
| 580 | uint64_t v64; |
| 581 | |
| 582 | switch (version) { |
| 583 | case OF_VERSION_1_0: |
| 584 | case OF_VERSION_1_1: |
| 585 | of_wire_buffer_u32_get(wbuf, offset, &v32); |
| 586 | *value = v32; |
| 587 | break; |
| 588 | case OF_VERSION_1_2: |
| 589 | case OF_VERSION_1_3: |
| 590 | of_wire_buffer_u64_get(wbuf, offset, &v64); |
| 591 | *value = v64; |
| 592 | break; |
| 593 | default: |
| 594 | ASSERT(0); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Set a wild card bitmap value in a wire buffer |
| 600 | * @param wbuf The pointer to the wire buffer structure |
| 601 | * @param offset Offset in the wire buffer |
| 602 | * @param value The value to store |
| 603 | */ |
| 604 | |
| 605 | static inline void |
| 606 | of_wire_buffer_wc_bmap_set(int version, of_wire_buffer_t *wbuf, int offset, |
| 607 | of_wc_bmap_t value) |
| 608 | { |
| 609 | switch (version) { |
| 610 | case OF_VERSION_1_0: |
| 611 | case OF_VERSION_1_1: |
| 612 | of_wire_buffer_u32_set(wbuf, offset, (uint32_t)value); |
| 613 | break; |
| 614 | case OF_VERSION_1_2: |
| 615 | case OF_VERSION_1_3: |
| 616 | of_wire_buffer_u64_set(wbuf, offset, (uint64_t)value); |
| 617 | break; |
| 618 | default: |
| 619 | ASSERT(0); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /* match bitmap and wildcard bitmaps followed the same pattern */ |
| 624 | #define of_wire_buffer_match_bmap_get of_wire_buffer_wc_bmap_get |
| 625 | #define of_wire_buffer_match_bmap_set of_wire_buffer_wc_bmap_set |
| 626 | |
| 627 | /* Derived functions, mostly for fixed length name strings */ |
| 628 | #define of_wire_buffer_char_get of_wire_buffer_u8_get |
| 629 | #define of_wire_buffer_char_set of_wire_buffer_u8_set |
| 630 | |
| 631 | |
| 632 | /** |
| 633 | * Get an octet object from a wire buffer |
| 634 | * @param wbuf The pointer to the wire buffer structure |
| 635 | * @param offset Offset in the wire buffer |
| 636 | * @param value Pointer to where to put value |
| 637 | * |
| 638 | * of_octets_t is treated specially as the high level functions pass around |
| 639 | * pointers for "get" operators. |
| 640 | * |
| 641 | * Important: The length of data to copy is stored in the value->bytes |
| 642 | * variable. |
| 643 | */ |
| 644 | |
| 645 | static inline void |
| 646 | of_wire_buffer_octets_data_get(of_wire_buffer_t *wbuf, int offset, |
| 647 | of_octets_t *value) |
| 648 | { |
| 649 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + OF_OCTETS_BYTES_GET(value)); |
| 650 | buf_octets_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), |
| 651 | OF_OCTETS_POINTER_GET(value), |
| 652 | OF_OCTETS_BYTES_GET(value)); |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Set an octet object in a wire buffer |
| 657 | * @param wbuf The pointer to the wire buffer structure |
| 658 | * @param offset Offset in the wire buffer |
| 659 | * @param value Pointer to the octet stream to store |
| 660 | * @param cur_len Current length of data in the buffer |
| 661 | * |
| 662 | * of_octets_t is treated specially as the high level functions pass around |
| 663 | * pointers for "get" operators. |
| 664 | * |
| 665 | * @fixme Need to take into account cur_len |
| 666 | */ |
| 667 | |
| 668 | static inline void |
| 669 | of_wire_buffer_octets_data_set(of_wire_buffer_t *wbuf, int offset, |
| 670 | of_octets_t *value, int cur_len) |
| 671 | { |
| 672 | // FIXME need to adjust length of octets member in buffer |
| 673 | ASSERT(cur_len == 0 || cur_len == value->bytes); |
| 674 | |
| 675 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + OF_OCTETS_BYTES_GET(value)); |
| 676 | buf_octets_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), |
| 677 | OF_OCTETS_POINTER_GET(value), |
| 678 | OF_OCTETS_BYTES_GET(value)); |
| 679 | } |
| 680 | |
| 681 | static inline void |
| 682 | _wbuf_octets_set(of_wire_buffer_t *wbuf, int offset, uint8_t *src, int bytes) { |
| 683 | of_octets_t octets; |
| 684 | OF_OCTETS_POINTER_SET(&octets, src); |
| 685 | OF_OCTETS_BYTES_SET(&octets, bytes); |
| 686 | of_wire_buffer_octets_data_set(wbuf, offset, &octets, bytes); |
| 687 | } |
| 688 | |
| 689 | static inline void |
| 690 | _wbuf_octets_get(of_wire_buffer_t *wbuf, int offset, uint8_t *dst, int bytes) { |
| 691 | of_octets_t octets; |
| 692 | OF_OCTETS_POINTER_SET(&octets, dst); |
| 693 | OF_OCTETS_BYTES_SET(&octets, bytes); |
| 694 | of_wire_buffer_octets_data_get(wbuf, offset, &octets); |
| 695 | } |
| 696 | |
| 697 | |
| 698 | /** |
| 699 | * Get a MAC address from a wire buffer |
| 700 | * @param wbuf The pointer to the wire buffer structure |
| 701 | * @param offset Offset in the wire buffer |
| 702 | * @param mac Pointer to the mac address location |
| 703 | * |
| 704 | * Uses the octets function. |
| 705 | */ |
| 706 | |
| 707 | #define of_wire_buffer_mac_get(buf, offset, mac) \ |
| 708 | _wbuf_octets_get(buf, offset, (uint8_t *)mac, 6) |
| 709 | |
| 710 | /** |
| 711 | * Set a MAC address in a wire buffer |
| 712 | * @param wbuf The pointer to the wire buffer structure |
| 713 | * @param offset Offset in the wire buffer |
| 714 | * @param mac The variable holding the mac address to store |
| 715 | * |
| 716 | * Uses the octets function. |
| 717 | */ |
| 718 | |
| 719 | #define of_wire_buffer_mac_set(buf, offset, mac) \ |
| 720 | _wbuf_octets_set(buf, offset, (uint8_t *)&mac, 6) |
| 721 | |
| 722 | |
| 723 | /** |
| 724 | * Get a port name string from a wire buffer |
| 725 | * @param wbuf The pointer to the wire buffer structure |
| 726 | * @param offset Offset in the wire buffer |
| 727 | * @param mac The mac address |
| 728 | * |
| 729 | * Uses the octets function. |
| 730 | */ |
| 731 | #define of_wire_buffer_port_name_get(buf, offset, portname) \ |
| 732 | _wbuf_octets_get(buf, offset, (uint8_t *)portname, \ |
| 733 | OF_MAX_PORT_NAME_LEN) |
| 734 | |
| 735 | /** |
| 736 | * Set a port name address in a wire buffer |
| 737 | * @param wbuf The pointer to the wire buffer structure |
| 738 | * @param offset Offset in the wire buffer |
| 739 | * @param portname Where to store the port name |
| 740 | * |
| 741 | * Uses the octets function. |
| 742 | */ |
| 743 | |
| 744 | #define of_wire_buffer_port_name_set(buf, offset, portname) \ |
| 745 | _wbuf_octets_set(buf, offset, (uint8_t *)portname, \ |
| 746 | OF_MAX_PORT_NAME_LEN) |
| 747 | |
| 748 | |
| 749 | /** |
| 750 | * Get a table name string from a wire buffer |
| 751 | * @param wbuf The pointer to the wire buffer structure |
| 752 | * @param offset Offset in the wire buffer |
| 753 | * @param portname The port name |
| 754 | * |
| 755 | * Uses the octets function. |
| 756 | */ |
| 757 | |
| 758 | #define of_wire_buffer_tab_name_get(buf, offset, tabname) \ |
| 759 | _wbuf_octets_get(buf, offset, (uint8_t *)tabname, \ |
| 760 | OF_MAX_TABLE_NAME_LEN) |
| 761 | |
| 762 | /** |
| 763 | * Set a table name address in a wire buffer |
| 764 | * @param wbuf The pointer to the wire buffer structure |
| 765 | * @param offset Offset in the wire buffer |
| 766 | * @param mac Where to store the table name |
| 767 | * |
| 768 | * Uses the octets function. |
| 769 | */ |
| 770 | |
| 771 | #define of_wire_buffer_tab_name_set(buf, offset, tabname) \ |
| 772 | _wbuf_octets_set(buf, offset, (uint8_t *)tabname, \ |
| 773 | OF_MAX_TABLE_NAME_LEN) |
| 774 | |
| 775 | /** |
| 776 | * Get a description string from a wire buffer |
| 777 | * @param wbuf The pointer to the wire buffer structure |
| 778 | * @param offset Offset in the wire buffer |
| 779 | * @param desc Where to store the description string |
| 780 | * |
| 781 | * Uses the octets function. |
| 782 | */ |
| 783 | |
| 784 | #define of_wire_buffer_desc_str_get(buf, offset, desc) \ |
| 785 | _wbuf_octets_get(buf, offset, (uint8_t *)desc, OF_DESC_STR_LEN) |
| 786 | |
| 787 | /** |
| 788 | * Set a description string in a wire buffer |
| 789 | * @param wbuf The pointer to the wire buffer structure |
| 790 | * @param offset Offset in the wire buffer |
| 791 | * @param desc The description string |
| 792 | * |
| 793 | * Uses the octets function. |
| 794 | */ |
| 795 | |
| 796 | #define of_wire_buffer_desc_str_set(buf, offset, desc) \ |
| 797 | _wbuf_octets_set(buf, offset, (uint8_t *)desc, OF_DESC_STR_LEN) |
| 798 | |
| 799 | /** |
| 800 | * Get a serial number string from a wire buffer |
| 801 | * @param wbuf The pointer to the wire buffer structure |
| 802 | * @param offset Offset in the wire buffer |
| 803 | * @param sernum Where to store the serial number string |
| 804 | * |
| 805 | * Uses the octets function. |
| 806 | */ |
| 807 | |
| 808 | #define of_wire_buffer_ser_num_get(buf, offset, sernum) \ |
| 809 | _wbuf_octets_get(buf, offset, (uint8_t *)sernum, OF_SERIAL_NUM_LEN) |
| 810 | |
| 811 | /** |
| 812 | * Set a serial number string in a wire buffer |
| 813 | * @param wbuf The pointer to the wire buffer structure |
| 814 | * @param offset Offset in the wire buffer |
| 815 | * @param desc The serial number string |
| 816 | * |
| 817 | * Uses the octets function. |
| 818 | */ |
| 819 | |
| 820 | #define of_wire_buffer_ser_num_set(buf, offset, sernum) \ |
| 821 | _wbuf_octets_set(buf, offset, (uint8_t *)sernum, OF_SERIAL_NUM_LEN) |
| 822 | |
| 823 | /** |
| 824 | * Get an ipv6 address from a wire buffer |
| 825 | * @param wbuf The pointer to the wire buffer structure |
| 826 | * @param offset Offset in the wire buffer |
| 827 | * @param addr Pointer to where to store the ipv6 address |
| 828 | * |
| 829 | * Uses the octets function. |
| 830 | */ |
| 831 | |
| 832 | #define of_wire_buffer_ipv6_get(buf, offset, addr) \ |
| 833 | _wbuf_octets_get(buf, offset, (uint8_t *)addr, sizeof(of_ipv6_t)) |
| 834 | |
| 835 | /** |
| 836 | * Set an ipv6 address in a wire buffer |
| 837 | * @param wbuf The pointer to the wire buffer structure |
| 838 | * @param offset Offset in the wire buffer |
| 839 | * @param addr The variable holding ipv6 address to store |
| 840 | * |
| 841 | * Uses the octets function. |
| 842 | */ |
| 843 | |
| 844 | #define of_wire_buffer_ipv6_set(buf, offset, addr) \ |
| 845 | _wbuf_octets_set(buf, offset, (uint8_t *)&addr, sizeof(of_ipv6_t)) |
| 846 | |
| 847 | /* Relocate data from start offset to the end of the buffer to a new position */ |
| 848 | static inline void |
| 849 | of_wire_buffer_move_end(of_wire_buffer_t *wbuf, int start_offset, int new_offset) |
| 850 | { |
| 851 | int bytes; |
| 852 | int new_length; |
| 853 | |
| 854 | if (new_offset > start_offset) { |
| 855 | bytes = new_offset - start_offset; |
| 856 | new_length = wbuf->alloc_bytes + bytes; |
| 857 | OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, new_length); |
| 858 | } else { |
| 859 | bytes = start_offset - new_offset; |
| 860 | new_length = wbuf->alloc_bytes - bytes; |
| 861 | } |
| 862 | |
| 863 | MEMMOVE(&wbuf->buf[new_offset], &wbuf->buf[start_offset], bytes); |
| 864 | wbuf->alloc_bytes = new_length; |
| 865 | } |
| 866 | |
| 867 | /* Given a wire buffer object and the offset of the start of an of_match struct, |
| 868 | * return its total length in the buffer |
| 869 | */ |
| 870 | static inline int |
| 871 | of_match_bytes(of_wire_buffer_t *wbuf, int offset) { |
| 872 | uint16_t len; |
| 873 | of_wire_buffer_u16_get(wbuf, offset + 2, &len); |
| 874 | return OF_MATCH_BYTES(len); |
| 875 | } |
| 876 | |
| 877 | extern void |
| 878 | of_wire_buffer_replace_data(of_wire_buffer_t *wbuf, |
| 879 | int offset, |
| 880 | int old_len, |
| 881 | uint8_t *data, |
| 882 | int new_len); |
| 883 | |
| 884 | #endif /* _OF_WIRE_BUF_H_ */ |