Catalli's threaded switch
[sliver-openvswitch.git] / lib / netlink.c
index c4bf31e..4e83747 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009 Nicira Networks.
+ * Copyright (c) 2008, 2009, 2010 Nicira Networks.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,9 +31,9 @@
 #include "poll-loop.h"
 #include "timeval.h"
 #include "util.h"
-
 #include "vlog.h"
-#define THIS_MODULE VLM_netlink
+
+VLOG_DEFINE_THIS_MODULE(netlink)
 
 /* Linux header file confusion causes this to be undefined. */
 #ifndef SOL_NETLINK
@@ -57,7 +57,7 @@ struct nl_sock
 };
 
 /* Next nlmsghdr sequence number.
- * 
+ *
  * This implementation uses sequence numbers that are unique process-wide, to
  * avoid a hypothetical race: send request, close socket, open new socket that
  * reuses the old socket's PID value, send request on new socket, receive reply
@@ -185,7 +185,7 @@ error:
 
 /* Destroys netlink socket 'sock'. */
 void
-nl_sock_destroy(struct nl_sock *sock) 
+nl_sock_destroy(struct nl_sock *sock)
 {
     if (sock) {
         close(sock->fd);
@@ -195,18 +195,20 @@ nl_sock_destroy(struct nl_sock *sock)
 }
 
 /* Tries to send 'msg', which must contain a Netlink message, to the kernel on
- * 'sock'.  nlmsg_len in 'msg' will be finalized to match msg->size before the
- * message is sent.
+ * 'sock'.  nlmsg_len in 'msg' will be finalized to match msg->size, and
+ * nlmsg_pid will be set to 'sock''s pid, before the message is sent.
  *
  * Returns 0 if successful, otherwise a positive errno value.  If
  * '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 ofpbuf *msg, bool wait) 
+nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
 {
+    struct nlmsghdr *nlmsg = nl_msg_nlmsghdr(msg);
     int error;
 
-    nl_msg_nlmsghdr(msg)->nlmsg_len = msg->size;
+    nlmsg->nlmsg_len = msg->size;
+    nlmsg->nlmsg_pid = sock->pid;
     do {
         int retval;
         retval = send(sock->fd, msg->data, msg->size, wait ? 0 : MSG_DONTWAIT);
@@ -221,14 +223,14 @@ nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
 
 /* Tries to send the 'n_iov' chunks of data in 'iov' to the kernel on 'sock' as
  * a single Netlink message.  (The message must be fully formed and not require
- * finalization of its nlmsg_len field.)
+ * finalization of its nlmsg_len or nlmsg_pid fields.)
  *
  * Returns 0 if successful, otherwise a positive errno value.  If '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_sendv(struct nl_sock *sock, const struct iovec iov[], size_t n_iov,
-              bool wait) 
+              bool wait)
 {
     struct msghdr msg;
     int error;
@@ -260,7 +262,7 @@ nl_sock_sendv(struct nl_sock *sock, const struct iovec iov[], size_t n_iov,
  * 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 ofpbuf **bufp, bool wait) 
+nl_sock_recv(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
 {
     uint8_t tmp;
     ssize_t bufsize = 2048;
@@ -284,13 +286,13 @@ nl_sock_recv(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
 try_again:
     /* Attempt to read the message.  We don't know the size of the data
      * yet, so we take a guess at 2048.  If we're wrong, we keep trying
-     * and doubling the buffer size each time. 
+     * and doubling the buffer size each time.
      */
     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); 
