4 * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
6 * Author: Pavel Emelianov <xemul@openvz.org>
7 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
11 #include <linux/netdevice.h>
12 #include <linux/slab.h>
13 #include <linux/ethtool.h>
14 #include <linux/etherdevice.h>
18 #include <linux/veth.h>
20 #define DRV_NAME "veth"
21 #define DRV_VERSION "1.0"
23 #define MIN_MTU 68 /* Min L3 MTU */
24 #define MAX_MTU 65535 /* Max L3 MTU (arbitrary) */
26 struct 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
;
32 unsigned long rx_dropped
;
36 struct net_device
*peer
;
37 struct veth_net_stats __percpu
*stats
;
45 const char string
[ETH_GSTRING_LEN
];
46 } ethtool_stats_keys
[] = {
50 static int veth_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
54 ethtool_cmd_speed_set(cmd
, SPEED_10000
);
55 cmd
->duplex
= DUPLEX_FULL
;
58 cmd
->transceiver
= XCVR_INTERNAL
;
59 cmd
->autoneg
= AUTONEG_DISABLE
;
65 static void veth_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
67 strcpy(info
->driver
, DRV_NAME
);
68 strcpy(info
->version
, DRV_VERSION
);
69 strcpy(info
->fw_version
, "N/A");
72 static void veth_get_strings(struct net_device
*dev
, u32 stringset
, u8
*buf
)
76 memcpy(buf
, ðtool_stats_keys
, sizeof(ethtool_stats_keys
));
81 static int veth_get_sset_count(struct net_device
*dev
, int sset
)
85 return ARRAY_SIZE(ethtool_stats_keys
);
91 static void veth_get_ethtool_stats(struct net_device
*dev
,
92 struct ethtool_stats
*stats
, u64
*data
)
94 struct veth_priv
*priv
;
96 priv
= netdev_priv(dev
);
97 data
[0] = priv
->peer
->ifindex
;
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
,
113 static netdev_tx_t
veth_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
115 struct net_device
*rcv
= NULL
;
116 struct veth_priv
*priv
, *rcv_priv
;
117 struct veth_net_stats
*stats
, *rcv_stats
;
120 priv
= netdev_priv(dev
);
122 rcv_priv
= netdev_priv(rcv
);
124 stats
= this_cpu_ptr(priv
->stats
);
125 rcv_stats
= this_cpu_ptr(rcv_priv
->stats
);
127 if (!(rcv
->flags
& IFF_UP
))
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
;
137 if (dev_forward_skb(rcv
, skb
) != NET_RX_SUCCESS
)
140 stats
->tx_bytes
+= length
;
143 rcv_stats
->rx_bytes
+= length
;
144 rcv_stats
->rx_packets
++;
154 rcv_stats
->rx_dropped
++;
162 static struct net_device_stats
*veth_get_stats(struct net_device
*dev
)
164 struct veth_priv
*priv
;
166 struct veth_net_stats
*stats
, total
= {0};
168 priv
= netdev_priv(dev
);
170 for_each_possible_cpu(cpu
) {
171 stats
= per_cpu_ptr(priv
->stats
, cpu
);
173 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
;
180 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
;
190 static int veth_open(struct net_device
*dev
)
192 struct veth_priv
*priv
;
194 priv
= netdev_priv(dev
);
195 if (priv
->peer
== NULL
)
198 if (priv
->peer
->flags
& IFF_UP
) {
199 netif_carrier_on(dev
);
200 netif_carrier_on(priv
->peer
);
205 static int veth_close(struct net_device
*dev
)
207 struct veth_priv
*priv
= netdev_priv(dev
);
209 netif_carrier_off(dev
);
210 netif_carrier_off(priv
->peer
);
215 static int is_valid_veth_mtu(int new_mtu
)
217 return new_mtu
>= MIN_MTU
&& new_mtu
<= MAX_MTU
;
220 static int veth_change_mtu(struct net_device
*dev
, int new_mtu
)
222 if (!is_valid_veth_mtu(new_mtu
))
228 static int veth_dev_init(struct net_device
*dev
)
230 struct veth_net_stats __percpu
*stats
;
231 struct veth_priv
*priv
;
233 stats
= alloc_percpu(struct veth_net_stats
);
237 priv
= netdev_priv(dev
);
242 static void veth_dev_free(struct net_device
*dev
)
244 struct veth_priv
*priv
;
246 priv
= netdev_priv(dev
);
247 free_percpu(priv
->stats
);
251 static const struct net_device_ops veth_netdev_ops
= {
252 .ndo_init
= veth_dev_init
,
253 .ndo_open
= veth_open
,
254 .ndo_stop
= veth_close
,
255 .ndo_start_xmit
= veth_xmit
,
256 .ndo_change_mtu
= veth_change_mtu
,
257 .ndo_get_stats
= veth_get_stats
,
258 .ndo_set_mac_address
= eth_mac_addr
,
261 static void veth_setup(struct net_device
*dev
)
265 dev
->netdev_ops
= &veth_netdev_ops
;
266 dev
->ethtool_ops
= &veth_ethtool_ops
;
267 dev
->features
|= NETIF_F_LLTX
;
268 dev
->destructor
= veth_dev_free
;
270 dev
->hw_features
= NETIF_F_NO_CSUM
| NETIF_F_SG
| NETIF_F_RXCSUM
;
277 static int veth_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
279 if (tb
[IFLA_ADDRESS
]) {
280 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
282 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
283 return -EADDRNOTAVAIL
;
286 if (!is_valid_veth_mtu(nla_get_u32(tb
[IFLA_MTU
])))
292 static struct rtnl_link_ops veth_link_ops
;
294 static int veth_newlink(struct net
*src_net
, struct net_device
*dev
,
295 struct nlattr
*tb
[], struct nlattr
*data
[])
298 struct net_device
*peer
;
299 struct veth_priv
*priv
;
300 char ifname
[IFNAMSIZ
];
301 struct nlattr
*peer_tb
[IFLA_MAX
+ 1], **tbp
;
302 struct ifinfomsg
*ifmp
;
306 * create and register peer first
308 if (data
!= NULL
&& data
[VETH_INFO_PEER
] != NULL
) {
309 struct nlattr
*nla_peer
;
311 nla_peer
= data
[VETH_INFO_PEER
];
312 ifmp
= nla_data(nla_peer
);
313 err
= nla_parse(peer_tb
, IFLA_MAX
,
314 nla_data(nla_peer
) + sizeof(struct ifinfomsg
),
315 nla_len(nla_peer
) - sizeof(struct ifinfomsg
),
320 err
= veth_validate(peer_tb
, NULL
);
330 if (tbp
[IFLA_IFNAME
])
331 nla_strlcpy(ifname
, tbp
[IFLA_IFNAME
], IFNAMSIZ
);
333 snprintf(ifname
, IFNAMSIZ
, DRV_NAME
"%%d");
335 net
= rtnl_link_get_net(src_net
, tbp
);
339 peer
= rtnl_create_link(src_net
, net
, ifname
, &veth_link_ops
, tbp
);
342 return PTR_ERR(peer
);
345 if (tbp
[IFLA_ADDRESS
] == NULL
)
346 random_ether_addr(peer
->dev_addr
);
348 err
= register_netdevice(peer
);
352 goto err_register_peer
;
354 netif_carrier_off(peer
);
356 err
= rtnl_configure_link(peer
, ifmp
);
358 goto err_configure_peer
;
363 * note, that since we've registered new device the dev's name
364 * should be re-allocated
367 if (tb
[IFLA_ADDRESS
] == NULL
)
368 random_ether_addr(dev
->dev_addr
);
371 nla_strlcpy(dev
->name
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
373 snprintf(dev
->name
, IFNAMSIZ
, DRV_NAME
"%%d");
375 if (strchr(dev
->name
, '%')) {
376 err
= dev_alloc_name(dev
, dev
->name
);
381 err
= register_netdevice(dev
);
383 goto err_register_dev
;
385 netif_carrier_off(dev
);
388 * tie the deviced together
391 priv
= netdev_priv(dev
);
394 priv
= netdev_priv(peer
);
402 unregister_netdevice(peer
);
410 static void veth_dellink(struct net_device
*dev
, struct list_head
*head
)
412 struct veth_priv
*priv
;
413 struct net_device
*peer
;
415 priv
= netdev_priv(dev
);
418 unregister_netdevice_queue(dev
, head
);
419 unregister_netdevice_queue(peer
, head
);
422 static const struct nla_policy veth_policy
[VETH_INFO_MAX
+ 1];
424 static struct rtnl_link_ops veth_link_ops
= {
426 .priv_size
= sizeof(struct veth_priv
),
428 .validate
= veth_validate
,
429 .newlink
= veth_newlink
,
430 .dellink
= veth_dellink
,
431 .policy
= veth_policy
,
432 .maxtype
= VETH_INFO_MAX
,
439 static __init
int veth_init(void)
441 return rtnl_link_register(&veth_link_ops
);
444 static __exit
void veth_exit(void)
446 rtnl_link_unregister(&veth_link_ops
);
449 module_init(veth_init
);
450 module_exit(veth_exit
);
452 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
453 MODULE_LICENSE("GPL v2");
454 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);