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"
30 #include "netlink-protocol.h"
32 #include "poll-loop.h"
33 #include "socket-util.h"
37 VLOG_DEFINE_THIS_MODULE(netlink_socket);
39 COVERAGE_DEFINE(netlink_overflow);
40 COVERAGE_DEFINE(netlink_received);
41 COVERAGE_DEFINE(netlink_recv_jumbo);
42 COVERAGE_DEFINE(netlink_send);
43 COVERAGE_DEFINE(netlink_sent);
45 /* Linux header file confusion causes this to be undefined. */
47 #define SOL_NETLINK 270
50 /* A single (bad) Netlink message can in theory dump out many, many log
51 * messages, so the burst size is set quite high here to avoid missing useful
52 * information. Also, at high logging levels we log *all* Netlink messages. */
53 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600);
55 static void log_nlmsg(const char *function, int error,
56 const void *message, size_t size, int protocol);
58 /* Netlink sockets. */
69 static int alloc_pid(uint32_t *);
70 static void free_pid(uint32_t);
71 static int nl_sock_cow__(struct nl_sock *);
73 /* Creates a new netlink socket for the given netlink 'protocol'
74 * (NETLINK_ROUTE, NETLINK_GENERIC, ...). Returns 0 and sets '*sockp' to the
75 * new socket if successful, otherwise returns a positive errno value. */
77 nl_sock_create(int protocol, struct nl_sock **sockp)
80 struct sockaddr_nl local, remote;
84 sock = malloc(sizeof *sock);
89 sock->fd = socket(AF_NETLINK, SOCK_RAW, protocol);
91 VLOG_ERR("fcntl: %s", strerror(errno));
94 sock->protocol = protocol;
95 sock->any_groups = false;
98 retval = alloc_pid(&sock->pid);
103 /* Bind local address as our selected pid. */
104 memset(&local, 0, sizeof local);
105 local.nl_family = AF_NETLINK;
106 local.nl_pid = sock->pid;
107 if (bind(sock->fd, (struct sockaddr *) &local, sizeof local) < 0) {
108 VLOG_ERR("bind(%"PRIu32"): %s", sock->pid, strerror(errno));
112 /* Bind remote address as the kernel (pid 0). */
113 memset(&remote, 0, sizeof remote);
114 remote.nl_family = AF_NETLINK;
116 if (connect(sock->fd, (struct sockaddr *) &remote, sizeof remote) < 0) {
117 VLOG_ERR("connect(0): %s", strerror(errno));
140 /* Creates a new netlink socket for the same protocol as 'src'. Returns 0 and
141 * sets '*sockp' to the new socket if successful, otherwise returns a positive
144 nl_sock_clone(const struct nl_sock *src, struct nl_sock **sockp)
146 return nl_sock_create(src->protocol, sockp);
149 /* Destroys netlink socket 'sock'. */
151 nl_sock_destroy(struct nl_sock *sock)
164 /* Tries to add 'sock' as a listener for 'multicast_group'. Returns 0 if
165 * successful, otherwise a positive errno value.
167 * Multicast group numbers are always positive.
169 * It is not an error to attempt to join a multicast group to which a socket
170 * already belongs. */
172 nl_sock_join_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
174 int error = nl_sock_cow__(sock);
178 if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
179 &multicast_group, sizeof multicast_group) < 0) {
180 VLOG_WARN("could not join multicast group %u (%s)",
181 multicast_group, strerror(errno));
184 sock->any_groups = true;
188 /* Tries to make 'sock' stop listening to 'multicast_group'. Returns 0 if
189 * successful, otherwise a positive errno value.
191 * Multicast group numbers are always positive.
193 * It is not an error to attempt to leave a multicast group to which a socket
196 * On success, reading from 'sock' will still return any messages that were
197 * received on 'multicast_group' before the group was left. */
199 nl_sock_leave_mcgroup(struct nl_sock *sock, unsigned int multicast_group)
202 if (setsockopt(sock->fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
203 &multicast_group, sizeof multicast_group) < 0) {
204 VLOG_WARN("could not leave multicast group %u (%s)",
205 multicast_group, strerror(errno));
212 nl_sock_send__(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
214 struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg);
217 nlmsg->nlmsg_len = msg->size;
218 nlmsg->nlmsg_pid = sock->pid;
221 retval = send(sock->fd, msg->data, msg->size, wait ? 0 : MSG_DONTWAIT);
222 error = retval < 0 ? errno : 0;
223 } while (error == EINTR);
224 log_nlmsg(__func__, error, msg->data, msg->size, sock->protocol);
226 COVERAGE_INC(netlink_sent);
231 /* Tries to send 'msg', which must contain a Netlink message, to the kernel on
232 * 'sock'. nlmsg_len in 'msg' will be finalized to match msg->size, and
233 * nlmsg_pid will be set to 'sock''s pid, before the message is sent.
235 * Returns 0 if successful, otherwise a positive errno value. If
236 * 'wait' is true, then the send will wait until buffer space is ready;
237 * otherwise, returns EAGAIN if the 'sock' send buffer is full. */
239 nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
241 int error = nl_sock_cow__(sock);
245 return nl_sock_send__(sock, msg, wait);
248 /* This stress option is useful for testing that OVS properly tolerates
249 * -ENOBUFS on NetLink sockets. Such errors are unavoidable because they can
250 * occur if the kernel cannot temporarily allocate enough GFP_ATOMIC memory to
251 * reply to a request. They can also occur if messages arrive on a multicast
252 * channel faster than OVS can process them. */
254 netlink_overflow, "simulate netlink socket receive buffer overflow",
258 nl_sock_recv__(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
260 /* We can't accurately predict the size of the data to be received. Most
261 * received data will fit in a 2 kB buffer, so we allocate that much space.
262 * In case the data is actually bigger than that, we make available enough
263 * additional space to allow Netlink messages to be up to 64 kB long (a
264 * reasonable figure since that's the maximum length of a Netlink
266 enum { MAX_SIZE = 65536 };
267 enum { HEAD_SIZE = 2048 };
268 enum { TAIL_SIZE = MAX_SIZE - HEAD_SIZE };
270 struct nlmsghdr *nlmsghdr;
271 uint8_t tail[TAIL_SIZE];
279 buf = ofpbuf_new(HEAD_SIZE);
280 iov[0].iov_base = buf->data;
281 iov[0].iov_len = HEAD_SIZE;
282 iov[1].iov_base = tail;
283 iov[1].iov_len = TAIL_SIZE;
285 memset(&msg, 0, sizeof msg);
290 retval = recvmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT);
291 } while (retval < 0 && errno == EINTR);
295 if (error == ENOBUFS) {
296 /* Socket receive buffer overflow dropped one or more messages that
297 * the kernel tried to send to us. */
298 COVERAGE_INC(netlink_overflow);
304 if (msg.msg_flags & MSG_TRUNC) {
305 VLOG_ERR_RL(&rl, "truncated message (longer than %d bytes)", MAX_SIZE);
310 ofpbuf_put_uninit(buf, MIN(retval, HEAD_SIZE));
311 if (retval > HEAD_SIZE) {
312 COVERAGE_INC(netlink_recv_jumbo);
313 ofpbuf_put(buf, tail, retval - HEAD_SIZE);
316 nlmsghdr = buf->data;
317 if (retval < sizeof *nlmsghdr
318 || nlmsghdr->nlmsg_len < sizeof *nlmsghdr
319 || nlmsghdr->nlmsg_len > retval) {
320 VLOG_ERR_RL(&rl, "received invalid nlmsg (%zd bytes < %d)",
321 retval, NLMSG_HDRLEN);
326 if (STRESS(netlink_overflow)) {
332 log_nlmsg(__func__, 0, buf->data, buf->size, sock->protocol);
333 COVERAGE_INC(netlink_received);
338 /* Tries to receive a netlink message from the kernel on 'sock'. If
339 * successful, stores the received message into '*bufp' and returns 0. The
340 * caller is responsible for destroying the message with ofpbuf_delete(). On
341 * failure, returns a positive errno value and stores a null pointer into
344 * If 'wait' is true, nl_sock_recv waits for a message to be ready; otherwise,
345 * returns EAGAIN if the 'sock' receive buffer is empty. */
347 nl_sock_recv(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
349 int error = nl_sock_cow__(sock);
353 return nl_sock_recv__(sock, bufp, wait);
356 /* Sends 'request' to the kernel via 'sock' and waits for a response. If
357 * successful, returns 0. On failure, returns a positive errno value.
359 * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's
360 * reply, which the caller is responsible for freeing with ofpbuf_delete(), and
361 * on failure '*replyp' is set to NULL. If 'replyp' is null, then the kernel's
362 * reply, if any, is discarded.
364 * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
365 * be set to 'sock''s pid, before the message is sent. NLM_F_ACK will be set
368 * The caller is responsible for destroying 'request'.
370 * Bare Netlink is an unreliable transport protocol. This function layers
371 * reliable delivery and reply semantics on top of bare Netlink.
373 * In Netlink, sending a request to the kernel is reliable enough, because the
374 * kernel will tell us if the message cannot be queued (and we will in that
375 * case put it on the transmit queue and wait until it can be delivered).
377 * Receiving the reply is the real problem: if the socket buffer is full when
378 * the kernel tries to send the reply, the reply will be dropped. However, the
379 * kernel sets a flag that a reply has been dropped. The next call to recv
380 * then returns ENOBUFS. We can then re-send the request.
384 * 1. Netlink depends on sequence numbers to match up requests and
385 * replies. The sender of a request supplies a sequence number, and
386 * the reply echos back that sequence number.
388 * This is fine, but (1) some kernel netlink implementations are
389 * broken, in that they fail to echo sequence numbers and (2) this
390 * function will drop packets with non-matching sequence numbers, so
391 * that only a single request can be usefully transacted at a time.
393 * 2. Resending the request causes it to be re-executed, so the request
394 * needs to be idempotent.
397 nl_sock_transact(struct nl_sock *sock,
398 const struct ofpbuf *request, struct ofpbuf **replyp)
400 uint32_t seq = nl_msg_nlmsghdr(request)->nlmsg_seq;
401 struct nlmsghdr *nlmsghdr;
402 struct ofpbuf *reply;
409 /* Ensure that we get a reply even if this message doesn't ordinarily call
411 nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_ACK;
414 retval = nl_sock_send(sock, request, true);
420 retval = nl_sock_recv(sock, &reply, true);
422 if (retval == ENOBUFS) {
423 COVERAGE_INC(netlink_overflow);
424 VLOG_DBG_RL(&rl, "receive buffer overflow, resending request");
430 nlmsghdr = nl_msg_nlmsghdr(reply);
431 if (seq != nlmsghdr->nlmsg_seq) {
432 VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32,
433 nl_msg_nlmsghdr(reply)->nlmsg_seq, seq);
434 ofpbuf_delete(reply);
438 /* If the reply is an error, discard the reply and return the error code.
440 * Except: if the reply is just an acknowledgement (error code of 0), and
441 * the caller is interested in the reply (replyp != NULL), pass the reply
442 * up to the caller. Otherwise the caller will get a return value of 0
443 * and null '*replyp', which makes unwary callers likely to segfault. */
444 if (nl_msg_nlmsgerr(reply, &retval) && (retval || !replyp)) {
445 ofpbuf_delete(reply);
447 VLOG_DBG_RL(&rl, "received NAK error=%d (%s)",
448 retval, strerror(retval));
450 return retval != EAGAIN ? retval : EPROTO;
456 ofpbuf_delete(reply);
461 /* Drain all the messages currently in 'sock''s receive queue. */
463 nl_sock_drain(struct nl_sock *sock)
465 int error = nl_sock_cow__(sock);
469 return drain_rcvbuf(sock->fd);
472 /* The client is attempting some operation on 'sock'. If 'sock' has an ongoing
473 * dump operation, then replace 'sock''s fd with a new socket and hand 'sock''s
474 * old fd over to the dump. */
476 nl_sock_cow__(struct nl_sock *sock)
478 struct nl_sock *copy;
487 error = nl_sock_clone(sock, ©);
497 sock->pid = copy->pid;
500 sock->dump->sock = copy;
506 /* Starts a Netlink "dump" operation, by sending 'request' to the kernel via
507 * 'sock', and initializes 'dump' to reflect the state of the operation.
509 * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
510 * be set to 'sock''s pid, before the message is sent. NLM_F_DUMP and
511 * NLM_F_ACK will be set in nlmsg_flags.
513 * This Netlink socket library is designed to ensure that the dump is reliable
514 * and that it will not interfere with other operations on 'sock', including
515 * destroying or sending and receiving messages on 'sock'. One corner case is
518 * - If 'sock' has been used to send a request (e.g. with nl_sock_send())
519 * whose response has not yet been received (e.g. with nl_sock_recv()).
520 * This is unusual: usually nl_sock_transact() is used to send a message
521 * and receive its reply all in one go.
523 * This function provides no status indication. An error status for the entire
524 * dump operation is provided when it is completed by calling nl_dump_done().
526 * The caller is responsible for destroying 'request'.
528 * The new 'dump' is independent of 'sock'. 'sock' and 'dump' may be destroyed
532 nl_dump_start(struct nl_dump *dump,
533 struct nl_sock *sock, const struct ofpbuf *request)
535 struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(request);
536 nlmsghdr->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK;
537 dump->seq = nlmsghdr->nlmsg_seq;
539 if (sock->any_groups || sock->dump) {
540 /* 'sock' might belong to some multicast group, or it already has an
541 * onoging dump. Clone the socket to avoid possibly intermixing
542 * multicast messages or previous dump results with our results. */
543 dump->status = nl_sock_clone(sock, &dump->sock);
552 dump->status = nl_sock_send__(sock, request, true);
555 /* Helper function for nl_dump_next(). */
557 nl_dump_recv(struct nl_dump *dump, struct ofpbuf **bufferp)
559 struct nlmsghdr *nlmsghdr;
560 struct ofpbuf *buffer;
563 retval = nl_sock_recv__(dump->sock, bufferp, true);
565 return retval == EINTR ? EAGAIN : retval;
569 nlmsghdr = nl_msg_nlmsghdr(buffer);
570 if (dump->seq != nlmsghdr->nlmsg_seq) {
571 VLOG_DBG_RL(&rl, "ignoring seq %#"PRIx32" != expected %#"PRIx32,
572 nlmsghdr->nlmsg_seq, dump->seq);
576 if (nl_msg_nlmsgerr(buffer, &retval)) {
577 VLOG_INFO_RL(&rl, "netlink dump request error (%s)",
579 return retval && retval != EAGAIN ? retval : EPROTO;
585 /* Attempts to retrieve another reply from 'dump', which must have been
586 * initialized with nl_dump_start().
588 * If successful, returns true and points 'reply->data' and 'reply->size' to
589 * the message that was retrieved. The caller must not modify 'reply' (because
590 * it points into the middle of a larger buffer).
592 * On failure, returns false and sets 'reply->data' to NULL and 'reply->size'
593 * to 0. Failure might indicate an actual error or merely the end of replies.
594 * An error status for the entire dump operation is provided when it is
595 * completed by calling nl_dump_done().
598 nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply)
600 struct nlmsghdr *nlmsghdr;
608 if (dump->buffer && !dump->buffer->size) {
609 ofpbuf_delete(dump->buffer);
612 while (!dump->buffer) {
613 int retval = nl_dump_recv(dump, &dump->buffer);
615 ofpbuf_delete(dump->buffer);
617 if (retval != EAGAIN) {
618 dump->status = retval;
624 nlmsghdr = nl_msg_next(dump->buffer, reply);
626 VLOG_WARN_RL(&rl, "netlink dump reply contains message fragment");
627 dump->status = EPROTO;
629 } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) {
637 /* Completes Netlink dump operation 'dump', which must have been initialized
638 * with nl_dump_start(). Returns 0 if the dump operation was error-free,
639 * otherwise a positive errno value describing the problem. */
641 nl_dump_done(struct nl_dump *dump)
643 /* Drain any remaining messages that the client didn't read. Otherwise the
644 * kernel will continue to queue them up and waste buffer space. */
645 while (!dump->status) {
647 if (!nl_dump_next(dump, &reply)) {
648 assert(dump->status);
653 if (dump->sock->dump) {
654 dump->sock->dump = NULL;
656 nl_sock_destroy(dump->sock);
659 ofpbuf_delete(dump->buffer);
660 return dump->status == EOF ? 0 : dump->status;
663 /* Causes poll_block() to wake up when any of the specified 'events' (which is
664 * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'. */
666 nl_sock_wait(const struct nl_sock *sock, short int events)
668 poll_fd_wait(sock->fd, events);
674 struct hmap_node hmap_node;
679 static struct hmap genl_families = HMAP_INITIALIZER(&genl_families);
681 static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = {
682 [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16},
683 [CTRL_ATTR_MCAST_GROUPS] = {.type = NL_A_NESTED},
686 static struct genl_family *
687 find_genl_family_by_id(uint16_t id)
689 struct genl_family *family;
691 HMAP_FOR_EACH_IN_BUCKET (family, hmap_node, hash_int(id, 0),
693 if (family->id == id) {
701 define_genl_family(uint16_t id, const char *name)
703 struct genl_family *family = find_genl_family_by_id(id);
706 if (!strcmp(family->name, name)) {
711 family = xmalloc(sizeof *family);
713 hmap_insert(&genl_families, &family->hmap_node, hash_int(id, 0));
715 family->name = xstrdup(name);
719 genl_family_to_name(uint16_t id)
721 if (id == GENL_ID_CTRL) {
724 struct genl_family *family = find_genl_family_by_id(id);
725 return family ? family->name : "unknown";
730 do_lookup_genl_family(const char *name, struct nlattr **attrs,
731 struct ofpbuf **replyp)
733 struct nl_sock *sock;
734 struct ofpbuf request, *reply;
738 error = nl_sock_create(NETLINK_GENERIC, &sock);
743 ofpbuf_init(&request, 0);
744 nl_msg_put_genlmsghdr(&request, 0, GENL_ID_CTRL, NLM_F_REQUEST,
745 CTRL_CMD_GETFAMILY, 1);
746 nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name);
747 error = nl_sock_transact(sock, &request, &reply);
748 ofpbuf_uninit(&request);
750 nl_sock_destroy(sock);
754 if (!nl_policy_parse(reply, NLMSG_HDRLEN + GENL_HDRLEN,
755 family_policy, attrs, ARRAY_SIZE(family_policy))
756 || nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]) == 0) {
757 nl_sock_destroy(sock);
758 ofpbuf_delete(reply);
762 nl_sock_destroy(sock);
767 /* Finds the multicast group called 'group_name' in genl family 'family_name'.
768 * When successful, writes its result to 'multicast_group' and returns 0.
769 * Otherwise, clears 'multicast_group' and returns a positive error code. */
771 nl_lookup_genl_mcgroup(const char *family_name, const char *group_name,
772 unsigned int *multicast_group)
774 struct nlattr *family_attrs[ARRAY_SIZE(family_policy)];
775 struct ofpbuf all_mcs;
776 struct ofpbuf *reply;
781 *multicast_group = 0;
782 error = do_lookup_genl_family(family_name, family_attrs, &reply);
787 nl_attr_get_nested(family_attrs[CTRL_ATTR_MCAST_GROUPS], &all_mcs);
788 NL_ATTR_FOR_EACH (mc, left, all_mcs.data, all_mcs.size) {
789 static const struct nl_policy mc_policy[] = {
790 [CTRL_ATTR_MCAST_GRP_ID] = {.type = NL_A_U32},
791 [CTRL_ATTR_MCAST_GRP_NAME] = {.type = NL_A_STRING},
794 struct nlattr *mc_attrs[ARRAY_SIZE(mc_policy)];
797 if (!nl_parse_nested(mc, mc_policy, mc_attrs, ARRAY_SIZE(mc_policy))) {
802 mc_name = nl_attr_get_string(mc_attrs[CTRL_ATTR_MCAST_GRP_NAME]);
803 if (!strcmp(group_name, mc_name)) {
805 nl_attr_get_u32(mc_attrs[CTRL_ATTR_MCAST_GRP_ID]);
813 ofpbuf_delete(reply);
817 /* If '*number' is 0, translates the given Generic Netlink family 'name' to a
818 * number and stores it in '*number'. If successful, returns 0 and the caller
819 * may use '*number' as the family number. On failure, returns a positive
820 * errno value and '*number' caches the errno value. */
822 nl_lookup_genl_family(const char *name, int *number)
825 struct nlattr *attrs[ARRAY_SIZE(family_policy)];
826 struct ofpbuf *reply;
829 error = do_lookup_genl_family(name, attrs, &reply);
831 *number = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]);
832 define_genl_family(*number, name);
836 ofpbuf_delete(reply);
838 assert(*number != 0);
840 return *number > 0 ? 0 : -*number;
845 * Every Netlink socket must be bound to a unique 32-bit PID. By convention,
846 * programs that have a single Netlink socket use their Unix process ID as PID,
847 * and programs with multiple Netlink sockets add a unique per-socket
848 * identifier in the bits above the Unix process ID.
850 * The kernel has Netlink PID 0.
853 /* Parameters for how many bits in the PID should come from the Unix process ID
854 * and how many unique per-socket. */
855 #define SOCKET_BITS 10
856 #define MAX_SOCKETS (1u << SOCKET_BITS)
858 #define PROCESS_BITS (32 - SOCKET_BITS)
859 #define MAX_PROCESSES (1u << PROCESS_BITS)
860 #define PROCESS_MASK ((uint32_t) (MAX_PROCESSES - 1))
862 /* Bit vector of unused socket identifiers. */
863 static uint32_t avail_sockets[ROUND_UP(MAX_SOCKETS, 32)];
865 /* Allocates and returns a new Netlink PID. */
867 alloc_pid(uint32_t *pid)
871 for (i = 0; i < MAX_SOCKETS; i++) {
872 if ((avail_sockets[i / 32] & (1u << (i % 32))) == 0) {
873 avail_sockets[i / 32] |= 1u << (i % 32);
874 *pid = (getpid() & PROCESS_MASK) | (i << PROCESS_BITS);
878 VLOG_ERR("netlink pid space exhausted");
882 /* Makes the specified 'pid' available for reuse. */
884 free_pid(uint32_t pid)
886 int sock = pid >> PROCESS_BITS;
887 assert(avail_sockets[sock / 32] & (1u << (sock % 32)));
888 avail_sockets[sock / 32] &= ~(1u << (sock % 32));
892 nlmsghdr_to_string(const struct nlmsghdr *h, int protocol, struct ds *ds)
898 static const struct nlmsg_flag flags[] = {
899 { NLM_F_REQUEST, "REQUEST" },
900 { NLM_F_MULTI, "MULTI" },
901 { NLM_F_ACK, "ACK" },
902 { NLM_F_ECHO, "ECHO" },
903 { NLM_F_DUMP, "DUMP" },
904 { NLM_F_ROOT, "ROOT" },
905 { NLM_F_MATCH, "MATCH" },
906 { NLM_F_ATOMIC, "ATOMIC" },
908 const struct nlmsg_flag *flag;
911 ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16,
912 h->nlmsg_len, h->nlmsg_type);
913 if (h->nlmsg_type == NLMSG_NOOP) {
914 ds_put_cstr(ds, "(no-op)");
915 } else if (h->nlmsg_type == NLMSG_ERROR) {
916 ds_put_cstr(ds, "(error)");
917 } else if (h->nlmsg_type == NLMSG_DONE) {
918 ds_put_cstr(ds, "(done)");
919 } else if (h->nlmsg_type == NLMSG_OVERRUN) {
920 ds_put_cstr(ds, "(overrun)");
921 } else if (h->nlmsg_type < NLMSG_MIN_TYPE) {
922 ds_put_cstr(ds, "(reserved)");
923 } else if (protocol == NETLINK_GENERIC) {
924 ds_put_format(ds, "(%s)", genl_family_to_name(h->nlmsg_type));
926 ds_put_cstr(ds, "(family-defined)");
928 ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags);
929 flags_left = h->nlmsg_flags;
930 for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
931 if ((flags_left & flag->bits) == flag->bits) {
932 ds_put_format(ds, "[%s]", flag->name);
933 flags_left &= ~flag->bits;
937 ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left);
939 ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32"(%d:%d))",
940 h->nlmsg_seq, h->nlmsg_pid,
941 (int) (h->nlmsg_pid & PROCESS_MASK),
942 (int) (h->nlmsg_pid >> PROCESS_BITS));
946 nlmsg_to_string(const struct ofpbuf *buffer, int protocol)
948 struct ds ds = DS_EMPTY_INITIALIZER;
949 const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN);
951 nlmsghdr_to_string(h, protocol, &ds);
952 if (h->nlmsg_type == NLMSG_ERROR) {
953 const struct nlmsgerr *e;
954 e = ofpbuf_at(buffer, NLMSG_HDRLEN,
955 NLMSG_ALIGN(sizeof(struct nlmsgerr)));
957 ds_put_format(&ds, " error(%d", e->error);
959 ds_put_format(&ds, "(%s)", strerror(-e->error));
961 ds_put_cstr(&ds, ", in-reply-to(");
962 nlmsghdr_to_string(&e->msg, protocol, &ds);
963 ds_put_cstr(&ds, "))");
965 ds_put_cstr(&ds, " error(truncated)");
967 } else if (h->nlmsg_type == NLMSG_DONE) {
968 int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error);
970 ds_put_format(&ds, " done(%d", *error);
972 ds_put_format(&ds, "(%s)", strerror(-*error));
974 ds_put_cstr(&ds, ")");
976 ds_put_cstr(&ds, " done(truncated)");
978 } else if (protocol == NETLINK_GENERIC) {
979 struct genlmsghdr *genl = nl_msg_genlmsghdr(buffer);
981 ds_put_format(&ds, ",genl(cmd=%"PRIu8",version=%"PRIu8")",
982 genl->cmd, genl->version);
986 ds_put_cstr(&ds, "nl(truncated)");
992 log_nlmsg(const char *function, int error,
993 const void *message, size_t size, int protocol)
995 struct ofpbuf buffer;
998 if (!VLOG_IS_DBG_ENABLED()) {
1002 ofpbuf_use_const(&buffer, message, size);
1003 nlmsg = nlmsg_to_string(&buffer, protocol);
1004 VLOG_DBG_RL(&rl, "%s (%s): %s", function, strerror(error), nlmsg);