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