From df3aad2c5570847895562ff96a725190571f028c Mon Sep 17 00:00:00 2001 From: Jakub Witczak Date: Thu, 13 Mar 2025 13:38:29 +0100 Subject: [PATCH] ssh: reduce log processing for plain connections - avoid unnecessary data processing Origin: https://github.com/erlang/otp/commit/df3aad2c5570847895562ff96a725190571f028c --- lib/ssh/src/ssh.hrl | 8 ++- lib/ssh/src/ssh_acceptor.erl | 68 +++++++++++++++++--------- lib/ssh/src/ssh_connection_handler.erl | 12 +++-- 3 files changed, 61 insertions(+), 27 deletions(-) diff --git a/lib/ssh/src/ssh.hrl b/lib/ssh/src/ssh.hrl index aecce6c1efea..6cd615e43a40 100644 --- a/lib/ssh/src/ssh.hrl +++ b/lib/ssh/src/ssh.hrl @@ -558,5 +558,11 @@ -define(CIRC_BUF_IN_ONCE(VALUE), ((fun(V) -> ?CIRC_BUF_IN(V), V end)(VALUE)) ). - + +-define(SELECT_MSG(__Fun), + (fun() -> + #{level := __Level} = logger:get_primary_config(), + __Fun(__Level) + end)()). + -endif. % SSH_HRL defined diff --git a/lib/ssh/src/ssh_acceptor.erl b/lib/ssh/src/ssh_acceptor.erl index 00587eecbae1..7f131526bdff 100644 --- a/lib/ssh/src/ssh_acceptor.erl +++ b/lib/ssh/src/ssh_acceptor.erl @@ -206,39 +206,63 @@ handle_error(Reason, ToAddress, ToPort, _) -> handle_error(Reason, ToAddress, ToPort, FromAddress, FromPort) -> case Reason of {max_sessions, MaxSessions} -> - error_logger:info_report( - lists:concat(["Ssh login attempt to ",ssh_lib:format_address_port(ToAddress,ToPort), - " from ",ssh_lib:format_address_port(FromAddress,FromPort), - " denied due to option max_sessions limits to ", - MaxSessions, " sessions." - ]) - ); - + MsgFun = + fun(debug) -> + lists:concat(["Ssh login attempt to ", + ssh_lib:format_address_port(ToAddress,ToPort), + " from ", + ssh_lib:format_address_port(FromAddress,FromPort), + " denied due to option max_sessions limits to ", + MaxSessions, " sessions."]); + (_) -> + ["Ssh login attempt denied max_session limits"] + end, + error_logger:info_report(?SELECT_MSG(MsgFun)); Limit when Limit==enfile ; Limit==emfile -> %% Out of sockets... - error_logger:info_report([atom_to_list(Limit),": out of accept sockets on ", - ssh_lib:format_address_port(ToAddress, ToPort), - " - retrying"]), + MsgFun = + fun(debug) -> + [atom_to_list(Limit),": out of accept sockets on ", + ssh_lib:format_address_port(ToAddress, ToPort), + " - retrying"]; + (_) -> + ["Out of accept sockets on - retrying"] + end, + error_logger:info_report(?SELECT_MSG(MsgFun)), timer:sleep(?SLEEP_TIME); - closed -> - error_logger:info_report(["The ssh accept socket on ",ssh_lib:format_address_port(ToAddress,ToPort), - "was closed by a third party."] - ); - + MsgFun = + fun(debug) -> + ["The ssh accept socket on ", ssh_lib:format_address_port(ToAddress,ToPort), + "was closed by a third party."]; + (_) -> + ["The ssh accept socket on was closed by a third party"] + end, + error_logger:info_report(?SELECT_MSG(MsgFun)); timeout -> ok; - Error when is_list(Error) -> ok; Error when FromAddress=/=undefined, FromPort=/=undefined -> - error_logger:info_report(["Accept failed on ",ssh_lib:format_address_port(ToAddress,ToPort), - " for connect from ",ssh_lib:format_address_port(FromAddress,FromPort), - io_lib:format(": ~p", [Error])]); + MsgFun = + fun(debug) -> + ["Accept failed on ",ssh_lib:format_address_port(ToAddress,ToPort), + " for connect from ",ssh_lib:format_address_port(FromAddress,FromPort), + io_lib:format(": ~p", [Error])]; + (_) -> + [io_lib:format("Accept failed on for connection: ~p", [Error])] + end, + error_logger:info_report(?SELECT_MSG(MsgFun)); Error -> - error_logger:info_report(["Accept failed on ",ssh_lib:format_address_port(ToAddress,ToPort), - io_lib:format(": ~p", [Error])]) + MsgFun = + fun(debug) -> + ["Accept failed on ",ssh_lib:format_address_port(ToAddress,ToPort), + io_lib:format(": ~p", [Error])]; + (_) -> + [io_lib:format("Accept failed on for connection: ~p", [Error])] + end, + error_logger:info_report(?SELECT_MSG(MsgFun)) end. %%%---------------------------------------------------------------- diff --git a/lib/ssh/src/ssh_connection_handler.erl b/lib/ssh/src/ssh_connection_handler.erl index 4ef45516ca23..bb62fc9219f5 100644 --- a/lib/ssh/src/ssh_connection_handler.erl +++ b/lib/ssh/src/ssh_connection_handler.erl @@ -648,11 +648,15 @@ handle_event(internal, {version_exchange,Version}, {hello,Role}, D0) -> %%% timeout after tcp:connect but then nothing arrives handle_event(state_timeout, no_hello_received, {hello,_Role}=StateName, D0 = #data{ssh_params = Ssh0}) -> - Time = ?GET_OPT(hello_timeout, Ssh0#ssh.opts), + MsgFun = + fun (debug) -> + Time = ?GET_OPT(hello_timeout, Ssh0#ssh.opts), + lists:concat(["No HELLO received within ",ssh_lib:format_time_ms(Time)]); + (_) -> + ["No HELLO received within hello_timeout"] + end, {Shutdown, D} = - ?send_disconnect(?SSH_DISCONNECT_PROTOCOL_ERROR, - lists:concat(["No HELLO received within ",ssh_lib:format_time_ms(Time)]), - StateName, D0), + ?send_disconnect(?SSH_DISCONNECT_PROTOCOL_ERROR, ?SELECT_MSG(MsgFun), StateName, D0), {stop, Shutdown, D};