vswitch: Use ovsrec_qos_get_queues() to avoid open-coding binary search.
[openvswitch] / datapath / vport.c
index 83b42d550f294eb10dab56b9833fe5a1999105bb..38c71476e4f81b33b2aeca191a5269b3d541adb4 100644 (file)
 #include "vport.h"
 #include "vport-internal_dev.h"
 
-extern struct vport_ops netdev_vport_ops;
-extern struct vport_ops internal_vport_ops;
-extern struct vport_ops patch_vport_ops;
-extern struct vport_ops gre_vport_ops;
-
+/* List of statically compiled vport implementations.  Don't forget to also
+ * add yours to the list at the bottom of vport.h. */
 static struct vport_ops *base_vport_ops_list[] = {
        &netdev_vport_ops,
        &internal_vport_ops,
@@ -113,11 +110,6 @@ vport_init(void)
        for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
                struct vport_ops *new_ops = base_vport_ops_list[i];
 
-               if (new_ops->get_stats && new_ops->flags & VPORT_F_GEN_STATS) {
-                       printk(KERN_INFO "openvswitch: both get_stats() and VPORT_F_GEN_STATS defined on vport %s, dropping VPORT_F_GEN_STATS\n", new_ops->type);
-                       new_ops->flags &= ~VPORT_F_GEN_STATS;
-               }
-
                if (new_ops->init)
                        err = new_ops->init();
                else
@@ -403,56 +395,59 @@ vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
                goto out;
        }
 
-       if (vport->ops->get_stats) {
-               rcu_read_lock();
-               err = vport->ops->get_stats(vport, &stats_req.stats);
-               rcu_read_unlock();
+       err = vport_get_stats(vport, &stats_req.stats);
 
-       } else if (vport->ops->flags & VPORT_F_GEN_STATS) {
-               int i;
+out:
+       vport_unlock();
 
-               memset(&stats_req.stats, 0, sizeof(struct odp_vport_stats));
+       if (!err)
+               if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
+                       err = -EFAULT;
 
-               for_each_possible_cpu(i) {
-                       const struct vport_percpu_stats *percpu_stats;
+       return err;
+}
 
-                       percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
-                       stats_req.stats.rx_bytes        += percpu_stats->rx_bytes;
-                       stats_req.stats.rx_packets      += percpu_stats->rx_packets;
-                       stats_req.stats.tx_bytes        += percpu_stats->tx_bytes;
-                       stats_req.stats.tx_packets      += percpu_stats->tx_packets;
-               }
+/**
+ *     vport_user_stats_set - sets offset device stats (for userspace callers)
+ *
+ * @ustats_req: Stats set parameters.
+ *
+ * Provides a set of transmit, receive, and error stats to be added as an
+ * offset to the collect data when stats are retreived.  Some devices may not
+ * support setting the stats, in which case the result will always be
+ * -EOPNOTSUPP.  This function is for userspace callers and assumes no locks
+ * are held.
+ */
+int
+vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
+{
+       struct odp_vport_stats_req stats_req;
+       struct vport *vport;
+       int err;
 
-               spin_lock_bh(&vport->err_stats.lock);
+       if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
+               return -EFAULT;
 
-               stats_req.stats.rx_dropped      = vport->err_stats.rx_dropped;
-               stats_req.stats.rx_errors       = vport->err_stats.rx_errors
-                                               + vport->err_stats.rx_frame_err
-                                               + vport->err_stats.rx_over_err
-                                               + vport->err_stats.rx_crc_err;
-               stats_req.stats.rx_frame_err    = vport->err_stats.rx_frame_err;
-               stats_req.stats.rx_over_err     = vport->err_stats.rx_over_err;
-               stats_req.stats.rx_crc_err      = vport->err_stats.rx_crc_err;
-               stats_req.stats.tx_dropped      = vport->err_stats.tx_dropped;
-               stats_req.stats.tx_errors       = vport->err_stats.tx_errors;
-               stats_req.stats.collisions      = vport->err_stats.collisions;
+       stats_req.devname[IFNAMSIZ - 1] = '\0';
 
-               spin_unlock_bh(&vport->err_stats.lock);
+       rtnl_lock();
+       vport_lock();
 
-               err = 0;
-       } else
-               err = -EOPNOTSUPP;
+       vport = vport_locate(stats_req.devname);
+       if (!vport) {
+               err = -ENODEV;
+               goto out;
+       }
+
+       err = vport_set_stats(vport, &stats_req.stats);
 
 out:
        vport_unlock();
-
-       if (!err)
-               if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
-                       err = -EFAULT;
-
+       rtnl_unlock();
        return err;
 }
 
