blob: 9b0a7b9ab1b8f160a46b93eddd21d859e1cdfd66 [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>
Eric Dumazetcf05c702011-06-19 22:48:34 -070015#include <linux/u64_stats_sync.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070016
Jiri Pirkof7b12602014-02-18 20:53:18 +010017#include <net/rtnetlink.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070018#include <net/dst.h>
19#include <net/xfrm.h>
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +090020#include <net/xdp.h>
Stephen Hemmingerecef9692007-12-25 17:23:59 -080021#include <linux/veth.h>
Paul Gortmaker9d9779e2011-07-03 15:21:01 -040022#include <linux/module.h>
Toshiaki Makita948d4f22018-08-03 16:58:10 +090023#include <linux/bpf.h>
24#include <linux/filter.h>
25#include <linux/ptr_ring.h>
Toshiaki Makita948d4f22018-08-03 16:58:10 +090026#include <linux/bpf_trace.h>
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070027
28#define DRV_NAME "veth"
29#define DRV_VERSION "1.0"
30
Toshiaki Makita9fc8d512018-08-03 16:58:13 +090031#define VETH_XDP_FLAG BIT(0)
Toshiaki Makita948d4f22018-08-03 16:58:10 +090032#define VETH_RING_SIZE 256
33#define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
34
Toshiaki Makitad1396002018-08-03 16:58:17 +090035/* Separating two types of XDP xmit */
36#define VETH_XDP_TX BIT(0)
37#define VETH_XDP_REDIR BIT(1)
38
Eric Dumazet26811282012-12-29 16:02:43 +000039struct pcpu_vstats {
40 u64 packets;
41 u64 bytes;
Eric Dumazetcf05c702011-06-19 22:48:34 -070042 struct u64_stats_sync syncp;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070043};
44
45struct veth_priv {
Toshiaki Makita948d4f22018-08-03 16:58:10 +090046 struct napi_struct xdp_napi;
47 struct net_device *dev;
48 struct bpf_prog __rcu *xdp_prog;
49 struct bpf_prog *_xdp_prog;
Eric Dumazetd0e2c552013-01-04 15:42:40 +000050 struct net_device __rcu *peer;
Eric Dumazet26811282012-12-29 16:02:43 +000051 atomic64_t dropped;
Toshiaki Makitad1396002018-08-03 16:58:17 +090052 struct xdp_mem_info xdp_mem;
Paolo Abeni163e5292016-02-26 10:45:41 +010053 unsigned requested_headroom;
Toshiaki Makita948d4f22018-08-03 16:58:10 +090054 bool rx_notify_masked;
55 struct ptr_ring xdp_ring;
56 struct xdp_rxq_info xdp_rxq;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070057};
58
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070059/*
60 * ethtool interface
61 */
62
63static struct {
64 const char string[ETH_GSTRING_LEN];
65} ethtool_stats_keys[] = {
66 { "peer_ifindex" },
67};
68
Philippe Reynes56607b92017-03-29 08:24:21 +020069static int veth_get_link_ksettings(struct net_device *dev,
70 struct ethtool_link_ksettings *cmd)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070071{
Philippe Reynes56607b92017-03-29 08:24:21 +020072 cmd->base.speed = SPEED_10000;
73 cmd->base.duplex = DUPLEX_FULL;
74 cmd->base.port = PORT_TP;
75 cmd->base.autoneg = AUTONEG_DISABLE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070076 return 0;
77}
78
79static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
80{
Rick Jones33a5ba12011-11-15 14:59:53 +000081 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
82 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070083}
84
85static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
86{
87 switch(stringset) {
88 case ETH_SS_STATS:
89 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
90 break;
91 }
92}
93
Jeff Garzikb9f2c042007-10-03 18:07:32 -070094static int veth_get_sset_count(struct net_device *dev, int sset)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -070095{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070096 switch (sset) {
97 case ETH_SS_STATS:
98 return ARRAY_SIZE(ethtool_stats_keys);
99 default:
100 return -EOPNOTSUPP;
101 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700102}
103
104static void veth_get_ethtool_stats(struct net_device *dev,
105 struct ethtool_stats *stats, u64 *data)
106{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000107 struct veth_priv *priv = netdev_priv(dev);
108 struct net_device *peer = rtnl_dereference(priv->peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700109
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000110 data[0] = peer ? peer->ifindex : 0;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700111}
112
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700113static const struct ethtool_ops veth_ethtool_ops = {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700114 .get_drvinfo = veth_get_drvinfo,
115 .get_link = ethtool_op_get_link,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700116 .get_strings = veth_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700117 .get_sset_count = veth_get_sset_count,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700118 .get_ethtool_stats = veth_get_ethtool_stats,
Philippe Reynes56607b92017-03-29 08:24:21 +0200119 .get_link_ksettings = veth_get_link_ksettings,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700120};
121
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900122/* general routines */
123
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900124static bool veth_is_xdp_frame(void *ptr)
125{
126 return (unsigned long)ptr & VETH_XDP_FLAG;
127}
128
129static void *veth_ptr_to_xdp(void *ptr)
130{
131 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
132}
133
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900134static void *veth_xdp_to_ptr(void *ptr)
135{
136 return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
137}
138
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900139static void veth_ptr_free(void *ptr)
140{
141 if (veth_is_xdp_frame(ptr))
142 xdp_return_frame(veth_ptr_to_xdp(ptr));
143 else
144 kfree_skb(ptr);
145}
146
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900147static void __veth_xdp_flush(struct veth_priv *priv)
148{
149 /* Write ptr_ring before reading rx_notify_masked */
150 smp_mb();
151 if (!priv->rx_notify_masked) {
152 priv->rx_notify_masked = true;
153 napi_schedule(&priv->xdp_napi);
154 }
155}
156
157static int veth_xdp_rx(struct veth_priv *priv, struct sk_buff *skb)
158{
159 if (unlikely(ptr_ring_produce(&priv->xdp_ring, skb))) {
160 dev_kfree_skb_any(skb);
161 return NET_RX_DROP;
162 }
163
164 return NET_RX_SUCCESS;
165}
166
167static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb, bool xdp)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700168{
Eric Dumazet26811282012-12-29 16:02:43 +0000169 struct veth_priv *priv = netdev_priv(dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900170
171 return __dev_forward_skb(dev, skb) ?: xdp ?
172 veth_xdp_rx(priv, skb) :
173 netif_rx(skb);
174}
175
176static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
177{
178 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000179 struct net_device *rcv;
Eric Dumazet26811282012-12-29 16:02:43 +0000180 int length = skb->len;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900181 bool rcv_xdp = false;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700182
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000183 rcu_read_lock();
184 rcv = rcu_dereference(priv->peer);
185 if (unlikely(!rcv)) {
186 kfree_skb(skb);
187 goto drop;
188 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700189
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900190 rcv_priv = netdev_priv(rcv);
191 rcv_xdp = rcu_access_pointer(rcv_priv->xdp_prog);
192
193 if (likely(veth_forward_skb(rcv, skb, rcv_xdp) == NET_RX_SUCCESS)) {
Eric Dumazet26811282012-12-29 16:02:43 +0000194 struct pcpu_vstats *stats = this_cpu_ptr(dev->vstats);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700195
Eric Dumazet26811282012-12-29 16:02:43 +0000196 u64_stats_update_begin(&stats->syncp);
197 stats->bytes += length;
198 stats->packets++;
199 u64_stats_update_end(&stats->syncp);
200 } else {
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000201drop:
Eric Dumazet26811282012-12-29 16:02:43 +0000202 atomic64_inc(&priv->dropped);
203 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900204
205 if (rcv_xdp)
206 __veth_xdp_flush(rcv_priv);
207
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000208 rcu_read_unlock();
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900209
Patrick McHardy6ed10652009-06-23 06:03:08 +0000210 return NETDEV_TX_OK;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700211}
212
Eric Dumazet26811282012-12-29 16:02:43 +0000213static u64 veth_stats_one(struct pcpu_vstats *result, struct net_device *dev)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700214{
Eric Dumazetcf05c702011-06-19 22:48:34 -0700215 struct veth_priv *priv = netdev_priv(dev);
David S. Miller11687a12009-06-25 02:45:42 -0700216 int cpu;
David S. Miller11687a12009-06-25 02:45:42 -0700217
Eric Dumazet26811282012-12-29 16:02:43 +0000218 result->packets = 0;
219 result->bytes = 0;
Eric Dumazet2b1c8b02009-11-18 07:09:39 +0000220 for_each_possible_cpu(cpu) {
Eric Dumazet26811282012-12-29 16:02:43 +0000221 struct pcpu_vstats *stats = per_cpu_ptr(dev->vstats, cpu);
222 u64 packets, bytes;
Eric Dumazetcf05c702011-06-19 22:48:34 -0700223 unsigned int start;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700224
Eric Dumazetcf05c702011-06-19 22:48:34 -0700225 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700226 start = u64_stats_fetch_begin_irq(&stats->syncp);
Eric Dumazet26811282012-12-29 16:02:43 +0000227 packets = stats->packets;
228 bytes = stats->bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700229 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
Eric Dumazet26811282012-12-29 16:02:43 +0000230 result->packets += packets;
231 result->bytes += bytes;
David S. Miller11687a12009-06-25 02:45:42 -0700232 }
Eric Dumazet26811282012-12-29 16:02:43 +0000233 return atomic64_read(&priv->dropped);
234}
235
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800236static void veth_get_stats64(struct net_device *dev,
237 struct rtnl_link_stats64 *tot)
Eric Dumazet26811282012-12-29 16:02:43 +0000238{
239 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000240 struct net_device *peer;
Eric Dumazet26811282012-12-29 16:02:43 +0000241 struct pcpu_vstats one;
242
243 tot->tx_dropped = veth_stats_one(&one, dev);
244 tot->tx_bytes = one.bytes;
245 tot->tx_packets = one.packets;
246
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000247 rcu_read_lock();
248 peer = rcu_dereference(priv->peer);
249 if (peer) {
250 tot->rx_dropped = veth_stats_one(&one, peer);
251 tot->rx_bytes = one.bytes;
252 tot->rx_packets = one.packets;
253 }
254 rcu_read_unlock();
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700255}
256
Gao feng5c70ef82013-10-04 16:52:24 +0800257/* fake multicast ability */
258static void veth_set_multicast_list(struct net_device *dev)
259{
260}
261
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900262static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
263 int buflen)
264{
265 struct sk_buff *skb;
266
267 if (!buflen) {
268 buflen = SKB_DATA_ALIGN(headroom + len) +
269 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
270 }
271 skb = build_skb(head, buflen);
272 if (!skb)
273 return NULL;
274
275 skb_reserve(skb, headroom);
276 skb_put(skb, len);
277
278 return skb;
279}
280
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900281static int veth_xdp_xmit(struct net_device *dev, int n,
282 struct xdp_frame **frames, u32 flags)
283{
284 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
285 struct net_device *rcv;
286 unsigned int max_len;
287 int i, drops = 0;
288
289 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
290 return -EINVAL;
291
292 rcv = rcu_dereference(priv->peer);
293 if (unlikely(!rcv))
294 return -ENXIO;
295
296 rcv_priv = netdev_priv(rcv);
297 /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
298 * side. This means an XDP program is loaded on the peer and the peer
299 * device is up.
300 */
301 if (!rcu_access_pointer(rcv_priv->xdp_prog))
302 return -ENXIO;
303
304 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
305
306 spin_lock(&rcv_priv->xdp_ring.producer_lock);
307 for (i = 0; i < n; i++) {
308 struct xdp_frame *frame = frames[i];
309 void *ptr = veth_xdp_to_ptr(frame);
310
311 if (unlikely(frame->len > max_len ||
312 __ptr_ring_produce(&rcv_priv->xdp_ring, ptr))) {
313 xdp_return_frame_rx_napi(frame);
314 drops++;
315 }
316 }
317 spin_unlock(&rcv_priv->xdp_ring.producer_lock);
318
319 if (flags & XDP_XMIT_FLUSH)
320 __veth_xdp_flush(rcv_priv);
321
322 return n - drops;
323}
324
Toshiaki Makitad1396002018-08-03 16:58:17 +0900325static void veth_xdp_flush(struct net_device *dev)
326{
327 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
328 struct net_device *rcv;
329
330 rcu_read_lock();
331 rcv = rcu_dereference(priv->peer);
332 if (unlikely(!rcv))
333 goto out;
334
335 rcv_priv = netdev_priv(rcv);
336 /* xdp_ring is initialized on receive side? */
337 if (unlikely(!rcu_access_pointer(rcv_priv->xdp_prog)))
338 goto out;
339
340 __veth_xdp_flush(rcv_priv);
341out:
342 rcu_read_unlock();
343}
344
345static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
346{
347 struct xdp_frame *frame = convert_to_xdp_frame(xdp);
348
349 if (unlikely(!frame))
350 return -EOVERFLOW;
351
352 return veth_xdp_xmit(dev, 1, &frame, 0);
353}
354
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900355static struct sk_buff *veth_xdp_rcv_one(struct veth_priv *priv,
Toshiaki Makitad1396002018-08-03 16:58:17 +0900356 struct xdp_frame *frame,
357 unsigned int *xdp_xmit)
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900358{
359 void *hard_start = frame->data - frame->headroom;
360 void *head = hard_start - sizeof(struct xdp_frame);
361 int len = frame->len, delta = 0;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900362 struct xdp_frame orig_frame;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900363 struct bpf_prog *xdp_prog;
364 unsigned int headroom;
365 struct sk_buff *skb;
366
367 rcu_read_lock();
368 xdp_prog = rcu_dereference(priv->xdp_prog);
369 if (likely(xdp_prog)) {
370 struct xdp_buff xdp;
371 u32 act;
372
373 xdp.data_hard_start = hard_start;
374 xdp.data = frame->data;
375 xdp.data_end = frame->data + frame->len;
376 xdp.data_meta = frame->data - frame->metasize;
377 xdp.rxq = &priv->xdp_rxq;
378
379 act = bpf_prog_run_xdp(xdp_prog, &xdp);
380
381 switch (act) {
382 case XDP_PASS:
383 delta = frame->data - xdp.data;
384 len = xdp.data_end - xdp.data;
385 break;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900386 case XDP_TX:
387 orig_frame = *frame;
388 xdp.data_hard_start = head;
389 xdp.rxq->mem = frame->mem;
390 if (unlikely(veth_xdp_tx(priv->dev, &xdp) < 0)) {
391 trace_xdp_exception(priv->dev, xdp_prog, act);
392 frame = &orig_frame;
393 goto err_xdp;
394 }
395 *xdp_xmit |= VETH_XDP_TX;
396 rcu_read_unlock();
397 goto xdp_xmit;
398 case XDP_REDIRECT:
399 orig_frame = *frame;
400 xdp.data_hard_start = head;
401 xdp.rxq->mem = frame->mem;
402 if (xdp_do_redirect(priv->dev, &xdp, xdp_prog)) {
403 frame = &orig_frame;
404 goto err_xdp;
405 }
406 *xdp_xmit |= VETH_XDP_REDIR;
407 rcu_read_unlock();
408 goto xdp_xmit;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900409 default:
410 bpf_warn_invalid_xdp_action(act);
411 case XDP_ABORTED:
412 trace_xdp_exception(priv->dev, xdp_prog, act);
413 case XDP_DROP:
414 goto err_xdp;
415 }
416 }
417 rcu_read_unlock();
418
419 headroom = sizeof(struct xdp_frame) + frame->headroom - delta;
420 skb = veth_build_skb(head, headroom, len, 0);
421 if (!skb) {
422 xdp_return_frame(frame);
423 goto err;
424 }
425
426 xdp_scrub_frame(frame);
427 skb->protocol = eth_type_trans(skb, priv->dev);
428err:
429 return skb;
430err_xdp:
431 rcu_read_unlock();
432 xdp_return_frame(frame);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900433xdp_xmit:
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900434 return NULL;
435}
436
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900437static struct sk_buff *veth_xdp_rcv_skb(struct veth_priv *priv,
Toshiaki Makitad1396002018-08-03 16:58:17 +0900438 struct sk_buff *skb,
439 unsigned int *xdp_xmit)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900440{
441 u32 pktlen, headroom, act, metalen;
442 void *orig_data, *orig_data_end;
443 struct bpf_prog *xdp_prog;
444 int mac_len, delta, off;
445 struct xdp_buff xdp;
446
447 rcu_read_lock();
448 xdp_prog = rcu_dereference(priv->xdp_prog);
449 if (unlikely(!xdp_prog)) {
450 rcu_read_unlock();
451 goto out;
452 }
453
454 mac_len = skb->data - skb_mac_header(skb);
455 pktlen = skb->len + mac_len;
456 headroom = skb_headroom(skb) - mac_len;
457
458 if (skb_shared(skb) || skb_head_is_locked(skb) ||
459 skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
460 struct sk_buff *nskb;
461 int size, head_off;
462 void *head, *start;
463 struct page *page;
464
465 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
466 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
467 if (size > PAGE_SIZE)
468 goto drop;
469
470 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
471 if (!page)
472 goto drop;
473
474 head = page_address(page);
475 start = head + VETH_XDP_HEADROOM;
476 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
477 page_frag_free(head);
478 goto drop;
479 }
480
481 nskb = veth_build_skb(head,
482 VETH_XDP_HEADROOM + mac_len, skb->len,
483 PAGE_SIZE);
484 if (!nskb) {
485 page_frag_free(head);
486 goto drop;
487 }
488
489 skb_copy_header(nskb, skb);
490 head_off = skb_headroom(nskb) - skb_headroom(skb);
491 skb_headers_offset_update(nskb, head_off);
492 if (skb->sk)
493 skb_set_owner_w(nskb, skb->sk);
494 consume_skb(skb);
495 skb = nskb;
496 }
497
498 xdp.data_hard_start = skb->head;
499 xdp.data = skb_mac_header(skb);
500 xdp.data_end = xdp.data + pktlen;
501 xdp.data_meta = xdp.data;
502 xdp.rxq = &priv->xdp_rxq;
503 orig_data = xdp.data;
504 orig_data_end = xdp.data_end;
505
506 act = bpf_prog_run_xdp(xdp_prog, &xdp);
507
508 switch (act) {
509 case XDP_PASS:
510 break;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900511 case XDP_TX:
512 get_page(virt_to_page(xdp.data));
513 consume_skb(skb);
514 xdp.rxq->mem = priv->xdp_mem;
515 if (unlikely(veth_xdp_tx(priv->dev, &xdp) < 0)) {
516 trace_xdp_exception(priv->dev, xdp_prog, act);
517 goto err_xdp;
518 }
519 *xdp_xmit |= VETH_XDP_TX;
520 rcu_read_unlock();
521 goto xdp_xmit;
522 case XDP_REDIRECT:
523 get_page(virt_to_page(xdp.data));
524 consume_skb(skb);
525 xdp.rxq->mem = priv->xdp_mem;
526 if (xdp_do_redirect(priv->dev, &xdp, xdp_prog))
527 goto err_xdp;
528 *xdp_xmit |= VETH_XDP_REDIR;
529 rcu_read_unlock();
530 goto xdp_xmit;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900531 default:
532 bpf_warn_invalid_xdp_action(act);
533 case XDP_ABORTED:
534 trace_xdp_exception(priv->dev, xdp_prog, act);
535 case XDP_DROP:
536 goto drop;
537 }
538 rcu_read_unlock();
539
540 delta = orig_data - xdp.data;
541 off = mac_len + delta;
542 if (off > 0)
543 __skb_push(skb, off);
544 else if (off < 0)
545 __skb_pull(skb, -off);
546 skb->mac_header -= delta;
547 off = xdp.data_end - orig_data_end;
548 if (off != 0)
549 __skb_put(skb, off);
550 skb->protocol = eth_type_trans(skb, priv->dev);
551
552 metalen = xdp.data - xdp.data_meta;
553 if (metalen)
554 skb_metadata_set(skb, metalen);
555out:
556 return skb;
557drop:
558 rcu_read_unlock();
559 kfree_skb(skb);
560 return NULL;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900561err_xdp:
562 rcu_read_unlock();
563 page_frag_free(xdp.data);
564xdp_xmit:
565 return NULL;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900566}
567
Toshiaki Makitad1396002018-08-03 16:58:17 +0900568static int veth_xdp_rcv(struct veth_priv *priv, int budget,
569 unsigned int *xdp_xmit)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900570{
571 int i, done = 0;
572
573 for (i = 0; i < budget; i++) {
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900574 void *ptr = __ptr_ring_consume(&priv->xdp_ring);
575 struct sk_buff *skb;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900576
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900577 if (!ptr)
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900578 break;
579
Toshiaki Makitad1396002018-08-03 16:58:17 +0900580 if (veth_is_xdp_frame(ptr)) {
581 skb = veth_xdp_rcv_one(priv, veth_ptr_to_xdp(ptr),
582 xdp_xmit);
583 } else {
584 skb = veth_xdp_rcv_skb(priv, ptr, xdp_xmit);
585 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900586
587 if (skb)
588 napi_gro_receive(&priv->xdp_napi, skb);
589
590 done++;
591 }
592
593 return done;
594}
595
596static int veth_poll(struct napi_struct *napi, int budget)
597{
598 struct veth_priv *priv =
599 container_of(napi, struct veth_priv, xdp_napi);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900600 unsigned int xdp_xmit = 0;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900601 int done;
602
Toshiaki Makitad1396002018-08-03 16:58:17 +0900603 xdp_set_return_frame_no_direct();
604 done = veth_xdp_rcv(priv, budget, &xdp_xmit);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900605
606 if (done < budget && napi_complete_done(napi, done)) {
607 /* Write rx_notify_masked before reading ptr_ring */
608 smp_store_mb(priv->rx_notify_masked, false);
609 if (unlikely(!__ptr_ring_empty(&priv->xdp_ring))) {
610 priv->rx_notify_masked = true;
611 napi_schedule(&priv->xdp_napi);
612 }
613 }
614
Toshiaki Makitad1396002018-08-03 16:58:17 +0900615 if (xdp_xmit & VETH_XDP_TX)
616 veth_xdp_flush(priv->dev);
617 if (xdp_xmit & VETH_XDP_REDIR)
618 xdp_do_flush_map();
619 xdp_clear_return_frame_no_direct();
620
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900621 return done;
622}
623
624static int veth_napi_add(struct net_device *dev)
625{
626 struct veth_priv *priv = netdev_priv(dev);
627 int err;
628
629 err = ptr_ring_init(&priv->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
630 if (err)
631 return err;
632
633 netif_napi_add(dev, &priv->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
634 napi_enable(&priv->xdp_napi);
635
636 return 0;
637}
638
639static void veth_napi_del(struct net_device *dev)
640{
641 struct veth_priv *priv = netdev_priv(dev);
642
643 napi_disable(&priv->xdp_napi);
644 netif_napi_del(&priv->xdp_napi);
645 priv->rx_notify_masked = false;
Toshiaki Makita9fc8d512018-08-03 16:58:13 +0900646 ptr_ring_cleanup(&priv->xdp_ring, veth_ptr_free);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900647}
648
649static int veth_enable_xdp(struct net_device *dev)
650{
651 struct veth_priv *priv = netdev_priv(dev);
652 int err;
653
654 if (!xdp_rxq_info_is_reg(&priv->xdp_rxq)) {
655 err = xdp_rxq_info_reg(&priv->xdp_rxq, dev, 0);
656 if (err < 0)
657 return err;
658
659 err = xdp_rxq_info_reg_mem_model(&priv->xdp_rxq,
660 MEM_TYPE_PAGE_SHARED, NULL);
661 if (err < 0)
662 goto err;
663
664 err = veth_napi_add(dev);
665 if (err)
666 goto err;
Toshiaki Makitad1396002018-08-03 16:58:17 +0900667
668 /* Save original mem info as it can be overwritten */
669 priv->xdp_mem = priv->xdp_rxq.mem;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900670 }
671
672 rcu_assign_pointer(priv->xdp_prog, priv->_xdp_prog);
673
674 return 0;
675err:
676 xdp_rxq_info_unreg(&priv->xdp_rxq);
677
678 return err;
679}
680
681static void veth_disable_xdp(struct net_device *dev)
682{
683 struct veth_priv *priv = netdev_priv(dev);
684
685 rcu_assign_pointer(priv->xdp_prog, NULL);
686 veth_napi_del(dev);
Toshiaki Makitad1396002018-08-03 16:58:17 +0900687 priv->xdp_rxq.mem = priv->xdp_mem;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900688 xdp_rxq_info_unreg(&priv->xdp_rxq);
689}
690
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700691static int veth_open(struct net_device *dev)
692{
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000693 struct veth_priv *priv = netdev_priv(dev);
694 struct net_device *peer = rtnl_dereference(priv->peer);
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900695 int err;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700696
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000697 if (!peer)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700698 return -ENOTCONN;
699
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900700 if (priv->_xdp_prog) {
701 err = veth_enable_xdp(dev);
702 if (err)
703 return err;
704 }
705
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000706 if (peer->flags & IFF_UP) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700707 netif_carrier_on(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +0000708 netif_carrier_on(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700709 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900710
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700711 return 0;
712}
713
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000714static int veth_close(struct net_device *dev)
715{
716 struct veth_priv *priv = netdev_priv(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000717 struct net_device *peer = rtnl_dereference(priv->peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000718
719 netif_carrier_off(dev);
Eric Dumazet2efd32e2013-01-10 08:32:45 +0000720 if (peer)
721 netif_carrier_off(peer);
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000722
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900723 if (priv->_xdp_prog)
724 veth_disable_xdp(dev);
725
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000726 return 0;
727}
728
Jarod Wilson91572082016-10-20 13:55:20 -0400729static int is_valid_veth_mtu(int mtu)
Eric Biederman38d40812009-03-03 23:36:04 -0800730{
Jarod Wilson91572082016-10-20 13:55:20 -0400731 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
Eric Biederman38d40812009-03-03 23:36:04 -0800732}
733
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700734static int veth_dev_init(struct net_device *dev)
735{
WANG Cong1c213bd2014-02-13 11:46:28 -0800736 dev->vstats = netdev_alloc_pcpu_stats(struct pcpu_vstats);
Eric Dumazet26811282012-12-29 16:02:43 +0000737 if (!dev->vstats)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700738 return -ENOMEM;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700739 return 0;
740}
741
David S. Miller11687a12009-06-25 02:45:42 -0700742static void veth_dev_free(struct net_device *dev)
743{
Eric Dumazet26811282012-12-29 16:02:43 +0000744 free_percpu(dev->vstats);
David S. Miller11687a12009-06-25 02:45:42 -0700745}
746
WANG Congbb446c12014-06-23 15:36:02 -0700747#ifdef CONFIG_NET_POLL_CONTROLLER
748static void veth_poll_controller(struct net_device *dev)
749{
750 /* veth only receives frames when its peer sends one
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900751 * Since it has nothing to do with disabling irqs, we are guaranteed
WANG Congbb446c12014-06-23 15:36:02 -0700752 * never to have pending data when we poll for it so
753 * there is nothing to do here.
754 *
755 * We need this though so netpoll recognizes us as an interface that
756 * supports polling, which enables bridge devices in virt setups to
757 * still use netconsole
758 */
759}
760#endif /* CONFIG_NET_POLL_CONTROLLER */
761
Nicolas Dichtela45253b2015-04-02 17:07:11 +0200762static int veth_get_iflink(const struct net_device *dev)
763{
764 struct veth_priv *priv = netdev_priv(dev);
765 struct net_device *peer;
766 int iflink;
767
768 rcu_read_lock();
769 peer = rcu_dereference(priv->peer);
770 iflink = peer ? peer->ifindex : 0;
771 rcu_read_unlock();
772
773 return iflink;
774}
775
Toshiaki Makitadc224822018-08-03 16:58:11 +0900776static netdev_features_t veth_fix_features(struct net_device *dev,
777 netdev_features_t features)
778{
779 struct veth_priv *priv = netdev_priv(dev);
780 struct net_device *peer;
781
782 peer = rtnl_dereference(priv->peer);
783 if (peer) {
784 struct veth_priv *peer_priv = netdev_priv(peer);
785
786 if (peer_priv->_xdp_prog)
787 features &= ~NETIF_F_GSO_SOFTWARE;
788 }
789
790 return features;
791}
792
Paolo Abeni163e5292016-02-26 10:45:41 +0100793static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
794{
795 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
796 struct net_device *peer;
797
798 if (new_hr < 0)
799 new_hr = 0;
800
801 rcu_read_lock();
802 peer = rcu_dereference(priv->peer);
803 if (unlikely(!peer))
804 goto out;
805
806 peer_priv = netdev_priv(peer);
807 priv->requested_headroom = new_hr;
808 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
809 dev->needed_headroom = new_hr;
810 peer->needed_headroom = new_hr;
811
812out:
813 rcu_read_unlock();
814}
815
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900816static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
817 struct netlink_ext_ack *extack)
818{
819 struct veth_priv *priv = netdev_priv(dev);
820 struct bpf_prog *old_prog;
821 struct net_device *peer;
Toshiaki Makitadc224822018-08-03 16:58:11 +0900822 unsigned int max_mtu;
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900823 int err;
824
825 old_prog = priv->_xdp_prog;
826 priv->_xdp_prog = prog;
827 peer = rtnl_dereference(priv->peer);
828
829 if (prog) {
830 if (!peer) {
831 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
832 err = -ENOTCONN;
833 goto err;
834 }
835
Toshiaki Makitadc224822018-08-03 16:58:11 +0900836 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
837 peer->hard_header_len -
838 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
839 if (peer->mtu > max_mtu) {
840 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
841 err = -ERANGE;
842 goto err;
843 }
844
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900845 if (dev->flags & IFF_UP) {
846 err = veth_enable_xdp(dev);
847 if (err) {
848 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
849 goto err;
850 }
851 }
Toshiaki Makitadc224822018-08-03 16:58:11 +0900852
853 if (!old_prog) {
854 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
855 peer->max_mtu = max_mtu;
856 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900857 }
858
859 if (old_prog) {
Toshiaki Makitadc224822018-08-03 16:58:11 +0900860 if (!prog) {
861 if (dev->flags & IFF_UP)
862 veth_disable_xdp(dev);
863
864 if (peer) {
865 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
866 peer->max_mtu = ETH_MAX_MTU;
867 }
868 }
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900869 bpf_prog_put(old_prog);
870 }
871
Toshiaki Makitadc224822018-08-03 16:58:11 +0900872 if ((!!old_prog ^ !!prog) && peer)
873 netdev_update_features(peer);
874
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900875 return 0;
876err:
877 priv->_xdp_prog = old_prog;
878
879 return err;
880}
881
882static u32 veth_xdp_query(struct net_device *dev)
883{
884 struct veth_priv *priv = netdev_priv(dev);
885 const struct bpf_prog *xdp_prog;
886
887 xdp_prog = priv->_xdp_prog;
888 if (xdp_prog)
889 return xdp_prog->aux->id;
890
891 return 0;
892}
893
894static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
895{
896 switch (xdp->command) {
897 case XDP_SETUP_PROG:
898 return veth_xdp_set(dev, xdp->prog, xdp->extack);
899 case XDP_QUERY_PROG:
900 xdp->prog_id = veth_xdp_query(dev);
901 return 0;
902 default:
903 return -EINVAL;
904 }
905}
906
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800907static const struct net_device_ops veth_netdev_ops = {
Daniel Lezcanoee923622009-02-22 00:04:45 -0800908 .ndo_init = veth_dev_init,
909 .ndo_open = veth_open,
Eric W. Biederman2cf48a12009-02-25 19:47:29 +0000910 .ndo_stop = veth_close,
Daniel Lezcanoee923622009-02-22 00:04:45 -0800911 .ndo_start_xmit = veth_xmit,
stephen hemminger6311cc42011-06-08 14:53:59 +0000912 .ndo_get_stats64 = veth_get_stats64,
Gao feng5c70ef82013-10-04 16:52:24 +0800913 .ndo_set_rx_mode = veth_set_multicast_list,
Daniel Lezcanoee923622009-02-22 00:04:45 -0800914 .ndo_set_mac_address = eth_mac_addr,
WANG Congbb446c12014-06-23 15:36:02 -0700915#ifdef CONFIG_NET_POLL_CONTROLLER
916 .ndo_poll_controller = veth_poll_controller,
917#endif
Nicolas Dichtela45253b2015-04-02 17:07:11 +0200918 .ndo_get_iflink = veth_get_iflink,
Toshiaki Makitadc224822018-08-03 16:58:11 +0900919 .ndo_fix_features = veth_fix_features,
Toshiaki Makita1a04a822015-07-31 15:03:25 +0900920 .ndo_features_check = passthru_features_check,
Paolo Abeni163e5292016-02-26 10:45:41 +0100921 .ndo_set_rx_headroom = veth_set_rx_headroom,
Toshiaki Makita948d4f22018-08-03 16:58:10 +0900922 .ndo_bpf = veth_xdp,
Toshiaki Makitaaf87a3a2018-08-03 16:58:14 +0900923 .ndo_xdp_xmit = veth_xdp_xmit,
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800924};
925
Alexander Duyck732912d72016-04-19 14:02:26 -0400926#define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
Xin Longc80fafb2016-08-25 13:21:49 +0800927 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
Alexander Duyck732912d72016-04-19 14:02:26 -0400928 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
Patrick McHardy28d2b132013-04-19 02:04:32 +0000929 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
930 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
Eric Dumazet80933152012-12-29 16:26:10 +0000931
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700932static void veth_setup(struct net_device *dev)
933{
934 ether_setup(dev);
935
Neil Horman550fd082011-07-26 06:05:38 +0000936 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
Hannes Frederic Sowa23ea5a92012-10-30 16:22:01 +0000937 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Phil Sutter02f01ec2015-08-18 10:30:29 +0200938 dev->priv_flags |= IFF_NO_QUEUE;
Paolo Abeni163e5292016-02-26 10:45:41 +0100939 dev->priv_flags |= IFF_PHONY_HEADROOM;
Neil Horman550fd082011-07-26 06:05:38 +0000940
Stephen Hemminger4456e7b2008-11-19 21:50:10 -0800941 dev->netdev_ops = &veth_netdev_ops;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700942 dev->ethtool_ops = &veth_ethtool_ops;
943 dev->features |= NETIF_F_LLTX;
Eric Dumazet80933152012-12-29 16:26:10 +0000944 dev->features |= VETH_FEATURES;
Toshiaki Makita8d0d21f2014-02-18 21:20:08 +0900945 dev->vlan_features = dev->features &
Vlad Yasevich3f8c7072014-03-27 22:14:48 -0400946 ~(NETIF_F_HW_VLAN_CTAG_TX |
947 NETIF_F_HW_VLAN_STAG_TX |
948 NETIF_F_HW_VLAN_CTAG_RX |
949 NETIF_F_HW_VLAN_STAG_RX);
David S. Millercf124db2017-05-08 12:52:56 -0400950 dev->needs_free_netdev = true;
951 dev->priv_destructor = veth_dev_free;
Jarod Wilson91572082016-10-20 13:55:20 -0400952 dev->max_mtu = ETH_MAX_MTU;
Michał Mirosława2c725f2011-03-31 01:01:35 +0000953
Eric Dumazet80933152012-12-29 16:26:10 +0000954 dev->hw_features = VETH_FEATURES;
Eric Dumazet82d81892013-10-25 18:25:03 -0700955 dev->hw_enc_features = VETH_FEATURES;
David Ahern607fca92016-08-24 20:10:45 -0700956 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700957}
958
959/*
960 * netlink interface
961 */
962
Matthias Schiffera8b8a8892017-06-25 23:56:01 +0200963static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
964 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700965{
966 if (tb[IFLA_ADDRESS]) {
967 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
968 return -EINVAL;
969 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
970 return -EADDRNOTAVAIL;
971 }
Eric Biederman38d40812009-03-03 23:36:04 -0800972 if (tb[IFLA_MTU]) {
973 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
974 return -EINVAL;
975 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700976 return 0;
977}
978
979static struct rtnl_link_ops veth_link_ops;
980
Eric W. Biederman81adee42009-11-08 00:53:51 -0800981static int veth_newlink(struct net *src_net, struct net_device *dev,
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +0200982 struct nlattr *tb[], struct nlattr *data[],
983 struct netlink_ext_ack *extack)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700984{
985 int err;
986 struct net_device *peer;
987 struct veth_priv *priv;
988 char ifname[IFNAMSIZ];
989 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
Tom Gundersen55177502014-07-14 16:37:25 +0200990 unsigned char name_assign_type;
Patrick McHardy3729d502010-02-26 06:34:54 +0000991 struct ifinfomsg *ifmp;
Eric W. Biederman81adee42009-11-08 00:53:51 -0800992 struct net *net;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700993
994 /*
995 * create and register peer first
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700996 */
Pavel Emelyanove314dbd2007-09-25 16:14:46 -0700997 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
998 struct nlattr *nla_peer;
999
1000 nla_peer = data[VETH_INFO_PEER];
Patrick McHardy3729d502010-02-26 06:34:54 +00001001 ifmp = nla_data(nla_peer);
Jiri Pirkof7b12602014-02-18 20:53:18 +01001002 err = rtnl_nla_parse_ifla(peer_tb,
1003 nla_data(nla_peer) + sizeof(struct ifinfomsg),
Johannes Bergfceb6432017-04-12 14:34:07 +02001004 nla_len(nla_peer) - sizeof(struct ifinfomsg),
1005 NULL);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001006 if (err < 0)
1007 return err;
1008
Matthias Schiffera8b8a8892017-06-25 23:56:01 +02001009 err = veth_validate(peer_tb, NULL, extack);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001010 if (err < 0)
1011 return err;
1012
1013 tbp = peer_tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001014 } else {
1015 ifmp = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001016 tbp = tb;
Patrick McHardy3729d502010-02-26 06:34:54 +00001017 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001018
Serhey Popovych191cdb32017-06-21 12:12:24 +03001019 if (ifmp && tbp[IFLA_IFNAME]) {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001020 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
Tom Gundersen55177502014-07-14 16:37:25 +02001021 name_assign_type = NET_NAME_USER;
1022 } else {
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001023 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
Tom Gundersen55177502014-07-14 16:37:25 +02001024 name_assign_type = NET_NAME_ENUM;
1025 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001026
Eric W. Biederman81adee42009-11-08 00:53:51 -08001027 net = rtnl_link_get_net(src_net, tbp);
1028 if (IS_ERR(net))
1029 return PTR_ERR(net);
1030
Tom Gundersen55177502014-07-14 16:37:25 +02001031 peer = rtnl_create_link(net, ifname, name_assign_type,
1032 &veth_link_ops, tbp);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001033 if (IS_ERR(peer)) {
1034 put_net(net);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001035 return PTR_ERR(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001036 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001037
Serhey Popovych191cdb32017-06-21 12:12:24 +03001038 if (!ifmp || !tbp[IFLA_ADDRESS])
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001039 eth_hw_addr_random(peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001040
Pavel Emelyanove6f8f1a2012-08-08 21:53:03 +00001041 if (ifmp && (dev->ifindex != 0))
1042 peer->ifindex = ifmp->ifi_index;
1043
Stephen Hemminger72d249552017-12-07 15:40:20 -08001044 peer->gso_max_size = dev->gso_max_size;
1045 peer->gso_max_segs = dev->gso_max_segs;
1046
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001047 err = register_netdevice(peer);
Eric W. Biederman81adee42009-11-08 00:53:51 -08001048 put_net(net);
1049 net = NULL;
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001050 if (err < 0)
1051 goto err_register_peer;
1052
1053 netif_carrier_off(peer);
1054
Patrick McHardy3729d502010-02-26 06:34:54 +00001055 err = rtnl_configure_link(peer, ifmp);
1056 if (err < 0)
1057 goto err_configure_peer;
1058
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001059 /*
1060 * register dev last
1061 *
1062 * note, that since we've registered new device the dev's name
1063 * should be re-allocated
1064 */
1065
1066 if (tb[IFLA_ADDRESS] == NULL)
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001067 eth_hw_addr_random(dev);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001068
Jiri Pirko6c8c4442011-04-30 01:28:17 +00001069 if (tb[IFLA_IFNAME])
1070 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1071 else
1072 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1073
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001074 err = register_netdevice(dev);
1075 if (err < 0)
1076 goto err_register_dev;
1077
1078 netif_carrier_off(dev);
1079
1080 /*
1081 * tie the deviced together
1082 */
1083
1084 priv = netdev_priv(dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001085 priv->dev = dev;
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001086 rcu_assign_pointer(priv->peer, peer);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001087
1088 priv = netdev_priv(peer);
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001089 priv->dev = peer;
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001090 rcu_assign_pointer(priv->peer, dev);
Toshiaki Makita948d4f22018-08-03 16:58:10 +09001091
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001092 return 0;
1093
1094err_register_dev:
1095 /* nothing to do */
Patrick McHardy3729d502010-02-26 06:34:54 +00001096err_configure_peer:
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001097 unregister_netdevice(peer);
1098 return err;
1099
1100err_register_peer:
1101 free_netdev(peer);
1102 return err;
1103}
1104
Eric Dumazet23289a32009-10-27 07:06:36 +00001105static void veth_dellink(struct net_device *dev, struct list_head *head)
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001106{
1107 struct veth_priv *priv;
1108 struct net_device *peer;
1109
1110 priv = netdev_priv(dev);
Eric Dumazetd0e2c552013-01-04 15:42:40 +00001111 peer = rtnl_dereference(priv->peer);
1112
1113 /* Note : dellink() is called from default_device_exit_batch(),
1114 * before a rcu_synchronize() point. The devices are guaranteed
1115 * not being freed before one RCU grace period.
1116 */
1117 RCU_INIT_POINTER(priv->peer, NULL);
Eric Dumazet24540532009-10-30 01:00:27 -07001118 unregister_netdevice_queue(dev, head);
Eric Dumazetf45a5c22013-02-08 20:10:49 +00001119
1120 if (peer) {
1121 priv = netdev_priv(peer);
1122 RCU_INIT_POINTER(priv->peer, NULL);
1123 unregister_netdevice_queue(peer, head);
1124 }
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001125}
1126
Thomas Graf23711432012-02-15 04:09:46 +00001127static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1128 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1129};
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001130
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +01001131static struct net *veth_get_link_net(const struct net_device *dev)
1132{
1133 struct veth_priv *priv = netdev_priv(dev);
1134 struct net_device *peer = rtnl_dereference(priv->peer);
1135
1136 return peer ? dev_net(peer) : dev_net(dev);
1137}
1138
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001139static struct rtnl_link_ops veth_link_ops = {
1140 .kind = DRV_NAME,
1141 .priv_size = sizeof(struct veth_priv),
1142 .setup = veth_setup,
1143 .validate = veth_validate,
1144 .newlink = veth_newlink,
1145 .dellink = veth_dellink,
1146 .policy = veth_policy,
1147 .maxtype = VETH_INFO_MAX,
Nicolas Dichtele5f4e7b2015-01-20 15:15:46 +01001148 .get_link_net = veth_get_link_net,
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001149};
1150
1151/*
1152 * init/fini
1153 */
1154
1155static __init int veth_init(void)
1156{
1157 return rtnl_link_register(&veth_link_ops);
1158}
1159
1160static __exit void veth_exit(void)
1161{
Patrick McHardy68365452008-01-20 17:25:14 -08001162 rtnl_link_unregister(&veth_link_ops);
Pavel Emelyanove314dbd2007-09-25 16:14:46 -07001163}
1164
1165module_init(veth_init);
1166module_exit(veth_exit);
1167
1168MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1169MODULE_LICENSE("GPL v2");
1170MODULE_ALIAS_RTNL_LINK(DRV_NAME);