]> git.karo-electronics.de Git - linux-beck.git/commitdiff
ovs: Turn vports with dependencies into separate modules
authorThomas Graf <tgraf@suug.ch>
Wed, 22 Oct 2014 15:29:06 +0000 (17:29 +0200)
committerDavid S. Miller <davem@davemloft.net>
Tue, 28 Oct 2014 18:43:18 +0000 (14:43 -0400)
The internal and netdev vport remain part of openvswitch.ko. Encap
vports including vxlan, gre, and geneve can be built as separate
modules and are loaded on demand. Modules can be unloaded after use.
Datapath ports keep a reference to the vport module during their
lifetime.

Allows to remove the error prone maintenance of the global list
vport_ops_list.

Signed-off-by: Thomas Graf <tgraf@suug.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/openvswitch/Kconfig
net/openvswitch/Makefile
net/openvswitch/datapath.c
net/openvswitch/vport-geneve.c
net/openvswitch/vport-gre.c
net/openvswitch/vport-internal_dev.c
net/openvswitch/vport-netdev.c
net/openvswitch/vport-netdev.h
net/openvswitch/vport-vxlan.c
net/openvswitch/vport.c
net/openvswitch/vport.h

index ba3bb8203b999bce68dba7e5d158465e1cab9e02..2a9673e39ca116d0d01d34e6a56e174f5fb7513f 100644 (file)
@@ -29,11 +29,11 @@ config OPENVSWITCH
          If unsure, say N.
 
 config OPENVSWITCH_GRE
-       bool "Open vSwitch GRE tunneling support"
+       tristate "Open vSwitch GRE tunneling support"
        depends on INET
        depends on OPENVSWITCH
-       depends on NET_IPGRE_DEMUX && !(OPENVSWITCH=y && NET_IPGRE_DEMUX=m)
-       default y
+       depends on NET_IPGRE_DEMUX
+       default OPENVSWITCH
        ---help---
          If you say Y here, then the Open vSwitch will be able create GRE
          vport.
@@ -43,11 +43,11 @@ config OPENVSWITCH_GRE
          If unsure, say Y.
 
 config OPENVSWITCH_VXLAN
-       bool "Open vSwitch VXLAN tunneling support"
+       tristate "Open vSwitch VXLAN tunneling support"
        depends on INET
        depends on OPENVSWITCH
-       depends on VXLAN && !(OPENVSWITCH=y && VXLAN=m)
-       default y
+       depends on VXLAN
+       default OPENVSWITCH
        ---help---
          If you say Y here, then the Open vSwitch will be able create vxlan vport.
 
@@ -56,11 +56,11 @@ config OPENVSWITCH_VXLAN
          If unsure, say Y.
 
 config OPENVSWITCH_GENEVE
-       bool "Open vSwitch Geneve tunneling support"
+       tristate "Open vSwitch Geneve tunneling support"
        depends on INET
        depends on OPENVSWITCH
-       depends on GENEVE && !(OPENVSWITCH=y && GENEVE=m)
-       default y
+       depends on GENEVE
+       default OPENVSWITCH
        ---help---
          If you say Y here, then the Open vSwitch will be able create geneve vport.
 
index 9a33a273c375bdad208f5fa6b36d73743c791d00..91b9478413ef1ee97602666142c65e9a2fad5228 100644 (file)
@@ -15,14 +15,6 @@ openvswitch-y := \
        vport-internal_dev.o \
        vport-netdev.o
 
-ifneq ($(CONFIG_OPENVSWITCH_GENEVE),)
-openvswitch-y += vport-geneve.o
-endif
-
-ifneq ($(CONFIG_OPENVSWITCH_VXLAN),)
-openvswitch-y += vport-vxlan.o
-endif
-
-ifneq ($(CONFIG_OPENVSWITCH_GRE),)
-openvswitch-y += vport-gre.o
-endif
+obj-$(CONFIG_OPENVSWITCH_GENEVE)+= vport-geneve.o
+obj-$(CONFIG_OPENVSWITCH_VXLAN)        += vport-vxlan.o
+obj-$(CONFIG_OPENVSWITCH_GRE)  += vport-gre.o
index e6d7255183eba31267ec29705441a019681ca617..aecddb9bb80a3f951d830d4a4fa1c64831f6c3e2 100644 (file)
@@ -59,6 +59,7 @@
 #include "vport-netdev.h"
 
 int ovs_net_id __read_mostly;
