odp-netlink.h: Autogenerate a version of odp-netlink for windows kernel.
[cascardo/ovs.git] / datapath-windows / include / OvsNetlink.h
1 /*
2  * Copyright (c) 2008, 2009, 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 __OVS_NETLINK_H_
18 #define __OVS_NETLINK_H_ 1
19
20 #include "lib/netlink-protocol.h"
21
22 /* Returns X / Y, rounding up.  X must be nonnegative to round correctly. */
23 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
24
25 /* Returns X rounded up to the nearest multiple of Y. */
26 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
27
28 static __inline int
29 nl_attr_is_valid(const struct nlattr *nla, size_t maxlen)
30 {
31     return (maxlen >= sizeof *nla
32             && nla->nla_len >= sizeof *nla
33             && nla->nla_len <= maxlen);
34 }
35
36 static __inline size_t
37 nl_attr_len_pad(const struct nlattr *nla, size_t maxlen)
38 {
39     size_t len = NLA_ALIGN(nla->nla_len);
40
41     return len <= maxlen ? len : nla->nla_len;
42 }
43
44 /* This macro is careful to check for attributes with bad lengths. */
45 #define NL_ATTR_FOR_EACH(ITER, LEFT, ATTRS, ATTRS_LEN)                  \
46     for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN);                        \
47          nl_attr_is_valid(ITER, LEFT);                                  \
48          (LEFT) -= nl_attr_len_pad(ITER, LEFT), (ITER) = nl_attr_next(ITER))
49
50 /* This macro does not check for attributes with bad lengths.  It should only
51  * be used with messages from trusted sources or with messages that have
52  * already been validated (e.g. with NL_ATTR_FOR_EACH).  */
53 #define NL_ATTR_FOR_EACH_UNSAFE(ITER, LEFT, ATTRS, ATTRS_LEN)           \
54     for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN);                        \
55          (LEFT) > 0;                                                    \
56          (LEFT) -= NLA_ALIGN((ITER)->nla_len), (ITER) = nl_attr_next(ITER))
57
58 /* These were introduced all together in 2.6.24. */
59 #ifndef NLA_TYPE_MASK
60 #define NLA_F_NESTED        (1 << 15)
61 #define NLA_F_NET_BYTEORDER (1 << 14)
62 #define NLA_TYPE_MASK       ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
63 #endif
64
65 /* Netlink attribute iteration. */
66 static __inline struct nlattr *
67 nl_attr_next(const struct nlattr *nla)
68 {
69     return (struct nlattr *) ((uint8_t *) nla + NLA_ALIGN(nla->nla_len));
70 }
71
72  /* Returns the bits of 'nla->nla_type' that are significant for determining
73   * its type. */
74 static __inline int
75 nl_attr_type(const struct nlattr *nla)
76 {
77    return nla->nla_type & NLA_TYPE_MASK;
78 }
79
80 static __inline void *
81 nl_attr_data(const struct nlattr *nla)
82 {
83     return ((char *)nla + NLA_HDRLEN);
84 }
85
86 /* Returns the number of bytes in the payload of attribute 'nla'. */
87 static __inline uint32_t
88 nl_attr_get_size(const struct nlattr *nla)
89 {
90     return nla->nla_len - NLA_HDRLEN;
91 }
92
93 /* Returns the first byte in the payload of attribute 'nla'. */
94 static __inline const void *
95 nl_attr_get(const struct nlattr *nla)
96 {
97     ASSERT(nla->nla_len >= NLA_HDRLEN);
98     return nla + 1;
99 }
100
101 #define NL_ATTR_GET_AS(NLA, TYPE) \
102         (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
103
104 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
105  * first byte of the payload. */
106 static const void *
107 nl_attr_get_unspec(const struct nlattr *nla, size_t size)
108 {
109     DBG_UNREFERENCED_PARAMETER(size);
110     ASSERT(nla->nla_len >= NLA_HDRLEN + size);
111     return nla + 1;
112 }
113
114 /* Returns the 64-bit network byte order value in 'nla''s payload.
115  *
116  * Asserts that 'nla''s payload is at least 8 bytes long. */
117 static __inline __be64
118 nl_attr_get_be64(const struct nlattr *nla)
119 {
120     return NL_ATTR_GET_AS(nla, __be64);
121 }
122
123 /* Returns the 32-bit network byte order value in 'nla''s payload.
124  *
125  * Asserts that 'nla''s payload is at least 4 bytes long. */
126 static __inline __be32
127 nl_attr_get_be32(const struct nlattr *nla)
128 {
129     return NL_ATTR_GET_AS(nla, __be32);
130 }
131
132 /* Returns the 8-bit value in 'nla''s payload. */
133 static __inline uint8_t
134 nl_attr_get_u8(const struct nlattr *nla)
135 {
136     return NL_ATTR_GET_AS(nla, uint8_t);
137 }
138
139
140 /* Returns the 32-bit host byte order value in 'nla''s payload.
141  *
142  * Asserts that 'nla''s payload is at least 4 bytes long. */
143 static __inline uint32_t
144 nl_attr_get_u32(const struct nlattr *nla)
145 {
146     return NL_ATTR_GET_AS(nla, uint32_t);
147 }
148
149
150 static __inline const struct nlattr *
151 nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type)
152 {
153     const struct nlattr *nla;
154     size_t left;
155
156     NL_ATTR_FOR_EACH (nla, left, attrs, size) {
157         if (nl_attr_type(nla) == type) {
158             return nla;
159         }
160     }
161     return NULL;
162 }
163
164 /* Returns the first Netlink attribute within 'nla' with the specified
165  * 'type'.
166  *
167  * This function does not validate the attribute's length. */
168 static __inline const struct nlattr *
169 nl_attr_find_nested(const struct nlattr *nla, uint16_t type)
170 {
171     return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type);
172 }
173
174 #endif /* __OVS_NETLINK_H_ */