public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 3/3] conf: Terminate on EMFILE or ENFILE on sockets for port mapping
Date: Thu, 9 Mar 2023 09:45:01 +1100	[thread overview]
Message-ID: <ZAkP7fuGv9jJ6wHu@yekko> (raw)
In-Reply-To: <20230308123348.2232214-4-sbrivio@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 4060 bytes --]

On Wed, Mar 08, 2023 at 01:33:48PM +0100, Stefano Brivio wrote:
> In general, we don't terminate or report failures if we fail to bind
> to some ports out of a given port range specifier, to allow users to
> conveniently specify big port ranges (or "all") without having to
> care about ports that might already be in use.
> 
> However, running out of the open file descriptors quota is a
> different story: we can't do what the user requested in a very
> substantial way.
> 
> For example, if the user specifies '-t all' and we can only bind
> 1024 sockets, the behaviour is rather unexpected.
> 
> Fail whenever socket creation returns -ENFILE or -EMFILE.
> 
> Link: https://bugs.passt.top/show_bug.cgi?id=27
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  conf.c | 36 +++++++++++++++++++++++++++++-------
>  1 file changed, 29 insertions(+), 7 deletions(-)
> 
> diff --git a/conf.c b/conf.c
> index 582c391..ee56501 100644
> --- a/conf.c
> +++ b/conf.c
> @@ -184,6 +184,7 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
>  	bool exclude_only = true, bound_one = false;
>  	uint8_t exclude[PORT_BITMAP_SIZE] = { 0 };
>  	sa_family_t af = AF_UNSPEC;
> +	int ret;
>  
>  	if (!strcmp(optarg, "none")) {
>  		if (fwd->mode)
> @@ -218,11 +219,18 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
>  
>  		for (i = 0; i < PORT_EPHEMERAL_MIN; i++) {
>  			if (optname == 't') {
> -				if (!tcp_sock_init(c, AF_UNSPEC, NULL, NULL, i))
> +				ret = tcp_sock_init(c, AF_UNSPEC, NULL, NULL,
> +						    i);
> +				if (ret == -ENFILE || ret == -EMFILE)
> +					goto enfile;
> +				if (!ret)
>  					bound_one = true;
>  			} else if (optname == 'u') {
> -				if (!udp_sock_init(c, 0, AF_UNSPEC, NULL, NULL,
> -						   i))
> +				ret = udp_sock_init(c, 0, AF_UNSPEC, NULL, NULL,
> +						    i);
> +				if (ret == -ENFILE || ret == -EMFILE)
> +					goto enfile;
> +				if (!ret)
>  					bound_one = true;
>  			}
>  		}
> @@ -303,10 +311,16 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
>  			bitmap_set(fwd->map, i);
>  
>  			if (optname == 't') {
> -				if (!tcp_sock_init(c, af, addr, ifname, i))
> +				ret = tcp_sock_init(c, af, addr, ifname, i);
> +				if (ret == -ENFILE || ret == -EMFILE)
> +					goto enfile;
> +				if (!ret)
>  					bound_one = true;
>  			} else if (optname == 'u') {
> -				if (!udp_sock_init(c, 0, af, addr, ifname, i))
> +				ret = udp_sock_init(c, 0, af, addr, ifname, i);
> +				if (ret == -ENFILE || ret == -EMFILE)
> +					goto enfile;
> +				if (!ret)
>  					bound_one = true;
>  			} else {
>  				/* No way to check in advance for -T and -U */
> @@ -358,10 +372,16 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
>  			fwd->delta[i] = mapped_range.first - orig_range.first;
>  
>  			if (optname == 't') {
> -				if (!tcp_sock_init(c, af, addr, ifname, i))
> +				ret = tcp_sock_init(c, af, addr, ifname, i);
> +				if (ret == -ENFILE || ret == -EMFILE)
> +					goto enfile;
> +				if (!ret)
>  					bound_one = true;
>  			} else if (optname == 'u') {
> -				if (!udp_sock_init(c, 0, af, addr, ifname, i))
> +				ret = udp_sock_init(c, 0, af, addr, ifname, i);
> +				if (ret == -ENFILE || ret == -EMFILE)
> +					goto enfile;
> +				if (!ret)
>  					bound_one = true;
>  			} else {
>  				/* No way to check in advance for -T and -U */
> @@ -374,6 +394,8 @@ static void conf_ports(const struct ctx *c, char optname, const char *optarg,
>  		goto bind_fail;
>  
>  	return;
> +enfile:
> +	die("Can't open enough sockets for port specifier: %s", optarg);
>  bad:
>  	die("Invalid port specifier %s", optarg);
>  overlap:

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2023-03-08 22:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-08 12:33 [PATCH 0/3] Fail gracefully on too many open files Stefano Brivio
2023-03-08 12:33 ` [PATCH 1/3] tcp, udp, util: Pass socket creation errors all the way up Stefano Brivio
2023-03-08 22:16   ` David Gibson
2023-03-08 12:33 ` [PATCH 2/3] tcp, udp: Fix partial success return codes in {tcp,udp}_sock_init() Stefano Brivio
2023-03-08 22:43   ` David Gibson
2023-03-08 12:33 ` [PATCH 3/3] conf: Terminate on EMFILE or ENFILE on sockets for port mapping Stefano Brivio
2023-03-08 22:45   ` David Gibson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZAkP7fuGv9jJ6wHu@yekko \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).