veth.c 12.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

J
Jiri Pirko 已提交
17
#include <net/rtnetlink.h>
18 19
#include <net/dst.h>
#include <net/xfrm.h>
20
#include <linux/veth.h>
21
#include <linux/module.h>
22 23 24 25

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

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

struct veth_priv {
33
	struct net_device __rcu	*peer;
E
Eric Dumazet 已提交
34
	atomic64_t		dropped;
35
	unsigned		requested_headroom;
36 37 38 39 40 41 42 43 44 45 46 47
};

/*
 * ethtool interface
 */

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

48 49
static int veth_get_link_ksettings(struct net_device *dev,
				   struct ethtool_link_ksettings *cmd)
50
{
51 52 53 54
	cmd->base.speed		= SPEED_10000;
	cmd->base.duplex	= DUPLEX_FULL;
	cmd->base.port		= PORT_TP;
	cmd->base.autoneg	= AUTONEG_DISABLE;
55 56 57 58 59
	return 0;
}

static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
{
60 61
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
62 63 64 65 66 67 68 69 70 71 72
}

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;
	}
}

73
static int veth_get_sset_count(struct net_device *dev, int sset)
74
{
75 76 77 78 79 80
	switch (sset) {
	case ETH_SS_STATS:
		return ARRAY_SIZE(ethtool_stats_keys);
	default:
		return -EOPNOTSUPP;
	}
81 82 83 84 85
}

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

89
	data[0] = peer ? peer->ifindex : 0;
90 91
}

92
static const struct ethtool_ops veth_ethtool_ops = {
93 94 95
	.get_drvinfo		= veth_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_strings		= veth_get_strings,
96
	.get_sset_count		= veth_get_sset_count,
97
	.get_ethtool_stats	= veth_get_ethtool_stats,
98
	.get_link_ksettings	= veth_get_link_ksettings,
99 100
};

101
static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
102
{
E
Eric Dumazet 已提交
103
	struct veth_priv *priv = netdev_priv(dev);
104
	struct net_device *rcv;
E
Eric Dumazet 已提交
105
	int length = skb->len;
106

107 108 109 110 111 112
	rcu_read_lock();
	rcv = rcu_dereference(priv->peer);
	if (unlikely(!rcv)) {
		kfree_skb(skb);
		goto drop;
	}
113

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

E
Eric Dumazet 已提交
117 118 119 120 121
		u64_stats_update_begin(&stats->syncp);
		stats->bytes += length;
		stats->packets++;
		u64_stats_update_end(&stats->syncp);
	} else {
122
drop:
E
Eric Dumazet 已提交
123 124
		atomic64_inc(&priv->dropped);
	}
125
	rcu_read_unlock();
126
	return NETDEV_TX_OK;
127 128 129 130 131 132
}

/*
 * general routines
 */

E
Eric Dumazet 已提交
133
static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
134
{
135
	struct veth_priv *priv = netdev_priv(dev);
136
	int cpu;
137

E
Eric Dumazet 已提交
138 139
	result->packets = 0;
	result->bytes = 0;
E
Eric Dumazet 已提交
140
	for_each_possible_cpu(cpu) {
E
Eric Dumazet 已提交
141 142
		struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
		u64 packets, bytes;
143 144 145
		unsigned int start;

		do {
146
			start = u64_stats_fetch_begin_irq(&stats->syncp);
E
Eric Dumazet 已提交
147 148
			packets = stats->packets;
			bytes = stats->bytes;
149
		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
E
Eric Dumazet 已提交
150 151
		result->packets += packets;
		result->bytes += bytes;
152
	}
E
Eric Dumazet 已提交
153 154 155
	return atomic64_read(&priv->dropped);
}

156 157
static void veth_get_stats64(struct net_device *dev,
			     struct rtnl_link_stats64 *tot)
E
Eric Dumazet 已提交
158 159
{
	struct veth_priv *priv = netdev_priv(dev);
160
	struct net_device *peer;
E
Eric Dumazet 已提交
161 162 163 164 165 166
	struct pcpu_vstats one;

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

167 168 169 170 171 172 173 174
	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();
175 176
}

177 178 179 180 181
/* fake multicast ability */
static void veth_set_multicast_list(struct net_device *dev)
{
}

182 183
static int veth_open(struct net_device *dev)
{
184 185
	struct veth_priv *priv = netdev_priv(dev);
	struct net_device *peer = rtnl_dereference(priv->peer);
186

187
	if (!peer)
188 189
		return -ENOTCONN;

190
	if (peer->flags & IFF_UP) {
191
		netif_carrier_on(dev);
192
		netif_carrier_on(peer);
193 194 195 196
	}
	return 0;
}

E
Eric W. Biederman 已提交
197 198 199
static int veth_close(struct net_device *dev)
{
	struct veth_priv *priv = netdev_priv(dev);
200
	struct net_device *peer = rtnl_dereference(priv->peer);
E
Eric W. Biederman 已提交
201 202

	netif_carrier_off(dev);
203 204
	if (peer)
		netif_carrier_off(peer);
E
Eric W. Biederman 已提交
205 206 207 208

	return 0;
}

209
static int is_valid_veth_mtu(int mtu)
E
Eric Biederman 已提交
210
{
211
	return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
E
Eric Biederman 已提交
212 213
}

214 215
static int veth_dev_init(struct net_device *dev)
{
216
	dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats);
E
Eric Dumazet 已提交
217
	if (!dev->vstats)
218 219 220 221
		return -ENOMEM;
	return 0;
}

