From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <t.lamprecht@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id D80E1616F1
 for <pve-devel@lists.proxmox.com>; Fri,  4 Dec 2020 07:37:50 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id C88BDA9EB
 for <pve-devel@lists.proxmox.com>; Fri,  4 Dec 2020 07:37:20 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (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 firstgate.proxmox.com (Proxmox) with ESMTPS id 334E4A9E0
 for <pve-devel@lists.proxmox.com>; Fri,  4 Dec 2020 07:37:19 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 02ED443EFD
 for <pve-devel@lists.proxmox.com>; Fri,  4 Dec 2020 07:37:19 +0100 (CET)
To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>,
 Stoiko Ivanov <s.ivanov@proxmox.com>
References: <20201203184322.20253-1-s.ivanov@proxmox.com>
 <20201203184322.20253-4-s.ivanov@proxmox.com>
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
Message-ID: <f6e09680-c3d6-a453-6c55-7666d0c6ed26@proxmox.com>
Date: Fri, 4 Dec 2020 07:37:17 +0100
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101
 Thunderbird/84.0
MIME-Version: 1.0
In-Reply-To: <20201203184322.20253-4-s.ivanov@proxmox.com>
Content-Type: text/plain; charset=UTF-8
Content-Language: en-US
Content-Transfer-Encoding: quoted-printable
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.073 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 NICE_REPLY_A           -0.001 Looks like a legit reply (A)
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [anyevent.pm, perl.org]
Subject: Re: [pve-devel] [PATCH http-server 3/3] add debug log for problems
 during accept
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 04 Dec 2020 06:37:50 -0000

On 03.12.20 19:43, Stoiko Ivanov wrote:
> Co-Authored-by: Dominik Csapak <d.csapak@proxmox.com>
> Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
> ---
>  PVE/APIServer/AnyEvent.pm | 9 +++++++++
>  1 file changed, 9 insertions(+)
>=20
> diff --git a/PVE/APIServer/AnyEvent.pm b/PVE/APIServer/AnyEvent.pm
> index 7038b07..d33f6b0 100644
> --- a/PVE/APIServer/AnyEvent.pm
> +++ b/PVE/APIServer/AnyEvent.pm
> @@ -1520,6 +1520,11 @@ sub check_host_access {
> =20
>      my $cip =3D Net::IP->new($clientip);
> =20
> +    if (!$cip) {
> +	print "$$: check_host_access: clientip not parsable: $@\n" if $self->=
{debug};

I'd avoid using variable names 1:1 in log messages for such things, use "=
client IP"
(for not hard coding sub name see below)

> +	return 0;
> +    }
> +
>      my $match_allow =3D 0;
>      my $match_deny =3D 0;
> =20
> @@ -1527,6 +1532,7 @@ sub check_host_access {
>  	foreach my $t (@{$self->{allow_from}}) {
>  	    if ($t->overlaps($cip)) {
>  		$match_allow =3D 1;
> +		print "$$: check_host_access: clientip allowed: ". $t->prefix() . "\=
n" if $self->{debug};
>  		last;
>  	    }
>  	}
> @@ -1535,6 +1541,7 @@ sub check_host_access {
>      if ($self->{deny_from}) {
>  	foreach my $t (@{$self->{deny_from}}) {
>  	    if ($t->overlaps($cip)) {
> +		print "$$: check_host_access: clientip denied: ". $t->prefix() . "\n=
" if $self->{debug};
>  		$match_deny =3D 1;
>  		last;
>  	    }
> @@ -1571,6 +1578,7 @@ sub accept_connections {
>  		my ($pfamily, $pport, $phost) =3D PVE::Tools::unpack_sockaddr_in46($=
sin);
>  		($reqstate->{peer_port}, $reqstate->{peer_host}) =3D ($pport,  Socke=
t::inet_ntop($pfamily, $phost));
>  	    } else {
> +		print "$$: ACCEPT connection: getpeername failed: $!\n" if $self->{d=
ebug};
>  		shutdown($clientfh, 1);
>  		next;
>  	    }
> @@ -1619,6 +1627,7 @@ sub accept_connections {
> =20
>      if (my $err =3D $@) {
>  	syslog('err', $err);
> +	print "$$: ACCEPT connection error: $err\n" if $self->{debug};
>  	shutdown($clientfh, 1) if $early_err;
>  	$self->{end_loop} =3D 1;
>      }
>=20

can we use a helper method for the printing? Something along:

my sub dprintln {
    my ($line) =3D @_;
    return if !$self->{debug};
    print "worker[$$]: $line\n";
}

could maybe drop the worker prefix, but I'd say that all processes accept=
ing connections
can be classified as workers..

If you want to get real fancy (I mean, for perl ;) you could use caller[0=
] to get some call
context to print.

    my ($pkg, $pkgfile, $line, $sub) =3D caller(1);

    print "worker[$$]: $sub: $line\n";

or even

    print "worker[$$]: $pkg +$line: $sub: $line\n";

We could later put such a helper in PVE::Tools and add to EXPORT_OK, coul=
d be helpful,
may want to add some bells n' whistels (control flags?) then - but that's=
 out of scope
for this series.

[0]: https://perldoc.perl.org/functions/caller