1/*
2 *  drivers/net/veth.c
3 *
4 *  Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
5 *
6 * Author: Pavel Emelianov <xemul@openvz.org>
7 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
8 *
9 */
10
11#include <linux/netdevice.h>
12#include <linux/slab.h>
13#include <linux/ethtool.h>
14#include <linux/etherdevice.h>
15#include <linux/u64_stats_sync.h>
16
17#include <net/rtnetlink.h>
18#include <net/dst.h>
19#include <net/xfrm.h>
20#include <linux/veth.h>
21#include <linux/module.h>
22
23#define DRV_NAME	"veth"
24#define DRV_VERSION	"1.0"
25
26#define MIN_MTU 68		/* Min L3 MTU */
27#define MAX_MTU 65535		/* Max L3 MTU (arbitrary) */
28
29struct pcpu_vstats {
30	u64			packets;
31	u64			bytes;
32	struct u64_stats_sync	syncp;
33};
34
35struct veth_priv {
36	struct net_device __rcu	*peer;
37	atomic64_t		dropped;
38};
39
40/*
41 * ethtool interface
42 */
43
44static struct {
45	const char string[ETH_GSTRING_LEN];
46} ethtool_stats_keys[] = {
47	{ "peer_ifindex" },
48};
49
50static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
51{
52	cmd->supported		= 0;
53	cmd->advertising	= 0;
54	ethtool_cmd_speed_set(cmd, SPEED_10000);
55	cmd->duplex		= DUPLEX_FULL;
56	cmd->port		= PORT_TP;
57	cmd->phy_address	= 0;
58	cmd->transceiver	= XCVR_INTERNAL;
59	cmd->autoneg		= AUTONEG_DISABLE;
60	cmd->maxtxpkt		= 0;
61	cmd->maxrxpkt		= 0;
62	return 0;
63}
64
65static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
66{
67	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
68	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
69}
70
71static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
72{
73	switch(stringset) {
74	case ETH_SS_STATS:
75		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
76		break;
77	}
78}
79
80static int veth_get_sset_count(struct net_device *dev, int sset)
81{
82	switch (sset) {
83	case ETH_SS_STATS:
84		return ARRAY_SIZE(ethtool_stats_keys);
85	default:
86		return -EOPNOTSUPP;
87	}
88}
89
90static void veth_get_ethtool_stats(struct net_device *dev,
91		struct ethtool_stats *stats, u64 *data)
92{
93	struct veth_priv *priv = netdev_priv(dev);
94	struct net_device *peer = rtnl_dereference(priv->peer);
95
96	data[0] = peer ? peer->ifindex : 0;
97}
98
99static const struct ethtool_ops veth_ethtool_ops = {
100	.get_settings		= veth_get_settings,
101	.get_drvinfo		= veth_get_drvinfo,
102	.get_link		= ethtool_op_get_link,
103	.get_strings		= veth_get_strings,
104	.get_sset_count		= veth_get_sset_count,
105	.get_ethtool_stats	= veth_get_ethtool_stats,
106};
107
108static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
109{
110	struct veth_priv *priv = netdev_priv(dev);
111	struct net_device *rcv;
112	int length = skb->len;
113
114	rcu_read_lock();
115	rcv = rcu_dereference(priv->peer);
116	if (unlikely(!rcv)) {
117		kfree_skb(skb);
118		goto drop;
119	}
120
121	if (likely(dev_forward_skb(rcv, skb) == NET_RX_SUCCESS)) {
122		struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
123
124		u64_stats_update_begin(&stats->syncp);
125		stats->bytes += length;
126		stats->packets++;
127		u64_stats_update_end(&stats->syncp);
128	} else {
129drop:
130		atomic64_inc(&priv->dropped);
131	}
132	rcu_read_unlock();
133	return NETDEV_TX_OK;
134}
135
136/*
137 * general routines
138 */
139
140static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
141{
142	struct veth_priv *priv = netdev_priv(dev);
143	int cpu;
144
145	result->packets = 0;
146	result->bytes = 0;
147	for_each_possible_cpu(cpu) {
148		struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
149		u64 packets, bytes;
150		unsigned int start;
151
152		do {
153			start = u64_stats_fetch_begin_irq(&stats->syncp);
154			packets = stats->packets;
155			bytes = stats->bytes;
156		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
157		result->packets += packets;
158		result->bytes += bytes;
159	}
160	return atomic64_read(&priv->dropped);
161}
162
163static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
164						  struct rtnl_link_stats64 *tot)
165{
166	struct veth_priv *priv = netdev_priv(dev);
167	struct net_device *peer;
168	struct pcpu_vstats one;
169
170	tot->tx_dropped = veth_stats_one(&one, dev);
171	tot->tx_bytes = one.bytes;
172	tot->tx_packets = one.packets;
173
174	rcu_read_lock();
175	peer = rcu_dereference(priv->peer);
176	if (peer) {
177		tot->rx_dropped = veth_stats_one(&one, peer);
178		tot->rx_bytes = one.bytes;
179		tot->rx_packets = one.packets;
180	}
181	rcu_read_unlock();
182
183	return tot;
184}
185
186/* fake multicast ability */
187static void veth_set_multicast_list(struct net_device *dev)
188{
189}
190
191static int veth_open(struct net_device *dev)
192{
193	struct veth_priv *priv = netdev_priv(dev);
194	struct net_device *peer = rtnl_dereference(priv->peer);
195
196	if (!peer)
197		return -ENOTCONN;
198
199	if (peer->flags & IFF_UP) {
200		netif_carrier_on(dev);
201		netif_carrier_on(peer);
202	}
203	return 0;
204}
205
206static int veth_close(struct net_device *dev)
207{
208	struct veth_priv *priv = netdev_priv(dev);
209	struct net_device *peer = rtnl_dereference(priv->peer);
210
211	netif_carrier_off(dev);
212	if (peer)
213		netif_carrier_off(peer);
214
215	return 0;
216}
217
218static int is_valid_veth_mtu(int new_mtu)
219{
220	return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
221}
222
223static int veth_change_mtu(struct net_device *dev, int new_mtu)
224{
225	if (!is_valid_veth_mtu(new_mtu))
226		return -EINVAL;
227	dev->mtu = new_mtu;
228	return 0;
229}
230
231static int veth_dev_init(struct net_device *dev)
232{
233	dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats);
234	if (!dev->vstats)
235		return -ENOMEM;
236	return 0;
237}
238
239static void veth_dev_free(struct net_device *dev)
240{
241	free_percpu(dev->vstats);
242	free_netdev(dev);
243}
244
245#ifdef CONFIG_NET_POLL_CONTROLLER
246static void veth_poll_controller(struct net_device *dev)
247{
248	/* veth only receives frames when its peer sends one
249	 * Since it's a synchronous operation, we are guaranteed
250	 * never to have pending data when we poll for it so
251	 * there is nothing to do here.
252	 *
253	 * We need this though so netpoll recognizes us as an interface that
254	 * supports polling, which enables bridge devices in virt setups to
255	 * still use netconsole
256	 */
257}
258#endif	/* CONFIG_NET_POLL_CONTROLLER */
259
260static int veth_get_iflink(const struct net_device *dev)
261{
262	struct veth_priv *priv = netdev_priv(dev);
263	struct net_device *peer;
264	int iflink;
265
266	rcu_read_lock();
267	peer = rcu_dereference(priv->peer);
268	iflink = peer ? peer->ifindex : 0;
269	rcu_read_unlock();
270
271	return iflink;
272}
273
274static const struct net_device_ops veth_netdev_ops = {
275	.ndo_init            = veth_dev_init,
276	.ndo_open            = veth_open,
277	.ndo_stop            = veth_close,
278	.ndo_start_xmit      = veth_xmit,
279	.ndo_change_mtu      = veth_change_mtu,
280	.ndo_get_stats64     = veth_get_stats64,
281	.ndo_set_rx_mode     = veth_set_multicast_list,
282	.ndo_set_mac_address = eth_mac_addr,
283#ifdef CONFIG_NET_POLL_CONTROLLER
284	.ndo_poll_controller	= veth_poll_controller,
285#endif
286	.ndo_get_iflink		= veth_get_iflink,
287};
288
289#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_ALL_TSO |    \
290		       NETIF_F_HW_CSUM | NETIF_F_RXCSUM | NETIF_F_HIGHDMA | \
291		       NETIF_F_GSO_GRE | NETIF_F_GSO_UDP_TUNNEL |	    \
292		       NETIF_F_GSO_IPIP | NETIF_F_GSO_SIT | NETIF_F_UFO	|   \
293		       NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
294		       NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
295
296static void veth_setup(struct net_device *dev)
297{
298	ether_setup(dev);
299
300	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
301	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
302
303	dev->netdev_ops = &veth_netdev_ops;
304	dev->ethtool_ops = &veth_ethtool_ops;
305	dev->features |= NETIF_F_LLTX;
306	dev->features |= VETH_FEATURES;
307	dev->vlan_features = dev->features &
308			     ~(NETIF_F_HW_VLAN_CTAG_TX |
309			       NETIF_F_HW_VLAN_STAG_TX |
310			       NETIF_F_HW_VLAN_CTAG_RX |
311			       NETIF_F_HW_VLAN_STAG_RX);
312	dev->destructor = veth_dev_free;
313
314	dev->hw_features = VETH_FEATURES;
315	dev->hw_enc_features = VETH_FEATURES;
316}
317
318/*
319 * netlink interface
320 */
321
322static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
323{
324	if (tb[IFLA_ADDRESS]) {
325		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
326			return -EINVAL;
327		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
328			return -EADDRNOTAVAIL;
329	}
330	if (tb[IFLA_MTU]) {
331		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
332			return -EINVAL;
333	}
334	return 0;
335}
336
337static struct rtnl_link_ops veth_link_ops;
338
339static int veth_newlink(struct net *src_net, struct net_device *dev,
340			 struct nlattr *tb[], struct nlattr *data[])
341{
342	int err;
343	struct net_device *peer;
344	struct veth_priv *priv;
345	char ifname[IFNAMSIZ];
346	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
347	unsigned char name_assign_type;
348	struct ifinfomsg *ifmp;
349	struct net *net;
350
351	/*
352	 * create and register peer first
353	 */
354	if (data != NULL && data[VETH_INFO_PEER] != NULL) {
355		struct nlattr *nla_peer;
356
357		nla_peer = data[VETH_INFO_PEER];
358		ifmp = nla_data(nla_peer);
359		err = rtnl_nla_parse_ifla(peer_tb,
360					  nla_data(nla_peer) + sizeof(struct ifinfomsg),
361					  nla_len(nla_peer) - sizeof(struct ifinfomsg));
362		if (err < 0)
363			return err;
364
365		err = veth_validate(peer_tb, NULL);
366		if (err < 0)
367			return err;
368
369		tbp = peer_tb;
370	} else {
371		ifmp = NULL;
372		tbp = tb;
373	}
374
375	if (tbp[IFLA_IFNAME]) {
376		nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
377		name_assign_type = NET_NAME_USER;
378	} else {
379		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
380		name_assign_type = NET_NAME_ENUM;
381	}
382
383	net = rtnl_link_get_net(src_net, tbp);
384	if (IS_ERR(net))
385		return PTR_ERR(net);
386
387	peer = rtnl_create_link(net, ifname, name_assign_type,
388				&veth_link_ops, tbp);
389	if (IS_ERR(peer)) {
390		put_net(net);
391		return PTR_ERR(peer);
392	}
393
394	if (tbp[IFLA_ADDRESS] == NULL)
395		eth_hw_addr_random(peer);
396
397	if (ifmp && (dev->ifindex != 0))
398		peer->ifindex = ifmp->ifi_index;
399
400	err = register_netdevice(peer);
401	put_net(net);
402	net = NULL;
403	if (err < 0)
404		goto err_register_peer;
405
406	netif_carrier_off(peer);
407
408	err = rtnl_configure_link(peer, ifmp);
409	if (err < 0)
410		goto err_configure_peer;
411
412	/*
413	 * register dev last
414	 *
415	 * note, that since we've registered new device the dev's name
416	 * should be re-allocated
417	 */
418
419	if (tb[IFLA_ADDRESS] == NULL)
420		eth_hw_addr_random(dev);
421
422	if (tb[IFLA_IFNAME])
423		nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
424	else
425		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
426
427	err = register_netdevice(dev);
428	if (err < 0)
429		goto err_register_dev;
430
431	netif_carrier_off(dev);
432
433	/*
434	 * tie the deviced together
435	 */
436
437	priv = netdev_priv(dev);
438	rcu_assign_pointer(priv->peer, peer);
439
440	priv = netdev_priv(peer);
441	rcu_assign_pointer(priv->peer, dev);
442	return 0;
443
444err_register_dev:
445	/* nothing to do */
446err_configure_peer:
447	unregister_netdevice(peer);
448	return err;
449
450err_register_peer:
451	free_netdev(peer);
452	return err;
453}
454
455static void veth_dellink(struct net_device *dev, struct list_head *head)
456{
457	struct veth_priv *priv;
458	struct net_device *peer;
459
460	priv = netdev_priv(dev);
461	peer = rtnl_dereference(priv->peer);
462
463	/* Note : dellink() is called from default_device_exit_batch(),
464	 * before a rcu_synchronize() point. The devices are guaranteed
465	 * not being freed before one RCU grace period.
466	 */
467	RCU_INIT_POINTER(priv->peer, NULL);
468	unregister_netdevice_queue(dev, head);
469
470	if (peer) {
471		priv = netdev_priv(peer);
472		RCU_INIT_POINTER(priv->peer, NULL);
473		unregister_netdevice_queue(peer, head);
474	}
475}
476
477static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
478	[VETH_INFO_PEER]	= { .len = sizeof(struct ifinfomsg) },
479};
480
481static struct net *veth_get_link_net(const struct net_device *dev)
482{
483	struct veth_priv *priv = netdev_priv(dev);
484	struct net_device *peer = rtnl_dereference(priv->peer);
485
486	return peer ? dev_net(peer) : dev_net(dev);
487}
488
489static struct rtnl_link_ops veth_link_ops = {
490	.kind		= DRV_NAME,
491	.priv_size	= sizeof(struct veth_priv),
492	.setup		= veth_setup,
493	.validate	= veth_validate,
494	.newlink	= veth_newlink,
495	.dellink	= veth_dellink,
496	.policy		= veth_policy,
497	.maxtype	= VETH_INFO_MAX,
498	.get_link_net	= veth_get_link_net,
499};
500
501/*
502 * init/fini
503 */
504
505static __init int veth_init(void)
506{
507	return rtnl_link_register(&veth_link_ops);
508}
509
510static __exit void veth_exit(void)
511{
512	rtnl_link_unregister(&veth_link_ops);
513}
514
515module_init(veth_init);
516module_exit(veth_exit);
517
518MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
519MODULE_LICENSE("GPL v2");
520MODULE_ALIAS_RTNL_LINK(DRV_NAME);
521