match: Only print tp_src and tp_dst for TCP and UDP.
[sliver-openvswitch.git] / lib / ofpbuf.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef OFPBUF_H
18 #define OFPBUF_H 1
19
20 #include <stddef.h>
21 #include <stdint.h>
22 #include "list.h"
23 #include "util.h"
24
25 #ifdef  __cplusplus
26 extern "C" {
27 #endif
28
29 enum ofpbuf_source {
30     OFPBUF_MALLOC,              /* Obtained via malloc(). */
31     OFPBUF_STACK,               /* Un-movable stack space or static buffer. */
32     OFPBUF_STUB                 /* Starts on stack, may expand into heap. */
33 };
34
35 /* Buffer for holding arbitrary data.  An ofpbuf is automatically reallocated
36  * as necessary if it grows too large for the available memory. */
37 struct ofpbuf {
38     void *base;                 /* First byte of allocated space. */
39     size_t allocated;           /* Number of bytes allocated. */
40     enum ofpbuf_source source;  /* Source of memory allocated as 'base'. */
41
42     void *data;                 /* First byte actually in use. */
43     size_t size;                /* Number of bytes in use. */
44
45     void *l2;                   /* Link-level header. */
46     void *l2_5;                 /* MPLS label stack */
47     void *l3;                   /* Network-level header. */
48     void *l4;                   /* Transport-level header. */
49     void *l7;                   /* Application data. */
50
51     struct list list_node;      /* Private list element for use by owner. */
52     void *private_p;            /* Private pointer for use by owner. */
53 };
54
55 /* Declares NAME as a SIZE-byte array aligned properly for storing any kind of
56  * data.  For use with ofpbuf_use_stack(). */
57 #define OFPBUF_STACK_BUFFER(NAME, SIZE) uint64_t NAME[DIV_ROUND_UP(SIZE, 8)]
58
59 void ofpbuf_use(struct ofpbuf *, void *, size_t);
60 void ofpbuf_use_stack(struct ofpbuf *, void *, size_t);
61 void ofpbuf_use_stub(struct ofpbuf *, void *, size_t);
62 void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
63
64 void ofpbuf_init(struct ofpbuf *, size_t);
65 void ofpbuf_uninit(struct ofpbuf *);
66 void *ofpbuf_get_uninit_pointer(struct ofpbuf *);
67 void ofpbuf_reinit(struct ofpbuf *, size_t);
68
69 struct ofpbuf *ofpbuf_new(size_t);
70 struct ofpbuf *ofpbuf_new_with_headroom(size_t, size_t headroom);
71 struct ofpbuf *ofpbuf_clone(const struct ofpbuf *);
72 struct ofpbuf *ofpbuf_clone_with_headroom(const struct ofpbuf *,
73                                           size_t headroom);
74 struct ofpbuf *ofpbuf_clone_data(const void *, size_t);
75 struct ofpbuf *ofpbuf_clone_data_with_headroom(const void *, size_t,
76                                                size_t headroom);
77 void ofpbuf_delete(struct ofpbuf *);
78
79 void *ofpbuf_at(const struct ofpbuf *, size_t offset, size_t size);
80 void *ofpbuf_at_assert(const struct ofpbuf *, size_t offset, size_t size);
81 void *ofpbuf_tail(const struct ofpbuf *);
82 void *ofpbuf_end(const struct ofpbuf *);
83
84 void *ofpbuf_put_uninit(struct ofpbuf *, size_t);
85 void *ofpbuf_put_zeros(struct ofpbuf *, size_t);
86 void *ofpbuf_put(struct ofpbuf *, const void *, size_t);
87 char *ofpbuf_put_hex(struct ofpbuf *, const char *s, size_t *n);
88 void ofpbuf_reserve(struct ofpbuf *, size_t);
89 void *ofpbuf_push_uninit(struct ofpbuf *b, size_t);
90 void *ofpbuf_push_zeros(struct ofpbuf *, size_t);
91 void *ofpbuf_push(struct ofpbuf *b, const void *, size_t);
92
93 size_t ofpbuf_headroom(const struct ofpbuf *);
94 size_t ofpbuf_tailroom(const struct ofpbuf *);
95 void ofpbuf_prealloc_headroom(struct ofpbuf *, size_t);
96 void ofpbuf_prealloc_tailroom(struct ofpbuf *, size_t);
97 void ofpbuf_trim(struct ofpbuf *);
98 void ofpbuf_padto(struct ofpbuf *, size_t);
99
100 void ofpbuf_clear(struct ofpbuf *);
101 void *ofpbuf_pull(struct ofpbuf *, size_t);
102 void *ofpbuf_try_pull(struct ofpbuf *, size_t);
103
104 void *ofpbuf_steal_data(struct ofpbuf *);
105
106 char *ofpbuf_to_string(const struct ofpbuf *, size_t maxbytes);
107
108 static inline struct ofpbuf *ofpbuf_from_list(const struct list *list)
109 {
110     return CONTAINER_OF(list, struct ofpbuf, list_node);
111 }
112 void ofpbuf_list_delete(struct list *);
113
114 #ifdef  __cplusplus
115 }
116 #endif
117
118 #endif /* ofpbuf.h */