From patchwork Thu Dec 14 21:24:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13493709 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 B2EE86DCE2 for ; Thu, 14 Dec 2023 21:24:54 +0000 (UTC) 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="Cyf3iykr" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-6d099d316a8so4811293b3a.0 for ; Thu, 14 Dec 2023 13:24:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1702589093; x=1703193893; 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=bfxMwaDm/UpAh8oyv+FTsTMGaJLyPSpoOmUmbTmoPUs=; b=Cyf3iykr9LHtJiO7lvTE5W1jYqxhZ/lGXBlUpgZtGVYuAurBydEEYyFi96oc36U79W QNN0t95661zD2t3Ut39NDy1AbYpaXS1iYMJKR5nu0I8bzzDLnVvSUJGwLuhN3/pn0u1V PDRYK1+12gYnbWvtVhNXWgTtoa39FJ/mI5HVUHROssvmcuN8WXjglJ/IdE5FA6jL6QqP hOMVq9KiYgU3GCnUfhT2zzuhTI1YZwCj+tPjXkeRCK0yOuOu/oFs5EwdE7ZB9kQ3ZXE1 aLi1q72mEmPKQrmOiI8OgItgSTVPbGPr1M7PstKEDf+BIUv/Frdcx4lyGaQgXDVkkBrf iDbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702589093; x=1703193893; 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=bfxMwaDm/UpAh8oyv+FTsTMGaJLyPSpoOmUmbTmoPUs=; b=HVtykzYXkL+ayzIVBRME1papt/OZOqvHR+v/Fjqfd34VaP1q3zwMqrMcgGVX7tPPy+ jEFHwLXTzUjNWWeKPz/KlnYnbuOlKR1ORMhrrStYCUVrGzXMjheyYSNp2Z3uWfMXaRjA EcTgl+tGJ2dr5E4a6SG4Awuk7kD8myg4p+d/dngwDR9PjF+2JoHZk+Ga9qsKNSda2OsQ DXgBct5TtCLrKD5nf9QGbYZGc5bhbSwYTC1LCJdJi3ilBzqhMZPvPukWE6ijvmH17q8l qNoLiYDjb/aQhnt2vL9HYeJ0d05v3wum4NYq5/6vL1+8J7x4KqWasq01Y+S4+URXGJeP KrOw== X-Gm-Message-State: AOJu0YwsM29bU8gzZ5BLtCnvUfwOUJMM133YfXxOsI0IpOJHrIAT2dr8 uwY6LgeOBUD+Orro6ek+nE+mdQ== X-Google-Smtp-Source: AGHT+IGgXqBBN+R+Av4CbpIbqg/owIy3ACk/aT0wccs7JZkjAcD5V5GenL7Ri4T50Xb8/ER3hYV7Kw== X-Received: by 2002:a05:6a00:ccd:b0:6ce:6f37:fa42 with SMTP id b13-20020a056a000ccd00b006ce6f37fa42mr13716761pfv.12.1702589093703; Thu, 14 Dec 2023 13:24:53 -0800 (PST) Received: from localhost (fwdproxy-prn-007.fbsv.net. [2a03:2880:ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id t27-20020a056a00139b00b006cd08377a13sm12173554pfg.190.2023.12.14.13.24.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 13:24:53 -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 v3 1/4] netdevsim: allow two netdevsim ports to be connected Date: Thu, 14 Dec 2023 13:24:40 -0800 Message-Id: <20231214212443.3638210-2-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231214212443.3638210-1-dw@davidwei.uk> References: <20231214212443.3638210-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 a debugfs file in /sys/kernel/debug/netdevsim/netdevsimN/ports/A/peer Writing "M B" to this file will link port A of netdevsim N with port B of netdevsim M. Reading this file will return the linked netdevsim id and port, if any. Signed-off-by: David Wei --- drivers/net/netdevsim/bus.c | 17 ++++++ drivers/net/netdevsim/dev.c | 88 +++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdev.c | 6 +++ drivers/net/netdevsim/netdevsim.h | 3 ++ 4 files changed, 114 insertions(+) diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index bcbc1e19edde..1ef95661a3f5 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -323,6 +323,23 @@ static struct device_driver nsim_driver = { .owner = THIS_MODULE, }; +struct nsim_bus_dev *nsim_bus_dev_get(unsigned int id) +{ + struct nsim_bus_dev *nsim_bus_dev; + + mutex_lock(&nsim_bus_dev_list_lock); + list_for_each_entry(nsim_bus_dev, &nsim_bus_dev_list, list) { + if (nsim_bus_dev->dev.id == id) { + get_device(&nsim_bus_dev->dev); + mutex_unlock(&nsim_bus_dev_list_lock); + return nsim_bus_dev; + } + } + mutex_unlock(&nsim_bus_dev_list_lock); + + return NULL; +} + int nsim_bus_init(void) { int err; diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c index b4d3b9cde8bd..034145ba1861 100644 --- a/drivers/net/netdevsim/dev.c +++ b/drivers/net/netdevsim/dev.c @@ -388,6 +388,91 @@ static const struct file_operations nsim_dev_rate_parent_fops = { .owner = THIS_MODULE, }; +static ssize_t nsim_dev_peer_read(struct file *file, char __user *data, + size_t count, loff_t *ppos) +{ + struct nsim_dev_port *nsim_dev_port; + struct netdevsim *peer; + unsigned int id, port; + char buf[23]; + ssize_t len; + + nsim_dev_port = file->private_data; + rcu_read_lock(); + peer = rcu_dereference(nsim_dev_port->ns->peer); + if (!peer) { + rcu_read_unlock(); + return 0; + } + + id = peer->nsim_bus_dev->dev.id; + port = peer->nsim_dev_port->port_index; + len = scnprintf(buf, sizeof(buf), "%u %u\n", id, port); + + rcu_read_unlock(); + return simple_read_from_buffer(data, count, ppos, buf, len); +} + +static ssize_t nsim_dev_peer_write(struct file *file, + const char __user *data, + size_t count, loff_t *ppos) +{ + struct nsim_dev_port *nsim_dev_port, *peer_dev_port; + struct nsim_bus_dev *peer_bus_dev; + struct nsim_dev *peer_dev; + unsigned int id, port; + char buf[22]; + ssize_t ret; + + if (count >= sizeof(buf)) + return -ENOSPC; + + ret = copy_from_user(buf, data, count); + if (ret) + return -EFAULT; + buf[count] = '\0'; + + ret = sscanf(buf, "%u %u", &id, &port); + if (ret != 2) { + pr_err("Format for adding a peer is \"id port\" (uint uint)"); + return -EINVAL; + } + + /* invalid netdevsim id */ + peer_bus_dev = nsim_bus_dev_get(id); + if (!peer_bus_dev) + return -EINVAL; + + ret = -EINVAL; + /* cannot link to self */ + nsim_dev_port = file->private_data; + if (nsim_dev_port->ns->nsim_bus_dev == peer_bus_dev && + nsim_dev_port->port_index == port) + goto out; + + peer_dev = dev_get_drvdata(&peer_bus_dev->dev); + list_for_each_entry(peer_dev_port, &peer_dev->port_list, list) { + if (peer_dev_port->port_index != port) + continue; + rcu_assign_pointer(nsim_dev_port->ns->peer, peer_dev_port->ns); + rcu_assign_pointer(peer_dev_port->ns->peer, nsim_dev_port->ns); + ret = count; + goto out; + } + +out: + put_device(&peer_bus_dev->dev); + return ret; +} + +static const struct file_operations nsim_dev_peer_fops = { + .open = simple_open, + .read = nsim_dev_peer_read, + .write = nsim_dev_peer_write, + .llseek = generic_file_llseek, + .owner = THIS_MODULE, +}; + static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) { @@ -418,6 +503,9 @@ static int nsim_dev_port_debugfs_init(struct nsim_dev *nsim_dev, } debugfs_create_symlink("dev", nsim_dev_port->ddir, dev_link_name); + debugfs_create_file("peer", 0600, nsim_dev_port->ddir, + nsim_dev_port, &nsim_dev_peer_fops); + return 0; } diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index aecaf5f44374..e290c54b0e70 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -388,6 +388,7 @@ nsim_create(struct nsim_dev *nsim_dev, struct nsim_dev_port *nsim_dev_port) ns->nsim_dev = nsim_dev; ns->nsim_dev_port = nsim_dev_port; ns->nsim_bus_dev = nsim_dev->nsim_bus_dev; + RCU_INIT_POINTER(ns->peer, NULL); SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev); SET_NETDEV_DEVLINK_PORT(dev, &nsim_dev_port->devlink_port); nsim_ethtool_init(ns); @@ -407,9 +408,14 @@ 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); + RCU_INIT_POINTER(ns->peer, NULL); unregister_netdevice(dev); + if (peer) + RCU_INIT_POINTER(peer->peer, NULL); if (nsim_dev_port_is_pf(ns->nsim_dev_port)) { nsim_macsec_teardown(ns); nsim_ipsec_teardown(ns); diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h index 028c825b86db..61ac3a80cf9a 100644 --- a/drivers/net/netdevsim/netdevsim.h +++ b/drivers/net/netdevsim/netdevsim.h @@ -125,6 +125,7 @@ struct netdevsim { } udp_ports; struct nsim_ethtool ethtool; + struct netdevsim __rcu *peer; }; struct netdevsim * @@ -415,5 +416,7 @@ struct nsim_bus_dev { bool init; }; +struct nsim_bus_dev *nsim_bus_dev_get(unsigned int id); + int nsim_bus_init(void); void nsim_bus_exit(void); From patchwork Thu Dec 14 21:24:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13493706 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 998846A34D for ; Thu, 14 Dec 2023 21:24:55 +0000 (UTC) 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="nyRKneGr" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1d0c4d84bf6so52743935ad.1 for ; Thu, 14 Dec 2023 13:24:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1702589095; x=1703193895; 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=3foAg4FsvwtEbfkJJ5e/Q650NfAxHXH88hs23zYw8ys=; b=nyRKneGrabCI5a/XdTxvTODFaRyyOQGxf3X+SfnRLgic8NFqH0utve/CuWcgM/3aDx YJ6d4/5Qyhh3g8YR5CWfnxtkFKxX0/yvOYt/WMG49J98WpMdc20J/FX3vIH+U+MfELm8 Qx7Cf+MBZoJYib3lsQv0+w5xf+zKjoXKXB8LIpyjM05++BqW7M71+Pf4abiW1kGPJBsn NTdzqmMDk98MvVKariYHhG7ohyhgiBgXe1tH5fotA+Uw9HmvEQif0hrZirN5jUH/0bnw HX1wlSkJWyLeF3+L1Y9HTMeXw1N7FAIwL+yxb/mqZsjWQqWBMMxUi+4Jk/+sstn+yfbd EW/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702589095; x=1703193895; 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=3foAg4FsvwtEbfkJJ5e/Q650NfAxHXH88hs23zYw8ys=; b=ji1FI5GwUDYopMUJjB06F+573ygzgetCwpPU5TsDJ5ekBuMIeHztzEf2rrLXsXswGF JhjoxS9uSjldOQTzSA5wB/zRN2VzIJvE2zJA4Ga7zMODEZo9RVEATpiOJodtQ9+Xlb/y LdQufm0EslSGXTWz6R/E0Ndem1gNHXW/m8W1+kUUEzemNua7y215dzx70/3JgUVs7LjA uXl3xMjeZS17IdTtd73bZ5ePXK9CEWyoZClf9k70n2LJjVFbw7OjGdtE1EUzZwNNegbu d48MCcMQER2i14mESz2u2KMHAoNLYwArEOVSu+lRSfE67UmjbruCXXVyfv8DguguK7J5 7rmQ== X-Gm-Message-State: AOJu0YyhnTR0b6A3DwytTu0ZheAarTT3L2BlXYz+s20c7CYpjUSmxm3L 7fjikBIQfqlpAriKHD9GRM5jYg== X-Google-Smtp-Source: AGHT+IExf7RtGyLo+Dgp5MVDXfE0p99qrWWuz1Qu//T5ZXqk4ex2NWm9TzKURRXVcMHEHx6P4qJl8A== X-Received: by 2002:a17:902:728e:b0:1d0:a35b:8cf0 with SMTP id d14-20020a170902728e00b001d0a35b8cf0mr5662225pll.132.1702589094693; Thu, 14 Dec 2023 13:24:54 -0800 (PST) Received: from localhost (fwdproxy-prn-012.fbsv.net. [2a03:2880:ff:c::face:b00c]) by smtp.gmail.com with ESMTPSA id c2-20020a170902848200b001d09c539c96sm1388102plo.229.2023.12.14.13.24.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 13:24:54 -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 v3 2/4] netdevsim: forward skbs from one connected port to another Date: Thu, 14 Dec 2023 13:24:41 -0800 Message-Id: <20231214212443.3638210-3-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231214212443.3638210-1-dw@davidwei.uk> References: <20231214212443.3638210-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. Signed-off-by: David Wei --- drivers/net/netdevsim/netdev.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index e290c54b0e70..c5f53b1dbdcc 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -29,19 +29,33 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) { struct netdevsim *ns = netdev_priv(dev); + struct netdevsim *peer_ns; + int ret = NETDEV_TX_OK; + rcu_read_lock(); if (!nsim_ipsec_tx(ns, skb)) - goto out; + goto err; u64_stats_update_begin(&ns->syncp); ns->tx_packets++; ns->tx_bytes += skb->len; u64_stats_update_end(&ns->syncp); -out: - dev_kfree_skb(skb); + peer_ns = rcu_dereference(ns->peer); + if (!peer_ns) + goto err; + + skb_tx_timestamp(skb); + if (unlikely(dev_forward_skb(peer_ns->netdev, skb) == NET_RX_DROP)) + ret = NET_XMIT_DROP; - return NETDEV_TX_OK; + rcu_read_unlock(); + return ret; + +err: + rcu_read_unlock(); + dev_kfree_skb(skb); + return ret; } static void nsim_set_rx_mode(struct net_device *dev) @@ -302,7 +316,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; From patchwork Thu Dec 14 21:24:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13493708 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.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 9467268B9A for ; Thu, 14 Dec 2023 21:24:56 +0000 (UTC) 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="y0KFBQhl" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1d35569b8c6so14325585ad.3 for ; Thu, 14 Dec 2023 13:24:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1702589095; x=1703193895; 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=DhP0/KeAkEXkYbNZIC9cYiWhoMF1Ptz+Mj0ZXLXOzVQ=; b=y0KFBQhlxNF3FnerE1gi9SHKzQgLLwxFtUd2JuKE53IXwR8srIRj88GdlKaPQ29+Ls 37D0w3u6T3YsZxPqp3Lt99wH20xnnTLUAZStioIsBSX7Tg9q/tzssZDVUDfgowJLejEu rjCX1p3OJ/ow5+bQtdPymUv5ZuT95q3WJKbVzuLnHMYmsOu7M+/wrXJY+ZhETJa05uLx 038U/kwEJWiELQTDCc3diK40+jbCOP/Gh8kQni9gzfEe8NQGIOPk5N9ZHF2/mV0KovET fdHkE1knGVqQTHfrqNJzYKhOQWlA6ATW0mQkMGbWbWgApezil724GNZWxV7r2kRgkWMc 79Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702589095; x=1703193895; 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=DhP0/KeAkEXkYbNZIC9cYiWhoMF1Ptz+Mj0ZXLXOzVQ=; b=DkZsZ3rm5YQp9uiD1aMQGVUjapPWMybmmeB860Ij/WF/iXMwgGR0g696PRO/Vi5XFF ryYblViEP8vRHFjgpPHrIMEdPZ+ToTHSLNLsEDeAx/dQOt9h3xefKwD6W8zMVWT8CQuy EkUEC181ZpADF5S82SlZ9qbF11XF/uoRUBKR7xH5LkUpUni1NkoJv2Izce4vftntTb0+ 5lUdMh3E7RVcCL6ZHalN+ppwJ466GHz7Ej3gfIykAyf3bisUSXLzy1bGF/mpVu2+bE8v GPlfwQ3mvujUDYe6ez0AY4h6EwHm1uk/9KXnrW4gK+O3xDraaxfB2sHGsibsMuH89Y9a nQug== X-Gm-Message-State: AOJu0YxKb5Na6CcabfawKfPNK2k+3bFYuTV349Kb3n6QGkiqW57dSwux DwXpMWcRKi0MsZnyZqtMXYHiWw== X-Google-Smtp-Source: AGHT+IFB+lAYzXehEFCIH0SbCX1pzt8YnQ86sRciI2rf0ZS5uc5FLCoQnBPqpzRA008ks9hO77CZrQ== X-Received: by 2002:a17:902:784a:b0:1d0:4778:fb3f with SMTP id e10-20020a170902784a00b001d04778fb3fmr4902602pln.32.1702589095684; Thu, 14 Dec 2023 13:24:55 -0800 (PST) Received: from localhost (fwdproxy-prn-018.fbsv.net. [2a03:2880:ff:12::face:b00c]) by smtp.gmail.com with ESMTPSA id j18-20020a170902c3d200b001d35e9471absm2603219plj.195.2023.12.14.13.24.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 13:24:55 -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 v3 3/4] netdevsim: add selftest for forwarding skb between connected ports Date: Thu, 14 Dec 2023 13:24:42 -0800 Message-Id: <20231214212443.3638210-4-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231214212443.3638210-1-dw@davidwei.uk> References: <20231214212443.3638210-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/peer.sh | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.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..942526640d59 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh @@ -0,0 +1,123 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +NSIM_DEV_1_ID=$((RANDOM % 1024)) +NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID +NSIM_DEV_1_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_DEV_1_ID +NSIM_DEV_2_ID=$((RANDOM % 1024)) +NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID +NSIM_DEV_2_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_DEV_2_ID + +NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device +NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_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 +### + +modprobe netdevsim + +# linking + +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW + +echo "$NSIM_DEV_2_ID 0" > ${NSIM_DEV_1_DFS}/ports/0/peer 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netdevsim should fail" + exit 1 +fi + +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW + +echo "$NSIM_DEV_2_ID 0" > ${NSIM_DEV_1_DFS}/ports/0/peer +if [ $? -ne 0 ]; then + echo "linking netdevsim1 port0 with netdevsim2 port0 should succeed" + exit 1 +fi + +# argument error checking + +echo "$NSIM_DEV_2_ID 1" > ${NSIM_DEV_1_DFS}/ports/0/peer 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent port in a netdevsim should fail" + exit 1 +fi + +echo "$NSIM_DEV_1_ID 0" > ${NSIM_DEV_1_DFS}/ports/0/peer 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with self should fail" + exit 1 +fi + +echo "$NSIM_DEV_2_ID a" > ${NSIM_DEV_1_DFS}/ports/0/peer 2>/dev/null +if [ $? -eq 0 ]; then + echo "invalid arg should fail" + exit 1 +fi + +# send/recv packets + +socat_check || exit 4 + +setup_ns + +tmp_file=$(mktemp) +ip netns exec nssv socat TCP-LISTEN:1234,fork $tmp_file & +pid=$! + +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" + exit 1 +fi + +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 0 From patchwork Thu Dec 14 21:24:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13493707 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 A5FD96AB80 for ; Thu, 14 Dec 2023 21:24:57 +0000 (UTC) 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="MhMpjkgZ" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6ce939ecfc2so8180940b3a.2 for ; Thu, 14 Dec 2023 13:24:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1702589097; x=1703193897; 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=3V2M0TkGcjsOwvR9lkJ0gKcZe2oUBlp6oN2aOhqh2f8=; b=MhMpjkgZr86K7R1FRht0N1fb96QQSKyF4PlTrv07PsKN1mfo83fF27AqbHIfyegafV 8sptKfBl2gh5dzPiJLP5U6OlELyKH45oIlrvofnbo29iw1nh3EyiNUpdSaVcw4C+t4B0 mDyr1ed3opYL7yj2Opaq7fhBswxsGiOXxJhQJIcv+L19fyk1Cle0OAJBflyqzy2sxpY/ /XvqQwNNCjV7BQ45INBhKo+61lbUeg2CC+AdF+ajSFKRX+mnkQhiIgf/UFpnwz2qMMxi 8J+CqBQzL5+f6lLgZBKV+EhwpybpQKXYWY5FS1VsxtLrRR61VmOxumMEG2TtiRO43XL6 A5nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702589097; x=1703193897; 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=3V2M0TkGcjsOwvR9lkJ0gKcZe2oUBlp6oN2aOhqh2f8=; b=Xkc61DHEOyg0xh+xaDkabldPNLXpEUyof4Bv4+DBDOnHw565e2AW7GLmWeU9rTx4Ca P4aOlXishd+DG50HvPnH7e/qxd5qLzS/DOOKDC9JDOBWW48ebkWEh13LpQItTN4rMDxa 7qpL0ya/Pfu4UzK/swliGKdaEMajqoK5+lBZL3r9vdAVBvsow04Tg2JUXH0sridhbrGq BvfNcgUfl/L9ZI7Qm80F/B+iK9yGyfXqz1vA/YwbhPhctW+XewrcLxp76tAj2Q2pxW/4 7kkloL1qzEaJdstv5nqnF7xmzhOs/BGjR/z00y5G4LWX1z+9N7Rx9Sot9Dpt7GCC307F pzkg== X-Gm-Message-State: AOJu0YzHvmJ/V/qzbZHZvZ13zttvcFZ0MxsWMqe6XkkdAnjOnsLYsyxS EGJoiwUWni+eMqn2RE3AWJbGOA== X-Google-Smtp-Source: AGHT+IEcb3NHJbQUK7LaZGs53Is/RasWpVu56PxuoxOCVIPhdO8SPh1F/zn0bpJcxt7osGhlahqdOg== X-Received: by 2002:a05:6a00:14cb:b0:6d0:d864:219d with SMTP id w11-20020a056a0014cb00b006d0d864219dmr3605140pfu.3.1702589096709; Thu, 14 Dec 2023 13:24:56 -0800 (PST) Received: from localhost (fwdproxy-prn-019.fbsv.net. [2a03:2880:ff:13::face:b00c]) by smtp.gmail.com with ESMTPSA id d4-20020a056a0010c400b006ce7344328asm12199901pfu.77.2023.12.14.13.24.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 13:24:56 -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 v3 4/4] netdevsim: add Makefile for selftests Date: Thu, 14 Dec 2023 13:24:43 -0800 Message-Id: <20231214212443.3638210-5-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231214212443.3638210-1-dw@davidwei.uk> References: <20231214212443.3638210-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 a Makefile for netdevsim selftests and add selftests path to MAINTAINERS # make run_tests ... # timeout set to 45 # selftests: drivers/net/netdevsim: peer.sh # modprobe: FATAL: Module netdevsim is builtin. ok 11 selftests: drivers/net/netdevsim: peer.sh Signed-off-by: David Wei --- MAINTAINERS | 1 + .../selftests/drivers/net/netdevsim/Makefile | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 tools/testing/selftests/drivers/net/netdevsim/Makefile diff --git a/MAINTAINERS b/MAINTAINERS index a151988646fe..cfa0804772b5 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -14899,6 +14899,7 @@ NETDEVSIM M: Jakub Kicinski S: Maintained F: drivers/net/netdevsim/* +F: tools/testing/selftests/drivers/net/netdevsim/* NETEM NETWORK EMULATOR M: Stephen Hemminger diff --git a/tools/testing/selftests/drivers/net/netdevsim/Makefile b/tools/testing/selftests/drivers/net/netdevsim/Makefile new file mode 100644 index 000000000000..5bace0b7fb57 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/Makefile @@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0+ OR MIT + +TEST_PROGS = devlink.sh \ + devlink_in_netns.sh \ + devlink_trap.sh \ + ethtool-coalesce.sh \ + ethtool-fec.sh \ + ethtool-pause.sh \ + ethtool-ring.sh \ + fib.sh \ + hw_stats_l3.sh \ + nexthop.sh \ + peer.sh \ + psample.sh \ + tc-mq-visibility.sh \ + udp_tunnel_nic.sh \ + +include ../../../lib.mk