Catalli's threaded switch
[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 interface.
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 #include <stdbool.h>
30 #include <sys/uio.h>
31 #include <stdint.h>
32
33 struct ofpbuf;
34 struct nl_sock;
35 struct nlattr;
36
37 /* Netlink sockets. */
38
39 int nl_sock_create(int protocol, int multicast_group,
40                    size_t so_sndbuf, size_t so_rcvbuf,
41                    struct nl_sock **);
42 void nl_sock_destroy(struct nl_sock *);
43
44 int nl_sock_send(struct nl_sock *, const struct ofpbuf *, bool wait);
45 int nl_sock_sendv(struct nl_sock *sock, const struct iovec iov[], size_t n_iov,
46                   bool wait);
47 int nl_sock_recv(struct nl_sock *, struct ofpbuf **, bool wait);
48 int nl_sock_transact(struct nl_sock *, const struct ofpbuf *request,
49                      struct ofpbuf **reply);
50
51 void nl_sock_wait(const struct nl_sock *, short int events);
52
53 /* Table dumping. */
54 struct nl_dump {
55     struct nl_sock *sock;       /* Socket being dumped. */
56     uint32_t seq;               /* Expected nlmsg_seq for replies. */
57     struct ofpbuf *buffer;      /* Receive buffer currently being iterated. */
58     int status;                 /* 0=OK, EOF=done, or positive errno value. */
59 };
60
61 void nl_dump_start(struct nl_dump *, struct nl_sock *,
62                    const struct ofpbuf *request);
63 bool nl_dump_next(struct nl_dump *, struct ofpbuf *reply);
64 int nl_dump_done(struct nl_dump *);
65 \f
66 /* Netlink messages. */
67
68 /* Accessing headers and data. */
69 struct nlmsghdr *nl_msg_nlmsghdr(const struct ofpbuf *);
70 struct genlmsghdr *nl_msg_genlmsghdr(const struct ofpbuf *);
71 bool nl_msg_nlmsgerr(const struct ofpbuf *, int *error);
72 void nl_msg_reserve(struct ofpbuf *, size_t);
73
74 /* Appending headers and raw data. */
75 void nl_msg_put_nlmsghdr(struct ofpbuf *, size_t expected_payload,
76                          uint32_t type, uint32_t flags);
77 void nl_msg_put_genlmsghdr(struct ofpbuf *, size_t expected_payload,
78                            int family, uint32_t flags,
79                            uint8_t cmd, uint8_t version);
80 void nl_msg_put(struct ofpbuf *, const void *, size_t);
81 void *nl_msg_put_uninit(struct ofpbuf *, size_t);
82
83 /* Appending attributes. */
84 void *nl_msg_put_unspec_uninit(struct ofpbuf *, uint16_t type, size_t);
85 void nl_msg_put_unspec(struct ofpbuf *, uint16_t type, const void *, size_t);
86 void nl_msg_put_flag(struct ofpbuf *, uint16_t type);
87 void nl_msg_put_u8(struct ofpbuf *, uint16_t type, uint8_t value);
88 void nl_msg_put_u16(struct ofpbuf *, uint16_t type, uint16_t value);
89 void nl_msg_put_u32(struct ofpbuf *, uint16_t type, uint32_t value);
90 void nl_msg_put_u64(struct ofpbuf *, uint16_t type, uint64_t value);
91 void nl_msg_put_string(struct ofpbuf *, uint16_t type, const char *value);
92
93 size_t nl_msg_start_nested(struct ofpbuf *, uint16_t type);
94 void nl_msg_end_nested(struct ofpbuf *, size_t offset);
95 void nl_msg_put_nested(struct ofpbuf *, uint16_t type,
96                        const void *data, size_t size);
97
98 /* Separating buffers into individual messages. */
99 struct nlmsghdr *nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg);
100 \f
101 /* Netlink attribute types. */
102 enum nl_attr_type
103 {
104     NL_A_NO_ATTR = 0,
105     NL_A_UNSPEC,
106     NL_A_U8,
107     NL_A_U16,
108     NL_A_U32,
109     NL_A_U64,
110     NL_A_STRING,
111     NL_A_FLAG,
112     NL_A_NESTED,
113     N_NL_ATTR_TYPES
114 };
115
116 /* Netlink attribute parsing. */
117 const void *nl_attr_get(const struct nlattr *);
118 size_t nl_attr_get_size(const struct nlattr *);
119 const void *nl_attr_get_unspec(const struct nlattr *, size_t size);
120 bool nl_attr_get_flag(const struct nlattr *);
121 uint8_t nl_attr_get_u8(const struct nlattr *);
122 uint16_t nl_attr_get_u16(const struct nlattr *);
123 uint32_t nl_attr_get_u32(const struct nlattr *);
124 uint64_t nl_attr_get_u64(const struct nlattr *);
125 const char *nl_attr_get_string(const struct nlattr *);
126 void nl_attr_get_nested(const struct nlattr *, struct ofpbuf *);
127
128 /* Netlink attribute policy.
129  *
130  * Specifies how to parse a single attribute from a Netlink message payload.
131  */
132 struct nl_policy
133 {
134     enum nl_attr_type type;
135     size_t min_len, max_len;
136     bool optional;
137 };
138
139 bool nl_policy_parse(const struct ofpbuf *, size_t offset,
140                      const struct nl_policy[],
141                      struct nlattr *[], size_t n_attrs);
142 bool nl_parse_nested(const struct nlattr *, const struct nl_policy[],
143                      struct nlattr *[], size_t n_attrs);
144 \f
145 /* Miscellaneous. */
146
147 int nl_lookup_genl_family(const char *name, int *number);
148
149 #endif /* netlink.h */