From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 353B95A0279 for ; Thu, 21 Dec 2023 07:53:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1703141610; bh=Ukkpf7x45ig4tYOes5Hctlyp5dS+VDCtbAtbH3rVKuM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QEXAXgNeJgmx0r8QbfTfu1/n6sx5IM+8GU90xR2I+7j3KcypSfUBqL1rILe/olg0b filRS9+VDHpPJLnCpB9HM2bZu72LVEB0+zyU7VenHxkan/TXOVOJmFYtNGPGQDbU1z XOauaOdRYdpxVBPFCqSC8L6OtihEx75Us1AyrO1WA4pFe+xI+MJuf5IkwygRLvrZhh m4QTOKE6De1T998+DvqLOOlLHR8KZYZUZpVm59Ql4mnLg8L8VGOxtt5+h38toUVYXh hZDZpLOkz7YSqOVIFlMWd0q0ycPBw9dvaAN3wLKCuKleQcaiT77myfPGTJd7UwYIeO Lsd99bKTRh5hg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Swh3G5Z3Zz4xSn; Thu, 21 Dec 2023 17:53:30 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 12/12] icmp: Dedicated functions for starting and closing ping sequences Date: Thu, 21 Dec 2023 17:53:27 +1100 Message-ID: <20231221065327.1307827-13-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231221065327.1307827-1-david@gibson.dropbear.id.au> References: <20231221065327.1307827-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: HQ3FUKARNN4F24JCAXW4ARSJGTXJGZL3 X-Message-ID-Hash: HQ3FUKARNN4F24JCAXW4ARSJGTXJGZL3 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: ICMP sockets are cleaned up on a timeout implemented in icmp_timer_one(), and the logic to do that cleanup is open coded in that function. Similarly new sockets are opened when we discover we don't have an existing one in icmp_tap_handler(), and again the logic is open-coded. That's not the worst thing, but it's a bit cleaner to have dedicated functions for the creation and destruction of ping sockets. This will also make things a bit easier for future changes we have in mind. Signed-off-by: David Gibson --- icmp.c | 102 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/icmp.c b/icmp.c index 129a7f1..d669351 100644 --- a/icmp.c +++ b/icmp.c @@ -132,6 +132,70 @@ unexpected: warn("%s: Unexpected packet on ping socket", pname); } +/** + * icmp_ping_close() - Close out and cleanup a ping sequence + * @c: Execution context + * @id_map: id map entry of the sequence to close + */ +static void icmp_ping_close(const struct ctx *c, struct icmp_id_sock *id_map) +{ + epoll_ctl(c->epollfd, EPOLL_CTL_DEL, id_map->sock, NULL); + close(id_map->sock); + id_map->sock = -1; + id_map->seq = -1; +} + +/** + * icmp_ping_new() - Prepare a new ping socket for a new id + * @c: Execution context + * @id_map: id map entry of the sequence to open + * @af: Address family, AF_INET or AF_INET6 + * @id: ICMP id for the new sequence + * + * Return: Newly opened ping socket fd, or -1 on failure + */ +static int icmp_ping_new(const struct ctx *c, struct icmp_id_sock *id_map, + int af, uint16_t id) +{ + uint8_t proto = af == AF_INET ? IPPROTO_ICMP : IPPROTO_ICMPV6; + const char *const pname = af == AF_INET ? "ICMP" : "ICMPv6"; + union icmp_epoll_ref iref = { .id = id }; + const void *bind_addr; + const char *bind_if; + int s; + + if (af == AF_INET) { + bind_addr = &c->ip4.addr_out; + bind_if = c->ip4.ifname_out; + } else { + bind_addr = &c->ip6.addr_out; + bind_if = c->ip6.ifname_out; + } + + s = sock_l4(c, af, proto, bind_addr, bind_if, 0, iref.u32); + + if (s < 0) { + warn("Cannot open \"ping\" socket. You might need to:"); + warn(" sysctl -w net.ipv4.ping_group_range=\"0 2147483647\""); + warn("...echo requests/replies will fail."); + goto cancel; + } + + if (s > FD_REF_MAX) + goto cancel; + + id_map->sock = s; + + debug("%s: new socket %i for echo ID %"PRIu16, pname, s, id); + + return s; + +cancel: + if (s >= 0) + close(s); + return -1; +} + /** * icmp_tap_handler() - Handle packets from tap * @c: Execution context @@ -148,7 +212,6 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, int af, const void *saddr, const void *daddr, const struct pool *p, const struct timespec *now) { - uint8_t proto = af == AF_INET ? IPPROTO_ICMP : IPPROTO_ICMPV6; const char *const pname = af == AF_INET ? "ICMP" : "ICMPv6"; union { struct sockaddr sa; @@ -200,37 +263,9 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, int af, ASSERT(0); } - if ((s = id_map->sock) < 0) { - union icmp_epoll_ref iref = { .id = id }; - const void *bind_addr; - const char *bind_if; - - if (af == AF_INET) { - bind_addr = &c->ip4.addr_out; - bind_if = c->ip4.ifname_out; - } else { - bind_addr = &c->ip6.addr_out; - bind_if = c->ip6.ifname_out; - } - - s = sock_l4(c, af, proto, bind_addr, bind_if, 0, iref.u32); - - if (s < 0) { - warn("Cannot open \"ping\" socket. You might need to:"); - warn(" sysctl -w net.ipv4.ping_group_range=\"0 2147483647\""); - warn("...echo requests/replies will fail."); - return 1; - } - - if (s > FD_REF_MAX) { - close(s); + if ((s = id_map->sock) < 0) + if ((s = icmp_ping_new(c, id_map, af, id)) < 0) return 1; - } - - id_map->sock = s; - - debug("%s: new socket %i for echo ID %"PRIu16, pname, s, id); - } id_map->ts = now->tv_sec; @@ -257,10 +292,7 @@ static void icmp_timer_one(const struct ctx *c, struct icmp_id_sock *id_map, if (id_map->sock < 0 || now->tv_sec - id_map->ts <= ICMP_ECHO_TIMEOUT) return; - epoll_ctl(c->epollfd, EPOLL_CTL_DEL, id_map->sock, NULL); - close(id_map->sock); - id_map->sock = -1; - id_map->seq = -1; + icmp_ping_close(c, id_map); } /** -- 2.43.0