tap: tap as an independent module
authorSainath Grandhi <sainath.grandhi@intel.com>
Sat, 11 Feb 2017 00:03:51 +0000 (16:03 -0800)
committerDavid S. Miller <davem@davemloft.net>
Sun, 12 Feb 2017 01:59:41 +0000 (20:59 -0500)
This patch makes tap a separate module for other types of virtual interfaces, for example,
ipvlan to use.

Signed-off-by: Sainath Grandhi <sainath.grandhi@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/Kconfig
drivers/net/Makefile
drivers/net/macvtap.c [new file with mode: 0644]
drivers/net/macvtap_main.c [deleted file]
drivers/net/tap.c
drivers/vhost/Kconfig
include/linux/if_tap.h

index a993cbeb9e0c84326a63369226b14ed008702a13..5763503fe4e640a5041788fb360fadd51007a3c3 100644 (file)
@@ -135,6 +135,7 @@ config MACVTAP
        tristate "MAC-VLAN based tap driver"
        depends on MACVLAN
        depends on INET
+       select TAP
        help
          This adds a specialized tap character device driver that is based
          on the MAC-VLAN network interface, called macvtap. A macvtap device
@@ -287,6 +288,12 @@ config TUN
 
          If you don't know what to use this for, you don't need it.
 
+config TAP
+       tristate
+       ---help---
+         This option is selected by any driver implementing tap user space
+         interface for a virtual interface to re-use core tap functionality.
+
 config TUN_VNET_CROSS_LE
        bool "Support for cross-endian vnet headers on little-endian kernels"
        default n
index 19b03a9fe0f6edc485e71cdbeb3decb30569c5d2..7dd86ca02d0d4c48f53f0e7846bae0a366748f51 100644 (file)
@@ -21,6 +21,7 @@ obj-$(CONFIG_PHYLIB) += phy/
 obj-$(CONFIG_RIONET) += rionet.o
 obj-$(CONFIG_NET_TEAM) += team/
 obj-$(CONFIG_TUN) += tun.o
+obj-$(CONFIG_TAP) += tap.o
 obj-$(CONFIG_VETH) += veth.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
 obj-$(CONFIG_VXLAN) += vxlan.o
@@ -29,8 +30,6 @@ obj-$(CONFIG_GTP) += gtp.o
 obj-$(CONFIG_NLMON) += nlmon.o
 obj-$(CONFIG_NET_VRF) += vrf.o
 
-macvtap-objs := macvtap_main.o tap.o
-
 #
 # Networking Drivers
 #
