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