macvtap.c 6.0 KB
Newer Older
S
Sainath Grandhi 已提交
1 2
#include <linux/etherdevice.h>
#include <linux/if_macvlan.h>
3
#include <linux/if_tap.h>
S
Sainath Grandhi 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#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>

27 28 29 30 31
struct macvtap_dev {
	struct macvlan_dev vlan;
	struct tap_dev    tap;
};

S
Sainath Grandhi 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/*
 * 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)

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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);
}

S
Sainath Grandhi 已提交
80 81 82 83 84
static int macvtap_newlink(struct net *src_net,
			   struct net_device *dev,
			   struct nlattr *tb[],
			   struct nlattr *data[])
{
85
	struct macvtap_dev *vlantap = netdev_priv(dev);
S
Sainath Grandhi 已提交
86 87
	int err;

88
	INIT_LIST_HEAD(&vlantap->tap.queue_list);
S
Sainath Grandhi 已提交
89 90 91 92

	/* Since macvlan supports all offloads by default, make
	 * tap support all offloads also.
	 */
93
	vlantap->tap.tap_features = TUN_OFFLOADS;
S
Sainath Grandhi 已提交
94

95 96 97 98 99 100 101 102
	/* 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);
S
Sainath Grandhi 已提交
103 104 105 106 107 108 109 110 111 112 113 114
	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;
	}

115 116
	vlantap->tap.dev = vlantap->vlan.dev;

S
Sainath Grandhi 已提交
117 118 119 120 121 122
	return 0;
}

static void macvtap_dellink(struct net_device *dev,
			    struct list_head *head)
{
123 124
	struct macvtap_dev *vlantap = netdev_priv(dev);

S
Sainath Grandhi 已提交
125
	netdev_rx_handler_unregister(dev);
126
	tap_del_queues(&vlantap->tap);
S
Sainath Grandhi 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140
	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,
141
	.priv_size      = sizeof(struct macvtap_dev),
S
Sainath Grandhi 已提交
142 143 144 145 146 147
};

static int macvtap_device_event(struct notifier_block *unused,
				unsigned long event, void *ptr)
{
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
148
	struct macvtap_dev *vlantap;
S
Sainath Grandhi 已提交
149 150 151 152 153 154 155 156 157
	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);
158
	vlantap = netdev_priv(dev);
S
Sainath Grandhi 已提交
159 160 161 162 163 164 165

	switch (event) {
	case NETDEV_REGISTER:
		/* Create the device node here after the network device has
		 * been registered but before register_netdevice has
		 * finished running.
		 */
166
		err = tap_get_minor(macvtap_major, &vlantap->tap);
S
Sainath Grandhi 已提交
167 168 169
		if (err)
			return notifier_from_errno(err);

170
		devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
S
Sainath Grandhi 已提交
171 172 173
		classdev = device_create(&macvtap_class, &dev->dev, devt,
					 dev, tap_name);
		if (IS_ERR(classdev)) {
174
			tap_free_minor(macvtap_major, &vlantap->tap);
S
Sainath Grandhi 已提交
175 176 177 178 179 180 181 182 183
			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 */
184
		if (vlantap->tap.minor == 0)
S
Sainath Grandhi 已提交
185 186
			break;
		sysfs_remove_link(&dev->dev.kobj, tap_name);
187
		devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
S
Sainath Grandhi 已提交
188
		device_destroy(&macvtap_class, devt);
189
		tap_free_minor(macvtap_major, &vlantap->tap);
S
Sainath Grandhi 已提交
190 191
		break;
	case NETDEV_CHANGE_TX_QUEUE_LEN:
192
		if (tap_queue_resize(&vlantap->tap))
S
Sainath Grandhi 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
			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;

208
	err = tap_create_cdev(&macvtap_cdev, &macvtap_major, "macvtap");
S
Sainath Grandhi 已提交
209 210

	if (err)
211
		goto out1;
S
Sainath Grandhi 已提交
212 213 214

	err = class_register(&macvtap_class);
	if (err)
215
		goto out2;
S
Sainath Grandhi 已提交
216 217 218

	err = register_netdevice_notifier(&macvtap_notifier_block);
	if (err)
219
		goto out3;
S
Sainath Grandhi 已提交
220 221 222

	err = macvlan_link_register(&macvtap_link_ops);
	if (err)
223
		goto out4;
S
Sainath Grandhi 已提交
224 225 226 227

	return 0;

out4:
228
	unregister_netdevice_notifier(&macvtap_notifier_block);
S
Sainath Grandhi 已提交
229
out3:
230
	class_unregister(&macvtap_class);
S
Sainath Grandhi 已提交
231
out2:
232
	tap_destroy_cdev(macvtap_major, &macvtap_cdev);
S
Sainath Grandhi 已提交
233 234 235 236 237 238 239 240 241 242
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);
243
	tap_destroy_cdev(macvtap_major, &macvtap_cdev);
S
Sainath Grandhi 已提交
244 245 246 247 248 249
}
module_exit(macvtap_exit);

MODULE_ALIAS_RTNL_LINK("macvtap");
MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
MODULE_LICENSE("GPL");