54541a42b2b9b3d981c01ea476f96b3e33586988
[cascardo/ovs.git] / include / openvswitch / types.h
1 /*
2  * Copyright (c) 2010, 2011, 2013, 2014 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 OPENVSWITCH_TYPES_H
18 #define OPENVSWITCH_TYPES_H 1
19
20 #include <sys/types.h>
21 #include <stdint.h>
22
23 #ifdef __CHECKER__
24 #define OVS_BITWISE __attribute__((bitwise))
25 #define OVS_FORCE __attribute__((force))
26 #else
27 #define OVS_BITWISE
28 #define OVS_FORCE
29 #endif
30
31 /* The ovs_be<N> types indicate that an object is in big-endian, not
32  * native-endian, byte order.  They are otherwise equivalent to uint<N>_t. */
33 typedef uint16_t OVS_BITWISE ovs_be16;
34 typedef uint32_t OVS_BITWISE ovs_be32;
35 typedef uint64_t OVS_BITWISE ovs_be64;
36
37 #define OVS_BE16_MAX ((OVS_FORCE ovs_be16) 0xffff)
38 #define OVS_BE32_MAX ((OVS_FORCE ovs_be32) 0xffffffff)
39 #define OVS_BE64_MAX ((OVS_FORCE ovs_be64) 0xffffffffffffffffULL)
40 \f
41 /* These types help with a few funny situations:
42  *
43  *   - The Ethernet header is 14 bytes long, which misaligns everything after
44  *     that.  One can put 2 "shim" bytes before the Ethernet header, but this
45  *     helps only if there is exactly one Ethernet header.  If there are two,
46  *     as with GRE and VXLAN (and if the inner header doesn't use this
47  *     trick--GRE and VXLAN don't) then you have the choice of aligning the
48  *     inner data or the outer data.  So it seems better to treat 32-bit fields
49  *     in protocol headers as aligned only on 16-bit boundaries.
50  *
51  *   - ARP headers contain misaligned 32-bit fields.
52  *
53  *   - Netlink and OpenFlow contain 64-bit values that are only guaranteed to
54  *     be aligned on 32-bit boundaries.
55  *
56  * lib/unaligned.h has helper functions for accessing these. */
57
58 /* A 32-bit value, in host byte order, that is only aligned on a 16-bit
59  * boundary.  */
60 typedef struct {
61 #ifdef WORDS_BIGENDIAN
62         uint16_t hi, lo;
63 #else
64         uint16_t lo, hi;
65 #endif
66 } ovs_16aligned_u32;
67
68 /* A 32-bit value, in network byte order, that is only aligned on a 16-bit
69  * boundary. */
70 typedef struct {
71         ovs_be16 hi, lo;
72 } ovs_16aligned_be32;
73
74 /* A 64-bit value, in host byte order, that is only aligned on a 32-bit
75  * boundary.  */
76 typedef struct {
77 #ifdef WORDS_BIGENDIAN
78         uint32_t hi, lo;
79 #else
80         uint32_t lo, hi;
81 #endif
82 } ovs_32aligned_u64;
83
84 /* A 64-bit value, in network byte order, that is only aligned on a 32-bit
85  * boundary. */
86 typedef struct {
87         ovs_be32 hi, lo;
88 } ovs_32aligned_be64;
89
90 /* ofp_port_t represents the port number of a OpenFlow switch.
91  * odp_port_t represents the port number on the datapath.
92  * ofp11_port_t represents the OpenFlow-1.1 port number. */
93 typedef uint16_t OVS_BITWISE ofp_port_t;
94 typedef uint32_t OVS_BITWISE odp_port_t;
95 typedef uint32_t OVS_BITWISE ofp11_port_t;
96
97 /* Macro functions that cast int types to ofp/odp/ofp11 types. */
98 #define OFP_PORT_C(X) ((OVS_FORCE ofp_port_t) (X))
99 #define ODP_PORT_C(X) ((OVS_FORCE odp_port_t) (X))
100 #define OFP11_PORT_C(X) ((OVS_FORCE ofp11_port_t) (X))
101
102 #endif /* openvswitch/types.h */