datapath: Fix compilation on kernel 2.6.32
[cascardo/ovs.git] / datapath / vport.c
index 0709c33..024491f 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/kconfig.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
+#include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/percpu.h>
 #include <linux/rcupdate.h>
 static void ovs_vport_record_error(struct vport *,
                                   enum vport_err_type err_type);
 
-/* List of statically compiled vport implementations.  Don't forget to also
- * add yours to the list at the bottom of vport.h. */
-static const struct vport_ops *vport_ops_list[] = {
-       &ovs_netdev_vport_ops,
-       &ovs_internal_vport_ops,
-       &ovs_geneve_vport_ops,
-#if IS_ENABLED(CONFIG_NET_IPGRE_DEMUX)
-       &ovs_gre_vport_ops,
-       &ovs_gre64_vport_ops,
-#endif
-       &ovs_vxlan_vport_ops,
-       &ovs_lisp_vport_ops,
-};
+static LIST_HEAD(vport_ops_list);
 
 /* Protected by RCU read lock for reading, ovs_mutex for writing. */
 static struct hlist_head *dev_table;
@@ -82,12 +71,38 @@ void ovs_vport_exit(void)
        kfree(dev_table);
 }
 
-static struct hlist_head *hash_bucket(struct net *net, const char *name)
+static struct hlist_head *hash_bucket(const struct net *net, const char *name)
 {
        unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
        return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
 }
 
+int ovs_vport_ops_register(struct vport_ops *ops)
+{
+       int err = -EEXIST;
+       struct vport_ops *o;
+
+       ovs_lock();
+       list_for_each_entry(o, &vport_ops_list, list)
+       if (ops->type == o->type)
+               goto errout;
+
+       list_add_tail(&ops->list, &vport_ops_list);
+       err = 0;
+errout:
+       ovs_unlock();
+       return err;
+}
+EXPORT_SYMBOL_GPL(ovs_vport_ops_register);
+
+void ovs_vport_ops_unregister(struct vport_ops *ops)
+{
+       ovs_lock();
+       list_del(&ops->list);
+       ovs_unlock();
+}
+EXPORT_SYMBOL_GPL(ovs_vport_ops_unregister);
+
 /**
  *     ovs_vport_locate - find a port that has already been created
  *
@@ -95,7 +110,7 @@ static struct hlist_head *hash_bucket(struct net *net, const char *name)
  *
  * Must be called with ovs or RCU read lock.
  */
-struct vport *ovs_vport_locate(struct net *net, const char *name)
+struct vport *ovs_vport_locate(const struct net *net, const char *name)
 {
        struct hlist_head *bucket = hash_bucket(net, name);
        struct vport *vport;
@@ -151,10 +166,20 @@ struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
                return ERR_PTR(-ENOMEM);
        }
 
-       spin_lock_init(&vport->stats_lock);
-
        return vport;
 }
+EXPORT_SYMBOL_GPL(ovs_vport_alloc);
+
+static struct vport_ops *ovs_vport_lookup(const struct vport_parms *parms)
+{
+       struct vport_ops *ops;
+
+       list_for_each_entry(ops, &vport_ops_list, list)
+               if (ops->type == parms->type)
+                       return ops;
+
+       return NULL;
+}
 
 /**
  *     ovs_vport_free - uninitialize and free vport
@@ -168,10 +193,11 @@ struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
  */
 void ovs_vport_free(struct vport *vport)
 {
-       kfree((struct vport_portids __force *)vport->upcall_portids);
+       kfree(rcu_dereference_raw(vport->upcall_portids));
        free_percpu(vport->percpu_stats);
        kfree(vport);
 }
