Merge branch 'batman-adv/next' of git://git.open-mesh.org/ecsv/linux-merge
[pandora-kernel.git] / drivers / net / veth.c
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
26 struct veth_net_stats {
27         u64     rx_packets;
28         u64     tx_packets;
29         u64     rx_bytes;
30         u64     tx_bytes;
31         u64     tx_dropped;
32         u64     rx_dropped;
33 };
34
35 struct veth_priv {
36         struct net_device *peer;
37         struct veth_net_stats __percpu *stats;
38 };
39
40 /*
41  * ethtool interface
42  */
43
44 static struct {
45         const char string[ETH_GSTRING_LEN];
46 } ethtool_stats_keys[] = {
47         { "peer_ifindex" },
48 };
49
50 static 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
65 static 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
72 static 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
81 static int veth_get_sset_count(struct net_device *dev, int sset)
82 {
83         switch (sset) {
84         case ETH_SS_STATS:
85                 return ARRAY_SIZE(ethtool_stats_keys);
86         default:
87                 return -EOPNOTSUPP;
88         }
89 }
90
91 static 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
100 static const struct ethtool_ops veth_ethtool_ops = {
101         .get_settings           = veth_get_settings,
102         .get_drvinfo            = veth_get_drvinfo,
103         .get_link               = ethtool_op_get_link,
104         .get_strings            = veth_get_strings,
105         .get_sset_count         = veth_get_sset_count,
106         .get_ethtool_stats      = veth_get_ethtool_stats,
107 };
108
109 /*
110  * xmit
111  */
112
113 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
114 {
115         struct net_device *rcv = NULL;
116         struct veth_priv *priv, *rcv_priv;
117         struct veth_net_stats *stats, *rcv_stats;
118         int length;
119
120         priv = netdev_priv(dev);
121         rcv = priv->peer;
122         rcv_priv = netdev_priv(rcv);
123
124         stats = this_cpu_ptr(priv->stats);
125         rcv_stats = this_cpu_ptr(rcv_priv->stats);
126
127         if (!(rcv->flags & IFF_UP))
128                 goto tx_drop;
129
130         /* don't change ip_summed == CHECKSUM_PARTIAL, as that
131            will cause bad checksum on forwarded packets */
132         if (skb->ip_summed == CHECKSUM_NONE &&
133             rcv->features & NETIF_F_RXCSUM)
134                 skb->ip_summed = CHECKSUM_UNNECESSARY;
135
136         length = skb->len;
137         if (dev_forward_skb(rcv, skb) != NET_RX_SUCCESS)
138                 goto rx_drop;
139
140         stats->tx_bytes += length;
141         stats->tx_packets++;
142
143         rcv_stats->rx_bytes += length;
144         rcv_stats->rx_packets++;
145
146         return NETDEV_TX_OK;
147
148 tx_drop:
149         kfree_skb(skb);
150         stats->tx_dropped++;
151         return NETDEV_TX_OK;
152
153 rx_drop:
154         rcv_stats->rx_dropped++;
155         return NETDEV_TX_OK;
156 }
157
158 /*
159  * general routines
160  */
161
162 static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
163                                                   struct rtnl_link_stats64 *tot)
164 {
165         struct veth_priv *priv;
166         int cpu;
167         struct veth_net_stats *stats;
168
169         priv = netdev_priv(dev);
170
171         for_each_possible_cpu(cpu) {
172                 stats = per_cpu_ptr(priv->stats, cpu);
173
174                 tot->rx_packets += stats->rx_packets;
175                 tot->tx_packets += stats->tx_packets;
176                 tot->rx_bytes   += stats->rx_bytes;
177                 tot->tx_bytes   += stats->tx_bytes;
178                 tot->tx_dropped += stats->tx_dropped;
179                 tot->rx_dropped += stats->rx_dropped;
180         }
181
182         return tot;
183 }
184
185 static int veth_open(struct net_device *dev)
186 {
187         struct veth_priv *priv;
188
189         priv = netdev_priv(dev);
190         if (priv->peer == NULL)
191                 return -ENOTCONN;
192
193         if (priv->peer->flags & IFF_UP) {
194                 netif_carrier_on(dev);
195                 netif_carrier_on(priv->peer);
196         }
197         return 0;
198 }
199
200 static int veth_close(struct net_device *dev)
201 {
202         struct veth_priv *priv = netdev_priv(dev);
203
204         netif_carrier_off(dev);
205         netif_carrier_off(priv->peer);
206
207         return 0;
208 }
209
210 static int is_valid_veth_mtu(int new_mtu)
211 {
212         return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
213 }
214
215 static int veth_change_mtu(struct net_device *dev, int new_mtu)
216 {
217         if (!is_valid_veth_mtu(new_mtu))
218                 return -EINVAL;
219         dev->mtu = new_mtu;
220         return 0;
221 }
222
223 static int veth_dev_init(struct net_device *dev)
224 {
225         struct veth_net_stats __percpu *stats;
226         struct veth_priv *priv;
227
228         stats = alloc_percpu(struct veth_net_stats);
229         if (stats == NULL)
230                 return -ENOMEM;
231
232         priv = netdev_priv(dev);
233         priv->stats = stats;
234         return 0;
235 }
236
237 static void veth_dev_free(struct net_device *dev)
238 {
239         struct veth_priv *priv;
240
241         priv = netdev_priv(dev);
242         free_percpu(priv->stats);
243         free_netdev(dev);
244 }
245
246 static const struct net_device_ops veth_netdev_ops = {
247         .ndo_init            = veth_dev_init,
248         .ndo_open            = veth_open,
249         .ndo_stop            = veth_close,
250         .ndo_start_xmit      = veth_xmit,
251         .ndo_change_mtu      = veth_change_mtu,
252         .ndo_get_stats64     = veth_get_stats64,
253         .ndo_set_mac_address = eth_mac_addr,
254 };
255
256 static void veth_setup(struct net_device *dev)
257 {
258         ether_setup(dev);
259
260         dev->netdev_ops = &veth_netdev_ops;
261         dev->ethtool_ops = &veth_ethtool_ops;
262         dev->features |= NETIF_F_LLTX;
263         dev->destructor = veth_dev_free;
264
265         dev->hw_features = NETIF_F_NO_CSUM | NETIF_F_SG | NETIF_F_RXCSUM;
266 }
267
268 /*
269  * netlink interface
270  */
271
272 static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
273 {
274         if (tb[IFLA_ADDRESS]) {
275                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
276                         return -EINVAL;
277                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
278                         return -EADDRNOTAVAIL;
279         }
280         if (tb[IFLA_MTU]) {
281                 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
282                         return -EINVAL;
283         }
284         return 0;
285 }
286
287 static struct rtnl_link_ops veth_link_ops;
288
289 static int veth_newlink(struct net *src_net, struct net_device *dev,
290                          struct nlattr *tb[], struct nlattr *data[])
291 {
292         int err;
293         struct net_device *peer;
294         struct veth_priv *priv;
295         char ifname[IFNAMSIZ];
296         struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
297         struct ifinfomsg *ifmp;
298         struct net *net;
299
300         /*
301          * create and register peer first
302          */
303         if (data != NULL && data[VETH_INFO_PEER] != NULL) {
304                 struct nlattr *nla_peer;
305
306                 nla_peer = data[VETH_INFO_PEER];
307                 ifmp = nla_data(nla_peer);
308                 err = nla_parse(peer_tb, IFLA_MAX,
309                                 nla_data(nla_peer) + sizeof(struct ifinfomsg),
310                                 nla_len(nla_peer) - sizeof(struct ifinfomsg),
311                                 ifla_policy);
312                 if (err < 0)
313                         return err;
314
315                 err = veth_validate(peer_tb, NULL);
316                 if (err < 0)
317                         return err;
318
319                 tbp = peer_tb;
320         } else {
321                 ifmp = NULL;
322                 tbp = tb;
323         }
324
325         if (tbp[IFLA_IFNAME])
326                 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
327         else
328                 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
329
330         net = rtnl_link_get_net(src_net, tbp);
331         if (IS_ERR(net))
332                 return PTR_ERR(net);
333
334         peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
335         if (IS_ERR(peer)) {
336                 put_net(net);
337                 return PTR_ERR(peer);
338         }
339
340         if (tbp[IFLA_ADDRESS] == NULL)
341                 random_ether_addr(peer->dev_addr);
342
343         err = register_netdevice(peer);
344         put_net(net);
345         net = NULL;
346         if (err < 0)
347                 goto err_register_peer;
348
349         netif_carrier_off(peer);
350
351         err = rtnl_configure_link(peer, ifmp);
352         if (err < 0)
353                 goto err_configure_peer;
354
355         /*
356          * register dev last
357          *
358          * note, that since we've registered new device the dev's name
359          * should be re-allocated
360          */
361
362         if (tb[IFLA_ADDRESS] == NULL)
363                 random_ether_addr(dev->dev_addr);
364
365         if (tb[IFLA_IFNAME])
366                 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
367         else
368                 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
369
370         if (strchr(dev->name, '%')) {
371                 err = dev_alloc_name(dev, dev->name);
372                 if (err < 0)
373                         goto err_alloc_name;
374         }
375
376         err = register_netdevice(dev);
377         if (err < 0)
378                 goto err_register_dev;
379
380         netif_carrier_off(dev);
381
382         /*
383          * tie the deviced together
384          */
385
386         priv = netdev_priv(dev);
387         priv->peer = peer;
388
389         priv = netdev_priv(peer);
390         priv->peer = dev;
391         return 0;
392
393 err_register_dev:
394         /* nothing to do */
395 err_alloc_name:
396 err_configure_peer:
397         unregister_netdevice(peer);
398         return err;
399
400 err_register_peer:
401         free_netdev(peer);
402         return err;
403 }
404
405 static void veth_dellink(struct net_device *dev, struct list_head *head)
406 {
407         struct veth_priv *priv;
408         struct net_device *peer;
409
410         priv = netdev_priv(dev);
411         peer = priv->peer;
412
413         unregister_netdevice_queue(dev, head);
414         unregister_netdevice_queue(peer, head);
415 }
416
417 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1];
418
419 static struct rtnl_link_ops veth_link_ops = {
420         .kind           = DRV_NAME,
421         .priv_size      = sizeof(struct veth_priv),
422         .setup          = veth_setup,
423         .validate       = veth_validate,
424         .newlink        = veth_newlink,
425         .dellink        = veth_dellink,
426         .policy         = veth_policy,
427         .maxtype        = VETH_INFO_MAX,
428 };
429
430 /*
431  * init/fini
432  */
433
434 static __init int veth_init(void)
435 {
436         return rtnl_link_register(&veth_link_ops);
437 }
438
439 static __exit void veth_exit(void)
440 {
441         rtnl_link_unregister(&veth_link_ops);
442 }
443
444 module_init(veth_init);
445 module_exit(veth_exit);
446
447 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
448 MODULE_LICENSE("GPL v2");
449 MODULE_ALIAS_RTNL_LINK(DRV_NAME);