diff --git a/drivers/net/macvtap.c b/drivers/net/macvtap.c
new file mode 100644 (file)
index 0000000..a4bfc10
--- /dev/null
@@ -0,0 +1,249 @@
+#include <linux/etherdevice.h>
+#include <linux/if_macvlan.h>
+#include <linux/if_tap.h>
+#include <linux/if_vlan.h>
+#include <linux/interrupt.h>
+#include <linux/nsproxy.h>
+#include <linux/compat.h>
+#include <linux/if_tun.h>
+#include <linux/module.h>
+#include <linux/skbuff.h>
+#include <linux/cache.h>
+#include <linux/sched.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/wait.h>
+#include <linux/cdev.h>
+#include <linux/idr.h>
+#include <linux/fs.h>
+#include <linux/uio.h>
+
+#include <net/net_namespace.h>
+#include <net/rtnetlink.h>
+#include <net/sock.h>
+#include <linux/virtio_net.h>
+#include <linux/skb_array.h>
+
+struct macvtap_dev {
+       struct macvlan_dev vlan;
+       struct tap_dev    tap;
+};
+
+/*
+ * Variables for dealing with macvtaps device numbers.
+ */
+static dev_t macvtap_major;
+
+static const void *macvtap_net_namespace(struct device *d)
+{
+       struct net_device *dev = to_net_dev(d->parent);
+       return dev_net(dev);
+}
+
+static struct class macvtap_class = {
+       .name = "macvtap",
+       .owner = THIS_MODULE,
+       .ns_type = &net_ns_type_operations,
+       .namespace = macvtap_net_namespace,
+};
+static struct cdev macvtap_cdev;
+
+#define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
+                     NETIF_F_TSO6 | NETIF_F_UFO)
+
+static void macvtap_count_tx_dropped(struct tap_dev *tap)
+{
+       struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
+       struct macvlan_dev *vlan = &vlantap->vlan;
+
+       this_cpu_inc(vlan->pcpu_stats->tx_dropped);
+}
+
+static void macvtap_count_rx_dropped(struct tap_dev *tap)
+{
+       struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
+       struct macvlan_dev *vlan = &vlantap->vlan;
+
+       macvlan_count_rx(vlan, 0, 0, 0);
+}
+
+static void macvtap_update_features(struct tap_dev *tap,
+                                   netdev_features_t features)
+{
+       struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
+       struct macvlan_dev *vlan = &vlantap->vlan;
+
+       vlan->set_features = features;
+       netdev_update_features(vlan->dev);
+}
+
+static int macvtap_newlink(struct net *src_net,
+                          struct net_device *dev,
+                          struct nlattr *tb[],
+                          struct nlattr *data[])
+{
+       struct macvtap_dev *vlantap = netdev_priv(dev);
+       int err;
+
+       INIT_LIST_HEAD(&vlantap->tap.queue_list);
+
+       /* Since macvlan supports all offloads by default, make
+        * tap support all offloads also.
+        */
+       vlantap->tap.tap_features = TUN_OFFLOADS;
+
+       /* Register callbacks for rx/tx drops accounting and updating
+        * net_device features
+        */
+       vlantap->tap.count_tx_dropped = macvtap_count_tx_dropped;
+       vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped;
+       vlantap->tap.update_features  = macvtap_update_features;
+
+       err = netdev_rx_handler_register(dev, tap_handle_frame, &vlantap->tap);
+       if (err)
+               return err;
+
+       /* Don't put anything that may fail after macvlan_common_newlink
+        * because we can't undo what it does.
+        */
+       err = macvlan_common_newlink(src_net, dev, tb, data);
+       if (err) {
+               netdev_rx_handler_unregister(dev);
+               return err;
+       }
+
+       vlantap->tap.dev = vlantap->vlan.dev;
+
+       return 0;
+}
+
+static void macvtap_dellink(struct net_device *dev,
+                           struct list_head *head)
+{
+       struct macvtap_dev *vlantap = netdev_priv(dev);
+
+       netdev_rx_handler_unregister(dev);
+       tap_del_queues(&vlantap->tap);
+       macvlan_dellink(dev, head);
+}
+
+static void macvtap_setup(struct net_device *dev)
+{
+       macvlan_common_setup(dev);
+       dev->tx_queue_len = TUN_READQ_SIZE;
+}
+
+static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
+       .kind           = "macvtap",
+       .setup          = macvtap_setup,
+       .newlink        = macvtap_newlink,
+       .dellink        = macvtap_dellink,
+       .priv_size      = sizeof(struct macvtap_dev),
+};
+
+static int macvtap_device_event(struct notifier_block *unused,
+                               unsigned long event, void *ptr)
+{
+       struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+       struct macvtap_dev *vlantap;
+       struct device *classdev;
+       dev_t devt;
+       int err;
+       char tap_name[IFNAMSIZ];
+
+       if (dev->rtnl_link_ops != &macvtap_link_ops)
+               return NOTIFY_DONE;
+
+       snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
+       vlantap = netdev_priv(dev);
+
+       switch (event) {
+       case NETDEV_REGISTER:
+               /* Create the device node here after the network device has
+                * been registered but before register_netdevice has
+                * finished running.
+                */
+               err = tap_get_minor(macvtap_major, &vlantap->tap);
+               if (err)
+                       return notifier_from_errno(err);
+
+               devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
+               classdev = device_create(&macvtap_class, &dev->dev, devt,
+                                        dev, tap_name);
+               if (IS_ERR(classdev)) {
+                       tap_free_minor(macvtap_major, &vlantap->tap);
+                       return notifier_from_errno(PTR_ERR(classdev));
+               }
+               err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
+                                       tap_name);
+               if (err)
+                       return notifier_from_errno(err);
+               break;
+       case NETDEV_UNREGISTER:
+               /* vlan->minor == 0 if NETDEV_REGISTER above failed */
+               if (vlantap->tap.minor == 0)
+                       break;
+               sysfs_remove_link(&dev->dev.kobj, tap_name);
+               devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
+               device_destroy(&macvtap_class, devt);
+               tap_free_minor(macvtap_major, &vlantap->tap);
+               break;
+       case NETDEV_CHANGE_TX_QUEUE_LEN:
+               if (tap_queue_resize(&vlantap->tap))
+                       return NOTIFY_BAD;
+               break;
+       }
+
+       return NOTIFY_DONE;
+}
+
+static struct notifier_block macvtap_notifier_block __read_mostly = {
+       .notifier_call  = macvtap_device_event,
+};
+
+static int macvtap_init(void)
+{
+       int err;
+
+       err = tap_create_cdev(&macvtap_cdev, &macvtap_major, "macvtap");
+
+       if (err)
+               goto out1;
+
+       err = class_register(&macvtap_class);
+       if (err)
+               goto out2;
+
+       err = register_netdevice_notifier(&macvtap_notifier_block);
+       if (err)
+               goto out3;
+
+       err = macvlan_link_register(&macvtap_link_ops);
+       if (err)
+               goto out4;
+
+       return 0;
+
+out4:
+       unregister_netdevice_notifier(&macvtap_notifier_block);
+out3:
+       class_unregister(&macvtap_class);
+out2:
+       tap_destroy_cdev(macvtap_major, &macvtap_cdev);
+out1:
+       return err;
+}
+module_init(macvtap_init);
+
+static void macvtap_exit(void)
+{
+       rtnl_link_unregister(&macvtap_link_ops);
+       unregister_netdevice_notifier(&macvtap_notifier_block);
+       class_unregister(&macvtap_class);
+       tap_destroy_cdev(macvtap_major, &macvtap_cdev);
+}
+module_exit(macvtap_exit);
+
+MODULE_ALIAS_RTNL_LINK("macvtap");
+MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
+MODULE_LICENSE("GPL");
diff --git a/drivers/net/macvtap_main.c b/drivers/net/macvtap_main.c
deleted file mode 100644 (file)
index a4bfc10..0000000
+++ /dev/null
@@ -1,249 +0,0 @@
-#include <linux/etherdevice.h>
-#include <linux/if_macvlan.h>
-#include <linux/if_tap.h>
-#include <linux/if_vlan.h>
-#include <linux/interrupt.h>
-#include <linux/nsproxy.h>
-#include <linux/compat.h>
-#include <linux/if_tun.h>
-#include <linux/module.h>
-#include <linux/skbuff.h>
-#include <linux/cache.h>
-#include <linux/sched.h>
-#include <linux/types.h>
-#include <linux/slab.h>
-#include <linux/wait.h>
-#include <linux/cdev.h>
-#include <linux/idr.h>
-#include <linux/fs.h>
-#include <linux/uio.h>
-
-#include <net/net_namespace.h>
-#include <net/rtnetlink.h>
-#include <net/sock.h>
-#include <linux/virtio_net.h>
-#include <linux/skb_array.h>
-
-struct macvtap_dev {
-       struct macvlan_dev vlan;
-       struct tap_dev    tap;
-};
-
-/*
- * Variables for dealing with macvtaps device numbers.
- */
-static dev_t macvtap_major;
-
-static const void *macvtap_net_namespace(struct device *d)
-{
-       struct net_device *dev = to_net_dev(d->parent);
-       return dev_net(dev);
-}
-
-static struct class macvtap_class = {
-       .name = "macvtap",
-       .owner = THIS_MODULE,
-       .ns_type = &net_ns_type_operations,
-       .namespace = macvtap_net_namespace,
-};
-static struct cdev macvtap_cdev;
-
-#define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
-                     NETIF_F_TSO6 | NETIF_F_UFO)
-
-static void macvtap_count_tx_dropped(struct tap_dev *tap)
-{
-       struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
-       struct macvlan_dev *vlan = &vlantap->vlan;
-
-       this_cpu_inc(vlan->pcpu_stats->tx_dropped);
-}
-
-static void macvtap_count_rx_dropped(struct tap_dev *tap)
-{
-       struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
-       struct macvlan_dev *vlan = &vlantap->vlan;
-
-       macvlan_count_rx(vlan, 0, 0, 0);
-}
-
-static void macvtap_update_features(struct tap_dev *tap,
-                                   netdev_features_t features)
-{
-       struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
-       struct macvlan_dev *vlan = &vlantap->vlan;
-
-       vlan->set_features = features;
-       netdev_update_features(vlan->dev);
-}
-
-static int macvtap_newlink(struct net *src_net,
-                          struct net_device *dev,
-                          struct nlattr *tb[],
-                          struct nlattr *data[])
-{
-       struct macvtap_dev *vlantap = netdev_priv(dev);
-       int err;
-
-       INIT_LIST_HEAD(&vlantap->tap.queue_list);
-
-       /* Since macvlan supports all offloads by default, make
-        * tap support all offloads also.
-        */
-       vlantap->tap.tap_features = TUN_OFFLOADS;
-
-       /* Register callbacks for rx/tx drops accounting and updating
-        * net_device features
-        */
-       vlantap->tap.count_tx_dropped = macvtap_count_tx_dropped;
-       vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped;
-       vlantap->tap.update_features  = macvtap_update_features;
-
-       err = netdev_rx_handler_register(dev, tap_handle_frame, &vlantap->tap);
-       if (err)
-               return err;
-
-       /* Don't put anything that may fail after macvlan_common_newlink
-        * because we can't undo what it does.
-        */
-       err = macvlan_common_newlink(src_net, dev, tb, data);
-       if (err) {
-               netdev_rx_handler_unregister(dev);
-               return err;
-       }
-
-       vlantap->tap.dev = vlantap->vlan.dev;
-
-       return 0;
-}
-
-static void macvtap_dellink(struct net_device *dev,
-                           struct list_head *head)
-{
-       struct macvtap_dev *vlantap = netdev_priv(dev);
-
-       netdev_rx_handler_unregister(dev);
-       tap_del_queues(&vlantap->tap);
-       macvlan_dellink(dev, head);
-}
-
-static void macvtap_setup(struct net_device *dev)
-{
-       macvlan_common_setup(dev);
-       dev->tx_queue_len = TUN_READQ_SIZE;
-}
-
-static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
-       .kind           = "macvtap",
-       .setup          = macvtap_setup,
-       .newlink        = macvtap_newlink,
-       .dellink        = macvtap_dellink,
-       .priv_size      = sizeof(struct macvtap_dev),
-};
-
-static int macvtap_device_event(struct notifier_block *unused,
-                               unsigned long event, void *ptr)
-{
-       struct net_device *dev = netdev_notifier_info_to_dev(ptr);
-       struct macvtap_dev *vlantap;
-       struct device *classdev;
-       dev_t devt;
-       int err;
-       char tap_name[IFNAMSIZ];
-
-       if (dev->rtnl_link_ops != &macvtap_link_ops)
-               return NOTIFY_DONE;
-
-       snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
-       vlantap = netdev_priv(dev);
-
-       switch (event) {
-       case NETDEV_REGISTER:
-               /* Create the device node here after the network device has
-                * been registered but before register_netdevice has
-                * finished running.
-                */
-               err = tap_get_minor(macvtap_major, &vlantap->tap);
-               if (err)
-                       return notifier_from_errno(err);
-
-               devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
-               classdev = device_create(&macvtap_class, &dev->dev, devt,
-                                        dev, tap_name);
-               if (IS_ERR(classdev)) {
-                       tap_free_minor(macvtap_major, &vlantap->tap);
-                       return notifier_from_errno(PTR_ERR(classdev));
-               }
-               err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
-                                       tap_name);
-               if (err)
-                       return notifier_from_errno(err);
-               break;
-       case NETDEV_UNREGISTER:
-               /* vlan->minor == 0 if NETDEV_REGISTER above failed */
-               if (vlantap->tap.minor == 0)
-                       break;
-               sysfs_remove_link(&dev->dev.kobj, tap_name);
-               devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
-               device_destroy(&macvtap_class, devt);
-               tap_free_minor(macvtap_major, &vlantap->tap);
-               break;
-       case NETDEV_CHANGE_TX_QUEUE_LEN:
-               if (tap_queue_resize(&vlantap->tap))
-                       return NOTIFY_BAD;
-               break;
-       }
-
-       return NOTIFY_DONE;
-}
-
-static struct notifier_block macvtap_notifier_block __read_mostly = {
-       .notifier_call  = macvtap_device_event,
-};
-
-static int macvtap_init(void)
-{
-       int err;
-
-       err = tap_create_cdev(&macvtap_cdev, &macvtap_major, "macvtap");
-
-       if (err)
-               goto out1;
-
-       err = class_register(&macvtap_class);
-       if (err)
-               goto out2;
-
-       err = register_netdevice_notifier(&macvtap_notifier_block);
-       if (err)
-               goto out3;
-
-       err = macvlan_link_register(&macvtap_link_ops);
-       if (err)
-               goto out4;
-
-       return 0;
-
-out4:
-       unregister_netdevice_notifier(&macvtap_notifier_block);
-out3:
-       class_unregister(&macvtap_class);
-out2:
-       tap_destroy_cdev(macvtap_major, &macvtap_cdev);
-out1:
-       return err;
-}
-module_init(macvtap_init);
-
-static void macvtap_exit(void)
-{
-       rtnl_link_unregister(&macvtap_link_ops);
-       unregister_netdevice_notifier(&macvtap_notifier_block);
-       class_unregister(&macvtap_class);
-       tap_destroy_cdev(macvtap_major, &macvtap_cdev);
-}
-module_exit(macvtap_exit);
-
-MODULE_ALIAS_RTNL_LINK("macvtap");
-MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
-MODULE_LICENSE("GPL");
index 71bbf0b6327db4b54b73bf13d42def70a8d3cd64..35b55a2fa1a1540d742bf8b18981a630fddf9bb2 100644 (file)
@@ -312,6 +312,7 @@ void tap_del_queues(struct tap_dev *tap)
        /* guarantee that any future tap_set_queue will fail */
        tap->numvtaps = MAX_TAP_QUEUES;
 }
