tests: test "load" and "move" actions.
[sliver-openvswitch.git] / lib / netlink-socket.c
index 27811d8..719f658 100644 (file)
@@ -24,6 +24,8 @@
 #include <unistd.h>
 #include "coverage.h"
 #include "dynamic-string.h"
+#include "hash.h"
+#include "hmap.h"
 #include "netlink.h"
 #include "netlink-protocol.h"
 #include "ofpbuf.h"
@@ -36,7 +38,7 @@ VLOG_DEFINE_THIS_MODULE(netlink_socket);
 
 COVERAGE_DEFINE(netlink_overflow);
 COVERAGE_DEFINE(netlink_received);
-COVERAGE_DEFINE(netlink_recv_retry);
+COVERAGE_DEFINE(netlink_recv_jumbo);
 COVERAGE_DEFINE(netlink_send);
 COVERAGE_DEFINE(netlink_sent);
 
@@ -91,6 +93,7 @@ nl_sock_create(int protocol, struct nl_sock **sockp)
     }
     sock->protocol = protocol;
     sock->any_groups = false;
+    sock->dump = NULL;
 
     retval = alloc_pid(&sock->pid);
     if (retval) {
@@ -242,39 +245,6 @@ nl_sock_send(struct nl_sock *sock, const struct ofpbuf *msg, bool wait)
     return nl_sock_send__(sock, msg, 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 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)
-{
-    struct msghdr msg;
-    int error;
-
-    COVERAGE_INC(netlink_send);
-    memset(&msg, 0, sizeof msg);
-    msg.msg_iov = (struct iovec *) iov;
-    msg.msg_iovlen = n_iov;
-    do {
-        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,
-                  sock->protocol);
-        if (!error) {
-            COVERAGE_INC(netlink_sent);
-        }
-    }
-    return error;
-}
-
 /* This stress option is useful for testing that OVS properly tolerates
  * -ENOBUFS on NetLink sockets.  Such errors are unavoidable because they can
  * occur if the kernel cannot temporarily allocate enough GFP_ATOMIC memory to
@@ -287,73 +257,68 @@ STRESS_OPTION(
 static int
 nl_sock_recv__(struct nl_sock *sock, struct ofpbuf **bufp, bool wait)
 {
-    uint8_t tmp;
-    ssize_t bufsize = 2048;
-    ssize_t nbytes, nbytes2;
-    struct ofpbuf *buf;
+    /* We can't accurately predict the size of the data to be received.  Most
+     * received data will fit in a 2 kB buffer, so we allocate that much space.
+     * In case the data is actually bigger than that, we make available enough
+     * additional space to allow Netlink messages to be up to 64 kB long (a
+     * reasonable figure since that's the maximum length of a Netlink
+     * attribute). */
+    enum { MAX_SIZE = 65536 };
+    enum { HEAD_SIZE = 2048 };
+    enum { TAIL_SIZE = MAX_SIZE - HEAD_SIZE };
+
     struct nlmsghdr *nlmsghdr;
-    struct iovec iov;
-    struct msghdr msg = {
-        .msg_name = NULL,
-        .msg_namelen = 0,
-        .msg_iov = &iov,
-        .msg_iovlen = 1,
-        .msg_control = NULL,
-        .msg_controllen = 0,
-        .msg_flags = 0
-    };
+    uint8_t tail[TAIL_SIZE];
+    struct iovec iov[2];
+    struct ofpbuf *buf;
+    struct msghdr msg;
+    ssize_t retval;
 
-    buf = ofpbuf_new(bufsize);
     *bufp = NULL;
 
-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.
-     */
-    nlmsghdr = ofpbuf_put_uninit(buf, bufsize);
-    iov.iov_base = nlmsghdr;
-    iov.iov_len = bufsize;
+    buf = ofpbuf_new(HEAD_SIZE);
+    iov[0].iov_base = buf->data;
+    iov[0].iov_len = HEAD_SIZE;
+    iov[1].iov_base = tail;
+    iov[1].iov_len = TAIL_SIZE;
+
+    memset(&msg, 0, sizeof msg);
+    msg.msg_iov = iov;
+    msg.msg_iovlen = 2;
+
     do {
-        nbytes = recvmsg(sock->fd, &msg, (wait ? 0 : MSG_DONTWAIT) | MSG_PEEK);
-    } while (nbytes < 0 && errno == EINTR);
-    if (nbytes < 0) {
+        retval = recvmsg(sock->fd, &msg, wait ? 0 : MSG_DONTWAIT);
+    } while (retval < 0 && errno == EINTR);
+
+    if (retval < 0) {
+        int error = errno;
+        if (error == ENOBUFS) {
+            /* Socket receive buffer overflow dropped one or more messages that
+             * the kernel tried to send to us. */
+            COVERAGE_INC(netlink_overflow);
+        }
         ofpbuf_delete(buf);
-        return errno;
+        return error;
     }
+
     if (msg.msg_flags & MSG_TRUNC) {
-        COVERAGE_INC(netlink_recv_retry);
-        bufsize *= 2;
-        ofpbuf_reinit(buf, bufsize);
-        goto try_again;
+        VLOG_ERR_RL(&rl, "truncated message (longer than %d bytes)", MAX_SIZE);
+        ofpbuf_delete(buf);
+        return E2BIG;
     }
-    buf->size = nbytes;
 
