From patchwork Fri Jan 26 01:23:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13531926 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) (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 9B728399 for ; Fri, 26 Jan 2024 01:24:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232243; cv=none; b=G+tpkxVO/mZPNAQ9gya7nKrx4NDtMhlTOUPFpsI96z7CEhluVyN1o6TjIj8uZy29LeuXx4TnnR0LsKW09hggd2uG77+7BhyxJiQ2nGzEWXZa29Px9j9wGqEDveYkwXus8STr4Muk50ZdsiHUCNYJ4kvXs9IrpYPLBM75zS64fsk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232243; c=relaxed/simple; bh=IBPIKdOKvY0WW5LdMDKnfSfvdbdzz92967TpjFSqs1k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WVP8qjbDeaDaBPyR2Gg/e1mN2pxWQhnv5//hji6eT3ePIPnuJreLIEcQ12zoHu3/lIfojhX+Wcj71y/7MHUNS9m4v2IWWa0chV4Y8NycdZ9xbjX/+kYgcMfb2FnbYDLkqLVIkgYHGDtzZy+gpArVBBNc5B92xUljyv6+gY3tfoE= 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=Q54Wkknm; arc=none smtp.client-ip=209.85.215.175 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="Q54Wkknm" Received: by mail-pg1-f175.google.com with SMTP id 41be03b00d2f7-5cf765355ecso20496a12.0 for ; Thu, 25 Jan 2024 17:24:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1706232241; x=1706837041; 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=APMEWTkERNCaAJZd7Uy5jGDylwysKt0DpK+jOI64rCo=; b=Q54WkknmpSYKGN435uaLToD4pdQf9SsZbySmaacc7VWXUGqFN2DQFDklka6ePvZW2V YbWT1GgLqi5y8+pw0mH5xiR9XywifLJOKGRF6q+8eZtv4FeFNaXZ/SKxIiv5Cysb361i QM6oGAkw/o7+2ZMU6vl7aBTn0lnuGzp/fGMgTPQRnJ5KJjVRKMzBu/4Wws8RYL7ZYLLz u+1S6Av5C9NDk0bflczWaU9jc+ThJmFJsmbjnnohK0cK1QtsacNajuOorM/cvgZJ6vHy 7RDy0d5FRJrYs5KLUhaAA74XUsqKaEb5LenTY2aaZtiiXLakV+s7yyR0NHQVuYRsa5vJ 0QRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706232241; x=1706837041; 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=APMEWTkERNCaAJZd7Uy5jGDylwysKt0DpK+jOI64rCo=; b=XMdP7l07wUTpb4AZqBOsxUBt7U8AtTX1wcMckR2dsBxGf5vn6jWVD0ctBdwHR+ABG7 pP45Cucjp/yO/FezT6/xtSmY2QhULXjYyu8374c/1w+xNGw+O+cgNZrG+uEws65i0vUU wz9pBULQTQt2S3FICXBcN40R683Bt1uEm9IUrQV2SyNEDz5cH0FD/MlLQtSJuqkSoBZo hwyEjPGjUBlWLhv5vmwd6I73h856c1g1HX06K983IW8qrOvkLFsJAj6o1lId+bEDU8XR V9UBn1YhvWOVLheFdoMZEcd/NuppuW1jIFj7cAqvuT4ioMKGqb8/PbneUtR9qECsFSpA Vsng== X-Gm-Message-State: AOJu0YzISKtoadTk3ipgk/3nqjONuyWXuYNQzXh0jDWmGCAP0YGGZOlg wR4JjN9oPaQ88B7n9uzd+lXdVYrToJWafaf/XXLEq9zKg4STjyVqIWkBlbKGBQY= X-Google-Smtp-Source: AGHT+IEpQ4Kbe3j6uJXbEYA0OmfK894+m07iEZtQ93vidPIVOFZGt3tp+DNAALmYA4GQO83zo+xr5A== X-Received: by 2002:a17:90b:4ad1:b0:293:eeac:1588 with SMTP id mh17-20020a17090b4ad100b00293eeac1588mr92332pjb.10.1706232240903; Thu, 25 Jan 2024 17:24:00 -0800 (PST) Received: from localhost (fwdproxy-prn-009.fbsv.net. [2a03:2880:ff:9::face:b00c]) by smtp.gmail.com with ESMTPSA id mf4-20020a17090b184400b0028cc9afaae9sm152299pjb.34.2024.01.25.17.24.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 17:24:00 -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 v6 1/4] netdevsim: allow two netdevsim ports to be connected Date: Thu, 25 Jan 2024 17:23:54 -0800 Message-Id: <20240126012357.535494-2-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240126012357.535494-1-dw@davidwei.uk> References: <20240126012357.535494-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 netdevsim bus attribute to sysfs: /sys/bus/netdevsim/link_device Writing "A M B N" to this file will link netdevsim M in netnsid A with netdevsim N in netnsid B. rtnl_lock is taken to ensure nothing changes during the linking. Signed-off-by: David Wei --- drivers/net/netdevsim/bus.c | 72 +++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdev.c | 11 +++++ drivers/net/netdevsim/netdevsim.h | 2 + 3 files changed, 85 insertions(+) diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index bcbc1e19edde..be8ac2e60c69 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -232,9 +232,81 @@ 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) +{ + unsigned int netnsid_a, netnsid_b, ifidx_a, ifidx_b; + struct netdevsim *nsim_a, *nsim_b; + struct net_device *dev_a, *dev_b; + struct net *ns_a, *ns_b; + int err; + + err = sscanf(buf, "%u %u %u %u", &netnsid_a, &ifidx_a, &netnsid_b, &ifidx_b); + if (err != 4) { + pr_err("Format for linking two devices is \"netnsid_a ifidx_a netnsid_b ifidx_b\" (uint uint unit uint).\n"); + return -EINVAL; + } + + err = -EINVAL; + rtnl_lock(); + ns_a = get_net_ns_by_id(current->nsproxy->net_ns, netnsid_a); + if (!ns_a) { + pr_err("Could not find netns with id: %d\n", netnsid_a); + goto out_unlock_rtnl; + } + + dev_a = dev_get_by_index(ns_a, ifidx_a); + if (!dev_a) { + pr_err("Could not find device with ifindex %d in netnsid %d\n", ifidx_a, netnsid_a); + goto out_put_netns_a; + } + + if (!netdev_is_nsim(dev_a)) { + pr_err("Device with ifindex %d in netnsid %d is not a netdevsim\n", ifidx_a, netnsid_a); + goto out_put_dev_a; + } + + ns_b = get_net_ns_by_id(current->nsproxy->net_ns, netnsid_b); + if (!ns_b) { + pr_err("Could not find netns with id: %d\n", netnsid_b); + goto out_put_dev_a; + } + + dev_b = dev_get_by_index(ns_b, ifidx_b); + if (!dev_b) { + pr_err("Could not find device with ifindex %d in netnsid %d\n", ifidx_b, netnsid_b); + goto out_put_netns_b; + } + + if (!netdev_is_nsim(dev_b)) { + pr_err("Device with ifindex %d in netnsid %d is not a netdevsim\n", ifidx_b, netnsid_b); + goto out_put_dev_b; + } + + err = 0; + nsim_a = netdev_priv(dev_a); + nsim_b = netdev_priv(dev_b); + rcu_assign_pointer(nsim_a->peer, nsim_b); + rcu_assign_pointer(nsim_b->peer, nsim_a); + +out_put_dev_b: + dev_put(dev_b); +out_put_netns_b: + put_net(ns_b); +out_put_dev_a: + dev_put(dev_a); +out_put_netns_a: + put_net(ns_a); +out_unlock_rtnl: + rtnl_unlock(); + + return !err ? count : err; +} +static BUS_ATTR_WO(link_device); + static struct attribute *nsim_bus_attrs[] = { &bus_attr_new_device.attr, &bus_attr_del_device.attr, + &bus_attr_link_device.attr, NULL }; ATTRIBUTE_GROUPS(nsim_bus); diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 77e8250282a5..969248ffeca8 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -394,6 +394,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); @@ -413,8 +414,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 +433,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 Fri Jan 26 01:23:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13531927 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (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 DA19610E9 for ; Fri, 26 Jan 2024 01:24:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232244; cv=none; b=XRyi+c2HgftLJPO9m5xv4CTmYG3N+pDi0qNzy1AmKuBrNxcon09BgJcFJ44kmTomFZqLu2N75krS3O2c4331th7Z+60iAMnpmD7EAA5Dke1C3tE9zyI3P4m79R/6sRULVaZHff7wzcyL50dI7GGlyBxnF3HTqlMrqBw0TQMEqjE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232244; c=relaxed/simple; bh=0dEbDzAhv1TUAkHSGAvsgLdeAUMn6bqVB7CmvYmsqUA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Bm96aBu5Zf4Ui7jBfxsbe76LIdK5bLuPvNDPmypw5rXTCCej8G9eJMjdHC142J7SZyE8MpMXkWrmjcSOLOvzJr2mc/moOcnzve6ASiG2yg8/QMS9nSKH3sCq0ry1RAM6Ej0u7fzRxkwrFM6R4Yh60R1i+QOw0CvXwdUBo/mzLV8= 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=dRLuWRH0; arc=none smtp.client-ip=209.85.167.173 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="dRLuWRH0" Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3bd7c15a745so49486b6e.2 for ; Thu, 25 Jan 2024 17:24:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1706232242; x=1706837042; 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=85fyyKTHxy6LvDMpMXtf8guHa2fdoQiZVPwGGnMfL70=; b=dRLuWRH0qsnQG1w87ntdOQ+xiGSHTNwBezaOkgb9QtVnGue0VpyOmpVEZCk/YDNQEm X15hToofRo15a93yf7xUkWbAeFCQk8KfMRzKnwdesMTYIfU2BWS3dhMAiA9BluUA1C9c IzNEJeOHJHaoI8UXIH5t9A5mqTAiW4JyMHKPD9VdApblIsj59+IShYK3w69UvdjNcoi5 5eUQH4FvwnOmo3vopMpLcUnHhBd5Ly02Pi+w2f2W+GfRd7rVIilbbrx+hoeXt5RCT1En I1M9iTbYvVvckynjAKotmRZZvyc/FB//kXtgc6PsqZnbviO8g7Mp9wuoaVkp9An7BKgC vw6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706232242; x=1706837042; 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=85fyyKTHxy6LvDMpMXtf8guHa2fdoQiZVPwGGnMfL70=; b=Zo90IDXPy3rGrVCCUS8oVOL9M8oVf0X22D7eNsvm8QOeL+FcSS3BONFOopFpC2zBOI 96KlmKKe/7X94V7bfhQCsP8GH9DD38lBWjWsqr6Z33+rNHqP9J2J4WebU91eziuhjl8O n7PdNkY1C8pi/Hm12PgzMD/E2IL96wr1ct076Ab5s43l7MrKtDELTtQEeNrovHKl/lF5 YznKm7sweOIxUnyPZp5xEIrRNcrqiMmHisoxWLdz6IKRu6xSTeWre02PHvCZCWdwqyKW Pzc3WrF54FNd57DBh9Lntv+PGruEzqMfH8r4H0QZ7Nch+aarcYzemEmlaxHZlAYJ53o9 8r4A== X-Gm-Message-State: AOJu0YzM2Z/NItdKYn85oVDwXa1RWlzW92SgMlDjAogeI9ElmdjN6hj/ EluBRT9N9h/dJwE19ZwhPK+0yvT3lf+0YwRy5XtVi3lkLf1420NrR4Bu8gpuUvk= X-Google-Smtp-Source: AGHT+IFCo5SjhiHa2lT+gQo5jz9r2WPt435kfZKoZh1Kg+P+KG6X0Fb+RuoEO26EW5dS1CYQis7iJg== X-Received: by 2002:a05:6808:6545:b0:3bd:a590:8a00 with SMTP id fn5-20020a056808654500b003bda5908a00mr671597oib.85.1706232241869; Thu, 25 Jan 2024 17:24:01 -0800 (PST) Received: from localhost (fwdproxy-prn-116.fbsv.net. [2a03:2880:ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id q21-20020a62e115000000b006ddd22b79f6sm152247pfh.120.2024.01.25.17.24.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 17:24:01 -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 v6 2/4] netdevsim: forward skbs from one connected port to another Date: Thu, 25 Jan 2024 17:23:55 -0800 Message-Id: <20240126012357.535494-3-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240126012357.535494-1-dw@davidwei.uk> References: <20240126012357.535494-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 rtnl_lock, making it safe with concurrent calls to linking two netdevsims together. Signed-off-by: David Wei --- drivers/net/netdevsim/netdev.c | 24 ++++++++++++++++++++---- drivers/net/netdevsim/netdevsim.h | 1 + 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 969248ffeca8..978c34334018 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -29,19 +29,35 @@ 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; + unsigned int len = skb->len; + 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) + 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; + ns->tx_bytes += len; + if (ret == NET_XMIT_DROP) + ns->tx_dropped++; 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 +86,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 +319,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 Fri Jan 26 01:23:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13531928 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (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 D306215BF for ; Fri, 26 Jan 2024 01:24:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232245; cv=none; b=mCTOXT1z4I7L+8L/vkJYBR9/r5paxeg+fhDV3fW+RsVDAF2B1nEXaUvg+gqLPxXP9LIx+0ZymEdVqv55/t6+hzkQTiOfxyg1W/6ud5iAne7X08eXRiSUqZXQsBHHY/QOiY7N2mq8oOepAsx0QA8fLTo5gt8N1PWiLc8oZjinnk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232245; c=relaxed/simple; bh=74xb1S+58KTAkR+KlZoZt6XeFGiV0DmBRji/NtusKYs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=piageADjaewJmRLLNay0dp1lkHzrf+qaFSkA+s+efC0U6UA9G4gBsZlG4ywMeTyD9U6dQKG3ob0IGWFHLeRiNUUtilYNMMtc+3RRtPPArMLMU8fAVA9xh41BlqgG8yVS0N6IDdC3cAUk8T1c9haErGMYoatVU+FGgtmF9hGBp7c= 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=CeuH0Yc+; arc=none smtp.client-ip=209.85.215.176 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="CeuH0Yc+" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-5ce942efda5so5555214a12.2 for ; Thu, 25 Jan 2024 17:24:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1706232243; x=1706837043; 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=UXL8wH7GmvH1gdgfeRX5m+iM2fXhAn+u6nfVnG2vWa8=; b=CeuH0Yc+M03SpXNvkwZqN/A0t0WSjJYZs1wpDavYjcUYbajTd8Hac5pFTZkbS1cJ7O Mhbaxeuvm+WdzG+/FHVMIoGqG1p89SYPMdGmqcMS1958eKrdV62iWDcDCi2/6jdNyXkD XNpMvVe3QJA+iUD4PXFuEmVHGM66uL5Ead0uSvuL3HTHJwj+6fVSa77zieNd0vIwzWFw cyVpvNJLA1t2jtP7yXD4zYMzuaQqHK9OI2p+dRzE1HoM1wf6M14/lELHz6+Ljt49kRaF 7BRWpfql1jTU3f90YIb/UaagPbRAPi/kEiowXCp/X3LVP5u9fRz6+nXO7U8zRU1ROJE5 /p9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706232243; x=1706837043; 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=UXL8wH7GmvH1gdgfeRX5m+iM2fXhAn+u6nfVnG2vWa8=; b=oR9rVOw0Bt/AK9lDxbX3QNFIBNBlem9IlhufS+Jlj1Wuwlqpu9t68f4tTBefPldgmD gvv0uSamj1vDKDAwnEWwFf5vPnkV1PIrDX8mkl8W4PJd3JmpAdPB2gcmOxlVVYRljkA5 kSMl0EUgTcufl4QMDWzqkD+21UTs1aWaYCbIolK8KFxiqR5JkURsADShsMKzl+7xFNzL OjzK1RC3J4pNpFvEwinrSGoCkLmZX0ZmhY1RUwzRy18vTbTInF70a/U/LRFc04AI3B6x xmwMX2DalQI6E99POzm6e6SdtCVeVZLkC4v1exKjrRM6Fx0B8+4mPtjE64FcSmya6xrh ZoEg== X-Gm-Message-State: AOJu0YxG0FMnThTrY/eKCHhM2cz2ONazB/PzFPipxUM39Li7vqM+a+4Z NQ+pZ6pbFMQTgf8+443Su6Pzh3dM5Uq2oaC6xntQFTG71yOgkfEpVLE5UBgbBO4= X-Google-Smtp-Source: AGHT+IE9k1IE4NvEgmyARUYqW6sSWPhq9qE0pwTvps/Wd17av1qfnT2YG5yGFlQJgp3wvc3ng0HGjg== X-Received: by 2002:a05:6a21:3403:b0:19c:86f3:e3f2 with SMTP id yn3-20020a056a21340300b0019c86f3e3f2mr247121pzb.21.1706232243119; Thu, 25 Jan 2024 17:24:03 -0800 (PST) Received: from localhost (fwdproxy-prn-116.fbsv.net. [2a03:2880:ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id j12-20020a056a00234c00b006d9b35b2602sm155187pfj.3.2024.01.25.17.24.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 17:24:02 -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 v6 3/4] netdevsim: add selftest for forwarding skb between connected ports Date: Thu, 25 Jan 2024 17:23:56 -0800 Message-Id: <20240126012357.535494-4-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240126012357.535494-1-dw@davidwei.uk> References: <20240126012357.535494-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 | 124 ++++++++++++++++++ 1 file changed, 124 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..4fdb43fec044 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh @@ -0,0 +1,124 @@ +#!/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 +NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_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 > $NSIM_DEV_SYS_NEW + +setup_ns + +NSIM_DEV_1_NETNSID=$(ip netns list-id | grep nssv | awk '{print $2}') +NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex) + +NSIM_DEV_2_NETNSID=$(ip netns list-id | grep nscl | awk '{print $2}') +NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex) + +echo "$NSIM_DEV_1_NETNSID $NSIM_DEV_1_IFIDX $NSIM_DEV_2_NETNSID 20" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netdevsim should fail" + exit 1 +fi + +echo "$NSIM_DEV_1_NETNSID $NSIM_DEV_1_IFIDX 20 $NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netnsid should fail" + exit 1 +fi + +echo "$NSIM_DEV_1_NETNSID $NSIM_DEV_1_IFIDX $NSIM_DEV_2_NETNSID $NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK +if [ $? -ne 0 ]; then + echo "linking netdevsim1 with netdevsim2 should succeed" + exit 1 +fi + +# argument error checking + +echo "$NSIM_DEV_1_NETNSID $NSIM_DEV_1_IFIDX $NSIM_DEV_2_NETNSID a" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "invalid arg should fail" + exit 1 +fi + +# send/recv packets + +socat_check || exit 4 + +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_2_ID > $NSIM_DEV_SYS_DEL + +kill $pid +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_DEL + +cleanup_ns + +modprobe -r netdevsim + +exit $res From patchwork Fri Jan 26 01:23:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13531929 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 BD79E4687 for ; Fri, 26 Jan 2024 01:24:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232247; cv=none; b=PEti12VESbYD5682ZSreKirzaUfImLYWWR7iwgmJeCxKQ6TvE+7/6x3+jagyLpZ0lslaTf3AFjbEyuDkvk5shpnE/Er20U5RzDwYvrXi8rrgpDcNzrLktFjsqDwl48par5MXTCC4ZevR+ySY4gGCtNesTQXa7qShZ1x4kMKm/Wo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706232247; c=relaxed/simple; bh=eCjWuhzdddCrMZiFX6ANjmJWjtgIDAgjsgKRZp3Q99E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hmVGpAnQWhI3oQo43ezreZbXX1MUBrpLcfK1gBfEoOmNlmqYoefgcgbH5f2OBy/gKlSOPmTIAeOWWGAiAD0TkpTvm9ztEmEE71twy+QmPHEp7Mur0XJpv5AP8u9K3xeg3dmShdS0W7CDxGBJoKSg0aAZ7hjRQNYdnKrleQz6Lys= 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=tbikyoYp; arc=none smtp.client-ip=209.85.214.169 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="tbikyoYp" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1d893950211so5893385ad.0 for ; Thu, 25 Jan 2024 17:24:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1706232244; x=1706837044; 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=t6CX8D0lpq6HiM0LfR6MAxAUUlJjyu7IdjFJ5kI3qyM=; b=tbikyoYpd2o6RlvvCEKU3iMhH54O35e2iV4SqCceGnf5KEtbcN0PLJ4gJAbMvTGCeK TIL3eEMpmYrljL76iuLaK4/xTSFQ9qvzLBmQmLES7ccFrRIWR6frSxLRpb+k6AThmFKp /dSKxyD2VhEKM6jKY80UGnypEvLHxOmIQ1bY/zveFB9cbJRULpobj7/SyNwLgVqrPlak xQxb9JgE3fikUblnAaJ0yX1QGA7b1vO316E7npZkTkcJDDwViItwtrnjXUknJHnGTy8B 626jQ1+5MyiVKc0iozoT56/ia2cEWjcP4EPm75FCFGnQ0WG7eVBlcoAmCvvrh6ZF0Fzg /8zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706232244; x=1706837044; 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=t6CX8D0lpq6HiM0LfR6MAxAUUlJjyu7IdjFJ5kI3qyM=; b=hrjfZSTzakiwnOMIxyzzaPAvEF02OiJoBArI7fKu9eNSYWugx0Rf6fXozMIN1YDTpa FOrRueV6+IgXxpRgN4Y996nyE15/P32nUDXzAppvsZ6IIPgo3ze3n/AnHL836NyOotg+ JEnbXKqXYWdJ/zRUG0fyBGiY0t4CtqfTeEjECRt1SiLP1x7fy322iF3nhnj5TnQXZNtD 6Ntr/82QeS8hvINqZWJBWk2HtQIKnYmBhD7KJlJhtdH7ILwS3IqThj77yWGmvmm93IU5 KyFfEfMVsKT0/DCFR0SMZRrtVRNzOwZeS+/P8EaYIVK0Yh+z3XIFqbxykkz0UGoOwpev Ud+w== X-Gm-Message-State: AOJu0Yz9WPXEmYXNLPjUjWgdpiwGowYgziVV1XDulF2FZxUIjBmGt0Sl R3ewy4YN3wc5QCh3Lg/Mb5uHOsw2rbsyZ08d6cpeCfpnVlvGte8A1QO3z1NGu5E= X-Google-Smtp-Source: AGHT+IHbmlADsrnFRBINz+pLzcqL2xK6k8ZUNw6nlBqsHxIAUAD/tguq3pmhgL0FSgCQsL/xDdx9dw== X-Received: by 2002:a17:902:e743:b0:1d5:c1c4:7f05 with SMTP id p3-20020a170902e74300b001d5c1c47f05mr670335plf.3.1706232244095; Thu, 25 Jan 2024 17:24:04 -0800 (PST) Received: from localhost (fwdproxy-prn-119.fbsv.net. [2a03:2880:ff:77::face:b00c]) by smtp.gmail.com with ESMTPSA id v7-20020a170903238700b001d72817a62dsm114362plh.264.2024.01.25.17.24.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 17:24:03 -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 v6 4/4] netdevsim: add Makefile for selftests Date: Thu, 25 Jan 2024 17:23:57 -0800 Message-Id: <20240126012357.535494-5-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240126012357.535494-1-dw@davidwei.uk> References: <20240126012357.535494-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 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 8709c7cd3656..6b3608db5bda 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15061,6 +15061,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