veth.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  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>
#include <linux/ethtool.h>
#include <linux/etherdevice.h>

#include <net/dst.h>
#include <net/xfrm.h>
17
#include <linux/veth.h>
18 19 20 21

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

E
Eric Biederman 已提交
22 23 24 25
#define MIN_MTU 68		/* Min L3 MTU */
#define MAX_MTU 65535		/* Max L3 MTU (arbitrary) */
#define MTU_PAD (ETH_HLEN + 4)  /* Max difference between L2 and L3 size MTU */

26 27 28 29 30 31
struct veth_net_stats {
	unsigned long	rx_packets;
	unsigned long	tx_packets;
	unsigned long	rx_bytes;
	unsigned long	tx_bytes;
	unsigned long	tx_dropped;
E
Eric Biederman 已提交
32
	unsigned long	rx_dropped;
33 34 35 36
};

struct veth_priv {
	struct net_device *peer;
37
	struct veth_net_stats __percpu *stats;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 80 81
	unsigned ip_summed;
};

/*
 * 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;
	cmd->speed		= SPEED_10000;
	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)
{
	strcpy(info->driver, DRV_NAME);
	strcpy(info->version, DRV_VERSION);
	strcpy(info->fw_version, "N/A");
}

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

82
static int veth_get_sset_count(struct net_device *dev, int sset)
83
{
84 85 86 87 88 89
	switch (sset) {
	case ETH_SS_STATS:
		return ARRAY_SIZE(ethtool_stats_keys);
	default:
		return -EOPNOTSUPP;
	}
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

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

static u32 veth_get_rx_csum(struct net_device *dev)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	return priv->ip_summed == CHECKSUM_UNNECESSARY;
}

static int veth_set_rx_csum(struct net_device *dev, u32 data)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	priv->ip_summed = data ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
	return 0;
}

static u32 veth_get_tx_csum(struct net_device *dev)
{
	return (dev->features & NETIF_F_NO_CSUM) != 0;
}

static int veth_set_tx_csum(struct net_device *dev, u32 data)
{
	if (data)
		dev->features |= NETIF_F_NO_CSUM;
	else
		dev->features &= ~NETIF_F_NO_CSUM;
	return 0;
}

132
static const struct ethtool_ops veth_ethtool_ops = {
133 134 135 136 137 138 139 140 141 142
	.get_settings		= veth_get_settings,
	.get_drvinfo		= veth_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_rx_csum		= veth_get_rx_csum,
	.set_rx_csum		= veth_set_rx_csum,
	.get_tx_csum		= veth_get_tx_csum,
	.set_tx_csum		= veth_set_tx_csum,
	.get_sg			= ethtool_op_get_sg,
	.set_sg			= ethtool_op_set_sg,
	.get_strings		= veth_get_strings,
143
	.get_sset_count		= veth_get_sset_count,
144 145 146 147 148 149 150
	.get_ethtool_stats	= veth_get_ethtool_stats,
};

/*
 * xmit
 */

151
static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
152 153 154
{
	struct net_device *rcv = NULL;
	struct veth_priv *priv, *rcv_priv;
E
Eric Biederman 已提交
155
	struct veth_net_stats *stats, *rcv_stats;
156
	int length;
157 158 159 160 161

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

162 163
	stats = this_cpu_ptr(priv->stats);
	rcv_stats = this_cpu_ptr(rcv_priv->stats);
164 165

	if (!(rcv->flags & IFF_UP))
E
Eric Biederman 已提交
166 167
		goto tx_drop;

168 169 170
	if (dev->features & NETIF_F_NO_CSUM)
		skb->ip_summed = rcv_priv->ip_summed;

171 172 173
	length = skb->len + ETH_HLEN;
	if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)
		goto rx_drop;
174 175 176 177

	stats->tx_bytes += length;
	stats->tx_packets++;

E
Eric Biederman 已提交
178 179
	rcv_stats->rx_bytes += length;
	rcv_stats->rx_packets++;
180

181
	return NETDEV_TX_OK;
182

E
Eric Biederman 已提交
183
tx_drop:
184 185
	kfree_skb(skb);
	stats->tx_dropped++;
186
	return NETDEV_TX_OK;
E
Eric Biederman 已提交
187 188 189 190

rx_drop:
	kfree_skb(skb);
	rcv_stats->rx_dropped++;
191
	return NETDEV_TX_OK;
192 193 194 195 196 197 198 199
}

/*
 * general routines
 */

