Branch data Line data Source code
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/types.h>
23 : : #include <sys/uio.h>
24 : : #include <unistd.h>
25 : : #include "coverage.h"
26 : : #include "openvswitch/dynamic-string.h"
27 : : #include "hash.h"
28 : : #include "openvswitch/hmap.h"
29 : : #include "netlink.h"
30 : : #include "netlink-protocol.h"
31 : : #include "odp-netlink.h"
32 : : #include "openvswitch/ofpbuf.h"
33 : : #include "ovs-thread.h"
34 : : #include "poll-loop.h"
35 : : #include "seq.h"
36 : : #include "socket-util.h"
37 : : #include "util.h"
38 : : #include "openvswitch/vlog.h"
39 : :
40 : 20190 : VLOG_DEFINE_THIS_MODULE(netlink_socket);
41 : :
42 : 92814 : COVERAGE_DEFINE(netlink_overflow);
43 : 272346 : COVERAGE_DEFINE(netlink_received);
44 : 95694 : COVERAGE_DEFINE(netlink_recv_jumbo);
45 : 204624 : COVERAGE_DEFINE(netlink_sent);
46 : :
47 : : /* Linux header file confusion causes this to be undefined. */
48 : : #ifndef SOL_NETLINK
49 : : #define SOL_NETLINK 270
50 : : #endif
51 : :
52 : : /* A single (bad) Netlink message can in theory dump out many, many log
53 : : * messages, so the burst size is set quite high here to avoid missing useful
54 : : * information. Also, at high logging levels we log *all* Netlink messages. */
55 : : static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600);
56 : :
57 : : static uint32_t nl_sock_allocate_seq(struct nl_sock *, unsigned int n);
58 : : static void log_nlmsg(const char *function, int error,
59 : : const void *message, size_t size, int protocol);
60 : : #ifdef _WIN32
61 : : static int get_sock_pid_from_kernel(struct nl_sock *sock);
62 : : static int set_sock_property(struct nl_sock *sock);
63 : : #endif
64 : :
65 : : /* Netlink sockets. */
66 : :
67 : : struct nl_sock {
68 : : #ifdef _WIN32
69 : : HANDLE handle;
70 : : OVERLAPPED overlapped;
71 : : DWORD read_ioctl;
72 : : #else
73 : : int fd;
74 : : #endif
75 : : uint32_t next_seq;
76 : : uint32_t pid;
77 : : int protocol;
78 : : unsigned int rcvbuf; /* Receive buffer size (SO_RCVBUF). */
79 : : };
80 : :
81 : : /* Compile-time limit on iovecs, so that we can allocate a maximum-size array
82 : : * of iovecs on the stack. */
83 : : #define MAX_IOVS 128
84 : :
85 : : /* Maximum number of iovecs that may be passed to sendmsg, capped at a
86 : : * minimum of _XOPEN_IOV_MAX (16) and a maximum of MAX_IOVS.
87 : : *
88 : : * Initialized by nl_sock_create(). */
89 : : static int max_iovs;
90 : :
91 : : static int nl_pool_alloc(int protocol, struct nl_sock **sockp);
92 : : static void nl_pool_release(struct nl_sock *);
93 : :
94 : : /* Creates a new netlink socket for the given netlink 'protocol'
95 : : * (NETLINK_ROUTE, NETLINK_GENERIC, ...). Returns 0 and sets '*sockp' to the
96 : : * new socket if successful, otherwise returns a positive errno value. */
97 : : int
98 : 3975 : nl_sock_create(int protocol, struct nl_sock **sockp)
99 : : {
100 : : static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
101 : : struct nl_sock *sock;
102 : : #ifndef _WIN32
103 : : struct sockaddr_nl local, remote;
104 : : #endif
105 : : socklen_t local_size;
106 : : int rcvbuf;
107 : 3975 : int retval = 0;
108 : :
109 [ + + ]: 3975 : if (ovsthread_once_start(&once)) {
110 : 740 : int save_errno = errno;
111 : 740 : errno = 0;
112 : :
113 : 740 : max_iovs = sysconf(_SC_UIO_MAXIOV);
114 [ - + ]: 740 : if (max_iovs < _XOPEN_IOV_MAX) {
115 [ # # ][ # # ]: 0 : if (max_iovs == -1 && errno) {
116 [ # # ]: 0 : VLOG_WARN("sysconf(_SC_UIO_MAXIOV): %s", ovs_strerror(errno));
117 : : }
118 : 0 : max_iovs = _XOPEN_IOV_MAX;
119 [ + - ]: 740 : } else if (max_iovs > MAX_IOVS) {
120 : 740 : max_iovs = MAX_IOVS;
121 : : }
122 : :
123 : 740 : errno = save_errno;
124 : 740 : ovsthread_once_done(&once);
125 : : }
126 : :
127 : 3975 : *sockp = NULL;
128 : 3975 : sock = xmalloc(sizeof *sock);
129 : :
130 : : #ifdef _WIN32
131 : : sock->overlapped.hEvent = NULL;
132 : : sock->handle = CreateFile(OVS_DEVICE_NAME_USER,
133 : : GENERIC_READ | GENERIC_WRITE,
134 : : FILE_SHARE_READ | FILE_SHARE_WRITE,
135 : : NULL, OPEN_EXISTING,
136 : : FILE_FLAG_OVERLAPPED, NULL);
137 : :
138 : : if (sock->handle == INVALID_HANDLE_VALUE) {
139 : : VLOG_ERR("fcntl: %s", ovs_lasterror_to_string());
140 : : goto error;
141 : : }
142 : :
143 : : memset(&sock->overlapped, 0, sizeof sock->overlapped);
144 : : sock->overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
145 : : if (sock->overlapped.hEvent == NULL) {
146 : : VLOG_ERR("fcntl: %s", ovs_lasterror_to_string());
147 : : goto error;
148 : : }
149 : : /* Initialize the type/ioctl to Generic */
150 : : sock->read_ioctl = OVS_IOCTL_READ;
151 : : #else
152 : 3975 : sock->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
153 [ - + ]: 3975 : if (sock->fd < 0) {
154 [ # # ]: 0 : VLOG_ERR("fcntl: %s", ovs_strerror(errno));
155 : 0 : goto error;
156 : : }
157 : : #endif
158 : :
159 : 3975 : sock->protocol = protocol;
160 : 3975 : sock->next_seq = 1;
161 : :
162 : 3975 : rcvbuf = 1024 * 1024;
163 : : #ifdef _WIN32
164 : : sock->rcvbuf = rcvbuf;
165 : : retval = get_sock_pid_from_kernel(sock);
166 : : if (retval != 0) {
167 : : goto error;
168 : : }
169 : : retval = set_sock_property(sock);
170 : : if (retval != 0) {
171 : : goto error;
172 : : }
173 : : #else
174 [ + + ]: 3975 : if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUFFORCE,
175 : : &rcvbuf, sizeof rcvbuf)) {
176 : : /* Only root can use SO_RCVBUFFORCE. Everyone else gets EPERM.
177 : : * Warn only if the failure is therefore unexpected. */
178 [ - + ]: 2102 : if (errno != EPERM) {
179 [ # # ]: 0 : VLOG_WARN_RL(&rl, "setting %d-byte socket receive buffer failed "
180 : : "(%s)", rcvbuf, ovs_strerror(errno));
181 : : }
182 : : }
183 : :
184 : 3975 : retval = get_socket_rcvbuf(sock->fd);
185 [ - + ]: 3975 : if (retval < 0) {
186 : 0 : retval = -retval;
187 : 0 : goto error;
188 : : }
189 : 3975 : sock->rcvbuf = retval;
190 : 3975 : retval = 0;
191 : :
192 : : /* Connect to kernel (pid 0) as remote address. */
193 : 3975 : memset(&remote, 0, sizeof remote);
194 : 3975 : remote.nl_family = AF_NETLINK;
195 : 3975 : remote.nl_pid = 0;
196 [ - + ]: 3975 : if (connect(sock->fd, (struct sockaddr *) &remote, sizeof remote) < 0) {
197 [ # # ]: 0 : VLOG_ERR("connect(0): %s", ovs_strerror(errno));
198 : 0 : goto error;
199 : : }
200 : :
201 : : /* Obtain pid assigned by kernel. */
202 : 3975 : local_size = sizeof local;
203 [ - + ]: 3975 : if (getsockname(sock->fd, (struct sockaddr *) &local, &local_size) < 0) {
204 [ # # ]: 0 : VLOG_ERR("getsockname: %s", ovs_strerror(errno));
205 : 0 : goto error;
206 : : }
207 [ + - ][ - + ]: 3975 : if (local_size < sizeof local || local.nl_family != AF_NETLINK) {
208 [ # # ]: 0 : VLOG_ERR("getsockname returned bad Netlink name");
209 : 0 : retval = EINVAL;
210 : 0 : goto error;
211 : : }
212 : 3975 : sock->pid = local.nl_pid;
213 : : #endif
214 : :
215 : 3975 : *sockp = sock;
216 : 3975 : return 0;
217 : :
218 : : error:
219 [ # # ]: 0 : if (retval == 0) {
220 : 0 : retval = errno;
221 [ # # ]: 0 : if (retval == 0) {
222 : 0 : retval = EINVAL;
223 : : }
224 : : }
225 : : #ifdef _WIN32
226 : : if (sock->overlapped.hEvent) {
227 : : CloseHandle(sock->overlapped.hEvent);
228 : : }
229 : : if (sock->handle != INVALID_HANDLE_VALUE) {
230 : : CloseHandle(sock->handle);
231 : : }
232 : : #else
233 [ # # ]: 0 : if (sock->fd >= 0) {
234 : 0 : close(sock->fd);
235 : : }
236 : : #endif
237 : 0 : free(sock);
238 : 3975 : return retval;
239 : : }
240 : :
241 : : /* Creates a new netlink socket for the same protocol as 'src'. Returns 0 and
242 : : * sets '*sockp' to the new socket if successful, otherwise returns a positive
243 : : * errno value. */
244 : : int
245 : 0 : nl_sock_clone(const struct nl_sock *src, struct nl_sock **sockp)
246 : : {
247 : 0 : return nl_sock_create(src->protocol, sockp);
248 : : }
249 : :
250 : : /* Destroys netlink socket 'sock'. */
251 : : void
252 : 19110 : nl_sock_destroy(struct nl_sock *sock)
253 : : {
254 [ + + ]: 19110 : if (sock) {
255 : : #ifdef _WIN32
256 : : if (sock->overlapped.hEvent) {
257 : : CloseHandle(sock->overlapped.hEvent);
258 : : }
259 : : CloseHandle(sock->handle);
260 : : #else
261 : 1026 : close(sock->fd);
262 : : #endif
263 : 1026 : free(sock);
264 : : }
265 : 19110 : }
266 : :
267 : : #ifdef _WIN32
268 : : /* Reads the pid for 'sock' generated in the kernel datapath. The function
269 : : * uses a separate IOCTL instead of a transaction semantic to avoid unnecessary
270 : : * message overhead. */
271 : : static int
272 : : get_sock_pid_from_kernel(struct nl_sock *sock)
273 : : {
274 : : uint32_t pid = 0;
275 : : int retval = 0;
276 : : DWORD bytes = 0;
277 : :
278 : : if (!DeviceIoControl(sock->handle, OVS_IOCTL_GET_PID,
279 : : NULL, 0, &pid, sizeof(pid),
280 : : &bytes, NULL)) {
281 : : retval = EINVAL;
282 : : } else {
283 : : if (bytes < sizeof(pid)) {
284 : : retval = EINVAL;
285 : : } else {
286 : : sock->pid = pid;
287 : : }
288 : : }
289 : :
290 : : return retval;
291 : : }
292 : :
293 : : /* Used for setting and managing socket properties in userspace and kernel.
294 : : * Currently two attributes are tracked - pid and protocol
295 : : * protocol - supplied by userspace based on the netlink family. Windows uses
296 : : * this property to set the value in kernel datapath.
297 : : * eg: (NETLINK_GENERIC/ NETLINK_NETFILTER)
298 : : * pid - generated by windows kernel and set in userspace. The property
299 : : * is not modified.
300 : : * Also verify if Protocol and PID in Kernel reflects the values in userspace
301 : : * */
302 : : static int
303 : : set_sock_property(struct nl_sock *sock)
304 : : {
305 : : static const struct nl_policy ovs_socket_policy[] = {
306 : : [OVS_NL_ATTR_SOCK_PROTO] = { .type = NL_A_BE32, .optional = true },
307 : : [OVS_NL_ATTR_SOCK_PID] = { .type = NL_A_BE32, .optional = true }
308 : : };
309 : :
310 : : struct ofpbuf request, *reply;
311 : : struct ovs_header *ovs_header;
312 : : struct nlattr *attrs[ARRAY_SIZE(ovs_socket_policy)];
313 : : int retval = 0;
314 : : int error;
315 : :
316 : : ofpbuf_init(&request, 0);
317 : : nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0,
318 : : OVS_CTRL_CMD_SOCK_PROP, OVS_WIN_CONTROL_VERSION);
319 : : ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header);
320 : : ovs_header->dp_ifindex = 0;
321 : :
322 : : nl_msg_put_be32(&request, OVS_NL_ATTR_SOCK_PROTO, sock->protocol);
323 : : /* pid is already set as part of get_sock_pid_from_kernel()
324 : : * This is added to maintain consistency
325 : : */
326 : : nl_msg_put_be32(&request, OVS_NL_ATTR_SOCK_PID, sock->pid);
327 : :
328 : : error = nl_sock_transact(sock, &request, &reply);
329 : : ofpbuf_uninit(&request);
330 : : if (error) {
331 : : retval = EINVAL;
332 : : }
333 : :
334 : : if (!nl_policy_parse(reply,
335 : : NLMSG_HDRLEN + GENL_HDRLEN + sizeof *ovs_header,
336 : : ovs_socket_policy, attrs,
337 : : ARRAY_SIZE(ovs_socket_policy))) {
338 : : ofpbuf_delete(reply);
339 : : retval = EINVAL;
340 : : }
341 : : /* Verify if the properties are setup properly */
342 : : if (attrs[OVS_NL_ATTR_SOCK_PROTO]) {
343 : : int protocol = nl_attr_get_be32(attrs[OVS_NL_ATTR_SOCK_PROTO]);
344 : : if (protocol != sock->protocol) {
345 : : VLOG_ERR("Invalid protocol returned:%d expected:%d",
346 : : protocol, sock->protocol);
347 : : retval = EINVAL;
348 : : }
349 : : }
350 : :
351 : : if (attrs[OVS_NL_ATTR_SOCK_PID]) {
352 : : int pid = nl_attr_get_be32(attrs[OVS_NL_ATTR_SOCK_PID]);
353 : : if (pid != sock->pid) {
354 : : VLOG_ERR("Invalid pid returned:%d expected:%d",
355 : : pid, sock->pid);
356 : : retval = EINVAL;
357 : : }
358 : : }
359 : :
360 : : return retval;
361 : : }
362 : : #endif /* _WIN32 */
363 : :
364 : : #ifdef _WIN32
365 : : static int __inline
366 : : nl_sock_mcgroup(struct nl_sock *sock, unsigned int multicast_group, bool join)
367 : : {
368 : : struct ofpbuf request;
369 : : uint64_t request_stub[128];
370 : : struct ovs_header *ovs_header;
371 : : struct nlmsghdr *nlmsg;
372 : : int error;
373 : :
374 : : ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
375 : :
376 : : nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0,
377 : : OVS_CTRL_CMD_MC_SUBSCRIBE_REQ,
378 : : OVS_WIN_CONTROL_VERSION);
379 : :
380 : : ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header);
381 : : ovs_header->dp_ifindex = 0;
382 : :
383 : : nl_msg_put_u32(&request, OVS_NL_ATTR_MCAST_GRP, multicast_group);
384 : : nl_msg_put_u8(&request, OVS_NL_ATTR_MCAST_JOIN, join ? 1 : 0);
385 : :
386 : : error = nl_sock_send(sock, &request, true);
387 : : ofpbuf_uninit(&request);
388 : : return error;
389 : : }
390 : : #endif
391 : : /* Tries to add 'sock' as a listener for 'multicast_group'. Returns 0 if
392 : : * successful, otherwise a positive errno value.
393 : : *
394 : : * A socket that is subscribed to a multicast group that receives asynchronous
395 : : * notifications must not be used for Netlink transactions or dumps, because
396 : : * transactions and dumps can cause notifications to be lost.
397 : : *
398 : : * Multicast group numbers are always positive.
399 : : *
400 : : * It is not an error to attempt to join a multicast group to which a socket
401 : : * already belongs. */
402 : : int
403 : 5196 : nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
404 : : {
405 : : #ifdef _WIN32
406 : : /* Set the socket type as a "multicast" socket */
407 : : sock->read_ioctl = OVS_IOCTL_READ_EVENT;
408 : : int error = nl_sock_mcgroup(sock, multicast_group, true);
409 : : if (error) {
410 : : sock->read_ioctl = OVS_IOCTL_READ;
411 : : VLOG_WARN("could not join multicast group %u (%s)",
412 : : multicast_group, ovs_strerror(error));
413 : : return error;
414 : : }
415 : : #else
416 [ - + ]: 5196 : if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
417 : : &multicast_group, sizeof multicast_group) < 0) {
418 [ # # ]: 0 : VLOG_WARN("could not join multicast group %u (%s)",
419 : : multicast_group, ovs_strerror(errno));
420 : 0 : return errno;
421 : : }
422 : : #endif
423 : 5196 : return 0;
424 : : }
425 : :
426 : : #ifdef _WIN32
427 : : int
428 : : nl_sock_subscribe_packets(struct nl_sock *sock)
429 : : {
430 : : int error;
431 : :
432 : : if (sock->read_ioctl != OVS_IOCTL_READ) {
433 : : return EINVAL;
434 : : }
435 : :
436 : : error = nl_sock_subscribe_packet__(sock, true);
437 : : if (error) {
438 : : VLOG_WARN("could not subscribe packets (%s)",
439 : : ovs_strerror(error));
440 : : return error;
441 : : }
442 : : sock->read_ioctl = OVS_IOCTL_READ_PACKET;
443 : :
444 : : return 0;
445 : : }
446 : :
447 : : int
448 : : nl_sock_unsubscribe_packets(struct nl_sock *sock)
449 : : {
450 : : ovs_assert(sock->read_ioctl == OVS_IOCTL_READ_PACKET);
451 : :
452 : : int error = nl_sock_subscribe_packet__(sock, false);
453 : : if (error) {
454 : : VLOG_WARN("could not unsubscribe to packets (%s)",
455 : : ovs_strerror(error));
456 : : return error;
457 : : }
458 : :
459 : : sock->read_ioctl = OVS_IOCTL_READ;
460 : : return 0;
461 : : }
462 : :
463 : : int
464 : : nl_sock_subscribe_packet__(struct nl_sock *sock, bool subscribe)
465 : : {
466 : : struct ofpbuf request;
467 : : uint64_t request_stub[128];
468 : : struct ovs_header *ovs_header;
469 : : struct nlmsghdr *nlmsg;
470 : : int error;
471 : :
472 : : ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
473 : : nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0,
474 : : OVS_CTRL_CMD_PACKET_SUBSCRIBE_REQ,
475 : : OVS_WIN_CONTROL_VERSION);
476 : :
477 : : ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header);
478 : : ovs_header->dp_ifindex = 0;
479 : : nl_msg_put_u8(&request, OVS_NL_ATTR_PACKET_SUBSCRIBE, subscribe ? 1 : 0);
480 : : nl_msg_put_u32(&request, OVS_NL_ATTR_PACKET_PID, sock->pid);
481 : :
482 : : error = nl_sock_send(sock, &request, true);
483 : : ofpbuf_uninit(&request);
484 : : return error;
485 : : }
486 : : #endif
487 : :
488 : : /* Tries to make 'sock' stop listening to 'multicast_group'. Returns 0 if
489 : : * successful, otherwise a positive errno value.
490 : : *
491 : : * Multicast group numbers are always positive.
492 : : *
493 : : * It is not an error to attempt to leave a multicast group to which a socket
494 : : * does not belong.
495 : : *
496 : : * On success, reading from 'sock' will still return any messages that were
497 : : * received on 'multicast_group' before the group was left. */
498 : : int
499 : 2 : nl_sock_leave_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
500 : : {
501 : : #ifdef _WIN32
502 : : int error = nl_sock_mcgroup(sock, multicast_group, false);
503 : : if (error) {
504 : : VLOG_WARN("could not leave multicast group %u (%s)",
505 : : multicast_group, ovs_strerror(error));
506 : : return error;
507 : : }
508 : : sock->read_ioctl = OVS_IOCTL_READ;
509 : : #else
510 [ - + ]: 2 : if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
511 : : &multicast_group, sizeof multicast_group) < 0) {
512 [ # # ]: 0 : VLOG_WARN("could not leave multicast group %u (%s)",
513 : : multicast_group, ovs_strerror(errno));
514 : 0 : return errno;
515 : : }
516 : : #endif
517 : 2 : return 0;
518 : : }
519 : :
520 : : static int
521 : 5122 : nl_sock_send__(struct nl_sock *sock, const struct ofpbuf *msg,
522 : : uint32_t nlmsg_seq, bool wait)
523 : : {
524 : 5122 : struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg);
525 : : int error;
526 : :
527 : 5122 : nlmsg->nlmsg_len = msg->size;
528 : 5122 : nlmsg->nlmsg_seq = nlmsg_seq;
529 : 5122 : nlmsg->nlmsg_pid = sock->pid;
530 : : do {
531 : : int retval;
532 : : #ifdef _WIN32
533 : : DWORD bytes;
534 : :
535 : : if (!DeviceIoControl(sock->handle, OVS_IOCTL_WRITE,
536 : : msg->data, msg->size, NULL, 0,
537 : : &bytes, NULL)) {
538 : : retval = -1;
539 : : /* XXX: Map to a more appropriate error based on GetLastError(). */
540 : : errno = EINVAL;
541 : : VLOG_DBG_RL(&rl, "fatal driver failure in write: %s",
542 : : ovs_lasterror_to_string());
543 : : } else {
544 : : retval = msg->size;
545 : : }
546 : : #else
547 [ + - ]: 5122 : retval = send(sock->fd, msg->data, msg->size,
548 : : wait ? 0 : MSG_DONTWAIT);
549 : : #endif
550 [ - + ]: 5122 : error = retval < 0 ? errno : 0;
551 [ - + ]: 5122 : } while (error == EINTR);
552 : 5122 : log_nlmsg(__func__, error, msg->data, msg->size, sock->protocol);
553 [ + - ]: 5122 : if (!error) {
554 : 5122 : COVERAGE_INC(netlink_sent);
555 : : }
556 : 5122 : return error;
557 : : }
558 : :
559 : : /* Tries to send 'msg', which must contain a Netlink message, to the kernel on
560 : : * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid
561 : : * will be set to 'sock''s pid, and nlmsg_seq will be initialized to a fresh
562 : : * sequence number, before the message is sent.
563 : : *
564 : : * Returns 0 if successful, otherwise a positive errno value. If
565 : : * 'wait' is true, then the send will wait until buffer space is ready;
566 : : * otherwise, returns EAGAIN if the 'sock' send buffer is full. */
567 : : int
568 : 0 : nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
569 : : {
570 : 0 : return nl_sock_send_seq(sock, msg, nl_sock_allocate_seq(sock, 1), wait);
571 : : }
572 : :
573 : : /* Tries to send 'msg', which must contain a Netlink message, to the kernel on
574 : : * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, nlmsg_pid
575 : : * will be set to 'sock''s pid, and nlmsg_seq will be initialized to
576 : : * 'nlmsg_seq', before the message is sent.
577 : : *
578 : : * Returns 0 if successful, otherwise a positive errno value. If
579 : : * 'wait' is true, then the send will wait until buffer space is ready;
580 : : * otherwise, returns EAGAIN if the 'sock' send buffer is full.
581 : : *
582 : : * This function is suitable for sending a reply to a request that was received
583 : : * with sequence number 'nlmsg_seq'. Otherwise, use nl_sock_send() instead. */
584 : : int
585 : 0 : nl_sock_send_seq(struct nl_sock *sock, const struct ofpbuf *msg,
586 : : uint32_t nlmsg_seq, bool wait)
587 : : {
588 : 0 : return nl_sock_send__(sock, msg, nlmsg_seq, wait);
589 : : }
590 : :
591 : : static int
592 : 529775 : nl_sock_recv__(struct nl_sock *sock, struct ofpbuf *buf, bool wait)
593 : : {
594 : : /* We can't accurately predict the size of the data to be received. The
595 : : * caller is supposed to have allocated enough space in 'buf' to handle the
596 : : * "typical" case. To handle exceptions, we make available enough space in
597 : : * 'tail' to allow Netlink messages to be up to 64 kB long (a reasonable
598 : : * figure since that's the maximum length of a Netlink attribute). */
599 : : struct nlmsghdr *nlmsghdr;
600 : : uint8_t tail[65536];
601 : : struct iovec iov[2];
602 : : struct msghdr msg;
603 : : ssize_t retval;
604 : : int error;
605 : :
606 [ - + ]: 529775 : ovs_assert(buf->allocated >= sizeof *nlmsghdr);
607 : 529775 : ofpbuf_clear(buf);
608 : :
609 : 529775 : iov[0].iov_base = buf->base;
610 : 529775 : iov[0].iov_len = buf->allocated;
611 : 529775 : iov[1].iov_base = tail;
612 : 529775 : iov[1].iov_len = sizeof tail;
613 : :
614 : 529775 : memset(&msg, 0, sizeof msg);
615 : 529775 : msg.msg_iov = iov;
616 : 529775 : msg.msg_iovlen = 2;
617 : :
618 : : /* Receive a Netlink message from the kernel.
619 : : *
620 : : * This works around a kernel bug in which the kernel returns an error code
621 : : * as if it were the number of bytes read. It doesn't actually modify
622 : : * anything in the receive buffer in that case, so we can initialize the
623 : : * Netlink header with an impossible message length and then, upon success,
624 : : * check whether it changed. */
625 : 529775 : nlmsghdr = buf->base;
626 : : do {
627 : 529775 : nlmsghdr->nlmsg_len = UINT32_MAX;
628 : : #ifdef _WIN32
629 : : DWORD bytes;
630 : : if (!DeviceIoControl(sock->handle, sock->read_ioctl,
631 : : NULL, 0, tail, sizeof tail, &bytes, NULL)) {
632 : : VLOG_DBG_RL(&rl, "fatal driver failure in transact: %s",
633 : : ovs_lasterror_to_string());
634 : : retval = -1;
635 : : /* XXX: Map to a more appropriate error. */
636 : : errno = EINVAL;
637 : : } else {
638 : : retval = bytes;
639 : : if (retval == 0) {
640 : : retval = -1;
641 : : errno = EAGAIN;
642 : : } else {
643 : : if (retval >= buf->allocated) {
644 : : ofpbuf_reinit(buf, retval);
645 : : nlmsghdr = buf->base;
646 : : nlmsghdr->nlmsg_len = UINT32_MAX;
647 : : }
648 : : memcpy(buf->data, tail, retval);
649 : : buf->size = retval;
650 : : }
651 : : }
652 : : #else
653 [ - + ]: 529775 : retval = recvmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT);
654 : : #endif
655 [ + + ][ + - ]: 529777 : error = (retval < 0 ? errno
[ - + ]
656 : : : retval == 0 ? ECONNRESET /* not possible? */
657 : 29923 : : nlmsghdr->nlmsg_len != UINT32_MAX ? 0
658 : : : retval);
659 [ - + ]: 529777 : } while (error == EINTR);
660 [ + + ]: 529777 : if (error) {
661 [ - + ]: 499854 : if (error == ENOBUFS) {
662 : : /* Socket receive buffer overflow dropped one or more messages that
663 : : * the kernel tried to send to us. */
664 : 0 : COVERAGE_INC(netlink_overflow);
665 : : }
666 : 499853 : return error;
667 : : }
668 : :
669 [ - + ]: 29923 : if (msg.msg_flags & MSG_TRUNC) {
670 [ # # ]: 0 : VLOG_ERR_RL(&rl, "truncated message (longer than %"PRIuSIZE" bytes)",
671 : : sizeof tail);
672 : 0 : return E2BIG;
673 : : }
674 : :
675 [ + - ]: 29923 : if (retval < sizeof *nlmsghdr
676 [ + - ]: 29923 : || nlmsghdr->nlmsg_len < sizeof *nlmsghdr
677 [ - + ]: 29923 : || nlmsghdr->nlmsg_len > retval) {
678 [ # # ]: 0 : VLOG_ERR_RL(&rl, "received invalid nlmsg (%"PRIuSIZE" bytes < %"PRIuSIZE")",
679 : : retval, sizeof *nlmsghdr);
680 : 0 : return EPROTO;
681 : : }
682 : : #ifndef _WIN32
683 : 29923 : buf->size = MIN(retval, buf->allocated);
684 [ + + ]: 29923 : if (retval > buf->allocated) {
685 : 480 : COVERAGE_INC(netlink_recv_jumbo);
686 : 480 : ofpbuf_put(buf, tail, retval - buf->allocated);
687 : : }
688 : : #endif
689 : :
690 : 29923 : log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol);
691 : 29923 : COVERAGE_INC(netlink_received);
692 : :
693 : 529776 : return 0;
694 : : }
695 : :
696 : : /* Tries to receive a Netlink message from the kernel on 'sock' into 'buf'. If
697 : : * 'wait' is true, waits for a message to be ready. Otherwise, fails with
698 : : * EAGAIN if the 'sock' receive buffer is empty.
699 : : *
700 : : * The caller must have initialized 'buf' with an allocation of at least
701 : : * NLMSG_HDRLEN bytes. For best performance, the caller should allocate enough
702 : : * space for a "typical" message.
703 : : *
704 : : * On success, returns 0 and replaces 'buf''s previous content by the received
705 : : * message. This function expands 'buf''s allocated memory, as necessary, to
706 : : * hold the actual size of the received message.
707 : : *
708 : : * On failure, returns a positive errno value and clears 'buf' to zero length.
709 : : * 'buf' retains its previous memory allocation.
710 : : *
711 : : * Regardless of success or failure, this function resets 'buf''s headroom to
712 : : * 0. */
713 : : int
714 : 504211 : nl_sock_recv(struct nl_sock *sock, struct ofpbuf *buf, bool wait)
715 : : {
716 : 504211 : return nl_sock_recv__(sock, buf, wait);
717 : : }
718 : :
719 : : static void
720 : 13630 : nl_sock_record_errors__(struct nl_transaction **transactions, size_t n,
721 : : int error)
722 : : {
723 : : size_t i;
724 : :
725 [ + + ]: 17549 : for (i = 0; i < n; i++) {
726 : 3919 : struct nl_transaction *txn = transactions[i];
727 : :
728 : 3919 : txn->error = error;
729 [ + + ]: 3919 : if (txn->reply) {
730 : 111 : ofpbuf_clear(txn->reply);
731 : : }
732 : : }
733 : 13630 : }
734 : :
735 : : static int
736 : 13513 : nl_sock_transact_multiple__(struct nl_sock *sock,
737 : : struct nl_transaction **transactions, size_t n,
738 : : size_t *done)
739 : : {
740 : : uint64_t tmp_reply_stub[1024 / 8];
741 : : struct nl_transaction tmp_txn;
742 : : struct ofpbuf tmp_reply;
743 : :
744 : : uint32_t base_seq;
745 : : struct iovec iovs[MAX_IOVS];
746 : : struct msghdr msg;
747 : : int error;
748 : : int i;
749 : :
750 : 13513 : base_seq = nl_sock_allocate_seq(sock, n);
751 : 13513 : *done = 0;
752 [ + + ]: 28220 : for (i = 0; i < n; i++) {
753 : 14707 : struct nl_transaction *txn = transactions[i];
754 : 14707 : struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(txn->request);
755 : :
756 : 14707 : nlmsg->nlmsg_len = txn->request->size;
757 : 14707 : nlmsg->nlmsg_seq = base_seq + i;
758 : 14707 : nlmsg->nlmsg_pid = sock->pid;
759 : :
760 : 14707 : iovs[i].iov_base = txn->request->data;
761 : 14707 : iovs[i].iov_len = txn->request->size;
762 : : }
763 : :
764 : : #ifndef _WIN32
765 : 13513 : memset(&msg, 0, sizeof msg);
766 : 13513 : msg.msg_iov = iovs;
767 : 13513 : msg.msg_iovlen = n;
768 : : do {
769 [ - + ]: 13513 : error = sendmsg(sock->fd, &msg, 0) < 0 ? errno : 0;
770 [ - + ]: 13513 : } while (error == EINTR);
771 : :
772 [ + + ]: 28220 : for (i = 0; i < n; i++) {
773 : 14707 : struct nl_transaction *txn = transactions[i];
774 : :
775 : 14707 : log_nlmsg(__func__, error, txn->request->data,
776 : 14707 : txn->request->size, sock->protocol);
777 : : }
778 [ + - ]: 13513 : if (!error) {
779 : 13513 : COVERAGE_ADD(netlink_sent, n);
780 : : }
781 : :
782 [ - + ]: 13513 : if (error) {
783 : 0 : return error;
784 : : }
785 : :
786 : 13513 : ofpbuf_use_stub(&tmp_reply, tmp_reply_stub, sizeof tmp_reply_stub);
787 : 13513 : tmp_txn.request = NULL;
788 : 13513 : tmp_txn.reply = &tmp_reply;
789 : 13513 : tmp_txn.error = 0;
790 [ + + ]: 24301 : while (n > 0) {
791 : : struct nl_transaction *buf_txn, *txn;
792 : : uint32_t seq;
793 : :
794 : : /* Find a transaction whose buffer we can use for receiving a reply.
795 : : * If no such transaction is left, use tmp_txn. */
796 : 13630 : buf_txn = &tmp_txn;
797 [ + + ]: 19134 : for (i = 0; i < n; i++) {
798 [ + + ]: 14735 : if (transactions[i]->reply) {
799 : 9231 : buf_txn = transactions[i];
800 : 9231 : break;
801 : : }
802 : : }
803 : :
804 : : /* Receive a reply. */
805 : 13630 : error = nl_sock_recv__(sock, buf_txn->reply, false);
806 [ + + ]: 13630 : if (error) {
807 [ + - ]: 2842 : if (error == EAGAIN) {
808 : 2842 : nl_sock_record_errors__(transactions, n, 0);
809 : 2842 : *done += n;
810 : 2842 : error = 0;
811 : : }
812 : 2842 : break;
813 : : }
814 : :
815 : : /* Match the reply up with a transaction. */
816 : 10788 : seq = nl_msg_nlmsghdr(buf_txn->reply)->nlmsg_seq;
817 [ + - ][ - + ]: 10788 : if (seq < base_seq || seq >= base_seq + n) {
818 [ # # ]: 0 : VLOG_DBG_RL(&rl, "ignoring unexpected seq %#"PRIx32, seq);
819 : 0 : continue;
820 : : }
821 : 10788 : i = seq - base_seq;
822 : 10788 : txn = transactions[i];
823 : :
824 : : /* Fill in the results for 'txn'. */
825 [ + + ]: 10788 : if (nl_msg_nlmsgerr(buf_txn->reply, &txn->error)) {
826 [ + + ]: 1523 : if (txn->reply) {
827 : 277 : ofpbuf_clear(txn->reply);
828 : : }
829 [ + - ]: 1523 : if (txn->error) {
830 [ - + ]: 1523 : VLOG_DBG_RL(&rl, "received NAK error=%d (%s)",
831 : : error, ovs_strerror(txn->error));
832 : : }
833 : : } else {
834 : 9265 : txn->error = 0;
835 [ + + ][ - + ]: 9265 : if (txn->reply && txn != buf_txn) {
836 : : /* Swap buffers. */
837 : 0 : struct ofpbuf *reply = buf_txn->reply;
838 : 0 : buf_txn->reply = txn->reply;
839 : 0 : txn->reply = reply;
840 : : }
841 : : }
842 : :
843 : : /* Fill in the results for transactions before 'txn'. (We have to do
844 : : * this after the results for 'txn' itself because of the buffer swap
845 : : * above.) */
846 : 10788 : nl_sock_record_errors__(transactions, i, 0);
847 : :
848 : : /* Advance. */
849 : 10788 : *done += i + 1;
850 : 10788 : transactions += i + 1;
851 : 10788 : n -= i + 1;
852 : 10788 : base_seq += i + 1;
853 : : }
854 : 13513 : ofpbuf_uninit(&tmp_reply);
855 : : #else
856 : : error = 0;
857 : : uint8_t reply_buf[65536];
858 : : for (i = 0; i < n; i++) {
859 : : DWORD reply_len;
860 : : bool ret;
861 : : struct nl_transaction *txn = transactions[i];
862 : : struct nlmsghdr *request_nlmsg, *reply_nlmsg;
863 : :
864 : : ret = DeviceIoControl(sock->handle, OVS_IOCTL_TRANSACT,
865 : : txn->request->data,
866 : : txn->request->size,
867 : : reply_buf, sizeof reply_buf,
868 : : &reply_len, NULL);
869 : :
870 : : if (ret && reply_len == 0) {
871 : : /*
872 : : * The current transaction did not produce any data to read and that
873 : : * is not an error as such. Continue with the remainder of the
874 : : * transactions.
875 : : */
876 : : txn->error = 0;
877 : : if (txn->reply) {
878 : : ofpbuf_clear(txn->reply);
879 : : }
880 : : } else if (!ret) {
881 : : /* XXX: Map to a more appropriate error. */
882 : : error = EINVAL;
883 : : VLOG_DBG_RL(&rl, "fatal driver failure: %s",
884 : : ovs_lasterror_to_string());
885 : : break;
886 : : }
887 : :
888 : : if (reply_len != 0) {
889 : : if (reply_len < sizeof *reply_nlmsg) {
890 : : nl_sock_record_errors__(transactions, n, 0);
891 : : VLOG_DBG_RL(&rl, "insufficient length of reply %#"PRIu32
892 : : " for seq: %#"PRIx32, reply_len, request_nlmsg->nlmsg_seq);
893 : : break;
894 : : }
895 : :
896 : : /* Validate the sequence number in the reply. */
897 : : request_nlmsg = nl_msg_nlmsghdr(txn->request);
898 : : reply_nlmsg = (struct nlmsghdr *)reply_buf;
899 : :
900 : : if (request_nlmsg->nlmsg_seq != reply_nlmsg->nlmsg_seq) {
901 : : ovs_assert(request_nlmsg->nlmsg_seq == reply_nlmsg->nlmsg_seq);
902 : : VLOG_DBG_RL(&rl, "mismatched seq request %#"PRIx32
903 : : ", reply %#"PRIx32, request_nlmsg->nlmsg_seq,
904 : : reply_nlmsg->nlmsg_seq);
905 : : break;
906 : : }
907 : :
908 : : /* Handle errors embedded within the netlink message. */
909 : : ofpbuf_use_stub(&tmp_reply, reply_buf, sizeof reply_buf);
910 : : tmp_reply.size = sizeof reply_buf;
911 : : if (nl_msg_nlmsgerr(&tmp_reply, &txn->error)) {
912 : : if (txn->reply) {
913 : : ofpbuf_clear(txn->reply);
914 : : }
915 : : if (txn->error) {
916 : : VLOG_DBG_RL(&rl, "received NAK error=%d (%s)",
917 : : error, ovs_strerror(txn->error));
918 : : }
919 : : } else {
920 : : txn->error = 0;
921 : : if (txn->reply) {
922 : : /* Copy the reply to the buffer specified by the caller. */
923 : : if (reply_len > txn->reply->allocated) {
924 : : ofpbuf_reinit(txn->reply, reply_len);
925 : : }
926 : : memcpy(txn->reply->data, reply_buf, reply_len);
927 : : txn->reply->size = reply_len;
928 : : }
929 : : }
930 : : ofpbuf_uninit(&tmp_reply);
931 : : }
932 : :
933 : : /* Count the number of successful transactions. */
934 : : (*done)++;
935 : :
936 : : }
937 : :
938 : : if (!error) {
939 : : COVERAGE_ADD(netlink_sent, n);
940 : : }
941 : : #endif
942 : :
943 : 13513 : return error;
944 : : }
945 : :
946 : : static void
947 : 13505 : nl_sock_transact_multiple(struct nl_sock *sock,
948 : : struct nl_transaction **transactions, size_t n)
949 : : {
950 : : int max_batch_count;
951 : : int error;
952 : :
953 [ - + ]: 13505 : if (!n) {
954 : 0 : return;
955 : : }
956 : :
957 : : /* In theory, every request could have a 64 kB reply. But the default and
958 : : * maximum socket rcvbuf size with typical Dom0 memory sizes both tend to
959 : : * be a bit below 128 kB, so that would only allow a single message in a
960 : : * "batch". So we assume that replies average (at most) 4 kB, which allows
961 : : * a good deal of batching.
962 : : *
963 : : * In practice, most of the requests that we batch either have no reply at
964 : : * all or a brief reply. */
965 [ + - ]: 13505 : max_batch_count = MAX(sock->rcvbuf / 4096, 1);
966 : 13505 : max_batch_count = MIN(max_batch_count, max_iovs);
967 : :
968 [ + + ]: 27018 : while (n > 0) {
969 : : size_t count, bytes;
970 : : size_t done;
971 : :
972 : : /* Batch up to 'max_batch_count' transactions. But cap it at about a
973 : : * page of requests total because big skbuffs are expensive to
974 : : * allocate in the kernel. */
975 : : #if defined(PAGESIZE)
976 : : enum { MAX_BATCH_BYTES = MAX(1, PAGESIZE - 512) };
977 : : #else
978 : : enum { MAX_BATCH_BYTES = 4096 - 512 };
979 : : #endif
980 : 13513 : bytes = transactions[0]->request->size;
981 [ + + ][ + - ]: 14707 : for (count = 1; count < n && count < max_batch_count; count++) {
982 [ + + ]: 1202 : if (bytes + transactions[count]->request->size > MAX_BATCH_BYTES) {
983 : 8 : break;
984 : : }
985 : 1194 : bytes += transactions[count]->request->size;
986 : : }
987 : :
988 : 13513 : error = nl_sock_transact_multiple__(sock, transactions, count, &done);
989 : 13513 : transactions += done;
990 : 13513 : n -= done;
991 : :
992 [ - + ]: 13513 : if (error == ENOBUFS) {
993 [ # # ]: 0 : VLOG_DBG_RL(&rl, "receive buffer overflow, resending request");
994 [ - + ]: 13513 : } else if (error) {
995 [ # # ]: 0 : VLOG_ERR_RL(&rl, "transaction error (%s)", ovs_strerror(error));
996 : 0 : nl_sock_record_errors__(transactions, n, error);
997 [ # # ]: 0 : if (error != EAGAIN) {
998 : : /* A fatal error has occurred. Abort the rest of
999 : : * transactions. */
1000 : 13513 : break;
1001 : : }
1002 : : }
1003 : : }
1004 : : }
1005 : :
1006 : : static int
1007 : 9119 : nl_sock_transact(struct nl_sock *sock, const struct ofpbuf *request,
1008 : : struct ofpbuf **replyp)
1009 : : {
1010 : : struct nl_transaction *transactionp;
1011 : : struct nl_transaction transaction;
1012 : :
1013 : 9119 : transaction.request = CONST_CAST(struct ofpbuf *, request);
1014 [ + + ]: 9119 : transaction.reply = replyp ? ofpbuf_new(1024) : NULL;
1015 : 9119 : transactionp = &transaction;
1016 : :
1017 : 9119 : nl_sock_transact_multiple(sock, &transactionp, 1);
1018 : :
1019 [ + + ]: 9119 : if (replyp) {
1020 [ + + ]: 7345 : if (transaction.error) {
1021 : 277 : ofpbuf_delete(transaction.reply);
1022 : 277 : *replyp = NULL;
1023 : : } else {
1024 : 7068 : *replyp = transaction.reply;
1025 : : }
1026 : : }
1027 : :
1028 : 9119 : return transaction.error;
1029 : : }
1030 : :
1031 : : /* Drain all the messages currently in 'sock''s receive queue. */
1032 : : int
1033 : 0 : nl_sock_drain(struct nl_sock *sock)
1034 : : {
1035 : : #ifdef _WIN32
1036 : : return 0;
1037 : : #else
1038 : 0 : return drain_rcvbuf(sock->fd);
1039 : : #endif
1040 : : }
1041 : :
1042 : : /* Starts a Netlink "dump" operation, by sending 'request' to the kernel on a
1043 : : * Netlink socket created with the given 'protocol', and initializes 'dump' to
1044 : : * reflect the state of the operation.
1045 : : *
1046 : : * 'request' must contain a Netlink message. Before sending the message,
1047 : : * nlmsg_len will be finalized to match request->size, and nlmsg_pid will be
1048 : : * set to the Netlink socket's pid. NLM_F_DUMP and NLM_F_ACK will be set in
1049 : : * nlmsg_flags.
1050 : : *
1051 : : * The design of this Netlink socket library ensures that the dump is reliable.
1052 : : *
1053 : : * This function provides no status indication. nl_dump_done() provides an
1054 : : * error status for the entire dump operation.
1055 : : *
1056 : : * The caller must eventually destroy 'request'.
1057 : : */
1058 : : void
1059 : 5122 : nl_dump_start(struct nl_dump *dump, int protocol, const struct ofpbuf *request)
1060 : : {
1061 : 5122 : nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK;
1062 : :
1063 : 5122 : ovs_mutex_init(&dump->mutex);
1064 : 5122 : ovs_mutex_lock(&dump->mutex);
1065 : 5122 : dump->status = nl_pool_alloc(protocol, &dump->sock);
1066 [ + - ]: 5122 : if (!dump->status) {
1067 : 5122 : dump->status = nl_sock_send__(dump->sock, request,
1068 : : nl_sock_allocate_seq(dump->sock, 1),
1069 : : true);
1070 : : }
1071 : 5122 : dump->nl_seq = nl_msg_nlmsghdr(request)->nlmsg_seq;
1072 : 5122 : ovs_mutex_unlock(&dump->mutex);
1073 : 5122 : }
1074 : :
1075 : : static int
1076 : 11936 : nl_dump_refill(struct nl_dump *dump, struct ofpbuf *buffer)
1077 : : OVS_REQUIRES(dump->mutex)
1078 : : {
1079 : : struct nlmsghdr *nlmsghdr;
1080 : : int error;
1081 : :
1082 [ + + ]: 23872 : while (!buffer->size) {
1083 : 11936 : error = nl_sock_recv__(dump->sock, buffer, false);
1084 [ - + ]: 11936 : if (error) {
1085 : : /* The kernel never blocks providing the results of a dump, so
1086 : : * error == EAGAIN means that we've read the whole thing, and
1087 : : * therefore transform it into EOF. (The kernel always provides
1088 : : * NLMSG_DONE as a sentinel. Some other thread must have received
1089 : : * that already but not yet signaled it in 'status'.)
1090 : : *
1091 : : * Any other error is just an error. */
1092 [ # # ]: 0 : return error == EAGAIN ? EOF : error;
1093 : : }
1094 : :
1095 : 11936 : nlmsghdr = nl_msg_nlmsghdr(buffer);
1096 [ - + ]: 11936 : if (dump->nl_seq != nlmsghdr->nlmsg_seq) {
1097 [ # # ]: 0 : VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32,
1098 : : nlmsghdr->nlmsg_seq, dump->nl_seq);
1099 : 0 : ofpbuf_clear(buffer);
1100 : : }
1101 : : }
1102 : :
1103 [ - + ][ # # ]: 11936 : if (nl_msg_nlmsgerr(buffer, &error) && error) {
1104 [ # # ]: 0 : VLOG_INFO_RL(&rl, "netlink dump request error (%s)",
1105 : : ovs_strerror(error));
1106 : 0 : ofpbuf_clear(buffer);
1107 : 0 : return error;
1108 : : }
1109 : :
1110 : 11936 : return 0;
1111 : : }
1112 : :
1113 : : static int
1114 : 79864 : nl_dump_next__(struct ofpbuf *reply, struct ofpbuf *buffer)
1115 : : {
1116 : 79864 : struct nlmsghdr *nlmsghdr = nl_msg_next(buffer, reply);
1117 [ - + ]: 79864 : if (!nlmsghdr) {
1118 [ # # ]: 0 : VLOG_WARN_RL(&rl, "netlink dump contains message fragment");
1119 : 0 : return EPROTO;
1120 [ + + ]: 79864 : } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) {
1121 : 5122 : return EOF;
1122 : : } else {
1123 : 74742 : return 0;
1124 : : }
1125 : : }
1126 : :
1127 : : /* Attempts to retrieve another reply from 'dump' into 'buffer'. 'dump' must
1128 : : * have been initialized with nl_dump_start(), and 'buffer' must have been
1129 : : * initialized. 'buffer' should be at least NL_DUMP_BUFSIZE bytes long.
1130 : : *
1131 : : * If successful, returns true and points 'reply->data' and
1132 : : * 'reply->size' to the message that was retrieved. The caller must not
1133 : : * modify 'reply' (because it points within 'buffer', which will be used by
1134 : : * future calls to this function).
1135 : : *
1136 : : * On failure, returns false and sets 'reply->data' to NULL and
1137 : : * 'reply->size' to 0. Failure might indicate an actual error or merely
1138 : : * the end of replies. An error status for the entire dump operation is
1139 : : * provided when it is completed by calling nl_dump_done().
1140 : : *
1141 : : * Multiple threads may call this function, passing the same nl_dump, however
1142 : : * each must provide independent buffers. This function may cache multiple
1143 : : * replies in the buffer, and these will be processed before more replies are
1144 : : * fetched. When this function returns false, other threads may continue to
1145 : : * process replies in their buffers, but they will not fetch more replies.
1146 : : */
1147 : : bool
1148 : 79864 : nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply, struct ofpbuf *buffer)
1149 : : {
1150 : 79864 : int retval = 0;
1151 : :
1152 : : /* If the buffer is empty, refill it.
1153 : : *
1154 : : * If the buffer is not empty, we don't check the dump's status.
1155 : : * Otherwise, we could end up skipping some of the dump results if thread A
1156 : : * hits EOF while thread B is in the midst of processing a batch. */
1157 [ + + ]: 79864 : if (!buffer->size) {
1158 : 11936 : ovs_mutex_lock(&dump->mutex);
1159 [ + - ]: 11936 : if (!dump->status) {
1160 : : /* Take the mutex here to avoid an in-kernel race. If two threads
1161 : : * try to read from a Netlink dump socket at once, then the socket
1162 : : * error can be set to EINVAL, which will be encountered on the
1163 : : * next recv on that socket, which could be anywhere due to the way
1164 : : * that we pool Netlink sockets. Serializing the recv calls avoids
1165 : : * the issue. */
1166 : 11936 : dump->status = nl_dump_refill(dump, buffer);
1167 : : }
1168 : 11936 : retval = dump->status;
1169 : 11936 : ovs_mutex_unlock(&dump->mutex);
1170 : : }
1171 : :
1172 : : /* Fetch the next message from the buffer. */
1173 [ + - ]: 79864 : if (!retval) {
1174 : 79864 : retval = nl_dump_next__(reply, buffer);
1175 [ + + ]: 79864 : if (retval) {
1176 : : /* Record 'retval' as the dump status, but don't overwrite an error
1177 : : * with EOF. */
1178 : 5122 : ovs_mutex_lock(&dump->mutex);
1179 [ + - ]: 5122 : if (dump->status <= 0) {
1180 : 5122 : dump->status = retval;
1181 : : }
1182 : 5122 : ovs_mutex_unlock(&dump->mutex);
1183 : : }
1184 : : }
1185 : :
1186 [ + + ]: 79864 : if (retval) {
1187 : 5122 : reply->data = NULL;
1188 : 5122 : reply->size = 0;
1189 : : }
1190 : 79864 : return !retval;
1191 : : }
1192 : :
1193 : : /* Completes Netlink dump operation 'dump', which must have been initialized
1194 : : * with nl_dump_start(). Returns 0 if the dump operation was error-free,
1195 : : * otherwise a positive errno value describing the problem. */
1196 : : int
1197 : 5122 : nl_dump_done(struct nl_dump *dump)
1198 : : {
1199 : : int status;
1200 : :
1201 : 5122 : ovs_mutex_lock(&dump->mutex);
1202 : 5122 : status = dump->status;
1203 : 5122 : ovs_mutex_unlock(&dump->mutex);
1204 : :
1205 : : /* Drain any remaining messages that the client didn't read. Otherwise the
1206 : : * kernel will continue to queue them up and waste buffer space.
1207 : : *
1208 : : * XXX We could just destroy and discard the socket in this case. */
1209 [ - + ]: 5122 : if (!status) {
1210 : : uint64_t tmp_reply_stub[NL_DUMP_BUFSIZE / 8];
1211 : : struct ofpbuf reply, buf;
1212 : :
1213 : 0 : ofpbuf_use_stub(&buf, tmp_reply_stub, sizeof tmp_reply_stub);
1214 [ # # ]: 0 : while (nl_dump_next(dump, &reply, &buf)) {
1215 : : /* Nothing to do. */
1216 : : }
1217 : 0 : ofpbuf_uninit(&buf);
1218 : :
1219 : 0 : ovs_mutex_lock(&dump->mutex);
1220 : 0 : status = dump->status;
1221 : 0 : ovs_mutex_unlock(&dump->mutex);
1222 [ # # ]: 0 : ovs_assert(status);
1223 : : }
1224 : :
1225 : 5122 : nl_pool_release(dump->sock);
1226 : 5122 : ovs_mutex_destroy(&dump->mutex);
1227 : :
1228 [ - + ]: 5122 : return status == EOF ? 0 : status;
1229 : : }
1230 : :
1231 : : #ifdef _WIN32
1232 : : /* Pend an I/O request in the driver. The driver completes the I/O whenever
1233 : : * an event or a packet is ready to be read. Once the I/O is completed
1234 : : * the overlapped structure event associated with the pending I/O will be set
1235 : : */
1236 : : static int
1237 : : pend_io_request(struct nl_sock *sock)
1238 : : {
1239 : : struct ofpbuf request;
1240 : : uint64_t request_stub[128];
1241 : : struct ovs_header *ovs_header;
1242 : : struct nlmsghdr *nlmsg;
1243 : : uint32_t seq;
1244 : : int retval = 0;
1245 : : int error;
1246 : : DWORD bytes;
1247 : : OVERLAPPED *overlapped = CONST_CAST(OVERLAPPED *, &sock->overlapped);
1248 : : uint16_t cmd = OVS_CTRL_CMD_WIN_PEND_PACKET_REQ;
1249 : :
1250 : : ovs_assert(sock->read_ioctl == OVS_IOCTL_READ_PACKET ||
1251 : : sock->read_ioctl == OVS_IOCTL_READ_EVENT);
1252 : : if (sock->read_ioctl == OVS_IOCTL_READ_EVENT) {
1253 : : cmd = OVS_CTRL_CMD_WIN_PEND_REQ;
1254 : : }
1255 : :
1256 : : int ovs_msg_size = sizeof (struct nlmsghdr) + sizeof (struct genlmsghdr) +
1257 : : sizeof (struct ovs_header);
1258 : :
1259 : : ofpbuf_use_stub(&request, request_stub, sizeof request_stub);
1260 : :
1261 : : seq = nl_sock_allocate_seq(sock, 1);
1262 : : nl_msg_put_genlmsghdr(&request, 0, OVS_WIN_NL_CTRL_FAMILY_ID, 0,
1263 : : cmd, OVS_WIN_CONTROL_VERSION);
1264 : : nlmsg = nl_msg_nlmsghdr(&request);
1265 : : nlmsg->nlmsg_seq = seq;
1266 : : nlmsg->nlmsg_pid = sock->pid;
1267 : :
1268 : : ovs_header = ofpbuf_put_uninit(&request, sizeof *ovs_header);
1269 : : ovs_header->dp_ifindex = 0;
1270 : : nlmsg->nlmsg_len = request.size;
1271 : :
1272 : : if (!DeviceIoControl(sock->handle, OVS_IOCTL_WRITE,
1273 : : request.data, request.size,
1274 : : NULL, 0, &bytes, overlapped)) {
1275 : : error = GetLastError();
1276 : : /* Check if the I/O got pended */
1277 : : if (error != ERROR_IO_INCOMPLETE && error != ERROR_IO_PENDING) {
1278 : : VLOG_ERR("nl_sock_wait failed - %s\n", ovs_format_message(error));
1279 : : retval = EINVAL;
1280 : : }
1281 : : } else {
1282 : : retval = EAGAIN;
1283 : : }
1284 : :
1285 : : done:
1286 : : ofpbuf_uninit(&request);
1287 : : return retval;
1288 : : }
1289 : : #endif /* _WIN32 */
1290 : :
1291 : : /* Causes poll_block() to wake up when any of the specified 'events' (which is
1292 : : * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'.
1293 : : * On Windows, 'sock' is not treated as const, and may be modified. */
1294 : : void
1295 : 1849546 : nl_sock_wait(const struct nl_sock *sock, short int events)
1296 : : {
1297 : : #ifdef _WIN32
1298 : : if (sock->overlapped.Internal != STATUS_PENDING) {
1299 : : int ret = pend_io_request(CONST_CAST(struct nl_sock *, sock));
1300 : : if (ret == 0) {
1301 : : poll_wevent_wait(sock->overlapped.hEvent);
1302 : : } else {
1303 : : poll_immediate_wake();
1304 : : }
1305 : : } else {
1306 : : poll_wevent_wait(sock->overlapped.hEvent);
1307 : : }
1308 : : #else
1309 : 1849546 : poll_fd_wait(sock->fd, events);
1310 : : #endif
1311 : 1849546 : }
1312 : :
1313 : : #ifndef _WIN32
1314 : : /* Returns the underlying fd for 'sock', for use in "poll()"-like operations
1315 : : * that can't use nl_sock_wait().
1316 : : *
1317 : : * It's a little tricky to use the returned fd correctly, because nl_sock does
1318 : : * "copy on write" to allow a single nl_sock to be used for notifications,
1319 : : * transactions, and dumps. If 'sock' is used only for notifications and
1320 : : * transactions (and never for dump) then the usage is safe. */
1321 : : int
1322 : 590 : nl_sock_fd(const struct nl_sock *sock)
1323 : : {
1324 : 590 : return sock->fd;
1325 : : }
1326 : : #endif
1327 : :
1328 : : /* Returns the PID associated with this socket. */
1329 : : uint32_t
1330 : 600 : nl_sock_pid(const struct nl_sock *sock)
1331 : : {
1332 : 600 : return sock->pid;
1333 : : }
1334 : :
1335 : : /* Miscellaneous. */
1336 : :
1337 : : struct genl_family {
1338 : : struct hmap_node hmap_node;
1339 : : uint16_t id;
1340 : : char *name;
1341 : : };
1342 : :
1343 : : static struct hmap genl_families = HMAP_INITIALIZER(&genl_families);
1344 : :
1345 : : static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = {
1346 : : [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16},
1347 : : [CTRL_ATTR_MCAST_GROUPS] = {.type = NL_A_NESTED, .optional = true},
1348 : : };
1349 : :
1350 : : static struct genl_family *
1351 : 508 : find_genl_family_by_id(uint16_t id)
1352 : : {
1353 : : struct genl_family *family;
1354 : :
1355 [ + + ][ - + ]: 786 : HMAP_FOR_EACH_IN_BUCKET (family, hmap_node, hash_int(id, 0),
1356 : : &genl_families) {
1357 [ - + ]: 278 : if (family->id == id) {
1358 : 0 : return family;
1359 : : }
1360 : : }
1361 : 508 : return NULL;
1362 : : }
1363 : :
1364 : : static void
1365 : 508 : define_genl_family(uint16_t id, const char *name)
1366 : : {
1367 : 508 : struct genl_family *family = find_genl_family_by_id(id);
1368 : :
1369 [ - + ]: 508 : if (family) {
1370 [ # # ]: 0 : if (!strcmp(family->name, name)) {
1371 : 0 : return;
1372 : : }
1373 : 0 : free(family->name);
1374 : : } else {
1375 : 508 : family = xmalloc(sizeof *family);
1376 : 508 : family->id = id;
1377 : 508 : hmap_insert(&genl_families, &family->hmap_node, hash_int(id, 0));
1378 : : }
1379 : 508 : family->name = xstrdup(name);
1380 : : }
1381 : :
1382 : : static const char *
1383 : 0 : genl_family_to_name(uint16_t id)
1384 : : {
1385 [ # # ]: 0 : if (id == GENL_ID_CTRL) {
1386 : 0 : return "control";
1387 : : } else {
1388 : 0 : struct genl_family *family = find_genl_family_by_id(id);
1389 [ # # ]: 0 : return family ? family->name : "unknown";
1390 : : }
1391 : : }
1392 : :
1393 : : #ifndef _WIN32
1394 : : static int
1395 : 666 : do_lookup_genl_family(const char *name, struct nlattr **attrs,
1396 : : struct ofpbuf **replyp)
1397 : : {
1398 : : struct nl_sock *sock;
1399 : : struct ofpbuf request, *reply;
1400 : : int error;
1401 : :
1402 : 666 : *replyp = NULL;
1403 : 666 : error = nl_sock_create(NETLINK_GENERIC, &sock);
1404 [ - + ]: 666 : if (error) {
1405 : 0 : return error;
1406 : : }
1407 : :
1408 : 666 : ofpbuf_init(&request, 0);
1409 : 666 : nl_msg_put_genlmsghdr(&request, 0, GENL_ID_CTRL, NLM_F_REQUEST,
1410 : : CTRL_CMD_GETFAMILY, 1);
1411 : 666 : nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name);
1412 : 666 : error = nl_sock_transact(sock, &request, &reply);
1413 : 666 : ofpbuf_uninit(&request);
1414 [ + + ]: 666 : if (error) {
1415 : 31 : nl_sock_destroy(sock);
1416 : 31 : return error;
1417 : : }
1418 : :
1419 [ + - ]: 635 : if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
1420 : : family_policy, attrs, ARRAY_SIZE(family_policy))
1421 [ - + ]: 635 : || nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]) == 0) {
1422 : 0 : nl_sock_destroy(sock);
1423 : 0 : ofpbuf_delete(reply);
1424 : 0 : return EPROTO;
1425 : : }
1426 : :
1427 : 635 : nl_sock_destroy(sock);
1428 : 635 : *replyp = reply;
1429 : 666 : return 0;
1430 : : }
1431 : : #else
1432 : : static int
1433 : : do_lookup_genl_family(const char *name, struct nlattr **attrs,
1434 : : struct ofpbuf **replyp)
1435 : : {
1436 : : struct nlmsghdr *nlmsg;
1437 : : struct ofpbuf *reply;
1438 : : int error;
1439 : : uint16_t family_id;
1440 : : const char *family_name;
1441 : : uint32_t family_version;
1442 : : uint32_t family_attrmax;
1443 : : uint32_t mcgrp_id = OVS_WIN_NL_INVALID_MCGRP_ID;
1444 : : const char *mcgrp_name = NULL;
1445 : :
1446 : : *replyp = NULL;
1447 : : reply = ofpbuf_new(1024);
1448 : :
1449 : : /* CTRL_ATTR_MCAST_GROUPS is supported only for VPORT family. */
1450 : : if (!strcmp(name, OVS_WIN_CONTROL_FAMILY)) {
1451 : : family_id = OVS_WIN_NL_CTRL_FAMILY_ID;
1452 : : family_name = OVS_WIN_CONTROL_FAMILY;
1453 : : family_version = OVS_WIN_CONTROL_VERSION;
1454 : : family_attrmax = OVS_WIN_CONTROL_ATTR_MAX;
1455 : : } else if (!strcmp(name, OVS_DATAPATH_FAMILY)) {
1456 : : family_id = OVS_WIN_NL_DATAPATH_FAMILY_ID;
1457 : : family_name = OVS_DATAPATH_FAMILY;
1458 : : family_version = OVS_DATAPATH_VERSION;
1459 : : family_attrmax = OVS_DP_ATTR_MAX;
1460 : : } else if (!strcmp(name, OVS_PACKET_FAMILY)) {
1461 : : family_id = OVS_WIN_NL_PACKET_FAMILY_ID;
1462 : : family_name = OVS_PACKET_FAMILY;
1463 : : family_version = OVS_PACKET_VERSION;
1464 : : family_attrmax = OVS_PACKET_ATTR_MAX;
1465 : : } else if (!strcmp(name, OVS_VPORT_FAMILY)) {
1466 : : family_id = OVS_WIN_NL_VPORT_FAMILY_ID;
1467 : : family_name = OVS_VPORT_FAMILY;
1468 : : family_version = OVS_VPORT_VERSION;
1469 : : family_attrmax = OVS_VPORT_ATTR_MAX;
1470 : : mcgrp_id = OVS_WIN_NL_VPORT_MCGRP_ID;
1471 : : mcgrp_name = OVS_VPORT_MCGROUP;
1472 : : } else if (!strcmp(name, OVS_FLOW_FAMILY)) {
1473 : : family_id = OVS_WIN_NL_FLOW_FAMILY_ID;
1474 : : family_name = OVS_FLOW_FAMILY;
1475 : : family_version = OVS_FLOW_VERSION;
1476 : : family_attrmax = OVS_FLOW_ATTR_MAX;
1477 : : } else if (!strcmp(name, OVS_WIN_NETDEV_FAMILY)) {
1478 : : family_id = OVS_WIN_NL_NETDEV_FAMILY_ID;
1479 : : family_name = OVS_WIN_NETDEV_FAMILY;
1480 : : family_version = OVS_WIN_NETDEV_VERSION;
1481 : : family_attrmax = OVS_WIN_NETDEV_ATTR_MAX;
1482 : : } else {
1483 : : ofpbuf_delete(reply);
1484 : : return EINVAL;
1485 : : }
1486 : :
1487 : : nl_msg_put_genlmsghdr(reply, 0, GENL_ID_CTRL, 0,
1488 : : CTRL_CMD_NEWFAMILY, family_version);
1489 : : /* CTRL_ATTR_HDRSIZE and CTRL_ATTR_OPS are not populated, but the
1490 : : * callers do not seem to need them. */
1491 : : nl_msg_put_u16(reply, CTRL_ATTR_FAMILY_ID, family_id);
1492 : : nl_msg_put_string(reply, CTRL_ATTR_FAMILY_NAME, family_name);
1493 : : nl_msg_put_u32(reply, CTRL_ATTR_VERSION, family_version);
1494 : : nl_msg_put_u32(reply, CTRL_ATTR_MAXATTR, family_attrmax);
1495 : :
1496 : : if (mcgrp_id != OVS_WIN_NL_INVALID_MCGRP_ID) {
1497 : : size_t mcgrp_ofs1 = nl_msg_start_nested(reply, CTRL_ATTR_MCAST_GROUPS);
1498 : : size_t mcgrp_ofs2= nl_msg_start_nested(reply,
1499 : : OVS_WIN_NL_VPORT_MCGRP_ID - OVS_WIN_NL_MCGRP_START_ID);
1500 : : nl_msg_put_u32(reply, CTRL_ATTR_MCAST_GRP_ID, mcgrp_id);
1501 : : ovs_assert(mcgrp_name != NULL);
1502 : : nl_msg_put_string(reply, CTRL_ATTR_MCAST_GRP_NAME, mcgrp_name);
1503 : : nl_msg_end_nested(reply, mcgrp_ofs2);
1504 : : nl_msg_end_nested(reply, mcgrp_ofs1);
1505 : : }
1506 : :
1507 : : /* Set the total length of the netlink message. */
1508 : : nlmsg = nl_msg_nlmsghdr(reply);
1509 : : nlmsg->nlmsg_len = reply->size;
1510 : :
1511 : : if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
1512 : : family_policy, attrs, ARRAY_SIZE(family_policy))
1513 : : || nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]) == 0) {
1514 : : ofpbuf_delete(reply);
1515 : : return EPROTO;
1516 : : }
1517 : :
1518 : : *replyp = reply;
1519 : : return 0;
1520 : : }
1521 : : #endif
1522 : :
1523 : : /* Finds the multicast group called 'group_name' in genl family 'family_name'.
1524 : : * When successful, writes its result to 'multicast_group' and returns 0.
1525 : : * Otherwise, clears 'multicast_group' and returns a positive error code.
1526 : : */
1527 : : int
1528 : 127 : nl_lookup_genl_mcgroup(const char *family_name, const char *group_name,
1529 : : unsigned int *multicast_group)
1530 : : {
1531 : : struct nlattr *family_attrs[ARRAY_SIZE(family_policy)];
1532 : : const struct nlattr *mc;
1533 : : struct ofpbuf *reply;
1534 : : unsigned int left;
1535 : : int error;
1536 : :
1537 : 127 : *multicast_group = 0;
1538 : 127 : error = do_lookup_genl_family(family_name, family_attrs, &reply);
1539 [ - + ]: 127 : if (error) {
1540 : 0 : return error;
1541 : : }
1542 : :
1543 [ - + ]: 127 : if (!family_attrs[CTRL_ATTR_MCAST_GROUPS]) {
1544 : 0 : error = EPROTO;
1545 : 0 : goto exit;
1546 : : }
1547 : :
1548 [ + - ]: 127 : NL_NESTED_FOR_EACH (mc, left, family_attrs[CTRL_ATTR_MCAST_GROUPS]) {
1549 : : static const struct nl_policy mc_policy[] = {
1550 : : [CTRL_ATTR_MCAST_GRP_ID] = {.type = NL_A_U32},
1551 : : [CTRL_ATTR_MCAST_GRP_NAME] = {.type = NL_A_STRING},
1552 : : };
1553 : :
1554 : : struct nlattr *mc_attrs[ARRAY_SIZE(mc_policy)];
1555 : : const char *mc_name;
1556 : :
1557 [ - + ]: 127 : if (!nl_parse_nested(mc, mc_policy, mc_attrs, ARRAY_SIZE(mc_policy))) {
1558 : 0 : error = EPROTO;
1559 : 127 : goto exit;
1560 : : }
1561 : :
1562 : 127 : mc_name = nl_attr_get_string(mc_attrs[CTRL_ATTR_MCAST_GRP_NAME]);
1563 [ + - ]: 127 : if (!strcmp(group_name, mc_name)) {
1564 : 127 : *multicast_group =
1565 : 127 : nl_attr_get_u32(mc_attrs[CTRL_ATTR_MCAST_GRP_ID]);
1566 : 127 : error = 0;
1567 : 127 : goto exit;
1568 : : }
1569 : : }
1570 : 0 : error = EPROTO;
1571 : :
1572 : : exit:
1573 : 127 : ofpbuf_delete(reply);
1574 : 127 : return error;
1575 : : }
1576 : :
1577 : : /* If '*number' is 0, translates the given Generic Netlink family 'name' to a
1578 : : * number and stores it in '*number'. If successful, returns 0 and the caller
1579 : : * may use '*number' as the family number. On failure, returns a positive
1580 : : * errno value and '*number' caches the errno value. */
1581 : : int
1582 : 539 : nl_lookup_genl_family(const char *name, int *number)
1583 : : {
1584 [ + - ]: 539 : if (*number == 0) {
1585 : : struct nlattr *attrs[ARRAY_SIZE(family_policy)];
1586 : : struct ofpbuf *reply;
1587 : : int error;
1588 : :
1589 : 539 : error = do_lookup_genl_family(name, attrs, &reply);
1590 [ + + ]: 539 : if (!error) {
1591 : 508 : *number = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]);
1592 : 508 : define_genl_family(*number, name);
1593 : : } else {
1594 : 31 : *number = -error;
1595 : : }
1596 : 539 : ofpbuf_delete(reply);
1597 : :
1598 [ - + ]: 539 : ovs_assert(*number != 0);
1599 : : }
1600 [ + + ]: 539 : return *number > 0 ? 0 : -*number;
1601 : : }
1602 : :
1603 : : struct nl_pool {
1604 : : struct nl_sock *socks[16];
1605 : : int n;
1606 : : };
1607 : :
1608 : : static struct ovs_mutex pool_mutex = OVS_MUTEX_INITIALIZER;
1609 : : static struct nl_pool pools[MAX_LINKS] OVS_GUARDED_BY(pool_mutex);
1610 : :
1611 : : static int
1612 : 17961 : nl_pool_alloc(int protocol, struct nl_sock **sockp)
1613 : : {
1614 : 17961 : struct nl_sock *sock = NULL;
1615 : : struct nl_pool *pool;
1616 : :
1617 [ + - ][ - + ]: 17961 : ovs_assert(protocol >= 0 && protocol < ARRAY_SIZE(pools));
1618 : :
1619 : 17961 : ovs_mutex_lock(&pool_mutex);
1620 : 17961 : pool = &pools[protocol];
1621 [ + + ]: 17961 : if (pool->n > 0) {
1622 : 16996 : sock = pool->socks[--pool->n];
1623 : : }
1624 : 17961 : ovs_mutex_unlock(&pool_mutex);
1625 : :
1626 [ + + ]: 17961 : if (sock) {
1627 : 16996 : *sockp = sock;
1628 : 16996 : return 0;
1629 : : } else {
1630 : 965 : return nl_sock_create(protocol, sockp);
1631 : : }
1632 : : }
1633 : :
1634 : : static void
1635 : 17961 : nl_pool_release(struct nl_sock *sock)
1636 : : {
1637 [ + - ]: 17961 : if (sock) {
1638 : 17961 : struct nl_pool *pool = &pools[sock->protocol];
1639 : :
1640 : 17961 : ovs_mutex_lock(&pool_mutex);
1641 [ + - ]: 17961 : if (pool->n < ARRAY_SIZE(pool->socks)) {
1642 : 17961 : pool->socks[pool->n++] = sock;
1643 : 17961 : sock = NULL;
1644 : : }
1645 : 17961 : ovs_mutex_unlock(&pool_mutex);
1646 : :
1647 : 17961 : nl_sock_destroy(sock);
1648 : : }
1649 : 17961 : }
1650 : :
1651 : : /* Sends 'request' to the kernel on a Netlink socket for the given 'protocol'
1652 : : * (e.g. NETLINK_ROUTE or NETLINK_GENERIC) and waits for a response. If
1653 : : * successful, returns 0. On failure, returns a positive errno value.
1654 : : *
1655 : : * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's
1656 : : * reply, which the caller is responsible for freeing with ofpbuf_delete(), and
1657 : : * on failure '*replyp' is set to NULL. If 'replyp' is null, then the kernel's
1658 : : * reply, if any, is discarded.
1659 : : *
1660 : : * Before the message is sent, nlmsg_len in 'request' will be finalized to
1661 : : * match msg->size, nlmsg_pid will be set to the pid of the socket used
1662 : : * for sending the request, and nlmsg_seq will be initialized.
1663 : : *
1664 : : * The caller is responsible for destroying 'request'.
1665 : : *
1666 : : * Bare Netlink is an unreliable transport protocol. This function layers
1667 : : * reliable delivery and reply semantics on top of bare Netlink.
1668 : : *
1669 : : * In Netlink, sending a request to the kernel is reliable enough, because the
1670 : : * kernel will tell us if the message cannot be queued (and we will in that
1671 : : * case put it on the transmit queue and wait until it can be delivered).
1672 : : *
1673 : : * Receiving the reply is the real problem: if the socket buffer is full when
1674 : : * the kernel tries to send the reply, the reply will be dropped. However, the
1675 : : * kernel sets a flag that a reply has been dropped. The next call to recv
1676 : : * then returns ENOBUFS. We can then re-send the request.
1677 : : *
1678 : : * Caveats:
1679 : : *
1680 : : * 1. Netlink depends on sequence numbers to match up requests and
1681 : : * replies. The sender of a request supplies a sequence number, and
1682 : : * the reply echos back that sequence number.
1683 : : *
1684 : : * This is fine, but (1) some kernel netlink implementations are
1685 : : * broken, in that they fail to echo sequence numbers and (2) this
1686 : : * function will drop packets with non-matching sequence numbers, so
1687 : : * that only a single request can be usefully transacted at a time.
1688 : : *
1689 : : * 2. Resending the request causes it to be re-executed, so the request
1690 : : * needs to be idempotent.
1691 : : */
1692 : : int
1693 : 8453 : nl_transact(int protocol, const struct ofpbuf *request,
1694 : : struct ofpbuf **replyp)
1695 : : {
1696 : : struct nl_sock *sock;
1697 : : int error;
1698 : :
1699 : 8453 : error = nl_pool_alloc(protocol, &sock);
1700 [ - + ]: 8453 : if (error) {
1701 : 0 : *replyp = NULL;
1702 : 0 : return error;
1703 : : }
1704 : :
1705 : 8453 : error = nl_sock_transact(sock, request, replyp);
1706 : :
1707 : 8453 : nl_pool_release(sock);
1708 : 8453 : return error;
1709 : : }
1710 : :
1711 : : /* Sends the 'request' member of the 'n' transactions in 'transactions' on a
1712 : : * Netlink socket for the given 'protocol' (e.g. NETLINK_ROUTE or
1713 : : * NETLINK_GENERIC), in order, and receives responses to all of them. Fills in
1714 : : * the 'error' member of each transaction with 0 if it was successful,
1715 : : * otherwise with a positive errno value. If 'reply' is nonnull, then it will
1716 : : * be filled with the reply if the message receives a detailed reply. In other
1717 : : * cases, i.e. where the request failed or had no reply beyond an indication of
1718 : : * success, 'reply' will be cleared if it is nonnull.
1719 : : *
1720 : : * The caller is responsible for destroying each request and reply, and the
1721 : : * transactions array itself.
1722 : : *
1723 : : * Before sending each message, this function will finalize nlmsg_len in each
1724 : : * 'request' to match the ofpbuf's size, set nlmsg_pid to the pid of the socket
1725 : : * used for the transaction, and initialize nlmsg_seq.
1726 : : *
1727 : : * Bare Netlink is an unreliable transport protocol. This function layers
1728 : : * reliable delivery and reply semantics on top of bare Netlink. See
1729 : : * nl_transact() for some caveats.
1730 : : */
1731 : : void
1732 : 4386 : nl_transact_multiple(int protocol,
1733 : : struct nl_transaction **transactions, size_t n)
1734 : : {
1735 : : struct nl_sock *sock;
1736 : : int error;
1737 : :
1738 : 4386 : error = nl_pool_alloc(protocol, &sock);
1739 [ + - ]: 4386 : if (!error) {
1740 : 4386 : nl_sock_transact_multiple(sock, transactions, n);
1741 : 4386 : nl_pool_release(sock);
1742 : : } else {
1743 : 0 : nl_sock_record_errors__(transactions, n, error);
1744 : : }
1745 : 4386 : }
1746 : :
1747 : :
1748 : : static uint32_t
1749 : 18635 : nl_sock_allocate_seq(struct nl_sock *sock, unsigned int n)
1750 : : {
1751 : 18635 : uint32_t seq = sock->next_seq;
1752 : :
1753 : 18635 : sock->next_seq += n;
1754 : :
1755 : : /* Make it impossible for the next request for sequence numbers to wrap
1756 : : * around to 0. Start over with 1 to avoid ever using a sequence number of
1757 : : * 0, because the kernel uses sequence number 0 for notifications. */
1758 [ - + ]: 18635 : if (sock->next_seq >= UINT32_MAX / 2) {
1759 : 0 : sock->next_seq = 1;
1760 : : }
1761 : :
1762 : 18635 : return seq;
1763 : : }
1764 : :
1765 : : static void
1766 : 0 : nlmsghdr_to_string(const struct nlmsghdr *h, int protocol, struct ds *ds)
1767 : : {
1768 : : struct nlmsg_flag {
1769 : : unsigned int bits;
1770 : : const char *name;
1771 : : };
1772 : : static const struct nlmsg_flag flags[] = {
1773 : : { NLM_F_REQUEST, "REQUEST" },
1774 : : { NLM_F_MULTI, "MULTI" },
1775 : : { NLM_F_ACK, "ACK" },
1776 : : { NLM_F_ECHO, "ECHO" },
1777 : : { NLM_F_DUMP, "DUMP" },
1778 : : { NLM_F_ROOT, "ROOT" },
1779 : : { NLM_F_MATCH, "MATCH" },
1780 : : { NLM_F_ATOMIC, "ATOMIC" },
1781 : : };
1782 : : const struct nlmsg_flag *flag;
1783 : : uint16_t flags_left;
1784 : :
1785 : 0 : ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16,
1786 : 0 : h->nlmsg_len, h->nlmsg_type);
1787 [ # # ]: 0 : if (h->nlmsg_type == NLMSG_NOOP) {
1788 : 0 : ds_put_cstr(ds, "(no-op)");
1789 [ # # ]: 0 : } else if (h->nlmsg_type == NLMSG_ERROR) {
1790 : 0 : ds_put_cstr(ds, "(error)");
1791 [ # # ]: 0 : } else if (h->nlmsg_type == NLMSG_DONE) {
1792 : 0 : ds_put_cstr(ds, "(done)");
1793 [ # # ]: 0 : } else if (h->nlmsg_type == NLMSG_OVERRUN) {
1794 : 0 : ds_put_cstr(ds, "(overrun)");
1795 [ # # ]: 0 : } else if (h->nlmsg_type < NLMSG_MIN_TYPE) {
1796 : 0 : ds_put_cstr(ds, "(reserved)");
1797 [ # # ]: 0 : } else if (protocol == NETLINK_GENERIC) {
1798 : 0 : ds_put_format(ds, "(%s)", genl_family_to_name(h->nlmsg_type));
1799 : : } else {
1800 : 0 : ds_put_cstr(ds, "(family-defined)");
1801 : : }
1802 : 0 : ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags);
1803 : 0 : flags_left = h->nlmsg_flags;
1804 [ # # ]: 0 : for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1805 [ # # ]: 0 : if ((flags_left & flag->bits) == flag->bits) {
1806 : 0 : ds_put_format(ds, "[%s]", flag->name);
1807 : 0 : flags_left &= ~flag->bits;
1808 : : }
1809 : : }
1810 [ # # ]: 0 : if (flags_left) {
1811 : 0 : ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left);
1812 : : }
1813 : 0 : ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32,
1814 : : h->nlmsg_seq, h->nlmsg_pid);
1815 : 0 : }
1816 : :
1817 : : static char *
1818 : 0 : nlmsg_to_string(const struct ofpbuf *buffer, int protocol)
1819 : : {
1820 : 0 : struct ds ds = DS_EMPTY_INITIALIZER;
1821 : 0 : const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN);
1822 [ # # ]: 0 : if (h) {
1823 : 0 : nlmsghdr_to_string(h, protocol, &ds);
1824 [ # # ]: 0 : if (h->nlmsg_type == NLMSG_ERROR) {
1825 : : const struct nlmsgerr *e;
1826 : 0 : e = ofpbuf_at(buffer, NLMSG_HDRLEN,
1827 : : NLMSG_ALIGN(sizeof(struct nlmsgerr)));
1828 [ # # ]: 0 : if (e) {
1829 : 0 : ds_put_format(&ds, " error(%d", e->error);
1830 [ # # ]: 0 : if (e->error < 0) {
1831 : 0 : ds_put_format(&ds, "(%s)", ovs_strerror(-e->error));
1832 : : }
1833 : 0 : ds_put_cstr(&ds, ", in-reply-to(");
1834 : 0 : nlmsghdr_to_string(&e->msg, protocol, &ds);
1835 : 0 : ds_put_cstr(&ds, "))");
1836 : : } else {
1837 : 0 : ds_put_cstr(&ds, " error(truncated)");
1838 : : }
1839 [ # # ]: 0 : } else if (h->nlmsg_type == NLMSG_DONE) {
1840 : 0 : int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error);
1841 [ # # ]: 0 : if (error) {
1842 : 0 : ds_put_format(&ds, " done(%d", *error);
1843 [ # # ]: 0 : if (*error < 0) {
1844 : 0 : ds_put_format(&ds, "(%s)", ovs_strerror(-*error));
1845 : : }
1846 : 0 : ds_put_cstr(&ds, ")");
1847 : : } else {
1848 : 0 : ds_put_cstr(&ds, " done(truncated)");
1849 : : }
1850 [ # # ]: 0 : } else if (protocol == NETLINK_GENERIC) {
1851 : 0 : struct genlmsghdr *genl = nl_msg_genlmsghdr(buffer);
1852 [ # # ]: 0 : if (genl) {
1853 : 0 : ds_put_format(&ds, ",genl(cmd=%"PRIu8",version=%"PRIu8")",
1854 : 0 : genl->cmd, genl->version);
1855 : : }
1856 : : }
1857 : : } else {
1858 : 0 : ds_put_cstr(&ds, "nl(truncated)");
1859 : : }
1860 : 0 : return ds.string;
1861 : : }
1862 : :
1863 : : static void
1864 : 49751 : log_nlmsg(const char *function, int error,
1865 : : const void *message, size_t size, int protocol)
1866 : : {
1867 [ + - ]: 49751 : if (!VLOG_IS_DBG_ENABLED()) {
1868 : 49752 : return;
1869 : : }
1870 : :
1871 : 0 : struct ofpbuf buffer = ofpbuf_const_initializer(message, size);
1872 : 0 : char *nlmsg = nlmsg_to_string(&buffer, protocol);
1873 [ # # ]: 0 : VLOG_DBG_RL(&rl, "%s (%s): %s", function, ovs_strerror(error), nlmsg);
1874 : 0 : free(nlmsg);
1875 : : }
|