From 34996e42f82bfd60bc2c191e5cae3c6ab233ec6c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 7 Aug 2024 15:11:27 +0200 Subject: Merging upstream version 6.9.7. Signed-off-by: Daniel Baumann --- drivers/net/netdevsim/bus.c | 149 +++++++++++++++++++++++++++++++++++++- drivers/net/netdevsim/netdev.c | 54 ++++++++++++-- drivers/net/netdevsim/netdevsim.h | 3 + 3 files changed, 199 insertions(+), 7 deletions(-) (limited to 'drivers/net/netdevsim') diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index bcbc1e19ed..64c0cdd31b 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -129,7 +129,7 @@ static void nsim_bus_dev_release(struct device *dev) complete(&nsim_bus_devs_released); } -static struct device_type nsim_bus_dev_type = { +static const struct device_type nsim_bus_dev_type = { .groups = nsim_bus_dev_attr_groups, .release = nsim_bus_dev_release, }; @@ -232,9 +232,154 @@ del_device_store(const struct bus_type *bus, const char *buf, size_t count) } static BUS_ATTR_WO(del_device); +static ssize_t link_device_store(const struct bus_type *bus, const char *buf, size_t count) +{ + struct netdevsim *nsim_a, *nsim_b, *peer; + struct net_device *dev_a, *dev_b; + unsigned int ifidx_a, ifidx_b; + int netnsfd_a, netnsfd_b, err; + struct net *ns_a, *ns_b; + + err = sscanf(buf, "%d:%u %d:%u", &netnsfd_a, &ifidx_a, &netnsfd_b, + &ifidx_b); + if (err != 4) { + pr_err("Format for linking two devices is \"netnsfd_a:ifidx_a netnsfd_b:ifidx_b\" (int uint int uint).\n"); + return -EINVAL; + } + + ns_a = get_net_ns_by_fd(netnsfd_a); + if (IS_ERR(ns_a)) { + pr_err("Could not find netns with fd: %d\n", netnsfd_a); + return -EINVAL; + } + + ns_b = get_net_ns_by_fd(netnsfd_b); + if (IS_ERR(ns_b)) { + pr_err("Could not find netns with fd: %d\n", netnsfd_b); + put_net(ns_a); + return -EINVAL; + } + + err = -EINVAL; + rtnl_lock(); + dev_a = __dev_get_by_index(ns_a, ifidx_a); + if (!dev_a) { + pr_err("Could not find device with ifindex %u in netnsfd %d\n", + ifidx_a, netnsfd_a); + goto out_err; + } + + if (!netdev_is_nsim(dev_a)) { + pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n", + ifidx_a, netnsfd_a); + goto out_err; + } + + dev_b = __dev_get_by_index(ns_b, ifidx_b); + if (!dev_b) { + pr_err("Could not find device with ifindex %u in netnsfd %d\n", + ifidx_b, netnsfd_b); + goto out_err; + } + + if (!netdev_is_nsim(dev_b)) { + pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n", + ifidx_b, netnsfd_b); + goto out_err; + } + + if (dev_a == dev_b) { + pr_err("Cannot link a netdevsim to itself\n"); + goto out_err; + } + + err = -EBUSY; + nsim_a = netdev_priv(dev_a); + peer = rtnl_dereference(nsim_a->peer); + if (peer) { + pr_err("Netdevsim %d:%u is already linked\n", netnsfd_a, + ifidx_a); + goto out_err; + } + + nsim_b = netdev_priv(dev_b); + peer = rtnl_dereference(nsim_b->peer); + if (peer) { + pr_err("Netdevsim %d:%u is already linked\n", netnsfd_b, + ifidx_b); + goto out_err; + } + + err = 0; + rcu_assign_pointer(nsim_a->peer, nsim_b); + rcu_assign_pointer(nsim_b->peer, nsim_a); + +out_err: + put_net(ns_b); + put_net(ns_a); + rtnl_unlock(); + + return !err ? count : err; +} +static BUS_ATTR_WO(link_device); + +static ssize_t unlink_device_store(const struct bus_type *bus, const char *buf, size_t count) +{ + struct netdevsim *nsim, *peer; + struct net_device *dev; + unsigned int ifidx; + int netnsfd, err; + struct net *ns; + + err = sscanf(buf, "%u:%u", &netnsfd, &ifidx); + if (err != 2) { + pr_err("Format for unlinking a device is \"netnsfd:ifidx\" (int uint).\n"); + return -EINVAL; + } + + ns = get_net_ns_by_fd(netnsfd); + if (IS_ERR(ns)) { + pr_err("Could not find netns with fd: %d\n", netnsfd); + return -EINVAL; + } + + err = -EINVAL; + rtnl_lock(); + dev = __dev_get_by_index(ns, ifidx); + if (!dev) { + pr_err("Could not find device with ifindex %u in netnsfd %d\n", + ifidx, netnsfd); + goto out_put_netns; + } + + if (!netdev_is_nsim(dev)) { + pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n", + ifidx, netnsfd); + goto out_put_netns; + } + + nsim = netdev_priv(dev); + peer = rtnl_dereference(nsim->peer); + if (!peer) + goto out_put_netns; + + err = 0; + RCU_INIT_POINTER(nsim->peer, NULL); + RCU_INIT_POINTER(peer->peer, NULL); + +out_put_netns: + put_net(ns); + rtnl_unlock(); + + return !err ? count : err; +} +static BUS_ATTR_WO(unlink_device); + static struct attribute *nsim_bus_attrs[] = { &bus_attr_new_device.attr, &bus_attr_del_device.attr, + &bus_attr_link_device.attr, + &bus_attr_unlink_device.attr, NULL }; ATTRIBUTE_GROUPS(nsim_bus); @@ -260,7 +405,7 @@ static int nsim_num_vf(struct device *dev) return nsim_bus_dev->num_vfs; } -static struct bus_type nsim_bus = { +static const struct bus_type nsim_bus = { .name = DRV_NAME, .dev_name = DRV_NAME, .bus_groups = nsim_bus_groups, diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 77e8250282..d405809030 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -29,18 +29,35 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) { struct netdevsim *ns = netdev_priv(dev); + unsigned int len = skb->len; + struct netdevsim *peer_ns; + rcu_read_lock(); if (!nsim_ipsec_tx(ns, skb)) - goto out; + goto out_drop_free; + peer_ns = rcu_dereference(ns->peer); + if (!peer_ns) + goto out_drop_free; + + skb_tx_timestamp(skb); + if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP)) + goto out_drop_cnt; + + rcu_read_unlock(); u64_stats_update_begin(&ns->syncp); ns->tx_packets++; - ns->tx_bytes += skb->len; + ns->tx_bytes += len; u64_stats_update_end(&ns->syncp); + return NETDEV_TX_OK; -out: +out_drop_free: dev_kfree_skb(skb); - +out_drop_cnt: + rcu_read_unlock(); + u64_stats_update_begin(&ns->syncp); + ns->tx_dropped++; + u64_stats_update_end(&ns->syncp); return NETDEV_TX_OK; } @@ -70,6 +87,7 @@ nsim_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats) start = u64_stats_fetch_begin(&ns->syncp); stats->tx_bytes = ns->tx_bytes; stats->tx_packets = ns->tx_packets; + stats->tx_dropped = ns->tx_dropped; } while (u64_stats_fetch_retry(&ns->syncp, start)); } @@ -265,6 +283,22 @@ nsim_set_features(struct net_device *dev, netdev_features_t features) return 0; } +static int nsim_get_iflink(const struct net_device *dev) +{ + struct netdevsim *nsim, *peer; + int iflink; + + nsim = netdev_priv(dev); + + rcu_read_lock(); + peer = rcu_dereference(nsim->peer); + iflink = peer ? READ_ONCE(peer->netdev->ifindex) : + READ_ONCE(dev->ifindex); + rcu_read_unlock(); + + return iflink; +} + static const struct net_device_ops nsim_netdev_ops = { .ndo_start_xmit = nsim_start_xmit, .ndo_set_rx_mode = nsim_set_rx_mode, @@ -282,6 +316,7 @@ static const struct net_device_ops nsim_netdev_ops = { .ndo_set_vf_rss_query_en = nsim_set_vf_rss_query_en, .ndo_setup_tc = nsim_setup_tc, .ndo_set_features = nsim_set_features, + .ndo_get_iflink = nsim_get_iflink, .ndo_bpf = nsim_bpf, }; @@ -302,7 +337,6 @@ static void nsim_setup(struct net_device *dev) eth_hw_addr_random(dev); dev->tx_queue_len = 0; - dev->flags |= IFF_NOARP; dev->flags &= ~IFF_MULTICAST; dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE; @@ -413,8 +447,13 @@ err_free_netdev: void nsim_destroy(struct netdevsim *ns) { struct net_device *dev = ns->netdev; + struct netdevsim *peer; rtnl_lock(); + peer = rtnl_dereference(ns->peer); + if (peer) + RCU_INIT_POINTER(peer->peer, NULL); + RCU_INIT_POINTER(ns->peer, NULL); unregister_netdevice(dev); if (nsim_dev_port_is_pf(ns->nsim_dev_port)) { nsim_macsec_teardown(ns); @@ -427,6 +466,11 @@ void nsim_destroy(struct netdevsim *ns) free_netdev(dev); } +bool netdev_is_nsim(struct net_device *dev) +{ + return dev->netdev_ops == &nsim_netdev_ops; +} + static int nsim_validate(struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h index 028c825b86..553c4b9b4f 100644 --- a/drivers/net/netdevsim/netdevsim.h +++ b/drivers/net/netdevsim/netdevsim.h @@ -98,6 +98,7 @@ struct netdevsim { u64 tx_packets; u64 tx_bytes; + u64 tx_dropped; struct u64_stats_sync syncp; struct nsim_bus_dev *nsim_bus_dev; @@ -125,11 +126,13 @@ struct netdevsim { } udp_ports; struct nsim_ethtool ethtool; + struct netdevsim __rcu *peer; }; struct netdevsim * nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port); void nsim_destroy(struct netdevsim *ns); +bool netdev_is_nsim(struct net_device *dev); void nsim_ethtool_init(struct netdevsim *ns); -- cgit v1.2.3