+EXPORT_SYMBOL_GPL(tap_del_queues);
 
 rx_handler_result_t tap_handle_frame(struct sk_buff **pskb)
 {
@@ -389,6 +390,7 @@ drop:
        kfree_skb(skb);
        return RX_HANDLER_CONSUMED;
 }
+EXPORT_SYMBOL_GPL(tap_handle_frame);
 
 static struct major_info *tap_get_major(int major)
 {
@@ -428,6 +430,7 @@ unlock:
        rcu_read_unlock();
        return retval < 0 ? retval : 0;
 }
+EXPORT_SYMBOL_GPL(tap_get_minor);
 
 void tap_free_minor(dev_t major, struct tap_dev *tap)
 {
@@ -449,6 +452,7 @@ void tap_free_minor(dev_t major, struct tap_dev *tap)
 unlock:
        rcu_read_unlock();
 }
+EXPORT_SYMBOL_GPL(tap_free_minor);
 
 static struct tap_dev *dev_get_by_tap_file(int major, int minor)
 {
@@ -1210,6 +1214,7 @@ int tap_queue_resize(struct tap_dev *tap)
        kfree(arrays);
        return ret;
 }
+EXPORT_SYMBOL_GPL(tap_queue_resize);
 
 static int tap_list_add(dev_t major, const char *device_name)
 {
@@ -1257,6 +1262,7 @@ out2:
 out1:
        return err;
 }
