debian: Fix cross build.
[cascardo/ovs.git] / lib / netlink-socket.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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_SOCKET_H
18 #define NETLINK_SOCKET_H 1
19
20 /* Netlink socket definitions.
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 functions for working with Netlink sockets, which
30  * are Linux-specific.  For Netlink protocol definitions, see
31  * netlink-protocol.h.  For helper functions for working with Netlink messages,
32  * see netlink.h.
33  *
34  *
35  * Thread-safety
36  * =============
37  *
38  * Most of the netlink functions are not fully thread-safe: Only a single
39  * thread may use a given nl_sock or nl_dump at one time. The exceptions are:
40  *
41  *    - nl_sock_recv() is conditionally thread-safe: it may be called from
42  *      different threads with the same nl_sock, but each caller must provide
43  *      an independent receive buffer.
44  *
45  *    - nl_dump_next() is conditionally thread-safe: it may be called from
46  *      different threads with the same nl_dump, but each caller must provide
47  *      independent buffers.
48  */
49
50 #include <stdbool.h>
51 #include <stddef.h>
52 #include <stdint.h>
53 #include "ofpbuf.h"
54 #include "ovs-atomic.h"
55 #include "ovs-thread.h"
56
57 struct nl_sock;
58
59 #ifndef HAVE_NETLINK
60 #error "netlink-socket.h is only for hosts that support Netlink sockets"
61 #endif
62
63 /* Netlink sockets. */
64 int nl_sock_create(int protocol, struct nl_sock **);
65 int nl_sock_clone(const struct nl_sock *, struct nl_sock **);
66 void nl_sock_destroy(struct nl_sock *);
67
68 int nl_sock_join_mcgroup(struct nl_sock *, unsigned int multicast_group);
69 int nl_sock_leave_mcgroup(struct nl_sock *, unsigned int multicast_group);
70
71 int nl_sock_send(struct nl_sock *, const struct ofpbuf *, bool wait);
72 int nl_sock_send_seq(struct nl_sock *, const struct ofpbuf *,
73                      uint32_t nlmsg_seq, bool wait);
74 int nl_sock_recv(struct nl_sock *, struct ofpbuf *, bool wait);
75 int nl_sock_transact(struct nl_sock *, const struct ofpbuf *request,
76                      struct ofpbuf **replyp);
77
78 int nl_sock_drain(struct nl_sock *);
79
80 void nl_sock_wait(const struct nl_sock *, short int events);
81 int nl_sock_fd(const struct nl_sock *);
82
83 uint32_t nl_sock_pid(const struct nl_sock *);
84
85 /* Batching transactions. */
86 struct nl_transaction {
87     /* Filled in by client. */
88     struct ofpbuf *request;     /* Request to send. */
89
90     /* The client must initialize 'reply' to one of:
91      *
92      *   - NULL, if it does not care to examine the reply.
93      *
94      *   - Otherwise, to an ofpbuf with a memory allocation of at least
95      *     NLMSG_HDRLEN bytes.
96      */
97     struct ofpbuf *reply;       /* Reply (empty if reply was an error code). */
98     int error;                  /* Positive errno value, 0 if no error. */
99 };
100
101 void nl_sock_transact_multiple(struct nl_sock *,
102                                struct nl_transaction **, size_t n);
103
104 /* Transactions without an allocated socket. */
105 int nl_transact(int protocol, const struct ofpbuf *request,
106                 struct ofpbuf **replyp);
107 void nl_transact_multiple(int protocol, struct nl_transaction **, size_t n);
108
109 /* Table dumping. */
110 #define NL_DUMP_BUFSIZE         4096
111
112 struct nl_dump {
113     struct nl_sock *sock;       /* Socket being dumped. */
114     uint32_t nl_seq;            /* Expected nlmsg_seq for replies. */
115     atomic_uint status;         /* Low bit set if we read final message.
116                                  * Other bits hold an errno (0 for success). */
117     struct seq *status_seq;     /* Tracks changes to the above 'status'. */
118     struct ovs_mutex mutex;
119 };
120
121 void nl_dump_start(struct nl_dump *, int protocol,
122                    const struct ofpbuf *request);
123 bool nl_dump_next(struct nl_dump *, struct ofpbuf *reply, struct ofpbuf *buf);
124 bool nl_dump_peek(struct ofpbuf *reply, struct ofpbuf *buf);
125 int nl_dump_done(struct nl_dump *);
126
127 /* Miscellaneous */
128 int nl_lookup_genl_family(const char *name, int *number);
129 int nl_lookup_genl_mcgroup(const char *family_name, const char *group_name,
130                            unsigned int *multicast_group);
131
132 #endif /* netlink-socket.h */