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