[ovs-dev] [PATCH 4/5] dpif-linux: Implement the API functions to allow multiple handler threads read upcall.

Alex Wang alexw at nicira.com
Thu Feb 27 19:44:25 UTC 2014


Signed-off-by: Alex Wang <alexw at nicira.com>

---
major changes since RFC:
- for cache access efficiency, in 'recv()' index the handlers
  array first and then port channels array.
---
 lib/dpif-linux.c |  551 +++++++++++++++++++++++++++++++++++-------------------
 lib/dpif-linux.h |    3 +-
 2 files changed, 359 insertions(+), 195 deletions(-)

diff --git a/lib/dpif-linux.c b/lib/dpif-linux.c
index b6229d0..325c0c0 100644
--- a/lib/dpif-linux.c
+++ b/lib/dpif-linux.c
@@ -36,6 +36,7 @@
 #include "dpif-provider.h"
 #include "dynamic-string.h"
 #include "flow.h"
+#include "fat-rwlock.h"
 #include "netdev.h"
 #include "netdev-linux.h"
 #include "netdev-vport.h"
@@ -132,7 +133,19 @@ struct dpif_channel {
     long long int last_poll;    /* Last time this channel was polled. */
 };
 
-static void report_loss(struct dpif *, struct dpif_channel *);
+static void report_loss(struct dpif *, struct dpif_channel *, uint32_t ch_idx,
+                        uint32_t handler_id);
+
+struct dpif_epoll {
+    struct epoll_event *epoll_events;
+    int epoll_fd;               /* epoll fd that includes channel socks. */
+    int n_events;               /* Num events returned by epoll_wait(). */
+    int event_offset;           /* Offset into 'epoll_events'. */
+};
+
+struct dpif_handler {
+    struct dpif_channel *channels;/* Array of channels for each handler. */
+};
 
 /* Datapath interface for the openvswitch Linux kernel module. */
 struct dpif_linux {
@@ -140,13 +153,11 @@ struct dpif_linux {
     int dp_ifindex;
 
     /* Upcall messages. */
-    struct ovs_mutex upcall_lock;
-    int uc_array_size;          /* Size of 'channels' and 'epoll_events'. */
-    struct dpif_channel *channels;
-    struct epoll_event *epoll_events;
-    int epoll_fd;               /* epoll fd that includes channel socks. */
-    int n_events;               /* Num events returned by epoll_wait(). */
-    int event_offset;           /* Offset into 'epoll_events'. */
+    struct fat_rwlock upcall_lock;
+    int uc_array_size;             /* Size of 'handlers' and 'epoll_events'. */
+    struct dpif_epoll *epolls;
+    struct dpif_handler *handlers;
+    uint32_t n_handlers;           /* Num of upcall handlers. */
 
     /* Change notification. */
     struct nl_sock *port_notifier; /* vport multicast group subscriber. */
@@ -172,8 +183,7 @@ static int dpif_linux_init(void);
 static int open_dpif(const struct dpif_linux_dp *, struct dpif **);
 static uint32_t dpif_linux_port_get_pid(const struct dpif *,
                                         odp_port_t port_no, uint32_t hash);
-static int dpif_linux_refresh_channels(struct dpif *);
-
+static int dpif_linux_refresh_channels(struct dpif *, uint32_t n_handlers);
 static void dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *,
                                        struct ofpbuf *);
 static int dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *,
@@ -252,8 +262,7 @@ open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
 
     dpif = xzalloc(sizeof *dpif);
     dpif->port_notifier = NULL;
-    ovs_mutex_init(&dpif->upcall_lock);
-    dpif->epoll_fd = -1;
+    fat_rwlock_init(&dpif->upcall_lock);
 
     dpif_init(&dpif->dpif, &dpif_linux_class, dp->name,
               dp->dp_ifindex, dp->dp_ifindex);
@@ -264,64 +273,110 @@ open_dpif(const struct dpif_linux_dp *dp, struct dpif **dpifp)
     return 0;
 }
 
+/* Destroys the netlink sockets pointed by the elements in 'socksp'
+ * and frees the 'socksp'.  */
 static void