+        nbytes = recvmsg(sock->fd, &msg, (wait ? 0 : MSG_DONTWAIT) | MSG_PEEK);
     } while (nbytes < 0 && errno == EINTR);
     if (nbytes < 0) {
         ofpbuf_delete(buf);
@@ -339,19 +341,26 @@ try_again:
 }
 
 /* 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 ofpbuf_delete().  On failure,
- * returns a positive errno value and stores a null pointer into '*replyp'.
+ * successful, returns 0.  On failure, returns a positive errno value.
+ *
+ * If 'replyp' is nonnull, then on success '*replyp' is set to the kernel's
+ * reply, which the caller is responsible for freeing with ofpbuf_delete(), and
+ * on failure '*replyp' is set to NULL.  If 'replyp' is null, then the kernel's
+ * reply, if any, is discarded.
+ *
+ * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
+ * be set to 'sock''s pid, before the message is sent.  NLM_F_ACK will be set
+ * in nlmsg_flags.
  *
  * The caller is responsible for destroying 'request'.
  *
  * Bare Netlink is an unreliable transport protocol.  This function layers
  * reliable delivery and reply semantics on top of bare Netlink.
- * 
+ *
  * In Netlink, sending a request to the kernel is reliable enough, because the
  * kernel will tell us if the message cannot be queued (and we will in that
  * case put it on the transmit queue and wait until it can be delivered).
- * 
+ *
  * Receiving the reply is the real problem: if the socket buffer is full when
  * the kernel tries to send the reply, the reply will be dropped.  However, the
  * kernel sets a flag that a reply has been dropped.  The next call to recv
@@ -373,19 +382,21 @@ try_again:
  */
 int
 nl_sock_transact(struct nl_sock *sock,
-                 const struct ofpbuf *request, struct ofpbuf **replyp) 
+                 const struct ofpbuf *request, struct ofpbuf **replyp)
 {
     uint32_t seq = nl_msg_nlmsghdr(request)->nlmsg_seq;
     struct nlmsghdr *nlmsghdr;
     struct ofpbuf *reply;
     int retval;
 
-    *replyp = NULL;
+    if (replyp) {
+        *replyp = NULL;
+    }
 
     /* Ensure that we get a reply even if this message doesn't ordinarily call
      * for one. */
     nl_msg_nlmsghdr(request)->nlmsg_flags |= NLM_F_ACK;
-    
+
 send:
     retval = nl_sock_send(sock, request, true);
     if (retval) {
@@ -410,7 +421,14 @@ recv:
         ofpbuf_delete(reply);
         goto recv;
     }
-    if (nl_msg_nlmsgerr(reply, &retval)) {
+
+    /* If the reply is an error, discard the reply and return the error code.
+     *
+     * Except: if the reply is just an acknowledgement (error code of 0), and
+     * the caller is interested in the reply (replyp != NULL), pass the reply
+     * up to the caller.  Otherwise the caller will get a return value of 0
+     * and null '*replyp', which makes unwary callers likely to segfault. */
+    if (nl_msg_nlmsgerr(reply, &retval) && (retval || !replyp)) {
         ofpbuf_delete(reply);
         if (retval) {
             VLOG_DBG_RL(&rl, "received NAK error=%d (%s)",
@@ -419,10 +437,151 @@ recv:
         return retval != EAGAIN ? retval : EPROTO;
     }
 
-    *replyp = reply;
+    if (replyp) {
+        *replyp = reply;
+    } else {
+        ofpbuf_delete(reply);
+    }
     return 0;
 }
 
+/* Starts a Netlink "dump" operation, by sending 'request' to the kernel via
+ * 'sock', and initializes 'dump' to reflect the state of the operation.
+ *
+ * nlmsg_len in 'msg' will be finalized to match msg->size, and nlmsg_pid will
+ * be set to 'sock''s pid, before the message is sent.  NLM_F_DUMP and
+ * NLM_F_ACK will be set in nlmsg_flags.
+ *
+ * The properties of Netlink make dump operations reliable as long as all of
+ * the following are true:
+ *
+ *   - At most a single dump is in progress at a time on a given nl_sock.
+ *
+ *   - The nl_sock is not subscribed to any multicast groups.
+ *
+ *   - The nl_sock is not used to send any other messages before the dump
+ *     operation is complete.
+ *
+ * This function provides no status indication.  An error status for the entire
+ * dump operation is provided when it is completed by calling nl_dump_done().
+ *
+ * The caller is responsible for destroying 'request'.  The caller must not
+ * close 'sock' before it completes the dump operation (by calling
+ * nl_dump_done()).
+ */
+void
+nl_dump_start(struct nl_dump *dump,
+              struct nl_sock *sock, const struct ofpbuf *request)
+{
+    struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(request);
+    nlmsghdr->nlmsg_flags |= NLM_F_DUMP | NLM_F_ACK;
+    dump->seq = nlmsghdr->nlmsg_seq;
+    dump->sock = sock;
+    dump->status = nl_sock_send(sock, request, true);
+    dump->buffer = NULL;
+}
+
+/* Helper function for nl_dump_next(). */
+static int
+nl_dump_recv(struct nl_dump *dump, struct ofpbuf **bufferp)
+{
+    struct nlmsghdr *nlmsghdr;
+    struct ofpbuf *buffer;
+    int retval;
+
+    retval = nl_sock_recv(dump->sock, bufferp, true);
+    if (retval) {
+        return retval == EINTR ? EAGAIN : retval;
+    }
+    buffer = *bufferp;
+
+    nlmsghdr = nl_msg_nlmsghdr(buffer);
+    if (dump->seq != nlmsghdr->nlmsg_seq) {
+        VLOG_DBG_RL(&rl, "ignoring seq %"PRIu32" != expected %"PRIu32,
+                    nlmsghdr->nlmsg_seq, dump->seq);
+        return EAGAIN;
+    }
+
+    if (nl_msg_nlmsgerr(buffer, &retval)) {
+        VLOG_INFO_RL(&rl, "netlink dump request error (%s)",
+                     strerror(retval));
+        return retval && retval != EAGAIN ? retval : EPROTO;
+    }
+
+    return 0;
+}
+
+/* Attempts to retrieve another reply from 'dump', which must have been
+ * initialized with nl_dump_start().
+ *
+ * If successful, returns true and points 'reply->data' and 'reply->size' to
+ * the message that was retrieved.  The caller must not modify 'reply' (because
+ * it points into the middle of a larger buffer).
+ *
+ * On failure, returns false and sets 'reply->data' to NULL and 'reply->size'
+ * to 0.  Failure might indicate an actual error or merely the end of replies.
+ * An error status for the entire dump operation is provided when it is
+ * completed by calling nl_dump_done().
+ */
+bool
+nl_dump_next(struct nl_dump *dump, struct ofpbuf *reply)
+{
+    struct nlmsghdr *nlmsghdr;
+
+    reply->data = NULL;
+    reply->size = 0;
+    if (dump->status) {
+        return false;
+    }
+
+    if (dump->buffer && !dump->buffer->size) {
+        ofpbuf_delete(dump->buffer);
+        dump->buffer = NULL;
+    }
+    while (!dump->buffer) {
+        int retval = nl_dump_recv(dump, &dump->buffer);
+        if (retval) {
+            ofpbuf_delete(dump->buffer);
+            dump->buffer = NULL;
+            if (retval != EAGAIN) {
+                dump->status = retval;
+                return false;
+            }
+        }
+    }
+
+    nlmsghdr = nl_msg_next(dump->buffer, reply);
+    if (!nlmsghdr) {
+        VLOG_WARN_RL(&rl, "netlink dump reply contains message fragment");
+        dump->status = EPROTO;
+        return false;
+    } else if (nlmsghdr->nlmsg_type == NLMSG_DONE) {
+        dump->status = EOF;
+        return false;
+    }
+
+    return true;
+}
+
+/* Completes Netlink dump operation 'dump', which must have been initialized
+ * with nl_dump_start().  Returns 0 if the dump operation was error-free,
+ * otherwise a positive errno value describing the problem. */
+int
+nl_dump_done(struct nl_dump *dump)
+{
+    /* Drain any remaining messages that the client didn't read.  Otherwise the
+     * kernel will continue to queue them up and waste buffer space. */
+    while (!dump->status) {
+        struct ofpbuf reply;
+        if (!nl_dump_next(dump, &reply)) {
+            assert(dump->status);
+        }
+    }
+
+    ofpbuf_delete(dump->buffer);
+    return dump->status == EOF ? 0 : dump->status;
+}
+
 /* Causes poll_block() to wake up when any of the specified 'events' (which is
  * a OR'd combination of POLLIN, POLLOUT, etc.) occur on 'sock'. */
 void
@@ -437,7 +596,7 @@ nl_sock_wait(const struct nl_sock *sock, short int events)
  *
  * 'msg' must be at least as large as a nlmsghdr. */
 struct nlmsghdr *
-nl_msg_nlmsghdr(const struct ofpbuf *msg) 
+nl_msg_nlmsghdr(const struct ofpbuf *msg)
 {
     return ofpbuf_at_assert(msg, 0, NLMSG_HDRLEN);
 }
@@ -447,7 +606,7 @@ nl_msg_nlmsghdr(const struct ofpbuf *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 ofpbuf *msg) 
+nl_msg_genlmsghdr(const struct ofpbuf *msg)
 {
     return ofpbuf_at(msg, NLMSG_HDRLEN, GENL_HDRLEN);
 }
@@ -458,7 +617,7 @@ nl_msg_genlmsghdr(const struct ofpbuf *msg)
  *
  * 'msg' must be at least as large as a nlmsghdr. */
 bool
-nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp) 
+nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp)
 {
     if (nl_msg_nlmsghdr(msg)->nlmsg_type == NLMSG_ERROR) {
         struct nlmsgerr *err = ofpbuf_at(msg, NLMSG_HDRLEN, sizeof *err);
@@ -481,14 +640,13 @@ nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp)
 /* 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 ofpbuf *msg, size_t size) 
+nl_msg_reserve(struct ofpbuf *msg, size_t size)
 {
     ofpbuf_prealloc_tailroom(msg, NLMSG_ALIGN(size));
 }
 
 /* Puts a nlmsghdr at the beginning of 'msg', which must be initially empty.
- * Uses the given 'type' and 'flags'.  'sock' is used to obtain a PID and
- * sequence number for proper routing of replies.  'expected_payload' should be
+ * Uses the given 'type' and 'flags'.  'expected_payload' should be
  * an estimate of the number of payload bytes to be supplied; if the size of
  * the payload is unknown a value of 0 is acceptable.
  *
@@ -500,11 +658,14 @@ nl_msg_reserve(struct ofpbuf *msg, size_t size)
  * is often NLM_F_REQUEST indicating that a request is being made, commonly
  * or'd with NLM_F_ACK to request an acknowledgement.
  *
- * nl_msg_put_genlmsghdr is more convenient for composing a Generic Netlink
+ * Sets the new nlmsghdr's nlmsg_pid field to 0 for now.  nl_sock_send() will
+ * fill it in just before sending the message.
+ *
+ * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink
  * message. */
 void
-nl_msg_put_nlmsghdr(struct ofpbuf *msg, struct nl_sock *sock,
-                    size_t expected_payload, uint32_t type, uint32_t flags) 
+nl_msg_put_nlmsghdr(struct ofpbuf *msg,
+                    size_t expected_payload, uint32_t type, uint32_t flags)
 {
     struct nlmsghdr *nlmsghdr;
 
@@ -516,14 +677,13 @@ nl_msg_put_nlmsghdr(struct ofpbuf *msg, struct nl_sock *sock,
     nlmsghdr->nlmsg_type = type;
     nlmsghdr->nlmsg_flags = flags;
     nlmsghdr->nlmsg_seq = ++next_seq;
-    nlmsghdr->nlmsg_pid = sock->pid;
+    nlmsghdr->nlmsg_pid = 0;
 }
 
 /* Puts a nlmsghdr and genlmsghdr at the beginning of 'msg', which must be
- * initially empty.  'sock' is used to obtain a PID and sequence number for
- * proper routing of replies.  'expected_payload' should be an estimate of the
- * number of payload bytes to be supplied; if the size of the payload is
- * unknown a value of 0 is acceptable.
+ * initially empty.  'expected_payload' should be an estimate of the number of
+ * payload bytes to be supplied; if the size of the payload is unknown a value
+ * of 0 is acceptable.
  *
  * 'family' is the family number obtained via nl_lookup_genl_family().
  *
@@ -536,17 +696,18 @@ nl_msg_put_nlmsghdr(struct ofpbuf *msg, struct nl_sock *sock,
  *
  * 'version' is a version number specific to the family and command (often 1).
  *
- * nl_msg_put_nlmsghdr should be used to compose Netlink messages that are not
- * Generic Netlink messages. */
+ * Sets the new nlmsghdr's nlmsg_pid field to 0 for now.  nl_sock_send() will
+ * fill it in just before sending the message.
+ *
+ * nl_msg_put_nlmsghdr() should be used to compose Netlink messages that are
+ * not Generic Netlink messages. */
 void
-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)
+nl_msg_put_genlmsghdr(struct ofpbuf *msg, size_t expected_payload,
+                      int family, uint32_t flags, uint8_t cmd, uint8_t version)
 {
     struct genlmsghdr *genlmsghdr;
 
-    nl_msg_put_nlmsghdr(msg, sock, GENL_HDRLEN + expected_payload,
-                        family, flags);
+    nl_msg_put_nlmsghdr(msg, GENL_HDRLEN + expected_payload, family, flags);
     assert(msg->size == NLMSG_HDRLEN);
     genlmsghdr = nl_msg_put_uninit(msg, GENL_HDRLEN);
     genlmsghdr->cmd = cmd;
@@ -558,7 +719,7 @@ nl_msg_put_genlmsghdr(struct ofpbuf *msg, struct nl_sock *sock,
  * the tail end of 'msg'.  Data in 'msg' is reallocated and copied if
  * necessary. */
 void
-nl_msg_put(struct ofpbuf *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);
 }
@@ -567,12 +728,12 @@ nl_msg_put(struct ofpbuf *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 ofpbuf *msg, size_t size) 
+nl_msg_put_uninit(struct ofpbuf *msg, size_t size)
 {
     size_t pad = NLMSG_ALIGN(size) - size;
     char *p = ofpbuf_put_uninit(msg, size + pad);
     if (pad) {
-        memset(p + size, 0, pad); 
+        memset(p + size, 0, pad);
     }
     return p;
 }
@@ -582,7 +743,7 @@ nl_msg_put_uninit(struct ofpbuf *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 ofpbuf *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);
@@ -598,7 +759,7 @@ nl_msg_put_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
  * attribute, which is left uninitialized. */
 void
 nl_msg_put_unspec(struct ofpbuf *msg, uint16_t type,
-                  const void *data, size_t size) 
+                  const void *data, size_t size)
 {
     memcpy(nl_msg_put_unspec_uninit(msg, type, size), data, size);
 }
@@ -607,7 +768,7 @@ nl_msg_put_unspec(struct ofpbuf *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 ofpbuf *msg, uint16_t type) 
+nl_msg_put_flag(struct ofpbuf *msg, uint16_t type)
 {
     nl_msg_put_unspec(msg, type, NULL, 0);
 }
@@ -615,7 +776,7 @@ nl_msg_put_flag(struct ofpbuf *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 ofpbuf *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);
 }
@@ -652,20 +813,69 @@ nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value)
     nl_msg_put_unspec(msg, type, value, strlen(value) + 1);
 }
 
