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