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);