]> git.proxmox.com Git - ovs.git/blob - lib/unaligned.h
ofproto-dpif: Print slow-path actions instead of "drop" in dump-flows.
[ovs.git] / lib / unaligned.h
1 /*
2 * Copyright (c) 2010, 2011 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 UNALIGNED_H
18 #define UNALIGNED_H 1
19
20 #include <stdint.h>
21 #include "byte-order.h"
22 #include "openvswitch/types.h"
23 #include "type-props.h"
24 #include "util.h"
25
26 /* Public API. */
27 static inline uint16_t get_unaligned_u16(const uint16_t *);
28 static inline uint32_t get_unaligned_u32(const uint32_t *);
29 static inline void put_unaligned_u16(uint16_t *, uint16_t);
30 static inline void put_unaligned_u32(uint32_t *, uint32_t);
31 static inline void put_unaligned_u64(uint64_t *, uint64_t);
32
33 static inline ovs_be16 get_unaligned_be16(const ovs_be16 *);
34 static inline ovs_be32 get_unaligned_be32(const ovs_be32 *);
35 static inline ovs_be64 get_unaligned_be64(const ovs_be64 *);
36 static inline void put_unaligned_be16(ovs_be16 *, ovs_be16);
37 static inline void put_unaligned_be32(ovs_be32 *, ovs_be32);
38 static inline void put_unaligned_be64(ovs_be64 *, ovs_be64);
39
40 #ifdef __GNUC__
41 /* GCC implementations. */
42 #define GCC_UNALIGNED_ACCESSORS(TYPE, ABBREV) \
43 struct unaligned_##ABBREV { \
44 TYPE x __attribute__((__packed__)); \
45 }; \
46 static inline struct unaligned_##ABBREV * \
47 unaligned_##ABBREV(const TYPE *p) \
48 { \
49 return (struct unaligned_##ABBREV *) p; \
50 } \
51 \
52 static inline TYPE \
53 get_unaligned_##ABBREV(const TYPE *p) \
54 { \
55 return unaligned_##ABBREV(p)->x; \
56 } \
57 \
58 static inline void \
59 put_unaligned_##ABBREV(TYPE *p, TYPE x) \
60 { \
61 unaligned_##ABBREV(p)->x = x; \
62 }
63
64 GCC_UNALIGNED_ACCESSORS(uint16_t, u16);
65 GCC_UNALIGNED_ACCESSORS(uint32_t, u32);
66 GCC_UNALIGNED_ACCESSORS(uint64_t, u64__); /* Special case: see below. */
67
68 GCC_UNALIGNED_ACCESSORS(ovs_be16, be16);
69 GCC_UNALIGNED_ACCESSORS(ovs_be32, be32);
70 GCC_UNALIGNED_ACCESSORS(ovs_be64, be64);
71 #else
72 /* Generic implementations. */
73
74 static inline uint16_t get_unaligned_u16(const uint16_t *p_)
75 {
76 const uint8_t *p = (const uint8_t *) p_;
77 return ntohs((p[0] << 8) | p[1]);
78 }
79
80 static inline void put_unaligned_u16(uint16_t *p_, uint16_t x_)
81 {
82 uint8_t *p = (uint8_t *) p_;
83 uint16_t x = ntohs(x_);
84
85 p[0] = x >> 8;
86 p[1] = x;
87 }
88
89 static inline uint32_t get_unaligned_u32(const uint32_t *p_)
90 {
91 const uint8_t *p = (const uint8_t *) p_;
92 return ntohl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
93 }
94
95 static inline void put_unaligned_u32(uint32_t *p_, uint32_t x_)
96 {
97 uint8_t *p = (uint8_t *) p_;
98 uint32_t x = ntohl(x_);
99
100 p[0] = x >> 24;
101 p[1] = x >> 16;
102 p[2] = x >> 8;
103 p[3] = x;
104 }
105
106 static inline uint64_t get_unaligned_u64__(const uint64_t *p_)
107 {
108 const uint8_t *p = (const uint8_t *) p_;
109 return ntohll(((uint64_t) p[0] << 56)
110 | ((uint64_t) p[1] << 48)
111 | ((uint64_t) p[2] << 40)
112 | ((uint64_t) p[3] << 32)
113 | (p[4] << 24)
114 | (p[5] << 16)
115 | (p[6] << 8)
116 | p[7]);
117 }
118
119 static inline void put_unaligned_u64__(uint64_t *p_, uint64_t x_)
120 {
121 uint8_t *p = (uint8_t *) p_;
122 uint64_t x = ntohll(x_);
123
124 p[0] = x >> 56;
125 p[1] = x >> 48;
126 p[2] = x >> 40;
127 p[3] = x >> 32;
128 p[4] = x >> 24;
129 p[5] = x >> 16;
130 p[6] = x >> 8;
131 p[7] = x;
132 }
133
134 /* Only sparse cares about the difference between uint<N>_t and ovs_be<N>, and
135 * that takes the GCC branch, so there's no point in working too hard on these
136 * accessors. */
137 #define get_unaligned_be16 get_unaligned_u16
138 #define get_unaligned_be32 get_unaligned_u32
139 #define get_unaligned_be64 get_unaligned_u64
140 #define put_unaligned_be16 put_unaligned_u16
141 #define put_unaligned_be32 put_unaligned_u32
142 #define put_unaligned_be64 put_unaligned_u64
143 #endif
144
145 /* uint64_t get_unaligned_u64(uint64_t *p);
146 *
147 * Returns the value of the possibly misaligned uint64_t at 'p'. 'p' may
148 * actually be any type that points to a 64-bit integer. That is, on Unix-like
149 * 32-bit ABIs, it may point to an "unsigned long long int", and on Unix-like
150 * 64-bit ABIs, it may point to an "unsigned long int" or an "unsigned long
151 * long int".
152 *
153 * This is special-cased because on some Linux targets, the kernel __u64 is
154 * unsigned long long int and the userspace uint64_t is unsigned long int, so
155 * that any single function prototype would fail to accept one or the other.
156 *
157 * Below, "sizeof (*(P) % 1)" verifies that *P has an integer type, since
158 * operands to % must be integers.
159 */
160 #define get_unaligned_u64(P) \
161 (BUILD_ASSERT(sizeof *(P) == 8), \
162 BUILD_ASSERT_GCCONLY(!TYPE_IS_SIGNED(typeof(*(P)))), \
163 (void) sizeof (*(P) % 1), \
164 get_unaligned_u64__((const uint64_t *) (P)))
165
166 /* Stores 'x' at possibly misaligned address 'p'.
167 *
168 * put_unaligned_u64() could be overloaded in the same way as
169 * get_unaligned_u64(), but so far it has not proven necessary.
170 */
171 static inline void
172 put_unaligned_u64(uint64_t *p, uint64_t x)
173 {
174 put_unaligned_u64__(p, x);
175 }
176 \f
177 /* Returns the value in 'x'. */
178 static inline uint64_t
179 get_32aligned_u64(const ovs_32aligned_u64 *x)
180 {
181 return ((uint64_t) x->hi << 32) | x->lo;
182 }
183
184 /* Stores 'value' in 'x'. */
185 static inline void
186 put_32aligned_u64(ovs_32aligned_u64 *x, uint64_t value)
187 {
188 x->hi = value >> 32;
189 x->lo = value;
190 }
191
192 #ifndef __CHECKER__
193 /* Returns the value of 'x'. */
194 static inline ovs_be64
195 get_32aligned_be64(const ovs_32aligned_be64 *x)
196 {
197 #ifdef WORDS_BIGENDIAN
198 return ((ovs_be64) x->hi << 32) | x->lo;
199 #else
200 return ((ovs_be64) x->lo << 32) | x->hi;
201 #endif
202 }
203
204 /* Stores network byte order 'value' into 'x'. */
205 static inline void
206 put_32aligned_be64(ovs_32aligned_be64 *x, ovs_be64 value)
207 {
208 #if WORDS_BIGENDIAN
209 x->hi = value >> 32;
210 x->lo = value;
211 #else
212 x->hi = value;
213 x->lo = value >> 32;
214 #endif
215 }
216 #else /* __CHECKER__ */
217 /* Making sparse happy with these functions also makes them unreadable, so
218 * don't bother to show it their implementations. */
219 ovs_be64 get_32aligned_be64(const ovs_32aligned_be64 *);
220 void put_32aligned_be64(ovs_32aligned_be64 *, ovs_be64);
221 #endif
222
223 #endif /* unaligned.h */