+EXPORT_SYMBOL_GPL(ovs_vport_free);
 
 /**
  *     ovs_vport_add - add vport device (for kernel callers)
@@ -183,31 +209,40 @@ void ovs_vport_free(struct vport *vport)
  */
 struct vport *ovs_vport_add(const struct vport_parms *parms)
 {
+       struct vport_ops *ops;
        struct vport *vport;
-       int err = 0;
-       int i;
 
-       for (i = 0; i < ARRAY_SIZE(vport_ops_list); i++) {
-               if (vport_ops_list[i]->type == parms->type) {
-                       struct hlist_head *bucket;
+       ops = ovs_vport_lookup(parms);
+       if (ops) {
+               struct hlist_head *bucket;
 
-                       vport = vport_ops_list[i]->create(parms);
-                       if (IS_ERR(vport)) {
-                               err = PTR_ERR(vport);
-                               goto out;
-                       }
+               if (!try_module_get(ops->owner))
+                       return ERR_PTR(-EAFNOSUPPORT);
 
-                       bucket = hash_bucket(ovs_dp_get_net(vport->dp),
-                                            vport->ops->get_name(vport));
-                       hlist_add_head_rcu(&vport->hash_node, bucket);
+               vport = ops->create(parms);
+               if (IS_ERR(vport)) {
+                       module_put(ops->owner);
                        return vport;
                }
+
+               bucket = hash_bucket(ovs_dp_get_net(vport->dp),
+                                    vport->ops->get_name(vport));
+               hlist_add_head_rcu(&vport->hash_node, bucket);
+               return vport;
        }
 
-       err = -EAFNOSUPPORT;
+       /* Unlock to attempt module load and return -EAGAIN if load
+        * was successful as we need to restart the port addition
+        * workflow.
+        */
+       ovs_unlock();
+       request_module("vport-type-%d", parms->type);
+       ovs_lock();
 
-out:
-       return ERR_PTR(err);
+       if (!ovs_vport_lookup(parms))
+               return ERR_PTR(-EAFNOSUPPORT);
+       else
+               return ERR_PTR(-EAGAIN);
 }
 
 /**
@@ -239,6 +274,7 @@ void ovs_vport_del(struct vport *vport)
        ASSERT_OVSL();
 
        hlist_del_rcu(&vport->hash_node);
+       module_put(vport->ops->owner);
        vport->ops->destroy(vport);
 }
 
@@ -266,14 +302,10 @@ void ovs_vport_get_stats(struct vport *vport, struct ovs_vport_stats *stats)
         * netdev-stats can be directly read over netlink-ioctl.
         */
 
-       spin_lock_bh(&vport->stats_lock);
-
-       stats->rx_errors        = vport->err_stats.rx_errors;
-       stats->tx_errors        = vport->err_stats.tx_errors;
-       stats->tx_dropped       = vport->err_stats.tx_dropped;
-       stats->rx_dropped       = vport->err_stats.rx_dropped;
-
-       spin_unlock_bh(&vport->stats_lock);
+       stats->rx_errors  = atomic_long_read(&vport->err_stats.rx_errors);
+       stats->tx_errors  = atomic_long_read(&vport->err_stats.tx_errors);
+       stats->tx_dropped = atomic_long_read(&vport->err_stats.tx_dropped);
+       stats->rx_dropped = atomic_long_read(&vport->err_stats.rx_dropped);
 
        stats->rx_bytes         = 0;
        stats->rx_packets       = 0;
@@ -358,7 +390,7 @@ static void vport_portids_destroy_rcu_cb(struct rcu_head *rcu)
  *
  * Must be called with ovs_mutex.
  */
-int ovs_vport_set_upcall_portids(struct vport *vport,  struct nlattr *ids)
+int ovs_vport_set_upcall_portids(struct vport *vport, const struct nlattr *ids)
 {
        struct vport_portids *old, *vport_portids;
 
@@ -423,12 +455,12 @@ int ovs_vport_get_upcall_portids(const struct vport *vport,
  *
  * Returns the portid of the target socket.  Must be called with rcu_read_lock.
  */
-u32 ovs_vport_find_upcall_portid(const struct vport *p, struct sk_buff *skb)
+u32 ovs_vport_find_upcall_portid(const struct vport *vport, struct sk_buff *skb)
 {
        struct vport_portids *ids;
        u32 hash;
 
-       ids = rcu_dereference(p->upcall_portids);
+       ids = rcu_dereference(vport->upcall_portids);
 
        if (ids->n_ids == 1 && ids->ids[0] == 0)
                return 0;
@@ -449,7 +481,7 @@ u32 ovs_vport_find_upcall_portid(const struct vport *p, struct sk_buff *skb)
  * called compute_ip_summed() to initialize the checksumming fields.
  */
 void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
-                      struct ovs_tunnel_info *tun_info)
+                      const struct ovs_tunnel_info *tun_info)
 {
        struct pcpu_sw_netstats *stats;
        struct sw_flow_key key;
@@ -458,7 +490,7 @@ void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
        stats = this_cpu_ptr(vport->percpu_stats);
        u64_stats_update_begin(&stats->syncp);
        stats->rx_packets++;
-       stats->rx_bytes += skb->len;
+       stats->rx_bytes += skb->len + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
        u64_stats_update_end(&stats->syncp);
 
        ovs_skb_init_inner_protocol(skb);
@@ -469,9 +501,9 @@ void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
                kfree_skb(skb);
                return;
        }
-
-       ovs_dp_process_packet(skb);
+       ovs_dp_process_packet(skb, &key);
 }
+EXPORT_SYMBOL_GPL(ovs_vport_receive);
 
 /**
  *     ovs_vport_send - send a packet on a device
@@ -497,10 +529,9 @@ int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
                u64_stats_update_end(&stats->syncp);
        } else if (sent < 0) {
                ovs_vport_record_error(vport, VPORT_E_TX_ERROR);
-               kfree_skb(skb);
-       } else
+       } else {
                ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
-
+       }
        return sent;
 }
 
@@ -516,27 +547,24 @@ int ovs_vport_send(struct vport *vport, struct sk_buff *skb)
 static void ovs_vport_record_error(struct vport *vport,
                                   enum vport_err_type err_type)
 {
-       spin_lock(&vport->stats_lock);
-
        switch (err_type) {
        case VPORT_E_RX_DROPPED:
-               vport->err_stats.rx_dropped++;
+               atomic_long_inc(&vport->err_stats.rx_dropped);
                break;
 
        case VPORT_E_RX_ERROR:
-               vport->err_stats.rx_errors++;
+               atomic_long_inc(&vport->err_stats.rx_errors);
                break;
 
        case VPORT_E_TX_DROPPED:
-               vport->err_stats.tx_dropped++;
+               atomic_long_inc(&vport->err_stats.tx_dropped);
                break;
 
        case VPORT_E_TX_ERROR:
-               vport->err_stats.tx_errors++;
+               atomic_long_inc(&vport->err_stats.tx_errors);
                break;
        }
 
-       spin_unlock(&vport->stats_lock);
 }
 
 static void free_vport_rcu(struct rcu_head *rcu)
@@ -553,6 +581,7 @@ void ovs_vport_deferred_free(struct vport *vport)
 
        call_rcu(&vport->rcu, free_vport_rcu);
 }
+EXPORT_SYMBOL_GPL(ovs_vport_deferred_free);
 
 int ovs_tunnel_get_egress_info(struct ovs_tunnel_info *egress_tun_info,
                               struct net *net,
@@ -601,6 +630,7 @@ int ovs_tunnel_get_egress_info(struct ovs_tunnel_info *egress_tun_info,
 
        return 0;
 }
+EXPORT_SYMBOL_GPL(ovs_tunnel_get_egress_info);
 
 int ovs_vport_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
                                  struct ovs_tunnel_info *info)