From patchwork Sat Feb 10 00:32:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13552005 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 CD7C51FB3 for ; Sat, 10 Feb 2024 00:32:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707525181; cv=none; b=MGYeH82UW1ESx4X1FtQ1uFAkTKCg1XJ+SmjtCjoSnb+l6XnENXCbMiGWzVNpwmphSgVQMpmapJBEuOWpHLn5Rs6Qyox1tlgsSDsBxROR+5z+W2hIrzgjjpi+WOzjt0xVzi8t638F9FnCyQYD8yCPP3r8fmeKlxZu+Yakf1l0Ev4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707525181; c=relaxed/simple; bh=ua5FlgwlbLI/krpJR329L52YoNxt8f23aRJl8Yls6Xo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nAPShKvUTLdATVsi6Ue4mV37robdYCu+zCFQWJqt9uN957TZ4dqc6aG2/xQNYmrPZ+678FJSo3BBk02BVxZVeL7VVCPH+qYidPs9CwM5Vwvstzdw7JCutVai9T6BRyWU2LNx2XH52E5BuIr2UaFDZHM5ox+QVwpyc/aG6DNuft0= 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=cbMXJooG; arc=none smtp.client-ip=209.85.214.179 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="cbMXJooG" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1d93edfa76dso12720995ad.1 for ; Fri, 09 Feb 2024 16:32:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1707525179; x=1708129979; 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=TX6IXW2MOyUvIcwe8DD66ySCaXJgK+9LvJoSEqu60Mw=; b=cbMXJooGOs4xuLuYffiEGUJ4H03/NU+p6Vd+UrDh6YNXbyxr64J02bZPJfeBsROkyW rLxgQenlCBLU/2T9eypAdyTqXRtXDMVU7BBuUMiuJ00rlCsiB/Q/gMZzjbUQr0xQsUtz KAV88cxfkpxVm0AUm7cz3qltiWb6rv4EnANSoJwgSCov1x5zlXvnRbKji33d8eU1O7rh O9IJWJHzDrkDIXELfRKESWci7bIRweZvyBVWRma8xlCAzu7ae4TIK6SN/XG2cIWh5hHi e5twDiWGgDfDFBqeUW+YT2rdTqb3B+fpz9WxaUtuycvXcOhTGq048vhIsNjJcdBw4kJy U9tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707525179; x=1708129979; 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=TX6IXW2MOyUvIcwe8DD66ySCaXJgK+9LvJoSEqu60Mw=; b=QgCiIlNbeH/ZOgRei0CSvHL66qOL7YAw7I9PlK14mzuYz/dkDQaYl5QNWlHFvrHLUn kR74ewyO5mBkObPWACiTCKcEfX8OV40RIZeGi9++yX+nGgadPIF17pJbTN/66V0eeygO cmr41OPZOia1hyhY1iE4alBJAjw0wOJOV9Or9loOQjQH1DLCF8T43CKvqwNA0dlxxdn6 8SUDfhX1fgaxoOVVWuhRo6lMLa802bowA+DxhIkWrdmSNFUF/L6vVebgQuCOjzVi6hcV a8MDUc0k5gtMwwLduxp+7zOuZU29YzsNRknePkEb8tIVFHO8bgnUrasIEgL8hb8Jt6Q+ gAbg== X-Forwarded-Encrypted: i=1; AJvYcCU20DZ1Ot3fTa2iqLAVkKR3CHwLEv5R4zNebkXAvwH8xn2Hm3lVr3sw2F77rQLJJYhk07hCzvqSjVpNBfLhVA2gWtHggtxs X-Gm-Message-State: AOJu0YxiFM2Aq4gxPNSr55W5riM+CVSpU6YSfSI2F9xZu6gIbHUoeNlz UOTpxrxGTo6rDXROncKFtgpp84NHnflHQJCRp02n8RUXR+R2PfFCKt6tLuv7PBY= X-Google-Smtp-Source: AGHT+IFx6pcrEqCZkHs8cg6uSKhzUzsEUI7hiJXNoyzbFW2JidxKXZhoNyFgeGsmRAcq1mi6fwaa7A== X-Received: by 2002:a17:902:6e10:b0:1d9:7121:170e with SMTP id u16-20020a1709026e1000b001d97121170emr829136plk.35.1707525179100; Fri, 09 Feb 2024 16:32:59 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCX2YQPnqT8oXW3K1TkrKa+s16nB6xyF0w9fTckUVfeXLCwXdk9kkYq3NuPKls62UAZvOGQ4b+KTShKlMw6JSxpYZKnXfG6+UWcgK8I6cYYaAVDJijULsQqN2YmhHkmT23EG93wjVbV6w//ZGTKteDi12IHmxmdFUxEbD5b2O1EKhInpAa/jqWFy1U9zzckcswibNPfQ Received: from localhost (fwdproxy-prn-020.fbsv.net. [2a03:2880:ff:14::face:b00c]) by smtp.gmail.com with ESMTPSA id d8-20020a170903230800b001d8f81ecea1sm2055382plh.172.2024.02.09.16.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 16:32:58 -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 v9 1/3] netdevsim: allow two netdevsim ports to be connected Date: Fri, 9 Feb 2024 16:32:38 -0800 Message-Id: <20240210003240.847392-2-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240210003240.847392-1-dw@davidwei.uk> References: <20240210003240.847392-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 two netdevsim bus attribute to sysfs: /sys/bus/netdevsim/link_device /sys/bus/netdevsim/unlink_device Writing "A M B N" to link_device will link netdevsim M in netnsid A with netdevsim N in netnsid B. Writing "A M" to unlink_device will unlink netdevsim M in netnsid A from its peer, if any. rtnl_lock is taken to ensure nothing changes during the linking. Signed-off-by: David Wei --- drivers/net/netdevsim/bus.c | 135 ++++++++++++++++++++++++++++++ drivers/net/netdevsim/netdev.c | 10 +++ drivers/net/netdevsim/netdevsim.h | 2 + 3 files changed, 147 insertions(+) diff --git a/drivers/net/netdevsim/bus.c b/drivers/net/netdevsim/bus.c index bcbc1e19edde..7f98e4d4e738 100644 --- a/drivers/net/netdevsim/bus.c +++ b/drivers/net/netdevsim/bus.c @@ -232,9 +232,144 @@ 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) +{ + struct netdevsim *nsim_a, *nsim_b, *peer; + struct net_device *dev_a, *dev_b; + unsigned int ifidx_a, ifidx_b; + int netnsfd_a, netnsfd_b, err; + struct net *ns_a, *ns_b; + + err = sscanf(buf, "%d:%u %d:%u", &netnsfd_a, &ifidx_a, &netnsfd_b, &ifidx_b); + if (err != 4) { + pr_err("Format for linking two devices is \"netnsfd_a:ifidx_a netnsfd_b:ifidx_b\" (int uint int uint).\n"); + return -EINVAL; + } + + ns_a = get_net_ns_by_fd(netnsfd_a); + if (IS_ERR(ns_a)) { + pr_err("Could not find netns with fd: %d\n", netnsfd_a); + return -EINVAL; + } + + ns_b = get_net_ns_by_fd(netnsfd_b); + if (IS_ERR(ns_b)) { + pr_err("Could not find netns with fd: %d\n", netnsfd_b); + return -EINVAL; + } + + err = -EINVAL; + rtnl_lock(); + dev_a = __dev_get_by_index(ns_a, ifidx_a); + if (!dev_a) { + pr_err("Could not find device with ifindex %u in netnsfd %d\n", ifidx_a, netnsfd_a); + goto out_put_netns_a; + } + + if (!netdev_is_nsim(dev_a)) { + pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n", ifidx_a, netnsfd_a); + goto out_put_netns_a; + } + + dev_b = __dev_get_by_index(ns_b, ifidx_b); + if (!dev_b) { + pr_err("Could not find device with ifindex %u in netnsfd %d\n", ifidx_b, netnsfd_b); + goto out_put_netns_b; + } + + if (!netdev_is_nsim(dev_b)) { + pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n", ifidx_b, netnsfd_b); + goto out_put_netns_b; + } + + if (dev_a == dev_b) { + pr_err("Cannot link a netdevsim to itself\n"); + goto out_put_netns_b; + } + + err = 0; + nsim_a = netdev_priv(dev_a); + peer = rtnl_dereference(nsim_a->peer); + if (peer) { + pr_err("Netdevsim %d:%u is already linked\n", netnsfd_a, ifidx_a); + goto out_put_netns_b; + } + + nsim_b = netdev_priv(dev_b); + peer = rtnl_dereference(nsim_b->peer); + if (peer) { + pr_err("Netdevsim %d:%u is already linked\n", netnsfd_b, ifidx_b); + goto out_put_netns_b; + } + + rcu_assign_pointer(nsim_a->peer, nsim_b); + rcu_assign_pointer(nsim_b->peer, nsim_a); + +out_put_netns_b: + put_net(ns_b); +out_put_netns_a: + put_net(ns_a); + rtnl_unlock(); + + return !err ? count : err; +} +static BUS_ATTR_WO(link_device); + +static ssize_t unlink_device_store(const struct bus_type *bus, const char *buf, size_t count) +{ + struct netdevsim *nsim, *peer; + struct net_device *dev; + unsigned int ifidx; + int netnsfd, err; + struct net *ns; + + err = sscanf(buf, "%u:%u", &netnsfd, &ifidx); + if (err != 2) { + pr_err("Format for unlinking a device is \"netnsfd:ifidx\" (int uint).\n"); + return -EINVAL; + } + + ns = get_net_ns_by_fd(netnsfd); + if (IS_ERR(ns)) { + pr_err("Could not find netns with fd: %d\n", netnsfd); + return -EINVAL; + } + + err = -EINVAL; + rtnl_lock(); + dev = __dev_get_by_index(ns, ifidx); + if (!dev) { + pr_err("Could not find device with ifindex %u in netnsfd %d\n", ifidx, netnsfd); + goto out_put_netns; + } + + if (!netdev_is_nsim(dev)) { + pr_err("Device with ifindex %u in netnsfd %d is not a netdevsim\n", ifidx, netnsfd); + goto out_put_netns; + } + + err = 0; + nsim = netdev_priv(dev); + peer = rtnl_dereference(nsim->peer); + if (!peer) + goto out_put_netns; + + RCU_INIT_POINTER(nsim->peer, NULL); + RCU_INIT_POINTER(peer->peer, NULL); + +out_put_netns: + put_net(ns); + rtnl_unlock(); + + return !err ? count : err; +} +static BUS_ATTR_WO(unlink_device); + static struct attribute *nsim_bus_attrs[] = { &bus_attr_new_device.attr, &bus_attr_del_device.attr, + &bus_attr_link_device.attr, + &bus_attr_unlink_device.attr, NULL }; ATTRIBUTE_GROUPS(nsim_bus); diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 77e8250282a5..9063f4f2971b 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -413,8 +413,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 +432,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 Sat Feb 10 00:32:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13552006 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (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 BCFEE652 for ; Sat, 10 Feb 2024 00:33:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707525182; cv=none; b=HKZm/21TCxNUf4aN+CBYrP6COVyMJbaUTSeuZbf9snQ767N0+72zq4lFTQt4JjAxCjVYnz819fc6sSm9Ldj6fTux1O874l3YriRKmNMncbma358TdGkFDS8h0cRxIA+xZCptQyb6FuIr2NiSqR91nlYyY3zAW5Vy0MKheTulI44= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707525182; c=relaxed/simple; bh=CbrBjdCJZVNgm7/r6FMlMVyJdljO71fGEsbpOnVsSPo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gYmXaQ9NpHQV38KI7kGAzOKM21bnBLUR035dv19AY7QktzjuayDRPY4wo6t7M2GNivOgvs6TVOfMvbVk+/ZePsJzG6bQyvF49bXgRxhbvpcxpmF5vzAoSsNGHcWFPMTM8df7pYDtDDO5EOsLj/ikg5uXTvHMyP1pNpuDnxbsbt0= 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=JcgjU6po; arc=none smtp.client-ip=209.85.214.170 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="JcgjU6po" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1d7858a469aso11762155ad.2 for ; Fri, 09 Feb 2024 16:33:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1707525180; x=1708129980; 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=4pDjhVSjaegSSMNXa3GUHfuVucfP0rlcP/EmOTiSZb8=; b=JcgjU6poaN9bmPimLTLS75OXayf1KIhe6mOhuOrRGb2R1N5nbn8ngjU7Nkj+OXjlZu DFOTlVqwnvrlDAO61VIlQKeCskv0lEi37gZnRyvo0Ym4duu2c5x+M9VoaMnQpNlxWuCI pPxCCMDJ+OjUhMyNNeb+Vt2sxKtEbJSTNZaL58YKVfkrcRzbGB+GsE+a24hWf4+r4qi3 0JDVYX6t0uXdzQasf0L+kUQ3/jBcPeC16TDnfJO7UBrGvUtHBavAqKU3dopcY/yCRFgV WEPrczkCHC0p763+guiriIK//YuEQrJrNeWr9vqQp2J3t3oowyIY0qjD+OfmUINatuIx kH8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707525180; x=1708129980; 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=4pDjhVSjaegSSMNXa3GUHfuVucfP0rlcP/EmOTiSZb8=; b=eDtNEzDCzPHHJHh3sZfLC6dRBMunj+vDu7E2X05rOae2Dhgn37Logxrrz0AFAn9xZL fXg4dys610ppTh2FkeyV+vzcdbSKT3OlyhKeFgCZFpDgLXQ9uHqNR3a4Z9W7BPymhFrz hN7uWWFkKZxWaLpvJrVuIdTX5cr2eHtLa/MfmoCU73uVwPbqHwFERhr9v9SAA04RdzLV ps/UZPqimQQVuZ1GNXYkdAGIqQuTD0yTaCvmQAQFDF2Aqxej2X6UY8lo9Ya2cm2z2mjU NniowlEGL/trHy/zOl+riq3pKsQVs4AeBx7m99DlPqcaYMfqPX54klRbGLiiLLtn44/D cjTw== X-Gm-Message-State: AOJu0YzyRVabgZBsr9HjG0XBY+yFpMmqkhILV0vm7a4wJL0JmLzKKqNy ApbEJRvBw9ZxYPkRINQkN8I4Ma8wO29iOoULXWnXu3Au+B+m+e9gvbudQ8Qts5Q= X-Google-Smtp-Source: AGHT+IG6PM+9qD0ZPdMsC9jFgIS+sZI3BffyDPkqHXfiI+nzzikWqZphiFoFcTZqRBZP3qcOHEDPFA== X-Received: by 2002:a17:902:c949:b0:1d9:8942:d280 with SMTP id i9-20020a170902c94900b001d98942d280mr1047144pla.69.1707525180081; Fri, 09 Feb 2024 16:33:00 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUAzX3+xjuS10Ccm/AgtGSu42NDwRzyOUD8b3Xf+TNqcs9QGXBMbTx4Qx/SMhAqpbaVZPhi6lTltAKnI/2qcSfjO77jELASGH5JgdLFj25Z0OYPyrIz9WyEI6bIgjduzArG1AMKQ+SrNVKMDQPJurvs+BsNDtmgDLV0vHcubeoXBjgoyPDbxDRM8A1dsSvX80N4IBI4 Received: from localhost (fwdproxy-prn-018.fbsv.net. [2a03:2880:ff:12::face:b00c]) by smtp.gmail.com with ESMTPSA id mi3-20020a170902fcc300b001d944bf2d83sm2080103plb.7.2024.02.09.16.32.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 16:32:59 -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 v9 2/3] netdevsim: forward skbs from one connected port to another Date: Fri, 9 Feb 2024 16:32:39 -0800 Message-Id: <20240210003240.847392-3-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240210003240.847392-1-dw@davidwei.uk> References: <20240210003240.847392-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 both the newly added nsim_dev_list_lock and rtnl_lock. This makes it safe with concurrent calls to linking two netdevsims together. Signed-off-by: David Wei --- drivers/net/netdevsim/netdev.c | 28 +++++++++++++++++++++++----- drivers/net/netdevsim/netdevsim.h | 1 + 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c index 9063f4f2971b..13d3e1536451 100644 --- a/drivers/net/netdevsim/netdev.c +++ b/drivers/net/netdevsim/netdev.c @@ -29,19 +29,37 @@ static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev) { struct netdevsim *ns = netdev_priv(dev); + unsigned int len = skb->len; + struct netdevsim *peer_ns; + 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; + if (ret == NET_XMIT_DROP) { + ns->tx_dropped++; + } else { + ns->tx_packets++; + ns->tx_bytes += len; + } 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 +88,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 +321,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 Sat Feb 10 00:32:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13552007 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 B8C5F20F1 for ; Sat, 10 Feb 2024 00:33:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707525183; cv=none; b=cASUiFHLht+JPOaCRWAZhsXoOwwf/DbbTL3AVpF3F9/QbsFZMrtBaUAo756K/8WouvJoDfVls6jVco6+cofnXOLKGqX/nXcjS7QC116vbqyIsiPiD4eCrPf51+e0kuRS0MVc0id9CDxQy9BvO2xCM6V7xaMvu2kdlQ63t/jMLtg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707525183; c=relaxed/simple; bh=22FbttwhJsXxkbiGLGQiGiw9/68ZxM9lKImRyOV9gLQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mUt9jYyL4xoa7JzCAWXy/Vr0MiA9kbbk8cFCZWt6wrWU99BJtBCXMDp6iaAdyIC02e9O+Grtl9Q5G/HGR0Y8i8vWS0ZT0r+0kO9empbzDlGrbpQpwK9GKgHLMWei1vFypi2rV+NZeL4i1Em4zvR4ZLdtt7pEnKRIAKB+duIsvfc= 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=QavA3BsO; arc=none smtp.client-ip=209.85.210.178 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="QavA3BsO" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6e09493eb8eso707819b3a.1 for ; Fri, 09 Feb 2024 16:33:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1707525181; x=1708129981; 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=BoS7Yv4Dm9pKnywF75Lod+pluPkWkES1eynTinaQnGs=; b=QavA3BsOrlFP7gPbT5DnS2S2SPQ6m57bdvO/IdoqOIiOeEmfizYFQlqLvDo1Llu4kP Ivwy2MFxN4J5jb7laRacF8RAFaRAvG3LS+zm5ak7xhBHfWlkksy5D5bvkZu/iA2bbstf miVTAMRgGCeYMlsjp5FgZ9OKPh20uVi1N3Z4xHxoW9ILNf1J507n0VE8v7+5yOkkCkNK yyn6a684BphoCc/frrlSTyrp562WDD21K5e4lkK1nQ7GXn+UByTO6GSiBSLeQVjvC0tl 5It7jVzEzJ7kRyNXZOwIDudiBm+iFkG84MEosUxt5t+8Q/hRtylihhN9MjykXgng10j+ ZkzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707525181; x=1708129981; 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=BoS7Yv4Dm9pKnywF75Lod+pluPkWkES1eynTinaQnGs=; b=JXbSswQthnVhzPLX22rHyPvIFVLow7CA2PzhOgbd3pOC9k1YuQ3awsoJoGGdxqneKw MqQvi+EpyW9T5ecDBubEFKBkKYwr5uRyQ71+J4CUoSLMiy7Zkz8E+jKabkw+PAQOWJHC U8MlDu8kcx6u0f3T1aT9lAzqxVCqEqyHFySpjdPEAJj6lPD6gFdHp2TnVB2RQpREc28d Zj03yWr+F2+Io/64tKJJ+mzhwRAzg3rArm9DXsUf64+Re7zHnmev823wR6sIln11WK8L 9UIKLMN/JSJHAUKjksRXiCWSwimXsZabXRz3Te0ykWefZDfDqAG52rg+bgQIi4zzcgC8 dP9w== X-Gm-Message-State: AOJu0Yxlqm3qRZk+IO5c8aAIn6LiaQ/fMbm3mdGFFKOB+SOa9Oy/uPHo 4AlQroy0tV9Gik0y7Hpmy1LWn/xq8fCsfoWvSy5Tc9azpCjsX85wXnQVHkiZy9w= X-Google-Smtp-Source: AGHT+IECpK29xtVqkBXVFIWn424IpgwjbB4zlaw23sy/W3qJ7AQVO+oddO1wX2F8QMG1Zh5MJkO2Zw== X-Received: by 2002:a05:6a21:e85:b0:19e:abe0:f0e with SMTP id ma5-20020a056a210e8500b0019eabe00f0emr3616897pzb.5.1707525181025; Fri, 09 Feb 2024 16:33:01 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUWHXSdM60tzH+oPD7cRLhYsqTg1wb5M5Mlf6cmPbeJBZh/4snANf/pjiX+bGb1n0Rr7jHLdkvogdbu4HEJyWhRTYgDa16aNMcOm0Nd8uRqFvmb5AYgK7B4wmr8ePYOicQ6oALmDJcWfWFu4mJBZGzfTm0KATBAlKLZVENfyMAmJjlwgpdNH+LZTqachh3Kr3O01XVK Received: from localhost (fwdproxy-prn-020.fbsv.net. [2a03:2880:ff:14::face:b00c]) by smtp.gmail.com with ESMTPSA id s21-20020a63af55000000b005cf5cbac29asm2431660pgo.53.2024.02.09.16.33.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 16:33: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 v9 3/3] netdevsim: add selftest for forwarding skb between connected ports Date: Fri, 9 Feb 2024 16:32:40 -0800 Message-Id: <20240210003240.847392-4-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240210003240.847392-1-dw@davidwei.uk> References: <20240210003240.847392-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/Makefile | 1 + .../selftests/drivers/net/netdevsim/peer.sh | 133 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.sh diff --git a/tools/testing/selftests/drivers/net/netdevsim/Makefile b/tools/testing/selftests/drivers/net/netdevsim/Makefile index 7a29a05bea8b..5bace0b7fb57 100644 --- a/tools/testing/selftests/drivers/net/netdevsim/Makefile +++ b/tools/testing/selftests/drivers/net/netdevsim/Makefile @@ -10,6 +10,7 @@ TEST_PROGS = devlink.sh \ fib.sh \ hw_stats_l3.sh \ nexthop.sh \ + peer.sh \ psample.sh \ tc-mq-visibility.sh \ udp_tunnel_nic.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..07f46d195954 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh @@ -0,0 +1,133 @@ +#!/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_2_ID=$((RANDOM % 1024)) +NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/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 +NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_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_FD=$((RANDOM % 1024)) +exec {NSIM_DEV_1_FD} $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_FD:$NSIM_DEV_1_IFIDX 2000:$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_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with self should fail" + exit 1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$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_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD: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_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_UNLINK + +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