veth.c 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 *  drivers/net/veth.c
 *
 *  Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
 *
 * Author: Pavel Emelianov <xemul@openvz.org>
 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
 *
 */

#include <linux/netdevice.h>
12
#include <linux/slab.h>
13 14
#include <linux/ethtool.h>
#include <linux/etherdevice.h>
15
#include <linux/u64_stats_sync.h>
16 17 18

#include <net/dst.h>
#include <net/xfrm.h>
19
#include <linux/veth.h>
20
#include <linux/module.h>
21 22 23 24

#define DRV_NAME	"veth"
#define DRV_VERSION	"1.0"

E
Eric Biederman 已提交
25 26 27
#define MIN_MTU 68		/* Min L3 MTU */
#define MAX_MTU 65535		/* Max L3 MTU (arbitrary) */

E
Eric Dumazet 已提交
28 29 30
struct pcpu_vstats {
	u64			packets;
	u64			bytes;
31
	struct u64_stats_sync	syncp;
32 33 34
};

struct veth_priv {
35
	struct net_device __rcu	*peer;
E
Eric Dumazet 已提交
36
	atomic64_t		dropped;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
};

/*
 * ethtool interface
 */

static struct {
	const char string[ETH_GSTRING_LEN];
} ethtool_stats_keys[] = {
	{ "peer_ifindex" },
};

static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	cmd->supported		= 0;
	cmd->advertising	= 0;
53
	ethtool_cmd_speed_set(cmd, SPEED_10000);
54 55 56 57 58 59 60 61 62 63 64 65
	cmd->duplex		= DUPLEX_FULL;
	cmd->port		= PORT_TP;
	cmd->phy_address	= 0;
	cmd->transceiver	= XCVR_INTERNAL;
	cmd->autoneg		= AUTONEG_DISABLE;
	cmd->maxtxpkt		= 0;
	cmd->maxrxpkt		= 0;
	return 0;
}

static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
66 67
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
68 69 70 71 72 73 74 75 76 77 78
}

static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
{
	switch(stringset) {
	case ETH_SS_STATS:
		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
		break;
	}
}

79
static int veth_get_sset_count(struct net_device *dev, int sset)
80
{
81 82 83 84 85 86
	switch (sset) {
	case ETH_SS_STATS:
		return ARRAY_SIZE(ethtool_stats_keys);
	default:
		return -EOPNOTSUPP;
	}
87 88 89 90 91
}

static void veth_get_ethtool_stats(struct net_device *dev,
		struct ethtool_stats *stats, u64 *data)
{
92 93
	struct veth_priv *priv = netdev_priv(dev);
	struct net_device *peer = rtnl_dereference(priv->peer);
94

95
	data[0] = peer ? peer->ifindex : 0;
96 97
}

98
static const struct ethtool_ops veth_ethtool_ops = {
99 100 101 102
	.get_settings		= veth_get_settings,
	.get_drvinfo		= veth_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_strings		= veth_get_strings,
103
	.get_sset_count		= veth_get_sset_count,
104 105 106
	.get_ethtool_stats	= veth_get_ethtool_stats,
};

107
static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
108
{
E
Eric Dumazet 已提交
109
	struct veth_priv *priv = netdev_priv(dev);
110
	struct net_device *rcv;
E
Eric Dumazet 已提交
111
	int length = skb->len;
112

113 114 115 116 117 118
	rcu_read_lock();
	rcv = rcu_dereference(priv->peer);
	if (unlikely(!rcv)) {
		kfree_skb(skb);
		goto drop;
	}
119
	/* don't change ip_summed == CHECKSUM_PARTIAL, as that
E
Eric Dumazet 已提交
120 121
	 * will cause bad checksum on forwarded packets
	 */
M
Michał Mirosław 已提交
122 123 124
	if (skb->ip_summed == CHECKSUM_NONE &&
	    rcv->features & NETIF_F_RXCSUM)
		skb->ip_summed = CHECKSUM_UNNECESSARY;
125

E
Eric Dumazet 已提交
126 127
	if (likely(dev_forward_skb(rcv, skb) == NET_RX_SUCCESS)) {
		struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
128

E
Eric Dumazet 已提交
129 130 131 132 133
		u64_stats_update_begin(&stats->syncp);
		stats->bytes += length;
		stats->packets++;
		u64_stats_update_end(&stats->syncp);
	} else {
134
drop:
E
Eric Dumazet 已提交
135 136
		atomic64_inc(&priv->dropped);
	}
137
	rcu_read_unlock();
138
	return NETDEV_TX_OK;
139 140 141 142 143 144
}

/*
 * general routines
 */

