From patchwork Fri Sep 2 07:14:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ammar Faizi X-Patchwork-Id: 12963716 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27A17C54EE9 for ; Fri, 2 Sep 2022 07:15:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232001AbiIBHPm (ORCPT ); Fri, 2 Sep 2022 03:15:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233577AbiIBHPl (ORCPT ); Fri, 2 Sep 2022 03:15:41 -0400 Received: from gnuweeb.org (gnuweeb.org [51.81.211.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28D1233358 for ; Fri, 2 Sep 2022 00:15:40 -0700 (PDT) Received: from localhost.localdomain (unknown [182.2.70.226]) by gnuweeb.org (Postfix) with ESMTPSA id 1EC8F80C57; Fri, 2 Sep 2022 07:15:35 +0000 (UTC) X-GW-Data: lPqxHiMPbJw1wb7CM9QUryAGzr0yq5atzVDdxTR0iA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1662102939; bh=8KdROfXyudkGNGd97z8dQf49Bep8VmZZWdSZ9sU9JZw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=If5xjmlQ/tKpAS5LrXKcAVuz8lZKHv7qRE+tQNv6xH4j2uN4dz1zL16xhkZqt8eE+ pDFcsF0eeeUZUTZ9qO0M2yPmii+ZfAHGWGBhEnazzP0q4HIiSrCohMfkH4UMu/PqJf kjyD7K1kL1kFsYfUM6I8d45mPRcgb3H3kXyAYSjOZ95W9AHNTiID6jIiLI/ObL0PMh k3iexUors/Vyi9sQIGuw8MhmhFxUFcSaMBALDpl3HKV4ijjPQQZjlNxFOxw+lu8gW9 oWIJjoCBxNtaJUvipIDfBOq1KVPFQRkX2UytVvgfOMiWBIh9JKACIton80R6FBaKi5 tMKWt6sOF1Nqw== From: Ammar Faizi To: Jens Axboe Cc: Ammar Faizi , Dylan Yudaken , Facebook Kernel Team , Pavel Begunkov , io-uring Mailing List , GNU/Weeb Mailing List , Kanna Scarlet , Muhammad Rizki , Alviro Iskandar Setiawan Subject: [PATCH liburing v2 03/12] t/socket-rw: Don't brute force the port number Date: Fri, 2 Sep 2022 14:14:56 +0700 Message-Id: <20220902071153.3168814-4-ammar.faizi@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220902071153.3168814-1-ammar.faizi@intel.com> References: <20220902071153.3168814-1-ammar.faizi@intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org From: Ammar Faizi Don't brute force the port number, use `t_bind_ephemeral_port()`, much simpler and reliable for choosing a port number that is not in use. Cc: Dylan Yudaken Cc: Facebook Kernel Team Cc: Pavel Begunkov Reviewed-by: Alviro Iskandar Setiawan Tested-by: Alviro Iskandar Setiawan Signed-off-by: Ammar Faizi --- test/socket-rw.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/test/socket-rw.c b/test/socket-rw.c index 4fbf032..6211b01 100644 --- a/test/socket-rw.c +++ b/test/socket-rw.c @@ -16,14 +16,15 @@ #include #include #include #include #include #include "liburing.h" +#include "helpers.h" int main(int argc, char *argv[]) { int p_fd[2], ret; int32_t recv_s0; int32_t val = 1; struct sockaddr_in addr; @@ -39,25 +40,15 @@ int main(int argc, char *argv[]) ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val)); assert(ret != -1); ret = setsockopt(recv_s0, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); assert(ret != -1); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("127.0.0.1"); - - do { - addr.sin_port = htons((rand() % 61440) + 4096); - ret = bind(recv_s0, (struct sockaddr*)&addr, sizeof(addr)); - if (!ret) - break; - if (errno != EADDRINUSE) { - perror("bind"); - exit(1); - } - } while (1); + assert(!t_bind_ephemeral_port(recv_s0, &addr)); ret = listen(recv_s0, 128); assert(ret != -1); p_fd[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP); val = 1;