datapath: Convert odp_flow_key to use Netlink attributes instead.
[sliver-openvswitch.git] / datapath / linux-2.6 / compat-2.6 / include / net / netlink.h
1 #ifndef __NET_NETLINK_WRAPPER_H
2 #define __NET_NETLINK_WRAPPER_H 1
3
4 #include <linux/version.h>
5 #include_next <net/netlink.h>
6
7 #ifndef HAVE_NLA_NUL_STRING
8 #define NLA_NUL_STRING NLA_STRING
9
10 static inline int VERIFY_NUL_STRING(struct nlattr *attr)
11 {
12         return (!attr || (nla_len(attr)
13                           && memchr(nla_data(attr), '\0', nla_len(attr)))
14                 ? 0 : -EINVAL);
15 }
16 #else
17 static inline int VERIFY_NUL_STRING(struct nlattr *attr)
18 {
19         return 0;
20 }
21 #endif  /* !HAVE_NLA_NUL_STRING */
22
23 #ifndef NLA_PUT_BE16
24 #define NLA_PUT_BE16(skb, attrtype, value) \
25         NLA_PUT_TYPE(skb, __be16, attrtype, value)
26 #endif  /* !NLA_PUT_BE16 */
27
28 #ifndef NLA_PUT_BE32
29 #define NLA_PUT_BE32(skb, attrtype, value) \
30         NLA_PUT_TYPE(skb, __be32, attrtype, value)
31 #endif  /* !NLA_PUT_BE32 */
32
33 #ifndef NLA_PUT_BE64
34 #define NLA_PUT_BE64(skb, attrtype, value) \
35         NLA_PUT_TYPE(skb, __be64, attrtype, value)
36 #endif  /* !NLA_PUT_BE64 */
37
38 #ifndef HAVE_NLA_GET_BE16
39 /**
40  * nla_get_be16 - return payload of __be16 attribute
41  * @nla: __be16 netlink attribute
42  */
43 static inline __be16 nla_get_be16(const struct nlattr *nla)
44 {
45         return *(__be16 *) nla_data(nla);
46 }
47 #endif  /* !HAVE_NLA_GET_BE16 */
48
49 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
50 /**
51  * nla_get_be32 - return payload of __be32 attribute
52  * @nla: __be32 netlink attribute
53  */
54 static inline __be32 nla_get_be32(const struct nlattr *nla)
55 {
56         return *(__be32 *) nla_data(nla);
57 }
58 #endif
59
60 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
61 /* These functions' nlattr source arguments weren't "const" before 2.6.29, so
62  * cast their arguments to the non-"const" versions.  Using macros for this
63  * isn't exactly a brilliant idea, but it seems less error-prone than copying
64  * the definitions of all umpteen functions. */
65 #define nla_get_u64(nla)   (nla_get_u64)  ((struct nlattr *) (nla))
66 #define nla_get_u32(nla)   (nla_get_u32)  ((struct nlattr *) (nla))
67 #define nla_get_u16(nla)   (nla_get_u16)  ((struct nlattr *) (nla))
68 #define nla_get_u8(nla)    (nla_get_u8)   ((struct nlattr *) (nla))
69 /* nla_get_be64 is handled separately below. */
70 #define nla_get_be32(nla)  (nla_get_be32) ((struct nlattr *) (nla))
71 #define nla_get_be16(nla)  (nla_get_be16) ((struct nlattr *) (nla))
72 #define nla_get_be8(nla)   (nla_get_be8)  ((struct nlattr *) (nla))
73 #define nla_get_flag(nla)  (nla_get_flag) ((struct nlattr *) (nla))
74 #define nla_get_msecs(nla) (nla_get_msecs)((struct nlattr *) (nla))
75 #define nla_memcpy(dst, src, count) \
76         (nla_memcpy)(dst, (struct nlattr *)(src), count)
77 #endif
78
79 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,34)
80 /* This function was introduced in 2.6.31, but initially it performed an
81  * unaligned access, so we replace it up to 2.6.34 where it was fixed.  */
82 #define nla_get_be64 rpl_nla_get_be64
83 static inline __be64 nla_get_be64(const struct nlattr *nla)
84 {
85         __be64 tmp;
86
87         /* The additional cast is necessary because  */
88         nla_memcpy(&tmp, (struct nlattr *) nla, sizeof(tmp));
89
90         return tmp;
91 }
92 #endif
93
94 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
95 /**
96  * nla_type - attribute type
97  * @nla: netlink attribute
98  */
99 static inline int nla_type(const struct nlattr *nla)
100 {
101         return nla->nla_type & NLA_TYPE_MASK;
102 }
103 #endif
104
105 /* The following nla_put_be{16,32,64} functions are not in any version of Linux
106  * (although NLA_PUT_BE{16,32,64} are), so we will probably want to add them
107  * as part of the patch series when we submit Open vSwitch upstream. */
108
109 /**
110  * nla_put_be16 - Add a be16 netlink attribute to a socket buffer
111  * @skb: socket buffer to add attribute to
112  * @attrtype: attribute type
113  * @value: numeric value
114  */
115 static inline int nla_put_be16(struct sk_buff *skb, int attrtype, __be16 value)
116 {
117         return nla_put(skb, attrtype, sizeof(__be16), &value);
118 }
119
120 /**
121  * nla_put_be32 - Add a be32 netlink attribute to a socket buffer
122  * @skb: socket buffer to add attribute to
123  * @attrtype: attribute type
124  * @value: numeric value
125  */
126 static inline int nla_put_be32(struct sk_buff *skb, int attrtype, __be32 value)
127 {
128         return nla_put(skb, attrtype, sizeof(__be32), &value);
129 }
130
131 /**
132  * nla_put_64 - Add a be64 netlink attribute to a socket buffer
133  * @skb: socket buffer to add attribute to
134  * @attrtype: attribute type
135  * @value: numeric value
136  */
137 static inline int nla_put_be64(struct sk_buff *skb, int attrtype, __be64 value)
138 {
139         return nla_put(skb, attrtype, sizeof(__be64), &value);
140 }
141
142 #endif /* net/netlink.h */