netlink: Split into generic and Linux-specific parts.
[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_string(struct ofpbuf *, uint16_t type, const char *value);
65
66 size_t nl_msg_start_nested(struct ofpbuf *, uint16_t type);
67 void nl_msg_end_nested(struct ofpbuf *, size_t offset);
68 void nl_msg_put_nested(struct ofpbuf *, uint16_t type,
69                        const void *data, size_t size);
70
71 /* Separating buffers into individual messages. */
72 struct nlmsghdr *nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg);
73 \f
74 /* Netlink attribute types. */
75 enum nl_attr_type
76 {
77     NL_A_NO_ATTR = 0,
78     NL_A_UNSPEC,
79     NL_A_U8,
80     NL_A_U16,
81     NL_A_U32,
82     NL_A_U64,
83     NL_A_STRING,
84     NL_A_FLAG,
85     NL_A_NESTED,
86     N_NL_ATTR_TYPES
87 };
88
89 /* Netlink attribute parsing. */
90 const void *nl_attr_get(const struct nlattr *);
91 size_t nl_attr_get_size(const struct nlattr *);
92 const void *nl_attr_get_unspec(const struct nlattr *, size_t size);
93 bool nl_attr_get_flag(const struct nlattr *);
94 uint8_t nl_attr_get_u8(const struct nlattr *);
95 uint16_t nl_attr_get_u16(const struct nlattr *);
96 uint32_t nl_attr_get_u32(const struct nlattr *);
97 uint64_t nl_attr_get_u64(const struct nlattr *);
98 const char *nl_attr_get_string(const struct nlattr *);
99 void nl_attr_get_nested(const struct nlattr *, struct ofpbuf *);
100
101 /* Netlink attribute policy.
102  *
103  * Specifies how to parse a single attribute from a Netlink message payload.
104  */
105 struct nl_policy
106 {
107     enum nl_attr_type type;
108     size_t min_len, max_len;
109     bool optional;
110 };
111
112 bool nl_policy_parse(const struct ofpbuf *, size_t offset,
113                      const struct nl_policy[],
114                      struct nlattr *[], size_t n_attrs);
115 bool nl_parse_nested(const struct nlattr *, const struct nl_policy[],
116                      struct nlattr *[], size_t n_attrs);
117
118 #endif /* netlink.h */