Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7486053ybi; Thu, 1 Aug 2019 08:54:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqz8wbv+zHK+R/lT+d/iOPtjI9AI+P+T3MACGASA4nq55SZdYksGBeUdCA3Iv9OlkCGE2Out X-Received: by 2002:aa7:9210:: with SMTP id 16mr55804731pfo.11.1564674846919; Thu, 01 Aug 2019 08:54:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564674846; cv=none; d=google.com; s=arc-20160816; b=HW/IUBEDZtaJsKeP3RKwaBGi0U9Efij6558UrH5St+kP6BqVnRQlA87e3QWeGlxgaQ lJKnRgdeYqAokhI3DKMAchu8QWOPChGtlFsJwQJgG3lMlVi56xh4dN4nBFC958Rx/0zy B8ywlAxXyrEOruJbslD20bJqSnitpJ/343JJautFSU1szax87/wejwVp1ftOrLJ5u+UI Z08rufD3rL+xc7Z0bJrUVdVRbbwK5acS9KVZX/ZjTbCDzEPwprPE2LwobgSRTAHUmYda +RhyenFVbrXPog8ihVuYNeUVDiDQgt/ud5p9FgjANzR8OqZDKjnkURe91qhbYN7SaHWc z/hg== 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=rjZBcDbwKTWr1KnPwqkqax1TrmTdKe+5D9m/RRcDPgU=; b=Z+2j9nRJ9Y3HHTbCN6EsgoTegnG6gw0YG0pfFqdARNPrFEhdCLc2r1S0sNDwj2+d6/ 5A1kGL45R9m+6d49oF8meBSrB0FlMZ2scGrFkcyB+ZMAmhBs1HGh0gvyzYsGi0uI9wau Cc9Lj6PUNgS6QL1D/K76TaFEcHHaLoBvxBWPWIdtkVI437M447NLhcfT6U5T0kp032A2 M3MfHoBd4NyKmEbO/DMeuTtWGLmZSl0H9FTYShqYTnPgQnbQCSR+YVN0HzXy5hVFtZCr XQ0nFXySe0BTy9JoDKXQMfPEM673uJMk+FUXdQiyKbQqrWAc/rQbSVHl2+zv3HpOrPto cFoA== 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 gn22si33644882plb.422.2019.08.01.08.53.51; Thu, 01 Aug 2019 08:54:06 -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 S1732770AbfHAP0W (ORCPT + 99 others); Thu, 1 Aug 2019 11:26:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55936 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732756AbfHAP0T (ORCPT ); Thu, 1 Aug 2019 11:26:19 -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 720BD30B6FAA; Thu, 1 Aug 2019 15:26:19 +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 63010600D1; Thu, 1 Aug 2019 15:26:17 +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 11/11] vsock_test: wait for the remote to close the connection Date: Thu, 1 Aug 2019 17:25:41 +0200 Message-Id: <20190801152541.245833-12-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.42]); Thu, 01 Aug 2019 15:26:19 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before check if a send returns -EPIPE, we need to make sure the connection is closed. To do that, we use epoll API to wait EPOLLRDHUP or EPOLLHUP events on the socket. Reported-by: Jorgen Hansen Signed-off-by: Stefano Garzarella --- tools/testing/vsock/util.c | 38 ++++++++++++++++++++++++++++++++ tools/testing/vsock/util.h | 1 + tools/testing/vsock/vsock_test.c | 10 +++++++++ 3 files changed, 49 insertions(+) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 41b94495ecb1..425181fe196c 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "timeout.h" #include "control.h" @@ -61,6 +62,43 @@ unsigned int vsock_get_local_cid(int fd) return svm.svm_cid; } +/* Wait for the remote to close the connection */ +void vsock_wait_remote_close(int fd) +{ + struct epoll_event ev; + int epollfd, nfds; + + epollfd = epoll_create1(0); + if (epollfd == -1) { + perror("epoll_create1"); + exit(EXIT_FAILURE); + } + + ev.events = EPOLLRDHUP | EPOLLHUP; + ev.data.fd = fd; + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) { + perror("epoll_ctl"); + exit(EXIT_FAILURE); + } + + nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000); + if (nfds == -1) { + perror("epoll_wait"); + exit(EXIT_FAILURE); + } + + if (nfds == 0) { + fprintf(stderr, "epoll_wait timed out\n"); + exit(EXIT_FAILURE); + } + + assert(nfds == 1); + assert(ev.events & (EPOLLRDHUP | EPOLLHUP)); + assert(ev.data.fd == fd); + + close(epollfd); +} + /* 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 7fdb8100f035..89816966c05b 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -45,6 +45,7 @@ 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 vsock_wait_remote_close(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, diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index 64adf45501ca..a664675bec5a 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -84,6 +84,11 @@ static void test_stream_client_close_server(const struct test_opts *opts) control_expectln("CLOSED"); + /* Wait for the remote to close the connection, before check + * -EPIPE error on send. + */ + vsock_wait_remote_close(fd); + send_byte(fd, -EPIPE); /* Skip the read of data wrote by the peer if we are on VMCI and @@ -113,6 +118,11 @@ static void test_stream_server_close_client(const struct test_opts *opts) control_expectln("CLOSED"); + /* Wait for the remote to close the connection, before check + * -EPIPE error on send. + */ + vsock_wait_remote_close(fd); + send_byte(fd, -EPIPE); /* Skip the read of data wrote by the peer if we are on VMCI and -- 2.20.1