E
Eric Dumazet 已提交
145
static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
146
{
147
	struct veth_priv *priv = netdev_priv(dev);
148
	int cpu;
149

E
Eric Dumazet 已提交
150 151
	result->packets = 0;
	result->bytes = 0;
E
Eric Dumazet 已提交
152
	for_each_possible_cpu(cpu) {
E
Eric Dumazet 已提交
153 154
		struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
		u64 packets, bytes;
155 156 157 158
		unsigned int start;

		do {
			start = u64_stats_fetch_begin_bh(&stats->syncp);
E
Eric Dumazet 已提交
159 160
			packets = stats->packets;
			bytes = stats->bytes;
161
		} while (u64_stats_fetch_retry_bh(&stats->syncp, start));
E
Eric Dumazet 已提交
162 163
		result->packets += packets;
		result->bytes += bytes;
164
	}
E
Eric Dumazet 已提交
165 166 167 168 169 170 171
	return atomic64_read(&priv->dropped);
}

static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
						  struct rtnl_link_stats64 *tot)
{
	struct veth_priv *priv = netdev_priv(dev);
172
	struct net_device *peer;
E
Eric Dumazet 已提交
173 174 175 176 177 178
	struct pcpu_vstats one;

	tot->tx_dropped = veth_stats_one(&one, dev);
	tot->tx_bytes = one.bytes;
	tot->tx_packets = one.packets;

179 180 181 182 183 184 185 186
	rcu_read_lock();
	peer = rcu_dereference(priv->peer);
	if (peer) {
		tot->rx_dropped = veth_stats_one(&one, peer);
		tot->rx_bytes = one.bytes;
		tot->rx_packets = one.packets;
	}
	rcu_read_unlock();
187 188

	return tot;
189 190 191 192
}

static int veth_open(struct net_device *dev)
{
193 194
	struct veth_priv *priv = netdev_priv(dev);
	struct net_device *peer = rtnl_dereference(priv->peer);
195

196
	if (!peer)
197 198
		return -ENOTCONN;

199
	if (peer->flags & IFF_UP) {
200
		netif_carrier_on(dev);
201
		netif_carrier_on(peer);
202 203 204 205
	}
	return 0;
}

E
Eric W. Biederman 已提交
206 207 208
static int veth_close(struct net_device *dev)
{
	struct veth_priv *priv = netdev_priv(dev);
209
	struct net_device *peer = rtnl_dereference(priv->peer);
E
Eric W. Biederman 已提交
210 211

	netif_carrier_off(dev);
212 213
	if (peer)
		netif_carrier_off(peer);
E
Eric W. Biederman 已提交
214 215 216 217

	return 0;
}

E
Eric Biederman 已提交
218 219
static int is_valid_veth_mtu(int new_mtu)
{
220
	return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
E
Eric Biederman 已提交
221 222 223 224 225 226 227 228 229 230
}

static int veth_change_mtu(struct net_device *dev, int new_mtu)
{
	if (!is_valid_veth_mtu(new_mtu))
		return -EINVAL;
	dev->mtu = new_mtu;
	return 0;
}

231 232
static int veth_dev_init(struct net_device *dev)
{
E
Eric Dumazet 已提交
233 234
	dev->vstats = alloc_percpu(struct pcpu_vstats);
	if (!dev->vstats)
235 236 237 238 239
		return -ENOMEM;

	return 0;
}

240 241
static void veth_dev_free(struct net_device *dev)
{
E
Eric Dumazet 已提交
242
	free_percpu(dev->vstats);
243 244 245
	free_netdev(dev);
}

246
static const struct net_device_ops veth_netdev_ops = {
247 248
	.ndo_init            = veth_dev_init,
	.ndo_open            = veth_open,
E
Eric W. Biederman 已提交
249
	.ndo_stop            = veth_close,
250
	.ndo_start_xmit      = veth_xmit,
E
Eric Biederman 已提交
251
	.ndo_change_mtu      = veth_change_mtu,
252
	.ndo_get_stats64     = veth_get_stats64,
253
	.ndo_set_mac_address = eth_mac_addr,
254 255
};

E
Eric Dumazet 已提交
256 257
#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO |    \
		       NETIF_F_HW_CSUM | NETIF_F_RXCSUM | NETIF_F_HIGHDMA | \
258 259
		       NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
		       NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
E
Eric Dumazet 已提交
260

261 262 263 264
static void veth_setup(struct net_device *dev)
{
	ether_setup(dev);

265
	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
266
	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
267

268
	dev->netdev_ops = &veth_netdev_ops;
269 270
	dev->ethtool_ops = &veth_ethtool_ops;
	dev->features |= NETIF_F_LLTX;
E
Eric Dumazet 已提交
271
	dev->features |= VETH_FEATURES;
272
	dev->destructor = veth_dev_free;
M
Michał Mirosław 已提交
273

E
Eric Dumazet 已提交
274
	dev->hw_features = VETH_FEATURES;
275 276 277 278 279 280 281 282 283 284 285 286 287 288
}

/*
 * netlink interface
 */

static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
{
	if (tb[IFLA_ADDRESS]) {
		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
			return -EINVAL;
		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
			return -EADDRNOTAVAIL;
	}
E
Eric Biederman 已提交
289 290 291 292
	if (tb[IFLA_MTU]) {
		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
			return -EINVAL;
	}
293 294 295 296 297
	return 0;
}

static struct rtnl_link_ops veth_link_ops;

