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 void 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
185
186static void veth_set_multicast_list(struct net_device *dev)
187{
188}
189
190static int veth_open(struct net_device *dev)
191{
192 struct veth_priv *priv = netdev_priv(dev);
193 struct net_device *peer = rtnl_dereference(priv->peer);
194
195 if (!peer)
196 return -ENOTCONN;
197
198 if (peer->flags & IFF_UP) {
199 netif_carrier_on(dev);
200 netif_carrier_on(peer);
201 }
202 return 0;
203}
204
205static int veth_close(struct net_device *dev)
206{
207 struct veth_priv *priv = netdev_priv(dev);
208 struct net_device *peer = rtnl_dereference(priv->peer);
209
210 netif_carrier_off(dev);
211 if (peer)
212 netif_carrier_off(peer);
213
214 return 0;
215}
216
217static int is_valid_veth_mtu(int new_mtu)
218{
219 return new_mtu >= MIN_MTU && new_mtu <= MAX_MTU;
220}
221
222static int veth_change_mtu(struct net_device *dev, int new_mtu)
223{
224 if (!is_valid_veth_mtu(new_mtu))
225 return -EINVAL;
226 dev->mtu = new_mtu;
227 return 0;
228}
229
230static int veth_dev_init(struct net_device *dev)
231{
232 dev->vstats = alloc_percpu(struct pcpu_vstats);
233 if (!dev->vstats)
234 return -ENOMEM;
235
236 return 0;
237}
238
239static void veth_dev_free(struct net_device *dev)
240{
241 free_percpu(dev->vstats);
242}
243
244static int veth_get_iflink(const struct net_device *dev)
245{
246 struct veth_priv *priv = netdev_priv(dev);
247 struct net_device *peer;
248 int iflink;
249
250 rcu_read_lock();
251 peer = rcu_dereference(priv->peer);
252 iflink = peer ? peer->ifindex : 0;
253 rcu_read_unlock();
254
255 return iflink;
256}
257
258static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
259{
260 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
261 struct net_device *peer;
262
263 if (new_hr < 0)
264 new_hr = 0;
265
266 rcu_read_lock();
267 peer = rcu_dereference(priv->peer);
268 if (unlikely(!peer))
269 goto out;
270
271 peer_priv = netdev_priv(peer);
272 priv->requested_headroom = new_hr;
273 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
274 dev->needed_headroom = new_hr;
275 peer->needed_headroom = new_hr;
276
277out:
278 rcu_read_unlock();
279}
280
281static const struct net_device_ops veth_netdev_ops = {
282 .ndo_init = veth_dev_init,
283 .ndo_open = veth_open,
284 .ndo_stop = veth_close,
285 .ndo_start_xmit = veth_xmit,
286 .ndo_change_mtu_rh74 = veth_change_mtu,
287 .ndo_get_stats64 = veth_get_stats64,
288 .ndo_set_rx_mode = veth_set_multicast_list,
289 .ndo_set_mac_address = eth_mac_addr,
290 .ndo_get_iflink = veth_get_iflink,
291 .ndo_size = sizeof(struct net_device_ops),
292 .extended.ndo_set_rx_headroom = veth_set_rx_headroom,
293};
294
295#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
296 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
297 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
298 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
299 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
300
301static void veth_setup(struct net_device *dev)
302{
303 ether_setup(dev);
304
305 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
306 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
307 dev->priv_flags |= IFF_NO_QUEUE;
308 dev->priv_flags |= IFF_PHONY_HEADROOM;
309
310 dev->netdev_ops = &veth_netdev_ops;
311 dev->ethtool_ops = &veth_ethtool_ops;
312 dev->features |= NETIF_F_LLTX;
313 dev->features |= VETH_FEATURES;
314 dev->vlan_features = dev->features &
315 ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX);
316 dev->extended->needs_free_netdev = true;
317 dev->extended->priv_destructor = veth_dev_free;
318
319 dev->hw_features = VETH_FEATURES;
320 dev->hw_enc_features = VETH_FEATURES;
321 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
322}
323
324
325
326
327
328static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
329{
330 if (tb[IFLA_ADDRESS]) {
331 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
332 return -EINVAL;
333 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
334 return -EADDRNOTAVAIL;
335 }
336 if (tb[IFLA_MTU]) {
337 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
338 return -EINVAL;
339 }
340 return 0;
341}
342
343static struct rtnl_link_ops veth_link_ops;
344
345static int veth_newlink(struct net *src_net, struct net_device *dev,
346 struct nlattr *tb[], struct nlattr *data[])
347{
348 int err;
349 struct net_device *peer;
350 struct veth_priv *priv;
351 char ifname[IFNAMSIZ];
352 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
353 struct ifinfomsg *ifmp;
354 struct net *net;
355
356
357
358
359 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
360 struct nlattr *nla_peer;
361
362 nla_peer = data[VETH_INFO_PEER];
363 ifmp = nla_data(nla_peer);
364 err = rtnl_nla_parse_ifla(peer_tb,
365 nla_data(nla_peer) + sizeof(struct ifinfomsg),
366 nla_len(nla_peer) - sizeof(struct ifinfomsg));
367 if (err < 0)
368 return err;
369
370 err = veth_validate(peer_tb, NULL);
371 if (err < 0)
372 return err;
373
374 tbp = peer_tb;
375 } else {
376 ifmp = NULL;
377 tbp = tb;
378 }
379
380 if (ifmp && tbp[IFLA_IFNAME])
381 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
382 else
383 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
384
385 net = rtnl_link_get_net(src_net, tbp);
386 if (IS_ERR(net))
387 return PTR_ERR(net);
388
389 peer = rtnl_create_link(net, ifname, &veth_link_ops, tbp);
390 if (IS_ERR(peer)) {
391 put_net(net);
392 return PTR_ERR(peer);
393 }
394
395 if (!ifmp || !tbp[IFLA_ADDRESS])
396 eth_hw_addr_random(peer);
397
398 if (ifmp && (dev->ifindex != 0))
399 peer->ifindex = ifmp->ifi_index;
400
401 err = register_netdevice(peer);
402 put_net(net);
403 net = NULL;
404 if (err < 0)
405 goto err_register_peer;
406
407 netif_carrier_off(peer);
408
409 err = rtnl_configure_link(peer, ifmp);
410 if (err < 0)
411 goto err_configure_peer;
412
413
414
415
416
417
418
419
420 if (tb[IFLA_ADDRESS] == NULL)
421 eth_hw_addr_random(dev);
422
423 if (tb[IFLA_IFNAME])
424 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
425 else
426 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
427
428 if (strchr(dev->name, '%')) {
429 err = dev_alloc_name(dev, dev->name);
430 if (err < 0)
431 goto err_alloc_name;
432 }
433
434 err = register_netdevice(dev);
435 if (err < 0)
436 goto err_register_dev;
437
438 netif_carrier_off(dev);
439
440
441
442
443
444 priv = netdev_priv(dev);
445 rcu_assign_pointer(priv->peer, peer);
446
447 priv = netdev_priv(peer);
448 rcu_assign_pointer(priv->peer, dev);
449 return 0;
450
451err_register_dev:
452
453err_alloc_name:
454err_configure_peer:
455 unregister_netdevice(peer);
456 return err;
457
458err_register_peer:
459 free_netdev(peer);
460 return err;
461}
462
463static void veth_dellink(struct net_device *dev, struct list_head *head)
464{
465 struct veth_priv *priv;
466 struct net_device *peer;
467
468 priv = netdev_priv(dev);
469 peer = rtnl_dereference(priv->peer);
470
471
472
473
474
475 RCU_INIT_POINTER(priv->peer, NULL);
476 unregister_netdevice_queue(dev, head);
477
478 if (peer) {
479 priv = netdev_priv(peer);
480 RCU_INIT_POINTER(priv->peer, NULL);
481 unregister_netdevice_queue(peer, head);
482 }
483}
484
485static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
486 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
487};
488
489static struct net *veth_get_link_net(const struct net_device *dev)
490{
491 struct veth_priv *priv = netdev_priv(dev);
492 struct net_device *peer = rtnl_dereference(priv->peer);
493
494 return peer ? dev_net(peer) : dev_net(dev);
495}
496
497static struct rtnl_link_ops veth_link_ops = {
498 .kind = DRV_NAME,
499 .priv_size = sizeof(struct veth_priv),
500 .setup = veth_setup,
501 .validate = veth_validate,
502 .newlink = veth_newlink,
503 .dellink = veth_dellink,
504 .policy = veth_policy,
505 .maxtype = VETH_INFO_MAX,
506 .get_link_net = veth_get_link_net,
507};
508
509
510
511
512
513static __init int veth_init(void)
514{
515 return rtnl_link_register(&veth_link_ops);
516}
517
518static __exit void veth_exit(void)
519{
520 rtnl_link_unregister(&veth_link_ops);
521}
522
523module_init(veth_init);
524module_exit(veth_exit);
525
526MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
527MODULE_LICENSE("GPL v2");
528MODULE_ALIAS_RTNL_LINK(DRV_NAME);
529