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