netlink: New function nl_attr_type().
[sliver-openvswitch.git] / lib / netlink.h
1 /*
2  * Copyright (c) 2008, 2009, 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 NETLINK_H
18 #define NETLINK_H 1
19
20 /* Netlink message helpers.
21  *
22  * Netlink is a datagram-based network protocol primarily for communication
23  * between user processes and the kernel, and mainly on Linux.  Netlink is
24  * specified in RFC 3549, "Linux Netlink as an IP Services Protocol".
25  *
26  * Netlink is not suitable for use in physical networks of heterogeneous
27  * machines because host byte order is used throughout.
28  *
29  * This header file defines helper functions for working with Netlink messages.
30  * For Netlink protocol definitions, see netlink-protocol.h.  For
31  * Linux-specific definitions for Netlink sockets, see netlink-socket.h.
32  */
33
34 #include <stdbool.h>
35 #include <stddef.h>
36 #include <stdint.h>
37
38 struct ofpbuf;
39 struct nlattr;
40
41 /* Accessing headers and data. */
42 struct nlmsghdr *nl_msg_nlmsghdr(const struct ofpbuf *);
43 struct genlmsghdr *nl_msg_genlmsghdr(const struct ofpbuf *);
44 bool nl_msg_nlmsgerr(const struct ofpbuf *, int *error);
45 void nl_msg_reserve(struct ofpbuf *, size_t);
46
47 /* Appending headers and raw data. */
48 void nl_msg_put_nlmsghdr(struct ofpbuf *, size_t expected_payload,
49                          uint32_t type, uint32_t flags);
50 void nl_msg_put_genlmsghdr(struct ofpbuf *, size_t expected_payload,
51                            int family, uint32_t flags,
52                            uint8_t cmd, uint8_t version);
53 void nl_msg_put(struct ofpbuf *, const void *, size_t);
54 void *nl_msg_put_uninit(struct ofpbuf *, size_t);
55
56 /* Appending attributes. */
57 void *nl_msg_put_unspec_uninit(struct ofpbuf *, uint16_t type, size_t);
58 void nl_msg_put_unspec(struct ofpbuf *, uint16_t type, const void *, size_t);
59 void nl_msg_put_flag(struct ofpbuf *, uint16_t type);
60 void nl_msg_put_u8(struct ofpbuf *, uint16_t type, uint8_t value);
61 void nl_msg_put_u16(struct ofpbuf *, uint16_t type, uint16_t value);
62 void nl_msg_put_u32(struct ofpbuf *, uint16_t type, uint32_t value);
63 void nl_msg_put_u64(struct ofpbuf *, uint16_t type, uint64_t value);
64 void nl_msg_put_be16(struct ofpbuf *, uint16_t type, ovs_be16 value);
65 void nl_msg_put_be32(struct ofpbuf *, uint16_t type, ovs_be32 value);
66 void nl_msg_put_be64(struct ofpbuf *, uint16_t type, ovs_be64 value);
67 void nl_msg_put_string(struct ofpbuf *, uint16_t type, const char *value);
68
69 size_t nl_msg_start_nested(struct ofpbuf *, uint16_t type);
70 void nl_msg_end_nested(struct ofpbuf *, size_t offset);
71 void nl_msg_put_nested(struct ofpbuf *, uint16_t type,
72                        const void *data, size_t size);
73
74 /* Separating buffers into individual messages. */
75 struct nlmsghdr *nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg);
76 \f
77 /* Netlink attribute types. */
78 enum nl_attr_type
79 {
80     NL_A_NO_ATTR = 0,
81     NL_A_UNSPEC,
82     NL_A_U8,
83     NL_A_U16,
84     NL_A_U32,
85     NL_A_U64,
86     NL_A_STRING,
87     NL_A_FLAG,
88     NL_A_NESTED,
89     N_NL_ATTR_TYPES
90 };
91
92 /* Netlink attribute parsing. */
93 int nl_attr_type(const struct nlattr *);
94 const void *nl_attr_get(const struct nlattr *);
95 size_t nl_attr_get_size(const struct nlattr *);
96 const void *nl_attr_get_unspec(const struct nlattr *, size_t size);
97 bool nl_attr_get_flag(const struct nlattr *);
98 uint8_t nl_attr_get_u8(const struct nlattr *);
99 uint16_t nl_attr_get_u16(const struct nlattr *);
100 uint32_t nl_attr_get_u32(const struct nlattr *);
101 uint64_t nl_attr_get_u64(const struct nlattr *);
102 ovs_be16 nl_attr_get_be16(const struct nlattr *);
103 ovs_be32 nl_attr_get_be32(const struct nlattr *);
104 ovs_be64 nl_attr_get_be64(const struct nlattr *);
105 const char *nl_attr_get_string(const struct nlattr *);
106 void nl_attr_get_nested(const struct nlattr *, struct ofpbuf *);
107
108 /* Netlink attribute policy.
109  *
110  * Specifies how to parse a single attribute from a Netlink message payload.
111  */
112 struct nl_policy
113 {
114     enum nl_attr_type type;
115     size_t min_len, max_len;
116     bool optional;
117 };
118
119 bool nl_policy_parse(const struct ofpbuf *, size_t offset,
120                      const struct nl_policy[],
121                      struct nlattr *[], size_t n_attrs);
122 bool nl_parse_nested(const struct nlattr *, const struct nl_policy[],
123                      struct nlattr *[], size_t n_attrs);
124
125 #endif /* netlink.h */