+ 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;
+ }
+