+
 /**
  *     vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
  *
@@ -699,7 +694,7 @@ vport_alloc(int priv_size, const struct vport_ops *ops)
                if (!vport->percpu_stats)
                        return ERR_PTR(-ENOMEM);
 
-               spin_lock_init(&vport->err_stats.lock);
+               spin_lock_init(&vport->stats_lock);
        }
 
        return vport;
@@ -926,6 +921,34 @@ vport_set_addr(struct vport *vport, const unsigned char *addr)
                return -EOPNOTSUPP;
 }
 
+/**
+ *     vport_set_stats - sets offset device stats (for kernel callers)
+ *
+ * @vport: vport on which to set stats
+ * @stats: stats to set
+ *
+ * Provides a set of transmit, receive, and error stats to be added as an
+ * offset to the collect data when stats are retreived.  Some devices may not
+ * support setting the stats, in which case the result will always be
+ * -EOPNOTSUPP.  RTNL lock must be held.
+ */
+int
+vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
+{
+       ASSERT_RTNL();
+
+       if (vport->ops->flags & VPORT_F_GEN_STATS) {
+               spin_lock_bh(&vport->stats_lock);
+               memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
+               spin_unlock_bh(&vport->stats_lock);
+
+               return 0;
+       } else if (vport->ops->set_stats)
+               return vport->ops->set_stats(vport, stats);
+       else
+               return -EOPNOTSUPP;
+}
+
 /**
  *     vport_get_name - retrieve device name
  *
@@ -1001,6 +1024,92 @@ vport_get_kobj(const struct vport *vport)
                return NULL;
 }
 
+/**
+ *     vport_get_stats - retrieve device stats (for kernel callers)
+ *
+ * @vport: vport from which to retrieve the stats
+ * @stats: location to store stats
+ *
+ * Retrieves transmit, receive, and error stats for the given device.
+ */
+int
+vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
+{
+       struct odp_vport_stats dev_stats;
+       struct odp_vport_stats *dev_statsp = NULL;
+       int err;
+
+       if (vport->ops->get_stats) {
+               if (vport->ops->flags & VPORT_F_GEN_STATS)
+                       dev_statsp = &dev_stats;
+               else
+                       dev_statsp = stats;
+
+               rcu_read_lock();
+               err = vport->ops->get_stats(vport, dev_statsp);
+               rcu_read_unlock();
+
+               if (err)
+                       goto out;
+       }
+
+       if (vport->ops->flags & VPORT_F_GEN_STATS) {
+               int i;
+
+               /* We potentially have 3 sources of stats that need to be
+                * combined: those we have collected (split into err_stats and
+                * percpu_stats), offset_stats from set_stats(), and device
+                * error stats from get_stats() (for errors that happen
+                * downstream and therefore aren't reported through our
+                * vport_record_error() function). */
+
+               spin_lock_bh(&vport->stats_lock);
+
+               memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
+
+               stats->rx_errors        += vport->err_stats.rx_errors
+                                               + vport->err_stats.rx_frame_err
+                                               + vport->err_stats.rx_over_err
+                                               + vport->err_stats.rx_crc_err;
+               stats->tx_errors        += vport->err_stats.tx_errors;
+               stats->tx_dropped       += vport->err_stats.tx_dropped;
+               stats->rx_dropped       += vport->err_stats.rx_dropped;
+               stats->rx_over_err      += vport->err_stats.rx_over_err;
+               stats->rx_crc_err       += vport->err_stats.rx_crc_err;
+               stats->rx_frame_err     += vport->err_stats.rx_frame_err;
+               stats->collisions       += vport->err_stats.collisions;
+
+               spin_unlock_bh(&vport->stats_lock);
+
+               if (dev_statsp) {
+                       stats->rx_errors        += dev_statsp->rx_errors;
+                       stats->tx_errors        += dev_statsp->tx_errors;
+                       stats->rx_dropped       += dev_statsp->rx_dropped;
+                       stats->tx_dropped       += dev_statsp->tx_dropped;
+                       stats->rx_over_err      += dev_statsp->rx_over_err;
+                       stats->rx_crc_err       += dev_statsp->rx_crc_err;
+                       stats->rx_frame_err     += dev_statsp->rx_frame_err;
+                       stats->collisions       += dev_statsp->collisions;
+               }
+
+               for_each_possible_cpu(i) {
+                       const struct vport_percpu_stats *percpu_stats;
+
+                       percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
+                       stats->rx_bytes         += percpu_stats->rx_bytes;
+                       stats->rx_packets       += percpu_stats->rx_packets;
+                       stats->tx_bytes         += percpu_stats->tx_bytes;
+                       stats->tx_packets       += percpu_stats->tx_packets;
+               }
+
+               err = 0;
+       } else
+               err = -EOPNOTSUPP;
+
+out:
+       return err;
+}
+
 /**
  *     vport_get_flags - retrieve device flags
  *
@@ -1191,7 +1300,7 @@ vport_record_error(struct vport *vport, enum vport_err_type err_type)
 {
        if (vport->ops->flags & VPORT_F_GEN_STATS) {
 
-               spin_lock_bh(&vport->err_stats.lock);
+               spin_lock_bh(&vport->stats_lock);
 
                switch (err_type) {
                case VPORT_E_RX_DROPPED:
@@ -1227,6 +1336,6 @@ vport_record_error(struct vport *vport, enum vport_err_type err_type)
                        break;
                };
 
-               spin_unlock_bh(&vport->err_stats.lock);
+               spin_unlock_bh(&vport->stats_lock);
        }
 }