veth.c 9.4 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 {
E
Eric Dumazet 已提交
35 36
	struct net_device	*peer;
	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 92 93 94 95 96 97
}

static void veth_get_ethtool_stats(struct net_device *dev,
		struct ethtool_stats *stats, u64 *data)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	data[0] = priv->peer->ifindex;
}

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 110 111
	struct veth_priv *priv = netdev_priv(dev);
	struct net_device *rcv = priv->peer;
	int length = skb->len;
112

113
	/* don't change ip_summed == CHECKSUM_PARTIAL, as that
E
Eric Dumazet 已提交
114 115
	 * will cause bad checksum on forwarded packets
	 */
M
Michał Mirosław 已提交
116 117 118
	if (skb->ip_summed == CHECKSUM_NONE &&
	    rcv->features & NETIF_F_RXCSUM)
		skb->ip_summed = CHECKSUM_UNNECESSARY;
119

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

E
Eric Dumazet 已提交
123 124 125 126 127 128 129
		u64_stats_update_begin(&stats->syncp);
		stats->bytes += length;
		stats->packets++;
		u64_stats_update_end(&stats->syncp);
	} else {
		atomic64_inc(&priv->dropped);
	}
130

131
	return NETDEV_TX_OK;
132 133 134 135 136 137
}

/*
 * general routines
 */

E
Eric Dumazet 已提交
138
static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
139
{
140
	struct veth_priv *priv = netdev_priv(dev);
141
	int cpu;
142

E
Eric Dumazet 已提交
143 144
	result->packets = 0;
	result->bytes = 0;
E
Eric Dumazet 已提交
145
	for_each_possible_cpu(cpu) {
E
Eric Dumazet 已提交
146 147
		struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
		u64 packets, bytes;
148 149 150 151
		unsigned int start;

		do {
			start = u64_stats_fetch_begin_bh(&stats->syncp);
E
Eric Dumazet 已提交
152 153
			packets = stats->packets;
			bytes = stats->bytes;
154
		} while (u64_stats_fetch_retry_bh(&stats->syncp, start));
E
Eric Dumazet 已提交
155 156
		result->packets += packets;
		result->bytes += bytes;
157
	}
E
Eric Dumazet 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
	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);
	struct pcpu_vstats one;

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

	tot->rx_dropped = veth_stats_one(&one, priv->peer);
	tot->rx_bytes = one.bytes;
	tot->rx_packets = one.packets;
174 175

	return tot;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

static int veth_open(struct net_device *dev)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	if (priv->peer == NULL)
		return -ENOTCONN;

	if (priv->peer->flags & IFF_UP) {
		netif_carrier_on(dev);
		netif_carrier_on(priv->peer);
	}
	return 0;
}

E
Eric W. Biederman 已提交
193 194 195 196 197 198 199 200 201 202
static int veth_close(struct net_device *dev)
{
	struct veth_priv *priv = netdev_priv(dev);

	netif_carrier_off(dev);
	netif_carrier_off(priv->peer);

	return 0;
}

E
Eric Biederman 已提交
203 204
static int is_valid_veth_mtu(int new_mtu)
{
205
	return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
E
Eric Biederman 已提交
206 207 208 209 210 211 212 213 214 215
}

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

216 217
static int veth_dev_init(struct net_device *dev)
{
E
Eric Dumazet 已提交
218 219
	dev->vstats = alloc_percpu(struct pcpu_vstats);
	if (!dev->vstats)
220 221 222 223 224
		return -ENOMEM;

	return 0;
}

225 226
static void veth_dev_free(struct net_device *dev)
{
E
Eric Dumazet 已提交
227
	free_percpu(dev->vstats);
228 229 230
	free_netdev(dev);
}

231
static const struct net_device_ops veth_netdev_ops = {
232 233
	.ndo_init            = veth_dev_init,
	.ndo_open            = veth_open,
E
Eric W. Biederman 已提交
234
	.ndo_stop            = veth_close,
235
	.ndo_start_xmit      = veth_xmit,
E
Eric Biederman 已提交
236
	.ndo_change_mtu      = veth_change_mtu,
237
	.ndo_get_stats64     = veth_get_stats64,
238
	.ndo_set_mac_address = eth_mac_addr,
239 240
};