+EXPORT_SYMBOL(ovs_net_id);
 
 static struct genl_family dp_packet_genl_family;
 static struct genl_family dp_flow_genl_family;
@@ -1764,6 +1765,7 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
                return -ENOMEM;
 
        ovs_lock();
+restart:
        dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
        err = -ENODEV;
        if (!dp)
@@ -1795,8 +1797,11 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
 
        vport = new_vport(&parms);
        err = PTR_ERR(vport);
-       if (IS_ERR(vport))
+       if (IS_ERR(vport)) {
+               if (err == -EAGAIN)
+                       goto restart;
                goto exit_unlock_free;
+       }
 
        err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
                                      info->snd_seq, 0, OVS_VPORT_CMD_NEW);
@@ -2112,12 +2117,18 @@ static int __init dp_init(void)
        if (err)
                goto error_netns_exit;
 
+       err = ovs_netdev_init();
+       if (err)
+               goto error_unreg_notifier;
+
        err = dp_register_genl();
        if (err < 0)
-               goto error_unreg_notifier;
+               goto error_unreg_netdev;
 
        return 0;
 
+error_unreg_netdev:
+       ovs_netdev_exit();
 error_unreg_notifier:
        unregister_netdevice_notifier(&ovs_dp_device_notifier);
 error_netns_exit:
