blob: 0922a695f920952e85d71b70e8156b09694e7d0c [file] [log] [blame]
Rich Lanea06d0c32013-03-25 08:52:03 -07001:: # 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 Laned983aa52013-06-13 11:48:37 -070028:: include('_copyright.c')
Rich Lanea06d0c32013-03-25 08:52:03 -070029
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 */
53typedef 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
Rich Lanea06d0c32013-03-25 08:52:03 -070065#define WBUF_BUF(wbuf) (wbuf)->buf
66#define WBUF_ALLOC_BYTES(wbuf) (wbuf)->alloc_bytes
67#define WBUF_CURRENT_BYTES(wbuf) (wbuf)->current_bytes
68
69/**
70 * For read access, throw an error code if current buffer
71 * is not big enough.
72 * @param wbuf Pointer to an of_wire_buffer_t structure
73 * @param offset The extent of the buffer required
74 */
75#define OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset) \
Rich Lanee57f0432014-02-19 10:31:53 -080076 LOCI_ASSERT(((wbuf) != NULL) && (WBUF_BUF(wbuf) != NULL) && \
Rich Lanea06d0c32013-03-25 08:52:03 -070077 (offset > 0) && (WBUF_CURRENT_BYTES(wbuf) >= offset))
78
79/*
80 * Index a wire buffer
81 * Index a wire object (from obj_offset)
82 * Index a LOCI object
83 */
84
85/**
86 * Return a pointer to a particular offset in a wire buffer's data
87 * @param wbuf Pointer to an of_wire_buffer_t structure
88 * @param offset The location to reference
89 */
90#define OF_WIRE_BUFFER_INDEX(wbuf, offset) (&((WBUF_BUF(wbuf))[offset]))
91
Rich Lanea06d0c32013-03-25 08:52:03 -070092/****************************************************************
93 * Object specific macros; of_object_t includes a wire_object
94 ****************************************************************/
95
96/**
97 * Return a pointer to a particular offset in the underlying buffer
98 * associated with a wire object
99 * @param obj Pointer to an of_object_t object
100 * @param offset The location to reference relative to the start of the object
101 */
102#define OF_OBJECT_BUFFER_INDEX(obj, offset) \
Rich Lanecdd542d2014-04-03 16:13:12 -0700103 OF_WIRE_BUFFER_INDEX((obj)->wbuf, (obj)->obj_offset + offset)
Rich Lanea06d0c32013-03-25 08:52:03 -0700104
105/**
106 * Return the absolute offset as an integer from a object-relative offset
107 * @param obj Pointer to an of_wire_object_t structure
108 * @param offset The location to reference relative to the start of the object
109 */
110#define OF_OBJECT_ABSOLUTE_OFFSET(obj, offset) \
Rich Lanecdd542d2014-04-03 16:13:12 -0700111 ((obj)->obj_offset + offset)
Rich Lanea06d0c32013-03-25 08:52:03 -0700112
113
114/**
115 * Map a generic object to the underlying wire buffer object (not the octets)
116 *
117 * Treat as private
118 */
Rich Lanecdd542d2014-04-03 16:13:12 -0700119#define OF_OBJECT_TO_WBUF(obj) ((obj)->wbuf)
Rich Lanea06d0c32013-03-25 08:52:03 -0700120
121
Rich Lanea06d0c32013-03-25 08:52:03 -0700122/**
123 * Allocate a wire buffer object and the underlying data buffer.
124 * The wire buffer is initally empty (current_bytes == 0).
125 * @param a_bytes The number of bytes to allocate.
126 * @returns A wire buffer object if successful or NULL
127 */
128static inline of_wire_buffer_t *
129of_wire_buffer_new(int a_bytes)
130{
131 of_wire_buffer_t *wbuf;
132
133 wbuf = (of_wire_buffer_t *)MALLOC(sizeof(of_wire_buffer_t));
134 if (wbuf == NULL) {
135 return NULL;
136 }
137 MEMSET(wbuf, 0, sizeof(of_wire_buffer_t));
138
Rich Lanea06d0c32013-03-25 08:52:03 -0700139 if ((wbuf->buf = (uint8_t *)MALLOC(a_bytes)) == NULL) {
140 FREE(wbuf);
141 return NULL;
142 }
Rich Lanea06d0c32013-03-25 08:52:03 -0700143 wbuf->current_bytes = 0;
144 wbuf->alloc_bytes = a_bytes;
145
146 return (of_wire_buffer_t *)wbuf;
147}
148
149/**
150 * Allocate a wire buffer object and bind it to an existing buffer.
151 * @param buf Existing buffer.
152 * @param bytes Size of buf.
153 * @param buf_free Function called to deallocate buf.
154 * @returns A wire buffer object if successful or NULL
155 */
156static inline of_wire_buffer_t *
157of_wire_buffer_new_bind(uint8_t *buf, int bytes, of_buffer_free_f buf_free)
158{
159 of_wire_buffer_t *wbuf;
160
161 wbuf = (of_wire_buffer_t *)MALLOC(sizeof(of_wire_buffer_t));
162 if (wbuf == NULL) {
163 return NULL;
164 }
165
166 wbuf->buf = buf;
167 wbuf->free = buf_free;
168 wbuf->current_bytes = bytes;
169 wbuf->alloc_bytes = bytes;
170
171 return (of_wire_buffer_t *)wbuf;
172}
173
174static inline void
175of_wire_buffer_free(of_wire_buffer_t *wbuf)
176{
177 if (wbuf == NULL) return;
178
179 if (wbuf->buf != NULL) {
180 if (wbuf->free != NULL) {
181 wbuf->free(wbuf->buf);
182 } else {
183 FREE(wbuf->buf);
184 }
185 }
186
187 FREE(wbuf);
188}
189
190static inline void
191of_wire_buffer_steal(of_wire_buffer_t *wbuf, uint8_t **buffer)
192{
193 *buffer = wbuf->buf;
194 /* Mark underlying data buffer as taken */
195 wbuf->buf = NULL;
196 of_wire_buffer_free(wbuf);
197}
198
199/**
200 * Increase the currently used length of the wire buffer.
201 * Will fail an assertion if the allocated length is not long enough.
202 *
203 * @param wbuf Pointer to the wire buffer structure
204 * @param bytes Total number of bytes buffer should grow to
205 */
206
207static inline void
208of_wire_buffer_grow(of_wire_buffer_t *wbuf, int bytes)
209{
Rich Lanee57f0432014-02-19 10:31:53 -0800210 LOCI_ASSERT(wbuf != NULL);
211 LOCI_ASSERT(wbuf->alloc_bytes >= bytes);
Rich Lanea06d0c32013-03-25 08:52:03 -0700212 if (bytes > wbuf->current_bytes) {
Rich Lanedc9bc7f2014-04-05 11:19:20 -0700213 MEMSET(wbuf->buf + wbuf->current_bytes, 0, bytes - wbuf->current_bytes);
Rich Lanea06d0c32013-03-25 08:52:03 -0700214 wbuf->current_bytes = bytes;
215 }
216}
217
218/* TBD */
219
220
221/**
222 * Get a uint8_t scalar from a wire buffer
223 * @param wbuf The pointer to the wire buffer structure
224 * @param offset Offset in the wire buffer
225 * @param value Pointer to where to put value
226 *
227 * The underlying buffer accessor funtions handle endian and alignment.
228 */
229
230static inline void
231of_wire_buffer_u8_get(of_wire_buffer_t *wbuf, int offset, uint8_t *value)
232{
233 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint8_t));
234 buf_u8_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
235}
236
237/**
238 * Set a uint8_t scalar in a wire buffer
239 * @param wbuf The pointer to the wire buffer structure
240 * @param offset Offset in the wire buffer
241 * @param value The value to store
242 *
243 * The underlying buffer accessor funtions handle endian and alignment.
244 */
245
246static inline void
247of_wire_buffer_u8_set(of_wire_buffer_t *wbuf, int offset, uint8_t value)
248{
249 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint8_t));
250 buf_u8_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
251}
252
253/**
254 * Get a uint16_t scalar from a wire buffer
255 * @param wbuf The pointer to the wire buffer structure
256 * @param offset Offset in the wire buffer
257 * @param value Pointer to where to put value
258 *
259 * The underlying buffer accessor funtions handle endian and alignment.
260 */
261
262static inline void
263of_wire_buffer_u16_get(of_wire_buffer_t *wbuf, int offset, uint16_t *value)
264{
265 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint16_t));
266 buf_u16_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
267}
268
269/**
270 * Set a uint16_t scalar in a wire buffer
271 * @param wbuf The pointer to the wire buffer structure
272 * @param offset Offset in the wire buffer
273 * @param value The value to store
274 *
275 * The underlying buffer accessor funtions handle endian and alignment.
276 */
277
278static inline void
279of_wire_buffer_u16_set(of_wire_buffer_t *wbuf, int offset, uint16_t value)
280{
281 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint16_t));
282 buf_u16_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
283}
284
285/**
286 * Get a uint32_t scalar from a wire buffer
287 * @param wbuf The pointer to the wire buffer structure
288 * @param offset Offset in the wire buffer
289 * @param value Pointer to where to put value
290 *
291 * The underlying buffer accessor funtions handle endian and alignment.
292 */
293
294static inline void
295of_wire_buffer_u32_get(of_wire_buffer_t *wbuf, int offset, uint32_t *value)
296{
297 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint32_t));
298 buf_u32_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
299}
300
301/**
302 * Set a uint32_t scalar in a wire buffer
303 * @param wbuf The pointer to the wire buffer structure
304 * @param offset Offset in the wire buffer
305 * @param value The value to store
306 *
307 * The underlying buffer accessor funtions handle endian and alignment.
308 */
309
310static inline void
311of_wire_buffer_u32_set(of_wire_buffer_t *wbuf, int offset, uint32_t value)
312{
313 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint32_t));
314 buf_u32_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
315}
316
Andreas Wundsamb566a162013-07-18 19:30:23 -0700317
318/**
319 * Get a uint32_t scalar from a wire buffer
320 * @param wbuf The pointer to the wire buffer structure
321 * @param offset Offset in the wire buffer
322 * @param value Pointer to where to put value
323 *
324 * The underlying buffer accessor funtions handle endian and alignment.
325 */
326
327static inline void
328of_wire_buffer_ipv4_get(of_wire_buffer_t *wbuf, int offset, of_ipv4_t *value)
329{
330 of_wire_buffer_u32_get(wbuf, offset, value);
331}
332
333/**
334 * Set a ipv4 (uint32_t) scalar in a wire buffer
335 * @param wbuf The pointer to the wire buffer structure
336 * @param offset Offset in the wire buffer
337 * @param value The value to store
338 *
339 * The underlying buffer accessor funtions handle endian and alignment.
340 */
341
342static inline void
343of_wire_buffer_ipv4_set(of_wire_buffer_t *wbuf, int offset, of_ipv4_t value)
344{
345 of_wire_buffer_u32_set(wbuf, offset, value);
346}
347
348
Rich Lanea06d0c32013-03-25 08:52:03 -0700349/**
350 * Get a uint64_t scalar from a wire buffer
351 * @param wbuf The pointer to the wire buffer structure
352 * @param offset Offset in the wire buffer
353 * @param value Pointer to where to put value
354 *
355 * The underlying buffer accessor funtions handle endian and alignment.
356 */
357
358static inline void
359of_wire_buffer_u64_get(of_wire_buffer_t *wbuf, int offset, uint64_t *value)
360{
361 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint64_t));
362 buf_u64_get(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
363}
364
365/**
366 * Set a uint64_t scalar in a wire buffer
367 * @param wbuf The pointer to the wire buffer structure
368 * @param offset Offset in the wire buffer
369 * @param value The value to store
370 *
371 * The underlying buffer accessor funtions handle endian and alignment.
372 */
373
374static inline void
375of_wire_buffer_u64_set(of_wire_buffer_t *wbuf, int offset, uint64_t value)
376{
377 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(uint64_t));
378 buf_u64_set(OF_WIRE_BUFFER_INDEX(wbuf, offset), value);
379}
380
381/**
382 * Get a generic OF match structure from a wire buffer
383 * @param wbuf The pointer to the wire buffer structure
384 * @param offset Offset in the wire buffer
385 * @param value Pointer to the structure to update
386 *
387 * NOT IMPLEMENTED.
388 *
389 */
390
391static inline void
392of_wire_buffer_match_get(int version, of_wire_buffer_t *wbuf, int offset,
393 of_match_t *value)
394{
Rich Lanee57f0432014-02-19 10:31:53 -0800395 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700396}
397
398/**
399 * Set a generic OF match structure in a wire buffer
400 * @param wbuf The pointer to the wire buffer structure
401 * @param offset Offset in the wire buffer
402 * @param value Pointer to the structure to store
403 *
404 * NOT IMPLEMENTED.
405 *
406 */
407
408static inline void
409of_wire_buffer_match_set(int version, of_wire_buffer_t *wbuf, int offset,
410 of_match_t *value)
411{
Rich Lanee57f0432014-02-19 10:31:53 -0800412 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700413}
414
415/**
416 * Get a port description object from a wire buffer
417 * @param wbuf The pointer to the wire buffer structure
418 * @param offset Offset in the wire buffer
419 * @param value Pointer to the structure to fill out
420 *
421 * NOT IMPLEMENTED.
422 *
423 * @fixme Where should this go?
424 */
425
426static inline void
427of_wire_buffer_of_port_desc_get(int version, of_wire_buffer_t *wbuf, int offset,
428 void *value)
429{
Rich Lanee57f0432014-02-19 10:31:53 -0800430 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700431}
432
433/**
434 * Set a port description object in a wire buffer
435 * @param wbuf The pointer to the wire buffer structure
436 * @param offset Offset in the wire buffer
437 * @param value Pointer to the structure to fill out
438 *
439 * NOT IMPLEMENTED.
440 *
441 * @fixme Where should this go?
442 */
443
444static inline void
445of_wire_buffer_of_port_desc_set(int version, of_wire_buffer_t *wbuf, int offset,
446 void *value)
447{
Rich Lanee57f0432014-02-19 10:31:53 -0800448 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700449}
450
451/**
452 * Get a port number scalar from a wire buffer
453 * @param wbuf The pointer to the wire buffer structure
454 * @param offset Offset in the wire buffer
455 * @param value Pointer to where to put value
456 *
457 * Port numbers are version specific.
458 */
459
460static inline void
461of_wire_buffer_port_no_get(int version, of_wire_buffer_t *wbuf, int offset,
462 of_port_no_t *value)
463{
464 uint16_t v16;
465 uint32_t v32;
466
467 switch (version) {
468 case OF_VERSION_1_0:
469 of_wire_buffer_u16_get(wbuf, offset, &v16);
470 *value = v16;
471 break;
472 case OF_VERSION_1_1:
473 case OF_VERSION_1_2:
474 case OF_VERSION_1_3:
475 of_wire_buffer_u32_get(wbuf, offset, &v32);
476 *value = v32;
477 break;
478 default:
Rich Lanee57f0432014-02-19 10:31:53 -0800479 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700480 }
481}
482
483/**
484 * Set a port number scalar from a wire buffer
485 * @param wbuf The pointer to the wire buffer structure
486 * @param offset Offset in the wire buffer
487 * @param value The value to store in the buffer
488 *
489 * Port numbers are version specific.
490 */
491
492static inline void
493of_wire_buffer_port_no_set(int version, of_wire_buffer_t *wbuf, int offset,
494 of_port_no_t value)
495{
496
497 switch (version) {
498 case OF_VERSION_1_0:
499 of_wire_buffer_u16_set(wbuf, offset, (uint16_t)value);
500 break;
501 case OF_VERSION_1_1:
502 case OF_VERSION_1_2:
503 case OF_VERSION_1_3:
504 of_wire_buffer_u32_set(wbuf, offset, (uint32_t)value);
505 break;
506 default:
Rich Lanee57f0432014-02-19 10:31:53 -0800507 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700508 }
509}
510
511/**
512 * Get a flow mod command value from a wire buffer
513 * @param wbuf The pointer to the wire buffer structure
514 * @param offset Offset in the wire buffer
515 * @param value Pointer to where to put value
516 */
517
518static inline void
519of_wire_buffer_fm_cmd_get(int version, of_wire_buffer_t *wbuf, int offset,
520 of_fm_cmd_t *value)
521{
522 uint16_t v16;
523 uint8_t v8;
524
525 switch (version) {
526 case OF_VERSION_1_0:
527 of_wire_buffer_u16_get(wbuf, offset, &v16);
528 *value = v16;
529 break;
530 case OF_VERSION_1_1:
531 case OF_VERSION_1_2:
532 case OF_VERSION_1_3:
533 of_wire_buffer_u8_get(wbuf, offset, &v8);
534 *value = v8;
535 break;
536 default:
Rich Lanee57f0432014-02-19 10:31:53 -0800537 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700538 }
539}
540
541/**
542 * Set a flow mod command value in a wire buffer
543 * @param wbuf The pointer to the wire buffer structure
544 * @param offset Offset in the wire buffer
545 * @param value The value to store
546 */
547
548static inline void
549of_wire_buffer_fm_cmd_set(int version, of_wire_buffer_t *wbuf, int offset,
550 of_fm_cmd_t value)
551{
552 switch (version) {
553 case OF_VERSION_1_0:
554 of_wire_buffer_u16_set(wbuf, offset, (uint16_t)value);
555 break;
556 case OF_VERSION_1_1:
557 case OF_VERSION_1_2:
558 case OF_VERSION_1_3:
559 of_wire_buffer_u8_set(wbuf, offset, (uint8_t)value);
560 break;
561 default:
Rich Lanee57f0432014-02-19 10:31:53 -0800562 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700563 }
564}
565
566/**
567 * Get a wild card bitmap value from a wire buffer
568 * @param wbuf The pointer to the wire buffer structure
569 * @param offset Offset in the wire buffer
570 * @param value Pointer to where to store the value
571 */
572
573static inline void
574of_wire_buffer_wc_bmap_get(int version, of_wire_buffer_t *wbuf, int offset,
575 of_wc_bmap_t *value)
576{
577 uint32_t v32;
578 uint64_t v64;
579
580 switch (version) {
581 case OF_VERSION_1_0:
582 case OF_VERSION_1_1:
583 of_wire_buffer_u32_get(wbuf, offset, &v32);
584 *value = v32;
585 break;
586 case OF_VERSION_1_2:
587 case OF_VERSION_1_3:
588 of_wire_buffer_u64_get(wbuf, offset, &v64);
589 *value = v64;
590 break;
591 default:
Rich Lanee57f0432014-02-19 10:31:53 -0800592 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700593 }
594}
595
596/**
597 * Set a wild card bitmap value in a wire buffer
598 * @param wbuf The pointer to the wire buffer structure
599 * @param offset Offset in the wire buffer
600 * @param value The value to store
601 */
602
603static inline void
604of_wire_buffer_wc_bmap_set(int version, of_wire_buffer_t *wbuf, int offset,
605 of_wc_bmap_t value)
606{
607 switch (version) {
608 case OF_VERSION_1_0:
609 case OF_VERSION_1_1:
610 of_wire_buffer_u32_set(wbuf, offset, (uint32_t)value);
611 break;
612 case OF_VERSION_1_2:
613 case OF_VERSION_1_3:
614 of_wire_buffer_u64_set(wbuf, offset, (uint64_t)value);
615 break;
616 default:
Rich Lanee57f0432014-02-19 10:31:53 -0800617 LOCI_ASSERT(0);
Rich Lanea06d0c32013-03-25 08:52:03 -0700618 }
619}
620
621/* match bitmap and wildcard bitmaps followed the same pattern */
622#define of_wire_buffer_match_bmap_get of_wire_buffer_wc_bmap_get
623#define of_wire_buffer_match_bmap_set of_wire_buffer_wc_bmap_set
624
625/* Derived functions, mostly for fixed length name strings */
626#define of_wire_buffer_char_get of_wire_buffer_u8_get
627#define of_wire_buffer_char_set of_wire_buffer_u8_set
628
629
630/**
631 * Get an octet object from a wire buffer
632 * @param wbuf The pointer to the wire buffer structure
633 * @param offset Offset in the wire buffer
634 * @param value Pointer to where to put value
635 *
636 * of_octets_t is treated specially as the high level functions pass around
637 * pointers for "get" operators.
638 *
639 * Important: The length of data to copy is stored in the value->bytes
640 * variable.
641 */
642
643static inline void
644of_wire_buffer_octets_data_get(of_wire_buffer_t *wbuf, int offset,
645 of_octets_t *value)
646{
647 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + OF_OCTETS_BYTES_GET(value));
648 buf_octets_get(OF_WIRE_BUFFER_INDEX(wbuf, offset),
649 OF_OCTETS_POINTER_GET(value),
650 OF_OCTETS_BYTES_GET(value));
651}
652
653/**
654 * Set an octet object in a wire buffer
655 * @param wbuf The pointer to the wire buffer structure
656 * @param offset Offset in the wire buffer
657 * @param value Pointer to the octet stream to store
658 * @param cur_len Current length of data in the buffer
659 *
660 * of_octets_t is treated specially as the high level functions pass around
661 * pointers for "get" operators.
662 *
663 * @fixme Need to take into account cur_len
664 */
665
666static inline void
667of_wire_buffer_octets_data_set(of_wire_buffer_t *wbuf, int offset,
668 of_octets_t *value, int cur_len)
669{
670 // FIXME need to adjust length of octets member in buffer
Rich Lanee57f0432014-02-19 10:31:53 -0800671 LOCI_ASSERT(cur_len == 0 || cur_len == value->bytes);
Rich Lanea06d0c32013-03-25 08:52:03 -0700672
673 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + OF_OCTETS_BYTES_GET(value));
674 buf_octets_set(OF_WIRE_BUFFER_INDEX(wbuf, offset),
675 OF_OCTETS_POINTER_GET(value),
676 OF_OCTETS_BYTES_GET(value));
677}
678
679static inline void
680_wbuf_octets_set(of_wire_buffer_t *wbuf, int offset, uint8_t *src, int bytes) {
681 of_octets_t octets;
682 OF_OCTETS_POINTER_SET(&octets, src);
683 OF_OCTETS_BYTES_SET(&octets, bytes);
684 of_wire_buffer_octets_data_set(wbuf, offset, &octets, bytes);
685}
686
687static inline void
688_wbuf_octets_get(of_wire_buffer_t *wbuf, int offset, uint8_t *dst, int bytes) {
689 of_octets_t octets;
690 OF_OCTETS_POINTER_SET(&octets, dst);
691 OF_OCTETS_BYTES_SET(&octets, bytes);
692 of_wire_buffer_octets_data_get(wbuf, offset, &octets);
693}
694
695
696/**
697 * Get a MAC address from a wire buffer
698 * @param wbuf The pointer to the wire buffer structure
699 * @param offset Offset in the wire buffer
700 * @param mac Pointer to the mac address location
701 *
702 * Uses the octets function.
703 */
704
705#define of_wire_buffer_mac_get(buf, offset, mac) \
706 _wbuf_octets_get(buf, offset, (uint8_t *)mac, 6)
707
708/**
709 * Set a MAC address in a wire buffer
710 * @param wbuf The pointer to the wire buffer structure
711 * @param offset Offset in the wire buffer
712 * @param mac The variable holding the mac address to store
713 *
714 * Uses the octets function.
715 */
716
717#define of_wire_buffer_mac_set(buf, offset, mac) \
718 _wbuf_octets_set(buf, offset, (uint8_t *)&mac, 6)
719
720
721/**
722 * Get a port name string from a wire buffer
723 * @param wbuf The pointer to the wire buffer structure
724 * @param offset Offset in the wire buffer
725 * @param mac The mac address
726 *
727 * Uses the octets function.
728 */
729#define of_wire_buffer_port_name_get(buf, offset, portname) \
730 _wbuf_octets_get(buf, offset, (uint8_t *)portname, \
731 OF_MAX_PORT_NAME_LEN)
732
733/**
734 * Set a port name address in a wire buffer
735 * @param wbuf The pointer to the wire buffer structure
736 * @param offset Offset in the wire buffer
737 * @param portname Where to store the port name
738 *
739 * Uses the octets function.
740 */
741
742#define of_wire_buffer_port_name_set(buf, offset, portname) \
743 _wbuf_octets_set(buf, offset, (uint8_t *)portname, \
744 OF_MAX_PORT_NAME_LEN)
745
746
747/**
748 * Get a table name string from a wire buffer
749 * @param wbuf The pointer to the wire buffer structure
750 * @param offset Offset in the wire buffer
751 * @param portname The port name
752 *
753 * Uses the octets function.
754 */
755
756#define of_wire_buffer_tab_name_get(buf, offset, tabname) \
757 _wbuf_octets_get(buf, offset, (uint8_t *)tabname, \
758 OF_MAX_TABLE_NAME_LEN)
759
760/**
761 * Set a table name address in a wire buffer
762 * @param wbuf The pointer to the wire buffer structure
763 * @param offset Offset in the wire buffer
764 * @param mac Where to store the table name
765 *
766 * Uses the octets function.
767 */
768
769#define of_wire_buffer_tab_name_set(buf, offset, tabname) \
770 _wbuf_octets_set(buf, offset, (uint8_t *)tabname, \
771 OF_MAX_TABLE_NAME_LEN)
772
773/**
774 * Get a description string from a wire buffer
775 * @param wbuf The pointer to the wire buffer structure
776 * @param offset Offset in the wire buffer
777 * @param desc Where to store the description string
778 *
779 * Uses the octets function.
780 */
781
782#define of_wire_buffer_desc_str_get(buf, offset, desc) \
783 _wbuf_octets_get(buf, offset, (uint8_t *)desc, OF_DESC_STR_LEN)
784
785/**
786 * Set a description string in a wire buffer
787 * @param wbuf The pointer to the wire buffer structure
788 * @param offset Offset in the wire buffer
789 * @param desc The description string
790 *
791 * Uses the octets function.
792 */
793
794#define of_wire_buffer_desc_str_set(buf, offset, desc) \
795 _wbuf_octets_set(buf, offset, (uint8_t *)desc, OF_DESC_STR_LEN)
796
797/**
798 * Get a serial number string from a wire buffer
799 * @param wbuf The pointer to the wire buffer structure
800 * @param offset Offset in the wire buffer
801 * @param sernum Where to store the serial number string
802 *
803 * Uses the octets function.
804 */
805
806#define of_wire_buffer_ser_num_get(buf, offset, sernum) \
807 _wbuf_octets_get(buf, offset, (uint8_t *)sernum, OF_SERIAL_NUM_LEN)
808
809/**
810 * Set a serial number string in a wire buffer
811 * @param wbuf The pointer to the wire buffer structure
812 * @param offset Offset in the wire buffer
813 * @param desc The serial number string
814 *
815 * Uses the octets function.
816 */
817
818#define of_wire_buffer_ser_num_set(buf, offset, sernum) \
819 _wbuf_octets_set(buf, offset, (uint8_t *)sernum, OF_SERIAL_NUM_LEN)
820
821/**
Rich Lanef8a3d002014-03-19 13:33:52 -0700822 * Get a str64 string from a wire buffer
823 * @param wbuf The pointer to the wire buffer structure
824 * @param offset Offset in the wire buffer
825 * @param s The string
826 *
827 * Uses the octets function.
828 */
829
830#define of_wire_buffer_str64_get(buf, offset, s) \
831 _wbuf_octets_get(buf, offset, (uint8_t *)s, 64)
832
833/**
834 * Set a str64 string in a wire buffer
835 * @param wbuf The pointer to the wire buffer structure
836 * @param offset Offset in the wire buffer
837 * @param s Where to store the str64
838 *
839 * Uses the octets function.
840 */
841
842#define of_wire_buffer_str64_set(buf, offset, s) \
843 _wbuf_octets_set(buf, offset, (uint8_t *)s, 64)
844
845/**
Rich Lanea06d0c32013-03-25 08:52:03 -0700846 * Get an ipv6 address from a wire buffer
847 * @param wbuf The pointer to the wire buffer structure
848 * @param offset Offset in the wire buffer
849 * @param addr Pointer to where to store the ipv6 address
850 *
851 * Uses the octets function.
852 */
853
854#define of_wire_buffer_ipv6_get(buf, offset, addr) \
855 _wbuf_octets_get(buf, offset, (uint8_t *)addr, sizeof(of_ipv6_t))
856
857/**
858 * Set an ipv6 address in a wire buffer
859 * @param wbuf The pointer to the wire buffer structure
860 * @param offset Offset in the wire buffer
861 * @param addr The variable holding ipv6 address to store
862 *
863 * Uses the octets function.
864 */
865
866#define of_wire_buffer_ipv6_set(buf, offset, addr) \
867 _wbuf_octets_set(buf, offset, (uint8_t *)&addr, sizeof(of_ipv6_t))
868
Rich Lane3b2fd832013-09-24 13:44:08 -0700869/**
870 * Get an bitmap_128 address from a wire buffer
871 * @param wbuf The pointer to the wire buffer structure
872 * @param offset Offset in the wire buffer
873 * @param addr Pointer to where to store the bitmap_128 address
874 *
875 * Uses the octets function.
876 */
877
878#define of_wire_buffer_bitmap_128_get(buf, offset, addr) \
879 (of_wire_buffer_u64_get(buf, offset, &addr->hi), of_wire_buffer_u64_get(buf, offset+8, &addr->lo))
880
881/**
882 * Set an bitmap_128 address in a wire buffer
883 * @param wbuf The pointer to the wire buffer structure
884 * @param offset Offset in the wire buffer
885 * @param addr The variable holding bitmap_128 address to store
886 *
887 * Uses the octets function.
888 */
889
890#define of_wire_buffer_bitmap_128_set(buf, offset, addr) \
891 (of_wire_buffer_u64_set(buf, offset, addr.hi), of_wire_buffer_u64_set(buf, offset+8, addr.lo))
892
Rich Lanefab0c822013-12-30 11:46:48 -0800893/**
894 * Get a checksum_128 from a wire buffer
895 * @param wbuf The pointer to the wire buffer structure
896 * @param offset Offset in the wire buffer
897 * @param checksum Pointer to where to store the checksum_128
898 */
899
900#define of_wire_buffer_checksum_128_get(buf, offset, checksum) \
901 (of_wire_buffer_u64_get(buf, offset, &checksum->hi), of_wire_buffer_u64_get(buf, offset+8, &checksum->lo))
902
903/**
904 * Set a checksum_128 in a wire buffer
905 * @param wbuf The pointer to the wire buffer structure
906 * @param offset Offset in the wire buffer
907 * @param checksum The variable holding checksum_128 to store
908 */
909
910#define of_wire_buffer_checksum_128_set(buf, offset, checksum) \
911 (of_wire_buffer_u64_set(buf, offset, checksum.hi), of_wire_buffer_u64_set(buf, offset+8, checksum.lo))
912
Rich Lanea06d0c32013-03-25 08:52:03 -0700913/* Relocate data from start offset to the end of the buffer to a new position */
914static inline void
915of_wire_buffer_move_end(of_wire_buffer_t *wbuf, int start_offset, int new_offset)
916{
917 int bytes;
918 int new_length;
919
920 if (new_offset > start_offset) {
921 bytes = new_offset - start_offset;
922 new_length = wbuf->alloc_bytes + bytes;
923 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, new_length);
924 } else {
925 bytes = start_offset - new_offset;
926 new_length = wbuf->alloc_bytes - bytes;
927 }
928
929 MEMMOVE(&wbuf->buf[new_offset], &wbuf->buf[start_offset], bytes);
930 wbuf->alloc_bytes = new_length;
931}
932
933/* Given a wire buffer object and the offset of the start of an of_match struct,
934 * return its total length in the buffer
935 */
936static inline int
937of_match_bytes(of_wire_buffer_t *wbuf, int offset) {
938 uint16_t len;
939 of_wire_buffer_u16_get(wbuf, offset + 2, &len);
940 return OF_MATCH_BYTES(len);
941}
942
943extern void
944of_wire_buffer_replace_data(of_wire_buffer_t *wbuf,
945 int offset,
946 int old_len,
947 uint8_t *data,
948 int new_len);
949
950#endif /* _OF_WIRE_BUF_H_ */