1
2
3
4
5
6
7
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
27#define MAX_MTU 65535
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 unsigned requested_headroom;
39};
40
41
42
43
44
45static struct {
46 const char string[ETH_GSTRING_LEN];
47} ethtool_stats_keys[] = {
48 { "peer_ifindex" },
49};
50
51static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
52{
53 cmd->supported = 0;
54 cmd->advertising = 0;
55 ethtool_cmd_speed_set(cmd, SPEED_10000);
56 cmd->duplex = DUPLEX_FULL;
57 cmd->port = PORT_TP;
58 cmd->phy_address = 0;
59 cmd->transceiver = XCVR_INTERNAL;
60 cmd->autoneg = AUTONEG_DISABLE;
61 cmd->maxtxpkt = 0;
62 cmd->maxrxpkt = 0;
63 return 0;
64}
65
66static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
67{
68 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
69 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
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, ðtool_stats_keys, sizeof(ethtool_stats_keys));
77 break;
78 }
79}
80
81static 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
91static void veth_get_ethtool_stats(struct net_device *dev,
92 struct ethtool_stats *stats, u64 *data)
93{
94 struct veth_priv *priv = netdev_priv(dev);
95 struct net_device *peer = rtnl_dereference(priv->peer);
96
97 data[0] = peer ? peer->ifindex : 0;
98}
99
100static 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
109static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
110{
111 struct veth_priv *priv = netdev_priv(dev);
112 struct net_device *rcv;
113 int length = skb->len;
114
115 rcu_read_lock();
116 rcv = rcu_dereference(priv->peer);
117 if (unlikely(!rcv)) {
118 kfree_skb(skb);
119 goto drop;
120 }
121
122 if (likely(dev_forward_skb(rcv, skb) == NET_RX_SUCCESS)) {
123 struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
124
125 u64_stats_update_begin(&stats->syncp);
126 stats->bytes += length;
127 stats->packets++;
128 u64_stats_update_end(&stats->syncp);
129 } else {
130drop:
131 atomic64_inc(&priv->dropped);
132 }
133 rcu_read_unlock();
134 return NETDEV_TX_OK;
135}
136
137
138
139
140
141static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
142{
143 struct veth_priv *priv = netdev_priv(dev);
144 int cpu;
145
146 result->packets = 0;
147 result->bytes = 0;
148 for_each_possible_cpu(cpu) {
149 struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
150 u64 packets, bytes;
151 unsigned int start;
152
153 do {
154 start = u64_stats_fetch_begin_irq(&stats->syncp);
155 packets = stats->packets;
156 bytes = stats->bytes;
157 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
158 result->packets += packets;
159 result->bytes += bytes;
160 }
161 return atomic64_read(&priv->dropped);
162}
163
164static struct rtnl_link_stats64 *veth_get_stats64(struct net_device *dev,
165 struct rtnl_link_stats64 *tot)
166{
167 struct veth_priv *priv = netdev_priv(dev);
168 struct net_device *peer;
169 struct pcpu_vstats one;
170
171 tot->tx_dropped = veth_stats_one(&one, dev);
172 tot->tx_bytes = one.bytes;
173 tot->tx_packets = one.packets;
174
175 rcu_read_lock();
176 peer = rcu_dereference(priv->peer);
177 if (peer) {
178 tot->rx_dropped = veth_stats_one(&one, peer);
179 tot->rx_bytes = one.bytes;
180 tot->rx_packets = one.packets;
181 }
182 rcu_read_unlock();
183
184 return tot;
185}
186
187static int veth_open(struct net_device *dev)
188{
189 struct veth_priv *priv = netdev_priv(dev);
190 struct net_device *peer = rtnl_dereference(priv->peer);
191
192 if (!peer)
193 return -ENOTCONN;
194
195 if (peer->flags & IFF_UP) {
196 netif_carrier_on(dev);
197 netif_carrier_on(peer);
198 }
199 return 0;
200}
201
202static int veth_close(struct net_device *dev)
203{
204 struct veth_priv *priv = netdev_priv(dev);
205 struct net_device *peer = rtnl_dereference(priv->peer);
206
207 netif_carrier_off(dev);
208 if (peer)
209 netif_carrier_off(peer);
210
211 return 0;
212}
213
214static int is_valid_veth_mtu(int new_mtu)
215{
216 return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
217}
218
219static int veth_change_mtu(struct net_device *dev, int new_mtu)
220{
221 if (!is_valid_veth_mtu(new_mtu))
222 return -EINVAL;
223 dev->mtu = new_mtu;
224 return 0;
225}
226
227static int veth_dev_init(struct net_device *dev)
228{
229 dev->vstats = alloc_percpu(struct pcpu_vstats);
230 if (!dev->vstats)
231 return -ENOMEM;
232
233 return 0;
234}
235
236static void veth_dev_free(struct net_device *dev)
237{
238 free_percpu(dev->vstats);
239 free_netdev(dev);
240}
241
242static int veth_get_iflink(const struct net_device *dev)
243{
244 struct veth_priv *priv = netdev_priv(dev);
245 struct net_device *peer;
246 int iflink;
247
248 rcu_read_lock();
249 peer = rcu_dereference(priv->peer);
250 iflink = peer ? peer->ifindex : 0;
251 rcu_read_unlock();
252
253 return iflink;
254}
255
256static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
257{
258 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
259 struct net_device *peer;
260
261 if (new_hr < 0)
262 new_hr = 0;
263
264 rcu_read_lock();
265 peer = rcu_dereference(priv->peer);
266 if (unlikely(!peer))
267 goto out;
268
269 peer_priv = netdev_priv(peer);
270 priv->requested_headroom = new_hr;
271 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
272 dev->needed_headroom = new_hr;
273 peer->needed_headroom = new_hr;
274
275out:
276 rcu_read_unlock();
277}
278
279static const struct net_device_ops veth_netdev_ops = {
280 .ndo_init = veth_dev_init,
281 .ndo_open = veth_open,
282 .ndo_stop = veth_close,
283 .ndo_start_xmit = veth_xmit,
284 .ndo_change_mtu = veth_change_mtu,
285 .ndo_get_stats64 = veth_get_stats64,
286 .ndo_set_mac_address = eth_mac_addr,
287 .ndo_get_iflink = veth_get_iflink,
288 .ndo_size = sizeof(struct net_device_ops),
289 .extended.ndo_set_rx_headroom = veth_set_rx_headroom,
290};
291
292#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
293 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
294 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
295 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
296 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
297
298static void veth_setup(struct net_device *dev)
299{
300 ether_setup(dev);
301
302 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
303 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
304 dev->priv_flags |= IFF_NO_QUEUE;
305 dev->priv_flags |= IFF_PHONY_HEADROOM;
306
307 dev->netdev_ops = &veth_netdev_ops;
308 dev->ethtool_ops = &veth_ethtool_ops;
309 dev->features |= NETIF_F_LLTX;
310 dev->features |= VETH_FEATURES;
311 dev->vlan_features = dev->features &
312 ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX);
313 dev->destructor = veth_dev_free;
314
315 dev->hw_features = VETH_FEATURES;
316 dev->hw_enc_features = VETH_FEATURES;
317}
318
319
320
321
322
323static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
324{
325 if (tb[IFLA_ADDRESS]) {
326 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
327 return -EINVAL;
328 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
329 return -EADDRNOTAVAIL;
330 }
331 if (tb[IFLA_MTU]) {
332 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
333 return -EINVAL;
334 }
335 return 0;
336}
337
338static struct rtnl_link_ops veth_link_ops;
339
340static int veth_newlink(struct net *src_net, struct net_device *dev,
341 struct nlattr *tb[], struct nlattr *data[])
342{
343 int err;
344 struct net_device *peer;
345 struct veth_priv *priv;
346 char ifname[IFNAMSIZ];
347 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
348 struct ifinfomsg *ifmp;
349 struct net *net;
350
351
352
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 else
378 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
379
380 net = rtnl_link_get_net(src_net, tbp);
381 if (IS_ERR(net))
382 return PTR_ERR(net);
383
384 peer = rtnl_create_link(net, ifname, &veth_link_ops, tbp);
385 if (IS_ERR(peer)) {
386 put_net(net);
387 return PTR_ERR(peer);
388 }
389
390 if (tbp[IFLA_ADDRESS] == NULL)
391 eth_hw_addr_random(peer);
392
393 if (ifmp && (dev->ifindex != 0))
394 peer->ifindex = ifmp->ifi_index;
395
396 err = register_netdevice(peer);
397 put_net(net);
398 net = NULL;
399 if (err < 0)
400 goto err_register_peer;
401
402 netif_carrier_off(peer);
403
404 err = rtnl_configure_link(peer, ifmp);
405 if (err < 0)
406 goto err_configure_peer;
407
408
409
410
411
412
413
414
415 if (tb[IFLA_ADDRESS] == NULL)
416 eth_hw_addr_random(dev);
417
418 if (tb[IFLA_IFNAME])
419 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
420 else
421 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
422
423 if (strchr(dev->name, '%')) {
424 err = dev_alloc_name(dev, dev->name);
425 if (err < 0)
426 goto err_alloc_name;
427 }
428
429 err = register_netdevice(dev);
430 if (err < 0)
431 goto err_register_dev;
432
433 netif_carrier_off(dev);
434
435
436
437
438
439 priv = netdev_priv(dev);
440 rcu_assign_pointer(priv->peer, peer);
441
442 priv = netdev_priv(peer);
443 rcu_assign_pointer(priv->peer, dev);
444 return 0;
445
446err_register_dev:
447
448err_alloc_name:
449err_configure_peer:
450 unregister_netdevice(peer);
451 return err;
452
453err_register_peer:
454 free_netdev(peer);
455 return err;
456}
457
458static void veth_dellink(struct net_device *dev, struct list_head *head)
459{
460 struct veth_priv *priv;
461 struct net_device *peer;
462
463 priv = netdev_priv(dev);
464 peer = rtnl_dereference(priv->peer);
465
466
467
468
469
470 RCU_INIT_POINTER(priv->peer, NULL);
471 unregister_netdevice_queue(dev, head);
472
473 if (peer) {
474 priv = netdev_priv(peer);
475 RCU_INIT_POINTER(priv->peer, NULL);
476 unregister_netdevice_queue(peer, head);
477 }
478}
479
480static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
481 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
482};
483
484static struct net *veth_get_link_net(const struct net_device *dev)
485{
486 struct veth_priv *priv = netdev_priv(dev);
487 struct net_device *peer = rtnl_dereference(priv->peer);
488
489 return peer ? dev_net(peer) : dev_net(dev);
490}
491
492static struct rtnl_link_ops veth_link_ops = {
493 .kind = DRV_NAME,
494 .priv_size = sizeof(struct veth_priv),
495 .setup = veth_setup,
496 .validate = veth_validate,
497 .newlink = veth_newlink,
498 .dellink = veth_dellink,
499 .policy = veth_policy,
500 .maxtype = VETH_INFO_MAX,
501 .get_link_net = veth_get_link_net,
502};
503
504
505
506
507
508static __init int veth_init(void)
509{
510 return rtnl_link_register(&veth_link_ops);
511}
512
513static __exit void veth_exit(void)
514{
515 rtnl_link_unregister(&veth_link_ops);
516}
517
518module_init(veth_init);
519module_exit(veth_exit);
520
521MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
522MODULE_LICENSE("GPL v2");
523MODULE_ALIAS_RTNL_LINK(DRV_NAME);
524