blob: 8461576fa015439f4f9e183f25ad008414d4b0e1 [file] [log] [blame]
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001/*
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
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070011#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070013#include <linux/ethtool.h>
14#include <linux/etherdevice.h>
15
16#include <net/dst.h>
17#include <net/xfrm.h>
Stephen Hemmingerecef9692007-12-25 17:23:59 -080018#include <linux/veth.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070019
20#define DRV_NAME "veth"
21#define DRV_VERSION "1.0"
22
Eric Biederman38d40812009-03-03 23:36:04 -080023#define MIN_MTU 68 /* Min L3 MTU */
24#define MAX_MTU 65535 /* Max L3 MTU (arbitrary) */
Eric Biederman38d40812009-03-03 23:36:04 -080025
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070026struct veth_net_stats {
27 unsigned long rx_packets;
28 unsigned long tx_packets;
29 unsigned long rx_bytes;
30 unsigned long tx_bytes;
31 unsigned long tx_dropped;
Eric Biederman38d40812009-03-03 23:36:04 -080032 unsigned long rx_dropped;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070033};
34
35struct veth_priv {
36 struct net_device *peer;
Tejun Heo47d74272010-02-16 15:21:08 +000037 struct veth_net_stats __percpu *stats;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070038};
39
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070040/*
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;
David Decotigny70739492011-04-27 18:32:40 +000054 ethtool_cmd_speed_set(cmd, SPEED_10000);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070055 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 strcpy(info->driver, DRV_NAME);
68 strcpy(info->version, DRV_VERSION);
69 strcpy(info->fw_version, "N/A");
70}
71
72static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
73{
74 switch(stringset) {
75 case ETH_SS_STATS:
76 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
77 break;
78 }
79}
80
Jeff Garzikb9f2c042007-10-03 18:07:32 -070081static int veth_get_sset_count(struct net_device *dev, int sset)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070082{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070083 switch (sset) {
84 case ETH_SS_STATS:
85 return ARRAY_SIZE(ethtool_stats_keys);
86 default:
87 return -EOPNOTSUPP;
88 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070089}
90
91static void veth_get_ethtool_stats(struct net_device *dev,
92 struct ethtool_stats *stats, u64 *data)
93{
94 struct veth_priv *priv;
95
96 priv = netdev_priv(dev);
97 data[0] = priv->peer->ifindex;
98}
99
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700100static const struct ethtool_ops veth_ethtool_ops = {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700101 .get_settings = veth_get_settings,
102 .get_drvinfo = veth_get_drvinfo,
103 .get_link = ethtool_op_get_link,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700104 .get_strings = veth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700105 .get_sset_count = veth_get_sset_count,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700106 .get_ethtool_stats = veth_get_ethtool_stats,
107};
108
109/*
110 * xmit
111 */
112
Stephen Hemminger424efe92009-08-31 19:50:51 +0000113static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700114{
115 struct net_device *rcv = NULL;
116 struct veth_priv *priv, *rcv_priv;
Eric Biederman38d40812009-03-03 23:36:04 -0800117 struct veth_net_stats *stats, *rcv_stats;
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900118 int length;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700119
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700120 priv = netdev_priv(dev);
121 rcv = priv->peer;
122 rcv_priv = netdev_priv(rcv);
123
Christoph Lametere7dcaa42009-10-03 19:48:23 +0900124 stats = this_cpu_ptr(priv->stats);
125 rcv_stats = this_cpu_ptr(rcv_priv->stats);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700126
127 if (!(rcv->flags & IFF_UP))
Eric Biederman38d40812009-03-03 23:36:04 -0800128 goto tx_drop;
129
Michał Mirosław0b796752010-12-14 12:35:13 +0000130 /* don't change ip_summed == CHECKSUM_PARTIAL, as that
131 will cause bad checksum on forwarded packets */
Michał Mirosława2c725f2011-03-31 01:01:35 +0000132 if (skb->ip_summed == CHECKSUM_NONE &&
133 rcv->features & NETIF_F_RXCSUM)
134 skb->ip_summed = CHECKSUM_UNNECESSARY;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700135
Eric W. Biederman675071a2011-03-21 18:24:53 -0700136 length = skb->len;
Arnd Bergmann44540962009-11-26 06:07:08 +0000137 if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)
138 goto rx_drop;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700139
140 stats->tx_bytes += length;
141 stats->tx_packets++;
142
Eric Biederman38d40812009-03-03 23:36:04 -0800143 rcv_stats->rx_bytes += length;
144 rcv_stats->rx_packets++;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700145
Patrick McHardy6ed10652009-06-23 06:03:08 +0000146 return NETDEV_TX_OK;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700147
Eric Biederman38d40812009-03-03 23:36:04 -0800148tx_drop:
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700149 kfree_skb(skb);
150 stats->tx_dropped++;
Patrick McHardy6ed10652009-06-23 06:03:08 +0000151 return NETDEV_TX_OK;
Eric Biederman38d40812009-03-03 23:36:04 -0800152
153rx_drop:
Eric Biederman38d40812009-03-03 23:36:04 -0800154 rcv_stats->rx_dropped++;
Patrick McHardy6ed10652009-06-23 06:03:08 +0000155 return NETDEV_TX_OK;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700156}
157
158/*
159 * general routines
160 */
161
162static struct net_device_stats *veth_get_stats(struct net_device *dev)
163{
David S. Miller11687a12009-06-25 02:45:42 -0700164 struct veth_priv *priv;
David S. Miller11687a12009-06-25 02:45:42 -0700165 int cpu;
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000166 struct veth_net_stats *stats, total = {0};
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700167
David S. Miller11687a12009-06-25 02:45:42 -0700168 priv = netdev_priv(dev);
David S. Miller11687a12009-06-25 02:45:42 -0700169
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000170 for_each_possible_cpu(cpu) {
David S. Miller11687a12009-06-25 02:45:42 -0700171 stats = per_cpu_ptr(priv->stats, cpu);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700172
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000173 total.rx_packets += stats->rx_packets;
174 total.tx_packets += stats->tx_packets;
175 total.rx_bytes += stats->rx_bytes;
176 total.tx_bytes += stats->tx_bytes;
177 total.tx_dropped += stats->tx_dropped;
178 total.rx_dropped += stats->rx_dropped;
David S. Miller11687a12009-06-25 02:45:42 -0700179 }
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000180 dev->stats.rx_packets = total.rx_packets;
181 dev->stats.tx_packets = total.tx_packets;
182 dev->stats.rx_bytes = total.rx_bytes;
183 dev->stats.tx_bytes = total.tx_bytes;
184 dev->stats.tx_dropped = total.tx_dropped;
185 dev->stats.rx_dropped = total.rx_dropped;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700186
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000187 return &dev->stats;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700188}
189
190static int veth_open(struct net_device *dev)
191{
192 struct veth_priv *priv;
193
194 priv = netdev_priv(dev);
195 if (priv->peer == NULL)
196 return -ENOTCONN;
197
198 if (priv->peer->flags & IFF_UP) {
199 netif_carrier_on(dev);
200 netif_carrier_on(priv->peer);
201 }
202 return 0;
203}
204
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000205static int veth_close(struct net_device *dev)
206{
207 struct veth_priv *priv = netdev_priv(dev);
208
209 netif_carrier_off(dev);
210 netif_carrier_off(priv->peer);
211
212 return 0;
213}
214
Eric Biederman38d40812009-03-03 23:36:04 -0800215static int is_valid_veth_mtu(int new_mtu)
216{
Eric Dumazet807540b2010-09-23 05:40:09 +0000217 return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
Eric Biederman38d40812009-03-03 23:36:04 -0800218}
219
220static int veth_change_mtu(struct net_device *dev, int new_mtu)
221{
222 if (!is_valid_veth_mtu(new_mtu))
223 return -EINVAL;
224 dev->mtu = new_mtu;
225 return 0;
226}
227
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700228static int veth_dev_init(struct net_device *dev)
229{
Tejun Heo47d74272010-02-16 15:21:08 +0000230 struct veth_net_stats __percpu *stats;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700231 struct veth_priv *priv;
232
233 stats = alloc_percpu(struct veth_net_stats);
234 if (stats == NULL)
235 return -ENOMEM;
236
237 priv = netdev_priv(dev);
238 priv->stats = stats;
239 return 0;
240}
241
David S. Miller11687a12009-06-25 02:45:42 -0700242static void veth_dev_free(struct net_device *dev)
243{
244 struct veth_priv *priv;
245
246 priv = netdev_priv(dev);
247 free_percpu(priv->stats);
248 free_netdev(dev);
249}
250
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800251static const struct net_device_ops veth_netdev_ops = {
Daniel Lezcanoee923622009-02-22 00:04:45 -0800252 .ndo_init = veth_dev_init,
253 .ndo_open = veth_open,
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000254 .ndo_stop = veth_close,
Daniel Lezcanoee923622009-02-22 00:04:45 -0800255 .ndo_start_xmit = veth_xmit,
Eric Biederman38d40812009-03-03 23:36:04 -0800256 .ndo_change_mtu = veth_change_mtu,
Daniel Lezcanoee923622009-02-22 00:04:45 -0800257 .ndo_get_stats = veth_get_stats,
258 .ndo_set_mac_address = eth_mac_addr,
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800259};
260
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700261static void veth_setup(struct net_device *dev)
262{
263 ether_setup(dev);
264
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800265 dev->netdev_ops = &veth_netdev_ops;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700266 dev->ethtool_ops = &veth_ethtool_ops;
267 dev->features |= NETIF_F_LLTX;
David S. Miller11687a12009-06-25 02:45:42 -0700268 dev->destructor = veth_dev_free;
Michał Mirosława2c725f2011-03-31 01:01:35 +0000269
270 dev->hw_features = NETIF_F_NO_CSUM | NETIF_F_SG | NETIF_F_RXCSUM;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700271}
272
273/*
274 * netlink interface
275 */
276
277static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
278{
279 if (tb[IFLA_ADDRESS]) {
280 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
281 return -EINVAL;
282 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
283 return -EADDRNOTAVAIL;
284 }
Eric Biederman38d40812009-03-03 23:36:04 -0800285 if (tb[IFLA_MTU]) {
286 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
287 return -EINVAL;
288 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700289 return 0;
290}
291
292static struct rtnl_link_ops veth_link_ops;
293
Eric W. Biederman81adee42009-11-08 00:53:51 -0800294static int veth_newlink(struct net *src_net, struct net_device *dev,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700295 struct nlattr *tb[], struct nlattr *data[])
296{
297 int err;
298 struct net_device *peer;
299 struct veth_priv *priv;
300 char ifname[IFNAMSIZ];
301 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
Patrick McHardy3729d502010-02-26 06:34:54 +0000302 struct ifinfomsg *ifmp;
Eric W. Biederman81adee42009-11-08 00:53:51 -0800303 struct net *net;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700304
305 /*
306 * create and register peer first
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700307 */
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700308 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
309 struct nlattr *nla_peer;
310
311 nla_peer = data[VETH_INFO_PEER];
Patrick McHardy3729d502010-02-26 06:34:54 +0000312 ifmp = nla_data(nla_peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700313 err = nla_parse(peer_tb, IFLA_MAX,
314 nla_data(nla_peer) + sizeof(struct ifinfomsg),
315 nla_len(nla_peer) - sizeof(struct ifinfomsg),
316 ifla_policy);
317 if (err < 0)
318 return err;
319
320 err = veth_validate(peer_tb, NULL);
321 if (err < 0)
322 return err;
323
324 tbp = peer_tb;
Patrick McHardy3729d502010-02-26 06:34:54 +0000325 } else {
326 ifmp = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700327 tbp = tb;
Patrick McHardy3729d502010-02-26 06:34:54 +0000328 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700329
330 if (tbp[IFLA_IFNAME])
331 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
332 else
333 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
334
Eric W. Biederman81adee42009-11-08 00:53:51 -0800335 net = rtnl_link_get_net(src_net, tbp);
336 if (IS_ERR(net))
337 return PTR_ERR(net);
338
339 peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
340 if (IS_ERR(peer)) {
341 put_net(net);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700342 return PTR_ERR(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -0800343 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700344
345 if (tbp[IFLA_ADDRESS] == NULL)
346 random_ether_addr(peer->dev_addr);
347
348 err = register_netdevice(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -0800349 put_net(net);
350 net = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700351 if (err < 0)
352 goto err_register_peer;
353
354 netif_carrier_off(peer);
355
Patrick McHardy3729d502010-02-26 06:34:54 +0000356 err = rtnl_configure_link(peer, ifmp);
357 if (err < 0)
358 goto err_configure_peer;
359
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700360 /*
361 * register dev last
362 *
363 * note, that since we've registered new device the dev's name
364 * should be re-allocated
365 */
366
367 if (tb[IFLA_ADDRESS] == NULL)
368 random_ether_addr(dev->dev_addr);
369
Jiri Pirko6c8c4442011-04-30 01:28:17 +0000370 if (tb[IFLA_IFNAME])
371 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
372 else
373 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
374
375 if (strchr(dev->name, '%')) {
376 err = dev_alloc_name(dev, dev->name);
377 if (err < 0)
378 goto err_alloc_name;
379 }
380
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700381 err = register_netdevice(dev);
382 if (err < 0)
383 goto err_register_dev;
384
385 netif_carrier_off(dev);
386
387 /*
388 * tie the deviced together
389 */
390
391 priv = netdev_priv(dev);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700392 priv->peer = peer;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700393
394 priv = netdev_priv(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700395 priv->peer = dev;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700396 return 0;
397
398err_register_dev:
399 /* nothing to do */
Jiri Pirko6c8c4442011-04-30 01:28:17 +0000400err_alloc_name:
Patrick McHardy3729d502010-02-26 06:34:54 +0000401err_configure_peer:
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700402 unregister_netdevice(peer);
403 return err;
404
405err_register_peer:
406 free_netdev(peer);
407 return err;
408}
409
Eric Dumazet23289a32009-10-27 07:06:36 +0000410static void veth_dellink(struct net_device *dev, struct list_head *head)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700411{
412 struct veth_priv *priv;
413 struct net_device *peer;
414
415 priv = netdev_priv(dev);
416 peer = priv->peer;
417
Eric Dumazet24540532009-10-30 01:00:27 -0700418 unregister_netdevice_queue(dev, head);
419 unregister_netdevice_queue(peer, head);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700420}
421
422static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
423
424static struct rtnl_link_ops veth_link_ops = {
425 .kind = DRV_NAME,
426 .priv_size = sizeof(struct veth_priv),
427 .setup = veth_setup,
428 .validate = veth_validate,
429 .newlink = veth_newlink,
430 .dellink = veth_dellink,
431 .policy = veth_policy,
432 .maxtype = VETH_INFO_MAX,
433};
434
435/*
436 * init/fini
437 */
438
439static __init int veth_init(void)
440{
441 return rtnl_link_register(&veth_link_ops);
442}
443
444static __exit void veth_exit(void)
445{
Patrick McHardy68365452008-01-20 17:25:14 -0800446 rtnl_link_unregister(&veth_link_ops);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700447}
448
449module_init(veth_init);
450module_exit(veth_exit);
451
452MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
453MODULE_LICENSE("GPL v2");
454MODULE_ALIAS_RTNL_LINK(DRV_NAME);