Merge tag 'driver-core-3.19-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / net / openvswitch / vport.c
index 6015802ebe6fee99b0b44c6f39da8f3bcd0f71f7..9584526c077804f21d22726d4bb5bd7cc4260d36 100644 (file)
@@ -28,6 +28,7 @@
 #include <linux/rtnetlink.h>
 #include <linux/compat.h>
 #include <net/net_namespace.h>
+#include <linux/module.h>
 
 #include "datapath.h"
 #include "vport.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,
-
-#ifdef CONFIG_OPENVSWITCH_GRE
-       &ovs_gre_vport_ops,
-#endif
-#ifdef CONFIG_OPENVSWITCH_VXLAN
-       &ovs_vxlan_vport_ops,
-#endif
-#ifdef CONFIG_OPENVSWITCH_GENEVE
-       &ovs_geneve_vport_ops,
-#endif
-};
+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 +68,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 +107,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;
@@ -153,6 +165,7 @@ struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
 
        return vport;
 }
+EXPORT_SYMBOL_GPL(ovs_vport_alloc);
 
 /**
  *     ovs_vport_free - uninitialize and free vport
@@ -173,6 +186,18 @@ void ovs_vport_free(struct vport *vport)
        free_percpu(vport->percpu_stats);
        kfree(vport);
 }
+EXPORT_SYMBOL_GPL(ovs_vport_free);
+
+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_add - add vport device (for kernel callers)
@@ -184,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);
 }
 
 /**
@@ -242,6 +276,8 @@ void ovs_vport_del(struct vport *vport)
        hlist_del_rcu(&vport->hash_node);
 
        vport->ops->destroy(vport);
+
+       module_put(vport->ops->owner);
 }
 
 /**
@@ -344,7 +380,7 @@ int ovs_vport_get_options(const struct vport *vport, struct sk_buff *skb)
  *
  * 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;
 
@@ -435,7 +471,7 @@ u32 ovs_vport_find_upcall_portid(const struct vport *vport, struct sk_buff *skb)
  * skb->data should point to the Ethernet header.
  */
 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;
@@ -457,6 +493,7 @@ void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
        }
        ovs_dp_process_packet(skb, &key);
 }
+EXPORT_SYMBOL_GPL(ovs_vport_receive);
 
 /**
  *     ovs_vport_send - send a packet on a device
@@ -535,3 +572,65 @@ 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,
+                              const struct ovs_tunnel_info *tun_info,
+                              u8 ipproto,
+                              u32 skb_mark,
+                              __be16 tp_src,
+                              __be16 tp_dst)
+{
+       const struct ovs_key_ipv4_tunnel *tun_key;
+       struct rtable *rt;
+       struct flowi4 fl;
+
+       if (unlikely(!tun_info))
+               return -EINVAL;
+
+       tun_key = &tun_info->tunnel;
+
+       /* Route lookup to get srouce IP address.
+        * The process may need to be changed if the corresponding process
+        * in vports ops changed.
+        */
+       memset(&fl, 0, sizeof(fl));
+       fl.daddr = tun_key->ipv4_dst;
+       fl.saddr = tun_key->ipv4_src;
+       fl.flowi4_tos = RT_TOS(tun_key->ipv4_tos);
+       fl.flowi4_mark = skb_mark;
+       fl.flowi4_proto = ipproto;
+
+       rt = ip_route_output_key(net, &fl);
+       if (IS_ERR(rt))
+               return PTR_ERR(rt);
+
+       ip_rt_put(rt);
+
+       /* Generate egress_tun_info based on tun_info,
+        * saddr, tp_src and tp_dst
+        */
+       __ovs_flow_tun_info_init(egress_tun_info,
+                                fl.saddr, tun_key->ipv4_dst,
+                                tun_key->ipv4_tos,
+                                tun_key->ipv4_ttl,
+                                tp_src, tp_dst,
+                                tun_key->tun_id,
+                                tun_key->tun_flags,
+                                tun_info->options,
+                                tun_info->options_len);
+
+       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)
+{
+       /* get_egress_tun_info() is only implemented on tunnel ports. */
+       if (unlikely(!vport->ops->get_egress_tun_info))
+               return -EINVAL;
+
+       return vport->ops->get_egress_tun_info(vport, skb, info);
+}