Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / lib / netlink.h
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 \f
53 /* Netlink messages. */
54
55 /* Accessing headers and data. */
56 struct nlmsghdr *nl_msg_nlmsghdr(const struct ofpbuf *);
57 struct genlmsghdr *nl_msg_genlmsghdr(const struct ofpbuf *);
58 bool nl_msg_nlmsgerr(const struct ofpbuf *, int *error);
59 void nl_msg_reserve(struct ofpbuf *, size_t);
60
61 /* Appending headers and raw data. */
62 void nl_msg_put_nlmsghdr(struct ofpbuf *, struct nl_sock *,
63                          size_t expected_payload,
64                          uint32_t type, uint32_t flags);
65 void nl_msg_put_genlmsghdr(struct ofpbuf *, struct nl_sock *,
66                            size_t expected_payload, int family, uint32_t flags,
67                            uint8_t cmd, uint8_t version);
68 void nl_msg_put(struct ofpbuf *, const void *, size_t);
69 void *nl_msg_put_uninit(struct ofpbuf *, size_t);
70
71 /* Appending attributes. */
72 void *nl_msg_put_unspec_uninit(struct ofpbuf *, uint16_t type, size_t);
73 void nl_msg_put_unspec(struct ofpbuf *, uint16_t type, const void *, size_t);
74 void nl_msg_put_flag(struct ofpbuf *, uint16_t type);
75 void nl_msg_put_u8(struct ofpbuf *, uint16_t type, uint8_t value);
76 void nl_msg_put_u16(struct ofpbuf *, uint16_t type, uint16_t value);
77 void nl_msg_put_u32(struct ofpbuf *, uint16_t type, uint32_t value);
78 void nl_msg_put_u64(struct ofpbuf *, uint16_t type, uint64_t value);
79 void nl_msg_put_string(struct ofpbuf *, uint16_t type, const char *value);
80 void nl_msg_put_nested(struct ofpbuf *, uint16_t type, struct ofpbuf *);
81 \f
82 /* Netlink attribute types. */
83 enum nl_attr_type
84 {
85     NL_A_NO_ATTR = 0,
86     NL_A_UNSPEC,
87     NL_A_U8,
88     NL_A_U16,
89     NL_A_U32,
90     NL_A_U64,
91     NL_A_STRING,
92     NL_A_FLAG,
93     NL_A_NESTED,
94     N_NL_ATTR_TYPES
95 };
96
97 /* Netlink attribute parsing. */
98 const void *nl_attr_get(const struct nlattr *);
99 size_t nl_attr_get_size(const struct nlattr *);
100 const void *nl_attr_get_unspec(const struct nlattr *, size_t size);
101 bool nl_attr_get_flag(const struct nlattr *);
102 uint8_t nl_attr_get_u8(const struct nlattr *);
103 uint16_t nl_attr_get_u16(const struct nlattr *);
104 uint32_t nl_attr_get_u32(const struct nlattr *);
105 uint64_t nl_attr_get_u64(const struct nlattr *);
106 const char *nl_attr_get_string(const struct nlattr *);
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 \f
123 /* Miscellaneous. */
124
125 int nl_lookup_genl_family(const char *name, int *number);
126
127 #endif /* netlink.h */