@@ -2137,6 +2148,7 @@ error:
 static void dp_cleanup(void)
 {
        dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
+       ovs_netdev_exit();
        unregister_netdevice_notifier(&ovs_dp_device_notifier);
        unregister_pernet_device(&ovs_net_ops);
        rcu_barrier();
index 106a9d80b663e5929ddbce87dd175b995d3113e2..70c9765011f4b5ae28c2dbc0e7f1e1f5cffd3e0c 100644 (file)
@@ -17,6 +17,7 @@
 #include <linux/rculist.h>
 #include <linux/udp.h>
 #include <linux/if_vlan.h>
+#include <linux/module.h>
 
 #include <net/geneve.h>
 #include <net/icmp.h>
@@ -28,6 +29,8 @@
 #include "datapath.h"
 #include "vport.h"
 
+static struct vport_ops ovs_geneve_vport_ops;
+
 /**
  * struct geneve_port - Keeps track of open UDP ports
  * @gs: The socket created for this port number.
@@ -225,11 +228,29 @@ static const char *geneve_get_name(const struct vport *vport)
        return geneve_port->name;
 }
 
-const struct vport_ops ovs_geneve_vport_ops = {
+static struct vport_ops ovs_geneve_vport_ops = {
        .type           = OVS_VPORT_TYPE_GENEVE,
        .create         = geneve_tnl_create,
        .destroy        = geneve_tnl_destroy,
        .get_name       = geneve_get_name,
        .get_options    = geneve_get_options,
        .send           = geneve_tnl_send,
+       .owner          = THIS_MODULE,
 };
+
+static int __init ovs_geneve_tnl_init(void)
+{
+       return ovs_vport_ops_register(&ovs_geneve_vport_ops);
+}
+
+static void __exit ovs_geneve_tnl_exit(void)
+{
+       ovs_vport_ops_unregister(&ovs_geneve_vport_ops);
+}
+
+module_init(ovs_geneve_tnl_init);
+module_exit(ovs_geneve_tnl_exit);
+
+MODULE_DESCRIPTION("OVS: Geneve swiching port");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("vport-type-5");
index 108b82da2fd94169bab809df9d771232fa0c53b5..00270b608844c2a429140e4d2015b858c403c286 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/jhash.h>
 #include <linux/list.h>
 #include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/workqueue.h>
 #include <linux/rculist.h>
 #include <net/route.h>
@@ -45,6 +46,8 @@
 #include "datapath.h"
 #include "vport.h"
 
+static struct vport_ops ovs_gre_vport_ops;
+
 /* Returns the least-significant 32 bits of a __be64. */
 static __be32 be64_get_low32(__be64 x)
 {
@@ -281,10 +284,28 @@ static void gre_tnl_destroy(struct vport *vport)
        gre_exit();
 }
 
-const struct vport_ops ovs_gre_vport_ops = {
+static struct vport_ops ovs_gre_vport_ops = {
        .type           = OVS_VPORT_TYPE_GRE,
        .create         = gre_create,
        .destroy        = gre_tnl_destroy,
        .get_name       = gre_get_name,
        .send           = gre_tnl_send,
+       .owner          = THIS_MODULE,
 };
+
+static int __init ovs_gre_tnl_init(void)
+{
+       return ovs_vport_ops_register(&ovs_gre_vport_ops);
+}
+
+static void __exit ovs_gre_tnl_exit(void)
+{
+       ovs_vport_ops_unregister(&ovs_gre_vport_ops);
+}
+
+module_init(ovs_gre_tnl_init);
+module_exit(ovs_gre_tnl_exit);
+
+MODULE_DESCRIPTION("OVS: GRE switching port");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("vport-type-3");
index 84516126e5f3a74b4b88cd87e1dd316ec11b98d9..10dc07e1678bcfceb638650869542a57399895ed 100644 (file)
@@ -36,6 +36,8 @@ struct internal_dev {
        struct vport *vport;
 };
 
+static struct vport_ops ovs_internal_vport_ops;
+
 static struct internal_dev *internal_dev_priv(struct net_device *netdev)
 {
        return netdev_priv(netdev);
@@ -238,7 +240,7 @@ static int internal_dev_recv(struct vport *vport, struct sk_buff *skb)
        return len;
 }
 
-const struct vport_ops ovs_internal_vport_ops = {
+static struct vport_ops ovs_internal_vport_ops = {
        .type           = OVS_VPORT_TYPE_INTERNAL,
        .create         = internal_dev_create,
        .destroy        = internal_dev_destroy,
@@ -261,10 +263,21 @@ struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
 
 int ovs_internal_dev_rtnl_link_register(void)
 {
-       return rtnl_link_register(&internal_dev_link_ops);
+       int err;
+
+       err = rtnl_link_register(&internal_dev_link_ops);
+       if (err < 0)
+               return err;
+
+       err = ovs_vport_ops_register(&ovs_internal_vport_ops);
+       if (err < 0)
+               rtnl_link_unregister(&internal_dev_link_ops);
+
+       return err;
 }
 
 void ovs_internal_dev_rtnl_link_unregister(void)
 {
+       ovs_vport_ops_unregister(&ovs_internal_vport_ops);
        rtnl_link_unregister(&internal_dev_link_ops);
 }
index d21f77d875ba7b690a0c46231e0b539172488562..877ee74b4f0832e19a2df7ab47db4f00289033dc 100644 (file)
@@ -33,6 +33,8 @@
 #include "vport-internal_dev.h"
 #include "vport-netdev.h"
 
+static struct vport_ops ovs_netdev_vport_ops;
+
 /* Must be called with rcu_read_lock. */
 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
 {
@@ -224,10 +226,20 @@ struct vport *ovs_netdev_get_vport(struct net_device *dev)
                return NULL;
 }
 
-const struct vport_ops ovs_netdev_vport_ops = {
+static struct vport_ops ovs_netdev_vport_ops = {
        .type           = OVS_VPORT_TYPE_NETDEV,
        .create         = netdev_create,
        .destroy        = netdev_destroy,
        .get_name       = ovs_netdev_get_name,
        .send           = netdev_send,
 };
+
+int __init ovs_netdev_init(void)
+{
+       return ovs_vport_ops_register(&ovs_netdev_vport_ops);
+}
+
+void ovs_netdev_exit(void)
+{
+       ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
+}
index 8df01c1127e546d06ba2f8c3fde44b51c58ad0de..6f7038e79c524cc66dc53188992b0ed9ff6c23ed 100644 (file)
@@ -41,4 +41,7 @@ netdev_vport_priv(const struct vport *vport)
 const char *ovs_netdev_get_name(const struct vport *);
 void ovs_netdev_detach_dev(struct vport *);
 
+int __init ovs_netdev_init(void);
+void ovs_netdev_exit(void);
+
 #endif /* vport_netdev.h */
index 2735e01dca73a40e4f60027152a79efa435724a7..965e7500c5a6da101de0743f5757e202924111a9 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/net.h>
 #include <linux/rculist.h>
 #include <linux/udp.h>
+#include <linux/module.h>
 
 #include <net/icmp.h>
 #include <net/ip.h>
@@ -50,6 +51,8 @@ struct vxlan_port {
        char name[IFNAMSIZ];
 };
 
+static struct vport_ops ovs_vxlan_vport_ops;
+
 static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
 {
        return vport_priv(vport);
@@ -192,11 +195,29 @@ static const char *vxlan_get_name(const struct vport *vport)
        return vxlan_port->name;
 }
 
-const struct vport_ops ovs_vxlan_vport_ops = {
+static struct vport_ops ovs_vxlan_vport_ops = {
        .type           = OVS_VPORT_TYPE_VXLAN,
        .create         = vxlan_tnl_create,
        .destroy        = vxlan_tnl_destroy,
        .get_name       = vxlan_get_name,
        .get_options    = vxlan_get_options,
        .send           = vxlan_tnl_send,
+       .owner          = THIS_MODULE,
 };
+
+static int __init ovs_vxlan_tnl_init(void)
+{
+       return ovs_vport_ops_register(&ovs_vxlan_vport_ops);
+}
+
+static void __exit ovs_vxlan_tnl_exit(void)
+{
+       ovs_vport_ops_unregister(&ovs_vxlan_vport_ops);
+}
+
+module_init(ovs_vxlan_tnl_init);
+module_exit(ovs_vxlan_tnl_exit);
+
+MODULE_DESCRIPTION("OVS: VXLAN switching port");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("vport-type-4");
index 6015802ebe6fee99b0b44c6f39da8f3bcd0f71f7..8168ef02133730f72d3d5d8ef17997acc7fe5a50 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;
@@ -88,6 +74,32 @@ static struct hlist_head *hash_bucket(struct net *net, const char *name)
        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(ovs_vport_ops_register);
+
+void ovs_vport_ops_unregister(struct vport_ops *ops)
+{
+       ovs_lock();
+       list_del(&ops->list);
+       ovs_unlock();
+}
+EXPORT_SYMBOL(ovs_vport_ops_unregister);
+
 /**
  *     ovs_vport_locate - find a port that has already been created
  *
@@ -153,6 +165,7 @@ struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
 
        return vport;
 }
+EXPORT_SYMBOL(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(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);
 }
 
 /**
@@ -457,6 +493,7 @@ void ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
        }
        ovs_dp_process_packet(skb, &key);
 }
+EXPORT_SYMBOL(ovs_vport_receive);
 
 /**
  *     ovs_vport_send - send a packet on a device
@@ -535,3 +572,4 @@ void ovs_vport_deferred_free(struct vport *vport)
 
        call_rcu(&vport->rcu, free_vport_rcu);
 }
+EXPORT_SYMBOL(ovs_vport_deferred_free);
index 8942125de3a6c051e9d8d818f747bd3cce023be4..e41c3facf799735518471a60af569be5dab555df 100644 (file)
@@ -161,6 +161,9 @@ struct vport_ops {
        const char *(*get_name)(const struct vport *);
 
        int (*send)(struct vport *, struct sk_buff *);
+
+       struct module *owner;
+       struct list_head list;
 };
 
 enum vport_err_type {
@@ -209,14 +212,6 @@ static inline struct vport *vport_from_priv(void *priv)
 void ovs_vport_receive(struct vport *, struct sk_buff *,
                       struct ovs_tunnel_info *);
 
-/* List of statically compiled vport implementations.  Don't forget to also
- * add yours to the list at the top of vport.c. */
-extern const struct vport_ops ovs_netdev_vport_ops;
-extern const struct vport_ops ovs_internal_vport_ops;
-extern const struct vport_ops ovs_gre_vport_ops;
-extern const struct vport_ops ovs_vxlan_vport_ops;
-extern const struct vport_ops ovs_geneve_vport_ops;
-
 static inline void ovs_skb_postpush_rcsum(struct sk_buff *skb,
                                      const void *start, unsigned int len)
 {
@@ -224,4 +219,7 @@ static inline void ovs_skb_postpush_rcsum(struct sk_buff *skb,
                skb->csum = csum_add(skb->csum, csum_partial(start, len, 0));
 }
 
+int ovs_vport_ops_register(struct vport_ops *ops);
+void ovs_vport_ops_unregister(struct vport_ops *ops);
+
 #endif /* vport.h */