blob: 9ade2c42d53abeed15bf492af246ba2cf23c3b7b [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;
Rich Lanefacd0a42014-10-14 11:18:40 -0700472 default:
Rich Lanea06d0c32013-03-25 08:52:03 -0700473 of_wire_buffer_u32_get(wbuf, offset, &v32);
474 *value = v32;
475 break;
Rich Lanea06d0c32013-03-25 08:52:03 -0700476 }
477}
478
479/**
480 * Set a port number scalar from a wire buffer
481 * @param wbuf The pointer to the wire buffer structure
482 * @param offset Offset in the wire buffer
483 * @param value The value to store in the buffer
484 *
485 * Port numbers are version specific.
486 */
487
488static inline void
489of_wire_buffer_port_no_set(int version, of_wire_buffer_t *wbuf, int offset,
490 of_port_no_t value)
491{
492
493 switch (version) {
494 case OF_VERSION_1_0:
495 of_wire_buffer_u16_set(wbuf, offset, (uint16_t)value);
496 break;
Rich Lanefacd0a42014-10-14 11:18:40 -0700497 default:
Rich Lanea06d0c32013-03-25 08:52:03 -0700498 of_wire_buffer_u32_set(wbuf, offset, (uint32_t)value);
499 break;
Rich Lanea06d0c32013-03-25 08:52:03 -0700500 }
501}
502
503/**
504 * Get a flow mod command value from a wire buffer
505 * @param wbuf The pointer to the wire buffer structure
506 * @param offset Offset in the wire buffer
507 * @param value Pointer to where to put value
508 */
509
510static inline void
511of_wire_buffer_fm_cmd_get(int version, of_wire_buffer_t *wbuf, int offset,
512 of_fm_cmd_t *value)
513{
514 uint16_t v16;
515 uint8_t v8;
516
517 switch (version) {
518 case OF_VERSION_1_0:
519 of_wire_buffer_u16_get(wbuf, offset, &v16);
520 *value = v16;
521 break;
Rich Lanefacd0a42014-10-14 11:18:40 -0700522 default:
Rich Lanea06d0c32013-03-25 08:52:03 -0700523 of_wire_buffer_u8_get(wbuf, offset, &v8);
524 *value = v8;
525 break;
Rich Lanea06d0c32013-03-25 08:52:03 -0700526 }
527}
528
529/**
530 * Set a flow mod command value in a wire buffer
531 * @param wbuf The pointer to the wire buffer structure
532 * @param offset Offset in the wire buffer
533 * @param value The value to store
534 */
535
536static inline void
537of_wire_buffer_fm_cmd_set(int version, of_wire_buffer_t *wbuf, int offset,
538 of_fm_cmd_t value)
539{
540 switch (version) {
541 case OF_VERSION_1_0:
542 of_wire_buffer_u16_set(wbuf, offset, (uint16_t)value);
543 break;
Rich Lanefacd0a42014-10-14 11:18:40 -0700544 default:
Rich Lanea06d0c32013-03-25 08:52:03 -0700545 of_wire_buffer_u8_set(wbuf, offset, (uint8_t)value);
546 break;
Rich Lanea06d0c32013-03-25 08:52:03 -0700547 }
548}
549
550/**
551 * Get a wild card bitmap value from a wire buffer
552 * @param wbuf The pointer to the wire buffer structure
553 * @param offset Offset in the wire buffer
554 * @param value Pointer to where to store the value
555 */
556
557static inline void
558of_wire_buffer_wc_bmap_get(int version, of_wire_buffer_t *wbuf, int offset,
559 of_wc_bmap_t *value)
560{
561 uint32_t v32;
562 uint64_t v64;
563
564 switch (version) {
565 case OF_VERSION_1_0:
566 case OF_VERSION_1_1:
567 of_wire_buffer_u32_get(wbuf, offset, &v32);
568 *value = v32;
569 break;
Rich Lanefacd0a42014-10-14 11:18:40 -0700570 default:
Rich Lanea06d0c32013-03-25 08:52:03 -0700571 of_wire_buffer_u64_get(wbuf, offset, &v64);
572 *value = v64;
573 break;
Rich Lanea06d0c32013-03-25 08:52:03 -0700574 }
575}
576
577/**
578 * Set a wild card bitmap value in a wire buffer
579 * @param wbuf The pointer to the wire buffer structure
580 * @param offset Offset in the wire buffer
581 * @param value The value to store
582 */
583
584static inline void
585of_wire_buffer_wc_bmap_set(int version, of_wire_buffer_t *wbuf, int offset,
586 of_wc_bmap_t value)
587{
588 switch (version) {
589 case OF_VERSION_1_0:
590 case OF_VERSION_1_1:
591 of_wire_buffer_u32_set(wbuf, offset, (uint32_t)value);
592 break;
Rich Lanefacd0a42014-10-14 11:18:40 -0700593 default:
Rich Lanea06d0c32013-03-25 08:52:03 -0700594 of_wire_buffer_u64_set(wbuf, offset, (uint64_t)value);
595 break;
Rich Lanea06d0c32013-03-25 08:52:03 -0700596 }
597}
598
599/* match bitmap and wildcard bitmaps followed the same pattern */
600#define of_wire_buffer_match_bmap_get of_wire_buffer_wc_bmap_get
601#define of_wire_buffer_match_bmap_set of_wire_buffer_wc_bmap_set
602
603/* Derived functions, mostly for fixed length name strings */
604#define of_wire_buffer_char_get of_wire_buffer_u8_get
605#define of_wire_buffer_char_set of_wire_buffer_u8_set
606
607
608/**
609 * Get an octet object from a wire buffer
610 * @param wbuf The pointer to the wire buffer structure
611 * @param offset Offset in the wire buffer
612 * @param value Pointer to where to put value
613 *
614 * of_octets_t is treated specially as the high level functions pass around
615 * pointers for "get" operators.
616 *
617 * Important: The length of data to copy is stored in the value->bytes
618 * variable.
619 */
620
621static inline void
622of_wire_buffer_octets_data_get(of_wire_buffer_t *wbuf, int offset,
623 of_octets_t *value)
624{
625 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + OF_OCTETS_BYTES_GET(value));
626 buf_octets_get(OF_WIRE_BUFFER_INDEX(wbuf, offset),
627 OF_OCTETS_POINTER_GET(value),
628 OF_OCTETS_BYTES_GET(value));
629}
630
631/**
632 * Set an octet object in a wire buffer
633 * @param wbuf The pointer to the wire buffer structure
634 * @param offset Offset in the wire buffer
635 * @param value Pointer to the octet stream to store
636 * @param cur_len Current length of data in the buffer
637 *
638 * of_octets_t is treated specially as the high level functions pass around
639 * pointers for "get" operators.
640 *
641 * @fixme Need to take into account cur_len
642 */
643
644static inline void
645of_wire_buffer_octets_data_set(of_wire_buffer_t *wbuf, int offset,
646 of_octets_t *value, int cur_len)
647{
648 // FIXME need to adjust length of octets member in buffer
Rich Lanee57f0432014-02-19 10:31:53 -0800649 LOCI_ASSERT(cur_len == 0 || cur_len == value->bytes);
Rich Lanea06d0c32013-03-25 08:52:03 -0700650
651 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + OF_OCTETS_BYTES_GET(value));
652 buf_octets_set(OF_WIRE_BUFFER_INDEX(wbuf, offset),
653 OF_OCTETS_POINTER_GET(value),
654 OF_OCTETS_BYTES_GET(value));
655}
656
657static inline void
658_wbuf_octets_set(of_wire_buffer_t *wbuf, int offset, uint8_t *src, int bytes) {
659 of_octets_t octets;
660 OF_OCTETS_POINTER_SET(&octets, src);
661 OF_OCTETS_BYTES_SET(&octets, bytes);
662 of_wire_buffer_octets_data_set(wbuf, offset, &octets, bytes);
663}
664
665static inline void
666_wbuf_octets_get(of_wire_buffer_t *wbuf, int offset, uint8_t *dst, int bytes) {
667 of_octets_t octets;
668 OF_OCTETS_POINTER_SET(&octets, dst);
669 OF_OCTETS_BYTES_SET(&octets, bytes);
670 of_wire_buffer_octets_data_get(wbuf, offset, &octets);
671}
672
673
674/**
675 * Get a MAC address from a wire buffer
676 * @param wbuf The pointer to the wire buffer structure
677 * @param offset Offset in the wire buffer
678 * @param mac Pointer to the mac address location
679 *
680 * Uses the octets function.
681 */
682
683#define of_wire_buffer_mac_get(buf, offset, mac) \
684 _wbuf_octets_get(buf, offset, (uint8_t *)mac, 6)
685
686/**
687 * Set a MAC address in a wire buffer
688 * @param wbuf The pointer to the wire buffer structure
689 * @param offset Offset in the wire buffer
690 * @param mac The variable holding the mac address to store
691 *
692 * Uses the octets function.
693 */
694
695#define of_wire_buffer_mac_set(buf, offset, mac) \
696 _wbuf_octets_set(buf, offset, (uint8_t *)&mac, 6)
697
698
699/**
700 * Get a port name string from a wire buffer
701 * @param wbuf The pointer to the wire buffer structure
702 * @param offset Offset in the wire buffer
703 * @param mac The mac address
704 *
705 * Uses the octets function.
706 */
707#define of_wire_buffer_port_name_get(buf, offset, portname) \
708 _wbuf_octets_get(buf, offset, (uint8_t *)portname, \
709 OF_MAX_PORT_NAME_LEN)
710
711/**
712 * Set a port name address in a wire buffer
713 * @param wbuf The pointer to the wire buffer structure
714 * @param offset Offset in the wire buffer
715 * @param portname Where to store the port name
716 *
717 * Uses the octets function.
718 */
719
720#define of_wire_buffer_port_name_set(buf, offset, portname) \
721 _wbuf_octets_set(buf, offset, (uint8_t *)portname, \
722 OF_MAX_PORT_NAME_LEN)
723
724
725/**
726 * Get a table name string from a wire buffer
727 * @param wbuf The pointer to the wire buffer structure
728 * @param offset Offset in the wire buffer
729 * @param portname The port name
730 *
731 * Uses the octets function.
732 */
733
734#define of_wire_buffer_tab_name_get(buf, offset, tabname) \
735 _wbuf_octets_get(buf, offset, (uint8_t *)tabname, \
736 OF_MAX_TABLE_NAME_LEN)
737
738/**
739 * Set a table name address in a wire buffer
740 * @param wbuf The pointer to the wire buffer structure
741 * @param offset Offset in the wire buffer
742 * @param mac Where to store the table name
743 *
744 * Uses the octets function.
745 */
746
747#define of_wire_buffer_tab_name_set(buf, offset, tabname) \
748 _wbuf_octets_set(buf, offset, (uint8_t *)tabname, \
749 OF_MAX_TABLE_NAME_LEN)
750
751/**
752 * Get a description string from a wire buffer
753 * @param wbuf The pointer to the wire buffer structure
754 * @param offset Offset in the wire buffer
755 * @param desc Where to store the description string
756 *
757 * Uses the octets function.
758 */
759
760#define of_wire_buffer_desc_str_get(buf, offset, desc) \
761 _wbuf_octets_get(buf, offset, (uint8_t *)desc, OF_DESC_STR_LEN)
762
763/**
764 * Set a description string in a wire buffer
765 * @param wbuf The pointer to the wire buffer structure
766 * @param offset Offset in the wire buffer
767 * @param desc The description string
768 *
769 * Uses the octets function.
770 */
771
772#define of_wire_buffer_desc_str_set(buf, offset, desc) \
773 _wbuf_octets_set(buf, offset, (uint8_t *)desc, OF_DESC_STR_LEN)
774
775/**
776 * Get a serial number 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 sernum Where to store the serial number string
780 *
781 * Uses the octets function.
782 */
783
784#define of_wire_buffer_ser_num_get(buf, offset, sernum) \
785 _wbuf_octets_get(buf, offset, (uint8_t *)sernum, OF_SERIAL_NUM_LEN)
786
787/**
788 * Set a serial number 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 serial number string
792 *
793 * Uses the octets function.
794 */
795
796#define of_wire_buffer_ser_num_set(buf, offset, sernum) \
797 _wbuf_octets_set(buf, offset, (uint8_t *)sernum, OF_SERIAL_NUM_LEN)
798
799/**
Rich Lanef8a3d002014-03-19 13:33:52 -0700800 * Get a str64 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 s The string
804 *
805 * Uses the octets function.
806 */
807
808#define of_wire_buffer_str64_get(buf, offset, s) \
809 _wbuf_octets_get(buf, offset, (uint8_t *)s, 64)
810
811/**
812 * Set a str64 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 s Where to store the str64
816 *
817 * Uses the octets function.
818 */
819
820#define of_wire_buffer_str64_set(buf, offset, s) \
821 _wbuf_octets_set(buf, offset, (uint8_t *)s, 64)
822
823/**
Rich Lanea06d0c32013-03-25 08:52:03 -0700824 * 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
Rich Lane3b2fd832013-09-24 13:44:08 -0700847/**
848 * Get an bitmap_128 address from a wire buffer
849 * @param wbuf The pointer to the wire buffer structure
850 * @param offset Offset in the wire buffer
851 * @param addr Pointer to where to store the bitmap_128 address
852 *
853 * Uses the octets function.
854 */
855
856#define of_wire_buffer_bitmap_128_get(buf, offset, addr) \
857 (of_wire_buffer_u64_get(buf, offset, &addr->hi), of_wire_buffer_u64_get(buf, offset+8, &addr->lo))
858
859/**
860 * Set an bitmap_128 address in a wire buffer
861 * @param wbuf The pointer to the wire buffer structure
862 * @param offset Offset in the wire buffer
863 * @param addr The variable holding bitmap_128 address to store
864 *
865 * Uses the octets function.
866 */
867
868#define of_wire_buffer_bitmap_128_set(buf, offset, addr) \
869 (of_wire_buffer_u64_set(buf, offset, addr.hi), of_wire_buffer_u64_set(buf, offset+8, addr.lo))
870
Rich Lanefab0c822013-12-30 11:46:48 -0800871/**
872 * Get a checksum_128 from a wire buffer
873 * @param wbuf The pointer to the wire buffer structure
874 * @param offset Offset in the wire buffer
875 * @param checksum Pointer to where to store the checksum_128
876 */
877
878#define of_wire_buffer_checksum_128_get(buf, offset, checksum) \
879 (of_wire_buffer_u64_get(buf, offset, &checksum->hi), of_wire_buffer_u64_get(buf, offset+8, &checksum->lo))
880
881/**
882 * Set a checksum_128 in a wire buffer
883 * @param wbuf The pointer to the wire buffer structure
884 * @param offset Offset in the wire buffer
885 * @param checksum The variable holding checksum_128 to store
886 */
887
888#define of_wire_buffer_checksum_128_set(buf, offset, checksum) \
889 (of_wire_buffer_u64_set(buf, offset, checksum.hi), of_wire_buffer_u64_set(buf, offset+8, checksum.lo))
890
Rich Lane119289c2014-12-01 13:44:08 -0800891
892/**
893 * Get a bitmap_512 from a wire buffer
894 * @param wbuf The pointer to the wire buffer structure
895 * @param offset Offset in the wire buffer
896 * @param value Pointer to where to put value
897 *
898 * The underlying buffer accessor funtions handle endian and alignment.
899 */
900
901static inline void
902of_wire_buffer_bitmap_512_get(of_wire_buffer_t *wbuf, int offset, of_bitmap_512_t *value)
903{
904 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(of_bitmap_512_t));
905 int i;
906 for (i = 0; i < 8; i++) {
907 buf_u64_get(OF_WIRE_BUFFER_INDEX(wbuf, offset+i*8), &value->words[i]);
908 }
909}
910
911/**
912 * Set a bitmap_512 in a wire buffer
913 * @param wbuf The pointer to the wire buffer structure
914 * @param offset Offset in the wire buffer
915 * @param value The value to store
916 *
917 * The underlying buffer accessor funtions handle endian and alignment.
918 */
919
920static inline void
921of_wire_buffer_bitmap_512_set(of_wire_buffer_t *wbuf, int offset, of_bitmap_512_t value)
922{
923 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, offset + (int) sizeof(of_bitmap_512_t));
924 int i;
925 for (i = 0; i < 8; i++) {
926 buf_u64_set(OF_WIRE_BUFFER_INDEX(wbuf, offset+i*8), value.words[i]);
927 }
928}
929
Rich Lanea06d0c32013-03-25 08:52:03 -0700930/* Relocate data from start offset to the end of the buffer to a new position */
931static inline void
932of_wire_buffer_move_end(of_wire_buffer_t *wbuf, int start_offset, int new_offset)
933{
934 int bytes;
935 int new_length;
936
937 if (new_offset > start_offset) {
938 bytes = new_offset - start_offset;
939 new_length = wbuf->alloc_bytes + bytes;
940 OF_WIRE_BUFFER_ACCESS_CHECK(wbuf, new_length);
941 } else {
942 bytes = start_offset - new_offset;
943 new_length = wbuf->alloc_bytes - bytes;
944 }
945
946 MEMMOVE(&wbuf->buf[new_offset], &wbuf->buf[start_offset], bytes);
947 wbuf->alloc_bytes = new_length;
948}
949
950/* Given a wire buffer object and the offset of the start of an of_match struct,
951 * return its total length in the buffer
952 */
953static inline int
954of_match_bytes(of_wire_buffer_t *wbuf, int offset) {
955 uint16_t len;
956 of_wire_buffer_u16_get(wbuf, offset + 2, &len);
957 return OF_MATCH_BYTES(len);
958}
959
960extern void
961of_wire_buffer_replace_data(of_wire_buffer_t *wbuf,
962 int offset,
963 int old_len,
964 uint8_t *data,
965 int new_len);
966
967#endif /* _OF_WIRE_BUF_H_ */