-/* Appends a Netlink attribute of the given 'type' and the given buffered
- * netlink message in 'nested_msg' to 'msg'.  The nlmsg_len field in
- * 'nested_msg' is finalized to match 'nested_msg->size'. */
+/* Adds the header for nested Netlink attributes to 'msg', with the specified
+ * 'type', and returns the header's offset within 'msg'.  The caller should add
+ * the content for the nested Netlink attribute to 'msg' (e.g. using the other
+ * nl_msg_*() functions), and then pass the returned offset to
+ * nl_msg_end_nested() to finish up the nested attributes. */
+size_t
+nl_msg_start_nested(struct ofpbuf *msg, uint16_t type)
+{
+    size_t offset = msg->size;
+    nl_msg_put_unspec(msg, type, NULL, 0);
+    return offset;
+}
+
+/* Finalizes a nested Netlink attribute in 'msg'.  'offset' should be the value
+ * returned by nl_msg_start_nested(). */
+void
+nl_msg_end_nested(struct ofpbuf *msg, size_t offset)
+{
+    struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr);
+    attr->nla_len = msg->size - offset;
+}
+
+/* Appends a nested Netlink attribute of the given 'type', with the 'size'
+ * bytes of content starting at 'data', to 'msg'. */
 void
 nl_msg_put_nested(struct ofpbuf *msg,
-                  uint16_t type, struct ofpbuf *nested_msg)
+                  uint16_t type, const void *data, size_t size)
 {
-    nl_msg_nlmsghdr(nested_msg)->nlmsg_len = nested_msg->size;
-    nl_msg_put_unspec(msg, type, nested_msg->data, nested_msg->size);
+    size_t offset = nl_msg_start_nested(msg, type);
+    nl_msg_put(msg, data, size);
+    nl_msg_end_nested(msg, offset);
 }
 
