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