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