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