298
static int veth_newlink(struct net *src_net, struct net_device *dev,
299 300 301 302 303 304 305
			 struct nlattr *tb[], struct nlattr *data[])
{
	int err;
	struct net_device *peer;
	struct veth_priv *priv;
	char ifname[IFNAMSIZ];
	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
306
	struct ifinfomsg *ifmp;
307
	struct net *net;
308 309 310 311 312 313 314 315

	/*
	 * create and register peer first
	 */
	if (data != NULL && data[VETH_INFO_PEER] != NULL) {
		struct nlattr *nla_peer;

		nla_peer = data[VETH_INFO_PEER];
316
		ifmp = nla_data(nla_peer);
317 318 319 320 321 322 323 324 325 326 327 328
		err = nla_parse(peer_tb, IFLA_MAX,
				nla_data(nla_peer) + sizeof(struct ifinfomsg),
				nla_len(nla_peer) - sizeof(struct ifinfomsg),
				ifla_policy);
		if (err < 0)
			return err;

		err = veth_validate(peer_tb, NULL);
		if (err < 0)
			return err;

		tbp = peer_tb;
329 330
	} else {
		ifmp = NULL;
331
		tbp = tb;
332
	}
333 334 335 336 337 338

	if (tbp[IFLA_IFNAME])
		nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
	else
		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");

339 340 341 342
	net = rtnl_link_get_net(src_net, tbp);
	if (IS_ERR(net))
		return PTR_ERR(net);

343
	peer = rtnl_create_link(net, ifname, &veth_link_ops, tbp);
344 345
	if (IS_ERR(peer)) {
		put_net(net);
346
		return PTR_ERR(peer);
347
	}
348 349

	if (tbp[IFLA_ADDRESS] == NULL)
350
		eth_hw_addr_random(peer);
351 352 353

	if (ifmp && (dev->ifindex != 0))
		peer->ifindex = ifmp->ifi_index;
354 355

	err = register_netdevice(peer);
356 357
	put_net(net);
	net = NULL;
358 359 360 361 362
	if (err < 0)
		goto err_register_peer;

	netif_carrier_off(peer);

363 364 365 366
	err = rtnl_configure_link(peer, ifmp);
	if (err < 0)
		goto err_configure_peer;

367 368 369 370 371 372 373 374
	/*
	 * register dev last
	 *
	 * note, that since we've registered new device the dev's name
	 * should be re-allocated
	 */

	if (tb[IFLA_ADDRESS] == NULL)
375
		eth_hw_addr_random(dev);
376

377 378 379 380 381
	if (tb[IFLA_IFNAME])
		nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
	else
		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");

382 383 384 385 386 387 388 389 390 391 392
	err = register_netdevice(dev);
	if (err < 0)
		goto err_register_dev;

	netif_carrier_off(dev);

	/*
	 * tie the deviced together
	 */

	priv = netdev_priv(dev);
393
	rcu_assign_pointer(priv->peer, peer);
394 395

	priv = netdev_priv(peer);
396
	rcu_assign_pointer(priv->peer, dev);
397 398 399 400
	return 0;

err_register_dev:
	/* nothing to do */
401
err_configure_peer:
402 403 404 405 406 407 408 409
	unregister_netdevice(peer);
	return err;

err_register_peer:
	free_netdev(peer);
	return err;
}

410
static void veth_dellink(struct net_device *dev, struct list_head *head)
411 412 413 414 415
{
	struct veth_priv *priv;
	struct net_device *peer;

	priv = netdev_priv(dev);
416 417 418 419 420 421 422
	peer = rtnl_dereference(priv->peer);

	/* Note : dellink() is called from default_device_exit_batch(),
	 * before a rcu_synchronize() point. The devices are guaranteed
	 * not being freed before one RCU grace period.
	 */
	RCU_INIT_POINTER(priv->peer, NULL);
E
Eric Dumazet 已提交
423
	unregister_netdevice_queue(dev, head);
424 425 426 427 428 429

	if (peer) {
		priv = netdev_priv(peer);
		RCU_INIT_POINTER(priv->peer, NULL);
		unregister_netdevice_queue(peer, head);
	}
430 431
}

432 433 434
static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
	[VETH_INFO_PEER]	= { .len = sizeof(struct ifinfomsg) },
};
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

static struct rtnl_link_ops veth_link_ops = {
	.kind		= DRV_NAME,
	.priv_size	= sizeof(struct veth_priv),
	.setup		= veth_setup,
	.validate	= veth_validate,
	.newlink	= veth_newlink,
	.dellink	= veth_dellink,
	.policy		= veth_policy,
	.maxtype	= VETH_INFO_MAX,
};

/*
 * init/fini
 */

static __init int veth_init(void)
{
	return rtnl_link_register(&veth_link_ops);
}

static __exit void veth_exit(void)
{
458
	rtnl_link_unregister(&veth_link_ops);
459 460 461 462 463 464 465 466
}

module_init(veth_init);
module_exit(veth_exit);

MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS_RTNL_LINK(DRV_NAME);