GNU Linux-libre 5.4.241-gnu1
[releases.git] / drivers / net / veth.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  drivers/net/veth.c
4  *
5  *  Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
6  *
7  * Author: Pavel Emelianov <xemul@openvz.org>
8  * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
9  *
10  */
11
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ethtool.h>
15 #include <linux/etherdevice.h>
16 #include <linux/u64_stats_sync.h>
17
18 #include <net/rtnetlink.h>
19 #include <net/dst.h>
20 #include <net/xfrm.h>
21 #include <net/xdp.h>
22 #include <linux/veth.h>
23 #include <linux/module.h>
24 #include <linux/bpf.h>
25 #include <linux/filter.h>
26 #include <linux/ptr_ring.h>
27 #include <linux/bpf_trace.h>
28 #include <linux/net_tstamp.h>
29
30 #define DRV_NAME        "veth"
31 #define DRV_VERSION     "1.0"
32
33 #define VETH_XDP_FLAG           BIT(0)
34 #define VETH_RING_SIZE          256
35 #define VETH_XDP_HEADROOM       (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
36
37 /* Separating two types of XDP xmit */
38 #define VETH_XDP_TX             BIT(0)
39 #define VETH_XDP_REDIR          BIT(1)
40
41 #define VETH_XDP_TX_BULK_SIZE   16
42
43 struct veth_rq_stats {
44         u64                     xdp_packets;
45         u64                     xdp_bytes;
46         u64                     xdp_drops;
47         struct u64_stats_sync   syncp;
48 };
49
50 struct veth_rq {
51         struct napi_struct      xdp_napi;
52         struct net_device       *dev;
53         struct bpf_prog __rcu   *xdp_prog;
54         struct xdp_mem_info     xdp_mem;
55         struct veth_rq_stats    stats;
56         bool                    rx_notify_masked;
57         struct ptr_ring         xdp_ring;
58         struct xdp_rxq_info     xdp_rxq;
59 };
60
61 struct veth_priv {
62         struct net_device __rcu *peer;
63         atomic64_t              dropped;
64         struct bpf_prog         *_xdp_prog;
65         struct veth_rq          *rq;
66         unsigned int            requested_headroom;
67 };
68
69 struct veth_xdp_tx_bq {
70         struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
71         unsigned int count;
72 };
73
74 /*
75  * ethtool interface
76  */
77
78 struct veth_q_stat_desc {
79         char    desc[ETH_GSTRING_LEN];
80         size_t  offset;
81 };
82
83 #define VETH_RQ_STAT(m) offsetof(struct veth_rq_stats, m)
84
85 static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
86         { "xdp_packets",        VETH_RQ_STAT(xdp_packets) },
87         { "xdp_bytes",          VETH_RQ_STAT(xdp_bytes) },
88         { "xdp_drops",          VETH_RQ_STAT(xdp_drops) },
89 };
90
91 #define VETH_RQ_STATS_LEN       ARRAY_SIZE(veth_rq_stats_desc)
92
93 static struct {
94         const char string[ETH_GSTRING_LEN];
95 } ethtool_stats_keys[] = {
96         { "peer_ifindex" },
97 };
98
99 static int veth_get_link_ksettings(struct net_device *dev,
100                                    struct ethtool_link_ksettings *cmd)
101 {
102         cmd->base.speed         = SPEED_10000;
103         cmd->base.duplex        = DUPLEX_FULL;
104         cmd->base.port          = PORT_TP;
105         cmd->base.autoneg       = AUTONEG_DISABLE;
106         return 0;
107 }
108
109 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
110 {
111         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
112         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
113 }
114
115 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
116 {
117         char *p = (char *)buf;
118         int i, j;
119
120         switch(stringset) {
121         case ETH_SS_STATS:
122                 memcpy(p, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
123                 p += sizeof(ethtool_stats_keys);
124                 for (i = 0; i < dev->real_num_rx_queues; i++) {
125                         for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
126                                 snprintf(p, ETH_GSTRING_LEN,
127                                          "rx_queue_%u_%.11s",
128                                          i, veth_rq_stats_desc[j].desc);
129                                 p += ETH_GSTRING_LEN;
130                         }
131                 }
132                 break;
133         }
134 }
135
136 static int veth_get_sset_count(struct net_device *dev, int sset)
137 {
138         switch (sset) {
139         case ETH_SS_STATS:
140                 return ARRAY_SIZE(ethtool_stats_keys) +
141                        VETH_RQ_STATS_LEN * dev->real_num_rx_queues;
142         default:
143                 return -EOPNOTSUPP;
144         }
145 }
146
147 static void veth_get_ethtool_stats(struct net_device *dev,
148                 struct ethtool_stats *stats, u64 *data)
149 {
150         struct veth_priv *priv = netdev_priv(dev);
151         struct net_device *peer = rtnl_dereference(priv->peer);
152         int i, j, idx;
153
154         data[0] = peer ? peer->ifindex : 0;
155         idx = 1;
156         for (i = 0; i < dev->real_num_rx_queues; i++) {
157                 const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
158                 const void *stats_base = (void *)rq_stats;
159                 unsigned int start;
160                 size_t offset;
161
162                 do {
163                         start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
164                         for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
165                                 offset = veth_rq_stats_desc[j].offset;
166                                 data[idx + j] = *(u64 *)(stats_base + offset);
167                         }
168                 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
169                 idx += VETH_RQ_STATS_LEN;
170         }
171 }
172
173 static const struct ethtool_ops veth_ethtool_ops = {
174         .get_drvinfo            = veth_get_drvinfo,
175         .get_link               = ethtool_op_get_link,
176         .get_strings            = veth_get_strings,
177         .get_sset_count         = veth_get_sset_count,
178         .get_ethtool_stats      = veth_get_ethtool_stats,
179         .get_link_ksettings     = veth_get_link_ksettings,
180         .get_ts_info            = ethtool_op_get_ts_info,
181 };
182
183 /* general routines */
184
185 static bool veth_is_xdp_frame(void *ptr)
186 {
187         return (unsigned long)ptr & VETH_XDP_FLAG;
188 }
189
190 static void *veth_ptr_to_xdp(void *ptr)
191 {
192         return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
193 }
194
195 static void *veth_xdp_to_ptr(void *ptr)
196 {
197         return (void *)((unsigned long)ptr | VETH_XDP_FLAG);
198 }
199
200 static void veth_ptr_free(void *ptr)
201 {
202         if (veth_is_xdp_frame(ptr))
203                 xdp_return_frame(veth_ptr_to_xdp(ptr));
204         else
205                 kfree_skb(ptr);
206 }
207
208 static void __veth_xdp_flush(struct veth_rq *rq)
209 {
210         /* Write ptr_ring before reading rx_notify_masked */
211         smp_mb();
212         if (!READ_ONCE(rq->rx_notify_masked) &&
213             napi_schedule_prep(&rq->xdp_napi)) {
214                 WRITE_ONCE(rq->rx_notify_masked, true);
215                 __napi_schedule(&rq->xdp_napi);
216         }
217 }
218
219 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
220 {
221         if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
222                 dev_kfree_skb_any(skb);
223                 return NET_RX_DROP;
224         }
225
226         return NET_RX_SUCCESS;
227 }
228
229 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
230                             struct veth_rq *rq, bool xdp)
231 {
232         return __dev_forward_skb(dev, skb) ?: xdp ?
233                 veth_xdp_rx(rq, skb) :
234                 netif_rx(skb);
235 }
236
237 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
238 {
239         struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
240         struct veth_rq *rq = NULL;
241         struct net_device *rcv;
242         int length = skb->len;
243         bool rcv_xdp = false;
244         int rxq;
245
246         rcu_read_lock();
247         rcv = rcu_dereference(priv->peer);
248         if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
249                 kfree_skb(skb);
250                 goto drop;
251         }
252
253         rcv_priv = netdev_priv(rcv);
254         rxq = skb_get_queue_mapping(skb);
255         if (rxq < rcv->real_num_rx_queues) {
256                 rq = &rcv_priv->rq[rxq];
257                 rcv_xdp = rcu_access_pointer(rq->xdp_prog);
258         }
259
260         skb_tx_timestamp(skb);
261         if (likely(veth_forward_skb(rcv, skb, rq, rcv_xdp) == NET_RX_SUCCESS)) {
262                 if (!rcv_xdp) {
263                         struct pcpu_lstats *stats = this_cpu_ptr(dev->lstats);
264
265                         u64_stats_update_begin(&stats->syncp);
266                         stats->bytes += length;
267                         stats->packets++;
268                         u64_stats_update_end(&stats->syncp);
269                 }
270         } else {
271 drop:
272                 atomic64_inc(&priv->dropped);
273         }
274
275         if (rcv_xdp)
276                 __veth_xdp_flush(rq);
277
278         rcu_read_unlock();
279
280         return NETDEV_TX_OK;
281 }
282
283 static u64 veth_stats_tx(struct pcpu_lstats *result, struct net_device *dev)
284 {
285         struct veth_priv *priv = netdev_priv(dev);
286         int cpu;
287
288         result->packets = 0;
289         result->bytes = 0;
290         for_each_possible_cpu(cpu) {
291                 struct pcpu_lstats *stats = per_cpu_ptr(dev->lstats, cpu);
292                 u64 packets, bytes;
293                 unsigned int start;
294
295                 do {
296                         start = u64_stats_fetch_begin_irq(&stats->syncp);
297                         packets = stats->packets;
298                         bytes = stats->bytes;
299                 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
300                 result->packets += packets;
301                 result->bytes += bytes;
302         }
303         return atomic64_read(&priv->dropped);
304 }
305
306 static void veth_stats_rx(struct veth_rq_stats *result, struct net_device *dev)
307 {
308         struct veth_priv *priv = netdev_priv(dev);
309         int i;
310
311         result->xdp_packets = 0;
312         result->xdp_bytes = 0;
313         result->xdp_drops = 0;
314         for (i = 0; i < dev->num_rx_queues; i++) {
315                 struct veth_rq_stats *stats = &priv->rq[i].stats;
316                 u64 packets, bytes, drops;
317                 unsigned int start;
318
319                 do {
320                         start = u64_stats_fetch_begin_irq(&stats->syncp);
321                         packets = stats->xdp_packets;
322                         bytes = stats->xdp_bytes;
323                         drops = stats->xdp_drops;
324                 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
325                 result->xdp_packets += packets;
326                 result->xdp_bytes += bytes;
327                 result->xdp_drops += drops;
328         }
329 }
330
331 static void veth_get_stats64(struct net_device *dev,
332                              struct rtnl_link_stats64 *tot)
333 {
334         struct veth_priv *priv = netdev_priv(dev);
335         struct net_device *peer;
336         struct veth_rq_stats rx;
337         struct pcpu_lstats tx;
338
339         tot->tx_dropped = veth_stats_tx(&tx, dev);
340         tot->tx_bytes = tx.bytes;
341         tot->tx_packets = tx.packets;
342
343         veth_stats_rx(&rx, dev);
344         tot->rx_dropped = rx.xdp_drops;
345         tot->rx_bytes = rx.xdp_bytes;
346         tot->rx_packets = rx.xdp_packets;
347
348         rcu_read_lock();
349         peer = rcu_dereference(priv->peer);
350         if (peer) {
351                 tot->rx_dropped += veth_stats_tx(&tx, peer);
352                 tot->rx_bytes += tx.bytes;
353                 tot->rx_packets += tx.packets;
354
355                 veth_stats_rx(&rx, peer);
356                 tot->tx_bytes += rx.xdp_bytes;
357                 tot->tx_packets += rx.xdp_packets;
358         }
359         rcu_read_unlock();
360 }
361
362 /* fake multicast ability */
363 static void veth_set_multicast_list(struct net_device *dev)
364 {
365 }
366
367 static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
368                                       int buflen)
369 {
370         struct sk_buff *skb;
371
372         if (!buflen) {
373                 buflen = SKB_DATA_ALIGN(headroom + len) +
374                          SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
375         }
376         skb = build_skb(head, buflen);
377         if (!skb)
378                 return NULL;
379
380         skb_reserve(skb, headroom);
381         skb_put(skb, len);
382
383         return skb;
384 }
385
386 static int veth_select_rxq(struct net_device *dev)
387 {
388         return smp_processor_id() % dev->real_num_rx_queues;
389 }
390
391 static int veth_xdp_xmit(struct net_device *dev, int n,
392                          struct xdp_frame **frames, u32 flags)
393 {
394         struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
395         struct net_device *rcv;
396         int i, ret, drops = n;
397         unsigned int max_len;
398         struct veth_rq *rq;
399
400         if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
401                 ret = -EINVAL;
402                 goto drop;
403         }
404
405         rcv = rcu_dereference(priv->peer);
406         if (unlikely(!rcv)) {
407                 ret = -ENXIO;
408                 goto drop;
409         }
410
411         rcv_priv = netdev_priv(rcv);
412         rq = &rcv_priv->rq[veth_select_rxq(rcv)];
413         /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
414          * side. This means an XDP program is loaded on the peer and the peer
415          * device is up.
416          */
417         if (!rcu_access_pointer(rq->xdp_prog)) {
418                 ret = -ENXIO;
419                 goto drop;
420         }
421
422         drops = 0;
423         max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
424
425         spin_lock(&rq->xdp_ring.producer_lock);
426         for (i = 0; i < n; i++) {
427                 struct xdp_frame *frame = frames[i];
428                 void *ptr = veth_xdp_to_ptr(frame);
429
430                 if (unlikely(frame->len > max_len ||
431                              __ptr_ring_produce(&rq->xdp_ring, ptr))) {
432                         xdp_return_frame_rx_napi(frame);
433                         drops++;
434                 }
435         }
436         spin_unlock(&rq->xdp_ring.producer_lock);
437
438         if (flags & XDP_XMIT_FLUSH)
439                 __veth_xdp_flush(rq);
440
441         if (likely(!drops))
442                 return n;
443
444         ret = n - drops;
445 drop:
446         atomic64_add(drops, &priv->dropped);
447
448         return ret;
449 }
450
451 static void veth_xdp_flush_bq(struct net_device *dev, struct veth_xdp_tx_bq *bq)
452 {
453         int sent, i, err = 0;
454
455         sent = veth_xdp_xmit(dev, bq->count, bq->q, 0);
456         if (sent < 0) {
457                 err = sent;
458                 sent = 0;
459                 for (i = 0; i < bq->count; i++)
460                         xdp_return_frame(bq->q[i]);
461         }
462         trace_xdp_bulk_tx(dev, sent, bq->count - sent, err);
463
464         bq->count = 0;
465 }
466
467 static void veth_xdp_flush(struct net_device *dev, struct veth_xdp_tx_bq *bq)
468 {
469         struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
470         struct net_device *rcv;
471         struct veth_rq *rq;
472
473         rcu_read_lock();
474         veth_xdp_flush_bq(dev, bq);
475         rcv = rcu_dereference(priv->peer);
476         if (unlikely(!rcv))
477                 goto out;
478
479         rcv_priv = netdev_priv(rcv);
480         rq = &rcv_priv->rq[veth_select_rxq(rcv)];
481         /* xdp_ring is initialized on receive side? */
482         if (unlikely(!rcu_access_pointer(rq->xdp_prog)))
483                 goto out;
484
485         __veth_xdp_flush(rq);
486 out:
487         rcu_read_unlock();
488 }
489
490 static int veth_xdp_tx(struct net_device *dev, struct xdp_buff *xdp,
491                        struct veth_xdp_tx_bq *bq)
492 {
493         struct xdp_frame *frame = convert_to_xdp_frame(xdp);
494
495         if (unlikely(!frame))
496                 return -EOVERFLOW;
497
498         if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
499                 veth_xdp_flush_bq(dev, bq);
500
501         bq->q[bq->count++] = frame;
502
503         return 0;
504 }
505
506 static struct sk_buff *veth_xdp_rcv_one(struct veth_rq *rq,
507                                         struct xdp_frame *frame,
508                                         unsigned int *xdp_xmit,
509                                         struct veth_xdp_tx_bq *bq)
510 {
511         void *hard_start = frame->data - frame->headroom;
512         int len = frame->len, delta = 0;
513         struct xdp_frame orig_frame;
514         struct bpf_prog *xdp_prog;
515         unsigned int headroom;
516         struct sk_buff *skb;
517
518         /* bpf_xdp_adjust_head() assures BPF cannot access xdp_frame area */
519         hard_start -= sizeof(struct xdp_frame);
520
521         rcu_read_lock();
522         xdp_prog = rcu_dereference(rq->xdp_prog);
523         if (likely(xdp_prog)) {
524                 struct xdp_buff xdp;
525                 u32 act;
526
527                 xdp.data_hard_start = hard_start;
528                 xdp.data = frame->data;
529                 xdp.data_end = frame->data + frame->len;
530                 xdp.data_meta = frame->data - frame->metasize;
531                 xdp.rxq = &rq->xdp_rxq;
532
533                 act = bpf_prog_run_xdp(xdp_prog, &xdp);
534
535                 switch (act) {
536                 case XDP_PASS:
537                         delta = frame->data - xdp.data;
538                         len = xdp.data_end - xdp.data;
539                         break;
540                 case XDP_TX:
541                         orig_frame = *frame;
542                         xdp.rxq->mem = frame->mem;
543                         if (unlikely(veth_xdp_tx(rq->dev, &xdp, bq) < 0)) {
544                                 trace_xdp_exception(rq->dev, xdp_prog, act);
545                                 frame = &orig_frame;
546                                 goto err_xdp;
547                         }
548                         *xdp_xmit |= VETH_XDP_TX;
549                         rcu_read_unlock();
550                         goto xdp_xmit;
551                 case XDP_REDIRECT:
552                         orig_frame = *frame;
553                         xdp.rxq->mem = frame->mem;
554                         if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
555                                 frame = &orig_frame;
556                                 goto err_xdp;
557                         }
558                         *xdp_xmit |= VETH_XDP_REDIR;
559                         rcu_read_unlock();
560                         goto xdp_xmit;
561                 default:
562                         bpf_warn_invalid_xdp_action(act);
563                         /* fall through */
564                 case XDP_ABORTED:
565                         trace_xdp_exception(rq->dev, xdp_prog, act);
566                         /* fall through */
567                 case XDP_DROP:
568                         goto err_xdp;
569                 }
570         }
571         rcu_read_unlock();
572
573         headroom = sizeof(struct xdp_frame) + frame->headroom - delta;
574         skb = veth_build_skb(hard_start, headroom, len, 0);
575         if (!skb) {
576                 xdp_return_frame(frame);
577                 goto err;
578         }
579
580         xdp_release_frame(frame);
581         xdp_scrub_frame(frame);
582         skb->protocol = eth_type_trans(skb, rq->dev);
583 err:
584         return skb;
585 err_xdp:
586         rcu_read_unlock();
587         xdp_return_frame(frame);
588 xdp_xmit:
589         return NULL;
590 }
591
592 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq, struct sk_buff *skb,
593                                         unsigned int *xdp_xmit,
594                                         struct veth_xdp_tx_bq *bq)
595 {
596         u32 pktlen, headroom, act, metalen;
597         void *orig_data, *orig_data_end;
598         struct bpf_prog *xdp_prog;
599         int mac_len, delta, off;
600         struct xdp_buff xdp;
601
602         skb_orphan(skb);
603
604         rcu_read_lock();
605         xdp_prog = rcu_dereference(rq->xdp_prog);
606         if (unlikely(!xdp_prog)) {
607                 rcu_read_unlock();
608                 goto out;
609         }
610
611         mac_len = skb->data - skb_mac_header(skb);
612         pktlen = skb->len + mac_len;
613         headroom = skb_headroom(skb) - mac_len;
614
615         if (skb_shared(skb) || skb_head_is_locked(skb) ||
616             skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
617                 struct sk_buff *nskb;
618                 int size, head_off;
619                 void *head, *start;
620                 struct page *page;
621
622                 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
623                        SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
624                 if (size > PAGE_SIZE)
625                         goto drop;
626
627                 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
628                 if (!page)
629                         goto drop;
630
631                 head = page_address(page);
632                 start = head + VETH_XDP_HEADROOM;
633                 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
634                         page_frag_free(head);
635                         goto drop;
636                 }
637
638                 nskb = veth_build_skb(head,
639                                       VETH_XDP_HEADROOM + mac_len, skb->len,
640                                       PAGE_SIZE);
641                 if (!nskb) {
642                         page_frag_free(head);
643                         goto drop;
644                 }
645
646                 skb_copy_header(nskb, skb);
647                 head_off = skb_headroom(nskb) - skb_headroom(skb);
648                 skb_headers_offset_update(nskb, head_off);
649                 consume_skb(skb);
650                 skb = nskb;
651         }
652
653         xdp.data_hard_start = skb->head;
654         xdp.data = skb_mac_header(skb);
655         xdp.data_end = xdp.data + pktlen;
656         xdp.data_meta = xdp.data;
657         xdp.rxq = &rq->xdp_rxq;
658         orig_data = xdp.data;
659         orig_data_end = xdp.data_end;
660
661         act = bpf_prog_run_xdp(xdp_prog, &xdp);
662
663         switch (act) {
664         case XDP_PASS:
665                 break;
666         case XDP_TX:
667                 get_page(virt_to_page(xdp.data));
668                 consume_skb(skb);
669                 xdp.rxq->mem = rq->xdp_mem;
670                 if (unlikely(veth_xdp_tx(rq->dev, &xdp, bq) < 0)) {
671                         trace_xdp_exception(rq->dev, xdp_prog, act);
672                         goto err_xdp;
673                 }
674                 *xdp_xmit |= VETH_XDP_TX;
675                 rcu_read_unlock();
676                 goto xdp_xmit;
677         case XDP_REDIRECT:
678                 get_page(virt_to_page(xdp.data));
679                 consume_skb(skb);
680                 xdp.rxq->mem = rq->xdp_mem;
681                 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog))
682                         goto err_xdp;
683                 *xdp_xmit |= VETH_XDP_REDIR;
684                 rcu_read_unlock();
685                 goto xdp_xmit;
686         default:
687                 bpf_warn_invalid_xdp_action(act);
688                 /* fall through */
689         case XDP_ABORTED:
690                 trace_xdp_exception(rq->dev, xdp_prog, act);
691                 /* fall through */
692         case XDP_DROP:
693                 goto drop;
694         }
695         rcu_read_unlock();
696
697         delta = orig_data - xdp.data;
698         off = mac_len + delta;
699         if (off > 0)
700                 __skb_push(skb, off);
701         else if (off < 0)
702                 __skb_pull(skb, -off);
703         skb->mac_header -= delta;
704         off = xdp.data_end - orig_data_end;
705         if (off != 0)
706                 __skb_put(skb, off);
707         skb->protocol = eth_type_trans(skb, rq->dev);
708
709         metalen = xdp.data - xdp.data_meta;
710         if (metalen)
711                 skb_metadata_set(skb, metalen);
712 out:
713         return skb;
714 drop:
715         rcu_read_unlock();
716         kfree_skb(skb);
717         return NULL;
718 err_xdp:
719         rcu_read_unlock();
720         page_frag_free(xdp.data);
721 xdp_xmit:
722         return NULL;
723 }
724
725 static int veth_xdp_rcv(struct veth_rq *rq, int budget, unsigned int *xdp_xmit,
726                         struct veth_xdp_tx_bq *bq)
727 {
728         int i, done = 0, drops = 0, bytes = 0;
729
730         for (i = 0; i < budget; i++) {
731                 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
732                 unsigned int xdp_xmit_one = 0;
733                 struct sk_buff *skb;
734
735                 if (!ptr)
736                         break;
737
738                 if (veth_is_xdp_frame(ptr)) {
739                         struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
740
741                         bytes += frame->len;
742                         skb = veth_xdp_rcv_one(rq, frame, &xdp_xmit_one, bq);
743                 } else {
744                         skb = ptr;
745                         bytes += skb->len;
746                         skb = veth_xdp_rcv_skb(rq, skb, &xdp_xmit_one, bq);
747                 }
748                 *xdp_xmit |= xdp_xmit_one;
749
750                 if (skb)
751                         napi_gro_receive(&rq->xdp_napi, skb);
752                 else if (!xdp_xmit_one)
753                         drops++;
754
755                 done++;
756         }
757
758         u64_stats_update_begin(&rq->stats.syncp);
759         rq->stats.xdp_packets += done;
760         rq->stats.xdp_bytes += bytes;
761         rq->stats.xdp_drops += drops;
762         u64_stats_update_end(&rq->stats.syncp);
763
764         return done;
765 }
766
767 static int veth_poll(struct napi_struct *napi, int budget)
768 {
769         struct veth_rq *rq =
770                 container_of(napi, struct veth_rq, xdp_napi);
771         unsigned int xdp_xmit = 0;
772         struct veth_xdp_tx_bq bq;
773         int done;
774
775         bq.count = 0;
776
777         xdp_set_return_frame_no_direct();
778         done = veth_xdp_rcv(rq, budget, &xdp_xmit, &bq);
779
780         if (done < budget && napi_complete_done(napi, done)) {
781                 /* Write rx_notify_masked before reading ptr_ring */
782                 smp_store_mb(rq->rx_notify_masked, false);
783                 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
784                         if (napi_schedule_prep(&rq->xdp_napi)) {
785                                 WRITE_ONCE(rq->rx_notify_masked, true);
786                                 __napi_schedule(&rq->xdp_napi);
787                         }
788                 }
789         }
790
791         if (xdp_xmit & VETH_XDP_TX)
792                 veth_xdp_flush(rq->dev, &bq);
793         if (xdp_xmit & VETH_XDP_REDIR)
794                 xdp_do_flush_map();
795         xdp_clear_return_frame_no_direct();
796
797         return done;
798 }
799
800 static int veth_napi_add(struct net_device *dev)
801 {
802         struct veth_priv *priv = netdev_priv(dev);
803         int err, i;
804
805         for (i = 0; i < dev->real_num_rx_queues; i++) {
806                 struct veth_rq *rq = &priv->rq[i];
807
808                 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
809                 if (err)
810                         goto err_xdp_ring;
811         }
812
813         for (i = 0; i < dev->real_num_rx_queues; i++) {
814                 struct veth_rq *rq = &priv->rq[i];
815
816                 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
817                 napi_enable(&rq->xdp_napi);
818         }
819
820         return 0;
821 err_xdp_ring:
822         for (i--; i >= 0; i--)
823                 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
824
825         return err;
826 }
827
828 static void veth_napi_del(struct net_device *dev)
829 {
830         struct veth_priv *priv = netdev_priv(dev);
831         int i;
832
833         for (i = 0; i < dev->real_num_rx_queues; i++) {
834                 struct veth_rq *rq = &priv->rq[i];
835
836                 napi_disable(&rq->xdp_napi);
837                 napi_hash_del(&rq->xdp_napi);
838         }
839         synchronize_net();
840
841         for (i = 0; i < dev->real_num_rx_queues; i++) {
842                 struct veth_rq *rq = &priv->rq[i];
843
844                 netif_napi_del(&rq->xdp_napi);
845                 rq->rx_notify_masked = false;
846                 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
847         }
848 }
849
850 static int veth_enable_xdp(struct net_device *dev)
851 {
852         struct veth_priv *priv = netdev_priv(dev);
853         int err, i;
854
855         if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
856                 for (i = 0; i < dev->real_num_rx_queues; i++) {
857                         struct veth_rq *rq = &priv->rq[i];
858
859                         err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i);
860                         if (err < 0)
861                                 goto err_rxq_reg;
862
863                         err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
864                                                          MEM_TYPE_PAGE_SHARED,
865                                                          NULL);
866                         if (err < 0)
867                                 goto err_reg_mem;
868
869                         /* Save original mem info as it can be overwritten */
870                         rq->xdp_mem = rq->xdp_rxq.mem;
871                 }
872
873                 err = veth_napi_add(dev);
874                 if (err)
875                         goto err_rxq_reg;
876         }
877
878         for (i = 0; i < dev->real_num_rx_queues; i++)
879                 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
880
881         return 0;
882 err_reg_mem:
883         xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
884 err_rxq_reg:
885         for (i--; i >= 0; i--)
886                 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
887
888         return err;
889 }
890
891 static void veth_disable_xdp(struct net_device *dev)
892 {
893         struct veth_priv *priv = netdev_priv(dev);
894         int i;
895
896         for (i = 0; i < dev->real_num_rx_queues; i++)
897                 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
898         veth_napi_del(dev);
899         for (i = 0; i < dev->real_num_rx_queues; i++) {
900                 struct veth_rq *rq = &priv->rq[i];
901
902                 rq->xdp_rxq.mem = rq->xdp_mem;
903                 xdp_rxq_info_unreg(&rq->xdp_rxq);
904         }
905 }
906
907 static int veth_open(struct net_device *dev)
908 {
909         struct veth_priv *priv = netdev_priv(dev);
910         struct net_device *peer = rtnl_dereference(priv->peer);
911         int err;
912
913         if (!peer)
914                 return -ENOTCONN;
915
916         if (priv->_xdp_prog) {
917                 err = veth_enable_xdp(dev);
918                 if (err)
919                         return err;
920         }
921
922         if (peer->flags & IFF_UP) {
923                 netif_carrier_on(dev);
924                 netif_carrier_on(peer);
925         }
926
927         return 0;
928 }
929
930 static int veth_close(struct net_device *dev)
931 {
932         struct veth_priv *priv = netdev_priv(dev);
933         struct net_device *peer = rtnl_dereference(priv->peer);
934
935         netif_carrier_off(dev);
936         if (peer)
937                 netif_carrier_off(peer);
938
939         if (priv->_xdp_prog)
940                 veth_disable_xdp(dev);
941
942         return 0;
943 }
944
945 static int is_valid_veth_mtu(int mtu)
946 {
947         return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
948 }
949
950 static int veth_alloc_queues(struct net_device *dev)
951 {
952         struct veth_priv *priv = netdev_priv(dev);
953         int i;
954
955         priv->rq = kcalloc(dev->num_rx_queues, sizeof(*priv->rq), GFP_KERNEL);
956         if (!priv->rq)
957                 return -ENOMEM;
958
959         for (i = 0; i < dev->num_rx_queues; i++) {
960                 priv->rq[i].dev = dev;
961                 u64_stats_init(&priv->rq[i].stats.syncp);
962         }
963
964         return 0;
965 }
966
967 static void veth_free_queues(struct net_device *dev)
968 {
969         struct veth_priv *priv = netdev_priv(dev);
970
971         kfree(priv->rq);
972 }
973
974 static int veth_dev_init(struct net_device *dev)
975 {
976         int err;
977
978         dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
979         if (!dev->lstats)
980                 return -ENOMEM;
981
982         err = veth_alloc_queues(dev);
983         if (err) {
984                 free_percpu(dev->lstats);
985                 return err;
986         }
987
988         return 0;
989 }
990
991 static void veth_dev_free(struct net_device *dev)
992 {
993         veth_free_queues(dev);
994         free_percpu(dev->lstats);
995 }
996
997 #ifdef CONFIG_NET_POLL_CONTROLLER
998 static void veth_poll_controller(struct net_device *dev)
999 {
1000         /* veth only receives frames when its peer sends one
1001          * Since it has nothing to do with disabling irqs, we are guaranteed
1002          * never to have pending data when we poll for it so
1003          * there is nothing to do here.
1004          *
1005          * We need this though so netpoll recognizes us as an interface that
1006          * supports polling, which enables bridge devices in virt setups to
1007          * still use netconsole
1008          */
1009 }
1010 #endif  /* CONFIG_NET_POLL_CONTROLLER */
1011
1012 static int veth_get_iflink(const struct net_device *dev)
1013 {
1014         struct veth_priv *priv = netdev_priv(dev);
1015         struct net_device *peer;
1016         int iflink;
1017
1018         rcu_read_lock();
1019         peer = rcu_dereference(priv->peer);
1020         iflink = peer ? peer->ifindex : 0;
1021         rcu_read_unlock();
1022
1023         return iflink;
1024 }
1025
1026 static netdev_features_t veth_fix_features(struct net_device *dev,
1027                                            netdev_features_t features)
1028 {
1029         struct veth_priv *priv = netdev_priv(dev);
1030         struct net_device *peer;
1031
1032         peer = rtnl_dereference(priv->peer);
1033         if (peer) {
1034                 struct veth_priv *peer_priv = netdev_priv(peer);
1035
1036                 if (peer_priv->_xdp_prog)
1037                         features &= ~NETIF_F_GSO_SOFTWARE;
1038         }
1039
1040         return features;
1041 }
1042
1043 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1044 {
1045         struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1046         struct net_device *peer;
1047
1048         if (new_hr < 0)
1049                 new_hr = 0;
1050
1051         rcu_read_lock();
1052         peer = rcu_dereference(priv->peer);
1053         if (unlikely(!peer))
1054                 goto out;
1055
1056         peer_priv = netdev_priv(peer);
1057         priv->requested_headroom = new_hr;
1058         new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1059         dev->needed_headroom = new_hr;
1060         peer->needed_headroom = new_hr;
1061
1062 out:
1063         rcu_read_unlock();
1064 }
1065
1066 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1067                         struct netlink_ext_ack *extack)
1068 {
1069         struct veth_priv *priv = netdev_priv(dev);
1070         struct bpf_prog *old_prog;
1071         struct net_device *peer;
1072         unsigned int max_mtu;
1073         int err;
1074
1075         old_prog = priv->_xdp_prog;
1076         priv->_xdp_prog = prog;
1077         peer = rtnl_dereference(priv->peer);
1078
1079         if (prog) {
1080                 if (!peer) {
1081                         NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1082                         err = -ENOTCONN;
1083                         goto err;
1084                 }
1085
1086                 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
1087                           peer->hard_header_len -
1088                           SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1089                 if (peer->mtu > max_mtu) {
1090                         NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1091                         err = -ERANGE;
1092                         goto err;
1093                 }
1094
1095                 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1096                         NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1097                         err = -ENOSPC;
1098                         goto err;
1099                 }
1100
1101                 if (dev->flags & IFF_UP) {
1102                         err = veth_enable_xdp(dev);
1103                         if (err) {
1104                                 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1105                                 goto err;
1106                         }
1107                 }
1108
1109                 if (!old_prog) {
1110                         peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1111                         peer->max_mtu = max_mtu;
1112                 }
1113         }
1114
1115         if (old_prog) {
1116                 if (!prog) {
1117                         if (dev->flags & IFF_UP)
1118                                 veth_disable_xdp(dev);
1119
1120                         if (peer) {
1121                                 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1122                                 peer->max_mtu = ETH_MAX_MTU;
1123                         }
1124                 }
1125                 bpf_prog_put(old_prog);
1126         }
1127
1128         if ((!!old_prog ^ !!prog) && peer)
1129                 netdev_update_features(peer);
1130
1131         return 0;
1132 err:
1133         priv->_xdp_prog = old_prog;
1134
1135         return err;
1136 }
1137
1138 static u32 veth_xdp_query(struct net_device *dev)
1139 {
1140         struct veth_priv *priv = netdev_priv(dev);
1141         const struct bpf_prog *xdp_prog;
1142
1143         xdp_prog = priv->_xdp_prog;
1144         if (xdp_prog)
1145                 return xdp_prog->aux->id;
1146
1147         return 0;
1148 }
1149
1150 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1151 {
1152         switch (xdp->command) {
1153         case XDP_SETUP_PROG:
1154                 return veth_xdp_set(dev, xdp->prog, xdp->extack);
1155         case XDP_QUERY_PROG:
1156                 xdp->prog_id = veth_xdp_query(dev);
1157                 return 0;
1158         default:
1159                 return -EINVAL;
1160         }
1161 }
1162
1163 static const struct net_device_ops veth_netdev_ops = {
1164         .ndo_init            = veth_dev_init,
1165         .ndo_open            = veth_open,
1166         .ndo_stop            = veth_close,
1167         .ndo_start_xmit      = veth_xmit,
1168         .ndo_get_stats64     = veth_get_stats64,
1169         .ndo_set_rx_mode     = veth_set_multicast_list,
1170         .ndo_set_mac_address = eth_mac_addr,
1171 #ifdef CONFIG_NET_POLL_CONTROLLER
1172         .ndo_poll_controller    = veth_poll_controller,
1173 #endif
1174         .ndo_get_iflink         = veth_get_iflink,
1175         .ndo_fix_features       = veth_fix_features,
1176         .ndo_features_check     = passthru_features_check,
1177         .ndo_set_rx_headroom    = veth_set_rx_headroom,
1178         .ndo_bpf                = veth_xdp,
1179         .ndo_xdp_xmit           = veth_xdp_xmit,
1180 };
1181
1182 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1183                        NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1184                        NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1185                        NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1186                        NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1187
1188 static void veth_setup(struct net_device *dev)
1189 {
1190         ether_setup(dev);
1191
1192         dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1193         dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1194         dev->priv_flags |= IFF_NO_QUEUE;
1195         dev->priv_flags |= IFF_PHONY_HEADROOM;
1196
1197         dev->netdev_ops = &veth_netdev_ops;
1198         dev->ethtool_ops = &veth_ethtool_ops;
1199         dev->features |= NETIF_F_LLTX;
1200         dev->features |= VETH_FEATURES;
1201         dev->vlan_features = dev->features &
1202                              ~(NETIF_F_HW_VLAN_CTAG_TX |
1203                                NETIF_F_HW_VLAN_STAG_TX |
1204                                NETIF_F_HW_VLAN_CTAG_RX |
1205                                NETIF_F_HW_VLAN_STAG_RX);
1206         dev->needs_free_netdev = true;
1207         dev->priv_destructor = veth_dev_free;
1208         dev->max_mtu = ETH_MAX_MTU;
1209
1210         dev->hw_features = VETH_FEATURES;
1211         dev->hw_enc_features = VETH_FEATURES;
1212         dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1213 }
1214
1215 /*
1216  * netlink interface
1217  */
1218
1219 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1220                          struct netlink_ext_ack *extack)
1221 {
1222         if (tb[IFLA_ADDRESS]) {
1223                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1224                         return -EINVAL;
1225                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1226                         return -EADDRNOTAVAIL;
1227         }
1228         if (tb[IFLA_MTU]) {
1229                 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1230                         return -EINVAL;
1231         }
1232         return 0;
1233 }
1234
1235 static struct rtnl_link_ops veth_link_ops;
1236
1237 static int veth_newlink(struct net *src_net, struct net_device *dev,
1238                         struct nlattr *tb[], struct nlattr *data[],
1239                         struct netlink_ext_ack *extack)
1240 {
1241         int err;
1242         struct net_device *peer;
1243         struct veth_priv *priv;
1244         char ifname[IFNAMSIZ];
1245         struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1246         unsigned char name_assign_type;
1247         struct ifinfomsg *ifmp;
1248         struct net *net;
1249
1250         /*
1251          * create and register peer first
1252          */
1253         if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1254                 struct nlattr *nla_peer;
1255
1256                 nla_peer = data[VETH_INFO_PEER];
1257                 ifmp = nla_data(nla_peer);
1258                 err = rtnl_nla_parse_ifla(peer_tb,
1259                                           nla_data(nla_peer) + sizeof(struct ifinfomsg),
1260                                           nla_len(nla_peer) - sizeof(struct ifinfomsg),
1261                                           NULL);
1262                 if (err < 0)
1263                         return err;
1264
1265                 err = veth_validate(peer_tb, NULL, extack);
1266                 if (err < 0)
1267                         return err;
1268
1269                 tbp = peer_tb;
1270         } else {
1271                 ifmp = NULL;
1272                 tbp = tb;
1273         }
1274
1275         if (ifmp && tbp[IFLA_IFNAME]) {
1276                 nla_strlcpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1277                 name_assign_type = NET_NAME_USER;
1278         } else {
1279                 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1280                 name_assign_type = NET_NAME_ENUM;
1281         }
1282
1283         net = rtnl_link_get_net(src_net, tbp);
1284         if (IS_ERR(net))
1285                 return PTR_ERR(net);
1286
1287         peer = rtnl_create_link(net, ifname, name_assign_type,
1288                                 &veth_link_ops, tbp, extack);
1289         if (IS_ERR(peer)) {
1290                 put_net(net);
1291                 return PTR_ERR(peer);
1292         }
1293
1294         if (!ifmp || !tbp[IFLA_ADDRESS])
1295                 eth_hw_addr_random(peer);
1296
1297         if (ifmp && (dev->ifindex != 0))
1298                 peer->ifindex = ifmp->ifi_index;
1299
1300         peer->gso_max_size = dev->gso_max_size;
1301         peer->gso_max_segs = dev->gso_max_segs;
1302
1303         err = register_netdevice(peer);
1304         put_net(net);
1305         net = NULL;
1306         if (err < 0)
1307                 goto err_register_peer;
1308
1309         netif_carrier_off(peer);
1310
1311         err = rtnl_configure_link(peer, ifmp);
1312         if (err < 0)
1313                 goto err_configure_peer;
1314
1315         /*
1316          * register dev last
1317          *
1318          * note, that since we've registered new device the dev's name
1319          * should be re-allocated
1320          */
1321
1322         if (tb[IFLA_ADDRESS] == NULL)
1323                 eth_hw_addr_random(dev);
1324
1325         if (tb[IFLA_IFNAME])
1326                 nla_strlcpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1327         else
1328                 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1329
1330         err = register_netdevice(dev);
1331         if (err < 0)
1332                 goto err_register_dev;
1333
1334         netif_carrier_off(dev);
1335
1336         /*
1337          * tie the deviced together
1338          */
1339
1340         priv = netdev_priv(dev);
1341         rcu_assign_pointer(priv->peer, peer);
1342
1343         priv = netdev_priv(peer);
1344         rcu_assign_pointer(priv->peer, dev);
1345
1346         return 0;
1347
1348 err_register_dev:
1349         /* nothing to do */
1350 err_configure_peer:
1351         unregister_netdevice(peer);
1352         return err;
1353
1354 err_register_peer:
1355         free_netdev(peer);
1356         return err;
1357 }
1358
1359 static void veth_dellink(struct net_device *dev, struct list_head *head)
1360 {
1361         struct veth_priv *priv;
1362         struct net_device *peer;
1363
1364         priv = netdev_priv(dev);
1365         peer = rtnl_dereference(priv->peer);
1366
1367         /* Note : dellink() is called from default_device_exit_batch(),
1368          * before a rcu_synchronize() point. The devices are guaranteed
1369          * not being freed before one RCU grace period.
1370          */
1371         RCU_INIT_POINTER(priv->peer, NULL);
1372         unregister_netdevice_queue(dev, head);
1373
1374         if (peer) {
1375                 priv = netdev_priv(peer);
1376                 RCU_INIT_POINTER(priv->peer, NULL);
1377                 unregister_netdevice_queue(peer, head);
1378         }
1379 }
1380
1381 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1382         [VETH_INFO_PEER]        = { .len = sizeof(struct ifinfomsg) },
1383 };
1384
1385 static struct net *veth_get_link_net(const struct net_device *dev)
1386 {
1387         struct veth_priv *priv = netdev_priv(dev);
1388         struct net_device *peer = rtnl_dereference(priv->peer);
1389
1390         return peer ? dev_net(peer) : dev_net(dev);
1391 }
1392
1393 static struct rtnl_link_ops veth_link_ops = {
1394         .kind           = DRV_NAME,
1395         .priv_size      = sizeof(struct veth_priv),
1396         .setup          = veth_setup,
1397         .validate       = veth_validate,
1398         .newlink        = veth_newlink,
1399         .dellink        = veth_dellink,
1400         .policy         = veth_policy,
1401         .maxtype        = VETH_INFO_MAX,
1402         .get_link_net   = veth_get_link_net,
1403 };
1404
1405 /*
1406  * init/fini
1407  */
1408
1409 static __init int veth_init(void)
1410 {
1411         return rtnl_link_register(&veth_link_ops);
1412 }
1413
1414 static __exit void veth_exit(void)
1415 {
1416         rtnl_link_unregister(&veth_link_ops);
1417 }
1418
1419 module_init(veth_init);
1420 module_exit(veth_exit);
1421
1422 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1423 MODULE_LICENSE("GPL v2");
1424 MODULE_ALIAS_RTNL_LINK(DRV_NAME);