From patchwork Mon Feb 12 22:05:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13554114 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0DFC4C63F for ; Mon, 12 Feb 2024 22:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707775553; cv=none; b=DhhDTNtnOkP4bbNP+toqcTsV1wEkBTLF7wIFohSxXzfNqnQbptl1+y4F8VbLsFG7o2axaRNZwvQ1rtwBHTegaj+wadD9/LWtQHbIKOoxg1NUbhH55NPoD6G2CNJJV8VAX/SJSNOG0LYBXs8wFV9c3vEf0Kloq3wQEVZl5ZZrLds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707775553; c=relaxed/simple; bh=0MOnMdwRFX+dop9REMdKnrHyttPB/b1CWskzz7FnzdQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pvV3xMmnR+UymQ1p1EBqKkiI2Mnqi/rgmUO88LViIO0dim5Dv8wUIPlUPoiyUtGXPgvPWxYBrXtFcXplk5mV4ZyBLRLIqM43MF5Pa400+x4zY0PDPTb4fgyEaWS7J0eAtZ7oCTd4fQoxBLhAU4xNhNFRc5yQ2SdzU2kGzLW7au4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk; spf=none smtp.mailfrom=davidwei.uk; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b=eQ1cbXui; arc=none smtp.client-ip=209.85.210.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b="eQ1cbXui" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6e0dcf0a936so674168b3a.0 for ; Mon, 12 Feb 2024 14:05:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1707775551; x=1708380351; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tpvAV5bh7+9HTdlXs/ImEQRlIrypU0HqHp/bHD697NU=; b=eQ1cbXuiRPibLg0J1SvVVqmAM6FTKfPWknHq8wzRYg7MjOgoFjK54LnMCix3lY+71o 7S2YFqi3hd5cwEHp9FtpWos2aR0wA7yk1cdZYpS5jUDgSEgv+hiGvuS8kSdjgi8jtUoS K5S9ie/iwR0njN2Rsqw3z7WM5PKCulHwsE6vIAixqJI74DxRuiF9ODf0Oj/H+iHPlXBY w/kMOdYMKOuV0c2wOX6zk2gL5+v6DreJuVTqHjXY0LT7GLxSfB4TYZillKgSIJIjvIOM sQa65cvQJpU0Bz/JnTSWYLF5enbzqGZShh8eC7Aw57/rryF3Igx+LdWFvTKjn4MH3it3 6hKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707775551; x=1708380351; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tpvAV5bh7+9HTdlXs/ImEQRlIrypU0HqHp/bHD697NU=; b=CCOV5GAEZHR/jr8jVa2hp5FX6JG2ttX1CIyp7GCxJolbNe/wQ1tCGyIX7W0FGNA5nh YVxOS2UbCgtPjRSGI79mgZC3GwZD74wG4HyBGhIGxhL5zn3USicSyPQIVHCdGvJhpTFx 2E8GPHJ6ysTMm0z+bQAACL8sRZzA67qbZJYTnbbCvewsUM0a3uEXKUAJCQvV2MMeGTUi Uarpg9n+b7GZ3dZK8WgGenGeGscysQj1hldPFqmneYfdIceLhkq/xKqe//+5ZhykntGU yZGEv7wamYMOqIv83XLQH7SggHSxEnqJrtlF1cxuzjbpKwL9JCOLr1MgJJxWFVuXGk95 VZHw== X-Forwarded-Encrypted: i=1; AJvYcCX4rmJAriPvQ60rolHtadnr3HGvzx0uCEhZGFP+wvlOr9Qixo7sjN/UNMKAZRxQWXMCl8eN5v3hMVkSV4nsqNV1QsO9vcC0 X-Gm-Message-State: AOJu0YwtdxVZWWSnZzFcoXE5b850nbdt5k0nl4JL9ni7RcmXmt5VQ/99 61BAZn0o2k/eEho8FCewUBX67Bm7KJGMUkHgc52SPCob8kL4DBT9D0BEMYUhcmRbcg5QjpRXbcc E X-Google-Smtp-Source: AGHT+IH57iJZKan8ozxVeAe7lbGTM9rMOlObt/b5dhsUHUR3iNFjQz4mpstd1a7QzCVJH3tp84uQUA== X-Received: by 2002:aa7:980d:0:b0:6e0:eea0:3a34 with SMTP id e13-20020aa7980d000000b006e0eea03a34mr899222pfl.2.1707775551314; Mon, 12 Feb 2024 14:05:51 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVlNrf2sBPZHy0e7+BJNdfLXSj3hJ54wZky4YYnOa7GoJHfYoRCTwhh5OxZ3fv+55JLVebSr+SVLqo6EtCr3IEQXC02aO65qzYkuWLeDNZ/F4fQjGbYj/7Aq26DTaiaNVZ7X/0DyjWxOGrhphBC4C9nYsy9nAqlWqlbc1FoKhTTzPnfBwoBkjBuzejikiYAyBzulr9Y Received: from localhost (fwdproxy-prn-116.fbsv.net. [2a03:2880:ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id ln18-20020a056a003cd200b006e0e24c71d7sm2221598pfb.62.2024.02.12.14.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 14:05:51 -0800 (PST) From: David Wei To: Jakub Kicinski , Jiri Pirko , Sabrina Dubroca , netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH net-next v10 1/3] netdevsim: allow two netdevsim ports to be connected Date: Mon, 12 Feb 2024 14:05:42 -0800 Message-Id: <20240212220544.70546-2-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240212220544.70546-1-dw@davidwei.uk> References: <20240212220544.70546-1-dw@davidwei.uk> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add two netdevsim bus attribute to sysfs: /sys/bus/netdevsim/link_device /sys/bus/netdevsim/unlink_device Writing "A M B N" to link_device will link netdevsim M in netnsid A with netdevsim N in netnsid B. Writing "A M" to unlink_device will unlink netdevsim M in netnsid A from its peer, if any. rtnl_lock is taken to ensure nothing changes during the linking. Signed-off-by: David Wei --- drivers/net/netdevsim/bus.c | 135 ++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdev.c | 10 +++ drivers/net/netdevsim/netdevsim.h | 2 + 3 files changed, 147 insertions(+) diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index aedab1d9623a..819c74a7c625 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -232,9 +232,144 @@ 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); + 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_put_netns_a; + } + + 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_put_netns_a; + } + + 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_put_netns_b; + } + + 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_put_netns_b; + } + + if (dev_a == dev_b) { + pr_err("Cannot link a netdevsim to itself\n"); + goto out_put_netns_b; + } + + err = 0; + 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_put_netns_b; + } + + 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_put_netns_b; + } + + rcu_assign_pointer(nsim_a->peer, nsim_b); + rcu_assign_pointer(nsim_b->peer, nsim_a); + +out_put_netns_b: + put_net(ns_b); +out_put_netns_a: + 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; + } + + err = 0; + nsim = netdev_priv(dev); + peer = rtnl_dereference(nsim->peer); + if (!peer) + goto out_put_netns; + + 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); diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 77e8250282a5..9063f4f2971b 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -413,8 +413,13 @@ nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) 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 +432,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 028c825b86db..c8b45b0d955e 100644 --- a/drivers/net/netdevsim/netdevsim.h +++ b/drivers/net/netdevsim/netdevsim.h @@ -125,11 +125,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); From patchwork Mon Feb 12 22:05:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13554115 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E67D54CB22 for ; Mon, 12 Feb 2024 22:05:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707775554; cv=none; b=L84Wezk699HM1iNa9AjGyPSEK1O39iBiIvmNfACmFw1yto6J/GFLjA9PkETHz0U9HbOY6AXdQ8v8pqX7ct7uH864C6a9z6iMtyWKb/b7cXoa55PVU4izfUoDUSQddvoXm93Tc1rFHdcIY1vIRT9x1H8F6TE4m7BS+zcSeECkVAU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707775554; c=relaxed/simple; bh=WuVQ9tyy4qXVzj8fg5jT1T8KjKLN2WSR7176fQweBTk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lALQh2KKtHce/J3AIBH5ksiwoSCiDaITFp2N7McxSDdIkwm78tU2aNX0wK5fBGiYQsfKQWZMyAqgRf2hIGTPuBh/2B2nftpuH3nyc08aKGO2tYyGyeEYg0yuJ9mlABZeFMX9JvpCtz/g6YhDAP0s4P7bvA7CiQr/ZzzKFxIUxq8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk; spf=none smtp.mailfrom=davidwei.uk; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b=CO51u7KD; arc=none smtp.client-ip=209.85.214.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b="CO51u7KD" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d9b2400910so23888215ad.0 for ; Mon, 12 Feb 2024 14:05:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1707775552; x=1708380352; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PVNOE9NzOpHv+S2/C5c0rGW7n1qxuHkxnBJNavNR7kw=; b=CO51u7KDCAGXK4icFMP5kN+dUJyac2e04AZtcMQjqzytFLGtKz0xvD3j+9DB+ChXHD fI+nb8nfAMU7fEQw9AnheUoi9haBp43R2CHpaB31WDjupmKp3Ziv1RE0/Vqboq4uylFf liRhJGtFCYXuwOAkbjBnvHAqGgIBxG1ZN5eCmZ2RxqWtlYqKTtlq06dCsNgs1nbXqNJk 01P7SSTnjH69LyrvQLxtjpUUZ07iWjKK3UhyA9u4eaIszMS66ntjFMBaYKyr0opmg8jm rpjCo8Wsksmk0lUBxxE9UzD+Rrh/vT+a5TnugPf7ZJBJ6X0RIXQ63241jtFgLVXJ4Idb /Q1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707775552; x=1708380352; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PVNOE9NzOpHv+S2/C5c0rGW7n1qxuHkxnBJNavNR7kw=; b=lCHtbOQmFNnGKLHhYp3Ezy4OUUkrrVBWiRGMSzbR6HTfZigc7aya2KrJg0TIzgsi95 xjFiEZn2OtrMEXTjyAxpcAKotTYuq7DC/LfkhyUUyb6ZiS59Sa3c0W1RG8xJLcYAFgut Uvlp7F3IiRC7A/dVNAkY3bAhWsGROF8vwAuI5FtemZZof/tgPr/kc/yrh4SnEnGB4oty yLu5CdLKRU8lpwxQjGzrnTDGj5d+y2JGRuIfKPkxwQSpvwaWOL788l5uBWKTDfq1rHDk CssiFdIltmpLLjNNRkoNvZGeOFaCEcrbf4DYOf+0zQRS3VhKtIvoXaWzrewr97PcuklQ n03g== X-Forwarded-Encrypted: i=1; AJvYcCWiqvCwg94AjWGabAMEScU2QIIWfcLxaBA+uXwvRgqzbesMAavfMiQcMQ84zEqNb6Bfr+V/IYE7qc3hZpYmDmz0JvoKftON X-Gm-Message-State: AOJu0YwRbVDbJZEAV7rJUc/6lv5Qwz8Ttr5S+LrPFAclPjI7SJHcvsiT QKsJbSh1EhCys5jb1hpiWR0/VUm02bA/gy6VX4tJGvSaucTz2RTHQAEPtIk6EwU= X-Google-Smtp-Source: AGHT+IEpgBQg5SfXqypwtNJUkX6VoTn+E6MMOsjUyxyIQtua4QrR9IsoWFzTvP6hnjRxSU1Q9kMKTA== X-Received: by 2002:a17:902:cf42:b0:1d8:cfc9:a323 with SMTP id e2-20020a170902cf4200b001d8cfc9a323mr1106707plg.34.1707775552251; Mon, 12 Feb 2024 14:05:52 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWhlsFTf1oNWINmIAJCMDvNkkWn99i3/SIOS6ovhfcegLR8vECHB6hGJQbPMPzKEVu49yRmyGg2j7Z6/m7sZMFgIVrTxbftOLU/R3h0V4vEH4/U7i3znblSCKiKA8gxombFEonWLuTiWaxRvVH6BRBG4JmOyxhGmrzFrdRoS6Gj9EvzYO+QClBiaG/AV5ncmFd9jaN4 Received: from localhost (fwdproxy-prn-012.fbsv.net. [2a03:2880:ff:c::face:b00c]) by smtp.gmail.com with ESMTPSA id 5-20020a170902c10500b001d9b749d28bsm797159pli.285.2024.02.12.14.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 14:05:52 -0800 (PST) From: David Wei To: Jakub Kicinski , Jiri Pirko , Sabrina Dubroca , netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH net-next v10 2/3] netdevsim: forward skbs from one connected port to another Date: Mon, 12 Feb 2024 14:05:43 -0800 Message-Id: <20240212220544.70546-3-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240212220544.70546-1-dw@davidwei.uk> References: <20240212220544.70546-1-dw@davidwei.uk> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Forward skbs sent from one netdevsim port to its connected netdevsim port using dev_forward_skb, in a spirit similar to veth. Add a tx_dropped variable to struct netdevsim, tracking the number of skbs that could not be forwarded using dev_forward_skb(). The xmit() function accessing the peer ptr is protected by an RCU read critical section. The rcu_read_lock() is functionally redundant as since v5.0 all softirqs are implicitly RCU read critical sections; but it is useful for human readers. If another CPU is concurrently in nsim_destroy(), then it will first set the peer ptr to NULL. This does not affect any existing readers that dereferenced a non-NULL peer. Then, in unregister_netdevice(), there is a synchronize_rcu() before the netdev is actually unregistered and freed. This ensures that any readers i.e. xmit() that got a non-NULL peer will complete before the netdev is freed. Any readers after the RCU_INIT_POINTER() but before synchronize_rcu() will dereference NULL, making it safe. The codepath to nsim_destroy() and nsim_create() takes both the newly added nsim_dev_list_lock and rtnl_lock. This makes it safe with concurrent calls to linking two netdevsims together. Signed-off-by: David Wei --- drivers/net/netdevsim/netdev.c | 30 +++++++++++++++++++++++++----- drivers/net/netdevsim/netdevsim.h | 1 + 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 9063f4f2971b..d151859fa2c0 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -29,19 +29,39 @@ 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; + int ret = NETDEV_TX_OK; if (!nsim_ipsec_tx(ns, skb)) goto out; + rcu_read_lock(); + peer_ns = rcu_dereference(ns->peer); + if (!peer_ns) { + dev_kfree_skb(skb); + goto out_stats; + } + + skb_tx_timestamp(skb); + if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP)) + ret = NET_XMIT_DROP; + +out_stats: + rcu_read_unlock(); u64_stats_update_begin(&ns->syncp); - ns->tx_packets++; - ns->tx_bytes += skb->len; + if (ret == NET_XMIT_DROP) { + ns->tx_dropped++; + } else { + ns->tx_packets++; + ns->tx_bytes += len; + } u64_stats_update_end(&ns->syncp); + return ret; out: dev_kfree_skb(skb); - - return NETDEV_TX_OK; + return ret; } static void nsim_set_rx_mode(struct net_device *dev) @@ -70,6 +90,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)); } @@ -302,7 +323,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; diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h index c8b45b0d955e..553c4b9b4f63 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; From patchwork Mon Feb 12 22:05:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13554116 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAD164D584 for ; Mon, 12 Feb 2024 22:05:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707775555; cv=none; b=Sb0deK2SYC8mnRJdMKi2CrniX383cZBFqAmhrNPG6IZK+pgnLcuXExeMaXhrURLGVV+IaLWcbtPFoJIkcDRLesKhv40K0AxZAdaCptfxgPE16Wi66dBTjRWjq/NhD5xJsfqbbKD2JRmv4tIoTCwRYyhmJNYzjcOqJqtASVMVYbM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707775555; c=relaxed/simple; bh=fiA5KOUUpJT4R0pr92kiAeU8kaGWFhr4wAe6YC+qCwc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Op1NcsOiVG++x+fQnjpKt06ERjgB7Pknhk3OAosj8tl+jilnjTssV3/Lsgk9HZJo7fG1ERPnyaOBEl85pems8gpJCtRPJvNGdc86sEZYW7sQm0R8OeHV2NuPmuGlo5gXawbo18/eh3IaUAnAfM6R9NFnwgQiDusLkr9laZY1vy8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk; spf=none smtp.mailfrom=davidwei.uk; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b=a6jc+8Cp; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=davidwei.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=davidwei-uk.20230601.gappssmtp.com header.i=@davidwei-uk.20230601.gappssmtp.com header.b="a6jc+8Cp" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1d73066880eso34524045ad.3 for ; Mon, 12 Feb 2024 14:05:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1707775553; x=1708380353; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/zMatM8mJqDlYq7rZI8vigNbL9xkDJ4MAdk7FJc0Hmc=; b=a6jc+8CpQAgErtrKURkcikaI5ptL68ZpgNlwmq7TgzFiowZ7RLsqH3Iy/d0wA/V6JS N9WlbaTNUJRH24+5q7pdIMmdC5NZKRJ63fWvDzHuqDrkYRsuzqzlWtFTljKtjSIDhKNj rm6JQRo90CmOFitazo56vsYmTk25A2j+YbSdBtPt2AxingKu9g5FUNmOThIDDHph7SQ7 /ry4IjGV/1+U4EtoQoK5yPeu14SqM7XMdHEx1ohN02F5qvG/lzvasgvhqGDjEo0j1RZY z7516Ql6Fb8aIcWE4pLjguf1xVPMN52nl1H1h6hL3gcVR5ZNHNeiT4ZcQ25AX0cqghqV By6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707775553; x=1708380353; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/zMatM8mJqDlYq7rZI8vigNbL9xkDJ4MAdk7FJc0Hmc=; b=SrNsCj6xTuEPaIbCzJ/TlEiVY0HS11+y+v4j+7MrmwJlDWdt5MDnOYiRVvMHBgYPGd Et1qkPiOdjXNO5dBBYUUZxrMCBDVcHX+G1m3p27X89LzQdm7XnBpk7Lb+xRV4oZBr25Q PNS1YxOAA047lctyIVlsEDJCsli+BLOX5QMDpbpjezr4pzGyRvFtXY6s+lv8JJHKKBJ7 dFGp0NqCVEsxiBUj63rUYKl8wP4ltNgs/ln0CD2OjpB/6Tv1Ho6Ls9KohAWnd5hngwLH hCMSyFR1iG5QiKbhBSPUGMoTPgpcC/RCtgcs4Fv937qfOTD295pIQ6SvJGmMS8SqT5cT zMlw== X-Forwarded-Encrypted: i=1; AJvYcCXgXfFN5lCI4GsmcvWT7Dl0sL+cbjA/KByRAz7A18qd81ySPr1rQV5yURwwCEcvbap6gUskKJbiJKhDcqP4tbnn/6yB4N7x X-Gm-Message-State: AOJu0YwTT4FbiWXnQOvk68rYbQ+AGK1xeEKA5w1oxRSJczIhbfmYakuZ d/89jwi9waGdvIwfUO3Y8gS75eBwmvfT+KTKyRc2q1o1EfFuGe3bzs5nwPB5I00= X-Google-Smtp-Source: AGHT+IHtj76uoZJCkNWXEpiXARzZxDV0OCxAMOzV0ox24R4oUCBLCbW2rJRwubslv5Vo4su4NWAarA== X-Received: by 2002:a17:902:7c93:b0:1db:2ad9:9393 with SMTP id y19-20020a1709027c9300b001db2ad99393mr881406pll.48.1707775553211; Mon, 12 Feb 2024 14:05:53 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUkwUOvAbrK7wEGwRFzx/vnZMgqJxNs3h+6F4Led4yFb8kwgqjEqRcJRRNW1UxR75tFMmJdsjy9gnq2NRPtpi4XAr1f5SsCmiEdv9b0trPRHOIz4MyKoUrMzl9N7sCB/WBFDy0IKrWM0a/ppgDOsiDGO334LEzfmywMfuU72udkt/ml3uqiDMQyqjzCwxK6TNCfPUs0 Received: from localhost (fwdproxy-prn-010.fbsv.net. [2a03:2880:ff:a::face:b00c]) by smtp.gmail.com with ESMTPSA id r22-20020a170902be1600b001d9773a1993sm790221pls.213.2024.02.12.14.05.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 14:05:52 -0800 (PST) From: David Wei To: Jakub Kicinski , Jiri Pirko , Sabrina Dubroca , netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH net-next v10 3/3] netdevsim: add selftest for forwarding skb between connected ports Date: Mon, 12 Feb 2024 14:05:44 -0800 Message-Id: <20240212220544.70546-4-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240212220544.70546-1-dw@davidwei.uk> References: <20240212220544.70546-1-dw@davidwei.uk> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Connect two netdevsim ports in different namespaces together, then send packets between them using socat. Signed-off-by: David Wei --- .../selftests/drivers/net/netdevsim/Makefile | 1 + .../selftests/drivers/net/netdevsim/peer.sh | 138 ++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.sh diff --git a/tools/testing/selftests/drivers/net/netdevsim/Makefile b/tools/testing/selftests/drivers/net/netdevsim/Makefile index 7a29a05bea8b..5bace0b7fb57 100644 --- a/tools/testing/selftests/drivers/net/netdevsim/Makefile +++ b/tools/testing/selftests/drivers/net/netdevsim/Makefile @@ -10,6 +10,7 @@ TEST_PROGS = devlink.sh \ fib.sh \ hw_stats_l3.sh \ nexthop.sh \ + peer.sh \ psample.sh \ tc-mq-visibility.sh \ udp_tunnel_nic.sh \ diff --git a/tools/testing/selftests/drivers/net/netdevsim/peer.sh b/tools/testing/selftests/drivers/net/netdevsim/peer.sh new file mode 100755 index 000000000000..274e0d977975 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh @@ -0,0 +1,138 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +NSIM_DEV_1_ID=$((256 + RANDOM % 256)) +NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID +NSIM_DEV_2_ID=$((512 + RANDOM % 256)) +NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID + +NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device +NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device +NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device +NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device + +socat_check() +{ + if [ ! -x "$(command -v socat)" ]; then + echo "socat command not found. Skipping test" + return 1 + fi + + return 0 +} + +setup_ns() +{ + set -e + ip netns add nssv + ip netns add nscl + + NSIM_DEV_1_NAME=$(find $NSIM_DEV_1_SYS/net -maxdepth 1 -type d ! \ + -path $NSIM_DEV_1_SYS/net -exec basename {} \;) + NSIM_DEV_2_NAME=$(find $NSIM_DEV_2_SYS/net -maxdepth 1 -type d ! \ + -path $NSIM_DEV_2_SYS/net -exec basename {} \;) + + ip link set $NSIM_DEV_1_NAME netns nssv + ip link set $NSIM_DEV_2_NAME netns nscl + + ip netns exec nssv ip addr add '192.168.1.1/24' dev $NSIM_DEV_1_NAME + ip netns exec nscl ip addr add '192.168.1.2/24' dev $NSIM_DEV_2_NAME + + ip netns exec nssv ip link set dev $NSIM_DEV_1_NAME up + ip netns exec nscl ip link set dev $NSIM_DEV_2_NAME up + set +e +} + +cleanup_ns() +{ + ip netns del nscl + ip netns del nssv +} + +### +### Code start +### + +socat_check || exit 4 + +modprobe netdevsim + +# linking + +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW + +setup_ns + +NSIM_DEV_1_FD=$((256 + RANDOM % 256)) +exec {NSIM_DEV_1_FD} $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netdevsim should fail" + cleanup_ns + exit 1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netnsid should fail" + cleanup_ns + exit 1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with self should fail" + cleanup_ns + exit 1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK +if [ $? -ne 0 ]; then + echo "linking netdevsim1 with netdevsim2 should succeed" + cleanup_ns + exit 1 +fi + +# argument error checking + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:a" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "invalid arg should fail" + cleanup_ns + exit 1 +fi + +# send/recv packets + +tmp_file=$(mktemp) +ip netns exec nssv socat TCP-LISTEN:1234,fork $tmp_file & +pid=$! +res=0 + +echo "HI" | ip netns exec nscl socat STDIN TCP:192.168.1.1:1234 + +count=$(cat $tmp_file | wc -c) +if [[ $count -ne 3 ]]; then + echo "expected 3 bytes, got $count" + res=1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_UNLINK + +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_DEL + +kill $pid +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_DEL + +cleanup_ns + +modprobe -r netdevsim + +exit $res