-    /* We successfully read the message, so recv again to clear the queue */
-    iov.iov_base = &tmp;
-    iov.iov_len = 1;
-    do {
-        nbytes2 = recvmsg(sock->fd, &msg, MSG_DONTWAIT);
-    } while (nbytes2 < 0 && errno == EINTR);
-    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. */
-            COVERAGE_INC(netlink_overflow);
-            ofpbuf_delete(buf);
-            return ENOBUFS;
-        } else {
-            VLOG_ERR_RL(&rl, "failed to remove nlmsg from socket: %s\n",
-                        strerror(errno));
-        }
+    ofpbuf_put_uninit(buf, MIN(retval, HEAD_SIZE));
+    if (retval > HEAD_SIZE) {
+        COVERAGE_INC(netlink_recv_jumbo);
+        ofpbuf_put(buf, tail, retval - HEAD_SIZE);
     }
-    if (nbytes < sizeof *nlmsghdr
+
+    nlmsghdr = buf->data;
+    if (retval < sizeof *nlmsghdr
         || nlmsghdr->nlmsg_len < sizeof *nlmsghdr
-        || nlmsghdr->nlmsg_len > nbytes) {
+        || nlmsghdr->nlmsg_len > retval) {
         VLOG_ERR_RL(&rl, "received invalid nlmsg (%zd bytes < %d)",
-                    bufsize, NLMSG_HDRLEN);
+                    retval, NLMSG_HDRLEN);
         ofpbuf_delete(buf);
         return EPROTO;
     }
@@ -705,10 +670,61 @@ nl_sock_wait(const struct nl_sock *sock, short int events)
 \f
 /* Miscellaneous.  */
 
+struct genl_family {
+    struct hmap_node hmap_node;
+    uint16_t id;
+    char *name;
+};
+
+static struct hmap genl_families = HMAP_INITIALIZER(&genl_families);
+
 static const struct nl_policy family_policy[CTRL_ATTR_MAX + 1] = {
     [CTRL_ATTR_FAMILY_ID] = {.type = NL_A_U16},
 };
 
+static struct genl_family *
+find_genl_family_by_id(uint16_t id)
+{
+    struct genl_family *family;
+
+    HMAP_FOR_EACH_IN_BUCKET (family, hmap_node, hash_int(id, 0),
+                             &genl_families) {
+        if (family->id == id) {
+            return family;
+        }
+    }
+    return NULL;
+}
+
+static void
+define_genl_family(uint16_t id, const char *name)
+{
+    struct genl_family *family = find_genl_family_by_id(id);
+
+    if (family) {
+        if (!strcmp(family->name, name)) {
+            return;
+        }
+        free(family->name);
+    } else {
+        family = xmalloc(sizeof *family);
+        family->id = id;
+        hmap_insert(&genl_families, &family->hmap_node, hash_int(id, 0));
+    }
+    family->name = xstrdup(name);
+}
+
+static const char *
+genl_family_to_name(uint16_t id)
+{
+    if (id == GENL_ID_CTRL) {
+        return "control";
+    } else {
+        struct genl_family *family = find_genl_family_by_id(id);
+        return family ? family->name : "unknown";
+    }
+}
+
 static int do_lookup_genl_family(const char *name)
 {
     struct nl_sock *sock;
@@ -742,9 +758,12 @@ static int do_lookup_genl_family(const char *name)
     retval = nl_attr_get_u16(attrs[CTRL_ATTR_FAMILY_ID]);
     if (retval == 0) {
         retval = -EPROTO;
+    } else {
+        define_genl_family(retval, name);
     }
     nl_sock_destroy(sock);
     ofpbuf_delete(reply);
+
     return retval;
 }
 
@@ -811,7 +830,7 @@ free_pid(uint32_t pid)
 }
 \f
 static void
-nlmsghdr_to_string(const struct nlmsghdr *h, struct ds *ds)
+nlmsghdr_to_string(const struct nlmsghdr *h, int protocol, struct ds *ds)
 {
     struct nlmsg_flag {
         unsigned int bits;
@@ -842,6 +861,8 @@ nlmsghdr_to_string(const struct nlmsghdr *h, struct ds *ds)
         ds_put_cstr(ds, "(overrun)");
     } else if (h->nlmsg_type < NLMSG_MIN_TYPE) {
         ds_put_cstr(ds, "(reserved)");
+    } else if (protocol == NETLINK_GENERIC) {
+        ds_put_format(ds, "(%s)", genl_family_to_name(h->nlmsg_type));
     } else {
         ds_put_cstr(ds, "(family-defined)");
     }
@@ -868,7 +889,7 @@ nlmsg_to_string(const struct ofpbuf *buffer, int protocol)
     struct ds ds = DS_EMPTY_INITIALIZER;
     const struct nlmsghdr *h = ofpbuf_at(buffer, 0, NLMSG_HDRLEN);
     if (h) {
-        nlmsghdr_to_string(h, &ds);
+        nlmsghdr_to_string(h, protocol, &ds);
         if (h->nlmsg_type == NLMSG_ERROR) {
             const struct nlmsgerr *e;
             e = ofpbuf_at(buffer, NLMSG_HDRLEN,
@@ -879,7 +900,7 @@ nlmsg_to_string(const struct ofpbuf *buffer, int protocol)
                     ds_put_format(&ds, "(%s)", strerror(-e->error));
                 }
                 ds_put_cstr(&ds, ", in-reply-to(");
-                nlmsghdr_to_string(&e->msg, &ds);
+                nlmsghdr_to_string(&e->msg, protocol, &ds);
                 ds_put_cstr(&ds, "))");
             } else {
                 ds_put_cstr(&ds, " error(truncated)");