-destroy_channels(struct dpif_linux *dpif)
+vport_del_socksp(struct nl_sock **socksp, uint32_t n_socks)
 {
-    unsigned int i;
+    size_t i;
 
-    if (dpif->epoll_fd < 0) {
-        return;
+    for (i = 0; i < n_socks; i++) {
+        nl_sock_destroy(socksp[i]);
     }
 
-    for (i = 0; i < dpif->uc_array_size; i++ ) {
-        struct dpif_linux_vport vport_request;
-        struct dpif_channel *ch = &dpif->channels[i];
-        uint32_t upcall_pid = 0;
+    free(socksp);
+}
 
-        if (!ch->sock) {
-            continue;
+/* Creates an array of netlink sockets.  Returns an array of the
+ * corresponding pointers.  Records the error in 'error'. */
+static struct nl_sock **
+vport_create_socksp(uint32_t n_socks, int *error)
+{
+    struct nl_sock **socksp = xzalloc(n_socks * sizeof *socksp);
+    size_t i;
+
+    for (i = 0; i < n_socks; i++) {
+        *error = nl_sock_create(NETLINK_GENERIC, &socksp[i]);
+        if (*error) {
+            goto error;
         }
+    }
 
-        epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
+    return socksp;
 
-        /* Turn off upcalls. */
-        dpif_linux_vport_init(&vport_request);
-        vport_request.cmd = OVS_VPORT_CMD_SET;
-        vport_request.dp_ifindex = dpif->dp_ifindex;
-        vport_request.port_no = u32_to_odp(i);
-        vport_request.upcall_pid = &upcall_pid;
-        dpif_linux_vport_transact(&vport_request, NULL, NULL);
+error:
+    vport_del_socksp(socksp, n_socks);
+
+    return NULL;
+}
+
+/* Given the array of pointers to netlink sockets 'socksp', returns
+ * the array of corresponding pids. If the 'socksp' is NULL, returns
+ * a single-element array of value 0. */
+static uint32_t *
+vport_socksp_to_pids(struct nl_sock **socksp, uint32_t n_socks)
+{
+    uint32_t *pids;
+
+    if (!socksp) {
+        pids = xzalloc(sizeof *pids);
+    } else {
+        size_t i;
 
-        nl_sock_destroy(ch->sock);
+        pids = xzalloc(n_socks * sizeof *pids);
+        for (i = 0; i < n_socks; i++) {
+            pids[i] = nl_sock_pid(socksp[i]);
+        }
     }
 
-    free(dpif->channels);
-    dpif->channels = NULL;
-    dpif->uc_array_size = 0;
+    return pids;
+}
 
-    free(dpif->epoll_events);
-    dpif->epoll_events = NULL;
-    dpif->n_events = dpif->event_offset = 0;
+/* Given the port number 'port_idx', extracts the pids of netlink sockets
+ * associated to the port and assigns it to 'upcall_pids'. */
+static bool
+vport_get_pids(struct dpif_linux *dpif, uint32_t port_idx,
+               uint32_t **upcall_pids)
+{
+    uint32_t *pids;
+    size_t i;
 
-    /* Don't close dpif->epoll_fd since that would cause other threads that
-     * call dpif_recv_wait() to wait on an arbitrary fd or a closed fd. */
+    /* Since the nl_sock can only be assigned in either all
+     * or none "dpif->handlers" channels, the following check
+     * would suffice. */
+    if (!dpif->handlers[0].channels[port_idx].sock) {
+        return false;
+    }
+
+    pids = xzalloc(dpif->n_handlers * sizeof *pids);
+
+    for (i = 0; i < dpif->n_handlers; i++) {
+        pids[i] = nl_sock_pid(dpif->handlers[i].channels[port_idx].sock);
+    }
+
+    *upcall_pids = pids;
+
+    return true;
 }
 
 static int
-add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
+vport_add_channels(struct dpif_linux *dpif, odp_port_t port_no,
+                   struct nl_sock **socksp)
 {
     struct epoll_event event;
+    struct dpif_epoll *epolls = dpif->epolls;
     uint32_t port_idx = odp_to_u32(port_no);
+    size_t i, j;
+    int error;
 
-    if (dpif->epoll_fd < 0) {
+    if (epolls == NULL) {
         return 0;
     }
 
-    /* We assume that the datapath densely chooses port numbers, which
-     * can therefore be used as an index into an array of channels. */
+    /* We assume that the datapath densely chooses port numbers, which can
+     * therefore be used as an index into 'channels' of 'dpif->handlers'. */
     if (port_idx >= dpif->uc_array_size) {
         uint32_t new_size = port_idx + 1;
-        uint32_t i;
 
         if (new_size > MAX_PORTS) {
             VLOG_WARN_RL(&error_rl, "%s: datapath port %"PRIu32" too big",
@@ -329,52 +384,124 @@ add_channel(struct dpif_linux *dpif, odp_port_t port_no, struct nl_sock *sock)
             return EFBIG;
         }
 
-        dpif->channels = xrealloc(dpif->channels,
-                                  new_size * sizeof *dpif->channels);
-        for (i = dpif->uc_array_size; i < new_size; i++) {
-            dpif->channels[i].sock = NULL;
+        for (i = 0; i < dpif->n_handlers; i++) {
+            struct dpif_handler *handler = &dpif->handlers[i];
+
+            handler->channels = xrealloc(handler->channels,
+                                         new_size * sizeof *handler->channels);
+
+            for (j = dpif->uc_array_size; j < new_size; j++) {
+                handler->channels[j].sock = NULL;
+            }
+
         }
 
-        dpif->epoll_events = xrealloc(dpif->epoll_events,
-                                      new_size * sizeof *dpif->epoll_events);
+        for (i = 0; i < dpif->n_handlers; i++) {
+            epolls[i].epoll_events = xrealloc(epolls[i].epoll_events,
+                                              new_size * sizeof
+                                              *epolls[i].epoll_events);
+        }
         dpif->uc_array_size = new_size;
     }
 
     memset(&event, 0, sizeof event);
     event.events = EPOLLIN;
     event.data.u32 = port_idx;
-    if (epoll_ctl(dpif->epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(sock),
-                  &event) < 0) {
-        return errno;
-    }
 
-    nl_sock_destroy(dpif->channels[port_idx].sock);
-    dpif->channels[port_idx].sock = sock;
-    dpif->channels[port_idx].last_poll = LLONG_MIN;
+    for (i = 0; i < dpif->n_handlers; i++) {
+        if (epoll_ctl(epolls[i].epoll_fd, EPOLL_CTL_ADD, nl_sock_fd(socksp[i]),
+                      &event) < 0) {
+            error = errno;
+            goto error;
+        }
+        dpif->handlers[i].channels[port_idx].sock = socksp[i];
+        dpif->handlers[i].channels[port_idx].last_poll = LLONG_MIN;
+    }
 
     return 0;
+
+error:
+    for (j = 0; j < i; j++) {
+        epoll_ctl(dpif->epolls[i].epoll_fd, EPOLL_CTL_DEL,
+                  nl_sock_fd(socksp[j]), NULL);
+        dpif->handlers[j].channels[port_idx].sock = NULL;
+    }
+
+    return error;
 }
 
 static void
-del_channel(struct dpif_linux *dpif, odp_port_t port_no)
+vport_del_channels(struct dpif_linux *dpif, odp_port_t port_no)
 {
-    struct dpif_channel *ch;
     uint32_t port_idx = odp_to_u32(port_no);
+    size_t i;
+
+    if (!dpif->epolls || port_idx >= dpif->uc_array_size) {
+        return;
+    }
 
-    if (dpif->epoll_fd < 0 || port_idx >= dpif->uc_array_size) {
+    /* Since the sock can only be assigned in either all or none
+     * of "dpif->handlers" channels, the following check would
+     * suffice. */
+    if (!dpif->handlers[0].channels[port_idx].sock) {
         return;
     }
 
-    ch = &dpif->channels[port_idx];
-    if (!ch->sock) {
+    for (i = 0; i < dpif->n_handlers; i++) {
+        struct dpif_handler *handler = &dpif->handlers[i];
+
+        epoll_ctl(dpif->epolls[i].epoll_fd, EPOLL_CTL_DEL,
+                  nl_sock_fd(handler->channels[port_idx].sock), NULL);
+        nl_sock_destroy(handler->channels[port_idx].sock);
+        handler->channels[port_idx].sock = NULL;
+        dpif->epolls[i].event_offset = dpif->epolls[i].n_events = 0;
+    }
+}
+
+static void
+destroy_all_channels(struct dpif_linux *dpif)
+{
+    unsigned int i;
+
+    if (!dpif->epolls) {
         return;
     }
 
-    epoll_ctl(dpif->epoll_fd, EPOLL_CTL_DEL, nl_sock_fd(ch->sock), NULL);
-    dpif->event_offset = dpif->n_events = 0;
+    for (i = 0; i < dpif->uc_array_size; i++ ) {
+        struct dpif_linux_vport vport_request;
+        uint32_t upcall_pids = 0;
+
+        /* Since the sock can only be assigned in either all or none
+         * of "dpif->handlers" channels, the following check would
+         * suffice. */
+        if (!dpif->handlers[0].channels[i].sock) {
+            continue;
+        }
+
+        /* Turn off upcalls. */
+        dpif_linux_vport_init(&vport_request);
+        vport_request.cmd = OVS_VPORT_CMD_SET;
+        vport_request.dp_ifindex = dpif->dp_ifindex;
+        vport_request.port_no = u32_to_odp(i);
+        vport_request.upcall_pids = &upcall_pids;
+        dpif_linux_vport_transact(&vport_request, NULL, NULL);
+
+        vport_del_channels(dpif, u32_to_odp(i));
+    }
+
+    for (i = 0; i < dpif->n_handlers; i++) {
+        free(dpif->handlers[i].channels);
+        free(dpif->epolls[i].epoll_events);
+    }
+
+    free(dpif->handlers);
+    free(dpif->epolls);
+    dpif->handlers = NULL;
+    dpif->epolls = NULL;
+    dpif->uc_array_size = 0;
 
-    nl_sock_destroy(ch->sock);
-    ch->sock = NULL;
+    /* Don't close dpif->epoll_fd since that would cause other threads that
+     * call dpif_recv_wait(dpif) to wait on an arbitrary fd or a closed fd. */
 }
 
 static void
@@ -383,11 +510,12 @@ dpif_linux_close(struct dpif *dpif_)
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
     nl_sock_destroy(dpif->port_notifier);
-    destroy_channels(dpif);
-    if (dpif->epoll_fd >= 0) {
-        close(dpif->epoll_fd);
-    }
-    ovs_mutex_destroy(&dpif->upcall_lock);
+
+    fat_rwlock_wrlock(&dpif->upcall_lock);
+    destroy_all_channels(dpif);
+    fat_rwlock_unlock(&dpif->upcall_lock);
+
+    fat_rwlock_destroy(&dpif->upcall_lock);
     free(dpif);
 }
 
@@ -407,10 +535,13 @@ static void
 dpif_linux_run(struct dpif *dpif_)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+
+    fat_rwlock_wrlock(&dpif->upcall_lock);
     if (dpif->refresh_channels) {
         dpif->refresh_channels = false;
-        dpif_linux_refresh_channels(dpif_);
+        dpif_linux_refresh_channels(dpif_, dpif->n_handlers);
     }
+    fat_rwlock_unlock(&dpif->upcall_lock);
 }
 
 static int
@@ -503,16 +634,16 @@ dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
                                                   namebuf, sizeof namebuf);
     const char *type = netdev_get_type(netdev);
     struct dpif_linux_vport request, reply;
-    struct nl_sock *sock = NULL;
-    uint32_t upcall_pid;
     struct ofpbuf *buf;
     uint64_t options_stub[64 / 8];
     struct ofpbuf options;
-    int error;
+    struct nl_sock **socksp = NULL;
+    uint32_t *upcall_pids;
+    int error = 0;
 
-    if (dpif->epoll_fd >= 0) {
-        error = nl_sock_create(NETLINK_GENERIC, &sock);
-        if (error) {
+    if (dpif->epolls) {
+        socksp = vport_create_socksp(dpif->n_handlers, &error);
+        if (!socksp) {
             return error;
         }
     }
@@ -525,7 +656,7 @@ dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
         VLOG_WARN_RL(&error_rl, "%s: cannot create port `%s' because it has "
                      "unsupported type `%s'",
                      dpif_name(dpif_), name, type);
-        nl_sock_destroy(sock);
+        vport_del_socksp(socksp, dpif->n_handlers);
         return EINVAL;
     }
     request.name = name;
@@ -544,27 +675,25 @@ dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
     }
 
     request.port_no = *port_nop;
-    upcall_pid = sock ? nl_sock_pid(sock) : 0;
-    request.upcall_pid = &upcall_pid;
+    upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
+    request.n_pids = dpif->n_handlers;
+    request.upcall_pids = upcall_pids;
 
     error = dpif_linux_vport_transact(&request, &reply, &buf);
     if (!error) {
         *port_nop = reply.port_no;
-        VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
-                 dpif_name(dpif_), reply.port_no, upcall_pid);
     } else {
         if (error == EBUSY && *port_nop != ODPP_NONE) {
             VLOG_INFO("%s: requested port %"PRIu32" is in use",
                       dpif_name(dpif_), *port_nop);
         }
-        nl_sock_destroy(sock);
-        ofpbuf_delete(buf);
-        return error;
+
+        vport_del_socksp(socksp, dpif->n_handlers);
+        goto exit;
     }
-    ofpbuf_delete(buf);
 
-    if (sock) {
-        error = add_channel(dpif, *port_nop, sock);
+    if (socksp) {
+        error = vport_add_channels(dpif, *port_nop, socksp);
         if (error) {
             VLOG_INFO("%s: could not add channel for port %s",
                       dpif_name(dpif_), name);
@@ -576,12 +705,17 @@ dpif_linux_port_add__(struct dpif *dpif_, struct netdev *netdev,
             request.port_no = *port_nop;
             dpif_linux_vport_transact(&request, NULL, NULL);
 
-            nl_sock_destroy(sock);
-            return error;
+            vport_del_socksp(socksp, dpif->n_handlers);
+            goto exit;
         }
     }
+    free(socksp);
 
-    return 0;
+exit:
+    ofpbuf_delete(buf);
+    free(upcall_pids);
+
+    return error;
 }
 
 static int
@@ -591,9 +725,9 @@ dpif_linux_port_add(struct dpif *dpif_, struct netdev *netdev,
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     int error;
 
-    ovs_mutex_lock(&dpif->upcall_lock);
+    fat_rwlock_wrlock(&dpif->upcall_lock);
     error = dpif_linux_port_add__(dpif_, netdev, port_nop);
-    ovs_mutex_unlock(&dpif->upcall_lock);
+    fat_rwlock_unlock(&dpif->upcall_lock);
 
     return error;
 }
@@ -611,7 +745,7 @@ dpif_linux_port_del__(struct dpif *dpif_, odp_port_t port_no)
     vport.port_no = port_no;
     error = dpif_linux_vport_transact(&vport, NULL, NULL);
 
-    del_channel(dpif, port_no);
+    vport_del_channels(dpif, port_no);
 
     return error;
 }
@@ -622,9 +756,9 @@ dpif_linux_port_del(struct dpif *dpif_, odp_port_t port_no)
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     int error;
 
-    ovs_mutex_lock(&dpif->upcall_lock);
+    fat_rwlock_wrlock(&dpif->upcall_lock);
     error = dpif_linux_port_del__(dpif_, port_no);
-    ovs_mutex_unlock(&dpif->upcall_lock);
+    fat_rwlock_unlock(&dpif->upcall_lock);
 
     return error;
 }
@@ -676,21 +810,22 @@ dpif_linux_port_query_by_name(const struct dpif *dpif, const char *devname,
 
 static uint32_t
 dpif_linux_port_get_pid(const struct dpif *dpif_, odp_port_t port_no,
-                        uint32_t hash OVS_UNUSED)
+                        uint32_t hash)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     uint32_t port_idx = odp_to_u32(port_no);
     uint32_t pid = 0;
 
-    ovs_mutex_lock(&dpif->upcall_lock);
-    if (dpif->epoll_fd >= 0) {
+    fat_rwlock_rdlock(&dpif->upcall_lock);
+    if (dpif->epolls) {
         /* The ODPP_NONE "reserved" port number uses the "ovs-system"'s
          * channel, since it is not heavily loaded. */
         uint32_t idx = port_idx >= dpif->uc_array_size ? 0 : port_idx;
-        const struct nl_sock *sock = dpif->channels[idx].sock;
-        pid = sock ? nl_sock_pid(sock) : 0;
+        struct dpif_handler *h = &dpif->handlers[hash % dpif->n_handlers];
+
+        pid = nl_sock_pid(h->channels[idx].sock);
     }
-    ovs_mutex_unlock(&dpif->upcall_lock);
+    fat_rwlock_unlock(&dpif->upcall_lock);
 
     return pid;
 }
@@ -833,7 +968,7 @@ dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
                         || vport.cmd == OVS_VPORT_CMD_SET)) {
                     VLOG_DBG("port_changed: dpif:%s vport:%s cmd:%"PRIu8,
                              dpif->dpif.full_name, vport.name, vport.cmd);
-                    if (vport.cmd == OVS_VPORT_CMD_DEL) {
+                    if (vport.cmd == OVS_VPORT_CMD_DEL && dpif->epolls) {
                         dpif->refresh_channels = true;
                     }
                     *devnamep = xstrdup(vport.name);
@@ -1278,10 +1413,10 @@ dpif_linux_operate(struct dpif *dpif, struct dpif_op **ops, size_t n_ops)
 }
 
 /* Synchronizes 'dpif->channels' with the set of vports currently in 'dpif' in
- * the kernel, by adding a new channel for any kernel vport that lacks one and
- * deleting any channels that have no backing kernel vports. */
+ * the kernel, by adding a new set of channels for any kernel vport that lacks
+ * one and deleting any channels that have no backing kernel vports. */
 static int
-dpif_linux_refresh_channels(struct dpif *dpif_)
+dpif_linux_refresh_channels(struct dpif *dpif_, uint32_t n_handlers)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     unsigned long int *keep_channels;
@@ -1291,52 +1426,77 @@ dpif_linux_refresh_channels(struct dpif *dpif_)
     int retval = 0;
     size_t i;
 
-    /* To start with, we need an epoll fd. */
-    if (dpif->epoll_fd < 0) {
-        dpif->epoll_fd = epoll_create(10);
-        if (dpif->epoll_fd < 0) {
-            return errno;
+    if (dpif->n_handlers != n_handlers) {
+        destroy_all_channels(dpif);
+        dpif->epolls = xzalloc(n_handlers * sizeof *dpif->epolls);
+        for (i = 0; i < n_handlers; i++) {
+            dpif->epolls[i].epoll_fd = epoll_create(dpif->uc_array_size ?
+                                                    dpif->uc_array_size : 10);
+            if (dpif->epolls[i].epoll_fd < 0) {
+                size_t j;
+
+                for (j = 0; j < i; j++) {
+                    close(dpif->epolls[j].epoll_fd);
+                }
+                free(dpif->epolls);
+                dpif->epolls = NULL;
+
+                return errno;
+            }
         }
+        dpif->handlers = xzalloc(n_handlers * sizeof *dpif->handlers);
+        dpif->n_handlers = n_handlers;
+    }
+
+    for (i = 0; i < n_handlers; i++) {
+        dpif->epolls[i].event_offset = dpif->epolls[i].n_events = 0;
     }
 
     keep_channels_nbits = dpif->uc_array_size;
     keep_channels = bitmap_allocate(keep_channels_nbits);
 
-    dpif->n_events = dpif->event_offset = 0;
-
     dpif_linux_port_dump_start__(dpif_, &dump);
     while (!dpif_linux_port_dump_next__(dpif_, &dump, &vport)) {
         uint32_t port_no = odp_to_u32(vport.port_no);
-        struct nl_sock *sock = (port_no < dpif->uc_array_size
-                                ? dpif->channels[port_no].sock
-                                : NULL);
-        bool new_sock = !sock;
+        uint32_t *upcall_pids = NULL;
         int error;
 
-        if (new_sock) {
-            error = nl_sock_create(NETLINK_GENERIC, &sock);
+        if (port_no >= dpif->uc_array_size
+            || !vport_get_pids(dpif, port_no, &upcall_pids)) {
+            struct nl_sock **socksp = vport_create_socksp(dpif->n_handlers,
+                                                          &error);
+
+            if (!socksp) {
+                goto error;
+            }
+
+            error = vport_add_channels(dpif, vport.port_no, socksp);
             if (error) {
+                VLOG_INFO("%s: could not add channels for port %s",
+                          dpif_name(dpif_), vport.name);
+                vport_del_socksp(socksp, dpif->n_handlers);
                 retval = error;
                 goto error;
             }
+            upcall_pids = vport_socksp_to_pids(socksp, dpif->n_handlers);
+            free(socksp);
         }
 
         /* Configure the vport to deliver misses to 'sock'. */
-        if (!vport.upcall_pid || *vport.upcall_pid != nl_sock_pid(sock)) {
-            uint32_t upcall_pid = nl_sock_pid(sock);
+        if (vport.upcall_pids[0] == 0
+            || vport.n_pids != dpif->n_handlers
+            || memcmp(upcall_pids, vport.upcall_pids, n_handlers * sizeof
+                      *upcall_pids)) {
             struct dpif_linux_vport vport_request;
 
             dpif_linux_vport_init(&vport_request);
             vport_request.cmd = OVS_VPORT_CMD_SET;
             vport_request.dp_ifindex = dpif->dp_ifindex;
             vport_request.port_no = vport.port_no;
-            vport_request.upcall_pid = &upcall_pid;
+            vport_request.n_pids = dpif->n_handlers;
+            vport_request.upcall_pids = upcall_pids;
             error = dpif_linux_vport_transact(&vport_request, NULL, NULL);
-            if (!error) {
-                VLOG_DBG("%s: assigning port %"PRIu32" to netlink pid %"PRIu32,
-                         dpif_name(&dpif->dpif), vport_request.port_no,
-                         upcall_pid);
-            } else {
+            if (error) {
                 VLOG_WARN_RL(&error_rl,
                              "%s: failed to set upcall pid on port: %s",
                              dpif_name(&dpif->dpif), ovs_strerror(error));
@@ -1352,31 +1512,20 @@ dpif_linux_refresh_channels(struct dpif *dpif_)
             }
         }
 
-        if (new_sock) {
-            error = add_channel(dpif, vport.port_no, sock);
-            if (error) {
-                VLOG_INFO("%s: could not add channel for port %s",
-                          dpif_name(dpif_), vport.name);
-                retval = error;
-                goto error;
-            }
-        }
-
         if (port_no < keep_channels_nbits) {
             bitmap_set1(keep_channels, port_no);
         }
         continue;
 
     error:
-        nl_sock_destroy(sock);
+        vport_del_channels(dpif, vport.port_no);
     }
     nl_dump_done(&dump);
 
     /* Discard any saved channels that we didn't reuse. */
     for (i = 0; i < keep_channels_nbits; i++) {
         if (!bitmap_is_set(keep_channels, i)) {
-            nl_sock_destroy(dpif->channels[i].sock);
-            dpif->channels[i].sock = NULL;
+            vport_del_channels(dpif, u32_to_odp(i));
         }
     }
     free(keep_channels);
@@ -1385,39 +1534,47 @@ dpif_linux_refresh_channels(struct dpif *dpif_)
 }
 
 static int
-dpif_linux_recv_set__(struct dpif *dpif_, bool enable)
+dpif_linux_recv_set__(struct dpif *dpif_, bool enable, uint32_t n_handlers)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
-    if ((dpif->epoll_fd >= 0) == enable) {
+    if ((dpif->epolls != NULL) == enable) {
+        if (enable && dpif->n_handlers != n_handlers) {
+            dpif_linux_refresh_channels(dpif_, n_handlers);
+        }
         return 0;
     } else if (!enable) {
-        destroy_channels(dpif);
+        destroy_all_channels(dpif);
         return 0;
     } else {
-        return dpif_linux_refresh_channels(dpif_);
+        return dpif_linux_refresh_channels(dpif_, n_handlers);
     }
 }
 
 static int
-dpif_linux_recv_set(struct dpif *dpif_, bool enable,
-                    uint32_t n_handlers OVS_UNUSED)
+dpif_linux_recv_set(struct dpif *dpif_, bool enable, uint32_t n_handlers)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     int error;
 
-    ovs_mutex_lock(&dpif->upcall_lock);
-    error = dpif_linux_recv_set__(dpif_, enable);
-    ovs_mutex_unlock(&dpif->upcall_lock);
+    fat_rwlock_wrlock(&dpif->upcall_lock);
+    error = dpif_linux_recv_set__(dpif_, enable, n_handlers);
+    fat_rwlock_unlock(&dpif->upcall_lock);
 
     return error;
 }
 
 static int
-dpif_linux_handlers_set(struct dpif *dpif OVS_UNUSED,
-                        uint32_t n_handlers OVS_UNUSED)
+dpif_linux_handlers_set(struct dpif *dpif_, uint32_t n_handlers)
 {
-    return 0;
+    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+    int error;
+
+    fat_rwlock_wrlock(&dpif->upcall_lock);
+    error = dpif_linux_recv_set__(dpif_, dpif->epolls, n_handlers);
+    fat_rwlock_unlock(&dpif->upcall_lock);
+
+    return error;
 }
 
 static int
@@ -1494,38 +1651,39 @@ parse_odp_packet(struct ofpbuf *buf, struct dpif_upcall *upcall,
 }
 
 static int
-dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
-                  struct ofpbuf *buf)
+dpif_linux_recv__(struct dpif *dpif_, uint32_t handler_id,
+                  struct dpif_upcall *upcall, struct ofpbuf *buf)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+    struct dpif_epoll *epoll = dpif->epolls ? &dpif->epolls[handler_id] : NULL;
     int read_tries = 0;
 
-    if (dpif->epoll_fd < 0) {
-       return EAGAIN;
+    if (!epoll) {
+        return EAGAIN;
     }
 
-    if (dpif->event_offset >= dpif->n_events) {
+    if (epoll->event_offset >= epoll->n_events) {
         int retval;
 
-        dpif->event_offset = dpif->n_events = 0;
+        epoll->event_offset = epoll->n_events = 0;
 
         do {
-            retval = epoll_wait(dpif->epoll_fd, dpif->epoll_events,
+            retval = epoll_wait(epoll->epoll_fd, epoll->epoll_events,
                                 dpif->uc_array_size, 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)", ovs_strerror(errno));
         } else if (retval > 0) {
-            dpif->n_events = retval;
+            epoll->n_events = retval;
         }
     }
 
-    while (dpif->event_offset < dpif->n_events) {
-        int idx = dpif->epoll_events[dpif->event_offset].data.u32;
-        struct dpif_channel *ch = &dpif->channels[idx];
+    while (epoll->event_offset < epoll->n_events) {
+        int idx = epoll->epoll_events[epoll->event_offset].data.u32;
+        struct dpif_channel *ch = &dpif->handlers[handler_id].channels[idx];
 
-        dpif->event_offset++;
+        epoll->event_offset++;
 
         for (;;) {
             int dp_ifindex;
@@ -1541,7 +1699,7 @@ dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
                  * packets that the buffer overflowed.  Try again
                  * immediately because there's almost certainly a packet
                  * waiting for us. */
-                report_loss(dpif_, ch);
+                report_loss(dpif_, ch, idx, handler_id);
                 continue;
             }
 
@@ -1566,29 +1724,29 @@ dpif_linux_recv__(struct dpif *dpif_, struct dpif_upcall *upcall,
 }
 
 static int
-dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id OVS_UNUSED,
+dpif_linux_recv(struct dpif *dpif_, uint32_t handler_id,
                 struct dpif_upcall *upcall, struct ofpbuf *buf)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     int error;
 
-    ovs_mutex_lock(&dpif->upcall_lock);
-    error = dpif_linux_recv__(dpif_, upcall, buf);
-    ovs_mutex_unlock(&dpif->upcall_lock);
+    fat_rwlock_rdlock(&dpif->upcall_lock);
+    error = dpif_linux_recv__(dpif_, handler_id, upcall, buf);
+    fat_rwlock_unlock(&dpif->upcall_lock);
 
     return error;
 }
 
 static void
-dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id OVS_UNUSED)
+dpif_linux_recv_wait(struct dpif *dpif_, uint32_t handler_id)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
-    ovs_mutex_lock(&dpif->upcall_lock);
-    if (dpif->epoll_fd >= 0) {
-        poll_fd_wait(dpif->epoll_fd, POLLIN);
+    fat_rwlock_rdlock(&dpif->upcall_lock);
+    if (dpif->epolls) {
+        poll_fd_wait(dpif->epolls[handler_id].epoll_fd, POLLIN);
     }
-    ovs_mutex_unlock(&dpif->upcall_lock);
+    fat_rwlock_unlock(&dpif->upcall_lock);
 }
 
 static void
@@ -1596,18 +1754,21 @@ dpif_linux_recv_purge(struct dpif *dpif_)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 
-    ovs_mutex_lock(&dpif->upcall_lock);
-    if (dpif->epoll_fd >= 0) {
-        struct dpif_channel *ch;
+    fat_rwlock_rdlock(&dpif->upcall_lock);
+    if (dpif->epolls) {
+        size_t i, j;
 
-        for (ch = dpif->channels; ch < &dpif->channels[dpif->uc_array_size];
-             ch++) {
-            if (ch->sock) {
-                nl_sock_drain(ch->sock);
+        for (i = 0; i < dpif->n_handlers; i++ ) {
+            if (!dpif->handlers[i].channels[0].sock) {
+                continue;
+            }
+
+            for (j = 0; j < dpif->uc_array_size; j++) {
+                nl_sock_drain(dpif->handlers[i].channels[j].sock);
             }
         }
     }
-    ovs_mutex_unlock(&dpif->upcall_lock);
+    fat_rwlock_unlock(&dpif->upcall_lock);
 }
 
 const struct dpif_class dpif_linux_class = {
@@ -1714,7 +1875,7 @@ dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
         [OVS_VPORT_ATTR_PORT_NO] = { .type = NL_A_U32 },
         [OVS_VPORT_ATTR_TYPE] = { .type = NL_A_U32 },
         [OVS_VPORT_ATTR_NAME] = { .type = NL_A_STRING, .max_len = IFNAMSIZ },
-        [OVS_VPORT_ATTR_UPCALL_PIDS] = { .type = NL_A_U32 },
+        [OVS_VPORT_ATTR_UPCALL_PIDS] = { .type = NL_A_UNSPEC },
         [OVS_VPORT_ATTR_STATS] = { NL_POLICY_FOR(struct ovs_vport_stats),
                                    .optional = true },
         [OVS_VPORT_ATTR_OPTIONS] = { .type = NL_A_NESTED, .optional = true },
@@ -1745,10 +1906,10 @@ dpif_linux_vport_from_ofpbuf(struct dpif_linux_vport *vport,
     vport->type = nl_attr_get_u32(a[OVS_VPORT_ATTR_TYPE]);
     vport->name = nl_attr_get_string(a[OVS_VPORT_ATTR_NAME]);
     if (a[OVS_VPORT_ATTR_UPCALL_PIDS]) {
-        vport->upcall_pid = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PIDS]);
-    }
-    if (a[OVS_VPORT_ATTR_STATS]) {
-        vport->stats = nl_attr_get(a[OVS_VPORT_ATTR_STATS]);
+        vport->n_pids = nl_attr_get_size(a[OVS_VPORT_ATTR_UPCALL_PIDS])
+                        / (sizeof *vport->upcall_pids);
+        vport->upcall_pids = nl_attr_get(a[OVS_VPORT_ATTR_UPCALL_PIDS]);
+
     }
     if (a[OVS_VPORT_ATTR_OPTIONS]) {
         vport->options = nl_attr_get(a[OVS_VPORT_ATTR_OPTIONS]);
@@ -1783,8 +1944,10 @@ dpif_linux_vport_to_ofpbuf(const struct dpif_linux_vport *vport,
         nl_msg_put_string(buf, OVS_VPORT_ATTR_NAME, vport->name);
     }
 
-    if (vport->upcall_pid) {
-        nl_msg_put_u32(buf, OVS_VPORT_ATTR_UPCALL_PIDS, *vport->upcall_pid);
+    if (vport->upcall_pids) {
+        nl_msg_put_unspec(buf, OVS_VPORT_ATTR_UPCALL_PIDS,
+                          vport->upcall_pids,
+                          vport->n_pids * sizeof *vport->upcall_pids);
     }
 
     if (vport->stats) {
@@ -2189,9 +2352,9 @@ dpif_linux_flow_get_stats(const struct dpif_linux_flow *flow,
 /* Logs information about a packet that was recently lost in 'ch' (in
  * 'dpif_'). */
 static void
-report_loss(struct dpif *dpif_, struct dpif_channel *ch)
+report_loss(struct dpif *dpif_, struct dpif_channel *ch, uint32_t ch_idx,
+            uint32_t handler_id)
 {
-    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
     struct ds s;
 
@@ -2205,7 +2368,7 @@ report_loss(struct dpif *dpif_, struct dpif_channel *ch)
                       time_msec() - ch->last_poll);
     }
 
-    VLOG_WARN("%s: lost packet on channel %"PRIdPTR"%s",
-              dpif_name(dpif_), ch - dpif->channels, ds_cstr(&s));
+    VLOG_WARN("%s: lost packet on port channel %u of handler %u",
+              dpif_name(dpif_), ch_idx, handler_id);
     ds_destroy(&s);
 }
diff --git a/lib/dpif-linux.h b/lib/dpif-linux.h
index ec94ccf..02c7b03 100644
--- a/lib/dpif-linux.h
+++ b/lib/dpif-linux.h
@@ -32,6 +32,7 @@ struct dpif_linux_vport {
 
     /* ovs_vport header. */
     int dp_ifindex;
+    uint32_t n_pids;
     odp_port_t port_no;                    /* ODPP_NONE if unknown. */
     enum ovs_vport_type type;
 
@@ -41,7 +42,7 @@ struct dpif_linux_vport {
      * 32-bit boundaries, so use get_unaligned_u64() to access its values.
      */
     const char *name;                      /* OVS_VPORT_ATTR_NAME. */
-    const uint32_t *upcall_pid;            /* OVS_VPORT_ATTR_UPCALL_PID. */
+    const uint32_t *upcall_pids;           /* OVS_VPORT_ATTR_UPCALL_PIDS. */
     const struct ovs_vport_stats *stats;   /* OVS_VPORT_ATTR_STATS. */
     const struct nlattr *options;          /* OVS_VPORT_ATTR_OPTIONS. */
     size_t options_len;
-- 
1.7.9.5




More information about the dev mailing list