222 223
static void veth_dev_free(struct net_device *dev)
{
E
Eric Dumazet 已提交
224
	free_percpu(dev->vstats);
225 226
}

W
WANG Cong 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
#ifdef CONFIG_NET_POLL_CONTROLLER
static void veth_poll_controller(struct net_device *dev)
{
	/* veth only receives frames when its peer sends one
	 * Since it's a synchronous operation, we are guaranteed
	 * never to have pending data when we poll for it so
	 * there is nothing to do here.
	 *
	 * We need this though so netpoll recognizes us as an interface that
	 * supports polling, which enables bridge devices in virt setups to
	 * still use netconsole
	 */
}
#endif	/* CONFIG_NET_POLL_CONTROLLER */

242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int veth_get_iflink(const struct net_device *dev)
{
	struct veth_priv *priv = netdev_priv(dev);
	struct net_device *peer;
	int iflink;

	rcu_read_lock();
	peer = rcu_dereference(priv->peer);
	iflink = peer ? peer->ifindex : 0;
	rcu_read_unlock();

	return iflink;
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
{
	struct veth_priv *peer_priv, *priv = netdev_priv(dev);
	struct net_device *peer;

	if (new_hr < 0)
		new_hr = 0;

	rcu_read_lock();
	peer = rcu_dereference(priv->peer);
	if (unlikely(!peer))
		goto out;

	peer_priv = netdev_priv(peer);
	priv->requested_headroom = new_hr;
	new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
	dev->needed_headroom = new_hr;
	peer->needed_headroom = new_hr;

out:
	rcu_read_unlock();
}

279
static const struct net_device_ops veth_netdev_ops = {
280 281
	.ndo_init            = veth_dev_init,
	.ndo_open            = veth_open,
E
Eric W. Biederman 已提交
282
	.ndo_stop            = veth_close,
283
	.ndo_start_xmit      = veth_xmit,
284
	.ndo_get_stats64     = veth_get_stats64,
285
	.ndo_set_rx_mode     = veth_set_multicast_list,
286
	.ndo_set_mac_address = eth_mac_addr,
W
WANG Cong 已提交
287 288 289
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= veth_poll_controller,
#endif
290
	.ndo_get_iflink		= veth_get_iflink,
291
	.ndo_features_check	= passthru_features_check,
292
	.ndo_set_rx_headroom	= veth_set_rx_headroom,
293 294
};

295
#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
296
		       NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
297
		       NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
298 299
		       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 已提交
300

301 302 303 304
static void veth_setup(struct net_device *dev)
{
	ether_setup(dev);

305
	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
306
	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
307
	dev->priv_flags |= IFF_NO_QUEUE;
308
	dev->priv_flags |= IFF_PHONY_HEADROOM;
309

310
	dev->netdev_ops = &veth_netdev_ops;
311 312
	dev->ethtool_ops = &veth_ethtool_ops;
	dev->features |= NETIF_F_LLTX;
E
Eric Dumazet 已提交
313
	dev->features |= VETH_FEATURES;
314
	dev->vlan_features = dev->features &
315 316 317 318
			     ~(NETIF_F_HW_VLAN_CTAG_TX |
			       NETIF_F_HW_VLAN_STAG_TX |
			       NETIF_F_HW_VLAN_CTAG_RX |
			       NETIF_F_HW_VLAN_STAG_RX);
319 320
	dev->needs_free_netdev = true;
	dev->priv_destructor = veth_dev_free;
321
	dev->max_mtu = ETH_MAX_MTU;
M
Michał Mirosław 已提交
322

E
Eric Dumazet 已提交
323
	dev->hw_features = VETH_FEATURES;
324
	dev->hw_enc_features = VETH_FEATURES;
D
David Ahern 已提交
325
	dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
326 327 328 329 330 331
}

/*
 * netlink interface
 */

332 333
static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
			 struct netlink_ext_ack *extack)
334 335 336 337 338 339 340
{
	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 已提交
341 342 343 344
	if (tb[IFLA_MTU]) {
		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
			return -EINVAL;
	}
345 346 347 348 349
	return 0;
}

