Merge remote-tracking branch 'ovs-dev/master'
authorGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Wed, 31 Jul 2013 20:17:34 +0000 (22:17 +0200)
committerGiuseppe Lettieri <g.lettieri@iet.unipi.it>
Wed, 31 Jul 2013 20:17:34 +0000 (22:17 +0200)
Conflicts:
.gitignore

1  2 
.gitignore
lib/dpif-netdev.c
lib/dpif.c
lib/netdev.c

diff --combined .gitignore
@@@ -49,4 -49,4 +49,5 @@@ Module.symver
  TAGS
  cscope.*
  tags
 +myexp/
+ _debian
diff --combined lib/dpif-netdev.c
@@@ -52,6 -52,7 +52,7 @@@
  #include "shash.h"
  #include "sset.h"
  #include "timeval.h"
+ #include "unixctl.h"
  #include "util.h"
  #include "vlog.h"
  
@@@ -139,6 -140,9 +140,9 @@@ struct dpif_netdev 
  /* All netdev-based datapaths. */
  static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
  
+ /* Global lock for all data. */
+ static struct ovs_mutex dp_netdev_mutex = OVS_MUTEX_INITIALIZER;
  static int get_port_by_number(struct dp_netdev *, odp_port_t port_no,
                                struct dp_netdev_port **portp);
  static int get_port_by_name(struct dp_netdev *, const char *devname,
@@@ -180,9 -184,12 +184,12 @@@ dpif_netdev_enumerate(struct sset *all_
  {
      struct shash_node *node;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      SHASH_FOR_EACH(node, &dp_netdevs) {
          sset_add(all_dps, node->name);
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return 0;
  }
  
@@@ -192,17 -199,10 +199,17 @@@ dpif_netdev_class_is_dummy(const struc
      return class != &dpif_netdev_class;
  }
  
 +static bool
 +dpif_netdev_class_is_planetlab(const struct dpif_class *class)
 +{
 +    return class == &dpif_planetlab_class;
 +}
 +
  static const char *
  dpif_netdev_port_open_type(const struct dpif_class *class, const char *type)
  {
      return strcmp(type, "internal") ? type
 +                  : dpif_netdev_class_is_planetlab(class) ? "pltap"
                    : dpif_netdev_class_is_dummy(class) ? "dummy"
                    : "tap";
  }
@@@ -230,8 -230,7 +237,8 @@@ choose_port(struct dp_netdev *dp, cons
  {
      uint32_t port_no;
  
 -    if (dp->class != &dpif_netdev_class) {
 +    if (dp->class != &dpif_netdev_class && 
 +        dp->class != &dpif_planetlab_class) {
          const char *p;
          int start_no = 0;
  
@@@ -301,28 -300,23 +308,23 @@@ dpif_netdev_open(const struct dpif_clas
                   bool create, struct dpif **dpifp)
  {
      struct dp_netdev *dp;
+     int error;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      dp = shash_find_data(&dp_netdevs, name);
      if (!dp) {
-         if (!create) {
-             return ENODEV;
-         } else {
-             int error = create_dp_netdev(name, class, &dp);
-             if (error) {
-                 return error;
-             }
-             ovs_assert(dp != NULL);
-         }
+         error = create ? create_dp_netdev(name, class, &dp) : ENODEV;
      } else {
-         if (dp->class != class) {
-             return EINVAL;
-         } else if (create) {
-             return EEXIST;
-         }
+         error = (dp->class != class ? EINVAL
+                  : create ? EEXIST
+                  : 0);
+     }
+     if (!error) {
+         *dpifp = create_dpif_netdev(dp);
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
  
-     *dpifp = create_dpif_netdev(dp);
-     return 0;
+     return error;
  }
  
  static void
@@@ -359,19 -353,28 +361,28 @@@ static voi
  dpif_netdev_close(struct dpif *dpif)
  {
      struct dp_netdev *dp = get_dp_netdev(dpif);
+     ovs_mutex_lock(&dp_netdev_mutex);
      ovs_assert(dp->open_cnt > 0);
      if (--dp->open_cnt == 0 && dp->destroyed) {
          shash_find_and_delete(&dp_netdevs, dp->name);
          dp_netdev_free(dp);
      }
      free(dpif);
+     ovs_mutex_unlock(&dp_netdev_mutex);
  }
  
  static int
  dpif_netdev_destroy(struct dpif *dpif)
  {
      struct dp_netdev *dp = get_dp_netdev(dpif);
+     ovs_mutex_lock(&dp_netdev_mutex);
      dp->destroyed = true;
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return 0;
  }
  
@@@ -379,10 -382,14 +390,14 @@@ static in
  dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
  {
      struct dp_netdev *dp = get_dp_netdev(dpif);
+     ovs_mutex_lock(&dp_netdev_mutex);
      stats->n_flows = hmap_count(&dp->flow_table);
      stats->n_hit = dp->n_hit;
      stats->n_missed = dp->n_missed;
      stats->n_lost = dp->n_lost;
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return 0;
  }
  
@@@ -452,32 -459,44 +467,44 @@@ dpif_netdev_port_add(struct dpif *dpif
      char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
      const char *dpif_port;
      odp_port_t port_no;
+     int error;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      dpif_port = netdev_vport_get_dpif_port(netdev, namebuf, sizeof namebuf);
      if (*port_nop != ODPP_NONE) {
          uint32_t port_idx = odp_to_u32(*port_nop);
          if (port_idx >= MAX_PORTS) {
-             return EFBIG;
+             error = EFBIG;
          } else if (dp->ports[port_idx]) {
-             return EBUSY;
+             error = EBUSY;
+         } else {
+             error = 0;
+             port_no = *port_nop;
          }
-         port_no = *port_nop;
      } else {
          port_no = choose_port(dp, dpif_port);
+         error = port_no == ODPP_NONE ? EFBIG : 0;
      }
-     if (port_no != ODPP_NONE) {
+     if (!error) {
          *port_nop = port_no;
-         return do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
+         error = do_add_port(dp, dpif_port, netdev_get_type(netdev), port_no);
      }
-     return EFBIG;
+     ovs_mutex_unlock(&dp_netdev_mutex);
+     return error;
  }
  
  static int
  dpif_netdev_port_del(struct dpif *dpif, odp_port_t port_no)
  {
      struct dp_netdev *dp = get_dp_netdev(dpif);
-     return (port_no == ODPP_LOCAL ?
-                            EINVAL : do_del_port(dp, port_no));
+     int error;
+     ovs_mutex_lock(&dp_netdev_mutex);
+     error = port_no == ODPP_LOCAL ? EINVAL : do_del_port(dp, port_no);
+     ovs_mutex_unlock(&dp_netdev_mutex);
+     return error;
  }
  
  static bool
@@@ -555,10 -574,13 +582,13 @@@ dpif_netdev_port_query_by_number(const 
      struct dp_netdev_port *port;
      int error;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      error = get_port_by_number(dp, port_no, &port);
      if (!error && dpif_port) {
          answer_port_query(port, dpif_port);
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return error;
  }
  
@@@ -570,10 -592,13 +600,13 @@@ dpif_netdev_port_query_by_name(const st
      struct dp_netdev_port *port;
      int error;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      error = get_port_by_name(dp, devname, &port);
      if (!error && dpif_port) {
          answer_port_query(port, dpif_port);
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return error;
  }
  
@@@ -605,7 -630,11 +638,11 @@@ static in
  dpif_netdev_flow_flush(struct dpif *dpif)
  {
      struct dp_netdev *dp = get_dp_netdev(dpif);
+     ovs_mutex_lock(&dp_netdev_mutex);
      dp_netdev_flow_flush(dp);
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return 0;
  }
  
@@@ -629,6 -658,7 +666,7 @@@ dpif_netdev_port_dump_next(const struc
      struct dp_netdev *dp = get_dp_netdev(dpif);
      uint32_t port_idx;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      for (port_idx = odp_to_u32(state->port_no);
           port_idx < MAX_PORTS; port_idx++) {
          struct dp_netdev_port *port = dp->ports[port_idx];
              dpif_port->type = port->type;
              dpif_port->port_no = port->port_no;
              state->port_no = u32_to_odp(port_idx + 1);
+             ovs_mutex_unlock(&dp_netdev_mutex);
              return 0;
          }
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return EOF;
  }
  
@@@ -658,21 -692,34 +700,34 @@@ static in
  dpif_netdev_port_poll(const struct dpif *dpif_, char **devnamep OVS_UNUSED)
  {
      struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
+     int error;
+     ovs_mutex_lock(&dp_netdev_mutex);
      if (dpif->dp_serial != dpif->dp->serial) {
          dpif->dp_serial = dpif->dp->serial;
-         return ENOBUFS;
+         error = ENOBUFS;
      } else {
-         return EAGAIN;
+         error = EAGAIN;
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
+     return error;
  }
  
  static void
  dpif_netdev_port_poll_wait(const struct dpif *dpif_)
  {
      struct dpif_netdev *dpif = dpif_netdev_cast(dpif_);
+     /* XXX In a multithreaded process, there is a race window between this
+      * function and the poll_block() in one thread and a change in
+      * dpif->dp->serial in another thread. */
+     ovs_mutex_lock(&dp_netdev_mutex);
      if (dpif->dp_serial != dpif->dp->serial) {
          poll_immediate_wake();
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
  }
  
  static struct dp_netdev_flow *
@@@ -745,18 -792,21 +800,21 @@@ dpif_netdev_flow_get(const struct dpif 
          return error;
      }
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      flow = dp_netdev_lookup_flow(dp, &key);
-     if (!flow) {
-         return ENOENT;
+     if (flow) {
+         if (stats) {
+             get_dpif_flow_stats(flow, stats);
+         }
+         if (actionsp) {
+             *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
+         }
+     } else {
+         error = ENOENT;
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
  
-     if (stats) {
-         get_dpif_flow_stats(flow, stats);
-     }
-     if (actionsp) {
-         *actionsp = ofpbuf_clone_data(flow->actions, flow->actions_len);
-     }
-     return 0;
+     return error;
  }
  
  static int
@@@ -811,6 -861,7 +869,7 @@@ dpif_netdev_flow_put(struct dpif *dpif
          return error;
      }
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      flow = dp_netdev_lookup_flow(dp, &key);
      if (!flow) {
          if (put->flags & DPIF_FP_CREATE) {
                  if (put->stats) {
                      memset(put->stats, 0, sizeof *put->stats);
                  }
-                 return dp_netdev_flow_add(dp, &key, put->actions,
-                                           put->actions_len);
+                 error = dp_netdev_flow_add(dp, &key, put->actions,
+                                            put->actions_len);
              } else {
-                 return EFBIG;
+                 error = EFBIG;
              }
          } else {
-             return ENOENT;
+             error = ENOENT;
          }
      } else {
          if (put->flags & DPIF_FP_MODIFY) {
-             int error = set_flow_actions(flow, put->actions, put->actions_len);
+             error = set_flow_actions(flow, put->actions, put->actions_len);
              if (!error) {
                  if (put->stats) {
                      get_dpif_flow_stats(flow, put->stats);
                      clear_stats(flow);
                  }
              }
-             return error;
          } else {
-             return EEXIST;
+             error = EEXIST;
          }
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
+     return error;
  }
  
  static int
@@@ -857,16 -910,19 +918,19 @@@ dpif_netdev_flow_del(struct dpif *dpif
          return error;
      }
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      flow = dp_netdev_lookup_flow(dp, &key);
      if (flow) {
          if (del->stats) {
              get_dpif_flow_stats(flow, del->stats);
          }
          dp_netdev_free_flow(dp, flow);
-         return 0;
      } else {
-         return ENOENT;
+         error = ENOENT;
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
+     return error;
  }
  
  struct dp_netdev_flow_state {
@@@ -901,8 -957,10 +965,10 @@@ dpif_netdev_flow_dump_next(const struc
      struct dp_netdev_flow *flow;
      struct hmap_node *node;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
      node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
      if (!node) {
+         ovs_mutex_unlock(&dp_netdev_mutex);
          return EOF;
      }
  
          *stats = &state->stats;
      }
  
+     ovs_mutex_unlock(&dp_netdev_mutex);
      return 0;
  }
  
@@@ -971,8 -1030,10 +1038,10 @@@ dpif_netdev_execute(struct dpif *dpif, 
      error = dpif_netdev_flow_from_nlattrs(execute->key, execute->key_len,
                                            &key);
      if (!error) {
+         ovs_mutex_lock(&dp_netdev_mutex);
          dp_netdev_execute_actions(dp, &copy, &key,
                                    execute->actions, execute->actions_len);
+         ovs_mutex_unlock(&dp_netdev_mutex);
      }
  
      ofpbuf_uninit(&copy);
@@@ -1012,7 -1073,11 +1081,11 @@@ static in
  dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
                   struct ofpbuf *buf)
  {
-     struct dp_netdev_queue *q = find_nonempty_queue(dpif);
+     struct dp_netdev_queue *q;
+     int error;
+     ovs_mutex_lock(&dp_netdev_mutex);
+     q = find_nonempty_queue(dpif);
      if (q) {
          struct dp_netdev_upcall *u = &q->upcalls[q->tail++ & QUEUE_MASK];
  
          ofpbuf_uninit(buf);
          *buf = u->buf;
  
-         return 0;
+         error = 0;
      } else {
-         return EAGAIN;
+         error = EAGAIN;
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
+     return error;
  }
  
  static void
  dpif_netdev_recv_wait(struct dpif *dpif)
  {
+     /* XXX In a multithreaded process, there is a race window between this
+      * function and the poll_block() in one thread and a packet being queued in
+      * another thread. */
+     ovs_mutex_lock(&dp_netdev_mutex);
      if (find_nonempty_queue(dpif)) {
          poll_immediate_wake();
-     } else {
-         /* No messages ready to be received, and dp_wait() will ensure that we
-          * wake up to queue new messages, so there is nothing to do. */
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
  }
  
  static void
  dpif_netdev_recv_purge(struct dpif *dpif)
  {
      struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
+     ovs_mutex_lock(&dp_netdev_mutex);
      dp_netdev_purge_queues(dpif_netdev->dp);
+     ovs_mutex_unlock(&dp_netdev_mutex);
  }
  \f
  static void
@@@ -1084,10 -1157,12 +1165,12 @@@ dp_netdev_port_input(struct dp_netdev *
  static void
  dpif_netdev_run(struct dpif *dpif)
  {
-     struct dp_netdev *dp = get_dp_netdev(dpif);
      struct dp_netdev_port *port;
+     struct dp_netdev *dp;
      struct ofpbuf packet;
  
+     ovs_mutex_lock(&dp_netdev_mutex);
+     dp = get_dp_netdev(dpif);
      ofpbuf_init(&packet,
                  DP_NETDEV_HEADROOM + VLAN_ETH_HEADER_LEN + dp->max_mtu);
  
          }
      }
      ofpbuf_uninit(&packet);
+     ovs_mutex_unlock(&dp_netdev_mutex);
  }
  
  static void
  dpif_netdev_wait(struct dpif *dpif)
  {
-     struct dp_netdev *dp = get_dp_netdev(dpif);
      struct dp_netdev_port *port;
  
-     LIST_FOR_EACH (port, node, &dp->port_list) {
+     /* There is a race here, if thread A calls dpif_netdev_wait(dpif) and
+      * thread B calls dpif_port_add(dpif) or dpif_port_remove(dpif) before
+      * A makes it to poll_block().
+      *
+      * But I think it doesn't matter:
+      *
+      *     - In the dpif_port_add() case, A will not wake up when a packet
+      *       arrives on the new port, but this would also happen if the
+      *       ordering were reversed.
+      *
+      *     - In the dpif_port_remove() case, A might wake up spuriously, but
+      *       that is harmless. */
+     ovs_mutex_lock(&dp_netdev_mutex);
+     LIST_FOR_EACH (port, node, &get_dp_netdev(dpif)->port_list) {
          if (port->rx) {
              netdev_rx_wait(port->rx);
          }
      }
+     ovs_mutex_unlock(&dp_netdev_mutex);
  }
  
  static void
@@@ -1201,51 -1291,78 +1299,86 @@@ dp_netdev_execute_actions(struct dp_net
                          dp_netdev_output_port, dp_netdev_action_userspace);
  }
  
 +#define DPIF_NETDEV_CLASS_FUNCTIONS                   \
 +    dpif_netdev_enumerate,                            \
 +    dpif_netdev_port_open_type,                               \
 +    dpif_netdev_open,                                 \
 +    dpif_netdev_close,                                        \
 +    dpif_netdev_destroy,                              \
 +    dpif_netdev_run,                                  \
 +    dpif_netdev_wait,                                 \
 +    dpif_netdev_get_stats,                            \
 +    dpif_netdev_port_add,                             \
 +    dpif_netdev_port_del,                             \
 +    dpif_netdev_port_query_by_number,                 \
 +    dpif_netdev_port_query_by_name,                   \
 +    dpif_netdev_get_max_ports,                                \
 +    NULL,                       /* port_get_pid */    \
 +    dpif_netdev_port_dump_start,                      \
 +    dpif_netdev_port_dump_next,                               \
 +    dpif_netdev_port_dump_done,                               \
 +    dpif_netdev_port_poll,                            \
 +    dpif_netdev_port_poll_wait,                               \
 +    dpif_netdev_flow_get,                             \
 +    dpif_netdev_flow_put,                             \
 +    dpif_netdev_flow_del,                             \
 +    dpif_netdev_flow_flush,                           \
 +    dpif_netdev_flow_dump_start,                      \
 +    dpif_netdev_flow_dump_next,                               \
 +    dpif_netdev_flow_dump_done,                               \
 +    dpif_netdev_execute,                              \
 +    NULL,                       /* operate */         \
 +    dpif_netdev_recv_set,                             \
 +    dpif_netdev_queue_to_priority,                    \
 +    dpif_netdev_recv,                                 \
 +    dpif_netdev_recv_wait,                            \
 +    dpif_netdev_recv_purge,                           \
 +
  const struct dpif_class dpif_netdev_class = {
      "netdev",
 -    dpif_netdev_enumerate,
 -    dpif_netdev_port_open_type,
 -    dpif_netdev_open,
 -    dpif_netdev_close,
 -    dpif_netdev_destroy,
 -    dpif_netdev_run,
 -    dpif_netdev_wait,
 -    dpif_netdev_get_stats,
 -    dpif_netdev_port_add,
 -    dpif_netdev_port_del,
 -    dpif_netdev_port_query_by_number,
 -    dpif_netdev_port_query_by_name,
 -    dpif_netdev_get_max_ports,
 -    NULL,                       /* port_get_pid */
 -    dpif_netdev_port_dump_start,
 -    dpif_netdev_port_dump_next,
 -    dpif_netdev_port_dump_done,
 -    dpif_netdev_port_poll,
 -    dpif_netdev_port_poll_wait,
 -    dpif_netdev_flow_get,
 -    dpif_netdev_flow_put,
 -    dpif_netdev_flow_del,
 -    dpif_netdev_flow_flush,
 -    dpif_netdev_flow_dump_start,
 -    dpif_netdev_flow_dump_next,
 -    dpif_netdev_flow_dump_done,
 -    dpif_netdev_execute,
 -    NULL,                       /* operate */
 -    dpif_netdev_recv_set,
 -    dpif_netdev_queue_to_priority,
 -    dpif_netdev_recv,
 -    dpif_netdev_recv_wait,
 -    dpif_netdev_recv_purge,
 +    DPIF_NETDEV_CLASS_FUNCTIONS
 +};
 +
 +const struct dpif_class dpif_planetlab_class = {
 +    "planetlab",
 +    DPIF_NETDEV_CLASS_FUNCTIONS
  };
  
+ static void
+ dpif_dummy_change_port_number(struct unixctl_conn *conn, int argc OVS_UNUSED,
+                               const char *argv[], void *aux OVS_UNUSED)
+ {
+     struct dp_netdev_port *port;
+     struct dp_netdev *dp;
+     int port_no;
+     dp = shash_find_data(&dp_netdevs, argv[1]);
+     if (!dp || !dpif_netdev_class_is_dummy(dp->class)) {
+         unixctl_command_reply_error(conn, "unknown datapath or not a dummy");
+         return;
+     }
+     if (get_port_by_name(dp, argv[2], &port)) {
+         unixctl_command_reply_error(conn, "unknown port");
+         return;
+     }
+     port_no = atoi(argv[3]);
+     if (port_no <= 0 || port_no >= MAX_PORTS) {
+         unixctl_command_reply_error(conn, "bad port number");
+         return;
+     }
+     if (dp->ports[port_no]) {
+         unixctl_command_reply_error(conn, "port number already in use");
+         return;
+     }
+     dp->ports[odp_to_u32(port->port_no)] = NULL;
+     dp->ports[port_no] = port;
+     port->port_no = u32_to_odp(port_no);
+     dp->serial++;
+     unixctl_command_reply(conn, NULL);
+ }
  static void
  dpif_dummy_register__(const char *type)
  {
@@@ -1275,5 -1392,8 +1408,9 @@@ dpif_dummy_register(bool override
      }
  
      dpif_dummy_register__("dummy");
+     unixctl_command_register("dpif-dummy/change-port-number",
+                              "DP PORT NEW-NUMBER",
+                              3, 3, dpif_dummy_change_port_number, NULL);
  }
 +
diff --combined lib/dpif.c
@@@ -61,7 -61,6 +61,7 @@@ static const struct dpif_class *base_dp
      &dpif_linux_class,
  #endif
      &dpif_netdev_class,
 +    &dpif_planetlab_class,
  };
  
  struct registered_dpif_class {
@@@ -71,6 -70,9 +71,9 @@@
  static struct shash dpif_classes = SHASH_INITIALIZER(&dpif_classes);
  static struct sset dpif_blacklist = SSET_INITIALIZER(&dpif_blacklist);
  
+ /* Protects 'dpif_classes', including the refcount, and 'dpif_blacklist'. */
+ static struct ovs_mutex dpif_mutex = OVS_MUTEX_INITIALIZER;
  /* Rate limit for individual messages going to or from the datapath, output at
   * DBG level.  This is very high because, if these are enabled, it is because
   * we really need to see them. */
@@@ -110,10 -112,8 +113,8 @@@ dp_initialize(void
      }
  }
  
- /* Registers a new datapath provider.  After successful registration, new
-  * datapaths of that type can be opened using dpif_open(). */
- int
- dp_register_provider(const struct dpif_class *new_class)
+ static int
+ dp_register_provider__(const struct dpif_class *new_class)
  {
      struct registered_dpif_class *registered_class;
  
      return 0;
  }
  
+ /* Registers a new datapath provider.  After successful registration, new
+  * datapaths of that type can be opened using dpif_open(). */
+ int
+ dp_register_provider(const struct dpif_class *new_class)
+ {
+     int error;
+     ovs_mutex_lock(&dpif_mutex);
+     error = dp_register_provider__(new_class);
+     ovs_mutex_unlock(&dpif_mutex);
+     return error;
+ }
  /* Unregisters a datapath provider.  'type' must have been previously
   * registered and not currently be in use by any dpifs.  After unregistration
   * new datapaths of that type cannot be opened using dpif_open(). */
- int
- dp_unregister_provider(const char *type)
static int
+ dp_unregister_provider__(const char *type)
  {
      struct shash_node *node;
      struct registered_dpif_class *registered_class;
      return 0;
  }
  
+ /* Unregisters a datapath provider.  'type' must have been previously
+  * registered and not currently be in use by any dpifs.  After unregistration
+  * new datapaths of that type cannot be opened using dpif_open(). */
+ int
+ dp_unregister_provider(const char *type)
+ {
+     int error;
+     dp_initialize();
+     ovs_mutex_lock(&dpif_mutex);
+     error = dp_unregister_provider__(type);
+     ovs_mutex_unlock(&dpif_mutex);
+     return error;
+ }
  /* Blacklists a provider.  Causes future calls of dp_register_provider() with
   * a dpif_class which implements 'type' to fail. */
  void
  dp_blacklist_provider(const char *type)
  {
+     ovs_mutex_lock(&dpif_mutex);
      sset_add(&dpif_blacklist, type);
+     ovs_mutex_unlock(&dpif_mutex);
  }
  
  /* Clears 'types' and enumerates the types of all currently registered datapath
@@@ -184,10 -217,36 +218,36 @@@ dp_enumerate_types(struct sset *types
      dp_initialize();
      sset_clear(types);
  
+     ovs_mutex_lock(&dpif_mutex);
      SHASH_FOR_EACH(node, &dpif_classes) {
          const struct registered_dpif_class *registered_class = node->data;
          sset_add(types, registered_class->dpif_class->type);
      }
+     ovs_mutex_unlock(&dpif_mutex);
+ }
+ static void
+ dp_class_unref(struct registered_dpif_class *rc)
+ {
+     ovs_mutex_lock(&dpif_mutex);
+     ovs_assert(rc->refcount);
+     rc->refcount--;
+     ovs_mutex_unlock(&dpif_mutex);
+ }
+ static struct registered_dpif_class *
+ dp_class_lookup(const char *type)
+ {
+     struct registered_dpif_class *rc;
+     ovs_mutex_lock(&dpif_mutex);
+     rc = shash_find_data(&dpif_classes, type);
+     if (rc) {
+         rc->refcount++;
+     }
+     ovs_mutex_unlock(&dpif_mutex);
+     return rc;
  }
  
  /* Clears 'names' and enumerates the names of all known created datapaths with
  int
  dp_enumerate_names(const char *type, struct sset *names)
  {
-     const struct registered_dpif_class *registered_class;
+     struct registered_dpif_class *registered_class;
      const struct dpif_class *dpif_class;
      int error;
  
      dp_initialize();
      sset_clear(names);
  
-     registered_class = shash_find_data(&dpif_classes, type);
+     registered_class = dp_class_lookup(type);
      if (!registered_class) {
          VLOG_WARN("could not enumerate unknown type: %s", type);
          return EAFNOSUPPORT;
  
      dpif_class = registered_class->dpif_class;
      error = dpif_class->enumerate ? dpif_class->enumerate(names) : 0;
      if (error) {
          VLOG_WARN("failed to enumerate %s datapaths: %s", dpif_class->type,
                     ovs_strerror(error));
      }
+     dp_class_unref(registered_class);
  
      return error;
  }
@@@ -254,8 -313,7 +314,7 @@@ do_open(const char *name, const char *t
      dp_initialize();
  
      type = dpif_normalize_type(type);
-     registered_class = shash_find_data(&dpif_classes, type);
+     registered_class = dp_class_lookup(type);
      if (!registered_class) {
          VLOG_WARN("could not create datapath %s of unknown type %s", name,
                    type);
                                                 name, create, &dpif);
      if (!error) {
          ovs_assert(dpif->dpif_class == registered_class->dpif_class);
-         registered_class->refcount++;
+     } else {
+         dp_class_unref(registered_class);
      }
  
  exit:
@@@ -327,15 -386,11 +387,11 @@@ voi
  dpif_close(struct dpif *dpif)
  {
      if (dpif) {
-         struct registered_dpif_class *registered_class;
-         registered_class = shash_find_data(&dpif_classes,
-                 dpif->dpif_class->type);
-         ovs_assert(registered_class);
-         ovs_assert(registered_class->refcount);
+         struct registered_dpif_class *rc;
  
-         registered_class->refcount--;
+         rc = shash_find_data(&dpif_classes, dpif->dpif_class->type);
          dpif_uninit(dpif, true);
+         dp_class_unref(rc);
      }
  }
  
@@@ -422,18 -477,18 +478,18 @@@ dpif_get_dp_stats(const struct dpif *dp
  const char *
  dpif_port_open_type(const char *datapath_type, const char *port_type)
  {
-     struct registered_dpif_class *registered_class;
+     struct registered_dpif_class *rc;
  
      datapath_type = dpif_normalize_type(datapath_type);
  
-     registered_class = shash_find_data(&dpif_classes, datapath_type);
-     if (!registered_class
-             || !registered_class->dpif_class->port_open_type) {
-         return port_type;
+     ovs_mutex_lock(&dpif_mutex);
+     rc = shash_find_data(&dpif_classes, datapath_type);
+     if (rc && rc->dpif_class->port_open_type) {
+         port_type = rc->dpif_class->port_open_type(rc->dpif_class, port_type);
      }
+     ovs_mutex_unlock(&dpif_mutex);
  
-     return registered_class->dpif_class->port_open_type(
-                           registered_class->dpif_class, port_type);
+     return port_type;
  }
  
  /* Attempts to add 'netdev' as a port on 'dpif'.  If 'port_nop' is
diff --combined lib/netdev.c
@@@ -89,8 -89,6 +89,8 @@@ netdev_initialize(void
          netdev_register_provider(&netdev_tap_class);
          netdev_register_provider(&netdev_bsd_class);
  #endif
 +      netdev_register_provider(&netdev_tunnel_class);
 +      netdev_register_provider(&netdev_pltap_class);
      }
  }
  
@@@ -1248,7 -1246,8 +1248,8 @@@ netdev_delete_queue(struct netdev *netd
  /* Obtains statistics about 'queue_id' on 'netdev'.  On success, returns 0 and
   * fills 'stats' with the queue's statistics; individual members of 'stats' may
   * be set to all-1-bits if the statistic is unavailable.  On failure, returns a
-  * positive errno value and fills 'stats' with all-1-bits. */
+  * positive errno value and fills 'stats' with values indicating unsupported
+  * statistics. */
  int
  netdev_get_queue_stats(const struct netdev *netdev, unsigned int queue_id,
                         struct netdev_queue_stats *stats)
                ? class->get_queue_stats(netdev, queue_id, stats)
                : EOPNOTSUPP);
      if (retval) {
-         memset(stats, 0xff, sizeof *stats);
+         stats->tx_bytes = UINT64_MAX;
+         stats->tx_packets = UINT64_MAX;
+         stats->tx_errors = UINT64_MAX;
+         stats->created = LLONG_MIN;
      }
      return retval;
  }