From patchwork Sun Dec 10 01:04:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13486264 X-Patchwork-Delegate: kuba@kernel.org 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="kA7dJMJq" Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AD26FE for ; Sat, 9 Dec 2023 17:04:54 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-286d6c95b8cso3262115a91.0 for ; Sat, 09 Dec 2023 17:04:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1702170294; x=1702775094; 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=Urjueb9GWfRKCUtnogLF8xlESFGo3375daOG48rl+n0=; b=kA7dJMJqHcphGYcyhCTK4qDQbIwqLmke6Km/fuQYp6uvyzdoeI19JJTP1+Sp8LHfnz IOhDCgYpyVNQXPMqA5JpXgNu2hVTPuX/QlqKSEiKcHw92DoS0yRu8LldBeCSgjBxFgV+ LtVZ/Y1JmSkXzrLf4GBEQCTcm4yKpiVMlAgFOqtPHVon2J89Or0B/b4b5ulGus5DjQUj cMveKEJiNzkHypZkLUhVPWkcJvtHQMPFVPPZJzbDdeDTcn51R58GG98R9xetYhS0DeE/ U9rPVfsuL3M7CrgenrGcaM0oxt4fpnH09rdSudZo4R3IkhRlhqSbiWujr6NpcIPuNneq LTTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702170294; x=1702775094; 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=Urjueb9GWfRKCUtnogLF8xlESFGo3375daOG48rl+n0=; b=bq9HvN0qBk++Q8bEVOIjz7pJg31vyvxG6GVbTqHblCxzItiZRy/LsOMst9AcEL37vJ sKtSkDAVJdePrOF2Xv0Gpnh0Xxm4A0NyUJcfnmhlYIXFI0xnUFty/tQoUhl2bEx/kqzD 2P/m2qc7lqhMz1ql6yUXo3XsyZ+2UwlFDY5AhvnnsJw/OftD98d8SaxxC1ietJ6M8bAq M+nYPCz7XyiHoF4W+Sk7j9rAOi4/+egciyTFMHoNSWh397hf64LYdYTzZRcKcT9lFew3 w3IBy83+P8+bijAyfjBYgegx30TNyxTGTRbstFP7/DpqPEUcc0089wIxlMJraUDflvJL EJkQ== X-Gm-Message-State: AOJu0Yy1JptBLCBriioD5FB+HtVN9LjMqKIBI6jaDXSWiYZadH8+Rhxv x7W8KRAyDz3H1YW3Gg6wIMtsqw== X-Google-Smtp-Source: AGHT+IGNuP18hFe07NdXZmxdkToykSf4D7MhH8ZcS96adOTD3N6M3deg5Hj8VlHgR6til4eYANyeFQ== X-Received: by 2002:a17:902:7e82:b0:1d0:9416:efb7 with SMTP id z2-20020a1709027e8200b001d09416efb7mr2383123pla.108.1702170293717; Sat, 09 Dec 2023 17:04:53 -0800 (PST) Received: from localhost (fwdproxy-prn-019.fbsv.net. [2a03:2880:ff:13::face:b00c]) by smtp.gmail.com with ESMTPSA id x24-20020a170902821800b001d0c418174fsm3933659pln.117.2023.12.09.17.04.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Dec 2023 17:04: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 v2 1/3] netdevsim: allow two netdevsim ports to be connected Date: Sat, 9 Dec 2023 17:04:46 -0800 Message-Id: <20231210010448.816126-2-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231210010448.816126-1-dw@davidwei.uk> References: <20231210010448.816126-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 From: David Wei 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 | 10 ++++ drivers/net/netdevsim/dev.c | 85 +++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdev.c | 6 +++ drivers/net/netdevsim/netdevsim.h | 3 ++ 4 files changed, 104 insertions(+) diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index bcbc1e19edde..3e4378e9dbee 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -364,3 +364,13 @@ void nsim_bus_exit(void) driver_unregister(&nsim_driver); bus_unregister(&nsim_bus); } + +struct nsim_bus_dev *nsim_bus_dev_get(unsigned int id) +{ + struct nsim_bus_dev *nsim_bus_dev; + list_for_each_entry(nsim_bus_dev, &nsim_bus_dev_list, list) { + if (nsim_bus_dev->dev.id == id) + return nsim_bus_dev; + } + return NULL; +} diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c index b4d3b9cde8bd..7af219ff6fa9 100644 --- a/drivers/net/netdevsim/dev.c +++ b/drivers/net/netdevsim/dev.c @@ -388,6 +388,88 @@ 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) { + len = scnprintf(buf, sizeof(buf), "\n"); + goto out; + } + + id = peer->nsim_bus_dev->dev.id; + port = peer->nsim_dev_port->port_index; + len = scnprintf(buf, sizeof(buf), "%u %u\n", id, port); + +out: + 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; + + /* 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) + return -EINVAL; + + 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); + return count; + } + + return -EINVAL; +} + +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 +500,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..2601c3ad1d17 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 * @@ -417,3 +418,5 @@ struct nsim_bus_dev { int nsim_bus_init(void); void nsim_bus_exit(void); + +struct nsim_bus_dev *nsim_bus_dev_get(unsigned int id); From patchwork Sun Dec 10 01:04:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13486263 X-Patchwork-Delegate: kuba@kernel.org 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="2LaqxtRT" Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D1C910B for ; Sat, 9 Dec 2023 17:04:55 -0800 (PST) Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-35f3e4ce411so6587475ab.0 for ; Sat, 09 Dec 2023 17:04:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1702170294; x=1702775094; 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=WA4d9nyIn2ziQ9SblDQqTiSyrr25pUtyIEL1aLjRQfs=; b=2LaqxtRTco/B3qw9aRu73MzmWtMLfNoafZAzDK0ugGuyRxQLJrUDUVfpgpvU9r0y+C uUaD8R7D4iddnWqxR+Wz6r0JhQXEtfrJmbKgyPqVex+W69PouV7mH8h+Bh717OZb658t wliA8Svxsk5djGMihQceD41FNWAbMiysVXIC/+VFTnkHua6M4+0q5BKkvfAySHDsq+5H MmjX9LMi/G35Cue8NsGR0G2NmyGtarADsEWnCeNXdBWWLp6e10Hb/rZygP+5MSbR8LLK a03Yb6AVcxZF117LcHZouj2w/b2PSr6965Ox6zO9jdhFxOPrVys9baDVj/n1TbGU5E+F SYGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702170294; x=1702775094; 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=WA4d9nyIn2ziQ9SblDQqTiSyrr25pUtyIEL1aLjRQfs=; b=WNdUV+TS1DzmGpAGzUmx+7wIhjqgcpkFVtN12baYojnqFVZAlPsV+KX8eESmJh1Y+x sDXpmVLl9OLOZNDlP/7AILhB+xffH8aL2WpBqwApqAr0DldEydreukx9drN7mHDPLLta vbqAWm3rNc2hYCpWR0lVq3UNPEriPql4cjXAk2Kw+sZj/C516dbBrvfwb+cKODU2D55j 1hyPYCWvOmr7NsFHdkHPAnYxrHbgRpAsCPK60NpWhpf3yTrS/H5zTcS5/SwnBQuLp9L0 H6cjc03ty89HU42J5s6JTiEHAHoGZHdKgaQZ6u6cXeZVDqdd/K8jG9pEMi5MzXvzEWTv G/PA== X-Gm-Message-State: AOJu0YzHrLEFKbZEn3NsvjcSaFbfGYvFVMF7Mx9anSjBTGMTajDAXT4M HCwo6zo0zKLXz25BhDlZZGANEQ== X-Google-Smtp-Source: AGHT+IGvVpM1zUIMlGm6ma9K+TKgNJGbud5iDdTA+ps7uKsYaldBJF05SmLrBzVnwfKTSxlPwHeznQ== X-Received: by 2002:a05:6e02:1a89:b0:35d:5995:798d with SMTP id k9-20020a056e021a8900b0035d5995798dmr3837106ilv.39.1702170294775; Sat, 09 Dec 2023 17:04:54 -0800 (PST) Received: from localhost (fwdproxy-prn-119.fbsv.net. [2a03:2880:ff:77::face:b00c]) by smtp.gmail.com with ESMTPSA id h17-20020a170902f7d100b001d0695106c4sm3974840plw.105.2023.12.09.17.04.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Dec 2023 17:04: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 v2 2/3] netdevsim: forward skbs from one connected port to another Date: Sat, 9 Dec 2023 17:04:47 -0800 Message-Id: <20231210010448.816126-3-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231210010448.816126-1-dw@davidwei.uk> References: <20231210010448.816126-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 From: David Wei 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 Sun Dec 10 01:04:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13486265 X-Patchwork-Delegate: kuba@kernel.org 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="qgZWE3IU" Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31C1F114 for ; Sat, 9 Dec 2023 17:04:56 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1d0538d9bbcso30474795ad.3 for ; Sat, 09 Dec 2023 17:04:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1702170295; x=1702775095; 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=rluyofcHo6kIZ2AnpUIUhpo/4gKMCAzt00LUTIVlWKI=; b=qgZWE3IU4Y2+uMlKxc4Nd0bXbLKOpPXo1067eZb/ygOF/P36sNfxChEI7/CVzYVx2M eql6BVD2QptVEq6uGzKg+Rwj9OGyAwYzGNR7ABc4Li5SQafmqSgUVvzU468xKAjBGTIM 1P09wQCZ/chfpxjfb5owyBWnqp0zSnCMVRlubxYlm0KvCYIlXwLUEQK3u2/3vqakgXRf /H1PykOFtEalrxJy/7avyXCB48Bhbo/FAJ4Ko5GAebiVnxUnDRcbATbtVeEXmkulevu8 rH1NF2vOzXzB2l6INSlUGvNvj/FiJsgPj9TM0tNJu5ClEeYRHi+6GQGbpK2RzTawgLUE L8BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702170295; x=1702775095; 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=rluyofcHo6kIZ2AnpUIUhpo/4gKMCAzt00LUTIVlWKI=; b=ULjYpY7ryxsSTR0vD9U/3L4oQwpy7kuImiGGYnhU6yVNEL5bFMxJZrR63MEa7Rhlvm N4sNJ2Cg5Efm31fIzTQyhIJm6JdA6r8F5run7OPmYe8AZfMEUf/Ylmi9vy1DyifU7cIx E4PZLzJimgMxFxNWbf2dxXbxgjq2f7UNannYdcvI/q/7PATGN84Ic6HD8OKHXT0pIvXg U0UhtWumAHcevJvv/W47vPxrO9XWJ2HN3MCJ8vsTDdnP0icIVnkF3RiPKp7/2YdVWzvw 8tXYetLYZ19XTQe92MQqfX7b5JfupLss81quZa7Y8FGpvtw4sSsaQ5IYUgQI36cqWzG8 ZTwA== X-Gm-Message-State: AOJu0YwmrHms4MJhxk/zYEGPI8GNuQ8DL+djE0CXgifoKvvZmGS3MCfb co6N4l1YtC1QmxOAIss7eOXLKg== X-Google-Smtp-Source: AGHT+IHRcWlNOY09ivtcSeKfsCMrJd0DVreRBiguZd34Kq6tvPvdnx+lkQMgynO+YBGnjDFX3wgkqg== X-Received: by 2002:a17:902:7c03:b0:1d0:8554:5dd5 with SMTP id x3-20020a1709027c0300b001d085545dd5mr2202176pll.37.1702170295707; Sat, 09 Dec 2023 17:04:55 -0800 (PST) Received: from localhost (fwdproxy-prn-002.fbsv.net. [2a03:2880:ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id s15-20020a170902b18f00b001cf6783fd41sm3954886plr.17.2023.12.09.17.04.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Dec 2023 17:04: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 v2 3/3] netdevsim: add selftest for forwarding skb between connected ports Date: Sat, 9 Dec 2023 17:04:48 -0800 Message-Id: <20231210010448.816126-4-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231210010448.816126-1-dw@davidwei.uk> References: <20231210010448.816126-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 From: David Wei 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 | 111 ++++++++++++++++++ 1 file changed, 111 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..d1d59a932174 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +NSIM_DEV_SYS=/sys/bus/netdevsim +NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim + +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 + + ip link set eni1np1 netns nssv + ip link set eni2np1 netns nscl + + ip netns exec nssv ip addr add '192.168.1.1/24' dev eni1np1 + ip netns exec nscl ip addr add '192.168.1.2/24' dev eni2np1 + + ip netns exec nssv ip link set dev eni1np1 up + ip netns exec nscl ip link set dev eni2np1 up + set +e +} + +cleanup_ns() +{ + ip netns del nscl + ip netns del nssv +} + +### +### Code start +### + +modprobe netdevsim + +# linking + +echo 1 > ${NSIM_DEV_SYS}/new_device + +echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netdevsim should fail" + exit 1 +fi + +echo 2 > /sys/bus/netdevsim/new_device + +echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer +if [ $? -ne 0 ]; then + echo "linking netdevsim1 port0 with netdevsim2 port0 should succeed" + exit 1 +fi + +# argument error checking + +echo "2 1" > ${NSIM_DEV_DFS}1/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 "1 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with self should fail" + exit 1 +fi + +echo "2 a" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null +if [ $? -eq 0 ]; then + echo "invalid arg should fail" + exit 1 +fi + +# send/recv packets + +socat_check || exit 1 + +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 2 > ${NSIM_DEV_SYS}/del_device + +kill $pid +echo 1 > ${NSIM_DEV_SYS}/del_device + +cleanup_ns + +modprobe -r netdevsim + +exit 0