2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "netlink-socket.h"
23 #include <sys/types.h>
26 #include "dynamic-string.h"
28 #include "netlink-protocol.h"
30 #include "poll-loop.h"
31 #include "socket-util.h"
35 VLOG_DEFINE_THIS_MODULE(netlink_socket);
37 COVERAGE_DEFINE(netlink_overflow);
38 COVERAGE_DEFINE(netlink_received);
39 COVERAGE_DEFINE(netlink_recv_retry);
40 COVERAGE_DEFINE(netlink_send);
41 COVERAGE_DEFINE(netlink_sent);
43 /* Linux header file confusion causes this to be undefined. */
45 #define SOL_NETLINK 270
48 /* A single (bad) Netlink message can in theory dump out many, many log
49 * messages, so the burst size is set quite high here to avoid missing useful
50 * information. Also, at high logging levels we log *all* Netlink messages. */
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600);
53 static void log_nlmsg(const char *function, int error,
54 const void *message, size_t size, int protocol);
56 /* Netlink sockets. */
67 static int alloc_pid(uint32_t *);
68 static void free_pid(uint32_t);
69 static int nl_sock_cow__(struct nl_sock *);
71 /* Creates a new netlink socket for the given netlink 'protocol'
72 * (NETLINK_ROUTE, NETLINK_GENERIC, ...). Returns 0 and sets '*sockp' to the
73 * new socket if successful, otherwise returns a positive errno value. */
75 nl_sock_create(int protocol, struct nl_sock **sockp)
78 struct sockaddr_nl local, remote;
82 sock = malloc(sizeof *sock);
87 sock->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
89 VLOG_ERR("fcntl: %s", strerror(errno));
92 sock->protocol = protocol;
93 sock->any_groups = false;
95 retval = alloc_pid(&sock->pid);
100 /* Bind local address as our selected pid. */
101 memset(&local, 0, sizeof local);
102 local.nl_family = AF_NETLINK;
103 local.nl_pid = sock->pid;
104 if (bind(sock->fd, (struct sockaddr *) &local, sizeof local) < 0) {
105 VLOG_ERR("bind(%"PRIu32"): %s", sock->pid, strerror(errno));
109 /* Bind remote address as the kernel (pid 0). */
110 memset(&remote, 0, sizeof remote);
111 remote.nl_family = AF_NETLINK;
113 if (connect(sock->fd, (struct sockaddr *) &remote, sizeof remote) < 0) {
114 VLOG_ERR("connect(0): %s", strerror(errno));
137 /* Creates a new netlink socket for the same protocol as 'src'. Returns 0 and
138 * sets '*sockp' to the new socket if successful, otherwise returns a positive
141 nl_sock_clone(const struct nl_sock *src, struct nl_sock **sockp)
143 return nl_sock_create(src->protocol, sockp);
146 /* Destroys netlink socket 'sock'. */
148 nl_sock_destroy(struct nl_sock *sock)
161 /* Tries to add 'sock' as a listener for 'multicast_group'. Returns 0 if
162 * successful, otherwise a positive errno value.
164 * Multicast group numbers are always positive.
166 * It is not an error to attempt to join a multicast group to which a socket
167 * already belongs. */
169 nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
171 int error = nl_sock_cow__(sock);
175 if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
176 &multicast_group, sizeof multicast_group) < 0) {
177 VLOG_WARN("could not join multicast group %u (%s)",
178 multicast_group, strerror(errno));
181 sock->any_groups = true;
185 /* Tries to make 'sock' stop listening to 'multicast_group'. Returns 0 if
186 * successful, otherwise a positive errno value.
188 * Multicast group numbers are always positive.
190 * It is not an error to attempt to leave a multicast group to which a socket
193 * On success, reading from 'sock' will still return any messages that were
194 * received on 'multicast_group' before the group was left. */
196 nl_sock_leave_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
199 if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
200 &multicast_group, sizeof multicast_group) < 0) {
201 VLOG_WARN("could not leave multicast group %u (%s)",
202 multicast_group, strerror(errno));
209 nl_sock_send__(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
211 struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg);
214 nlmsg->nlmsg_len = msg->size;
215 nlmsg->nlmsg_pid = sock->pid;
218 retval = send(sock->fd, msg->data, msg->size, wait ? 0 : MSG_DONTWAIT);
219 error = retval < 0 ? errno : 0;
220 } while (error == EINTR);
221 log_nlmsg(__func__, error, msg->data, msg->size, sock->protocol);
223 COVERAGE_INC(netlink_sent);
228 /* Tries to send 'msg', which must contain a Netlink message, to the kernel on
229 * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, and
230 * nlmsg_pid will be set to 'sock''s pid, before the message is sent.
232 * Returns 0 if successful, otherwise a positive errno value. If
233 * 'wait' is true, then the send will wait until buffer space is ready;
234 * otherwise, returns EAGAIN if the 'sock' send buffer is full. */
236 nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
238 int error = nl_sock_cow__(sock);
242 return nl_sock_send__(sock, msg, wait);
245 /* Tries to send the 'n_iov' chunks of data in 'iov' to the kernel on 'sock' as
246 * a single Netlink message. (The message must be fully formed and not require
247 * finalization of its nlmsg_len or nlmsg_pid fields.)
249 * Returns 0 if successful, otherwise a positive errno value. If 'wait' is
250 * true, then the send will wait until buffer space is ready; otherwise,
251 * returns EAGAIN if the 'sock' send buffer is full. */
253 nl_sock_sendv(struct nl_sock *sock, const struct iovec iov[], size_t n_iov,
259 COVERAGE_INC(netlink_send);
260 memset(&msg, 0, sizeof msg);
261 msg.msg_iov = (struct iovec *) iov;
262 msg.msg_iovlen = n_iov;
265 retval = sendmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT);
266 error = retval < 0 ? errno : 0;
267 } while (error == EINTR);
268 if (error != EAGAIN) {
269 log_nlmsg(__func__, error, iov[0].iov_base, iov[0].iov_len,
272 COVERAGE_INC(netlink_sent);
278 /* This stress option is useful for testing that OVS properly tolerates
279 * -ENOBUFS on NetLink sockets. Such errors are unavoidable because they can
280 * occur if the kernel cannot temporarily allocate enough GFP_ATOMIC memory to
281 * reply to a request. They can also occur if messages arrive on a multicast
282 * channel faster than OVS can process them. */
284 netlink_overflow, "simulate netlink socket receive buffer overflow",
288 nl_sock_recv__(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
291 ssize_t bufsize = 2048;
292 ssize_t nbytes, nbytes2;
294 struct nlmsghdr *nlmsghdr;
296 struct msghdr msg = {
306 buf = ofpbuf_new(bufsize);
310 /* Attempt to read the message. We don't know the size of the data
311 * yet, so we take a guess at 2048. If we're wrong, we keep trying
312 * and doubling the buffer size each time.
314 nlmsghdr = ofpbuf_put_uninit(buf, bufsize);
315 iov.iov_base = nlmsghdr;
316 iov.iov_len = bufsize;
318 nbytes = recvmsg(sock->fd, &msg, (wait ? 0 : MSG_DONTWAIT) | MSG_PEEK);
319 } while (nbytes < 0 && errno == EINTR);
324 if (msg.msg_flags & MSG_TRUNC) {
325 COVERAGE_INC(netlink_recv_retry);
327 ofpbuf_reinit(buf, bufsize);
332 /* We successfully read the message, so recv again to clear the queue */
336 nbytes2 = recvmsg(sock->fd, &msg, MSG_DONTWAIT);
337 } while (nbytes2 < 0 && errno == EINTR);
339 if (errno == ENOBUFS) {
340 /* The kernel is notifying us that a message it tried to send to us
341 * was dropped. We have to pass this along to the caller in case
342 * it wants to retry a request. So kill the buffer, which we can
343 * re-read next time. */
344 COVERAGE_INC(netlink_overflow);
348 VLOG_ERR_RL(&rl, "failed to remove nlmsg from socket: %s\n",
352 if (nbytes < sizeof *nlmsghdr
353 || nlmsghdr->nlmsg_len < sizeof *nlmsghdr
354 || nlmsghdr->nlmsg_len > nbytes) {
355 VLOG_ERR_RL(&rl, "received invalid nlmsg (%zd bytes < %d)",
356 bufsize, NLMSG_HDRLEN);
361 if (STRESS(netlink_overflow)) {
367 log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol);
368 COVERAGE_INC(netlink_received);
373 /* Tries to receive a netlink message from the kernel on 'sock'. If
374 * successful, stores the received message into '*bufp' and returns 0. The
375 * caller is responsible for destroying the message with ofpbuf_delete(). On
376 * failure, returns a positive errno value and stores a null pointer into
379 * If 'wait' is true, nl_sock_recv waits for a message to be ready; otherwise,
380 * returns EAGAIN if the 'sock' receive buffer is empty. */
382 nl_sock_recv(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
384 int error = nl_sock_cow__(sock);
388 return nl_sock_recv__(sock, bufp, wait);
391 /* Sends 'request' to the kernel via 'sock' and waits for a response. If
392 * successful, returns 0. On failure, returns a positive errno value.
394 * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's
395 * reply, which the caller is responsible for freeing with ofpbuf_delete(), and
396 * on failure '*replyp' is set to NULL. If 'replyp' is null, then the kernel's
397 * reply, if any, is discarded.
399 * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
400 * be set to 'sock''s pid, before the message is sent. NLM_F_ACK will be set
403 * The caller is responsible for destroying 'request'.
405 * Bare Netlink is an unreliable transport protocol. This function layers
406 * reliable delivery and reply semantics on top of bare Netlink.
408 * In Netlink, sending a request to the kernel is reliable enough, because the
409 * kernel will tell us if the message cannot be queued (and we will in that
410 * case put it on the transmit queue and wait until it can be delivered).
412 * Receiving the reply is the real problem: if the socket buffer is full when
413 * the kernel tries to send the reply, the reply will be dropped. However, the
414 * kernel sets a flag that a reply has been dropped. The next call to recv
415 * then returns ENOBUFS. We can then re-send the request.
419 * 1. Netlink depends on sequence numbers to match up requests and
420 * replies. The sender of a request supplies a sequence number, and
421 * the reply echos back that sequence number.
423 * This is fine, but (1) some kernel netlink implementations are
424 * broken, in that they fail to echo sequence numbers and (2) this
425 * function will drop packets with non-matching sequence numbers, so
426 * that only a single request can be usefully transacted at a time.
428 * 2. Resending the request causes it to be re-executed, so the request
429 * needs to be idempotent.
432 nl_sock_transact(struct nl_sock *sock,
433 const struct ofpbuf *request, struct ofpbuf **replyp)
435 uint32_t seq = nl_msg_nlmsghdr(request)->nlmsg_seq;
436 struct nlmsghdr *nlmsghdr;
437 struct ofpbuf *reply;
444 /* Ensure that we get a reply even if this message doesn't ordinarily call
446 nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_ACK;
449 retval = nl_sock_send(sock, request, true);
455 retval = nl_sock_recv(sock, &reply, true);
457 if (retval == ENOBUFS) {
458 COVERAGE_INC(netlink_overflow);
459 VLOG_DBG_RL(&rl, "receive buffer overflow, resending request");
465 nlmsghdr = nl_msg_nlmsghdr(reply);
466 if (seq != nlmsghdr->nlmsg_seq) {
467 VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32,
468 nl_msg_nlmsghdr(reply)->nlmsg_seq, seq);
469 ofpbuf_delete(reply);
473 /* If the reply is an error, discard the reply and return the error code.
475 * Except: if the reply is just an acknowledgement (error code of 0), and
476 * the caller is interested in the reply (replyp != NULL), pass the reply
477 * up to the caller. Otherwise the caller will get a return value of 0
478 * and null '*replyp', which makes unwary callers likely to segfault. */
479 if (nl_msg_nlmsgerr(reply, &retval) && (retval || !replyp)) {
480 ofpbuf_delete(reply);
482 VLOG_DBG_RL(&rl, "received NAK error=%d (%s)",
483 retval, strerror(retval));
485 return retval != EAGAIN ? retval : EPROTO;
491 ofpbuf_delete(reply);
496 /* Drain all the messages currently in 'sock''s receive queue. */
498 nl_sock_drain(struct nl_sock *sock)
500 int error = nl_sock_cow__(sock);
504 return drain_rcvbuf(sock->fd);
507 /* The client is attempting some operation on 'sock'. If 'sock' has an ongoing
508 * dump operation, then replace 'sock''s fd with a new socket and hand 'sock''s
509 * old fd over to the dump. */
511 nl_sock_cow__(struct nl_sock *sock)
513 struct nl_sock *copy;
522 error = nl_sock_clone(sock, ©);
532 sock->pid = copy->pid;
535 sock->dump->sock = copy;
541 /* Starts a Netlink "dump" operation, by sending 'request' to the kernel via
542 * 'sock', and initializes 'dump' to reflect the state of the operation.
544 * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
545 * be set to 'sock''s pid, before the message is sent. NLM_F_DUMP and
546 * NLM_F_ACK will be set in nlmsg_flags.
548 * This Netlink socket library is designed to ensure that the dump is reliable
549 * and that it will not interfere with other operations on 'sock', including
550 * destroying or sending and receiving messages on 'sock'. One corner case is
553 * - If 'sock' has been used to send a request (e.g. with nl_sock_send())
554 * whose response has not yet been received (e.g. with nl_sock_recv()).
555 * This is unusual: usually nl_sock_transact() is used to send a message
556 * and receive its reply all in one go.
558 * This function provides no status indication. An error status for the entire
559 * dump operation is provided when it is completed by calling nl_dump_done().
561 * The caller is responsible for destroying 'request'.
563 * The new 'dump' is independent of 'sock'. 'sock' and 'dump' may be destroyed
567 nl_dump_start(struct nl_dump *dump,
568 struct nl_sock *sock, const struct ofpbuf *request)
570 struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(request);
571 nlmsghdr->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK;
572 dump->seq = nlmsghdr->nlmsg_seq;
574 if (sock->any_groups || sock->dump) {
575 /* 'sock' might belong to some multicast group, or it already has an
576 * onoging dump. Clone the socket to avoid possibly intermixing
577 * multicast messages or previous dump results with our results. */
578 dump->status = nl_sock_clone(sock, &dump->sock);
587 dump->status = nl_sock_send__(sock, request, true);
590 /* Helper function for nl_dump_next(). */
592 nl_dump_recv(struct nl_dump *dump, struct ofpbuf **bufferp)
594 struct nlmsghdr *nlmsghdr;
595 struct ofpbuf *buffer;
598 retval = nl_sock_recv__(dump->sock, bufferp, true);
600 return retval == EINTR ? EAGAIN : retval;
604 nlmsghdr = nl_msg_nlmsghdr(buffer);
605 if (dump->seq != nlmsghdr->nlmsg_seq) {
606 VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32,
607 nlmsghdr->nlmsg_seq, dump->seq);
611 if (nl_msg_nlmsgerr(buffer, &retval)) {
612 VLOG_INFO_RL(&rl, "netlink dump request error (%s)",
614 return retval && retval != EAGAIN ? retval : EPROTO;
620 /* Attempts to retrieve another reply from 'dump', which must have been
621 * initialized with nl_dump_start().
623 * If successful, returns true and points 'reply->data' and 'reply->size' to
624 * the message that was retrieved. The caller must not modify 'reply' (because
625 * it points into the middle of a larger buffer).
627 * On failure, returns false and sets 'reply->data' to NULL and 'reply->size'
628 * to 0. Failure might indicate an actual error or merely the end of replies.
629 * An error status for the entire dump operation is provided when it is
630 * completed by calling nl_dump_done().
633 nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply)
635 struct nlmsghdr *nlmsghdr;
643 if (dump->buffer && !dump->buffer->size) {
644 ofpbuf_delete(dump->buffer);
647 while (!dump->buffer) {
648 int retval = nl_dump_recv(dump, &dump->buffer);
650 ofpbuf_delete(dump->buffer);
652 if (retval != EAGAIN) {
653 dump->status = retval;
659 nlmsghdr = nl_msg_next(dump->buffer, reply);
661 VLOG_WARN_RL(&rl, "netlink dump reply contains message fragment");
662 dump->status = EPROTO;
664 } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) {
672 /* Completes Netlink dump operation 'dump', which must have been initialized
673 * with nl_dump_start(). Returns 0 if the dump operation was error-free,
674 * otherwise a positive errno value describing the problem. */
676 nl_dump_done(struct nl_dump *dump)
678 /* Drain any remaining messages that the client didn't read. Otherwise the
679 * kernel will continue to queue them up and waste buffer space. */
680 while (!dump->status) {
682 if (!nl_dump_next(dump, &reply)) {
683 assert(dump->status);
688 if (dump->sock->dump) {
689 dump->sock->dump = NULL;
691 nl_sock_destroy(dump->sock);
694 ofpbuf_delete(dump->buffer);
695 return dump->status == EOF ? 0 : dump->status;
698 /* Causes poll_block() to wake up when any of the specified 'events' (which is
699 * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'. */
701 nl_sock_wait(const struct nl_sock *sock, short int events)
703 poll_fd_wait(sock->fd, events);
708 static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = {
709 [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16},
712 static int do_lookup_genl_family(const char *name)
714 struct nl_sock *sock;
715 struct ofpbuf request, *reply;
716 struct nlattr *attrs[ARRAY_SIZE(family_policy)];
719 retval = nl_sock_create(NETLINK_GENERIC, &sock);
724 ofpbuf_init(&request, 0);
725 nl_msg_put_genlmsghdr(&request, 0, GENL_ID_CTRL, NLM_F_REQUEST,
726 CTRL_CMD_GETFAMILY, 1);
727 nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name);
728 retval = nl_sock_transact(sock, &request, &reply);
729 ofpbuf_uninit(&request);
731 nl_sock_destroy(sock);
735 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
736 family_policy, attrs, ARRAY_SIZE(family_policy))) {
737 nl_sock_destroy(sock);
738 ofpbuf_delete(reply);
742 retval = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]);
746 nl_sock_destroy(sock);
747 ofpbuf_delete(reply);
751 /* If '*number' is 0, translates the given Generic Netlink family 'name' to a
752 * number and stores it in '*number'. If successful, returns 0 and the caller
753 * may use '*number' as the family number. On failure, returns a positive
754 * errno value and '*number' caches the errno value. */
756 nl_lookup_genl_family(const char *name, int *number)
759 *number = do_lookup_genl_family(name);
760 assert(*number != 0);
762 return *number > 0 ? 0 : -*number;
767 * Every Netlink socket must be bound to a unique 32-bit PID. By convention,
768 * programs that have a single Netlink socket use their Unix process ID as PID,
769 * and programs with multiple Netlink sockets add a unique per-socket
770 * identifier in the bits above the Unix process ID.
772 * The kernel has Netlink PID 0.
775 /* Parameters for how many bits in the PID should come from the Unix process ID
776 * and how many unique per-socket. */
777 #define SOCKET_BITS 10
778 #define MAX_SOCKETS (1u << SOCKET_BITS)
780 #define PROCESS_BITS (32 - SOCKET_BITS)
781 #define MAX_PROCESSES (1u << PROCESS_BITS)
782 #define PROCESS_MASK ((uint32_t) (MAX_PROCESSES - 1))
784 /* Bit vector of unused socket identifiers. */
785 static uint32_t avail_sockets[ROUND_UP(MAX_SOCKETS, 32)];
787 /* Allocates and returns a new Netlink PID. */
789 alloc_pid(uint32_t *pid)
793 for (i = 0; i < MAX_SOCKETS; i++) {
794 if ((avail_sockets[i / 32] & (1u << (i % 32))) == 0) {
795 avail_sockets[i / 32] |= 1u << (i % 32);
796 *pid = (getpid() & PROCESS_MASK) | (i << PROCESS_BITS);
800 VLOG_ERR("netlink pid space exhausted");
804 /* Makes the specified 'pid' available for reuse. */
806 free_pid(uint32_t pid)
808 int sock = pid >> PROCESS_BITS;
809 assert(avail_sockets[sock / 32] & (1u << (sock % 32)));
810 avail_sockets[sock / 32] &= ~(1u << (sock % 32));
814 nlmsghdr_to_string(const struct nlmsghdr *h, struct ds *ds)
820 static const struct nlmsg_flag flags[] = {
821 { NLM_F_REQUEST, "REQUEST" },
822 { NLM_F_MULTI, "MULTI" },
823 { NLM_F_ACK, "ACK" },
824 { NLM_F_ECHO, "ECHO" },
825 { NLM_F_DUMP, "DUMP" },
826 { NLM_F_ROOT, "ROOT" },
827 { NLM_F_MATCH, "MATCH" },
828 { NLM_F_ATOMIC, "ATOMIC" },
830 const struct nlmsg_flag *flag;
833 ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16,
834 h->nlmsg_len, h->nlmsg_type);
835 if (h->nlmsg_type == NLMSG_NOOP) {
836 ds_put_cstr(ds, "(no-op)");
837 } else if (h->nlmsg_type == NLMSG_ERROR) {
838 ds_put_cstr(ds, "(error)");
839 } else if (h->nlmsg_type == NLMSG_DONE) {
840 ds_put_cstr(ds, "(done)");
841 } else if (h->nlmsg_type == NLMSG_OVERRUN) {
842 ds_put_cstr(ds, "(overrun)");
843 } else if (h->nlmsg_type < NLMSG_MIN_TYPE) {
844 ds_put_cstr(ds, "(reserved)");
846 ds_put_cstr(ds, "(family-defined)");
848 ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags);
849 flags_left = h->nlmsg_flags;
850 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
851 if ((flags_left & flag->bits) == flag->bits) {
852 ds_put_format(ds, "[%s]", flag->name);
853 flags_left &= ~flag->bits;
857 ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left);
859 ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32"(%d:%d))",
860 h->nlmsg_seq, h->nlmsg_pid,
861 (int) (h->nlmsg_pid & PROCESS_MASK),
862 (int) (h->nlmsg_pid >> PROCESS_BITS));
866 nlmsg_to_string(const struct ofpbuf *buffer, int protocol)
868 struct ds ds = DS_EMPTY_INITIALIZER;
869 const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN);
871 nlmsghdr_to_string(h, &ds);
872 if (h->nlmsg_type == NLMSG_ERROR) {
873 const struct nlmsgerr *e;
874 e = ofpbuf_at(buffer, NLMSG_HDRLEN,
875 NLMSG_ALIGN(sizeof(struct nlmsgerr)));
877 ds_put_format(&ds, " error(%d", e->error);
879 ds_put_format(&ds, "(%s)", strerror(-e->error));
881 ds_put_cstr(&ds, ", in-reply-to(");
882 nlmsghdr_to_string(&e->msg, &ds);
883 ds_put_cstr(&ds, "))");
885 ds_put_cstr(&ds, " error(truncated)");
887 } else if (h->nlmsg_type == NLMSG_DONE) {
888 int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error);
890 ds_put_format(&ds, " done(%d", *error);
892 ds_put_format(&ds, "(%s)", strerror(-*error));
894 ds_put_cstr(&ds, ")");
896 ds_put_cstr(&ds, " done(truncated)");
898 } else if (protocol == NETLINK_GENERIC) {
899 struct genlmsghdr *genl = nl_msg_genlmsghdr(buffer);
901 ds_put_format(&ds, ",genl(cmd=%"PRIu8",version=%"PRIu8")",
902 genl->cmd, genl->version);
906 ds_put_cstr(&ds, "nl(truncated)");
912 log_nlmsg(const char *function, int error,
913 const void *message, size_t size, int protocol)
915 struct ofpbuf buffer;
918 if (!VLOG_IS_DBG_ENABLED()) {
922 ofpbuf_use_const(&buffer, message, size);
923 nlmsg = nlmsg_to_string(&buffer, protocol);
924 VLOG_DBG_RL(&rl, "%s (%s): %s", function, strerror(error), nlmsg);