+EXPORT_SYMBOL_GPL(tap_create_cdev);
 
 void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
 {
@@ -1272,3 +1278,8 @@ void tap_destroy_cdev(dev_t major, struct cdev *tap_cdev)
                }
        }
 }
+EXPORT_SYMBOL_GPL(tap_destroy_cdev);
+
+MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
+MODULE_AUTHOR("Sainath Grandhi <sainath.grandhi@intel.com>");
+MODULE_LICENSE("GPL");
index 40764ecad9ce16724535b9867bb6a1aa7758403b..cfdecea5078f4095546adcee397213cd0f2b67b1 100644 (file)
@@ -1,6 +1,6 @@
 config VHOST_NET
        tristate "Host kernel accelerator for virtio net"
-       depends on NET && EVENTFD && (TUN || !TUN) && (MACVTAP || !MACVTAP)
+       depends on NET && EVENTFD && (TUN || !TUN) && (TAP || !TAP)
        select VHOST
        ---help---
          This kernel module can be loaded in host kernel to accelerate
index 362e71c16efb695db52b40f06fb360b00551e845..3482c3c2037d8c35a3825bc1b16ff573c9d60905 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef _LINUX_IF_TAP_H_
 #define _LINUX_IF_TAP_H_
 
-#if IS_ENABLED(CONFIG_MACVTAP)
+#if IS_ENABLED(CONFIG_TAP)
 struct socket *tap_get_socket(struct file *);
 #else
 #include <linux/err.h>
@@ -12,7 +12,7 @@ static inline struct socket *tap_get_socket(struct file *f)
 {
        return ERR_PTR(-EINVAL);
 }
-#endif /* CONFIG_MACVTAP */
+#endif /* CONFIG_TAP */
 
 #include <net/sock.h>
 #include <linux/skb_array.h>