From patchwork Wed Aug 25 10:42:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Doucha X-Patchwork-Id: 1520630 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.cz header.i=@suse.cz header.a=rsa-sha256 header.s=susede2_rsa header.b=LuDImNTw; dkim=fail reason="signature verification failed" header.d=suse.cz header.i=@suse.cz header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=Ne1kyYCQ; dkim-atps=neutral Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GvjHy1NrGz9sVw for ; Wed, 25 Aug 2021 20:43:25 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 6BCE43C3072 for ; Wed, 25 Aug 2021 12:43:23 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-6.smtp.seeweb.it (in-6.smtp.seeweb.it [IPv6:2001:4b78:1:20::6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 2F9F93C2183 for ; Wed, 25 Aug 2021 12:43:02 +0200 (CEST) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-6.smtp.seeweb.it (Postfix) with ESMTPS id DBFB71400B65 for ; Wed, 25 Aug 2021 12:43:01 +0200 (CEST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 2ADA92213C for ; Wed, 25 Aug 2021 10:43:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1629888181; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3P6uwswxjwshpS0UQASxy5qDTGQQ9fVLDDrqL3UwyT8=; b=LuDImNTw0R6ldbdYxt8axKWaE2oWfIV+QFBt06PfO1me9g6NIbtuO2mb+/BC0LGLT8Zng/ zz0JVS/xk6FqbkHVB2DA6GFVfnVd+HZf97DZ9FRApAn4aR19uipOyP3IFpQVGGJfk9VeFI 1ghBgp1QHVzH9Or1PCE4NECBOJz2KJQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1629888181; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=3P6uwswxjwshpS0UQASxy5qDTGQQ9fVLDDrqL3UwyT8=; b=Ne1kyYCQhGQp5j1RO7fVEdyyadUjk0OT1nITMg0FtgcfoGJXQa065qduYEe5kXKXEq+aV6 yGX28ZUSWXmaOrBg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 16DE313C16 for ; Wed, 25 Aug 2021 10:43:01 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id HhagBLUeJmGELwAAMHmgww (envelope-from ) for ; Wed, 25 Aug 2021 10:43:01 +0000 From: Martin Doucha To: ltp@lists.linux.it Date: Wed, 25 Aug 2021 12:42:58 +0200 Message-Id: <20210825104300.15255-1-mdoucha@suse.cz> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-6.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-6.smtp.seeweb.it Subject: [LTP] [PATCH 1/3] Add NETDEV_ADD_DEVICE() helper function X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Martin Doucha --- doc/network-c-api.txt | 4 ++++ include/tst_netdevice.h | 5 +++++ lib/tst_netdevice.c | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/doc/network-c-api.txt b/doc/network-c-api.txt index 145836a81..f24614fd8 100644 --- a/doc/network-c-api.txt +++ b/doc/network-c-api.txt @@ -162,6 +162,10 @@ stage. on success, 0 on error. Add +"CONFIG_VETH"+ to +test.needs_kconfigs+ if your test calls this function. +- +int NETDEV_ADD_DEVICE(const char *ifname, const char *devtype)+ - Creates + a new network device named +ifname+ of specified device type. Returns 1 on + success, 0 on error. + - +int REMOVE_NETDEV(const char *ifname)+ – Removes network device +ifname+. Returns 1 on success, 0 on error. diff --git a/include/tst_netdevice.h b/include/tst_netdevice.h index 3a6698731..0bd33765b 100644 --- a/include/tst_netdevice.h +++ b/include/tst_netdevice.h @@ -23,6 +23,11 @@ int tst_create_veth_pair(const char *file, const int lineno, #define CREATE_VETH_PAIR(ifname1, ifname2) \ tst_create_veth_pair(__FILE__, __LINE__, (ifname1), (ifname2)) +int tst_netdev_add_device(const char *file, const int lineno, + const char *ifname, const char *devtype); +#define NETDEV_ADD_DEVICE(ifname, devtype) \ + tst_netdev_add_device(__FILE__, __LINE__, (ifname), (devtype)) + int tst_remove_netdev(const char *file, const int lineno, const char *ifname); #define REMOVE_NETDEV(ifname) tst_remove_netdev(__FILE__, __LINE__, (ifname)) diff --git a/lib/tst_netdevice.c b/lib/tst_netdevice.c index a4bc22635..9a8c622e2 100644 --- a/lib/tst_netdevice.c +++ b/lib/tst_netdevice.c @@ -157,6 +157,50 @@ int tst_create_veth_pair(const char *file, const int lineno, return ret; } +int tst_netdev_add_device(const char *file, const int lineno, + const char *ifname, const char *devtype) +{ + int ret; + struct ifinfomsg info = { .ifi_family = AF_UNSPEC }; + struct tst_rtnl_context *ctx; + struct tst_rtnl_attr_list attrs[] = { + {IFLA_IFNAME, ifname, strlen(ifname) + 1, NULL}, + {IFLA_LINKINFO, NULL, 0, (const struct tst_rtnl_attr_list[]){ + {IFLA_INFO_KIND, devtype, strlen(devtype), NULL}, + {0, NULL, -1, NULL} + }}, + {0, NULL, -1, NULL} + }; + + if (strlen(ifname) >= IFNAMSIZ) { + tst_brk_(file, lineno, TBROK, + "Network device name \"%s\" too long", ifname); + return 0; + } + + ctx = create_request(file, lineno, RTM_NEWLINK, + NLM_F_CREATE | NLM_F_EXCL, &info, sizeof(info)); + + if (!ctx) + return 0; + + if (tst_rtnl_add_attr_list(file, lineno, ctx, attrs) != 2) { + tst_rtnl_destroy_context(file, lineno, ctx); + return 0; + } + + ret = tst_rtnl_send_validate(file, lineno, ctx); + tst_rtnl_destroy_context(file, lineno, ctx); + + if (!ret) { + tst_brk_(file, lineno, TBROK, + "Failed to create %s device %s: %s", devtype, ifname, + tst_strerrno(tst_rtnl_errno)); + } + + return ret; +} + int tst_remove_netdev(const char *file, const int lineno, const char *ifname) { struct ifinfomsg info = { .ifi_family = AF_UNSPEC };