X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetlink.c;h=cb4e6c1a15a750e3e9b9e254f7ff2aab5a7da2f5;hb=e10dfcf357301f359acea912f3efd652c8208b3e;hp=898f7400a25360b7bdb093dbf2416b638dbfba92;hpb=6f8d45bf7dd4007410f42f7233243cff409d06c6;p=sliver-openvswitch.git diff --git a/lib/netlink.c b/lib/netlink.c index 898f7400a..cb4e6c1a1 100644 --- a/lib/netlink.c +++ b/lib/netlink.c @@ -31,6 +31,7 @@ * derivatives without specific, written prior permission. */ +#include #include "netlink.h" #include #include @@ -40,7 +41,10 @@ #include #include #include -#include "buffer.h" +#include "dynamic-string.h" +#include "netlink-protocol.h" +#include "ofpbuf.h" +#include "timeval.h" #include "util.h" #include "vlog.h" @@ -50,6 +54,14 @@ #ifndef SOL_NETLINK #define SOL_NETLINK 270 #endif + +/* A single (bad) Netlink message can in theory dump out many, many log + * messages, so the burst size is set quite high here to avoid missing useful + * information. Also, at high logging levels we log *all* Netlink messages. */ +static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600); + +static void log_nlmsg(const char *function, int error, + const void *message, size_t size); /* Netlink sockets. */ @@ -93,7 +105,7 @@ nl_sock_create(int protocol, int multicast_group, if (next_seq == 0) { /* Pick initial sequence number. */ - next_seq = getpid() ^ time(0); + next_seq = getpid() ^ time_now(); } *sockp = NULL; @@ -150,7 +162,13 @@ nl_sock_create(int protocol, int multicast_group, goto error_free_pid; } -#ifdef NETLINK_ADD_MEMBERSHIP + /* Older kernel headers failed to define this macro. We want our programs + * to support the newer kernel features even if compiled with older + * headers, so define it ourselves in such a case. */ +#ifndef NETLINK_ADD_MEMBERSHIP +#define NETLINK_ADD_MEMBERSHIP 1 +#endif + /* This method of joining multicast groups is only supported by newish * kernels, but it allows for an arbitrary number of multicast groups. */ if (multicast_group > 32 @@ -160,7 +178,6 @@ nl_sock_create(int protocol, int multicast_group, multicast_group, strerror(errno)); goto error_free_pid; } -#endif *sockp = sock; return 0; @@ -200,15 +217,18 @@ nl_sock_destroy(struct nl_sock *sock) * 'wait' is true, then the send will wait until buffer space is ready; * otherwise, returns EAGAIN if the 'sock' send buffer is full. */ int -nl_sock_send(struct nl_sock *sock, const struct buffer *msg, bool wait) +nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait) { - int retval; + int error; nl_msg_nlmsghdr(msg)->nlmsg_len = msg->size; do { + int retval; retval = send(sock->fd, msg->data, msg->size, wait ? 0 : MSG_DONTWAIT); - } while (retval < 0 && errno == EINTR); - return retval < 0 ? errno : 0; + error = retval < 0 ? errno : 0; + } while (error == EINTR); + log_nlmsg(__func__, error, msg->data, msg->size); + return error; } /* Tries to send the 'n_iov' chunks of data in 'iov' to the kernel on 'sock' as @@ -223,32 +243,37 @@ nl_sock_sendv(struct nl_sock *sock, const struct iovec iov[], size_t n_iov, bool wait) { struct msghdr msg; - int retval; + int error; memset(&msg, 0, sizeof msg); msg.msg_iov = (struct iovec *) iov; msg.msg_iovlen = n_iov; do { - retval = sendmsg(sock->fd, &msg, MSG_DONTWAIT); - } while (retval < 0 && errno == EINTR); - return retval < 0 ? errno : 0; + int retval; + retval = sendmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT); + error = retval < 0 ? errno : 0; + } while (error == EINTR); + if (error != EAGAIN) { + log_nlmsg(__func__, error, iov[0].iov_base, iov[0].iov_len); + } + return error; } /* Tries to receive a netlink message from the kernel on 'sock'. If * successful, stores the received message into '*bufp' and returns 0. The - * caller is responsible for destroying the message with buffer_delete(). On + * caller is responsible for destroying the message with ofpbuf_delete(). On * failure, returns a positive errno value and stores a null pointer into * '*bufp'. * * If 'wait' is true, nl_sock_recv waits for a message to be ready; otherwise, * returns EAGAIN if the 'sock' receive buffer is empty. */ int -nl_sock_recv(struct nl_sock *sock, struct buffer **bufp, bool wait) +nl_sock_recv(struct nl_sock *sock, struct ofpbuf **bufp, bool wait) { uint8_t tmp; ssize_t bufsize = 2048; ssize_t nbytes, nbytes2; - struct buffer *buf; + struct ofpbuf *buf; struct nlmsghdr *nlmsghdr; struct iovec iov; struct msghdr msg = { @@ -261,7 +286,7 @@ nl_sock_recv(struct nl_sock *sock, struct buffer **bufp, bool wait) .msg_flags = 0 }; - buf = buffer_new(bufsize); + buf = ofpbuf_new(bufsize); *bufp = NULL; try_again: @@ -269,19 +294,19 @@ try_again: * yet, so we take a guess at 2048. If we're wrong, we keep trying * and doubling the buffer size each time. */ - nlmsghdr = buffer_put_uninit(buf, bufsize); + nlmsghdr = ofpbuf_put_uninit(buf, bufsize); iov.iov_base = nlmsghdr; iov.iov_len = bufsize; do { nbytes = recvmsg(sock->fd, &msg, (wait ? 0 : MSG_DONTWAIT) | MSG_PEEK); } while (nbytes < 0 && errno == EINTR); if (nbytes < 0) { - buffer_delete(buf); + ofpbuf_delete(buf); return errno; } if (msg.msg_flags & MSG_TRUNC) { bufsize *= 2; - buffer_reinit(buf, bufsize); + ofpbuf_reinit(buf, bufsize); goto try_again; } buf->size = nbytes; @@ -291,24 +316,36 @@ try_again: iov.iov_len = 1; do { nbytes2 = recvmsg(sock->fd, &msg, MSG_DONTWAIT); - if (nbytes2 < 0) { - VLOG_ERR("failed to remove nlmsg from socket: %d\n", errno); - } } while (nbytes2 < 0 && errno == EINTR); - - if (!NLMSG_OK(nlmsghdr, nbytes)) { - VLOG_ERR("received invalid nlmsg (%zd bytes < %d)", - bufsize, NLMSG_HDRLEN); - buffer_delete(buf); + if (nbytes2 < 0) { + if (errno == ENOBUFS) { + /* The kernel is notifying us that a message it tried to send to us + * was dropped. We have to pass this along to the caller in case + * it wants to retry a request. So kill the buffer, which we can + * re-read next time. */ + ofpbuf_delete(buf); + return ENOBUFS; + } else { + VLOG_ERR_RL(&rl, "failed to remove nlmsg from socket: %s\n", + strerror(errno)); + } + } + if (nbytes < sizeof *nlmsghdr + || nlmsghdr->nlmsg_len < sizeof *nlmsghdr + || nlmsghdr->nlmsg_len > nbytes) { + VLOG_ERR_RL(&rl, "received invalid nlmsg (%zd bytes < %d)", + bufsize, NLMSG_HDRLEN); + ofpbuf_delete(buf); return EPROTO; } *bufp = buf; + log_nlmsg(__func__, 0, buf->data, buf->size); return 0; } /* Sends 'request' to the kernel via 'sock' and waits for a response. If * successful, stores the reply into '*replyp' and returns 0. The caller is - * responsible for destroying the reply with buffer_delete(). On failure, + * responsible for destroying the reply with ofpbuf_delete(). On failure, * returns a positive errno value and stores a null pointer into '*replyp'. * * Bare Netlink is an unreliable transport protocol. This function layers @@ -339,11 +376,11 @@ try_again: */ int nl_sock_transact(struct nl_sock *sock, - const struct buffer *request, struct buffer **replyp) + const struct ofpbuf *request, struct ofpbuf **replyp) { uint32_t seq = nl_msg_nlmsghdr(request)->nlmsg_seq; struct nlmsghdr *nlmsghdr; - struct buffer *reply; + struct ofpbuf *reply; int retval; *replyp = NULL; @@ -362,7 +399,7 @@ recv: retval = nl_sock_recv(sock, &reply, true); if (retval) { if (retval == ENOBUFS) { - VLOG_DBG("receive buffer overflow, resending request"); + VLOG_DBG_RL(&rl, "receive buffer overflow, resending request"); goto send; } else { return retval; @@ -370,14 +407,15 @@ recv: } nlmsghdr = nl_msg_nlmsghdr(reply); if (seq != nlmsghdr->nlmsg_seq) { - VLOG_DBG("ignoring seq %"PRIu32" != expected %"PRIu32, - nl_msg_nlmsghdr(reply)->nlmsg_seq, seq); - buffer_delete(reply); + VLOG_DBG_RL(&rl, "ignoring seq %"PRIu32" != expected %"PRIu32, + nl_msg_nlmsghdr(reply)->nlmsg_seq, seq); + ofpbuf_delete(reply); goto recv; } if (nl_msg_nlmsgerr(reply, &retval)) { if (retval) { - VLOG_DBG("received NAK error=%d (%s)", retval, strerror(retval)); + VLOG_DBG_RL(&rl, "received NAK error=%d (%s)", + retval, strerror(retval)); } return retval != EAGAIN ? retval : EPROTO; } @@ -399,9 +437,9 @@ nl_sock_fd(const struct nl_sock *sock) * * 'msg' must be at least as large as a nlmsghdr. */ struct nlmsghdr * -nl_msg_nlmsghdr(const struct buffer *msg) +nl_msg_nlmsghdr(const struct ofpbuf *msg) { - return buffer_at_assert(msg, 0, NLMSG_HDRLEN); + return ofpbuf_at_assert(msg, 0, NLMSG_HDRLEN); } /* Returns the genlmsghdr just past 'msg''s nlmsghdr. @@ -409,9 +447,9 @@ nl_msg_nlmsghdr(const struct buffer *msg) * Returns a null pointer if 'msg' is not large enough to contain an nlmsghdr * and a genlmsghdr. */ struct genlmsghdr * -nl_msg_genlmsghdr(const struct buffer *msg) +nl_msg_genlmsghdr(const struct ofpbuf *msg) { - return buffer_at(msg, NLMSG_HDRLEN, GENL_HDRLEN); + return ofpbuf_at(msg, NLMSG_HDRLEN, GENL_HDRLEN); } /* If 'buffer' is a NLMSG_ERROR message, stores 0 in '*errorp' if it is an ACK @@ -420,14 +458,14 @@ nl_msg_genlmsghdr(const struct buffer *msg) * * 'msg' must be at least as large as a nlmsghdr. */ bool -nl_msg_nlmsgerr(const struct buffer *msg, int *errorp) +nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp) { if (nl_msg_nlmsghdr(msg)->nlmsg_type == NLMSG_ERROR) { - struct nlmsgerr *err = buffer_at(msg, NLMSG_HDRLEN, sizeof *err); + struct nlmsgerr *err = ofpbuf_at(msg, NLMSG_HDRLEN, sizeof *err); int code = EPROTO; if (!err) { - VLOG_ERR("received invalid nlmsgerr (%zd bytes < %zd)", - msg->size, NLMSG_HDRLEN + sizeof *err); + VLOG_ERR_RL(&rl, "received invalid nlmsgerr (%zd bytes < %zd)", + msg->size, NLMSG_HDRLEN + sizeof *err); } else if (err->error <= 0 && err->error > INT_MIN) { code = -err->error; } @@ -440,12 +478,12 @@ nl_msg_nlmsgerr(const struct buffer *msg, int *errorp) } } -/* Ensures that 'b' has room for at least 'size' bytes plus netlink pading at +/* Ensures that 'b' has room for at least 'size' bytes plus netlink padding at * its tail end, reallocating and copying its data if necessary. */ void -nl_msg_reserve(struct buffer *msg, size_t size) +nl_msg_reserve(struct ofpbuf *msg, size_t size) { - buffer_reserve_tailroom(msg, NLMSG_ALIGN(size)); + ofpbuf_prealloc_tailroom(msg, NLMSG_ALIGN(size)); } /* Puts a nlmsghdr at the beginning of 'msg', which must be initially empty. @@ -465,7 +503,7 @@ nl_msg_reserve(struct buffer *msg, size_t size) * nl_msg_put_genlmsghdr is more convenient for composing a Generic Netlink * message. */ void -nl_msg_put_nlmsghdr(struct buffer *msg, struct nl_sock *sock, +nl_msg_put_nlmsghdr(struct ofpbuf *msg, struct nl_sock *sock, size_t expected_payload, uint32_t type, uint32_t flags) { struct nlmsghdr *nlmsghdr; @@ -501,7 +539,7 @@ nl_msg_put_nlmsghdr(struct buffer *msg, struct nl_sock *sock, * nl_msg_put_nlmsghdr should be used to compose Netlink messages that are not * Generic Netlink messages. */ void -nl_msg_put_genlmsghdr(struct buffer *msg, struct nl_sock *sock, +nl_msg_put_genlmsghdr(struct ofpbuf *msg, struct nl_sock *sock, size_t expected_payload, int family, uint32_t flags, uint8_t cmd, uint8_t version) { @@ -520,7 +558,7 @@ nl_msg_put_genlmsghdr(struct buffer *msg, struct nl_sock *sock, * the tail end of 'msg'. Data in 'msg' is reallocated and copied if * necessary. */ void -nl_msg_put(struct buffer *msg, const void *data, size_t size) +nl_msg_put(struct ofpbuf *msg, const void *data, size_t size) { memcpy(nl_msg_put_uninit(msg, size), data, size); } @@ -529,10 +567,10 @@ nl_msg_put(struct buffer *msg, const void *data, size_t size) * end of 'msg', reallocating and copying its data if necessary. Returns a * pointer to the first byte of the new data, which is left uninitialized. */ void * -nl_msg_put_uninit(struct buffer *msg, size_t size) +nl_msg_put_uninit(struct ofpbuf *msg, size_t size) { size_t pad = NLMSG_ALIGN(size) - size; - char *p = buffer_put_uninit(msg, size + pad); + char *p = ofpbuf_put_uninit(msg, size + pad); if (pad) { memset(p + size, 0, pad); } @@ -544,7 +582,7 @@ nl_msg_put_uninit(struct buffer *msg, size_t size) * 'msg', reallocating and copying its data if necessary. Returns a pointer to * the first byte of data in the attribute, which is left uninitialized. */ void * -nl_msg_put_unspec_uninit(struct buffer *msg, uint16_t type, size_t size) +nl_msg_put_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size) { size_t total_size = NLA_HDRLEN + size; struct nlattr* nla = nl_msg_put_uninit(msg, total_size); @@ -559,7 +597,7 @@ nl_msg_put_unspec_uninit(struct buffer *msg, uint16_t type, size_t size) * its data if necessary. Returns a pointer to the first byte of data in the * attribute, which is left uninitialized. */ void -nl_msg_put_unspec(struct buffer *msg, uint16_t type, +nl_msg_put_unspec(struct ofpbuf *msg, uint16_t type, const void *data, size_t size) { memcpy(nl_msg_put_unspec_uninit(msg, type, size), data, size); @@ -569,7 +607,7 @@ nl_msg_put_unspec(struct buffer *msg, uint16_t type, * (Some Netlink protocols use the presence or absence of an attribute as a * Boolean flag.) */ void -nl_msg_put_flag(struct buffer *msg, uint16_t type) +nl_msg_put_flag(struct ofpbuf *msg, uint16_t type) { nl_msg_put_unspec(msg, type, NULL, 0); } @@ -577,7 +615,7 @@ nl_msg_put_flag(struct buffer *msg, uint16_t type) /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value' * to 'msg'. */ void -nl_msg_put_u8(struct buffer *msg, uint16_t type, uint8_t value) +nl_msg_put_u8(struct ofpbuf *msg, uint16_t type, uint8_t value) { nl_msg_put_unspec(msg, type, &value, sizeof value); } @@ -585,7 +623,7 @@ nl_msg_put_u8(struct buffer *msg, uint16_t type, uint8_t value) /* Appends a Netlink attribute of the given 'type' and the given 16-bit 'value' * to 'msg'. */ void -nl_msg_put_u16(struct buffer *msg, uint16_t type, uint16_t value) +nl_msg_put_u16(struct ofpbuf *msg, uint16_t type, uint16_t value) { nl_msg_put_unspec(msg, type, &value, sizeof value); } @@ -593,7 +631,7 @@ nl_msg_put_u16(struct buffer *msg, uint16_t type, uint16_t value) /* Appends a Netlink attribute of the given 'type' and the given 32-bit 'value' * to 'msg'. */ void -nl_msg_put_u32(struct buffer *msg, uint16_t type, uint32_t value) +nl_msg_put_u32(struct ofpbuf *msg, uint16_t type, uint32_t value) { nl_msg_put_unspec(msg, type, &value, sizeof value); } @@ -601,7 +639,7 @@ nl_msg_put_u32(struct buffer *msg, uint16_t type, uint32_t value) /* Appends a Netlink attribute of the given 'type' and the given 64-bit 'value' * to 'msg'. */ void -nl_msg_put_u64(struct buffer *msg, uint16_t type, uint64_t value) +nl_msg_put_u64(struct ofpbuf *msg, uint16_t type, uint64_t value) { nl_msg_put_unspec(msg, type, &value, sizeof value); } @@ -609,7 +647,7 @@ nl_msg_put_u64(struct buffer *msg, uint16_t type, uint64_t value) /* Appends a Netlink attribute of the given 'type' and the given * null-terminated string 'value' to 'msg'. */ void -nl_msg_put_string(struct buffer *msg, uint16_t type, const char *value) +nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value) { nl_msg_put_unspec(msg, type, value, strlen(value) + 1); } @@ -618,8 +656,8 @@ nl_msg_put_string(struct buffer *msg, uint16_t type, const char *value) * netlink message in 'nested_msg' to 'msg'. The nlmsg_len field in * 'nested_msg' is finalized to match 'nested_msg->size'. */ void -nl_msg_put_nested(struct buffer *msg, - uint16_t type, struct buffer *nested_msg) +nl_msg_put_nested(struct ofpbuf *msg, + uint16_t type, struct ofpbuf *nested_msg) { nl_msg_nlmsghdr(nested_msg)->nlmsg_len = nested_msg->size; nl_msg_put_unspec(msg, type, nested_msg->data, nested_msg->size); @@ -725,7 +763,7 @@ static const size_t attr_len_range[][2] = { * with nla_type == i is parsed; a pointer to attribute i is stored in * attrs[i]. Returns true if successful, false on failure. */ bool -nl_policy_parse(const struct buffer *msg, const struct nl_policy policy[], +nl_policy_parse(const struct ofpbuf *msg, const struct nl_policy policy[], struct nlattr *attrs[], size_t n_attrs) { void *p, *tail; @@ -744,31 +782,32 @@ nl_policy_parse(const struct buffer *msg, const struct nl_policy policy[], } } - p = buffer_at(msg, NLMSG_HDRLEN + GENL_HDRLEN, 0); + p = ofpbuf_at(msg, NLMSG_HDRLEN + GENL_HDRLEN, 0); if (p == NULL) { - VLOG_DBG("missing headers in nl_policy_parse"); + VLOG_DBG_RL(&rl, "missing headers in nl_policy_parse"); return false; } - tail = buffer_tail(msg); + tail = ofpbuf_tail(msg); while (p < tail) { - size_t offset = p - msg->data; + size_t offset = (char*)p - (char*)msg->data; struct nlattr *nla = p; size_t len, aligned_len; uint16_t type; /* Make sure its claimed length is plausible. */ if (nla->nla_len < NLA_HDRLEN) { - VLOG_DBG("%zu: attr shorter than NLA_HDRLEN (%"PRIu16")", - offset, nla->nla_len); + VLOG_DBG_RL(&rl, "%zu: attr shorter than NLA_HDRLEN (%"PRIu16")", + offset, nla->nla_len); return false; } len = nla->nla_len - NLA_HDRLEN; aligned_len = NLA_ALIGN(len); - if (aligned_len > tail - p) { - VLOG_DBG("%zu: attr %"PRIu16" aligned data len (%zu) " - "> bytes left (%tu)", - offset, nla->nla_type, aligned_len, tail - p); + if (aligned_len > (char*)tail - (char*)p) { + VLOG_DBG_RL(&rl, "%zu: attr %"PRIu16" aligned data len (%zu) " + "> bytes left (%tu)", + offset, nla->nla_type, aligned_len, + (char*)tail - (char*)p); return false; } @@ -781,19 +820,20 @@ nl_policy_parse(const struct buffer *msg, const struct nl_policy policy[], min_len = p->min_len ? p->min_len : attr_len_range[p->type][0]; max_len = p->max_len ? p->max_len : attr_len_range[p->type][1]; if (len < min_len || len > max_len) { - VLOG_DBG("%zu: attr %"PRIu16" length %zu not in allowed range " - "%zu...%zu", offset, type, len, min_len, max_len); + VLOG_DBG_RL(&rl, "%zu: attr %"PRIu16" length %zu not in " + "allowed range %zu...%zu", + offset, type, len, min_len, max_len); return false; } if (p->type == NL_A_STRING) { if (((char *) nla)[nla->nla_len - 1]) { - VLOG_DBG("%zu: attr %"PRIu16" lacks null terminator", - offset, type); + VLOG_DBG_RL(&rl, "%zu: attr %"PRIu16" lacks null at end", + offset, type); return false; } if (memchr(nla + 1, '\0', len - 1) != NULL) { - VLOG_DBG("%zu: attr %"PRIu16" lies about string length", - offset, type); + VLOG_DBG_RL(&rl, "%zu: attr %"PRIu16" has bad length", + offset, type); return false; } } @@ -805,10 +845,10 @@ nl_policy_parse(const struct buffer *msg, const struct nl_policy policy[], } else { /* Skip attribute type that we don't care about. */ } - p += NLA_ALIGN(nla->nla_len); + p = (char*)p + NLA_ALIGN(nla->nla_len); } if (n_required) { - VLOG_DBG("%zu required attrs missing", n_required); + VLOG_DBG_RL(&rl, "%zu required attrs missing", n_required); return false; } return true; @@ -823,7 +863,7 @@ static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = { static int do_lookup_genl_family(const char *name) { struct nl_sock *sock; - struct buffer request, *reply; + struct ofpbuf request, *reply; struct nlattr *attrs[ARRAY_SIZE(family_policy)]; int retval; @@ -832,12 +872,12 @@ static int do_lookup_genl_family(const char *name) return -retval; } - buffer_init(&request, 0); + ofpbuf_init(&request, 0); nl_msg_put_genlmsghdr(&request, sock, 0, GENL_ID_CTRL, NLM_F_REQUEST, CTRL_CMD_GETFAMILY, 1); nl_msg_put_string(&request, CTRL_ATTR_FAMILY_NAME, name); retval = nl_sock_transact(sock, &request, &reply); - buffer_uninit(&request); + ofpbuf_uninit(&request); if (retval) { nl_sock_destroy(sock); return -retval; @@ -846,7 +886,7 @@ static int do_lookup_genl_family(const char *name) if (!nl_policy_parse(reply, family_policy, attrs, ARRAY_SIZE(family_policy))) { nl_sock_destroy(sock); - buffer_delete(reply); + ofpbuf_delete(reply); return -EPROTO; } @@ -855,7 +895,7 @@ static int do_lookup_genl_family(const char *name) retval = -EPROTO; } nl_sock_destroy(sock); - buffer_delete(reply); + ofpbuf_delete(reply); return retval; } @@ -920,3 +960,114 @@ free_pid(uint32_t pid) assert(avail_sockets[sock / 32] & (1u << (sock % 32))); avail_sockets[sock / 32] &= ~(1u << (sock % 32)); } + +static void +nlmsghdr_to_string(const struct nlmsghdr *h, struct ds *ds) +{ + struct nlmsg_flag { + unsigned int bits; + const char *name; + }; + static const struct nlmsg_flag flags[] = { + { NLM_F_REQUEST, "REQUEST" }, + { NLM_F_MULTI, "MULTI" }, + { NLM_F_ACK, "ACK" }, + { NLM_F_ECHO, "ECHO" }, + { NLM_F_DUMP, "DUMP" }, + { NLM_F_ROOT, "ROOT" }, + { NLM_F_MATCH, "MATCH" }, + { NLM_F_ATOMIC, "ATOMIC" }, + }; + const struct nlmsg_flag *flag; + uint16_t flags_left; + + ds_put_format(ds, "nl(len:%"PRIu32", type=%"PRIu16, + h->nlmsg_len, h->nlmsg_type); + if (h->nlmsg_type == NLMSG_NOOP) { + ds_put_cstr(ds, "(no-op)"); + } else if (h->nlmsg_type == NLMSG_ERROR) { + ds_put_cstr(ds, "(error)"); + } else if (h->nlmsg_type == NLMSG_DONE) { + ds_put_cstr(ds, "(done)"); + } else if (h->nlmsg_type == NLMSG_OVERRUN) { + ds_put_cstr(ds, "(overrun)"); + } else if (h->nlmsg_type < NLMSG_MIN_TYPE) { + ds_put_cstr(ds, "(reserved)"); + } else { + ds_put_cstr(ds, "(family-defined)"); + } + ds_put_format(ds, ", flags=%"PRIx16, h->nlmsg_flags); + flags_left = h->nlmsg_flags; + for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) { + if ((flags_left & flag->bits) == flag->bits) { + ds_put_format(ds, "[%s]", flag->name); + flags_left &= ~flag->bits; + } + } + if (flags_left) { + ds_put_format(ds, "[OTHER:%"PRIx16"]", flags_left); + } + ds_put_format(ds, ", seq=%"PRIx32", pid=%"PRIu32"(%d:%d))", + h->nlmsg_seq, h->nlmsg_pid, + (int) (h->nlmsg_pid & PROCESS_MASK), + (int) (h->nlmsg_pid >> PROCESS_BITS)); +} + +static char * +nlmsg_to_string(const struct ofpbuf *buffer) +{ + struct ds ds = DS_EMPTY_INITIALIZER; + const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN); + if (h) { + nlmsghdr_to_string(h, &ds); + if (h->nlmsg_type == NLMSG_ERROR) { + const struct nlmsgerr *e; + e = ofpbuf_at(buffer, NLMSG_HDRLEN, + NLMSG_ALIGN(sizeof(struct nlmsgerr))); + if (e) { + ds_put_format(&ds, " error(%d", e->error); + if (e->error < 0) { + ds_put_format(&ds, "(%s)", strerror(-e->error)); + } + ds_put_cstr(&ds, ", in-reply-to("); + nlmsghdr_to_string(&e->msg, &ds); + ds_put_cstr(&ds, "))"); + } else { + ds_put_cstr(&ds, " error(truncated)"); + } + } else if (h->nlmsg_type == NLMSG_DONE) { + int *error = ofpbuf_at(buffer, NLMSG_HDRLEN, sizeof *error); + if (error) { + ds_put_format(&ds, " done(%d", *error); + if (*error < 0) { + ds_put_format(&ds, "(%s)", strerror(-*error)); + } + ds_put_cstr(&ds, ")"); + } else { + ds_put_cstr(&ds, " done(truncated)"); + } + } + } else { + ds_put_cstr(&ds, "nl(truncated)"); + } + return ds.string; +} + +static void +log_nlmsg(const char *function, int error, + const void *message, size_t size) +{ + struct ofpbuf buffer; + char *nlmsg; + + if (!VLOG_IS_DBG_ENABLED()) { + return; + } + + buffer.data = (void *) message; + buffer.size = size; + nlmsg = nlmsg_to_string(&buffer); + VLOG_DBG_RL(&rl, "%s (%s): %s", function, strerror(error), nlmsg); + free(nlmsg); +} +