4 * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
6 * Author: Pavel Emelianov <xemul@openvz.org>
7 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
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>
17 #include <net/rtnetlink.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 #include <linux/net_tstamp.h>
29 #define DRV_NAME "veth"
30 #define DRV_VERSION "1.0"
32 #define VETH_XDP_FLAG BIT(0)
33 #define VETH_RING_SIZE 256
34 #define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
36 /* Separating two types of XDP xmit */
37 #define VETH_XDP_TX BIT(0)
38 #define VETH_XDP_REDIR BIT(1)
40 struct veth_rq_stats
{
44 struct u64_stats_sync syncp
;
48 struct napi_struct xdp_napi
;
49 struct net_device
*dev
;
50 struct bpf_prog __rcu
*xdp_prog
;
51 struct xdp_mem_info xdp_mem
;
52 struct veth_rq_stats stats
;
53 bool rx_notify_masked
;
54 struct ptr_ring xdp_ring
;
55 struct xdp_rxq_info xdp_rxq
;
59 struct net_device __rcu
*peer
;
61 struct bpf_prog
*_xdp_prog
;
63 unsigned int requested_headroom
;
70 struct veth_q_stat_desc
{
71 char desc
[ETH_GSTRING_LEN
];
75 #define VETH_RQ_STAT(m) offsetof(struct veth_rq_stats, m)
77 static const struct veth_q_stat_desc veth_rq_stats_desc
[] = {
78 { "xdp_packets", VETH_RQ_STAT(xdp_packets
) },
79 { "xdp_bytes", VETH_RQ_STAT(xdp_bytes
) },
80 { "xdp_drops", VETH_RQ_STAT(xdp_drops
) },
83 #define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
86 const char string
[ETH_GSTRING_LEN
];
87 } ethtool_stats_keys
[] = {
91 static int veth_get_link_ksettings(struct net_device
*dev
,
92 struct ethtool_link_ksettings
*cmd
)
94 cmd
->base
.speed
= SPEED_10000
;
95 cmd
->base
.duplex
= DUPLEX_FULL
;
96 cmd
->base
.port
= PORT_TP
;
97 cmd
->base
.autoneg
= AUTONEG_DISABLE
;
101 static void veth_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
103 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
104 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
107 static void veth_get_strings(struct net_device
*dev
, u32 stringset
, u8
*buf
)
109 char *p
= (char *)buf
;
114 memcpy(p
, ðtool_stats_keys
, sizeof(ethtool_stats_keys
));
115 p
+= sizeof(ethtool_stats_keys
);
116 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
117 for (j
= 0; j
< VETH_RQ_STATS_LEN
; j
++) {
118 snprintf(p
, ETH_GSTRING_LEN
, "rx_queue_%u_%s",
119 i
, veth_rq_stats_desc
[j
].desc
);
120 p
+= ETH_GSTRING_LEN
;
127 static int veth_get_sset_count(struct net_device
*dev
, int sset
)
131 return ARRAY_SIZE(ethtool_stats_keys
) +
132 VETH_RQ_STATS_LEN
* dev
->real_num_rx_queues
;
138 static void veth_get_ethtool_stats(struct net_device
*dev
,
139 struct ethtool_stats
*stats
, u64
*data
)
141 struct veth_priv
*priv
= netdev_priv(dev
);
142 struct net_device
*peer
= rtnl_dereference(priv
->peer
);
145 data
[0] = peer
? peer
->ifindex
: 0;
147 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
148 const struct veth_rq_stats
*rq_stats
= &priv
->rq
[i
].stats
;
149 const void *stats_base
= (void *)rq_stats
;
154 start
= u64_stats_fetch_begin_irq(&rq_stats
->syncp
);
155 for (j
= 0; j
< VETH_RQ_STATS_LEN
; j
++) {
156 offset
= veth_rq_stats_desc
[j
].offset
;
157 data
[idx
+ j
] = *(u64
*)(stats_base
+ offset
);
159 } while (u64_stats_fetch_retry_irq(&rq_stats
->syncp
, start
));
160 idx
+= VETH_RQ_STATS_LEN
;
164 static int veth_get_ts_info(struct net_device
*dev
,
165 struct ethtool_ts_info
*info
)
167 info
->so_timestamping
=
168 SOF_TIMESTAMPING_TX_SOFTWARE
|
169 SOF_TIMESTAMPING_RX_SOFTWARE
|
170 SOF_TIMESTAMPING_SOFTWARE
;
171 info
->phc_index
= -1;
176 static const struct ethtool_ops veth_ethtool_ops
= {
177 .get_drvinfo
= veth_get_drvinfo
,
178 .get_link
= ethtool_op_get_link
,
179 .get_strings
= veth_get_strings
,
180 .get_sset_count
= veth_get_sset_count
,
181 .get_ethtool_stats
= veth_get_ethtool_stats
,
182 .get_link_ksettings
= veth_get_link_ksettings
,
183 .get_ts_info
= veth_get_ts_info
,
186 /* general routines */
188 static bool veth_is_xdp_frame(void *ptr
)
190 return (unsigned long)ptr
& VETH_XDP_FLAG
;
193 static void *veth_ptr_to_xdp(void *ptr
)
195 return (void *)((unsigned long)ptr
& ~VETH_XDP_FLAG
);
198 static void *veth_xdp_to_ptr(void *ptr
)
200 return (void *)((unsigned long)ptr
| VETH_XDP_FLAG
);
203 static void veth_ptr_free(void *ptr
)
205 if (veth_is_xdp_frame(ptr
))
206 xdp_return_frame(veth_ptr_to_xdp(ptr
));
211 static void __veth_xdp_flush(struct veth_rq
*rq
)
213 /* Write ptr_ring before reading rx_notify_masked */
215 if (!rq
->rx_notify_masked
) {
216 rq
->rx_notify_masked
= true;
217 napi_schedule(&rq
->xdp_napi
);
221 static int veth_xdp_rx(struct veth_rq
*rq
, struct sk_buff
*skb
)
223 if (unlikely(ptr_ring_produce(&rq
->xdp_ring
, skb
))) {
224 dev_kfree_skb_any(skb
);
228 return NET_RX_SUCCESS
;
231 static int veth_forward_skb(struct net_device
*dev
, struct sk_buff
*skb
,
232 struct veth_rq
*rq
, bool xdp
)
234 return __dev_forward_skb(dev
, skb
) ?: xdp
?
235 veth_xdp_rx(rq
, skb
) :
239 static netdev_tx_t
veth_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
241 struct veth_priv
*rcv_priv
, *priv
= netdev_priv(dev
);
242 struct veth_rq
*rq
= NULL
;
243 struct net_device
*rcv
;
244 int length
= skb
->len
;
245 bool rcv_xdp
= false;
249 rcv
= rcu_dereference(priv
->peer
);
250 if (unlikely(!rcv
)) {
255 rcv_priv
= netdev_priv(rcv
);
256 rxq
= skb_get_queue_mapping(skb
);
257 if (rxq
< rcv
->real_num_rx_queues
) {
258 rq
= &rcv_priv
->rq
[rxq
];
259 rcv_xdp
= rcu_access_pointer(rq
->xdp_prog
);
261 skb_record_rx_queue(skb
, rxq
);
264 skb_tx_timestamp(skb
);
265 if (likely(veth_forward_skb(rcv
, skb
, rq
, rcv_xdp
) == NET_RX_SUCCESS
)) {
267 struct pcpu_lstats
*stats
= this_cpu_ptr(dev
->lstats
);
269 u64_stats_update_begin(&stats
->syncp
);
270 stats
->bytes
+= length
;
272 u64_stats_update_end(&stats
->syncp
);
276 atomic64_inc(&priv
->dropped
);
280 __veth_xdp_flush(rq
);
287 static u64
veth_stats_tx(struct pcpu_lstats
*result
, struct net_device
*dev
)
289 struct veth_priv
*priv
= netdev_priv(dev
);
294 for_each_possible_cpu(cpu
) {
295 struct pcpu_lstats
*stats
= per_cpu_ptr(dev
->lstats
, cpu
);
300 start
= u64_stats_fetch_begin_irq(&stats
->syncp
);
301 packets
= stats
->packets
;
302 bytes
= stats
->bytes
;
303 } while (u64_stats_fetch_retry_irq(&stats
->syncp
, start
));
304 result
->packets
+= packets
;
305 result
->bytes
+= bytes
;
307 return atomic64_read(&priv
->dropped
);
310 static void veth_stats_rx(struct veth_rq_stats
*result
, struct net_device
*dev
)
312 struct veth_priv
*priv
= netdev_priv(dev
);
315 result
->xdp_packets
= 0;
316 result
->xdp_bytes
= 0;
317 result
->xdp_drops
= 0;
318 for (i
= 0; i
< dev
->num_rx_queues
; i
++) {
319 struct veth_rq_stats
*stats
= &priv
->rq
[i
].stats
;
320 u64 packets
, bytes
, drops
;
324 start
= u64_stats_fetch_begin_irq(&stats
->syncp
);
325 packets
= stats
->xdp_packets
;
326 bytes
= stats
->xdp_bytes
;
327 drops
= stats
->xdp_drops
;
328 } while (u64_stats_fetch_retry_irq(&stats
->syncp
, start
));
329 result
->xdp_packets
+= packets
;
330 result
->xdp_bytes
+= bytes
;
331 result
->xdp_drops
+= drops
;
335 static void veth_get_stats64(struct net_device
*dev
,
336 struct rtnl_link_stats64
*tot
)
338 struct veth_priv
*priv
= netdev_priv(dev
);
339 struct net_device
*peer
;
340 struct veth_rq_stats rx
;
341 struct pcpu_lstats tx
;
343 tot
->tx_dropped
= veth_stats_tx(&tx
, dev
);
344 tot
->tx_bytes
= tx
.bytes
;
345 tot
->tx_packets
= tx
.packets
;
347 veth_stats_rx(&rx
, dev
);
348 tot
->rx_dropped
= rx
.xdp_drops
;
349 tot
->rx_bytes
= rx
.xdp_bytes
;
350 tot
->rx_packets
= rx
.xdp_packets
;
353 peer
= rcu_dereference(priv
->peer
);
355 tot
->rx_dropped
+= veth_stats_tx(&tx
, peer
);
356 tot
->rx_bytes
+= tx
.bytes
;
357 tot
->rx_packets
+= tx
.packets
;
359 veth_stats_rx(&rx
, peer
);
360 tot
->tx_bytes
+= rx
.xdp_bytes
;
361 tot
->tx_packets
+= rx
.xdp_packets
;
366 /* fake multicast ability */
367 static void veth_set_multicast_list(struct net_device
*dev
)
371 static struct sk_buff
*veth_build_skb(void *head
, int headroom
, int len
,
377 buflen
= SKB_DATA_ALIGN(headroom
+ len
) +
378 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
380 skb
= build_skb(head
, buflen
);
384 skb_reserve(skb
, headroom
);
390 static int veth_select_rxq(struct net_device
*dev
)
392 return smp_processor_id() % dev
->real_num_rx_queues
;
395 static int veth_xdp_xmit(struct net_device
*dev
, int n
,
396 struct xdp_frame
**frames
, u32 flags
)
398 struct veth_priv
*rcv_priv
, *priv
= netdev_priv(dev
);
399 struct net_device
*rcv
;
400 int i
, ret
, drops
= n
;
401 unsigned int max_len
;
404 if (unlikely(flags
& ~XDP_XMIT_FLAGS_MASK
)) {
409 rcv
= rcu_dereference(priv
->peer
);
410 if (unlikely(!rcv
)) {
415 rcv_priv
= netdev_priv(rcv
);
416 rq
= &rcv_priv
->rq
[veth_select_rxq(rcv
)];
417 /* Non-NULL xdp_prog ensures that xdp_ring is initialized on receive
418 * side. This means an XDP program is loaded on the peer and the peer
421 if (!rcu_access_pointer(rq
->xdp_prog
)) {
427 max_len
= rcv
->mtu
+ rcv
->hard_header_len
+ VLAN_HLEN
;
429 spin_lock(&rq
->xdp_ring
.producer_lock
);
430 for (i
= 0; i
< n
; i
++) {
431 struct xdp_frame
*frame
= frames
[i
];
432 void *ptr
= veth_xdp_to_ptr(frame
);
434 if (unlikely(frame
->len
> max_len
||
435 __ptr_ring_produce(&rq
->xdp_ring
, ptr
))) {
436 xdp_return_frame_rx_napi(frame
);
440 spin_unlock(&rq
->xdp_ring
.producer_lock
);
442 if (flags
& XDP_XMIT_FLUSH
)
443 __veth_xdp_flush(rq
);
450 atomic64_add(drops
, &priv
->dropped
);
455 static void veth_xdp_flush(struct net_device
*dev
)
457 struct veth_priv
*rcv_priv
, *priv
= netdev_priv(dev
);
458 struct net_device
*rcv
;
462 rcv
= rcu_dereference(priv
->peer
);
466 rcv_priv
= netdev_priv(rcv
);
467 rq
= &rcv_priv
->rq
[veth_select_rxq(rcv
)];
468 /* xdp_ring is initialized on receive side? */
469 if (unlikely(!rcu_access_pointer(rq
->xdp_prog
)))
472 __veth_xdp_flush(rq
);
477 static int veth_xdp_tx(struct net_device
*dev
, struct xdp_buff
*xdp
)
479 struct xdp_frame
*frame
= convert_to_xdp_frame(xdp
);
481 if (unlikely(!frame
))
484 return veth_xdp_xmit(dev
, 1, &frame
, 0);
487 static struct sk_buff
*veth_xdp_rcv_one(struct veth_rq
*rq
,
488 struct xdp_frame
*frame
,
489 unsigned int *xdp_xmit
)
491 void *hard_start
= frame
->data
- frame
->headroom
;
492 void *head
= hard_start
- sizeof(struct xdp_frame
);
493 int len
= frame
->len
, delta
= 0;
494 struct xdp_frame orig_frame
;
495 struct bpf_prog
*xdp_prog
;
496 unsigned int headroom
;
500 xdp_prog
= rcu_dereference(rq
->xdp_prog
);
501 if (likely(xdp_prog
)) {
505 xdp
.data_hard_start
= hard_start
;
506 xdp
.data
= frame
->data
;
507 xdp
.data_end
= frame
->data
+ frame
->len
;
508 xdp
.data_meta
= frame
->data
- frame
->metasize
;
509 xdp
.rxq
= &rq
->xdp_rxq
;
511 act
= bpf_prog_run_xdp(xdp_prog
, &xdp
);
515 delta
= frame
->data
- xdp
.data
;
516 len
= xdp
.data_end
- xdp
.data
;
520 xdp
.data_hard_start
= head
;
521 xdp
.rxq
->mem
= frame
->mem
;
522 if (unlikely(veth_xdp_tx(rq
->dev
, &xdp
) < 0)) {
523 trace_xdp_exception(rq
->dev
, xdp_prog
, act
);
527 *xdp_xmit
|= VETH_XDP_TX
;
532 xdp
.data_hard_start
= head
;
533 xdp
.rxq
->mem
= frame
->mem
;
534 if (xdp_do_redirect(rq
->dev
, &xdp
, xdp_prog
)) {
538 *xdp_xmit
|= VETH_XDP_REDIR
;
542 bpf_warn_invalid_xdp_action(act
);
544 trace_xdp_exception(rq
->dev
, xdp_prog
, act
);
551 headroom
= sizeof(struct xdp_frame
) + frame
->headroom
- delta
;
552 skb
= veth_build_skb(head
, headroom
, len
, 0);
554 xdp_return_frame(frame
);
558 xdp_scrub_frame(frame
);
559 skb
->protocol
= eth_type_trans(skb
, rq
->dev
);
564 xdp_return_frame(frame
);
569 static struct sk_buff
*veth_xdp_rcv_skb(struct veth_rq
*rq
, struct sk_buff
*skb
,
570 unsigned int *xdp_xmit
)
572 u32 pktlen
, headroom
, act
, metalen
;
573 void *orig_data
, *orig_data_end
;
574 struct bpf_prog
*xdp_prog
;
575 int mac_len
, delta
, off
;
581 xdp_prog
= rcu_dereference(rq
->xdp_prog
);
582 if (unlikely(!xdp_prog
)) {
587 mac_len
= skb
->data
- skb_mac_header(skb
);
588 pktlen
= skb
->len
+ mac_len
;
589 headroom
= skb_headroom(skb
) - mac_len
;
591 if (skb_shared(skb
) || skb_head_is_locked(skb
) ||
592 skb_is_nonlinear(skb
) || headroom
< XDP_PACKET_HEADROOM
) {
593 struct sk_buff
*nskb
;
598 size
= SKB_DATA_ALIGN(VETH_XDP_HEADROOM
+ pktlen
) +
599 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
600 if (size
> PAGE_SIZE
)
603 page
= alloc_page(GFP_ATOMIC
| __GFP_NOWARN
);
607 head
= page_address(page
);
608 start
= head
+ VETH_XDP_HEADROOM
;
609 if (skb_copy_bits(skb
, -mac_len
, start
, pktlen
)) {
610 page_frag_free(head
);
614 nskb
= veth_build_skb(head
,
615 VETH_XDP_HEADROOM
+ mac_len
, skb
->len
,
618 page_frag_free(head
);
622 skb_copy_header(nskb
, skb
);
623 head_off
= skb_headroom(nskb
) - skb_headroom(skb
);
624 skb_headers_offset_update(nskb
, head_off
);
629 xdp
.data_hard_start
= skb
->head
;
630 xdp
.data
= skb_mac_header(skb
);
631 xdp
.data_end
= xdp
.data
+ pktlen
;
632 xdp
.data_meta
= xdp
.data
;
633 xdp
.rxq
= &rq
->xdp_rxq
;
634 orig_data
= xdp
.data
;
635 orig_data_end
= xdp
.data_end
;
637 act
= bpf_prog_run_xdp(xdp_prog
, &xdp
);
643 get_page(virt_to_page(xdp
.data
));
645 xdp
.rxq
->mem
= rq
->xdp_mem
;
646 if (unlikely(veth_xdp_tx(rq
->dev
, &xdp
) < 0)) {
647 trace_xdp_exception(rq
->dev
, xdp_prog
, act
);
650 *xdp_xmit
|= VETH_XDP_TX
;
654 get_page(virt_to_page(xdp
.data
));
656 xdp
.rxq
->mem
= rq
->xdp_mem
;
657 if (xdp_do_redirect(rq
->dev
, &xdp
, xdp_prog
))
659 *xdp_xmit
|= VETH_XDP_REDIR
;
663 bpf_warn_invalid_xdp_action(act
);
665 trace_xdp_exception(rq
->dev
, xdp_prog
, act
);
671 delta
= orig_data
- xdp
.data
;
672 off
= mac_len
+ delta
;
674 __skb_push(skb
, off
);
676 __skb_pull(skb
, -off
);
677 skb
->mac_header
-= delta
;
678 off
= xdp
.data_end
- orig_data_end
;
681 skb
->protocol
= eth_type_trans(skb
, rq
->dev
);
683 metalen
= xdp
.data
- xdp
.data_meta
;
685 skb_metadata_set(skb
, metalen
);
694 page_frag_free(xdp
.data
);
699 static int veth_xdp_rcv(struct veth_rq
*rq
, int budget
, unsigned int *xdp_xmit
)
701 int i
, done
= 0, drops
= 0, bytes
= 0;
703 for (i
= 0; i
< budget
; i
++) {
704 void *ptr
= __ptr_ring_consume(&rq
->xdp_ring
);
705 unsigned int xdp_xmit_one
= 0;
711 if (veth_is_xdp_frame(ptr
)) {
712 struct xdp_frame
*frame
= veth_ptr_to_xdp(ptr
);
715 skb
= veth_xdp_rcv_one(rq
, frame
, &xdp_xmit_one
);
719 skb
= veth_xdp_rcv_skb(rq
, skb
, &xdp_xmit_one
);
721 *xdp_xmit
|= xdp_xmit_one
;
724 napi_gro_receive(&rq
->xdp_napi
, skb
);
725 else if (!xdp_xmit_one
)
731 u64_stats_update_begin(&rq
->stats
.syncp
);
732 rq
->stats
.xdp_packets
+= done
;
733 rq
->stats
.xdp_bytes
+= bytes
;
734 rq
->stats
.xdp_drops
+= drops
;
735 u64_stats_update_end(&rq
->stats
.syncp
);
740 static int veth_poll(struct napi_struct
*napi
, int budget
)
743 container_of(napi
, struct veth_rq
, xdp_napi
);
744 unsigned int xdp_xmit
= 0;
747 xdp_set_return_frame_no_direct();
748 done
= veth_xdp_rcv(rq
, budget
, &xdp_xmit
);
750 if (done
< budget
&& napi_complete_done(napi
, done
)) {
751 /* Write rx_notify_masked before reading ptr_ring */
752 smp_store_mb(rq
->rx_notify_masked
, false);
753 if (unlikely(!__ptr_ring_empty(&rq
->xdp_ring
))) {
754 rq
->rx_notify_masked
= true;
755 napi_schedule(&rq
->xdp_napi
);
759 if (xdp_xmit
& VETH_XDP_TX
)
760 veth_xdp_flush(rq
->dev
);
761 if (xdp_xmit
& VETH_XDP_REDIR
)
763 xdp_clear_return_frame_no_direct();
768 static int veth_napi_add(struct net_device
*dev
)
770 struct veth_priv
*priv
= netdev_priv(dev
);
773 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
774 struct veth_rq
*rq
= &priv
->rq
[i
];
776 err
= ptr_ring_init(&rq
->xdp_ring
, VETH_RING_SIZE
, GFP_KERNEL
);
781 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
782 struct veth_rq
*rq
= &priv
->rq
[i
];
784 netif_napi_add(dev
, &rq
->xdp_napi
, veth_poll
, NAPI_POLL_WEIGHT
);
785 napi_enable(&rq
->xdp_napi
);
790 for (i
--; i
>= 0; i
--)
791 ptr_ring_cleanup(&priv
->rq
[i
].xdp_ring
, veth_ptr_free
);
796 static void veth_napi_del(struct net_device
*dev
)
798 struct veth_priv
*priv
= netdev_priv(dev
);
801 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
802 struct veth_rq
*rq
= &priv
->rq
[i
];
804 napi_disable(&rq
->xdp_napi
);
805 napi_hash_del(&rq
->xdp_napi
);
809 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
810 struct veth_rq
*rq
= &priv
->rq
[i
];
812 netif_napi_del(&rq
->xdp_napi
);
813 rq
->rx_notify_masked
= false;
814 ptr_ring_cleanup(&rq
->xdp_ring
, veth_ptr_free
);
818 static int veth_enable_xdp(struct net_device
*dev
)
820 struct veth_priv
*priv
= netdev_priv(dev
);
823 if (!xdp_rxq_info_is_reg(&priv
->rq
[0].xdp_rxq
)) {
824 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
825 struct veth_rq
*rq
= &priv
->rq
[i
];
827 err
= xdp_rxq_info_reg(&rq
->xdp_rxq
, dev
, i
);
831 err
= xdp_rxq_info_reg_mem_model(&rq
->xdp_rxq
,
832 MEM_TYPE_PAGE_SHARED
,
837 /* Save original mem info as it can be overwritten */
838 rq
->xdp_mem
= rq
->xdp_rxq
.mem
;
841 err
= veth_napi_add(dev
);
846 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++)
847 rcu_assign_pointer(priv
->rq
[i
].xdp_prog
, priv
->_xdp_prog
);
851 xdp_rxq_info_unreg(&priv
->rq
[i
].xdp_rxq
);
853 for (i
--; i
>= 0; i
--)
854 xdp_rxq_info_unreg(&priv
->rq
[i
].xdp_rxq
);
859 static void veth_disable_xdp(struct net_device
*dev
)
861 struct veth_priv
*priv
= netdev_priv(dev
);
864 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++)
865 rcu_assign_pointer(priv
->rq
[i
].xdp_prog
, NULL
);
867 for (i
= 0; i
< dev
->real_num_rx_queues
; i
++) {
868 struct veth_rq
*rq
= &priv
->rq
[i
];
870 rq
->xdp_rxq
.mem
= rq
->xdp_mem
;
871 xdp_rxq_info_unreg(&rq
->xdp_rxq
);
875 static int veth_open(struct net_device
*dev
)
877 struct veth_priv
*priv
= netdev_priv(dev
);
878 struct net_device
*peer
= rtnl_dereference(priv
->peer
);
884 if (priv
->_xdp_prog
) {
885 err
= veth_enable_xdp(dev
);
890 if (peer
->flags
& IFF_UP
) {
891 netif_carrier_on(dev
);
892 netif_carrier_on(peer
);
898 static int veth_close(struct net_device
*dev
)
900 struct veth_priv
*priv
= netdev_priv(dev
);
901 struct net_device
*peer
= rtnl_dereference(priv
->peer
);
903 netif_carrier_off(dev
);
905 netif_carrier_off(peer
);
908 veth_disable_xdp(dev
);
913 static int is_valid_veth_mtu(int mtu
)
915 return mtu
>= ETH_MIN_MTU
&& mtu
<= ETH_MAX_MTU
;
918 static int veth_alloc_queues(struct net_device
*dev
)
920 struct veth_priv
*priv
= netdev_priv(dev
);
923 priv
->rq
= kcalloc(dev
->num_rx_queues
, sizeof(*priv
->rq
), GFP_KERNEL
);
927 for (i
= 0; i
< dev
->num_rx_queues
; i
++) {
928 priv
->rq
[i
].dev
= dev
;
929 u64_stats_init(&priv
->rq
[i
].stats
.syncp
);
935 static void veth_free_queues(struct net_device
*dev
)
937 struct veth_priv
*priv
= netdev_priv(dev
);
942 static int veth_dev_init(struct net_device
*dev
)
946 dev
->lstats
= netdev_alloc_pcpu_stats(struct pcpu_lstats
);
950 err
= veth_alloc_queues(dev
);
952 free_percpu(dev
->lstats
);
959 static void veth_dev_free(struct net_device
*dev
)
961 veth_free_queues(dev
);
962 free_percpu(dev
->lstats
);
965 #ifdef CONFIG_NET_POLL_CONTROLLER
966 static void veth_poll_controller(struct net_device
*dev
)
968 /* veth only receives frames when its peer sends one
969 * Since it has nothing to do with disabling irqs, we are guaranteed
970 * never to have pending data when we poll for it so
971 * there is nothing to do here.
973 * We need this though so netpoll recognizes us as an interface that
974 * supports polling, which enables bridge devices in virt setups to
975 * still use netconsole
978 #endif /* CONFIG_NET_POLL_CONTROLLER */
980 static int veth_get_iflink(const struct net_device
*dev
)
982 struct veth_priv
*priv
= netdev_priv(dev
);
983 struct net_device
*peer
;
987 peer
= rcu_dereference(priv
->peer
);
988 iflink
= peer
? peer
->ifindex
: 0;
994 static netdev_features_t
veth_fix_features(struct net_device
*dev
,
995 netdev_features_t features
)
997 struct veth_priv
*priv
= netdev_priv(dev
);
998 struct net_device
*peer
;
1000 peer
= rtnl_dereference(priv
->peer
);
1002 struct veth_priv
*peer_priv
= netdev_priv(peer
);
1004 if (peer_priv
->_xdp_prog
)
1005 features
&= ~NETIF_F_GSO_SOFTWARE
;
1011 static void veth_set_rx_headroom(struct net_device
*dev
, int new_hr
)
1013 struct veth_priv
*peer_priv
, *priv
= netdev_priv(dev
);
1014 struct net_device
*peer
;
1020 peer
= rcu_dereference(priv
->peer
);
1021 if (unlikely(!peer
))
1024 peer_priv
= netdev_priv(peer
);
1025 priv
->requested_headroom
= new_hr
;
1026 new_hr
= max(priv
->requested_headroom
, peer_priv
->requested_headroom
);
1027 dev
->needed_headroom
= new_hr
;
1028 peer
->needed_headroom
= new_hr
;
1034 static int veth_xdp_set(struct net_device
*dev
, struct bpf_prog
*prog
,
1035 struct netlink_ext_ack
*extack
)
1037 struct veth_priv
*priv
= netdev_priv(dev
);
1038 struct bpf_prog
*old_prog
;
1039 struct net_device
*peer
;
1040 unsigned int max_mtu
;
1043 old_prog
= priv
->_xdp_prog
;
1044 priv
->_xdp_prog
= prog
;
1045 peer
= rtnl_dereference(priv
->peer
);
1049 NL_SET_ERR_MSG_MOD(extack
, "Cannot set XDP when peer is detached");
1054 max_mtu
= PAGE_SIZE
- VETH_XDP_HEADROOM
-
1055 peer
->hard_header_len
-
1056 SKB_DATA_ALIGN(sizeof(struct skb_shared_info
));
1057 if (peer
->mtu
> max_mtu
) {
1058 NL_SET_ERR_MSG_MOD(extack
, "Peer MTU is too large to set XDP");
1063 if (dev
->real_num_rx_queues
< peer
->real_num_tx_queues
) {
1064 NL_SET_ERR_MSG_MOD(extack
, "XDP expects number of rx queues not less than peer tx queues");
1069 if (dev
->flags
& IFF_UP
) {
1070 err
= veth_enable_xdp(dev
);
1072 NL_SET_ERR_MSG_MOD(extack
, "Setup for XDP failed");
1078 peer
->hw_features
&= ~NETIF_F_GSO_SOFTWARE
;
1079 peer
->max_mtu
= max_mtu
;
1085 if (dev
->flags
& IFF_UP
)
1086 veth_disable_xdp(dev
);
1089 peer
->hw_features
|= NETIF_F_GSO_SOFTWARE
;
1090 peer
->max_mtu
= ETH_MAX_MTU
;
1093 bpf_prog_put(old_prog
);
1096 if ((!!old_prog
^ !!prog
) && peer
)
1097 netdev_update_features(peer
);
1101 priv
->_xdp_prog
= old_prog
;
1106 static u32
veth_xdp_query(struct net_device
*dev
)
1108 struct veth_priv
*priv
= netdev_priv(dev
);
1109 const struct bpf_prog
*xdp_prog
;
1111 xdp_prog
= priv
->_xdp_prog
;
1113 return xdp_prog
->aux
->id
;
1118 static int veth_xdp(struct net_device
*dev
, struct netdev_bpf
*xdp
)
1120 switch (xdp
->command
) {
1121 case XDP_SETUP_PROG
:
1122 return veth_xdp_set(dev
, xdp
->prog
, xdp
->extack
);
1123 case XDP_QUERY_PROG
:
1124 xdp
->prog_id
= veth_xdp_query(dev
);
1131 static const struct net_device_ops veth_netdev_ops
= {
1132 .ndo_init
= veth_dev_init
,
1133 .ndo_open
= veth_open
,
1134 .ndo_stop
= veth_close
,
1135 .ndo_start_xmit
= veth_xmit
,
1136 .ndo_get_stats64
= veth_get_stats64
,
1137 .ndo_set_rx_mode
= veth_set_multicast_list
,
1138 .ndo_set_mac_address
= eth_mac_addr
,
1139 #ifdef CONFIG_NET_POLL_CONTROLLER
1140 .ndo_poll_controller
= veth_poll_controller
,
1142 .ndo_get_iflink
= veth_get_iflink
,
1143 .ndo_fix_features
= veth_fix_features
,
1144 .ndo_features_check
= passthru_features_check
,
1145 .ndo_set_rx_headroom
= veth_set_rx_headroom
,
1146 .ndo_bpf
= veth_xdp
,
1147 .ndo_xdp_xmit
= veth_xdp_xmit
,
1150 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1151 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1152 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1153 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1154 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1156 static void veth_setup(struct net_device
*dev
)
1160 dev
->priv_flags
&= ~IFF_TX_SKB_SHARING
;
1161 dev
->priv_flags
|= IFF_LIVE_ADDR_CHANGE
;
1162 dev
->priv_flags
|= IFF_NO_QUEUE
;
1163 dev
->priv_flags
|= IFF_PHONY_HEADROOM
;
1165 dev
->netdev_ops
= &veth_netdev_ops
;
1166 dev
->ethtool_ops
= &veth_ethtool_ops
;
1167 dev
->features
|= NETIF_F_LLTX
;
1168 dev
->features
|= VETH_FEATURES
;
1169 dev
->vlan_features
= dev
->features
&
1170 ~(NETIF_F_HW_VLAN_CTAG_TX
|
1171 NETIF_F_HW_VLAN_STAG_TX
|
1172 NETIF_F_HW_VLAN_CTAG_RX
|
1173 NETIF_F_HW_VLAN_STAG_RX
);
1174 dev
->needs_free_netdev
= true;
1175 dev
->priv_destructor
= veth_dev_free
;
1176 dev
->max_mtu
= ETH_MAX_MTU
;
1178 dev
->hw_features
= VETH_FEATURES
;
1179 dev
->hw_enc_features
= VETH_FEATURES
;
1180 dev
->mpls_features
= NETIF_F_HW_CSUM
| NETIF_F_GSO_SOFTWARE
;
1187 static int veth_validate(struct nlattr
*tb
[], struct nlattr
*data
[],
1188 struct netlink_ext_ack
*extack
)
1190 if (tb
[IFLA_ADDRESS
]) {
1191 if (nla_len(tb
[IFLA_ADDRESS
]) != ETH_ALEN
)
1193 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
1194 return -EADDRNOTAVAIL
;
1197 if (!is_valid_veth_mtu(nla_get_u32(tb
[IFLA_MTU
])))
1203 static struct rtnl_link_ops veth_link_ops
;
1205 static int veth_newlink(struct net
*src_net
, struct net_device
*dev
,
1206 struct nlattr
*tb
[], struct nlattr
*data
[],
1207 struct netlink_ext_ack
*extack
)
1210 struct net_device
*peer
;
1211 struct veth_priv
*priv
;
1212 char ifname
[IFNAMSIZ
];
1213 struct nlattr
*peer_tb
[IFLA_MAX
+ 1], **tbp
;
1214 unsigned char name_assign_type
;
1215 struct ifinfomsg
*ifmp
;
1219 * create and register peer first
1221 if (data
!= NULL
&& data
[VETH_INFO_PEER
] != NULL
) {
1222 struct nlattr
*nla_peer
;
1224 nla_peer
= data
[VETH_INFO_PEER
];
1225 ifmp
= nla_data(nla_peer
);
1226 err
= rtnl_nla_parse_ifla(peer_tb
,
1227 nla_data(nla_peer
) + sizeof(struct ifinfomsg
),
1228 nla_len(nla_peer
) - sizeof(struct ifinfomsg
),
1233 err
= veth_validate(peer_tb
, NULL
, extack
);
1243 if (ifmp
&& tbp
[IFLA_IFNAME
]) {
1244 nla_strlcpy(ifname
, tbp
[IFLA_IFNAME
], IFNAMSIZ
);
1245 name_assign_type
= NET_NAME_USER
;
1247 snprintf(ifname
, IFNAMSIZ
, DRV_NAME
"%%d");
1248 name_assign_type
= NET_NAME_ENUM
;
1251 net
= rtnl_link_get_net(src_net
, tbp
);
1253 return PTR_ERR(net
);
1255 peer
= rtnl_create_link(net
, ifname
, name_assign_type
,
1256 &veth_link_ops
, tbp
, extack
);
1259 return PTR_ERR(peer
);
1262 if (!ifmp
|| !tbp
[IFLA_ADDRESS
])
1263 eth_hw_addr_random(peer
);
1265 if (ifmp
&& (dev
->ifindex
!= 0))
1266 peer
->ifindex
= ifmp
->ifi_index
;
1268 peer
->gso_max_size
= dev
->gso_max_size
;
1269 peer
->gso_max_segs
= dev
->gso_max_segs
;
1271 err
= register_netdevice(peer
);
1275 goto err_register_peer
;
1277 netif_carrier_off(peer
);
1279 err
= rtnl_configure_link(peer
, ifmp
);
1281 goto err_configure_peer
;
1286 * note, that since we've registered new device the dev's name
1287 * should be re-allocated
1290 if (tb
[IFLA_ADDRESS
] == NULL
)
1291 eth_hw_addr_random(dev
);
1293 if (tb
[IFLA_IFNAME
])
1294 nla_strlcpy(dev
->name
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1296 snprintf(dev
->name
, IFNAMSIZ
, DRV_NAME
"%%d");
1298 err
= register_netdevice(dev
);
1300 goto err_register_dev
;
1302 netif_carrier_off(dev
);
1305 * tie the deviced together
1308 priv
= netdev_priv(dev
);
1309 rcu_assign_pointer(priv
->peer
, peer
);
1311 priv
= netdev_priv(peer
);
1312 rcu_assign_pointer(priv
->peer
, dev
);
1319 unregister_netdevice(peer
);
1327 static void veth_dellink(struct net_device
*dev
, struct list_head
*head
)
1329 struct veth_priv
*priv
;
1330 struct net_device
*peer
;
1332 priv
= netdev_priv(dev
);
1333 peer
= rtnl_dereference(priv
->peer
);
1335 /* Note : dellink() is called from default_device_exit_batch(),
1336 * before a rcu_synchronize() point. The devices are guaranteed
1337 * not being freed before one RCU grace period.
1339 RCU_INIT_POINTER(priv
->peer
, NULL
);
1340 unregister_netdevice_queue(dev
, head
);
1343 priv
= netdev_priv(peer
);
1344 RCU_INIT_POINTER(priv
->peer
, NULL
);
1345 unregister_netdevice_queue(peer
, head
);
1349 static const struct nla_policy veth_policy
[VETH_INFO_MAX
+ 1] = {
1350 [VETH_INFO_PEER
] = { .len
= sizeof(struct ifinfomsg
) },
1353 static struct net
*veth_get_link_net(const struct net_device
*dev
)
1355 struct veth_priv
*priv
= netdev_priv(dev
);
1356 struct net_device
*peer
= rtnl_dereference(priv
->peer
);
1358 return peer
? dev_net(peer
) : dev_net(dev
);
1361 static struct rtnl_link_ops veth_link_ops
= {
1363 .priv_size
= sizeof(struct veth_priv
),
1364 .setup
= veth_setup
,
1365 .validate
= veth_validate
,
1366 .newlink
= veth_newlink
,
1367 .dellink
= veth_dellink
,
1368 .policy
= veth_policy
,
1369 .maxtype
= VETH_INFO_MAX
,
1370 .get_link_net
= veth_get_link_net
,
1377 static __init
int veth_init(void)
1379 return rtnl_link_register(&veth_link_ops
);
1382 static __exit
void veth_exit(void)
1384 rtnl_link_unregister(&veth_link_ops
);
1387 module_init(veth_init
);
1388 module_exit(veth_exit
);
1390 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1391 MODULE_LICENSE("GPL v2");
1392 MODULE_ALIAS_RTNL_LINK(DRV_NAME
);