From patchwork Thu Dec 7 17:21:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13483700 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="VSXxRNVr" Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B72910FC for ; Thu, 7 Dec 2023 09:21:33 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6cb74a527ceso727762b3a.2 for ; Thu, 07 Dec 2023 09:21:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1701969692; x=1702574492; 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=heulfPpJ9MGP7pjH1Ng6D31Ma3get7g7XoGIrJm9YXc=; b=VSXxRNVrPE/6V85Cu01H4MZuXPVri6nO1Ph1IadXDjF4LcCbR5DbLBE1Rs8o8q2RWj PGr44DK1TDU1/tg4pN5IrVP9111NdxJrdVqk2ycXktVHhO0UC9wzqjTQOJvmpxLQX+sv Ue+Pymj3tznJ1SfsrKi5G/zwjlTX7GTnklSDk1Q2scnuX2zxX/2e20vLC2+YZgTEWWSs bWT5cJP3KcfIt1KLeMKIf4odjmXRPN/GzDUoti5uDtDGjeAYmatYiEmp53ZXXYnrsovw sa8frlNTzEiMs84gLIWoySKgSQPOTr/Eg5e2iLDjKsbRjMT+ckfwcHlAE0SRKdw5swxN FD+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701969692; x=1702574492; 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=heulfPpJ9MGP7pjH1Ng6D31Ma3get7g7XoGIrJm9YXc=; b=D/X9kWrGtY1QhQhTA97HtAm1lsV/1tIkE1N6k3topeiHqfG+7K2cAQX1s1EdZsNN73 klC6EUSKy0BucnwFeqB95hH8znTxUm80bQWqkVSbPsnghXOTNnEQTbppFxGGZsO/FX6K s4+F5jbYGbUx8Hu/gdCGfZGwvHutPOw5Va/0Ifl9gNSgbvnmFbU4eqXMKAHLVQUq8Pab D5HrAvtFZM2b9wiKCF5EaoKp5104dMV+s4PzHqIIrftSWIDmrwlNDyucxkzXSbz4C+pB a1XNwEdT8C9RLNNnDchsYuoX1Yxe99IKzld70FgVrNv4+McZLvD0YAZuQK0pIZJNgSki PAyA== X-Gm-Message-State: AOJu0YyiOuS3o/WhppvW7X4wnIxBkOjlb566jckRsADT401ZxXimZb+w eidw+Gjs3Y4wdtuVReb8/NEMTw== X-Google-Smtp-Source: AGHT+IGcMgxMVo6I885XKd2BKlpemBAz1ZCwbrlQgo4RllrZZcU/3Andz9Pgv7JA6Q+bK6ERd3eS8A== X-Received: by 2002:aa7:998f:0:b0:6ce:2732:1e00 with SMTP id k15-20020aa7998f000000b006ce27321e00mr2359817pfh.58.1701969691576; Thu, 07 Dec 2023 09:21:31 -0800 (PST) Received: from localhost (fwdproxy-prn-013.fbsv.net. [2a03:2880:ff:d::face:b00c]) by smtp.gmail.com with ESMTPSA id j12-20020a056a00234c00b006ce461447f5sm29936pfj.67.2023.12.07.09.21.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 09:21:31 -0800 (PST) From: David Wei To: Jakub Kicinski , netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH net-next 1/3] netdevsim: allow two netdevsim ports to be Date: Thu, 7 Dec 2023 09:21:15 -0800 Message-Id: <20231207172117.3671183-2-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231207172117.3671183-1-dw@davidwei.uk> References: <20231207172117.3671183-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/B/link 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 | 97 +++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdev.c | 5 ++ drivers/net/netdevsim/netdevsim.h | 3 + 4 files changed, 115 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..72ad61f141a2 100644 --- a/drivers/net/netdevsim/dev.c +++ b/drivers/net/netdevsim/dev.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -388,6 +389,99 @@ static const struct file_operations nsim_dev_rate_parent_fops = { .owner = THIS_MODULE, }; +static ssize_t nsim_dev_link_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[11]; + ssize_t len; + + nsim_dev_port = file->private_data; + peer = 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: + return simple_read_from_buffer(data, count, ppos, buf, len); +} + +static ssize_t nsim_dev_link_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 *token, *cur; + char buf[10]; + ssize_t ret; + + if (count >= sizeof(buf)) + return -ENOSPC; + + ret = copy_from_user(buf, data, count); + if (ret) + return -EFAULT; + buf[count] = '\0'; + + cur = buf; + token = strsep(&cur, " "); + if (!token) + return -EINVAL; + ret = kstrtouint(token, 10, &id); + if (ret) + return ret; + + token = strsep(&cur, " "); + if (!token) + return -EINVAL; + ret = kstrtouint(token, 10, &port); + if (ret) + return ret; + + /* too many args */ + if (strsep(&cur, " ")) + return -E2BIG; + + /* cannot link to self */ + nsim_dev_port = file->private_data; + if (nsim_dev_port->ns->nsim_bus_dev->dev.id == id) + return -EINVAL; + + /* invalid netdevsim id */ + peer_bus_dev = nsim_bus_dev_get(id); + if (!peer_bus_dev) + 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) { + nsim_dev_port->ns->peer = peer_dev_port->ns; + peer_dev_port->ns->peer = nsim_dev_port->ns; + return count; + } + } + + return -EINVAL; +} + +static const struct file_operations nsim_dev_link_fops = { + .open = simple_open, + .read = nsim_dev_link_read, + .write = nsim_dev_link_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 +512,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("link", 0600, nsim_dev_port->ddir, + nsim_dev_port, &nsim_dev_link_fops); + return 0; } diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index aecaf5f44374..1abdcd470f21 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; + 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); @@ -409,6 +410,10 @@ void nsim_destroy(struct netdevsim *ns) struct net_device *dev = ns->netdev; rtnl_lock(); + if (ns->peer) { + ns->peer->peer = NULL; + ns->peer = NULL; + } unregister_netdevice(dev); if (nsim_dev_port_is_pf(ns->nsim_dev_port)) { nsim_macsec_teardown(ns); diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h index 028c825b86db..ac7b34a83585 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 *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 Thu Dec 7 17:21:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13483701 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="NyaSdnJD" Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6702C1710 for ; Thu, 7 Dec 2023 09:21:33 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1d05199f34dso8801075ad.3 for ; Thu, 07 Dec 2023 09:21:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1701969693; x=1702574493; 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=gahfqE4ym5/ZFoex4VO8iYbRYCUAR6PpiEFdkfxKNPg=; b=NyaSdnJDvqHxL0/p+j0DTO6DGuKKsNPZ67Zsg9pN5Bw7wibW1HV2ZAP+fq+uOtMiQF db7Wobc+AqznNO2yFAxZ+0SffaDcKYyrVmZqo06BBKiHlH9u4E4A4RX7lRmPuOI/N60O p7jI83yZJBd0/1YCdrEmXLXPnhkz1I/5A/wgcPtWe+CafBDrFOT4LFrHqQuKw/AADEgv iNXOcAowwLXpYtJ1PyAeveILjzKbikrBxpAaMktC8LR795XUEnJeYYHu1Gt0aB8+Rl0I 3sNOcCuy+3LhjG3ClcaZdOOt3o7mmRY3wjL8/UB6MnazX4CZmJuagn3ucYUvEog4S7QT aO4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701969693; x=1702574493; 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=gahfqE4ym5/ZFoex4VO8iYbRYCUAR6PpiEFdkfxKNPg=; b=Hee0AP0digzPs6/IMkX8U2NysmT80hSUZ4aTGQG4NYHIjF/MLjJOEZoR88NYkpU6UP b7h316AEJH+6/a0li2z/kA+0D1RP/MLjJh2qcS6TDbPGE4oUtqQd/QFkCon8oBWk5BAm G9dtbhMozZJ6Ga1EAei1vKcM1GA6n/cvMqK8WvNVdirWE+fq0jRchM2f593KhuUKOvqE Br/q97wqpHHswvEz88u3C26NStO3RclieOFPLOX60+MQUI7VFmCQx+Bya6g3cfx9i9J6 KlSgEzvGfGlIIYaCfv/+LUNfdGYCOaSQBD3Qy3zzUSyRax5HYNp3zVBhGuHYynLMiM78 Q8bA== X-Gm-Message-State: AOJu0YxQwsp4+m5PN8yBBomysSxQpP59nn2KnGXXf6jhyXwu1PGcY8LT HWBRFlsxI7PP9wR7YAM8V6ytbw== X-Google-Smtp-Source: AGHT+IE0MO3FU0aMdQASCBQ/zJQ9SHwQf5XgFAegg87v9xGbNQUCMY5EzRUGJy4AuUcG16RJMDY/6w== X-Received: by 2002:a17:902:b195:b0:1d0:6ffd:6e6e with SMTP id s21-20020a170902b19500b001d06ffd6e6emr2747878plr.102.1701969692868; Thu, 07 Dec 2023 09:21:32 -0800 (PST) Received: from localhost (fwdproxy-prn-022.fbsv.net. [2a03:2880:ff:16::face:b00c]) by smtp.gmail.com with ESMTPSA id d1-20020a170902728100b001d087820175sm64098pll.40.2023.12.07.09.21.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 09:21:32 -0800 (PST) From: David Wei To: Jakub Kicinski , netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH net-next 2/3] netdevsim: forward skbs from one connected Date: Thu, 7 Dec 2023 09:21:16 -0800 Message-Id: <20231207172117.3671183-3-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231207172117.3671183-1-dw@davidwei.uk> References: <20231207172117.3671183-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 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 1abdcd470f21..698819072c4f 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -29,19 +29,30 @@ 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; 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 = 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; + return ret; + +err: + dev_kfree_skb(skb); + return ret; } static void nsim_set_rx_mode(struct net_device *dev) @@ -302,7 +313,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 7 17:21:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13483702 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="rBlULXfA" Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F577A3 for ; Thu, 7 Dec 2023 09:21:34 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-5c629a9fe79so826237a12.3 for ; Thu, 07 Dec 2023 09:21:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1701969694; x=1702574494; 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=yJ1yns42BwbRaoCcXYx5fKSmGMhxoE4SxiHQAoi05zE=; b=rBlULXfAZDA4cP+ZsuWcqcCA/TTlZYZK6F35NT7H9hpViB7u54MU1S6KEaztTUvrOQ 35+jvCyuaKeLakM8l7WZ/MDfpYBV0ms+Sa+VLgQB0lXguxAD1iZoBifZUlz8BvKV+nAf 6pDtBQeueqEG1gcQhrF+JMXWO+yR0VOwqsm4oOnMh0PFY7ZjhCMaCwKtlbvERHH6rq/d fmnl7ln0j/nKmLfkbZcWw0raBu9cPLVo+LK6yd+x53kZYGq8vlq7s+/mHsZxChJ0Cizw 21HW9RMpXzBjcNv7Xd9dymJnv1Mr+LVzMQIlsHblXTEm81yjH5ZN5wCmNu8/9374uXg4 82ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701969694; x=1702574494; 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=yJ1yns42BwbRaoCcXYx5fKSmGMhxoE4SxiHQAoi05zE=; b=juCxHBs02W074T9Ut9ZVvQsZ79XOEGoss9tFVdGWzLV7WnfbTAoIVSezH7+709QMa+ bTXBtW63emZxJEXobR/PxsgwrjccOgUp6VsaliMmRzrCKfLcj7Y2mXiAIT119kt39Nt/ rNeB//kNWgr1erx47e/qF02ScvEYaNKRiLqzNNg/Q7D8NrtXgQWlSKl7oTOrCLN0u0fx w2boXSZowJcTHBL3EJAhZR1WGlkY9TnW9fn36RDJ2wsY3FC1PiE6VSmluXO5dPJLk51a Gxlk2fhrV3AviHS3PEIArUY3aUSFFuuxdkc+aJ1LExeC71qT/Meb/nq9a56zB5Z1yaTR CUqA== X-Gm-Message-State: AOJu0YzGn7YG/76WEEEXvFffRF0dXNsSpVLCmcK4/w8yRd/hRi2QnP7w Zs7Wpvbh1DGaa8vNtsUlM5lGuQ== X-Google-Smtp-Source: AGHT+IEWQ1iRLRGdHXFVSXApyc+/TK2vXKPt1GJTRJSZtY3s5R4Eu+eRjPWYfAB5l1o9tCzQ5Ft5fQ== X-Received: by 2002:a05:6a20:441c:b0:187:9521:92b9 with SMTP id ce28-20020a056a20441c00b00187952192b9mr3095685pzb.53.1701969693735; Thu, 07 Dec 2023 09:21:33 -0800 (PST) Received: from localhost (fwdproxy-prn-002.fbsv.net. [2a03:2880:ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id c7-20020a6566c7000000b005c215baacc1sm7228pgw.70.2023.12.07.09.21.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Dec 2023 09:21:33 -0800 (PST) From: David Wei To: Jakub Kicinski , netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Paolo Abeni Subject: [PATCH net-next 3/3] selftest: netdevsim: add selftest for Date: Thu, 7 Dec 2023 09:21:17 -0800 Message-Id: <20231207172117.3671183-4-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231207172117.3671183-1-dw@davidwei.uk> References: <20231207172117.3671183-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 --- .../drivers/net/netdevsim/forward.sh | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/netdevsim/forward.sh diff --git a/tools/testing/selftests/drivers/net/netdevsim/forward.sh b/tools/testing/selftests/drivers/net/netdevsim/forward.sh new file mode 100755 index 000000000000..7464fb42576d --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/forward.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/link 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netdevsim should fail" + return 1 +fi + +echo 2 > /sys/bus/netdevsim/new_device + +echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/link +if [ $? -ne 0 ]; then + echo "linking netdevsim1 port0 with netdevsim2 port0 should succeed" + return 1 +fi + +# argument error checking + +echo "2 1" > ${NSIM_DEV_DFS}1/ports/0/link 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent port in a netdevsim should fail" + return 1 +fi + +echo "2 a" > ${NSIM_DEV_DFS}1/ports/0/link 2>/dev/null +if [ $? -eq 0 ]; then + echo "invalid arg should fail" + return 1 +fi + +echo "2 0 1" > ${NSIM_DEV_DFS}1/ports/0/link 2>/dev/null +if [ $? -eq 0 ]; then + echo "invalid arg should fail" + return 1 +fi + +# send/recv packets + +socat_check || return 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" + return 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