dpif-linux: Make dpif_linux_port_query_by_name() query only one datapath.
[sliver-openvswitch.git] / lib / dpif-linux.c
index 7d0628c..818d86f 100644 (file)
 #include <linux/pkt_sched.h>
 #include <linux/rtnetlink.h>
 #include <linux/sockios.h>
+#include <poll.h>
 #include <stdlib.h>
+#include <strings.h>
+#include <sys/epoll.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
@@ -63,6 +66,7 @@ BUILD_ASSERT_DECL(IS_POW2(LRU_MAX_PORTS));
 
 enum { N_UPCALL_SOCKS = 16 };
 BUILD_ASSERT_DECL(IS_POW2(N_UPCALL_SOCKS));
+BUILD_ASSERT_DECL(N_UPCALL_SOCKS <= 32); /* We use a 32-bit word as a mask. */
 
 /* This ethtool flag was introduced in Linux 2.6.24, so it might be
  * missing if we have old headers. */
@@ -135,8 +139,9 @@ struct dpif_linux {
 
     /* Upcall messages. */
     struct nl_sock *upcall_socks[N_UPCALL_SOCKS];
-    int last_read_upcall;
-    unsigned int listen_mask;
+    uint32_t ready_mask;        /* 1-bit for each sock with unread messages. */
+    unsigned int listen_mask;   /* Mask of DPIF_UC_* bits. */
+    int epoll_fd;               /* epoll fd that includes the upcall socks. */
 
     /* Change notification. */
     struct sset changed_ports;  /* Ports that have changed. */
@@ -271,6 +276,7 @@ open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
     dpif = xzalloc(sizeof *dpif);
     dpif->port_notifier = nln_notifier_create(nln, dpif_linux_port_changed,
                                               dpif);
+    dpif->epoll_fd = -1;
 
     dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
               dp->dp_ifindex, dp->dp_ifindex);
@@ -291,6 +297,10 @@ destroy_upcall_socks(struct dpif_linux *dpif)
 {
     int i;
 
+    if (dpif->epoll_fd >= 0) {
+        close(dpif->epoll_fd);
+        dpif->epoll_fd = -1;
+    }
     for (i = 0; i < N_UPCALL_SOCKS; i++) {
         nl_sock_destroy(dpif->upcall_socks[i]);
         dpif->upcall_socks[i] = NULL;
@@ -447,9 +457,15 @@ dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no,
 
     error = dpif_linux_vport_transact(&request, &reply, &buf);
     if (!error) {
-        dpif_port->name = xstrdup(reply.name);
-        dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
-        dpif_port->port_no = reply.port_no;
+        if (reply.dp_ifindex != request.dp_ifindex) {
+            /* A query by name reported that 'port_name' is in some datapath
+             * other than 'dpif', but the caller wants to know about 'dpif'. */
+            error = ENODEV;
+        } else {
+            dpif_port->name = xstrdup(reply.name);
+            dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply));
+            dpif_port->port_no = reply.port_no;
+        }
         ofpbuf_delete(buf);
     }
     return error;
@@ -1002,13 +1018,31 @@ dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
         int i;
         int error;
 
+        dpif->epoll_fd = epoll_create(N_UPCALL_SOCKS);
+        if (dpif->epoll_fd < 0) {
+            return errno;
+        }
+
         for (i = 0; i < N_UPCALL_SOCKS; i++) {
+            struct epoll_event event;
+
             error = nl_sock_create(NETLINK_GENERIC, &dpif->upcall_socks[i]);
             if (error) {
                 destroy_upcall_socks(dpif);
                 return error;
             }
+
+            event.events = EPOLLIN;
+            event.data.u32 = i;
+            if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD,
+                          nl_sock_fd(dpif->upcall_socks[i]), &event) < 0) {
+                error = errno;
+                destroy_upcall_socks(dpif);
+                return error;
+            }
         }
+
+        dpif->ready_mask = 0;
     }
 
     dpif->listen_mask = listen_mask;
@@ -1088,48 +1122,62 @@ static int
 dpif_linux_recv(struct dpif *dpif_, struct dpif_upcall *upcall)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
-    int i;
     int read_tries = 0;
 
     if (!dpif->listen_mask) {
        return EAGAIN;
     }
 
