2 * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __OVS_NETLINK_H_
18 #define __OVS_NETLINK_H_ 1
20 #include "lib/netlink-protocol.h"
22 /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */
23 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
25 /* Returns X rounded up to the nearest multiple of Y. */
26 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
29 nl_attr_is_valid(const struct nlattr *nla, size_t maxlen)
31 return (maxlen >= sizeof *nla
32 && nla->nla_len >= sizeof *nla
33 && nla->nla_len <= maxlen);
36 static __inline size_t
37 nl_attr_len_pad(const struct nlattr *nla, size_t maxlen)
39 size_t len = NLA_ALIGN(nla->nla_len);
41 return len <= maxlen ? len : nla->nla_len;
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))
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); \
56 (LEFT) -= NLA_ALIGN((ITER)->nla_len), (ITER) = nl_attr_next(ITER))
58 /* These were introduced all together in 2.6.24. */
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)
65 /* Netlink attribute iteration. */
66 static __inline struct nlattr *
67 nl_attr_next(const struct nlattr *nla)
69 return (struct nlattr *) ((uint8_t *) nla + NLA_ALIGN(nla->nla_len));
72 /* Returns the bits of 'nla->nla_type' that are significant for determining
75 nl_attr_type(const struct nlattr *nla)
77 return nla->nla_type & NLA_TYPE_MASK;
80 static __inline void *
81 nl_attr_data(const struct nlattr *nla)
83 return ((char *)nla + NLA_HDRLEN);
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)
90 return nla->nla_len - NLA_HDRLEN;
93 /* Returns the first byte in the payload of attribute 'nla'. */
94 static __inline const void *
95 nl_attr_get(const struct nlattr *nla)
97 ASSERT(nla->nla_len >= NLA_HDRLEN);
101 #define NL_ATTR_GET_AS(NLA, TYPE) \
102 (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
104 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
105 * first byte of the payload. */
107 nl_attr_get_unspec(const struct nlattr *nla, size_t size)
109 DBG_UNREFERENCED_PARAMETER(size);
110 ASSERT(nla->nla_len >= NLA_HDRLEN + size);
114 /* Returns the 64-bit network byte order value in 'nla''s payload.
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)
120 return NL_ATTR_GET_AS(nla, __be64);
123 /* Returns the 32-bit network byte order value in 'nla''s payload.
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)
129 return NL_ATTR_GET_AS(nla, __be32);
132 /* Returns the 8-bit value in 'nla''s payload. */
133 static __inline uint8_t
134 nl_attr_get_u8(const struct nlattr *nla)
136 return NL_ATTR_GET_AS(nla, uint8_t);
140 /* Returns the 32-bit host byte order value in 'nla''s payload.
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)
146 return NL_ATTR_GET_AS(nla, uint32_t);
150 static __inline const struct nlattr *
151 nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type)
153 const struct nlattr *nla;
156 NL_ATTR_FOR_EACH (nla, left, attrs, size) {
157 if (nl_attr_type(nla) == type) {
164 /* Returns the first Netlink attribute within 'nla' with the specified
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)
171 return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type);
174 #endif /* __OVS_NETLINK_H_ */