static struct net_device_stats *veth_get_stats(struct net_device *dev)
{
200 201
	struct veth_priv *priv;
	int cpu;
E
Eric Dumazet 已提交
202
	struct veth_net_stats *stats, total = {0};
203

204
	priv = netdev_priv(dev);
205

E
Eric Dumazet 已提交
206
	for_each_possible_cpu(cpu) {
207
		stats = per_cpu_ptr(priv->stats, cpu);
208

E
Eric Dumazet 已提交
209 210 211 212 213 214
		total.rx_packets += stats->rx_packets;
		total.tx_packets += stats->tx_packets;
		total.rx_bytes   += stats->rx_bytes;
		total.tx_bytes   += stats->tx_bytes;
		total.tx_dropped += stats->tx_dropped;
		total.rx_dropped += stats->rx_dropped;
215
	}
E
Eric Dumazet 已提交
216 217 218 219 220 221 222 223
	dev->stats.rx_packets = total.rx_packets;
	dev->stats.tx_packets = total.tx_packets;
	dev->stats.rx_bytes   = total.rx_bytes;
	dev->stats.tx_bytes   = total.tx_bytes;
	dev->stats.tx_dropped = total.tx_dropped;
	dev->stats.rx_dropped = total.rx_dropped;

	return &dev->stats;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

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 已提交
241 242 243 244 245 246 247 248 249 250
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 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263
static int is_valid_veth_mtu(int new_mtu)
{
	return (new_mtu >= MIN_MTU && new_mtu <= MAX_MTU);
}

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

264 265
static int veth_dev_init(struct net_device *dev)
{
266
	struct veth_net_stats __percpu *stats;
267 268 269 270 271 272 273 274 275 276 277
	struct veth_priv *priv;

	stats = alloc_percpu(struct veth_net_stats);
	if (stats == NULL)
		return -ENOMEM;

	priv = netdev_priv(dev);
	priv->stats = stats;
	return 0;
}

278 279 280 281 282 283 284 285 286
static void veth_dev_free(struct net_device *dev)
{
	struct veth_priv *priv;

	priv = netdev_priv(dev);
	free_percpu(priv->stats);
	free_netdev(dev);
}

287
static const struct net_device_ops veth_netdev_ops = {
288 289
	.ndo_init            = veth_dev_init,
	.ndo_open            = veth_open,
E
Eric W. Biederman 已提交
290
	.ndo_stop            = veth_close,
291
	.ndo_start_xmit      = veth_xmit,
E
Eric Biederman 已提交
292
	.ndo_change_mtu      = veth_change_mtu,
293 294
	.ndo_get_stats       = veth_get_stats,
	.ndo_set_mac_address = eth_mac_addr,
295 296
};

297 298 299 300
static void veth_setup(struct net_device *dev)
{
	ether_setup(dev);

301
	dev->netdev_ops = &veth_netdev_ops;
302 303
	dev->ethtool_ops = &veth_ethtool_ops;
	dev->features |= NETIF_F_LLTX;
304
	dev->destructor = veth_dev_free;
305 306 307 308 309 310 311 312 313 314 315 316 317 318
}

/*
 * 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 已提交
319 320 321 322
	if (tb[IFLA_MTU]) {
		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
			return -EINVAL;
	}
323 324 325 326 327
	return 0;
}

static struct rtnl_link_ops veth_link_ops;

328
static int veth_newlink(struct net *src_net, struct net_device *dev,
329 330 331 332 333 334 335
			 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;
336
	struct net *net;
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

	/*
	 * create and register peer first
	 *
	 * struct ifinfomsg is at the head of VETH_INFO_PEER, but we
	 * skip it since no info from it is useful yet
	 */

	if (data != NULL && data[VETH_INFO_PEER] != NULL) {
		struct nlattr *nla_peer;

		nla_peer = data[VETH_INFO_PEER];
		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;
	} else
		tbp = tb;

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

369 370 371 372 373 374 375
	net = rtnl_link_get_net(src_net, tbp);
	if (IS_ERR(net))
		return PTR_ERR(net);

	peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
	if (IS_ERR(peer)) {
		put_net(net);
376
		return PTR_ERR(peer);
377
	}
378 379 380 381 382

	if (tbp[IFLA_ADDRESS] == NULL)
		random_ether_addr(peer->dev_addr);

	err = register_netdevice(peer);
383 384
	put_net(net);
	net = NULL;
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	if (err < 0)
		goto err_register_peer;

	netif_carrier_off(peer);

	/*
	 * register dev last
	 *
	 * note, that since we've registered new device the dev's name
	 * should be re-allocated
	 */

	if (tb[IFLA_ADDRESS] == NULL)
		random_ether_addr(dev->dev_addr);

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

	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 */
err_alloc_name:
	unregister_netdevice(peer);
	return err;

err_register_peer:
	free_netdev(peer);
	return err;
}

439
static void veth_dellink(struct net_device *dev, struct list_head *head)
440 441 442 443 444 445 446
{
	struct veth_priv *priv;
	struct net_device *peer;

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

E
Eric Dumazet 已提交
447 448
	unregister_netdevice_queue(dev, head);
	unregister_netdevice_queue(peer, head);
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
}

static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];

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)
{
475
	rtnl_link_unregister(&veth_link_ops);
476 477 478 479 480 481 482 483
}

module_init(veth_init);
module_exit(veth_exit);

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