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