From patchwork Thu Feb 15 19:43:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wei X-Patchwork-Id: 13559101 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 A9C0F13A890 for ; Thu, 15 Feb 2024 19:43:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708026212; cv=none; b=im255mMc0VgtbZ2D93860ggKJox+moEm5Nb1oOuq9U6usovkoXC+uWE1RFGY1GNz0qsat3H60D42mHGezwfLcqLc4qVLCNtrBaB6OoAKXpX08h2WVhVK6KX9AQRrqZaIasalaKzMrfPb/F9mMU8PUtjhuyQQAzdL9BIs8SJnvDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708026212; c=relaxed/simple; bh=0cUVoJTJr3QlY4VeIpHF08NETkkBRKcCVRXoF8XuXsc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Md/OolLxvq2mgyvJ+lAtEnOEKUAExv6MRn0ysjEpBYK9tyPuZtHeXl0zp0WhDsE4B/YoXT7Hd+joViv6uq2ghHRdwJSdMGQRN0t4qrUtlOi6jqxUNWT26OLD59Z6fi2rUSTlSobvjg6BxY7LS1MPSfYdW9SlSvOF/qqtR8f2TQw= 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=hOrba9R7; arc=none smtp.client-ip=209.85.214.181 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="hOrba9R7" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-1d918008b99so10195745ad.3 for ; Thu, 15 Feb 2024 11:43:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=davidwei-uk.20230601.gappssmtp.com; s=20230601; t=1708026210; x=1708631010; 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=ofa1RGnWd2lIotrEIt0lDKejuMRo/GrQuejfvweLENM=; b=hOrba9R7Vmv4z9n5BsyKC8JjXM5Lz/kgV0Nyhe1g887Xyo2NFKgD0tNgQUCcvQzRe4 UWLP3LV9tJR9L7JyztA5z/JLpEy3XbtDODiMssrAbIoyXybCnlL7Kpg1USftSjh7mdZM 6ZderDx3MWheGulltXjLv5rUnd2VdCxZHk7Alx3qrHpbCfzRAI/v+xnfNCHB5LPrzmsj hl9p6cFFNuJ3MbAkv3xtb2pK7UcTAHqfia2c/gDZIxOjT2ETDfjdPc+T4prkDWFizz++ AivwHwPchgwkPmUmg5hzJrZ5MaFm5cA6NKTtWs1x5EH1bjodtM/7paO+4EoAoLyz8+6k dJJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708026210; x=1708631010; 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=ofa1RGnWd2lIotrEIt0lDKejuMRo/GrQuejfvweLENM=; b=ABllDMp1oBhVZ0A7tyYZtGQGn8lrnLMgJ9ZUOhPEXWJhS30fN7VnYiUSLFlej1V9/Y 3945R1u0KAKlr3yFrQ+Xys2P2Jor3e/66N72KsGEVKKM80woGB9fT2/FBksaz6KQ7bYI D0OFMp5OaDJQtZrTHkktMe2qi4Z+r5B18hvMPcfG78Sz/qei9Y8PfXk9EhGze38l0+6+ QtsjGiWg9gHpfxjfDreDtlDLYf/EBiZ/sgnEz36y6m8QM7NMrJ2FHorLFWXJ5/aMDhOp 1tI3+0ley4StERMoaYzVN50rPD0ALRIraI4428tb+ravtZ1uqiHkbzA70C+bXb7RK18h dAsQ== X-Forwarded-Encrypted: i=1; AJvYcCWZneicv3OLwEHzaERILZ21mIG7paAN4EHxDdNv6gmRnh2cFPvWc4Rxo5I2DOp8Ib06e0FEIZjJ+q67oxasQy7hQj1N+Y7o X-Gm-Message-State: AOJu0YyufqzTqkC8AHLSdlMk6MwlBhp/0SLycQjC3Fa+GVywAkRhfpwf 3S23N/5hXGTCXfuzWWsa3QrVrdoTAvWLlUvYPH3VU7UEwwchsGeot2MM/9HSLV0= X-Google-Smtp-Source: AGHT+IGb+1niMXOr2Rpw7FTwjOc8Qlh0P/ebw0stYPP7Qb4XiCeSOszANEsdItmKGfHOPJ86wg6juA== X-Received: by 2002:a17:903:187:b0:1db:2c9a:2150 with SMTP id z7-20020a170903018700b001db2c9a2150mr3044215plg.54.1708026209995; Thu, 15 Feb 2024 11:43:29 -0800 (PST) Received: from localhost (fwdproxy-prn-005.fbsv.net. [2a03:2880:ff:5::face:b00c]) by smtp.gmail.com with ESMTPSA id jy8-20020a17090342c800b001dba356b96esm108650plb.306.2024.02.15.11.43.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 11:43:29 -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 v11 3/3] netdevsim: add selftest for forwarding skb between connected ports Date: Thu, 15 Feb 2024 11:43:25 -0800 Message-Id: <20240215194325.1364466-4-dw@davidwei.uk> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240215194325.1364466-1-dw@davidwei.uk> References: <20240215194325.1364466-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 | 139 ++++++++++++++++++ 2 files changed, 140 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..c3051399c509 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh @@ -0,0 +1,139 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +NSIM_DEV_1_ID=$((256 + RANDOM % 256)) +NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID +NSIM_DEV_2_ID=$((512 + RANDOM % 256)) +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 +### + +socat_check || exit 4 + +modprobe netdevsim + +# linking + +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW +udevadm settle + +setup_ns + +NSIM_DEV_1_FD=$((256 + RANDOM % 256)) +exec {NSIM_DEV_1_FD} $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netdevsim should fail" + cleanup_ns + 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" + cleanup_ns + 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" + cleanup_ns + 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" + cleanup_ns + 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" + cleanup_ns + exit 1 +fi + +# send/recv packets + +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