]> git.proxmox.com Git - mirror_ovs.git/blame - lib/unaligned.h
ovsdb-idl: Fix iteration over tracked rows with no actual data.
[mirror_ovs.git] / lib / unaligned.h
CommitLineData
afa3a931 1/*
79461d20 2 * Copyright (c) 2010, 2011, 2014 Nicira, Inc.
afa3a931
BP
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>
10a24935 21#include "byte-order.h"
9ea5d2d5 22#include "openvswitch/types.h"
ae06a561 23#include "openvswitch/type-props.h"
320232ec 24#include "util.h"
afa3a931
BP
25
26/* Public API. */
27static inline uint16_t get_unaligned_u16(const uint16_t *);
28static inline uint32_t get_unaligned_u32(const uint32_t *);
afa3a931
BP
29static inline void put_unaligned_u16(uint16_t *, uint16_t);
30static inline void put_unaligned_u32(uint32_t *, uint32_t);
31static inline void put_unaligned_u64(uint64_t *, uint64_t);
32
9ea5d2d5
BP
33static inline ovs_be16 get_unaligned_be16(const ovs_be16 *);
34static inline ovs_be32 get_unaligned_be32(const ovs_be32 *);
35static inline ovs_be64 get_unaligned_be64(const ovs_be64 *);
36static inline void put_unaligned_be16(ovs_be16 *, ovs_be16);
37static inline void put_unaligned_be32(ovs_be32 *, ovs_be32);
38static inline void put_unaligned_be64(ovs_be64 *, ovs_be64);
39
79461d20
BP
40/* uint64_t get_unaligned_u64(uint64_t *p);
41 *
42 * Returns the value of the possibly misaligned uint64_t at 'p'. 'p' may
43 * actually be any type that points to a 64-bit integer. That is, on Unix-like
44 * 32-bit ABIs, it may point to an "unsigned long long int", and on Unix-like
45 * 64-bit ABIs, it may point to an "unsigned long int" or an "unsigned long
46 * long int".
47 *
48 * This is special-cased because on some Linux targets, the kernel __u64 is
49 * unsigned long long int and the userspace uint64_t is unsigned long int, so
50 * that any single function prototype would fail to accept one or the other.
51 *
52 * Below, "sizeof (*(P) % 1)" verifies that *P has an integer type, since
53 * operands to % must be integers.
54 */
55#define get_unaligned_u64(P) \
56 (BUILD_ASSERT(sizeof *(P) == 8), \
57 BUILD_ASSERT_GCCONLY(!TYPE_IS_SIGNED(typeof(*(P)))), \
58 (void) sizeof (*(P) % 1), \
59 get_unaligned_u64__((const uint64_t *) (P)))
60
afa3a931
BP
61#ifdef __GNUC__
62/* GCC implementations. */
9ea5d2d5
BP
63#define GCC_UNALIGNED_ACCESSORS(TYPE, ABBREV) \
64struct unaligned_##ABBREV { \
65 TYPE x __attribute__((__packed__)); \
66}; \
67static inline struct unaligned_##ABBREV * \
68unaligned_##ABBREV(const TYPE *p) \
69{ \
70 return (struct unaligned_##ABBREV *) p; \
71} \
72 \
73static inline TYPE \
74get_unaligned_##ABBREV(const TYPE *p) \
75{ \
76 return unaligned_##ABBREV(p)->x; \
77} \
78 \
79static inline void \
80put_unaligned_##ABBREV(TYPE *p, TYPE x) \
81{ \
82 unaligned_##ABBREV(p)->x = x; \
afa3a931
BP
83}
84
9ea5d2d5
BP
85GCC_UNALIGNED_ACCESSORS(uint16_t, u16);
86GCC_UNALIGNED_ACCESSORS(uint32_t, u32);
320232ec 87GCC_UNALIGNED_ACCESSORS(uint64_t, u64__); /* Special case: see below. */
9ea5d2d5
BP
88
89GCC_UNALIGNED_ACCESSORS(ovs_be16, be16);
90GCC_UNALIGNED_ACCESSORS(ovs_be32, be32);
91GCC_UNALIGNED_ACCESSORS(ovs_be64, be64);
afa3a931
BP
92#else
93/* Generic implementations. */
94
95static inline uint16_t get_unaligned_u16(const uint16_t *p_)
96{
97 const uint8_t *p = (const uint8_t *) p_;
98 return ntohs((p[0] << 8) | p[1]);
99}
100
101static inline void put_unaligned_u16(uint16_t *p_, uint16_t x_)
102{
103 uint8_t *p = (uint8_t *) p_;
104 uint16_t x = ntohs(x_);
105
106 p[0] = x >> 8;
107 p[1] = x;
108}
109
110static inline uint32_t get_unaligned_u32(const uint32_t *p_)
111{
112 const uint8_t *p = (const uint8_t *) p_;
113 return ntohl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
114}
115
116static inline void put_unaligned_u32(uint32_t *p_, uint32_t x_)
117{
118 uint8_t *p = (uint8_t *) p_;
119 uint32_t x = ntohl(x_);
120
121 p[0] = x >> 24;
122 p[1] = x >> 16;
123 p[2] = x >> 8;
124 p[3] = x;
125}
126
320232ec 127static inline uint64_t get_unaligned_u64__(const uint64_t *p_)
afa3a931
BP
128{
129 const uint8_t *p = (const uint8_t *) p_;
130 return ntohll(((uint64_t) p[0] << 56)
131 | ((uint64_t) p[1] << 48)
132 | ((uint64_t) p[2] << 40)
133 | ((uint64_t) p[3] << 32)
134 | (p[4] << 24)
135 | (p[5] << 16)
136 | (p[6] << 8)
137 | p[7]);
138}
139
320232ec 140static inline void put_unaligned_u64__(uint64_t *p_, uint64_t x_)
afa3a931
BP
141{
142 uint8_t *p = (uint8_t *) p_;
143 uint64_t x = ntohll(x_);
144
145 p[0] = x >> 56;
146 p[1] = x >> 48;
147 p[2] = x >> 40;
148 p[3] = x >> 32;
149 p[4] = x >> 24;
150 p[5] = x >> 16;
151 p[6] = x >> 8;
152 p[7] = x;
153}
9ea5d2d5
BP
154
155/* Only sparse cares about the difference between uint<N>_t and ovs_be<N>, and
156 * that takes the GCC branch, so there's no point in working too hard on these
157 * accessors. */
158#define get_unaligned_be16 get_unaligned_u16
159#define get_unaligned_be32 get_unaligned_u32
9ea5d2d5
BP
160#define put_unaligned_be16 put_unaligned_u16
161#define put_unaligned_be32 put_unaligned_u32
162#define put_unaligned_be64 put_unaligned_u64
320232ec 163
79461d20
BP
164/* We do not #define get_unaligned_be64 as for the other be<N> functions above,
165 * because such a definition would mean that get_unaligned_be64() would have a
166 * different interface in each branch of the #if: with GCC it would take a
167 * "ovs_be64 *", with other compilers any pointer-to-64-bit-type (but not void
168 * *). The latter means code like "get_unaligned_be64(ofpbuf_data(b))" would
169 * work with GCC but not with other compilers, which is surprising and
170 * undesirable. Hence this wrapper function. */
171static inline ovs_be64
172get_unaligned_be64(const ovs_be64 *p)
173{
174 return get_unaligned_u64(p);
175}
176#endif
320232ec
BP
177
178/* Stores 'x' at possibly misaligned address 'p'.
179 *
180 * put_unaligned_u64() could be overloaded in the same way as
181 * get_unaligned_u64(), but so far it has not proven necessary.
182 */
183static inline void
184put_unaligned_u64(uint64_t *p, uint64_t x)
185{
186 put_unaligned_u64__(p, x);
187}
7bef2c91 188\f
7c457c33
BP
189/* Returns the value in 'x'. */
190static inline uint32_t
191get_16aligned_u32(const ovs_16aligned_u32 *x)
192{
193 return ((uint32_t) x->hi << 16) | x->lo;
194}
195
196/* Stores 'value' in 'x'. */
197static inline void
198put_16aligned_u32(ovs_16aligned_u32 *x, uint32_t value)
199{
200 x->hi = value >> 16;
201 x->lo = value;
202}
203
7bef2c91
BP
204/* Returns the value in 'x'. */
205static inline uint64_t
206get_32aligned_u64(const ovs_32aligned_u64 *x)
207{
05fe1764 208 return ((uint64_t) x->hi << 32) | x->lo;
7bef2c91
BP
209}
210
211/* Stores 'value' in 'x'. */
212static inline void
213put_32aligned_u64(ovs_32aligned_u64 *x, uint64_t value)
214{
05fe1764
BP
215 x->hi = value >> 32;
216 x->lo = value;
7bef2c91
BP
217}
218
62a78fe5
BP
219/* Returns the value in 'x'. */
220static inline ovs_u128
221get_32aligned_u128(const ovs_32aligned_u128 *x)
222{
c0f84d8f
SS
223 ovs_u128 u;
224 u.u32[0] = x->u32[0];
225 u.u32[1] = x->u32[1];
226 u.u32[2] = x->u32[2];
227 u.u32[3] = x->u32[3];
62a78fe5
BP
228 return u;
229}
230
231/* Stores 'value' in 'x'. */
232static inline void
233put_32aligned_u128(ovs_32aligned_u128 *x, ovs_u128 value)
234{
235 x->u32[0] = value.u32[0];
236 x->u32[1] = value.u32[1];
237 x->u32[2] = value.u32[2];
238 x->u32[3] = value.u32[3];
239}
240
6506f45c 241#ifndef __CHECKER__
7c457c33
BP
242/* Returns the value of 'x'. */
243static inline ovs_be32
244get_16aligned_be32(const ovs_16aligned_be32 *x)
245{
246#ifdef WORDS_BIGENDIAN
247 return ((ovs_be32) x->hi << 16) | x->lo;
248#else
249 return ((ovs_be32) x->lo << 16) | x->hi;
250#endif
251}
252
253/* Stores network byte order 'value' into 'x'. */
254static inline void
255put_16aligned_be32(ovs_16aligned_be32 *x, ovs_be32 value)
256{
257#if WORDS_BIGENDIAN
258 x->hi = value >> 16;
259 x->lo = value;
260#else
261 x->hi = value;
262 x->lo = value >> 16;
263#endif
264}
265
7bef2c91
BP
266/* Returns the value of 'x'. */
267static inline ovs_be64
268get_32aligned_be64(const ovs_32aligned_be64 *x)
269{
270#ifdef WORDS_BIGENDIAN
05fe1764 271 return ((ovs_be64) x->hi << 32) | x->lo;
7bef2c91 272#else
05fe1764 273 return ((ovs_be64) x->lo << 32) | x->hi;
7bef2c91
BP
274#endif
275}
276
277/* Stores network byte order 'value' into 'x'. */
278static inline void
279put_32aligned_be64(ovs_32aligned_be64 *x, ovs_be64 value)
280{
281#if WORDS_BIGENDIAN
05fe1764
BP
282 x->hi = value >> 32;
283 x->lo = value;
7bef2c91
BP
284#else
285 x->hi = value;
286 x->lo = value >> 32;
287#endif
288}
62a78fe5
BP
289
290/* Returns the value of 'x'. */
291static inline ovs_be128
292get_32aligned_be128(const ovs_32aligned_be128 *x)
293{
c0f84d8f
SS
294 ovs_be128 u;
295 u.be32[0] = x->be32[0];
296 u.be32[1] = x->be32[1];
297 u.be32[2] = x->be32[2];
298 u.be32[3] = x->be32[3];
62a78fe5
BP
299 return u;
300}
301
302/* Stores network byte order 'value' into 'x'. */
303static inline void
304put_32aligned_be128(ovs_32aligned_be128 *x, ovs_be128 value)
305{
306 x->be32[0] = value.be32[0];
307 x->be32[1] = value.be32[1];
308 x->be32[2] = value.be32[2];
309 x->be32[3] = value.be32[3];
310}
6506f45c
BP
311#else /* __CHECKER__ */
312/* Making sparse happy with these functions also makes them unreadable, so
313 * don't bother to show it their implementations. */
7c457c33
BP
314ovs_be32 get_16aligned_be32(const ovs_16aligned_be32 *);
315void put_16aligned_be32(ovs_16aligned_be32 *, ovs_be32);
6506f45c
BP
316ovs_be64 get_32aligned_be64(const ovs_32aligned_be64 *);
317void put_32aligned_be64(ovs_32aligned_be64 *, ovs_be64);
62a78fe5
BP
318ovs_be128 get_32aligned_be128(const ovs_32aligned_be128 *);
319void put_32aligned_be128(ovs_32aligned_be128 *, ovs_be128);
6506f45c 320#endif
afa3a931
BP
321
322#endif /* unaligned.h */