]> git.proxmox.com Git - ovs.git/blame - lib/unaligned.h
debian: Don't require ipsec_local_ip to configure IPsec
[ovs.git] / lib / unaligned.h
CommitLineData
afa3a931
BP
1/*
2 * Copyright (c) 2010 Nicira Networks.
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"
afa3a931
BP
23
24/* Public API. */
25static inline uint16_t get_unaligned_u16(const uint16_t *);
26static inline uint32_t get_unaligned_u32(const uint32_t *);
27static inline uint64_t get_unaligned_u64(const uint64_t *);
28static inline void put_unaligned_u16(uint16_t *, uint16_t);
29static inline void put_unaligned_u32(uint32_t *, uint32_t);
30static inline void put_unaligned_u64(uint64_t *, uint64_t);
31
9ea5d2d5
BP
32static inline ovs_be16 get_unaligned_be16(const ovs_be16 *);
33static inline ovs_be32 get_unaligned_be32(const ovs_be32 *);
34static inline ovs_be64 get_unaligned_be64(const ovs_be64 *);
35static inline void put_unaligned_be16(ovs_be16 *, ovs_be16);
36static inline void put_unaligned_be32(ovs_be32 *, ovs_be32);
37static inline void put_unaligned_be64(ovs_be64 *, ovs_be64);
38
afa3a931
BP
39#ifdef __GNUC__
40/* GCC implementations. */
9ea5d2d5
BP
41#define GCC_UNALIGNED_ACCESSORS(TYPE, ABBREV) \
42struct unaligned_##ABBREV { \
43 TYPE x __attribute__((__packed__)); \
44}; \
45static inline struct unaligned_##ABBREV * \
46unaligned_##ABBREV(const TYPE *p) \
47{ \
48 return (struct unaligned_##ABBREV *) p; \
49} \
50 \
51static inline TYPE \
52get_unaligned_##ABBREV(const TYPE *p) \
53{ \
54 return unaligned_##ABBREV(p)->x; \
55} \
56 \
57static inline void \
58put_unaligned_##ABBREV(TYPE *p, TYPE x) \
59{ \
60 unaligned_##ABBREV(p)->x = x; \
afa3a931
BP
61}
62
9ea5d2d5
BP
63GCC_UNALIGNED_ACCESSORS(uint16_t, u16);
64GCC_UNALIGNED_ACCESSORS(uint32_t, u32);
65GCC_UNALIGNED_ACCESSORS(uint64_t, u64);
66
67GCC_UNALIGNED_ACCESSORS(ovs_be16, be16);
68GCC_UNALIGNED_ACCESSORS(ovs_be32, be32);
69GCC_UNALIGNED_ACCESSORS(ovs_be64, be64);
afa3a931
BP
70#else
71/* Generic implementations. */
72
73static inline uint16_t get_unaligned_u16(const uint16_t *p_)
74{
75 const uint8_t *p = (const uint8_t *) p_;
76 return ntohs((p[0] << 8) | p[1]);
77}
78
79static inline void put_unaligned_u16(uint16_t *p_, uint16_t x_)
80{
81 uint8_t *p = (uint8_t *) p_;
82 uint16_t x = ntohs(x_);
83
84 p[0] = x >> 8;
85 p[1] = x;
86}
87
88static inline uint32_t get_unaligned_u32(const uint32_t *p_)
89{
90 const uint8_t *p = (const uint8_t *) p_;
91 return ntohl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
92}
93
94static inline void put_unaligned_u32(uint32_t *p_, uint32_t x_)
95{
96 uint8_t *p = (uint8_t *) p_;
97 uint32_t x = ntohl(x_);
98
99 p[0] = x >> 24;
100 p[1] = x >> 16;
101 p[2] = x >> 8;
102 p[3] = x;
103}
104
105static inline uint64_t get_unaligned_u64(const uint64_t *p_)
106{
107 const uint8_t *p = (const uint8_t *) p_;
108 return ntohll(((uint64_t) p[0] << 56)
109 | ((uint64_t) p[1] << 48)
110 | ((uint64_t) p[2] << 40)
111 | ((uint64_t) p[3] << 32)
112 | (p[4] << 24)
113 | (p[5] << 16)
114 | (p[6] << 8)
115 | p[7]);
116}
117
118static inline void put_unaligned_u64(uint64_t *p_, uint64_t x_)
119{
120 uint8_t *p = (uint8_t *) p_;
121 uint64_t x = ntohll(x_);
122
123 p[0] = x >> 56;
124 p[1] = x >> 48;
125 p[2] = x >> 40;
126 p[3] = x >> 32;
127 p[4] = x >> 24;
128 p[5] = x >> 16;
129 p[6] = x >> 8;
130 p[7] = x;
131}
9ea5d2d5
BP
132
133/* Only sparse cares about the difference between uint<N>_t and ovs_be<N>, and
134 * that takes the GCC branch, so there's no point in working too hard on these
135 * accessors. */
136#define get_unaligned_be16 get_unaligned_u16
137#define get_unaligned_be32 get_unaligned_u32
138#define get_unaligned_be64 get_unaligned_u64
139#define put_unaligned_be16 put_unaligned_u16
140#define put_unaligned_be32 put_unaligned_u32
141#define put_unaligned_be64 put_unaligned_u64
afa3a931
BP
142#endif
143
144#endif /* unaligned.h */