/src/openvswitch/lib/netlink-socket.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 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 | | #include <config.h> |
18 | | #include "netlink-socket.h" |
19 | | #include <errno.h> |
20 | | #include <inttypes.h> |
21 | | #include <stdlib.h> |
22 | | #include <sys/socket.h> |
23 | | #include <sys/types.h> |
24 | | #include <sys/uio.h> |
25 | | #include <unistd.h> |
26 | | #include "coverage.h" |
27 | | #include "openvswitch/dynamic-string.h" |
28 | | #include "hash.h" |
29 | | #include "openvswitch/hmap.h" |
30 | | #include "netlink.h" |
31 | | #include "netlink-protocol.h" |
32 | | #include "netnsid.h" |
33 | | #include "odp-netlink.h" |
34 | | #include "openvswitch/ofpbuf.h" |
35 | | #include "ovs-thread.h" |
36 | | #include "openvswitch/poll-loop.h" |
37 | | #include "seq.h" |
38 | | #include "socket-util.h" |
39 | | #include "util.h" |
40 | | #include "openvswitch/vlog.h" |
41 | | |
42 | | VLOG_DEFINE_THIS_MODULE(netlink_socket); |
43 | | |
44 | | COVERAGE_DEFINE(netlink_overflow); |
45 | | COVERAGE_DEFINE(netlink_received); |
46 | | COVERAGE_DEFINE(netlink_recv_jumbo); |
47 | | COVERAGE_DEFINE(netlink_sent); |
48 | | |
49 | | /* Linux header file confusion causes this to be undefined. */ |
50 | | #ifndef SOL_NETLINK |
51 | | #define SOL_NETLINK 270 |
52 | | #endif |
53 | | |
54 | | /* A single (bad) Netlink message can in theory dump out many, many log |
55 | | * messages, so the burst size is set quite high here to avoid missing useful |
56 | | * information. Also, at high logging levels we log *all* Netlink messages. */ |
57 | | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600); |
58 | | |
59 | | static uint32_t nl_sock_allocate_seq(struct nl_sock *, unsigned int n); |
60 | | static void log_nlmsg(const char *function, int error, |
61 | | const void *message, size_t size, int protocol); |
62 | | |
63 | | /* Netlink sockets. */ |
64 | | |
65 | | struct nl_sock { |
66 | | int fd; |
67 | | uint32_t next_seq; |
68 | | uint32_t pid; |
69 | | int protocol; |
70 | | unsigned int rcvbuf; /* Receive buffer size (SO_RCVBUF). */ |
71 | | }; |
72 | | |
73 | | /* Compile-time limit on iovecs, so that we can allocate a maximum-size array |
74 | | * of iovecs on the stack. */ |
75 | 0 | #define MAX_IOVS 128 |
76 | | |
77 | | /* Maximum number of iovecs that may be passed to sendmsg, capped at a |
78 | | * minimum of _XOPEN_IOV_MAX (16) and a maximum of MAX_IOVS. |
79 | | * |
80 | | * Initialized by nl_sock_create(). */ |
81 | | static int max_iovs; |
82 | | |
83 | | static int nl_pool_alloc(int protocol, struct nl_sock **sockp); |
84 | | static void nl_pool_release(struct nl_sock *); |
85 | | |
86 | | /* Creates a new netlink socket for the given netlink 'protocol' |
87 | | * (NETLINK_ROUTE, NETLINK_GENERIC, ...). Returns 0 and sets '*sockp' to the |
88 | | * new socket if successful, otherwise returns a positive errno value. */ |
89 | | int |
90 | | nl_sock_create(int protocol, struct nl_sock **sockp) |
91 | 0 | { |
92 | 0 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
93 | 0 | struct nl_sock *sock; |
94 | 0 | struct sockaddr_nl local, remote; |
95 | 0 | int one = 1; |
96 | 0 | socklen_t local_size; |
97 | 0 | int rcvbuf; |
98 | 0 | int retval = 0; |
99 | |
|
100 | 0 | if (ovsthread_once_start(&once)) { |
101 | 0 | int save_errno = errno; |
102 | 0 | errno = 0; |
103 | |
|
104 | 0 | max_iovs = sysconf(_SC_UIO_MAXIOV); |
105 | 0 | if (max_iovs < _XOPEN_IOV_MAX) { |
106 | 0 | if (max_iovs == -1 && errno) { |
107 | 0 | VLOG_WARN("sysconf(_SC_UIO_MAXIOV): %s", ovs_strerror(errno)); |
108 | 0 | } |
109 | 0 | max_iovs = _XOPEN_IOV_MAX; |
110 | 0 | } else if (max_iovs > MAX_IOVS) { |
111 | 0 | max_iovs = MAX_IOVS; |
112 | 0 | } |
113 | |
|
114 | 0 | errno = save_errno; |
115 | 0 | ovsthread_once_done(&once); |
116 | 0 | } |
117 | |
|
118 | 0 | *sockp = NULL; |
119 | 0 | sock = xmalloc(sizeof *sock); |
120 | |
|
121 | 0 | sock->fd = socket(AF_NETLINK, SOCK_RAW, protocol); |
122 | 0 | if (sock->fd < 0) { |
123 | 0 | VLOG_ERR("fcntl: %s", ovs_strerror(errno)); |
124 | 0 | goto error; |
125 | 0 | } |
126 | | |
127 | 0 | sock->protocol = protocol; |
128 | 0 | sock->next_seq = 1; |
129 | |
|
130 | 0 | rcvbuf = 1024 * 1024 * 4; |
131 | 0 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_EXT_ACK, &one, sizeof one)) { |
132 | 0 | VLOG_WARN_RL(&rl, "setting extended ack support failed (%s)", |
133 | 0 | ovs_strerror(errno)); |
134 | 0 | } |
135 | |
|
136 | 0 | if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUFFORCE, |
137 | 0 | &rcvbuf, sizeof rcvbuf)) { |
138 | | /* Only root can use SO_RCVBUFFORCE. Everyone else gets EPERM. |
139 | | * Warn only if the failure is therefore unexpected. */ |
140 | 0 | if (errno != EPERM) { |
141 | 0 | VLOG_WARN_RL(&rl, "setting %d-byte socket receive buffer failed " |
142 | 0 | "(%s)", rcvbuf, ovs_strerror(errno)); |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | /* Strict checking only supported for NETLINK_ROUTE. */ |
147 | 0 | if (protocol == NETLINK_ROUTE |
148 | 0 | && setsockopt(sock->fd, SOL_NETLINK, NETLINK_GET_STRICT_CHK, |
149 | 0 | &one, sizeof one) < 0) { |
150 | 0 | VLOG_RL(&rl, errno == ENOPROTOOPT ? VLL_DBG : VLL_WARN, |
151 | 0 | "netlink: could not enable strict checking (%s)", |
152 | 0 | ovs_strerror(errno)); |
153 | 0 | } |
154 | |
|
155 | 0 | retval = get_socket_rcvbuf(sock->fd); |
156 | 0 | if (retval < 0) { |
157 | 0 | retval = -retval; |
158 | 0 | goto error; |
159 | 0 | } |
160 | 0 | sock->rcvbuf = retval; |
161 | 0 | retval = 0; |
162 | | |
163 | | /* Connect to kernel (pid 0) as remote address. */ |
164 | 0 | memset(&remote, 0, sizeof remote); |
165 | 0 | remote.nl_family = AF_NETLINK; |
166 | 0 | remote.nl_pid = 0; |
167 | 0 | if (connect(sock->fd, (struct sockaddr *) &remote, sizeof remote) < 0) { |
168 | 0 | VLOG_ERR("connect(0): %s", ovs_strerror(errno)); |
169 | 0 | goto error; |
170 | 0 | } |
171 | | |
172 | | /* Obtain pid assigned by kernel. */ |
173 | 0 | memset(&local, 0, sizeof local); |
174 | 0 | local_size = sizeof local; |
175 | 0 | if (getsockname(sock->fd, (struct sockaddr *) &local, &local_size) < 0) { |
176 | 0 | VLOG_ERR("getsockname: %s", ovs_strerror(errno)); |
177 | 0 | goto error; |
178 | 0 | } |
179 | 0 | if (local_size < sizeof local || local.nl_family != AF_NETLINK) { |
180 | 0 | VLOG_ERR("getsockname returned bad Netlink name"); |
181 | 0 | retval = EINVAL; |
182 | 0 | goto error; |
183 | 0 | } |
184 | 0 | sock->pid = local.nl_pid; |
185 | |
|
186 | 0 | *sockp = sock; |
187 | 0 | return 0; |
188 | | |
189 | 0 | error: |
190 | 0 | if (retval == 0) { |
191 | 0 | retval = errno; |
192 | 0 | if (retval == 0) { |
193 | 0 | retval = EINVAL; |
194 | 0 | } |
195 | 0 | } |
196 | 0 | if (sock->fd >= 0) { |
197 | 0 | close(sock->fd); |
198 | 0 | } |
199 | 0 | free(sock); |
200 | 0 | return retval; |
201 | 0 | } |
202 | | |
203 | | /* Creates a new netlink socket for the same protocol as 'src'. Returns 0 and |
204 | | * sets '*sockp' to the new socket if successful, otherwise returns a positive |
205 | | * errno value. */ |
206 | | int |
207 | | nl_sock_clone(const struct nl_sock *src, struct nl_sock **sockp) |
208 | 0 | { |
209 | 0 | return nl_sock_create(src->protocol, sockp); |
210 | 0 | } |
211 | | |
212 | | /* Destroys netlink socket 'sock'. */ |
213 | | void |
214 | | nl_sock_destroy(struct nl_sock *sock) |
215 | 0 | { |
216 | 0 | if (sock) { |
217 | 0 | close(sock->fd); |
218 | 0 | free(sock); |
219 | 0 | } |
220 | 0 | } |
221 | | |
222 | | /* Tries to add 'sock' as a listener for 'multicast_group'. Returns 0 if |
223 | | * successful, otherwise a positive errno value. |
224 | | * |
225 | | * A socket that is subscribed to a multicast group that receives asynchronous |
226 | | * notifications must not be used for Netlink transactions or dumps, because |
227 | | * transactions and dumps can cause notifications to be lost. |
228 | | * |
229 | | * Multicast group numbers are always positive. |
230 | | * |
231 | | * It is not an error to attempt to join a multicast group to which a socket |
232 | | * already belongs. */ |
233 | | int |
234 | | nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group) |
235 | 0 | { |
236 | 0 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, |
237 | 0 | &multicast_group, sizeof multicast_group) < 0) { |
238 | 0 | VLOG_WARN("could not join multicast group %u (%s)", |
239 | 0 | multicast_group, ovs_strerror(errno)); |
240 | 0 | return errno; |
241 | 0 | } |
242 | 0 | return 0; |
243 | 0 | } |
244 | | |
245 | | /* When 'enable' is true, it tries to enable 'sock' to receive netlink |
246 | | * notifications form all network namespaces that have an nsid assigned |
247 | | * into the network namespace where the socket has been opened. The |
248 | | * running kernel needs to provide support for that. When 'enable' is |
249 | | * false, it will receive netlink notifications only from the network |
250 | | * namespace where the socket has been opened. |
251 | | * |
252 | | * Returns 0 if successful, otherwise a positive errno. */ |
253 | | int |
254 | | nl_sock_listen_all_nsid(struct nl_sock *sock, bool enable) |
255 | 0 | { |
256 | 0 | int error; |
257 | 0 | int val = enable ? 1 : 0; |
258 | |
|
259 | 0 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_LISTEN_ALL_NSID, &val, |
260 | 0 | sizeof val) < 0) { |
261 | 0 | error = errno; |
262 | 0 | VLOG_INFO("netlink: could not %s listening to all nsid (%s)", |
263 | 0 | enable ? "enable" : "disable", ovs_strerror(error)); |
264 | 0 | return errno; |
265 | 0 | } |
266 | | |
267 | 0 | return 0; |
268 | 0 | } |
269 | | |
270 | | /* Tries to make 'sock' stop listening to 'multicast_group'. Returns 0 if |
271 | | * successful, otherwise a positive errno value. |
272 | | * |
273 | | * Multicast group numbers are always positive. |
274 | | * |
275 | | * It is not an error to attempt to leave a multicast group to which a socket |
276 | | * does not belong. |
277 | | * |
278 | | * On success, reading from 'sock' will still return any messages that were |
279 | | * received on 'multicast_group' before the group was left. */ |
280 | | int |
281 | | nl_sock_leave_mcgroup(struct nl_sock *sock, unsigned int multicast_group) |
282 | 0 | { |
283 | 0 | if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP, |
284 | 0 | &multicast_group, sizeof multicast_group) < 0) { |
285 | 0 | VLOG_WARN("could not leave multicast group %u (%s)", |
286 | 0 | multicast_group, ovs_strerror(errno)); |
287 | 0 | return errno; |
288 | 0 | } |
289 | 0 | return 0; |
290 | 0 | } |
291 | | |
292 | | static int |
293 | | nl_sock_send__(struct nl_sock *sock, const struct ofpbuf *msg, |
294 | | uint32_t nlmsg_seq, bool wait) |
295 | 0 | { |
296 | 0 | struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg); |
297 | 0 | int error; |
298 | |
|
299 | 0 | nlmsg->nlmsg_len = msg->size; |
300 | 0 | nlmsg->nlmsg_seq = nlmsg_seq; |
301 | 0 | nlmsg->nlmsg_pid = sock->pid; |
302 | 0 | do { |
303 | 0 | int retval; |
304 | 0 | retval = send(sock->fd, msg->data, msg->size, |
305 | 0 | wait ? 0 : MSG_DONTWAIT); |
306 | 0 | error = retval < 0 ? errno : 0; |
307 | 0 | } while (error == EINTR); |
308 | 0 | log_nlmsg(__func__, error, msg->data, msg->size, sock->protocol); |
309 | 0 | if (!error) { |
310 | 0 | COVERAGE_INC(netlink_sent); |
311 | 0 | } |
312 | 0 | return error; |
313 | 0 | } |
314 | | |
315 | | /* Tries to send 'msg', which must contain a Netlink message, to the kernel on |
316 | | * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid |
317 | | * will be set to 'sock''s pid, and nlmsg_seq will be initialized to a fresh |
318 | | * sequence number, before the message is sent. |
319 | | * |
320 | | * Returns 0 if successful, otherwise a positive errno value. If |
321 | | * 'wait' is true, then the send will wait until buffer space is ready; |
322 | | * otherwise, returns EAGAIN if the 'sock' send buffer is full. */ |
323 | | int |
324 | | nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait) |
325 | 0 | { |
326 | 0 | return nl_sock_send_seq(sock, msg, nl_sock_allocate_seq(sock, 1), wait); |
327 | 0 | } |
328 | | |
329 | | /* Tries to send 'msg', which must contain a Netlink message, to the kernel on |
330 | | * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid |
331 | | * will be set to 'sock''s pid, and nlmsg_seq will be initialized to |
332 | | * 'nlmsg_seq', before the message is sent. |
333 | | * |
334 | | * Returns 0 if successful, otherwise a positive errno value. If |
335 | | * 'wait' is true, then the send will wait until buffer space is ready; |
336 | | * otherwise, returns EAGAIN if the 'sock' send buffer is full. |
337 | | * |
338 | | * This function is suitable for sending a reply to a request that was received |
339 | | * with sequence number 'nlmsg_seq'. Otherwise, use nl_sock_send() instead. */ |
340 | | int |
341 | | nl_sock_send_seq(struct nl_sock *sock, const struct ofpbuf *msg, |
342 | | uint32_t nlmsg_seq, bool wait) |
343 | 0 | { |
344 | 0 | return nl_sock_send__(sock, msg, nlmsg_seq, wait); |
345 | 0 | } |
346 | | |
347 | | static int |
348 | | nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, int *nsid, bool wait) |
349 | 0 | { |
350 | | /* We can't accurately predict the size of the data to be received. The |
351 | | * caller is supposed to have allocated enough space in 'buf' to handle the |
352 | | * "typical" case. To handle exceptions, we make available enough space in |
353 | | * 'tail' to allow Netlink messages to be up to 64 kB long (a reasonable |
354 | | * figure since that's the maximum length of a Netlink attribute). */ |
355 | 0 | struct nlmsghdr *nlmsghdr; |
356 | 0 | uint8_t tail[65536]; |
357 | 0 | struct iovec iov[2]; |
358 | 0 | struct msghdr msg; |
359 | 0 | uint8_t msgctrl[64]; |
360 | 0 | struct cmsghdr *cmsg; |
361 | 0 | ssize_t retval; |
362 | 0 | int *ptr; |
363 | 0 | int error; |
364 | |
|
365 | 0 | ovs_assert(buf->allocated >= sizeof *nlmsghdr); |
366 | 0 | ofpbuf_clear(buf); |
367 | |
|
368 | 0 | iov[0].iov_base = buf->base; |
369 | 0 | iov[0].iov_len = buf->allocated; |
370 | 0 | iov[1].iov_base = tail; |
371 | 0 | iov[1].iov_len = sizeof tail; |
372 | |
|
373 | 0 | memset(&msg, 0, sizeof msg); |
374 | 0 | msg.msg_iov = iov; |
375 | 0 | msg.msg_iovlen = 2; |
376 | 0 | msg.msg_control = msgctrl; |
377 | 0 | msg.msg_controllen = sizeof msgctrl; |
378 | | |
379 | | /* Receive a Netlink message from the kernel. |
380 | | * |
381 | | * This works around a kernel bug in which the kernel returns an error code |
382 | | * as if it were the number of bytes read. It doesn't actually modify |
383 | | * anything in the receive buffer in that case, so we can initialize the |
384 | | * Netlink header with an impossible message length and then, upon success, |
385 | | * check whether it changed. */ |
386 | 0 | nlmsghdr = buf->base; |
387 | 0 | do { |
388 | 0 | nlmsghdr->nlmsg_len = UINT32_MAX; |
389 | 0 | retval = recvmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT); |
390 | 0 | error = (retval < 0 ? errno |
391 | 0 | : retval == 0 ? ECONNRESET /* not possible? */ |
392 | 0 | : nlmsghdr->nlmsg_len != UINT32_MAX ? 0 |
393 | 0 | : retval); |
394 | 0 | } while (error == EINTR); |
395 | 0 | if (error) { |
396 | 0 | if (error == ENOBUFS) { |
397 | | /* Socket receive buffer overflow dropped one or more messages that |
398 | | * the kernel tried to send to us. */ |
399 | 0 | COVERAGE_INC(netlink_overflow); |
400 | 0 | } |
401 | 0 | return error; |
402 | 0 | } |
403 | | |
404 | 0 | if (msg.msg_flags & MSG_TRUNC) { |
405 | 0 | VLOG_ERR_RL(&rl, "truncated message (longer than %"PRIuSIZE" bytes)", |
406 | 0 | sizeof tail); |
407 | 0 | return E2BIG; |
408 | 0 | } |
409 | | |
410 | 0 | if (retval < sizeof *nlmsghdr |
411 | 0 | || nlmsghdr->nlmsg_len < sizeof *nlmsghdr |
412 | 0 | || nlmsghdr->nlmsg_len > retval) { |
413 | 0 | VLOG_ERR_RL(&rl, "received invalid nlmsg (%"PRIuSIZE" bytes < %"PRIuSIZE")", |
414 | 0 | retval, sizeof *nlmsghdr); |
415 | 0 | return EPROTO; |
416 | 0 | } |
417 | | |
418 | 0 | buf->size = MIN(retval, buf->allocated); |
419 | 0 | if (retval > buf->allocated) { |
420 | 0 | COVERAGE_INC(netlink_recv_jumbo); |
421 | 0 | ofpbuf_put(buf, tail, retval - buf->allocated); |
422 | 0 | } |
423 | |
|
424 | 0 | if (nsid) { |
425 | | /* The network namespace id from which the message was sent comes |
426 | | * as ancillary data. For older kernels, this data is either not |
427 | | * available or it might be -1, so it falls back to local network |
428 | | * namespace (no id). Latest kernels return a valid ID only if |
429 | | * available or nothing. */ |
430 | 0 | netnsid_set_local(nsid); |
431 | |
|
432 | 0 | cmsg = CMSG_FIRSTHDR(&msg); |
433 | 0 | while (cmsg != NULL) { |
434 | 0 | if (cmsg->cmsg_level == SOL_NETLINK |
435 | 0 | && cmsg->cmsg_type == NETLINK_LISTEN_ALL_NSID) { |
436 | 0 | ptr = ALIGNED_CAST(int *, CMSG_DATA(cmsg)); |
437 | 0 | netnsid_set(nsid, *ptr); |
438 | 0 | } |
439 | 0 | if (cmsg->cmsg_level == SOL_SOCKET |
440 | 0 | && cmsg->cmsg_type == SCM_RIGHTS) { |
441 | | /* This is unexpected and unwanted, close all fds */ |
442 | 0 | int nfds; |
443 | 0 | int i; |
444 | 0 | nfds = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) |
445 | 0 | / sizeof(int); |
446 | 0 | ptr = ALIGNED_CAST(int *, CMSG_DATA(cmsg)); |
447 | 0 | for (i = 0; i < nfds; i++) { |
448 | 0 | VLOG_ERR_RL(&rl, "closing unexpected received fd (%d).", |
449 | 0 | ptr[i]); |
450 | 0 | close(ptr[i]); |
451 | 0 | } |
452 | 0 | } |
453 | |
|
454 | 0 | cmsg = CMSG_NXTHDR(&msg, cmsg); |
455 | 0 | } |
456 | 0 | } |
457 | |
|
458 | 0 | log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol); |
459 | 0 | COVERAGE_INC(netlink_received); |
460 | |
|
461 | 0 | return 0; |
462 | 0 | } |
463 | | |
464 | | /* Tries to receive a Netlink message from the kernel on 'sock' into 'buf'. If |
465 | | * 'wait' is true, waits for a message to be ready. Otherwise, fails with |
466 | | * EAGAIN if the 'sock' receive buffer is empty. If 'nsid' is provided, the |
467 | | * network namespace id from which the message was sent will be provided. |
468 | | * |
469 | | * The caller must have initialized 'buf' with an allocation of at least |
470 | | * NLMSG_HDRLEN bytes. For best performance, the caller should allocate enough |
471 | | * space for a "typical" message. |
472 | | * |
473 | | * On success, returns 0 and replaces 'buf''s previous content by the received |
474 | | * message. This function expands 'buf''s allocated memory, as necessary, to |
475 | | * hold the actual size of the received message. |
476 | | * |
477 | | * On failure, returns a positive errno value and clears 'buf' to zero length. |
478 | | * 'buf' retains its previous memory allocation. |
479 | | * |
480 | | * Regardless of success or failure, this function resets 'buf''s headroom to |
481 | | * 0. */ |
482 | | int |
483 | | nl_sock_recv(struct nl_sock *sock, struct ofpbuf *buf, int *nsid, bool wait) |
484 | 0 | { |
485 | 0 | return nl_sock_recv__(sock, buf, nsid, wait); |
486 | 0 | } |
487 | | |
488 | | static void |
489 | | nl_sock_record_errors__(struct nl_transaction **transactions, size_t n, |
490 | | int error) |
491 | 0 | { |
492 | 0 | size_t i; |
493 | |
|
494 | 0 | for (i = 0; i < n; i++) { |
495 | 0 | struct nl_transaction *txn = transactions[i]; |
496 | |
|
497 | 0 | txn->error = error; |
498 | 0 | if (txn->reply) { |
499 | 0 | ofpbuf_clear(txn->reply); |
500 | 0 | } |
501 | 0 | } |
502 | 0 | } |
503 | | |
504 | | static int |
505 | | nl_sock_transact_multiple__(struct nl_sock *sock, |
506 | | struct nl_transaction **transactions, size_t n, |
507 | | size_t *done) |
508 | 0 | { |
509 | 0 | uint64_t tmp_reply_stub[1024 / 8]; |
510 | 0 | struct nl_transaction tmp_txn; |
511 | 0 | struct ofpbuf tmp_reply; |
512 | |
|
513 | 0 | uint32_t base_seq; |
514 | 0 | struct iovec iovs[MAX_IOVS]; |
515 | 0 | struct msghdr msg; |
516 | 0 | int error; |
517 | 0 | int i; |
518 | |
|
519 | 0 | base_seq = nl_sock_allocate_seq(sock, n); |
520 | 0 | *done = 0; |
521 | 0 | for (i = 0; i < n; i++) { |
522 | 0 | struct nl_transaction *txn = transactions[i]; |
523 | 0 | struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(txn->request); |
524 | |
|
525 | 0 | nlmsg->nlmsg_len = txn->request->size; |
526 | 0 | nlmsg->nlmsg_seq = base_seq + i; |
527 | 0 | nlmsg->nlmsg_pid = sock->pid; |
528 | |
|
529 | 0 | iovs[i].iov_base = txn->request->data; |
530 | 0 | iovs[i].iov_len = txn->request->size; |
531 | 0 | } |
532 | |
|
533 | 0 | memset(&msg, 0, sizeof msg); |
534 | 0 | msg.msg_iov = iovs; |
535 | 0 | msg.msg_iovlen = n; |
536 | 0 | do { |
537 | 0 | error = sendmsg(sock->fd, &msg, 0) < 0 ? errno : 0; |
538 | 0 | } while (error == EINTR); |
539 | |
|
540 | 0 | for (i = 0; i < n; i++) { |
541 | 0 | struct nl_transaction *txn = transactions[i]; |
542 | |
|
543 | 0 | log_nlmsg(__func__, error, txn->request->data, |
544 | 0 | txn->request->size, sock->protocol); |
545 | 0 | } |
546 | 0 | if (!error) { |
547 | 0 | COVERAGE_ADD(netlink_sent, n); |
548 | 0 | } |
549 | |
|
550 | 0 | if (error) { |
551 | 0 | return error; |
552 | 0 | } |
553 | | |
554 | 0 | ofpbuf_use_stub(&tmp_reply, tmp_reply_stub, sizeof tmp_reply_stub); |
555 | 0 | tmp_txn.request = NULL; |
556 | 0 | tmp_txn.reply = &tmp_reply; |
557 | 0 | tmp_txn.error = 0; |
558 | 0 | while (n > 0) { |
559 | 0 | struct nl_transaction *buf_txn, *txn; |
560 | 0 | uint32_t seq; |
561 | | |
562 | | /* Find a transaction whose buffer we can use for receiving a reply. |
563 | | * If no such transaction is left, use tmp_txn. */ |
564 | 0 | buf_txn = &tmp_txn; |
565 | 0 | for (i = 0; i < n; i++) { |
566 | 0 | if (transactions[i]->reply) { |
567 | 0 | buf_txn = transactions[i]; |
568 | 0 | break; |
569 | 0 | } |
570 | 0 | } |
571 | | |
572 | | /* Receive a reply. */ |
573 | 0 | error = nl_sock_recv__(sock, buf_txn->reply, NULL, false); |
574 | 0 | if (error) { |
575 | 0 | if (error == EAGAIN) { |
576 | 0 | nl_sock_record_errors__(transactions, n, 0); |
577 | 0 | *done += n; |
578 | 0 | error = 0; |
579 | 0 | } |
580 | 0 | break; |
581 | 0 | } |
582 | | |
583 | | /* Match the reply up with a transaction. */ |
584 | 0 | seq = nl_msg_nlmsghdr(buf_txn->reply)->nlmsg_seq; |
585 | 0 | if (seq < base_seq || seq >= base_seq + n) { |
586 | 0 | VLOG_DBG_RL(&rl, "ignoring unexpected seq %#"PRIx32, seq); |
587 | 0 | continue; |
588 | 0 | } |
589 | 0 | i = seq - base_seq; |
590 | 0 | txn = transactions[i]; |
591 | |
|
592 | 0 | const char *err_msg = NULL; |
593 | | /* Fill in the results for 'txn'. */ |
594 | 0 | if (nl_msg_nlmsgerr(buf_txn->reply, &txn->error, &err_msg)) { |
595 | 0 | if (txn->error) { |
596 | 0 | VLOG_DBG_RL(&rl, "received NAK error=%d - %s", |
597 | 0 | txn->error, |
598 | 0 | err_msg ? err_msg : ovs_strerror(txn->error)); |
599 | 0 | } |
600 | 0 | if (txn->reply) { |
601 | 0 | ofpbuf_clear(txn->reply); |
602 | 0 | } |
603 | 0 | } else { |
604 | 0 | txn->error = 0; |
605 | 0 | if (txn->reply && txn != buf_txn) { |
606 | | /* Swap buffers. */ |
607 | 0 | struct ofpbuf *reply = buf_txn->reply; |
608 | 0 | buf_txn->reply = txn->reply; |
609 | 0 | txn->reply = reply; |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | /* Fill in the results for transactions before 'txn'. (We have to do |
614 | | * this after the results for 'txn' itself because of the buffer swap |
615 | | * above.) */ |
616 | 0 | nl_sock_record_errors__(transactions, i, 0); |
617 | | |
618 | | /* Advance. */ |
619 | 0 | *done += i + 1; |
620 | 0 | transactions += i + 1; |
621 | 0 | n -= i + 1; |
622 | 0 | base_seq += i + 1; |
623 | 0 | } |
624 | 0 | ofpbuf_uninit(&tmp_reply); |
625 | |
|
626 | 0 | return error; |
627 | 0 | } |
628 | | |
629 | | static void |
630 | | nl_sock_transact_multiple(struct nl_sock *sock, |
631 | | struct nl_transaction **transactions, size_t n) |
632 | 0 | { |
633 | 0 | int max_batch_count; |
634 | 0 | int error; |
635 | |
|
636 | 0 | if (!n) { |
637 | 0 | return; |
638 | 0 | } |
639 | | |
640 | | /* In theory, every request could have a 64 kB reply. But the default and |
641 | | * maximum socket rcvbuf size with typical Dom0 memory sizes both tend to |
642 | | * be a bit below 128 kB, so that would only allow a single message in a |
643 | | * "batch". So we assume that replies average (at most) 4 kB, which allows |
644 | | * a good deal of batching. |
645 | | * |
646 | | * In practice, most of the requests that we batch either have no reply at |
647 | | * all or a brief reply. */ |
648 | 0 | max_batch_count = MAX(sock->rcvbuf / 4096, 1); |
649 | 0 | max_batch_count = MIN(max_batch_count, max_iovs); |
650 | |
|
651 | 0 | while (n > 0) { |
652 | 0 | size_t count, bytes; |
653 | 0 | size_t done; |
654 | | |
655 | | /* Batch up to 'max_batch_count' transactions. But cap it at about a |
656 | | * page of requests total because big skbuffs are expensive to |
657 | | * allocate in the kernel. */ |
658 | | #if defined(PAGESIZE) |
659 | | enum { MAX_BATCH_BYTES = MAX(1, PAGESIZE - 512) }; |
660 | | #else |
661 | 0 | enum { MAX_BATCH_BYTES = 4096 - 512 }; |
662 | 0 | #endif |
663 | 0 | bytes = transactions[0]->request->size; |
664 | 0 | for (count = 1; count < n && count < max_batch_count; count++) { |
665 | 0 | if (bytes + transactions[count]->request->size > MAX_BATCH_BYTES) { |
666 | 0 | break; |
667 | 0 | } |
668 | 0 | bytes += transactions[count]->request->size; |
669 | 0 | } |
670 | |
|
671 | 0 | error = nl_sock_transact_multiple__(sock, transactions, count, &done); |
672 | 0 | transactions += done; |
673 | 0 | n -= done; |
674 | |
|
675 | 0 | if (error == ENOBUFS) { |
676 | 0 | VLOG_DBG_RL(&rl, "receive buffer overflow, resending request"); |
677 | 0 | } else if (error) { |
678 | 0 | VLOG_ERR_RL(&rl, "transaction error (%s)", ovs_strerror(error)); |
679 | 0 | nl_sock_record_errors__(transactions, n, error); |
680 | 0 | if (error != EAGAIN) { |
681 | | /* A fatal error has occurred. Abort the rest of |
682 | | * transactions. */ |
683 | 0 | break; |
684 | 0 | } |
685 | 0 | } |
686 | 0 | } |
687 | 0 | } |
688 | | |
689 | | static int |
690 | | nl_sock_transact(struct nl_sock *sock, const struct ofpbuf *request, |
691 | | struct ofpbuf **replyp) |
692 | 0 | { |
693 | 0 | struct nl_transaction *transactionp; |
694 | 0 | struct nl_transaction transaction; |
695 | |
|
696 | 0 | transaction.request = CONST_CAST(struct ofpbuf *, request); |
697 | 0 | transaction.reply = replyp ? ofpbuf_new(1024) : NULL; |
698 | 0 | transactionp = &transaction; |
699 | |
|
700 | 0 | nl_sock_transact_multiple(sock, &transactionp, 1); |
701 | |
|
702 | 0 | if (replyp) { |
703 | 0 | if (transaction.error) { |
704 | 0 | ofpbuf_delete(transaction.reply); |
705 | 0 | *replyp = NULL; |
706 | 0 | } else { |
707 | 0 | *replyp = transaction.reply; |
708 | 0 | } |
709 | 0 | } |
710 | |
|
711 | 0 | return transaction.error; |
712 | 0 | } |
713 | | |
714 | | /* Drain all the messages currently in 'sock''s receive queue. */ |
715 | | int |
716 | | nl_sock_drain(struct nl_sock *sock) |
717 | 0 | { |
718 | 0 | return drain_rcvbuf(sock->fd); |
719 | 0 | } |
720 | | |
721 | | /* Starts a Netlink "dump" operation, by sending 'request' to the kernel on a |
722 | | * Netlink socket created with the given 'protocol', and initializes 'dump' to |
723 | | * reflect the state of the operation. |
724 | | * |
725 | | * 'request' must contain a Netlink message. Before sending the message, |
726 | | * nlmsg_len will be finalized to match request->size, and nlmsg_pid will be |
727 | | * set to the Netlink socket's pid. NLM_F_DUMP and NLM_F_ACK will be set in |
728 | | * nlmsg_flags. |
729 | | * |
730 | | * The design of this Netlink socket library ensures that the dump is reliable. |
731 | | * |
732 | | * This function provides no status indication. nl_dump_done() provides an |
733 | | * error status for the entire dump operation. |
734 | | * |
735 | | * The caller must eventually destroy 'request'. |
736 | | */ |
737 | | void |
738 | | nl_dump_start(struct nl_dump *dump, int protocol, const struct ofpbuf *request) |
739 | 0 | { |
740 | 0 | nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK; |
741 | |
|
742 | 0 | ovs_mutex_init(&dump->mutex); |
743 | 0 | ovs_mutex_lock(&dump->mutex); |
744 | 0 | dump->status = nl_pool_alloc(protocol, &dump->sock); |
745 | 0 | if (!dump->status) { |
746 | 0 | dump->status = nl_sock_send__(dump->sock, request, |
747 | 0 | nl_sock_allocate_seq(dump->sock, 1), |
748 | 0 | true); |
749 | 0 | } |
750 | 0 | dump->nl_seq = nl_msg_nlmsghdr(request)->nlmsg_seq; |
751 | 0 | ovs_mutex_unlock(&dump->mutex); |
752 | 0 | } |
753 | | |
754 | | static int |
755 | | nl_dump_refill(struct nl_dump *dump, struct ofpbuf *buffer) |
756 | | OVS_REQUIRES(dump->mutex) |
757 | 0 | { |
758 | 0 | struct nlmsghdr *nlmsghdr; |
759 | 0 | int error; |
760 | |
|
761 | 0 | while (!buffer->size) { |
762 | 0 | error = nl_sock_recv__(dump->sock, buffer, NULL, false); |
763 | 0 | if (error) { |
764 | | /* The kernel never blocks providing the results of a dump, so |
765 | | * error == EAGAIN means that we've read the whole thing, and |
766 | | * therefore transform it into EOF. (The kernel always provides |
767 | | * NLMSG_DONE as a sentinel. Some other thread must have received |
768 | | * that already but not yet signaled it in 'status'.) |
769 | | * |
770 | | * Any other error is just an error. */ |
771 | 0 | return error == EAGAIN ? EOF : error; |
772 | 0 | } |
773 | | |
774 | 0 | nlmsghdr = nl_msg_nlmsghdr(buffer); |
775 | 0 | if (dump->nl_seq != nlmsghdr->nlmsg_seq) { |
776 | 0 | VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32, |
777 | 0 | nlmsghdr->nlmsg_seq, dump->nl_seq); |
778 | 0 | ofpbuf_clear(buffer); |
779 | 0 | } |
780 | 0 | } |
781 | | |
782 | 0 | if (nl_msg_nlmsgerr(buffer, &error, NULL) && error) { |
783 | 0 | VLOG_INFO_RL(&rl, "netlink dump request error (%s)", |
784 | 0 | ovs_strerror(error)); |
785 | 0 | ofpbuf_clear(buffer); |
786 | 0 | return error; |
787 | 0 | } |
788 | | |
789 | 0 | return 0; |
790 | 0 | } |
791 | | |
792 | | static int |
793 | | nl_dump_next__(struct ofpbuf *reply, struct ofpbuf *buffer) |
794 | 0 | { |
795 | 0 | struct nlmsghdr *nlmsghdr = nl_msg_next(buffer, reply); |
796 | 0 | if (!nlmsghdr) { |
797 | 0 | VLOG_WARN_RL(&rl, "netlink dump contains message fragment"); |
798 | 0 | return EPROTO; |
799 | 0 | } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) { |
800 | 0 | return EOF; |
801 | 0 | } else { |
802 | 0 | return 0; |
803 | 0 | } |
804 | 0 | } |
805 | | |
806 | | /* Attempts to retrieve another reply from 'dump' into 'buffer'. 'dump' must |
807 | | * have been initialized with nl_dump_start(), and 'buffer' must have been |
808 | | * initialized. 'buffer' should be at least NL_DUMP_BUFSIZE bytes long. |
809 | | * |
810 | | * If successful, returns true and points 'reply->data' and |
811 | | * 'reply->size' to the message that was retrieved. The caller must not |
812 | | * modify 'reply' (because it points within 'buffer', which will be used by |
813 | | * future calls to this function). |
814 | | * |
815 | | * On failure, returns false and sets 'reply->data' to NULL and |
816 | | * 'reply->size' to 0. Failure might indicate an actual error or merely |
817 | | * the end of replies. An error status for the entire dump operation is |
818 | | * provided when it is completed by calling nl_dump_done(). |
819 | | * |
820 | | * Multiple threads may call this function, passing the same nl_dump, however |
821 | | * each must provide independent buffers. This function may cache multiple |
822 | | * replies in the buffer, and these will be processed before more replies are |
823 | | * fetched. When this function returns false, other threads may continue to |
824 | | * process replies in their buffers, but they will not fetch more replies. |
825 | | */ |
826 | | bool |
827 | | nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply, struct ofpbuf *buffer) |
828 | 0 | { |
829 | 0 | int retval = 0; |
830 | | |
831 | | /* If the buffer is empty, refill it. |
832 | | * |
833 | | * If the buffer is not empty, we don't check the dump's status. |
834 | | * Otherwise, we could end up skipping some of the dump results if thread A |
835 | | * hits EOF while thread B is in the midst of processing a batch. */ |
836 | 0 | if (!buffer->size) { |
837 | 0 | ovs_mutex_lock(&dump->mutex); |
838 | 0 | if (!dump->status) { |
839 | | /* Take the mutex here to avoid an in-kernel race. If two threads |
840 | | * try to read from a Netlink dump socket at once, then the socket |
841 | | * error can be set to EINVAL, which will be encountered on the |
842 | | * next recv on that socket, which could be anywhere due to the way |
843 | | * that we pool Netlink sockets. Serializing the recv calls avoids |
844 | | * the issue. */ |
845 | 0 | dump->status = nl_dump_refill(dump, buffer); |
846 | 0 | } |
847 | 0 | retval = dump->status; |
848 | 0 | ovs_mutex_unlock(&dump->mutex); |
849 | 0 | } |
850 | | |
851 | | /* Fetch the next message from the buffer. */ |
852 | 0 | if (!retval) { |
853 | 0 | retval = nl_dump_next__(reply, buffer); |
854 | 0 | if (retval) { |
855 | | /* Record 'retval' as the dump status, but don't overwrite an error |
856 | | * with EOF. */ |
857 | 0 | ovs_mutex_lock(&dump->mutex); |
858 | 0 | if (dump->status <= 0) { |
859 | 0 | dump->status = retval; |
860 | 0 | } |
861 | 0 | ovs_mutex_unlock(&dump->mutex); |
862 | 0 | } |
863 | 0 | } |
864 | |
|
865 | 0 | if (retval) { |
866 | 0 | reply->data = NULL; |
867 | 0 | reply->size = 0; |
868 | 0 | } |
869 | 0 | return !retval; |
870 | 0 | } |
871 | | |
872 | | /* Completes Netlink dump operation 'dump', which must have been initialized |
873 | | * with nl_dump_start(). Returns 0 if the dump operation was error-free, |
874 | | * otherwise a positive errno value describing the problem. */ |
875 | | int |
876 | | nl_dump_done(struct nl_dump *dump) |
877 | 0 | { |
878 | 0 | int status; |
879 | |
|
880 | 0 | ovs_mutex_lock(&dump->mutex); |
881 | 0 | status = dump->status; |
882 | 0 | ovs_mutex_unlock(&dump->mutex); |
883 | | |
884 | | /* Drain any remaining messages that the client didn't read. Otherwise the |
885 | | * kernel will continue to queue them up and waste buffer space. |
886 | | * |
887 | | * XXX We could just destroy and discard the socket in this case. */ |
888 | 0 | if (!status) { |
889 | 0 | uint64_t tmp_reply_stub[NL_DUMP_BUFSIZE / 8]; |
890 | 0 | struct ofpbuf reply, buf; |
891 | |
|
892 | 0 | ofpbuf_use_stub(&buf, tmp_reply_stub, sizeof tmp_reply_stub); |
893 | 0 | while (nl_dump_next(dump, &reply, &buf)) { |
894 | | /* Nothing to do. */ |
895 | 0 | } |
896 | 0 | ofpbuf_uninit(&buf); |
897 | |
|
898 | 0 | ovs_mutex_lock(&dump->mutex); |
899 | 0 | status = dump->status; |
900 | 0 | ovs_mutex_unlock(&dump->mutex); |
901 | 0 | ovs_assert(status); |
902 | 0 | } |
903 | |
|
904 | 0 | nl_pool_release(dump->sock); |
905 | 0 | ovs_mutex_destroy(&dump->mutex); |
906 | |
|
907 | 0 | return status == EOF ? 0 : status; |
908 | 0 | } |
909 | | |
910 | | /* Causes poll_block() to wake up when any of the specified 'events' (which is |
911 | | * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'. */ |
912 | | void |
913 | | nl_sock_wait(const struct nl_sock *sock, short int events) |
914 | 0 | { |
915 | 0 | poll_fd_wait(sock->fd, events); |
916 | 0 | } |
917 | | |
918 | | /* Returns the underlying fd for 'sock', for use in "poll()"-like operations |
919 | | * that can't use nl_sock_wait(). |
920 | | * |
921 | | * It's a little tricky to use the returned fd correctly, because nl_sock does |
922 | | * "copy on write" to allow a single nl_sock to be used for notifications, |
923 | | * transactions, and dumps. If 'sock' is used only for notifications and |
924 | | * transactions (and never for dump) then the usage is safe. */ |
925 | | int |
926 | | nl_sock_fd(const struct nl_sock *sock) |
927 | 0 | { |
928 | 0 | return sock->fd; |
929 | 0 | } |
930 | | |
931 | | /* Returns the PID associated with this socket. */ |
932 | | uint32_t |
933 | | nl_sock_pid(const struct nl_sock *sock) |
934 | 0 | { |
935 | 0 | return sock->pid; |
936 | 0 | } |
937 | | |
938 | | /* Miscellaneous. */ |
939 | | |
940 | | struct genl_family { |
941 | | struct hmap_node hmap_node; |
942 | | uint16_t id; |
943 | | char *name; |
944 | | }; |
945 | | |
946 | | static struct hmap genl_families = HMAP_INITIALIZER(&genl_families); |
947 | | |
948 | | static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = { |
949 | | [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16}, |
950 | | [CTRL_ATTR_MCAST_GROUPS] = {.type = NL_A_NESTED, .optional = true}, |
951 | | }; |
952 | | |
953 | | static struct genl_family * |
954 | | find_genl_family_by_id(uint16_t id) |
955 | 0 | { |
956 | 0 | struct genl_family *family; |
957 | |
|
958 | 0 | HMAP_FOR_EACH_IN_BUCKET (family, hmap_node, hash_int(id, 0), |
959 | 0 | &genl_families) { |
960 | 0 | if (family->id == id) { |
961 | 0 | return family; |
962 | 0 | } |
963 | 0 | } |
964 | 0 | return NULL; |
965 | 0 | } |
966 | | |
967 | | static void |
968 | | define_genl_family(uint16_t id, const char *name) |
969 | 0 | { |
970 | 0 | struct genl_family *family = find_genl_family_by_id(id); |
971 | |
|
972 | 0 | if (family) { |
973 | 0 | if (!strcmp(family->name, name)) { |
974 | 0 | return; |
975 | 0 | } |
976 | 0 | free(family->name); |
977 | 0 | } else { |
978 | 0 | family = xmalloc(sizeof *family); |
979 | 0 | family->id = id; |
980 | 0 | hmap_insert(&genl_families, &family->hmap_node, hash_int(id, 0)); |
981 | 0 | } |
982 | 0 | family->name = xstrdup(name); |
983 | 0 | } |
984 | | |
985 | | static const char * |
986 | | genl_family_to_name(uint16_t id) |
987 | 0 | { |
988 | 0 | if (id == GENL_ID_CTRL) { |
989 | 0 | return "control"; |
990 | 0 | } else { |
991 | 0 | struct genl_family *family = find_genl_family_by_id(id); |
992 | 0 | return family ? family->name : "unknown"; |
993 | 0 | } |
994 | 0 | } |
995 | | |
996 | | static int |
997 | | do_lookup_genl_family(const char *name, struct nlattr **attrs, |
998 | | struct ofpbuf **replyp) |
999 | 0 | { |
1000 | 0 | struct nl_sock *sock; |
1001 | 0 | struct ofpbuf request, *reply; |
1002 | 0 | int error; |
1003 | |
|
1004 | 0 | *replyp = NULL; |
1005 | 0 | error = nl_sock_create(NETLINK_GENERIC, &sock); |
1006 | 0 | if (error) { |
1007 | 0 | return error; |
1008 | 0 | } |
1009 | | |
1010 | 0 | ofpbuf_init(&request, 0); |
1011 | 0 | nl_msg_put_genlmsghdr(&request, 0, GENL_ID_CTRL, NLM_F_REQUEST, |
1012 | 0 | CTRL_CMD_GETFAMILY, 1); |
1013 | 0 | nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name); |
1014 | 0 | error = nl_sock_transact(sock, &request, &reply); |
1015 | 0 | ofpbuf_uninit(&request); |
1016 | 0 | if (error) { |
1017 | 0 | nl_sock_destroy(sock); |
1018 | 0 | return error; |
1019 | 0 | } |
1020 | | |
1021 | 0 | if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN, |
1022 | 0 | family_policy, attrs, ARRAY_SIZE(family_policy)) |
1023 | 0 | || nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]) == 0) { |
1024 | 0 | nl_sock_destroy(sock); |
1025 | 0 | ofpbuf_delete(reply); |
1026 | 0 | return EPROTO; |
1027 | 0 | } |
1028 | | |
1029 | 0 | nl_sock_destroy(sock); |
1030 | 0 | *replyp = reply; |
1031 | 0 | return 0; |
1032 | 0 | } |
1033 | | |
1034 | | /* Finds the multicast group called 'group_name' in genl family 'family_name'. |
1035 | | * When successful, writes its result to 'multicast_group' and returns 0. |
1036 | | * Otherwise, clears 'multicast_group' and returns a positive error code. |
1037 | | */ |
1038 | | int |
1039 | | nl_lookup_genl_mcgroup(const char *family_name, const char *group_name, |
1040 | | unsigned int *multicast_group) |
1041 | 0 | { |
1042 | 0 | struct nlattr *family_attrs[ARRAY_SIZE(family_policy)]; |
1043 | 0 | const struct nlattr *mc; |
1044 | 0 | struct ofpbuf *reply; |
1045 | 0 | unsigned int left; |
1046 | 0 | int error; |
1047 | |
|
1048 | 0 | *multicast_group = 0; |
1049 | 0 | error = do_lookup_genl_family(family_name, family_attrs, &reply); |
1050 | 0 | if (error) { |
1051 | 0 | return error; |
1052 | 0 | } |
1053 | | |
1054 | 0 | if (!family_attrs[CTRL_ATTR_MCAST_GROUPS]) { |
1055 | 0 | error = EPROTO; |
1056 | 0 | goto exit; |
1057 | 0 | } |
1058 | | |
1059 | 0 | NL_NESTED_FOR_EACH (mc, left, family_attrs[CTRL_ATTR_MCAST_GROUPS]) { |
1060 | 0 | static const struct nl_policy mc_policy[] = { |
1061 | 0 | [CTRL_ATTR_MCAST_GRP_ID] = {.type = NL_A_U32}, |
1062 | 0 | [CTRL_ATTR_MCAST_GRP_NAME] = {.type = NL_A_STRING}, |
1063 | 0 | }; |
1064 | |
|
1065 | 0 | struct nlattr *mc_attrs[ARRAY_SIZE(mc_policy)]; |
1066 | 0 | const char *mc_name; |
1067 | |
|
1068 | 0 | if (!nl_parse_nested(mc, mc_policy, mc_attrs, ARRAY_SIZE(mc_policy))) { |
1069 | 0 | error = EPROTO; |
1070 | 0 | goto exit; |
1071 | 0 | } |
1072 | | |
1073 | 0 | mc_name = nl_attr_get_string(mc_attrs[CTRL_ATTR_MCAST_GRP_NAME]); |
1074 | 0 | if (!strcmp(group_name, mc_name)) { |
1075 | 0 | *multicast_group = |
1076 | 0 | nl_attr_get_u32(mc_attrs[CTRL_ATTR_MCAST_GRP_ID]); |
1077 | 0 | error = 0; |
1078 | 0 | goto exit; |
1079 | 0 | } |
1080 | 0 | } |
1081 | 0 | error = EPROTO; |
1082 | |
|
1083 | 0 | exit: |
1084 | 0 | ofpbuf_delete(reply); |
1085 | 0 | return error; |
1086 | 0 | } |
1087 | | |
1088 | | /* If '*number' is 0, translates the given Generic Netlink family 'name' to a |
1089 | | * number and stores it in '*number'. If successful, returns 0 and the caller |
1090 | | * may use '*number' as the family number. On failure, returns a positive |
1091 | | * errno value and '*number' caches the errno value. */ |
1092 | | int |
1093 | | nl_lookup_genl_family(const char *name, int *number) |
1094 | 0 | { |
1095 | 0 | if (*number == 0) { |
1096 | 0 | struct nlattr *attrs[ARRAY_SIZE(family_policy)]; |
1097 | 0 | struct ofpbuf *reply; |
1098 | 0 | int error; |
1099 | |
|
1100 | 0 | error = do_lookup_genl_family(name, attrs, &reply); |
1101 | 0 | if (!error) { |
1102 | 0 | *number = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]); |
1103 | 0 | define_genl_family(*number, name); |
1104 | 0 | } else { |
1105 | 0 | *number = -error; |
1106 | 0 | } |
1107 | 0 | ofpbuf_delete(reply); |
1108 | |
|
1109 | 0 | ovs_assert(*number != 0); |
1110 | 0 | } |
1111 | 0 | return *number > 0 ? 0 : -*number; |
1112 | 0 | } |
1113 | | |
1114 | | struct nl_pool { |
1115 | | struct nl_sock *socks[16]; |
1116 | | int n; |
1117 | | }; |
1118 | | |
1119 | | static struct ovs_mutex pool_mutex = OVS_MUTEX_INITIALIZER; |
1120 | | static struct nl_pool pools[MAX_LINKS] OVS_GUARDED_BY(pool_mutex); |
1121 | | |
1122 | | static int |
1123 | | nl_pool_alloc(int protocol, struct nl_sock **sockp) |
1124 | 0 | { |
1125 | 0 | struct nl_sock *sock = NULL; |
1126 | 0 | struct nl_pool *pool; |
1127 | |
|
1128 | 0 | ovs_assert(protocol >= 0 && protocol < ARRAY_SIZE(pools)); |
1129 | |
|
1130 | 0 | ovs_mutex_lock(&pool_mutex); |
1131 | 0 | pool = &pools[protocol]; |
1132 | 0 | if (pool->n > 0) { |
1133 | 0 | sock = pool->socks[--pool->n]; |
1134 | 0 | } |
1135 | 0 | ovs_mutex_unlock(&pool_mutex); |
1136 | |
|
1137 | 0 | if (sock) { |
1138 | 0 | *sockp = sock; |
1139 | 0 | return 0; |
1140 | 0 | } else { |
1141 | 0 | return nl_sock_create(protocol, sockp); |
1142 | 0 | } |
1143 | 0 | } |
1144 | | |
1145 | | static void |
1146 | | nl_pool_release(struct nl_sock *sock) |
1147 | 0 | { |
1148 | 0 | if (sock) { |
1149 | 0 | struct nl_pool *pool = &pools[sock->protocol]; |
1150 | |
|
1151 | 0 | ovs_mutex_lock(&pool_mutex); |
1152 | 0 | if (pool->n < ARRAY_SIZE(pool->socks)) { |
1153 | 0 | pool->socks[pool->n++] = sock; |
1154 | 0 | sock = NULL; |
1155 | 0 | } |
1156 | 0 | ovs_mutex_unlock(&pool_mutex); |
1157 | |
|
1158 | 0 | nl_sock_destroy(sock); |
1159 | 0 | } |
1160 | 0 | } |
1161 | | |
1162 | | /* Sends 'request' to the kernel on a Netlink socket for the given 'protocol' |
1163 | | * (e.g. NETLINK_ROUTE or NETLINK_GENERIC) and waits for a response. If |
1164 | | * successful, returns 0. On failure, returns a positive errno value. |
1165 | | * |
1166 | | * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's |
1167 | | * reply, which the caller is responsible for freeing with ofpbuf_delete(), and |
1168 | | * on failure '*replyp' is set to NULL. If 'replyp' is null, then the kernel's |
1169 | | * reply, if any, is discarded. |
1170 | | * |
1171 | | * Before the message is sent, nlmsg_len in 'request' will be finalized to |
1172 | | * match msg->size, nlmsg_pid will be set to the pid of the socket used |
1173 | | * for sending the request, and nlmsg_seq will be initialized. |
1174 | | * |
1175 | | * The caller is responsible for destroying 'request'. |
1176 | | * |
1177 | | * Bare Netlink is an unreliable transport protocol. This function layers |
1178 | | * reliable delivery and reply semantics on top of bare Netlink. |
1179 | | * |
1180 | | * In Netlink, sending a request to the kernel is reliable enough, because the |
1181 | | * kernel will tell us if the message cannot be queued (and we will in that |
1182 | | * case put it on the transmit queue and wait until it can be delivered). |
1183 | | * |
1184 | | * Receiving the reply is the real problem: if the socket buffer is full when |
1185 | | * the kernel tries to send the reply, the reply will be dropped. However, the |
1186 | | * kernel sets a flag that a reply has been dropped. The next call to recv |
1187 | | * then returns ENOBUFS. We can then re-send the request. |
1188 | | * |
1189 | | * Caveats: |
1190 | | * |
1191 | | * 1. Netlink depends on sequence numbers to match up requests and |
1192 | | * replies. The sender of a request supplies a sequence number, and |
1193 | | * the reply echos back that sequence number. |
1194 | | * |
1195 | | * This is fine, but (1) some kernel netlink implementations are |
1196 | | * broken, in that they fail to echo sequence numbers and (2) this |
1197 | | * function will drop packets with non-matching sequence numbers, so |
1198 | | * that only a single request can be usefully transacted at a time. |
1199 | | * |
1200 | | * 2. Resending the request causes it to be re-executed, so the request |
1201 | | * needs to be idempotent. |
1202 | | */ |
1203 | | int |
1204 | | nl_transact(int protocol, const struct ofpbuf *request, |
1205 | | struct ofpbuf **replyp) |
1206 | 0 | { |
1207 | 0 | struct nl_sock *sock; |
1208 | 0 | int error; |
1209 | |
|
1210 | 0 | error = nl_pool_alloc(protocol, &sock); |
1211 | 0 | if (error) { |
1212 | 0 | if (replyp) { |
1213 | 0 | *replyp = NULL; |
1214 | 0 | } |
1215 | 0 | return error; |
1216 | 0 | } |
1217 | | |
1218 | 0 | error = nl_sock_transact(sock, request, replyp); |
1219 | |
|
1220 | 0 | nl_pool_release(sock); |
1221 | 0 | return error; |
1222 | 0 | } |
1223 | | |
1224 | | /* Sends the 'request' member of the 'n' transactions in 'transactions' on a |
1225 | | * Netlink socket for the given 'protocol' (e.g. NETLINK_ROUTE or |
1226 | | * NETLINK_GENERIC), in order, and receives responses to all of them. Fills in |
1227 | | * the 'error' member of each transaction with 0 if it was successful, |
1228 | | * otherwise with a positive errno value. If 'reply' is nonnull, then it will |
1229 | | * be filled with the reply if the message receives a detailed reply. In other |
1230 | | * cases, i.e. where the request failed or had no reply beyond an indication of |
1231 | | * success, 'reply' will be cleared if it is nonnull. |
1232 | | * |
1233 | | * The caller is responsible for destroying each request and reply, and the |
1234 | | * transactions array itself. |
1235 | | * |
1236 | | * Before sending each message, this function will finalize nlmsg_len in each |
1237 | | * 'request' to match the ofpbuf's size, set nlmsg_pid to the pid of the socket |
1238 | | * used for the transaction, and initialize nlmsg_seq. |
1239 | | * |
1240 | | * Bare Netlink is an unreliable transport protocol. This function layers |
1241 | | * reliable delivery and reply semantics on top of bare Netlink. See |
1242 | | * nl_transact() for some caveats. |
1243 | | */ |
1244 | | void |
1245 | | nl_transact_multiple(int protocol, |
1246 | | struct nl_transaction **transactions, size_t n) |
1247 | 0 | { |
1248 | 0 | struct nl_sock *sock; |
1249 | 0 | int error; |
1250 | |
|
1251 | 0 | error = nl_pool_alloc(protocol, &sock); |
1252 | 0 | if (!error) { |
1253 | 0 | nl_sock_transact_multiple(sock, transactions, n); |
1254 | 0 | nl_pool_release(sock); |
1255 | 0 | } else { |
1256 | 0 | nl_sock_record_errors__(transactions, n, error); |
1257 | 0 | } |
1258 | 0 | } |
1259 | | |
1260 | | |
1261 | | static uint32_t |
1262 | | nl_sock_allocate_seq(struct nl_sock *sock, unsigned int n) |
1263 | 0 | { |
1264 | 0 | uint32_t seq = sock->next_seq; |
1265 | |
|
1266 | 0 | sock->next_seq += n; |
1267 | | |
1268 | | /* Make it impossible for the next request for sequence numbers to wrap |
1269 | | * around to 0. Start over with 1 to avoid ever using a sequence number of |
1270 | | * 0, because the kernel uses sequence number 0 for notifications. */ |
1271 | 0 | if (sock->next_seq >= UINT32_MAX / 2) { |
1272 | 0 | sock->next_seq = 1; |
1273 | 0 | } |
1274 | |
|
1275 | 0 | return seq; |
1276 | 0 | } |
1277 | | |
1278 | | static void |
1279 | | nlmsghdr_to_string(const struct nlmsghdr *h, int protocol, struct ds *ds) |
1280 | 0 | { |
1281 | 0 | struct nlmsg_flag { |
1282 | 0 | unsigned int bits; |
1283 | 0 | const char *name; |
1284 | 0 | }; |
1285 | 0 | static const struct nlmsg_flag flags[] = { |
1286 | 0 | { NLM_F_REQUEST, "REQUEST" }, |
1287 | 0 | { NLM_F_MULTI, "MULTI" }, |
1288 | 0 | { NLM_F_ACK, "ACK" }, |
1289 | 0 | { NLM_F_ECHO, "ECHO" }, |
1290 | 0 | { NLM_F_DUMP, "DUMP" }, |
1291 | 0 | { NLM_F_ROOT, "ROOT" }, |
1292 | 0 | { NLM_F_MATCH, "MATCH" }, |
1293 | 0 | { NLM_F_ATOMIC, "ATOMIC" }, |
1294 | 0 | }; |
1295 | 0 | const struct nlmsg_flag *flag; |
1296 | 0 | uint16_t flags_left; |
1297 | |
|
1298 | 0 | ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16, |
1299 | 0 | h->nlmsg_len, h->nlmsg_type); |
1300 | 0 | if (h->nlmsg_type == NLMSG_NOOP) { |
1301 | 0 | ds_put_cstr(ds, "(no-op)"); |
1302 | 0 | } else if (h->nlmsg_type == NLMSG_ERROR) { |
1303 | 0 | ds_put_cstr(ds, "(error)"); |
1304 | 0 | } else if (h->nlmsg_type == NLMSG_DONE) { |
1305 | 0 | ds_put_cstr(ds, "(done)"); |
1306 | 0 | } else if (h->nlmsg_type == NLMSG_OVERRUN) { |
1307 | 0 | ds_put_cstr(ds, "(overrun)"); |
1308 | 0 | } else if (h->nlmsg_type < NLMSG_MIN_TYPE) { |
1309 | 0 | ds_put_cstr(ds, "(reserved)"); |
1310 | 0 | } else if (protocol == NETLINK_GENERIC) { |
1311 | 0 | ds_put_format(ds, "(%s)", genl_family_to_name(h->nlmsg_type)); |
1312 | 0 | } else { |
1313 | 0 | ds_put_cstr(ds, "(family-defined)"); |
1314 | 0 | } |
1315 | 0 | ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags); |
1316 | 0 | flags_left = h->nlmsg_flags; |
1317 | 0 | for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { |
1318 | 0 | if ((flags_left & flag->bits) == flag->bits) { |
1319 | 0 | ds_put_format(ds, "[%s]", flag->name); |
1320 | 0 | flags_left &= ~flag->bits; |
1321 | 0 | } |
1322 | 0 | } |
1323 | 0 | if (flags_left) { |
1324 | 0 | ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left); |
1325 | 0 | } |
1326 | 0 | ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32, |
1327 | 0 | h->nlmsg_seq, h->nlmsg_pid); |
1328 | 0 | } |
1329 | | |
1330 | | static char * |
1331 | | nlmsg_to_string(const struct ofpbuf *buffer, int protocol) |
1332 | 0 | { |
1333 | 0 | struct ds ds = DS_EMPTY_INITIALIZER; |
1334 | 0 | const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN); |
1335 | 0 | if (h) { |
1336 | 0 | nlmsghdr_to_string(h, protocol, &ds); |
1337 | 0 | if (h->nlmsg_type == NLMSG_ERROR) { |
1338 | 0 | const struct nlmsgerr *e; |
1339 | 0 | e = ofpbuf_at(buffer, NLMSG_HDRLEN, |
1340 | 0 | NLMSG_ALIGN(sizeof(struct nlmsgerr))); |
1341 | 0 | if (e) { |
1342 | 0 | ds_put_format(&ds, " error(%d", e->error); |
1343 | 0 | if (e->error < 0) { |
1344 | 0 | ds_put_format(&ds, "(%s)", ovs_strerror(-e->error)); |
1345 | 0 | } |
1346 | 0 | ds_put_cstr(&ds, ", in-reply-to("); |
1347 | 0 | nlmsghdr_to_string(&e->msg, protocol, &ds); |
1348 | 0 | ds_put_cstr(&ds, "))"); |
1349 | 0 | } else { |
1350 | 0 | ds_put_cstr(&ds, " error(truncated)"); |
1351 | 0 | } |
1352 | 0 | } else if (h->nlmsg_type == NLMSG_DONE) { |
1353 | 0 | int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error); |
1354 | 0 | if (error) { |
1355 | 0 | ds_put_format(&ds, " done(%d", *error); |
1356 | 0 | if (*error < 0) { |
1357 | 0 | ds_put_format(&ds, "(%s)", ovs_strerror(-*error)); |
1358 | 0 | } |
1359 | 0 | ds_put_cstr(&ds, ")"); |
1360 | 0 | } else { |
1361 | 0 | ds_put_cstr(&ds, " done(truncated)"); |
1362 | 0 | } |
1363 | 0 | } else if (protocol == NETLINK_GENERIC) { |
1364 | 0 | struct genlmsghdr *genl = nl_msg_genlmsghdr(buffer); |
1365 | 0 | if (genl) { |
1366 | 0 | ds_put_format(&ds, ",genl(cmd=%"PRIu8",version=%"PRIu8")", |
1367 | 0 | genl->cmd, genl->version); |
1368 | 0 | } |
1369 | 0 | } |
1370 | 0 | } else { |
1371 | 0 | ds_put_cstr(&ds, "nl(truncated)"); |
1372 | 0 | } |
1373 | 0 | return ds.string; |
1374 | 0 | } |
1375 | | |
1376 | | static void |
1377 | | log_nlmsg(const char *function, int error, |
1378 | | const void *message, size_t size, int protocol) |
1379 | 0 | { |
1380 | 0 | if (!VLOG_IS_DBG_ENABLED()) { |
1381 | 0 | return; |
1382 | 0 | } |
1383 | | |
1384 | 0 | struct ofpbuf buffer = ofpbuf_const_initializer(message, size); |
1385 | 0 | char *nlmsg = nlmsg_to_string(&buffer, protocol); |
1386 | 0 | VLOG_DBG_RL(&rl, "%s (%s): %s", function, ovs_strerror(error), nlmsg); |
1387 | 0 | free(nlmsg); |
1388 | 0 | } |