static struct rtnl_link_ops veth_link_ops;

350
static int veth_newlink(struct net *src_net, struct net_device *dev,
351 352
			struct nlattr *tb[], struct nlattr *data[],
			struct netlink_ext_ack *extack)
353 354 355 356 357 358
{
	int err;
	struct net_device *peer;
	struct veth_priv *priv;
	char ifname[IFNAMSIZ];
	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
359
	unsigned char name_assign_type;
360
	struct ifinfomsg *ifmp;
361
	struct net *net;
362 363 364 365 366 367 368 369

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

		nla_peer = data[VETH_INFO_PEER];
370
		ifmp = nla_data(nla_peer);
J
Jiri Pirko 已提交
371 372
		err = rtnl_nla_parse_ifla(peer_tb,
					  nla_data(nla_peer) + sizeof(struct ifinfomsg),
373 374
					  nla_len(nla_peer) - sizeof(struct ifinfomsg),
					  NULL);
375 376 377
		if (err < 0)
			return err;

378
		err = veth_validate(peer_tb, NULL, extack);
379 380 381 382
		if (err < 0)
			return err;

		tbp = peer_tb;
383 384
	} else {
		ifmp = NULL;
385
		tbp = tb;
386
	}
387

388
	if (ifmp && tbp[IFLA_IFNAME]) {
389
		nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
390 391
		name_assign_type = NET_NAME_USER;
	} else {
392
		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
393 394
		name_assign_type = NET_NAME_ENUM;
	}
395

396 397 398 399
	net = rtnl_link_get_net(src_net, tbp);
	if (IS_ERR(net))
		return PTR_ERR(net);

400 401
	peer = rtnl_create_link(net, ifname, name_assign_type,
				&veth_link_ops, tbp);
402 403
	if (IS_ERR(peer)) {
		put_net(net);
404
		return PTR_ERR(peer);
405
	}
406

407
	if (!ifmp || !tbp[IFLA_ADDRESS])
408
		eth_hw_addr_random(peer);
409 410 411

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

S
Stephen Hemminger 已提交
413 414 415
	peer->gso_max_size = dev->gso_max_size;
	peer->gso_max_segs = dev->gso_max_segs;

416
	err = register_netdevice(peer);
417 418
	put_net(net);
	net = NULL;
419 420 421 422 423
	if (err < 0)
		goto err_register_peer;

	netif_carrier_off(peer);

424 425 426 427
	err = rtnl_configure_link(peer, ifmp);
	if (err < 0)
		goto err_configure_peer;

428 429 430 431 432 433 434 435
	/*
	 * register dev last
	 *
	 * note, that since we've registered new device the dev's name
	 * should be re-allocated
	 */

	if (tb[IFLA_ADDRESS] == NULL)
436
		eth_hw_addr_random(dev);
437

438 439 440 441 442
	if (tb[IFLA_IFNAME])
		nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
	else
		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");

443 444 445 446 447 448 449 450 451 452 453
	err = register_netdevice(dev);
	if (err < 0)
		goto err_register_dev;

	netif_carrier_off(dev);

	/*
	 * tie the deviced together
	 */

	priv = netdev_priv(dev);
454
	rcu_assign_pointer(priv->peer, peer);
455 456

	priv = netdev_priv(peer);
457
	rcu_assign_pointer(priv->peer, dev);
458 459 460 461
	return 0;

err_register_dev:
	/* nothing to do */
462
err_configure_peer:
463 464 465 466 467 468 469 470
	unregister_netdevice(peer);
	return err;

err_register_peer:
	free_netdev(peer);
	return err;
}

471
static void veth_dellink(struct net_device *dev, struct list_head *head)
472 473 474 475 476
{
	struct veth_priv *priv;
	struct net_device *peer;

	priv = netdev_priv(dev);
477 478 479 480 481 482 483
	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 已提交
484
	unregister_netdevice_queue(dev, head);
485 486 487 488 489 490

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

493 494 495
static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
	[VETH_INFO_PEER]	= { .len = sizeof(struct ifinfomsg) },
};
496

497 498 499 500 501 502 503 504
static struct net *veth_get_link_net(const struct net_device *dev)
{
	struct veth_priv *priv = netdev_priv(dev);
	struct net_device *peer = rtnl_dereference(priv->peer);

	return peer ? dev_net(peer) : dev_net(dev);
}

505 506 507 508 509 510 511 512 513
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,
514
	.get_link_net	= veth_get_link_net,
515 516 517 518 519 520 521 522 523 524 525 526 527
};

/*
 * init/fini
 */

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

static __exit void veth_exit(void)
{
528
	rtnl_link_unregister(&veth_link_ops);
529 530 531 532 533 534 535 536
}

module_init(veth_init);
module_exit(veth_exit);

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