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