-    for (i = 0; i < N_UPCALL_SOCKS; i++) {
-        struct nl_sock *upcall_sock;
-        dpif->last_read_upcall = (dpif->last_read_upcall + 1) &
-                                 (N_UPCALL_SOCKS - 1);
-        upcall_sock = dpif->upcall_socks[dpif->last_read_upcall];
+    if (!dpif->ready_mask) {
+        struct epoll_event events[N_UPCALL_SOCKS];
+        int retval;
+        int i;
 
-        if (nl_sock_woke(upcall_sock)) {
-            int dp_ifindex;
+        do {
+            retval = epoll_wait(dpif->epoll_fd, events, N_UPCALL_SOCKS, 0);
+        } while (retval < 0 && errno == EINTR);
+        if (retval < 0) {
+            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+            VLOG_WARN_RL(&rl, "epoll_wait failed (%s)", strerror(errno));
+        }
 
-            for (;;) {
-                struct ofpbuf *buf;
-                int error;
+        for (i = 0; i < retval; i++) {
+            dpif->ready_mask |= 1u << events[i].data.u32;
+        }
+    }
 
-                if (++read_tries > 50) {
-                    return EAGAIN;
-                }
+    while (dpif->ready_mask) {
+        int indx = ffs(dpif->ready_mask) - 1;
+        struct nl_sock *upcall_sock = dpif->upcall_socks[indx];
 
-                error = nl_sock_recv(upcall_sock, &buf, false);
-                if (error == EAGAIN) {
-                    break;
-                } else if (error) {
-                    return error;
-                }
+        dpif->ready_mask &= ~(1u << indx);
 
-                error = parse_odp_packet(buf, upcall, &dp_ifindex);
-                if (!error
-                    && dp_ifindex == dpif->dp_ifindex
-                    && dpif->listen_mask & (1u << upcall->type)) {
-                    return 0;
-                }
+        for (;;) {
+            struct ofpbuf *buf;
+            int dp_ifindex;
+            int error;
 
-                ofpbuf_delete(buf);
-                if (error) {
-                    return error;
-                }
+            if (++read_tries > 50) {
+                return EAGAIN;
+            }
+
+            error = nl_sock_recv(upcall_sock, &buf, false);
+            if (error == EAGAIN) {
+                break;
+            } else if (error) {
+                return error;
+            }
+
+            error = parse_odp_packet(buf, upcall, &dp_ifindex);
+            if (!error
+                && dp_ifindex == dpif->dp_ifindex
+                && dpif->listen_mask & (1u << upcall->type)) {
+                return 0;
+            }
+
+            ofpbuf_delete(buf);
+            if (error) {
+                return error;
             }
         }
     }
@@ -1141,15 +1189,12 @@ static void
 dpif_linux_recv_wait(struct dpif *dpif_)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
-    int i;
 
     if (!dpif->listen_mask) {
        return;
     }
 
-    for (i = 0; i < N_UPCALL_SOCKS; i++) {
-        nl_sock_wait(dpif->upcall_socks[i], POLLIN);
-    }
+    poll_fd_wait(dpif->epoll_fd, POLLIN);
 }
 
 static void
@@ -1329,9 +1374,7 @@ dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
         [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
         [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NL_A_U32 },
-        [OVS_VPORT_ATTR_STATS] = { .type = NL_A_UNSPEC,
-                                   .min_len = sizeof(struct ovs_vport_stats),
-                                   .max_len = sizeof(struct ovs_vport_stats),
+        [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
                                    .optional = true },
         [OVS_VPORT_ATTR_ADDRESS] = { .type = NL_A_UNSPEC,
                                      .min_len = ETH_ADDR_LEN,
@@ -1504,9 +1547,7 @@ dpif_linux_dp_from_ofpbuf(struct dpif_linux_dp *dp, const struct ofpbuf *buf)
 {
     static const struct nl_policy ovs_datapath_policy[] = {
         [OVS_DP_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
-        [OVS_DP_ATTR_STATS] = { .type = NL_A_UNSPEC,
-                                .min_len = sizeof(struct ovs_dp_stats),
-                                .max_len = sizeof(struct ovs_dp_stats),
+        [OVS_DP_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_dp_stats),
                                 .optional = true },
     };
 
@@ -1651,9 +1692,7 @@ dpif_linux_flow_from_ofpbuf(struct dpif_linux_flow *flow,
     static const struct nl_policy ovs_flow_policy[] = {
         [OVS_FLOW_ATTR_KEY] = { .type = NL_A_NESTED },
         [OVS_FLOW_ATTR_ACTIONS] = { .type = NL_A_NESTED, .optional = true },
-        [OVS_FLOW_ATTR_STATS] = { .type = NL_A_UNSPEC,
-                                  .min_len = sizeof(struct ovs_flow_stats),
-                                  .max_len = sizeof(struct ovs_flow_stats),
+        [OVS_FLOW_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_flow_stats),
                                   .optional = true },
         [OVS_FLOW_ATTR_TCP_FLAGS] = { .type = NL_A_U8, .optional = true },
         [OVS_FLOW_ATTR_USED] = { .type = NL_A_U64, .optional = true },