E
Eric Dumazet 已提交
241 242 243 244
#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO |    \
		       NETIF_F_HW_CSUM | NETIF_F_RXCSUM | NETIF_F_HIGHDMA | \
		       NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX)

245 246 247 248
static void veth_setup(struct net_device *dev)
{
	ether_setup(dev);

249
	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
250
	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
251

252
	dev->netdev_ops = &veth_netdev_ops;
253 254
	dev->ethtool_ops = &veth_ethtool_ops;
	dev->features |= NETIF_F_LLTX;
E
Eric Dumazet 已提交
255
	dev->features |= VETH_FEATURES;
256
	dev->destructor = veth_dev_free;
M
Michał Mirosław 已提交
257

E
Eric Dumazet 已提交
258
	dev->hw_features = VETH_FEATURES;
259 260 261 262 263 264 265 266 267 268 269 270 271 272
}

/*
 * 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 已提交
273 274 275 276
	if (tb[IFLA_MTU]) {
		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
			return -EINVAL;
	}
277 278 279 280 281
	return 0;
}

static struct rtnl_link_ops veth_link_ops;

282
static int veth_newlink(struct net *src_net, struct net_device *dev,
283 284 285 286 287 288 289
			 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;
290
	struct ifinfomsg *ifmp;
291
	struct net *net;
292 293 294 295 296 297 298 299

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

		nla_peer = data[VETH_INFO_PEER];
300
		ifmp = nla_data(nla_peer);
301 302 303 304 305 306 307 308 309 310 311 312
		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;
313 314
	} else {
		ifmp = NULL;
315
		tbp = tb;
316
	}
317 318 319 320 321 322

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

323 324 325 326
	net = rtnl_link_get_net(src_net, tbp);
	if (IS_ERR(net))
		return PTR_ERR(net);

327
	peer = rtnl_create_link(net, ifname, &veth_link_ops, tbp);
328 329
	if (IS_ERR(peer)) {
		put_net(net);
330
		return PTR_ERR(peer);
331
	}
332 333

	if (tbp[IFLA_ADDRESS] == NULL)
334
		eth_hw_addr_random(peer);
335 336 337

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

	err = register_netdevice(peer);
340 341
	put_net(net);
	net = NULL;
342 343 344 345 346
	if (err < 0)
		goto err_register_peer;

	netif_carrier_off(peer);

347 348 349 350
	err = rtnl_configure_link(peer, ifmp);
	if (err < 0)
		goto err_configure_peer;

351 352 353 354 355 356 357 358
	/*
	 * register dev last
	 *
	 * note, that since we've registered new device the dev's name
	 * should be re-allocated
	 */

	if (tb[IFLA_ADDRESS] == NULL)
359
		eth_hw_addr_random(dev);
360

361 362 363 364 365 366 367 368 369 370 371
	if (tb[IFLA_IFNAME])
		nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
	else
		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");

	if (strchr(dev->name, '%')) {
		err = dev_alloc_name(dev, dev->name);
		if (err < 0)
			goto err_alloc_name;
	}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	err = register_netdevice(dev);
	if (err < 0)
		goto err_register_dev;

	netif_carrier_off(dev);

	/*
	 * tie the deviced together
	 */

	priv = netdev_priv(dev);
	priv->peer = peer;

	priv = netdev_priv(peer);
	priv->peer = dev;
	return 0;

err_register_dev:
	/* nothing to do */
391
err_alloc_name:
392
err_configure_peer:
393 394 395 396 397 398 399 400
	unregister_netdevice(peer);
	return err;

err_register_peer:
	free_netdev(peer);
	return err;
}

401
static void veth_dellink(struct net_device *dev, struct list_head *head)
402 403 404 405 406 407 408
{
	struct veth_priv *priv;
	struct net_device *peer;

	priv = netdev_priv(dev);
	peer = priv->peer;

E
Eric Dumazet 已提交
409 410
	unregister_netdevice_queue(dev, head);
	unregister_netdevice_queue(peer, head);
411 412
}

413 414 415
static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
	[VETH_INFO_PEER]	= { .len = sizeof(struct ifinfomsg) },
};
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

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)
{
439
	rtnl_link_unregister(&veth_link_ops);
440 441 442 443 444 445 446 447
}

module_init(veth_init);
module_exit(veth_exit);

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