[pve-devel] [PATCH storage v2] fix #957 iscsi: improve check_connection logic
Mira Limbeck
m.limbeck at proxmox.com
Tue Mar 11 15:35:34 CET 2025
Thank you for the v2!
some comments inline
> +sub iscsi_test_session {
> + my ($sid) = @_;
> + my $cmd = [$ISCSIADM, '--mode', 'session', '--sid', $sid, '-P1'];
> +
> + my $res = 0;
> + eval {
> + run_command($cmd, errmsg => 'iscsi session test failed', outfunc => sub {
> + my $line = shift;
> + if ($line =~ m/^\s+iSCSI Session State: LOGGED_IN\s*$/) {
> + $res = 1;
> + }
> + });
> + };
> + if (my $err = $@) {
> + die $err;
> + };
This can be written as just `die "$@\n" if $@` instead.
It should contain some additional info though, for example:
`die "session check failed: $@\n" if $@;`
> + return $res;
> +}
> +
> sub iscsi_portals {
> my ($target, $portal_in) = @_;
>
> @@ -560,11 +579,24 @@ sub activate_volume {
> sub check_connection {
> my ($class, $storeid, $scfg) = @_;
>
> - my $portals = iscsi_portals($scfg->{target}, $scfg->{portal});
> -
> - for my $portal (@$portals) {
> - my $result = iscsi_test_portal($portal);
> - return $result if $result;
> + my $cache = {};
> + my $sessions = [];
> + eval {
> + $sessions = iscsi_session($cache, $scfg->{target});
> + };
iscsi_session only dies if the `iscsiadm` command fails or prints
something other than `: No active sessions.`. See the `!~` check in
iscsi_session_list.
> + if (my $err = $@) {
This will not be the case when there are no sessions, but when the
iscsiadm command fails. See above.
> + # no sessions, check portal via tcp
> + my $portals = iscsi_portals($scfg->{target}, $scfg->{portal});
> + for my $portal (@$portals) {
> + my $result = iscsi_test_portal($portal);
> + return $result if $result;
> + }
> + return 0;
> + }
> + # we have sessions, let's test them instead
> + for my $session (@$sessions) {
> + my $result = iscsi_test_session($session->{session_id});
> + return $result if $result;
> }
>
> return 0;
You could do something like this:
```
my $cache = {};
my $sessions = iscsi_session($cache, $scfg->{target});
my $portals = iscsi_portals($scfg->{target}, $scfg->{portal});
for my $portal ($portals->@*) {
my $session_exists = 0;
for my $session ($sessions->@*) {
next if $session->{portal} ne $portal;
$session_exists = 1;
my $result = iscsi_test_session($session->{session_id});
return $result if $result;
}
next if $session_exists;
my $result = iscsi_test_portal($portal);
return $result if $result;
}
```
This goes through all portals, and if a session exists for the portal,
does a session test instead of the tcp_ping.
But if no session exists, it still checks with a tcp_ping to see if we
should login or not.
More information about the pve-devel
mailing list