Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7486608ybi; Thu, 1 Aug 2019 08:54:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqzer+3RZswWY5xYw31I1wbiMNYKXPg7Ui8fCjvUc9t09I5zVWg645pSN9Bcb8qu4mmxAPO+ X-Received: by 2002:a65:5188:: with SMTP id h8mr8995262pgq.294.1564674876932; Thu, 01 Aug 2019 08:54:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564674876; cv=none; d=google.com; s=arc-20160816; b=bEIZUkZ1oGyUePX1a0ATxSgzDgdhr6yXjmgfLwDggzTCs9j9gti0WAm0IjpMlViXgK 1FhMoW5p1jcueifIUdmQ/Wq21TKC+VMPLGk0SVmSroIWR+F/Ya9UbDjpbI/SWzYYSVUe 5UO2z/3DsXr3ky1QY+koluaEurtm08cLSKgM/Iv3x+ymygore9lVBfXbSeR8lrDeJWc+ zLOI85IxNofnxWcoC+7Z18l16+V1si4qLPqwJlWpYPXy3nXUuclfaGpx5MQvQtm99Jzo WHjg9xNj1mk0ajyNQMf3yAY1PMGSXGdq7tgFonlC7Yyutw5/gwhM06P7K5/TkLxSiLna OUkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ruGQf4tZM4oAfD0ozLuggRwSNc5S+Q+FPXigsdIznNY=; b=FK2vVMeEfHwbejPaIGHO5NdjThTYhp1ZR6WbvbeDdkL0n4dE2OrpeaVXw0Q6N7UIfW Ejm4eEoPNfbphaE7gfxz5J2NL1da0WYrWmARYtcweJNmb34S9ENrWRi6f9zjx7yUWlUp av/OCjwzES+52IScGj5yKEaL/eReH9dndATGuTEj7fMCduFMP0gL3kffvb21dtV2P3J5 LS/4YWQqRMjW4E9Tl6mdBEqZEV8Sg+89xuLoq+VXe34LXjJjvR32bStF2YgyV4tSUqIi 4Jdtf+FrSVV7dM01tro3HdRU9HqRQS7WcUvT3rUdplRGWC3MAH2KazWeGXq2zB+tROAd KBnQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y4si34963272pgq.534.2019.08.01.08.54.21; Thu, 01 Aug 2019 08:54:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732711AbfHAP0N (ORCPT + 99 others); Thu, 1 Aug 2019 11:26:13 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58422 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731484AbfHAP0M (ORCPT ); Thu, 1 Aug 2019 11:26:12 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EFE0B927BF; Thu, 1 Aug 2019 15:26:11 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-117-63.ams2.redhat.com [10.36.117.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id ED24A600D1; Thu, 1 Aug 2019 15:26:09 +0000 (UTC) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: kvm@vger.kernel.org, Stefan Hajnoczi , Dexuan Cui , virtualization@lists.linux-foundation.org, "David S. Miller" , Jorgen Hansen , linux-kernel@vger.kernel.org Subject: [PATCH v2 08/11] VSOCK: add vsock_get_local_cid() test utility Date: Thu, 1 Aug 2019 17:25:38 +0200 Message-Id: <20190801152541.245833-9-sgarzare@redhat.com> In-Reply-To: <20190801152541.245833-1-sgarzare@redhat.com> References: <20190801152541.245833-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 01 Aug 2019 15:26:12 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds utility to get local CID, useful to understand if we are in the host or guest. Signed-off-by: Stefano Garzarella --- tools/testing/vsock/util.c | 17 +++++++++++++++++ tools/testing/vsock/util.h | 1 + 2 files changed, 18 insertions(+) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index d46a6e079b96..41b94495ecb1 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "timeout.h" #include "control.h" @@ -44,6 +45,22 @@ unsigned int parse_cid(const char *str) return n; } +/* Get the local CID */ +unsigned int vsock_get_local_cid(int fd) +{ + struct sockaddr_vm svm; + socklen_t svm_len = sizeof(svm); + + if (getsockname(fd, (struct sockaddr *) &svm, &svm_len)) { + perror("getsockname"); + exit(EXIT_FAILURE); + } + + assert(svm.svm_family == AF_VSOCK); + + return svm.svm_cid; +} + /* Connect to and return the file descriptor. */ int vsock_stream_connect(unsigned int cid, unsigned int port) { diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index fe524d393d67..379e02ab59bb 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -36,6 +36,7 @@ unsigned int parse_cid(const char *str); int vsock_stream_connect(unsigned int cid, unsigned int port); int vsock_stream_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp); +unsigned int vsock_get_local_cid(int fd); void send_byte(int fd, int expected_ret); void recv_byte(int fd, int expected_ret); void run_tests(const struct test_case *test_cases, -- 2.20.1