xref: /linux/drivers/net/veth.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
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 
16 #include <net/dst.h>
17 #include <net/xfrm.h>
18 #include <linux/veth.h>
19 
20 #define DRV_NAME	"veth"
21 #define DRV_VERSION	"1.0"
22 
23 #define MIN_MTU 68		/* Min L3 MTU */
24 #define MAX_MTU 65535		/* Max L3 MTU (arbitrary) */
25 #define MTU_PAD (ETH_HLEN + 4)  /* Max difference between L2 and L3 size MTU */
26 
27 struct veth_net_stats {
28 	unsigned long	rx_packets;
29 	unsigned long	tx_packets;
30 	unsigned long	rx_bytes;
31 	unsigned long	tx_bytes;
32 	unsigned long	tx_dropped;
33 	unsigned long	rx_dropped;
34 };
35 
36 struct veth_priv {
37 	struct net_device *peer;
38 	struct veth_net_stats __percpu *stats;
39 	unsigned ip_summed;
40 };
41 
42 /*
43  * ethtool interface
44  */
45 
46 static struct {
47 	const char string[ETH_GSTRING_LEN];
48 } ethtool_stats_keys[] = {
49 	{ "peer_ifindex" },
50 };
51 
52 static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
53 {
54 	cmd->supported		= 0;
55 	cmd->advertising	= 0;
56 	cmd->speed		= SPEED_10000;
57 	cmd->duplex		= DUPLEX_FULL;
58 	cmd->port		= PORT_TP;
59 	cmd->phy_address	= 0;
60 	cmd->transceiver	= XCVR_INTERNAL;
61 	cmd->autoneg		= AUTONEG_DISABLE;
62 	cmd->maxtxpkt		= 0;
63 	cmd->maxrxpkt		= 0;
64 	return 0;
65 }
66 
67 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
68 {
69 	strcpy(info->driver, DRV_NAME);
70 	strcpy(info->version, DRV_VERSION);
71 	strcpy(info->fw_version, "N/A");
72 }
73 
74 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
75 {
76 	switch(stringset) {
77 	case ETH_SS_STATS:
78 		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
79 		break;
80 	}
81 }
82 
83 static int veth_get_sset_count(struct net_device *dev, int sset)
84 {
85 	switch (sset) {
86 	case ETH_SS_STATS:
87 		return ARRAY_SIZE(ethtool_stats_keys);
88 	default:
89 		return -EOPNOTSUPP;
90 	}
91 }
92 
93 static void veth_get_ethtool_stats(struct net_device *dev,
94 		struct ethtool_stats *stats, u64 *data)
95 {
96 	struct veth_priv *priv;
97 
98 	priv = netdev_priv(dev);
99 	data[0] = priv->peer->ifindex;
100 }
101 
102 static u32 veth_get_rx_csum(struct net_device *dev)
103 {
104 	struct veth_priv *priv;
105 
106 	priv = netdev_priv(dev);
107 	return priv->ip_summed == CHECKSUM_UNNECESSARY;
108 }
109 
110 static int veth_set_rx_csum(struct net_device *dev, u32 data)
111 {
112 	struct veth_priv *priv;
113 
114 	priv = netdev_priv(dev);
115 	priv->ip_summed = data ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE;
116 	return 0;
117 }
118 
119 static u32 veth_get_tx_csum(struct net_device *dev)
120 {
121 	return (dev->features & NETIF_F_NO_CSUM) != 0;
122 }
123 
124 static int veth_set_tx_csum(struct net_device *dev, u32 data)
125 {
126 	if (data)
127 		dev->features |= NETIF_F_NO_CSUM;
128 	else
129 		dev->features &= ~NETIF_F_NO_CSUM;
130 	return 0;
131 }
132 
133 static const struct ethtool_ops veth_ethtool_ops = {
134 	.get_settings		= veth_get_settings,
135 	.get_drvinfo		= veth_get_drvinfo,
136 	.get_link		= ethtool_op_get_link,
137 	.get_rx_csum		= veth_get_rx_csum,
138 	.set_rx_csum		= veth_set_rx_csum,
139 	.get_tx_csum		= veth_get_tx_csum,
140 	.set_tx_csum		= veth_set_tx_csum,
141 	.get_sg			= ethtool_op_get_sg,
142 	.set_sg			= ethtool_op_set_sg,
143 	.get_strings		= veth_get_strings,
144 	.get_sset_count		= veth_get_sset_count,
145 	.get_ethtool_stats	= veth_get_ethtool_stats,
146 };
147 
148 /*
149  * xmit
150  */
151 
152 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
153 {
154 	struct net_device *rcv = NULL;
155 	struct veth_priv *priv, *rcv_priv;
156 	struct veth_net_stats *stats, *rcv_stats;
157 	int length;
158 
159 	priv = netdev_priv(dev);
160 	rcv = priv->peer;
161 	rcv_priv = netdev_priv(rcv);
162 
163 	stats = this_cpu_ptr(priv->stats);
164 	rcv_stats = this_cpu_ptr(rcv_priv->stats);
165 
166 	if (!(rcv->flags & IFF_UP))
167 		goto tx_drop;
168 
169 	if (dev->features & NETIF_F_NO_CSUM)
170 		skb->ip_summed = rcv_priv->ip_summed;
171 
172 	length = skb->len + ETH_HLEN;
173 	if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)
174 		goto rx_drop;
175 
176 	stats->tx_bytes += length;
177 	stats->tx_packets++;
178 
179 	rcv_stats->rx_bytes += length;
180 	rcv_stats->rx_packets++;
181 
182 	return NETDEV_TX_OK;
183 
184 tx_drop:
185 	kfree_skb(skb);
186 	stats->tx_dropped++;
187 	return NETDEV_TX_OK;
188 
189 rx_drop:
190 	kfree_skb(skb);
191 	rcv_stats->rx_dropped++;
192 	return NETDEV_TX_OK;
193 }
194 
195 /*
196  * general routines
197  */
198 
199 static struct net_device_stats *veth_get_stats(struct net_device *dev)
200 {
201 	struct veth_priv *priv;
202 	int cpu;
203 	struct veth_net_stats *stats, total = {0};
204 
205 	priv = netdev_priv(dev);
206 
207 	for_each_possible_cpu(cpu) {
208 		stats = per_cpu_ptr(priv->stats, cpu);
209 
210 		total.rx_packets += stats->rx_packets;
211 		total.tx_packets += stats->tx_packets;
212 		total.rx_bytes   += stats->rx_bytes;
213 		total.tx_bytes   += stats->tx_bytes;
214 		total.tx_dropped += stats->tx_dropped;
215 		total.rx_dropped += stats->rx_dropped;
216 	}
217 	dev->stats.rx_packets = total.rx_packets;
218 	dev->stats.tx_packets = total.tx_packets;
219 	dev->stats.rx_bytes   = total.rx_bytes;
220 	dev->stats.tx_bytes   = total.tx_bytes;
221 	dev->stats.tx_dropped = total.tx_dropped;
222 	dev->stats.rx_dropped = total.rx_dropped;
223 
224 	return &dev->stats;
225 }
226 
227 static int veth_open(struct net_device *dev)
228 {
229 	struct veth_priv *priv;
230 
231 	priv = netdev_priv(dev);
232 	if (priv->peer == NULL)
233 		return -ENOTCONN;
234 
235 	if (priv->peer->flags & IFF_UP) {
236 		netif_carrier_on(dev);
237 		netif_carrier_on(priv->peer);
238 	}
239 	return 0;
240 }
241 
242 static int veth_close(struct net_device *dev)
243 {
244 	struct veth_priv *priv = netdev_priv(dev);
245 
246 	netif_carrier_off(dev);
247 	netif_carrier_off(priv->peer);
248 
249 	return 0;
250 }
251 
252 static int is_valid_veth_mtu(int new_mtu)
253 {
254 	return (new_mtu >= MIN_MTU && new_mtu <= MAX_MTU);
255 }
256 
257 static int veth_change_mtu(struct net_device *dev, int new_mtu)
258 {
259 	if (!is_valid_veth_mtu(new_mtu))
260 		return -EINVAL;
261 	dev->mtu = new_mtu;
262 	return 0;
263 }
264 
265 static int veth_dev_init(struct net_device *dev)
266 {
267 	struct veth_net_stats __percpu *stats;
268 	struct veth_priv *priv;
269 
270 	stats = alloc_percpu(struct veth_net_stats);
271 	if (stats == NULL)
272 		return -ENOMEM;
273 
274 	priv = netdev_priv(dev);
275 	priv->stats = stats;
276 	return 0;
277 }
278 
279 static void veth_dev_free(struct net_device *dev)
280 {
281 	struct veth_priv *priv;
282 
283 	priv = netdev_priv(dev);
284 	free_percpu(priv->stats);
285 	free_netdev(dev);
286 }
287 
288 static const struct net_device_ops veth_netdev_ops = {
289 	.ndo_init            = veth_dev_init,
290 	.ndo_open            = veth_open,
291 	.ndo_stop            = veth_close,
292 	.ndo_start_xmit      = veth_xmit,
293 	.ndo_change_mtu      = veth_change_mtu,
294 	.ndo_get_stats       = veth_get_stats,
295 	.ndo_set_mac_address = eth_mac_addr,
296 };
297 
298 static void veth_setup(struct net_device *dev)
299 {
300 	ether_setup(dev);
301 
302 	dev->netdev_ops = &veth_netdev_ops;
303 	dev->ethtool_ops = &veth_ethtool_ops;
304 	dev->features |= NETIF_F_LLTX;
305 	dev->destructor = veth_dev_free;
306 }
307 
308 /*
309  * netlink interface
310  */
311 
312 static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
313 {
314 	if (tb[IFLA_ADDRESS]) {
315 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
316 			return -EINVAL;
317 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
318 			return -EADDRNOTAVAIL;
319 	}
320 	if (tb[IFLA_MTU]) {
321 		if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
322 			return -EINVAL;
323 	}
324 	return 0;
325 }
326 
327 static struct rtnl_link_ops veth_link_ops;
328 
329 static int veth_newlink(struct net *src_net, struct net_device *dev,
330 			 struct nlattr *tb[], struct nlattr *data[])
331 {
332 	int err;
333 	struct net_device *peer;
334 	struct veth_priv *priv;
335 	char ifname[IFNAMSIZ];
336 	struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
337 	struct ifinfomsg *ifmp;
338 	struct net *net;
339 
340 	/*
341 	 * create and register peer first
342 	 */
343 	if (data != NULL && data[VETH_INFO_PEER] != NULL) {
344 		struct nlattr *nla_peer;
345 
346 		nla_peer = data[VETH_INFO_PEER];
347 		ifmp = nla_data(nla_peer);
348 		err = nla_parse(peer_tb, IFLA_MAX,
349 				nla_data(nla_peer) + sizeof(struct ifinfomsg),
350 				nla_len(nla_peer) - sizeof(struct ifinfomsg),
351 				ifla_policy);
352 		if (err < 0)
353 			return err;
354 
355 		err = veth_validate(peer_tb, NULL);
356 		if (err < 0)
357 			return err;
358 
359 		tbp = peer_tb;
360 	} else {
361 		ifmp = NULL;
362 		tbp = tb;
363 	}
364 
365 	if (tbp[IFLA_IFNAME])
366 		nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
367 	else
368 		snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
369 
370 	net = rtnl_link_get_net(src_net, tbp);
371 	if (IS_ERR(net))
372 		return PTR_ERR(net);
373 
374 	peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
375 	if (IS_ERR(peer)) {
376 		put_net(net);
377 		return PTR_ERR(peer);
378 	}
379 
380 	if (tbp[IFLA_ADDRESS] == NULL)
381 		random_ether_addr(peer->dev_addr);
382 
383 	err = register_netdevice(peer);
384 	put_net(net);
385 	net = NULL;
386 	if (err < 0)
387 		goto err_register_peer;
388 
389 	netif_carrier_off(peer);
390 
391 	err = rtnl_configure_link(peer, ifmp);
392 	if (err < 0)
393 		goto err_configure_peer;
394 
395 	/*
396 	 * register dev last
397 	 *
398 	 * note, that since we've registered new device the dev's name
399 	 * should be re-allocated
400 	 */
401 
402 	if (tb[IFLA_ADDRESS] == NULL)
403 		random_ether_addr(dev->dev_addr);
404 
405 	if (tb[IFLA_IFNAME])
406 		nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
407 	else
408 		snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
409 
410 	if (strchr(dev->name, '%')) {
411 		err = dev_alloc_name(dev, dev->name);
412 		if (err < 0)
413 			goto err_alloc_name;
414 	}
415 
416 	err = register_netdevice(dev);
417 	if (err < 0)
418 		goto err_register_dev;
419 
420 	netif_carrier_off(dev);
421 
422 	/*
423 	 * tie the deviced together
424 	 */
425 
426 	priv = netdev_priv(dev);
427 	priv->peer = peer;
428 
429 	priv = netdev_priv(peer);
430 	priv->peer = dev;
431 	return 0;
432 
433 err_register_dev:
434 	/* nothing to do */
435 err_alloc_name:
436 err_configure_peer:
437 	unregister_netdevice(peer);
438 	return err;
439 
440 err_register_peer:
441 	free_netdev(peer);
442 	return err;
443 }
444 
445 static void veth_dellink(struct net_device *dev, struct list_head *head)
446 {
447 	struct veth_priv *priv;
448 	struct net_device *peer;
449 
450 	priv = netdev_priv(dev);
451 	peer = priv->peer;
452 
453 	unregister_netdevice_queue(dev, head);
454 	unregister_netdevice_queue(peer, head);
455 }
456 
457 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
458 
459 static struct rtnl_link_ops veth_link_ops = {
460 	.kind		= DRV_NAME,
461 	.priv_size	= sizeof(struct veth_priv),
462 	.setup		= veth_setup,
463 	.validate	= veth_validate,
464 	.newlink	= veth_newlink,
465 	.dellink	= veth_dellink,
466 	.policy		= veth_policy,
467 	.maxtype	= VETH_INFO_MAX,
468 };
469 
470 /*
471  * init/fini
472  */
473 
474 static __init int veth_init(void)
475 {
476 	return rtnl_link_register(&veth_link_ops);
477 }
478 
479 static __exit void veth_exit(void)
480 {
481 	rtnl_link_unregister(&veth_link_ops);
482 }
483 
484 module_init(veth_init);
485 module_exit(veth_exit);
486 
487 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
488 MODULE_LICENSE("GPL v2");
489 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
490