+/* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
+ * payload off 'buffer', stores header and payload in 'msg->data' and
+ * 'msg->size', and returns a pointer to the header.
+ *
+ * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
+ * is invalid, returns NULL without modifying 'buffer'. */
+struct nlmsghdr *
+nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg)
+{
+    if (buffer->size >= sizeof(struct nlmsghdr)) {
+        struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer);
+        size_t len = nlmsghdr->nlmsg_len;
+        if (len >= sizeof *nlmsghdr && len <= buffer->size) {
+            msg->data = nlmsghdr;
+            msg->size = len;
+            ofpbuf_pull(buffer, len);
+            return nlmsghdr;
+        }
+    }
+
+    msg->data = NULL;
+    msg->size = 0;
+    return NULL;
+}
+\f
+/* Attributes. */
+
 /* Returns the first byte in the payload of attribute 'nla'. */
 const void *
-nl_attr_get(const struct nlattr *nla) 
+nl_attr_get(const struct nlattr *nla)
 {
     assert(nla->nla_len >= NLA_HDRLEN);
     return nla + 1;
@@ -673,7 +883,7 @@ nl_attr_get(const struct nlattr *nla)
 
 /* Returns the number of bytes in the payload of attribute 'nla'. */
 size_t
-nl_attr_get_size(const struct nlattr *nla) 
+nl_attr_get_size(const struct nlattr *nla)
 {
     assert(nla->nla_len >= NLA_HDRLEN);
     return nla->nla_len - NLA_HDRLEN;
@@ -682,7 +892,7 @@ nl_attr_get_size(const struct nlattr *nla)
 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
  * first byte of the payload. */
 const void *
-nl_attr_get_unspec(const struct nlattr *nla, size_t size) 
+nl_attr_get_unspec(const struct nlattr *nla, size_t size)
 {
     assert(nla->nla_len >= NLA_HDRLEN + size);
     return nla + 1;
@@ -691,7 +901,7 @@ nl_attr_get_unspec(const struct nlattr *nla, size_t size)
 /* Returns true if 'nla' is nonnull.  (Some Netlink protocols use the presence
  * or absence of an attribute as a Boolean flag.) */
 bool
-nl_attr_get_flag(const struct nlattr *nla) 
+nl_attr_get_flag(const struct nlattr *nla)
 {
     return nla != NULL;
 }
@@ -703,7 +913,7 @@ nl_attr_get_flag(const struct nlattr *nla)
  *
  * Asserts that 'nla''s payload is at least 1 byte long. */
 uint8_t
-nl_attr_get_u8(const struct nlattr *nla) 
+nl_attr_get_u8(const struct nlattr *nla)
 {
     return NL_ATTR_GET_AS(nla, uint8_t);
 }
@@ -712,7 +922,7 @@ nl_attr_get_u8(const struct nlattr *nla)
  *
  * Asserts that 'nla''s payload is at least 2 bytes long. */
 uint16_t
-nl_attr_get_u16(const struct nlattr *nla) 
+nl_attr_get_u16(const struct nlattr *nla)
 {
     return NL_ATTR_GET_AS(nla, uint16_t);
 }
@@ -721,7 +931,7 @@ nl_attr_get_u16(const struct nlattr *nla)
  *
  * Asserts that 'nla''s payload is at least 4 bytes long. */
 uint32_t
-nl_attr_get_u32(const struct nlattr *nla) 
+nl_attr_get_u32(const struct nlattr *nla)
 {
     return NL_ATTR_GET_AS(nla, uint32_t);
 }
@@ -730,7 +940,7 @@ nl_attr_get_u32(const struct nlattr *nla)
  *
  * Asserts that 'nla''s payload is at least 8 bytes long. */
 uint64_t
-nl_attr_get_u64(const struct nlattr *nla) 
+nl_attr_get_u64(const struct nlattr *nla)
 {
     return NL_ATTR_GET_AS(nla, uint64_t);
 }
@@ -739,13 +949,22 @@ nl_attr_get_u64(const struct nlattr *nla)
  *
  * Asserts that 'nla''s payload contains a null-terminated string. */
 const char *
-nl_attr_get_string(const struct nlattr *nla) 
+nl_attr_get_string(const struct nlattr *nla)
 {
     assert(nla->nla_len > NLA_HDRLEN);
     assert(memchr(nl_attr_get(nla), '\0', nla->nla_len - NLA_HDRLEN) != NULL);
     return nl_attr_get(nla);
 }
 
+/* Initializes 'nested' to the payload of 'nla'.  Doesn't initialize every
+ * field in 'nested', but enough to poke around with it in a read-only way. */
+void
+nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested)
+{
+    nested->data = (void *) nl_attr_get(nla);
+    nested->size = nl_attr_get_size(nla);
+}
+
 /* Default minimum and maximum payload sizes for each type of attribute. */
 static const size_t attr_len_range[][2] = {
     [0 ... N_NL_ATTR_TYPES - 1] = { 0, SIZE_MAX },
@@ -755,7 +974,7 @@ static const size_t attr_len_range[][2] = {
     [NL_A_U64] = { 8, 8 },
     [NL_A_STRING] = { 1, SIZE_MAX },
     [NL_A_FLAG] = { 0, SIZE_MAX },
-    [NL_A_NESTED] = { NLMSG_HDRLEN, SIZE_MAX },
+    [NL_A_NESTED] = { 0, SIZE_MAX },
 };
 
 /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
@@ -857,14 +1076,28 @@ nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset,
     }
     return true;
 }
+
+/* Parses the Netlink attributes within 'nla'.  'policy[i]', for 0 <= i <
+ * n_attrs, specifies how the attribute 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_parse_nested(const struct nlattr *nla, const struct nl_policy policy[],
+                struct nlattr *attrs[], size_t n_attrs)
+{
+    struct ofpbuf buf;
+
+    nl_attr_get_nested(nla, &buf);
+    return nl_policy_parse(&buf, 0, policy, attrs, n_attrs);
+}
 \f
 /* Miscellaneous.  */
 
-static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = { 
+static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = {
     [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16},
 };
 
-static int do_lookup_genl_family(const char *name) 
+static int do_lookup_genl_family(const char *name)
 {
     struct nl_sock *sock;
     struct ofpbuf request, *reply;
@@ -877,7 +1110,7 @@ static int do_lookup_genl_family(const char *name)
     }
 
     ofpbuf_init(&request, 0);
-    nl_msg_put_genlmsghdr(&request, sock, 0, GENL_ID_CTRL, NLM_F_REQUEST,
+    nl_msg_put_genlmsghdr(&request, 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);
@@ -908,7 +1141,7 @@ static int do_lookup_genl_family(const char *name)
  * may use '*number' as the family number.  On failure, returns a positive
  * errno value and '*number' caches the errno value. */
 int
-nl_lookup_genl_family(const char *name, int *number) 
+nl_lookup_genl_family(const char *name, int *number)
 {
     if (*number == 0) {
         *number = do_lookup_genl_family(name);
@@ -972,7 +1205,7 @@ nlmsghdr_to_string(const struct nlmsghdr *h, struct ds *ds)
         unsigned int bits;
         const char *name;
     };
-    static const struct nlmsg_flag flags[] = { 
+    static const struct nlmsg_flag flags[] = {
         { NLM_F_REQUEST, "REQUEST" },
         { NLM_F_MULTI, "MULTI" },
         { NLM_F_ACK, "ACK" },