1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * drivers/net/veth.c
4 *
5 * Copyright (C) 2007 OpenVZ http://openvz.org, SWsoft Inc
6 *
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 * Ethtool interface from: Eric W. Biederman <ebiederm@xmission.com>
9 *
10 */
11
12 #include <linux/netdevice.h>
13 #include <linux/slab.h>
14 #include <linux/ethtool.h>
15 #include <linux/etherdevice.h>
16 #include <linux/u64_stats_sync.h>
17
18 #include <net/rtnetlink.h>
19 #include <net/dst.h>
20 #include <net/xfrm.h>
21 #include <net/xdp.h>
22 #include <linux/veth.h>
23 #include <linux/module.h>
24 #include <linux/bpf.h>
25 #include <linux/filter.h>
26 #include <linux/ptr_ring.h>
27 #include <linux/bpf_trace.h>
28 #include <linux/net_tstamp.h>
29
30 #define DRV_NAME "veth"
31 #define DRV_VERSION "1.0"
32
33 #define VETH_XDP_FLAG BIT(0)
34 #define VETH_RING_SIZE 256
35 #define VETH_XDP_HEADROOM (XDP_PACKET_HEADROOM + NET_IP_ALIGN)
36
37 #define VETH_XDP_TX_BULK_SIZE 16
38 #define VETH_XDP_BATCH 16
39
40 struct veth_stats {
41 u64 rx_drops;
42 /* xdp */
43 u64 xdp_packets;
44 u64 xdp_bytes;
45 u64 xdp_redirect;
46 u64 xdp_drops;
47 u64 xdp_tx;
48 u64 xdp_tx_err;
49 u64 peer_tq_xdp_xmit;
50 u64 peer_tq_xdp_xmit_err;
51 };
52
53 struct veth_rq_stats {
54 struct veth_stats vs;
55 struct u64_stats_sync syncp;
56 };
57
58 struct veth_rq {
59 struct napi_struct xdp_napi;
60 struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
61 struct net_device *dev;
62 struct bpf_prog __rcu *xdp_prog;
63 struct xdp_mem_info xdp_mem;
64 struct veth_rq_stats stats;
65 bool rx_notify_masked;
66 struct ptr_ring xdp_ring;
67 struct xdp_rxq_info xdp_rxq;
68 };
69
70 struct veth_priv {
71 struct net_device __rcu *peer;
72 atomic64_t dropped;
73 struct bpf_prog *_xdp_prog;
74 struct veth_rq *rq;
75 unsigned int requested_headroom;
76 };
77
78 struct veth_xdp_tx_bq {
79 struct xdp_frame *q[VETH_XDP_TX_BULK_SIZE];
80 unsigned int count;
81 };
82
83 /*
84 * ethtool interface
85 */
86
87 struct veth_q_stat_desc {
88 char desc[ETH_GSTRING_LEN];
89 size_t offset;
90 };
91
92 #define VETH_RQ_STAT(m) offsetof(struct veth_stats, m)
93
94 static const struct veth_q_stat_desc veth_rq_stats_desc[] = {
95 { "xdp_packets", VETH_RQ_STAT(xdp_packets) },
96 { "xdp_bytes", VETH_RQ_STAT(xdp_bytes) },
97 { "drops", VETH_RQ_STAT(rx_drops) },
98 { "xdp_redirect", VETH_RQ_STAT(xdp_redirect) },
99 { "xdp_drops", VETH_RQ_STAT(xdp_drops) },
100 { "xdp_tx", VETH_RQ_STAT(xdp_tx) },
101 { "xdp_tx_errors", VETH_RQ_STAT(xdp_tx_err) },
102 };
103
104 #define VETH_RQ_STATS_LEN ARRAY_SIZE(veth_rq_stats_desc)
105
106 static const struct veth_q_stat_desc veth_tq_stats_desc[] = {
107 { "xdp_xmit", VETH_RQ_STAT(peer_tq_xdp_xmit) },
108 { "xdp_xmit_errors", VETH_RQ_STAT(peer_tq_xdp_xmit_err) },
109 };
110
111 #define VETH_TQ_STATS_LEN ARRAY_SIZE(veth_tq_stats_desc)
112
113 static struct {
114 const char string[ETH_GSTRING_LEN];
115 } ethtool_stats_keys[] = {
116 { "peer_ifindex" },
117 };
118
veth_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)119 static int veth_get_link_ksettings(struct net_device *dev,
120 struct ethtool_link_ksettings *cmd)
121 {
122 cmd->base.speed = SPEED_10000;
123 cmd->base.duplex = DUPLEX_FULL;
124 cmd->base.port = PORT_TP;
125 cmd->base.autoneg = AUTONEG_DISABLE;
126 return 0;
127 }
128
veth_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)129 static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
130 {
131 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
132 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
133 }
134
veth_get_strings(struct net_device * dev,u32 stringset,u8 * buf)135 static void veth_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
136 {
137 char *p = (char *)buf;
138 int i, j;
139
140 switch(stringset) {
141 case ETH_SS_STATS:
142 memcpy(p, ðtool_stats_keys, sizeof(ethtool_stats_keys));
143 p += sizeof(ethtool_stats_keys);
144 for (i = 0; i < dev->real_num_rx_queues; i++) {
145 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
146 snprintf(p, ETH_GSTRING_LEN,
147 "rx_queue_%u_%.18s",
148 i, veth_rq_stats_desc[j].desc);
149 p += ETH_GSTRING_LEN;
150 }
151 }
152 for (i = 0; i < dev->real_num_tx_queues; i++) {
153 for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
154 snprintf(p, ETH_GSTRING_LEN,
155 "tx_queue_%u_%.18s",
156 i, veth_tq_stats_desc[j].desc);
157 p += ETH_GSTRING_LEN;
158 }
159 }
160 break;
161 }
162 }
163
veth_get_sset_count(struct net_device * dev,int sset)164 static int veth_get_sset_count(struct net_device *dev, int sset)
165 {
166 switch (sset) {
167 case ETH_SS_STATS:
168 return ARRAY_SIZE(ethtool_stats_keys) +
169 VETH_RQ_STATS_LEN * dev->real_num_rx_queues +
170 VETH_TQ_STATS_LEN * dev->real_num_tx_queues;
171 default:
172 return -EOPNOTSUPP;
173 }
174 }
175
veth_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)176 static void veth_get_ethtool_stats(struct net_device *dev,
177 struct ethtool_stats *stats, u64 *data)
178 {
179 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
180 struct net_device *peer = rtnl_dereference(priv->peer);
181 int i, j, idx;
182
183 data[0] = peer ? peer->ifindex : 0;
184 idx = 1;
185 for (i = 0; i < dev->real_num_rx_queues; i++) {
186 const struct veth_rq_stats *rq_stats = &priv->rq[i].stats;
187 const void *stats_base = (void *)&rq_stats->vs;
188 unsigned int start;
189 size_t offset;
190
191 do {
192 start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
193 for (j = 0; j < VETH_RQ_STATS_LEN; j++) {
194 offset = veth_rq_stats_desc[j].offset;
195 data[idx + j] = *(u64 *)(stats_base + offset);
196 }
197 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
198 idx += VETH_RQ_STATS_LEN;
199 }
200
201 if (!peer)
202 return;
203
204 rcv_priv = netdev_priv(peer);
205 for (i = 0; i < peer->real_num_rx_queues; i++) {
206 const struct veth_rq_stats *rq_stats = &rcv_priv->rq[i].stats;
207 const void *base = (void *)&rq_stats->vs;
208 unsigned int start, tx_idx = idx;
209 size_t offset;
210
211 tx_idx += (i % dev->real_num_tx_queues) * VETH_TQ_STATS_LEN;
212 do {
213 start = u64_stats_fetch_begin_irq(&rq_stats->syncp);
214 for (j = 0; j < VETH_TQ_STATS_LEN; j++) {
215 offset = veth_tq_stats_desc[j].offset;
216 data[tx_idx + j] += *(u64 *)(base + offset);
217 }
218 } while (u64_stats_fetch_retry_irq(&rq_stats->syncp, start));
219 }
220 }
221
veth_get_channels(struct net_device * dev,struct ethtool_channels * channels)222 static void veth_get_channels(struct net_device *dev,
223 struct ethtool_channels *channels)
224 {
225 channels->tx_count = dev->real_num_tx_queues;
226 channels->rx_count = dev->real_num_rx_queues;
227 channels->max_tx = dev->num_tx_queues;
228 channels->max_rx = dev->num_rx_queues;
229 }
230
231 static int veth_set_channels(struct net_device *dev,
232 struct ethtool_channels *ch);
233
234 static const struct ethtool_ops veth_ethtool_ops = {
235 .get_drvinfo = veth_get_drvinfo,
236 .get_link = ethtool_op_get_link,
237 .get_strings = veth_get_strings,
238 .get_sset_count = veth_get_sset_count,
239 .get_ethtool_stats = veth_get_ethtool_stats,
240 .get_link_ksettings = veth_get_link_ksettings,
241 .get_ts_info = ethtool_op_get_ts_info,
242 .get_channels = veth_get_channels,
243 .set_channels = veth_set_channels,
244 };
245
246 /* general routines */
247
veth_is_xdp_frame(void * ptr)248 static bool veth_is_xdp_frame(void *ptr)
249 {
250 return (unsigned long)ptr & VETH_XDP_FLAG;
251 }
252
veth_ptr_to_xdp(void * ptr)253 static struct xdp_frame *veth_ptr_to_xdp(void *ptr)
254 {
255 return (void *)((unsigned long)ptr & ~VETH_XDP_FLAG);
256 }
257
veth_xdp_to_ptr(struct xdp_frame * xdp)258 static void *veth_xdp_to_ptr(struct xdp_frame *xdp)
259 {
260 return (void *)((unsigned long)xdp | VETH_XDP_FLAG);
261 }
262
veth_ptr_free(void * ptr)263 static void veth_ptr_free(void *ptr)
264 {
265 if (veth_is_xdp_frame(ptr))
266 xdp_return_frame(veth_ptr_to_xdp(ptr));
267 else
268 kfree_skb(ptr);
269 }
270
__veth_xdp_flush(struct veth_rq * rq)271 static void __veth_xdp_flush(struct veth_rq *rq)
272 {
273 /* Write ptr_ring before reading rx_notify_masked */
274 smp_mb();
275 if (!READ_ONCE(rq->rx_notify_masked) &&
276 napi_schedule_prep(&rq->xdp_napi)) {
277 WRITE_ONCE(rq->rx_notify_masked, true);
278 __napi_schedule(&rq->xdp_napi);
279 }
280 }
281
veth_xdp_rx(struct veth_rq * rq,struct sk_buff * skb)282 static int veth_xdp_rx(struct veth_rq *rq, struct sk_buff *skb)
283 {
284 if (unlikely(ptr_ring_produce(&rq->xdp_ring, skb))) {
285 dev_kfree_skb_any(skb);
286 return NET_RX_DROP;
287 }
288
289 return NET_RX_SUCCESS;
290 }
291
veth_forward_skb(struct net_device * dev,struct sk_buff * skb,struct veth_rq * rq,bool xdp)292 static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
293 struct veth_rq *rq, bool xdp)
294 {
295 return __dev_forward_skb(dev, skb) ?: xdp ?
296 veth_xdp_rx(rq, skb) :
297 netif_rx(skb);
298 }
299
300 /* return true if the specified skb has chances of GRO aggregation
301 * Don't strive for accuracy, but try to avoid GRO overhead in the most
302 * common scenarios.
303 * When XDP is enabled, all traffic is considered eligible, as the xmit
304 * device has TSO off.
305 * When TSO is enabled on the xmit device, we are likely interested only
306 * in UDP aggregation, explicitly check for that if the skb is suspected
307 * - the sock_wfree destructor is used by UDP, ICMP and XDP sockets -
308 * to belong to locally generated UDP traffic.
309 */
veth_skb_is_eligible_for_gro(const struct net_device * dev,const struct net_device * rcv,const struct sk_buff * skb)310 static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,
311 const struct net_device *rcv,
312 const struct sk_buff *skb)
313 {
314 return !(dev->features & NETIF_F_ALL_TSO) ||
315 (skb->destructor == sock_wfree &&
316 rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD));
317 }
318
veth_xmit(struct sk_buff * skb,struct net_device * dev)319 static netdev_tx_t veth_xmit(struct sk_buff *skb, struct net_device *dev)
320 {
321 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
322 struct veth_rq *rq = NULL;
323 int ret = NETDEV_TX_OK;
324 struct net_device *rcv;
325 int length = skb->len;
326 bool use_napi = false;
327 int rxq;
328
329 rcu_read_lock();
330 rcv = rcu_dereference(priv->peer);
331 if (unlikely(!rcv) || !pskb_may_pull(skb, ETH_HLEN)) {
332 kfree_skb(skb);
333 goto drop;
334 }
335
336 rcv_priv = netdev_priv(rcv);
337 rxq = skb_get_queue_mapping(skb);
338 if (rxq < rcv->real_num_rx_queues) {
339 rq = &rcv_priv->rq[rxq];
340
341 /* The napi pointer is available when an XDP program is
342 * attached or when GRO is enabled
343 * Don't bother with napi/GRO if the skb can't be aggregated
344 */
345 use_napi = rcu_access_pointer(rq->napi) &&
346 veth_skb_is_eligible_for_gro(dev, rcv, skb);
347 }
348
349 skb_tx_timestamp(skb);
350 if (likely(veth_forward_skb(rcv, skb, rq, use_napi) == NET_RX_SUCCESS)) {
351 if (!use_napi)
352 dev_lstats_add(dev, length);
353 } else {
354 drop:
355 atomic64_inc(&priv->dropped);
356 ret = NET_XMIT_DROP;
357 }
358
359 if (use_napi)
360 __veth_xdp_flush(rq);
361
362 rcu_read_unlock();
363
364 return ret;
365 }
366
veth_stats_tx(struct net_device * dev,u64 * packets,u64 * bytes)367 static u64 veth_stats_tx(struct net_device *dev, u64 *packets, u64 *bytes)
368 {
369 struct veth_priv *priv = netdev_priv(dev);
370
371 dev_lstats_read(dev, packets, bytes);
372 return atomic64_read(&priv->dropped);
373 }
374
veth_stats_rx(struct veth_stats * result,struct net_device * dev)375 static void veth_stats_rx(struct veth_stats *result, struct net_device *dev)
376 {
377 struct veth_priv *priv = netdev_priv(dev);
378 int i;
379
380 result->peer_tq_xdp_xmit_err = 0;
381 result->xdp_packets = 0;
382 result->xdp_tx_err = 0;
383 result->xdp_bytes = 0;
384 result->rx_drops = 0;
385 for (i = 0; i < dev->num_rx_queues; i++) {
386 u64 packets, bytes, drops, xdp_tx_err, peer_tq_xdp_xmit_err;
387 struct veth_rq_stats *stats = &priv->rq[i].stats;
388 unsigned int start;
389
390 do {
391 start = u64_stats_fetch_begin_irq(&stats->syncp);
392 peer_tq_xdp_xmit_err = stats->vs.peer_tq_xdp_xmit_err;
393 xdp_tx_err = stats->vs.xdp_tx_err;
394 packets = stats->vs.xdp_packets;
395 bytes = stats->vs.xdp_bytes;
396 drops = stats->vs.rx_drops;
397 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
398 result->peer_tq_xdp_xmit_err += peer_tq_xdp_xmit_err;
399 result->xdp_tx_err += xdp_tx_err;
400 result->xdp_packets += packets;
401 result->xdp_bytes += bytes;
402 result->rx_drops += drops;
403 }
404 }
405
veth_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * tot)406 static void veth_get_stats64(struct net_device *dev,
407 struct rtnl_link_stats64 *tot)
408 {
409 struct veth_priv *priv = netdev_priv(dev);
410 struct net_device *peer;
411 struct veth_stats rx;
412 u64 packets, bytes;
413
414 tot->tx_dropped = veth_stats_tx(dev, &packets, &bytes);
415 tot->tx_bytes = bytes;
416 tot->tx_packets = packets;
417
418 veth_stats_rx(&rx, dev);
419 tot->tx_dropped += rx.xdp_tx_err;
420 tot->rx_dropped = rx.rx_drops + rx.peer_tq_xdp_xmit_err;
421 tot->rx_bytes = rx.xdp_bytes;
422 tot->rx_packets = rx.xdp_packets;
423
424 rcu_read_lock();
425 peer = rcu_dereference(priv->peer);
426 if (peer) {
427 veth_stats_tx(peer, &packets, &bytes);
428 tot->rx_bytes += bytes;
429 tot->rx_packets += packets;
430
431 veth_stats_rx(&rx, peer);
432 tot->tx_dropped += rx.peer_tq_xdp_xmit_err;
433 tot->rx_dropped += rx.xdp_tx_err;
434 tot->tx_bytes += rx.xdp_bytes;
435 tot->tx_packets += rx.xdp_packets;
436 }
437 rcu_read_unlock();
438 }
439
440 /* fake multicast ability */
veth_set_multicast_list(struct net_device * dev)441 static void veth_set_multicast_list(struct net_device *dev)
442 {
443 }
444
veth_build_skb(void * head,int headroom,int len,int buflen)445 static struct sk_buff *veth_build_skb(void *head, int headroom, int len,
446 int buflen)
447 {
448 struct sk_buff *skb;
449
450 skb = build_skb(head, buflen);
451 if (!skb)
452 return NULL;
453
454 skb_reserve(skb, headroom);
455 skb_put(skb, len);
456
457 return skb;
458 }
459
veth_select_rxq(struct net_device * dev)460 static int veth_select_rxq(struct net_device *dev)
461 {
462 return smp_processor_id() % dev->real_num_rx_queues;
463 }
464
veth_peer_dev(struct net_device * dev)465 static struct net_device *veth_peer_dev(struct net_device *dev)
466 {
467 struct veth_priv *priv = netdev_priv(dev);
468
469 /* Callers must be under RCU read side. */
470 return rcu_dereference(priv->peer);
471 }
472
veth_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags,bool ndo_xmit)473 static int veth_xdp_xmit(struct net_device *dev, int n,
474 struct xdp_frame **frames,
475 u32 flags, bool ndo_xmit)
476 {
477 struct veth_priv *rcv_priv, *priv = netdev_priv(dev);
478 int i, ret = -ENXIO, nxmit = 0;
479 struct net_device *rcv;
480 unsigned int max_len;
481 struct veth_rq *rq;
482
483 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
484 return -EINVAL;
485
486 rcu_read_lock();
487 rcv = rcu_dereference(priv->peer);
488 if (unlikely(!rcv))
489 goto out;
490
491 rcv_priv = netdev_priv(rcv);
492 rq = &rcv_priv->rq[veth_select_rxq(rcv)];
493 /* The napi pointer is set if NAPI is enabled, which ensures that
494 * xdp_ring is initialized on receive side and the peer device is up.
495 */
496 if (!rcu_access_pointer(rq->napi))
497 goto out;
498
499 max_len = rcv->mtu + rcv->hard_header_len + VLAN_HLEN;
500
501 spin_lock(&rq->xdp_ring.producer_lock);
502 for (i = 0; i < n; i++) {
503 struct xdp_frame *frame = frames[i];
504 void *ptr = veth_xdp_to_ptr(frame);
505
506 if (unlikely(frame->len > max_len ||
507 __ptr_ring_produce(&rq->xdp_ring, ptr)))
508 break;
509 nxmit++;
510 }
511 spin_unlock(&rq->xdp_ring.producer_lock);
512
513 if (flags & XDP_XMIT_FLUSH)
514 __veth_xdp_flush(rq);
515
516 ret = nxmit;
517 if (ndo_xmit) {
518 u64_stats_update_begin(&rq->stats.syncp);
519 rq->stats.vs.peer_tq_xdp_xmit += nxmit;
520 rq->stats.vs.peer_tq_xdp_xmit_err += n - nxmit;
521 u64_stats_update_end(&rq->stats.syncp);
522 }
523
524 out:
525 rcu_read_unlock();
526
527 return ret;
528 }
529
veth_ndo_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)530 static int veth_ndo_xdp_xmit(struct net_device *dev, int n,
531 struct xdp_frame **frames, u32 flags)
532 {
533 int err;
534
535 err = veth_xdp_xmit(dev, n, frames, flags, true);
536 if (err < 0) {
537 struct veth_priv *priv = netdev_priv(dev);
538
539 atomic64_add(n, &priv->dropped);
540 }
541
542 return err;
543 }
544
veth_xdp_flush_bq(struct veth_rq * rq,struct veth_xdp_tx_bq * bq)545 static void veth_xdp_flush_bq(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
546 {
547 int sent, i, err = 0, drops;
548
549 sent = veth_xdp_xmit(rq->dev, bq->count, bq->q, 0, false);
550 if (sent < 0) {
551 err = sent;
552 sent = 0;
553 }
554
555 for (i = sent; unlikely(i < bq->count); i++)
556 xdp_return_frame(bq->q[i]);
557
558 drops = bq->count - sent;
559 trace_xdp_bulk_tx(rq->dev, sent, drops, err);
560
561 u64_stats_update_begin(&rq->stats.syncp);
562 rq->stats.vs.xdp_tx += sent;
563 rq->stats.vs.xdp_tx_err += drops;
564 u64_stats_update_end(&rq->stats.syncp);
565
566 bq->count = 0;
567 }
568
veth_xdp_flush(struct veth_rq * rq,struct veth_xdp_tx_bq * bq)569 static void veth_xdp_flush(struct veth_rq *rq, struct veth_xdp_tx_bq *bq)
570 {
571 struct veth_priv *rcv_priv, *priv = netdev_priv(rq->dev);
572 struct net_device *rcv;
573 struct veth_rq *rcv_rq;
574
575 rcu_read_lock();
576 veth_xdp_flush_bq(rq, bq);
577 rcv = rcu_dereference(priv->peer);
578 if (unlikely(!rcv))
579 goto out;
580
581 rcv_priv = netdev_priv(rcv);
582 rcv_rq = &rcv_priv->rq[veth_select_rxq(rcv)];
583 /* xdp_ring is initialized on receive side? */
584 if (unlikely(!rcu_access_pointer(rcv_rq->xdp_prog)))
585 goto out;
586
587 __veth_xdp_flush(rcv_rq);
588 out:
589 rcu_read_unlock();
590 }
591
veth_xdp_tx(struct veth_rq * rq,struct xdp_buff * xdp,struct veth_xdp_tx_bq * bq)592 static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
593 struct veth_xdp_tx_bq *bq)
594 {
595 struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
596
597 if (unlikely(!frame))
598 return -EOVERFLOW;
599
600 if (unlikely(bq->count == VETH_XDP_TX_BULK_SIZE))
601 veth_xdp_flush_bq(rq, bq);
602
603 bq->q[bq->count++] = frame;
604
605 return 0;
606 }
607
veth_xdp_rcv_one(struct veth_rq * rq,struct xdp_frame * frame,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)608 static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
609 struct xdp_frame *frame,
610 struct veth_xdp_tx_bq *bq,
611 struct veth_stats *stats)
612 {
613 struct xdp_frame orig_frame;
614 struct bpf_prog *xdp_prog;
615
616 rcu_read_lock();
617 xdp_prog = rcu_dereference(rq->xdp_prog);
618 if (likely(xdp_prog)) {
619 struct xdp_buff xdp;
620 u32 act;
621
622 xdp_convert_frame_to_buff(frame, &xdp);
623 xdp.rxq = &rq->xdp_rxq;
624
625 act = bpf_prog_run_xdp(xdp_prog, &xdp);
626
627 switch (act) {
628 case XDP_PASS:
629 if (xdp_update_frame_from_buff(&xdp, frame))
630 goto err_xdp;
631 break;
632 case XDP_TX:
633 orig_frame = *frame;
634 xdp.rxq->mem = frame->mem;
635 if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
636 trace_xdp_exception(rq->dev, xdp_prog, act);
637 frame = &orig_frame;
638 stats->rx_drops++;
639 goto err_xdp;
640 }
641 stats->xdp_tx++;
642 rcu_read_unlock();
643 goto xdp_xmit;
644 case XDP_REDIRECT:
645 orig_frame = *frame;
646 xdp.rxq->mem = frame->mem;
647 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
648 frame = &orig_frame;
649 stats->rx_drops++;
650 goto err_xdp;
651 }
652 stats->xdp_redirect++;
653 rcu_read_unlock();
654 goto xdp_xmit;
655 default:
656 bpf_warn_invalid_xdp_action(act);
657 fallthrough;
658 case XDP_ABORTED:
659 trace_xdp_exception(rq->dev, xdp_prog, act);
660 fallthrough;
661 case XDP_DROP:
662 stats->xdp_drops++;
663 goto err_xdp;
664 }
665 }
666 rcu_read_unlock();
667
668 return frame;
669 err_xdp:
670 rcu_read_unlock();
671 xdp_return_frame(frame);
672 xdp_xmit:
673 return NULL;
674 }
675
676 /* frames array contains VETH_XDP_BATCH at most */
veth_xdp_rcv_bulk_skb(struct veth_rq * rq,void ** frames,int n_xdpf,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)677 static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
678 int n_xdpf, struct veth_xdp_tx_bq *bq,
679 struct veth_stats *stats)
680 {
681 void *skbs[VETH_XDP_BATCH];
682 int i;
683
684 if (xdp_alloc_skb_bulk(skbs, n_xdpf,
685 GFP_ATOMIC | __GFP_ZERO) < 0) {
686 for (i = 0; i < n_xdpf; i++)
687 xdp_return_frame(frames[i]);
688 stats->rx_drops += n_xdpf;
689
690 return;
691 }
692
693 for (i = 0; i < n_xdpf; i++) {
694 struct sk_buff *skb = skbs[i];
695
696 skb = __xdp_build_skb_from_frame(frames[i], skb,
697 rq->dev);
698 if (!skb) {
699 xdp_return_frame(frames[i]);
700 stats->rx_drops++;
701 continue;
702 }
703 napi_gro_receive(&rq->xdp_napi, skb);
704 }
705 }
706
veth_xdp_rcv_skb(struct veth_rq * rq,struct sk_buff * skb,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)707 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
708 struct sk_buff *skb,
709 struct veth_xdp_tx_bq *bq,
710 struct veth_stats *stats)
711 {
712 u32 pktlen, headroom, act, metalen, frame_sz;
713 void *orig_data, *orig_data_end;
714 struct bpf_prog *xdp_prog;
715 int mac_len, delta, off;
716 struct xdp_buff xdp;
717
718 skb_prepare_for_gro(skb);
719
720 rcu_read_lock();
721 xdp_prog = rcu_dereference(rq->xdp_prog);
722 if (unlikely(!xdp_prog)) {
723 rcu_read_unlock();
724 goto out;
725 }
726
727 mac_len = skb->data - skb_mac_header(skb);
728 pktlen = skb->len + mac_len;
729 headroom = skb_headroom(skb) - mac_len;
730
731 if (skb_shared(skb) || skb_head_is_locked(skb) ||
732 skb_is_nonlinear(skb) || headroom < XDP_PACKET_HEADROOM) {
733 struct sk_buff *nskb;
734 int size, head_off;
735 void *head, *start;
736 struct page *page;
737
738 size = SKB_DATA_ALIGN(VETH_XDP_HEADROOM + pktlen) +
739 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
740 if (size > PAGE_SIZE)
741 goto drop;
742
743 page = alloc_page(GFP_ATOMIC | __GFP_NOWARN);
744 if (!page)
745 goto drop;
746
747 head = page_address(page);
748 start = head + VETH_XDP_HEADROOM;
749 if (skb_copy_bits(skb, -mac_len, start, pktlen)) {
750 page_frag_free(head);
751 goto drop;
752 }
753
754 nskb = veth_build_skb(head, VETH_XDP_HEADROOM + mac_len,
755 skb->len, PAGE_SIZE);
756 if (!nskb) {
757 page_frag_free(head);
758 goto drop;
759 }
760
761 skb_copy_header(nskb, skb);
762 head_off = skb_headroom(nskb) - skb_headroom(skb);
763 skb_headers_offset_update(nskb, head_off);
764 consume_skb(skb);
765 skb = nskb;
766 }
767
768 /* SKB "head" area always have tailroom for skb_shared_info */
769 frame_sz = skb_end_pointer(skb) - skb->head;
770 frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
771 xdp_init_buff(&xdp, frame_sz, &rq->xdp_rxq);
772 xdp_prepare_buff(&xdp, skb->head, skb->mac_header, pktlen, true);
773
774 orig_data = xdp.data;
775 orig_data_end = xdp.data_end;
776
777 act = bpf_prog_run_xdp(xdp_prog, &xdp);
778
779 switch (act) {
780 case XDP_PASS:
781 break;
782 case XDP_TX:
783 get_page(virt_to_page(xdp.data));
784 consume_skb(skb);
785 xdp.rxq->mem = rq->xdp_mem;
786 if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
787 trace_xdp_exception(rq->dev, xdp_prog, act);
788 stats->rx_drops++;
789 goto err_xdp;
790 }
791 stats->xdp_tx++;
792 rcu_read_unlock();
793 goto xdp_xmit;
794 case XDP_REDIRECT:
795 get_page(virt_to_page(xdp.data));
796 consume_skb(skb);
797 xdp.rxq->mem = rq->xdp_mem;
798 if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
799 stats->rx_drops++;
800 goto err_xdp;
801 }
802 stats->xdp_redirect++;
803 rcu_read_unlock();
804 goto xdp_xmit;
805 default:
806 bpf_warn_invalid_xdp_action(act);
807 fallthrough;
808 case XDP_ABORTED:
809 trace_xdp_exception(rq->dev, xdp_prog, act);
810 fallthrough;
811 case XDP_DROP:
812 stats->xdp_drops++;
813 goto xdp_drop;
814 }
815 rcu_read_unlock();
816
817 /* check if bpf_xdp_adjust_head was used */
818 delta = orig_data - xdp.data;
819 off = mac_len + delta;
820 if (off > 0)
821 __skb_push(skb, off);
822 else if (off < 0)
823 __skb_pull(skb, -off);
824 skb->mac_header -= delta;
825
826 /* check if bpf_xdp_adjust_tail was used */
827 off = xdp.data_end - orig_data_end;
828 if (off != 0)
829 __skb_put(skb, off); /* positive on grow, negative on shrink */
830 skb->protocol = eth_type_trans(skb, rq->dev);
831
832 metalen = xdp.data - xdp.data_meta;
833 if (metalen)
834 skb_metadata_set(skb, metalen);
835 out:
836 return skb;
837 drop:
838 stats->rx_drops++;
839 xdp_drop:
840 rcu_read_unlock();
841 kfree_skb(skb);
842 return NULL;
843 err_xdp:
844 rcu_read_unlock();
845 page_frag_free(xdp.data);
846 xdp_xmit:
847 return NULL;
848 }
849
veth_xdp_rcv(struct veth_rq * rq,int budget,struct veth_xdp_tx_bq * bq,struct veth_stats * stats)850 static int veth_xdp_rcv(struct veth_rq *rq, int budget,
851 struct veth_xdp_tx_bq *bq,
852 struct veth_stats *stats)
853 {
854 int i, done = 0, n_xdpf = 0;
855 void *xdpf[VETH_XDP_BATCH];
856
857 for (i = 0; i < budget; i++) {
858 void *ptr = __ptr_ring_consume(&rq->xdp_ring);
859
860 if (!ptr)
861 break;
862
863 if (veth_is_xdp_frame(ptr)) {
864 /* ndo_xdp_xmit */
865 struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
866
867 stats->xdp_bytes += frame->len;
868 frame = veth_xdp_rcv_one(rq, frame, bq, stats);
869 if (frame) {
870 /* XDP_PASS */
871 xdpf[n_xdpf++] = frame;
872 if (n_xdpf == VETH_XDP_BATCH) {
873 veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf,
874 bq, stats);
875 n_xdpf = 0;
876 }
877 }
878 } else {
879 /* ndo_start_xmit */
880 struct sk_buff *skb = ptr;
881
882 stats->xdp_bytes += skb->len;
883 skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
884 if (skb) {
885 if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
886 netif_receive_skb(skb);
887 else
888 napi_gro_receive(&rq->xdp_napi, skb);
889 }
890 }
891 done++;
892 }
893
894 if (n_xdpf)
895 veth_xdp_rcv_bulk_skb(rq, xdpf, n_xdpf, bq, stats);
896
897 u64_stats_update_begin(&rq->stats.syncp);
898 rq->stats.vs.xdp_redirect += stats->xdp_redirect;
899 rq->stats.vs.xdp_bytes += stats->xdp_bytes;
900 rq->stats.vs.xdp_drops += stats->xdp_drops;
901 rq->stats.vs.rx_drops += stats->rx_drops;
902 rq->stats.vs.xdp_packets += done;
903 u64_stats_update_end(&rq->stats.syncp);
904
905 return done;
906 }
907
veth_poll(struct napi_struct * napi,int budget)908 static int veth_poll(struct napi_struct *napi, int budget)
909 {
910 struct veth_rq *rq =
911 container_of(napi, struct veth_rq, xdp_napi);
912 struct veth_stats stats = {};
913 struct veth_xdp_tx_bq bq;
914 int done;
915
916 bq.count = 0;
917
918 xdp_set_return_frame_no_direct();
919 done = veth_xdp_rcv(rq, budget, &bq, &stats);
920
921 if (stats.xdp_redirect > 0)
922 xdp_do_flush();
923
924 if (done < budget && napi_complete_done(napi, done)) {
925 /* Write rx_notify_masked before reading ptr_ring */
926 smp_store_mb(rq->rx_notify_masked, false);
927 if (unlikely(!__ptr_ring_empty(&rq->xdp_ring))) {
928 if (napi_schedule_prep(&rq->xdp_napi)) {
929 WRITE_ONCE(rq->rx_notify_masked, true);
930 __napi_schedule(&rq->xdp_napi);
931 }
932 }
933 }
934
935 if (stats.xdp_tx > 0)
936 veth_xdp_flush(rq, &bq);
937 xdp_clear_return_frame_no_direct();
938
939 return done;
940 }
941
__veth_napi_enable_range(struct net_device * dev,int start,int end)942 static int __veth_napi_enable_range(struct net_device *dev, int start, int end)
943 {
944 struct veth_priv *priv = netdev_priv(dev);
945 int err, i;
946
947 for (i = start; i < end; i++) {
948 struct veth_rq *rq = &priv->rq[i];
949
950 err = ptr_ring_init(&rq->xdp_ring, VETH_RING_SIZE, GFP_KERNEL);
951 if (err)
952 goto err_xdp_ring;
953 }
954
955 for (i = start; i < end; i++) {
956 struct veth_rq *rq = &priv->rq[i];
957
958 napi_enable(&rq->xdp_napi);
959 rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
960 }
961
962 return 0;
963
964 err_xdp_ring:
965 for (i--; i >= start; i--)
966 ptr_ring_cleanup(&priv->rq[i].xdp_ring, veth_ptr_free);
967
968 return err;
969 }
970
__veth_napi_enable(struct net_device * dev)971 static int __veth_napi_enable(struct net_device *dev)
972 {
973 return __veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
974 }
975
veth_napi_del_range(struct net_device * dev,int start,int end)976 static void veth_napi_del_range(struct net_device *dev, int start, int end)
977 {
978 struct veth_priv *priv = netdev_priv(dev);
979 int i;
980
981 for (i = start; i < end; i++) {
982 struct veth_rq *rq = &priv->rq[i];
983
984 rcu_assign_pointer(priv->rq[i].napi, NULL);
985 napi_disable(&rq->xdp_napi);
986 __netif_napi_del(&rq->xdp_napi);
987 }
988 synchronize_net();
989
990 for (i = start; i < end; i++) {
991 struct veth_rq *rq = &priv->rq[i];
992
993 rq->rx_notify_masked = false;
994 ptr_ring_cleanup(&rq->xdp_ring, veth_ptr_free);
995 }
996 }
997
veth_napi_del(struct net_device * dev)998 static void veth_napi_del(struct net_device *dev)
999 {
1000 veth_napi_del_range(dev, 0, dev->real_num_rx_queues);
1001 }
1002
veth_gro_requested(const struct net_device * dev)1003 static bool veth_gro_requested(const struct net_device *dev)
1004 {
1005 return !!(dev->wanted_features & NETIF_F_GRO);
1006 }
1007
veth_enable_xdp_range(struct net_device * dev,int start,int end,bool napi_already_on)1008 static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
1009 bool napi_already_on)
1010 {
1011 struct veth_priv *priv = netdev_priv(dev);
1012 int err, i;
1013
1014 for (i = start; i < end; i++) {
1015 struct veth_rq *rq = &priv->rq[i];
1016
1017 if (!napi_already_on)
1018 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
1019 err = xdp_rxq_info_reg(&rq->xdp_rxq, dev, i, rq->xdp_napi.napi_id);
1020 if (err < 0)
1021 goto err_rxq_reg;
1022
1023 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq,
1024 MEM_TYPE_PAGE_SHARED,
1025 NULL);
1026 if (err < 0)
1027 goto err_reg_mem;
1028
1029 /* Save original mem info as it can be overwritten */
1030 rq->xdp_mem = rq->xdp_rxq.mem;
1031 }
1032 return 0;
1033
1034 err_reg_mem:
1035 xdp_rxq_info_unreg(&priv->rq[i].xdp_rxq);
1036 err_rxq_reg:
1037 for (i--; i >= start; i--) {
1038 struct veth_rq *rq = &priv->rq[i];
1039
1040 xdp_rxq_info_unreg(&rq->xdp_rxq);
1041 if (!napi_already_on)
1042 netif_napi_del(&rq->xdp_napi);
1043 }
1044
1045 return err;
1046 }
1047
veth_disable_xdp_range(struct net_device * dev,int start,int end,bool delete_napi)1048 static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
1049 bool delete_napi)
1050 {
1051 struct veth_priv *priv = netdev_priv(dev);
1052 int i;
1053
1054 for (i = start; i < end; i++) {
1055 struct veth_rq *rq = &priv->rq[i];
1056
1057 rq->xdp_rxq.mem = rq->xdp_mem;
1058 xdp_rxq_info_unreg(&rq->xdp_rxq);
1059
1060 if (delete_napi)
1061 netif_napi_del(&rq->xdp_napi);
1062 }
1063 }
1064
veth_enable_xdp(struct net_device * dev)1065 static int veth_enable_xdp(struct net_device *dev)
1066 {
1067 bool napi_already_on = veth_gro_requested(dev) && (dev->flags & IFF_UP);
1068 struct veth_priv *priv = netdev_priv(dev);
1069 int err, i;
1070
1071 if (!xdp_rxq_info_is_reg(&priv->rq[0].xdp_rxq)) {
1072 err = veth_enable_xdp_range(dev, 0, dev->real_num_rx_queues, napi_already_on);
1073 if (err)
1074 return err;
1075
1076 if (!napi_already_on) {
1077 err = __veth_napi_enable(dev);
1078 if (err) {
1079 veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, true);
1080 return err;
1081 }
1082 }
1083 }
1084
1085 for (i = 0; i < dev->real_num_rx_queues; i++) {
1086 rcu_assign_pointer(priv->rq[i].xdp_prog, priv->_xdp_prog);
1087 rcu_assign_pointer(priv->rq[i].napi, &priv->rq[i].xdp_napi);
1088 }
1089
1090 return 0;
1091 }
1092
veth_disable_xdp(struct net_device * dev)1093 static void veth_disable_xdp(struct net_device *dev)
1094 {
1095 struct veth_priv *priv = netdev_priv(dev);
1096 int i;
1097
1098 for (i = 0; i < dev->real_num_rx_queues; i++)
1099 rcu_assign_pointer(priv->rq[i].xdp_prog, NULL);
1100
1101 if (!netif_running(dev) || !veth_gro_requested(dev))
1102 veth_napi_del(dev);
1103
1104 veth_disable_xdp_range(dev, 0, dev->real_num_rx_queues, false);
1105 }
1106
veth_napi_enable_range(struct net_device * dev,int start,int end)1107 static int veth_napi_enable_range(struct net_device *dev, int start, int end)
1108 {
1109 struct veth_priv *priv = netdev_priv(dev);
1110 int err, i;
1111
1112 for (i = start; i < end; i++) {
1113 struct veth_rq *rq = &priv->rq[i];
1114
1115 netif_napi_add(dev, &rq->xdp_napi, veth_poll, NAPI_POLL_WEIGHT);
1116 }
1117
1118 err = __veth_napi_enable_range(dev, start, end);
1119 if (err) {
1120 for (i = start; i < end; i++) {
1121 struct veth_rq *rq = &priv->rq[i];
1122
1123 netif_napi_del(&rq->xdp_napi);
1124 }
1125 return err;
1126 }
1127 return err;
1128 }
1129
veth_napi_enable(struct net_device * dev)1130 static int veth_napi_enable(struct net_device *dev)
1131 {
1132 return veth_napi_enable_range(dev, 0, dev->real_num_rx_queues);
1133 }
1134
veth_disable_range_safe(struct net_device * dev,int start,int end)1135 static void veth_disable_range_safe(struct net_device *dev, int start, int end)
1136 {
1137 struct veth_priv *priv = netdev_priv(dev);
1138
1139 if (start >= end)
1140 return;
1141
1142 if (priv->_xdp_prog) {
1143 veth_napi_del_range(dev, start, end);
1144 veth_disable_xdp_range(dev, start, end, false);
1145 } else if (veth_gro_requested(dev)) {
1146 veth_napi_del_range(dev, start, end);
1147 }
1148 }
1149
veth_enable_range_safe(struct net_device * dev,int start,int end)1150 static int veth_enable_range_safe(struct net_device *dev, int start, int end)
1151 {
1152 struct veth_priv *priv = netdev_priv(dev);
1153 int err;
1154
1155 if (start >= end)
1156 return 0;
1157
1158 if (priv->_xdp_prog) {
1159 /* these channels are freshly initialized, napi is not on there even
1160 * when GRO is requeste
1161 */
1162 err = veth_enable_xdp_range(dev, start, end, false);
1163 if (err)
1164 return err;
1165
1166 err = __veth_napi_enable_range(dev, start, end);
1167 if (err) {
1168 /* on error always delete the newly added napis */
1169 veth_disable_xdp_range(dev, start, end, true);
1170 return err;
1171 }
1172 } else if (veth_gro_requested(dev)) {
1173 return veth_napi_enable_range(dev, start, end);
1174 }
1175 return 0;
1176 }
1177
veth_set_channels(struct net_device * dev,struct ethtool_channels * ch)1178 static int veth_set_channels(struct net_device *dev,
1179 struct ethtool_channels *ch)
1180 {
1181 struct veth_priv *priv = netdev_priv(dev);
1182 unsigned int old_rx_count, new_rx_count;
1183 struct veth_priv *peer_priv;
1184 struct net_device *peer;
1185 int err;
1186
1187 /* sanity check. Upper bounds are already enforced by the caller */
1188 if (!ch->rx_count || !ch->tx_count)
1189 return -EINVAL;
1190
1191 /* avoid braking XDP, if that is enabled */
1192 peer = rtnl_dereference(priv->peer);
1193 peer_priv = peer ? netdev_priv(peer) : NULL;
1194 if (priv->_xdp_prog && peer && ch->rx_count < peer->real_num_tx_queues)
1195 return -EINVAL;
1196
1197 if (peer && peer_priv && peer_priv->_xdp_prog && ch->tx_count > peer->real_num_rx_queues)
1198 return -EINVAL;
1199
1200 old_rx_count = dev->real_num_rx_queues;
1201 new_rx_count = ch->rx_count;
1202 if (netif_running(dev)) {
1203 /* turn device off */
1204 netif_carrier_off(dev);
1205 if (peer)
1206 netif_carrier_off(peer);
1207
1208 /* try to allocate new resurces, as needed*/
1209 err = veth_enable_range_safe(dev, old_rx_count, new_rx_count);
1210 if (err)
1211 goto out;
1212 }
1213
1214 err = netif_set_real_num_rx_queues(dev, ch->rx_count);
1215 if (err)
1216 goto revert;
1217
1218 err = netif_set_real_num_tx_queues(dev, ch->tx_count);
1219 if (err) {
1220 int err2 = netif_set_real_num_rx_queues(dev, old_rx_count);
1221
1222 /* this error condition could happen only if rx and tx change
1223 * in opposite directions (e.g. tx nr raises, rx nr decreases)
1224 * and we can't do anything to fully restore the original
1225 * status
1226 */
1227 if (err2)
1228 pr_warn("Can't restore rx queues config %d -> %d %d",
1229 new_rx_count, old_rx_count, err2);
1230 else
1231 goto revert;
1232 }
1233
1234 out:
1235 if (netif_running(dev)) {
1236 /* note that we need to swap the arguments WRT the enable part
1237 * to identify the range we have to disable
1238 */
1239 veth_disable_range_safe(dev, new_rx_count, old_rx_count);
1240 netif_carrier_on(dev);
1241 if (peer)
1242 netif_carrier_on(peer);
1243 }
1244 return err;
1245
1246 revert:
1247 new_rx_count = old_rx_count;
1248 old_rx_count = ch->rx_count;
1249 goto out;
1250 }
1251
veth_open(struct net_device * dev)1252 static int veth_open(struct net_device *dev)
1253 {
1254 struct veth_priv *priv = netdev_priv(dev);
1255 struct net_device *peer = rtnl_dereference(priv->peer);
1256 int err;
1257
1258 if (!peer)
1259 return -ENOTCONN;
1260
1261 if (priv->_xdp_prog) {
1262 err = veth_enable_xdp(dev);
1263 if (err)
1264 return err;
1265 } else if (veth_gro_requested(dev)) {
1266 err = veth_napi_enable(dev);
1267 if (err)
1268 return err;
1269 }
1270
1271 if (peer->flags & IFF_UP) {
1272 netif_carrier_on(dev);
1273 netif_carrier_on(peer);
1274 }
1275
1276 return 0;
1277 }
1278
veth_close(struct net_device * dev)1279 static int veth_close(struct net_device *dev)
1280 {
1281 struct veth_priv *priv = netdev_priv(dev);
1282 struct net_device *peer = rtnl_dereference(priv->peer);
1283
1284 netif_carrier_off(dev);
1285 if (peer)
1286 netif_carrier_off(peer);
1287
1288 if (priv->_xdp_prog)
1289 veth_disable_xdp(dev);
1290 else if (veth_gro_requested(dev))
1291 veth_napi_del(dev);
1292
1293 return 0;
1294 }
1295
is_valid_veth_mtu(int mtu)1296 static int is_valid_veth_mtu(int mtu)
1297 {
1298 return mtu >= ETH_MIN_MTU && mtu <= ETH_MAX_MTU;
1299 }
1300
veth_alloc_queues(struct net_device * dev)1301 static int veth_alloc_queues(struct net_device *dev)
1302 {
1303 struct veth_priv *priv = netdev_priv(dev);
1304 int i;
1305
1306 priv->rq = kvcalloc(dev->num_rx_queues, sizeof(*priv->rq),
1307 GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
1308 if (!priv->rq)
1309 return -ENOMEM;
1310
1311 for (i = 0; i < dev->num_rx_queues; i++) {
1312 priv->rq[i].dev = dev;
1313 u64_stats_init(&priv->rq[i].stats.syncp);
1314 }
1315
1316 return 0;
1317 }
1318
veth_free_queues(struct net_device * dev)1319 static void veth_free_queues(struct net_device *dev)
1320 {
1321 struct veth_priv *priv = netdev_priv(dev);
1322
1323 kvfree(priv->rq);
1324 }
1325
veth_dev_init(struct net_device * dev)1326 static int veth_dev_init(struct net_device *dev)
1327 {
1328 int err;
1329
1330 dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
1331 if (!dev->lstats)
1332 return -ENOMEM;
1333
1334 err = veth_alloc_queues(dev);
1335 if (err) {
1336 free_percpu(dev->lstats);
1337 return err;
1338 }
1339
1340 return 0;
1341 }
1342
veth_dev_free(struct net_device * dev)1343 static void veth_dev_free(struct net_device *dev)
1344 {
1345 veth_free_queues(dev);
1346 free_percpu(dev->lstats);
1347 }
1348
1349 #ifdef CONFIG_NET_POLL_CONTROLLER
veth_poll_controller(struct net_device * dev)1350 static void veth_poll_controller(struct net_device *dev)
1351 {
1352 /* veth only receives frames when its peer sends one
1353 * Since it has nothing to do with disabling irqs, we are guaranteed
1354 * never to have pending data when we poll for it so
1355 * there is nothing to do here.
1356 *
1357 * We need this though so netpoll recognizes us as an interface that
1358 * supports polling, which enables bridge devices in virt setups to
1359 * still use netconsole
1360 */
1361 }
1362 #endif /* CONFIG_NET_POLL_CONTROLLER */
1363
veth_get_iflink(const struct net_device * dev)1364 static int veth_get_iflink(const struct net_device *dev)
1365 {
1366 struct veth_priv *priv = netdev_priv(dev);
1367 struct net_device *peer;
1368 int iflink;
1369
1370 rcu_read_lock();
1371 peer = rcu_dereference(priv->peer);
1372 iflink = peer ? peer->ifindex : 0;
1373 rcu_read_unlock();
1374
1375 return iflink;
1376 }
1377
veth_fix_features(struct net_device * dev,netdev_features_t features)1378 static netdev_features_t veth_fix_features(struct net_device *dev,
1379 netdev_features_t features)
1380 {
1381 struct veth_priv *priv = netdev_priv(dev);
1382 struct net_device *peer;
1383
1384 peer = rtnl_dereference(priv->peer);
1385 if (peer) {
1386 struct veth_priv *peer_priv = netdev_priv(peer);
1387
1388 if (peer_priv->_xdp_prog)
1389 features &= ~NETIF_F_GSO_SOFTWARE;
1390 }
1391 if (priv->_xdp_prog)
1392 features |= NETIF_F_GRO;
1393
1394 return features;
1395 }
1396
veth_set_features(struct net_device * dev,netdev_features_t features)1397 static int veth_set_features(struct net_device *dev,
1398 netdev_features_t features)
1399 {
1400 netdev_features_t changed = features ^ dev->features;
1401 struct veth_priv *priv = netdev_priv(dev);
1402 int err;
1403
1404 if (!(changed & NETIF_F_GRO) || !(dev->flags & IFF_UP) || priv->_xdp_prog)
1405 return 0;
1406
1407 if (features & NETIF_F_GRO) {
1408 err = veth_napi_enable(dev);
1409 if (err)
1410 return err;
1411 } else {
1412 veth_napi_del(dev);
1413 }
1414 return 0;
1415 }
1416
veth_set_rx_headroom(struct net_device * dev,int new_hr)1417 static void veth_set_rx_headroom(struct net_device *dev, int new_hr)
1418 {
1419 struct veth_priv *peer_priv, *priv = netdev_priv(dev);
1420 struct net_device *peer;
1421
1422 if (new_hr < 0)
1423 new_hr = 0;
1424
1425 rcu_read_lock();
1426 peer = rcu_dereference(priv->peer);
1427 if (unlikely(!peer))
1428 goto out;
1429
1430 peer_priv = netdev_priv(peer);
1431 priv->requested_headroom = new_hr;
1432 new_hr = max(priv->requested_headroom, peer_priv->requested_headroom);
1433 dev->needed_headroom = new_hr;
1434 peer->needed_headroom = new_hr;
1435
1436 out:
1437 rcu_read_unlock();
1438 }
1439
veth_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)1440 static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,
1441 struct netlink_ext_ack *extack)
1442 {
1443 struct veth_priv *priv = netdev_priv(dev);
1444 struct bpf_prog *old_prog;
1445 struct net_device *peer;
1446 unsigned int max_mtu;
1447 int err;
1448
1449 old_prog = priv->_xdp_prog;
1450 priv->_xdp_prog = prog;
1451 peer = rtnl_dereference(priv->peer);
1452
1453 if (prog) {
1454 if (!peer) {
1455 NL_SET_ERR_MSG_MOD(extack, "Cannot set XDP when peer is detached");
1456 err = -ENOTCONN;
1457 goto err;
1458 }
1459
1460 max_mtu = PAGE_SIZE - VETH_XDP_HEADROOM -
1461 peer->hard_header_len -
1462 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1463 if (peer->mtu > max_mtu) {
1464 NL_SET_ERR_MSG_MOD(extack, "Peer MTU is too large to set XDP");
1465 err = -ERANGE;
1466 goto err;
1467 }
1468
1469 if (dev->real_num_rx_queues < peer->real_num_tx_queues) {
1470 NL_SET_ERR_MSG_MOD(extack, "XDP expects number of rx queues not less than peer tx queues");
1471 err = -ENOSPC;
1472 goto err;
1473 }
1474
1475 if (dev->flags & IFF_UP) {
1476 err = veth_enable_xdp(dev);
1477 if (err) {
1478 NL_SET_ERR_MSG_MOD(extack, "Setup for XDP failed");
1479 goto err;
1480 }
1481 }
1482
1483 if (!old_prog) {
1484 if (!veth_gro_requested(dev)) {
1485 /* user-space did not require GRO, but adding
1486 * XDP is supposed to get GRO working
1487 */
1488 dev->features |= NETIF_F_GRO;
1489 netdev_features_change(dev);
1490 }
1491
1492 peer->hw_features &= ~NETIF_F_GSO_SOFTWARE;
1493 peer->max_mtu = max_mtu;
1494 }
1495 }
1496
1497 if (old_prog) {
1498 if (!prog) {
1499 if (dev->flags & IFF_UP)
1500 veth_disable_xdp(dev);
1501
1502 /* if user-space did not require GRO, since adding XDP
1503 * enabled it, clear it now
1504 */
1505 if (!veth_gro_requested(dev)) {
1506 dev->features &= ~NETIF_F_GRO;
1507 netdev_features_change(dev);
1508 }
1509
1510 if (peer) {
1511 peer->hw_features |= NETIF_F_GSO_SOFTWARE;
1512 peer->max_mtu = ETH_MAX_MTU;
1513 }
1514 }
1515 bpf_prog_put(old_prog);
1516 }
1517
1518 if ((!!old_prog ^ !!prog) && peer)
1519 netdev_update_features(peer);
1520
1521 return 0;
1522 err:
1523 priv->_xdp_prog = old_prog;
1524
1525 return err;
1526 }
1527
veth_xdp(struct net_device * dev,struct netdev_bpf * xdp)1528 static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1529 {
1530 switch (xdp->command) {
1531 case XDP_SETUP_PROG:
1532 return veth_xdp_set(dev, xdp->prog, xdp->extack);
1533 default:
1534 return -EINVAL;
1535 }
1536 }
1537
1538 static const struct net_device_ops veth_netdev_ops = {
1539 .ndo_init = veth_dev_init,
1540 .ndo_open = veth_open,
1541 .ndo_stop = veth_close,
1542 .ndo_start_xmit = veth_xmit,
1543 .ndo_get_stats64 = veth_get_stats64,
1544 .ndo_set_rx_mode = veth_set_multicast_list,
1545 .ndo_set_mac_address = eth_mac_addr,
1546 #ifdef CONFIG_NET_POLL_CONTROLLER
1547 .ndo_poll_controller = veth_poll_controller,
1548 #endif
1549 .ndo_get_iflink = veth_get_iflink,
1550 .ndo_fix_features = veth_fix_features,
1551 .ndo_set_features = veth_set_features,
1552 .ndo_features_check = passthru_features_check,
1553 .ndo_set_rx_headroom = veth_set_rx_headroom,
1554 .ndo_bpf = veth_xdp,
1555 .ndo_xdp_xmit = veth_ndo_xdp_xmit,
1556 .ndo_get_peer_dev = veth_peer_dev,
1557 };
1558
1559 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
1560 NETIF_F_RXCSUM | NETIF_F_SCTP_CRC | NETIF_F_HIGHDMA | \
1561 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL | \
1562 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX | \
1563 NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_STAG_RX )
1564
veth_setup(struct net_device * dev)1565 static void veth_setup(struct net_device *dev)
1566 {
1567 ether_setup(dev);
1568
1569 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1570 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1571 dev->priv_flags |= IFF_NO_QUEUE;
1572 dev->priv_flags |= IFF_PHONY_HEADROOM;
1573
1574 dev->netdev_ops = &veth_netdev_ops;
1575 dev->ethtool_ops = &veth_ethtool_ops;
1576 dev->features |= NETIF_F_LLTX;
1577 dev->features |= VETH_FEATURES;
1578 dev->vlan_features = dev->features &
1579 ~(NETIF_F_HW_VLAN_CTAG_TX |
1580 NETIF_F_HW_VLAN_STAG_TX |
1581 NETIF_F_HW_VLAN_CTAG_RX |
1582 NETIF_F_HW_VLAN_STAG_RX);
1583 dev->needs_free_netdev = true;
1584 dev->priv_destructor = veth_dev_free;
1585 dev->max_mtu = ETH_MAX_MTU;
1586
1587 dev->hw_features = VETH_FEATURES;
1588 dev->hw_enc_features = VETH_FEATURES;
1589 dev->mpls_features = NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
1590 }
1591
1592 /*
1593 * netlink interface
1594 */
1595
veth_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1596 static int veth_validate(struct nlattr *tb[], struct nlattr *data[],
1597 struct netlink_ext_ack *extack)
1598 {
1599 if (tb[IFLA_ADDRESS]) {
1600 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1601 return -EINVAL;
1602 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1603 return -EADDRNOTAVAIL;
1604 }
1605 if (tb[IFLA_MTU]) {
1606 if (!is_valid_veth_mtu(nla_get_u32(tb[IFLA_MTU])))
1607 return -EINVAL;
1608 }
1609 return 0;
1610 }
1611
1612 static struct rtnl_link_ops veth_link_ops;
1613
veth_disable_gro(struct net_device * dev)1614 static void veth_disable_gro(struct net_device *dev)
1615 {
1616 dev->features &= ~NETIF_F_GRO;
1617 dev->wanted_features &= ~NETIF_F_GRO;
1618 netdev_update_features(dev);
1619 }
1620
veth_init_queues(struct net_device * dev,struct nlattr * tb[])1621 static int veth_init_queues(struct net_device *dev, struct nlattr *tb[])
1622 {
1623 int err;
1624
1625 if (!tb[IFLA_NUM_TX_QUEUES] && dev->num_tx_queues > 1) {
1626 err = netif_set_real_num_tx_queues(dev, 1);
1627 if (err)
1628 return err;
1629 }
1630 if (!tb[IFLA_NUM_RX_QUEUES] && dev->num_rx_queues > 1) {
1631 err = netif_set_real_num_rx_queues(dev, 1);
1632 if (err)
1633 return err;
1634 }
1635 return 0;
1636 }
1637
veth_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1638 static int veth_newlink(struct net *src_net, struct net_device *dev,
1639 struct nlattr *tb[], struct nlattr *data[],
1640 struct netlink_ext_ack *extack)
1641 {
1642 int err;
1643 struct net_device *peer;
1644 struct veth_priv *priv;
1645 char ifname[IFNAMSIZ];
1646 struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
1647 unsigned char name_assign_type;
1648 struct ifinfomsg *ifmp;
1649 struct net *net;
1650
1651 /*
1652 * create and register peer first
1653 */
1654 if (data != NULL && data[VETH_INFO_PEER] != NULL) {
1655 struct nlattr *nla_peer;
1656
1657 nla_peer = data[VETH_INFO_PEER];
1658 ifmp = nla_data(nla_peer);
1659 err = rtnl_nla_parse_ifinfomsg(peer_tb, nla_peer, extack);
1660 if (err < 0)
1661 return err;
1662
1663 err = veth_validate(peer_tb, NULL, extack);
1664 if (err < 0)
1665 return err;
1666
1667 tbp = peer_tb;
1668 } else {
1669 ifmp = NULL;
1670 tbp = tb;
1671 }
1672
1673 if (ifmp && tbp[IFLA_IFNAME]) {
1674 nla_strscpy(ifname, tbp[IFLA_IFNAME], IFNAMSIZ);
1675 name_assign_type = NET_NAME_USER;
1676 } else {
1677 snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
1678 name_assign_type = NET_NAME_ENUM;
1679 }
1680
1681 net = rtnl_link_get_net(src_net, tbp);
1682 if (IS_ERR(net))
1683 return PTR_ERR(net);
1684
1685 peer = rtnl_create_link(net, ifname, name_assign_type,
1686 &veth_link_ops, tbp, extack);
1687 if (IS_ERR(peer)) {
1688 put_net(net);
1689 return PTR_ERR(peer);
1690 }
1691
1692 if (!ifmp || !tbp[IFLA_ADDRESS])
1693 eth_hw_addr_random(peer);
1694
1695 if (ifmp && (dev->ifindex != 0))
1696 peer->ifindex = ifmp->ifi_index;
1697
1698 peer->gso_max_size = dev->gso_max_size;
1699 peer->gso_max_segs = dev->gso_max_segs;
1700
1701 err = register_netdevice(peer);
1702 put_net(net);
1703 net = NULL;
1704 if (err < 0)
1705 goto err_register_peer;
1706
1707 /* keep GRO disabled by default to be consistent with the established
1708 * veth behavior
1709 */
1710 veth_disable_gro(peer);
1711 netif_carrier_off(peer);
1712
1713 err = rtnl_configure_link(peer, ifmp);
1714 if (err < 0)
1715 goto err_configure_peer;
1716
1717 /*
1718 * register dev last
1719 *
1720 * note, that since we've registered new device the dev's name
1721 * should be re-allocated
1722 */
1723
1724 if (tb[IFLA_ADDRESS] == NULL)
1725 eth_hw_addr_random(dev);
1726
1727 if (tb[IFLA_IFNAME])
1728 nla_strscpy(dev->name, tb[IFLA_IFNAME], IFNAMSIZ);
1729 else
1730 snprintf(dev->name, IFNAMSIZ, DRV_NAME "%%d");
1731
1732 err = register_netdevice(dev);
1733 if (err < 0)
1734 goto err_register_dev;
1735
1736 netif_carrier_off(dev);
1737
1738 /*
1739 * tie the deviced together
1740 */
1741
1742 priv = netdev_priv(dev);
1743 rcu_assign_pointer(priv->peer, peer);
1744 err = veth_init_queues(dev, tb);
1745 if (err)
1746 goto err_queues;
1747
1748 priv = netdev_priv(peer);
1749 rcu_assign_pointer(priv->peer, dev);
1750 err = veth_init_queues(peer, tb);
1751 if (err)
1752 goto err_queues;
1753
1754 veth_disable_gro(dev);
1755 return 0;
1756
1757 err_queues:
1758 unregister_netdevice(dev);
1759 err_register_dev:
1760 /* nothing to do */
1761 err_configure_peer:
1762 unregister_netdevice(peer);
1763 return err;
1764
1765 err_register_peer:
1766 free_netdev(peer);
1767 return err;
1768 }
1769
veth_dellink(struct net_device * dev,struct list_head * head)1770 static void veth_dellink(struct net_device *dev, struct list_head *head)
1771 {
1772 struct veth_priv *priv;
1773 struct net_device *peer;
1774
1775 priv = netdev_priv(dev);
1776 peer = rtnl_dereference(priv->peer);
1777
1778 /* Note : dellink() is called from default_device_exit_batch(),
1779 * before a rcu_synchronize() point. The devices are guaranteed
1780 * not being freed before one RCU grace period.
1781 */
1782 RCU_INIT_POINTER(priv->peer, NULL);
1783 unregister_netdevice_queue(dev, head);
1784
1785 if (peer) {
1786 priv = netdev_priv(peer);
1787 RCU_INIT_POINTER(priv->peer, NULL);
1788 unregister_netdevice_queue(peer, head);
1789 }
1790 }
1791
1792 static const struct nla_policy veth_policy[VETH_INFO_MAX + 1] = {
1793 [VETH_INFO_PEER] = { .len = sizeof(struct ifinfomsg) },
1794 };
1795
veth_get_link_net(const struct net_device * dev)1796 static struct net *veth_get_link_net(const struct net_device *dev)
1797 {
1798 struct veth_priv *priv = netdev_priv(dev);
1799 struct net_device *peer = rtnl_dereference(priv->peer);
1800
1801 return peer ? dev_net(peer) : dev_net(dev);
1802 }
1803
veth_get_num_queues(void)1804 static unsigned int veth_get_num_queues(void)
1805 {
1806 /* enforce the same queue limit as rtnl_create_link */
1807 int queues = num_possible_cpus();
1808
1809 if (queues > 4096)
1810 queues = 4096;
1811 return queues;
1812 }
1813
1814 static struct rtnl_link_ops veth_link_ops = {
1815 .kind = DRV_NAME,
1816 .priv_size = sizeof(struct veth_priv),
1817 .setup = veth_setup,
1818 .validate = veth_validate,
1819 .newlink = veth_newlink,
1820 .dellink = veth_dellink,
1821 .policy = veth_policy,
1822 .maxtype = VETH_INFO_MAX,
1823 .get_link_net = veth_get_link_net,
1824 .get_num_tx_queues = veth_get_num_queues,
1825 .get_num_rx_queues = veth_get_num_queues,
1826 };
1827
1828 /*
1829 * init/fini
1830 */
1831
veth_init(void)1832 static __init int veth_init(void)
1833 {
1834 return rtnl_link_register(&veth_link_ops);
1835 }
1836
veth_exit(void)1837 static __exit void veth_exit(void)
1838 {
1839 rtnl_link_unregister(&veth_link_ops);
1840 }
1841
1842 module_init(veth_init);
1843 module_exit(veth_exit);
1844
1845 MODULE_DESCRIPTION("Virtual Ethernet Tunnel");
1846 MODULE_LICENSE("GPL v2");
1847 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1848