From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id C23225A026D for ; Sat, 6 Jan 2024 17:01:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1704556875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pf52ICcVmEL7tRaxOqehaxwXuJWJ2zk91/zCFLpj2X4=; b=eRnQPLys6K1zRyFxlcO2AGQnbLpDGqIKfskMXX0pYeRWPVfKzO4sMsrVAxutxU6B73NZK3 GT56ZTjJUmfNahh1b/ZEg3KEqSXHCkV71+56aNMELG6HM2RLjIAzpYV7HucL15CuusvUhr sryzGseYzxW8qkOkPMlyR9j98PkJEIA= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-533-9c1PG2XSOXCMZI_gR1fTKQ-1; Sat, 06 Jan 2024 11:01:12 -0500 X-MC-Unique: 9c1PG2XSOXCMZI_gR1fTKQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id CDD151C0CCAD; Sat, 6 Jan 2024 16:01:11 +0000 (UTC) Received: from elisabeth (unknown [10.39.208.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 042645190; Sat, 6 Jan 2024 16:01:10 +0000 (UTC) Date: Sat, 6 Jan 2024 17:01:04 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH v2 12/12] icmp: Dedicated functions for starting and closing ping sequences Message-ID: <20240106170104.48d4518e@elisabeth> In-Reply-To: <20231221065327.1307827-13-david@gibson.dropbear.id.au> References: <20231221065327.1307827-1-david@gibson.dropbear.id.au> <20231221065327.1307827-13-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: BVIHR6UO3YDG7V5R4JMFRACDKOQDSARQ X-Message-ID-Hash: BVIHR6UO3YDG7V5R4JMFRACDKOQDSARQ X-MailFrom: sbrivio@redhat.com 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: passt-dev@passt.top 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: On Thu, 21 Dec 2023 17:53:27 +1100 David Gibson wrote: > 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 s/cleanup/clean up/ (verb) The commit title and this comment (referring to a sequence) are a bit misleading... this pretty much closes the socket, and as far as I can see setting the sequence to -1 doesn't actually have an effect (despite the fact it's a good practice). If you mean "sequence" not as "sequence number" but rather as a "sequence of pings", then I think we should rename one of the two... either 'seqnum' for the sequence number, or maybe s/sequence/socket/g here. > + * @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); > } > > /** -- Stefano